1 ;;; scribe.el --- scribe mode, and its idiosyncratic commands.
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.
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)))
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
)
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
))
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.
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."
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
130 scribe-close-parentheses
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
))
144 ;; This algorithm could probably be improved somewhat.
145 ;; Right now, it loses seriously...
148 "Run Scribe on the current buffer."
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
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
)))
161 (if (looking-at (concat "@\\w[" scribe-open-parentheses
"]"))
164 (skip-chars-backward "\\W")
166 (forward-sexp (max count
1))
167 (setq epos
(point))))
169 (while (and (< ccoun
(length scribe-open-parentheses
))
171 (or (search-forward (char-to-string
172 (aref scribe-open-parentheses ccoun
))
174 (search-forward (char-to-string
175 (aref scribe-close-parentheses ccoun
))
177 (setq ccoun
(1+ ccoun
))))
178 (if (>= ccoun
(length scribe-open-parentheses
))
179 (progn (goto-char epos
)
180 (insert "@end(" string
")")
182 (insert "@begin(" string
")"))
184 (insert (aref scribe-close-parentheses ccoun
))
186 (insert "@" string
(aref scribe-open-parentheses ccoun
))
189 (skip-chars-forward scribe-close-parentheses
))))
191 (defun scribe-underline-word (count)
192 "Underline COUNT words around point by means of Scribe constructs."
194 (scribe-envelop-word "u" count
))
196 (defun scribe-bold-word (count)
197 "Boldface COUNT words around point by means of Scribe constructs."
199 (scribe-envelop-word "b" count
))
201 (defun scribe-italicize-word (count)
202 "Italicize COUNT words around point by means of Scribe constructs."
204 (scribe-envelop-word "i" count
))
206 (defun scribe-begin ()
210 (scribe-envelop-word "Begin" 0)
211 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
217 (scribe-envelop-word "End" 0)
218 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
220 (defun scribe-chapter ()
224 (scribe-envelop-word "Chapter" 0)
225 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
227 (defun scribe-section ()
231 (scribe-envelop-word "Section" 0)
232 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
234 (defun scribe-subsection ()
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")
245 (insert "@end(" env
")\n")
247 (insert "@begin(" env
")\n")))
249 (defun scribe-insert-environment (env)
250 (interactive "sEnvironment: ")
251 (scribe-bracket-region-be env
(point) (point))
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 \"."
261 (if (or count
(not scribe-electric-quote
))
262 (self-insert-command (prefix-numeric-value count
))
263 (let (lastfore lastback lastquote
)
266 ((= (preceding-char) ?
\\) ?
\")
269 (setq lastfore
(save-excursion (and (search-backward
270 "``" (- (point) 1000) t
)
272 lastback
(save-excursion (and (search-backward
273 "''" (- (point) 1000) t
)
275 lastquote
(save-excursion (and (search-backward
276 "\"" (- (point) 100) t
)
279 (cond ((not lastfore
) "``")
280 ((not lastback
) "''")
281 ((> lastfore lastback
) "''")
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
) "''")
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."
297 (self-insert-command (prefix-numeric-value count
))
298 (let (at-command paren-char point-save
)
299 (if (or count
(not scribe-electric-parenthesis
))
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
)))))
309 (string-match (regexp-quote
310 (char-to-string (preceding-char)))
311 scribe-open-parentheses
)))
313 (insert (aref scribe-close-parentheses paren-char
)))))))
315 ;;; scribe.el ends here