(do_switch_frame): Mark previously displayed frame as obscured for
[bpt/emacs.git] / lisp / textmodes / text-mode.el
CommitLineData
55535639 1;;; text-mode.el --- text mode, and its idiosyncratic commands
d501f516 2
f2e3589a 3;; Copyright (C) 1985, 1992, 1994, 2001, 2002, 2003, 2004,
12dc447f 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
a2535589 5
58142744 6;; Maintainer: FSF
6228c05b 7;; Keywords: wp
58142744 8
a2535589
JA
9;; This file is part of GNU Emacs.
10
1fecc8fe 11;; GNU Emacs is free software: you can redistribute it and/or modify
a2535589 12;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
a2535589
JA
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
1fecc8fe 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
e144fd4e
DL
31(defcustom text-mode-hook nil
32 "Normal hook run when entering Text mode and many related modes."
33 :type 'hook
2809db33 34 :options '(turn-on-auto-fill turn-on-flyspell)
e144fd4e 35 :group 'data)
047d1f89 36
4c69a3be 37(defvar text-mode-variant nil
9e59bf58
SM
38 "Non-nil if this buffer's major mode is a variant of Text mode.
39Use (derived-mode-p 'text-mode) instead.")
40
41(defvar text-mode-syntax-table
42 (let ((st (make-syntax-table)))
43 (modify-syntax-entry ?\" ". " st)
44 (modify-syntax-entry ?\\ ". " st)
61ed2dcc
SM
45 ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
46 (modify-syntax-entry ?' "w p" st)
9e59bf58
SM
47 st)
48 "Syntax table used while in `text-mode'.")
49
50(defvar text-mode-map
51 (let ((map (make-sparse-keymap)))
52 (define-key map "\e\t" 'ispell-complete-word)
53 (define-key map "\es" 'center-line)
54 (define-key map "\eS" 'center-paragraph)
55 map)
56 "Keymap for `text-mode'.
57Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
a2535589
JA
58inherit all the commands defined in this map.")
59
a2535589 60\f
fdf1c7c3 61(define-derived-mode text-mode nil "Text"
6824710a 62 "Major mode for editing text written for humans to read.
91fe8c37 63In this mode, paragraphs are delimited only by blank or white lines.
6824710a
RS
64You can thus get the full benefit of adaptive filling
65 (see the variable `adaptive-fill-mode').
6b4bde1b 66\\{text-mode-map}
6824710a 67Turning on Text mode runs the normal hook `text-mode-hook'."
540f653d
RS
68 (make-local-variable 'text-mode-variant)
69 (setq text-mode-variant t)
706d2537
RS
70 (set (make-local-variable 'require-final-newline)
71 mode-require-final-newline)
9e59bf58 72 (set (make-local-variable 'indent-line-function) 'indent-relative))
6824710a 73
0fb36e36 74(define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
6824710a
RS
75 "Major mode for editing text, with leading spaces starting a paragraph.
76In this mode, you do not need blank lines between paragraphs
77when the first line of the following paragraph starts with whitespace.
9f78d51a 78`paragraph-indent-minor-mode' provides a similar facility as a minor mode.
6824710a
RS
79Special commands:
80\\{text-mode-map}
946340ae
RS
81Turning on Paragraph-Indent Text mode runs the normal hooks
82`text-mode-hook' and `paragraph-indent-text-mode-hook'."
61ed2dcc 83 :abbrev-table nil :syntax-table nil
9e59bf58 84 (paragraph-indent-minor-mode))
9f78d51a
DL
85
86(defun paragraph-indent-minor-mode ()
87 "Minor mode for editing text, with leading spaces starting a paragraph.
88In this mode, you do not need blank lines between paragraphs when the
89first line of the following paragraph starts with whitespace, as with
57fff5de 90`paragraph-indent-text-mode'.
9f78d51a
DL
91Turning on Paragraph-Indent minor mode runs the normal hook
92`paragraph-indent-text-mode-hook'."
93 (interactive)
94 (set (make-local-variable 'paragraph-start)
0fb36e36
SM
95 (concat "[ \t\n\f]\\|" paragraph-start))
96 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
9f78d51a 97 (run-hooks 'paragraph-indent-text-mode-hook))
db95369b 98
946340ae 99(defalias 'indented-text-mode 'text-mode)
a2535589 100
ebda95a2
RS
101;; This can be made a no-op once all modes that use text-mode-hook
102;; are "derived" from text-mode.
103(defun text-mode-hook-identify ()
104 "Mark that this mode has run `text-mode-hook'.
105This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
106 (set (make-local-variable 'text-mode-variant) t))
107
108(add-hook 'text-mode-hook 'text-mode-hook-identify)
def08a3f 109
4c69a3be
RS
110(defun toggle-text-mode-auto-fill ()
111 "Toggle whether to use Auto Fill in Text mode and related modes.
112This command affects all buffers that use modes related to Text mode,
113both existing buffers and buffers that you subsequently create."
114 (interactive)
fdf1c7c3 115 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
4c69a3be
RS
116 (if enable-mode
117 (add-hook 'text-mode-hook 'turn-on-auto-fill)
118 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
fdf1c7c3
SM
119 (dolist (buffer (buffer-list))
120 (with-current-buffer buffer
121 (if (or (derived-mode-p 'text-mode) text-mode-variant)
122 (auto-fill-mode (if enable-mode 1 0)))))
4c69a3be
RS
123 (message "Auto Fill %s in Text modes"
124 (if enable-mode "enabled" "disabled"))))
125\f
a2535589
JA
126(defun center-paragraph ()
127 "Center each nonblank line in the paragraph at or after point.
d367fa71 128See `center-line' for more info."
a2535589
JA
129 (interactive)
130 (save-excursion
131 (forward-paragraph)
132 (or (bolp) (newline 1))
133 (let ((end (point)))
134 (backward-paragraph)
135 (center-region (point) end))))
136
137(defun center-region (from to)
138 "Center each nonblank line starting in the region.
d367fa71 139See `center-line' for more info."
a2535589
JA
140 (interactive "r")
141 (if (> from to)
142 (let ((tem to))
143 (setq to from from tem)))
144 (save-excursion
145 (save-restriction
146 (narrow-to-region from to)
147 (goto-char from)
148 (while (not (eobp))
149 (or (save-excursion (skip-chars-forward " \t") (eolp))
150 (center-line))
151 (forward-line 1)))))
152
aaf6c7ef 153(defun center-line (&optional nlines)
a2535589
JA
154 "Center the line point is on, within the width specified by `fill-column'.
155This means adjusting the indentation so that it equals
aaf6c7ef
RS
156the distance between the end of the text and `fill-column'.
157The argument NLINES says how many lines to center."
158 (interactive "P")
159 (if nlines (setq nlines (prefix-numeric-value nlines)))
160 (while (not (eq nlines 0))
161 (save-excursion
162 (let ((lm (current-left-margin))
163 line-length)
164 (beginning-of-line)
165 (delete-horizontal-space)
166 (end-of-line)
167 (delete-horizontal-space)
168 (setq line-length (current-column))
169 (if (> (- fill-column lm line-length) 0)
84347194 170 (indent-line-to
aaf6c7ef
RS
171 (+ lm (/ (- fill-column lm line-length) 2))))))
172 (cond ((null nlines)
173 (setq nlines 0))
174 ((> nlines 0)
175 (setq nlines (1- nlines))
176 (forward-line 1))
177 ((< nlines 0)
178 (setq nlines (1+ nlines))
179 (forward-line -1)))))
d501f516 180
cbee283d 181;; arch-tag: a07ccaad-da13-4d7b-9c61-cd04f5926aab
d501f516 182;;; text-mode.el ends here