* frame.el (auto-raise-mode, auto-lower-mode): Doc fix.
[emacs.git] / lisp / textmodes / text-mode.el
blob3f6ad1faf8774e71658af281356a1b7856a2a403
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1992, 1994, 2001-2011 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: wp
7 ;; Package: emacs
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/>.
24 ;;; Commentary:
26 ;; This package provides the fundamental text mode documented in the
27 ;; Emacs user's manual.
29 ;;; Code:
31 (defcustom text-mode-hook nil
32 "Normal hook run when entering Text mode and many related modes."
33 :type 'hook
34 :options '(turn-on-auto-fill turn-on-flyspell)
35 :group 'wp)
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)
47 st)
48 "Syntax table used while in `text-mode'.")
50 (defvar text-mode-map
51 (let ((map (make-sparse-keymap)))
52 (define-key map "\e\t" 'ispell-complete-word)
53 map)
54 "Keymap for `text-mode'.
55 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
56 inherit all the commands defined in this map.")
59 (define-derived-mode text-mode nil "Text"
60 "Major mode for editing text written for humans to read.
61 In this mode, paragraphs are delimited only by blank or white lines.
62 You can thus get the full benefit of adaptive filling
63 (see the variable `adaptive-fill-mode').
64 \\{text-mode-map}
65 Turning on Text mode runs the normal hook `text-mode-hook'."
66 (make-local-variable 'text-mode-variant)
67 (setq text-mode-variant t)
68 (set (make-local-variable 'require-final-newline)
69 mode-require-final-newline)
70 (set (make-local-variable 'indent-line-function) 'indent-relative))
72 (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
73 "Major mode for editing text, with leading spaces starting a paragraph.
74 In this mode, you do not need blank lines between paragraphs
75 when the first line of the following paragraph starts with whitespace.
76 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
77 Special commands:
78 \\{text-mode-map}
79 Turning on Paragraph-Indent Text mode runs the normal hooks
80 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
81 :abbrev-table nil :syntax-table nil
82 (paragraph-indent-minor-mode))
84 (defun paragraph-indent-minor-mode ()
85 "Minor mode for editing text, with leading spaces starting a paragraph.
86 In this mode, you do not need blank lines between paragraphs when the
87 first line of the following paragraph starts with whitespace, as with
88 `paragraph-indent-text-mode'.
89 Turning on Paragraph-Indent minor mode runs the normal hook
90 `paragraph-indent-text-mode-hook'."
91 (interactive)
92 (set (make-local-variable 'paragraph-start)
93 (concat "[ \t\n\f]\\|" paragraph-start))
94 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
95 (run-hooks 'paragraph-indent-text-mode-hook))
97 (defalias 'indented-text-mode 'text-mode)
99 ;; This can be made a no-op once all modes that use text-mode-hook
100 ;; are "derived" from text-mode.
101 (defun text-mode-hook-identify ()
102 "Mark that this mode has run `text-mode-hook'.
103 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
104 (set (make-local-variable 'text-mode-variant) t))
106 (add-hook 'text-mode-hook 'text-mode-hook-identify)
108 (defun toggle-text-mode-auto-fill ()
109 "Toggle whether to use Auto Fill in Text mode and related modes.
110 This command affects all buffers that use modes related to Text mode,
111 both existing buffers and buffers that you subsequently create."
112 (interactive)
113 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
114 (if enable-mode
115 (add-hook 'text-mode-hook 'turn-on-auto-fill)
116 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
117 (dolist (buffer (buffer-list))
118 (with-current-buffer buffer
119 (if (or (derived-mode-p 'text-mode) text-mode-variant)
120 (auto-fill-mode (if enable-mode 1 0)))))
121 (message "Auto Fill %s in Text modes"
122 (if enable-mode "enabled" "disabled"))))
125 (define-key facemenu-keymap "\eS" 'center-paragraph)
127 (defun center-paragraph ()
128 "Center each nonblank line in the paragraph at or after point.
129 See `center-line' for more info."
130 (interactive)
131 (save-excursion
132 (forward-paragraph)
133 (or (bolp) (newline 1))
134 (let ((end (point)))
135 (backward-paragraph)
136 (center-region (point) end))))
138 (defun center-region (from to)
139 "Center each nonblank line starting in the region.
140 See `center-line' for more info."
141 (interactive "r")
142 (if (> from to)
143 (let ((tem to))
144 (setq to from from tem)))
145 (save-excursion
146 (save-restriction
147 (narrow-to-region from to)
148 (goto-char from)
149 (while (not (eobp))
150 (or (save-excursion (skip-chars-forward " \t") (eolp))
151 (center-line))
152 (forward-line 1)))))
154 (define-key facemenu-keymap "\es" 'center-line)
156 (defun center-line (&optional nlines)
157 "Center the line point is on, within the width specified by `fill-column'.
158 This means adjusting the indentation so that it equals
159 the distance between the end of the text and `fill-column'.
160 The argument NLINES says how many lines to center."
161 (interactive "P")
162 (if nlines (setq nlines (prefix-numeric-value nlines)))
163 (while (not (eq nlines 0))
164 (save-excursion
165 (let ((lm (current-left-margin))
166 line-length)
167 (beginning-of-line)
168 (delete-horizontal-space)
169 (end-of-line)
170 (delete-horizontal-space)
171 (setq line-length (current-column))
172 (if (> (- fill-column lm line-length) 0)
173 (indent-line-to
174 (+ lm (/ (- fill-column lm line-length) 2))))))
175 (cond ((null nlines)
176 (setq nlines 0))
177 ((> nlines 0)
178 (setq nlines (1- nlines))
179 (forward-line 1))
180 ((< nlines 0)
181 (setq nlines (1+ nlines))
182 (forward-line -1)))))
184 ;;; text-mode.el ends here