38412
|
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
|
657
|
2
|
7300
|
3 ;; Copyright (C) 1985, 1992, 1994 Free Software Foundation, Inc.
|
841
|
4
|
775
|
5 ;; Maintainer: FSF
|
38697
|
6 ;; Keywords: wp
|
36
|
7
|
|
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
|
727
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
36
|
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
|
14169
|
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.
|
2315
|
24
|
|
25 ;;; Commentary:
|
|
26
|
|
27 ;; This package provides the fundamental text mode documented in the
|
|
28 ;; Emacs user's manual.
|
|
29
|
775
|
30 ;;; Code:
|
36
|
31
|
26598
|
32 (defcustom text-mode-hook nil
|
|
33 "Normal hook run when entering Text mode and many related modes."
|
|
34 :type 'hook
|
28108
|
35 :options '(turn-on-auto-fill flyspell-mode)
|
26598
|
36 :group 'data)
|
19589
|
37
|
19590
|
38 (defvar text-mode-variant nil
|
40484
|
39 "Non-nil if this buffer's major mode is a variant of Text mode.
|
|
40 Use (derived-mode-p 'text-mode) instead.")
|
36
|
41
|
40484
|
42 (defvar text-mode-syntax-table
|
|
43 (let ((st (make-syntax-table)))
|
|
44 (modify-syntax-entry ?\" ". " st)
|
|
45 (modify-syntax-entry ?\\ ". " st)
|
47966
|
46 ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
|
|
47 (modify-syntax-entry ?' "w p" st)
|
40484
|
48 st)
|
|
49 "Syntax table used while in `text-mode'.")
|
36
|
50
|
40484
|
51 (defvar text-mode-map
|
|
52 (let ((map (make-sparse-keymap)))
|
|
53 (define-key map "\e\t" 'ispell-complete-word)
|
|
54 (define-key map "\es" 'center-line)
|
|
55 (define-key map "\eS" 'center-paragraph)
|
|
56 map)
|
|
57 "Keymap for `text-mode'.
|
|
58 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
|
36
|
59 inherit all the commands defined in this map.")
|
|
60
|
|
61
|
40360
|
62 (define-derived-mode text-mode nil "Text"
|
18132
|
63 "Major mode for editing text written for humans to read.
|
18256
|
64 In this mode, paragraphs are delimited only by blank or white lines.
|
18132
|
65 You can thus get the full benefit of adaptive filling
|
|
66 (see the variable `adaptive-fill-mode').
|
6323
|
67 \\{text-mode-map}
|
18132
|
68 Turning on Text mode runs the normal hook `text-mode-hook'."
|
43122
|
69 (make-local-variable 'text-mode-variant)
|
|
70 (setq text-mode-variant t)
|
47213
|
71 (set (make-local-variable 'require-final-newline) t)
|
40484
|
72 (set (make-local-variable 'indent-line-function) 'indent-relative))
|
18132
|
73
|
40481
7b44c3509111
(paragraph-indent-minor-mode): Don't set paragraph-separate.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
74 (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
|
18132
|
75 "Major mode for editing text, with leading spaces starting a paragraph.
|
|
76 In this mode, you do not need blank lines between paragraphs
|
|
77 when the first line of the following paragraph starts with whitespace.
|
27202
|
78 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
|
18132
|
79 Special commands:
|
|
80 \\{text-mode-map}
|
18222
|
81 Turning on Paragraph-Indent Text mode runs the normal hooks
|
|
82 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
|
47966
|
83 :abbrev-table nil :syntax-table nil
|
40484
|
84 (paragraph-indent-minor-mode))
|
27202
|
85
|
|
86 (defun paragraph-indent-minor-mode ()
|
|
87 "Minor mode for editing text, with leading spaces starting a paragraph.
|
|
88 In this mode, you do not need blank lines between paragraphs when the
|
|
89 first line of the following paragraph starts with whitespace, as with
|
|
90 `paragraph-indent-mode'.
|
|
91 Turning on Paragraph-Indent minor mode runs the normal hook
|
|
92 `paragraph-indent-text-mode-hook'."
|
|
93 (interactive)
|
|
94 (set (make-local-variable 'paragraph-start)
|
40481
7b44c3509111
(paragraph-indent-minor-mode): Don't set paragraph-separate.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
95 (concat "[ \t\n\f]\\|" paragraph-start))
|
7b44c3509111
(paragraph-indent-minor-mode): Don't set paragraph-separate.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
96 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
|
27202
|
97 (run-hooks 'paragraph-indent-text-mode-hook))
|
49599
|
98
|
18222
|
99 (defalias 'indented-text-mode 'text-mode)
|
36
|
100
|
43287
|
101 ;; This can be made a no-op once all modes that use text-mode-hook
|
|
102 ;; are "derived" from text-mode.
|
|
103 (defun text-mode-hook-identify ()
|
|
104 "Mark that this mode has run `text-mode-hook'.
|
|
105 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
|
|
106 (set (make-local-variable 'text-mode-variant) t))
|
|
107
|
|
108 (add-hook 'text-mode-hook 'text-mode-hook-identify)
|
43190
|
109
|
19590
|
110 (defun toggle-text-mode-auto-fill ()
|
|
111 "Toggle whether to use Auto Fill in Text mode and related modes.
|
|
112 This command affects all buffers that use modes related to Text mode,
|
|
113 both existing buffers and buffers that you subsequently create."
|
|
114 (interactive)
|
40360
|
115 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
|
19590
|
116 (if enable-mode
|
|
117 (add-hook 'text-mode-hook 'turn-on-auto-fill)
|
|
118 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
|
40360
|
119 (dolist (buffer (buffer-list))
|
|
120 (with-current-buffer buffer
|
|
121 (if (or (derived-mode-p 'text-mode) text-mode-variant)
|
|
122 (auto-fill-mode (if enable-mode 1 0)))))
|
19590
|
123 (message "Auto Fill %s in Text modes"
|
|
124 (if enable-mode "enabled" "disabled"))))
|
|
125
|
36
|
126 (defun center-paragraph ()
|
|
127 "Center each nonblank line in the paragraph at or after point.
|
1576
|
128 See `center-line' for more info."
|
36
|
129 (interactive)
|
|
130 (save-excursion
|
|
131 (forward-paragraph)
|
|
132 (or (bolp) (newline 1))
|
|
133 (let ((end (point)))
|
|
134 (backward-paragraph)
|
|
135 (center-region (point) end))))
|
|
136
|
|
137 (defun center-region (from to)
|
|
138 "Center each nonblank line starting in the region.
|
1576
|
139 See `center-line' for more info."
|
36
|
140 (interactive "r")
|
|
141 (if (> from to)
|
|
142 (let ((tem to))
|
|
143 (setq to from from tem)))
|
|
144 (save-excursion
|
|
145 (save-restriction
|
|
146 (narrow-to-region from to)
|
|
147 (goto-char from)
|
|
148 (while (not (eobp))
|
|
149 (or (save-excursion (skip-chars-forward " \t") (eolp))
|
|
150 (center-line))
|
|
151 (forward-line 1)))))
|
|
152
|
13023
|
153 (defun center-line (&optional nlines)
|
36
|
154 "Center the line point is on, within the width specified by `fill-column'.
|
|
155 This means adjusting the indentation so that it equals
|
13023
|
156 the distance between the end of the text and `fill-column'.
|
|
157 The argument NLINES says how many lines to center."
|
|
158 (interactive "P")
|
|
159 (if nlines (setq nlines (prefix-numeric-value nlines)))
|
|
160 (while (not (eq nlines 0))
|
|
161 (save-excursion
|
|
162 (let ((lm (current-left-margin))
|
|
163 line-length)
|
|
164 (beginning-of-line)
|
|
165 (delete-horizontal-space)
|
|
166 (end-of-line)
|
|
167 (delete-horizontal-space)
|
|
168 (setq line-length (current-column))
|
|
169 (if (> (- fill-column lm line-length) 0)
|
28108
|
170 (indent-line-to
|
13023
|
171 (+ lm (/ (- fill-column lm line-length) 2))))))
|
|
172 (cond ((null nlines)
|
|
173 (setq nlines 0))
|
|
174 ((> nlines 0)
|
|
175 (setq nlines (1- nlines))
|
|
176 (forward-line 1))
|
|
177 ((< nlines 0)
|
|
178 (setq nlines (1+ nlines))
|
|
179 (forward-line -1)))))
|
657
|
180
|
|
181 ;;; text-mode.el ends here
|