Fix 2010-11-22 change to diff.el.
[emacs.git] / lisp / textmodes / text-mode.el
blobb6868d3a8e8c1b1c5f715031b6256efdb92adb90
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1992, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
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 map)
55 "Keymap for `text-mode'.
56 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
57 inherit all the commands defined in this map.")
60 (define-derived-mode text-mode nil "Text"
61 "Major mode for editing text written for humans to read.
62 In this mode, paragraphs are delimited only by blank or white lines.
63 You can thus get the full benefit of adaptive filling
64 (see the variable `adaptive-fill-mode').
65 \\{text-mode-map}
66 Turning on Text mode runs the normal hook `text-mode-hook'."
67 (make-local-variable 'text-mode-variant)
68 (setq text-mode-variant t)
69 (set (make-local-variable 'require-final-newline)
70 mode-require-final-newline)
71 (set (make-local-variable 'indent-line-function) 'indent-relative))
73 (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
74 "Major mode for editing text, with leading spaces starting a paragraph.
75 In this mode, you do not need blank lines between paragraphs
76 when the first line of the following paragraph starts with whitespace.
77 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
78 Special commands:
79 \\{text-mode-map}
80 Turning on Paragraph-Indent Text mode runs the normal hooks
81 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
82 :abbrev-table nil :syntax-table nil
83 (paragraph-indent-minor-mode))
85 (defun paragraph-indent-minor-mode ()
86 "Minor mode for editing text, with leading spaces starting a paragraph.
87 In this mode, you do not need blank lines between paragraphs when the
88 first line of the following paragraph starts with whitespace, as with
89 `paragraph-indent-text-mode'.
90 Turning on Paragraph-Indent minor mode runs the normal hook
91 `paragraph-indent-text-mode-hook'."
92 (interactive)
93 (set (make-local-variable 'paragraph-start)
94 (concat "[ \t\n\f]\\|" paragraph-start))
95 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
96 (run-hooks 'paragraph-indent-text-mode-hook))
98 (defalias 'indented-text-mode 'text-mode)
100 ;; This can be made a no-op once all modes that use text-mode-hook
101 ;; are "derived" from text-mode.
102 (defun text-mode-hook-identify ()
103 "Mark that this mode has run `text-mode-hook'.
104 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
105 (set (make-local-variable 'text-mode-variant) t))
107 (add-hook 'text-mode-hook 'text-mode-hook-identify)
109 (defun toggle-text-mode-auto-fill ()
110 "Toggle whether to use Auto Fill in Text mode and related modes.
111 This command affects all buffers that use modes related to Text mode,
112 both existing buffers and buffers that you subsequently create."
113 (interactive)
114 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
115 (if enable-mode
116 (add-hook 'text-mode-hook 'turn-on-auto-fill)
117 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
118 (dolist (buffer (buffer-list))
119 (with-current-buffer buffer
120 (if (or (derived-mode-p 'text-mode) text-mode-variant)
121 (auto-fill-mode (if enable-mode 1 0)))))
122 (message "Auto Fill %s in Text modes"
123 (if enable-mode "enabled" "disabled"))))
126 (define-key facemenu-keymap "\eS" 'center-paragraph)
128 (defun center-paragraph ()
129 "Center each nonblank line in the paragraph at or after point.
130 See `center-line' for more info."
131 (interactive)
132 (save-excursion
133 (forward-paragraph)
134 (or (bolp) (newline 1))
135 (let ((end (point)))
136 (backward-paragraph)
137 (center-region (point) end))))
139 (defun center-region (from to)
140 "Center each nonblank line starting in the region.
141 See `center-line' for more info."
142 (interactive "r")
143 (if (> from to)
144 (let ((tem to))
145 (setq to from from tem)))
146 (save-excursion
147 (save-restriction
148 (narrow-to-region from to)
149 (goto-char from)
150 (while (not (eobp))
151 (or (save-excursion (skip-chars-forward " \t") (eolp))
152 (center-line))
153 (forward-line 1)))))
155 (define-key facemenu-keymap "\es" 'center-line)
157 (defun center-line (&optional nlines)
158 "Center the line point is on, within the width specified by `fill-column'.
159 This means adjusting the indentation so that it equals
160 the distance between the end of the text and `fill-column'.
161 The argument NLINES says how many lines to center."
162 (interactive "P")
163 (if nlines (setq nlines (prefix-numeric-value nlines)))
164 (while (not (eq nlines 0))
165 (save-excursion
166 (let ((lm (current-left-margin))
167 line-length)
168 (beginning-of-line)
169 (delete-horizontal-space)
170 (end-of-line)
171 (delete-horizontal-space)
172 (setq line-length (current-column))
173 (if (> (- fill-column lm line-length) 0)
174 (indent-line-to
175 (+ lm (/ (- fill-column lm line-length) 2))))))
176 (cond ((null nlines)
177 (setq nlines 0))
178 ((> nlines 0)
179 (setq nlines (1- nlines))
180 (forward-line 1))
181 ((< nlines 0)
182 (setq nlines (1+ nlines))
183 (forward-line -1)))))
185 ;; arch-tag: a07ccaad-da13-4d7b-9c61-cd04f5926aab
186 ;;; text-mode.el ends here