OSDN Git Service

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