Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; text-mode.el --- text mode, and its idiosyncratic commands |
d501f516 | 2 | |
ab422c4d PE |
3 | ;; Copyright (C) 1985, 1992, 1994, 2001-2013 Free Software Foundation, |
4 | ;; Inc. | |
a2535589 | 5 | |
58142744 | 6 | ;; Maintainer: FSF |
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) | |
9e59bf58 SM |
54 | map) |
55 | "Keymap for `text-mode'. | |
56 | Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode', | |
a2535589 JA |
57 | inherit all the commands defined in this map.") |
58 | ||
a2535589 | 59 | \f |
fdf1c7c3 | 60 | (define-derived-mode text-mode nil "Text" |
6824710a | 61 | "Major mode for editing text written for humans to read. |
91fe8c37 | 62 | In this mode, paragraphs are delimited only by blank or white lines. |
6824710a RS |
63 | You can thus get the full benefit of adaptive filling |
64 | (see the variable `adaptive-fill-mode'). | |
6b4bde1b | 65 | \\{text-mode-map} |
6824710a | 66 | Turning on Text mode runs the normal hook `text-mode-hook'." |
9962192e | 67 | (set (make-local-variable 'text-mode-variant) t) |
706d2537 RS |
68 | (set (make-local-variable 'require-final-newline) |
69 | mode-require-final-newline) | |
9e59bf58 | 70 | (set (make-local-variable 'indent-line-function) 'indent-relative)) |
6824710a | 71 | |
0fb36e36 | 72 | (define-derived-mode paragraph-indent-text-mode text-mode "Parindent" |
6824710a RS |
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. | |
9f78d51a | 76 | `paragraph-indent-minor-mode' provides a similar facility as a minor mode. |
6824710a RS |
77 | Special commands: |
78 | \\{text-mode-map} | |
946340ae RS |
79 | Turning on Paragraph-Indent Text mode runs the normal hooks |
80 | `text-mode-hook' and `paragraph-indent-text-mode-hook'." | |
61ed2dcc | 81 | :abbrev-table nil :syntax-table nil |
9e59bf58 | 82 | (paragraph-indent-minor-mode)) |
9f78d51a | 83 | |
4ffb41a9 | 84 | (define-minor-mode paragraph-indent-minor-mode |
9f78d51a DL |
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 | |
57fff5de | 88 | `paragraph-indent-text-mode'. |
9f78d51a DL |
89 | Turning on Paragraph-Indent minor mode runs the normal hook |
90 | `paragraph-indent-text-mode-hook'." | |
4ffb41a9 SM |
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)))) | |
db95369b | 107 | |
946340ae | 108 | (defalias 'indented-text-mode 'text-mode) |
a2535589 | 109 | |
ebda95a2 RS |
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)) | |
116 | ||
117 | (add-hook 'text-mode-hook 'text-mode-hook-identify) | |
def08a3f | 118 | |
4c69a3be RS |
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) | |
fdf1c7c3 | 124 | (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook)))) |
4c69a3be RS |
125 | (if enable-mode |
126 | (add-hook 'text-mode-hook 'turn-on-auto-fill) | |
127 | (remove-hook 'text-mode-hook 'turn-on-auto-fill)) | |
fdf1c7c3 SM |
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))))) | |
4c69a3be RS |
132 | (message "Auto Fill %s in Text modes" |
133 | (if enable-mode "enabled" "disabled")))) | |
134 | \f | |
4603452c JL |
135 | |
136 | (define-key facemenu-keymap "\eS" 'center-paragraph) | |
137 | ||
a2535589 JA |
138 | (defun center-paragraph () |
139 | "Center each nonblank line in the paragraph at or after point. | |
d367fa71 | 140 | See `center-line' for more info." |
a2535589 JA |
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)))) | |
148 | ||
149 | (defun center-region (from to) | |
150 | "Center each nonblank line starting in the region. | |
d367fa71 | 151 | See `center-line' for more info." |
a2535589 JA |
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))))) | |
164 | ||
4603452c JL |
165 | (define-key facemenu-keymap "\es" 'center-line) |
166 | ||
aaf6c7ef | 167 | (defun center-line (&optional nlines) |
a2535589 JA |
168 | "Center the line point is on, within the width specified by `fill-column'. |
169 | This means adjusting the indentation so that it equals | |
aaf6c7ef RS |
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) | |
84347194 | 184 | (indent-line-to |
aaf6c7ef RS |
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))))) | |
d501f516 ER |
194 | |
195 | ;;; text-mode.el ends here |