Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; text-mode.el --- text mode, and its idiosyncratic commands |
d501f516 | 2 | |
3731a850 TTN |
3 | ;; Copyright (C) 1985, 1992, 1994, 2002, 2003, 2004, |
4 | ;; 2005 Free Software Foundation, Inc. | |
a2535589 | 5 | |
58142744 | 6 | ;; Maintainer: FSF |
6228c05b | 7 | ;; Keywords: wp |
58142744 | 8 | |
a2535589 JA |
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 | |
daa37602 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
a2535589 JA |
14 | ;; 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 | |
b578f267 | 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
4fc5845f LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
d9ecc911 ER |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;; This package provides the fundamental text mode documented in the | |
29 | ;; Emacs user's manual. | |
30 | ||
22a89ee8 | 31 | ;;; Code: |
a2535589 | 32 | |
e144fd4e DL |
33 | (defcustom text-mode-hook nil |
34 | "Normal hook run when entering Text mode and many related modes." | |
35 | :type 'hook | |
84347194 | 36 | :options '(turn-on-auto-fill flyspell-mode) |
e144fd4e | 37 | :group 'data) |
047d1f89 | 38 | |
4c69a3be | 39 | (defvar text-mode-variant nil |
9e59bf58 SM |
40 | "Non-nil if this buffer's major mode is a variant of Text mode. |
41 | Use (derived-mode-p 'text-mode) instead.") | |
42 | ||
43 | (defvar text-mode-syntax-table | |
44 | (let ((st (make-syntax-table))) | |
45 | (modify-syntax-entry ?\" ". " st) | |
46 | (modify-syntax-entry ?\\ ". " st) | |
61ed2dcc SM |
47 | ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'. |
48 | (modify-syntax-entry ?' "w p" st) | |
9e59bf58 SM |
49 | st) |
50 | "Syntax table used while in `text-mode'.") | |
51 | ||
52 | (defvar text-mode-map | |
53 | (let ((map (make-sparse-keymap))) | |
54 | (define-key map "\e\t" 'ispell-complete-word) | |
55 | (define-key map "\es" 'center-line) | |
56 | (define-key map "\eS" 'center-paragraph) | |
57 | map) | |
58 | "Keymap for `text-mode'. | |
59 | Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode', | |
a2535589 JA |
60 | inherit all the commands defined in this map.") |
61 | ||
a2535589 | 62 | \f |
fdf1c7c3 | 63 | (define-derived-mode text-mode nil "Text" |
6824710a | 64 | "Major mode for editing text written for humans to read. |
91fe8c37 | 65 | In this mode, paragraphs are delimited only by blank or white lines. |
6824710a RS |
66 | You can thus get the full benefit of adaptive filling |
67 | (see the variable `adaptive-fill-mode'). | |
6b4bde1b | 68 | \\{text-mode-map} |
6824710a | 69 | Turning on Text mode runs the normal hook `text-mode-hook'." |
540f653d RS |
70 | (make-local-variable 'text-mode-variant) |
71 | (setq text-mode-variant t) | |
706d2537 RS |
72 | (set (make-local-variable 'require-final-newline) |
73 | mode-require-final-newline) | |
9e59bf58 | 74 | (set (make-local-variable 'indent-line-function) 'indent-relative)) |
6824710a | 75 | |
0fb36e36 | 76 | (define-derived-mode paragraph-indent-text-mode text-mode "Parindent" |
6824710a RS |
77 | "Major mode for editing text, with leading spaces starting a paragraph. |
78 | In this mode, you do not need blank lines between paragraphs | |
79 | when the first line of the following paragraph starts with whitespace. | |
9f78d51a | 80 | `paragraph-indent-minor-mode' provides a similar facility as a minor mode. |
6824710a RS |
81 | Special commands: |
82 | \\{text-mode-map} | |
946340ae RS |
83 | Turning on Paragraph-Indent Text mode runs the normal hooks |
84 | `text-mode-hook' and `paragraph-indent-text-mode-hook'." | |
61ed2dcc | 85 | :abbrev-table nil :syntax-table nil |
9e59bf58 | 86 | (paragraph-indent-minor-mode)) |
9f78d51a DL |
87 | |
88 | (defun paragraph-indent-minor-mode () | |
89 | "Minor mode for editing text, with leading spaces starting a paragraph. | |
90 | In this mode, you do not need blank lines between paragraphs when the | |
91 | first line of the following paragraph starts with whitespace, as with | |
57fff5de | 92 | `paragraph-indent-text-mode'. |
9f78d51a DL |
93 | Turning on Paragraph-Indent minor mode runs the normal hook |
94 | `paragraph-indent-text-mode-hook'." | |
95 | (interactive) | |
96 | (set (make-local-variable 'paragraph-start) | |
0fb36e36 SM |
97 | (concat "[ \t\n\f]\\|" paragraph-start)) |
98 | (set (make-local-variable 'indent-line-function) 'indent-to-left-margin) | |
9f78d51a | 99 | (run-hooks 'paragraph-indent-text-mode-hook)) |
db95369b | 100 | |
946340ae | 101 | (defalias 'indented-text-mode 'text-mode) |
a2535589 | 102 | |
ebda95a2 RS |
103 | ;; This can be made a no-op once all modes that use text-mode-hook |
104 | ;; are "derived" from text-mode. | |
105 | (defun text-mode-hook-identify () | |
106 | "Mark that this mode has run `text-mode-hook'. | |
107 | This is how `toggle-text-mode-auto-fill' knows which buffers to operate on." | |
108 | (set (make-local-variable 'text-mode-variant) t)) | |
109 | ||
110 | (add-hook 'text-mode-hook 'text-mode-hook-identify) | |
def08a3f | 111 | |
4c69a3be RS |
112 | (defun toggle-text-mode-auto-fill () |
113 | "Toggle whether to use Auto Fill in Text mode and related modes. | |
114 | This command affects all buffers that use modes related to Text mode, | |
115 | both existing buffers and buffers that you subsequently create." | |
116 | (interactive) | |
fdf1c7c3 | 117 | (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook)))) |
4c69a3be RS |
118 | (if enable-mode |
119 | (add-hook 'text-mode-hook 'turn-on-auto-fill) | |
120 | (remove-hook 'text-mode-hook 'turn-on-auto-fill)) | |
fdf1c7c3 SM |
121 | (dolist (buffer (buffer-list)) |
122 | (with-current-buffer buffer | |
123 | (if (or (derived-mode-p 'text-mode) text-mode-variant) | |
124 | (auto-fill-mode (if enable-mode 1 0))))) | |
4c69a3be RS |
125 | (message "Auto Fill %s in Text modes" |
126 | (if enable-mode "enabled" "disabled")))) | |
127 | \f | |
a2535589 JA |
128 | (defun center-paragraph () |
129 | "Center each nonblank line in the paragraph at or after point. | |
d367fa71 | 130 | See `center-line' for more info." |
a2535589 JA |
131 | (interactive) |
132 | (save-excursion | |
133 | (forward-paragraph) | |
134 | (or (bolp) (newline 1)) | |
135 | (let ((end (point))) | |
136 | (backward-paragraph) | |
137 | (center-region (point) end)))) | |
138 | ||
139 | (defun center-region (from to) | |
140 | "Center each nonblank line starting in the region. | |
d367fa71 | 141 | See `center-line' for more info." |
a2535589 JA |
142 | (interactive "r") |
143 | (if (> from to) | |
144 | (let ((tem to)) | |
145 | (setq to from from tem))) | |
146 | (save-excursion | |
147 | (save-restriction | |
148 | (narrow-to-region from to) | |
149 | (goto-char from) | |
150 | (while (not (eobp)) | |
151 | (or (save-excursion (skip-chars-forward " \t") (eolp)) | |
152 | (center-line)) | |
153 | (forward-line 1))))) | |
154 | ||
aaf6c7ef | 155 | (defun center-line (&optional nlines) |
a2535589 JA |
156 | "Center the line point is on, within the width specified by `fill-column'. |
157 | This means adjusting the indentation so that it equals | |
aaf6c7ef RS |
158 | the distance between the end of the text and `fill-column'. |
159 | The argument NLINES says how many lines to center." | |
160 | (interactive "P") | |
161 | (if nlines (setq nlines (prefix-numeric-value nlines))) | |
162 | (while (not (eq nlines 0)) | |
163 | (save-excursion | |
164 | (let ((lm (current-left-margin)) | |
165 | line-length) | |
166 | (beginning-of-line) | |
167 | (delete-horizontal-space) | |
168 | (end-of-line) | |
169 | (delete-horizontal-space) | |
170 | (setq line-length (current-column)) | |
171 | (if (> (- fill-column lm line-length) 0) | |
84347194 | 172 | (indent-line-to |
aaf6c7ef RS |
173 | (+ lm (/ (- fill-column lm line-length) 2)))))) |
174 | (cond ((null nlines) | |
175 | (setq nlines 0)) | |
176 | ((> nlines 0) | |
177 | (setq nlines (1- nlines)) | |
178 | (forward-line 1)) | |
179 | ((< nlines 0) | |
180 | (setq nlines (1+ nlines)) | |
181 | (forward-line -1))))) | |
d501f516 | 182 | |
ab5796a9 | 183 | ;;; arch-tag: a07ccaad-da13-4d7b-9c61-cd04f5926aab |
d501f516 | 184 | ;;; text-mode.el ends here |