Some fixes to follow coding conventions in files maintained by FSF.
[bpt/emacs.git] / lisp / textmodes / text-mode.el
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
2
3 ;; Copyright (C) 1985, 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This package provides the fundamental text mode documented in the
27 ;; Emacs user's manual.
28
29 ;;; Code:
30
31 (defcustom text-mode-hook nil
32 "Normal hook run when entering Text mode and many related modes."
33 :type 'hook
34 :options '(turn-on-auto-fill flyspell-mode)
35 :group 'data)
36
37 (defvar text-mode-variant nil
38 "Non-nil if this buffer's major mode is a variant of Text mode.")
39
40 (defvar text-mode-syntax-table nil
41 "Syntax table used while in text mode.")
42
43 (defvar text-mode-abbrev-table nil
44 "Abbrev table used while in text mode.")
45 (define-abbrev-table 'text-mode-abbrev-table ())
46
47 (if text-mode-syntax-table
48 ()
49 (setq text-mode-syntax-table (make-syntax-table))
50 (modify-syntax-entry ?\" ". " text-mode-syntax-table)
51 (modify-syntax-entry ?\\ ". " text-mode-syntax-table)
52 (modify-syntax-entry ?' "w " text-mode-syntax-table))
53
54 (defvar text-mode-map nil
55 "Keymap for Text mode.
56 Many other modes, such as Mail mode, Outline mode and Indented Text mode,
57 inherit all the commands defined in this map.")
58
59 (if text-mode-map
60 ()
61 (setq text-mode-map (make-sparse-keymap))
62 (define-key text-mode-map "\e\t" 'ispell-complete-word)
63 (define-key text-mode-map "\t" 'indent-relative)
64 (define-key text-mode-map "\es" 'center-line)
65 (define-key text-mode-map "\eS" 'center-paragraph))
66
67 \f
68 (defun text-mode ()
69 "Major mode for editing text written for humans to read.
70 In this mode, paragraphs are delimited only by blank or white lines.
71 You can thus get the full benefit of adaptive filling
72 (see the variable `adaptive-fill-mode').
73 \\{text-mode-map}
74 Turning on Text mode runs the normal hook `text-mode-hook'."
75 (interactive)
76 (kill-all-local-variables)
77 (use-local-map text-mode-map)
78 (setq local-abbrev-table text-mode-abbrev-table)
79 (set-syntax-table text-mode-syntax-table)
80 (make-local-variable 'paragraph-start)
81 (setq paragraph-start (concat page-delimiter "\\|[ \t]*$"))
82 (if (eq ?^ (aref paragraph-start 0))
83 (setq paragraph-start (substring paragraph-start 1)))
84 (make-local-variable 'paragraph-separate)
85 (setq paragraph-separate paragraph-start)
86 (make-local-variable 'indent-line-function)
87 (setq indent-line-function 'indent-relative-maybe)
88 (setq mode-name "Text")
89 (setq major-mode 'text-mode)
90 (run-hooks 'text-mode-hook))
91
92 (defun paragraph-indent-text-mode ()
93 "Major mode for editing text, with leading spaces starting a paragraph.
94 In this mode, you do not need blank lines between paragraphs
95 when the first line of the following paragraph starts with whitespace.
96 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
97 Special commands:
98 \\{text-mode-map}
99 Turning on Paragraph-Indent Text mode runs the normal hooks
100 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
101 (interactive)
102 (kill-all-local-variables)
103 (use-local-map text-mode-map)
104 (setq mode-name "Parindent")
105 (setq major-mode 'paragraph-indent-text-mode)
106 (setq local-abbrev-table text-mode-abbrev-table)
107 (set-syntax-table text-mode-syntax-table)
108 (run-hooks 'text-mode-hook 'paragraph-indent-text-mode-hook))
109
110 (defun paragraph-indent-minor-mode ()
111 "Minor mode for editing text, with leading spaces starting a paragraph.
112 In this mode, you do not need blank lines between paragraphs when the
113 first line of the following paragraph starts with whitespace, as with
114 `paragraph-indent-mode'.
115 Turning on Paragraph-Indent minor mode runs the normal hook
116 `paragraph-indent-text-mode-hook'."
117 (interactive)
118 (set (make-local-variable 'paragraph-start)
119 (default-value 'paragraph-start))
120 (set (make-local-variable 'paragraph-separate) paragraph-start)
121 (run-hooks 'paragraph-indent-text-mode-hook))
122
123 (defalias 'indented-text-mode 'text-mode)
124
125 (defun text-mode-hook-identify ()
126 "Mark that this mode has run `text-mode-hook'.
127 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
128 (make-local-variable 'text-mode-variant)
129 (setq text-mode-variant t))
130
131 (add-hook 'text-mode-hook 'text-mode-hook-identify)
132
133 (defun toggle-text-mode-auto-fill ()
134 "Toggle whether to use Auto Fill in Text mode and related modes.
135 This command affects all buffers that use modes related to Text mode,
136 both existing buffers and buffers that you subsequently create."
137 (interactive)
138 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook)))
139 (buffers (buffer-list)))
140 (if enable-mode
141 (add-hook 'text-mode-hook 'turn-on-auto-fill)
142 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
143 (while buffers
144 (with-current-buffer (car buffers)
145 (if text-mode-variant
146 (auto-fill-mode (if enable-mode 1 0))))
147 (setq buffers (cdr buffers)))
148 (message "Auto Fill %s in Text modes"
149 (if enable-mode "enabled" "disabled"))))
150 \f
151 (defun center-paragraph ()
152 "Center each nonblank line in the paragraph at or after point.
153 See `center-line' for more info."
154 (interactive)
155 (save-excursion
156 (forward-paragraph)
157 (or (bolp) (newline 1))
158 (let ((end (point)))
159 (backward-paragraph)
160 (center-region (point) end))))
161
162 (defun center-region (from to)
163 "Center each nonblank line starting in the region.
164 See `center-line' for more info."
165 (interactive "r")
166 (if (> from to)
167 (let ((tem to))
168 (setq to from from tem)))
169 (save-excursion
170 (save-restriction
171 (narrow-to-region from to)
172 (goto-char from)
173 (while (not (eobp))
174 (or (save-excursion (skip-chars-forward " \t") (eolp))
175 (center-line))
176 (forward-line 1)))))
177
178 (defun center-line (&optional nlines)
179 "Center the line point is on, within the width specified by `fill-column'.
180 This means adjusting the indentation so that it equals
181 the distance between the end of the text and `fill-column'.
182 The argument NLINES says how many lines to center."
183 (interactive "P")
184 (if nlines (setq nlines (prefix-numeric-value nlines)))
185 (while (not (eq nlines 0))
186 (save-excursion
187 (let ((lm (current-left-margin))
188 line-length)
189 (beginning-of-line)
190 (delete-horizontal-space)
191 (end-of-line)
192 (delete-horizontal-space)
193 (setq line-length (current-column))
194 (if (> (- fill-column lm line-length) 0)
195 (indent-line-to
196 (+ lm (/ (- fill-column lm line-length) 2))))))
197 (cond ((null nlines)
198 (setq nlines 0))
199 ((> nlines 0)
200 (setq nlines (1- nlines))
201 (forward-line 1))
202 ((< nlines 0)
203 (setq nlines (1+ nlines))
204 (forward-line -1)))))
205
206 ;;; text-mode.el ends here