Add 2010 to copyright years.
[bpt/emacs.git] / lisp / textmodes / text-mode.el
CommitLineData
55535639 1;;; text-mode.el --- text mode, and its idiosyncratic commands
d501f516 2
f2e3589a 3;; Copyright (C) 1985, 1992, 1994, 2001, 2002, 2003, 2004,
114f9c96 4;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
a2535589 5
58142744 6;; Maintainer: FSF
6228c05b 7;; Keywords: wp
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)
e144fd4e 35 :group 'data)
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'."
540f653d
RS
66 (make-local-variable 'text-mode-variant)
67 (setq text-mode-variant t)
706d2537
RS
68 (set (make-local-variable 'require-final-newline)
69 mode-require-final-newline)
9e59bf58 70 (set (make-local-variable 'indent-line-function) 'indent-relative))
6824710a 71
0fb36e36 72(define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
6824710a
RS
73 "Major mode for editing text, with leading spaces starting a paragraph.
74In this mode, you do not need blank lines between paragraphs
75when the first line of the following paragraph starts with whitespace.
9f78d51a 76`paragraph-indent-minor-mode' provides a similar facility as a minor mode.
6824710a
RS
77Special commands:
78\\{text-mode-map}
946340ae
RS
79Turning on Paragraph-Indent Text mode runs the normal hooks
80`text-mode-hook' and `paragraph-indent-text-mode-hook'."
61ed2dcc 81 :abbrev-table nil :syntax-table nil
9e59bf58 82 (paragraph-indent-minor-mode))
9f78d51a
DL
83
84(defun paragraph-indent-minor-mode ()
85 "Minor mode for editing text, with leading spaces starting a paragraph.
86In this mode, you do not need blank lines between paragraphs when the
87first line of the following paragraph starts with whitespace, as with
57fff5de 88`paragraph-indent-text-mode'.
9f78d51a
DL
89Turning on Paragraph-Indent minor mode runs the normal hook
90`paragraph-indent-text-mode-hook'."
91 (interactive)
92 (set (make-local-variable 'paragraph-start)
0fb36e36
SM
93 (concat "[ \t\n\f]\\|" paragraph-start))
94 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
9f78d51a 95 (run-hooks 'paragraph-indent-text-mode-hook))
db95369b 96
946340ae 97(defalias 'indented-text-mode 'text-mode)
a2535589 98
ebda95a2
RS
99;; This can be made a no-op once all modes that use text-mode-hook
100;; are "derived" from text-mode.
101(defun text-mode-hook-identify ()
102 "Mark that this mode has run `text-mode-hook'.
103This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
104 (set (make-local-variable 'text-mode-variant) t))
105
106(add-hook 'text-mode-hook 'text-mode-hook-identify)
def08a3f 107
4c69a3be
RS
108(defun toggle-text-mode-auto-fill ()
109 "Toggle whether to use Auto Fill in Text mode and related modes.
110This command affects all buffers that use modes related to Text mode,
111both existing buffers and buffers that you subsequently create."
112 (interactive)
fdf1c7c3 113 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
4c69a3be
RS
114 (if enable-mode
115 (add-hook 'text-mode-hook 'turn-on-auto-fill)
116 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
fdf1c7c3
SM
117 (dolist (buffer (buffer-list))
118 (with-current-buffer buffer
119 (if (or (derived-mode-p 'text-mode) text-mode-variant)
120 (auto-fill-mode (if enable-mode 1 0)))))
4c69a3be
RS
121 (message "Auto Fill %s in Text modes"
122 (if enable-mode "enabled" "disabled"))))
123\f
4603452c
JL
124
125(define-key facemenu-keymap "\eS" 'center-paragraph)
126
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
4603452c
JL
154(define-key facemenu-keymap "\es" 'center-line)
155
aaf6c7ef 156(defun center-line (&optional nlines)
a2535589
JA
157 "Center the line point is on, within the width specified by `fill-column'.
158This means adjusting the indentation so that it equals
aaf6c7ef
RS
159the distance between the end of the text and `fill-column'.
160The argument NLINES says how many lines to center."
161 (interactive "P")
162 (if nlines (setq nlines (prefix-numeric-value nlines)))
163 (while (not (eq nlines 0))
164 (save-excursion
165 (let ((lm (current-left-margin))
166 line-length)
167 (beginning-of-line)
168 (delete-horizontal-space)
169 (end-of-line)
170 (delete-horizontal-space)
171 (setq line-length (current-column))
172 (if (> (- fill-column lm line-length) 0)
84347194 173 (indent-line-to
aaf6c7ef
RS
174 (+ lm (/ (- fill-column lm line-length) 2))))))
175 (cond ((null nlines)
176 (setq nlines 0))
177 ((> nlines 0)
178 (setq nlines (1- nlines))
179 (forward-line 1))
180 ((< nlines 0)
181 (setq nlines (1+ nlines))
182 (forward-line -1)))))
d501f516 183
cbee283d 184;; arch-tag: a07ccaad-da13-4d7b-9c61-cd04f5926aab
d501f516 185;;; text-mode.el ends here