text-mode.el 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. ;;; text-mode.el --- text mode, and its idiosyncratic commands
  2. ;; Copyright (C) 1985, 1992, 1994, 2001-2017 Free Software Foundation,
  3. ;; Inc.
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: wp
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This package provides the fundamental text mode documented in the
  20. ;; Emacs user's manual.
  21. ;;; Code:
  22. ;; Normally non-nil defaults for hooks are bad, but since this file is
  23. ;; preloaded it's ok/better, and avoids this showing up in customize-rogue.
  24. (defcustom text-mode-hook '(text-mode-hook-identify)
  25. "Normal hook run when entering Text mode and many related modes."
  26. :type 'hook
  27. :options '(turn-on-auto-fill turn-on-flyspell)
  28. :group 'text)
  29. (defvar text-mode-variant nil
  30. "Non-nil if this buffer's major mode is a variant of Text mode.
  31. Use (derived-mode-p \\='text-mode) instead.")
  32. (defvar text-mode-syntax-table
  33. (let ((st (make-syntax-table)))
  34. (modify-syntax-entry ?\" ". " st)
  35. (modify-syntax-entry ?\\ ". " st)
  36. ;; We add `p' so that M-c on 'hello' leads to 'Hello' rather than 'hello'.
  37. (modify-syntax-entry ?' "w p" st)
  38. ;; UAX #29 says HEBREW PUNCTUATION GERESH behaves like a letter
  39. ;; for the purposes of finding word boundaries.
  40. (modify-syntax-entry #x5f3 "w ") ; GERESH
  41. ;; UAX #29 says HEBREW PUNCTUATION GERSHAYIM should not be a word
  42. ;; boundary when surrounded by letters. Our infrastructure for
  43. ;; finding a word boundary doesn't support 3-character
  44. ;; definitions, so for now simply make this a word-constituent
  45. ;; character. This leaves a problem of having GERSHAYIM at the
  46. ;; beginning or end of a word, where it should be a boundary;
  47. ;; FIXME.
  48. (modify-syntax-entry #x5f4 "w ") ; GERSHAYIM
  49. ;; These all should not be a word boundary when between letters,
  50. ;; according to UAX #29, so they again are prone to the same
  51. ;; problem as GERSHAYIM; FIXME.
  52. (modify-syntax-entry #xb7 "w ") ; MIDDLE DOT
  53. (modify-syntax-entry #x2027 "w ") ; HYPHENATION POINT
  54. (modify-syntax-entry #xff1a "w ") ; FULLWIDTH COLON
  55. st)
  56. "Syntax table used while in `text-mode'.")
  57. (defvar text-mode-map
  58. (let ((map (make-sparse-keymap)))
  59. (define-key map "\e\t" 'ispell-complete-word)
  60. (define-key map [menu-bar text]
  61. (cons "Text" (make-sparse-keymap "Text")))
  62. (bindings--define-key map [menu-bar text toggle-text-mode-auto-fill]
  63. '(menu-item "Auto Fill" toggle-text-mode-auto-fill
  64. :button (:toggle . (memq 'turn-on-auto-fill text-mode-hook))
  65. :help "Automatically fill text while typing in text modes (Auto Fill mode)"))
  66. (bindings--define-key map [menu-bar text paragraph-indent-minor-mode]
  67. '(menu-item "Paragraph Indent" paragraph-indent-minor-mode
  68. :button (:toggle . (bound-and-true-p paragraph-indent-minor-mode))
  69. :help "Toggle paragraph indent minor mode"))
  70. (bindings--define-key map [menu-bar text sep] menu-bar-separator)
  71. (bindings--define-key map [menu-bar text center-region]
  72. '(menu-item "Center Region" center-region
  73. :help "Center the marked region"
  74. :enable (region-active-p)))
  75. (bindings--define-key map [menu-bar text center-paragraph]
  76. '(menu-item "Center Paragraph" center-paragraph
  77. :help "Center the current paragraph"))
  78. (bindings--define-key map [menu-bar text center-line]
  79. '(menu-item "Center Line" center-line
  80. :help "Center the current line"))
  81. map)
  82. "Keymap for `text-mode'.
  83. Many other modes, such as `mail-mode', `outline-mode' and `indented-text-mode',
  84. inherit all the commands defined in this map.")
  85. (define-derived-mode text-mode nil "Text"
  86. "Major mode for editing text written for humans to read.
  87. In this mode, paragraphs are delimited only by blank or white lines.
  88. You can thus get the full benefit of adaptive filling
  89. (see the variable `adaptive-fill-mode').
  90. \\{text-mode-map}
  91. Turning on Text mode runs the normal hook `text-mode-hook'."
  92. (set (make-local-variable 'text-mode-variant) t)
  93. (set (make-local-variable 'require-final-newline)
  94. mode-require-final-newline)
  95. (set (make-local-variable 'indent-line-function) 'indent-relative))
  96. (define-derived-mode paragraph-indent-text-mode text-mode "Parindent"
  97. "Major mode for editing text, with leading spaces starting a paragraph.
  98. In this mode, you do not need blank lines between paragraphs
  99. when the first line of the following paragraph starts with whitespace.
  100. `paragraph-indent-minor-mode' provides a similar facility as a minor mode.
  101. Special commands:
  102. \\{text-mode-map}
  103. Turning on Paragraph-Indent Text mode runs the normal hooks
  104. `text-mode-hook' and `paragraph-indent-text-mode-hook'."
  105. :abbrev-table nil :syntax-table nil
  106. (paragraph-indent-minor-mode))
  107. (define-minor-mode paragraph-indent-minor-mode
  108. "Minor mode for editing text, with leading spaces starting a paragraph.
  109. In this mode, you do not need blank lines between paragraphs when the
  110. first line of the following paragraph starts with whitespace, as with
  111. `paragraph-indent-text-mode'.
  112. Turning on Paragraph-Indent minor mode runs the normal hook
  113. `paragraph-indent-text-mode-hook'."
  114. :initial-value nil
  115. ;; Change the definition of a paragraph start.
  116. (let ((ps-re "[ \t\n\f]\\|"))
  117. (if (eq t (compare-strings ps-re nil nil
  118. paragraph-start nil (length ps-re)))
  119. (if (not paragraph-indent-minor-mode)
  120. (set (make-local-variable 'paragraph-start)
  121. (substring paragraph-start (length ps-re))))
  122. (if paragraph-indent-minor-mode
  123. (set (make-local-variable 'paragraph-start)
  124. (concat ps-re paragraph-start)))))
  125. ;; Change the indentation function.
  126. (if paragraph-indent-minor-mode
  127. (add-function :override (local 'indent-line-function)
  128. #'indent-to-left-margin)
  129. (remove-function (local 'indent-line-function)
  130. #'indent-to-left-margin)))
  131. (defalias 'indented-text-mode 'text-mode)
  132. ;; This can be made a no-op once all modes that use text-mode-hook
  133. ;; are "derived" from text-mode. (As of 2015/04, and probably well before,
  134. ;; the only one I can find that doesn't so derive is rmail-edit-mode.)
  135. (defun text-mode-hook-identify ()
  136. "Mark that this mode has run `text-mode-hook'.
  137. This is how `toggle-text-mode-auto-fill' knows which buffers to operate on."
  138. (set (make-local-variable 'text-mode-variant) t))
  139. (defun toggle-text-mode-auto-fill ()
  140. "Toggle whether to use Auto Fill in Text mode and related modes.
  141. This command affects all buffers that use modes related to Text mode,
  142. both existing buffers and buffers that you subsequently create."
  143. (interactive)
  144. (let ((enable-mode (not (memq 'turn-on-auto-fill text-mode-hook))))
  145. (if enable-mode
  146. (add-hook 'text-mode-hook 'turn-on-auto-fill)
  147. (remove-hook 'text-mode-hook 'turn-on-auto-fill))
  148. (dolist (buffer (buffer-list))
  149. (with-current-buffer buffer
  150. (if (or (derived-mode-p 'text-mode) text-mode-variant)
  151. (auto-fill-mode (if enable-mode 1 0)))))
  152. (message "Auto Fill %s in Text modes"
  153. (if enable-mode "enabled" "disabled"))))
  154. (define-key facemenu-keymap "\eS" 'center-paragraph)
  155. (defun center-paragraph ()
  156. "Center each nonblank line in the paragraph at or after point.
  157. See `center-line' for more info."
  158. (interactive)
  159. (save-excursion
  160. (forward-paragraph)
  161. (or (bolp) (newline 1))
  162. (let ((end (point)))
  163. (backward-paragraph)
  164. (center-region (point) end))))
  165. (defun center-region (from to)
  166. "Center each nonblank line starting in the region.
  167. See `center-line' for more info."
  168. (interactive "r")
  169. (if (> from to)
  170. (let ((tem to))
  171. (setq to from from tem)))
  172. (save-excursion
  173. (save-restriction
  174. (narrow-to-region from to)
  175. (goto-char from)
  176. (while (not (eobp))
  177. (or (save-excursion (skip-chars-forward " \t") (eolp))
  178. (center-line))
  179. (forward-line 1)))))
  180. (define-key facemenu-keymap "\es" 'center-line)
  181. (defun center-line (&optional nlines)
  182. "Center the line point is on, within the width specified by `fill-column'.
  183. This means adjusting the indentation so that it equals
  184. the distance between the end of the text and `fill-column'.
  185. The argument NLINES says how many lines to center."
  186. (interactive "P")
  187. (if nlines (setq nlines (prefix-numeric-value nlines)))
  188. (while (not (eq nlines 0))
  189. (save-excursion
  190. (let ((lm (current-left-margin))
  191. line-length)
  192. (beginning-of-line)
  193. (delete-horizontal-space)
  194. (end-of-line)
  195. (delete-horizontal-space)
  196. (setq line-length (current-column))
  197. (if (> (- fill-column lm line-length) 0)
  198. (indent-line-to
  199. (+ lm (/ (- fill-column lm line-length) 2))))))
  200. (cond ((null nlines)
  201. (setq nlines 0))
  202. ((> nlines 0)
  203. (setq nlines (1- nlines))
  204. (forward-line 1))
  205. ((< nlines 0)
  206. (setq nlines (1+ nlines))
  207. (forward-line -1)))))
  208. (provide 'text-mode)
  209. ;;; text-mode.el ends here