Commit | Line | Data |
---|---|---|
72c0ae01 ER |
1 | ;;; two-column.el --- minor mode for editing of two-column text |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 1992-1995, 2001-2013 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: | |
1197ecfa GM |
127 | (defgroup two-column nil |
128 | "Minor mode for editing of two-column text." | |
129 | :prefix "2C-" | |
130 | :group 'frames) | |
131 | ||
132 | (defcustom 2C-mode-line-format | |
133 | '("-%*- %15b --" (-3 . "%p") "--%[(" mode-name | |
134 | minor-mode-alist "%n" mode-line-process ")%]%-") | |
135 | "Value of `mode-line-format' for a buffer in two-column minor mode." | |
136 | :type 'sexp | |
137 | :group 'two-column) | |
138 | ||
139 | (defcustom 2C-other-buffer-hook 'text-mode | |
140 | "Hook run in new buffer when it is associated with current one." | |
141 | :type 'function | |
142 | :group 'two-column) | |
e6b72410 | 143 | |
1197ecfa GM |
144 | (defcustom 2C-separator "" |
145 | "A string inserted between the two columns when merging. | |
146 | This gets set locally by \\[2C-split]." | |
147 | :type 'string | |
148 | :group 'two-column) | |
149 | (put '2C-separator 'permanent-local t) | |
150 | ||
151 | (defcustom 2C-window-width 40 | |
152 | "The width of the first column. (Must be at least `window-min-width'.) | |
153 | This value is local for every buffer that sets it." | |
154 | :type 'integer | |
155 | :group 'two-column) | |
156 | (make-variable-buffer-local '2C-window-width) | |
157 | (put '2C-window-width 'permanent-local t) | |
e6b72410 | 158 | |
1197ecfa GM |
159 | (defcustom 2C-beyond-fill-column 4 |
160 | "Base for calculating `fill-column' for a buffer in two-column minor mode. | |
161 | The value of `fill-column' becomes `2C-window-width' for this buffer | |
162 | minus this value." | |
163 | :type 'integer | |
164 | :group 'two-column) | |
e6b72410 | 165 | |
1197ecfa GM |
166 | (defcustom 2C-autoscroll t |
167 | "If non-nil, Emacs attempts to keep the two column's buffers aligned." | |
168 | :type 'boolean | |
169 | :group 'two-column) | |
72c0ae01 | 170 | |
1197ecfa | 171 | \f |
e6b72410 KH |
172 | (defvar 2C-mode-map |
173 | (let ((map (make-sparse-keymap))) | |
174 | (define-key map "2" '2C-two-columns) | |
175 | (define-key map [f2] '2C-two-columns) | |
176 | (define-key map "b" '2C-associate-buffer) | |
177 | (define-key map "s" '2C-split) | |
e6b72410 KH |
178 | map) |
179 | "Keymap for commands for setting up two-column mode.") | |
180 | ||
e6b72410 KH |
181 | ;;;###autoload (autoload '2C-command "two-column" () t 'keymap) |
182 | (fset '2C-command 2C-mode-map) | |
72c0ae01 | 183 | |
e6b72410 KH |
184 | ;; This one is for historical reasons and simple keyboards, it is not |
185 | ;; at all mnemonic. All usual sequences containing 2 were used, and | |
186 | ;; f2 could not be set up in a standard way under Emacs 18. | |
2c02e313 | 187 | ;;;###autoload (global-set-key "\C-x6" '2C-command) |
e6b72410 | 188 | |
2c02e313 | 189 | ;;;###autoload (global-set-key [f2] '2C-command) |
e6b72410 | 190 | |
e6b72410 KH |
191 | (defvar 2C-minor-mode-map |
192 | (let ((map (make-sparse-keymap))) | |
193 | (define-key map "1" '2C-merge) | |
194 | (define-key map "d" '2C-dissociate) | |
195 | (define-key map "o" '2C-associated-buffer) | |
8fe9ecef | 196 | (define-key map "\^m" '2C-newline) |
e6b72410 | 197 | (define-key map "|" '2C-toggle-autoscroll) |
30a7d33c KH |
198 | (define-key map "{" '2C-shrink-window-horizontally) |
199 | (define-key map "}" '2C-enlarge-window-horizontally) | |
e6b72410 KH |
200 | map) |
201 | "Keymap for commands for use in two-column mode.") | |
202 | ||
e6b72410 KH |
203 | (setq minor-mode-map-alist |
204 | (cons (cons '2C-mode | |
205 | (let ((map (make-sparse-keymap))) | |
206 | (substitute-key-definition '2C-command 2C-minor-mode-map | |
207 | map (current-global-map)) | |
30a7d33c KH |
208 | (substitute-key-definition 'enlarge-window-horizontally |
209 | '2C-enlarge-window-horizontally | |
210 | map (current-global-map)) | |
211 | (substitute-key-definition 'shrink-window-horizontally | |
212 | '2C-shrink-window-horizontally | |
213 | map (current-global-map)) | |
e6b72410 KH |
214 | map)) |
215 | minor-mode-map-alist)) | |
ded3e3d8 | 216 | |
1197ecfa | 217 | \f |
e6b72410 KH |
218 | ;; Markers seem to be the only buffer-id not affected by renaming a buffer. |
219 | ;; This nevertheless loses when a buffer is killed. The variable-name is | |
220 | ;; required by `describe-mode'. | |
221 | (defvar 2C-mode nil | |
72c0ae01 | 222 | "Marker to the associated buffer, if non-nil.") |
e6b72410 KH |
223 | (make-variable-buffer-local '2C-mode) |
224 | (put '2C-mode 'permanent-local t) | |
225 | ||
e6b72410 KH |
226 | (setq minor-mode-alist (cons '(2C-mode " 2C") minor-mode-alist)) |
227 | ||
e6b72410 KH |
228 | (defvar 2C-autoscroll-start nil) |
229 | (make-variable-buffer-local '2C-autoscroll-start) | |
72c0ae01 ER |
230 | \f |
231 | ;;;;; base functions ;;;;; | |
232 | ||
e6b72410 | 233 | ;; The access method for the other buffer. This tries to remedy against |
72c0ae01 | 234 | ;; lost local variables and lost buffers. |
8fe9ecef KH |
235 | (defun 2C-other (&optional req) |
236 | (or (if 2C-mode | |
237 | (or (prog1 | |
238 | (marker-buffer 2C-mode) | |
239 | (setq mode-line-format 2C-mode-line-format)) | |
240 | ;; The associated buffer somehow got killed. | |
241 | (progn | |
242 | ;; The other variables may later be useful if the user | |
243 | ;; reestablishes the association. | |
244 | (kill-local-variable '2C-mode) | |
245 | (kill-local-variable 'mode-line-format) | |
246 | nil))) | |
e8af40ee | 247 | (if req (error "You must first set two-column minor mode")))) |
72c0ae01 | 248 | |
72c0ae01 | 249 | |
e6b72410 KH |
250 | ;; function for setting up two-column minor mode in a buffer associated |
251 | ;; with the buffer pointed to by the marker other. | |
252 | (defun 2C-mode (other) | |
253 | "Minor mode for independently editing two columns. | |
254 | This is set up for two associated buffers by the three commands bound | |
255 | to \\[2C-two-columns] , \\[2C-associate-buffer] and \\[2C-split]. | |
256 | Turning on two-column mode calls the value of the variable `2C-mode-hook', | |
257 | if that value is non-nil. | |
258 | ||
259 | These buffers can be edited separately, for example with `fill-paragraph'. | |
260 | If you want to disable parallel scrolling temporarily, use \\[2C-toggle-autoscroll] . | |
72c0ae01 | 261 | |
65e7ca35 PE |
262 | If you include long lines that span both columns (e.g., source |
263 | code), they should be in what will be the first column, with the | |
264 | associated buffer having empty lines next to them. | |
72c0ae01 | 265 | |
e6b72410 KH |
266 | Potential uses are writing bilingual texts, or editing the comments of a |
267 | source code. See the file lisp/two-column.el for detailed examples. | |
268 | ||
72c0ae01 ER |
269 | You have the following commands at your disposal: |
270 | ||
e6b72410 KH |
271 | \\[2C-two-columns] Rearrange screen with current buffer first |
272 | \\[2C-associate-buffer] Reassociate buffer after changing major mode | |
72c0ae01 | 273 | \\[shrink-window-horizontally], \\[enlarge-window-horizontally] Shrink, enlarge current column |
8fe9ecef KH |
274 | \\[2C-associated-buffer] Switch to associated buffer at same point |
275 | \\[2C-newline] Insert newline(s) in both buffers at same point | |
e6b72410 KH |
276 | \\[2C-merge] Merge both buffers |
277 | \\[2C-dissociate] Dissociate the two buffers | |
72c0ae01 | 278 | |
865fe16f CY |
279 | These keybindings can be customized in your init file by |
280 | `2C-mode-map', `2C-minor-mode-map' and by binding `2C-command' to | |
281 | some prefix. | |
72c0ae01 ER |
282 | |
283 | The appearance of the screen can be customized by the variables | |
e6b72410 KH |
284 | `2C-window-width', `2C-beyond-fill-column', `2C-mode-line-format' and |
285 | `truncate-partial-width-windows'." | |
5181be3a | 286 | (add-hook 'post-command-hook '2C-autoscroll nil t) |
e6b72410 KH |
287 | (setq fill-column (- 2C-window-width |
288 | 2C-beyond-fill-column) | |
e6b72410 KH |
289 | mode-line-format 2C-mode-line-format |
290 | 2C-mode other) | |
291 | (run-hooks '2C-mode-hook)) | |
292 | ||
293 | ||
e6b72410 KH |
294 | ;;;###autoload |
295 | (defun 2C-two-columns (&optional buffer) | |
296 | "Split current window vertically for two-column editing. | |
408f1b9d | 297 | \\<global-map>When called the first time, associates a buffer with the current |
02de2661 RS |
298 | buffer in two-column minor mode (use \\[describe-mode] once in the mode, |
299 | for details.). It runs `2C-other-buffer-hook' in the new buffer. | |
e6b72410 | 300 | When called again, restores the screen layout with the current buffer |
17c3470e | 301 | first and the associated buffer to its right." |
72c0ae01 | 302 | (interactive "P") |
e6b72410 | 303 | ;; first go to full width, so that we can certainly split into two windows |
eaccbbd3 MR |
304 | (unless (window-full-width-p) |
305 | (enlarge-window 99999 t)) | |
2d197ffb | 306 | (split-window-right |
e6b72410 | 307 | (max window-min-width (min 2C-window-width |
3bcbd523 | 308 | (- (frame-width) window-min-width)))) |
e6b72410 | 309 | (if (2C-other) |
72c0ae01 ER |
310 | (progn |
311 | (other-window 1) | |
e6b72410 | 312 | (switch-to-buffer (2C-other)) |
72c0ae01 | 313 | (other-window -1) |
e6b72410 KH |
314 | (if 2C-autoscroll |
315 | (2C-toggle-autoscroll t))) | |
316 | ||
317 | (2C-mode (prog1 (point-marker) | |
318 | (other-window 1) | |
319 | (switch-to-buffer | |
320 | (or buffer | |
8fe9ecef | 321 | (generate-new-buffer (concat "2C/" (buffer-name))))) |
e6b72410 KH |
322 | (or buffer |
323 | (run-hooks '2C-other-buffer-hook)))) | |
eaae8106 | 324 | |
e6b72410 KH |
325 | (2C-mode (prog1 (point-marker) |
326 | (other-window -1))))) | |
327 | ||
328 | ||
6f5191a2 | 329 | ;;;###autoload |
e6b72410 | 330 | (defun 2C-associate-buffer () |
72c0ae01 ER |
331 | "Associate another buffer with this one in two-column minor mode. |
332 | Can also be used to associate a just previously visited file, by | |
333 | accepting the proposed default buffer. | |
334 | ||
e6b72410 | 335 | \(See \\[describe-mode] .)" |
72c0ae01 ER |
336 | (interactive) |
337 | (let ((b1 (current-buffer)) | |
e6b72410 | 338 | (b2 (or (2C-other) |
72c0ae01 | 339 | (read-buffer "Associate buffer: " (other-buffer))))) |
1197ecfa GM |
340 | (setq 2C-mode nil) |
341 | (with-current-buffer b2 | |
e6b72410 KH |
342 | (and (2C-other) |
343 | (not (eq b1 (2C-other))) | |
e8af40ee | 344 | (error "Buffer already associated with buffer `%s'" |
e6b72410 KH |
345 | (buffer-name (2C-other)))) |
346 | (setq b1 (and (assq '2C-window-width (buffer-local-variables)) | |
347 | 2C-window-width))) | |
72c0ae01 | 348 | ; if other buffer has a local width, adjust here too |
e6b72410 KH |
349 | (if b1 (setq 2C-window-width (- (frame-width) b1))) |
350 | (2C-two-columns b2))) | |
351 | ||
352 | ||
6f5191a2 | 353 | ;;;###autoload |
e6b72410 KH |
354 | (defun 2C-split (arg) |
355 | "Split a two-column text at point, into two buffers in two-column minor mode. | |
356 | Point becomes the local value of `2C-window-width'. Only lines that | |
357 | have the ARG same preceding characters at that column get split. The | |
358 | ARG preceding characters without any leading whitespace become the local | |
359 | value for `2C-separator'. This way lines that continue across both | |
72c0ae01 ER |
360 | columns remain untouched in the first buffer. |
361 | ||
e6b72410 KH |
362 | This function can be used with a prototype line, to set up things. You |
363 | write the first line of each column and then split that line. E.g.: | |
72c0ae01 | 364 | |
e6b72410 | 365 | First column's text sSs Second column's text |
72c0ae01 ER |
366 | \\___/\\ |
367 | / \\ | |
e6b72410 | 368 | 5 character Separator You type M-5 \\[2C-split] with the point here. |
72c0ae01 | 369 | |
e6b72410 KH |
370 | \(See \\[describe-mode] .)" |
371 | (interactive "*p") | |
372 | (and (2C-other) | |
72c0ae01 | 373 | (if (y-or-n-p (concat "Overwrite associated buffer `" |
e6b72410 | 374 | (buffer-name (2C-other)) |
72c0ae01 | 375 | "'? ")) |
9a529312 | 376 | (with-current-buffer (2C-other) |
72c0ae01 ER |
377 | (erase-buffer)) |
378 | (signal 'quit nil))) | |
379 | (let ((point (point)) | |
466c6fcc CY |
380 | ;; make next-line always come back to same column |
381 | (column (current-column)) | |
382 | ;; a counter for empty lines in other buffer | |
72c0ae01 ER |
383 | (n (1- (count-lines (point-min) (point)))) |
384 | chars other) | |
385 | (save-excursion | |
386 | (backward-char arg) | |
387 | (setq chars (buffer-substring (point) point)) | |
388 | (skip-chars-forward " \t" point) | |
e6b72410 KH |
389 | (make-local-variable '2C-separator) |
390 | (setq 2C-separator (buffer-substring (point) point) | |
466c6fcc CY |
391 | 2C-window-width (+ (fringe-columns 'left) |
392 | (fringe-columns 'right) | |
393 | (scroll-bar-columns 'left) | |
394 | (scroll-bar-columns 'right) | |
395 | (current-column)))) | |
e6b72410 KH |
396 | (2C-two-columns) |
397 | (setq other (2C-other)) | |
466c6fcc | 398 | ;; now we're ready to actually split |
72c0ae01 ER |
399 | (save-excursion |
400 | (while (not (eobp)) | |
466c6fcc | 401 | (if (not (and (= (current-column) column) |
72c0ae01 ER |
402 | (string= chars |
403 | (buffer-substring (point) | |
404 | (save-excursion | |
405 | (backward-char arg) | |
406 | (point)))))) | |
407 | (setq n (1+ n)) | |
408 | (setq point (point)) | |
409 | (backward-char arg) | |
410 | (skip-chars-backward " \t") | |
411 | (delete-region point (point)) | |
412 | (setq point (point)) | |
413 | (insert-char ?\n n) | |
414 | (append-to-buffer other point (progn (end-of-line) | |
415 | (if (eobp) | |
416 | (point) | |
417 | (1+ (point))))) | |
418 | (delete-region point (point)) | |
419 | (setq n 0)) | |
466c6fcc CY |
420 | (forward-line 1) |
421 | (move-to-column column))))) | |
72c0ae01 | 422 | |
e6b72410 | 423 | |
e6b72410 | 424 | (defun 2C-dissociate () |
72c0ae01 ER |
425 | "Turn off two-column minor mode in current and associated buffer. |
426 | If the associated buffer is unmodified and empty, it is killed." | |
427 | (interactive) | |
1197ecfa GM |
428 | (let ((buffer (current-buffer)) |
429 | (other (2C-other))) | |
430 | (if other | |
431 | (with-current-buffer other | |
432 | (when (or (not (2C-other)) (eq buffer (2C-other))) | |
433 | (if (and (not (buffer-modified-p)) (zerop (buffer-size))) | |
434 | (kill-buffer) | |
435 | (kill-local-variable '2C-mode) | |
436 | (kill-local-variable '2C-window-width) | |
437 | (kill-local-variable '2C-separator) | |
438 | (kill-local-variable 'mode-line-format) | |
439 | (kill-local-variable 'fill-column)))))) | |
440 | (kill-local-variable '2C-mode) | |
441 | (kill-local-variable '2C-window-width) | |
442 | (kill-local-variable '2C-separator) | |
443 | (kill-local-variable 'mode-line-format) | |
444 | (kill-local-variable 'fill-column) | |
445 | (force-mode-line-update)) | |
72c0ae01 | 446 | |
e6b72410 | 447 | |
72c0ae01 ER |
448 | ;; this doesn't use yank-rectangle, so that the first column can |
449 | ;; contain long lines | |
e6b72410 | 450 | (defun 2C-merge () |
72c0ae01 | 451 | "Merges the associated buffer with the current buffer. |
e6b72410 KH |
452 | They get merged at the column, which is the value of `2C-window-width', |
453 | i.e. usually at the vertical window separator. This separator gets | |
454 | replaced with white space. Beyond that the value of `2C-separator' gets | |
455 | inserted on merged lines. The two columns are thus pasted side by side, | |
456 | in a single text. If the other buffer is not displayed to the left of | |
457 | this one, then this one becomes the left column. | |
458 | ||
459 | If you want `2C-separator' on empty lines in the second column, | |
72c0ae01 | 460 | you should put just one space in them. In the final result, you can strip |
eaae8106 | 461 | off trailing spaces with \\[delete-trailing-whitespace]." |
72c0ae01 | 462 | (interactive) |
72c0ae01 ER |
463 | (and (> (car (window-edges)) 0) ; not touching left edge of screen |
464 | (eq (window-buffer (previous-window)) | |
8fe9ecef | 465 | (2C-other t)) |
72c0ae01 ER |
466 | (other-window -1)) |
467 | (save-excursion | |
468 | (let ((b1 (current-buffer)) | |
8fe9ecef | 469 | (b2 (2C-other t)) |
72c0ae01 ER |
470 | string) |
471 | (goto-char (point-min)) | |
472 | (set-buffer b2) | |
473 | (goto-char (point-min)) | |
474 | (while (not (eobp)) | |
475 | (setq string (buffer-substring (point) | |
476 | (progn (end-of-line) (point)))) | |
477 | (or (eobp) | |
478 | (forward-char)) ; next line | |
479 | (set-buffer b1) | |
480 | (if (string= string "") | |
481 | () | |
482 | (end-of-line) | |
e6b72410 KH |
483 | (indent-to-column 2C-window-width) |
484 | (insert 2C-separator string)) | |
7caf6803 | 485 | (forward-line 1) ; add one if necessary |
72c0ae01 | 486 | (set-buffer b2)))) |
eaccbbd3 MR |
487 | (unless (window-full-width-p) |
488 | (enlarge-window 99999 t))) | |
72c0ae01 ER |
489 | \f |
490 | ;;;;; utility functions ;;;;; | |
491 | ||
e6b72410 | 492 | (defun 2C-associated-buffer () |
72c0ae01 ER |
493 | "Switch to associated buffer." |
494 | (interactive) | |
8fe9ecef KH |
495 | (let ((line (+ (count-lines (point-min) (point)) |
496 | (if (bolp) 1 0))) | |
497 | (col (if (eolp) (if (bolp) 0) (current-column)))) | |
498 | (if (get-buffer-window (2C-other t)) | |
499 | (select-window (get-buffer-window (2C-other))) | |
500 | (switch-to-buffer (2C-other))) | |
e6ce8c42 GM |
501 | (goto-char (point-min)) |
502 | (newline (forward-line (1- line))) | |
8fe9ecef KH |
503 | (if col |
504 | (move-to-column col) | |
505 | (end-of-line 1)))) | |
506 | ||
507 | (defun 2C-newline (arg) | |
508 | "Insert ARG newlines in both buffers." | |
509 | (interactive "P") | |
510 | (save-window-excursion | |
511 | (2C-associated-buffer) | |
512 | (newline arg)) | |
513 | (newline arg)) | |
72c0ae01 | 514 | |
e6b72410 | 515 | (defun 2C-toggle-autoscroll (arg) |
3ecd3a56 GM |
516 | "Toggle autoscrolling. |
517 | With prefix argument ARG, turn on autoscrolling if ARG is | |
518 | positive, otherwise turn it off. When autoscrolling is turned | |
519 | on, this also realigns the two buffers." | |
72c0ae01 | 520 | (interactive "P") |
8fe9ecef | 521 | ;(sit-for 0) |
e6b72410 KH |
522 | (setq 2C-autoscroll-start (window-start)) |
523 | (if (setq 2C-autoscroll (if arg | |
524 | (>= (prefix-numeric-value arg) 0) | |
525 | (not 2C-autoscroll))) | |
526 | (select-window | |
527 | (prog1 (selected-window) | |
528 | (message "Autoscrolling is on.") | |
e6b72410 | 529 | (setq arg (count-lines (point-min) (window-start))) |
8fe9ecef | 530 | (if (get-buffer-window (2C-other t)) |
e6b72410 KH |
531 | (progn |
532 | (select-window (get-buffer-window (2C-other))) | |
533 | (setq arg (- arg (count-lines (point-min) (window-start)))) | |
534 | ;; make sure that other buffer has enough lines | |
535 | (save-excursion | |
536 | (insert-char ?\n | |
537 | (- arg (count-lines (window-start) | |
538 | (goto-char (point-max))) | |
539 | -1))) | |
540 | (scroll-up arg))))) | |
541 | (message "Autoscrolling is off."))) | |
542 | ||
543 | ||
e6b72410 KH |
544 | (defun 2C-autoscroll () |
545 | (if 2C-autoscroll | |
546 | ;; catch a mouse scroll on non-selected scrollbar | |
547 | (select-window | |
548 | (prog1 (selected-window) | |
1ba983e8 | 549 | (and (consp last-command-event) |
e6b72410 | 550 | (not (eq (selected-window) |
1ba983e8 GM |
551 | (car (car (cdr last-command-event))))) |
552 | (select-window (car (car (cdr last-command-event))))) | |
e6b72410 KH |
553 | ;; In some cases scrolling causes an error, but post-command-hook |
554 | ;; shouldn't, and should always stay in the original window | |
1197ecfa GM |
555 | (ignore-errors |
556 | (and (or 2C-autoscroll-start (2C-toggle-autoscroll t) nil) | |
557 | (/= (window-start) 2C-autoscroll-start) | |
558 | (2C-other) | |
559 | (get-buffer-window (2C-other)) | |
560 | (let ((lines (count-lines (window-start) | |
561 | 2C-autoscroll-start))) | |
562 | (if (< (window-start) 2C-autoscroll-start) | |
563 | (setq lines (- lines))) | |
564 | (setq 2C-autoscroll-start (window-start)) | |
565 | (select-window (get-buffer-window (2C-other))) | |
566 | ;; make sure that other buffer has enough lines | |
567 | (save-excursion | |
568 | (insert-char | |
569 | ?\n (- lines (count-lines (window-start) | |
570 | (goto-char (point-max))) | |
571 | -1))) | |
572 | (scroll-up lines) | |
573 | (setq 2C-autoscroll-start (window-start))))))))) | |
e6b72410 | 574 | |
72c0ae01 | 575 | |
30a7d33c | 576 | (defun 2C-enlarge-window-horizontally (arg) |
72c0ae01 ER |
577 | "Make current window ARG columns wider." |
578 | (interactive "p") | |
579 | (enlarge-window arg t) | |
e6b72410 KH |
580 | (and (2C-other) |
581 | (setq 2C-window-width (+ 2C-window-width arg)) | |
582 | (set-buffer (2C-other)) | |
583 | (setq 2C-window-width (- 2C-window-width arg)))) | |
72c0ae01 | 584 | |
30a7d33c | 585 | (defun 2C-shrink-window-horizontally (arg) |
72c0ae01 ER |
586 | "Make current window ARG columns narrower." |
587 | (interactive "p") | |
30a7d33c | 588 | (2C-enlarge-window-horizontally (- arg))) |
72c0ae01 | 589 | |
e6b72410 | 590 | |
3bcbd523 RS |
591 | (provide 'two-column) |
592 | ||
72c0ae01 | 593 | ;;; two-column.el ends here |