buffer-move.el 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ;;; buffer-move.el --- Swap buffers without typing C-x b on each window
  2. ;; Copyright (C) 2004 Lucas Bonnet <lukhas@free.fr>
  3. ;; Author: Lucas Bonnet <lucas@rincevent.net>
  4. ;; Keywords: lisp,convenience
  5. ;; Version: 0.4
  6. ;; URL : http://lukhas.free. fr/emacs/elisp/buffer-move.el
  7. ;; This program is free software; you can redistribute it and/or
  8. ;; modify it under the terms of the GNU General Public License
  9. ;; as published by the Free Software Foundation; either version 2
  10. ;; of the License, or (at your option) any later version.
  11. ;; This program 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 this program; if not, write to the Free Software
  17. ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  18. ;; 02111-1307, USA.
  19. ;;; Commentary:
  20. ;; This file is for lazy people wanting to swap buffers without
  21. ;; typing C-x b on each window. This is useful when you have :
  22. ;; +--------------+-------------+
  23. ;; | | |
  24. ;; | #emacs | #gnus |
  25. ;; | | |
  26. ;; +--------------+-------------+
  27. ;; | |
  28. ;; | .emacs |
  29. ;; | |
  30. ;; +----------------------------+
  31. ;; and you want to have :
  32. ;; +--------------+-------------+
  33. ;; | | |
  34. ;; | #gnus | .emacs |
  35. ;; | | |
  36. ;; +--------------+-------------+
  37. ;; | |
  38. ;; | #emacs |
  39. ;; | |
  40. ;; +----------------------------+
  41. ;; With buffer-move, just go in #gnus, do buf-move-left, go to #emacs
  42. ;; (which now should be on top right) and do buf-move-down.
  43. ;; To use it, simply put a (require 'buffer-move) in your ~/.emacs and
  44. ;; define some keybindings. For example, i use :
  45. ;; (global-set-key (kbd "<C-S-up>") 'buf-move-up)
  46. ;; (global-set-key (kbd "<C-S-down>") 'buf-move-down)
  47. ;; (global-set-key (kbd "<C-S-left>") 'buf-move-left)
  48. ;; (global-set-key (kbd "<C-S-right>") 'buf-move-right)
  49. ;;; Code:
  50. (require 'windmove)
  51. ;;;###autoload
  52. (defun buf-move-up ()
  53. "Swap the current buffer and the buffer above the split.
  54. If there is no split, ie no window above the current one, an
  55. error is signaled."
  56. ;; "Switches between the current buffer, and the buffer above the
  57. ;; split, if possible."
  58. (interactive)
  59. (let* ((other-win (windmove-find-other-window 'up))
  60. (buf-this-buf (window-buffer (selected-window))))
  61. (if (null other-win)
  62. (error "No window above this one")
  63. ;; swap top with this one
  64. (set-window-buffer (selected-window) (window-buffer other-win))
  65. ;; move this one to top
  66. (set-window-buffer other-win buf-this-buf)
  67. (select-window other-win))))
  68. ;;;###autoload
  69. (defun buf-move-down ()
  70. "Swap the current buffer and the buffer under the split.
  71. If there is no split, ie no window under the current one, an
  72. error is signaled."
  73. (interactive)
  74. (let* ((other-win (windmove-find-other-window 'down))
  75. (buf-this-buf (window-buffer (selected-window))))
  76. (if (or (null other-win)
  77. (string-match "^ \\*Minibuf" (buffer-name (window-buffer other-win))))
  78. (error "No window under this one")
  79. ;; swap top with this one
  80. (set-window-buffer (selected-window) (window-buffer other-win))
  81. ;; move this one to top
  82. (set-window-buffer other-win buf-this-buf)
  83. (select-window other-win))))
  84. ;;;###autoload
  85. (defun buf-move-left ()
  86. "Swap the current buffer and the buffer on the left of the split.
  87. If there is no split, ie no window on the left of the current
  88. one, an error is signaled."
  89. (interactive)
  90. (let* ((other-win (windmove-find-other-window 'left))
  91. (buf-this-buf (window-buffer (selected-window))))
  92. (if (null other-win)
  93. (error "No left split")
  94. ;; swap top with this one
  95. (set-window-buffer (selected-window) (window-buffer other-win))
  96. ;; move this one to top
  97. (set-window-buffer other-win buf-this-buf)
  98. (select-window other-win))))
  99. ;;;###autoload
  100. (defun buf-move-right ()
  101. "Swap the current buffer and the buffer on the right of the split.
  102. If there is no split, ie no window on the right of the current
  103. one, an error is signaled."
  104. (interactive)
  105. (let* ((other-win (windmove-find-other-window 'right))
  106. (buf-this-buf (window-buffer (selected-window))))
  107. (if (null other-win)
  108. (error "No right split")
  109. ;; swap top with this one
  110. (set-window-buffer (selected-window) (window-buffer other-win))
  111. ;; move this one to top
  112. (set-window-buffer other-win buf-this-buf)
  113. (select-window other-win))))
  114. (provide 'buffer-move)
  115. ;;; buffer-move.el ends here