1 ;;; scribe.el --- scribe mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: William Sommerfeld
6 ;; (according to ack.texi)
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file has been obsolete since Emacs 22.1.
29 ;; A major mode for editing source in written for the Scribe text formatter.
30 ;; Knows about Scribe syntax and standard layout rules. The command to
31 ;; run Scribe on a buffer is bogus; someone interested should fix it.
35 (defvar compile-command
)
42 (defvar scribe-mode-syntax-table nil
43 "Syntax table used while in scribe mode.")
45 (defvar scribe-mode-abbrev-table nil
46 "Abbrev table used while in scribe mode.")
48 (defcustom scribe-fancy-paragraphs nil
49 "*Non-nil makes Scribe mode use a different style of paragraph separation."
53 (defcustom scribe-electric-quote nil
54 "*Non-nil makes insert of double quote use `` or '' depending on context."
58 (defcustom scribe-electric-parenthesis nil
59 "*Non-nil makes parenthesis char ( (]}> ) automatically insert its close
60 if typed after an @Command form."
64 (defconst scribe-open-parentheses
"[({<"
65 "Open parenthesis characters for Scribe.")
67 (defconst scribe-close-parentheses
"])}>"
68 "Close parenthesis characters for Scribe.
69 These should match up with `scribe-open-parenthesis'.")
71 (if (null scribe-mode-syntax-table
)
72 (let ((st (syntax-table)))
75 (setq scribe-mode-syntax-table
(copy-syntax-table
76 text-mode-syntax-table
))
77 (set-syntax-table scribe-mode-syntax-table
)
78 (modify-syntax-entry ?
\" " ")
79 (modify-syntax-entry ?
\\ " ")
80 (modify-syntax-entry ?
@ "w ")
81 (modify-syntax-entry ?
< "(> ")
82 (modify-syntax-entry ?
> ")< ")
83 (modify-syntax-entry ?
[ "(] ")
84 (modify-syntax-entry ?
] ")[ ")
85 (modify-syntax-entry ?
{ "(} ")
86 (modify-syntax-entry ?
} "){ ")
87 (modify-syntax-entry ?
' "w "))
88 (set-syntax-table st
))))
90 (defvar scribe-mode-map nil
)
94 (setq scribe-mode-map
(make-sparse-keymap))
95 (define-key scribe-mode-map
"\t" 'scribe-tab
)
96 (define-key scribe-mode-map
"\e\t" 'tab-to-tab-stop
)
97 (define-key scribe-mode-map
"\es" 'center-line
)
98 (define-key scribe-mode-map
"\e}" 'up-list
)
99 (define-key scribe-mode-map
"\eS" 'center-paragraph
)
100 (define-key scribe-mode-map
"\"" 'scribe-insert-quote
)
101 (define-key scribe-mode-map
"(" 'scribe-parenthesis
)
102 (define-key scribe-mode-map
"[" 'scribe-parenthesis
)
103 (define-key scribe-mode-map
"{" 'scribe-parenthesis
)
104 (define-key scribe-mode-map
"<" 'scribe-parenthesis
)
105 (define-key scribe-mode-map
"\C-c\C-c" 'scribe-chapter
)
106 (define-key scribe-mode-map
"\C-c\C-t" 'scribe-section
)
107 (define-key scribe-mode-map
"\C-c\C-s" 'scribe-subsection
)
108 (define-key scribe-mode-map
"\C-c\C-v" 'scribe-insert-environment
)
109 (define-key scribe-mode-map
"\C-c\C-e" 'scribe-bracket-region-be
)
110 (define-key scribe-mode-map
"\C-c[" 'scribe-begin
)
111 (define-key scribe-mode-map
"\C-c]" 'scribe-end
)
112 (define-key scribe-mode-map
"\C-c\C-i" 'scribe-italicize-word
)
113 (define-key scribe-mode-map
"\C-c\C-b" 'scribe-bold-word
)
114 (define-key scribe-mode-map
"\C-c\C-u" 'scribe-underline-word
))
117 (define-derived-mode scribe-mode text-mode
"Scribe"
118 "Major mode for editing files of Scribe (a text formatter) source.
119 Scribe-mode is similar to text-mode, with a few extra commands added.
122 Interesting variables:
124 `scribe-fancy-paragraphs'
125 Non-nil makes Scribe mode use a different style of paragraph separation.
127 `scribe-electric-quote'
128 Non-nil makes insert of double quote use `` or '' depending on context.
130 `scribe-electric-parenthesis'
131 Non-nil makes an open-parenthesis char (one of `([<{')
132 automatically insert its close if typed after an @Command form."
133 (set (make-local-variable 'comment-start
) "@Comment[")
134 (set (make-local-variable 'comment-start-skip
) (concat "@Comment[" scribe-open-parentheses
"]"))
135 (set (make-local-variable 'comment-column
) 0)
136 (set (make-local-variable 'comment-end
) "]")
137 (set (make-local-variable 'paragraph-start
)
138 (concat "\\([\n\f]\\)\\|\\(@\\w+["
139 scribe-open-parentheses
141 scribe-close-parentheses
143 (set (make-local-variable 'paragraph-separate
)
144 (if scribe-fancy-paragraphs paragraph-start
"$"))
145 (set (make-local-variable 'sentence-end
)
146 "\\([.?!]\\|@:\\)[]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*")
147 (set (make-local-variable 'compile-command
)
148 (concat "scribe " (buffer-file-name))))
154 ;; This algorithm could probably be improved somewhat.
155 ;; Right now, it loses seriously...
158 "Run Scribe on the current buffer."
160 (call-interactively 'compile
))
162 (defun scribe-envelop-word (string count
)
163 "Surround current word with Scribe construct @STRING[...].
164 COUNT specifies how many words to surround. A negative count means
166 (let ((spos (point)) (epos (point)) (ccoun 0) noparens
)
167 (if (not (zerop count
))
168 (progn (if (= (char-syntax (preceding-char)) ?w
)
169 (forward-sexp (min -
1 count
)))
171 (if (looking-at (concat "@\\w[" scribe-open-parentheses
"]"))
174 (skip-chars-backward "\\W")
176 (forward-sexp (max count
1))
177 (setq epos
(point))))
179 (while (and (< ccoun
(length scribe-open-parentheses
))
181 (or (search-forward (char-to-string
182 (aref scribe-open-parentheses ccoun
))
184 (search-forward (char-to-string
185 (aref scribe-close-parentheses ccoun
))
187 (setq ccoun
(1+ ccoun
))))
188 (if (>= ccoun
(length scribe-open-parentheses
))
189 (progn (goto-char epos
)
190 (insert "@end(" string
")")
192 (insert "@begin(" string
")"))
194 (insert (aref scribe-close-parentheses ccoun
))
196 (insert "@" string
(aref scribe-open-parentheses ccoun
))
199 (skip-chars-forward scribe-close-parentheses
))))
201 (defun scribe-underline-word (count)
202 "Underline COUNT words around point by means of Scribe constructs."
204 (scribe-envelop-word "u" count
))
206 (defun scribe-bold-word (count)
207 "Boldface COUNT words around point by means of Scribe constructs."
209 (scribe-envelop-word "b" count
))
211 (defun scribe-italicize-word (count)
212 "Italicize COUNT words around point by means of Scribe constructs."
214 (scribe-envelop-word "i" count
))
216 (defun scribe-begin ()
220 (scribe-envelop-word "Begin" 0)
221 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
227 (scribe-envelop-word "End" 0)
228 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
230 (defun scribe-chapter ()
234 (scribe-envelop-word "Chapter" 0)
235 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
237 (defun scribe-section ()
241 (scribe-envelop-word "Section" 0)
242 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
244 (defun scribe-subsection ()
248 (scribe-envelop-word "SubSection" 0)
249 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
251 (defun scribe-bracket-region-be (env min max
)
252 (interactive "sEnvironment: \nr")
255 (insert "@end(" env
")\n")
257 (insert "@begin(" env
")\n")))
259 (defun scribe-insert-environment (env)
260 (interactive "sEnvironment: ")
261 (scribe-bracket-region-be env
(point) (point))
266 (defun scribe-insert-quote (count)
267 "Insert ``, '' or \" according to preceding character.
268 If `scribe-electric-quote' is non-nil, insert ``, '' or \" according
269 to preceding character. With numeric arg N, always insert N \" characters.
270 Else just insert \"."
272 (if (or count
(not scribe-electric-quote
))
273 (self-insert-command (prefix-numeric-value count
))
274 (let (lastfore lastback lastquote
)
277 ((= (preceding-char) ?
\\) ?
\")
280 (setq lastfore
(save-excursion (and (search-backward
281 "``" (- (point) 1000) t
)
283 lastback
(save-excursion (and (search-backward
284 "''" (- (point) 1000) t
)
286 lastquote
(save-excursion (and (search-backward
287 "\"" (- (point) 100) t
)
290 (cond ((not lastfore
) "``")
291 ((not lastback
) "''")
292 ((> lastfore lastback
) "''")
294 (cond ((and (not lastback
) (not lastfore
)) "\"")
295 ((and lastback
(not lastfore
) (> lastquote lastback
)) "\"")
296 ((and lastback
(not lastfore
) (> lastback lastquote
)) "``")
297 ((and lastfore
(not lastback
) (> lastquote lastfore
)) "\"")
298 ((and lastfore
(not lastback
) (> lastfore lastquote
)) "''")
299 ((and (> lastquote lastfore
) (> lastquote lastback
)) "\"")
300 ((> lastfore lastback
) "''")
303 (defun scribe-parenthesis (count)
304 "If scribe-electric-parenthesis is non-nil, insertion of an open-parenthesis
305 character inserts the following close parenthesis character if the
306 preceding text is of the form @Command."
308 (self-insert-command (prefix-numeric-value count
))
309 (let (at-command paren-char point-save
)
310 (if (or count
(not scribe-electric-parenthesis
))
314 (setq point-save
(point))
315 (skip-chars-backward (concat "^ \n\t\f" scribe-open-parentheses
))
316 (setq at-command
(and (equal (following-char) ?
@)
317 (/= (point) (1- point-save
)))))
320 (string-match (regexp-quote
321 (char-to-string (preceding-char)))
322 scribe-open-parentheses
)))
324 (insert (aref scribe-close-parentheses paren-char
)))))))
328 ;;; scribe.el ends here