paragraphs.el 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. ;;; paragraphs.el --- paragraph and sentence parsing
  2. ;; Copyright (C) 1985-1987, 1991, 1994-1997, 1999-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. ;; This package provides the paragraph-oriented commands documented in the
  20. ;; Emacs manual.
  21. ;;; Code:
  22. (defgroup paragraphs nil
  23. "Paragraph and sentence parsing."
  24. :group 'editing)
  25. (put 'use-hard-newlines 'permanent-local t)
  26. (define-minor-mode use-hard-newlines
  27. "Toggle distinguishing between hard and soft newlines.
  28. With a prefix argument ARG, enable the feature if ARG is
  29. positive, and disable it otherwise. If called from Lisp, enable
  30. it if ARG is omitted or nil.
  31. When enabled, the functions `newline' and `open-line' add the
  32. text-property `hard' to newlines that they insert, and a line is
  33. only considered as a candidate to match `paragraph-start' or
  34. `paragraph-separate' if it follows a hard newline.
  35. When enabling, if there are newlines in the buffer but no hard
  36. newlines, ask the user whether to mark as hard any newlines
  37. preceding a `paragraph-start' line. From a program, second arg
  38. INSERT specifies whether to do this; it can be `never' to change
  39. nothing, t or `always' to force marking, `guess' to try to do the
  40. right thing with no questions, nil or anything else to ask the
  41. user.
  42. Newlines not marked hard are called \"soft\", and are always internal
  43. to paragraphs. The fill functions insert and delete only soft newlines."
  44. :group 'paragraphs
  45. :extra-args (insert)
  46. (when use-hard-newlines
  47. ;; Turn mode on
  48. ;; Intuit hard newlines --
  49. ;; mark as hard any newlines preceding a paragraph-start line.
  50. (if (or (eq insert t) (eq insert 'always)
  51. (and (not (eq 'never insert))
  52. (not (text-property-any (point-min) (point-max) 'hard t))
  53. (save-excursion
  54. (goto-char (point-min))
  55. (search-forward "\n" nil t))
  56. (or (eq insert 'guess)
  57. (y-or-n-p "Make newlines between paragraphs hard? "))))
  58. (save-excursion
  59. (goto-char (point-min))
  60. (while (search-forward "\n" nil t)
  61. (let ((pos (point)))
  62. (move-to-left-margin)
  63. (when (looking-at paragraph-start)
  64. (set-hard-newline-properties (1- pos) pos))
  65. ;; If paragraph-separate, newline after it is hard too.
  66. (when (looking-at paragraph-separate)
  67. (set-hard-newline-properties (1- pos) pos)
  68. (end-of-line)
  69. (unless (eobp)
  70. (set-hard-newline-properties (point) (1+ (point)))))))))))
  71. (defcustom paragraph-start "\f\\|[ \t]*$" "\
  72. Regexp for beginning of a line that starts OR separates paragraphs.
  73. This regexp should match lines that separate paragraphs
  74. and should also match lines that start a paragraph
  75. \(and are part of that paragraph).
  76. This is matched against the text at the left margin, which is not necessarily
  77. the beginning of the line, so it should never use \"^\" as an anchor. This
  78. ensures that the paragraph functions will work equally well within a region
  79. of text indented by a margin setting.
  80. The variable `paragraph-separate' specifies how to distinguish
  81. lines that start paragraphs from lines that separate them.
  82. If the variable `use-hard-newlines' is non-nil, then only lines following a
  83. hard newline are considered to match."
  84. :group 'paragraphs
  85. :type 'regexp)
  86. (put 'paragraph-start 'safe-local-variable 'stringp)
  87. ;; paragraph-start requires a hard newline, but paragraph-separate does not:
  88. ;; It is assumed that paragraph-separate is distinctive enough to be believed
  89. ;; whenever it occurs, while it is reasonable to set paragraph-start to
  90. ;; something very minimal, even including "." (which makes every hard newline
  91. ;; start a new paragraph).
  92. (defcustom paragraph-separate "[ \t\f]*$"
  93. "Regexp for beginning of a line that separates paragraphs.
  94. If you change this, you may have to change `paragraph-start' also.
  95. This is matched against the text at the left margin, which is not necessarily
  96. the beginning of the line, so it should not use \"^\" as an anchor. This
  97. ensures that the paragraph functions will work equally within a region of
  98. text indented by a margin setting."
  99. :group 'paragraphs
  100. :type 'regexp)
  101. (put 'paragraph-separate 'safe-local-variable 'stringp)
  102. (defcustom sentence-end-double-space t
  103. "Non-nil means a single space does not end a sentence.
  104. This is relevant for filling. See also `sentence-end-without-period'
  105. and `colon-double-space'.
  106. This value is used by the function `sentence-end' to construct the
  107. regexp describing the end of a sentence, when the value of the variable
  108. `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
  109. :type 'boolean
  110. :group 'fill)
  111. (put 'sentence-end-double-space 'safe-local-variable 'booleanp)
  112. (defcustom sentence-end-without-period nil
  113. "Non-nil means a sentence will end without a period.
  114. For example, a sentence in Thai text ends with double space but
  115. without a period.
  116. This value is used by the function `sentence-end' to construct the
  117. regexp describing the end of a sentence, when the value of the variable
  118. `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
  119. :type 'boolean
  120. :group 'fill)
  121. (put 'sentence-end-without-period 'safe-local-variable 'booleanp)
  122. (defcustom sentence-end-without-space
  123. "。.?!"
  124. "String of characters that end sentence without following spaces.
  125. This value is used by the function `sentence-end' to construct the
  126. regexp describing the end of a sentence, when the value of the variable
  127. `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
  128. :group 'paragraphs
  129. :type 'string)
  130. (put 'sentence-end-without-space 'safe-local-variable 'stringp)
  131. (defcustom sentence-end nil
  132. "Regexp describing the end of a sentence.
  133. The value includes the whitespace following the sentence.
  134. All paragraph boundaries also end sentences, regardless.
  135. The value nil means to use the default value defined by the
  136. function `sentence-end'. You should always use this function
  137. to obtain the value of this variable."
  138. :group 'paragraphs
  139. :type '(choice regexp (const :tag "Use default value" nil)))
  140. (put 'sentence-end 'safe-local-variable 'string-or-null-p)
  141. (defcustom sentence-end-base "[.?!][]\"'”)}]*"
  142. "Regexp matching the basic end of a sentence, not including following space."
  143. :group 'paragraphs
  144. :type 'string
  145. :version "22.1")
  146. (put 'sentence-end-base 'safe-local-variable 'stringp)
  147. (defun sentence-end ()
  148. "Return the regexp describing the end of a sentence.
  149. This function returns either the value of the variable `sentence-end'
  150. if it is non-nil, or the default value constructed from the
  151. variables `sentence-end-base', `sentence-end-double-space',
  152. `sentence-end-without-period' and `sentence-end-without-space'.
  153. The default value specifies that in order to be recognized as the
  154. end of a sentence, the ending period, question mark, or exclamation point
  155. must be followed by two spaces, with perhaps some closing delimiters
  156. in between. See Info node `(elisp)Standard Regexps'."
  157. (or sentence-end
  158. ;; We accept non-break space along with space.
  159. (concat (if sentence-end-without-period "\\w[ \u00a0][ \u00a0]\\|")
  160. "\\("
  161. sentence-end-base
  162. (if sentence-end-double-space
  163. "\\($\\|[ \u00a0]$\\|\t\\|[ \u00a0][ \u00a0]\\)" "\\($\\|[\t \u00a0]\\)")
  164. "\\|[" sentence-end-without-space "]+"
  165. "\\)"
  166. "[ \u00a0\t\n]*")))
  167. (defcustom page-delimiter "^\014"
  168. "Regexp describing line-beginnings that separate pages."
  169. :group 'paragraphs
  170. :type 'regexp)
  171. (put 'page-delimiter 'safe-local-variable 'stringp)
  172. (defcustom paragraph-ignore-fill-prefix nil
  173. "Non-nil means the paragraph commands are not affected by `fill-prefix'.
  174. This is desirable in modes where blank lines are the paragraph delimiters."
  175. :group 'paragraphs
  176. :type 'boolean)
  177. (put 'paragraph-ignore-fill-prefix 'safe-local-variable 'booleanp)
  178. (defun forward-paragraph (&optional arg)
  179. "Move forward to end of paragraph.
  180. With argument ARG, do it ARG times;
  181. a negative argument ARG = -N means move backward N paragraphs.
  182. A line which `paragraph-start' matches either separates paragraphs
  183. \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
  184. A paragraph end is the beginning of a line which is not part of the paragraph
  185. to which the end of the previous line belongs, or the end of the buffer.
  186. Returns the count of paragraphs left to move."
  187. (interactive "^p")
  188. (or arg (setq arg 1))
  189. (let* ((opoint (point))
  190. (fill-prefix-regexp
  191. (and fill-prefix (not (equal fill-prefix ""))
  192. (not paragraph-ignore-fill-prefix)
  193. (regexp-quote fill-prefix)))
  194. ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
  195. ;; These regexps shouldn't be anchored, because we look for them
  196. ;; starting at the left-margin. This allows paragraph commands to
  197. ;; work normally with indented text.
  198. ;; This hack will not find problem cases like "whatever\\|^something".
  199. (parstart (if (and (not (equal "" paragraph-start))
  200. (equal ?^ (aref paragraph-start 0)))
  201. (substring paragraph-start 1)
  202. paragraph-start))
  203. (parsep (if (and (not (equal "" paragraph-separate))
  204. (equal ?^ (aref paragraph-separate 0)))
  205. (substring paragraph-separate 1)
  206. paragraph-separate))
  207. (parsep
  208. (if fill-prefix-regexp
  209. (concat parsep "\\|"
  210. fill-prefix-regexp "[ \t]*$")
  211. parsep))
  212. ;; This is used for searching.
  213. (sp-parstart (concat "^[ \t]*\\(?:" parstart "\\|" parsep "\\)"))
  214. start found-start)
  215. (while (and (< arg 0) (not (bobp)))
  216. (if (and (not (looking-at parsep))
  217. (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
  218. (looking-at parsep))
  219. (setq arg (1+ arg))
  220. (setq start (point))
  221. ;; Move back over paragraph-separating lines.
  222. (forward-char -1) (beginning-of-line)
  223. (while (and (not (bobp))
  224. (progn (move-to-left-margin)
  225. (looking-at parsep)))
  226. (forward-line -1))
  227. (if (bobp)
  228. nil
  229. (setq arg (1+ arg))
  230. ;; Go to end of the previous (non-separating) line.
  231. (end-of-line)
  232. ;; Search back for line that starts or separates paragraphs.
  233. (if (if fill-prefix-regexp
  234. ;; There is a fill prefix; it overrides parstart.
  235. (let (multiple-lines)
  236. (while (and (progn (beginning-of-line) (not (bobp)))
  237. (progn (move-to-left-margin)
  238. (not (looking-at parsep)))
  239. (looking-at fill-prefix-regexp))
  240. (unless (= (point) start)
  241. (setq multiple-lines t))
  242. (forward-line -1))
  243. (move-to-left-margin)
  244. ;; This deleted code caused a long hanging-indent line
  245. ;; not to be filled together with the following lines.
  246. ;; ;; Don't move back over a line before the paragraph
  247. ;; ;; which doesn't start with fill-prefix
  248. ;; ;; unless that is the only line we've moved over.
  249. ;; (and (not (looking-at fill-prefix-regexp))
  250. ;; multiple-lines
  251. ;; (forward-line 1))
  252. (not (bobp)))
  253. (while (and (re-search-backward sp-parstart nil 1)
  254. (setq found-start t)
  255. ;; Found a candidate, but need to check if it is a
  256. ;; REAL parstart.
  257. (progn (setq start (point))
  258. (move-to-left-margin)
  259. (not (looking-at parsep)))
  260. (not (and (looking-at parstart)
  261. (or (not use-hard-newlines)
  262. (bobp)
  263. (get-text-property
  264. (1- start) 'hard)))))
  265. (setq found-start nil)
  266. (goto-char start))
  267. found-start)
  268. ;; Found one.
  269. (progn
  270. ;; Move forward over paragraph separators.
  271. ;; We know this cannot reach the place we started
  272. ;; because we know we moved back over a non-separator.
  273. (while (and (not (eobp))
  274. (progn (move-to-left-margin)
  275. (looking-at parsep)))
  276. (forward-line 1))
  277. ;; If line before paragraph is just margin, back up to there.
  278. (end-of-line 0)
  279. (if (> (current-column) (current-left-margin))
  280. (forward-char 1)
  281. (skip-chars-backward " \t")
  282. (if (not (bolp))
  283. (forward-line 1))))
  284. ;; No starter or separator line => use buffer beg.
  285. (goto-char (point-min))))))
  286. (while (and (> arg 0) (not (eobp)))
  287. ;; Move forward over separator lines...
  288. (while (and (not (eobp))
  289. (progn (move-to-left-margin) (not (eobp)))
  290. (looking-at parsep))
  291. (forward-line 1))
  292. (unless (eobp) (setq arg (1- arg)))
  293. ;; ... and one more line.
  294. (forward-line 1)
  295. (if fill-prefix-regexp
  296. ;; There is a fill prefix; it overrides parstart.
  297. (while (and (not (eobp))
  298. (progn (move-to-left-margin) (not (eobp)))
  299. (not (looking-at parsep))
  300. (looking-at fill-prefix-regexp))
  301. (forward-line 1))
  302. (while (and (re-search-forward sp-parstart nil 1)
  303. (progn (setq start (match-beginning 0))
  304. (goto-char start)
  305. (not (eobp)))
  306. (progn (move-to-left-margin)
  307. (not (looking-at parsep)))
  308. (or (not (looking-at parstart))
  309. (and use-hard-newlines
  310. (not (get-text-property (1- start) 'hard)))))
  311. (forward-char 1))
  312. (if (< (point) (point-max))
  313. (goto-char start))))
  314. (constrain-to-field nil opoint t)
  315. ;; Return the number of steps that could not be done.
  316. arg))
  317. (defun backward-paragraph (&optional arg)
  318. "Move backward to start of paragraph.
  319. With argument ARG, do it ARG times;
  320. a negative argument ARG = -N means move forward N paragraphs.
  321. A paragraph start is the beginning of a line which is a
  322. `paragraph-start' or which is ordinary text and follows a
  323. `paragraph-separate'ing line; except: if the first real line of a
  324. paragraph is preceded by a blank line, the paragraph starts at that
  325. blank line.
  326. See `forward-paragraph' for more information."
  327. (interactive "^p")
  328. (or arg (setq arg 1))
  329. (forward-paragraph (- arg)))
  330. (defun mark-paragraph (&optional arg allow-extend)
  331. "Put point at beginning of this paragraph, mark at end.
  332. The paragraph marked is the one that contains point or follows point.
  333. With argument ARG, puts mark at end of a following paragraph, so that
  334. the number of paragraphs marked equals ARG.
  335. If ARG is negative, point is put at end of this paragraph, mark is put
  336. at beginning of this or a previous paragraph.
  337. Interactively, if this command is repeated
  338. or (in Transient Mark mode) if the mark is active,
  339. it marks the next ARG paragraphs after the ones already marked."
  340. (interactive "p\np")
  341. (unless arg (setq arg 1))
  342. (when (zerop arg)
  343. (error "Cannot mark zero paragraphs"))
  344. (cond ((and allow-extend
  345. (or (and (eq last-command this-command) (mark t))
  346. (and transient-mark-mode mark-active)))
  347. (set-mark
  348. (save-excursion
  349. (goto-char (mark))
  350. (forward-paragraph arg)
  351. (point))))
  352. (t
  353. (forward-paragraph arg)
  354. (push-mark nil t t)
  355. (backward-paragraph arg))))
  356. (defun kill-paragraph (arg)
  357. "Kill forward to end of paragraph.
  358. With arg N, kill forward to Nth end of paragraph;
  359. negative arg -N means kill backward to Nth start of paragraph."
  360. (interactive "p")
  361. (kill-region (point) (progn (forward-paragraph arg) (point))))
  362. (defun backward-kill-paragraph (arg)
  363. "Kill back to start of paragraph.
  364. With arg N, kill back to Nth start of paragraph;
  365. negative arg -N means kill forward to Nth end of paragraph."
  366. (interactive "p")
  367. (kill-region (point) (progn (backward-paragraph arg) (point))))
  368. (defun transpose-paragraphs (arg)
  369. "Interchange the current paragraph with the next one.
  370. With prefix argument ARG a non-zero integer, moves the current
  371. paragraph past ARG paragraphs, leaving point after the current paragraph.
  372. If ARG is positive, moves the current paragraph forwards, if
  373. ARG is negative moves it backwards. If ARG is zero, exchanges
  374. the current paragraph with the one containing the mark."
  375. (interactive "*p")
  376. (transpose-subr 'forward-paragraph arg))
  377. (defun start-of-paragraph-text ()
  378. (let ((opoint (point)) npoint)
  379. (forward-paragraph -1)
  380. (setq npoint (point))
  381. (skip-chars-forward " \t\n")
  382. ;; If the range of blank lines found spans the original start point,
  383. ;; try again from the beginning of it.
  384. ;; Must be careful to avoid infinite loop
  385. ;; when following a single return at start of buffer.
  386. (if (and (>= (point) opoint) (< npoint opoint))
  387. (progn
  388. (goto-char npoint)
  389. (if (> npoint (point-min))
  390. (start-of-paragraph-text))))))
  391. (defun end-of-paragraph-text ()
  392. (let ((opoint (point)))
  393. (forward-paragraph 1)
  394. (if (eq (preceding-char) ?\n) (forward-char -1))
  395. (if (<= (point) opoint)
  396. (progn
  397. (forward-char 1)
  398. (if (< (point) (point-max))
  399. (end-of-paragraph-text))))))
  400. (defun forward-sentence (&optional arg)
  401. "Move forward to next end of sentence. With argument, repeat.
  402. With negative argument, move backward repeatedly to start of sentence.
  403. The variable `sentence-end' is a regular expression that matches ends of
  404. sentences. Also, every paragraph boundary terminates sentences as well."
  405. (interactive "^p")
  406. (or arg (setq arg 1))
  407. (let ((opoint (point))
  408. (sentence-end (sentence-end)))
  409. (while (< arg 0)
  410. (let ((pos (point))
  411. par-beg par-text-beg)
  412. (save-excursion
  413. (start-of-paragraph-text)
  414. ;; Start of real text in the paragraph.
  415. ;; We move back to here if we don't see a sentence-end.
  416. (setq par-text-beg (point))
  417. ;; Start of the first line of the paragraph.
  418. ;; We use this as the search limit
  419. ;; to allow s1entence-end to match if it is anchored at
  420. ;; BOL and the paragraph starts indented.
  421. (beginning-of-line)
  422. (setq par-beg (point)))
  423. (if (and (re-search-backward sentence-end par-beg t)
  424. (or (< (match-end 0) pos)
  425. (re-search-backward sentence-end par-beg t)))
  426. (goto-char (match-end 0))
  427. (goto-char par-text-beg)))
  428. (setq arg (1+ arg)))
  429. (while (> arg 0)
  430. (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
  431. (if (re-search-forward sentence-end par-end t)
  432. (skip-chars-backward " \t\n")
  433. (goto-char par-end)))
  434. (setq arg (1- arg)))
  435. (constrain-to-field nil opoint t)))
  436. (defun repunctuate-sentences ()
  437. "Put two spaces at the end of sentences from point to the end of buffer.
  438. It works using `query-replace-regexp'."
  439. (interactive)
  440. (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
  441. "\\1\\2\\3 "))
  442. (defun backward-sentence (&optional arg)
  443. "Move backward to start of sentence. With arg, do it arg times.
  444. See `forward-sentence' for more information."
  445. (interactive "^p")
  446. (or arg (setq arg 1))
  447. (forward-sentence (- arg)))
  448. (defun kill-sentence (&optional arg)
  449. "Kill from point to end of sentence.
  450. With arg, repeat; negative arg -N means kill back to Nth start of sentence."
  451. (interactive "p")
  452. (kill-region (point) (progn (forward-sentence arg) (point))))
  453. (defun backward-kill-sentence (&optional arg)
  454. "Kill back from point to start of sentence.
  455. With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
  456. (interactive "p")
  457. (kill-region (point) (progn (backward-sentence arg) (point))))
  458. (defun mark-end-of-sentence (arg)
  459. "Put mark at end of sentence. Arg works as in `forward-sentence'.
  460. If this command is repeated, it marks the next ARG sentences after the
  461. ones already marked."
  462. (interactive "p")
  463. (push-mark
  464. (save-excursion
  465. (if (and (eq last-command this-command) (mark t))
  466. (goto-char (mark)))
  467. (forward-sentence arg)
  468. (point))
  469. nil t))
  470. (defun transpose-sentences (arg)
  471. "Interchange the current sentence with the next one.
  472. With prefix argument ARG a non-zero integer, moves the current
  473. sentence past ARG sentences, leaving point after the current sentence.
  474. If ARG is positive, moves the current sentence forwards, if
  475. ARG is negative moves it backwards. If ARG is zero, exchanges
  476. the current sentence with the one containing the mark."
  477. (interactive "*p")
  478. (transpose-subr 'forward-sentence arg))
  479. ;; Local Variables:
  480. ;; coding: utf-8
  481. ;; End:
  482. ;;; paragraphs.el ends here