fill.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ;; Fill commands for Emacs
  2. ;; Copyright (C) 1985, 1986 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. (defun set-fill-prefix ()
  18. "Set the fill-prefix to the current line up to point.
  19. Filling expects lines to start with the fill prefix
  20. and reinserts the fill prefix in each resulting line."
  21. (interactive)
  22. (setq fill-prefix (buffer-substring
  23. (save-excursion (beginning-of-line) (point))
  24. (point)))
  25. (if (equal fill-prefix "")
  26. (setq fill-prefix nil))
  27. (if fill-prefix
  28. (message "fill-prefix: \"%s\"" fill-prefix)
  29. (message "fill-prefix cancelled")))
  30. (defun fill-region-as-paragraph (from to &optional justify-flag)
  31. "Fill region as one paragraph: break lines to fit fill-column.
  32. Prefix arg means justify too.
  33. From program, pass args FROM, TO and JUSTIFY-FLAG."
  34. (interactive "r\nP")
  35. (save-restriction
  36. (narrow-to-region from to)
  37. (goto-char (point-min))
  38. (skip-chars-forward "\n")
  39. (narrow-to-region (point) (point-max))
  40. (setq from (point))
  41. (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
  42. (regexp-quote fill-prefix))))
  43. ;; Delete the fill prefix from every line except the first.
  44. ;; The first line may not even have a fill prefix.
  45. (and fpre
  46. (progn
  47. (goto-char (point-min))
  48. (forward-line 1)
  49. (while (not (eobp))
  50. (if (looking-at fpre)
  51. (delete-region (point) (match-end 0)))
  52. (forward-line 1))
  53. (goto-char (point-min))
  54. (and (looking-at fpre) (forward-char (length fill-prefix)))
  55. (setq from (point)))))
  56. ;; from is now before the text to fill,
  57. ;; but after any fill prefix on the first line.
  58. ;; Make sure sentences ending at end of line get an extra space.
  59. (goto-char from)
  60. (while (re-search-forward "[.?!][])""']*$" nil t)
  61. (insert ? ))
  62. ;; The change all newlines to spaces.
  63. (subst-char-in-region from (point-max) ?\n ?\ )
  64. ;; Flush excess spaces, except in the paragraph indentation.
  65. (goto-char from)
  66. (skip-chars-forward " \t")
  67. (while (re-search-forward " *" nil t)
  68. (delete-region
  69. (+ (match-beginning 0)
  70. (if (save-excursion
  71. (skip-chars-backward " ])\"'")
  72. (memq (preceding-char) '(?. ?? ?!)))
  73. 2 1))
  74. (match-end 0)))
  75. (goto-char (point-max))
  76. (delete-horizontal-space)
  77. (insert " ")
  78. (goto-char (point-min))
  79. (let ((prefixcol 0))
  80. (while (not (eobp))
  81. (move-to-column (1+ fill-column))
  82. (if (eobp)
  83. nil
  84. (skip-chars-backward "^ \n")
  85. (if (if (zerop prefixcol) (bolp) (>= prefixcol (current-column)))
  86. (skip-chars-forward "^ \n")
  87. (forward-char -1)))
  88. (delete-horizontal-space)
  89. (insert ?\n)
  90. (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
  91. (progn
  92. (insert fill-prefix)
  93. (setq prefixcol (current-column))))
  94. (and justify-flag (not (eobp))
  95. (progn
  96. (forward-line -1)
  97. (justify-current-line)
  98. (forward-line 1)))))))
  99. (defun fill-paragraph (arg)
  100. "Fill paragraph at or after point.
  101. Prefix arg means justify as well."
  102. (interactive "P")
  103. (save-excursion
  104. (forward-paragraph)
  105. (or (bolp) (newline 1))
  106. (let ((end (point)))
  107. (backward-paragraph)
  108. (fill-region-as-paragraph (point) end arg))))
  109. (defun fill-region (from to &optional justify-flag)
  110. "Fill each of the paragraphs in the region.
  111. Prefix arg (non-nil third arg, if called from program)
  112. means justify as well."
  113. (interactive "r\nP")
  114. (save-restriction
  115. (narrow-to-region from to)
  116. (goto-char (point-min))
  117. (while (not (eobp))
  118. (let ((initial (point))
  119. (end (progn
  120. (forward-paragraph 1) (point))))
  121. (forward-paragraph -1)
  122. (if (>= (point) initial)
  123. (fill-region-as-paragraph (point) end justify-flag)
  124. (goto-char end))))))
  125. (defun justify-current-line ()
  126. "Add spaces to line point is in, so it ends at fill-column."
  127. (interactive)
  128. (save-excursion
  129. (save-restriction
  130. (let (ncols beg)
  131. (beginning-of-line)
  132. (skip-chars-forward " \t")
  133. (setq beg (point))
  134. (end-of-line)
  135. (narrow-to-region beg (point))
  136. (goto-char beg)
  137. (while (re-search-forward " *" nil t)
  138. (delete-region
  139. (+ (match-beginning 0)
  140. (if (save-excursion
  141. (skip-chars-backward " ])\"'")
  142. (memq (preceding-char) '(?. ?? ?!)))
  143. 2 1))
  144. (match-end 0)))
  145. (goto-char beg)
  146. (while (re-search-forward "[.?!][])""']*\n" nil t)
  147. (forward-char -1)
  148. (insert ? ))
  149. (goto-char (point-max))
  150. (setq ncols (- fill-column (current-column)))
  151. (if (search-backward " " nil t)
  152. (while (> ncols 0)
  153. (let ((nmove (+ 3 (% (random) 3))))
  154. (while (> nmove 0)
  155. (or (search-backward " " nil t)
  156. (progn
  157. (goto-char (point-max))
  158. (search-backward " ")))
  159. (skip-chars-backward " ")
  160. (setq nmove (1- nmove))))
  161. (insert " ")
  162. (skip-chars-backward " ")
  163. (setq ncols (1- ncols))))))))
  164. (defun fill-individual-paragraphs (min max &optional justifyp mailp)
  165. "Fill each paragraph in region according to its individual fill prefix.
  166. Calling from a program, pass range to fill as first two arguments.
  167. Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
  168. JUSTIFY-FLAG to justify paragraphs (prefix arg),
  169. MAIL-FLAG for a mail message, i. e. don't fill header lines."
  170. (interactive "r\nP")
  171. (let (fill-prefix)
  172. (save-restriction
  173. (save-excursion
  174. (narrow-to-region min max)
  175. (goto-char (point-min))
  176. (while (progn
  177. (skip-chars-forward " \t\n")
  178. (not (eobp)))
  179. (setq fill-prefix (buffer-substring (point) (progn (beginning-of-line) (point))))
  180. (let ((fin (save-excursion (forward-paragraph) (point)))
  181. (start (point)))
  182. (if mailp
  183. (while (re-search-forward "[ \t]*[^ \t\n]*:" fin t)
  184. (forward-line 1)))
  185. (cond ((= start (point))
  186. (fill-region-as-paragraph (point) fin justifyp)
  187. (goto-char fin)))))))))