1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1992, 1994, 2001-2013 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This package provides the fundamental text mode documented in the
27 ;; Emacs user's manual.
31 (defcustom text-mode-hook nil
32 "Normal hook run when entering Text mode and many related modes."
34 :options
'(turn-on-auto-fill turn-on-flyspell
)
37 (defvar text-mode-variant nil
38 "Non-nil if this buffer's major mode is a variant of Text mode.
39 Use (derived-mode-p 'text-mode) instead.")
41 (defvar text-mode-syntax-table
42 (let ((st (make-syntax-table)))
43 (modify-syntax-entry ?
\" ". " st
)
44 (modify-syntax-entry ?
\\ ". " st
)
45 ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
46 (modify-syntax-entry ?
' "w p" st
)
48 "Syntax table used while in `text-mode'.")
51 (let ((map (make-sparse-keymap)))
52 (define-key map
"\e\t" 'ispell-complete-word
)
53 (define-key map
[menu-bar text
]
54 (cons "Text" (make-sparse-keymap "Text")))
55 (bindings--define-key map
[menu-bar text toggle-text-mode-auto-fill
]
56 '(menu-item "Auto Fill" toggle-text-mode-auto-fill
57 :button
(:toggle .
(memq 'turn-on-auto-fill text-mode-hook
))
58 :help
"Automatically fill text while typing in text modes (Auto Fill mode)"))
59 (bindings--define-key map
[menu-bar text paragraph-indent-minor-mode
]
60 '(menu-item "Paragraph Indent" paragraph-indent-minor-mode
61 :button
(:toggle .
(bound-and-true-p paragraph-indent-minor-mode
))
62 :help
"Toggle paragraph indent minor mode"))
63 (bindings--define-key map
[menu-bar text sep
] menu-bar-separator
)
64 (bindings--define-key map
[menu-bar text center-region
]
65 '(menu-item "Center Region" center-region
66 :help
"Center the marked region"
67 :enable
(region-active-p)))
68 (bindings--define-key map
[menu-bar text center-paragraph
]
69 '(menu-item "Center Paragraph" center-paragraph
70 :help
"Center the current paragraph"))
71 (bindings--define-key map
[menu-bar text center-line
]
72 '(menu-item "Center Line" center-line
73 :help
"Center the current line"))
75 "Keymap for `text-mode'.
76 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
77 inherit all the commands defined in this map.")
80 (define-derived-mode text-mode nil
"Text"
81 "Major mode for editing text written for humans to read.
82 In this mode, paragraphs are delimited only by blank or white lines.
83 You can thus get the full benefit of adaptive filling
84 (see the variable `adaptive-fill-mode').
86 Turning on Text mode runs the normal hook `text-mode-hook'."
87 (set (make-local-variable 'text-mode-variant
) t
)
88 (set (make-local-variable 'require-final-newline
)
89 mode-require-final-newline
)
90 (set (make-local-variable 'indent-line-function
) 'indent-relative
))
92 (define-derived-mode paragraph-indent-text-mode text-mode
"Parindent"
93 "Major mode for editing text, with leading spaces starting a paragraph.
94 In this mode, you do not need blank lines between paragraphs
95 when the first line of the following paragraph starts with whitespace.
96 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
99 Turning on Paragraph-Indent Text mode runs the normal hooks
100 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
101 :abbrev-table nil
:syntax-table nil
102 (paragraph-indent-minor-mode))
104 (define-minor-mode paragraph-indent-minor-mode
105 "Minor mode for editing text, with leading spaces starting a paragraph.
106 In this mode, you do not need blank lines between paragraphs when the
107 first line of the following paragraph starts with whitespace, as with
108 `paragraph-indent-text-mode'.
109 Turning on Paragraph-Indent minor mode runs the normal hook
110 `paragraph-indent-text-mode-hook'."
112 ;; Change the definition of a paragraph start.
113 (let ((ps-re "[ \t\n\f]\\|"))
114 (if (eq t
(compare-strings ps-re nil nil
115 paragraph-start nil
(length ps-re
)))
116 (if (not paragraph-indent-minor-mode
)
117 (set (make-local-variable 'paragraph-start
)
118 (substring paragraph-start
(length ps-re
))))
119 (if paragraph-indent-minor-mode
120 (set (make-local-variable 'paragraph-start
)
121 (concat ps-re paragraph-start
)))))
122 ;; Change the indentation function.
123 (if paragraph-indent-minor-mode
124 (add-function :override
(local 'indent-line-function
)
125 #'indent-to-left-margin
)
126 (remove-function (local 'indent-line-function
)
127 #'indent-to-left-margin
)))
129 (defalias 'indented-text-mode
'text-mode
)
131 ;; This can be made a no-op once all modes that use text-mode-hook
132 ;; are "derived" from text-mode.
133 (defun text-mode-hook-identify ()
134 "Mark that this mode has run `text-mode-hook'.
135 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
136 (set (make-local-variable 'text-mode-variant
) t
))
138 (add-hook 'text-mode-hook
'text-mode-hook-identify
)
140 (defun toggle-text-mode-auto-fill ()
141 "Toggle whether to use Auto Fill in Text mode and related modes.
142 This command affects all buffers that use modes related to Text mode,
143 both existing buffers and buffers that you subsequently create."
145 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook
))))
147 (add-hook 'text-mode-hook
'turn-on-auto-fill
)
148 (remove-hook 'text-mode-hook
'turn-on-auto-fill
))
149 (dolist (buffer (buffer-list))
150 (with-current-buffer buffer
151 (if (or (derived-mode-p 'text-mode
) text-mode-variant
)
152 (auto-fill-mode (if enable-mode
1 0)))))
153 (message "Auto Fill %s in Text modes"
154 (if enable-mode
"enabled" "disabled"))))
157 (define-key facemenu-keymap
"\eS" 'center-paragraph
)
159 (defun center-paragraph ()
160 "Center each nonblank line in the paragraph at or after point.
161 See `center-line' for more info."
165 (or (bolp) (newline 1))
168 (center-region (point) end
))))
170 (defun center-region (from to
)
171 "Center each nonblank line starting in the region.
172 See `center-line' for more info."
176 (setq to from from tem
)))
179 (narrow-to-region from to
)
182 (or (save-excursion (skip-chars-forward " \t") (eolp))
186 (define-key facemenu-keymap
"\es" 'center-line
)
188 (defun center-line (&optional nlines
)
189 "Center the line point is on, within the width specified by `fill-column'.
190 This means adjusting the indentation so that it equals
191 the distance between the end of the text and `fill-column'.
192 The argument NLINES says how many lines to center."
194 (if nlines
(setq nlines
(prefix-numeric-value nlines
)))
195 (while (not (eq nlines
0))
197 (let ((lm (current-left-margin))
200 (delete-horizontal-space)
202 (delete-horizontal-space)
203 (setq line-length
(current-column))
204 (if (> (- fill-column lm line-length
) 0)
206 (+ lm
(/ (- fill-column lm line-length
) 2))))))
210 (setq nlines
(1- nlines
))
213 (setq nlines
(1+ nlines
))
214 (forward-line -
1)))))
216 ;;; text-mode.el ends here