* lisp/icomplete.el (icomplete-with-completion-tables): Add :version.
[emacs.git] / lisp / textmodes / text-mode.el
bloba045af8522fd645c1027e2e33f154fa5f86506f2
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1992, 1994, 2001-2013 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: wp
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;; This package provides the fundamental text mode documented in the
27 ;; Emacs user's manual.
29 ;;; Code:
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)
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.")
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'.")
50 (defvar text-mode-map
51 (let ((map (make-sparse-keymap)))
52 (define-key map "\e\t" 'ispell-complete-word)
53 (define-key map [menu-bar text]
54 (cons "Text" (make-sparse-keymap "Text")))
55 (bindings--define-key map [menu-bar text toggle-text-mode-auto-fill]
56 '(menu-item "Auto Fill" toggle-text-mode-auto-fill
57 :button (:toggle . (memq 'turn-on-auto-fill text-mode-hook))
58 :help "Automatically fill text while typing in text modes (Auto Fill mode)"))
59 (bindings--define-key map [menu-bar text paragraph-indent-minor-mode]
60 '(menu-item "Paragraph Indent" paragraph-indent-minor-mode
61 :button (:toggle . (bound-and-true-p paragraph-indent-minor-mode))
62 :help "Toggle paragraph indent minor mode"))
63 (bindings--define-key map [menu-bar text sep] menu-bar-separator)
64 (bindings--define-key map [menu-bar text center-region]
65 '(menu-item "Center Region" center-region
66 :help "Center the marked region"
67 :enable (region-active-p)))
68 (bindings--define-key map [menu-bar text center-paragraph]
69 '(menu-item "Center Paragraph" center-paragraph
70 :help "Center the current paragraph"))
71 (bindings--define-key map [menu-bar text center-line]
72 '(menu-item "Center Line" center-line
73 :help "Center the current line"))
74 map)
75 "Keymap for `text-mode'.
76 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
77 inherit all the commands defined in this map.")
80 (define-derived-mode text-mode nil "Text"
81 "Major mode for editing text written for humans to read.
82 In this mode, paragraphs are delimited only by blank or white lines.
83 You can thus get the full benefit of adaptive filling
84 (see the variable `adaptive-fill-mode').
85 \\{text-mode-map}
86 Turning on Text mode runs the normal hook `text-mode-hook'."
87 (set (make-local-variable 'text-mode-variant) t)
88 (set (make-local-variable 'require-final-newline)
89 mode-require-final-newline)
90 (set (make-local-variable 'indent-line-function) 'indent-relative))
92 (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
93 "Major mode for editing text, with leading spaces starting a paragraph.
94 In this mode, you do not need blank lines between paragraphs
95 when the first line of the following paragraph starts with whitespace.
96 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
97 Special commands:
98 \\{text-mode-map}
99 Turning on Paragraph-Indent Text mode runs the normal hooks
100 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
101 :abbrev-table nil :syntax-table nil
102 (paragraph-indent-minor-mode))
104 (define-minor-mode paragraph-indent-minor-mode
105 "Minor mode for editing text, with leading spaces starting a paragraph.
106 In this mode, you do not need blank lines between paragraphs when the
107 first line of the following paragraph starts with whitespace, as with
108 `paragraph-indent-text-mode'.
109 Turning on Paragraph-Indent minor mode runs the normal hook
110 `paragraph-indent-text-mode-hook'."
111 :initial-value nil
112 ;; Change the definition of a paragraph start.
113 (let ((ps-re "[ \t\n\f]\\|"))
114 (if (eq t (compare-strings ps-re nil nil
115 paragraph-start nil (length ps-re)))
116 (if (not paragraph-indent-minor-mode)
117 (set (make-local-variable 'paragraph-start)
118 (substring paragraph-start (length ps-re))))
119 (if paragraph-indent-minor-mode
120 (set (make-local-variable 'paragraph-start)
121 (concat ps-re paragraph-start)))))
122 ;; Change the indentation function.
123 (if paragraph-indent-minor-mode
124 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
125 (if (eq indent-line-function 'indent-to-left-margin)
126 (set (make-local-variable 'indent-line-function) 'indent-region))))
128 (defalias 'indented-text-mode 'text-mode)
130 ;; This can be made a no-op once all modes that use text-mode-hook
131 ;; are "derived" from text-mode.
132 (defun text-mode-hook-identify ()
133 "Mark that this mode has run `text-mode-hook'.
134 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
135 (set (make-local-variable 'text-mode-variant) t))
137 (add-hook 'text-mode-hook 'text-mode-hook-identify)
139 (defun toggle-text-mode-auto-fill ()
140 "Toggle whether to use Auto Fill in Text mode and related modes.
141 This command affects all buffers that use modes related to Text mode,
142 both existing buffers and buffers that you subsequently create."
143 (interactive)
144 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
145 (if enable-mode
146 (add-hook 'text-mode-hook 'turn-on-auto-fill)
147 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
148 (dolist (buffer (buffer-list))
149 (with-current-buffer buffer
150 (if (or (derived-mode-p 'text-mode) text-mode-variant)
151 (auto-fill-mode (if enable-mode 1 0)))))
152 (message "Auto Fill %s in Text modes"
153 (if enable-mode "enabled" "disabled"))))
156 (define-key facemenu-keymap "\eS" 'center-paragraph)
158 (defun center-paragraph ()
159 "Center each nonblank line in the paragraph at or after point.
160 See `center-line' for more info."
161 (interactive)
162 (save-excursion
163 (forward-paragraph)
164 (or (bolp) (newline 1))
165 (let ((end (point)))
166 (backward-paragraph)
167 (center-region (point) end))))
169 (defun center-region (from to)
170 "Center each nonblank line starting in the region.
171 See `center-line' for more info."
172 (interactive "r")
173 (if (> from to)
174 (let ((tem to))
175 (setq to from from tem)))
176 (save-excursion
177 (save-restriction
178 (narrow-to-region from to)
179 (goto-char from)
180 (while (not (eobp))
181 (or (save-excursion (skip-chars-forward " \t") (eolp))
182 (center-line))
183 (forward-line 1)))))
185 (define-key facemenu-keymap "\es" 'center-line)
187 (defun center-line (&optional nlines)
188 "Center the line point is on, within the width specified by `fill-column'.
189 This means adjusting the indentation so that it equals
190 the distance between the end of the text and `fill-column'.
191 The argument NLINES says how many lines to center."
192 (interactive "P")
193 (if nlines (setq nlines (prefix-numeric-value nlines)))
194 (while (not (eq nlines 0))
195 (save-excursion
196 (let ((lm (current-left-margin))
197 line-length)
198 (beginning-of-line)
199 (delete-horizontal-space)
200 (end-of-line)
201 (delete-horizontal-space)
202 (setq line-length (current-column))
203 (if (> (- fill-column lm line-length) 0)
204 (indent-line-to
205 (+ lm (/ (- fill-column lm line-length) 2))))))
206 (cond ((null nlines)
207 (setq nlines 0))
208 ((> nlines 0)
209 (setq nlines (1- nlines))
210 (forward-line 1))
211 ((< nlines 0)
212 (setq nlines (1+ nlines))
213 (forward-line -1)))))
215 ;;; text-mode.el ends here