Initial Commit
[packages] / xemacs-packages / text-modes / scribe.el
1 ;;; scribe.el --- scribe mode, and its idiosyncratic commands.
2 ;; Keywords: wp
3
4 ;; Copyright (C) 1985 Free Software Foundation, Inc.
5
6 ;; This file is part of XEmacs.
7
8 ;; GNU Emacs is distributed in the hope that it will be useful,
9 ;; but without any warranty.  No author or distributor
10 ;; accepts responsibility to anyone for the consequences of using it
11 ;; or for whether it serves any particular purpose or works at all,
12 ;; unless he says so in writing.
13
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; document "GNU Emacs copying permission notice".   An exact copy
17 ;; of the document is supposed to have been given to you along with
18 ;; GNU Emacs so that you can know how you may redistribute it all.
19 ;; It should be in a file named COPYING.  Among other things, the
20 ;; copyright notice and this notice must be preserved on all copies.
21
22 (defgroup scribe nil
23   "Scribe mode, and its idiosyncratic commands."
24   :group 'wp)
25
26
27 (defvar scribe-mode-syntax-table nil
28   "Syntax table used while in scribe mode.")
29
30 (defvar scribe-mode-abbrev-table nil
31   "Abbrev table used while in scribe mode.")
32
33 (defcustom scribe-fancy-paragraphs nil
34   "*Non-NIL makes Scribe mode use a different style of paragraph separation."
35   :type 'boolean
36   :group 'scribe)
37
38 (defcustom scribe-electric-quote nil
39   "*Non-NIL makes insert of double quote use `` or '' depending on context."
40   :type 'boolean
41   :group 'scribe)
42
43 (defcustom scribe-electric-parenthesis nil
44   "*Non-NIL makes parenthesis char ( (]}> ) automatically insert its close
45 if typed after an @Command form."
46   :type 'boolean
47   :group 'scribe)
48
49 (defconst scribe-open-parentheses "[({<"
50   "Open parenthesis characters for Scribe.")
51
52 (defconst scribe-close-parentheses "])}>"
53   "Close parenthesis characters for Scribe.  These should match up with
54 scribe-open-parenthesis.")
55
56 (if (null scribe-mode-syntax-table)
57     (let ((st (syntax-table)))
58       (unwind-protect
59        (progn
60         (setq scribe-mode-syntax-table (copy-syntax-table
61                                         text-mode-syntax-table))
62         (set-syntax-table scribe-mode-syntax-table)
63         (modify-syntax-entry ?\" "    ")
64         (modify-syntax-entry ?\\ "    ")
65         (modify-syntax-entry ?@ "w   ")
66         (modify-syntax-entry ?< "(>  ")
67         (modify-syntax-entry ?> ")<  ")
68         (modify-syntax-entry ?[ "(]  ")
69         (modify-syntax-entry ?] ")[  ")
70         (modify-syntax-entry ?{ "(}  ")
71         (modify-syntax-entry ?} "){  ")
72         (modify-syntax-entry ?' "w   "))
73        (set-syntax-table st))))
74
75 (defvar scribe-mode-map nil)
76
77 (if scribe-mode-map
78     nil
79   (setq scribe-mode-map (make-sparse-keymap))
80   (define-key scribe-mode-map "\t" 'scribe-tab)
81   (define-key scribe-mode-map "\e\t" 'tab-to-tab-stop)
82   (define-key scribe-mode-map "\es" 'center-line)
83   (define-key scribe-mode-map "\e}" 'up-list)
84   (define-key scribe-mode-map "\eS" 'center-paragraph)
85   (define-key scribe-mode-map "\"" 'scribe-insert-quote)
86   (define-key scribe-mode-map "(" 'scribe-parenthesis)
87   (define-key scribe-mode-map "[" 'scribe-parenthesis)
88   (define-key scribe-mode-map "{" 'scribe-parenthesis)
89   (define-key scribe-mode-map "<" 'scribe-parenthesis)
90   (define-key scribe-mode-map "\^cc" 'scribe-chapter)
91   (define-key scribe-mode-map "\^cS" 'scribe-section)
92   (define-key scribe-mode-map "\^cs" 'scribe-subsection)
93   (define-key scribe-mode-map "\^ce" 'scribe-insert-environment)
94   (define-key scribe-mode-map "\^c\^e" 'scribe-bracket-region-be)
95   (define-key scribe-mode-map "\^c[" 'scribe-begin)
96   (define-key scribe-mode-map "\^c]" 'scribe-end)
97   (define-key scribe-mode-map "\^ci" 'scribe-italicize-word)
98   (define-key scribe-mode-map "\^cb" 'scribe-bold-word)
99   (define-key scribe-mode-map "\^cu" 'scribe-underline-word))
100
101 ;;;###autoload
102 (defun scribe-mode ()
103   "Major mode for editing files of Scribe (a text formatter) source.
104 Scribe-mode is similar text-mode, with a few extra commands added.
105 \\{scribe-mode-map}
106
107 Interesting variables:
108
109 scribe-fancy-paragraphs
110   Non-nil makes Scribe mode use a different style of paragraph separation.
111
112 scribe-electric-quote
113   Non-nil makes insert of double quote use `` or '' depending on context.
114
115 scribe-electric-parenthesis
116   Non-nil makes an open-parenthesis char (one of `([<{')
117   automatically insert its close if typed after an @Command form."
118   (interactive)
119   (kill-all-local-variables)
120   (use-local-map scribe-mode-map)
121   (setq mode-name "Scribe")
122   (setq major-mode 'scribe-mode)
123   (define-abbrev-table 'scribe-mode-abbrev-table ())
124   (setq local-abbrev-table scribe-mode-abbrev-table)
125   (make-local-variable 'comment-start)
126   (setq comment-start "@Comment[")
127   (make-local-variable 'comment-start-skip)
128   (setq comment-start-skip (concat "@Comment[" scribe-open-parentheses "]"))
129   (make-local-variable 'comment-column)
130   (setq comment-column 0)
131   (make-local-variable 'comment-end)
132   (setq comment-end "]")
133   (make-local-variable 'paragraph-start)
134   (setq paragraph-start (concat "\\(^[\n\f]\\)\\|\\(^@\\w+["
135                                  scribe-open-parentheses
136                                 "].*["
137                                  scribe-close-parentheses
138                                 "]$\\)"))
139   (make-local-variable 'paragraph-separate)
140   (setq paragraph-separate (if scribe-fancy-paragraphs
141                                paragraph-start "^$"))
142   (make-local-variable 'compile-command)
143   (setq compile-command (concat "scribe " (buffer-file-name)))
144   (set-syntax-table scribe-mode-syntax-table)
145   (run-hooks 'text-mode-hook 'scribe-mode-hook))
146
147 (defun scribe-tab ()
148   (interactive)
149   (insert "@\\"))
150
151 ;; This algorithm could probably be improved somewhat.
152 ;;  Right now, it loses seriously...
153
154 (defun scribe ()
155   "Run Scribe on the current buffer."
156   (interactive)
157   (call-interactively 'compile))
158
159 (defun scribe-envelop-word (string count)
160   "Surround current word with Scribe construct @STRING[...].  COUNT
161 specifies how many words to surround.  A negative count means to skip 
162 backward."
163   (let ((spos (point)) (epos (point)) (ccoun 0))
164     (if (not (zerop count))
165         (progn (if (= (char-syntax (preceding-char)) ?w)
166                    (forward-sexp (min -1 count)))
167                (setq spos (point))
168                (if (looking-at (concat "@\\w[" scribe-open-parentheses "]"))
169                    (forward-char 2)
170                  (goto-char epos)
171                  (skip-chars-backward "\\W")
172                  (forward-char -1))
173                (forward-sexp (max count 1))
174                (setq epos (point))))
175     (goto-char spos)
176     (while (and (< ccoun (length scribe-open-parentheses))
177                 (save-excursion
178                   (or (search-forward (char-to-string
179                                        (aref scribe-open-parentheses ccoun))
180                                       epos t)
181                       (search-forward (char-to-string
182                                        (aref scribe-close-parentheses ccoun))
183                                       epos t)))
184                 (setq ccoun (1+ ccoun))))
185     (if (>= ccoun (length scribe-open-parentheses))
186         (progn (goto-char epos)
187                (insert "@end(" string ")")
188                (goto-char spos)
189                (insert "@begin(" string ")"))
190       (goto-char epos)
191       (insert (aref scribe-close-parentheses ccoun))
192       (goto-char spos)
193       (insert "@" string (aref scribe-open-parentheses ccoun))
194       (goto-char epos)
195       (forward-char 3)
196       (skip-chars-forward scribe-close-parentheses))))
197
198 (defun scribe-underline-word (count)
199   "Underline COUNT words around point by means of Scribe constructs."
200   (interactive "p")
201   (scribe-envelop-word "u" count))
202
203 (defun scribe-bold-word (count)
204   "Boldface COUNT words around point by means of Scribe constructs."
205   (interactive "p")
206   (scribe-envelop-word "b" count))
207
208 (defun scribe-italicize-word (count)
209   "Italicize COUNT words around point by means of Scribe constructs."
210   (interactive "p")
211   (scribe-envelop-word "i" count))
212
213 (defun scribe-begin ()
214   (interactive)
215   (insert "\n")
216   (forward-char -1)
217   (scribe-envelop-word "Begin" 0)
218   (re-search-forward (concat "[" scribe-open-parentheses "]")))
219
220 (defun scribe-end ()
221   (interactive)
222   (insert "\n")
223   (forward-char -1)
224   (scribe-envelop-word "End" 0)
225   (re-search-forward (concat "[" scribe-open-parentheses "]")))
226
227 (defun scribe-chapter ()
228   (interactive)
229   (insert "\n")
230   (forward-char -1)
231   (scribe-envelop-word "Chapter" 0)
232   (re-search-forward (concat "[" scribe-open-parentheses "]")))
233
234 (defun scribe-section ()
235   (interactive)
236   (insert "\n")
237   (forward-char -1)
238   (scribe-envelop-word "Section" 0)
239   (re-search-forward (concat "[" scribe-open-parentheses "]")))
240
241 (defun scribe-subsection ()
242   (interactive)
243   (insert "\n")
244   (forward-char -1)
245   (scribe-envelop-word "SubSection" 0)
246   (re-search-forward (concat "[" scribe-open-parentheses "]")))
247
248 (defun scribe-bracket-region-be (env min max)
249   (interactive "sEnvironment: \nr")
250   (save-excursion
251     (goto-char max)
252     (insert "@end(" env ")\n")
253     (goto-char min)
254     (insert "@begin(" env ")\n")))
255
256 (defun scribe-insert-environment (env)
257   (interactive "sEnvironment: ")
258   (scribe-bracket-region-be env (point) (point))
259   (forward-line 1)
260   (insert ?\n)
261   (forward-char -1))
262
263 (defun scribe-insert-quote (count)
264   "If scribe-electric-quote is non-NIL, insert ``, '' or \" according
265 to preceding character.  With numeric arg N, always insert N \" characters.
266 Else just insert \"."
267   (interactive "P")
268   (if (or count (not scribe-electric-quote))
269       (self-insert-command (prefix-numeric-value count))
270     (let (lastfore lastback lastquote)
271       (insert
272        (cond
273         ((= (preceding-char) ?\\) ?\")
274         ((bobp) "``")
275         (t
276          (setq lastfore (save-excursion (and (search-backward
277                                               "``" (- (point) 1000) t)
278                                              (point)))
279                lastback (save-excursion (and (search-backward
280                                               "''" (- (point) 1000) t)
281                                              (point)))
282                lastquote (save-excursion (and (search-backward
283                                                "\"" (- (point) 100) t)
284                                               (point))))
285          (if (not lastquote)
286              (cond ((not lastfore) "``")
287                    ((not lastback) "''")
288                    ((> lastfore lastback) "''")
289                    (t "``"))
290            (cond ((and (not lastback) (not lastfore)) "\"")
291                  ((and lastback (not lastfore) (> lastquote lastback)) "\"")
292                  ((and lastback (not lastfore) (> lastback lastquote)) "``")
293                  ((and lastfore (not lastback) (> lastquote lastfore)) "\"")
294                  ((and lastfore (not lastback) (> lastfore lastquote)) "''")
295                  ((and (> lastquote lastfore) (> lastquote lastback)) "\"")
296                  ((> lastfore lastback) "''")
297                  (t "``")))))))))
298
299 (defun scribe-parenthesis (count)
300   "If scribe-electric-parenthesis is non-NIL, insertion of an open-parenthesis
301 character inserts the following close parenthesis character if the
302 preceding text is of the form @Command."
303   (interactive "P")
304   (self-insert-command (prefix-numeric-value count))
305   (let (at-command paren-char point-save)
306     (if (or count (not scribe-electric-parenthesis))
307         nil
308       (save-excursion
309         (forward-char -1)
310         (setq point-save (point))
311         (skip-chars-backward (concat "^ \n\t\f" scribe-open-parentheses))
312         (setq at-command (and (equal (following-char) ?@)
313                               (/= (point) (1- point-save)))))
314       (if (and at-command
315                (setq paren-char
316                      (string-match (regexp-quote
317                                     (char-to-string (preceding-char)))
318                                    scribe-open-parentheses)))
319           (save-excursion
320             (insert (aref scribe-close-parentheses paren-char)))))))
321
322 ;; XEmacs addition
323 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.mss\\'" . scribe-mode))