longlines.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. ;;; longlines.el --- automatically wrap long lines -*- coding:utf-8 -*-
  2. ;; Copyright (C) 2000-2001, 2004-2012 Free Software Foundation, Inc.
  3. ;; Authors: Kai Grossjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE>
  4. ;; Alex Schroeder <alex@gnu.org>
  5. ;; Chong Yidong <cyd@stupidchicken.com>
  6. ;; Maintainer: Chong Yidong <cyd@stupidchicken.com>
  7. ;; Keywords: convenience, wp
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Some text editors save text files with long lines, and they
  21. ;; automatically break these lines at whitespace, without actually
  22. ;; inserting any newline characters. When doing `M-q' in Emacs, you
  23. ;; are inserting newline characters. Longlines mode provides a file
  24. ;; format which wraps the long lines when reading a file and unwraps
  25. ;; the lines when saving the file. It can also wrap and unwrap
  26. ;; automatically as editing takes place.
  27. ;; Special thanks to Rod Smith for many useful bug reports.
  28. ;;; Code:
  29. (defgroup longlines nil
  30. "Automatic wrapping of long lines when loading files."
  31. :group 'fill)
  32. (defcustom longlines-auto-wrap t
  33. "Non-nil means long lines are automatically wrapped after each command.
  34. Otherwise, you can perform filling using `fill-paragraph' or
  35. `auto-fill-mode'. In any case, the soft newlines will be removed
  36. when the file is saved to disk."
  37. :group 'longlines
  38. :type 'boolean)
  39. (defcustom longlines-wrap-follows-window-size nil
  40. "Non-nil means wrapping and filling happen at the edge of the window.
  41. Otherwise, `fill-column' is used, regardless of the window size. This
  42. does not work well when the buffer is displayed in multiple windows
  43. with differing widths.
  44. If the value is an integer, that specifies the distance from the
  45. right edge of the window at which wrapping occurs. For any other
  46. non-nil value, wrapping occurs 2 characters from the right edge."
  47. :group 'longlines
  48. :type 'boolean)
  49. (defcustom longlines-show-hard-newlines nil
  50. "Non-nil means each hard newline is marked on the screen.
  51. \(The variable `longlines-show-effect' controls what they look like.)
  52. You can also enable the display temporarily, using the command
  53. `longlines-show-hard-newlines'."
  54. :group 'longlines
  55. :type 'boolean)
  56. (defcustom longlines-show-effect (propertize "¶\n" 'face 'escape-glyph)
  57. "A string to display when showing hard newlines.
  58. This is used when `longlines-show-hard-newlines' is on."
  59. :group 'longlines
  60. :type 'string)
  61. ;; Internal variables
  62. (defvar longlines-wrap-beg nil)
  63. (defvar longlines-wrap-end nil)
  64. (defvar longlines-wrap-point nil)
  65. (defvar longlines-showing nil)
  66. (defvar longlines-decoded nil)
  67. (make-variable-buffer-local 'longlines-wrap-beg)
  68. (make-variable-buffer-local 'longlines-wrap-end)
  69. (make-variable-buffer-local 'longlines-wrap-point)
  70. (make-variable-buffer-local 'longlines-showing)
  71. (make-variable-buffer-local 'longlines-decoded)
  72. ;; Mode
  73. (defvar message-indent-citation-function)
  74. ;;;###autoload
  75. (define-minor-mode longlines-mode
  76. "Toggle Long Lines mode in this buffer.
  77. With a prefix argument ARG, enable Long Lines mode if ARG is
  78. positive, and disable it otherwise. If called from Lisp, enable
  79. the mode if ARG is omitted or nil.
  80. When Long Lines mode is enabled, long lines are wrapped if they
  81. extend beyond `fill-column'. The soft newlines used for line
  82. wrapping will not show up when the text is yanked or saved to
  83. disk.
  84. If the variable `longlines-auto-wrap' is non-nil, lines are
  85. automatically wrapped whenever the buffer is changed. You can
  86. always call `fill-paragraph' to fill individual paragraphs.
  87. If the variable `longlines-show-hard-newlines' is non-nil, hard
  88. newlines are indicated with a symbol."
  89. :group 'longlines :lighter " ll"
  90. (if longlines-mode
  91. ;; Turn on longlines mode
  92. (progn
  93. (use-hard-newlines 1 'never)
  94. (set (make-local-variable 'require-final-newline) nil)
  95. (add-to-list 'buffer-file-format 'longlines)
  96. (add-hook 'change-major-mode-hook 'longlines-mode-off nil t)
  97. (add-hook 'before-revert-hook 'longlines-before-revert-hook nil t)
  98. (make-local-variable 'buffer-substring-filters)
  99. (make-local-variable 'longlines-auto-wrap)
  100. (set (make-local-variable 'isearch-search-fun-function)
  101. 'longlines-search-function)
  102. (set (make-local-variable 'replace-search-function)
  103. 'longlines-search-forward)
  104. (set (make-local-variable 'replace-re-search-function)
  105. 'longlines-re-search-forward)
  106. (add-to-list 'buffer-substring-filters 'longlines-encode-string)
  107. (when longlines-wrap-follows-window-size
  108. (let ((dw (if (and (integerp longlines-wrap-follows-window-size)
  109. (>= longlines-wrap-follows-window-size 0)
  110. (< longlines-wrap-follows-window-size
  111. (window-width)))
  112. longlines-wrap-follows-window-size
  113. 2)))
  114. (set (make-local-variable 'fill-column)
  115. (- (window-width) dw)))
  116. (add-hook 'window-configuration-change-hook
  117. 'longlines-window-change-function nil t))
  118. (let ((buffer-undo-list t)
  119. (inhibit-read-only t)
  120. (after-change-functions nil)
  121. (mod (buffer-modified-p))
  122. buffer-file-name buffer-file-truename)
  123. ;; Turning off undo is OK since (spaces + newlines) is
  124. ;; conserved, except for a corner case in
  125. ;; longlines-wrap-lines that we'll never encounter from here
  126. (save-restriction
  127. (widen)
  128. (unless longlines-decoded
  129. (longlines-decode-buffer)
  130. (setq longlines-decoded t))
  131. (longlines-wrap-region (point-min) (point-max)))
  132. (set-buffer-modified-p mod))
  133. (when (and longlines-show-hard-newlines
  134. (not longlines-showing))
  135. (longlines-show-hard-newlines))
  136. ;; Hacks to make longlines play nice with various modes.
  137. (cond ((eq major-mode 'mail-mode)
  138. (add-hook 'mail-setup-hook 'longlines-decode-buffer nil t)
  139. (or mail-citation-hook
  140. (add-hook 'mail-citation-hook 'mail-indent-citation nil t))
  141. (add-hook 'mail-citation-hook 'longlines-decode-region nil t))
  142. ((eq major-mode 'message-mode)
  143. (add-hook 'message-setup-hook 'longlines-decode-buffer nil t)
  144. (make-local-variable 'message-indent-citation-function)
  145. (if (not (listp message-indent-citation-function))
  146. (setq message-indent-citation-function
  147. (list message-indent-citation-function)))
  148. (add-to-list 'message-indent-citation-function
  149. 'longlines-decode-region t)))
  150. (add-hook 'after-change-functions 'longlines-after-change-function nil t)
  151. (add-hook 'post-command-hook 'longlines-post-command-function nil t)
  152. (when longlines-auto-wrap
  153. (auto-fill-mode 0)))
  154. ;; Turn off longlines mode
  155. (setq buffer-file-format (delete 'longlines buffer-file-format))
  156. (if longlines-showing
  157. (longlines-unshow-hard-newlines))
  158. (let ((buffer-undo-list t)
  159. (after-change-functions nil)
  160. (inhibit-read-only t)
  161. buffer-file-name buffer-file-truename)
  162. (if longlines-decoded
  163. (save-restriction
  164. (widen)
  165. (longlines-encode-region (point-min) (point-max))
  166. (setq longlines-decoded nil))))
  167. (remove-hook 'change-major-mode-hook 'longlines-mode-off t)
  168. (remove-hook 'after-change-functions 'longlines-after-change-function t)
  169. (remove-hook 'post-command-hook 'longlines-post-command-function t)
  170. (remove-hook 'before-revert-hook 'longlines-before-revert-hook t)
  171. (remove-hook 'window-configuration-change-hook
  172. 'longlines-window-change-function t)
  173. (when longlines-wrap-follows-window-size
  174. (kill-local-variable 'fill-column))
  175. (kill-local-variable 'isearch-search-fun-function)
  176. (kill-local-variable 'replace-search-function)
  177. (kill-local-variable 'replace-re-search-function)
  178. (kill-local-variable 'require-final-newline)
  179. (kill-local-variable 'buffer-substring-filters)
  180. (kill-local-variable 'use-hard-newlines)))
  181. (defun longlines-mode-off ()
  182. "Turn off longlines mode.
  183. This function exists to be called by `change-major-mode-hook' when the
  184. major mode changes."
  185. (longlines-mode 0))
  186. ;; Showing the effect of hard newlines in the buffer
  187. (defun longlines-show-hard-newlines (&optional arg)
  188. "Make hard newlines visible by adding a face.
  189. With optional argument ARG, make the hard newlines invisible again."
  190. (interactive "P")
  191. (if arg
  192. (longlines-unshow-hard-newlines)
  193. (setq longlines-showing t)
  194. (longlines-show-region (point-min) (point-max))))
  195. (defun longlines-show-region (beg end)
  196. "Make hard newlines between BEG and END visible."
  197. (let* ((pmin (min beg end))
  198. (pmax (max beg end))
  199. (pos (text-property-not-all pmin pmax 'hard nil))
  200. (mod (buffer-modified-p))
  201. (buffer-undo-list t)
  202. (inhibit-read-only t)
  203. (inhibit-modification-hooks t)
  204. buffer-file-name buffer-file-truename)
  205. (while pos
  206. (put-text-property pos (1+ pos) 'display
  207. (copy-sequence longlines-show-effect))
  208. (setq pos (text-property-not-all (1+ pos) pmax 'hard nil)))
  209. (restore-buffer-modified-p mod)))
  210. (defun longlines-unshow-hard-newlines ()
  211. "Make hard newlines invisible again."
  212. (interactive)
  213. (setq longlines-showing nil)
  214. (let ((pos (text-property-not-all (point-min) (point-max) 'hard nil))
  215. (mod (buffer-modified-p))
  216. (buffer-undo-list t)
  217. (inhibit-read-only t)
  218. (inhibit-modification-hooks t)
  219. buffer-file-name buffer-file-truename)
  220. (while pos
  221. (remove-text-properties pos (1+ pos) '(display))
  222. (setq pos (text-property-not-all (1+ pos) (point-max) 'hard nil)))
  223. (restore-buffer-modified-p mod)))
  224. ;; Wrapping the paragraphs.
  225. (defun longlines-wrap-region (beg end)
  226. "Wrap each successive line, starting with the line before BEG.
  227. Stop when we reach lines after END that don't need wrapping, or the
  228. end of the buffer."
  229. (let ((mod (buffer-modified-p)))
  230. (setq longlines-wrap-point (point))
  231. (goto-char beg)
  232. (forward-line -1)
  233. ;; Two successful longlines-wrap-line's in a row mean successive
  234. ;; lines don't need wrapping.
  235. (while (null (and (longlines-wrap-line)
  236. (or (eobp)
  237. (and (>= (point) end)
  238. (longlines-wrap-line))))))
  239. (goto-char longlines-wrap-point)
  240. (set-buffer-modified-p mod)))
  241. (defun longlines-wrap-line ()
  242. "If the current line needs to be wrapped, wrap it and return nil.
  243. If wrapping is performed, point remains on the line. If the line does
  244. not need to be wrapped, move point to the next line and return t."
  245. (if (longlines-set-breakpoint)
  246. (progn (insert-before-markers ?\n)
  247. (backward-char 1)
  248. (delete-char -1)
  249. (forward-char 1)
  250. nil)
  251. (if (longlines-merge-lines-p)
  252. (progn (end-of-line)
  253. ;; After certain commands (e.g. kill-line), there may be two
  254. ;; successive soft newlines in the buffer. In this case, we
  255. ;; replace these two newlines by a single space. Unfortunately,
  256. ;; this breaks the conservation of (spaces + newlines), so we
  257. ;; have to fiddle with longlines-wrap-point.
  258. (if (or (prog1 (bolp) (forward-char 1)) (eolp))
  259. (progn
  260. (delete-char -1)
  261. (if (> longlines-wrap-point (point))
  262. (setq longlines-wrap-point
  263. (1- longlines-wrap-point))))
  264. (insert-before-markers-and-inherit ?\s)
  265. (backward-char 1)
  266. (delete-char -1)
  267. (forward-char 1))
  268. nil)
  269. (forward-line 1)
  270. t)))
  271. (defun longlines-set-breakpoint ()
  272. "Place point where we should break the current line, and return t.
  273. If the line should not be broken, return nil; point remains on the
  274. line."
  275. (move-to-column fill-column)
  276. (if (and (re-search-forward "[^ ]" (line-end-position) 1)
  277. (> (current-column) fill-column))
  278. ;; This line is too long. Can we break it?
  279. (or (longlines-find-break-backward)
  280. (progn (move-to-column fill-column)
  281. (longlines-find-break-forward)))))
  282. (defun longlines-find-break-backward ()
  283. "Move point backward to the first available breakpoint and return t.
  284. If no breakpoint is found, return nil."
  285. (and (search-backward " " (line-beginning-position) 1)
  286. (save-excursion
  287. (skip-chars-backward " " (line-beginning-position))
  288. (null (bolp)))
  289. (progn (forward-char 1)
  290. (if (and fill-nobreak-predicate
  291. (run-hook-with-args-until-success
  292. 'fill-nobreak-predicate))
  293. (progn (skip-chars-backward " " (line-beginning-position))
  294. (longlines-find-break-backward))
  295. t))))
  296. (defun longlines-find-break-forward ()
  297. "Move point forward to the first available breakpoint and return t.
  298. If no break point is found, return nil."
  299. (and (search-forward " " (line-end-position) 1)
  300. (progn (skip-chars-forward " " (line-end-position))
  301. (null (eolp)))
  302. (if (and fill-nobreak-predicate
  303. (run-hook-with-args-until-success
  304. 'fill-nobreak-predicate))
  305. (longlines-find-break-forward)
  306. t)))
  307. (defun longlines-merge-lines-p ()
  308. "Return t if part of the next line can fit onto the current line.
  309. Otherwise, return nil. Text cannot be moved across hard newlines."
  310. (save-excursion
  311. (end-of-line)
  312. (and (null (eobp))
  313. (null (get-text-property (point) 'hard))
  314. (let ((space (- fill-column (current-column))))
  315. (forward-line 1)
  316. (if (eq (char-after) ? )
  317. t ; We can always merge some spaces
  318. (<= (if (search-forward " " (line-end-position) 1)
  319. (current-column)
  320. (1+ (current-column)))
  321. space))))))
  322. (defun longlines-decode-region (&optional beg end)
  323. "Turn all newlines between BEG and END into hard newlines.
  324. If BEG and END are nil, the point and mark are used."
  325. (if (null beg) (setq beg (point)))
  326. (if (null end) (setq end (mark t)))
  327. (save-excursion
  328. (let ((reg-max (max beg end)))
  329. (goto-char (min beg end))
  330. (while (search-forward "\n" reg-max t)
  331. (set-hard-newline-properties
  332. (match-beginning 0) (match-end 0))))))
  333. (defun longlines-decode-buffer ()
  334. "Turn all newlines in the buffer into hard newlines."
  335. (longlines-decode-region (point-min) (point-max)))
  336. (defun longlines-encode-region (beg end &optional _buffer)
  337. "Replace each soft newline between BEG and END with exactly one space.
  338. Hard newlines are left intact. The optional argument BUFFER exists for
  339. compatibility with `format-alist', and is ignored."
  340. (save-excursion
  341. (let ((reg-max (max beg end))
  342. (mod (buffer-modified-p)))
  343. (goto-char (min beg end))
  344. (while (search-forward "\n" reg-max t)
  345. (unless (get-text-property (match-beginning 0) 'hard)
  346. (replace-match " ")))
  347. (set-buffer-modified-p mod)
  348. end)))
  349. (defun longlines-encode-string (string)
  350. "Return a copy of STRING with each soft newline replaced by a space.
  351. Hard newlines are left intact."
  352. (let* ((str (copy-sequence string))
  353. (pos (string-match "\n" str)))
  354. (while pos
  355. (if (null (get-text-property pos 'hard str))
  356. (aset str pos ? ))
  357. (setq pos (string-match "\n" str (1+ pos))))
  358. str))
  359. ;; Auto wrap
  360. (defun longlines-auto-wrap (&optional arg)
  361. "Toggle automatic line wrapping.
  362. With optional argument ARG, turn on line wrapping if and only if ARG is positive.
  363. If automatic line wrapping is turned on, wrap the entire buffer."
  364. (interactive "P")
  365. (setq arg (if arg
  366. (> (prefix-numeric-value arg) 0)
  367. (not longlines-auto-wrap)))
  368. (if arg
  369. (progn
  370. (setq longlines-auto-wrap t)
  371. (longlines-wrap-region (point-min) (point-max))
  372. (message "Auto wrap enabled."))
  373. (setq longlines-auto-wrap nil)
  374. (message "Auto wrap disabled.")))
  375. (defun longlines-after-change-function (beg end _len)
  376. "Update `longlines-wrap-beg' and `longlines-wrap-end'.
  377. This is called by `after-change-functions' to keep track of the region
  378. that has changed."
  379. (when (and longlines-auto-wrap (not undo-in-progress))
  380. (setq longlines-wrap-beg
  381. (if longlines-wrap-beg (min longlines-wrap-beg beg) beg))
  382. (setq longlines-wrap-end
  383. (if longlines-wrap-end (max longlines-wrap-end end) end))))
  384. (defun longlines-post-command-function ()
  385. "Perform line wrapping on the parts of the buffer that have changed.
  386. This is called by `post-command-hook' after each command."
  387. (when (and longlines-auto-wrap longlines-wrap-beg)
  388. (if (or (eq this-command 'yank)
  389. (eq this-command 'yank-pop))
  390. (longlines-decode-region (point) (mark t)))
  391. (if longlines-showing
  392. (longlines-show-region longlines-wrap-beg longlines-wrap-end))
  393. (unless (or (eq this-command 'fill-paragraph)
  394. (eq this-command 'fill-region))
  395. (longlines-wrap-region longlines-wrap-beg longlines-wrap-end))
  396. (setq longlines-wrap-beg nil)
  397. (setq longlines-wrap-end nil)))
  398. (defun longlines-window-change-function ()
  399. "Re-wrap the buffer if the window width has changed.
  400. This is called by `window-configuration-change-hook'."
  401. (let ((dw (if (and (integerp longlines-wrap-follows-window-size)
  402. (>= longlines-wrap-follows-window-size 0)
  403. (< longlines-wrap-follows-window-size (window-width)))
  404. longlines-wrap-follows-window-size
  405. 2)))
  406. (when (/= fill-column (- (window-width) dw))
  407. (setq fill-column (- (window-width) dw))
  408. (longlines-wrap-region (point-min) (point-max)))))
  409. ;; Isearch
  410. (defun longlines-search-function ()
  411. (cond
  412. (isearch-word
  413. (if isearch-forward 'word-search-forward 'word-search-backward))
  414. (isearch-regexp
  415. (if isearch-forward 're-search-forward 're-search-backward))
  416. (t
  417. (if isearch-forward
  418. 'longlines-search-forward
  419. 'longlines-search-backward))))
  420. (defun longlines-search-forward (string &optional bound noerror count)
  421. (let ((search-spaces-regexp " *[ \n]"))
  422. (re-search-forward (regexp-quote string) bound noerror count)))
  423. (defun longlines-search-backward (string &optional bound noerror count)
  424. (let ((search-spaces-regexp " *[ \n]"))
  425. (re-search-backward (regexp-quote string) bound noerror count)))
  426. (defun longlines-re-search-forward (string &optional bound noerror count)
  427. (let ((search-spaces-regexp " *[ \n]"))
  428. (re-search-forward string bound noerror count)))
  429. ;; Loading and saving
  430. (defun longlines-before-revert-hook ()
  431. (add-hook 'after-revert-hook 'longlines-after-revert-hook nil t)
  432. (longlines-mode 0))
  433. (defun longlines-after-revert-hook ()
  434. (remove-hook 'after-revert-hook 'longlines-after-revert-hook t)
  435. (longlines-mode 1))
  436. (add-to-list
  437. 'format-alist
  438. (list 'longlines "Automatically wrap long lines." nil nil
  439. 'longlines-encode-region t nil))
  440. ;; Unloading
  441. (defun longlines-unload-function ()
  442. "Unload the longlines library."
  443. (save-current-buffer
  444. (dolist (buffer (buffer-list))
  445. (set-buffer buffer)
  446. (longlines-mode-off)))
  447. ;; continue standard unloading
  448. nil)
  449. (provide 'longlines)
  450. ;;; longlines.el ends here