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