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