1 ;;; scribe.el --- scribe mode, and its idiosyncratic commands.
4 ;; Copyright (C) 1985 Free Software Foundation, Inc.
6 ;; This file is part of XEmacs.
8 ;; GNU Emacs is distributed in the hope that it will be useful,
9 ;; but without any warranty. No author or distributor
10 ;; accepts responsibility to anyone for the consequences of using it
11 ;; or for whether it serves any particular purpose or works at all,
12 ;; unless he says so in writing.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; document "GNU Emacs copying permission notice". An exact copy
17 ;; of the document is supposed to have been given to you along with
18 ;; GNU Emacs so that you can know how you may redistribute it all.
19 ;; It should be in a file named COPYING. Among other things, the
20 ;; copyright notice and this notice must be preserved on all copies.
23 "Scribe mode, and its idiosyncratic commands."
27 (defvar scribe-mode-syntax-table nil
28 "Syntax table used while in scribe mode.")
30 (defvar scribe-mode-abbrev-table nil
31 "Abbrev table used while in scribe mode.")
33 (defcustom scribe-fancy-paragraphs nil
34 "*Non-NIL makes Scribe mode use a different style of paragraph separation."
38 (defcustom scribe-electric-quote nil
39 "*Non-NIL makes insert of double quote use `` or '' depending on context."
43 (defcustom scribe-electric-parenthesis nil
44 "*Non-NIL makes parenthesis char ( (]}> ) automatically insert its close
45 if typed after an @Command form."
49 (defconst scribe-open-parentheses "[({<"
50 "Open parenthesis characters for Scribe.")
52 (defconst scribe-close-parentheses "])}>"
53 "Close parenthesis characters for Scribe. These should match up with
54 scribe-open-parenthesis.")
56 (if (null scribe-mode-syntax-table)
57 (let ((st (syntax-table)))
60 (setq scribe-mode-syntax-table (copy-syntax-table
61 text-mode-syntax-table))
62 (set-syntax-table scribe-mode-syntax-table)
63 (modify-syntax-entry ?\" " ")
64 (modify-syntax-entry ?\\ " ")
65 (modify-syntax-entry ?@ "w ")
66 (modify-syntax-entry ?< "(> ")
67 (modify-syntax-entry ?> ")< ")
68 (modify-syntax-entry ?[ "(] ")
69 (modify-syntax-entry ?] ")[ ")
70 (modify-syntax-entry ?{ "(} ")
71 (modify-syntax-entry ?} "){ ")
72 (modify-syntax-entry ?' "w "))
73 (set-syntax-table st))))
75 (defvar scribe-mode-map nil)
79 (setq scribe-mode-map (make-sparse-keymap))
80 (define-key scribe-mode-map "\t" 'scribe-tab)
81 (define-key scribe-mode-map "\e\t" 'tab-to-tab-stop)
82 (define-key scribe-mode-map "\es" 'center-line)
83 (define-key scribe-mode-map "\e}" 'up-list)
84 (define-key scribe-mode-map "\eS" 'center-paragraph)
85 (define-key scribe-mode-map "\"" 'scribe-insert-quote)
86 (define-key scribe-mode-map "(" 'scribe-parenthesis)
87 (define-key scribe-mode-map "[" 'scribe-parenthesis)
88 (define-key scribe-mode-map "{" 'scribe-parenthesis)
89 (define-key scribe-mode-map "<" 'scribe-parenthesis)
90 (define-key scribe-mode-map "\^cc" 'scribe-chapter)
91 (define-key scribe-mode-map "\^cS" 'scribe-section)
92 (define-key scribe-mode-map "\^cs" 'scribe-subsection)
93 (define-key scribe-mode-map "\^ce" 'scribe-insert-environment)
94 (define-key scribe-mode-map "\^c\^e" 'scribe-bracket-region-be)
95 (define-key scribe-mode-map "\^c[" 'scribe-begin)
96 (define-key scribe-mode-map "\^c]" 'scribe-end)
97 (define-key scribe-mode-map "\^ci" 'scribe-italicize-word)
98 (define-key scribe-mode-map "\^cb" 'scribe-bold-word)
99 (define-key scribe-mode-map "\^cu" 'scribe-underline-word))
102 (defun scribe-mode ()
103 "Major mode for editing files of Scribe (a text formatter) source.
104 Scribe-mode is similar text-mode, with a few extra commands added.
107 Interesting variables:
109 scribe-fancy-paragraphs
110 Non-nil makes Scribe mode use a different style of paragraph separation.
112 scribe-electric-quote
113 Non-nil makes insert of double quote use `` or '' depending on context.
115 scribe-electric-parenthesis
116 Non-nil makes an open-parenthesis char (one of `([<{')
117 automatically insert its close if typed after an @Command form."
119 (kill-all-local-variables)
120 (use-local-map scribe-mode-map)
121 (setq mode-name "Scribe")
122 (setq major-mode 'scribe-mode)
123 (define-abbrev-table 'scribe-mode-abbrev-table ())
124 (setq local-abbrev-table scribe-mode-abbrev-table)
125 (make-local-variable 'comment-start)
126 (setq comment-start "@Comment[")
127 (make-local-variable 'comment-start-skip)
128 (setq comment-start-skip (concat "@Comment[" scribe-open-parentheses "]"))
129 (make-local-variable 'comment-column)
130 (setq comment-column 0)
131 (make-local-variable 'comment-end)
132 (setq comment-end "]")
133 (make-local-variable 'paragraph-start)
134 (setq paragraph-start (concat "\\(^[\n\f]\\)\\|\\(^@\\w+["
135 scribe-open-parentheses
137 scribe-close-parentheses
139 (make-local-variable 'paragraph-separate)
140 (setq paragraph-separate (if scribe-fancy-paragraphs
141 paragraph-start "^$"))
142 (make-local-variable 'compile-command)
143 (setq compile-command (concat "scribe " (buffer-file-name)))
144 (set-syntax-table scribe-mode-syntax-table)
145 (run-hooks 'text-mode-hook 'scribe-mode-hook))
151 ;; This algorithm could probably be improved somewhat.
152 ;; Right now, it loses seriously...
155 "Run Scribe on the current buffer."
157 (call-interactively 'compile))
159 (defun scribe-envelop-word (string count)
160 "Surround current word with Scribe construct @STRING[...]. COUNT
161 specifies how many words to surround. A negative count means to skip
163 (let ((spos (point)) (epos (point)) (ccoun 0))
164 (if (not (zerop count))
165 (progn (if (= (char-syntax (preceding-char)) ?w)
166 (forward-sexp (min -1 count)))
168 (if (looking-at (concat "@\\w[" scribe-open-parentheses "]"))
171 (skip-chars-backward "\\W")
173 (forward-sexp (max count 1))
174 (setq epos (point))))
176 (while (and (< ccoun (length scribe-open-parentheses))
178 (or (search-forward (char-to-string
179 (aref scribe-open-parentheses ccoun))
181 (search-forward (char-to-string
182 (aref scribe-close-parentheses ccoun))
184 (setq ccoun (1+ ccoun))))
185 (if (>= ccoun (length scribe-open-parentheses))
186 (progn (goto-char epos)
187 (insert "@end(" string ")")
189 (insert "@begin(" string ")"))
191 (insert (aref scribe-close-parentheses ccoun))
193 (insert "@" string (aref scribe-open-parentheses ccoun))
196 (skip-chars-forward scribe-close-parentheses))))
198 (defun scribe-underline-word (count)
199 "Underline COUNT words around point by means of Scribe constructs."
201 (scribe-envelop-word "u" count))
203 (defun scribe-bold-word (count)
204 "Boldface COUNT words around point by means of Scribe constructs."
206 (scribe-envelop-word "b" count))
208 (defun scribe-italicize-word (count)
209 "Italicize COUNT words around point by means of Scribe constructs."
211 (scribe-envelop-word "i" count))
213 (defun scribe-begin ()
217 (scribe-envelop-word "Begin" 0)
218 (re-search-forward (concat "[" scribe-open-parentheses "]")))
224 (scribe-envelop-word "End" 0)
225 (re-search-forward (concat "[" scribe-open-parentheses "]")))
227 (defun scribe-chapter ()
231 (scribe-envelop-word "Chapter" 0)
232 (re-search-forward (concat "[" scribe-open-parentheses "]")))
234 (defun scribe-section ()
238 (scribe-envelop-word "Section" 0)
239 (re-search-forward (concat "[" scribe-open-parentheses "]")))
241 (defun scribe-subsection ()
245 (scribe-envelop-word "SubSection" 0)
246 (re-search-forward (concat "[" scribe-open-parentheses "]")))
248 (defun scribe-bracket-region-be (env min max)
249 (interactive "sEnvironment: \nr")
252 (insert "@end(" env ")\n")
254 (insert "@begin(" env ")\n")))
256 (defun scribe-insert-environment (env)
257 (interactive "sEnvironment: ")
258 (scribe-bracket-region-be env (point) (point))
263 (defun scribe-insert-quote (count)
264 "If scribe-electric-quote is non-NIL, insert ``, '' or \" according
265 to preceding character. With numeric arg N, always insert N \" characters.
266 Else just insert \"."
268 (if (or count (not scribe-electric-quote))
269 (self-insert-command (prefix-numeric-value count))
270 (let (lastfore lastback lastquote)
273 ((= (preceding-char) ?\\) ?\")
276 (setq lastfore (save-excursion (and (search-backward
277 "``" (- (point) 1000) t)
279 lastback (save-excursion (and (search-backward
280 "''" (- (point) 1000) t)
282 lastquote (save-excursion (and (search-backward
283 "\"" (- (point) 100) t)
286 (cond ((not lastfore) "``")
287 ((not lastback) "''")
288 ((> lastfore lastback) "''")
290 (cond ((and (not lastback) (not lastfore)) "\"")
291 ((and lastback (not lastfore) (> lastquote lastback)) "\"")
292 ((and lastback (not lastfore) (> lastback lastquote)) "``")
293 ((and lastfore (not lastback) (> lastquote lastfore)) "\"")
294 ((and lastfore (not lastback) (> lastfore lastquote)) "''")
295 ((and (> lastquote lastfore) (> lastquote lastback)) "\"")
296 ((> lastfore lastback) "''")
299 (defun scribe-parenthesis (count)
300 "If scribe-electric-parenthesis is non-NIL, insertion of an open-parenthesis
301 character inserts the following close parenthesis character if the
302 preceding text is of the form @Command."
304 (self-insert-command (prefix-numeric-value count))
305 (let (at-command paren-char point-save)
306 (if (or count (not scribe-electric-parenthesis))
310 (setq point-save (point))
311 (skip-chars-backward (concat "^ \n\t\f" scribe-open-parentheses))
312 (setq at-command (and (equal (following-char) ?@)
313 (/= (point) (1- point-save)))))
316 (string-match (regexp-quote
317 (char-to-string (preceding-char)))
318 scribe-open-parentheses)))
320 (insert (aref scribe-close-parentheses paren-char)))))))
323 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.mss\\'" . scribe-mode))