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