(x-select-text, x-cut-buffer-or-selection-value): Check if any of the
[emacs.git] / lisp / textmodes / text-mode.el
blobad39b6f896e609044296534ce3958d528d219866
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.
40 Use (derived-mode-p 'text-mode) instead.")
42 (defvar text-mode-syntax-table
43 (let ((st (make-syntax-table)))
44 (modify-syntax-entry ?\" ". " st)
45 (modify-syntax-entry ?\\ ". " st)
46 (modify-syntax-entry ?' "w " st)
47 st)
48 "Syntax table used while in `text-mode'.")
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'.
57 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
58 inherit all the commands defined in this map.")
61 (define-derived-mode text-mode nil "Text"
62 "Major mode for editing text written for humans to read.
63 In this mode, paragraphs are delimited only by blank or white lines.
64 You can thus get the full benefit of adaptive filling
65 (see the variable `adaptive-fill-mode').
66 \\{text-mode-map}
67 Turning on Text mode runs the normal hook `text-mode-hook'."
68 (make-local-variable 'text-mode-variant)
69 (setq text-mode-variant t)
70 (set (make-local-variable 'indent-line-function) 'indent-relative))
72 (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
73 "Major mode for editing text, with leading spaces starting a paragraph.
74 In this mode, you do not need blank lines between paragraphs
75 when the first line of the following paragraph starts with whitespace.
76 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
77 Special commands:
78 \\{text-mode-map}
79 Turning on Paragraph-Indent Text mode runs the normal hooks
80 `text-mode-hook' and `paragraph-indent-text-mode-hook'."
81 (paragraph-indent-minor-mode))
83 (defun paragraph-indent-minor-mode ()
84 "Minor mode for editing text, with leading spaces starting a paragraph.
85 In this mode, you do not need blank lines between paragraphs when the
86 first line of the following paragraph starts with whitespace, as with
87 `paragraph-indent-mode'.
88 Turning on Paragraph-Indent minor mode runs the normal hook
89 `paragraph-indent-text-mode-hook'."
90 (interactive)
91 (set (make-local-variable 'paragraph-start)
92 (concat "[ \t\n\f]\\|" paragraph-start))
93 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
94 (run-hooks 'paragraph-indent-text-mode-hook))
96 (defalias 'indented-text-mode 'text-mode)
98 ;; This can be made a no-op once all modes that use text-mode-hook
99 ;; are "derived" from text-mode.
100 (defun text-mode-hook-identify ()
101 "Mark that this mode has run `text-mode-hook'.
102 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
103 (set (make-local-variable 'text-mode-variant) t))
105 (add-hook 'text-mode-hook 'text-mode-hook-identify)
107 (defun toggle-text-mode-auto-fill ()
108 "Toggle whether to use Auto Fill in Text mode and related modes.
109 This command affects all buffers that use modes related to Text mode,
110 both existing buffers and buffers that you subsequently create."
111 (interactive)
112 (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
113 (if enable-mode
114 (add-hook 'text-mode-hook 'turn-on-auto-fill)
115 (remove-hook 'text-mode-hook 'turn-on-auto-fill))
116 (dolist (buffer (buffer-list))
117 (with-current-buffer buffer
118 (if (or (derived-mode-p 'text-mode) text-mode-variant)
119 (auto-fill-mode (if enable-mode 1 0)))))
120 (message "Auto Fill %s in Text modes"
121 (if enable-mode "enabled" "disabled"))))
123 (defun center-paragraph ()
124 "Center each nonblank line in the paragraph at or after point.
125 See `center-line' for more info."
126 (interactive)
127 (save-excursion
128 (forward-paragraph)
129 (or (bolp) (newline 1))
130 (let ((end (point)))
131 (backward-paragraph)
132 (center-region (point) end))))
134 (defun center-region (from to)
135 "Center each nonblank line starting in the region.
136 See `center-line' for more info."
137 (interactive "r")
138 (if (> from to)
139 (let ((tem to))
140 (setq to from from tem)))
141 (save-excursion
142 (save-restriction
143 (narrow-to-region from to)
144 (goto-char from)
145 (while (not (eobp))
146 (or (save-excursion (skip-chars-forward " \t") (eolp))
147 (center-line))
148 (forward-line 1)))))
150 (defun center-line (&optional nlines)
151 "Center the line point is on, within the width specified by `fill-column'.
152 This means adjusting the indentation so that it equals
153 the distance between the end of the text and `fill-column'.
154 The argument NLINES says how many lines to center."
155 (interactive "P")
156 (if nlines (setq nlines (prefix-numeric-value nlines)))
157 (while (not (eq nlines 0))
158 (save-excursion
159 (let ((lm (current-left-margin))
160 line-length)
161 (beginning-of-line)
162 (delete-horizontal-space)
163 (end-of-line)
164 (delete-horizontal-space)
165 (setq line-length (current-column))
166 (if (> (- fill-column lm line-length) 0)
167 (indent-line-to
168 (+ lm (/ (- fill-column lm line-length) 2))))))
169 (cond ((null nlines)
170 (setq nlines 0))
171 ((> nlines 0)
172 (setq nlines (1- nlines))
173 (forward-line 1))
174 ((< nlines 0)
175 (setq nlines (1+ nlines))
176 (forward-line -1)))))
178 ;;; text-mode.el ends here