defstruct introspection
[emacs.git] / lisp / textmodes / text-mode.el
blob727697b5533fcffd52bb85b5650bc27059155f0e
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1992, 1994, 2001-2014 Free Software Foundation,
4 ;; Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: wp
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This package provides the fundamental text mode documented in the
28 ;; Emacs user's manual.
30 ;;; Code:
32 (defcustom text-mode-hook nil
33 "Normal hook run when entering Text mode and many related modes."
34 :type 'hook
35 :options '(turn-on-auto-fill turn-on-flyspell)
36 :group 'wp)
38 (defvar text-mode-variant nil
39 "Non-nil if this buffer's major mode is a variant of Text mode.
40 Use (derived-mode-p 'text-mode) instead.")
42 (defvar text-mode-syntax-table
43 (let ((st (make-syntax-table)))
44 (modify-syntax-entry ?\" ". " st)
45 (modify-syntax-entry ?\\ ". " st)
46 ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
47 (modify-syntax-entry ?' "w p" st)
48 st)
49 "Syntax table used while in `text-mode'.")
51 (defvar text-mode-map
52 (let ((map (make-sparse-keymap)))
53 (define-key map "\e\t" 'ispell-complete-word)
54 (define-key map [menu-bar text]
55 (cons "Text" (make-sparse-keymap "Text")))
56 (bindings--define-key map [menu-bar text toggle-text-mode-auto-fill]
57 '(menu-item "Auto Fill" toggle-text-mode-auto-fill
58 :button (:toggle . (memq 'turn-on-auto-fill text-mode-hook))
59 :help "Automatically fill text while typing in text modes (Auto Fill mode)"))
60 (bindings--define-key map [menu-bar text paragraph-indent-minor-mode]
61 '(menu-item "Paragraph Indent" paragraph-indent-minor-mode
62 :button (:toggle . (bound-and-true-p paragraph-indent-minor-mode))
63 :help "Toggle paragraph indent minor mode"))
64 (bindings--define-key map [menu-bar text sep] menu-bar-separator)
65 (bindings--define-key map [menu-bar text center-region]
66 '(menu-item "Center Region" center-region
67 :help "Center the marked region"
68 :enable (region-active-p)))
69 (bindings--define-key map [menu-bar text center-paragraph]
70 '(menu-item "Center Paragraph" center-paragraph
71 :help "Center the current paragraph"))
72 (bindings--define-key map [menu-bar text center-line]
73 '(menu-item "Center Line" center-line
74 :help "Center the current line"))
75 map)
76 "Keymap for `text-mode'.
77 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
78 inherit all the commands defined in this map.")
81 (define-derived-mode text-mode nil "Text"
82 "Major mode for editing text written for humans to read.
83 In this mode, paragraphs are delimited only by blank or white lines.
84 You can thus get the full benefit of adaptive filling
85 (see the variable `adaptive-fill-mode').
86 \\{text-mode-map}
87 Turning on Text mode runs the normal hook `text-mode-hook'."
88 (set (make-local-variable 'text-mode-variant) t)
89 (set (make-local-variable 'require-final-newline)
90 mode-require-final-newline)
91 (set (make-local-variable 'indent-line-function) 'indent-relative))
93 (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
94 "Major mode for editing text, with leading spaces starting a paragraph.
95 In this mode, you do not need blank lines between paragraphs
96 when the first line of the following paragraph starts with whitespace.
97 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
98 Special commands:
99 \\{text-mode-map}
100 Turning on Paragraph-Indent Text mode runs the normal hooks
101 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
102 :abbrev-table nil :syntax-table nil
103 (paragraph-indent-minor-mode))
105 (define-minor-mode paragraph-indent-minor-mode
106 "Minor mode for editing text, with leading spaces starting a paragraph.
107 In this mode, you do not need blank lines between paragraphs when the
108 first line of the following paragraph starts with whitespace, as with
109 `paragraph-indent-text-mode'.
110 Turning on Paragraph-Indent minor mode runs the normal hook
111 `paragraph-indent-text-mode-hook'."
112 :initial-value nil
113 ;; Change the definition of a paragraph start.
114 (let ((ps-re "[ \t\n\f]\\|"))
115 (if (eq t (compare-strings ps-re nil nil
116 paragraph-start nil (length ps-re)))
117 (if (not paragraph-indent-minor-mode)
118 (set (make-local-variable 'paragraph-start)
119 (substring paragraph-start (length ps-re))))
120 (if paragraph-indent-minor-mode
121 (set (make-local-variable 'paragraph-start)
122 (concat ps-re paragraph-start)))))
123 ;; Change the indentation function.
124 (if paragraph-indent-minor-mode
125 (add-function :override (local 'indent-line-function)
126 #'indent-to-left-margin)
127 (remove-function (local 'indent-line-function)
128 #'indent-to-left-margin)))
130 (defalias 'indented-text-mode 'text-mode)
132 ;; This can be made a no-op once all modes that use text-mode-hook
133 ;; are "derived" from text-mode.
134 (defun text-mode-hook-identify ()
135 "Mark that this mode has run `text-mode-hook'.
136 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
137 (set (make-local-variable 'text-mode-variant) t))
139 (add-hook 'text-mode-hook 'text-mode-hook-identify)
141 (defun toggle-text-mode-auto-fill ()
142 "Toggle whether to use Auto Fill in Text mode and related modes.
143 This command affects all buffers that use modes related to Text mode,
144 both existing buffers and buffers that you subsequently create."
145 (interactive)
146 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
147 (if enable-mode
148 (add-hook 'text-mode-hook 'turn-on-auto-fill)
149 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
150 (dolist (buffer (buffer-list))
151 (with-current-buffer buffer
152 (if (or (derived-mode-p 'text-mode) text-mode-variant)
153 (auto-fill-mode (if enable-mode 1 0)))))
154 (message "Auto Fill %s in Text modes"
155 (if enable-mode "enabled" "disabled"))))
158 (define-key facemenu-keymap "\eS" 'center-paragraph)
160 (defun center-paragraph ()
161 "Center each nonblank line in the paragraph at or after point.
162 See `center-line' for more info."
163 (interactive)
164 (save-excursion
165 (forward-paragraph)
166 (or (bolp) (newline 1))
167 (let ((end (point)))
168 (backward-paragraph)
169 (center-region (point) end))))
171 (defun center-region (from to)
172 "Center each nonblank line starting in the region.
173 See `center-line' for more info."
174 (interactive "r")
175 (if (> from to)
176 (let ((tem to))
177 (setq to from from tem)))
178 (save-excursion
179 (save-restriction
180 (narrow-to-region from to)
181 (goto-char from)
182 (while (not (eobp))
183 (or (save-excursion (skip-chars-forward " \t") (eolp))
184 (center-line))
185 (forward-line 1)))))
187 (define-key facemenu-keymap "\es" 'center-line)
189 (defun center-line (&optional nlines)
190 "Center the line point is on, within the width specified by `fill-column'.
191 This means adjusting the indentation so that it equals
192 the distance between the end of the text and `fill-column'.
193 The argument NLINES says how many lines to center."
194 (interactive "P")
195 (if nlines (setq nlines (prefix-numeric-value nlines)))
196 (while (not (eq nlines 0))
197 (save-excursion
198 (let ((lm (current-left-margin))
199 line-length)
200 (beginning-of-line)
201 (delete-horizontal-space)
202 (end-of-line)
203 (delete-horizontal-space)
204 (setq line-length (current-column))
205 (if (> (- fill-column lm line-length) 0)
206 (indent-line-to
207 (+ lm (/ (- fill-column lm line-length) 2))))))
208 (cond ((null nlines)
209 (setq nlines 0))
210 ((> nlines 0)
211 (setq nlines (1- nlines))
212 (forward-line 1))
213 ((< nlines 0)
214 (setq nlines (1+ nlines))
215 (forward-line -1)))))
217 ;;; text-mode.el ends here