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