two-column.el 22 KB

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