fill.el 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. ;;; fill.el --- fill commands for Emacs -*- coding: utf-8 -*-
  2. ;; Copyright (C) 1985-1986, 1992, 1994-1997, 1999, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Maintainer: FSF
  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. ;; All the commands for filling text. These are documented in the Emacs
  20. ;; manual.
  21. ;;; Code:
  22. (defgroup fill nil
  23. "Indenting and filling text."
  24. :link '(custom-manual "(emacs)Filling")
  25. :group 'editing)
  26. (defcustom fill-individual-varying-indent nil
  27. "Controls criterion for a new paragraph in `fill-individual-paragraphs'.
  28. Non-nil means changing indent doesn't end a paragraph.
  29. That mode can handle paragraphs with extra indentation on the first line,
  30. but it requires separator lines between paragraphs.
  31. A value of nil means that any change in indentation starts a new paragraph."
  32. :type 'boolean
  33. :group 'fill)
  34. (defcustom colon-double-space nil
  35. "Non-nil means put two spaces after a colon when filling."
  36. :type 'boolean
  37. :group 'fill)
  38. (put 'colon-double-space 'safe-local-variable 'booleanp)
  39. (defvar fill-paragraph-function nil
  40. "Mode-specific function to fill a paragraph, or nil if there is none.
  41. If the function returns nil, then `fill-paragraph' does its normal work.
  42. A value of t means explicitly \"do nothing special\".
  43. Note: This only affects `fill-paragraph' and not `fill-region'
  44. nor `auto-fill-mode', so it is often better to use some other hook,
  45. such as `fill-forward-paragraph-function'.")
  46. (defvar fill-paragraph-handle-comment t
  47. "Non-nil means paragraph filling will try to pay attention to comments.")
  48. (defcustom enable-kinsoku t
  49. "Non-nil means enable \"kinsoku\" processing on filling paragraphs.
  50. Kinsoku processing is designed to prevent certain characters from being
  51. placed at the beginning or end of a line by filling.
  52. See the documentation of `kinsoku' for more information."
  53. :type 'boolean
  54. :group 'fill)
  55. (defun set-fill-prefix ()
  56. "Set the fill prefix to the current line up to point.
  57. Filling expects lines to start with the fill prefix and
  58. reinserts the fill prefix in each resulting line."
  59. (interactive)
  60. (let ((left-margin-pos (save-excursion (move-to-left-margin) (point))))
  61. (if (> (point) left-margin-pos)
  62. (progn
  63. (setq fill-prefix (buffer-substring left-margin-pos (point)))
  64. (if (equal fill-prefix "")
  65. (setq fill-prefix nil)))
  66. (setq fill-prefix nil)))
  67. (if fill-prefix
  68. (message "fill-prefix: \"%s\"" fill-prefix)
  69. (message "fill-prefix canceled")))
  70. (defcustom adaptive-fill-mode t
  71. "Non-nil means determine a paragraph's fill prefix from its text."
  72. :type 'boolean
  73. :group 'fill)
  74. (defcustom adaptive-fill-regexp
  75. ;; Added `!' for doxygen comments starting with `//!' or `/*!'.
  76. ;; Added `%' for TeX comments.
  77. ;; RMS: deleted the code to match `1.' and `(1)'.
  78. ;; Update mail-mode's paragraph-separate if you change this.
  79. (purecopy "[ \t]*\\([-–!|#%;>*·•‣⁃◦]+[ \t]*\\)*")
  80. "Regexp to match text at start of line that constitutes indentation.
  81. If Adaptive Fill mode is enabled, a prefix matching this pattern
  82. on the first and second lines of a paragraph is used as the
  83. standard indentation for the whole paragraph.
  84. If the paragraph has just one line, the indentation is taken from that
  85. line, but in that case `adaptive-fill-first-line-regexp' also plays
  86. a role."
  87. :type 'regexp
  88. :group 'fill)
  89. (defcustom adaptive-fill-first-line-regexp (purecopy "\\`[ \t]*\\'")
  90. "Regexp specifying whether to set fill prefix from a one-line paragraph.
  91. When a paragraph has just one line, then after `adaptive-fill-regexp'
  92. finds the prefix at the beginning of the line, if it doesn't
  93. match this regexp, it is replaced with whitespace.
  94. By default, this regexp matches sequences of just spaces and tabs.
  95. However, we never use a prefix from a one-line paragraph
  96. if it would act as a paragraph-starter on the second line."
  97. :type 'regexp
  98. :group 'fill)
  99. (defcustom adaptive-fill-function nil
  100. "Function to call to choose a fill prefix for a paragraph, or nil.
  101. A nil value means the function has not determined the fill prefix."
  102. :type '(choice (const nil) function)
  103. :group 'fill)
  104. (defvar fill-indent-according-to-mode nil ;Screws up CC-mode's filling tricks.
  105. "Whether or not filling should try to use the major mode's indentation.")
  106. (defun current-fill-column ()
  107. "Return the fill-column to use for this line.
  108. The fill-column to use for a buffer is stored in the variable `fill-column',
  109. but can be locally modified by the `right-margin' text property, which is
  110. subtracted from `fill-column'.
  111. The fill column to use for a line is the first column at which the column
  112. number equals or exceeds the local fill-column - right-margin difference."
  113. (save-excursion
  114. (if fill-column
  115. (let* ((here (line-beginning-position))
  116. (here-col 0)
  117. (eol (progn (end-of-line) (point)))
  118. margin fill-col change col)
  119. ;; Look separately at each region of line with a different
  120. ;; right-margin.
  121. (while (and (setq margin (get-text-property here 'right-margin)
  122. fill-col (- fill-column (or margin 0))
  123. change (text-property-not-all
  124. here eol 'right-margin margin))
  125. (progn (goto-char (1- change))
  126. (setq col (current-column))
  127. (< col fill-col)))
  128. (setq here change
  129. here-col col))
  130. (max here-col fill-col)))))
  131. (defun canonically-space-region (beg end)
  132. "Remove extra spaces between words in region.
  133. Leave one space between words, two at end of sentences or after colons
  134. \(depending on values of `sentence-end-double-space', `colon-double-space',
  135. and `sentence-end-without-period').
  136. Remove indentation from each line."
  137. (interactive "*r")
  138. ;; Ideally, we'd want to scan the text from the end, so that changes to
  139. ;; text don't affect the boundary, but the regexp we match against does
  140. ;; not match as eagerly when matching backward, so we instead use
  141. ;; a marker.
  142. (unless (markerp end) (setq end (copy-marker end t)))
  143. (let ((end-spc-re (concat "\\(" (sentence-end) "\\) *\\| +")))
  144. (save-excursion
  145. (goto-char beg)
  146. ;; Nuke tabs; they get screwed up in a fill.
  147. ;; This is quick, but loses when a tab follows the end of a sentence.
  148. ;; Actually, it is difficult to tell that from "Mr.\tSmith".
  149. ;; Blame the typist.
  150. (subst-char-in-region beg end ?\t ?\s)
  151. (while (and (< (point) end)
  152. (re-search-forward end-spc-re end t))
  153. (delete-region
  154. (cond
  155. ;; `sentence-end' matched and did not match all spaces.
  156. ;; I.e. it only matched the number of spaces it needs: drop the rest.
  157. ((and (match-end 1) (> (match-end 0) (match-end 1))) (match-end 1))
  158. ;; `sentence-end' matched but with nothing left. Either that means
  159. ;; nothing should be removed, or it means it's the "old-style"
  160. ;; sentence-end which matches all it can. Keep only 2 spaces.
  161. ;; We probably don't even need to check `sentence-end-double-space'.
  162. ((match-end 1)
  163. (min (match-end 0)
  164. (+ (if sentence-end-double-space 2 1)
  165. (save-excursion (goto-char (match-end 0))
  166. (skip-chars-backward " ")
  167. (point)))))
  168. (t ;; It's not an end of sentence.
  169. (+ (match-beginning 0)
  170. ;; Determine number of spaces to leave:
  171. (save-excursion
  172. (skip-chars-backward " ]})\"'")
  173. (cond ((and sentence-end-double-space
  174. (or (memq (preceding-char) '(?. ?? ?!))
  175. (and sentence-end-without-period
  176. (= (char-syntax (preceding-char)) ?w)))) 2)
  177. ((and colon-double-space
  178. (= (preceding-char) ?:)) 2)
  179. ((char-equal (preceding-char) ?\n) 0)
  180. (t 1))))))
  181. (match-end 0))))))
  182. (defun fill-common-string-prefix (s1 s2)
  183. "Return the longest common prefix of strings S1 and S2, or nil if none."
  184. (let ((cmp (compare-strings s1 nil nil s2 nil nil)))
  185. (if (eq cmp t)
  186. s1
  187. (setq cmp (1- (abs cmp)))
  188. (unless (zerop cmp)
  189. (substring s1 0 cmp)))))
  190. (defun fill-match-adaptive-prefix ()
  191. (let ((str (or
  192. (and adaptive-fill-function (funcall adaptive-fill-function))
  193. (and adaptive-fill-regexp (looking-at adaptive-fill-regexp)
  194. (match-string-no-properties 0)))))
  195. (if (>= (+ (current-left-margin) (length str)) (current-fill-column))
  196. ;; Death to insanely long prefixes.
  197. nil
  198. str)))
  199. (defun fill-context-prefix (from to &optional first-line-regexp)
  200. "Compute a fill prefix from the text between FROM and TO.
  201. This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function'
  202. and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role;
  203. we reject a prefix based on a one-line paragraph if that prefix would
  204. act as a paragraph-separator."
  205. (or first-line-regexp
  206. (setq first-line-regexp adaptive-fill-first-line-regexp))
  207. (save-excursion
  208. (goto-char from)
  209. (if (eolp) (forward-line 1))
  210. ;; Move to the second line unless there is just one.
  211. (move-to-left-margin)
  212. (let (first-line-prefix
  213. ;; Non-nil if we are on the second line.
  214. second-line-prefix)
  215. (setq first-line-prefix
  216. ;; We don't need to consider `paragraph-start' here since it
  217. ;; will be explicitly checked later on.
  218. ;; Also setting first-line-prefix to nil prevents
  219. ;; second-line-prefix from being used.
  220. ;; ((looking-at paragraph-start) nil)
  221. (fill-match-adaptive-prefix))
  222. (forward-line 1)
  223. (if (< (point) to)
  224. (progn
  225. (move-to-left-margin)
  226. (setq second-line-prefix
  227. (cond ((looking-at paragraph-start) nil) ;Can it happen? -Stef
  228. (t (fill-match-adaptive-prefix))))
  229. ;; If we get a fill prefix from the second line,
  230. ;; make sure it or something compatible is on the first line too.
  231. (when second-line-prefix
  232. (unless first-line-prefix (setq first-line-prefix ""))
  233. ;; If the non-whitespace chars match the first line,
  234. ;; just use it (this subsumes the 2 checks used previously).
  235. ;; Used when first line is `/* ...' and second-line is
  236. ;; ` * ...'.
  237. (let ((tmp second-line-prefix)
  238. (re "\\`"))
  239. (while (string-match "\\`[ \t]*\\([^ \t]+\\)" tmp)
  240. (setq re (concat re ".*" (regexp-quote (match-string 1 tmp))))
  241. (setq tmp (substring tmp (match-end 0))))
  242. ;; (assert (string-match "\\`[ \t]*\\'" tmp))
  243. (if (string-match re first-line-prefix)
  244. second-line-prefix
  245. ;; Use the longest common substring of both prefixes,
  246. ;; if there is one.
  247. (fill-common-string-prefix first-line-prefix
  248. second-line-prefix)))))
  249. ;; If we get a fill prefix from a one-line paragraph,
  250. ;; maybe change it to whitespace,
  251. ;; and check that it isn't a paragraph starter.
  252. (if first-line-prefix
  253. (let ((result
  254. ;; If first-line-prefix comes from the first line,
  255. ;; see if it seems reasonable to use for all lines.
  256. ;; If not, replace it with whitespace.
  257. (if (or (and first-line-regexp
  258. (string-match first-line-regexp
  259. first-line-prefix))
  260. (and comment-start-skip
  261. (string-match comment-start-skip
  262. first-line-prefix)))
  263. first-line-prefix
  264. (make-string (string-width first-line-prefix) ?\s))))
  265. ;; But either way, reject it if it indicates the start
  266. ;; of a paragraph when text follows it.
  267. (if (not (eq 0 (string-match paragraph-start
  268. (concat result "a"))))
  269. result)))))))
  270. (defun fill-single-word-nobreak-p ()
  271. "Don't break a line after the first or before the last word of a sentence."
  272. ;; Actually, allow breaking before the last word of a sentence, so long as
  273. ;; it's not the last word of the paragraph.
  274. (or (looking-at (concat "[ \t]*\\sw+" "\\(?:" (sentence-end) "\\)[ \t]*$"))
  275. (save-excursion
  276. (skip-chars-backward " \t")
  277. (and (/= (skip-syntax-backward "w") 0)
  278. (/= (skip-chars-backward " \t") 0)
  279. (/= (skip-chars-backward ".?!:") 0)
  280. (looking-at (sentence-end))))))
  281. (defun fill-french-nobreak-p ()
  282. "Return nil if French style allows breaking the line at point.
  283. This is used in `fill-nobreak-predicate' to prevent breaking lines just
  284. after an opening paren or just before a closing paren or a punctuation
  285. mark such as `?' or `:'. It is common in French writing to put a space
  286. at such places, which would normally allow breaking the line at those
  287. places."
  288. (or (looking-at "[ \t]*[])}»?!;:-]")
  289. (save-excursion
  290. (skip-chars-backward " \t")
  291. (unless (bolp)
  292. (backward-char 1)
  293. (or (looking-at "[([{«]")
  294. ;; Don't cut right after a single-letter word.
  295. (and (memq (preceding-char) '(?\t ?\s))
  296. (eq (char-syntax (following-char)) ?w)))))))
  297. (defcustom fill-nobreak-predicate nil
  298. "List of predicates for recognizing places not to break a line.
  299. The predicates are called with no arguments, with point at the place to
  300. be tested. If it returns t, fill commands do not break the line there."
  301. :group 'fill
  302. :type 'hook
  303. :options '(fill-french-nobreak-p fill-single-word-nobreak-p))
  304. (defcustom fill-nobreak-invisible nil
  305. "Non-nil means that fill commands do not break lines in invisible text."
  306. :type 'boolean
  307. :group 'fill)
  308. (defun fill-nobreak-p ()
  309. "Return nil if breaking the line at point is allowed.
  310. Can be customized with the variables `fill-nobreak-predicate'
  311. and `fill-nobreak-invisible'."
  312. (or
  313. (and fill-nobreak-invisible (invisible-p (point)))
  314. (unless (bolp)
  315. (or
  316. ;; Don't break after a period followed by just one space.
  317. ;; Move back to the previous place to break.
  318. ;; The reason is that if a period ends up at the end of a
  319. ;; line, further fills will assume it ends a sentence.
  320. ;; If we now know it does not end a sentence, avoid putting
  321. ;; it at the end of the line.
  322. (and sentence-end-double-space
  323. (save-excursion
  324. (skip-chars-backward " ")
  325. (and (eq (preceding-char) ?.)
  326. (looking-at " \\([^ ]\\|$\\)"))))
  327. ;; Another approach to the same problem.
  328. (save-excursion
  329. (skip-chars-backward " ")
  330. (and (eq (preceding-char) ?.)
  331. (not (progn (forward-char -1) (looking-at (sentence-end))))))
  332. ;; Don't split a line if the rest would look like a new paragraph.
  333. (unless use-hard-newlines
  334. (save-excursion
  335. (skip-chars-forward " \t")
  336. ;; If this break point is at the end of the line,
  337. ;; which can occur for auto-fill, don't consider the newline
  338. ;; which follows as a reason to return t.
  339. (and (not (eolp))
  340. (looking-at paragraph-start))))
  341. (run-hook-with-args-until-success 'fill-nobreak-predicate)))))
  342. (defvar fill-find-break-point-function-table (make-char-table nil)
  343. "Char-table of special functions to find line breaking point.")
  344. (defvar fill-nospace-between-words-table (make-char-table nil)
  345. "Char-table of characters that don't use space between words.")
  346. (progn
  347. ;; Register `kinsoku' for scripts HAN, KANA, BOPOMOFO, and CJK-MISC.
  348. ;; Also tell that they don't use space between words.
  349. (map-char-table
  350. #'(lambda (key val)
  351. (when (memq val '(han kana bopomofo cjk-misc))
  352. (set-char-table-range fill-find-break-point-function-table
  353. key 'kinsoku)
  354. (set-char-table-range fill-nospace-between-words-table
  355. key t)))
  356. char-script-table)
  357. ;; Do the same thing also for full width characters and half
  358. ;; width kana variants.
  359. (set-char-table-range fill-find-break-point-function-table
  360. '(#xFF01 . #xFFE6) 'kinsoku)
  361. (set-char-table-range fill-nospace-between-words-table
  362. '(#xFF01 . #xFFE6) 'kinsoku))
  363. (defun fill-find-break-point (limit)
  364. "Move point to a proper line breaking position of the current line.
  365. Don't move back past the buffer position LIMIT.
  366. This function is called when we are going to break the current line
  367. after or before a non-ASCII character. If the charset of the
  368. character has the property `fill-find-break-point-function', this
  369. function calls the property value as a function with one arg LIMIT.
  370. If the charset has no such property, do nothing."
  371. (let ((func (or
  372. (aref fill-find-break-point-function-table (following-char))
  373. (aref fill-find-break-point-function-table (preceding-char)))))
  374. (if (and func (fboundp func))
  375. (funcall func limit))))
  376. (defun fill-delete-prefix (from to prefix)
  377. "Delete the fill prefix from every line except the first.
  378. The first line may not even have a fill prefix.
  379. Point is moved to just past the fill prefix on the first line."
  380. (let ((fpre (if (and prefix (not (string-match "\\`[ \t]*\\'" prefix)))
  381. (concat "[ \t]*\\("
  382. (replace-regexp-in-string
  383. "[ \t]+" "[ \t]*"
  384. (regexp-quote prefix))
  385. "\\)?[ \t]*")
  386. "[ \t]*")))
  387. (goto-char from)
  388. ;; Why signal an error here? The problem needs to be caught elsewhere.
  389. ;; (if (>= (+ (current-left-margin) (length prefix))
  390. ;; (current-fill-column))
  391. ;; (error "fill-prefix too long for specified width"))
  392. (forward-line 1)
  393. (while (< (point) to)
  394. (if (looking-at fpre)
  395. (delete-region (point) (match-end 0)))
  396. (forward-line 1))
  397. (goto-char from)
  398. (if (looking-at fpre)
  399. (goto-char (match-end 0)))
  400. (point)))
  401. ;; The `fill-space' property carries the string with which a newline
  402. ;; should be replaced when unbreaking a line (in fill-delete-newlines).
  403. ;; It is added to newline characters by fill-newline when the default
  404. ;; behavior of fill-delete-newlines is not what we want.
  405. (add-to-list 'text-property-default-nonsticky '(fill-space . t))
  406. (defun fill-delete-newlines (from to justify nosqueeze squeeze-after)
  407. (goto-char from)
  408. ;; Make sure sentences ending at end of line get an extra space.
  409. ;; loses on split abbrevs ("Mr.\nSmith")
  410. (let ((eol-double-space-re
  411. (cond
  412. ((not colon-double-space) (concat (sentence-end) "$"))
  413. ;; Try to add the : inside the `sentence-end' regexp.
  414. ((string-match "\\[[^][]*\\(\\.\\)[^][]*\\]" (sentence-end))
  415. (concat (replace-match ".:" nil nil (sentence-end) 1) "$"))
  416. ;; Can't find the right spot to insert the colon.
  417. (t "[.?!:][])}\"']*$")))
  418. (sentence-end-without-space-list
  419. (string-to-list sentence-end-without-space)))
  420. (while (re-search-forward eol-double-space-re to t)
  421. (or (>= (point) to) (memq (char-before) '(?\t ?\s))
  422. (memq (char-after (match-beginning 0))
  423. sentence-end-without-space-list)
  424. (insert-and-inherit ?\s))))
  425. (goto-char from)
  426. (if enable-multibyte-characters
  427. ;; Delete unnecessary newlines surrounded by words. The
  428. ;; character category `|' means that we can break a line at the
  429. ;; character. And, char-table
  430. ;; `fill-nospace-between-words-table' tells how to concatenate
  431. ;; words. If a character has non-nil value in the table, never
  432. ;; put spaces between words, thus delete a newline between them.
  433. ;; Otherwise, delete a newline only when a character preceding a
  434. ;; newline has non-nil value in that table.
  435. (while (search-forward "\n" to t)
  436. (if (get-text-property (match-beginning 0) 'fill-space)
  437. (replace-match (get-text-property (match-beginning 0) 'fill-space))
  438. (let ((prev (char-before (match-beginning 0)))
  439. (next (following-char)))
  440. (if (and (or (aref (char-category-set next) ?|)
  441. (aref (char-category-set prev) ?|))
  442. (or (aref fill-nospace-between-words-table next)
  443. (aref fill-nospace-between-words-table prev)))
  444. (delete-char -1))))))
  445. (goto-char from)
  446. (skip-chars-forward " \t")
  447. ;; Then change all newlines to spaces.
  448. (subst-char-in-region from to ?\n ?\s)
  449. (if (and nosqueeze (not (eq justify 'full)))
  450. nil
  451. (canonically-space-region (or squeeze-after (point)) to)
  452. ;; Remove trailing whitespace.
  453. ;; Maybe canonically-space-region should do that.
  454. (goto-char to) (delete-char (- (skip-chars-backward " \t"))))
  455. (goto-char from))
  456. (defun fill-move-to-break-point (linebeg)
  457. "Move to the position where the line should be broken.
  458. The break position will be always after LINEBEG and generally before point."
  459. ;; If the fill column is before linebeg, move to linebeg.
  460. (if (> linebeg (point)) (goto-char linebeg))
  461. ;; Move back to the point where we can break the line
  462. ;; at. We break the line between word or after/before
  463. ;; the character which has character category `|'. We
  464. ;; search space, \c| followed by a character, or \c|
  465. ;; following a character. If not found, place
  466. ;; the point at linebeg.
  467. (while
  468. (when (re-search-backward "[ \t]\\|\\c|.\\|.\\c|" linebeg 0)
  469. ;; In case of space, we place the point at next to
  470. ;; the point where the break occurs actually,
  471. ;; because we don't want to change the following
  472. ;; logic of original Emacs. In case of \c|, the
  473. ;; point is at the place where the break occurs.
  474. (forward-char 1)
  475. (when (fill-nobreak-p) (skip-chars-backward " \t" linebeg))))
  476. ;; Move back over the single space between the words.
  477. (skip-chars-backward " \t")
  478. ;; If the left margin and fill prefix by themselves
  479. ;; pass the fill-column. or if they are zero
  480. ;; but we have no room for even one word,
  481. ;; keep at least one word or a character which has
  482. ;; category `|' anyway.
  483. (if (>= linebeg (point))
  484. ;; Ok, skip at least one word or one \c| character.
  485. ;; Meanwhile, don't stop at a period followed by one space.
  486. (let ((to (line-end-position))
  487. (first t))
  488. (goto-char linebeg)
  489. (while (and (< (point) to) (or first (fill-nobreak-p)))
  490. ;; Find a breakable point while ignoring the
  491. ;; following spaces.
  492. (skip-chars-forward " \t")
  493. (if (looking-at "\\c|")
  494. (forward-char 1)
  495. (let ((pos (save-excursion
  496. (skip-chars-forward "^ \n\t")
  497. (point))))
  498. (if (re-search-forward "\\c|" pos t)
  499. (forward-char -1)
  500. (goto-char pos))))
  501. (setq first nil)))
  502. (if enable-multibyte-characters
  503. ;; If we are going to break the line after or
  504. ;; before a non-ascii character, we may have to
  505. ;; run a special function for the charset of the
  506. ;; character to find the correct break point.
  507. (if (not (and (eq (charset-after (1- (point))) 'ascii)
  508. (eq (charset-after (point)) 'ascii)))
  509. ;; Make sure we take SOMETHING after the fill prefix if any.
  510. (fill-find-break-point linebeg)))))
  511. ;; Like text-properties-at but don't include `composition' property.
  512. (defun fill-text-properties-at (pos)
  513. (let ((l (text-properties-at pos))
  514. prop-list)
  515. (while l
  516. (unless (eq (car l) 'composition)
  517. (setq prop-list
  518. (cons (car l) (cons (cadr l) prop-list))))
  519. (setq l (cddr l)))
  520. prop-list))
  521. (defun fill-newline ()
  522. ;; Replace whitespace here with one newline, then
  523. ;; indent to left margin.
  524. (skip-chars-backward " \t")
  525. (insert ?\n)
  526. ;; Give newline the properties of the space(s) it replaces
  527. (set-text-properties (1- (point)) (point)
  528. (fill-text-properties-at (point)))
  529. (and (looking-at "\\( [ \t]*\\)\\(\\c|\\)?")
  530. (or (aref (char-category-set (or (char-before (1- (point))) ?\000)) ?|)
  531. (match-end 2))
  532. ;; When refilling later on, this newline would normally not be replaced
  533. ;; by a space, so we need to mark it specially to re-install the space
  534. ;; when we unfill.
  535. (put-text-property (1- (point)) (point) 'fill-space (match-string 1)))
  536. ;; If we don't want breaks in invisible text, don't insert
  537. ;; an invisible newline.
  538. (if fill-nobreak-invisible
  539. (remove-text-properties (1- (point)) (point)
  540. '(invisible t)))
  541. (if (or fill-prefix
  542. (not fill-indent-according-to-mode))
  543. (fill-indent-to-left-margin)
  544. (indent-according-to-mode))
  545. ;; Insert the fill prefix after indentation.
  546. (and fill-prefix (not (equal fill-prefix ""))
  547. ;; Markers that were after the whitespace are now at point: insert
  548. ;; before them so they don't get stuck before the prefix.
  549. (insert-before-markers-and-inherit fill-prefix)))
  550. (defun fill-indent-to-left-margin ()
  551. "Indent current line to the column given by `current-left-margin'."
  552. (let ((beg (point)))
  553. (indent-line-to (current-left-margin))
  554. (put-text-property beg (point) 'face 'default)))
  555. (defun fill-region-as-paragraph (from to &optional justify
  556. nosqueeze squeeze-after)
  557. "Fill the region as one paragraph.
  558. It removes any paragraph breaks in the region and extra newlines at the end,
  559. indents and fills lines between the margins given by the
  560. `current-left-margin' and `current-fill-column' functions.
  561. \(In most cases, the variable `fill-column' controls the width.)
  562. It leaves point at the beginning of the line following the paragraph.
  563. Normally performs justification according to the `current-justification'
  564. function, but with a prefix arg, does full justification instead.
  565. From a program, optional third arg JUSTIFY can specify any type of
  566. justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
  567. between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
  568. means don't canonicalize spaces before that position.
  569. Return the `fill-prefix' used for filling.
  570. If `sentence-end-double-space' is non-nil, then period followed by one
  571. space does not end a sentence, so don't break a line there."
  572. (interactive (progn
  573. (barf-if-buffer-read-only)
  574. (list (region-beginning) (region-end)
  575. (if current-prefix-arg 'full))))
  576. (unless (memq justify '(t nil none full center left right))
  577. (setq justify 'full))
  578. ;; Make sure "to" is the endpoint.
  579. (goto-char (min from to))
  580. (setq to (max from to))
  581. ;; Ignore blank lines at beginning of region.
  582. (skip-chars-forward " \t\n")
  583. (let ((from-plus-indent (point))
  584. (oneleft nil))
  585. (beginning-of-line)
  586. ;; We used to round up to whole line, but that prevents us from
  587. ;; correctly handling filling of mixed code-and-comment where we do want
  588. ;; to fill the comment but not the code. So only use (point) if it's
  589. ;; further than `from', which means that `from' is followed by some
  590. ;; number of empty lines.
  591. (setq from (max (point) from))
  592. ;; Delete all but one soft newline at end of region.
  593. ;; And leave TO before that one.
  594. (goto-char to)
  595. (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
  596. (if (and oneleft
  597. (not (and use-hard-newlines
  598. (get-text-property (1- (point)) 'hard))))
  599. (delete-char -1)
  600. (backward-char 1)
  601. (setq oneleft t)))
  602. (setq to (copy-marker (point) t))
  603. ;; ;; If there was no newline, and there is text in the paragraph, then
  604. ;; ;; create a newline.
  605. ;; (if (and (not oneleft) (> to from-plus-indent))
  606. ;; (newline))
  607. (goto-char from-plus-indent))
  608. (if (not (> to (point)))
  609. nil ;; There is no paragraph, only whitespace: exit now.
  610. (or justify (setq justify (current-justification)))
  611. ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
  612. (let ((fill-prefix fill-prefix))
  613. ;; Figure out how this paragraph is indented, if desired.
  614. (when (and adaptive-fill-mode
  615. (or (null fill-prefix) (string= fill-prefix "")))
  616. (setq fill-prefix (fill-context-prefix from to))
  617. ;; Ignore a white-space only fill-prefix
  618. ;; if we indent-according-to-mode.
  619. (when (and fill-prefix fill-indent-according-to-mode
  620. (string-match "\\`[ \t]*\\'" fill-prefix))
  621. (setq fill-prefix nil)))
  622. (goto-char from)
  623. (beginning-of-line)
  624. (if (not justify) ; filling disabled: just check indentation
  625. (progn
  626. (goto-char from)
  627. (while (< (point) to)
  628. (if (and (not (eolp))
  629. (< (current-indentation) (current-left-margin)))
  630. (fill-indent-to-left-margin))
  631. (forward-line 1)))
  632. (if use-hard-newlines
  633. (remove-list-of-text-properties from to '(hard)))
  634. ;; Make sure first line is indented (at least) to left margin...
  635. (if (or (memq justify '(right center))
  636. (< (current-indentation) (current-left-margin)))
  637. (fill-indent-to-left-margin))
  638. ;; Delete the fill-prefix from every line.
  639. (fill-delete-prefix from to fill-prefix)
  640. (setq from (point))
  641. ;; FROM, and point, are now before the text to fill,
  642. ;; but after any fill prefix on the first line.
  643. (fill-delete-newlines from to justify nosqueeze squeeze-after)
  644. ;; This is the actual filling loop.
  645. (goto-char from)
  646. (let (linebeg)
  647. (while (< (point) to)
  648. (setq linebeg (point))
  649. (move-to-column (current-fill-column))
  650. (if (when (< (point) to)
  651. ;; Find the position where we'll break the line.
  652. (forward-char 1) ;Use an immediately following space, if any.
  653. (fill-move-to-break-point linebeg)
  654. ;; Check again to see if we got to the end of
  655. ;; the paragraph.
  656. (skip-chars-forward " \t")
  657. (< (point) to))
  658. ;; Found a place to cut.
  659. (progn
  660. (fill-newline)
  661. (when justify
  662. ;; Justify the line just ended, if desired.
  663. (save-excursion
  664. (forward-line -1)
  665. (justify-current-line justify nil t))))
  666. (goto-char to)
  667. ;; Justify this last line, if desired.
  668. (if justify (justify-current-line justify t t))))))
  669. ;; Leave point after final newline.
  670. (goto-char to)
  671. (unless (eobp) (forward-char 1))
  672. ;; Return the fill-prefix we used
  673. fill-prefix)))
  674. (defsubst skip-line-prefix (prefix)
  675. "If point is inside the string PREFIX at the beginning of line, move past it."
  676. (when (and prefix
  677. (< (- (point) (line-beginning-position)) (length prefix))
  678. (save-excursion
  679. (beginning-of-line)
  680. (looking-at (regexp-quote prefix))))
  681. (goto-char (match-end 0))))
  682. (defun fill-minibuffer-function (arg)
  683. "Fill a paragraph in the minibuffer, ignoring the prompt."
  684. (save-restriction
  685. (narrow-to-region (minibuffer-prompt-end) (point-max))
  686. (fill-paragraph arg)))
  687. (defvar fill-forward-paragraph-function 'forward-paragraph
  688. "Function to move over paragraphs used by the filling code.
  689. It is called with a single argument specifying the number of paragraphs to move.
  690. Just like `forward-paragraph', it should return the number of paragraphs
  691. left to move.")
  692. (defun fill-forward-paragraph (arg)
  693. (funcall fill-forward-paragraph-function arg))
  694. (defun fill-paragraph (&optional justify region)
  695. "Fill paragraph at or after point.
  696. If JUSTIFY is non-nil (interactively, with prefix argument), justify as well.
  697. If `sentence-end-double-space' is non-nil, then period followed by one
  698. space does not end a sentence, so don't break a line there.
  699. The variable `fill-column' controls the width for filling.
  700. If `fill-paragraph-function' is non-nil, we call it (passing our
  701. argument to it), and if it returns non-nil, we simply return its value.
  702. If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling.
  703. The REGION argument is non-nil if called interactively; in that
  704. case, if Transient Mark mode is enabled and the mark is active,
  705. call `fill-region' to fill each of the paragraphs in the active
  706. region, instead of just filling the current paragraph."
  707. (interactive (progn
  708. (barf-if-buffer-read-only)
  709. (list (if current-prefix-arg 'full) t)))
  710. (or
  711. ;; 1. Fill the region if it is active when called interactively.
  712. (and region transient-mark-mode mark-active
  713. (not (eq (region-beginning) (region-end)))
  714. (or (fill-region (region-beginning) (region-end) justify) t))
  715. ;; 2. Try fill-paragraph-function.
  716. (and (not (eq fill-paragraph-function t))
  717. (or fill-paragraph-function
  718. (and (minibufferp (current-buffer))
  719. (= 1 (point-min))))
  720. (let ((function (or fill-paragraph-function
  721. ;; In the minibuffer, don't count the width
  722. ;; of the prompt.
  723. 'fill-minibuffer-function))
  724. ;; If fill-paragraph-function is set, it probably takes care
  725. ;; of comments and stuff. If not, it will have to set
  726. ;; fill-paragraph-handle-comment back to t explicitly or
  727. ;; return nil.
  728. (fill-paragraph-handle-comment nil)
  729. (fill-paragraph-function t))
  730. (funcall function justify)))
  731. ;; 3. Try our syntax-aware filling code.
  732. (and fill-paragraph-handle-comment
  733. ;; Our code only handles \n-terminated comments right now.
  734. comment-start (equal comment-end "")
  735. (let ((fill-paragraph-handle-comment nil))
  736. (fill-comment-paragraph justify)))
  737. ;; 4. If it all fails, default to the good ol' text paragraph filling.
  738. (let ((before (point))
  739. (paragraph-start paragraph-start)
  740. ;; Fill prefix used for filling the paragraph.
  741. fill-pfx)
  742. ;; Try to prevent code sections and comment sections from being
  743. ;; filled together.
  744. (when (and fill-paragraph-handle-comment comment-start-skip)
  745. (setq paragraph-start
  746. (concat paragraph-start "\\|[ \t]*\\(?:"
  747. comment-start-skip "\\)")))
  748. (save-excursion
  749. ;; To make sure the return value of forward-paragraph is meaningful,
  750. ;; we have to start from the beginning of line, otherwise skipping
  751. ;; past the last few chars of a paragraph-separator would count as
  752. ;; a paragraph (and not skipping any chars at EOB would not count
  753. ;; as a paragraph even if it is).
  754. (move-to-left-margin)
  755. (if (not (zerop (fill-forward-paragraph 1)))
  756. ;; There's no paragraph at or after point: give up.
  757. (setq fill-pfx "")
  758. (let ((end (point))
  759. (beg (progn (fill-forward-paragraph -1) (point))))
  760. (goto-char before)
  761. (setq fill-pfx
  762. (if use-hard-newlines
  763. ;; Can't use fill-region-as-paragraph, since this
  764. ;; paragraph may still contain hard newlines. See
  765. ;; fill-region.
  766. (fill-region beg end justify)
  767. (fill-region-as-paragraph beg end justify))))))
  768. fill-pfx)))
  769. (declare-function comment-search-forward "newcomment" (limit &optional noerror))
  770. (declare-function comment-string-strip "newcomment" (str beforep afterp))
  771. (defun fill-comment-paragraph (&optional justify)
  772. "Fill current comment.
  773. If we're not in a comment, just return nil so that the caller
  774. can take care of filling. JUSTIFY is used as in `fill-paragraph'."
  775. (comment-normalize-vars)
  776. (let (has-code-and-comment ; Non-nil if it contains code and a comment.
  777. comin comstart)
  778. ;; Figure out what kind of comment we are looking at.
  779. (save-excursion
  780. (beginning-of-line)
  781. (when (setq comstart (comment-search-forward (line-end-position) t))
  782. (setq comin (point))
  783. (goto-char comstart) (skip-chars-backward " \t")
  784. (setq has-code-and-comment (not (bolp)))))
  785. (if (not (and comstart
  786. ;; Make sure the comment-start mark we found is accepted by
  787. ;; comment-start-skip. If not, all bets are off, and
  788. ;; we'd better not mess with it.
  789. (string-match comment-start-skip
  790. (buffer-substring comstart comin))))
  791. ;; Return nil, so the normal filling will take place.
  792. nil
  793. ;; Narrow to include only the comment, and then fill the region.
  794. (let* ((fill-prefix fill-prefix)
  795. (commark
  796. (comment-string-strip (buffer-substring comstart comin) nil t))
  797. (comment-re
  798. ;; A regexp more specialized than comment-start-skip, that only
  799. ;; matches the current commark rather than any valid commark.
  800. ;;
  801. ;; The specialized regexp only works for "normal" comment
  802. ;; syntax, not for Texinfo's "@c" (which can't be immediately
  803. ;; followed by word-chars) or Fortran's "C" (which needs to be
  804. ;; at bol), so check that comment-start-skip indeed allows the
  805. ;; commark to appear in the middle of the line and followed by
  806. ;; word chars. The choice of "\0" and "a" is mostly arbitrary.
  807. (if (string-match comment-start-skip (concat "\0" commark "a"))
  808. (concat "[ \t]*" (regexp-quote commark)
  809. ;; Make sure we only match comments that
  810. ;; use the exact same comment marker.
  811. "[^" (substring commark -1) "]")
  812. (concat "[ \t]*\\(?:" comment-start-skip "\\)")))
  813. (comment-fill-prefix ; Compute a fill prefix.
  814. (save-excursion
  815. (goto-char comstart)
  816. (if has-code-and-comment
  817. (concat
  818. (if (not indent-tabs-mode)
  819. (make-string (current-column) ?\s)
  820. (concat
  821. (make-string (/ (current-column) tab-width) ?\t)
  822. (make-string (% (current-column) tab-width) ?\s)))
  823. (buffer-substring (point) comin))
  824. (buffer-substring (line-beginning-position) comin))))
  825. beg end)
  826. (save-excursion
  827. (save-restriction
  828. (beginning-of-line)
  829. (narrow-to-region
  830. ;; Find the first line we should include in the region to fill.
  831. (if has-code-and-comment
  832. (line-beginning-position)
  833. (save-excursion
  834. (while (and (zerop (forward-line -1))
  835. (looking-at comment-re)))
  836. ;; We may have gone too far. Go forward again.
  837. (line-beginning-position
  838. (if (progn
  839. (goto-char
  840. (or (comment-search-forward (line-end-position) t)
  841. (point)))
  842. (looking-at comment-re))
  843. (progn (setq comstart (point)) 1)
  844. (progn (setq comstart (point)) 2)))))
  845. ;; Find the beginning of the first line past the region to fill.
  846. (save-excursion
  847. (while (progn (forward-line 1)
  848. (looking-at comment-re)))
  849. (point)))
  850. ;; Obey paragraph starters and boundaries within comments.
  851. (let* ((paragraph-separate
  852. ;; Use the default values since they correspond to
  853. ;; the values to use for plain text.
  854. (concat paragraph-separate "\\|[ \t]*\\(?:"
  855. comment-start-skip "\\)\\(?:"
  856. (default-value 'paragraph-separate) "\\)"))
  857. (paragraph-start
  858. (concat paragraph-start "\\|[ \t]*\\(?:"
  859. comment-start-skip "\\)\\(?:"
  860. (default-value 'paragraph-start) "\\)"))
  861. ;; We used to rely on fill-prefix to break paragraph at
  862. ;; comment-starter changes, but it did not work for the
  863. ;; first line (mixed comment&code).
  864. ;; We now use comment-re instead to "manually" make sure
  865. ;; we treat comment-marker changes as paragraph boundaries.
  866. ;; (paragraph-ignore-fill-prefix nil)
  867. ;; (fill-prefix comment-fill-prefix)
  868. (after-line (if has-code-and-comment
  869. (line-beginning-position 2))))
  870. (setq end (progn (forward-paragraph) (point)))
  871. ;; If this comment starts on a line with code,
  872. ;; include that line in the filling.
  873. (setq beg (progn (backward-paragraph)
  874. (if (eq (point) after-line)
  875. (forward-line -1))
  876. (point)))))
  877. ;; Find the fill-prefix to use.
  878. (cond
  879. (fill-prefix) ; Use the user-provided fill prefix.
  880. ((and adaptive-fill-mode ; Try adaptive fill mode.
  881. (setq fill-prefix (fill-context-prefix beg end))
  882. (string-match comment-start-skip fill-prefix)))
  883. (t
  884. (setq fill-prefix comment-fill-prefix)))
  885. ;; Don't fill with narrowing.
  886. (or
  887. (fill-region-as-paragraph
  888. (max comstart beg) end justify nil
  889. ;; Don't canonicalize spaces within the code just before
  890. ;; the comment.
  891. (save-excursion
  892. (goto-char beg)
  893. (if (looking-at fill-prefix)
  894. nil
  895. (re-search-forward comment-start-skip))))
  896. ;; Make sure we don't return nil.
  897. t))))))
  898. (defun fill-region (from to &optional justify nosqueeze to-eop)
  899. "Fill each of the paragraphs in the region.
  900. A prefix arg means justify as well.
  901. The `fill-column' variable controls the width.
  902. Noninteractively, the third argument JUSTIFY specifies which
  903. kind of justification to do: `full', `left', `right', `center',
  904. or `none' (equivalent to nil). A value of t means handle each
  905. paragraph as specified by its text properties.
  906. The fourth arg NOSQUEEZE non-nil means to leave whitespace other
  907. than line breaks untouched, and fifth arg TO-EOP non-nil means
  908. to keep filling to the end of the paragraph (or next hard newline,
  909. if variable `use-hard-newlines' is on).
  910. Return the fill-prefix used for filling the last paragraph.
  911. If `sentence-end-double-space' is non-nil, then period followed by one
  912. space does not end a sentence, so don't break a line there."
  913. (interactive (progn
  914. (barf-if-buffer-read-only)
  915. (list (region-beginning) (region-end)
  916. (if current-prefix-arg 'full))))
  917. (unless (memq justify '(t nil none full center left right))
  918. (setq justify 'full))
  919. (let (max beg fill-pfx)
  920. (goto-char (max from to))
  921. (when to-eop
  922. (skip-chars-backward "\n")
  923. (fill-forward-paragraph 1))
  924. (setq max (copy-marker (point) t))
  925. (goto-char (setq beg (min from to)))
  926. (beginning-of-line)
  927. (while (< (point) max)
  928. (let ((initial (point))
  929. end)
  930. ;; If using hard newlines, break at every one for filling
  931. ;; purposes rather than using paragraph breaks.
  932. (if use-hard-newlines
  933. (progn
  934. (while (and (setq end (text-property-any (point) max
  935. 'hard t))
  936. (not (= ?\n (char-after end)))
  937. (not (>= end max)))
  938. (goto-char (1+ end)))
  939. (setq end (if end (min max (1+ end)) max))
  940. (goto-char initial))
  941. (fill-forward-paragraph 1)
  942. (setq end (min max (point)))
  943. (fill-forward-paragraph -1))
  944. (if (< (point) beg)
  945. (goto-char beg))
  946. (if (and (>= (point) initial) (< (point) end))
  947. (setq fill-pfx
  948. (fill-region-as-paragraph (point) end justify nosqueeze))
  949. (goto-char end))))
  950. fill-pfx))
  951. (defcustom default-justification 'left
  952. "Method of justifying text not otherwise specified.
  953. Possible values are `left', `right', `full', `center', or `none'.
  954. The requested kind of justification is done whenever lines are filled.
  955. The `justification' text-property can locally override this variable."
  956. :type '(choice (const left)
  957. (const right)
  958. (const full)
  959. (const center)
  960. (const none))
  961. :safe 'symbolp
  962. :group 'fill)
  963. (make-variable-buffer-local 'default-justification)
  964. (defun current-justification ()
  965. "How should we justify this line?
  966. This returns the value of the text-property `justification',
  967. or the variable `default-justification' if there is no text-property.
  968. However, it returns nil rather than `none' to mean \"don't justify\"."
  969. (let ((j (or (get-text-property
  970. ;; Make sure we're looking at paragraph body.
  971. (save-excursion (skip-chars-forward " \t")
  972. (if (and (eobp) (not (bobp)))
  973. (1- (point)) (point)))
  974. 'justification)
  975. default-justification)))
  976. (if (eq 'none j)
  977. nil
  978. j)))
  979. (defun set-justification (begin end style &optional whole-par)
  980. "Set the region's justification style to STYLE.
  981. This commands prompts for the kind of justification to use.
  982. If the mark is not active, this command operates on the current paragraph.
  983. If the mark is active, it operates on the region. However, if the
  984. beginning and end of the region are not at paragraph breaks, they are
  985. moved to the beginning and end \(respectively) of the paragraphs they
  986. are in.
  987. If variable `use-hard-newlines' is true, all hard newlines are
  988. taken to be paragraph breaks.
  989. When calling from a program, operates just on region between BEGIN and END,
  990. unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
  991. extended to include entire paragraphs as in the interactive command."
  992. (interactive (list (if mark-active (region-beginning) (point))
  993. (if mark-active (region-end) (point))
  994. (let ((s (completing-read
  995. "Set justification to: "
  996. '(("left") ("right") ("full")
  997. ("center") ("none"))
  998. nil t)))
  999. (if (equal s "") (error ""))
  1000. (intern s))
  1001. t))
  1002. (save-excursion
  1003. (save-restriction
  1004. (if whole-par
  1005. (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
  1006. (paragraph-ignore-fill-prefix (if use-hard-newlines t
  1007. paragraph-ignore-fill-prefix)))
  1008. (goto-char begin)
  1009. (while (and (bolp) (not (eobp))) (forward-char 1))
  1010. (backward-paragraph)
  1011. (setq begin (point))
  1012. (goto-char end)
  1013. (skip-chars-backward " \t\n" begin)
  1014. (forward-paragraph)
  1015. (setq end (point))))
  1016. (narrow-to-region (point-min) end)
  1017. (unjustify-region begin (point-max))
  1018. (put-text-property begin (point-max) 'justification style)
  1019. (fill-region begin (point-max) nil t))))
  1020. (defun set-justification-none (b e)
  1021. "Disable automatic filling for paragraphs in the region.
  1022. If the mark is not active, this applies to the current paragraph."
  1023. (interactive (list (if mark-active (region-beginning) (point))
  1024. (if mark-active (region-end) (point))))
  1025. (set-justification b e 'none t))
  1026. (defun set-justification-left (b e)
  1027. "Make paragraphs in the region left-justified.
  1028. This means they are flush at the left margin and ragged on the right.
  1029. This is usually the default, but see the variable `default-justification'.
  1030. If the mark is not active, this applies to the current paragraph."
  1031. (interactive (list (if mark-active (region-beginning) (point))
  1032. (if mark-active (region-end) (point))))
  1033. (set-justification b e 'left t))
  1034. (defun set-justification-right (b e)
  1035. "Make paragraphs in the region right-justified.
  1036. This means they are flush at the right margin and ragged on the left.
  1037. If the mark is not active, this applies to the current paragraph."
  1038. (interactive (list (if mark-active (region-beginning) (point))
  1039. (if mark-active (region-end) (point))))
  1040. (set-justification b e 'right t))
  1041. (defun set-justification-full (b e)
  1042. "Make paragraphs in the region fully justified.
  1043. This makes lines flush on both margins by inserting spaces between words.
  1044. If the mark is not active, this applies to the current paragraph."
  1045. (interactive (list (if mark-active (region-beginning) (point))
  1046. (if mark-active (region-end) (point))))
  1047. (set-justification b e 'full t))
  1048. (defun set-justification-center (b e)
  1049. "Make paragraphs in the region centered.
  1050. If the mark is not active, this applies to the current paragraph."
  1051. (interactive (list (if mark-active (region-beginning) (point))
  1052. (if mark-active (region-end) (point))))
  1053. (set-justification b e 'center t))
  1054. ;; A line has up to six parts:
  1055. ;;
  1056. ;; >>> hello.
  1057. ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
  1058. ;;
  1059. ;; "Indent-1" is the left-margin indentation; normally it ends at column
  1060. ;; given by the `current-left-margin' function.
  1061. ;; "FP" is the fill-prefix. It can be any string, including whitespace.
  1062. ;; "Indent-2" is added to justify a line if the `current-justification' is
  1063. ;; `center' or `right'. In `left' and `full' justification regions, any
  1064. ;; whitespace there is part of the line's text, and should not be changed.
  1065. ;; Trailing whitespace is not counted as part of the line length when
  1066. ;; center- or right-justifying.
  1067. ;;
  1068. ;; All parts of the line are optional, although the final newline can
  1069. ;; only be missing on the last line of the buffer.
  1070. (defun justify-current-line (&optional how eop nosqueeze)
  1071. "Do some kind of justification on this line.
  1072. Normally does full justification: adds spaces to the line to make it end at
  1073. the column given by `current-fill-column'.
  1074. Optional first argument HOW specifies alternate type of justification:
  1075. it can be `left', `right', `full', `center', or `none'.
  1076. If HOW is t, will justify however the `current-justification' function says to.
  1077. If HOW is nil or missing, full justification is done by default.
  1078. Second arg EOP non-nil means that this is the last line of the paragraph, so
  1079. it will not be stretched by full justification.
  1080. Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
  1081. otherwise it is made canonical."
  1082. (interactive "*")
  1083. (if (eq t how) (setq how (or (current-justification) 'none))
  1084. (if (null how) (setq how 'full)
  1085. (or (memq how '(none left right center))
  1086. (setq how 'full))))
  1087. (or (memq how '(none left)) ; No action required for these.
  1088. (let ((fc (current-fill-column))
  1089. (pos (point-marker))
  1090. fp-end ; point at end of fill prefix
  1091. beg ; point at beginning of line's text
  1092. end ; point at end of line's text
  1093. indent ; column of `beg'
  1094. endcol ; column of `end'
  1095. ncols ; new indent point or offset
  1096. (nspaces 0) ; number of spaces between words
  1097. ; in line (not space characters)
  1098. (curr-fracspace 0) ; current fractional space amount
  1099. count)
  1100. (end-of-line)
  1101. ;; Check if this is the last line of the paragraph.
  1102. (if (and use-hard-newlines (null eop)
  1103. (get-text-property (point) 'hard))
  1104. (setq eop t))
  1105. (skip-chars-backward " \t")
  1106. ;; Quick exit if it appears to be properly justified already
  1107. ;; or there is no text.
  1108. (if (or (bolp)
  1109. (and (memq how '(full right))
  1110. (= (current-column) fc)))
  1111. nil
  1112. (setq end (point))
  1113. (beginning-of-line)
  1114. (skip-chars-forward " \t")
  1115. ;; Skip over fill-prefix.
  1116. (if (and fill-prefix
  1117. (not (string-equal fill-prefix ""))
  1118. (equal fill-prefix
  1119. (buffer-substring
  1120. (point) (min (point-max) (+ (length fill-prefix)
  1121. (point))))))
  1122. (forward-char (length fill-prefix))
  1123. (if (and adaptive-fill-mode
  1124. (looking-at adaptive-fill-regexp))
  1125. (goto-char (match-end 0))))
  1126. (setq fp-end (point))
  1127. (skip-chars-forward " \t")
  1128. ;; This is beginning of the line's text.
  1129. (setq indent (current-column))
  1130. (setq beg (point))
  1131. (goto-char end)
  1132. (setq endcol (current-column))
  1133. ;; HOW can't be null or left--we would have exited already
  1134. (cond ((eq 'right how)
  1135. (setq ncols (- fc endcol))
  1136. (if (< ncols 0)
  1137. ;; Need to remove some indentation
  1138. (delete-region
  1139. (progn (goto-char fp-end)
  1140. (if (< (current-column) (+ indent ncols))
  1141. (move-to-column (+ indent ncols) t))
  1142. (point))
  1143. (progn (move-to-column indent) (point)))
  1144. ;; Need to add some
  1145. (goto-char beg)
  1146. (indent-to (+ indent ncols))
  1147. ;; If point was at beginning of text, keep it there.
  1148. (if (= beg pos)
  1149. (move-marker pos (point)))))
  1150. ((eq 'center how)
  1151. ;; Figure out how much indentation is needed
  1152. (setq ncols (+ (current-left-margin)
  1153. (/ (- fc (current-left-margin) ;avail. space
  1154. (- endcol indent)) ;text width
  1155. 2)))
  1156. (if (< ncols indent)
  1157. ;; Have too much indentation - remove some
  1158. (delete-region
  1159. (progn (goto-char fp-end)
  1160. (if (< (current-column) ncols)
  1161. (move-to-column ncols t))
  1162. (point))
  1163. (progn (move-to-column indent) (point)))
  1164. ;; Have too little - add some
  1165. (goto-char beg)
  1166. (indent-to ncols)
  1167. ;; If point was at beginning of text, keep it there.
  1168. (if (= beg pos)
  1169. (move-marker pos (point)))))
  1170. ((eq 'full how)
  1171. ;; Insert extra spaces between words to justify line
  1172. (save-restriction
  1173. (narrow-to-region beg end)
  1174. (or nosqueeze
  1175. (canonically-space-region beg end))
  1176. (goto-char (point-max))
  1177. ;; count word spaces in line
  1178. (while (search-backward " " nil t)
  1179. (setq nspaces (1+ nspaces))
  1180. (skip-chars-backward " "))
  1181. (setq ncols (- fc endcol))
  1182. ;; Ncols is number of additional space chars needed
  1183. (when (and (> ncols 0) (> nspaces 0) (not eop))
  1184. (setq curr-fracspace (+ ncols (/ nspaces 2))
  1185. count nspaces)
  1186. (while (> count 0)
  1187. (skip-chars-forward " ")
  1188. (insert-char ?\s (/ curr-fracspace nspaces) t)
  1189. (search-forward " " nil t)
  1190. (setq count (1- count)
  1191. curr-fracspace
  1192. (+ (% curr-fracspace nspaces) ncols))))))
  1193. (t (error "Unknown justification value"))))
  1194. (goto-char pos)
  1195. (move-marker pos nil)))
  1196. nil)
  1197. (defun unjustify-current-line ()
  1198. "Remove justification whitespace from current line.
  1199. If the line is centered or right-justified, this function removes any
  1200. indentation past the left margin. If the line is full-justified, it removes
  1201. extra spaces between words. It does nothing in other justification modes."
  1202. (let ((justify (current-justification)))
  1203. (cond ((eq 'left justify) nil)
  1204. ((eq nil justify) nil)
  1205. ((eq 'full justify) ; full justify: remove extra spaces
  1206. (beginning-of-line-text)
  1207. (canonically-space-region (point) (line-end-position)))
  1208. ((memq justify '(center right))
  1209. (save-excursion
  1210. (move-to-left-margin nil t)
  1211. ;; Position ourselves after any fill-prefix.
  1212. (if (and fill-prefix
  1213. (not (string-equal fill-prefix ""))
  1214. (equal fill-prefix
  1215. (buffer-substring
  1216. (point) (min (point-max) (+ (length fill-prefix)
  1217. (point))))))
  1218. (forward-char (length fill-prefix)))
  1219. (delete-region (point) (progn (skip-chars-forward " \t")
  1220. (point))))))))
  1221. (defun unjustify-region (&optional begin end)
  1222. "Remove justification whitespace from region.
  1223. For centered or right-justified regions, this function removes any indentation
  1224. past the left margin from each line. For full-justified lines, it removes
  1225. extra spaces between words. It does nothing in other justification modes.
  1226. Arguments BEGIN and END are optional; default is the whole buffer."
  1227. (save-excursion
  1228. (save-restriction
  1229. (if end (narrow-to-region (point-min) end))
  1230. (goto-char (or begin (point-min)))
  1231. (while (not (eobp))
  1232. (unjustify-current-line)
  1233. (forward-line 1)))))
  1234. (defun fill-nonuniform-paragraphs (min max &optional justifyp citation-regexp)
  1235. "Fill paragraphs within the region, allowing varying indentation within each.
  1236. This command divides the region into \"paragraphs\",
  1237. only at paragraph-separator lines, then fills each paragraph
  1238. using as the fill prefix the smallest indentation of any line
  1239. in the paragraph.
  1240. When calling from a program, pass range to fill as first two arguments.
  1241. Optional third and fourth arguments JUSTIFYP and CITATION-REGEXP:
  1242. JUSTIFYP to justify paragraphs (prefix arg).
  1243. When filling a mail message, pass a regexp for CITATION-REGEXP
  1244. which will match the prefix of a line which is a citation marker
  1245. plus whitespace, but no other kind of prefix.
  1246. Also, if CITATION-REGEXP is non-nil, don't fill header lines."
  1247. (interactive (progn
  1248. (barf-if-buffer-read-only)
  1249. (list (region-beginning) (region-end)
  1250. (if current-prefix-arg 'full))))
  1251. (let ((fill-individual-varying-indent t))
  1252. (fill-individual-paragraphs min max justifyp citation-regexp)))
  1253. (defun fill-individual-paragraphs (min max &optional justify citation-regexp)
  1254. "Fill paragraphs of uniform indentation within the region.
  1255. This command divides the region into \"paragraphs\",
  1256. treating every change in indentation level or prefix as a paragraph boundary,
  1257. then fills each paragraph using its indentation level as the fill prefix.
  1258. There is one special case where a change in indentation does not start
  1259. a new paragraph. This is for text of this form:
  1260. foo> This line with extra indentation starts
  1261. foo> a paragraph that continues on more lines.
  1262. These lines are filled together.
  1263. When calling from a program, pass the range to fill
  1264. as the first two arguments.
  1265. Optional third and fourth arguments JUSTIFY and CITATION-REGEXP:
  1266. JUSTIFY to justify paragraphs (prefix arg).
  1267. When filling a mail message, pass a regexp for CITATION-REGEXP
  1268. which will match the prefix of a line which is a citation marker
  1269. plus whitespace, but no other kind of prefix.
  1270. Also, if CITATION-REGEXP is non-nil, don't fill header lines."
  1271. (interactive (progn
  1272. (barf-if-buffer-read-only)
  1273. (list (region-beginning) (region-end)
  1274. (if current-prefix-arg 'full))))
  1275. (save-restriction
  1276. (save-excursion
  1277. (goto-char min)
  1278. (beginning-of-line)
  1279. (narrow-to-region (point) max)
  1280. (if citation-regexp
  1281. (while (and (not (eobp))
  1282. (or (looking-at "[ \t]*[^ \t\n]+:")
  1283. (looking-at "[ \t]*$")))
  1284. (if (looking-at "[ \t]*[^ \t\n]+:")
  1285. (search-forward "\n\n" nil 'move)
  1286. (forward-line 1))))
  1287. (narrow-to-region (point) max)
  1288. ;; Loop over paragraphs.
  1289. (while (progn
  1290. ;; Skip over all paragraph-separating lines
  1291. ;; so as to not include them in any paragraph.
  1292. (while (and (not (eobp))
  1293. (progn (move-to-left-margin)
  1294. (and (not (eobp))
  1295. (looking-at paragraph-separate))))
  1296. (forward-line 1))
  1297. (skip-chars-forward " \t\n") (not (eobp)))
  1298. (move-to-left-margin)
  1299. (let ((start (point))
  1300. fill-prefix fill-prefix-regexp)
  1301. ;; Find end of paragraph, and compute the smallest fill-prefix
  1302. ;; that fits all the lines in this paragraph.
  1303. (while (progn
  1304. ;; Update the fill-prefix on the first line
  1305. ;; and whenever the prefix good so far is too long.
  1306. (if (not (and fill-prefix
  1307. (looking-at fill-prefix-regexp)))
  1308. (setq fill-prefix
  1309. (fill-individual-paragraphs-prefix
  1310. citation-regexp)
  1311. fill-prefix-regexp (regexp-quote fill-prefix)))
  1312. (forward-line 1)
  1313. (if (bolp)
  1314. ;; If forward-line went past a newline,
  1315. ;; move further to the left margin.
  1316. (move-to-left-margin))
  1317. ;; Now stop the loop if end of paragraph.
  1318. (and (not (eobp))
  1319. (if fill-individual-varying-indent
  1320. ;; If this line is a separator line, with or
  1321. ;; without prefix, end the paragraph.
  1322. (and
  1323. (not (looking-at paragraph-separate))
  1324. (save-excursion
  1325. (not (and (looking-at fill-prefix-regexp)
  1326. (progn (forward-char
  1327. (length fill-prefix))
  1328. (looking-at
  1329. paragraph-separate))))))
  1330. ;; If this line has more or less indent
  1331. ;; than the fill prefix wants, end the paragraph.
  1332. (and (looking-at fill-prefix-regexp)
  1333. ;; If fill prefix is shorter than a new
  1334. ;; fill prefix computed here, end paragraph.
  1335. (let ((this-line-fill-prefix
  1336. (fill-individual-paragraphs-prefix
  1337. citation-regexp)))
  1338. (>= (length fill-prefix)
  1339. (length this-line-fill-prefix)))
  1340. (save-excursion
  1341. (not (progn (forward-char
  1342. (length fill-prefix))
  1343. (or (looking-at "[ \t]")
  1344. (looking-at paragraph-separate)
  1345. (looking-at paragraph-start)))))
  1346. (not (and (equal fill-prefix "")
  1347. citation-regexp
  1348. (looking-at citation-regexp))))))))
  1349. ;; Fill this paragraph, but don't add a newline at the end.
  1350. (let ((had-newline (bolp)))
  1351. (fill-region-as-paragraph start (point) justify)
  1352. (if (and (bolp) (not had-newline))
  1353. (delete-char -1))))))))
  1354. (defun fill-individual-paragraphs-prefix (citation-regexp)
  1355. (let* ((adaptive-fill-first-line-regexp ".*")
  1356. (just-one-line-prefix
  1357. ;; Accept any prefix rather than just the ones matched by
  1358. ;; adaptive-fill-first-line-regexp.
  1359. (fill-context-prefix (point) (line-beginning-position 2)))
  1360. (two-lines-prefix
  1361. (fill-context-prefix (point) (line-beginning-position 3))))
  1362. (if (not just-one-line-prefix)
  1363. (buffer-substring
  1364. (point) (save-excursion (skip-chars-forward " \t") (point)))
  1365. ;; See if the citation part of JUST-ONE-LINE-PREFIX
  1366. ;; is the same as that of TWO-LINES-PREFIX,
  1367. ;; except perhaps with longer whitespace.
  1368. (if (and just-one-line-prefix two-lines-prefix
  1369. (let* ((one-line-citation-part
  1370. (fill-individual-paragraphs-citation
  1371. just-one-line-prefix citation-regexp))
  1372. (two-lines-citation-part
  1373. (fill-individual-paragraphs-citation
  1374. two-lines-prefix citation-regexp))
  1375. (adjusted-two-lines-citation-part
  1376. (substring two-lines-citation-part 0
  1377. (string-match "[ \t]*\\'"
  1378. two-lines-citation-part))))
  1379. (and
  1380. (string-match (concat "\\`"
  1381. (regexp-quote
  1382. adjusted-two-lines-citation-part)
  1383. "[ \t]*\\'")
  1384. one-line-citation-part)
  1385. (>= (string-width one-line-citation-part)
  1386. (string-width two-lines-citation-part)))))
  1387. two-lines-prefix
  1388. just-one-line-prefix))))
  1389. (defun fill-individual-paragraphs-citation (string citation-regexp)
  1390. (if citation-regexp
  1391. (if (string-match citation-regexp string)
  1392. (match-string 0 string)
  1393. "")
  1394. string))
  1395. ;;; fill.el ends here