OSDN Git Service

update URL (http://howm ==> https://howm)
[howm/howm.git] / illusion.el
1 ;;; illusion.el --- load, edit, and submit something which is not pure file
2 ;;; Copyright (C) 2005-2018
3 ;;;   HIRAOKA Kazuyuki <khi@users.osdn.me>
4 ;;; $Id: illusion.el,v 1.14 2011-12-31 15:07:29 hira Exp $
5 ;;;
6 ;;; This program is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 1, or (at your option)
9 ;;; any later version.
10 ;;;
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; The GNU General Public License is available by anonymouse ftp from
17 ;;; prep.ai.mit.edu in pub/gnu/COPYING.  Alternately, you can write to
18 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
19 ;;; USA.
20 ;;;--------------------------------------------------------------------
21
22 ;;; Commentary:
23
24 ;; Not yet. See the example 'yalot13' at the bottom of this file.
25
26 ;;; Code:
27
28 (require 'easy-mmode)
29 (require 'howm-common)
30
31 (defvar illusion-lighter " _i_")
32 (defvar illusion-submit-key "\C-c\C-c")
33
34 (defvar illusion-submit-func
35   (lambda ()
36     (error "Submission function is not defined."))
37   "Value of this variable is called when `illusion-submit' is executed.
38 It must return non-nil value for successful case.")
39 (make-variable-buffer-local 'illusion-submit-func)
40 (put 'illusion-submit-func 'risky-local-variable t)
41
42 (defun illusion-submit ()
43   (interactive)
44   (funcall illusion-submit-func)
45   (set-buffer-modified-p nil))
46
47 (defun illusion-generate (name loader submitter)
48   (switch-to-buffer (generate-new-buffer name))
49   (text-mode)
50   (illusion-mode 1)
51   (setq illusion-submit-func submitter)
52   (funcall loader)
53   (goto-char (point-min))
54   (set-buffer-modified-p nil))
55
56 (easy-mmode-define-minor-mode illusion-mode
57   "With no argument, this command toggles the mode.
58 Non-null prefix argument turns on the mode.
59 Null prefix argument turns off the mode.
60
61 When the mode is enabled, \\[illusion-submit] submits the content
62 with a manner which is suitable to current buffer.
63
64 key     binding
65 ---     -------
66 \\[illusion-submit]     Submit changes
67 "
68   nil ;; default = off
69   illusion-lighter ;; mode-line
70   `(
71     (,illusion-submit-key . illusion-submit)
72     )
73 )
74
75 ;; emacs20's easy-mmode-define-minor-mode can't have body. sigh...
76 (add-hook 'illusion-mode-on-hook
77           (lambda () (use-local-map illusion-mode-map)))
78
79 ;;; Example
80
81 ;; M-x yarot13-find-file to open rot13ed file.
82 ;; Edit it, and C-c C-c to save it.
83
84 ;; (personal note) ruby -pe '$_.tr! "a-zA-Z", "n-za-mN-ZA-M"'
85
86 (defun yarot13-find-file (file)
87   (interactive "Frot13 file: ")
88   (illusion-generate (concat "rot13:" (file-name-nondirectory file))
89                      `(lambda () (yarot13-insert-file-contents ,file))
90                      `(lambda () (yarot13-save-buffer-to ,file))))
91
92 (defun yarot13-insert-file-contents (file)
93   (if (file-exists-p file)
94       (let ((s (with-temp-buffer
95                  (howm-insert-file-contents file)
96                  (yarot13-rotate-buffer)
97                  (buffer-string))))
98         (insert s))
99     (message "(New file)")))
100
101 (defun yarot13-save-buffer-to (file)
102   (let ((s (buffer-string)))
103     (with-temp-buffer
104       (insert s)
105       (yarot13-rotate-buffer)
106       (set-visited-file-name file)
107       (basic-save-buffer))))
108
109 (defun yarot13-rotate-buffer ()
110   (save-excursion
111     (goto-char (point-min))
112     (while (not (eobp))
113       (let ((c (char-after)))
114         (setq c (yarot13-rotate-char c ?a ?z))
115         (setq c (yarot13-rotate-char c ?A ?Z))
116         (delete-char 1)
117         (insert-char c 1)))))
118
119 (defun yarot13-rotate-string (str)
120   (with-temp-buffer
121     (insert str)
122     (yarot13-rotate-buffer)
123     (buffer-string)))
124
125 (defun yarot13-rotate-char (x beg end)
126   (let ((d (- x beg))
127         (w (+ 1 (- end beg))))
128     (if (and (<= beg x) (<= x end))
129         (+ beg (mod (+ d (/ w 2)) w))
130       x)))
131
132 (provide 'illusion)
133
134 ;;; illusion.el ends here