Commit | Line | Data |
---|---|---|
72c0ae01 ER |
1 | ;;; two-column.el --- minor mode for editing of two-column text |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 1992-1995, 2001-2011 Free Software Foundation, Inc. |
e6b72410 | 4 | |
3e910376 | 5 | ;; Author: Daniel Pfeiffer <occitan@esperanto.org> |
e6b72410 | 6 | ;; Adapted-By: ESR, Daniel Pfeiffer |
6228c05b | 7 | ;; Keywords: wp |
e6b72410 | 8 | |
e8af40ee PJ |
9 | ;; This file is part of GNU Emacs. |
10 | ||
f3667b9f GM |
11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation, either version 3 of the License, or | |
14 | ;; (at your option) any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
e6b72410 | 23 | |
e8af40ee | 24 | ;;; Commentary: |
e6b72410 KH |
25 | |
26 | ;;; Komentario: Commentary: | |
27 | ||
28 | ;; Tiu programaro ebligas vin redakti This package gives you the ability | |
29 | ;; dukolumnan tekston. to edit text in a two-column format. | |
30 | ||
31 | ||
32 | ;; Vi havas tri eblecojn por eki tiun You have three ways to start up this | |
33 | ;; mal^cefan modalon. ^Ciu donas al vi minor mode. Each gives you a | |
34 | ;; horizontale disigatan fenestron, si- horizontally split window similar to | |
35 | ;; milan al fina apareco de via teksto: the final outcome of your text: | |
36 | ||
37 | ||
38 | ;; f2 2 asocias novan bufron nomatan associates a new buffer called | |
39 | ;; C-x 6 2 same, sed kun 2C/ anta^u. the same, but with 2C/ | |
40 | ;; prepended. | |
41 | ||
42 | ;; f2 b asocias alian bufron. Vi povas associates another buffer. | |
43 | ;; C-x 6 b anka^u asocii dataron, se vi This can be used to associate a | |
44 | ;; ^jus anta^ue faris C-x C-f. file if you just did C-x C-f. | |
45 | ||
46 | ;; f2 s disigas jam dukolumnan tekston splits a two-column text into | |
47 | ;; C-x 6 s en du bufroj ekde la nuna two buffers from the current | |
48 | ;; linio, kaj je la nuna kolumno. line and at the current column. | |
49 | ;; La anta^uaj signoj (ofte The preceding characters (often | |
50 | ;; tabeligilo a^u |) estas la tab or |) are the column | |
51 | ;; kolumna disiganto. Linioj kiuj separator. Lines that don't | |
52 | ;; ne enhavas ilin ne estas have them won't be separated. | |
53 | ;; disigitaj. Kiel la kvara kaj Like the fourth and fifth line | |
54 | ;; la kvina linio se vi disigas if you split this file from | |
55 | ;; ^ci dataron ekde la unua angla the first english word. | |
56 | ;; vorto. | |
57 | ||
58 | ;; Se vi volas meti longajn liniojn If you include long lines, i.e which | |
59 | ;; (ekz. programerojn) en la kunigotan will span both columns (eg. source | |
60 | ;; tekston, ili devas esti en la code), they should be in what will | |
61 | ;; estonte unua kolumno. La alia devas be the first column, with the | |
62 | ;; havi vakajn linion apud ili. associated buffer having empty lines | |
63 | ;; next to them. | |
64 | ||
65 | ;; Averto: en Emacs kiam vi ^san^gas la Attention: in Emacs when you change | |
66 | ;; ^cefan modalon, la mal^cefaj modaloj the major mode, the minor modes are | |
67 | ;; estas anka^u elmemorigitaj. Tiu- also purged from memory. In that | |
68 | ;; okaze vi devas religi la du bufrojn case you must reassociate the two | |
69 | ;; per iu C-x 6-ordono, ekz. C-x 6 b. buffers with any C-x 6-command, e.g. | |
70 | ;; C-x 6 b. | |
71 | ||
72 | ;; Kiam vi estos kontenta de la When you have edited both buffers to | |
73 | ;; rezulto, vi kunmetos la du kolumnojn your content, you merge them with | |
74 | ;; per C-x 6 1. Se vi poste vidas C-x 6 1. If you then see a problem, | |
75 | ;; problemon, vi neniigu la kunmeton you undo the merge with C-x u and | |
76 | ;; per C-x u kaj plue modifu la du continue to edit the two buffers. | |
77 | ;; bufrojn. Kiam vi ne plu volas tajpi When you no longer want to edit in | |
78 | ;; dukolumne, vi eliru el la mal^cefa two columns, you turn off the minor | |
79 | ;; modalo per C-x 6 d. mode with C-x 6 d. | |
80 | ||
81 | ||
82 | ;; Aldone al dukolumna redaktado, ek- In addition to two-column editing of | |
83 | ;; zemple por skribi dulingvan tekston text, for example for writing a | |
84 | ;; flank-al-flanke kiel ^ci tiu, aliaj bilingual text side-by-side as shown | |
85 | ;; interesaj uzoj trovitas por tiu mal- here, other interesting uses have | |
86 | ;; ^cefa modalo: been found for this minor mode: | |
87 | ||
88 | ;; Vi povas disigi la kolumnojn per {+} You can separate the columns with | |
89 | ;; ajna pla^ca ^ceno starigante {+} any string that pleases you, by | |
90 | ;; `2C-separator'. Ekzemple "{+} " {+} setting `2C-separator'. For example | |
91 | ;; por amuzi^gi. f2 s a^u C-x 6 s {+} "{+} " if you'd like to have fun. | |
92 | ;; traktas tiujn kun prefiksa {+} f2 s or C-x 6 s handles these with a | |
93 | ;; argumento kiu signifas la longon {+} prefix argument that means the | |
94 | ;; de tia ^ceno. {+} desired length of such a string. | |
95 | ||
96 | ||
97 | ;; Programistoj eble ^satus la eblecon Programmers might like the ability | |
98 | ;; forspliti la komentarian kolumnon de to split off the comment column of a | |
99 | ;; dosiero kiel la sekvanta. Vi povas file that looks like the following. | |
100 | ;; rearan^gigi la paragrafon. La pro- You can fill-paragraph the comment. | |
101 | ;; blemo estas ke koda^jo tuj lar- The problem is, code quickly gets | |
102 | ;; ^gi^gas, tiel ke vi bezonas pli rather wide, so you need to use a | |
103 | ;; mallar^gan komentarian kolumnon. narrower comment column. Code lines | |
104 | ;; Koda^jaj linioj tra `comment-column' that reach beyond `comment-column' | |
105 | ;; ne problemas, krom ke vi ne vidos are no problem, except that you | |
106 | ;; iliajn finojn dum redaktado. won't see their end during editing. | |
107 | ||
108 | ||
72c0ae01 ER |
109 | ;; BEGIN -- This is just some meaningless |
110 | ;; FOR i IN 1..10 LOOP -- code in Ada, that runs foobar | |
e6b72410 | 111 | ;; foobar( i ); -- once for each argument from one |
72c0ae01 ER |
112 | ;; END LOOP; -- to ten, and then we're already |
113 | ;; END; -- through with it. | |
e6b72410 KH |
114 | |
115 | ;; Pli bone ankora^u, vi povas pozici- Better yet, you can put the point | |
116 | ;; i^gi anta^u "This", tajpi M-3 f2 s before "This", type M-3 f2 s | |
117 | ;; kiu igas "-- " la separigilon inter which makes "-- " the separator | |
118 | ;; senkomentaria Ada bufro kaj nur- between a no-comments Ada buffer, | |
119 | ;; teksta komentaria bufro. Kiam vi and a plain text comment buffer. | |
120 | ;; denove kuni^gos ilin, ^ciu nevaka When you put them back together, | |
121 | ;; linio de l' dua kolumno denove every non-empty line of the 2nd | |
122 | ;; anta^uhavos "-- ". column will again be preceded by | |
123 | ;; "-- ". | |
124 | ||
72c0ae01 ER |
125 | |
126 | ;;; Code: | |
127 | \f | |
e6b72410 KH |
128 | |
129 | ;; Lucid patch | |
130 | (or (fboundp 'frame-width) | |
131 | (fset 'frame-width 'screen-width)) | |
132 | ||
133 | ||
3bcbd523 | 134 | ;;;;; Set up keymap ;;;;; |
72c0ae01 | 135 | |
e6b72410 KH |
136 | (defvar 2C-mode-map |
137 | (let ((map (make-sparse-keymap))) | |
138 | (define-key map "2" '2C-two-columns) | |
139 | (define-key map [f2] '2C-two-columns) | |
140 | (define-key map "b" '2C-associate-buffer) | |
141 | (define-key map "s" '2C-split) | |
e6b72410 KH |
142 | map) |
143 | "Keymap for commands for setting up two-column mode.") | |
144 | ||
145 | ||
146 | ||
147 | ;;;###autoload (autoload '2C-command "two-column" () t 'keymap) | |
148 | (fset '2C-command 2C-mode-map) | |
72c0ae01 | 149 | |
e6b72410 KH |
150 | ;; This one is for historical reasons and simple keyboards, it is not |
151 | ;; at all mnemonic. All usual sequences containing 2 were used, and | |
152 | ;; f2 could not be set up in a standard way under Emacs 18. | |
2c02e313 | 153 | ;;;###autoload (global-set-key "\C-x6" '2C-command) |
e6b72410 | 154 | |
2c02e313 | 155 | ;;;###autoload (global-set-key [f2] '2C-command) |
e6b72410 KH |
156 | |
157 | ||
158 | (defvar 2C-minor-mode-map | |
159 | (let ((map (make-sparse-keymap))) | |
160 | (define-key map "1" '2C-merge) | |
161 | (define-key map "d" '2C-dissociate) | |
162 | (define-key map "o" '2C-associated-buffer) | |
8fe9ecef | 163 | (define-key map "\^m" '2C-newline) |
e6b72410 | 164 | (define-key map "|" '2C-toggle-autoscroll) |
30a7d33c KH |
165 | (define-key map "{" '2C-shrink-window-horizontally) |
166 | (define-key map "}" '2C-enlarge-window-horizontally) | |
e6b72410 KH |
167 | map) |
168 | "Keymap for commands for use in two-column mode.") | |
169 | ||
170 | ||
171 | (setq minor-mode-map-alist | |
172 | (cons (cons '2C-mode | |
173 | (let ((map (make-sparse-keymap))) | |
174 | (substitute-key-definition '2C-command 2C-minor-mode-map | |
175 | map (current-global-map)) | |
30a7d33c KH |
176 | (substitute-key-definition 'enlarge-window-horizontally |
177 | '2C-enlarge-window-horizontally | |
178 | map (current-global-map)) | |
179 | (substitute-key-definition 'shrink-window-horizontally | |
180 | '2C-shrink-window-horizontally | |
181 | map (current-global-map)) | |
e6b72410 KH |
182 | map)) |
183 | minor-mode-map-alist)) | |
3bcbd523 RS |
184 | \f |
185 | ;;;;; variable declarations ;;;;; | |
72c0ae01 | 186 | |
ded3e3d8 | 187 | (defgroup two-column nil |
5cbebcab | 188 | "Minor mode for editing of two-column text." |
ded3e3d8 RS |
189 | :prefix "2C-" |
190 | :group 'frames) | |
191 | ||
192 | ||
e6b72410 KH |
193 | ;; Markers seem to be the only buffer-id not affected by renaming a buffer. |
194 | ;; This nevertheless loses when a buffer is killed. The variable-name is | |
195 | ;; required by `describe-mode'. | |
196 | (defvar 2C-mode nil | |
72c0ae01 | 197 | "Marker to the associated buffer, if non-nil.") |
e6b72410 KH |
198 | (make-variable-buffer-local '2C-mode) |
199 | (put '2C-mode 'permanent-local t) | |
200 | ||
201 | ||
202 | ||
203 | (setq minor-mode-alist (cons '(2C-mode " 2C") minor-mode-alist)) | |
204 | ||
72c0ae01 | 205 | |
72c0ae01 ER |
206 | |
207 | ;; rearranged, so that the pertinent info will show in 40 columns | |
ded3e3d8 | 208 | (defcustom 2C-mode-line-format |
72c0ae01 | 209 | '("-%*- %15b --" (-3 . "%p") "--%[(" mode-name |
e6b72410 | 210 | minor-mode-alist "%n" mode-line-process ")%]%-") |
1fc7dabf | 211 | "Value of `mode-line-format' for a buffer in two-column minor mode." |
ded3e3d8 RS |
212 | :type 'sexp |
213 | :group 'two-column) | |
72c0ae01 | 214 | |
e6b72410 | 215 | |
ded3e3d8 | 216 | (defcustom 2C-other-buffer-hook 'text-mode |
1fc7dabf | 217 | "Hook run in new buffer when it is associated with current one." |
ded3e3d8 RS |
218 | :type 'function |
219 | :group 'two-column) | |
e6b72410 KH |
220 | |
221 | ||
ded3e3d8 | 222 | (defcustom 2C-separator "" |
1fc7dabf | 223 | "A string inserted between the two columns when merging. |
ded3e3d8 RS |
224 | This gets set locally by \\[2C-split]." |
225 | :type 'string | |
226 | :group 'two-column) | |
e6b72410 KH |
227 | (put '2C-separator 'permanent-local t) |
228 | ||
229 | ||
72c0ae01 | 230 | |
ded3e3d8 | 231 | (defcustom 2C-window-width 40 |
1fc7dabf | 232 | "The width of the first column. (Must be at least `window-min-width') |
ded3e3d8 RS |
233 | This value is local for every buffer that sets it." |
234 | :type 'integer | |
235 | :group 'two-column) | |
e6b72410 KH |
236 | (make-variable-buffer-local '2C-window-width) |
237 | (put '2C-window-width 'permanent-local t) | |
72c0ae01 | 238 | |
e6b72410 KH |
239 | |
240 | ||
ded3e3d8 | 241 | (defcustom 2C-beyond-fill-column 4 |
1fc7dabf | 242 | "Base for calculating `fill-column' for a buffer in two-column minor mode. |
e6b72410 | 243 | The value of `fill-column' becomes `2C-window-width' for this buffer |
ded3e3d8 RS |
244 | minus this value." |
245 | :type 'integer | |
246 | :group 'two-column) | |
72c0ae01 | 247 | |
e6b72410 KH |
248 | |
249 | ||
ded3e3d8 RS |
250 | (defcustom 2C-autoscroll t |
251 | "If non-nil, Emacs attempts to keep the two column's buffers aligned." | |
252 | :type 'boolean | |
253 | :group 'two-column) | |
e6b72410 KH |
254 | |
255 | ||
256 | ||
257 | (defvar 2C-autoscroll-start nil) | |
258 | (make-variable-buffer-local '2C-autoscroll-start) | |
72c0ae01 ER |
259 | \f |
260 | ;;;;; base functions ;;;;; | |
261 | ||
e6b72410 | 262 | ;; The access method for the other buffer. This tries to remedy against |
72c0ae01 | 263 | ;; lost local variables and lost buffers. |
8fe9ecef KH |
264 | (defun 2C-other (&optional req) |
265 | (or (if 2C-mode | |
266 | (or (prog1 | |
267 | (marker-buffer 2C-mode) | |
268 | (setq mode-line-format 2C-mode-line-format)) | |
269 | ;; The associated buffer somehow got killed. | |
270 | (progn | |
271 | ;; The other variables may later be useful if the user | |
272 | ;; reestablishes the association. | |
273 | (kill-local-variable '2C-mode) | |
274 | (kill-local-variable 'mode-line-format) | |
275 | nil))) | |
e8af40ee | 276 | (if req (error "You must first set two-column minor mode")))) |
72c0ae01 | 277 | |
72c0ae01 | 278 | |
72c0ae01 | 279 | |
e6b72410 KH |
280 | ;; function for setting up two-column minor mode in a buffer associated |
281 | ;; with the buffer pointed to by the marker other. | |
282 | (defun 2C-mode (other) | |
283 | "Minor mode for independently editing two columns. | |
284 | This is set up for two associated buffers by the three commands bound | |
285 | to \\[2C-two-columns] , \\[2C-associate-buffer] and \\[2C-split]. | |
286 | Turning on two-column mode calls the value of the variable `2C-mode-hook', | |
287 | if that value is non-nil. | |
288 | ||
289 | These buffers can be edited separately, for example with `fill-paragraph'. | |
290 | If you want to disable parallel scrolling temporarily, use \\[2C-toggle-autoscroll] . | |
72c0ae01 ER |
291 | |
292 | If you include long lines, i.e which will span both columns (eg. | |
293 | source code), they should be in what will be the first column, with | |
294 | the associated buffer having empty lines next to them. | |
295 | ||
e6b72410 KH |
296 | Potential uses are writing bilingual texts, or editing the comments of a |
297 | source code. See the file lisp/two-column.el for detailed examples. | |
298 | ||
72c0ae01 ER |
299 | You have the following commands at your disposal: |
300 | ||
e6b72410 KH |
301 | \\[2C-two-columns] Rearrange screen with current buffer first |
302 | \\[2C-associate-buffer] Reassociate buffer after changing major mode | |
72c0ae01 | 303 | \\[shrink-window-horizontally], \\[enlarge-window-horizontally] Shrink, enlarge current column |
8fe9ecef KH |
304 | \\[2C-associated-buffer] Switch to associated buffer at same point |
305 | \\[2C-newline] Insert newline(s) in both buffers at same point | |
e6b72410 KH |
306 | \\[2C-merge] Merge both buffers |
307 | \\[2C-dissociate] Dissociate the two buffers | |
72c0ae01 | 308 | |
e6b72410 KH |
309 | These keybindings can be customized in your ~/.emacs by `2C-mode-map', |
310 | `2C-minor-mode-map' and by binding `2C-command' to some prefix. | |
72c0ae01 ER |
311 | |
312 | The appearance of the screen can be customized by the variables | |
e6b72410 KH |
313 | `2C-window-width', `2C-beyond-fill-column', `2C-mode-line-format' and |
314 | `truncate-partial-width-windows'." | |
5181be3a | 315 | (add-hook 'post-command-hook '2C-autoscroll nil t) |
e6b72410 KH |
316 | (setq fill-column (- 2C-window-width |
317 | 2C-beyond-fill-column) | |
e6b72410 KH |
318 | mode-line-format 2C-mode-line-format |
319 | 2C-mode other) | |
320 | (run-hooks '2C-mode-hook)) | |
321 | ||
322 | ||
72c0ae01 | 323 | |
e6b72410 KH |
324 | ;;;###autoload |
325 | (defun 2C-two-columns (&optional buffer) | |
326 | "Split current window vertically for two-column editing. | |
408f1b9d | 327 | \\<global-map>When called the first time, associates a buffer with the current |
02de2661 RS |
328 | buffer in two-column minor mode (use \\[describe-mode] once in the mode, |
329 | for details.). It runs `2C-other-buffer-hook' in the new buffer. | |
e6b72410 | 330 | When called again, restores the screen layout with the current buffer |
17c3470e | 331 | first and the associated buffer to its right." |
72c0ae01 | 332 | (interactive "P") |
e6b72410 | 333 | ;; first go to full width, so that we can certainly split into two windows |
eaccbbd3 MR |
334 | (unless (window-full-width-p) |
335 | (enlarge-window 99999 t)) | |
72c0ae01 | 336 | (split-window-horizontally |
e6b72410 | 337 | (max window-min-width (min 2C-window-width |
3bcbd523 | 338 | (- (frame-width) window-min-width)))) |
e6b72410 | 339 | (if (2C-other) |
72c0ae01 ER |
340 | (progn |
341 | (other-window 1) | |
e6b72410 | 342 | (switch-to-buffer (2C-other)) |
72c0ae01 | 343 | (other-window -1) |
e6b72410 KH |
344 | (if 2C-autoscroll |
345 | (2C-toggle-autoscroll t))) | |
346 | ||
347 | (2C-mode (prog1 (point-marker) | |
348 | (other-window 1) | |
349 | (switch-to-buffer | |
350 | (or buffer | |
8fe9ecef | 351 | (generate-new-buffer (concat "2C/" (buffer-name))))) |
e6b72410 KH |
352 | (or buffer |
353 | (run-hooks '2C-other-buffer-hook)))) | |
eaae8106 | 354 | |
e6b72410 KH |
355 | (2C-mode (prog1 (point-marker) |
356 | (other-window -1))))) | |
357 | ||
358 | ||
72c0ae01 | 359 | |
6f5191a2 | 360 | ;;;###autoload |
e6b72410 | 361 | (defun 2C-associate-buffer () |
72c0ae01 ER |
362 | "Associate another buffer with this one in two-column minor mode. |
363 | Can also be used to associate a just previously visited file, by | |
364 | accepting the proposed default buffer. | |
365 | ||
e6b72410 | 366 | \(See \\[describe-mode] .)" |
72c0ae01 ER |
367 | (interactive) |
368 | (let ((b1 (current-buffer)) | |
e6b72410 | 369 | (b2 (or (2C-other) |
72c0ae01 ER |
370 | (read-buffer "Associate buffer: " (other-buffer))))) |
371 | (save-excursion | |
e6b72410 | 372 | (setq 2C-mode nil) |
72c0ae01 | 373 | (set-buffer b2) |
e6b72410 KH |
374 | (and (2C-other) |
375 | (not (eq b1 (2C-other))) | |
e8af40ee | 376 | (error "Buffer already associated with buffer `%s'" |
e6b72410 KH |
377 | (buffer-name (2C-other)))) |
378 | (setq b1 (and (assq '2C-window-width (buffer-local-variables)) | |
379 | 2C-window-width))) | |
72c0ae01 | 380 | ; if other buffer has a local width, adjust here too |
e6b72410 KH |
381 | (if b1 (setq 2C-window-width (- (frame-width) b1))) |
382 | (2C-two-columns b2))) | |
383 | ||
384 | ||
72c0ae01 | 385 | |
6f5191a2 | 386 | ;;;###autoload |
e6b72410 KH |
387 | (defun 2C-split (arg) |
388 | "Split a two-column text at point, into two buffers in two-column minor mode. | |
389 | Point becomes the local value of `2C-window-width'. Only lines that | |
390 | have the ARG same preceding characters at that column get split. The | |
391 | ARG preceding characters without any leading whitespace become the local | |
392 | value for `2C-separator'. This way lines that continue across both | |
72c0ae01 ER |
393 | columns remain untouched in the first buffer. |
394 | ||
e6b72410 KH |
395 | This function can be used with a prototype line, to set up things. You |
396 | write the first line of each column and then split that line. E.g.: | |
72c0ae01 | 397 | |
e6b72410 | 398 | First column's text sSs Second column's text |
72c0ae01 ER |
399 | \\___/\\ |
400 | / \\ | |
e6b72410 | 401 | 5 character Separator You type M-5 \\[2C-split] with the point here. |
72c0ae01 | 402 | |
e6b72410 KH |
403 | \(See \\[describe-mode] .)" |
404 | (interactive "*p") | |
405 | (and (2C-other) | |
72c0ae01 | 406 | (if (y-or-n-p (concat "Overwrite associated buffer `" |
e6b72410 | 407 | (buffer-name (2C-other)) |
72c0ae01 | 408 | "'? ")) |
9a529312 | 409 | (with-current-buffer (2C-other) |
72c0ae01 ER |
410 | (erase-buffer)) |
411 | (signal 'quit nil))) | |
412 | (let ((point (point)) | |
466c6fcc CY |
413 | ;; make next-line always come back to same column |
414 | (column (current-column)) | |
415 | ;; a counter for empty lines in other buffer | |
72c0ae01 ER |
416 | (n (1- (count-lines (point-min) (point)))) |
417 | chars other) | |
418 | (save-excursion | |
419 | (backward-char arg) | |
420 | (setq chars (buffer-substring (point) point)) | |
421 | (skip-chars-forward " \t" point) | |
e6b72410 KH |
422 | (make-local-variable '2C-separator) |
423 | (setq 2C-separator (buffer-substring (point) point) | |
466c6fcc CY |
424 | 2C-window-width (+ (fringe-columns 'left) |
425 | (fringe-columns 'right) | |
426 | (scroll-bar-columns 'left) | |
427 | (scroll-bar-columns 'right) | |
428 | (current-column)))) | |
e6b72410 KH |
429 | (2C-two-columns) |
430 | (setq other (2C-other)) | |
466c6fcc | 431 | ;; now we're ready to actually split |
72c0ae01 ER |
432 | (save-excursion |
433 | (while (not (eobp)) | |
466c6fcc | 434 | (if (not (and (= (current-column) column) |
72c0ae01 ER |
435 | (string= chars |
436 | (buffer-substring (point) | |
437 | (save-excursion | |
438 | (backward-char arg) | |
439 | (point)))))) | |
440 | (setq n (1+ n)) | |
441 | (setq point (point)) | |
442 | (backward-char arg) | |
443 | (skip-chars-backward " \t") | |
444 | (delete-region point (point)) | |
445 | (setq point (point)) | |
446 | (insert-char ?\n n) | |
447 | (append-to-buffer other point (progn (end-of-line) | |
448 | (if (eobp) | |
449 | (point) | |
450 | (1+ (point))))) | |
451 | (delete-region point (point)) | |
452 | (setq n 0)) | |
466c6fcc CY |
453 | (forward-line 1) |
454 | (move-to-column column))))) | |
72c0ae01 | 455 | |
e6b72410 KH |
456 | |
457 | ||
458 | ||
459 | (defun 2C-dissociate () | |
72c0ae01 ER |
460 | "Turn off two-column minor mode in current and associated buffer. |
461 | If the associated buffer is unmodified and empty, it is killed." | |
462 | (interactive) | |
463 | (let ((buffer (current-buffer))) | |
464 | (save-excursion | |
e6b72410 KH |
465 | (and (2C-other) |
466 | (set-buffer (2C-other)) | |
467 | (or (not (2C-other)) | |
468 | (eq buffer (2C-other))) | |
72c0ae01 ER |
469 | (if (and (not (buffer-modified-p)) |
470 | (eobp) (bobp)) | |
471 | (kill-buffer nil) | |
e6b72410 KH |
472 | (kill-local-variable '2C-mode) |
473 | (kill-local-variable '2C-window-width) | |
474 | (kill-local-variable '2C-separator) | |
72c0ae01 ER |
475 | (kill-local-variable 'mode-line-format) |
476 | (kill-local-variable 'fill-column)))) | |
e6b72410 KH |
477 | (kill-local-variable '2C-mode) |
478 | (kill-local-variable '2C-window-width) | |
479 | (kill-local-variable '2C-separator) | |
72c0ae01 ER |
480 | (kill-local-variable 'mode-line-format) |
481 | (kill-local-variable 'fill-column))) | |
482 | ||
483 | ||
e6b72410 | 484 | |
72c0ae01 ER |
485 | ;; this doesn't use yank-rectangle, so that the first column can |
486 | ;; contain long lines | |
e6b72410 | 487 | (defun 2C-merge () |
72c0ae01 | 488 | "Merges the associated buffer with the current buffer. |
e6b72410 KH |
489 | They get merged at the column, which is the value of `2C-window-width', |
490 | i.e. usually at the vertical window separator. This separator gets | |
491 | replaced with white space. Beyond that the value of `2C-separator' gets | |
492 | inserted on merged lines. The two columns are thus pasted side by side, | |
493 | in a single text. If the other buffer is not displayed to the left of | |
494 | this one, then this one becomes the left column. | |
495 | ||
496 | If you want `2C-separator' on empty lines in the second column, | |
72c0ae01 | 497 | you should put just one space in them. In the final result, you can strip |
eaae8106 | 498 | off trailing spaces with \\[delete-trailing-whitespace]." |
72c0ae01 | 499 | (interactive) |
72c0ae01 ER |
500 | (and (> (car (window-edges)) 0) ; not touching left edge of screen |
501 | (eq (window-buffer (previous-window)) | |
8fe9ecef | 502 | (2C-other t)) |
72c0ae01 ER |
503 | (other-window -1)) |
504 | (save-excursion | |
505 | (let ((b1 (current-buffer)) | |
8fe9ecef | 506 | (b2 (2C-other t)) |
72c0ae01 ER |
507 | string) |
508 | (goto-char (point-min)) | |
509 | (set-buffer b2) | |
510 | (goto-char (point-min)) | |
511 | (while (not (eobp)) | |
512 | (setq string (buffer-substring (point) | |
513 | (progn (end-of-line) (point)))) | |
514 | (or (eobp) | |
515 | (forward-char)) ; next line | |
516 | (set-buffer b1) | |
517 | (if (string= string "") | |
518 | () | |
519 | (end-of-line) | |
e6b72410 KH |
520 | (indent-to-column 2C-window-width) |
521 | (insert 2C-separator string)) | |
7caf6803 | 522 | (forward-line 1) ; add one if necessary |
72c0ae01 | 523 | (set-buffer b2)))) |
eaccbbd3 MR |
524 | (unless (window-full-width-p) |
525 | (enlarge-window 99999 t))) | |
72c0ae01 ER |
526 | \f |
527 | ;;;;; utility functions ;;;;; | |
528 | ||
e6b72410 | 529 | (defun 2C-associated-buffer () |
72c0ae01 ER |
530 | "Switch to associated buffer." |
531 | (interactive) | |
8fe9ecef KH |
532 | (let ((line (+ (count-lines (point-min) (point)) |
533 | (if (bolp) 1 0))) | |
534 | (col (if (eolp) (if (bolp) 0) (current-column)))) | |
535 | (if (get-buffer-window (2C-other t)) | |
536 | (select-window (get-buffer-window (2C-other))) | |
537 | (switch-to-buffer (2C-other))) | |
e6ce8c42 GM |
538 | (goto-char (point-min)) |
539 | (newline (forward-line (1- line))) | |
8fe9ecef KH |
540 | (if col |
541 | (move-to-column col) | |
542 | (end-of-line 1)))) | |
543 | ||
544 | (defun 2C-newline (arg) | |
545 | "Insert ARG newlines in both buffers." | |
546 | (interactive "P") | |
547 | (save-window-excursion | |
548 | (2C-associated-buffer) | |
549 | (newline arg)) | |
550 | (newline arg)) | |
72c0ae01 | 551 | |
e6b72410 | 552 | (defun 2C-toggle-autoscroll (arg) |
3ecd3a56 GM |
553 | "Toggle autoscrolling. |
554 | With prefix argument ARG, turn on autoscrolling if ARG is | |
555 | positive, otherwise turn it off. When autoscrolling is turned | |
556 | on, this also realigns the two buffers." | |
72c0ae01 | 557 | (interactive "P") |
8fe9ecef | 558 | ;(sit-for 0) |
e6b72410 KH |
559 | (setq 2C-autoscroll-start (window-start)) |
560 | (if (setq 2C-autoscroll (if arg | |
561 | (>= (prefix-numeric-value arg) 0) | |
562 | (not 2C-autoscroll))) | |
563 | (select-window | |
564 | (prog1 (selected-window) | |
565 | (message "Autoscrolling is on.") | |
e6b72410 | 566 | (setq arg (count-lines (point-min) (window-start))) |
8fe9ecef | 567 | (if (get-buffer-window (2C-other t)) |
e6b72410 KH |
568 | (progn |
569 | (select-window (get-buffer-window (2C-other))) | |
570 | (setq arg (- arg (count-lines (point-min) (window-start)))) | |
571 | ;; make sure that other buffer has enough lines | |
572 | (save-excursion | |
573 | (insert-char ?\n | |
574 | (- arg (count-lines (window-start) | |
575 | (goto-char (point-max))) | |
576 | -1))) | |
577 | (scroll-up arg))))) | |
578 | (message "Autoscrolling is off."))) | |
579 | ||
580 | ||
581 | ||
582 | (defun 2C-autoscroll () | |
583 | (if 2C-autoscroll | |
584 | ;; catch a mouse scroll on non-selected scrollbar | |
585 | (select-window | |
586 | (prog1 (selected-window) | |
1ba983e8 | 587 | (and (consp last-command-event) |
e6b72410 | 588 | (not (eq (selected-window) |
1ba983e8 GM |
589 | (car (car (cdr last-command-event))))) |
590 | (select-window (car (car (cdr last-command-event))))) | |
e6b72410 KH |
591 | ;; In some cases scrolling causes an error, but post-command-hook |
592 | ;; shouldn't, and should always stay in the original window | |
593 | (condition-case () | |
594 | (and (or 2C-autoscroll-start (2C-toggle-autoscroll t) nil) | |
595 | (/= (window-start) 2C-autoscroll-start) | |
596 | (2C-other) | |
597 | (get-buffer-window (2C-other)) | |
598 | (let ((lines (count-lines (window-start) | |
599 | 2C-autoscroll-start))) | |
600 | (if (< (window-start) 2C-autoscroll-start) | |
601 | (setq lines (- lines))) | |
602 | (setq 2C-autoscroll-start (window-start)) | |
603 | (select-window (get-buffer-window (2C-other))) | |
604 | ;; make sure that other buffer has enough lines | |
605 | (save-excursion | |
606 | (insert-char | |
607 | ?\n (- lines (count-lines (window-start) | |
608 | (goto-char (point-max))) | |
609 | -1))) | |
610 | (scroll-up lines) | |
611 | (setq 2C-autoscroll-start (window-start)))) | |
612 | (error)))))) | |
613 | ||
614 | ||
72c0ae01 | 615 | |
30a7d33c | 616 | (defun 2C-enlarge-window-horizontally (arg) |
72c0ae01 ER |
617 | "Make current window ARG columns wider." |
618 | (interactive "p") | |
619 | (enlarge-window arg t) | |
e6b72410 KH |
620 | (and (2C-other) |
621 | (setq 2C-window-width (+ 2C-window-width arg)) | |
622 | (set-buffer (2C-other)) | |
623 | (setq 2C-window-width (- 2C-window-width arg)))) | |
72c0ae01 | 624 | |
30a7d33c | 625 | (defun 2C-shrink-window-horizontally (arg) |
72c0ae01 ER |
626 | "Make current window ARG columns narrower." |
627 | (interactive "p") | |
30a7d33c | 628 | (2C-enlarge-window-horizontally (- arg))) |
72c0ae01 | 629 | |
e6b72410 KH |
630 | |
631 | ||
3bcbd523 RS |
632 | (provide 'two-column) |
633 | ||
72c0ae01 | 634 | ;;; two-column.el ends here |