(text-mode-hook): New defvar.
[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
a2535589
JA
34(defvar text-mode-syntax-table nil
35 "Syntax table used while in text mode.")
36
37(defvar text-mode-abbrev-table nil
38 "Abbrev table used while in text mode.")
39(define-abbrev-table 'text-mode-abbrev-table ())
40
41(if text-mode-syntax-table
42 ()
43 (setq text-mode-syntax-table (make-syntax-table))
44 (modify-syntax-entry ?\" ". " text-mode-syntax-table)
45 (modify-syntax-entry ?\\ ". " text-mode-syntax-table)
46 (modify-syntax-entry ?' "w " text-mode-syntax-table))
47
48(defvar text-mode-map nil
49 "Keymap for Text mode.
50Many other modes, such as Mail mode, Outline mode and Indented Text mode,
51inherit all the commands defined in this map.")
52
53(if text-mode-map
54 ()
55 (setq text-mode-map (make-sparse-keymap))
fb14a9a7 56 (define-key text-mode-map "\e\t" 'ispell-complete-word)
946340ae 57 (define-key text-mode-map "\t" 'indent-relative)
a2535589
JA
58 (define-key text-mode-map "\es" 'center-line)
59 (define-key text-mode-map "\eS" 'center-paragraph))
60
61\f
a2535589 62(defun text-mode ()
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'."
a2535589
JA
69 (interactive)
70 (kill-all-local-variables)
71 (use-local-map text-mode-map)
6824710a
RS
72 (setq local-abbrev-table text-mode-abbrev-table)
73 (set-syntax-table text-mode-syntax-table)
74 (make-local-variable 'paragraph-start)
91fe8c37 75 (setq paragraph-start (concat "[ \t]*$\\|" page-delimiter))
6824710a
RS
76 (make-local-variable 'paragraph-separate)
77 (setq paragraph-separate paragraph-start)
a2535589
JA
78 (setq mode-name "Text")
79 (setq major-mode 'text-mode)
6824710a
RS
80 (run-hooks 'text-mode-hook))
81
946340ae 82(defun paragraph-indent-text-mode ()
6824710a
RS
83 "Major mode for editing text, with leading spaces starting a paragraph.
84In this mode, you do not need blank lines between paragraphs
85when the first line of the following paragraph starts with whitespace.
86Special commands:
87\\{text-mode-map}
946340ae
RS
88Turning on Paragraph-Indent Text mode runs the normal hooks
89`text-mode-hook' and `paragraph-indent-text-mode-hook'."
6824710a
RS
90 (interactive)
91 (kill-all-local-variables)
92 (use-local-map text-mode-map)
946340ae
RS
93 (setq mode-name "Parindent")
94 (setq major-mode 'paragraph-indent-text-mode)
a2535589
JA
95 (setq local-abbrev-table text-mode-abbrev-table)
96 (set-syntax-table text-mode-syntax-table)
946340ae 97 (run-hooks 'text-mode-hook 'paragraph-indent-text-mode-hook))
6824710a 98
946340ae 99(defalias 'indented-text-mode 'text-mode)
a2535589 100
a2535589
JA
101(defun center-paragraph ()
102 "Center each nonblank line in the paragraph at or after point.
d367fa71 103See `center-line' for more info."
a2535589
JA
104 (interactive)
105 (save-excursion
106 (forward-paragraph)
107 (or (bolp) (newline 1))
108 (let ((end (point)))
109 (backward-paragraph)
110 (center-region (point) end))))
111
112(defun center-region (from to)
113 "Center each nonblank line starting in the region.
d367fa71 114See `center-line' for more info."
a2535589
JA
115 (interactive "r")
116 (if (> from to)
117 (let ((tem to))
118 (setq to from from tem)))
119 (save-excursion
120 (save-restriction
121 (narrow-to-region from to)
122 (goto-char from)
123 (while (not (eobp))
124 (or (save-excursion (skip-chars-forward " \t") (eolp))
125 (center-line))
126 (forward-line 1)))))
127
aaf6c7ef 128(defun center-line (&optional nlines)
a2535589
JA
129 "Center the line point is on, within the width specified by `fill-column'.
130This means adjusting the indentation so that it equals
aaf6c7ef
RS
131the distance between the end of the text and `fill-column'.
132The argument NLINES says how many lines to center."
133 (interactive "P")
134 (if nlines (setq nlines (prefix-numeric-value nlines)))
135 (while (not (eq nlines 0))
136 (save-excursion
137 (let ((lm (current-left-margin))
138 line-length)
139 (beginning-of-line)
140 (delete-horizontal-space)
141 (end-of-line)
142 (delete-horizontal-space)
143 (setq line-length (current-column))
144 (if (> (- fill-column lm line-length) 0)
145 (indent-line-to
146 (+ lm (/ (- fill-column lm line-length) 2))))))
147 (cond ((null nlines)
148 (setq nlines 0))
149 ((> nlines 0)
150 (setq nlines (1- nlines))
151 (forward-line 1))
152 ((< nlines 0)
153 (setq nlines (1+ nlines))
154 (forward-line -1)))))
d501f516
ER
155
156;;; text-mode.el ends here