2017年02月25日

[emacs] ウィンドウの分割方向を変更する

C-x 2 や C-x 3 でウィンドウを分割できるが、分割する方向を変更するコマンドが見当たらないので 自作してみることにした。久しぶりの elisp だったが、なんとかうまく動作するもが出来上がったので、公開しておこう。

もともと C-x 2 や C-x 3 に割り当てられている 'split-window-below とかは、ウィンドウをどんどん分割してゆくが、自分はほとんど 二分割しか使わないし、むしろ二分割固定のほうが使いやすいので、再分割コマンドに 「未分割の場合は分割する」機能も追加して、C-x 2 と C-x 3 に割り当てておいた。

ウィンドウ分割がとても使いやすくなって満足している

;; 水平方向へ分割しなおす
(defun resplit-window-horizontally ()
  "Change buffers splitting direction"
  (interactive)
  (let* ((current (selected-window))
	 (other (next-window current 0))
	 (current-buf (window-buffer current))
	 (other-buf (window-buffer other)))
    (if (or (eq current other)
	    (window-minibuffer-p current))
        ;; then-form : 未分割の場合 水平分割する
	(split-window-horizontally)
      ;; else-form : 水平に分割しなおす
      ;; delete other splitted windows
      (delete-other-windows)
      (split-window-horizontally)
      (set-window-buffer (next-window current 0) other-buf))))

;; 縦方向へ分割しなおす
(defun resplit-window-vertically ()
  "Change buffers splitting direction"
  (interactive)
  (let* ((current (selected-window))
	 (other (next-window current 0))
	 (current-buf (window-buffer current))
	 (other-buf (window-buffer other)))
    (if (or (eq current other)
	    (window-minibuffer-p current))
	;; then-form : 未分割の場合 垂直に分割する
	(split-window-vertically)
      ;; else-form : 縦方向に再分割する
      ;; delete other splitted windows
      (delete-other-windows)
      (split-window-vertically)
      (set-window-buffer (next-window current 0) other-buf))))

;; キーバインド
(global-set-key (kbd "C-x 2") 'resplit-window-vertically)
(global-set-key (kbd "C-x 3") 'resplit-window-horizontally)



posted by tekniq at 18:59 | 日記