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