]> code.delx.au - gnu-emacs/blob - lisp/textmodes/text-mode.el
(text-mode-hook-identify): New function,
[gnu-emacs] / lisp / textmodes / text-mode.el
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands.
2
3 ;; Copyright (C) 1985, 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
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
11 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
23
24 ;;; Commentary:
25
26 ;; This package provides the fundamental text mode documented in the
27 ;; Emacs user's manual.
28
29 ;;; Code:
30
31 (defvar text-mode-hook nil
32 "Normal hook run when entering Text mode and many related modes.")
33
34 (defvar text-mode-variant nil
35 "Non-nil if this buffer's major mode is a variant of Text mode.")
36
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.
53 Many other modes, such as Mail mode, Outline mode and Indented Text mode,
54 inherit all the commands defined in this map.")
55
56 (if text-mode-map
57 ()
58 (setq text-mode-map (make-sparse-keymap))
59 (define-key text-mode-map "\e\t" 'ispell-complete-word)
60 (define-key text-mode-map "\t" 'indent-relative)
61 (define-key text-mode-map "\es" 'center-line)
62 (define-key text-mode-map "\eS" 'center-paragraph))
63
64 \f
65 (defun text-mode ()
66 "Major mode for editing text written for humans to read.
67 In this mode, paragraphs are delimited only by blank or white lines.
68 You can thus get the full benefit of adaptive filling
69 (see the variable `adaptive-fill-mode').
70 \\{text-mode-map}
71 Turning on Text mode runs the normal hook `text-mode-hook'."
72 (interactive)
73 (kill-all-local-variables)
74 (use-local-map text-mode-map)
75 (setq local-abbrev-table text-mode-abbrev-table)
76 (set-syntax-table text-mode-syntax-table)
77 (make-local-variable 'paragraph-start)
78 (setq paragraph-start (concat "[ \t]*$\\|" page-delimiter))
79 (make-local-variable 'paragraph-separate)
80 (setq paragraph-separate paragraph-start)
81 (setq mode-name "Text")
82 (setq major-mode 'text-mode)
83 (run-hooks 'text-mode-hook))
84
85 (defun paragraph-indent-text-mode ()
86 "Major mode for editing text, with leading spaces starting a paragraph.
87 In this mode, you do not need blank lines between paragraphs
88 when the first line of the following paragraph starts with whitespace.
89 Special commands:
90 \\{text-mode-map}
91 Turning on Paragraph-Indent Text mode runs the normal hooks
92 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
93 (interactive)
94 (kill-all-local-variables)
95 (use-local-map text-mode-map)
96 (setq mode-name "Parindent")
97 (setq major-mode 'paragraph-indent-text-mode)
98 (setq local-abbrev-table text-mode-abbrev-table)
99 (set-syntax-table text-mode-syntax-table)
100 (run-hooks 'text-mode-hook 'paragraph-indent-text-mode-hook))
101
102 (defalias 'indented-text-mode 'text-mode)
103
104 (defun text-mode-hook-identify ()
105 "Mark that this mode has run `text-mode-hook'.
106 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
107 (make-local-variable 'text-mode-variant)
108 (setq text-mode-variant t))
109
110 (add-hook 'text-mode-hook 'text-mode-hook-identify)
111
112 (defun toggle-text-mode-auto-fill ()
113 "Toggle whether to use Auto Fill in Text mode and related modes.
114 This command affects all buffers that use modes related to Text mode,
115 both existing buffers and buffers that you subsequently create."
116 (interactive)
117 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook)))
118 (buffers (buffer-list)))
119 (if enable-mode
120 (add-hook 'text-mode-hook 'turn-on-auto-fill)
121 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
122 (while buffers
123 (with-current-buffer (car buffers)
124 (if text-mode-variant
125 (auto-fill-mode (if enable-mode 1 0))))
126 (setq buffers (cdr buffers)))
127 (message "Auto Fill %s in Text modes"
128 (if enable-mode "enabled" "disabled"))))
129 \f
130 (defun center-paragraph ()
131 "Center each nonblank line in the paragraph at or after point.
132 See `center-line' for more info."
133 (interactive)
134 (save-excursion
135 (forward-paragraph)
136 (or (bolp) (newline 1))
137 (let ((end (point)))
138 (backward-paragraph)
139 (center-region (point) end))))
140
141 (defun center-region (from to)
142 "Center each nonblank line starting in the region.
143 See `center-line' for more info."
144 (interactive "r")
145 (if (> from to)
146 (let ((tem to))
147 (setq to from from tem)))
148 (save-excursion
149 (save-restriction
150 (narrow-to-region from to)
151 (goto-char from)
152 (while (not (eobp))
153 (or (save-excursion (skip-chars-forward " \t") (eolp))
154 (center-line))
155 (forward-line 1)))))
156
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)))))
184
185 ;;; text-mode.el ends here