Initial Commit
[packages] / xemacs-packages / text-modes / nroff-mode.el
1 ;;; nroff-mode.el --- GNU Emacs major mode for editing nroff source
2
3 ;; Copyright (C) 1985, 1986, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: wp
7
8 ;; This file is part of XEmacs.
9
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 ;; 02111-1307, USA.
24
25 ;;; Synched up with: FSF 19.34.
26
27 ;;; Commentary:
28
29 ;; This package is a major mode for editing nroff source code.  It knows
30 ;; about various nroff constructs, ms, mm, and me macros, and will fill
31 ;; and indent paragraphs properly in their presence.  It also includes
32 ;; a command to count text lines (excluding nroff constructs), a command
33 ;; to center a line, and movement commands that know how to skip macros.
34
35 ;; Paragraph filling and line-counting currently don't respect comments,
36 ;; as they should.
37
38 ;;; Code:
39
40 (defvar nroff-mode-abbrev-table nil
41   "Abbrev table used while in nroff mode.")
42 (define-abbrev-table 'nroff-mode-abbrev-table ())
43
44 (defvar nroff-mode-map nil
45      "Major mode keymap for nroff mode.")
46 (if (not nroff-mode-map)
47     (progn
48       (setq nroff-mode-map (make-sparse-keymap))
49       (define-key nroff-mode-map "\t"  'tab-to-tab-stop)
50       (define-key nroff-mode-map "\es" 'center-line)
51       (define-key nroff-mode-map "\e?" 'count-text-lines)
52       (define-key nroff-mode-map "\n"  'electric-nroff-newline)
53       (define-key nroff-mode-map "\en" 'forward-text-line)
54       (define-key nroff-mode-map "\ep" 'backward-text-line)))
55
56 (defvar nroff-mode-syntax-table nil
57   "Syntax table used while in nroff mode.")
58
59 (defvar nroff-font-lock-keywords
60   (list
61    ;; Directives are . or ' at start of line, followed by
62    ;; optional whitespace, then command (which my be longer than
63    ;; 2 characters in groff).  Perhaps the arguments should be
64    ;; fontified as well.
65    "^[.']\\s-*\\sw+"
66    ;; There are numerous groff escapes; the following get things
67    ;; like \-, \(em (standard troff) and \f[bar] (groff
68    ;; variants).  This won't currently do groff's \A'foo' and
69    ;; the like properly.  One might expect it to highlight an escape's
70    ;; arguments in common cases, like \f.
71    (concat "\\\\"                       ; backslash
72            "\\("                        ; followed by various possibilities
73          (mapconcat 'identity
74                     '("[f*n]*\\[.+]"    ; some groff extensions
75                       "(.."             ; two chars after (
76                       "[^(\"]"          ; single char escape
77                       ) "\\|")
78          "\\)")
79    )
80   "Font-lock highlighting control in nroff-mode.")
81
82 ;;;###autoload
83 (defun nroff-mode ()
84   "Major mode for editing text intended for nroff to format.
85 \\{nroff-mode-map}
86 Turning on Nroff mode runs `text-mode-hook', then `nroff-mode-hook'.
87 Also, try `nroff-electric-mode', for automatically inserting
88 closing requests for requests that are used in matched pairs."
89   (interactive)
90   (kill-all-local-variables)
91   (use-local-map nroff-mode-map)
92   (setq mode-name "Nroff")
93   (setq major-mode 'nroff-mode)
94   (if nroff-mode-syntax-table
95       ()
96     (setq nroff-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
97     ;; " isn't given string quote syntax in text-mode but it
98     ;; (arguably) should be for use round nroff arguments (with ` and
99     ;; ' used otherwise).
100     (modify-syntax-entry ?\" "\"  2" nroff-mode-syntax-table)
101     ;; Comments are delimited by \" and newline.
102     (modify-syntax-entry ?\\ "\\  1" nroff-mode-syntax-table)
103     (modify-syntax-entry ?\n ">  1" nroff-mode-syntax-table))
104   (set-syntax-table nroff-mode-syntax-table)
105   (make-local-variable 'font-lock-defaults)
106   (setq font-lock-defaults '(nroff-font-lock-keywords nil t))
107   (setq local-abbrev-table nroff-mode-abbrev-table)
108   (make-local-variable 'nroff-electric-mode)
109   (setq nroff-electric-mode nil)
110   (make-local-variable 'outline-regexp)
111   (setq outline-regexp "\\.H[ ]+[1-7]+ ")
112   (make-local-variable 'outline-level)
113   (setq outline-level 'nroff-outline-level)
114   ;; now define a bunch of variables for use by commands in this mode
115   (make-local-variable 'page-delimiter)
116   (setq page-delimiter "^\\.\\(bp\\|SK\\|OP\\)")
117   (make-local-variable 'paragraph-start)
118   (setq paragraph-start (concat "[.']\\|" paragraph-start))
119   (make-local-variable 'paragraph-separate)
120   (setq paragraph-separate (concat "[.']\\|" paragraph-separate))
121   ;; comment syntax added by mit-erl!gildea 18 Apr 86
122   (make-local-variable 'comment-start)
123   (setq comment-start "\\\" ")
124   (make-local-variable 'comment-start-skip)
125   (setq comment-start-skip "\\\\\"[ \t]*")
126   (make-local-variable 'comment-column)
127   (setq comment-column 24)
128   (make-local-variable 'comment-indent-function)
129   (setq comment-indent-function 'nroff-comment-indent)
130   (run-hooks 'text-mode-hook 'nroff-mode-hook))
131
132 (defun nroff-outline-level ()
133   (save-excursion
134     (looking-at outline-regexp)
135     (skip-chars-forward ".H ")
136     (string-to-int (buffer-substring (point) (+ 1 (point))))))
137
138 ;;; Compute how much to indent a comment in nroff/troff source.
139 ;;; By mit-erl!gildea April 86
140 (defun nroff-comment-indent ()
141   "Compute indent for an nroff/troff comment.
142 Puts a full-stop before comments on a line by themselves."
143   (let ((pt (point)))
144     (unwind-protect
145         (progn
146           (skip-chars-backward " \t")
147           (if (bolp)
148               (progn
149                 (setq pt (1+ pt))
150                 (insert ?.)
151                 1)
152             (if (save-excursion
153                   (backward-char 1)
154                   (looking-at "^[.']"))
155                 1
156               (max comment-column
157                    (* 8 (/ (+ (current-column)
158                               9) 8)))))) ; add 9 to ensure at least two blanks
159       (goto-char pt))))
160
161 (defun count-text-lines (start end &optional print)
162   "Count lines in region, except for nroff request lines.
163 All lines not starting with a period are counted up.
164 Interactively, print result in echo area.
165 Noninteractively, return number of non-request lines from START to END."
166   (interactive "r\np")
167   (if print
168       (message "Region has %d text lines" (count-text-lines start end))
169     (save-excursion
170       (save-restriction
171         (narrow-to-region start end)
172         (goto-char (point-min))
173         (- (buffer-size) (forward-text-line (buffer-size)))))))
174
175 (defun forward-text-line (&optional cnt)
176   "Go forward one nroff text line, skipping lines of nroff requests.
177 An argument is a repeat count; if negative, move backward."
178   (interactive "p")
179   (if (not cnt) (setq cnt 1))
180   (while (and (> cnt 0) (not (eobp)))
181     (forward-line 1)
182     (while (and (not (eobp)) (looking-at "[.']."))
183       (forward-line 1))
184     (setq cnt (- cnt 1)))
185   (while (and (< cnt 0) (not (bobp)))
186     (forward-line -1)
187     (while (and (not (bobp))
188                 (looking-at "[.']."))
189       (forward-line -1))
190     (setq cnt (+ cnt 1)))
191   cnt)
192
193 (defun backward-text-line (&optional cnt)
194   "Go backward one nroff text line, skipping lines of nroff requests.
195 An argument is a repeat count; negative means move forward."
196   (interactive "p")
197   (forward-text-line (- cnt)))
198
199 (defconst nroff-brace-table
200   '((".(b" . ".)b")
201     (".(l" . ".)l")
202     (".(q" . ".)q")
203     (".(c" . ".)c")
204     (".(x" . ".)x")
205     (".(z" . ".)z")
206     (".(d" . ".)d")
207     (".(f" . ".)f")
208     (".LG" . ".NL")
209     (".SM" . ".NL")
210     (".LD" . ".DE")
211     (".CD" . ".DE")
212     (".BD" . ".DE")
213     (".DS" . ".DE")
214     (".DF" . ".DE")
215     (".FS" . ".FE")
216     (".KS" . ".KE")
217     (".KF" . ".KE")
218     (".LB" . ".LE")
219     (".AL" . ".LE")
220     (".BL" . ".LE")
221     (".DL" . ".LE")
222     (".ML" . ".LE")
223     (".RL" . ".LE")
224     (".VL" . ".LE")
225     (".RS" . ".RE")
226     (".TS" . ".TE")
227     (".EQ" . ".EN")
228     (".PS" . ".PE")
229     (".BS" . ".BE")
230     (".G1" . ".G2")                     ; grap
231     (".na" . ".ad b")
232     (".nf" . ".fi")
233     (".de" . "..")))
234
235 (defun electric-nroff-newline (arg)
236   "Insert newline for nroff mode; special if electric-nroff mode.
237 In `electric-nroff-mode', if ending a line containing an nroff opening request,
238 automatically inserts the matching closing request after point."
239   (interactive "P")
240   (let ((completion (save-excursion
241                       (beginning-of-line)
242                       (and (null arg)
243                            nroff-electric-mode
244                            (<= (point) (- (point-max) 3))
245                            (cdr (assoc (buffer-substring (point)
246                                                          (+ 3 (point)))
247                                        nroff-brace-table)))))
248         (needs-nl (not (looking-at "[ \t]*$"))))
249     (if (null completion)
250         (newline (prefix-numeric-value arg))
251       (save-excursion
252         (insert "\n\n" completion)
253         (if needs-nl (insert "\n")))
254       (forward-char 1))))
255
256 ;;;###autoload
257 (defun electric-nroff-mode (&optional arg)
258   "Toggle `nroff-electric-newline' minor mode.
259 `nroff-electric-newline' forces Emacs to check for an nroff request at the
260 beginning of the line, and insert the matching closing request if necessary.
261 This command toggles that mode (off->on, on->off), with an argument,
262 turns it on iff arg is positive, otherwise off."
263   (interactive "P")
264   (or (eq major-mode 'nroff-mode) (error "Must be in nroff mode"))
265   ;; XEmacs: see below.
266 ;  (or (assq 'nroff-electric-mode minor-mode-alist)
267 ;      (setq minor-mode-alist (append minor-mode-alist
268 ;                                    (list '(nroff-electric-mode
269 ;                                            " Electric")))))
270   (setq nroff-electric-mode
271         (cond ((null arg) (null nroff-electric-mode))
272               (t (> (prefix-numeric-value arg) 0)))))
273
274 ;;;###autoload
275 (defvar nroff-electric-mode nil
276   "Non-nil if in electric-nroff minor mode.")
277 ;; XEmacs: do it right.  This must come after the defun of
278 ;; electric-nroff-mode so that add-minor-mode will recognize it as a
279 ;; command.
280 ;; perverse variable name.
281 ;;;###autoload
282 (add-minor-mode 'nroff-electric-mode " Electric" nil nil 'electric-nroff-mode)
283
284 ;; XEmacs additions
285 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.m\\(?:[mes]\\|an\\)\\'" . nroff-mode))
286 ;; Need to append this so it won't screw eg. ChangeLog.1 and .scm.[0-9]...
287 ;;;###autoload(setq auto-mode-alist (append auto-mode-alist '(("\\.[123456789]\\'" . nroff-mode))))
288
289 ;;; nroff-mode.el ends here