1 ;;; scribe.el --- scribe mode, and its idiosyncratic commands.
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
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.
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)))
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
)
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
))
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.
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."
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
129 scribe-close-parentheses
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
))
143 ;; This algorithm could probably be improved somewhat.
144 ;; Right now, it loses seriously...
147 "Run Scribe on the current buffer."
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
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
)))
160 (if (looking-at (concat "@\\w[" scribe-open-parentheses
"]"))
163 (skip-chars-backward "\\W")
165 (forward-sexp (max count
1))
166 (setq epos
(point))))
168 (while (and (< ccoun
(length scribe-open-parentheses
))
170 (or (search-forward (char-to-string
171 (aref scribe-open-parentheses ccoun
))
173 (search-forward (char-to-string
174 (aref scribe-close-parentheses ccoun
))
176 (setq ccoun
(1+ ccoun
))))
177 (if (>= ccoun
(length scribe-open-parentheses
))
178 (progn (goto-char epos
)
179 (insert "@end(" string
")")
181 (insert "@begin(" string
")"))
183 (insert (aref scribe-close-parentheses ccoun
))
185 (insert "@" string
(aref scribe-open-parentheses ccoun
))
188 (skip-chars-forward scribe-close-parentheses
))))
190 (defun scribe-underline-word (count)
191 "Underline COUNT words around point by means of Scribe constructs."
193 (scribe-envelop-word "u" count
))
195 (defun scribe-bold-word (count)
196 "Boldface COUNT words around point by means of Scribe constructs."
198 (scribe-envelop-word "b" count
))
200 (defun scribe-italicize-word (count)
201 "Italicize COUNT words around point by means of Scribe constructs."
203 (scribe-envelop-word "i" count
))
205 (defun scribe-begin ()
209 (scribe-envelop-word "Begin" 0)
210 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
216 (scribe-envelop-word "End" 0)
217 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
219 (defun scribe-chapter ()
223 (scribe-envelop-word "Chapter" 0)
224 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
226 (defun scribe-section ()
230 (scribe-envelop-word "Section" 0)
231 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
233 (defun scribe-subsection ()
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")
244 (insert "@end(" env
")\n")
246 (insert "@begin(" env
")\n")))
248 (defun scribe-insert-environment (env)
249 (interactive "sEnvironment: ")
250 (scribe-bracket-region-be env
(point) (point))
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 \"."
260 (if (or count
(not scribe-electric-quote
))
261 (self-insert-command (prefix-numeric-value count
))
262 (let (lastfore lastback lastquote
)
265 ((= (preceding-char) ?
\\) ?
\")
268 (setq lastfore
(save-excursion (and (search-backward
269 "``" (- (point) 1000) t
)
271 lastback
(save-excursion (and (search-backward
272 "''" (- (point) 1000) t
)
274 lastquote
(save-excursion (and (search-backward
275 "\"" (- (point) 100) t
)
278 (cond ((not lastfore
) "``")
279 ((not lastback
) "''")
280 ((> lastfore lastback
) "''")
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
) "''")
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."
296 (self-insert-command (prefix-numeric-value count
))
297 (let (at-command paren-char point-save
)
298 (if (or count
(not scribe-electric-parenthesis
))
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
)))))
308 (string-match (regexp-quote
309 (char-to-string (preceding-char)))
310 scribe-open-parentheses
)))
312 (insert (aref scribe-close-parentheses paren-char
)))))))
314 ;;; scribe.el ends here