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 | 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) | |
46 | (modify-syntax-entry ?' "w " 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 "\es" 'center-line) | |
54 | (define-key map "\eS" 'center-paragraph) | |
55 | map) | |
56 | "Keymap for `text-mode'. | |
57 | Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode', | |
a2535589 JA |
58 | inherit all the commands defined in this map.") |
59 | ||
a2535589 | 60 | \f |
fdf1c7c3 | 61 | (define-derived-mode text-mode nil "Text" |
6824710a | 62 | "Major mode for editing text written for humans to read. |
91fe8c37 | 63 | In this mode, paragraphs are delimited only by blank or white lines. |
6824710a RS |
64 | You can thus get the full benefit of adaptive filling |
65 | (see the variable `adaptive-fill-mode'). | |
6b4bde1b | 66 | \\{text-mode-map} |
6824710a | 67 | Turning on Text mode runs the normal hook `text-mode-hook'." |
9e59bf58 | 68 | (set (make-local-variable 'indent-line-function) 'indent-relative)) |
6824710a | 69 | |
0fb36e36 | 70 | (define-derived-mode paragraph-indent-text-mode text-mode "Parindent" |
6824710a RS |
71 | "Major mode for editing text, with leading spaces starting a paragraph. |
72 | In this mode, you do not need blank lines between paragraphs | |
73 | when the first line of the following paragraph starts with whitespace. | |
9f78d51a | 74 | `paragraph-indent-minor-mode' provides a similar facility as a minor mode. |
6824710a RS |
75 | Special commands: |
76 | \\{text-mode-map} | |
946340ae RS |
77 | Turning on Paragraph-Indent Text mode runs the normal hooks |
78 | `text-mode-hook' and `paragraph-indent-text-mode-hook'." | |
9e59bf58 | 79 | (paragraph-indent-minor-mode)) |
9f78d51a DL |
80 | |
81 | (defun paragraph-indent-minor-mode () | |
82 | "Minor mode for editing text, with leading spaces starting a paragraph. | |
83 | In this mode, you do not need blank lines between paragraphs when the | |
84 | first line of the following paragraph starts with whitespace, as with | |
85 | `paragraph-indent-mode'. | |
86 | Turning on Paragraph-Indent minor mode runs the normal hook | |
87 | `paragraph-indent-text-mode-hook'." | |
88 | (interactive) | |
89 | (set (make-local-variable 'paragraph-start) | |
0fb36e36 SM |
90 | (concat "[ \t\n\f]\\|" paragraph-start)) |
91 | (set (make-local-variable 'indent-line-function) 'indent-to-left-margin) | |
9f78d51a | 92 | (run-hooks 'paragraph-indent-text-mode-hook)) |
6824710a | 93 | |
946340ae | 94 | (defalias 'indented-text-mode 'text-mode) |
a2535589 | 95 | |
d910f08c RS |
96 | (defun text-mode-hook-identify () |
97 | "Mark that this mode has run `text-mode-hook'. | |
98 | This is how `toggle-text-mode-auto-fill' knows which buffers to operate on." | |
9e59bf58 | 99 | (set (make-local-variable 'text-mode-variant) t)) |
d910f08c RS |
100 | |
101 | (add-hook 'text-mode-hook 'text-mode-hook-identify) | |
102 | ||
4c69a3be RS |
103 | (defun toggle-text-mode-auto-fill () |
104 | "Toggle whether to use Auto Fill in Text mode and related modes. | |
105 | This command affects all buffers that use modes related to Text mode, | |
106 | both existing buffers and buffers that you subsequently create." | |
107 | (interactive) | |
fdf1c7c3 | 108 | (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook)))) |
4c69a3be RS |
109 | (if enable-mode |
110 | (add-hook 'text-mode-hook 'turn-on-auto-fill) | |
111 | (remove-hook 'text-mode-hook 'turn-on-auto-fill)) | |
fdf1c7c3 SM |
112 | (dolist (buffer (buffer-list)) |
113 | (with-current-buffer buffer | |
114 | (if (or (derived-mode-p 'text-mode) text-mode-variant) | |
115 | (auto-fill-mode (if enable-mode 1 0))))) | |
4c69a3be RS |
116 | (message "Auto Fill %s in Text modes" |
117 | (if enable-mode "enabled" "disabled")))) | |
118 | \f | |
a2535589 JA |
119 | (defun center-paragraph () |
120 | "Center each nonblank line in the paragraph at or after point. | |
d367fa71 | 121 | See `center-line' for more info." |
a2535589 JA |
122 | (interactive) |
123 | (save-excursion | |
124 | (forward-paragraph) | |
125 | (or (bolp) (newline 1)) | |
126 | (let ((end (point))) | |
127 | (backward-paragraph) | |
128 | (center-region (point) end)))) | |
129 | ||
130 | (defun center-region (from to) | |
131 | "Center each nonblank line starting in the region. | |
d367fa71 | 132 | See `center-line' for more info." |
a2535589 JA |
133 | (interactive "r") |
134 | (if (> from to) | |
135 | (let ((tem to)) | |
136 | (setq to from from tem))) | |
137 | (save-excursion | |
138 | (save-restriction | |
139 | (narrow-to-region from to) | |
140 | (goto-char from) | |
141 | (while (not (eobp)) | |
142 | (or (save-excursion (skip-chars-forward " \t") (eolp)) | |
143 | (center-line)) | |
144 | (forward-line 1))))) | |
145 | ||
aaf6c7ef | 146 | (defun center-line (&optional nlines) |
a2535589 JA |
147 | "Center the line point is on, within the width specified by `fill-column'. |
148 | This means adjusting the indentation so that it equals | |
aaf6c7ef RS |
149 | the distance between the end of the text and `fill-column'. |
150 | The argument NLINES says how many lines to center." | |
151 | (interactive "P") | |
152 | (if nlines (setq nlines (prefix-numeric-value nlines))) | |
153 | (while (not (eq nlines 0)) | |
154 | (save-excursion | |
155 | (let ((lm (current-left-margin)) | |
156 | line-length) | |
157 | (beginning-of-line) | |
158 | (delete-horizontal-space) | |
159 | (end-of-line) | |
160 | (delete-horizontal-space) | |
161 | (setq line-length (current-column)) | |
162 | (if (> (- fill-column lm line-length) 0) | |
84347194 | 163 | (indent-line-to |
aaf6c7ef RS |
164 | (+ lm (/ (- fill-column lm line-length) 2)))))) |
165 | (cond ((null nlines) | |
166 | (setq nlines 0)) | |
167 | ((> nlines 0) | |
168 | (setq nlines (1- nlines)) | |
169 | (forward-line 1)) | |
170 | ((< nlines 0) | |
171 | (setq nlines (1+ nlines)) | |
172 | (forward-line -1))))) | |
d501f516 ER |
173 | |
174 | ;;; text-mode.el ends here |