Merge from emacs-24; up to 2012-05-08T15:19:18Z!monnier@iro.umontreal.ca
[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 (define-minor-mode 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 :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))))
106
107 (defalias 'indented-text-mode 'text-mode)
108
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'.
113 This 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)
117
118 (defun toggle-text-mode-auto-fill ()
119 "Toggle whether to use Auto Fill in Text mode and related modes.
120 This command affects all buffers that use modes related to Text mode,
121 both existing buffers and buffers that you subsequently create."
122 (interactive)
123 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
124 (if enable-mode
125 (add-hook 'text-mode-hook 'turn-on-auto-fill)
126 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
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)))))
131 (message "Auto Fill %s in Text modes"
132 (if enable-mode "enabled" "disabled"))))
133 \f
134
135 (define-key facemenu-keymap "\eS" 'center-paragraph)
136
137 (defun center-paragraph ()
138 "Center each nonblank line in the paragraph at or after point.
139 See `center-line' for more info."
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.
150 See `center-line' for more info."
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
164 (define-key facemenu-keymap "\es" 'center-line)
165
166 (defun center-line (&optional nlines)
167 "Center the line point is on, within the width specified by `fill-column'.
168 This means adjusting the indentation so that it equals
169 the distance between the end of the text and `fill-column'.
170 The 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)
183 (indent-line-to
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)))))
193
194 ;;; text-mode.el ends here