OSDN Git Service

set version as 1.4.8-snapshot3
[howm/howm.git] / illusion.el
1 ;;; illusion.el --- load, edit, and submit something which is not pure file
2 ;;; Copyright (C) 2005-2020
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 (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   :init-value nil ;; default = off
68   :lighter illusion-lighter ;; mode-line
69   :keymap `(
70             (,illusion-submit-key . illusion-submit)
71             )
72   (use-local-map illusion-mode-map)
73 )
74
75 ;;; Example
76
77 ;; M-x yarot13-find-file to open rot13ed file.
78 ;; Edit it, and C-c C-c to save it.
79
80 ;; (personal note) ruby -pe '$_.tr! "a-zA-Z", "n-za-mN-ZA-M"'
81
82 (defun yarot13-find-file (file)
83   (interactive "Frot13 file: ")
84   (illusion-generate (concat "rot13:" (file-name-nondirectory file))
85                      `(lambda () (yarot13-insert-file-contents ,file))
86                      `(lambda () (yarot13-save-buffer-to ,file))))
87
88 (defun yarot13-insert-file-contents (file)
89   (if (file-exists-p file)
90       (let ((s (with-temp-buffer
91                  (howm-insert-file-contents file)
92                  (yarot13-rotate-buffer)
93                  (buffer-string))))
94         (insert s))
95     (message "(New file)")))
96
97 (defun yarot13-save-buffer-to (file)
98   (let ((s (buffer-string)))
99     (with-temp-buffer
100       (insert s)
101       (yarot13-rotate-buffer)
102       (set-visited-file-name file)
103       (basic-save-buffer))))
104
105 (defun yarot13-rotate-buffer ()
106   (save-excursion
107     (goto-char (point-min))
108     (while (not (eobp))
109       (let ((c (char-after)))
110         (setq c (yarot13-rotate-char c ?a ?z))
111         (setq c (yarot13-rotate-char c ?A ?Z))
112         (delete-char 1)
113         (insert-char c 1)))))
114
115 (defun yarot13-rotate-string (str)
116   (with-temp-buffer
117     (insert str)
118     (yarot13-rotate-buffer)
119     (buffer-string)))
120
121 (defun yarot13-rotate-char (x beg end)
122   (let ((d (- x beg))
123         (w (+ 1 (- end beg))))
124     (if (and (<= beg x) (<= x end))
125         (+ beg (mod (+ d (/ w 2)) w))
126       x)))
127
128 (provide 'illusion)
129
130 ;;; illusion.el ends here