Merge from emacs-24; up to 2012-05-08T15:19:18Z!monnier@iro.umontreal.ca
[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 82
4ffb41a9 83(define-minor-mode paragraph-indent-minor-mode
9f78d51a
DL
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'."
4ffb41a9
SM
90 :initial-value nil
91 ;; Change the definition of a paragraph start.
92 (let ((ps-re "[ \t\n\f]\\|"))
93 (if (eq t (compare-strings ps-re nil nil
94 paragraph-start nil (length ps-re)))
95 (if (not paragraph-indent-minor-mode)
96 (set (make-local-variable 'paragraph-start)
97 (substring paragraph-start (length ps-re))))
98 (if paragraph-indent-minor-mode
99 (set (make-local-variable 'paragraph-start)
100 (concat ps-re paragraph-start)))))
101 ;; Change the indentation function.
102 (if paragraph-indent-minor-mode
103 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
104 (if (eq indent-line-function 'indent-to-left-margin)
105 (set (make-local-variable 'indent-line-function) 'indent-region))))
db95369b 106
946340ae 107(defalias 'indented-text-mode 'text-mode)
a2535589 108
ebda95a2
RS
109;; This can be made a no-op once all modes that use text-mode-hook
110;; are "derived" from text-mode.
111(defun text-mode-hook-identify ()
112 "Mark that this mode has run `text-mode-hook'.
113This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
114 (set (make-local-variable 'text-mode-variant) t))
115
116(add-hook 'text-mode-hook 'text-mode-hook-identify)
def08a3f 117
4c69a3be
RS
118(defun toggle-text-mode-auto-fill ()
119 "Toggle whether to use Auto Fill in Text mode and related modes.
120This command affects all buffers that use modes related to Text mode,
121both existing buffers and buffers that you subsequently create."
122 (interactive)
fdf1c7c3 123 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
4c69a3be
RS
124 (if enable-mode
125 (add-hook 'text-mode-hook 'turn-on-auto-fill)
126 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
fdf1c7c3
SM
127 (dolist (buffer (buffer-list))
128 (with-current-buffer buffer
129 (if (or (derived-mode-p 'text-mode) text-mode-variant)
130 (auto-fill-mode (if enable-mode 1 0)))))
4c69a3be
RS
131 (message "Auto Fill %s in Text modes"
132 (if enable-mode "enabled" "disabled"))))
133\f
4603452c
JL
134
135(define-key facemenu-keymap "\eS" 'center-paragraph)
136
a2535589
JA
137(defun center-paragraph ()
138 "Center each nonblank line in the paragraph at or after point.
d367fa71 139See `center-line' for more info."
a2535589
JA
140 (interactive)
141 (save-excursion
142 (forward-paragraph)
143 (or (bolp) (newline 1))
144 (let ((end (point)))
145 (backward-paragraph)
146 (center-region (point) end))))
147
148(defun center-region (from to)
149 "Center each nonblank line starting in the region.
d367fa71 150See `center-line' for more info."
a2535589
JA
151 (interactive "r")
152 (if (> from to)
153 (let ((tem to))
154 (setq to from from tem)))
155 (save-excursion
156 (save-restriction
157 (narrow-to-region from to)
158 (goto-char from)
159 (while (not (eobp))
160 (or (save-excursion (skip-chars-forward " \t") (eolp))
161 (center-line))
162 (forward-line 1)))))
163
4603452c
JL
164(define-key facemenu-keymap "\es" 'center-line)
165
aaf6c7ef 166(defun center-line (&optional nlines)
a2535589
JA
167 "Center the line point is on, within the width specified by `fill-column'.
168This means adjusting the indentation so that it equals
aaf6c7ef
RS
169the distance between the end of the text and `fill-column'.
170The argument NLINES says how many lines to center."
171 (interactive "P")
172 (if nlines (setq nlines (prefix-numeric-value nlines)))
173 (while (not (eq nlines 0))
174 (save-excursion
175 (let ((lm (current-left-margin))
176 line-length)
177 (beginning-of-line)
178 (delete-horizontal-space)
179 (end-of-line)
180 (delete-horizontal-space)
181 (setq line-length (current-column))
182 (if (> (- fill-column lm line-length) 0)
84347194 183 (indent-line-to
aaf6c7ef
RS
184 (+ lm (/ (- fill-column lm line-length) 2))))))
185 (cond ((null nlines)
186 (setq nlines 0))
187 ((> nlines 0)
188 (setq nlines (1- nlines))
189 (forward-line 1))
190 ((< nlines 0)
191 (setq nlines (1+ nlines))
192 (forward-line -1)))))
d501f516
ER
193
194;;; text-mode.el ends here