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>
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)
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.
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,
19 ;;;--------------------------------------------------------------------
23 ;; Not yet. See the example 'yalot13' at the bottom of this file.
28 (require 'howm-common)
30 (defvar illusion-lighter " _i_")
31 (defvar illusion-submit-key "\C-c\C-c")
33 (defvar illusion-submit-func
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)
41 (defun illusion-submit ()
43 (funcall illusion-submit-func)
44 (set-buffer-modified-p nil))
46 (defun illusion-generate (name loader submitter)
47 (switch-to-buffer (generate-new-buffer name))
50 (setq illusion-submit-func submitter)
52 (goto-char (point-min))
53 (set-buffer-modified-p nil))
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.
60 When the mode is enabled, \\[illusion-submit] submits the content
61 with a manner which is suitable to current buffer.
65 \\[illusion-submit] Submit changes
68 illusion-lighter ;; mode-line
70 (,illusion-submit-key . illusion-submit)
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)))
80 ;; M-x yarot13-find-file to open rot13ed file.
81 ;; Edit it, and C-c C-c to save it.
83 ;; (personal note) ruby -pe '$_.tr! "a-zA-Z", "n-za-mN-ZA-M"'
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))))
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)
98 (message "(New file)")))
100 (defun yarot13-save-buffer-to (file)
101 (let ((s (buffer-string)))
104 (yarot13-rotate-buffer)
105 (set-visited-file-name file)
106 (basic-save-buffer))))
108 (defun yarot13-rotate-buffer ()
110 (goto-char (point-min))
112 (let ((c (char-after)))
113 (setq c (yarot13-rotate-char c ?a ?z))
114 (setq c (yarot13-rotate-char c ?A ?Z))
116 (insert-char c 1)))))
118 (defun yarot13-rotate-string (str)
121 (yarot13-rotate-buffer)
124 (defun yarot13-rotate-char (x beg end)
126 (w (+ 1 (- end beg))))
127 (if (and (<= beg x) (<= x end))
128 (+ beg (mod (+ d (/ w 2)) w))
133 ;;; illusion.el ends here