]> code.delx.au - gnu-emacs/blob - lisp/textmodes/text-mode.el
6c97ce6372bcf79fad0ed98a27fdad2bfdb789ff
[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-2013 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 (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.")
78
79 \f
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))
91
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))
103
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 (add-function :override (local 'indent-line-function)
125 #'indent-to-left-margin)
126 (remove-function (local 'indent-line-function)
127 #'indent-to-left-margin)))
128
129 (defalias 'indented-text-mode 'text-mode)
130
131 ;; This can be made a no-op once all modes that use text-mode-hook
132 ;; are "derived" from text-mode.
133 (defun text-mode-hook-identify ()
134 "Mark that this mode has run `text-mode-hook'.
135 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
136 (set (make-local-variable 'text-mode-variant) t))
137
138 (add-hook 'text-mode-hook 'text-mode-hook-identify)
139
140 (defun toggle-text-mode-auto-fill ()
141 "Toggle whether to use Auto Fill in Text mode and related modes.
142 This command affects all buffers that use modes related to Text mode,
143 both existing buffers and buffers that you subsequently create."
144 (interactive)
145 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
146 (if enable-mode
147 (add-hook 'text-mode-hook 'turn-on-auto-fill)
148 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
149 (dolist (buffer (buffer-list))
150 (with-current-buffer buffer
151 (if (or (derived-mode-p 'text-mode) text-mode-variant)
152 (auto-fill-mode (if enable-mode 1 0)))))
153 (message "Auto Fill %s in Text modes"
154 (if enable-mode "enabled" "disabled"))))
155 \f
156
157 (define-key facemenu-keymap "\eS" 'center-paragraph)
158
159 (defun center-paragraph ()
160 "Center each nonblank line in the paragraph at or after point.
161 See `center-line' for more info."
162 (interactive)
163 (save-excursion
164 (forward-paragraph)
165 (or (bolp) (newline 1))
166 (let ((end (point)))
167 (backward-paragraph)
168 (center-region (point) end))))
169
170 (defun center-region (from to)
171 "Center each nonblank line starting in the region.
172 See `center-line' for more info."
173 (interactive "r")
174 (if (> from to)
175 (let ((tem to))
176 (setq to from from tem)))
177 (save-excursion
178 (save-restriction
179 (narrow-to-region from to)
180 (goto-char from)
181 (while (not (eobp))
182 (or (save-excursion (skip-chars-forward " \t") (eolp))
183 (center-line))
184 (forward-line 1)))))
185
186 (define-key facemenu-keymap "\es" 'center-line)
187
188 (defun center-line (&optional nlines)
189 "Center the line point is on, within the width specified by `fill-column'.
190 This means adjusting the indentation so that it equals
191 the distance between the end of the text and `fill-column'.
192 The argument NLINES says how many lines to center."
193 (interactive "P")
194 (if nlines (setq nlines (prefix-numeric-value nlines)))
195 (while (not (eq nlines 0))
196 (save-excursion
197 (let ((lm (current-left-margin))
198 line-length)
199 (beginning-of-line)
200 (delete-horizontal-space)
201 (end-of-line)
202 (delete-horizontal-space)
203 (setq line-length (current-column))
204 (if (> (- fill-column lm line-length) 0)
205 (indent-line-to
206 (+ lm (/ (- fill-column lm line-length) 2))))))
207 (cond ((null nlines)
208 (setq nlines 0))
209 ((> nlines 0)
210 (setq nlines (1- nlines))
211 (forward-line 1))
212 ((< nlines 0)
213 (setq nlines (1+ nlines))
214 (forward-line -1)))))
215
216 ;;; text-mode.el ends here