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