Update FSF's address.
[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 5;; Maintainer: FSF
6228c05b 6;; Keywords: wp
58142744 7
a2535589
JA
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
daa37602 12;; the Free Software Foundation; either version 2, or (at your option)
a2535589
JA
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
b578f267 21;; along with GNU Emacs; see the file COPYING. If not, write to the
4fc5845f
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
d9ecc911
ER
24
25;;; Commentary:
26
27;; This package provides the fundamental text mode documented in the
28;; Emacs user's manual.
29
22a89ee8 30;;; Code:
a2535589 31
e144fd4e
DL
32(defcustom text-mode-hook nil
33 "Normal hook run when entering Text mode and many related modes."
34 :type 'hook
84347194 35 :options '(turn-on-auto-fill flyspell-mode)
e144fd4e 36 :group 'data)
047d1f89 37
4c69a3be 38(defvar text-mode-variant nil
9e59bf58
SM
39 "Non-nil if this buffer's major mode is a variant of Text mode.
40Use (derived-mode-p 'text-mode) instead.")
41
42(defvar text-mode-syntax-table
43 (let ((st (make-syntax-table)))
44 (modify-syntax-entry ?\" ". " st)
45 (modify-syntax-entry ?\\ ". " st)
61ed2dcc
SM
46 ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
47 (modify-syntax-entry ?' "w p" st)
9e59bf58
SM
48 st)
49 "Syntax table used while in `text-mode'.")
50
51(defvar text-mode-map
52 (let ((map (make-sparse-keymap)))
53 (define-key map "\e\t" 'ispell-complete-word)
54 (define-key map "\es" 'center-line)
55 (define-key map "\eS" 'center-paragraph)
56 map)
57 "Keymap for `text-mode'.
58Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
a2535589
JA
59inherit all the commands defined in this map.")
60
a2535589 61\f
fdf1c7c3 62(define-derived-mode text-mode nil "Text"
6824710a 63 "Major mode for editing text written for humans to read.
91fe8c37 64In this mode, paragraphs are delimited only by blank or white lines.
6824710a
RS
65You can thus get the full benefit of adaptive filling
66 (see the variable `adaptive-fill-mode').
6b4bde1b 67\\{text-mode-map}
6824710a 68Turning on Text mode runs the normal hook `text-mode-hook'."
540f653d
RS
69 (make-local-variable 'text-mode-variant)
70 (setq text-mode-variant t)
706d2537
RS
71 (set (make-local-variable 'require-final-newline)
72 mode-require-final-newline)
9e59bf58 73 (set (make-local-variable 'indent-line-function) 'indent-relative))
6824710a 74
0fb36e36 75(define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
6824710a
RS
76 "Major mode for editing text, with leading spaces starting a paragraph.
77In this mode, you do not need blank lines between paragraphs
78when the first line of the following paragraph starts with whitespace.
9f78d51a 79`paragraph-indent-minor-mode' provides a similar facility as a minor mode.
6824710a
RS
80Special commands:
81\\{text-mode-map}
946340ae
RS
82Turning on Paragraph-Indent Text mode runs the normal hooks
83`text-mode-hook' and `paragraph-indent-text-mode-hook'."
61ed2dcc 84 :abbrev-table nil :syntax-table nil
9e59bf58 85 (paragraph-indent-minor-mode))
9f78d51a
DL
86
87(defun paragraph-indent-minor-mode ()
88 "Minor mode for editing text, with leading spaces starting a paragraph.
89In this mode, you do not need blank lines between paragraphs when the
90first line of the following paragraph starts with whitespace, as with
57fff5de 91`paragraph-indent-text-mode'.
9f78d51a
DL
92Turning on Paragraph-Indent minor mode runs the normal hook
93`paragraph-indent-text-mode-hook'."
94 (interactive)
95 (set (make-local-variable 'paragraph-start)
0fb36e36
SM
96 (concat "[ \t\n\f]\\|" paragraph-start))
97 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
9f78d51a 98 (run-hooks 'paragraph-indent-text-mode-hook))
db95369b 99
946340ae 100(defalias 'indented-text-mode 'text-mode)
a2535589 101
ebda95a2
RS
102;; This can be made a no-op once all modes that use text-mode-hook
103;; are "derived" from text-mode.
104(defun text-mode-hook-identify ()
105 "Mark that this mode has run `text-mode-hook'.
106This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
107 (set (make-local-variable 'text-mode-variant) t))
108
109(add-hook 'text-mode-hook 'text-mode-hook-identify)
def08a3f 110
4c69a3be
RS
111(defun toggle-text-mode-auto-fill ()
112 "Toggle whether to use Auto Fill in Text mode and related modes.
113This command affects all buffers that use modes related to Text mode,
114both existing buffers and buffers that you subsequently create."
115 (interactive)
fdf1c7c3 116 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
4c69a3be
RS
117 (if enable-mode
118 (add-hook 'text-mode-hook 'turn-on-auto-fill)
119 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
fdf1c7c3
SM
120 (dolist (buffer (buffer-list))
121 (with-current-buffer buffer
122 (if (or (derived-mode-p 'text-mode) text-mode-variant)
123 (auto-fill-mode (if enable-mode 1 0)))))
4c69a3be
RS
124 (message "Auto Fill %s in Text modes"
125 (if enable-mode "enabled" "disabled"))))
126\f
a2535589
JA
127(defun center-paragraph ()
128 "Center each nonblank line in the paragraph at or after point.
d367fa71 129See `center-line' for more info."
a2535589
JA
130 (interactive)
131 (save-excursion
132 (forward-paragraph)
133 (or (bolp) (newline 1))
134 (let ((end (point)))
135 (backward-paragraph)
136 (center-region (point) end))))
137
138(defun center-region (from to)
139 "Center each nonblank line starting in the region.
d367fa71 140See `center-line' for more info."
a2535589
JA
141 (interactive "r")
142 (if (> from to)
143 (let ((tem to))
144 (setq to from from tem)))
145 (save-excursion
146 (save-restriction
147 (narrow-to-region from to)
148 (goto-char from)
149 (while (not (eobp))
150 (or (save-excursion (skip-chars-forward " \t") (eolp))
151 (center-line))
152 (forward-line 1)))))
153
aaf6c7ef 154(defun center-line (&optional nlines)
a2535589
JA
155 "Center the line point is on, within the width specified by `fill-column'.
156This means adjusting the indentation so that it equals
aaf6c7ef
RS
157the distance between the end of the text and `fill-column'.
158The argument NLINES says how many lines to center."
159 (interactive "P")
160 (if nlines (setq nlines (prefix-numeric-value nlines)))
161 (while (not (eq nlines 0))
162 (save-excursion
163 (let ((lm (current-left-margin))
164 line-length)
165 (beginning-of-line)
166 (delete-horizontal-space)
167 (end-of-line)
168 (delete-horizontal-space)
169 (setq line-length (current-column))
170 (if (> (- fill-column lm line-length) 0)
84347194 171 (indent-line-to
aaf6c7ef
RS
172 (+ lm (/ (- fill-column lm line-length) 2))))))
173 (cond ((null nlines)
174 (setq nlines 0))
175 ((> nlines 0)
176 (setq nlines (1- nlines))
177 (forward-line 1))
178 ((< nlines 0)
179 (setq nlines (1+ nlines))
180 (forward-line -1)))))
d501f516 181
ab5796a9 182;;; arch-tag: a07ccaad-da13-4d7b-9c61-cd04f5926aab
d501f516 183;;; text-mode.el ends here