[lice @ add data-types.lisp]
[lice.git] / text-mode.lisp
blob63fc63d5df231ba5fc53ebe239fd3536e0d1e360
1 ;;; text-mode.el --- text mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1992, 1994, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; This package provides the fundamental text mode documented in the
29 ;; Emacs user's manual.
31 ;;; Code:
33 (in-package "LICE")
35 (defcustom *text-mode-hook* nil
36 "Normal hook run when entering Text mode and many related modes."
37 :type 'hook
38 :options '(turn-on-auto-fill turn-on-flyspell)
39 :group 'data)
41 (define-buffer-local *text-mode-variant* nil
42 "Non-nil if this buffer's major mode is a variant of Text mode.
43 Use (derived-mode-p 'text-mode) instead.")
45 (defvar *text-mode-syntax-table*
46 (let ((st (make-syntax-table)))
47 (modify-syntax-entry #\" :punctuation :table st)
48 (modify-syntax-entry #\\ :punctuation :table st)
49 ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
50 (modify-syntax-entry #\' :word-constituent :flags '(:prefix) :table st)
51 st)
52 "Syntax table used while in `text-mode'.")
54 (defvar *text-mode-map*
55 (let ((map (make-sparse-keymap)))
56 (define-key map (kbd "M-TAB") 'ispell-complete-word)
57 (define-key map (kbd "M-s") 'center-line)
58 (define-key map (kbd "M-S") 'center-paragraph)
59 map)
60 "Keymap for `text-mode'.
61 Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
62 inherit all the commands defined in this map.")
65 (defvar *text-mode*
66 (make-instance 'major-mode
67 :name "Text"
68 :map *text-mode-map*
69 :syntax-table *text-mode-syntax-table*
70 :hook '*text-mode-hook*
71 :init
72 (lambda ()
73 (setf (make-local-variable '*text-mode-variant*) t
74 (make-local-variable '*require-final-newline*)
75 *mode-require-final-newline*
76 (make-local-variable '*indent-line-function*)
77 'indent-relative)))
78 "Major mode for editing text written for humans to read.
79 In this mode, paragraphs are delimited only by blank or white lines.
80 You can thus get the full benefit of adaptive filling
81 (see the variable `adaptive-fill-mode').
82 \\{text-mode-map}.")
84 (defcommand text-mode ()
85 "See `*text-mode*'."
86 (set-major-mode '*text-mode*))
88 (defvar *paragraph-indent-text-mode*
89 (make-instance 'major-mode
90 :name "parindent"
91 :inherit-map '(*text-mode*)
92 :inherit-syntax '(*text-mode*)
93 :inherit-init '(*text-mode*)
94 :init (lambda ()
95 (paragraph-indent-minor-mode)))
96 "Major mode for editing text, with leading spaces starting a paragraph.
97 In this mode, you do not need blank lines between paragraphs
98 when the first line of the following paragraph starts with whitespace.
99 `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
100 Special commands:
101 \\{text-mode-map}
102 Turning on Paragraph-Indent Text mode runs the normal hooks
103 `text-mode-hook' and `paragraph-indent-text-mode-hook'.")
105 (defcommand paragraph-indent-text-mode ()
106 "see `*paragraph-indent-text-mode*'."
107 (set-major-mode '*paragraph-indent-text-mode*))
109 (defcommand paragraph-indent-minor-mode ()
110 "Minor mode for editing text, with leading spaces starting a paragraph.
111 In this mode, you do not need blank lines between paragraphs when the
112 first line of the following paragraph starts with whitespace, as with
113 `paragraph-indent-text-mode'.
114 Turning on Paragraph-Indent minor mode runs the normal hook
115 `paragraph-indent-text-mode-hook'."
116 (setf (make-local-variable 'paragraph-start)
117 (concat "[ \t\n\f]\\|" paragraph-start))
118 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
119 (run-hooks '*paragraph-indent-text-mode-hook*))
121 ;;(defalias 'indented-text-mode 'text-mode)
123 ;; This can be made a no-op once all modes that use text-mode-hook
124 ;; are "derived" from text-mode.
125 (defun text-mode-hook-identify ()
126 "Mark that this mode has run `text-mode-hook'.
127 This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
128 (setf (make-local-variable *text-mode-variant*) t))
130 (add-hook '*text-mode-hook* 'text-mode-hook-identify)
132 (defcommand toggle-text-mode-auto-fill ()
133 "Toggle whether to use Auto Fill in Text mode and related modes.
134 This command affects all buffers that use modes related to Text mode,
135 both existing buffers and buffers that you subsequently create."
136 (let ((enable-mode (not (find 'turn-on-auto-fill *text-mode-hook*))))
137 (if enable-mode
138 (add-hook '*text-mode-hook* 'turn-on-auto-fill)
139 (remove-hook '*text-mode-hook* 'turn-on-auto-fill))
140 (dolist (buffer (buffer-list))
141 (with-current-buffer buffer
142 (if (or (eq (buffer-major-mode (current-buffer)) *text-mode*)
143 *text-mode-variant*)
144 (auto-fill-mode (if enable-mode 1 0)))))
145 (message "Auto Fill %s in Text modes"
146 (if enable-mode "enabled" "disabled"))))
148 (defcommand center-paragraph ()
149 "Center each nonblank line in the paragraph at or after point.
150 See `center-line' for more info."
151 (save-excursion
152 (forward-paragraph)
153 (or (bolp) (newline 1))
154 (let ((end (point)))
155 (backward-paragraph)
156 (center-region (point) end))))
158 (defcommand center-region ((from to)
159 :region-beginning :region-end)
160 "Center each nonblank line starting in the region.
161 See `center-line' for more info."
162 (if (> from to)
163 (let ((tem to))
164 (setq to from from tem)))
165 (save-excursion
166 (save-restriction
167 (narrow-to-region from to)
168 (goto-char from)
169 (while (not (eobp))
170 (or (save-excursion (skip-chars-forward " \t") (eolp))
171 (center-line))
172 (forward-line 1)))))
174 (defcommand center-line ((&optional nlines)
175 :raw-prefix)
176 "Center the line point is on, within the width specified by `fill-column'.
177 This means adjusting the indentation so that it equals
178 the distance between the end of the text and `fill-column'.
179 The argument NLINES says how many lines to center."
180 (if nlines (setq nlines (prefix-numeric-value nlines)))
181 (while (not (eq nlines 0))
182 (save-excursion
183 (let ((lm (current-left-margin))
184 line-length)
185 (beginning-of-line)
186 (delete-horizontal-space)
187 (end-of-line)
188 (delete-horizontal-space)
189 (setq line-length (current-column))
190 (if (> (- fill-column lm line-length) 0)
191 (indent-line-to
192 (+ lm (/ (- fill-column lm line-length) 2))))))
193 (cond ((null nlines)
194 (setq nlines 0))
195 ((> nlines 0)
196 (setq nlines (1- nlines))
197 (forward-line 1))
198 ((< nlines 0)
199 (setq nlines (1+ nlines))
200 (forward-line -1)))))
202 ;;; arch-tag: a07ccaad-da13-4d7b-9c61-cd04f5926aab
203 ;;; text-mode.el ends here