Some fixes to follow coding conventions in files maintained by FSF.
[bpt/emacs.git] / lisp / textmodes / text-mode.el
CommitLineData
55535639 1;;; text-mode.el --- text mode, and its idiosyncratic commands
d501f516 2
8f1204db 3;; Copyright (C) 1985, 1992, 1994 Free Software Foundation, Inc.
a2535589 4
58142744
ER
5;; Maintainer: FSF
6
a2535589
JA
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
daa37602 11;; the Free Software Foundation; either version 2, or (at your option)
a2535589
JA
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
b578f267
EN
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.
d9ecc911
ER
23
24;;; Commentary:
25
26;; This package provides the fundamental text mode documented in the
27;; Emacs user's manual.
28
22a89ee8 29;;; Code:
a2535589 30
e144fd4e
DL
31(defcustom text-mode-hook nil
32 "Normal hook run when entering Text mode and many related modes."
33 :type 'hook
84347194 34 :options '(turn-on-auto-fill flyspell-mode)
e144fd4e 35 :group 'data)
047d1f89 36
4c69a3be
RS
37(defvar text-mode-variant nil
38 "Non-nil if this buffer's major mode is a variant of Text mode.")
39
a2535589
JA
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.
56Many other modes, such as Mail mode, Outline mode and Indented Text mode,
57inherit all the commands defined in this map.")
58
59(if text-mode-map
60 ()
61 (setq text-mode-map (make-sparse-keymap))
fb14a9a7 62 (define-key text-mode-map "\e\t" 'ispell-complete-word)
946340ae 63 (define-key text-mode-map "\t" 'indent-relative)
a2535589
JA
64 (define-key text-mode-map "\es" 'center-line)
65 (define-key text-mode-map "\eS" 'center-paragraph))
66
67\f
a2535589 68(defun text-mode ()
6824710a 69 "Major mode for editing text written for humans to read.
91fe8c37 70In this mode, paragraphs are delimited only by blank or white lines.
6824710a
RS
71You can thus get the full benefit of adaptive filling
72 (see the variable `adaptive-fill-mode').
6b4bde1b 73\\{text-mode-map}
6824710a 74Turning on Text mode runs the normal hook `text-mode-hook'."
a2535589
JA
75 (interactive)
76 (kill-all-local-variables)
77 (use-local-map text-mode-map)
6824710a
RS
78 (setq local-abbrev-table text-mode-abbrev-table)
79 (set-syntax-table text-mode-syntax-table)
80 (make-local-variable 'paragraph-start)
855b9e33 81 (setq paragraph-start (concat page-delimiter "\\|[ \t]*$"))
9f78d51a
DL
82 (if (eq ?^ (aref paragraph-start 0))
83 (setq paragraph-start (substring paragraph-start 1)))
6824710a
RS
84 (make-local-variable 'paragraph-separate)
85 (setq paragraph-separate paragraph-start)
85761533
RS
86 (make-local-variable 'indent-line-function)
87 (setq indent-line-function 'indent-relative-maybe)
a2535589
JA
88 (setq mode-name "Text")
89 (setq major-mode 'text-mode)
6824710a
RS
90 (run-hooks 'text-mode-hook))
91
946340ae 92(defun paragraph-indent-text-mode ()
6824710a
RS
93 "Major mode for editing text, with leading spaces starting a paragraph.
94In this mode, you do not need blank lines between paragraphs
95when the first line of the following paragraph starts with whitespace.
9f78d51a 96`paragraph-indent-minor-mode' provides a similar facility as a minor mode.
6824710a
RS
97Special commands:
98\\{text-mode-map}
946340ae
RS
99Turning on Paragraph-Indent Text mode runs the normal hooks
100`text-mode-hook' and `paragraph-indent-text-mode-hook'."
6824710a
RS
101 (interactive)
102 (kill-all-local-variables)
103 (use-local-map text-mode-map)
946340ae
RS
104 (setq mode-name "Parindent")
105 (setq major-mode 'paragraph-indent-text-mode)
a2535589
JA
106 (setq local-abbrev-table text-mode-abbrev-table)
107 (set-syntax-table text-mode-syntax-table)
946340ae 108 (run-hooks 'text-mode-hook 'paragraph-indent-text-mode-hook))
9f78d51a
DL
109
110(defun paragraph-indent-minor-mode ()
111 "Minor mode for editing text, with leading spaces starting a paragraph.
112In this mode, you do not need blank lines between paragraphs when the
113first line of the following paragraph starts with whitespace, as with
114`paragraph-indent-mode'.
115Turning 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))
6824710a 122
946340ae 123(defalias 'indented-text-mode 'text-mode)
a2535589 124
d910f08c
RS
125(defun text-mode-hook-identify ()
126 "Mark that this mode has run `text-mode-hook'.
127This 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
4c69a3be
RS
133(defun toggle-text-mode-auto-fill ()
134 "Toggle whether to use Auto Fill in Text mode and related modes.
135This command affects all buffers that use modes related to Text mode,
136both 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
a2535589
JA
151(defun center-paragraph ()
152 "Center each nonblank line in the paragraph at or after point.
d367fa71 153See `center-line' for more info."
a2535589
JA
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.
d367fa71 164See `center-line' for more info."
a2535589
JA
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
aaf6c7ef 178(defun center-line (&optional nlines)
a2535589
JA
179 "Center the line point is on, within the width specified by `fill-column'.
180This means adjusting the indentation so that it equals
aaf6c7ef
RS
181the distance between the end of the text and `fill-column'.
182The 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)
84347194 195 (indent-line-to
aaf6c7ef
RS
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)))))
d501f516
ER
205
206;;; text-mode.el ends here