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