]> code.delx.au - gnu-emacs/blob - lisp/textmodes/text-mode.el
d13e3326b4e33b0fba2bbf78a1c669ae023cfb91
[gnu-emacs] / lisp / textmodes / text-mode.el
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands.
2
3 ;; Maintainer: FSF
4 ;; Last-Modified: 31 Oct 1989
5
6 ;; Copyright (C) 1985, 1992 Free Software Foundation, Inc.
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Code:
25
26 (defvar text-mode-syntax-table nil
27 "Syntax table used while in text mode.")
28
29 (defvar text-mode-abbrev-table nil
30 "Abbrev table used while in text mode.")
31 (define-abbrev-table 'text-mode-abbrev-table ())
32
33 (if text-mode-syntax-table
34 ()
35 (setq text-mode-syntax-table (make-syntax-table))
36 (modify-syntax-entry ?\" ". " text-mode-syntax-table)
37 (modify-syntax-entry ?\\ ". " text-mode-syntax-table)
38 (modify-syntax-entry ?' "w " text-mode-syntax-table))
39
40 (defvar text-mode-map nil
41 "Keymap for Text mode.
42 Many other modes, such as Mail mode, Outline mode and Indented Text mode,
43 inherit all the commands defined in this map.")
44
45 (if text-mode-map
46 ()
47 (setq text-mode-map (make-sparse-keymap))
48 (define-key text-mode-map "\t" 'tab-to-tab-stop)
49 (define-key text-mode-map "\es" 'center-line)
50 (define-key text-mode-map "\eS" 'center-paragraph))
51
52 \f
53 ;(defun non-saved-text-mode ()
54 ; "Like text-mode, but delete auto save file when file is saved for real."
55 ; (text-mode)
56 ; (make-local-variable 'delete-auto-save-files)
57 ; (setq delete-auto-save-files t))
58
59 (defun text-mode ()
60 "Major mode for editing text intended for humans to read. Special commands:\\{text-mode-map}
61 Turning on text-mode calls the value of the variable `text-mode-hook',
62 if that value is non-nil."
63 (interactive)
64 (kill-all-local-variables)
65 (use-local-map text-mode-map)
66 (setq mode-name "Text")
67 (setq major-mode 'text-mode)
68 (setq local-abbrev-table text-mode-abbrev-table)
69 (set-syntax-table text-mode-syntax-table)
70 (run-hooks 'text-mode-hook))
71
72 (defvar indented-text-mode-map ()
73 "Keymap for Indented Text mode.
74 All the commands defined in Text mode are inherited unless overridden.")
75
76 (if indented-text-mode-map
77 ()
78 ;; Make different definintion for TAB before the one in text-mode-map, but
79 ;; share the rest.
80 (let ((newmap (make-sparse-keymap)))
81 (define-key newmap "\t" 'indent-relative)
82 (setq indented-text-mode-map (nconc newmap text-mode-map))))
83
84 (defun indented-text-mode ()
85 "Major mode for editing indented text intended for humans to read.\\{indented-text-mode-map}
86 Turning on indented-text-mode calls the value of the variable `text-mode-hook',
87 if that value is non-nil."
88 (interactive)
89 (kill-all-local-variables)
90 (use-local-map text-mode-map)
91 (define-abbrev-table 'text-mode-abbrev-table ())
92 (setq local-abbrev-table text-mode-abbrev-table)
93 (set-syntax-table text-mode-syntax-table)
94 (make-local-variable 'indent-line-function)
95 (setq indent-line-function 'indent-relative-maybe)
96 (use-local-map indented-text-mode-map)
97 (setq mode-name "Indented Text")
98 (setq major-mode 'indented-text-mode)
99 (run-hooks 'text-mode-hook))
100
101 (defun center-paragraph ()
102 "Center each nonblank line in the paragraph at or after point.
103 See center-line for more info."
104 (interactive)
105 (save-excursion
106 (forward-paragraph)
107 (or (bolp) (newline 1))
108 (let ((end (point)))
109 (backward-paragraph)
110 (center-region (point) end))))
111
112 (defun center-region (from to)
113 "Center each nonblank line starting in the region.
114 See center-line for more info."
115 (interactive "r")
116 (if (> from to)
117 (let ((tem to))
118 (setq to from from tem)))
119 (save-excursion
120 (save-restriction
121 (narrow-to-region from to)
122 (goto-char from)
123 (while (not (eobp))
124 (or (save-excursion (skip-chars-forward " \t") (eolp))
125 (center-line))
126 (forward-line 1)))))
127
128 (defun center-line ()
129 "Center the line point is on, within the width specified by `fill-column'.
130 This means adjusting the indentation so that it equals
131 the distance between the end of the text and `fill-column'."
132 (interactive)
133 (save-excursion
134 (let (line-length)
135 (beginning-of-line)
136 (delete-horizontal-space)
137 (end-of-line)
138 (delete-horizontal-space)
139 (setq line-length (current-column))
140 (beginning-of-line)
141 (indent-to
142 (+ left-margin
143 (/ (- fill-column left-margin line-length) 2))))))
144
145 ;;; text-mode.el ends here