comparison lisp/longlines.el @ 90261:7beb78bc1f8e

Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-97 Merge from emacs--cvs-trunk--0 Patches applied: * emacs--cvs-trunk--0 (patch 616-696) - Add lisp/mh-e/.arch-inventory - Update from CVS - Merge from gnus--rel--5.10 - Update from CVS: lisp/smerge-mode.el: Add 'tools' to file keywords. - lisp/gnus/ChangeLog: Remove duplicate entry * gnus--rel--5.10 (patch 147-181) - Update from CVS - Merge from emacs--cvs-trunk--0 - Update from CVS: lisp/mml.el (mml-preview): Doc fix. - Update from CVS: texi/message.texi: Fix default values. - Update from CVS: texi/gnus.texi (RSS): Addition.
author Miles Bader <miles@gnu.org>
date Mon, 16 Jan 2006 08:37:27 +0000
parents e1a1deda5d65
children 3bd95f4f2941
comparison
equal deleted inserted replaced
90260:0ca0d9181b5e 90261:7beb78bc1f8e
1 ;;; longlines.el --- automatically wrap long lines 1 ;;; longlines.el --- automatically wrap long lines
2 2
3 ;; Copyright (C) 2000, 2001, 2005 Free Software Foundation, Inc. 3 ;; Copyright (C) 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
4 4
5 ;; Authors: Kai Grossjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> 5 ;; Authors: Kai Grossjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE>
6 ;; Alex Schroeder <alex@gnu.org> 6 ;; Alex Schroeder <alex@gnu.org>
7 ;; Chong Yidong <cyd@stupidchicken.com> 7 ;; Chong Yidong <cyd@stupidchicken.com>
8 ;; Maintainer: Chong Yidong <cyd@stupidchicken.com> 8 ;; Maintainer: Chong Yidong <cyd@stupidchicken.com>
9 ;; Keywords: convenience 9 ;; Keywords: convenience, wp
10 10
11 ;; This file is part of GNU Emacs. 11 ;; This file is part of GNU Emacs.
12 12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify 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 14 ;; it under the terms of the GNU General Public License as published by
106 (progn 106 (progn
107 (use-hard-newlines 1 'never) 107 (use-hard-newlines 1 'never)
108 (set (make-local-variable 'require-final-newline) nil) 108 (set (make-local-variable 'require-final-newline) nil)
109 (add-to-list 'buffer-file-format 'longlines) 109 (add-to-list 'buffer-file-format 'longlines)
110 (add-hook 'change-major-mode-hook 'longlines-mode-off nil t) 110 (add-hook 'change-major-mode-hook 'longlines-mode-off nil t)
111 (add-hook 'before-revert-hook 'longlines-before-revert-hook nil t)
111 (make-local-variable 'buffer-substring-filters) 112 (make-local-variable 'buffer-substring-filters)
112 (set (make-local-variable 'isearch-search-fun-function) 113 (set (make-local-variable 'isearch-search-fun-function)
113 'longlinges-search-function) 114 'longlines-search-function)
114 (add-to-list 'buffer-substring-filters 'longlines-encode-string) 115 (add-to-list 'buffer-substring-filters 'longlines-encode-string)
115 (when longlines-wrap-follows-window-size 116 (when longlines-wrap-follows-window-size
116 (set (make-local-variable 'fill-column) 117 (set (make-local-variable 'fill-column)
117 (- (window-width) window-min-width)) 118 (- (window-width) window-min-width))
118 (add-hook 'window-configuration-change-hook 119 (add-hook 'window-configuration-change-hook
119 'longlines-window-change-function nil t)) 120 'longlines-window-change-function nil t))
120 (let ((buffer-undo-list t) 121 (let ((buffer-undo-list t)
121 (inhibit-read-only t) 122 (inhibit-read-only t)
123 (after-change-functions nil)
122 (mod (buffer-modified-p))) 124 (mod (buffer-modified-p)))
123 ;; Turning off undo is OK since (spaces + newlines) is 125 ;; Turning off undo is OK since (spaces + newlines) is
124 ;; conserved, except for a corner case in 126 ;; conserved, except for a corner case in
125 ;; longlines-wrap-lines that we'll never encounter from here 127 ;; longlines-wrap-lines that we'll never encounter from here
126 (longlines-decode-region (point-min) (point-max)) 128 (save-restriction
127 (longlines-wrap-region (point-min) (point-max)) 129 (widen)
130 (longlines-decode-buffer)
131 (longlines-wrap-region (point-min) (point-max)))
128 (set-buffer-modified-p mod)) 132 (set-buffer-modified-p mod))
129 (when (and longlines-show-hard-newlines 133 (when (and longlines-show-hard-newlines
130 (not longlines-showing)) 134 (not longlines-showing))
131 (longlines-show-hard-newlines)) 135 (longlines-show-hard-newlines))
136
137 ;; Hacks to make longlines play nice with various modes.
138 (cond ((eq major-mode 'mail-mode)
139 (add-hook 'mail-setup-hook 'longlines-decode-buffer nil t)
140 (or mail-citation-hook
141 (add-hook 'mail-citation-hook 'mail-indent-citation nil t))
142 (add-hook 'mail-citation-hook 'longlines-decode-region nil t))
143 ((eq major-mode 'message-mode)
144 (add-hook 'message-setup-hook 'longlines-decode-buffer nil t)
145 (make-local-variable 'message-indent-citation-function)
146 (if (not (listp message-indent-citation-function))
147 (setq message-indent-citation-function
148 (list message-indent-citation-function)))
149 (add-to-list 'message-indent-citation-function
150 'longlines-decode-region t)))
151
132 (when longlines-auto-wrap 152 (when longlines-auto-wrap
133 (auto-fill-mode 0) 153 (auto-fill-mode 0)
134 (add-hook 'after-change-functions 154 (add-hook 'after-change-functions
135 'longlines-after-change-function nil t) 155 'longlines-after-change-function nil t)
136 (add-hook 'post-command-hook 156 (add-hook 'post-command-hook
138 ;; Turn off longlines mode 158 ;; Turn off longlines mode
139 (setq buffer-file-format (delete 'longlines buffer-file-format)) 159 (setq buffer-file-format (delete 'longlines buffer-file-format))
140 (if longlines-showing 160 (if longlines-showing
141 (longlines-unshow-hard-newlines)) 161 (longlines-unshow-hard-newlines))
142 (let ((buffer-undo-list t) 162 (let ((buffer-undo-list t)
163 (after-change-functions nil)
143 (inhibit-read-only t)) 164 (inhibit-read-only t))
144 (longlines-encode-region (point-min) (point-max))) 165 (save-restriction
166 (widen)
167 (longlines-encode-region (point-min) (point-max))))
145 (remove-hook 'change-major-mode-hook 'longlines-mode-off t) 168 (remove-hook 'change-major-mode-hook 'longlines-mode-off t)
146 (remove-hook 'before-kill-functions 'longlines-encode-region t)
147 (remove-hook 'after-change-functions 'longlines-after-change-function t) 169 (remove-hook 'after-change-functions 'longlines-after-change-function t)
148 (remove-hook 'post-command-hook 'longlines-post-command-function t) 170 (remove-hook 'post-command-hook 'longlines-post-command-function t)
171 (remove-hook 'before-revert-hook 'longlines-before-revert-hook t)
149 (remove-hook 'window-configuration-change-hook 172 (remove-hook 'window-configuration-change-hook
150 'longlines-window-change-function t) 173 'longlines-window-change-function t)
151 (when longlines-wrap-follows-window-size 174 (when longlines-wrap-follows-window-size
152 (kill-local-variable 'fill-column)) 175 (kill-local-variable 'fill-column))
153 (kill-local-variable 'isearch-search-fun-function) 176 (kill-local-variable 'isearch-search-fun-function)
214 (defun longlines-wrap-line () 237 (defun longlines-wrap-line ()
215 "If the current line needs to be wrapped, wrap it and return nil. 238 "If the current line needs to be wrapped, wrap it and return nil.
216 If wrapping is performed, point remains on the line. If the line does 239 If wrapping is performed, point remains on the line. If the line does
217 not need to be wrapped, move point to the next line and return t." 240 not need to be wrapped, move point to the next line and return t."
218 (if (longlines-set-breakpoint) 241 (if (longlines-set-breakpoint)
219 (progn (backward-char 1) 242 (progn (insert-before-markers ?\n)
220 (delete-char 1) 243 (backward-char 1)
221 (insert-char ?\n 1) 244 (delete-char -1)
245 (forward-char 1)
222 nil) 246 nil)
223 (if (longlines-merge-lines-p) 247 (if (longlines-merge-lines-p)
224 (progn (end-of-line) 248 (progn (end-of-line)
225 (delete-char 1)
226 ;; After certain commands (e.g. kill-line), there may be two 249 ;; After certain commands (e.g. kill-line), there may be two
227 ;; successive soft newlines in the buffer. In this case, we 250 ;; successive soft newlines in the buffer. In this case, we
228 ;; replace these two newlines by a single space. Unfortunately, 251 ;; replace these two newlines by a single space. Unfortunately,
229 ;; this breaks the conservation of (spaces + newlines), so we 252 ;; this breaks the conservation of (spaces + newlines), so we
230 ;; have to fiddle with longlines-wrap-point. 253 ;; have to fiddle with longlines-wrap-point.
231 (if (or (bolp) (eolp)) 254 (if (or (prog1 (bolp) (forward-char 1)) (eolp))
232 (if (> longlines-wrap-point (point)) 255 (progn
233 (setq longlines-wrap-point 256 (delete-char -1)
234 (1- longlines-wrap-point))) 257 (if (> longlines-wrap-point (point))
235 (insert-char ? 1)) 258 (setq longlines-wrap-point
259 (1- longlines-wrap-point))))
260 (insert-before-markers-and-inherit ?\ )
261 (backward-char 1)
262 (delete-char -1)
263 (forward-char 1))
236 nil) 264 nil)
237 (forward-line 1) 265 (forward-line 1)
238 t))) 266 t)))
239 267
240 (defun longlines-set-breakpoint () 268 (defun longlines-set-breakpoint ()
290 (<= (if (search-forward " " (line-end-position) 1) 318 (<= (if (search-forward " " (line-end-position) 1)
291 (current-column) 319 (current-column)
292 (1+ (current-column))) 320 (1+ (current-column)))
293 space)))))) 321 space))))))
294 322
295 (defun longlines-decode-region (beg end) 323 (defun longlines-decode-region (&optional beg end)
296 "Turn all newlines between BEG and END into hard newlines." 324 "Turn all newlines between BEG and END into hard newlines.
325 If BEG and END are nil, the point and mark are used."
326 (if (null beg) (setq beg (point)))
327 (if (null end) (setq end (mark t)))
297 (save-excursion 328 (save-excursion
298 (goto-char (min beg end)) 329 (let ((reg-max (max beg end)))
299 (while (search-forward "\n" (max beg end) t) 330 (goto-char (min beg end))
300 (set-hard-newline-properties 331 (while (search-forward "\n" reg-max t)
301 (match-beginning 0) (match-end 0))))) 332 (set-hard-newline-properties
333 (match-beginning 0) (match-end 0))))))
334
335 (defun longlines-decode-buffer ()
336 "Turn all newlines in the buffer into hard newlines."
337 (longlines-decode-region (point-min) (point-max)))
302 338
303 (defun longlines-encode-region (beg end &optional buffer) 339 (defun longlines-encode-region (beg end &optional buffer)
304 "Replace each soft newline between BEG and END with exactly one space. 340 "Replace each soft newline between BEG and END with exactly one space.
305 Hard newlines are left intact. The optional argument BUFFER exists for 341 Hard newlines are left intact. The optional argument BUFFER exists for
306 compatibility with `format-alist', and is ignored." 342 compatibility with `format-alist', and is ignored."
307 (save-excursion 343 (save-excursion
308 (let ((mod (buffer-modified-p))) 344 (let ((reg-max (max beg end))
345 (mod (buffer-modified-p)))
309 (goto-char (min beg end)) 346 (goto-char (min beg end))
310 (while (search-forward "\n" (max (max beg end)) t) 347 (while (search-forward "\n" reg-max t)
311 (unless (get-text-property (match-beginning 0) 'hard) 348 (unless (get-text-property (match-beginning 0) 'hard)
312 (replace-match " "))) 349 (replace-match " ")))
313 (set-buffer-modified-p mod) 350 (set-buffer-modified-p mod)
314 end))) 351 end)))
315 352
384 (longlines-wrap-region (point-min) (point-max)) 421 (longlines-wrap-region (point-min) (point-max))
385 (set-buffer-modified-p mod)))) 422 (set-buffer-modified-p mod))))
386 423
387 ;; Isearch 424 ;; Isearch
388 425
389 (defun longlinges-search-function () 426 (defun longlines-search-function ()
390 (cond 427 (cond
391 (isearch-word 428 (isearch-word
392 (if isearch-forward 'word-search-forward 'word-search-backward)) 429 (if isearch-forward 'word-search-forward 'word-search-backward))
393 (isearch-regexp 430 (isearch-regexp
394 (if isearch-forward 're-search-forward 're-search-backward)) 431 (if isearch-forward 're-search-forward 're-search-backward))
405 (let ((search-spaces-regexp "[ \n]+")) 442 (let ((search-spaces-regexp "[ \n]+"))
406 (re-search-backward (regexp-quote string) bound noerror count))) 443 (re-search-backward (regexp-quote string) bound noerror count)))
407 444
408 ;; Loading and saving 445 ;; Loading and saving
409 446
447 (defun longlines-before-revert-hook ()
448 (add-hook 'after-revert-hook 'longlines-after-revert-hook nil t)
449 (longlines-mode 0))
450
451 (defun longlines-after-revert-hook ()
452 (remove-hook 'after-revert-hook 'longlines-after-revert-hook t)
453 (longlines-mode 1))
454
410 (add-to-list 455 (add-to-list
411 'format-alist 456 'format-alist
412 (list 'longlines "Automatically wrap long lines." nil 457 (list 'longlines "Automatically wrap long lines." nil nil
413 'longlines-decode-region 'longlines-encode-region t nil)) 458 'longlines-encode-region t nil))
414 459
415 (provide 'longlines) 460 (provide 'longlines)
416 461
417 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624 462 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624
418 ;;; longlines.el ends here 463 ;;; longlines.el ends here