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