1 ;;; scribe.el --- scribe mode, and its idiosyncratic commands.
3 ;; 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.
26 ;; A major mode for editing source in written for the Scribe text formatter.
27 ;; Knows about Scribe syntax and standard layout rules. The command to
28 ;; run Scribe on a buffer is bogus; someone interested should fix it.
32 (defvar scribe-mode-syntax-table nil
33 "Syntax table used while in scribe mode.")
35 (defvar scribe-mode-abbrev-table nil
36 "Abbrev table used while in scribe mode.")
38 (defvar scribe-fancy-paragraphs nil
39 "*Non-NIL makes Scribe mode use a different style of paragraph separation.")
41 (defvar scribe-electric-quote nil
42 "*Non-NIL makes insert of double quote use `` or '' depending on context.")
44 (defvar scribe-electric-parenthesis nil
45 "*Non-NIL makes parenthesis char ( (]}> ) automatically insert its close
46 if typed after an @Command form.")
48 (defconst scribe-open-parentheses
"[({<"
49 "Open parenthesis characters for Scribe.")
51 (defconst scribe-close-parentheses
"])}>"
52 "Close parenthesis characters for Scribe.
53 These should match up with `scribe-open-parenthesis'.")
55 (if (null scribe-mode-syntax-table
)
56 (let ((st (syntax-table)))
59 (setq scribe-mode-syntax-table
(copy-syntax-table
60 text-mode-syntax-table
))
61 (set-syntax-table scribe-mode-syntax-table
)
62 (modify-syntax-entry ?
\" " ")
63 (modify-syntax-entry ?
\\ " ")
64 (modify-syntax-entry ?
@ "w ")
65 (modify-syntax-entry ?
< "(> ")
66 (modify-syntax-entry ?
> ")< ")
67 (modify-syntax-entry ?
[ "(] ")
68 (modify-syntax-entry ?
] ")[ ")
69 (modify-syntax-entry ?
{ "(} ")
70 (modify-syntax-entry ?
} "){ ")
71 (modify-syntax-entry ?
' "w "))
72 (set-syntax-table st
))))
74 (defvar scribe-mode-map nil
)
78 (setq scribe-mode-map
(make-sparse-keymap))
79 (define-key scribe-mode-map
"\t" 'scribe-tab
)
80 (define-key scribe-mode-map
"\e\t" 'tab-to-tab-stop
)
81 (define-key scribe-mode-map
"\es" 'center-line
)
82 (define-key scribe-mode-map
"\e}" 'up-list
)
83 (define-key scribe-mode-map
"\eS" 'center-paragraph
)
84 (define-key scribe-mode-map
"\"" 'scribe-insert-quote
)
85 (define-key scribe-mode-map
"(" 'scribe-parenthesis
)
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
"\^cc" 'scribe-chapter
)
90 (define-key scribe-mode-map
"\^cS" 'scribe-section
)
91 (define-key scribe-mode-map
"\^cs" 'scribe-subsection
)
92 (define-key scribe-mode-map
"\^ce" 'scribe-insert-environment
)
93 (define-key scribe-mode-map
"\^c\^e" 'scribe-bracket-region-be
)
94 (define-key scribe-mode-map
"\^c[" 'scribe-begin
)
95 (define-key scribe-mode-map
"\^c]" 'scribe-end
)
96 (define-key scribe-mode-map
"\^ci" 'scribe-italicize-word
)
97 (define-key scribe-mode-map
"\^cb" 'scribe-bold-word
)
98 (define-key scribe-mode-map
"\^cu" 'scribe-underline-word
))
101 (defun scribe-mode ()
102 "Major mode for editing files of Scribe (a text formatter) source.
103 Scribe-mode is similar text-mode, with a few extra commands added.
106 Interesting variables:
108 scribe-fancy-paragraphs
109 Non-nil makes Scribe mode use a different style of paragraph separation.
111 scribe-electric-quote
112 Non-nil makes insert of double quote use `` or '' depending on context.
114 scribe-electric-parenthesis
115 Non-nil makes an open-parenthesis char (one of `([<{')
116 automatically insert its close if typed after an @Command form."
118 (kill-all-local-variables)
119 (use-local-map scribe-mode-map
)
120 (setq mode-name
"Scribe")
121 (setq major-mode
'scribe-mode
)
122 (define-abbrev-table 'scribe-mode-abbrev-table
())
123 (setq local-abbrev-table scribe-mode-abbrev-table
)
124 (make-local-variable 'comment-start
)
125 (setq comment-start
"@Comment[")
126 (make-local-variable 'comment-start-skip
)
127 (setq comment-start-skip
(concat "@Comment[" scribe-open-parentheses
"]"))
128 (make-local-variable 'comment-column
)
129 (setq comment-column
0)
130 (make-local-variable 'comment-end
)
131 (setq comment-end
"]")
132 (make-local-variable 'paragraph-start
)
133 (setq paragraph-start
(concat "\\(^[\n\f]\\)\\|\\(^@\\w+["
134 scribe-open-parentheses
136 scribe-close-parentheses
138 (make-local-variable 'paragraph-separate
)
139 (setq paragraph-separate
(if scribe-fancy-paragraphs
140 paragraph-start
"^$"))
141 (make-local-variable 'compile-command
)
142 (setq compile-command
(concat "scribe " (buffer-file-name)))
143 (set-syntax-table scribe-mode-syntax-table
)
144 (run-hooks 'text-mode-hook
'scribe-mode-hook
))
150 ;; This algorithm could probably be improved somewhat.
151 ;; Right now, it loses seriously...
154 "Run Scribe on the current buffer."
156 (call-interactively 'compile
))
158 (defun scribe-envelop-word (string count
)
159 "Surround current word with Scribe construct @STRING[...].
160 COUNT specifies how many words to surround. A negative count means
162 (let ((spos (point)) (epos (point)) (ccoun 0) noparens
)
163 (if (not (zerop count
))
164 (progn (if (= (char-syntax (preceding-char)) ?w
)
165 (forward-sexp (min -
1 count
)))
167 (if (looking-at (concat "@\\w[" scribe-open-parentheses
"]"))
170 (skip-chars-backward "\\W")
172 (forward-sexp (max count
1))
173 (setq epos
(point))))
175 (while (and (< ccoun
(length scribe-open-parentheses
))
177 (or (search-forward (char-to-string
178 (aref scribe-open-parentheses ccoun
))
180 (search-forward (char-to-string
181 (aref scribe-close-parentheses ccoun
))
183 (setq ccoun
(1+ ccoun
))))
184 (if (>= ccoun
(length scribe-open-parentheses
))
185 (progn (goto-char epos
)
186 (insert "@end(" string
")")
188 (insert "@begin(" string
")"))
190 (insert (aref scribe-close-parentheses ccoun
))
192 (insert "@" string
(aref scribe-open-parentheses ccoun
))
195 (skip-chars-forward scribe-close-parentheses
))))
197 (defun scribe-underline-word (count)
198 "Underline COUNT words around point by means of Scribe constructs."
200 (scribe-envelop-word "u" count
))
202 (defun scribe-bold-word (count)
203 "Boldface COUNT words around point by means of Scribe constructs."
205 (scribe-envelop-word "b" count
))
207 (defun scribe-italicize-word (count)
208 "Italicize COUNT words around point by means of Scribe constructs."
210 (scribe-envelop-word "i" count
))
212 (defun scribe-begin ()
216 (scribe-envelop-word "Begin" 0)
217 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
223 (scribe-envelop-word "End" 0)
224 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
226 (defun scribe-chapter ()
230 (scribe-envelop-word "Chapter" 0)
231 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
233 (defun scribe-section ()
237 (scribe-envelop-word "Section" 0)
238 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
240 (defun scribe-subsection ()
244 (scribe-envelop-word "SubSection" 0)
245 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
247 (defun scribe-bracket-region-be (env min max
)
248 (interactive "sEnvironment: \nr")
251 (insert "@end(" env
")\n")
253 (insert "@begin(" env
")\n")))
255 (defun scribe-insert-environment (env)
256 (interactive "sEnvironment: ")
257 (scribe-bracket-region-be env
(point) (point))
262 (defun scribe-insert-quote (count)
263 "Insert ``, '' or \" according to preceding character.
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
)))))))
322 ;;; scribe.el ends here