nroff-mode.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;; GNU Emacs major mode for editing nroff source
  2. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (defvar nroff-mode-abbrev-table nil
  18. "Abbrev table used while in nroff mode.")
  19. (defvar nroff-mode-map nil
  20. "Major mode keymap for nroff-mode buffers")
  21. (if (not nroff-mode-map)
  22. (progn
  23. (setq nroff-mode-map (make-sparse-keymap))
  24. (define-key nroff-mode-map "\t" 'tab-to-tab-stop)
  25. (define-key nroff-mode-map "\es" 'center-line)
  26. (define-key nroff-mode-map "\e?" 'count-text-lines)
  27. (define-key nroff-mode-map "\n" 'electric-nroff-newline)
  28. (define-key nroff-mode-map "\en" 'forward-text-line)
  29. (define-key nroff-mode-map "\ep" 'backward-text-line)))
  30. (defun nroff-mode ()
  31. "Major mode for editing text intended for nroff to format.
  32. \\{nroff-mode-map}
  33. Turning on Nroff mode runs text-mode-hook, then nroff-mode-hook.
  34. Also, try nroff-electric-mode, for automatically inserting
  35. closing requests for requests that are used in matched pairs."
  36. (interactive)
  37. (kill-all-local-variables)
  38. (use-local-map nroff-mode-map)
  39. (setq mode-name "Nroff")
  40. (setq major-mode 'nroff-mode)
  41. (set-syntax-table text-mode-syntax-table)
  42. (setq local-abbrev-table nroff-mode-abbrev-table)
  43. (make-local-variable 'nroff-electric-mode)
  44. ;; now define a bunch of variables for use by commands in this mode
  45. (make-local-variable 'page-delimiter)
  46. (setq page-delimiter "^\\.bp")
  47. (make-local-variable 'paragraph-start)
  48. (setq paragraph-start (concat "^\\.\\|" paragraph-start))
  49. (make-local-variable 'paragraph-separate)
  50. (setq paragraph-separate (concat "^\\.\\|" paragraph-separate))
  51. ;; comment syntax added by mit-erl!gildea 18 Apr 86
  52. (make-local-variable 'comment-start)
  53. (setq comment-start "\\\" ")
  54. (make-local-variable 'comment-start-skip)
  55. (setq comment-start-skip "\\\\\"[ \t]*")
  56. (make-local-variable 'comment-column)
  57. (setq comment-column 24)
  58. (make-local-variable 'comment-indent-hook)
  59. (setq comment-indent-hook 'nroff-comment-indent)
  60. (run-hooks 'text-mode-hook 'nroff-mode-hook))
  61. ;;; Compute how much to indent a comment in nroff/troff source.
  62. ;;; By mit-erl!gildea April 86
  63. (defun nroff-comment-indent ()
  64. "Compute indent for an nroff/troff comment.
  65. Puts a full-stop before comments on a line by themselves."
  66. (let ((pt (point)))
  67. (unwind-protect
  68. (progn
  69. (skip-chars-backward " \t")
  70. (if (bolp)
  71. (progn
  72. (setq pt (1+ pt))
  73. (insert ?.)
  74. 1)
  75. (if (save-excursion
  76. (backward-char 1)
  77. (looking-at "^\\."))
  78. 1
  79. (max comment-column
  80. (* 8 (/ (+ (current-column)
  81. 9) 8)))))) ; add 9 to ensure at least two blanks
  82. (goto-char pt))))
  83. (defun count-text-lines (start end &optional print)
  84. "Count lines in region, except for nroff request lines.
  85. All lines not starting with a period are counted up.
  86. Interactively, print result in echo area.
  87. Noninteractively, return number of non-request lines from START to END."
  88. (interactive "r\np")
  89. (if print
  90. (message "Region has %d text lines" (count-text-lines start end))
  91. (save-excursion
  92. (save-restriction
  93. (narrow-to-region start end)
  94. (goto-char (point-min))
  95. (- (buffer-size) (forward-text-line (buffer-size)))))))
  96. (defun forward-text-line (&optional cnt)
  97. "Go forward one nroff text line, skipping lines of nroff requests.
  98. An argument is a repeat count; if negative, move backward."
  99. (interactive "p")
  100. (if (not cnt) (setq cnt 1))
  101. (while (and (> cnt 0) (not (eobp)))
  102. (forward-line 1)
  103. (while (and (not (eobp)) (looking-at "\\.."))
  104. (forward-line 1))
  105. (setq cnt (- cnt 1)))
  106. (while (and (< cnt 0) (not (bobp)))
  107. (forward-line -1)
  108. (while (and (not (bobp))
  109. (looking-at "\\.."))
  110. (forward-line -1))
  111. (setq cnt (+ cnt 1)))
  112. cnt)
  113. (defun backward-text-line (&optional cnt)
  114. "Go backward one nroff text line, skipping lines of nroff requests.
  115. An argument is a repeat count; negative means move forward."
  116. (interactive "p")
  117. (forward-text-line (- cnt)))
  118. (defconst nroff-brace-table
  119. '((".(b" . ".)b")
  120. (".(l" . ".)l")
  121. (".(q" . ".)q")
  122. (".(c" . ".)c")
  123. (".(x" . ".)x")
  124. (".(z" . ".)z")
  125. (".(d" . ".)d")
  126. (".(f" . ".)f")
  127. (".LG" . ".NL")
  128. (".SM" . ".NL")
  129. (".LD" . ".DE")
  130. (".CD" . ".DE")
  131. (".BD" . ".DE")
  132. (".DS" . ".DE")
  133. (".FS" . ".FE")
  134. (".KS" . ".KE")
  135. (".KF" . ".KE")
  136. (".RS" . ".RE")
  137. (".TS" . ".TE")
  138. (".EQ" . ".EN")
  139. (".PS" . ".PE")
  140. (".na" . ".ad b")
  141. (".nf" . ".fi")
  142. (".de" . "..")))
  143. (defun electric-nroff-newline (arg)
  144. "Insert newline for nroff mode; special if electric-nroff mode.
  145. In electric-nroff-mode, if ending a line containing an nroff opening request,
  146. automatically inserts the matching closing request after point."
  147. (interactive "P")
  148. (let ((completion (save-excursion
  149. (beginning-of-line)
  150. (and (null arg)
  151. nroff-electric-mode
  152. (<= (point) (- (point-max) 3))
  153. (cdr (assoc (buffer-substring (point)
  154. (+ 3 (point)))
  155. nroff-brace-table))))))
  156. (if (null completion)
  157. (newline (prefix-numeric-value arg))
  158. (save-excursion
  159. (insert "\n\n" completion "\n"))
  160. (forward-char 1))))
  161. (defun electric-nroff-mode (arg)
  162. "Toggle nroff-electric-newline minor mode
  163. Nroff-electric-newline forces emacs to check for an nroff
  164. request at the beginning of the line, and insert the
  165. matching closing request if necessary.
  166. This command toggles that mode (off->on, on->off),
  167. with an argument, turns it on iff arg is positive, otherwise off."
  168. (interactive "P")
  169. (or (eq major-mode 'nroff-mode) (error "Must be in nroff mode"))
  170. (or (assq 'nroff-electric-mode minor-mode-alist)
  171. (setq minor-mode-alist (append minor-mode-alist
  172. (list '(nroff-electric-mode
  173. " Electric")))))
  174. (setq nroff-electric-mode
  175. (cond ((null arg) (null nroff-electric-mode))
  176. (t (> (prefix-numeric-value arg) 0)))))