1 ;;; nroff-mode.el --- GNU Emacs major mode for editing nroff source
3 ;; Copyright (C) 1985, 1986, 1994, 1995 Free Software Foundation, Inc.
8 ;; This file is part of XEmacs.
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)
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.
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
25 ;;; Synched up with: FSF 19.34.
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.
35 ;; Paragraph filling and line-counting currently don't respect comments,
40 (defvar nroff-mode-abbrev-table nil
41 "Abbrev table used while in nroff mode.")
42 (define-abbrev-table 'nroff-mode-abbrev-table ())
44 (defvar nroff-mode-map nil
45 "Major mode keymap for nroff mode.")
46 (if (not nroff-mode-map)
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)))
56 (defvar nroff-mode-syntax-table nil
57 "Syntax table used while in nroff mode.")
59 (defvar nroff-font-lock-keywords
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
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
74 '("[f*n]*\\[.+]" ; some groff extensions
75 "(.." ; two chars after (
76 "[^(\"]" ; single char escape
80 "Font-lock highlighting control in nroff-mode.")
84 "Major mode for editing text intended for nroff to format.
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."
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
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
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))
132 (defun nroff-outline-level ()
134 (looking-at outline-regexp)
135 (skip-chars-forward ".H ")
136 (string-to-int (buffer-substring (point) (+ 1 (point))))))
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."
146 (skip-chars-backward " \t")
154 (looking-at "^[.']"))
157 (* 8 (/ (+ (current-column)
158 9) 8)))))) ; add 9 to ensure at least two blanks
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."
168 (message "Region has %d text lines" (count-text-lines start end))
171 (narrow-to-region start end)
172 (goto-char (point-min))
173 (- (buffer-size) (forward-text-line (buffer-size)))))))
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."
179 (if (not cnt) (setq cnt 1))
180 (while (and (> cnt 0) (not (eobp)))
182 (while (and (not (eobp)) (looking-at "[.']."))
184 (setq cnt (- cnt 1)))
185 (while (and (< cnt 0) (not (bobp)))
187 (while (and (not (bobp))
188 (looking-at "[.']."))
190 (setq cnt (+ cnt 1)))
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."
197 (forward-text-line (- cnt)))
199 (defconst nroff-brace-table
230 (".G1" . ".G2") ; grap
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."
240 (let ((completion (save-excursion
244 (<= (point) (- (point-max) 3))
245 (cdr (assoc (buffer-substring (point)
247 nroff-brace-table)))))
248 (needs-nl (not (looking-at "[ \t]*$"))))
249 (if (null completion)
250 (newline (prefix-numeric-value arg))
252 (insert "\n\n" completion)
253 (if needs-nl (insert "\n")))
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."
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
270 (setq nroff-electric-mode
271 (cond ((null arg) (null nroff-electric-mode))
272 (t (> (prefix-numeric-value arg) 0)))))
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
280 ;; perverse variable name.
282 (add-minor-mode 'nroff-electric-mode " Electric" nil nil 'electric-nroff-mode)
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))))
289 ;;; nroff-mode.el ends here