two-column.el 22 KB

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