(find-composition): Check if (char-after POS)
[emacs.git] / lisp / textmodes / text-mode.el
blob9e89cb17e300d9e38623f7941364f7b528caab4d
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1992, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: wp
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
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.
25 ;;; Commentary:
27 ;; This package provides the fundamental text mode documented in the
28 ;; Emacs user's manual.
30 ;;; Code:
32 (defcustom text-mode-hook nil
33 "Normal hook run when entering Text mode and many related modes."
34 :type 'hook
35 :options '(turn-on-auto-fill flyspell-mode)
36 :group 'data)
38 (defvar text-mode-variant nil
39 "Non-nil if this buffer's major mode is a variant of Text mode.")
41 (defvar text-mode-syntax-table nil
42 "Syntax table used while in text mode.")
44 (defvar text-mode-abbrev-table nil
45 "Abbrev table used while in text mode.")
46 (define-abbrev-table 'text-mode-abbrev-table ())
48 (if text-mode-syntax-table
50 (setq text-mode-syntax-table (make-syntax-table))
51 (modify-syntax-entry ?\" ". " text-mode-syntax-table)
52 (modify-syntax-entry ?\\ ". " text-mode-syntax-table)
53 (modify-syntax-entry ?' "w " text-mode-syntax-table))
55 (defvar text-mode-map nil
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.")
60 (if text-mode-map
62 (setq text-mode-map (make-sparse-keymap))
63 (define-key text-mode-map "\e\t" 'ispell-complete-word)
64 (define-key text-mode-map "\t" 'indent-relative)
65 (define-key text-mode-map "\es" 'center-line)
66 (define-key text-mode-map "\eS" 'center-paragraph))
69 (defun text-mode ()
70 "Major mode for editing text written for humans to read.
71 In this mode, paragraphs are delimited only by blank or white lines.
72 You can thus get the full benefit of adaptive filling
73 (see the variable `adaptive-fill-mode').
74 \\{text-mode-map}
75 Turning on Text mode runs the normal hook `text-mode-hook'."
76 (interactive)
77 (kill-all-local-variables)
78 (use-local-map text-mode-map)
79 (setq local-abbrev-table text-mode-abbrev-table)
80 (set-syntax-table text-mode-syntax-table)
81 (make-local-variable 'paragraph-start)
82 (setq paragraph-start (concat page-delimiter "\\|[ \t]*$"))
83 (if (eq ?^ (aref paragraph-start 0))
84 (setq paragraph-start (substring paragraph-start 1)))
85 (make-local-variable 'paragraph-separate)
86 (setq paragraph-separate paragraph-start)
87 (make-local-variable 'indent-line-function)
88 (setq indent-line-function 'indent-relative-maybe)
89 (setq mode-name "Text")
90 (setq major-mode 'text-mode)
91 (run-hooks 'text-mode-hook))
93 (defun paragraph-indent-text-mode ()
94 "Major mode for editing text, with leading spaces starting a paragraph.
95 In this mode, you do not need blank lines between paragraphs
96 when the first line of the following paragraph starts with whitespace.
97 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
98 Special commands:
99 \\{text-mode-map}
100 Turning on Paragraph-Indent Text mode runs the normal hooks
101 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
102 (interactive)
103 (kill-all-local-variables)
104 (use-local-map text-mode-map)
105 (setq mode-name "Parindent")
106 (setq major-mode 'paragraph-indent-text-mode)
107 (setq local-abbrev-table text-mode-abbrev-table)
108 (set-syntax-table text-mode-syntax-table)
109 (run-hooks 'text-mode-hook 'paragraph-indent-text-mode-hook))
111 (defun paragraph-indent-minor-mode ()
112 "Minor mode for editing text, with leading spaces starting a paragraph.
113 In this mode, you do not need blank lines between paragraphs when the
114 first line of the following paragraph starts with whitespace, as with
115 `paragraph-indent-mode'.
116 Turning on Paragraph-Indent minor mode runs the normal hook
117 `paragraph-indent-text-mode-hook'."
118 (interactive)
119 (set (make-local-variable 'paragraph-start)
120 (default-value 'paragraph-start))
121 (set (make-local-variable 'paragraph-separate) paragraph-start)
122 (run-hooks 'paragraph-indent-text-mode-hook))
124 (defalias 'indented-text-mode 'text-mode)
126 (defun text-mode-hook-identify ()
127 "Mark that this mode has run `text-mode-hook'.
128 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
129 (make-local-variable 'text-mode-variant)
130 (setq text-mode-variant t))
132 (add-hook 'text-mode-hook 'text-mode-hook-identify)
134 (defun toggle-text-mode-auto-fill ()
135 "Toggle whether to use Auto Fill in Text mode and related modes.
136 This command affects all buffers that use modes related to Text mode,
137 both existing buffers and buffers that you subsequently create."
138 (interactive)
139 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook)))
140 (buffers (buffer-list)))
141 (if enable-mode
142 (add-hook 'text-mode-hook 'turn-on-auto-fill)
143 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
144 (while buffers
145 (with-current-buffer (car buffers)
146 (if text-mode-variant
147 (auto-fill-mode (if enable-mode 1 0))))
148 (setq buffers (cdr buffers)))
149 (message "Auto Fill %s in Text modes"
150 (if enable-mode "enabled" "disabled"))))
152 (defun center-paragraph ()
153 "Center each nonblank line in the paragraph at or after point.
154 See `center-line' for more info."
155 (interactive)
156 (save-excursion
157 (forward-paragraph)
158 (or (bolp) (newline 1))
159 (let ((end (point)))
160 (backward-paragraph)
161 (center-region (point) end))))
163 (defun center-region (from to)
164 "Center each nonblank line starting in the region.
165 See `center-line' for more info."
166 (interactive "r")
167 (if (> from to)
168 (let ((tem to))
169 (setq to from from tem)))
170 (save-excursion
171 (save-restriction
172 (narrow-to-region from to)
173 (goto-char from)
174 (while (not (eobp))
175 (or (save-excursion (skip-chars-forward " \t") (eolp))
176 (center-line))
177 (forward-line 1)))))
179 (defun center-line (&optional nlines)
180 "Center the line point is on, within the width specified by `fill-column'.
181 This means adjusting the indentation so that it equals
182 the distance between the end of the text and `fill-column'.
183 The argument NLINES says how many lines to center."
184 (interactive "P")
185 (if nlines (setq nlines (prefix-numeric-value nlines)))
186 (while (not (eq nlines 0))
187 (save-excursion
188 (let ((lm (current-left-margin))
189 line-length)
190 (beginning-of-line)
191 (delete-horizontal-space)
192 (end-of-line)
193 (delete-horizontal-space)
194 (setq line-length (current-column))
195 (if (> (- fill-column lm line-length) 0)
196 (indent-line-to
197 (+ lm (/ (- fill-column lm line-length) 2))))))
198 (cond ((null nlines)
199 (setq nlines 0))
200 ((> nlines 0)
201 (setq nlines (1- nlines))
202 (forward-line 1))
203 ((< nlines 0)
204 (setq nlines (1+ nlines))
205 (forward-line -1)))))
207 ;;; text-mode.el ends here