text-mode.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ;; Text mode, and its ideosyncratic commands.
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (defvar text-mode-syntax-table nil
  18. "Syntax table used while in text mode.")
  19. (defvar text-mode-abbrev-table nil
  20. "Abbrev table used while in text mode.")
  21. (define-abbrev-table 'text-mode-abbrev-table ())
  22. (if text-mode-syntax-table
  23. ()
  24. (setq text-mode-syntax-table (make-syntax-table))
  25. (set-syntax-table text-mode-syntax-table)
  26. (modify-syntax-entry ?\" ". " text-mode-syntax-table)
  27. (modify-syntax-entry ?\\ ". " text-mode-syntax-table)
  28. (modify-syntax-entry ?' "w " text-mode-syntax-table))
  29. (defvar text-mode-map nil "")
  30. (if text-mode-map
  31. ()
  32. (setq text-mode-map (make-sparse-keymap))
  33. (define-key text-mode-map "\t" 'tab-to-tab-stop)
  34. (define-key text-mode-map "\es" 'center-line)
  35. (define-key text-mode-map "\eS" 'center-paragraph))
  36. ;(defun non-saved-text-mode ()
  37. ; "Like text-mode, but delete auto save file when file is saved for real."
  38. ; (text-mode)
  39. ; (make-local-variable 'delete-auto-save-files)
  40. ; (setq delete-auto-save-files t))
  41. (defun text-mode ()
  42. "Major mode for editing text intended for humans to read. Special commands:\\{text-mode-map}
  43. Turning on text-mode calls the value of the variable text-mode-hook,
  44. if that value is non-nil."
  45. (interactive)
  46. (kill-all-local-variables)
  47. (use-local-map text-mode-map)
  48. (setq mode-name "Text")
  49. (setq major-mode 'text-mode)
  50. (setq local-abbrev-table text-mode-abbrev-table)
  51. (set-syntax-table text-mode-syntax-table)
  52. (run-hooks 'text-mode-hook))
  53. (defvar indented-text-mode-map ())
  54. (if indented-text-mode-map
  55. ()
  56. (setq indented-text-mode-map (make-sparse-keymap))
  57. (define-key indented-text-mode-map "\t" 'indent-relative)
  58. (define-key indented-text-mode-map "\es" 'center-line)
  59. (define-key indented-text-mode-map "\eS" 'center-paragraph))
  60. (defun indented-text-mode ()
  61. "Major mode for editing indented text intended for humans to read.\\{indented-text-mode-map}
  62. Turning on indented-text-mode calls the value of the variable text-mode-hook,
  63. if that value is non-nil."
  64. (interactive)
  65. (kill-all-local-variables)
  66. (use-local-map text-mode-map)
  67. (define-abbrev-table 'text-mode-abbrev-table ())
  68. (setq local-abbrev-table text-mode-abbrev-table)
  69. (set-syntax-table text-mode-syntax-table)
  70. (make-local-variable 'indent-line-function)
  71. (setq indent-line-function 'indent-relative-maybe)
  72. (use-local-map indented-text-mode-map)
  73. (setq mode-name "Indented Text")
  74. (setq major-mode 'indented-text-mode)
  75. (run-hooks 'text-mode-hook))
  76. (defun center-paragraph ()
  77. "Center each line in the paragraph at or after point.
  78. See center-line for more info."
  79. (interactive)
  80. (save-excursion
  81. (forward-paragraph)
  82. (or (bolp) (newline 1))
  83. (let ((end (point)))
  84. (backward-paragraph)
  85. (center-region (point) end))))
  86. (defun center-region (from to)
  87. "Center each line starting in the region.
  88. See center-line for more info."
  89. (interactive "r")
  90. (if (> from to)
  91. (let ((tem to))
  92. (setq to from from tem)))
  93. (save-excursion
  94. (goto-char from)
  95. (while (< (point) to)
  96. (center-line)
  97. (forward-line 1))))
  98. (defun center-line ()
  99. "Center the line point is on.
  100. This means adjusting its indentation to match
  101. the distance between the end of the text and fill-column."
  102. (interactive)
  103. (save-excursion
  104. (let (line-length)
  105. (beginning-of-line)
  106. (delete-horizontal-space)
  107. (end-of-line)
  108. (delete-horizontal-space)
  109. (setq line-length (current-column))
  110. (beginning-of-line)
  111. (indent-to
  112. (+ left-margin
  113. (/ (- fill-column left-margin line-length) 2))))))