1 ;;; scribe.el --- scribe mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 2001-2016 Free Software Foundation, Inc.
5 ;; Author: William Sommerfeld
6 ;; (according to ack.texi)
7 ;; Maintainer: emacs-devel@gnu.org
9 ;; Obsolete-since: 22.1
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; A major mode for editing source in written for the Scribe text formatter.
29 ;; Knows about Scribe syntax and standard layout rules. The command to
30 ;; run Scribe on a buffer is bogus; someone interested should fix it.
34 (defvar compile-command
)
41 (defvar scribe-mode-syntax-table nil
42 "Syntax table used while in scribe mode.")
44 (defvar scribe-mode-abbrev-table nil
45 "Abbrev table used while in scribe mode.")
47 (defcustom scribe-fancy-paragraphs nil
48 "Non-nil makes Scribe mode use a different style of paragraph separation."
52 (defcustom scribe-electric-quote nil
53 "Non-nil makes insert of double quote use \\=`\\=` or \\='\\=' depending on context."
57 (defcustom scribe-electric-parenthesis nil
58 "Non-nil makes parenthesis char ( (]}> ) automatically insert its close
59 if typed after an @Command form."
63 (defconst scribe-open-parentheses
"[({<"
64 "Open parenthesis characters for Scribe.")
66 (defconst scribe-close-parentheses
"])}>"
67 "Close parenthesis characters for Scribe.
68 These should match up with `scribe-open-parenthesis'.")
70 (if (null scribe-mode-syntax-table
)
71 (let ((st (syntax-table)))
74 (setq scribe-mode-syntax-table
(copy-syntax-table
75 text-mode-syntax-table
))
76 (set-syntax-table scribe-mode-syntax-table
)
77 (modify-syntax-entry ?
\" " ")
78 (modify-syntax-entry ?
\\ " ")
79 (modify-syntax-entry ?
@ "w ")
80 (modify-syntax-entry ?
< "(> ")
81 (modify-syntax-entry ?
> ")< ")
82 (modify-syntax-entry ?
[ "(] ")
83 (modify-syntax-entry ?
] ")[ ")
84 (modify-syntax-entry ?
{ "(} ")
85 (modify-syntax-entry ?
} "){ ")
86 (modify-syntax-entry ?
' "w "))
87 (set-syntax-table st
))))
89 (defvar scribe-mode-map nil
)
93 (setq scribe-mode-map
(make-sparse-keymap))
94 (define-key scribe-mode-map
"\t" 'scribe-tab
)
95 (define-key scribe-mode-map
"\e\t" 'tab-to-tab-stop
)
96 (define-key scribe-mode-map
"\es" 'center-line
)
97 (define-key scribe-mode-map
"\e}" 'up-list
)
98 (define-key scribe-mode-map
"\eS" 'center-paragraph
)
99 (define-key scribe-mode-map
"\"" 'scribe-insert-quote
)
100 (define-key scribe-mode-map
"(" 'scribe-parenthesis
)
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
"\C-c\C-c" 'scribe-chapter
)
105 (define-key scribe-mode-map
"\C-c\C-t" 'scribe-section
)
106 (define-key scribe-mode-map
"\C-c\C-s" 'scribe-subsection
)
107 (define-key scribe-mode-map
"\C-c\C-v" 'scribe-insert-environment
)
108 (define-key scribe-mode-map
"\C-c\C-e" 'scribe-bracket-region-be
)
109 (define-key scribe-mode-map
"\C-c[" 'scribe-begin
)
110 (define-key scribe-mode-map
"\C-c]" 'scribe-end
)
111 (define-key scribe-mode-map
"\C-c\C-i" 'scribe-italicize-word
)
112 (define-key scribe-mode-map
"\C-c\C-b" 'scribe-bold-word
)
113 (define-key scribe-mode-map
"\C-c\C-u" 'scribe-underline-word
))
116 (define-derived-mode scribe-mode text-mode
"Scribe"
117 "Major mode for editing files of Scribe (a text formatter) source.
118 Scribe-mode is similar to text-mode, with a few extra commands added.
121 Interesting variables:
123 `scribe-fancy-paragraphs'
124 Non-nil makes Scribe mode use a different style of paragraph separation.
126 `scribe-electric-quote'
127 Non-nil makes insert of double quote use \\=`\\=` or \\='\\=' depending on context.
129 `scribe-electric-parenthesis'
130 Non-nil makes an open-parenthesis char (one of `([<{')
131 automatically insert its close if typed after an @Command form."
132 (set (make-local-variable 'comment-start
) "@Comment[")
133 (set (make-local-variable 'comment-start-skip
) (concat "@Comment[" scribe-open-parentheses
"]"))
134 (set (make-local-variable 'comment-column
) 0)
135 (set (make-local-variable 'comment-end
) "]")
136 (set (make-local-variable 'paragraph-start
)
137 (concat "\\([\n\f]\\)\\|\\(@\\w+["
138 scribe-open-parentheses
140 scribe-close-parentheses
142 (set (make-local-variable 'paragraph-separate
)
143 (if scribe-fancy-paragraphs paragraph-start
"$"))
144 (set (make-local-variable 'sentence-end
)
145 "\\([.?!]\\|@:\\)[]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*")
146 (set (make-local-variable 'compile-command
)
149 (shell-quote-argument (buffer-file-name))))))
155 ;; This algorithm could probably be improved somewhat.
156 ;; Right now, it loses seriously...
159 "Run Scribe on the current buffer."
161 (call-interactively 'compile
))
163 (defun scribe-envelop-word (string count
)
164 "Surround current word with Scribe construct @STRING[...].
165 COUNT specifies how many words to surround. A negative count means
167 (let ((spos (point)) (epos (point)) (ccoun 0) noparens
)
168 (if (not (zerop count
))
169 (progn (if (= (char-syntax (preceding-char)) ?w
)
170 (forward-sexp (min -
1 count
)))
172 (if (looking-at (concat "@\\w[" scribe-open-parentheses
"]"))
175 (skip-chars-backward "\\W")
177 (forward-sexp (max count
1))
178 (setq epos
(point))))
180 (while (and (< ccoun
(length scribe-open-parentheses
))
182 (or (search-forward (char-to-string
183 (aref scribe-open-parentheses ccoun
))
185 (search-forward (char-to-string
186 (aref scribe-close-parentheses ccoun
))
188 (setq ccoun
(1+ ccoun
))))
189 (if (>= ccoun
(length scribe-open-parentheses
))
190 (progn (goto-char epos
)
191 (insert "@end(" string
")")
193 (insert "@begin(" string
")"))
195 (insert (aref scribe-close-parentheses ccoun
))
197 (insert "@" string
(aref scribe-open-parentheses ccoun
))
200 (skip-chars-forward scribe-close-parentheses
))))
202 (defun scribe-underline-word (count)
203 "Underline COUNT words around point by means of Scribe constructs."
205 (scribe-envelop-word "u" count
))
207 (defun scribe-bold-word (count)
208 "Boldface COUNT words around point by means of Scribe constructs."
210 (scribe-envelop-word "b" count
))
212 (defun scribe-italicize-word (count)
213 "Italicize COUNT words around point by means of Scribe constructs."
215 (scribe-envelop-word "i" count
))
217 (defun scribe-begin ()
221 (scribe-envelop-word "Begin" 0)
222 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
228 (scribe-envelop-word "End" 0)
229 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
231 (defun scribe-chapter ()
235 (scribe-envelop-word "Chapter" 0)
236 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
238 (defun scribe-section ()
242 (scribe-envelop-word "Section" 0)
243 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
245 (defun scribe-subsection ()
249 (scribe-envelop-word "SubSection" 0)
250 (re-search-forward (concat "[" scribe-open-parentheses
"]")))
252 (defun scribe-bracket-region-be (env min max
)
253 (interactive "sEnvironment: \nr")
256 (insert "@end(" env
")\n")
258 (insert "@begin(" env
")\n")))
260 (defun scribe-insert-environment (env)
261 (interactive "sEnvironment: ")
262 (scribe-bracket-region-be env
(point) (point))
267 (defun scribe-insert-quote (count)
268 "Insert \\=`\\=`, \\='\\=' or \" according to preceding character.
269 If `scribe-electric-quote' is non-nil, insert \\=`\\=`, \\='\\=' or \" according
270 to preceding character. With numeric arg N, always insert N \" characters.
271 Else just insert \"."
273 (if (or count
(not scribe-electric-quote
))
274 (self-insert-command (prefix-numeric-value count
))
275 (let (lastfore lastback lastquote
)
278 ((= (preceding-char) ?
\\) ?
\")
281 (setq lastfore
(save-excursion (and (search-backward
282 "``" (- (point) 1000) t
)
284 lastback
(save-excursion (and (search-backward
285 "''" (- (point) 1000) t
)
287 lastquote
(save-excursion (and (search-backward
288 "\"" (- (point) 100) t
)
291 (cond ((not lastfore
) "``")
292 ((not lastback
) "''")
293 ((> lastfore lastback
) "''")
295 (cond ((and (not lastback
) (not lastfore
)) "\"")
296 ((and lastback
(not lastfore
) (> lastquote lastback
)) "\"")
297 ((and lastback
(not lastfore
) (> lastback lastquote
)) "``")
298 ((and lastfore
(not lastback
) (> lastquote lastfore
)) "\"")
299 ((and lastfore
(not lastback
) (> lastfore lastquote
)) "''")
300 ((and (> lastquote lastfore
) (> lastquote lastback
)) "\"")
301 ((> lastfore lastback
) "''")
304 (defun scribe-parenthesis (count)
305 "If scribe-electric-parenthesis is non-nil, insertion of an open-parenthesis
306 character inserts the following close parenthesis character if the
307 preceding text is of the form @Command."
309 (self-insert-command (prefix-numeric-value count
))
310 (let (at-command paren-char point-save
)
311 (if (or count
(not scribe-electric-parenthesis
))
315 (setq point-save
(point))
316 (skip-chars-backward (concat "^ \n\t\f" scribe-open-parentheses
))
317 (setq at-command
(and (equal (following-char) ?
@)
318 (/= (point) (1- point-save
)))))
321 (string-match (regexp-quote
322 (char-to-string (preceding-char)))
323 scribe-open-parentheses
)))
325 (insert (aref scribe-close-parentheses paren-char
)))))))
329 ;;; scribe.el ends here