1 ;;; scribe.el --- scribe mode, and its idiosyncratic commands.
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
5 ;; This file might become part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but without any warranty. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing.
13 ;; Everyone is granted permission to copy, modify and redistribute
14 ;; GNU Emacs, but only under the conditions described in the
15 ;; document "GNU Emacs copying permission notice". An exact copy
16 ;; of the document is supposed to have been given to you along with
17 ;; GNU Emacs so that you can know how you may redistribute it all.
18 ;; It should be in a file named COPYING. Among other things, the
19 ;; copyright notice and this notice must be preserved on all copies.
22 (defvar scribe-mode-syntax-table nil
23 "Syntax table used while in scribe mode.")
25 (defvar scribe-mode-abbrev-table nil
26 "Abbrev table used while in scribe mode.")
28 (defvar scribe-fancy-paragraphs nil
29 "*Non-NIL makes Scribe mode use a different style of paragraph separation.")
31 (defvar scribe-electric-quote nil
32 "*Non-NIL makes insert of double quote use `` or '' depending on context.")
34 (defvar scribe-electric-parenthesis nil
35 "*Non-NIL makes parenthesis char ( (]}> ) automatically insert its close
36 if typed after an @Command form.")
38 (defconst scribe-open-parentheses
"[({<"
39 "Open parenthesis characters for Scribe.")
41 (defconst scribe-close-parentheses
"])}>"
42 "Close parenthesis characters for Scribe. These should match up with
43 scribe-open-parenthesis.")
45 (if (null scribe-mode-syntax-table
)
46 (let ((st (syntax-table)))
49 (setq scribe-mode-syntax-table
(copy-syntax-table
50 text-mode-syntax-table
))
51 (set-syntax-table scribe-mode-syntax-table
)
52 (modify-syntax-entry ?
\" " ")
53 (modify-syntax-entry ?
\\ " ")
54 (modify-syntax-entry ?
@ "w ")
55 (modify-syntax-entry ?
< "(> ")
56 (modify-syntax-entry ?
> ")< ")
57 (modify-syntax-entry ?
[ "(] ")
58 (modify-syntax-entry ?
] ")[ ")
59 (modify-syntax-entry ?
{ "(} ")
60 (modify-syntax-entry ?
} "){ ")
61 (modify-syntax-entry ?
' "w "))
62 (set-syntax-table st
))))
64 (defvar scribe-mode-map nil
)
68 (setq scribe-mode-map
(make-sparse-keymap))
69 (define-key scribe-mode-map
"\t" 'scribe-tab
)
70 (define-key scribe-mode-map
"\e\t" 'tab-to-tab-stop
)
71 (define-key scribe-mode-map
"\es" 'center-line
)
72 (define-key scribe-mode-map
"\e}" 'up-list
)
73 (define-key scribe-mode-map
"\eS" 'center-paragraph
)
74 (define-key scribe-mode-map
"\"" 'scribe-insert-quote
)
75 (define-key scribe-mode-map
"(" 'scribe-parenthesis
)
76 (define-key scribe-mode-map
"[" 'scribe-parenthesis
)
77 (define-key scribe-mode-map
"{" 'scribe-parenthesis
)
78 (define-key scribe-mode-map
"<" 'scribe-parenthesis
)
79 (define-key scribe-mode-map
"\^cc" 'scribe-chapter
)
80 (define-key scribe-mode-map
"\^cS" 'scribe-section
)
81 (define-key scribe-mode-map
"\^cs" 'scribe-subsection
)
82 (define-key scribe-mode-map
"\^ce" 'scribe-insert-environment
)
83 (define-key scribe-mode-map
"\^c\^e" 'scribe-bracket-region-be
)
84 (define-key scribe-mode-map
"\^c[" 'scribe-begin
)
85 (define-key scribe-mode-map
"\^c]" 'scribe-end
)
86 (define-key scribe-mode-map
"\^ci" 'scribe-italicize-word
)
87 (define-key scribe-mode-map
"\^cb" 'scribe-bold-word
)
88 (define-key scribe-mode-map
"\^cu" 'scribe-underline-word
))
92 "Major mode for editing files of Scribe (a text formatter) source.
93 Scribe-mode is similar text-mode, with a few extra commands added.
96 Interesting variables:
98 scribe-fancy-paragraphs
99 Non-nil makes Scribe mode use a different style of paragraph separation.
101 scribe-electric-quote
102 Non-nil makes insert of double quote use `` or '' depending on context.
104 scribe-electric-parenthesis
105 Non-nil makes an open-parenthesis char (one of `([<{')
106 automatically insert its close if typed after an @Command form."
108 (kill-all-local-variables)
109 (use-local-map scribe-mode-map
)
110 (setq mode-name
"Scribe")
111 (setq major-mode
'scribe-mode
)
112 (define-abbrev-table 'scribe-mode-abbrev-table
())
113 (setq local-abbrev-table scribe-mode-abbrev-table
)
114 (make-local-variable 'comment-start
)
115 (setq comment-start
"@Comment[")
116 (make-local-variable 'comment-start-skip
)
117 (setq comment-start-skip
(concat "@Comment[" scribe-open-parentheses
"]"))
118 (make-local-variable 'comment-column
)
119 (setq comment-column
0)
120 (make-local-variable 'comment-end
)
121 (setq comment-end
"]")
122 (make-local-variable 'paragraph-start
)
123 (setq paragraph-start
(concat "\\(^[\n\f]\\)\\|\\(^@\\w+["
124 scribe-open-parentheses
126 scribe-close-parentheses
128 (make-local-variable 'paragraph-separate
)
129 (setq paragraph-separate
(if scribe-fancy-paragraphs
130 paragraph-start
"^$"))
131 (make-local-variable 'compile-command
)
132 (setq compile-command
(concat "scribe " (buffer-file-name)))
133 (set-syntax-table scribe-mode-syntax-table
)
134 (run-hooks 'text-mode-hook
'scribe-mode-hook
))
140 ;; This algorithm could probably be improved somewhat.
141 ;; Right now, it loses seriously...
144 "Run Scribe on the current buffer."
146 (call-interactively 'compile
))
148 (defun scribe-envelop-word (string count
)
149 "Surround current word with Scribe construct @STRING[...]. COUNT
150 specifies how many words to surround. A negative count means to skip
152 (let ((spos (point)) (epos (point)) (ccoun 0) noparens
)
153 (if (not (zerop count
))
154 (progn (if (= (char-syntax (preceding-char)) ?w
)
155 (forward-sexp (min -
1 count
)))
157 (if (looking-at (concat "@\\w[" scribe-open-parentheses
"]"))
160 (skip-chars-backward "\\W")
162 (forward-sexp (max count
1))
163 (setq epos
(point))))
165 (while (and (< ccoun
(length scribe-open-parentheses
))
167 (or (search-forward (char-to-string
168 (aref scribe-open-parentheses ccoun
))
170 (search-forward (char-to-string
171 (aref scribe-close-parentheses ccoun
))
173 (setq ccoun
(1+ ccoun
))))
174 (if (>= ccoun
(length scribe-open-parentheses
))
175 (progn (goto-char epos
)
176 (insert "@end(" string
")")
178 (insert "@begin(" string
")"))
180 (insert (aref scribe-close-parentheses ccoun
))
182 (insert "@" string
(aref scribe-open-parentheses ccoun
))
185 (skip-chars-forward scribe-close-parentheses
))))
187 (defun scribe-underline-word (count)
188 "Underline COUNT words around point by means of Scribe constructs."
190 (scribe-envelop-word "u" count
))
192 (defun scribe-bold-word (count)
193 "Boldface COUNT words around point by means of Scribe constructs."
195 (scribe-envelop-word "b" count
))
197 (defun scribe-italicize-word (count)
198 "Italicize COUNT words around point by means of Scribe constructs."
200 (scribe-envelop-word "i" count
))
202 (defun scribe-begin ()
206 (scribe-envelop-word "Begin" 0)
207 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
213 (scribe-envelop-word "End" 0)
214 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
216 (defun scribe-chapter ()
220 (scribe-envelop-word "Chapter" 0)
221 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
223 (defun scribe-section ()
227 (scribe-envelop-word "Section" 0)
228 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
230 (defun scribe-subsection ()
234 (scribe-envelop-word "SubSection" 0)
235 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
237 (defun scribe-bracket-region-be (env min max
)
238 (interactive "sEnvironment: \nr")
241 (insert "@end(" env
")\n")
243 (insert "@begin(" env
")\n")))
245 (defun scribe-insert-environment (env)
246 (interactive "sEnvironment: ")
247 (scribe-bracket-region-be env
(point) (point))
252 (defun scribe-insert-quote (count)
253 "If scribe-electric-quote is non-NIL, insert ``, '' or \" according
254 to preceding character. With numeric arg N, always insert N \" characters.
255 Else just insert \"."
257 (if (or count
(not scribe-electric-quote
))
258 (self-insert-command (prefix-numeric-value count
))
259 (let (lastfore lastback lastquote
)
262 ((= (preceding-char) ?
\\) ?
\")
265 (setq lastfore
(save-excursion (and (search-backward
266 "``" (- (point) 1000) t
)
268 lastback
(save-excursion (and (search-backward
269 "''" (- (point) 1000) t
)
271 lastquote
(save-excursion (and (search-backward
272 "\"" (- (point) 100) t
)
275 (cond ((not lastfore
) "``")
276 ((not lastback
) "''")
277 ((> lastfore lastback
) "''")
279 (cond ((and (not lastback
) (not lastfore
)) "\"")
280 ((and lastback
(not lastfore
) (> lastquote lastback
)) "\"")
281 ((and lastback
(not lastfore
) (> lastback lastquote
)) "``")
282 ((and lastfore
(not lastback
) (> lastquote lastfore
)) "\"")
283 ((and lastfore
(not lastback
) (> lastfore lastquote
)) "''")
284 ((and (> lastquote lastfore
) (> lastquote lastback
)) "\"")
285 ((> lastfore lastback
) "''")
288 (defun scribe-parenthesis (count)
289 "If scribe-electric-parenthesis is non-NIL, insertion of an open-parenthesis
290 character inserts the following close parenthesis character if the
291 preceding text is of the form @Command."
293 (self-insert-command (prefix-numeric-value count
))
294 (let (at-command paren-char point-save
)
295 (if (or count
(not scribe-electric-parenthesis
))
299 (setq point-save
(point))
300 (skip-chars-backward (concat "^ \n\t\f" scribe-open-parentheses
))
301 (setq at-command
(and (equal (following-char) ?
@)
302 (/= (point) (1- point-save
)))))
305 (string-match (regexp-quote
306 (char-to-string (preceding-char)))
307 scribe-open-parentheses
)))
309 (insert (aref scribe-close-parentheses paren-char
)))))))
311 ;;; scribe.el ends here