7
|
1 ;; scribe mode, and its ideosyncratic commands.
|
|
2 ;; Copyright (C) 1985 Free Software Foundation, Inc.
|
|
3
|
|
4 ;; This file might become part of GNU Emacs.
|
|
5
|
|
6 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
7 ;; but without any warranty. No author or distributor
|
|
8 ;; accepts responsibility to anyone for the consequences of using it
|
|
9 ;; or for whether it serves any particular purpose or works at all,
|
|
10 ;; unless he says so in writing.
|
|
11
|
|
12 ;; Everyone is granted permission to copy, modify and redistribute
|
|
13 ;; GNU Emacs, but only under the conditions described in the
|
|
14 ;; document "GNU Emacs copying permission notice". An exact copy
|
|
15 ;; of the document is supposed to have been given to you along with
|
|
16 ;; GNU Emacs so that you can know how you may redistribute it all.
|
|
17 ;; It should be in a file named COPYING. Among other things, the
|
|
18 ;; copyright notice and this notice must be preserved on all copies.
|
|
19
|
|
20
|
|
21 (defvar scribe-mode-syntax-table nil
|
|
22 "Syntax table used while in scribe mode.")
|
|
23
|
|
24 (defvar scribe-mode-abbrev-table nil
|
|
25 "Abbrev table used while in scribe mode.")
|
|
26
|
|
27 (defvar scribe-fancy-paragraphs nil
|
|
28 "*Non-NIL makes Scribe mode use a different style of paragraph separation.")
|
|
29
|
|
30 (defvar scribe-electric-quote nil
|
|
31 "*Non-NIL makes insert of double quote use `` or '' depending on context.")
|
|
32
|
|
33 (defvar scribe-electric-parenthesis nil
|
|
34 "*Non-NIL makes parenthesis char ( (]}> ) automatically insert its close
|
|
35 if typed after an @Command form.")
|
|
36
|
|
37 (defconst scribe-open-parentheses "[({<"
|
|
38 "Open parenthesis characters for Scribe.")
|
|
39
|
|
40 (defconst scribe-close-parentheses "])}>"
|
|
41 "Close parenthesis characters for Scribe. These should match up with
|
|
42 scribe-open-parenthesis.")
|
|
43
|
|
44 (if (null scribe-mode-syntax-table)
|
|
45 (let ((st (syntax-table)))
|
|
46 (unwind-protect
|
|
47 (progn
|
|
48 (setq scribe-mode-syntax-table (copy-syntax-table
|
|
49 text-mode-syntax-table))
|
|
50 (set-syntax-table scribe-mode-syntax-table)
|
|
51 (modify-syntax-entry ?\" " ")
|
|
52 (modify-syntax-entry ?\\ " ")
|
|
53 (modify-syntax-entry ?@ "w ")
|
|
54 (modify-syntax-entry ?< "(> ")
|
|
55 (modify-syntax-entry ?> ")< ")
|
|
56 (modify-syntax-entry ?[ "(] ")
|
|
57 (modify-syntax-entry ?] ")[ ")
|
|
58 (modify-syntax-entry ?{ "(} ")
|
|
59 (modify-syntax-entry ?} "){ ")
|
|
60 (modify-syntax-entry ?' "w "))
|
|
61 (set-syntax-table st))))
|
|
62
|
|
63 (defvar scribe-mode-map nil)
|
|
64
|
|
65 (if scribe-mode-map
|
|
66 nil
|
|
67 (setq scribe-mode-map (make-sparse-keymap))
|
|
68 (define-key scribe-mode-map "\t" 'scribe-tab)
|
|
69 (define-key scribe-mode-map "\e\t" 'tab-to-tab-stop)
|
|
70 (define-key scribe-mode-map "\es" 'center-line)
|
|
71 (define-key scribe-mode-map "\e}" 'up-list)
|
|
72 (define-key scribe-mode-map "\eS" 'center-paragraph)
|
|
73 (define-key scribe-mode-map "\"" 'scribe-insert-quote)
|
|
74 (define-key scribe-mode-map "(" 'scribe-parenthesis)
|
|
75 (define-key scribe-mode-map "[" 'scribe-parenthesis)
|
|
76 (define-key scribe-mode-map "{" 'scribe-parenthesis)
|
|
77 (define-key scribe-mode-map "<" 'scribe-parenthesis)
|
|
78 (define-key scribe-mode-map "\^cc" 'scribe-chapter)
|
|
79 (define-key scribe-mode-map "\^cS" 'scribe-section)
|
|
80 (define-key scribe-mode-map "\^cs" 'scribe-subsection)
|
|
81 (define-key scribe-mode-map "\^ce" 'scribe-insert-environment)
|
|
82 (define-key scribe-mode-map "\^c\^e" 'scribe-bracket-region-be)
|
|
83 (define-key scribe-mode-map "\^c[" 'scribe-begin)
|
|
84 (define-key scribe-mode-map "\^c]" 'scribe-end)
|
|
85 (define-key scribe-mode-map "\^ci" 'scribe-italicize-word)
|
|
86 (define-key scribe-mode-map "\^cb" 'scribe-bold-word)
|
|
87 (define-key scribe-mode-map "\^cu" 'scribe-underline-word))
|
|
88
|
258
|
89 ;;;###autoload
|
7
|
90 (defun scribe-mode ()
|
|
91 "Major mode for editing files of Scribe (a text formatter) source.
|
|
92 Scribe-mode is similar text-mode, with a few extra commands added.
|
|
93 \\{scribe-mode-map}
|
|
94
|
|
95 Interesting variables:
|
|
96
|
|
97 scribe-fancy-paragraphs
|
|
98 Non-nil makes Scribe mode use a different style of paragraph separation.
|
|
99
|
|
100 scribe-electric-quote
|
|
101 Non-nil makes insert of double quote use `` or '' depending on context.
|
|
102
|
|
103 scribe-electric-parenthesis
|
|
104 Non-nil makes an open-parenthesis char (one of `([<{')
|
|
105 automatically insert its close if typed after an @Command form."
|
|
106 (interactive)
|
|
107 (kill-all-local-variables)
|
|
108 (use-local-map scribe-mode-map)
|
|
109 (setq mode-name "Scribe")
|
|
110 (setq major-mode 'scribe-mode)
|
|
111 (define-abbrev-table 'scribe-mode-abbrev-table ())
|
|
112 (setq local-abbrev-table scribe-mode-abbrev-table)
|
|
113 (make-local-variable 'comment-start)
|
|
114 (setq comment-start "@Comment[")
|
|
115 (make-local-variable 'comment-start-skip)
|
|
116 (setq comment-start-skip (concat "@Comment[" scribe-open-parentheses "]"))
|
|
117 (make-local-variable 'comment-column)
|
|
118 (setq comment-column 0)
|
|
119 (make-local-variable 'comment-end)
|
|
120 (setq comment-end "]")
|
|
121 (make-local-variable 'paragraph-start)
|
|
122 (setq paragraph-start (concat "\\(^[\n\f]\\)\\|\\(^@\\w+["
|
|
123 scribe-open-parentheses
|
|
124 "].*["
|
|
125 scribe-close-parentheses
|
|
126 "]$\\)"))
|
|
127 (make-local-variable 'paragraph-separate)
|
|
128 (setq paragraph-separate (if scribe-fancy-paragraphs
|
|
129 paragraph-start "^$"))
|
|
130 (make-local-variable 'compile-command)
|
|
131 (setq compile-command (concat "scribe " (buffer-file-name)))
|
|
132 (set-syntax-table scribe-mode-syntax-table)
|
|
133 (run-hooks 'text-mode-hook 'scribe-mode-hook))
|
|
134
|
|
135 (defun scribe-tab ()
|
|
136 (interactive)
|
|
137 (insert "@\\"))
|
|
138
|
|
139 ;; This algorithm could probably be improved somewhat.
|
|
140 ;; Right now, it loses seriously...
|
|
141
|
|
142 (defun scribe ()
|
|
143 "Run Scribe on the current buffer."
|
|
144 (interactive)
|
|
145 (call-interactively 'compile))
|
|
146
|
|
147 (defun scribe-envelop-word (string count)
|
|
148 "Surround current word with Scribe construct @STRING[...]. COUNT
|
|
149 specifies how many words to surround. A negative count means to skip
|
|
150 backward."
|
|
151 (let ((spos (point)) (epos (point)) (ccoun 0) noparens)
|
|
152 (if (not (zerop count))
|
|
153 (progn (if (= (char-syntax (preceding-char)) ?w)
|
|
154 (forward-sexp (min -1 count)))
|
|
155 (setq spos (point))
|
|
156 (if (looking-at (concat "@\\w[" scribe-open-parentheses "]"))
|
|
157 (forward-char 2)
|
|
158 (goto-char epos)
|
|
159 (skip-chars-backward "\\W")
|
|
160 (forward-char -1))
|
|
161 (forward-sexp (max count 1))
|
|
162 (setq epos (point))))
|
|
163 (goto-char spos)
|
|
164 (while (and (< ccoun (length scribe-open-parentheses))
|
|
165 (save-excursion
|
|
166 (or (search-forward (char-to-string
|
|
167 (aref scribe-open-parentheses ccoun))
|
|
168 epos t)
|
|
169 (search-forward (char-to-string
|
|
170 (aref scribe-close-parentheses ccoun))
|
|
171 epos t)))
|
|
172 (setq ccoun (1+ ccoun))))
|
|
173 (if (>= ccoun (length scribe-open-parentheses))
|
|
174 (progn (goto-char epos)
|
|
175 (insert "@end(" string ")")
|
|
176 (goto-char spos)
|
|
177 (insert "@begin(" string ")"))
|
|
178 (goto-char epos)
|
|
179 (insert (aref scribe-close-parentheses ccoun))
|
|
180 (goto-char spos)
|
|
181 (insert "@" string (aref scribe-open-parentheses ccoun))
|
|
182 (goto-char epos)
|
|
183 (forward-char 3)
|
|
184 (skip-chars-forward scribe-close-parentheses))))
|
|
185
|
|
186 (defun scribe-underline-word (count)
|
|
187 "Underline COUNT words around point by means of Scribe constructs."
|
|
188 (interactive "p")
|
|
189 (scribe-envelop-word "u" count))
|
|
190
|
|
191 (defun scribe-bold-word (count)
|
|
192 "Boldface COUNT words around point by means of Scribe constructs."
|
|
193 (interactive "p")
|
|
194 (scribe-envelop-word "b" count))
|
|
195
|
|
196 (defun scribe-italicize-word (count)
|
|
197 "Italicize COUNT words around point by means of Scribe constructs."
|
|
198 (interactive "p")
|
|
199 (scribe-envelop-word "i" count))
|
|
200
|
|
201 (defun scribe-begin ()
|
|
202 (interactive)
|
|
203 (insert "\n")
|
|
204 (forward-char -1)
|
|
205 (scribe-envelop-word "Begin" 0)
|
|
206 (re-search-forward (concat "[" scribe-open-parentheses "]")))
|
|
207
|
|
208 (defun scribe-end ()
|
|
209 (interactive)
|
|
210 (insert "\n")
|
|
211 (forward-char -1)
|
|
212 (scribe-envelop-word "End" 0)
|
|
213 (re-search-forward (concat "[" scribe-open-parentheses "]")))
|
|
214
|
|
215 (defun scribe-chapter ()
|
|
216 (interactive)
|
|
217 (insert "\n")
|
|
218 (forward-char -1)
|
|
219 (scribe-envelop-word "Chapter" 0)
|
|
220 (re-search-forward (concat "[" scribe-open-parentheses "]")))
|
|
221
|
|
222 (defun scribe-section ()
|
|
223 (interactive)
|
|
224 (insert "\n")
|
|
225 (forward-char -1)
|
|
226 (scribe-envelop-word "Section" 0)
|
|
227 (re-search-forward (concat "[" scribe-open-parentheses "]")))
|
|
228
|
|
229 (defun scribe-subsection ()
|
|
230 (interactive)
|
|
231 (insert "\n")
|
|
232 (forward-char -1)
|
|
233 (scribe-envelop-word "SubSection" 0)
|
|
234 (re-search-forward (concat "[" scribe-open-parentheses "]")))
|
|
235
|
|
236 (defun scribe-bracket-region-be (env min max)
|
|
237 (interactive "sEnvironment: \nr")
|
|
238 (save-excursion
|
|
239 (goto-char max)
|
|
240 (insert "@end(" env ")\n")
|
|
241 (goto-char min)
|
|
242 (insert "@begin(" env ")\n")))
|
|
243
|
|
244 (defun scribe-insert-environment (env)
|
|
245 (interactive "sEnvironment: ")
|
|
246 (scribe-bracket-region-be env (point) (point))
|
|
247 (forward-line 1)
|
|
248 (insert ?\n)
|
|
249 (forward-char -1))
|
|
250
|
|
251 (defun scribe-insert-quote (count)
|
|
252 "If scribe-electric-quote is non-NIL, insert ``, '' or \" according
|
|
253 to preceding character. With numeric arg N, always insert N \" characters.
|
|
254 Else just insert \"."
|
|
255 (interactive "P")
|
|
256 (if (or count (not scribe-electric-quote))
|
|
257 (self-insert-command (prefix-numeric-value count))
|
|
258 (let (lastfore lastback lastquote)
|
|
259 (insert
|
|
260 (cond
|
|
261 ((= (preceding-char) ?\\) ?\")
|
|
262 ((bobp) "``")
|
|
263 (t
|
|
264 (setq lastfore (save-excursion (and (search-backward
|
|
265 "``" (- (point) 1000) t)
|
|
266 (point)))
|
|
267 lastback (save-excursion (and (search-backward
|
|
268 "''" (- (point) 1000) t)
|
|
269 (point)))
|
|
270 lastquote (save-excursion (and (search-backward
|
|
271 "\"" (- (point) 100) t)
|
|
272 (point))))
|
|
273 (if (not lastquote)
|
|
274 (cond ((not lastfore) "``")
|
|
275 ((not lastback) "''")
|
|
276 ((> lastfore lastback) "''")
|
|
277 (t "``"))
|
|
278 (cond ((and (not lastback) (not lastfore)) "\"")
|
|
279 ((and lastback (not lastfore) (> lastquote lastback)) "\"")
|
|
280 ((and lastback (not lastfore) (> lastback lastquote)) "``")
|
|
281 ((and lastfore (not lastback) (> lastquote lastfore)) "\"")
|
|
282 ((and lastfore (not lastback) (> lastfore lastquote)) "''")
|
|
283 ((and (> lastquote lastfore) (> lastquote lastback)) "\"")
|
|
284 ((> lastfore lastback) "''")
|
|
285 (t "``")))))))))
|
|
286
|
|
287 (defun scribe-parenthesis (count)
|
|
288 "If scribe-electric-parenthesis is non-NIL, insertion of an open-parenthesis
|
|
289 character inserts the following close parenthesis character if the
|
|
290 preceding text is of the form @Command."
|
|
291 (interactive "P")
|
|
292 (self-insert-command (prefix-numeric-value count))
|
|
293 (let (at-command paren-char point-save)
|
|
294 (if (or count (not scribe-electric-parenthesis))
|
|
295 nil
|
|
296 (save-excursion
|
|
297 (forward-char -1)
|
|
298 (setq point-save (point))
|
|
299 (skip-chars-backward (concat "^ \n\t\f" scribe-open-parentheses))
|
|
300 (setq at-command (and (equal (following-char) ?@)
|
|
301 (/= (point) (1- point-save)))))
|
|
302 (if (and at-command
|
|
303 (setq paren-char
|
|
304 (string-match (regexp-quote
|
|
305 (char-to-string (preceding-char)))
|
|
306 scribe-open-parentheses)))
|
|
307 (save-excursion
|
|
308 (insert (aref scribe-close-parentheses paren-char)))))))
|