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