Merge from trunk.
[bpt/emacs.git] / lisp / textmodes / text-mode.el
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
2
3 ;; Copyright (C) 1985, 1992, 1994, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: wp
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This package provides the fundamental text mode documented in the
27 ;; Emacs user's manual.
28
29 ;;; Code:
30
31 (defcustom text-mode-hook nil
32 "Normal hook run when entering Text mode and many related modes."
33 :type 'hook
34 :options '(turn-on-auto-fill turn-on-flyspell)
35 :group 'wp)
36
37 (defvar text-mode-variant nil
38 "Non-nil if this buffer's major mode is a variant of Text mode.
39 Use (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)
45 ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
46 (modify-syntax-entry ?' "w p" st)
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)
53 map)
54 "Keymap for `text-mode'.
55 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
56 inherit all the commands defined in this map.")
57
58 \f
59 (define-derived-mode text-mode nil "Text"
60 "Major mode for editing text written for humans to read.
61 In this mode, paragraphs are delimited only by blank or white lines.
62 You can thus get the full benefit of adaptive filling
63 (see the variable `adaptive-fill-mode').
64 \\{text-mode-map}
65 Turning on Text mode runs the normal hook `text-mode-hook'."
66 (set (make-local-variable 'text-mode-variant) t)
67 (set (make-local-variable 'require-final-newline)
68 mode-require-final-newline)
69 (set (make-local-variable 'indent-line-function) 'indent-relative))
70
71 (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
72 "Major mode for editing text, with leading spaces starting a paragraph.
73 In this mode, you do not need blank lines between paragraphs
74 when the first line of the following paragraph starts with whitespace.
75 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
76 Special commands:
77 \\{text-mode-map}
78 Turning on Paragraph-Indent Text mode runs the normal hooks
79 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
80 :abbrev-table nil :syntax-table nil
81 (paragraph-indent-minor-mode))
82
83 (defun paragraph-indent-minor-mode ()
84 "Minor mode for editing text, with leading spaces starting a paragraph.
85 In this mode, you do not need blank lines between paragraphs when the
86 first line of the following paragraph starts with whitespace, as with
87 `paragraph-indent-text-mode'.
88 Turning on Paragraph-Indent minor mode runs the normal hook
89 `paragraph-indent-text-mode-hook'."
90 (interactive)
91 (set (make-local-variable 'paragraph-start)
92 (concat "[ \t\n\f]\\|" paragraph-start))
93 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
94 (run-hooks 'paragraph-indent-text-mode-hook))
95
96 (defalias 'indented-text-mode 'text-mode)
97
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'.
102 This 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)
106
107 (defun toggle-text-mode-auto-fill ()
108 "Toggle whether to use Auto Fill in Text mode and related modes.
109 This command affects all buffers that use modes related to Text mode,
110 both existing buffers and buffers that you subsequently create."
111 (interactive)
112 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
113 (if enable-mode
114 (add-hook 'text-mode-hook 'turn-on-auto-fill)
115 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
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)))))
120 (message "Auto Fill %s in Text modes"
121 (if enable-mode "enabled" "disabled"))))
122 \f
123
124 (define-key facemenu-keymap "\eS" 'center-paragraph)
125
126 (defun center-paragraph ()
127 "Center each nonblank line in the paragraph at or after point.
128 See `center-line' for more info."
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.
139 See `center-line' for more info."
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
153 (define-key facemenu-keymap "\es" 'center-line)
154
155 (defun center-line (&optional nlines)
156 "Center the line point is on, within the width specified by `fill-column'.
157 This means adjusting the indentation so that it equals
158 the distance between the end of the text and `fill-column'.
159 The 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)
172 (indent-line-to
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)))))
182
183 ;;; text-mode.el ends here