1 ;;; illusion.el --- load, edit, and submit something which is not pure file
2 ;;; Copyright (C) 2005-2021
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 (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
67 :init-value nil ;; default = off
68 :lighter illusion-lighter ;; mode-line
70 (,illusion-submit-key . illusion-submit)
72 (use-local-map illusion-mode-map)
77 ;; M-x yarot13-find-file to open rot13ed file.
78 ;; Edit it, and C-c C-c to save it.
80 ;; (personal note) ruby -pe '$_.tr! "a-zA-Z", "n-za-mN-ZA-M"'
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))))
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)
95 (message "(New file)")))
97 (defun yarot13-save-buffer-to (file)
98 (let ((s (buffer-string)))
101 (yarot13-rotate-buffer)
102 (set-visited-file-name file)
103 (basic-save-buffer))))
105 (defun yarot13-rotate-buffer ()
107 (goto-char (point-min))
109 (let ((c (char-after)))
110 (setq c (yarot13-rotate-char c ?a ?z))
111 (setq c (yarot13-rotate-char c ?A ?Z))
113 (insert-char c 1)))))
115 (defun yarot13-rotate-string (str)
118 (yarot13-rotate-buffer)
121 (defun yarot13-rotate-char (x beg end)
123 (w (+ 1 (- end beg))))
124 (if (and (<= beg x) (<= x end))
125 (+ beg (mod (+ d (/ w 2)) w))
130 ;;; illusion.el ends here