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