Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / textmodes / text-mode.el
blob74fe2607f48fdb79a17494dfd554f79a46182e1d
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 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: wp
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 'data)
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 (define-key map "\es" 'center-line)
54 (define-key map "\eS" 'center-paragraph)
55 map)
56 "Keymap for `text-mode'.
57 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
58 inherit all the commands defined in this map.")
61 (define-derived-mode text-mode nil "Text"
62 "Major mode for editing text written for humans to read.
63 In this mode, paragraphs are delimited only by blank or white lines.
64 You can thus get the full benefit of adaptive filling
65 (see the variable `adaptive-fill-mode').
66 \\{text-mode-map}
67 Turning on Text mode runs the normal hook `text-mode-hook'."
68 (make-local-variable 'text-mode-variant)
69 (setq text-mode-variant t)
70 (set (make-local-variable 'require-final-newline)
71 mode-require-final-newline)
72 (set (make-local-variable 'indent-line-function) 'indent-relative))
74 (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
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.
78 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
79 Special commands:
80 \\{text-mode-map}
81 Turning on Paragraph-Indent Text mode runs the normal hooks
82 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
83 :abbrev-table nil :syntax-table nil
84 (paragraph-indent-minor-mode))
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-text-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)
95 (concat "[ \t\n\f]\\|" paragraph-start))
96 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
97 (run-hooks 'paragraph-indent-text-mode-hook))
99 (defalias 'indented-text-mode 'text-mode)
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))
108 (add-hook 'text-mode-hook 'text-mode-hook-identify)
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)
115 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
116 (if enable-mode
117 (add-hook 'text-mode-hook 'turn-on-auto-fill)
118 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
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)))))
123 (message "Auto Fill %s in Text modes"
124 (if enable-mode "enabled" "disabled"))))
126 (defun center-paragraph ()
127 "Center each nonblank line in the paragraph at or after point.
128 See `center-line' for more info."
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))))
137 (defun center-region (from to)
138 "Center each nonblank line starting in the region.
139 See `center-line' for more info."
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)))))
153 (defun center-line (&optional nlines)
154 "Center the line point is on, within the width specified by `fill-column'.
155 This means adjusting the indentation so that it equals
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)
170 (indent-line-to
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)))))
181 ;; arch-tag: a07ccaad-da13-4d7b-9c61-cd04f5926aab
182 ;;; text-mode.el ends here