*** empty log message ***
[bpt/emacs.git] / lisp / textmodes / nroff-mode.el
CommitLineData
6594deb0 1;;; nroff-mode.el --- GNU Emacs major mode for editing nroff source
4821e2af
ER
2
3;; Maintainer: FSF
e5167999 4;; Last-Modified: 09 May 1991
d7b4d18f 5;; Keywords: wp
4821e2af 6
a2535589
JA
7;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
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
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
a2535589
JA
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
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
4821e2af
ER
25;;; Code:
26
a2535589
JA
27(defvar nroff-mode-abbrev-table nil
28 "Abbrev table used while in nroff mode.")
29
30(defvar nroff-mode-map nil
6d1f885f 31 "Major mode keymap for nroff mode.")
a2535589
JA
32(if (not nroff-mode-map)
33 (progn
34 (setq nroff-mode-map (make-sparse-keymap))
35 (define-key nroff-mode-map "\t" 'tab-to-tab-stop)
36 (define-key nroff-mode-map "\es" 'center-line)
37 (define-key nroff-mode-map "\e?" 'count-text-lines)
38 (define-key nroff-mode-map "\n" 'electric-nroff-newline)
39 (define-key nroff-mode-map "\en" 'forward-text-line)
40 (define-key nroff-mode-map "\ep" 'backward-text-line)))
41
6594deb0 42;;;###autoload
a2535589
JA
43(defun nroff-mode ()
44 "Major mode for editing text intended for nroff to format.
45\\{nroff-mode-map}
6d1f885f
BP
46Turning on Nroff mode runs `text-mode-hook', then `nroff-mode-hook'.
47Also, try `nroff-electric-mode', for automatically inserting
a2535589
JA
48closing requests for requests that are used in matched pairs."
49 (interactive)
50 (kill-all-local-variables)
51 (use-local-map nroff-mode-map)
52 (setq mode-name "Nroff")
53 (setq major-mode 'nroff-mode)
54 (set-syntax-table text-mode-syntax-table)
55 (setq local-abbrev-table nroff-mode-abbrev-table)
56 (make-local-variable 'nroff-electric-mode)
57 ;; now define a bunch of variables for use by commands in this mode
58 (make-local-variable 'page-delimiter)
59 (setq page-delimiter "^\\.\\(bp\\|SK\\|OP\\)")
60 (make-local-variable 'paragraph-start)
61 (setq paragraph-start (concat "^[.']\\|" paragraph-start))
62 (make-local-variable 'paragraph-separate)
63 (setq paragraph-separate (concat "^[.']\\|" paragraph-separate))
64 ;; comment syntax added by mit-erl!gildea 18 Apr 86
65 (make-local-variable 'comment-start)
66 (setq comment-start "\\\" ")
67 (make-local-variable 'comment-start-skip)
68 (setq comment-start-skip "\\\\\"[ \t]*")
69 (make-local-variable 'comment-column)
70 (setq comment-column 24)
71 (make-local-variable 'comment-indent-hook)
72 (setq comment-indent-hook 'nroff-comment-indent)
73 (run-hooks 'text-mode-hook 'nroff-mode-hook))
74
75;;; Compute how much to indent a comment in nroff/troff source.
76;;; By mit-erl!gildea April 86
77(defun nroff-comment-indent ()
78 "Compute indent for an nroff/troff comment.
79Puts a full-stop before comments on a line by themselves."
80 (let ((pt (point)))
81 (unwind-protect
82 (progn
83 (skip-chars-backward " \t")
84 (if (bolp)
85 (progn
86 (setq pt (1+ pt))
87 (insert ?.)
88 1)
89 (if (save-excursion
90 (backward-char 1)
91 (looking-at "^[.']"))
92 1
93 (max comment-column
94 (* 8 (/ (+ (current-column)
95 9) 8)))))) ; add 9 to ensure at least two blanks
96 (goto-char pt))))
97
98(defun count-text-lines (start end &optional print)
99 "Count lines in region, except for nroff request lines.
100All lines not starting with a period are counted up.
101Interactively, print result in echo area.
102Noninteractively, return number of non-request lines from START to END."
103 (interactive "r\np")
104 (if print
105 (message "Region has %d text lines" (count-text-lines start end))
106 (save-excursion
107 (save-restriction
108 (narrow-to-region start end)
109 (goto-char (point-min))
110 (- (buffer-size) (forward-text-line (buffer-size)))))))
111
112(defun forward-text-line (&optional cnt)
113 "Go forward one nroff text line, skipping lines of nroff requests.
114An argument is a repeat count; if negative, move backward."
115 (interactive "p")
116 (if (not cnt) (setq cnt 1))
117 (while (and (> cnt 0) (not (eobp)))
118 (forward-line 1)
119 (while (and (not (eobp)) (looking-at "[.']."))
120 (forward-line 1))
121 (setq cnt (- cnt 1)))
122 (while (and (< cnt 0) (not (bobp)))
123 (forward-line -1)
124 (while (and (not (bobp))
125 (looking-at "[.']."))
126 (forward-line -1))
127 (setq cnt (+ cnt 1)))
128 cnt)
129
130(defun backward-text-line (&optional cnt)
131 "Go backward one nroff text line, skipping lines of nroff requests.
132An argument is a repeat count; negative means move forward."
133 (interactive "p")
134 (forward-text-line (- cnt)))
135
136(defconst nroff-brace-table
137 '((".(b" . ".)b")
138 (".(l" . ".)l")
139 (".(q" . ".)q")
140 (".(c" . ".)c")
141 (".(x" . ".)x")
142 (".(z" . ".)z")
143 (".(d" . ".)d")
144 (".(f" . ".)f")
145 (".LG" . ".NL")
146 (".SM" . ".NL")
147 (".LD" . ".DE")
148 (".CD" . ".DE")
149 (".BD" . ".DE")
150 (".DS" . ".DE")
151 (".DF" . ".DE")
152 (".FS" . ".FE")
153 (".KS" . ".KE")
154 (".KF" . ".KE")
155 (".LB" . ".LE")
156 (".AL" . ".LE")
157 (".BL" . ".LE")
158 (".DL" . ".LE")
159 (".ML" . ".LE")
160 (".RL" . ".LE")
161 (".VL" . ".LE")
162 (".RS" . ".RE")
163 (".TS" . ".TE")
164 (".EQ" . ".EN")
165 (".PS" . ".PE")
166 (".BS" . ".BE")
167 (".G1" . ".G2") ; grap
168 (".na" . ".ad b")
169 (".nf" . ".fi")
170 (".de" . "..")))
171
172(defun electric-nroff-newline (arg)
173 "Insert newline for nroff mode; special if electric-nroff mode.
6d1f885f 174In `electric-nroff-mode', if ending a line containing an nroff opening request,
a2535589
JA
175automatically inserts the matching closing request after point."
176 (interactive "P")
177 (let ((completion (save-excursion
178 (beginning-of-line)
179 (and (null arg)
180 nroff-electric-mode
181 (<= (point) (- (point-max) 3))
182 (cdr (assoc (buffer-substring (point)
183 (+ 3 (point)))
184 nroff-brace-table)))))
185 (needs-nl (not (looking-at "[ \t]*$"))))
186 (if (null completion)
187 (newline (prefix-numeric-value arg))
188 (save-excursion
189 (insert "\n\n" completion)
190 (if needs-nl (insert "\n")))
191 (forward-char 1))))
192
193(defun electric-nroff-mode (&optional arg)
6d1f885f
BP
194 "Toggle `nroff-electric-newline' minor mode.
195`nroff-electric-newline' forces Emacs to check for an nroff request at the
196beginning of the line, and insert the matching closing request if necessary.
197This command toggles that mode (off->on, on->off), with an argument,
198turns it on iff arg is positive, otherwise off."
a2535589
JA
199 (interactive "P")
200 (or (eq major-mode 'nroff-mode) (error "Must be in nroff mode"))
201 (or (assq 'nroff-electric-mode minor-mode-alist)
202 (setq minor-mode-alist (append minor-mode-alist
203 (list '(nroff-electric-mode
204 " Electric")))))
205 (setq nroff-electric-mode
206 (cond ((null arg) (null nroff-electric-mode))
207 (t (> (prefix-numeric-value arg) 0)))))
208
6594deb0 209;;; nroff-mode.el ends here