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