Add arch taglines
[bpt/emacs.git] / lisp / textmodes / text-mode.el
CommitLineData
55535639 1;;; text-mode.el --- text mode, and its idiosyncratic commands
d501f516 2
8f1204db 3;; Copyright (C) 1985, 1992, 1994 Free Software Foundation, Inc.
a2535589 4
58142744 5;; Maintainer: FSF
6228c05b 6;; Keywords: wp
58142744 7
a2535589
JA
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
daa37602 12;; the Free Software Foundation; either version 2, or (at your option)
a2535589
JA
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
d9ecc911
ER
24
25;;; Commentary:
26
27;; This package provides the fundamental text mode documented in the
28;; Emacs user's manual.
29
22a89ee8 30;;; Code:
a2535589 31
e144fd4e
DL
32(defcustom text-mode-hook nil
33 "Normal hook run when entering Text mode and many related modes."
34 :type 'hook
84347194 35 :options '(turn-on-auto-fill flyspell-mode)
e144fd4e 36 :group 'data)
047d1f89 37
4c69a3be 38(defvar text-mode-variant nil
9e59bf58
SM
39 "Non-nil if this buffer's major mode is a variant of Text mode.
40Use (derived-mode-p 'text-mode) instead.")
41
42(defvar text-mode-syntax-table
43 (let ((st (make-syntax-table)))
44 (modify-syntax-entry ?\" ". " st)
45 (modify-syntax-entry ?\\ ". " st)
61ed2dcc
SM
46 ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
47 (modify-syntax-entry ?' "w p" st)
9e59bf58
SM
48 st)
49 "Syntax table used while in `text-mode'.")
50
51(defvar text-mode-map
52 (let ((map (make-sparse-keymap)))
53 (define-key map "\e\t" 'ispell-complete-word)
54 (define-key map "\es" 'center-line)
55 (define-key map "\eS" 'center-paragraph)
56 map)
57 "Keymap for `text-mode'.
58Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
a2535589
JA
59inherit all the commands defined in this map.")
60
a2535589 61\f
fdf1c7c3 62(define-derived-mode text-mode nil "Text"
6824710a 63 "Major mode for editing text written for humans to read.
91fe8c37 64In this mode, paragraphs are delimited only by blank or white lines.
6824710a
RS
65You can thus get the full benefit of adaptive filling
66 (see the variable `adaptive-fill-mode').
6b4bde1b 67\\{text-mode-map}
6824710a 68Turning on Text mode runs the normal hook `text-mode-hook'."
540f653d
RS
69 (make-local-variable 'text-mode-variant)
70 (setq text-mode-variant t)
81cb34cd 71 (set (make-local-variable 'require-final-newline) t)
9e59bf58 72 (set (make-local-variable 'indent-line-function) 'indent-relative))
6824710a 73
0fb36e36 74(define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
6824710a
RS
75 "Major mode for editing text, with leading spaces starting a paragraph.
76In this mode, you do not need blank lines between paragraphs
77when the first line of the following paragraph starts with whitespace.
9f78d51a 78`paragraph-indent-minor-mode' provides a similar facility as a minor mode.
6824710a
RS
79Special commands:
80\\{text-mode-map}
946340ae
RS
81Turning on Paragraph-Indent Text mode runs the normal hooks
82`text-mode-hook' and `paragraph-indent-text-mode-hook'."
61ed2dcc 83 :abbrev-table nil :syntax-table nil
9e59bf58 84 (paragraph-indent-minor-mode))
9f78d51a
DL
85
86(defun paragraph-indent-minor-mode ()
87 "Minor mode for editing text, with leading spaces starting a paragraph.
88In this mode, you do not need blank lines between paragraphs when the
89first line of the following paragraph starts with whitespace, as with
90`paragraph-indent-mode'.
91Turning on Paragraph-Indent minor mode runs the normal hook
92`paragraph-indent-text-mode-hook'."
93 (interactive)
94 (set (make-local-variable 'paragraph-start)
0fb36e36
SM
95 (concat "[ \t\n\f]\\|" paragraph-start))
96 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
9f78d51a 97 (run-hooks 'paragraph-indent-text-mode-hook))
db95369b 98
946340ae 99(defalias 'indented-text-mode 'text-mode)
a2535589 100
ebda95a2
RS
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'.
105This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
106 (set (make-local-variable 'text-mode-variant) t))
107
108(add-hook 'text-mode-hook 'text-mode-hook-identify)
def08a3f 109
4c69a3be
RS
110(defun toggle-text-mode-auto-fill ()
111 "Toggle whether to use Auto Fill in Text mode and related modes.
112This command affects all buffers that use modes related to Text mode,
113both existing buffers and buffers that you subsequently create."
114 (interactive)
fdf1c7c3 115 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
4c69a3be
RS
116 (if enable-mode
117 (add-hook 'text-mode-hook 'turn-on-auto-fill)
118 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
fdf1c7c3
SM
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)))))
4c69a3be
RS
123 (message "Auto Fill %s in Text modes"
124 (if enable-mode "enabled" "disabled"))))
125\f
a2535589
JA
126(defun center-paragraph ()
127 "Center each nonblank line in the paragraph at or after point.
d367fa71 128See `center-line' for more info."
a2535589
JA
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))))
136
137(defun center-region (from to)
138 "Center each nonblank line starting in the region.
d367fa71 139See `center-line' for more info."
a2535589
JA
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)))))
152
aaf6c7ef 153(defun center-line (&optional nlines)
a2535589
JA
154 "Center the line point is on, within the width specified by `fill-column'.
155This means adjusting the indentation so that it equals
aaf6c7ef
RS
156the distance between the end of the text and `fill-column'.
157The 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)
84347194 170 (indent-line-to
aaf6c7ef
RS
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)))))
d501f516 180
ab5796a9 181;;; arch-tag: a07ccaad-da13-4d7b-9c61-cd04f5926aab
d501f516 182;;; text-mode.el ends here