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