]> code.delx.au - gnu-emacs/blob - lisp/textmodes/text-mode.el
* mail/rmail.el (rmail-show-message-1): When displaying a mime message,
[gnu-emacs] / lisp / textmodes / text-mode.el
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
2
3 ;; Copyright (C) 1985, 1992, 1994, 2001-2015 Free Software Foundation,
4 ;; Inc.
5
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: wp
8 ;; Package: emacs
9
10 ;; This file is part of GNU Emacs.
11
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.
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
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This package provides the fundamental text mode documented in the
28 ;; Emacs user's manual.
29
30 ;;; Code:
31
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)
37
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.")
41
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 ;; UAX #29 says HEBREW PUNCTUATION GERESH behaves like a letter
49 ;; for the purposes of finding word boundaries.
50 (modify-syntax-entry #x5f3 "w ") ; GERESH
51 ;; UAX #29 says HEBREW PUNCTUATION GERSHAYIM should not be a word
52 ;; boundary when surrounded by letters. Our infrastructure for
53 ;; finding a word boundary doesn't support 3-character
54 ;; definitions, so for now simply make this a word-constituent
55 ;; character. This leaves a problem of having GERSHAYIM at the
56 ;; beginning or end of a word, where it should be a boundary;
57 ;; FIXME.
58 (modify-syntax-entry #x5f4 "w ") ; GERSHAYIM
59 ;; These all should not be a word boundary when between letters,
60 ;; according to UAX #29, so they again are prone to the same
61 ;; problem as GERSHAYIM; FIXME.
62 (modify-syntax-entry #xb7 "w ") ; MIDDLE DOT
63 (modify-syntax-entry #x2027 "w ") ; HYPHENATION POINT
64 (modify-syntax-entry #xff1a "w ") ; FULLWIDTH COLON
65 st)
66 "Syntax table used while in `text-mode'.")
67
68 (defvar text-mode-map
69 (let ((map (make-sparse-keymap)))
70 (define-key map "\e\t" 'ispell-complete-word)
71 (define-key map [menu-bar text]
72 (cons "Text" (make-sparse-keymap "Text")))
73 (bindings--define-key map [menu-bar text toggle-text-mode-auto-fill]
74 '(menu-item "Auto Fill" toggle-text-mode-auto-fill
75 :button (:toggle . (memq 'turn-on-auto-fill text-mode-hook))
76 :help "Automatically fill text while typing in text modes (Auto Fill mode)"))
77 (bindings--define-key map [menu-bar text paragraph-indent-minor-mode]
78 '(menu-item "Paragraph Indent" paragraph-indent-minor-mode
79 :button (:toggle . (bound-and-true-p paragraph-indent-minor-mode))
80 :help "Toggle paragraph indent minor mode"))
81 (bindings--define-key map [menu-bar text sep] menu-bar-separator)
82 (bindings--define-key map [menu-bar text center-region]
83 '(menu-item "Center Region" center-region
84 :help "Center the marked region"
85 :enable (region-active-p)))
86 (bindings--define-key map [menu-bar text center-paragraph]
87 '(menu-item "Center Paragraph" center-paragraph
88 :help "Center the current paragraph"))
89 (bindings--define-key map [menu-bar text center-line]
90 '(menu-item "Center Line" center-line
91 :help "Center the current line"))
92 map)
93 "Keymap for `text-mode'.
94 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
95 inherit all the commands defined in this map.")
96
97 \f
98 (define-derived-mode text-mode nil "Text"
99 "Major mode for editing text written for humans to read.
100 In this mode, paragraphs are delimited only by blank or white lines.
101 You can thus get the full benefit of adaptive filling
102 (see the variable `adaptive-fill-mode').
103 \\{text-mode-map}
104 Turning on Text mode runs the normal hook `text-mode-hook'."
105 (set (make-local-variable 'text-mode-variant) t)
106 (set (make-local-variable 'require-final-newline)
107 mode-require-final-newline)
108 (set (make-local-variable 'indent-line-function) 'indent-relative))
109
110 (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
111 "Major mode for editing text, with leading spaces starting a paragraph.
112 In this mode, you do not need blank lines between paragraphs
113 when the first line of the following paragraph starts with whitespace.
114 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
115 Special commands:
116 \\{text-mode-map}
117 Turning on Paragraph-Indent Text mode runs the normal hooks
118 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
119 :abbrev-table nil :syntax-table nil
120 (paragraph-indent-minor-mode))
121
122 (define-minor-mode paragraph-indent-minor-mode
123 "Minor mode for editing text, with leading spaces starting a paragraph.
124 In this mode, you do not need blank lines between paragraphs when the
125 first line of the following paragraph starts with whitespace, as with
126 `paragraph-indent-text-mode'.
127 Turning on Paragraph-Indent minor mode runs the normal hook
128 `paragraph-indent-text-mode-hook'."
129 :initial-value nil
130 ;; Change the definition of a paragraph start.
131 (let ((ps-re "[ \t\n\f]\\|"))
132 (if (eq t (compare-strings ps-re nil nil
133 paragraph-start nil (length ps-re)))
134 (if (not paragraph-indent-minor-mode)
135 (set (make-local-variable 'paragraph-start)
136 (substring paragraph-start (length ps-re))))
137 (if paragraph-indent-minor-mode
138 (set (make-local-variable 'paragraph-start)
139 (concat ps-re paragraph-start)))))
140 ;; Change the indentation function.
141 (if paragraph-indent-minor-mode
142 (add-function :override (local 'indent-line-function)
143 #'indent-to-left-margin)
144 (remove-function (local 'indent-line-function)
145 #'indent-to-left-margin)))
146
147 (defalias 'indented-text-mode 'text-mode)
148
149 ;; This can be made a no-op once all modes that use text-mode-hook
150 ;; are "derived" from text-mode.
151 (defun text-mode-hook-identify ()
152 "Mark that this mode has run `text-mode-hook'.
153 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
154 (set (make-local-variable 'text-mode-variant) t))
155
156 (add-hook 'text-mode-hook 'text-mode-hook-identify)
157
158 (defun toggle-text-mode-auto-fill ()
159 "Toggle whether to use Auto Fill in Text mode and related modes.
160 This command affects all buffers that use modes related to Text mode,
161 both existing buffers and buffers that you subsequently create."
162 (interactive)
163 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
164 (if enable-mode
165 (add-hook 'text-mode-hook 'turn-on-auto-fill)
166 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
167 (dolist (buffer (buffer-list))
168 (with-current-buffer buffer
169 (if (or (derived-mode-p 'text-mode) text-mode-variant)
170 (auto-fill-mode (if enable-mode 1 0)))))
171 (message "Auto Fill %s in Text modes"
172 (if enable-mode "enabled" "disabled"))))
173 \f
174
175 (define-key facemenu-keymap "\eS" 'center-paragraph)
176
177 (defun center-paragraph ()
178 "Center each nonblank line in the paragraph at or after point.
179 See `center-line' for more info."
180 (interactive)
181 (save-excursion
182 (forward-paragraph)
183 (or (bolp) (newline 1))
184 (let ((end (point)))
185 (backward-paragraph)
186 (center-region (point) end))))
187
188 (defun center-region (from to)
189 "Center each nonblank line starting in the region.
190 See `center-line' for more info."
191 (interactive "r")
192 (if (> from to)
193 (let ((tem to))
194 (setq to from from tem)))
195 (save-excursion
196 (save-restriction
197 (narrow-to-region from to)
198 (goto-char from)
199 (while (not (eobp))
200 (or (save-excursion (skip-chars-forward " \t") (eolp))
201 (center-line))
202 (forward-line 1)))))
203
204 (define-key facemenu-keymap "\es" 'center-line)
205
206 (defun center-line (&optional nlines)
207 "Center the line point is on, within the width specified by `fill-column'.
208 This means adjusting the indentation so that it equals
209 the distance between the end of the text and `fill-column'.
210 The argument NLINES says how many lines to center."
211 (interactive "P")
212 (if nlines (setq nlines (prefix-numeric-value nlines)))
213 (while (not (eq nlines 0))
214 (save-excursion
215 (let ((lm (current-left-margin))
216 line-length)
217 (beginning-of-line)
218 (delete-horizontal-space)
219 (end-of-line)
220 (delete-horizontal-space)
221 (setq line-length (current-column))
222 (if (> (- fill-column lm line-length) 0)
223 (indent-line-to
224 (+ lm (/ (- fill-column lm line-length) 2))))))
225 (cond ((null nlines)
226 (setq nlines 0))
227 ((> nlines 0)
228 (setq nlines (1- nlines))
229 (forward-line 1))
230 ((< nlines 0)
231 (setq nlines (1+ nlines))
232 (forward-line -1)))))
233
234 ;;; text-mode.el ends here