Add 2013 to yet more FSF copyright years
[emacs.git] / lisp / textmodes / text-mode.el
blobd9ff04c9b2f990505984a6d2661fce9e4dad8b99
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1992, 1994, 2001-2013 Free Software Foundation,
4 ;; Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: wp
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This package provides the fundamental text mode documented in the
28 ;; Emacs user's manual.
30 ;;; Code:
32 (defcustom text-mode-hook nil
33 "Normal hook run when entering Text mode and many related modes."
34 :type 'hook
35 :options '(turn-on-auto-fill turn-on-flyspell)
36 :group 'wp)
38 (defvar text-mode-variant nil
39 "Non-nil if this buffer's major mode is a variant of Text mode.
40 Use (derived-mode-p 'text-mode) instead.")
42 (defvar text-mode-syntax-table
43 (let ((st (make-syntax-table)))
44 (modify-syntax-entry ?\" ". " st)
45 (modify-syntax-entry ?\\ ". " st)
46 ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
47 (modify-syntax-entry ?' "w p" st)
48 st)
49 "Syntax table used while in `text-mode'.")
51 (defvar text-mode-map
52 (let ((map (make-sparse-keymap)))
53 (define-key map "\e\t" 'ispell-complete-word)
54 map)
55 "Keymap for `text-mode'.
56 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
57 inherit all the commands defined in this map.")
60 (define-derived-mode text-mode nil "Text"
61 "Major mode for editing text written for humans to read.
62 In this mode, paragraphs are delimited only by blank or white lines.
63 You can thus get the full benefit of adaptive filling
64 (see the variable `adaptive-fill-mode').
65 \\{text-mode-map}
66 Turning on Text mode runs the normal hook `text-mode-hook'."
67 (set (make-local-variable 'text-mode-variant) t)
68 (set (make-local-variable 'require-final-newline)
69 mode-require-final-newline)
70 (set (make-local-variable 'indent-line-function) 'indent-relative))
72 (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
73 "Major mode for editing text, with leading spaces starting a paragraph.
74 In this mode, you do not need blank lines between paragraphs
75 when the first line of the following paragraph starts with whitespace.
76 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
77 Special commands:
78 \\{text-mode-map}
79 Turning on Paragraph-Indent Text mode runs the normal hooks
80 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
81 :abbrev-table nil :syntax-table nil
82 (paragraph-indent-minor-mode))
84 (define-minor-mode paragraph-indent-minor-mode
85 "Minor mode for editing text, with leading spaces starting a paragraph.
86 In this mode, you do not need blank lines between paragraphs when the
87 first line of the following paragraph starts with whitespace, as with
88 `paragraph-indent-text-mode'.
89 Turning on Paragraph-Indent minor mode runs the normal hook
90 `paragraph-indent-text-mode-hook'."
91 :initial-value nil
92 ;; Change the definition of a paragraph start.
93 (let ((ps-re "[ \t\n\f]\\|"))
94 (if (eq t (compare-strings ps-re nil nil
95 paragraph-start nil (length ps-re)))
96 (if (not paragraph-indent-minor-mode)
97 (set (make-local-variable 'paragraph-start)
98 (substring paragraph-start (length ps-re))))
99 (if paragraph-indent-minor-mode
100 (set (make-local-variable 'paragraph-start)
101 (concat ps-re paragraph-start)))))
102 ;; Change the indentation function.
103 (if paragraph-indent-minor-mode
104 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
105 (if (eq indent-line-function 'indent-to-left-margin)
106 (set (make-local-variable 'indent-line-function) 'indent-region))))
108 (defalias 'indented-text-mode 'text-mode)
110 ;; This can be made a no-op once all modes that use text-mode-hook
111 ;; are "derived" from text-mode.
112 (defun text-mode-hook-identify ()
113 "Mark that this mode has run `text-mode-hook'.
114 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
115 (set (make-local-variable 'text-mode-variant) t))
117 (add-hook 'text-mode-hook 'text-mode-hook-identify)
119 (defun toggle-text-mode-auto-fill ()
120 "Toggle whether to use Auto Fill in Text mode and related modes.
121 This command affects all buffers that use modes related to Text mode,
122 both existing buffers and buffers that you subsequently create."
123 (interactive)
124 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
125 (if enable-mode
126 (add-hook 'text-mode-hook 'turn-on-auto-fill)
127 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
128 (dolist (buffer (buffer-list))
129 (with-current-buffer buffer
130 (if (or (derived-mode-p 'text-mode) text-mode-variant)
131 (auto-fill-mode (if enable-mode 1 0)))))
132 (message "Auto Fill %s in Text modes"
133 (if enable-mode "enabled" "disabled"))))
136 (define-key facemenu-keymap "\eS" 'center-paragraph)
138 (defun center-paragraph ()
139 "Center each nonblank line in the paragraph at or after point.
140 See `center-line' for more info."
141 (interactive)
142 (save-excursion
143 (forward-paragraph)
144 (or (bolp) (newline 1))
145 (let ((end (point)))
146 (backward-paragraph)
147 (center-region (point) end))))
149 (defun center-region (from to)
150 "Center each nonblank line starting in the region.
151 See `center-line' for more info."
152 (interactive "r")
153 (if (> from to)
154 (let ((tem to))
155 (setq to from from tem)))
156 (save-excursion
157 (save-restriction
158 (narrow-to-region from to)
159 (goto-char from)
160 (while (not (eobp))
161 (or (save-excursion (skip-chars-forward " \t") (eolp))
162 (center-line))
163 (forward-line 1)))))
165 (define-key facemenu-keymap "\es" 'center-line)
167 (defun center-line (&optional nlines)
168 "Center the line point is on, within the width specified by `fill-column'.
169 This means adjusting the indentation so that it equals
170 the distance between the end of the text and `fill-column'.
171 The argument NLINES says how many lines to center."
172 (interactive "P")
173 (if nlines (setq nlines (prefix-numeric-value nlines)))
174 (while (not (eq nlines 0))
175 (save-excursion
176 (let ((lm (current-left-margin))
177 line-length)
178 (beginning-of-line)
179 (delete-horizontal-space)
180 (end-of-line)
181 (delete-horizontal-space)
182 (setq line-length (current-column))
183 (if (> (- fill-column lm line-length) 0)
184 (indent-line-to
185 (+ lm (/ (- fill-column lm line-length) 2))))))
186 (cond ((null nlines)
187 (setq nlines 0))
188 ((> nlines 0)
189 (setq nlines (1- nlines))
190 (forward-line 1))
191 ((< nlines 0)
192 (setq nlines (1+ nlines))
193 (forward-line -1)))))
195 ;;; text-mode.el ends here