File: illusion.el

package info (click to toggle)
howm 1.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,280 kB
  • sloc: lisp: 8,394; sh: 3,558; ruby: 996; makefile: 98; python: 47
file content (128 lines) | stat: -rw-r--r-- 3,836 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
;;; -*- lexical-binding: nil; -*-
;;; illusion.el --- load, edit, and submit something which is not pure file
;;; Copyright (C) 2005-2025
;;;   HIRAOKA Kazuyuki <kakkokakko@gmail.com>
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 1, or (at your option)
;;; any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; The GNU General Public License is available by anonymouse ftp from
;;; prep.ai.mit.edu in pub/gnu/COPYING.  Alternately, you can write to
;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
;;; USA.
;;;--------------------------------------------------------------------

;;; Commentary:

;; Not yet. See the example 'yalot13' at the bottom of this file.

;;; Code:

(require 'easy-mmode)
(require 'howm-common)

(defvar illusion-lighter " _i_")
(defvar illusion-submit-key "\C-c\C-c")

(defvar illusion-submit-func
  (lambda ()
    (error "Submission function is not defined."))
  "Value of this variable is called when `illusion-submit' is executed.
It must return non-nil value for successful case.")
(make-variable-buffer-local 'illusion-submit-func)
(put 'illusion-submit-func 'risky-local-variable t)

(defun illusion-submit ()
  (interactive)
  (funcall illusion-submit-func)
  (set-buffer-modified-p nil))

(defun illusion-generate (name loader submitter)
  (switch-to-buffer (generate-new-buffer name))
  (text-mode)
  (illusion-mode 1)
  (setq illusion-submit-func submitter)
  (funcall loader)
  (goto-char (point-min))
  (set-buffer-modified-p nil))

(define-minor-mode illusion-mode
  "Toggle Illusion mode.

When the mode is enabled, \\<illusion-mode-map>\\[illusion-submit] submits the content
with a manner which is suitable to current buffer.

key	binding
---	-------
\\[illusion-submit]	Submit changes
"
  :init-value nil ;; default = off
  :lighter illusion-lighter ;; mode-line
  :keymap `(
            (,illusion-submit-key . illusion-submit)
            )
)

;;; Example

;; M-x yarot13-find-file to open rot13ed file.
;; Edit it, and C-c C-c to save it.

;; (personal note) ruby -pe '$_.tr! "a-zA-Z", "n-za-mN-ZA-M"'

(defun yarot13-find-file (file)
  (interactive "Frot13 file: ")
  (illusion-generate (concat "rot13:" (file-name-nondirectory file))
                     `(lambda () (yarot13-insert-file-contents ,file))
                     `(lambda () (yarot13-save-buffer-to ,file))))

(defun yarot13-insert-file-contents (file)
  (if (file-exists-p file)
      (let ((s (with-temp-buffer
                 (howm-insert-file-contents file)
                 (yarot13-rotate-buffer)
                 (buffer-string))))
        (insert s))
    (message "(New file)")))

(defun yarot13-save-buffer-to (file)
  (let ((s (buffer-string)))
    (with-temp-buffer
      (insert s)
      (yarot13-rotate-buffer)
      (set-visited-file-name file)
      (basic-save-buffer))))

(defun yarot13-rotate-buffer ()
  (save-excursion
    (goto-char (point-min))
    (while (not (eobp))
      (let ((c (char-after)))
        (setq c (yarot13-rotate-char c ?a ?z))
        (setq c (yarot13-rotate-char c ?A ?Z))
        (delete-char 1)
        (insert-char c 1)))))

(defun yarot13-rotate-string (str)
  (with-temp-buffer
    (insert str)
    (yarot13-rotate-buffer)
    (buffer-string)))

(defun yarot13-rotate-char (x beg end)
  (let ((d (- x beg))
        (w (+ 1 (- end beg))))
    (if (and (<= beg x) (<= x end))
        (+ beg (mod (+ d (/ w 2)) w))
      x)))

(provide 'illusion)

;;; illusion.el ends here