text-mode.el 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ;;; text-mode.el --- text mode, and its idiosyncratic commands
  2. ;; Copyright (C) 1985, 1992, 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: wp
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This package provides the fundamental text mode documented in the
  19. ;; Emacs user's manual.
  20. ;;; Code:
  21. (defcustom text-mode-hook nil
  22. "Normal hook run when entering Text mode and many related modes."
  23. :type 'hook
  24. :options '(turn-on-auto-fill turn-on-flyspell)
  25. :group 'wp)
  26. (defvar text-mode-variant nil
  27. "Non-nil if this buffer's major mode is a variant of Text mode.
  28. Use (derived-mode-p 'text-mode) instead.")
  29. (defvar text-mode-syntax-table
  30. (let ((st (make-syntax-table)))
  31. (modify-syntax-entry ?\" ". " st)
  32. (modify-syntax-entry ?\\ ". " st)
  33. ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
  34. (modify-syntax-entry ?' "w p" st)
  35. st)
  36. "Syntax table used while in `text-mode'.")
  37. (defvar text-mode-map
  38. (let ((map (make-sparse-keymap)))
  39. (define-key map "\e\t" 'ispell-complete-word)
  40. map)
  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. (define-derived-mode text-mode nil "Text"
  45. "Major mode for editing text written for humans to read.
  46. In this mode, paragraphs are delimited only by blank or white lines.
  47. You can thus get the full benefit of adaptive filling
  48. (see the variable `adaptive-fill-mode').
  49. \\{text-mode-map}
  50. Turning on Text mode runs the normal hook `text-mode-hook'."
  51. (set (make-local-variable 'text-mode-variant) t)
  52. (set (make-local-variable 'require-final-newline)
  53. mode-require-final-newline)
  54. (set (make-local-variable 'indent-line-function) 'indent-relative))
  55. (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
  56. "Major mode for editing text, with leading spaces starting a paragraph.
  57. In this mode, you do not need blank lines between paragraphs
  58. when the first line of the following paragraph starts with whitespace.
  59. `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
  60. Special commands:
  61. \\{text-mode-map}
  62. Turning on Paragraph-Indent Text mode runs the normal hooks
  63. `text-mode-hook' and `paragraph-indent-text-mode-hook'."
  64. :abbrev-table nil :syntax-table nil
  65. (paragraph-indent-minor-mode))
  66. (defun paragraph-indent-minor-mode ()
  67. "Minor mode for editing text, with leading spaces starting a paragraph.
  68. In this mode, you do not need blank lines between paragraphs when the
  69. first line of the following paragraph starts with whitespace, as with
  70. `paragraph-indent-text-mode'.
  71. Turning on Paragraph-Indent minor mode runs the normal hook
  72. `paragraph-indent-text-mode-hook'."
  73. (interactive)
  74. (set (make-local-variable 'paragraph-start)
  75. (concat "[ \t\n\f]\\|" paragraph-start))
  76. (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
  77. (run-hooks 'paragraph-indent-text-mode-hook))
  78. (defalias 'indented-text-mode 'text-mode)
  79. ;; This can be made a no-op once all modes that use text-mode-hook
  80. ;; are "derived" from text-mode.
  81. (defun text-mode-hook-identify ()
  82. "Mark that this mode has run `text-mode-hook'.
  83. This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
  84. (set (make-local-variable 'text-mode-variant) t))
  85. (add-hook 'text-mode-hook 'text-mode-hook-identify)
  86. (defun toggle-text-mode-auto-fill ()
  87. "Toggle whether to use Auto Fill in Text mode and related modes.
  88. This command affects all buffers that use modes related to Text mode,
  89. both existing buffers and buffers that you subsequently create."
  90. (interactive)
  91. (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
  92. (if enable-mode
  93. (add-hook 'text-mode-hook 'turn-on-auto-fill)
  94. (remove-hook 'text-mode-hook 'turn-on-auto-fill))
  95. (dolist (buffer (buffer-list))
  96. (with-current-buffer buffer
  97. (if (or (derived-mode-p 'text-mode) text-mode-variant)
  98. (auto-fill-mode (if enable-mode 1 0)))))
  99. (message "Auto Fill %s in Text modes"
  100. (if enable-mode "enabled" "disabled"))))
  101. (define-key facemenu-keymap "\eS" 'center-paragraph)
  102. (defun center-paragraph ()
  103. "Center each nonblank line in the paragraph at or after point.
  104. See `center-line' for more info."
  105. (interactive)
  106. (save-excursion
  107. (forward-paragraph)
  108. (or (bolp) (newline 1))
  109. (let ((end (point)))
  110. (backward-paragraph)
  111. (center-region (point) end))))
  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. (define-key facemenu-keymap "\es" 'center-line)
  128. (defun center-line (&optional nlines)
  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. The argument NLINES says how many lines to center."
  133. (interactive "P")
  134. (if nlines (setq nlines (prefix-numeric-value nlines)))
  135. (while (not (eq nlines 0))
  136. (save-excursion
  137. (let ((lm (current-left-margin))
  138. line-length)
  139. (beginning-of-line)
  140. (delete-horizontal-space)
  141. (end-of-line)
  142. (delete-horizontal-space)
  143. (setq line-length (current-column))
  144. (if (> (- fill-column lm line-length) 0)
  145. (indent-line-to
  146. (+ lm (/ (- fill-column lm line-length) 2))))))
  147. (cond ((null nlines)
  148. (setq nlines 0))
  149. ((> nlines 0)
  150. (setq nlines (1- nlines))
  151. (forward-line 1))
  152. ((< nlines 0)
  153. (setq nlines (1+ nlines))
  154. (forward-line -1)))))
  155. ;;; text-mode.el ends here