1 ;;; two-column.el --- minor mode for editing of two-column text
3 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
6 ;; Adapted-By: ESR, Daniel Pfeiffer
10 ;; ^Ci dosiero estas ero de GNU Emacs. This file is part of GNU Emacs.
12 ;; GNU Emacs estas libera programaro; GNU Emacs is free software; you can
13 ;; vi povas disdoni ^gin kaj/a^u modifi redistribute it and/or modify it
14 ;; ^gin sub la kondi^coj de la GNU under the terms of the GNU General
15 ;; ^Generala Publika Licenco kiel pub- Public License as published by the
16 ;; likigita far la Liberprogramara Fon- Free Software Foundation; either
17 ;; da^jo; a^u eldono 2a, a^u (la^u via version 2, or (at your option) any
18 ;; elekto) ajna posta eldono. later version.
20 ;; GNU Emacs estas disdonata en la GNU Emacs is distributed in the hope
21 ;; espero ke ^gi estos utila, sed SEN that it will be useful, but WITHOUT
22 ;; IA GARANTIO; sen e^c la implicita ANY WARRANTY; without even the
23 ;; garantio de VENDEBLECO a^u PRETECO implied warranty of MERCHANTABILITY
24 ;; POR DETERMINITA CELO. Vidu la GNU or FITNESS FOR A PARTICULAR PURPOSE.
25 ;; ^Generala Publika Licenco por plenaj See the GNU General Public License
26 ;; detaloj. for more details.
28 ;; Vi devus ricevinti kopion de la GNU You should have received a copy of
29 ;; ^Generala Publika Licenco kune kun the GNU General Public License along
30 ;; GNU Emacs; vidu la dosieron COPYING. with GNU Emacs; see the file
31 ;; Alikaze skribu al la COPYING. If not, write to the
33 ;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
36 ;;; Komentario: Commentary:
38 ;; Tiu programaro ebligas vin redakti This package gives you the ability
39 ;; dukolumnan tekston. to edit text in a two-column format.
42 ;; Vi havas tri eblecojn por eki tiun You have three ways to start up this
43 ;; mal^cefan modalon. ^Ciu donas al vi minor mode. Each gives you a
44 ;; horizontale disigatan fenestron, si- horizontally split window similar to
45 ;; milan al fina apareco de via teksto: the final outcome of your text:
48 ;; f2 2 asocias novan bufron nomatan associates a new buffer called
49 ;; C-x 6 2 same, sed kun 2C/ anta^u. the same, but with 2C/
52 ;; f2 b asocias alian bufron. Vi povas associates another buffer.
53 ;; C-x 6 b anka^u asocii dataron, se vi This can be used to associate a
54 ;; ^jus anta^ue faris C-x C-f. file if you just did C-x C-f.
56 ;; f2 s disigas jam dukolumnan tekston splits a two-column text into
57 ;; C-x 6 s en du bufroj ekde la nuna two buffers from the current
58 ;; linio, kaj je la nuna kolumno. line and at the current column.
59 ;; La anta^uaj signoj (ofte The preceding characters (often
60 ;; tabeligilo a^u |) estas la tab or |) are the column
61 ;; kolumna disiganto. Linioj kiuj separator. Lines that don't
62 ;; ne enhavas ilin ne estas have them won't be separated.
63 ;; disigitaj. Kiel la kvara kaj Like the fourth and fifth line
64 ;; la kvina linio se vi disigas if you split this file from
65 ;; ^ci dataron ekde la unua angla the first english word.
68 ;; Se vi volas meti longajn liniojn If you include long lines, i.e which
69 ;; (ekz. programerojn) en la kunigotan will span both columns (eg. source
70 ;; tekston, ili devas esti en la code), they should be in what will
71 ;; estonte unua kolumno. La alia devas be the first column, with the
72 ;; havi vakajn linion apud ili. associated buffer having empty lines
75 ;; Averto: en Emacs kiam vi ^san^gas la Attention: in Emacs when you change
76 ;; ^cefan modalon, la mal^cefaj modaloj the major mode, the minor modes are
77 ;; estas anka^u elmemorigitaj. Tiu- also purged from memory. In that
78 ;; okaze vi devas religi la du bufrojn case you must reassociate the two
79 ;; per iu C-x 6-ordono, ekz. C-x 6 b. buffers with any C-x 6-command, e.g.
82 ;; Kiam vi estos kontenta de la When you have edited both buffers to
83 ;; rezulto, vi kunmetos la du kolumnojn your content, you merge them with
84 ;; per C-x 6 1. Se vi poste vidas C-x 6 1. If you then see a problem,
85 ;; problemon, vi neniigu la kunmeton you undo the merge with C-x u and
86 ;; per C-x u kaj plue modifu la du continue to edit the two buffers.
87 ;; bufrojn. Kiam vi ne plu volas tajpi When you no longer want to edit in
88 ;; dukolumne, vi eliru el la mal^cefa two columns, you turn off the minor
89 ;; modalo per C-x 6 d. mode with C-x 6 d.
92 ;; Aldone al dukolumna redaktado, ek- In addition to two-column editing of
93 ;; zemple por skribi dulingvan tekston text, for example for writing a
94 ;; flank-al-flanke kiel ^ci tiu, aliaj bilingual text side-by-side as shown
95 ;; interesaj uzoj trovitas por tiu mal- here, other interesting uses have
96 ;; ^cefa modalo: been found for this minor mode:
98 ;; Vi povas disigi la kolumnojn per {+} You can separate the columns with
99 ;; ajna pla^ca ^ceno starigante {+} any string that pleases you, by
100 ;; `2C-separator'. Ekzemple "{+} " {+} setting `2C-separator'. For example
101 ;; por amuzi^gi. f2 s a^u C-x 6 s {+} "{+} " if you'd like to have fun.
102 ;; traktas tiujn kun prefiksa {+} f2 s or C-x 6 s handles these with a
103 ;; argumento kiu signifas la longon {+} prefix argument that means the
104 ;; de tia ^ceno. {+} desired length of such a string.
107 ;; Programistoj eble ^satus la eblecon Programmers might like the ability
108 ;; forspliti la komentarian kolumnon de to split off the comment column of a
109 ;; dosiero kiel la sekvanta. Vi povas file that looks like the following.
110 ;; rearan^gigi la paragrafon. La pro- You can fill-paragraph the comment.
111 ;; blemo estas ke koda^jo tuj lar- The problem is, code quickly gets
112 ;; ^gi^gas, tiel ke vi bezonas pli rather wide, so you need to use a
113 ;; mallar^gan komentarian kolumnon. narrower comment column. Code lines
114 ;; Koda^jaj linioj tra `comment-column' that reach beyond `comment-column'
115 ;; ne problemas, krom ke vi ne vidos are no problem, except that you
116 ;; iliajn finojn dum redaktado. won't see their end during editing.
119 ;; BEGIN -- This is just some meaningless
120 ;; FOR i IN 1..10 LOOP -- code in Ada, that runs foobar
121 ;; foobar( i ); -- once for each argument from one
122 ;; END LOOP; -- to ten, and then we're already
123 ;; END; -- through with it.
125 ;; Pli bone ankora^u, vi povas pozici- Better yet, you can put the point
126 ;; i^gi anta^u "This", tajpi M-3 f2 s before "This", type M-3 f2 s
127 ;; kiu igas "-- " la separigilon inter which makes "-- " the separator
128 ;; senkomentaria Ada bufro kaj nur- between a no-comments Ada buffer,
129 ;; teksta komentaria bufro. Kiam vi and a plain text comment buffer.
130 ;; denove kuni^gos ilin, ^ciu nevaka When you put them back together,
131 ;; linio de l' dua kolumno denove every non-empty line of the 2nd
132 ;; anta^uhavos "-- ". column will again be preceded by
140 (or (fboundp 'frame-width)
141 (fset 'frame-width 'screen-width))
144 ;;;;; Set up keymap ;;;;;
147 (let ((map (make-sparse-keymap)))
148 (define-key map "2" '2C-two-columns)
149 (define-key map [f2] '2C-two-columns)
150 (define-key map "b" '2C-associate-buffer)
151 (define-key map "s" '2C-split)
152 (define-key map "{" 'shrink-window-horizontally)
153 (define-key map "}" 'enlarge-window-horizontally)
155 "Keymap for commands for setting up two-column mode.")
159 ;;;###autoload (autoload '2C-command "two-column" () t 'keymap)
160 (fset '2C-command 2C-mode-map)
163 ;; This one is for historical reasons and simple keyboards, it is not
164 ;; at all mnemonic. All usual sequences containing 2 were used, and
165 ;; f2 could not be set up in a standard way under Emacs 18.
166 (global-set-key "\C-x6" '2C-command)
170 (global-set-key [f2] '2C-command)
173 (defvar 2C-minor-mode-map
174 (let ((map (make-sparse-keymap)))
175 (define-key map "1" '2C-merge)
176 (define-key map "d" '2C-dissociate)
177 (define-key map "o" '2C-associated-buffer)
178 (define-key map "|" '2C-toggle-autoscroll)
180 "Keymap for commands for use in two-column mode.")
183 (setq minor-mode-map-alist
185 (let ((map (make-sparse-keymap)))
186 (substitute-key-definition '2C-command 2C-minor-mode-map
187 map (current-global-map))
189 minor-mode-map-alist))
191 ;;;;; variable declarations ;;;;;
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'.
197 "Marker to the associated buffer, if non-nil.")
198 (make-variable-buffer-local '2C-mode)
199 (put '2C-mode 'permanent-local t)
203 (setq minor-mode-alist (cons '(2C-mode " 2C") minor-mode-alist))
207 ;; rearranged, so that the pertinent info will show in 40 columns
208 (defvar 2C-mode-line-format
209 '("-%*- %15b --" (-3 . "%p") "--%[(" mode-name
210 minor-mode-alist "%n" mode-line-process ")%]%-")
211 "*Value of mode-line-format for a buffer in two-column minor mode.")
214 (defvar 2C-other-buffer-hook 'text-mode
215 "*Hook run in new buffer when it is associated with current one.")
218 (defvar 2C-separator ""
219 "*A string inserted between the two columns when merging.
220 This gets set locally by \\[2C-split].")
221 (put '2C-separator 'permanent-local t)
225 (defvar 2C-window-width 40
226 "*The width of the first column. (Must be at least `window-min-width')
227 This value is local for every buffer that sets it.")
228 (make-variable-buffer-local '2C-window-width)
229 (put '2C-window-width 'permanent-local t)
233 (defvar 2C-beyond-fill-column 4
234 "*Base for calculating `fill-column' for a buffer in two-column minor mode.
235 The value of `fill-column' becomes `2C-window-width' for this buffer
240 (defvar 2C-autoscroll t
241 "If this is non-nil, Emacs attempts to keep the two buffers aligned.")
245 (defvar 2C-autoscroll-start nil)
246 (make-variable-buffer-local '2C-autoscroll-start)
248 ;;;;; base functions ;;;;;
250 ;; The access method for the other buffer. This tries to remedy against
251 ;; lost local variables and lost buffers.
255 (marker-buffer 2C-mode)
256 (setq mode-line-format 2C-mode-line-format))
257 ;; The associated buffer somehow got killed.
259 ;; The other variables may later be useful if the user
260 ;; reestablishes the association.
261 (kill-local-variable '2C-mode)
262 (kill-local-variable 'mode-line-format)
267 ;; function for setting up two-column minor mode in a buffer associated
268 ;; with the buffer pointed to by the marker other.
269 (defun 2C-mode (other)
270 "Minor mode for independently editing two columns.
271 This is set up for two associated buffers by the three commands bound
272 to \\[2C-two-columns] , \\[2C-associate-buffer] and \\[2C-split].
273 Turning on two-column mode calls the value of the variable `2C-mode-hook',
274 if that value is non-nil.
276 These buffers can be edited separately, for example with `fill-paragraph'.
277 If you want to disable parallel scrolling temporarily, use \\[2C-toggle-autoscroll] .
279 If you include long lines, i.e which will span both columns (eg.
280 source code), they should be in what will be the first column, with
281 the associated buffer having empty lines next to them.
283 Potential uses are writing bilingual texts, or editing the comments of a
284 source code. See the file lisp/two-column.el for detailed examples.
286 You have the following commands at your disposal:
288 \\[2C-two-columns] Rearrange screen with current buffer first
289 \\[2C-associate-buffer] Reassociate buffer after changing major mode
290 \\[shrink-window-horizontally], \\[enlarge-window-horizontally] Shrink, enlarge current column
291 \\[2C-associated-buffer] Switch to associated buffer
292 \\[2C-merge] Merge both buffers
293 \\[2C-dissociate] Dissociate the two buffers
295 These keybindings can be customized in your ~/.emacs by `2C-mode-map',
296 `2C-minor-mode-map' and by binding `2C-command' to some prefix.
298 The appearance of the screen can be customized by the variables
299 `2C-window-width', `2C-beyond-fill-column', `2C-mode-line-format' and
300 `truncate-partial-width-windows'."
301 (make-variable-buffer-local 'post-command-hook)
302 (setq fill-column (- 2C-window-width
303 2C-beyond-fill-column)
304 post-command-hook '2C-autoscroll
305 mode-line-format 2C-mode-line-format
307 (run-hooks '2C-mode-hook))
312 (defun 2C-two-columns (&optional buffer)
313 "Split current window vertically for two-column editing.
314 When called the first time, associates a buffer with the current
315 buffer in two-column minor mode (see \\[describe-mode] ).
316 Runs `2C-other-buffer-hook' in the new buffer.
317 When called again, restores the screen layout with the current buffer
318 first and the associated buffer to it's right."
320 ;; first go to full width, so that we can certainly split into two windows
321 (if (< (window-width) (frame-width))
322 (enlarge-window 99999 t))
323 (split-window-horizontally
324 (max window-min-width (min 2C-window-width
325 (- (frame-width) window-min-width))))
329 (switch-to-buffer (2C-other))
332 (2C-toggle-autoscroll t)))
334 (2C-mode (prog1 (point-marker)
339 (concat "2C/" (buffer-name)))))
341 (run-hooks '2C-other-buffer-hook))))
343 (2C-mode (prog1 (point-marker)
344 (other-window -1)))))
349 (defun 2C-associate-buffer ()
350 "Associate another buffer with this one in two-column minor mode.
351 Can also be used to associate a just previously visited file, by
352 accepting the proposed default buffer.
354 \(See \\[describe-mode] .)"
356 (let ((b1 (current-buffer))
358 (read-buffer "Associate buffer: " (other-buffer)))))
363 (not (eq b1 (2C-other)))
364 (error "Buffer already associated with buffer `%s'."
365 (buffer-name (2C-other))))
366 (setq b1 (and (assq '2C-window-width (buffer-local-variables))
368 ; if other buffer has a local width, adjust here too
369 (if b1 (setq 2C-window-width (- (frame-width) b1)))
370 (2C-two-columns b2)))
375 (defun 2C-split (arg)
376 "Split a two-column text at point, into two buffers in two-column minor mode.
377 Point becomes the local value of `2C-window-width'. Only lines that
378 have the ARG same preceding characters at that column get split. The
379 ARG preceding characters without any leading whitespace become the local
380 value for `2C-separator'. This way lines that continue across both
381 columns remain untouched in the first buffer.
383 This function can be used with a prototype line, to set up things. You
384 write the first line of each column and then split that line. E.g.:
386 First column's text sSs Second column's text
389 5 character Separator You type M-5 \\[2C-split] with the point here.
391 \(See \\[describe-mode] .)"
394 (if (y-or-n-p (concat "Overwrite associated buffer `"
395 (buffer-name (2C-other))
398 (set-buffer (2C-other))
401 (let ((point (point))
402 ; make next-line always come back to same column
403 (goal-column (current-column))
404 ; a counter for empty lines in other buffer
405 (n (1- (count-lines (point-min) (point))))
409 (setq chars (buffer-substring (point) point))
410 (skip-chars-forward " \t" point)
411 (make-local-variable '2C-separator)
412 (setq 2C-separator (buffer-substring (point) point)
413 2C-window-width (current-column)))
415 (setq other (2C-other))
416 ; now we're ready to actually split
419 (if (not (and (= (current-column) goal-column)
421 (buffer-substring (point)
428 (skip-chars-backward " \t")
429 (delete-region point (point))
432 (append-to-buffer other point (progn (end-of-line)
436 (delete-region point (point))
443 (defun 2C-dissociate ()
444 "Turn off two-column minor mode in current and associated buffer.
445 If the associated buffer is unmodified and empty, it is killed."
447 (let ((buffer (current-buffer)))
450 (set-buffer (2C-other))
452 (eq buffer (2C-other)))
453 (if (and (not (buffer-modified-p))
456 (kill-local-variable '2C-mode)
457 (kill-local-variable '2C-window-width)
458 (kill-local-variable '2C-separator)
459 (kill-local-variable 'mode-line-format)
460 (kill-local-variable 'fill-column))))
461 (kill-local-variable '2C-mode)
462 (kill-local-variable '2C-window-width)
463 (kill-local-variable '2C-separator)
464 (kill-local-variable 'mode-line-format)
465 (kill-local-variable 'fill-column)))
469 ;; this doesn't use yank-rectangle, so that the first column can
470 ;; contain long lines
472 "Merges the associated buffer with the current buffer.
473 They get merged at the column, which is the value of `2C-window-width',
474 i.e. usually at the vertical window separator. This separator gets
475 replaced with white space. Beyond that the value of `2C-separator' gets
476 inserted on merged lines. The two columns are thus pasted side by side,
477 in a single text. If the other buffer is not displayed to the left of
478 this one, then this one becomes the left column.
480 If you want `2C-separator' on empty lines in the second column,
481 you should put just one space in them. In the final result, you can strip
482 off trailing spaces with \\[beginning-of-buffer] \\[replace-regexp] [ SPC TAB ] + $ RET RET"
486 (error "You must first set two-column minor mode."))
487 (and (> (car (window-edges)) 0) ; not touching left edge of screen
488 (eq (window-buffer (previous-window))
492 (let ((b1 (current-buffer))
495 (goto-char (point-min))
497 (goto-char (point-min))
499 (setq string (buffer-substring (point)
500 (progn (end-of-line) (point))))
502 (forward-char)) ; next line
504 (if (string= string "")
507 (indent-to-column 2C-window-width)
508 (insert 2C-separator string))
509 (next-line 1) ; add one if necessary
511 (if (< (window-width) (frame-width))
512 (enlarge-window 99999 t)))
514 ;;;;; utility functions ;;;;;
516 (defun 2C-associated-buffer ()
517 "Switch to associated buffer."
520 (error "You must set two-column minor mode."))
521 (if (get-buffer-window (2C-other))
522 (select-window (get-buffer-window (2C-other)))
523 (switch-to-buffer (2C-other))))
527 (defun 2C-toggle-autoscroll (arg)
528 "Toggle autoscrolling, or set it iff prefix ARG is non-nil and positive.
529 When autoscrolling is turned on, this also realigns the two buffers."
531 (setq 2C-autoscroll-start (window-start))
532 (if (setq 2C-autoscroll (if arg
533 (>= (prefix-numeric-value arg) 0)
534 (not 2C-autoscroll)))
536 (prog1 (selected-window)
537 (message "Autoscrolling is on.")
539 (error "You must set two-column minor mode."))
540 (setq arg (count-lines (point-min) (window-start)))
541 (if (get-buffer-window (2C-other))
543 (select-window (get-buffer-window (2C-other)))
544 (setq arg (- arg (count-lines (point-min) (window-start))))
545 ;; make sure that other buffer has enough lines
548 (- arg (count-lines (window-start)
549 (goto-char (point-max)))
552 (message "Autoscrolling is off.")))
556 (defun 2C-autoscroll ()
558 ;; catch a mouse scroll on non-selected scrollbar
560 (prog1 (selected-window)
561 (and (consp last-command-char)
562 (not (eq (selected-window)
563 (car (car (cdr last-command-char)))))
564 (select-window (car (car (cdr last-command-char)))))
565 ;; In some cases scrolling causes an error, but post-command-hook
566 ;; shouldn't, and should always stay in the original window
568 (and (or 2C-autoscroll-start (2C-toggle-autoscroll t) nil)
569 (/= (window-start) 2C-autoscroll-start)
571 (get-buffer-window (2C-other))
572 (let ((lines (count-lines (window-start)
573 2C-autoscroll-start)))
574 (if (< (window-start) 2C-autoscroll-start)
575 (setq lines (- lines)))
576 (setq 2C-autoscroll-start (window-start))
577 (select-window (get-buffer-window (2C-other)))
578 ;; make sure that other buffer has enough lines
581 ?\n (- lines (count-lines (window-start)
582 (goto-char (point-max)))
585 (setq 2C-autoscroll-start (window-start))))
590 (defun enlarge-window-horizontally (arg)
591 "Make current window ARG columns wider."
593 (enlarge-window arg t)
595 (setq 2C-window-width (+ 2C-window-width arg))
596 (set-buffer (2C-other))
597 (setq 2C-window-width (- 2C-window-width arg))))
599 (defun shrink-window-horizontally (arg)
600 "Make current window ARG columns narrower."
602 (enlarge-window-horizontally (- arg)))
606 (provide 'two-column)
608 ;;; two-column.el ends here