*** empty log message ***
[bpt/emacs.git] / lisp / textmodes / text-mode.el
CommitLineData
d501f516
ER
1;;; text-mode.el --- text mode, and its idiosyncratic commands.
2
8f1204db 3;; Copyright (C) 1985, 1992, 1994 Free Software Foundation, Inc.
a2535589 4
58142744
ER
5;; Maintainer: FSF
6
a2535589
JA
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software; you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
daa37602 11;; the Free Software Foundation; either version 2, or (at your option)
a2535589
JA
12;; any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
b578f267
EN
20;; along with GNU Emacs; see the file COPYING. If not, write to the
21;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22;; Boston, MA 02111-1307, USA.
d9ecc911
ER
23
24;;; Commentary:
25
26;; This package provides the fundamental text mode documented in the
27;; Emacs user's manual.
28
22a89ee8 29;;; Code:
a2535589 30
047d1f89
RS
31(defvar text-mode-hook nil
32 "Normal hook run when entering Text mode and many related modes.")
33
4c69a3be
RS
34(defvar text-mode-variant nil
35 "Non-nil if this buffer's major mode is a variant of Text mode.")
36
a2535589
JA
37(defvar text-mode-syntax-table nil
38 "Syntax table used while in text mode.")
39
40(defvar text-mode-abbrev-table nil
41 "Abbrev table used while in text mode.")
42(define-abbrev-table 'text-mode-abbrev-table ())
43
44(if text-mode-syntax-table
45 ()
46 (setq text-mode-syntax-table (make-syntax-table))
47 (modify-syntax-entry ?\" ". " text-mode-syntax-table)
48 (modify-syntax-entry ?\\ ". " text-mode-syntax-table)
49 (modify-syntax-entry ?' "w " text-mode-syntax-table))
50
51(defvar text-mode-map nil
52 "Keymap for Text mode.
53Many other modes, such as Mail mode, Outline mode and Indented Text mode,
54inherit all the commands defined in this map.")
55
56(if text-mode-map
57 ()
58 (setq text-mode-map (make-sparse-keymap))
fb14a9a7 59 (define-key text-mode-map "\e\t" 'ispell-complete-word)
946340ae 60 (define-key text-mode-map "\t" 'indent-relative)
a2535589
JA
61 (define-key text-mode-map "\es" 'center-line)
62 (define-key text-mode-map "\eS" 'center-paragraph))
63
64\f
a2535589 65(defun text-mode ()
6824710a 66 "Major mode for editing text written for humans to read.
91fe8c37 67In this mode, paragraphs are delimited only by blank or white lines.
6824710a
RS
68You can thus get the full benefit of adaptive filling
69 (see the variable `adaptive-fill-mode').
6b4bde1b 70\\{text-mode-map}
6824710a 71Turning on Text mode runs the normal hook `text-mode-hook'."
a2535589
JA
72 (interactive)
73 (kill-all-local-variables)
74 (use-local-map text-mode-map)
6824710a
RS
75 (setq local-abbrev-table text-mode-abbrev-table)
76 (set-syntax-table text-mode-syntax-table)
77 (make-local-variable 'paragraph-start)
91fe8c37 78 (setq paragraph-start (concat "[ \t]*$\\|" page-delimiter))
6824710a
RS
79 (make-local-variable 'paragraph-separate)
80 (setq paragraph-separate paragraph-start)
85761533
RS
81 (make-local-variable 'indent-line-function)
82 (setq indent-line-function 'indent-relative-maybe)
a2535589
JA
83 (setq mode-name "Text")
84 (setq major-mode 'text-mode)
6824710a
RS
85 (run-hooks 'text-mode-hook))
86
946340ae 87(defun paragraph-indent-text-mode ()
6824710a
RS
88 "Major mode for editing text, with leading spaces starting a paragraph.
89In this mode, you do not need blank lines between paragraphs
90when the first line of the following paragraph starts with whitespace.
91Special commands:
92\\{text-mode-map}
946340ae
RS
93Turning on Paragraph-Indent Text mode runs the normal hooks
94`text-mode-hook' and `paragraph-indent-text-mode-hook'."
6824710a
RS
95 (interactive)
96 (kill-all-local-variables)
97 (use-local-map text-mode-map)
946340ae
RS
98 (setq mode-name "Parindent")
99 (setq major-mode 'paragraph-indent-text-mode)
a2535589
JA
100 (setq local-abbrev-table text-mode-abbrev-table)
101 (set-syntax-table text-mode-syntax-table)
946340ae 102 (run-hooks 'text-mode-hook 'paragraph-indent-text-mode-hook))
6824710a 103
946340ae 104(defalias 'indented-text-mode 'text-mode)
a2535589 105
d910f08c
RS
106(defun text-mode-hook-identify ()
107 "Mark that this mode has run `text-mode-hook'.
108This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
109 (make-local-variable 'text-mode-variant)
110 (setq text-mode-variant t))
111
112(add-hook 'text-mode-hook 'text-mode-hook-identify)
113
4c69a3be
RS
114(defun toggle-text-mode-auto-fill ()
115 "Toggle whether to use Auto Fill in Text mode and related modes.
116This command affects all buffers that use modes related to Text mode,
117both existing buffers and buffers that you subsequently create."
118 (interactive)
119 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook)))
120 (buffers (buffer-list)))
121 (if enable-mode
122 (add-hook 'text-mode-hook 'turn-on-auto-fill)
123 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
124 (while buffers
125 (with-current-buffer (car buffers)
126 (if text-mode-variant
127 (auto-fill-mode (if enable-mode 1 0))))
128 (setq buffers (cdr buffers)))
129 (message "Auto Fill %s in Text modes"
130 (if enable-mode "enabled" "disabled"))))
131\f
a2535589
JA
132(defun center-paragraph ()
133 "Center each nonblank line in the paragraph at or after point.
d367fa71 134See `center-line' for more info."
a2535589
JA
135 (interactive)
136 (save-excursion
137 (forward-paragraph)
138 (or (bolp) (newline 1))
139 (let ((end (point)))
140 (backward-paragraph)
141 (center-region (point) end))))
142
143(defun center-region (from to)
144 "Center each nonblank line starting in the region.
d367fa71 145See `center-line' for more info."
a2535589
JA
146 (interactive "r")
147 (if (> from to)
148 (let ((tem to))
149 (setq to from from tem)))
150 (save-excursion
151 (save-restriction
152 (narrow-to-region from to)
153 (goto-char from)
154 (while (not (eobp))
155 (or (save-excursion (skip-chars-forward " \t") (eolp))
156 (center-line))
157 (forward-line 1)))))
158
aaf6c7ef 159(defun center-line (&optional nlines)
a2535589
JA
160 "Center the line point is on, within the width specified by `fill-column'.
161This means adjusting the indentation so that it equals
aaf6c7ef
RS
162the distance between the end of the text and `fill-column'.
163The argument NLINES says how many lines to center."
164 (interactive "P")
165 (if nlines (setq nlines (prefix-numeric-value nlines)))
166 (while (not (eq nlines 0))
167 (save-excursion
168 (let ((lm (current-left-margin))
169 line-length)
170 (beginning-of-line)
171 (delete-horizontal-space)
172 (end-of-line)
173 (delete-horizontal-space)
174 (setq line-length (current-column))
175 (if (> (- fill-column lm line-length) 0)
176 (indent-line-to
177 (+ lm (/ (- fill-column lm line-length) 2))))))
178 (cond ((null nlines)
179 (setq nlines 0))
180 ((> nlines 0)
181 (setq nlines (1- nlines))
182 (forward-line 1))
183 ((< nlines 0)
184 (setq nlines (1+ nlines))
185 (forward-line -1)))))
d501f516
ER
186
187;;; text-mode.el ends here