2001-07-15 16:15:35 +00:00
|
|
|
|
;;; text-mode.el --- text mode, and its idiosyncratic commands
|
1992-05-30 20:24:49 +00:00
|
|
|
|
|
2005-08-06 17:48:15 +00:00
|
|
|
|
;; Copyright (C) 1985, 1992, 1994, 2002, 2003, 2004,
|
2006-02-06 12:31:40 +00:00
|
|
|
|
;; 2005, 2006 Free Software Foundation, Inc.
|
1989-10-31 16:00:07 +00:00
|
|
|
|
|
1992-07-22 02:58:48 +00:00
|
|
|
|
;; Maintainer: FSF
|
2001-08-06 10:12:17 +00:00
|
|
|
|
;; Keywords: wp
|
1992-07-22 02:58:48 +00:00
|
|
|
|
|
1989-10-31 16:00:07 +00:00
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
1992-06-24 05:09:26 +00:00
|
|
|
|
;; the Free Software Foundation; either version 2, or (at your option)
|
1989-10-31 16:00:07 +00:00
|
|
|
|
;; any later version.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
1996-01-14 07:34:30 +00:00
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
2005-07-04 16:49:24 +00:00
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
1993-03-22 16:53:22 +00:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; This package provides the fundamental text mode documented in the
|
|
|
|
|
;; Emacs user's manual.
|
|
|
|
|
|
1992-07-14 21:51:51 +00:00
|
|
|
|
;;; Code:
|
1989-10-31 16:00:07 +00:00
|
|
|
|
|
1999-11-25 19:00:17 +00:00
|
|
|
|
(defcustom text-mode-hook nil
|
|
|
|
|
"Normal hook run when entering Text mode and many related modes."
|
|
|
|
|
:type 'hook
|
2006-05-30 18:37:15 +00:00
|
|
|
|
:options '(turn-on-auto-fill turn-on-flyspell)
|
1999-11-25 19:00:17 +00:00
|
|
|
|
:group 'data)
|
1997-08-28 02:37:58 +00:00
|
|
|
|
|
1997-08-28 02:53:44 +00:00
|
|
|
|
(defvar text-mode-variant nil
|
2001-10-30 06:00:10 +00:00
|
|
|
|
"Non-nil if this buffer's major mode is a variant of Text mode.
|
|
|
|
|
Use (derived-mode-p 'text-mode) instead.")
|
|
|
|
|
|
|
|
|
|
(defvar text-mode-syntax-table
|
|
|
|
|
(let ((st (make-syntax-table)))
|
|
|
|
|
(modify-syntax-entry ?\" ". " st)
|
|
|
|
|
(modify-syntax-entry ?\\ ". " st)
|
2002-10-21 21:04:50 +00:00
|
|
|
|
;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
|
|
|
|
|
(modify-syntax-entry ?' "w p" st)
|
2001-10-30 06:00:10 +00:00
|
|
|
|
st)
|
|
|
|
|
"Syntax table used while in `text-mode'.")
|
|
|
|
|
|
|
|
|
|
(defvar text-mode-map
|
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
|
(define-key map "\e\t" 'ispell-complete-word)
|
|
|
|
|
(define-key map "\es" 'center-line)
|
|
|
|
|
(define-key map "\eS" 'center-paragraph)
|
|
|
|
|
map)
|
|
|
|
|
"Keymap for `text-mode'.
|
|
|
|
|
Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
|
1989-10-31 16:00:07 +00:00
|
|
|
|
inherit all the commands defined in this map.")
|
|
|
|
|
|
|
|
|
|
|
2001-10-27 07:42:11 +00:00
|
|
|
|
(define-derived-mode text-mode nil "Text"
|
1997-06-03 04:27:53 +00:00
|
|
|
|
"Major mode for editing text written for humans to read.
|
1997-06-15 07:36:42 +00:00
|
|
|
|
In this mode, paragraphs are delimited only by blank or white lines.
|
1997-06-03 04:27:53 +00:00
|
|
|
|
You can thus get the full benefit of adaptive filling
|
|
|
|
|
(see the variable `adaptive-fill-mode').
|
1994-03-13 21:52:57 +00:00
|
|
|
|
\\{text-mode-map}
|
1997-06-03 04:27:53 +00:00
|
|
|
|
Turning on Text mode runs the normal hook `text-mode-hook'."
|
2002-02-06 14:57:57 +00:00
|
|
|
|
(make-local-variable 'text-mode-variant)
|
|
|
|
|
(setq text-mode-variant t)
|
2004-12-31 14:58:41 +00:00
|
|
|
|
(set (make-local-variable 'require-final-newline)
|
|
|
|
|
mode-require-final-newline)
|
2001-10-30 06:00:10 +00:00
|
|
|
|
(set (make-local-variable 'indent-line-function) 'indent-relative))
|
1997-06-03 04:27:53 +00:00
|
|
|
|
|
2001-10-30 05:26:44 +00:00
|
|
|
|
(define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
|
1997-06-03 04:27:53 +00:00
|
|
|
|
"Major mode for editing text, with leading spaces starting a paragraph.
|
|
|
|
|
In this mode, you do not need blank lines between paragraphs
|
|
|
|
|
when the first line of the following paragraph starts with whitespace.
|
2000-01-05 22:32:54 +00:00
|
|
|
|
`paragraph-indent-minor-mode' provides a similar facility as a minor mode.
|
1997-06-03 04:27:53 +00:00
|
|
|
|
Special commands:
|
|
|
|
|
\\{text-mode-map}
|
1997-06-11 19:02:06 +00:00
|
|
|
|
Turning on Paragraph-Indent Text mode runs the normal hooks
|
|
|
|
|
`text-mode-hook' and `paragraph-indent-text-mode-hook'."
|
2002-10-21 21:04:50 +00:00
|
|
|
|
:abbrev-table nil :syntax-table nil
|
2001-10-30 06:00:10 +00:00
|
|
|
|
(paragraph-indent-minor-mode))
|
2000-01-05 22:32:54 +00:00
|
|
|
|
|
|
|
|
|
(defun paragraph-indent-minor-mode ()
|
|
|
|
|
"Minor mode for editing text, with leading spaces starting a paragraph.
|
|
|
|
|
In this mode, you do not need blank lines between paragraphs when the
|
|
|
|
|
first line of the following paragraph starts with whitespace, as with
|
2003-09-28 08:49:44 +00:00
|
|
|
|
`paragraph-indent-text-mode'.
|
2000-01-05 22:32:54 +00:00
|
|
|
|
Turning on Paragraph-Indent minor mode runs the normal hook
|
|
|
|
|
`paragraph-indent-text-mode-hook'."
|
|
|
|
|
(interactive)
|
|
|
|
|
(set (make-local-variable 'paragraph-start)
|
2001-10-30 05:26:44 +00:00
|
|
|
|
(concat "[ \t\n\f]\\|" paragraph-start))
|
|
|
|
|
(set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
|
2000-01-05 22:32:54 +00:00
|
|
|
|
(run-hooks 'paragraph-indent-text-mode-hook))
|
2003-02-04 13:30:45 +00:00
|
|
|
|
|
1997-06-11 19:02:06 +00:00
|
|
|
|
(defalias 'indented-text-mode 'text-mode)
|
1989-10-31 16:00:07 +00:00
|
|
|
|
|
2002-02-14 01:31:21 +00:00
|
|
|
|
;; This can be made a no-op once all modes that use text-mode-hook
|
|
|
|
|
;; are "derived" from text-mode.
|
|
|
|
|
(defun text-mode-hook-identify ()
|
|
|
|
|
"Mark that this mode has run `text-mode-hook'.
|
|
|
|
|
This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
|
|
|
|
|
(set (make-local-variable 'text-mode-variant) t))
|
|
|
|
|
|
|
|
|
|
(add-hook 'text-mode-hook 'text-mode-hook-identify)
|
2002-02-08 13:57:03 +00:00
|
|
|
|
|
1997-08-28 02:53:44 +00:00
|
|
|
|
(defun toggle-text-mode-auto-fill ()
|
|
|
|
|
"Toggle whether to use Auto Fill in Text mode and related modes.
|
|
|
|
|
This command affects all buffers that use modes related to Text mode,
|
|
|
|
|
both existing buffers and buffers that you subsequently create."
|
|
|
|
|
(interactive)
|
2001-10-27 07:42:11 +00:00
|
|
|
|
(let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
|
1997-08-28 02:53:44 +00:00
|
|
|
|
(if enable-mode
|
|
|
|
|
(add-hook 'text-mode-hook 'turn-on-auto-fill)
|
|
|
|
|
(remove-hook 'text-mode-hook 'turn-on-auto-fill))
|
2001-10-27 07:42:11 +00:00
|
|
|
|
(dolist (buffer (buffer-list))
|
|
|
|
|
(with-current-buffer buffer
|
|
|
|
|
(if (or (derived-mode-p 'text-mode) text-mode-variant)
|
|
|
|
|
(auto-fill-mode (if enable-mode 1 0)))))
|
1997-08-28 02:53:44 +00:00
|
|
|
|
(message "Auto Fill %s in Text modes"
|
|
|
|
|
(if enable-mode "enabled" "disabled"))))
|
|
|
|
|
|
1989-10-31 16:00:07 +00:00
|
|
|
|
(defun center-paragraph ()
|
|
|
|
|
"Center each nonblank line in the paragraph at or after point.
|
1992-11-10 19:51:29 +00:00
|
|
|
|
See `center-line' for more info."
|
1989-10-31 16:00:07 +00:00
|
|
|
|
(interactive)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(forward-paragraph)
|
|
|
|
|
(or (bolp) (newline 1))
|
|
|
|
|
(let ((end (point)))
|
|
|
|
|
(backward-paragraph)
|
|
|
|
|
(center-region (point) end))))
|
|
|
|
|
|
|
|
|
|
(defun center-region (from to)
|
|
|
|
|
"Center each nonblank line starting in the region.
|
1992-11-10 19:51:29 +00:00
|
|
|
|
See `center-line' for more info."
|
1989-10-31 16:00:07 +00:00
|
|
|
|
(interactive "r")
|
|
|
|
|
(if (> from to)
|
|
|
|
|
(let ((tem to))
|
|
|
|
|
(setq to from from tem)))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(save-restriction
|
|
|
|
|
(narrow-to-region from to)
|
|
|
|
|
(goto-char from)
|
|
|
|
|
(while (not (eobp))
|
|
|
|
|
(or (save-excursion (skip-chars-forward " \t") (eolp))
|
|
|
|
|
(center-line))
|
|
|
|
|
(forward-line 1)))))
|
|
|
|
|
|
1995-09-10 17:44:25 +00:00
|
|
|
|
(defun center-line (&optional nlines)
|
1989-10-31 16:00:07 +00:00
|
|
|
|
"Center the line point is on, within the width specified by `fill-column'.
|
|
|
|
|
This means adjusting the indentation so that it equals
|
1995-09-10 17:44:25 +00:00
|
|
|
|
the distance between the end of the text and `fill-column'.
|
|
|
|
|
The argument NLINES says how many lines to center."
|
|
|
|
|
(interactive "P")
|
|
|
|
|
(if nlines (setq nlines (prefix-numeric-value nlines)))
|
|
|
|
|
(while (not (eq nlines 0))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(let ((lm (current-left-margin))
|
|
|
|
|
line-length)
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(delete-horizontal-space)
|
|
|
|
|
(end-of-line)
|
|
|
|
|
(delete-horizontal-space)
|
|
|
|
|
(setq line-length (current-column))
|
|
|
|
|
(if (> (- fill-column lm line-length) 0)
|
2000-03-12 15:22:14 +00:00
|
|
|
|
(indent-line-to
|
1995-09-10 17:44:25 +00:00
|
|
|
|
(+ lm (/ (- fill-column lm line-length) 2))))))
|
|
|
|
|
(cond ((null nlines)
|
|
|
|
|
(setq nlines 0))
|
|
|
|
|
((> nlines 0)
|
|
|
|
|
(setq nlines (1- nlines))
|
|
|
|
|
(forward-line 1))
|
|
|
|
|
((< nlines 0)
|
|
|
|
|
(setq nlines (1+ nlines))
|
|
|
|
|
(forward-line -1)))))
|
1992-05-30 20:24:49 +00:00
|
|
|
|
|
2003-09-01 15:45:59 +00:00
|
|
|
|
;;; arch-tag: a07ccaad-da13-4d7b-9c61-cd04f5926aab
|
1992-05-30 20:24:49 +00:00
|
|
|
|
;;; text-mode.el ends here
|