Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; text-mode.el --- text mode, and its idiosyncratic commands |
d501f516 | 2 | |
8f1204db | 3 | ;; Copyright (C) 1985, 1992, 1994 Free Software Foundation, Inc. |
a2535589 | 4 | |
58142744 | 5 | ;; Maintainer: FSF |
6228c05b | 6 | ;; Keywords: wp |
58142744 | 7 | |
a2535589 JA |
8 | ;; This file is part of GNU Emacs. |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
daa37602 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
a2535589 JA |
13 | ;; any later version. |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
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 | |
84347194 | 35 | :options '(turn-on-auto-fill flyspell-mode) |
e144fd4e | 36 | :group 'data) |
047d1f89 | 37 | |
4c69a3be RS |
38 | (defvar text-mode-variant nil |
39 | "Non-nil if this buffer's major mode is a variant of Text mode.") | |
40 | ||
a2535589 JA |
41 | (defvar text-mode-syntax-table nil |
42 | "Syntax table used while in text mode.") | |
43 | ||
44 | (defvar text-mode-abbrev-table nil | |
45 | "Abbrev table used while in text mode.") | |
46 | (define-abbrev-table 'text-mode-abbrev-table ()) | |
47 | ||
48 | (if text-mode-syntax-table | |
49 | () | |
50 | (setq text-mode-syntax-table (make-syntax-table)) | |
51 | (modify-syntax-entry ?\" ". " text-mode-syntax-table) | |
52 | (modify-syntax-entry ?\\ ". " text-mode-syntax-table) | |
53 | (modify-syntax-entry ?' "w " text-mode-syntax-table)) | |
54 | ||
55 | (defvar text-mode-map nil | |
56 | "Keymap for Text mode. | |
57 | Many other modes, such as Mail mode, Outline mode and Indented Text mode, | |
58 | inherit all the commands defined in this map.") | |
59 | ||
60 | (if text-mode-map | |
61 | () | |
62 | (setq text-mode-map (make-sparse-keymap)) | |
fb14a9a7 | 63 | (define-key text-mode-map "\e\t" 'ispell-complete-word) |
946340ae | 64 | (define-key text-mode-map "\t" 'indent-relative) |
a2535589 JA |
65 | (define-key text-mode-map "\es" 'center-line) |
66 | (define-key text-mode-map "\eS" 'center-paragraph)) | |
67 | ||
68 | \f | |
fdf1c7c3 | 69 | (define-derived-mode text-mode nil "Text" |
6824710a | 70 | "Major mode for editing text written for humans to read. |
91fe8c37 | 71 | In this mode, paragraphs are delimited only by blank or white lines. |
6824710a RS |
72 | You can thus get the full benefit of adaptive filling |
73 | (see the variable `adaptive-fill-mode'). | |
6b4bde1b | 74 | \\{text-mode-map} |
6824710a | 75 | Turning on Text mode runs the normal hook `text-mode-hook'." |
6824710a | 76 | (make-local-variable 'paragraph-start) |
855b9e33 | 77 | (setq paragraph-start (concat page-delimiter "\\|[ \t]*$")) |
9f78d51a DL |
78 | (if (eq ?^ (aref paragraph-start 0)) |
79 | (setq paragraph-start (substring paragraph-start 1))) | |
6824710a RS |
80 | (make-local-variable 'paragraph-separate) |
81 | (setq paragraph-separate paragraph-start) | |
85761533 | 82 | (make-local-variable 'indent-line-function) |
fdf1c7c3 | 83 | (setq indent-line-function 'indent-relative-maybe)) |
6824710a | 84 | |
946340ae | 85 | (defun paragraph-indent-text-mode () |
6824710a RS |
86 | "Major mode for editing text, with leading spaces starting a paragraph. |
87 | In this mode, you do not need blank lines between paragraphs | |
88 | when the first line of the following paragraph starts with whitespace. | |
9f78d51a | 89 | `paragraph-indent-minor-mode' provides a similar facility as a minor mode. |
6824710a RS |
90 | Special commands: |
91 | \\{text-mode-map} | |
946340ae RS |
92 | Turning on Paragraph-Indent Text mode runs the normal hooks |
93 | `text-mode-hook' and `paragraph-indent-text-mode-hook'." | |
6824710a RS |
94 | (interactive) |
95 | (kill-all-local-variables) | |
96 | (use-local-map text-mode-map) | |
946340ae RS |
97 | (setq mode-name "Parindent") |
98 | (setq major-mode 'paragraph-indent-text-mode) | |
a2535589 JA |
99 | (setq local-abbrev-table text-mode-abbrev-table) |
100 | (set-syntax-table text-mode-syntax-table) | |
946340ae | 101 | (run-hooks 'text-mode-hook 'paragraph-indent-text-mode-hook)) |
9f78d51a DL |
102 | |
103 | (defun paragraph-indent-minor-mode () | |
104 | "Minor mode for editing text, with leading spaces starting a paragraph. | |
105 | In this mode, you do not need blank lines between paragraphs when the | |
106 | first line of the following paragraph starts with whitespace, as with | |
107 | `paragraph-indent-mode'. | |
108 | Turning on Paragraph-Indent minor mode runs the normal hook | |
109 | `paragraph-indent-text-mode-hook'." | |
110 | (interactive) | |
111 | (set (make-local-variable 'paragraph-start) | |
112 | (default-value 'paragraph-start)) | |
113 | (set (make-local-variable 'paragraph-separate) paragraph-start) | |
114 | (run-hooks 'paragraph-indent-text-mode-hook)) | |
6824710a | 115 | |
946340ae | 116 | (defalias 'indented-text-mode 'text-mode) |
a2535589 | 117 | |
d910f08c RS |
118 | (defun text-mode-hook-identify () |
119 | "Mark that this mode has run `text-mode-hook'. | |
120 | This is how `toggle-text-mode-auto-fill' knows which buffers to operate on." | |
121 | (make-local-variable 'text-mode-variant) | |
122 | (setq text-mode-variant t)) | |
123 | ||
124 | (add-hook 'text-mode-hook 'text-mode-hook-identify) | |
125 | ||
4c69a3be RS |
126 | (defun toggle-text-mode-auto-fill () |
127 | "Toggle whether to use Auto Fill in Text mode and related modes. | |
128 | This command affects all buffers that use modes related to Text mode, | |
129 | both existing buffers and buffers that you subsequently create." | |
130 | (interactive) | |
fdf1c7c3 | 131 | (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook)))) |
4c69a3be RS |
132 | (if enable-mode |
133 | (add-hook 'text-mode-hook 'turn-on-auto-fill) | |
134 | (remove-hook 'text-mode-hook 'turn-on-auto-fill)) | |
fdf1c7c3 SM |
135 | (dolist (buffer (buffer-list)) |
136 | (with-current-buffer buffer | |
137 | (if (or (derived-mode-p 'text-mode) text-mode-variant) | |
138 | (auto-fill-mode (if enable-mode 1 0))))) | |
4c69a3be RS |
139 | (message "Auto Fill %s in Text modes" |
140 | (if enable-mode "enabled" "disabled")))) | |
141 | \f | |
a2535589 JA |
142 | (defun center-paragraph () |
143 | "Center each nonblank line in the paragraph at or after point. | |
d367fa71 | 144 | See `center-line' for more info." |
a2535589 JA |
145 | (interactive) |
146 | (save-excursion | |
147 | (forward-paragraph) | |
148 | (or (bolp) (newline 1)) | |
149 | (let ((end (point))) | |
150 | (backward-paragraph) | |
151 | (center-region (point) end)))) | |
152 | ||
153 | (defun center-region (from to) | |
154 | "Center each nonblank line starting in the region. | |
d367fa71 | 155 | See `center-line' for more info." |
a2535589 JA |
156 | (interactive "r") |
157 | (if (> from to) | |
158 | (let ((tem to)) | |
159 | (setq to from from tem))) | |
160 | (save-excursion | |
161 | (save-restriction | |
162 | (narrow-to-region from to) | |
163 | (goto-char from) | |
164 | (while (not (eobp)) | |
165 | (or (save-excursion (skip-chars-forward " \t") (eolp)) | |
166 | (center-line)) | |
167 | (forward-line 1))))) | |
168 | ||
aaf6c7ef | 169 | (defun center-line (&optional nlines) |
a2535589 JA |
170 | "Center the line point is on, within the width specified by `fill-column'. |
171 | This means adjusting the indentation so that it equals | |
aaf6c7ef RS |
172 | the distance between the end of the text and `fill-column'. |
173 | The argument NLINES says how many lines to center." | |
174 | (interactive "P") | |
175 | (if nlines (setq nlines (prefix-numeric-value nlines))) | |
176 | (while (not (eq nlines 0)) | |
177 | (save-excursion | |
178 | (let ((lm (current-left-margin)) | |
179 | line-length) | |
180 | (beginning-of-line) | |
181 | (delete-horizontal-space) | |
182 | (end-of-line) | |
183 | (delete-horizontal-space) | |
184 | (setq line-length (current-column)) | |
185 | (if (> (- fill-column lm line-length) 0) | |
84347194 | 186 | (indent-line-to |
aaf6c7ef RS |
187 | (+ lm (/ (- fill-column lm line-length) 2)))))) |
188 | (cond ((null nlines) | |
189 | (setq nlines 0)) | |
190 | ((> nlines 0) | |
191 | (setq nlines (1- nlines)) | |
192 | (forward-line 1)) | |
193 | ((< nlines 0) | |
194 | (setq nlines (1+ nlines)) | |
195 | (forward-line -1))))) | |
d501f516 ER |
196 | |
197 | ;;; text-mode.el ends here |