Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; text-mode.el --- text mode, and its idiosyncratic commands |
d501f516 | 2 | |
ba318903 PE |
3 | ;; Copyright (C) 1985, 1992, 1994, 2001-2014 Free Software Foundation, |
4 | ;; Inc. | |
a2535589 | 5 | |
34dc21db | 6 | ;; Maintainer: emacs-devel@gnu.org |
6228c05b | 7 | ;; Keywords: wp |
bd78fa1d | 8 | ;; Package: emacs |
58142744 | 9 | |
a2535589 JA |
10 | ;; This file is part of GNU Emacs. |
11 | ||
1fecc8fe | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
a2535589 | 13 | ;; it under the terms of the GNU General Public License as published by |
1fecc8fe GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
a2535589 JA |
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 | |
1fecc8fe | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
d9ecc911 ER |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; This package provides the fundamental text mode documented in the | |
28 | ;; Emacs user's manual. | |
29 | ||
22a89ee8 | 30 | ;;; Code: |
a2535589 | 31 | |
e144fd4e DL |
32 | (defcustom text-mode-hook nil |
33 | "Normal hook run when entering Text mode and many related modes." | |
34 | :type 'hook | |
2809db33 | 35 | :options '(turn-on-auto-fill turn-on-flyspell) |
eba5b4dd | 36 | :group 'wp) |
047d1f89 | 37 | |
4c69a3be | 38 | (defvar text-mode-variant nil |
9e59bf58 SM |
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) | |
61ed2dcc SM |
46 | ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'. |
47 | (modify-syntax-entry ?' "w p" st) | |
9e59bf58 SM |
48 | st) |
49 | "Syntax table used while in `text-mode'.") | |
50 | ||
51 | (defvar text-mode-map | |
52 | (let ((map (make-sparse-keymap))) | |
53 | (define-key map "\e\t" 'ispell-complete-word) | |
a066720c JA |
54 | (define-key map [menu-bar text] |
55 | (cons "Text" (make-sparse-keymap "Text"))) | |
56 | (bindings--define-key map [menu-bar text toggle-text-mode-auto-fill] | |
57 | '(menu-item "Auto Fill" toggle-text-mode-auto-fill | |
58 | :button (:toggle . (memq 'turn-on-auto-fill text-mode-hook)) | |
9db13baf | 59 | :help "Automatically fill text while typing in text modes (Auto Fill mode)")) |
a066720c JA |
60 | (bindings--define-key map [menu-bar text paragraph-indent-minor-mode] |
61 | '(menu-item "Paragraph Indent" paragraph-indent-minor-mode | |
62 | :button (:toggle . (bound-and-true-p paragraph-indent-minor-mode)) | |
63 | :help "Toggle paragraph indent minor mode")) | |
64 | (bindings--define-key map [menu-bar text sep] menu-bar-separator) | |
65 | (bindings--define-key map [menu-bar text center-region] | |
66 | '(menu-item "Center Region" center-region | |
67 | :help "Center the marked region" | |
68 | :enable (region-active-p))) | |
69 | (bindings--define-key map [menu-bar text center-paragraph] | |
70 | '(menu-item "Center Paragraph" center-paragraph | |
71 | :help "Center the current paragraph")) | |
72 | (bindings--define-key map [menu-bar text center-line] | |
73 | '(menu-item "Center Line" center-line | |
74 | :help "Center the current line")) | |
9e59bf58 SM |
75 | map) |
76 | "Keymap for `text-mode'. | |
77 | Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode', | |
a2535589 JA |
78 | inherit all the commands defined in this map.") |
79 | ||
a2535589 | 80 | \f |
fdf1c7c3 | 81 | (define-derived-mode text-mode nil "Text" |
6824710a | 82 | "Major mode for editing text written for humans to read. |
91fe8c37 | 83 | In this mode, paragraphs are delimited only by blank or white lines. |
6824710a RS |
84 | You can thus get the full benefit of adaptive filling |
85 | (see the variable `adaptive-fill-mode'). | |
6b4bde1b | 86 | \\{text-mode-map} |
6824710a | 87 | Turning on Text mode runs the normal hook `text-mode-hook'." |
9962192e | 88 | (set (make-local-variable 'text-mode-variant) t) |
706d2537 RS |
89 | (set (make-local-variable 'require-final-newline) |
90 | mode-require-final-newline) | |
9e59bf58 | 91 | (set (make-local-variable 'indent-line-function) 'indent-relative)) |
6824710a | 92 | |
0fb36e36 | 93 | (define-derived-mode paragraph-indent-text-mode text-mode "Parindent" |
6824710a RS |
94 | "Major mode for editing text, with leading spaces starting a paragraph. |
95 | In this mode, you do not need blank lines between paragraphs | |
96 | when the first line of the following paragraph starts with whitespace. | |
9f78d51a | 97 | `paragraph-indent-minor-mode' provides a similar facility as a minor mode. |
6824710a RS |
98 | Special commands: |
99 | \\{text-mode-map} | |
946340ae RS |
100 | Turning on Paragraph-Indent Text mode runs the normal hooks |
101 | `text-mode-hook' and `paragraph-indent-text-mode-hook'." | |
61ed2dcc | 102 | :abbrev-table nil :syntax-table nil |
9e59bf58 | 103 | (paragraph-indent-minor-mode)) |
9f78d51a | 104 | |
4ffb41a9 | 105 | (define-minor-mode paragraph-indent-minor-mode |
9f78d51a DL |
106 | "Minor mode for editing text, with leading spaces starting a paragraph. |
107 | In this mode, you do not need blank lines between paragraphs when the | |
108 | first line of the following paragraph starts with whitespace, as with | |
57fff5de | 109 | `paragraph-indent-text-mode'. |
9f78d51a DL |
110 | Turning on Paragraph-Indent minor mode runs the normal hook |
111 | `paragraph-indent-text-mode-hook'." | |
4ffb41a9 SM |
112 | :initial-value nil |
113 | ;; Change the definition of a paragraph start. | |
114 | (let ((ps-re "[ \t\n\f]\\|")) | |
115 | (if (eq t (compare-strings ps-re nil nil | |
116 | paragraph-start nil (length ps-re))) | |
117 | (if (not paragraph-indent-minor-mode) | |
118 | (set (make-local-variable 'paragraph-start) | |
119 | (substring paragraph-start (length ps-re)))) | |
120 | (if paragraph-indent-minor-mode | |
121 | (set (make-local-variable 'paragraph-start) | |
122 | (concat ps-re paragraph-start))))) | |
123 | ;; Change the indentation function. | |
124 | (if paragraph-indent-minor-mode | |
1f35d401 SM |
125 | (add-function :override (local 'indent-line-function) |
126 | #'indent-to-left-margin) | |
127 | (remove-function (local 'indent-line-function) | |
128 | #'indent-to-left-margin))) | |
db95369b | 129 | |
946340ae | 130 | (defalias 'indented-text-mode 'text-mode) |
a2535589 | 131 | |
ebda95a2 RS |
132 | ;; This can be made a no-op once all modes that use text-mode-hook |
133 | ;; are "derived" from text-mode. | |
134 | (defun text-mode-hook-identify () | |
135 | "Mark that this mode has run `text-mode-hook'. | |
136 | This is how `toggle-text-mode-auto-fill' knows which buffers to operate on." | |
137 | (set (make-local-variable 'text-mode-variant) t)) | |
138 | ||
139 | (add-hook 'text-mode-hook 'text-mode-hook-identify) | |
def08a3f | 140 | |
4c69a3be RS |
141 | (defun toggle-text-mode-auto-fill () |
142 | "Toggle whether to use Auto Fill in Text mode and related modes. | |
143 | This command affects all buffers that use modes related to Text mode, | |
144 | both existing buffers and buffers that you subsequently create." | |
145 | (interactive) | |
fdf1c7c3 | 146 | (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook)))) |
4c69a3be RS |
147 | (if enable-mode |
148 | (add-hook 'text-mode-hook 'turn-on-auto-fill) | |
149 | (remove-hook 'text-mode-hook 'turn-on-auto-fill)) | |
fdf1c7c3 SM |
150 | (dolist (buffer (buffer-list)) |
151 | (with-current-buffer buffer | |
152 | (if (or (derived-mode-p 'text-mode) text-mode-variant) | |
153 | (auto-fill-mode (if enable-mode 1 0))))) | |
4c69a3be RS |
154 | (message "Auto Fill %s in Text modes" |
155 | (if enable-mode "enabled" "disabled")))) | |
156 | \f | |
4603452c JL |
157 | |
158 | (define-key facemenu-keymap "\eS" 'center-paragraph) | |
159 | ||
a2535589 JA |
160 | (defun center-paragraph () |
161 | "Center each nonblank line in the paragraph at or after point. | |
d367fa71 | 162 | See `center-line' for more info." |
a2535589 JA |
163 | (interactive) |
164 | (save-excursion | |
165 | (forward-paragraph) | |
166 | (or (bolp) (newline 1)) | |
167 | (let ((end (point))) | |
168 | (backward-paragraph) | |
169 | (center-region (point) end)))) | |
170 | ||
171 | (defun center-region (from to) | |
172 | "Center each nonblank line starting in the region. | |
d367fa71 | 173 | See `center-line' for more info." |
a2535589 JA |
174 | (interactive "r") |
175 | (if (> from to) | |
176 | (let ((tem to)) | |
177 | (setq to from from tem))) | |
178 | (save-excursion | |
179 | (save-restriction | |
180 | (narrow-to-region from to) | |
181 | (goto-char from) | |
182 | (while (not (eobp)) | |
183 | (or (save-excursion (skip-chars-forward " \t") (eolp)) | |
184 | (center-line)) | |
185 | (forward-line 1))))) | |
186 | ||
4603452c JL |
187 | (define-key facemenu-keymap "\es" 'center-line) |
188 | ||
aaf6c7ef | 189 | (defun center-line (&optional nlines) |
a2535589 JA |
190 | "Center the line point is on, within the width specified by `fill-column'. |
191 | This means adjusting the indentation so that it equals | |
aaf6c7ef RS |
192 | the distance between the end of the text and `fill-column'. |
193 | The argument NLINES says how many lines to center." | |
194 | (interactive "P") | |
195 | (if nlines (setq nlines (prefix-numeric-value nlines))) | |
196 | (while (not (eq nlines 0)) | |
197 | (save-excursion | |
198 | (let ((lm (current-left-margin)) | |
199 | line-length) | |
200 | (beginning-of-line) | |
201 | (delete-horizontal-space) | |
202 | (end-of-line) | |
203 | (delete-horizontal-space) | |
204 | (setq line-length (current-column)) | |
205 | (if (> (- fill-column lm line-length) 0) | |
84347194 | 206 | (indent-line-to |
aaf6c7ef RS |
207 | (+ lm (/ (- fill-column lm line-length) 2)))))) |
208 | (cond ((null nlines) | |
209 | (setq nlines 0)) | |
210 | ((> nlines 0) | |
211 | (setq nlines (1- nlines)) | |
212 | (forward-line 1)) | |
213 | ((< nlines 0) | |
214 | (setq nlines (1+ nlines)) | |
215 | (forward-line -1))))) | |
d501f516 ER |
216 | |
217 | ;;; text-mode.el ends here |