annotate lisp/longlines.el @ 90136:7a3341d65a12

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