Commit | Line | Data |
---|---|---|
17cd3083 JB |
1 | ;;; scribe.el --- scribe mode, and its idiosyncratic commands |
2 | ||
3731a850 | 3 | ;; Copyright (C) 1985, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
17cd3083 JB |
4 | |
5 | ;; Maintainer: FSF | |
6 | ;; Keywords: wp | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation; either version 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
3a35cf56 LK |
22 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 | ;; Boston, MA 02110-1301, USA. | |
17cd3083 JB |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; A major mode for editing source in written for the Scribe text formatter. | |
28 | ;; Knows about Scribe syntax and standard layout rules. The command to | |
29 | ;; run Scribe on a buffer is bogus; someone interested should fix it. | |
30 | ||
31 | ;;; Code: | |
32 | ||
ece830a1 JB |
33 | (defvar compile-command) |
34 | ||
17cd3083 JB |
35 | (defgroup scribe nil |
36 | "Scribe mode." | |
37 | :prefix "scribe-" | |
38 | :group 'wp) | |
39 | ||
40 | (defvar scribe-mode-syntax-table nil | |
41 | "Syntax table used while in scribe mode.") | |
42 | ||
43 | (defvar scribe-mode-abbrev-table nil | |
44 | "Abbrev table used while in scribe mode.") | |
45 | ||
46 | (defcustom scribe-fancy-paragraphs nil | |
47 | "*Non-nil makes Scribe mode use a different style of paragraph separation." | |
48 | :type 'boolean | |
49 | :group 'scribe) | |
50 | ||
51 | (defcustom scribe-electric-quote nil | |
52 | "*Non-nil makes insert of double quote use `` or '' depending on context." | |
53 | :type 'boolean | |
54 | :group 'scribe) | |
55 | ||
56 | (defcustom scribe-electric-parenthesis nil | |
57 | "*Non-nil makes parenthesis char ( (]}> ) automatically insert its close | |
58 | if typed after an @Command form." | |
59 | :type 'boolean | |
60 | :group 'scribe) | |
61 | ||
62 | (defconst scribe-open-parentheses "[({<" | |
63 | "Open parenthesis characters for Scribe.") | |
64 | ||
65 | (defconst scribe-close-parentheses "])}>" | |
66 | "Close parenthesis characters for Scribe. | |
67 | These should match up with `scribe-open-parenthesis'.") | |
68 | ||
69 | (if (null scribe-mode-syntax-table) | |
70 | (let ((st (syntax-table))) | |
71 | (unwind-protect | |
72 | (progn | |
73 | (setq scribe-mode-syntax-table (copy-syntax-table | |
74 | text-mode-syntax-table)) | |
75 | (set-syntax-table scribe-mode-syntax-table) | |
76 | (modify-syntax-entry ?\" " ") | |
77 | (modify-syntax-entry ?\\ " ") | |
78 | (modify-syntax-entry ?@ "w ") | |
79 | (modify-syntax-entry ?< "(> ") | |
80 | (modify-syntax-entry ?> ")< ") | |
81 | (modify-syntax-entry ?[ "(] ") | |
82 | (modify-syntax-entry ?] ")[ ") | |
83 | (modify-syntax-entry ?{ "(} ") | |
84 | (modify-syntax-entry ?} "){ ") | |
85 | (modify-syntax-entry ?' "w ")) | |
86 | (set-syntax-table st)))) | |
87 | ||
88 | (defvar scribe-mode-map nil) | |
89 | ||
90 | (if scribe-mode-map | |
91 | nil | |
92 | (setq scribe-mode-map (make-sparse-keymap)) | |
93 | (define-key scribe-mode-map "\t" 'scribe-tab) | |
94 | (define-key scribe-mode-map "\e\t" 'tab-to-tab-stop) | |
95 | (define-key scribe-mode-map "\es" 'center-line) | |
96 | (define-key scribe-mode-map "\e}" 'up-list) | |
97 | (define-key scribe-mode-map "\eS" 'center-paragraph) | |
98 | (define-key scribe-mode-map "\"" 'scribe-insert-quote) | |
99 | (define-key scribe-mode-map "(" 'scribe-parenthesis) | |
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 "\C-c\C-c" 'scribe-chapter) | |
104 | (define-key scribe-mode-map "\C-c\C-t" 'scribe-section) | |
105 | (define-key scribe-mode-map "\C-c\C-s" 'scribe-subsection) | |
106 | (define-key scribe-mode-map "\C-c\C-v" 'scribe-insert-environment) | |
107 | (define-key scribe-mode-map "\C-c\C-e" 'scribe-bracket-region-be) | |
108 | (define-key scribe-mode-map "\C-c[" 'scribe-begin) | |
109 | (define-key scribe-mode-map "\C-c]" 'scribe-end) | |
110 | (define-key scribe-mode-map "\C-c\C-i" 'scribe-italicize-word) | |
111 | (define-key scribe-mode-map "\C-c\C-b" 'scribe-bold-word) | |
112 | (define-key scribe-mode-map "\C-c\C-u" 'scribe-underline-word)) | |
113 | ||
114 | ;;;###autoload | |
115 | (define-derived-mode scribe-mode text-mode "Scribe" | |
116 | "Major mode for editing files of Scribe (a text formatter) source. | |
117 | Scribe-mode is similar to text-mode, with a few extra commands added. | |
118 | \\{scribe-mode-map} | |
119 | ||
120 | Interesting variables: | |
121 | ||
122 | `scribe-fancy-paragraphs' | |
123 | Non-nil makes Scribe mode use a different style of paragraph separation. | |
124 | ||
125 | `scribe-electric-quote' | |
126 | Non-nil makes insert of double quote use `` or '' depending on context. | |
127 | ||
128 | `scribe-electric-parenthesis' | |
129 | Non-nil makes an open-parenthesis char (one of `([<{') | |
130 | automatically insert its close if typed after an @Command form." | |
131 | (set (make-local-variable 'comment-start) "@Comment[") | |
132 | (set (make-local-variable 'comment-start-skip) (concat "@Comment[" scribe-open-parentheses "]")) | |
133 | (set (make-local-variable 'comment-column) 0) | |
134 | (set (make-local-variable 'comment-end) "]") | |
135 | (set (make-local-variable 'paragraph-start) | |
136 | (concat "\\([\n\f]\\)\\|\\(@\\w+[" | |
137 | scribe-open-parentheses | |
138 | "].*[" | |
139 | scribe-close-parentheses | |
140 | "]$\\)")) | |
141 | (set (make-local-variable 'paragraph-separate) | |
142 | (if scribe-fancy-paragraphs paragraph-start "$")) | |
143 | (set (make-local-variable 'sentence-end) | |
144 | "\\([.?!]\\|@:\\)[]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") | |
145 | (set (make-local-variable 'compile-command) | |
146 | (concat "scribe " (buffer-file-name)))) | |
147 | ||
148 | (defun scribe-tab () | |
149 | (interactive) | |
150 | (insert "@\\")) | |
151 | ||
152 | ;; This algorithm could probably be improved somewhat. | |
153 | ;; Right now, it loses seriously... | |
154 | ||
155 | (defun scribe () | |
156 | "Run Scribe on the current buffer." | |
157 | (interactive) | |
158 | (call-interactively 'compile)) | |
159 | ||
160 | (defun scribe-envelop-word (string count) | |
161 | "Surround current word with Scribe construct @STRING[...]. | |
162 | COUNT specifies how many words to surround. A negative count means | |
163 | to skip backward." | |
164 | (let ((spos (point)) (epos (point)) (ccoun 0) noparens) | |
165 | (if (not (zerop count)) | |
166 | (progn (if (= (char-syntax (preceding-char)) ?w) | |
167 | (forward-sexp (min -1 count))) | |
168 | (setq spos (point)) | |
169 | (if (looking-at (concat "@\\w[" scribe-open-parentheses "]")) | |
170 | (forward-char 2) | |
171 | (goto-char epos) | |
172 | (skip-chars-backward "\\W") | |
173 | (forward-char -1)) | |
174 | (forward-sexp (max count 1)) | |
175 | (setq epos (point)))) | |
176 | (goto-char spos) | |
177 | (while (and (< ccoun (length scribe-open-parentheses)) | |
178 | (save-excursion | |
179 | (or (search-forward (char-to-string | |
180 | (aref scribe-open-parentheses ccoun)) | |
181 | epos t) | |
182 | (search-forward (char-to-string | |
183 | (aref scribe-close-parentheses ccoun)) | |
184 | epos t))) | |
185 | (setq ccoun (1+ ccoun)))) | |
186 | (if (>= ccoun (length scribe-open-parentheses)) | |
187 | (progn (goto-char epos) | |
188 | (insert "@end(" string ")") | |
189 | (goto-char spos) | |
190 | (insert "@begin(" string ")")) | |
191 | (goto-char epos) | |
192 | (insert (aref scribe-close-parentheses ccoun)) | |
193 | (goto-char spos) | |
194 | (insert "@" string (aref scribe-open-parentheses ccoun)) | |
195 | (goto-char epos) | |
196 | (forward-char 3) | |
197 | (skip-chars-forward scribe-close-parentheses)))) | |
198 | ||
199 | (defun scribe-underline-word (count) | |
200 | "Underline COUNT words around point by means of Scribe constructs." | |
201 | (interactive "p") | |
202 | (scribe-envelop-word "u" count)) | |
203 | ||
204 | (defun scribe-bold-word (count) | |
205 | "Boldface COUNT words around point by means of Scribe constructs." | |
206 | (interactive "p") | |
207 | (scribe-envelop-word "b" count)) | |
208 | ||
209 | (defun scribe-italicize-word (count) | |
210 | "Italicize COUNT words around point by means of Scribe constructs." | |
211 | (interactive "p") | |
212 | (scribe-envelop-word "i" count)) | |
213 | ||
214 | (defun scribe-begin () | |
215 | (interactive) | |
216 | (insert "\n") | |
217 | (forward-char -1) | |
218 | (scribe-envelop-word "Begin" 0) | |
219 | (re-search-forward (concat "[" scribe-open-parentheses "]"))) | |
220 | ||
221 | (defun scribe-end () | |
222 | (interactive) | |
223 | (insert "\n") | |
224 | (forward-char -1) | |
225 | (scribe-envelop-word "End" 0) | |
226 | (re-search-forward (concat "[" scribe-open-parentheses "]"))) | |
227 | ||
228 | (defun scribe-chapter () | |
229 | (interactive) | |
230 | (insert "\n") | |
231 | (forward-char -1) | |
232 | (scribe-envelop-word "Chapter" 0) | |
233 | (re-search-forward (concat "[" scribe-open-parentheses "]"))) | |
234 | ||
235 | (defun scribe-section () | |
236 | (interactive) | |
237 | (insert "\n") | |
238 | (forward-char -1) | |
239 | (scribe-envelop-word "Section" 0) | |
240 | (re-search-forward (concat "[" scribe-open-parentheses "]"))) | |
241 | ||
242 | (defun scribe-subsection () | |
243 | (interactive) | |
244 | (insert "\n") | |
245 | (forward-char -1) | |
246 | (scribe-envelop-word "SubSection" 0) | |
247 | (re-search-forward (concat "[" scribe-open-parentheses "]"))) | |
248 | ||
249 | (defun scribe-bracket-region-be (env min max) | |
250 | (interactive "sEnvironment: \nr") | |
251 | (save-excursion | |
252 | (goto-char max) | |
253 | (insert "@end(" env ")\n") | |
254 | (goto-char min) | |
255 | (insert "@begin(" env ")\n"))) | |
256 | ||
257 | (defun scribe-insert-environment (env) | |
258 | (interactive "sEnvironment: ") | |
259 | (scribe-bracket-region-be env (point) (point)) | |
260 | (forward-line 1) | |
261 | (insert ?\n) | |
262 | (forward-char -1)) | |
263 | ||
264 | (defun scribe-insert-quote (count) | |
265 | "Insert ``, '' or \" according to preceding character. | |
266 | If `scribe-electric-quote' is non-nil, insert ``, '' or \" according | |
267 | to preceding character. With numeric arg N, always insert N \" characters. | |
268 | Else just insert \"." | |
269 | (interactive "P") | |
270 | (if (or count (not scribe-electric-quote)) | |
271 | (self-insert-command (prefix-numeric-value count)) | |
272 | (let (lastfore lastback lastquote) | |
273 | (insert | |
274 | (cond | |
275 | ((= (preceding-char) ?\\) ?\") | |
276 | ((bobp) "``") | |
277 | (t | |
278 | (setq lastfore (save-excursion (and (search-backward | |
279 | "``" (- (point) 1000) t) | |
280 | (point))) | |
281 | lastback (save-excursion (and (search-backward | |
282 | "''" (- (point) 1000) t) | |
283 | (point))) | |
284 | lastquote (save-excursion (and (search-backward | |
285 | "\"" (- (point) 100) t) | |
286 | (point)))) | |
287 | (if (not lastquote) | |
288 | (cond ((not lastfore) "``") | |
289 | ((not lastback) "''") | |
290 | ((> lastfore lastback) "''") | |
291 | (t "``")) | |
292 | (cond ((and (not lastback) (not lastfore)) "\"") | |
293 | ((and lastback (not lastfore) (> lastquote lastback)) "\"") | |
294 | ((and lastback (not lastfore) (> lastback lastquote)) "``") | |
295 | ((and lastfore (not lastback) (> lastquote lastfore)) "\"") | |
296 | ((and lastfore (not lastback) (> lastfore lastquote)) "''") | |
297 | ((and (> lastquote lastfore) (> lastquote lastback)) "\"") | |
298 | ((> lastfore lastback) "''") | |
299 | (t "``"))))))))) | |
300 | ||
301 | (defun scribe-parenthesis (count) | |
302 | "If scribe-electric-parenthesis is non-nil, insertion of an open-parenthesis | |
303 | character inserts the following close parenthesis character if the | |
304 | preceding text is of the form @Command." | |
305 | (interactive "P") | |
306 | (self-insert-command (prefix-numeric-value count)) | |
307 | (let (at-command paren-char point-save) | |
308 | (if (or count (not scribe-electric-parenthesis)) | |
309 | nil | |
310 | (save-excursion | |
311 | (forward-char -1) | |
312 | (setq point-save (point)) | |
313 | (skip-chars-backward (concat "^ \n\t\f" scribe-open-parentheses)) | |
314 | (setq at-command (and (equal (following-char) ?@) | |
315 | (/= (point) (1- point-save))))) | |
316 | (if (and at-command | |
317 | (setq paren-char | |
318 | (string-match (regexp-quote | |
319 | (char-to-string (preceding-char))) | |
320 | scribe-open-parentheses))) | |
321 | (save-excursion | |
322 | (insert (aref scribe-close-parentheses paren-char))))))) | |
323 | ||
324 | (provide 'scribe) | |
325 | ||
ab5796a9 | 326 | ;;; arch-tag: 64f454c4-7544-4ea2-9d14-f0b668f2cdc6 |
17cd3083 | 327 | ;;; scribe.el ends here |