master.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ;;; master.el --- make a buffer the master over another buffer
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Alex Schroeder <alex@gnu.org>
  4. ;; Maintainer: Alex Schroeder <alex@gnu.org>
  5. ;; Version: 1.0.2
  6. ;; Keywords: comm
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; master-mode is a minor mode which enables you to scroll another
  20. ;; buffer (the slave) without leaving your current buffer (the master).
  21. ;; It can be used by sql.el, for example: The SQL buffer is the master
  22. ;; and its SQLi buffer is the slave. This allows you to scroll the SQLi
  23. ;; buffer containing the output from the SQL buffer containing the
  24. ;; commands.
  25. ;;
  26. ;; This is how to use sql.el and master.el together: The variable
  27. ;; sql-buffer contains the slave buffer. It is a local variable in the
  28. ;; SQL buffer.
  29. ;;
  30. ;; (add-hook 'sql-mode-hook
  31. ;; (function (lambda ()
  32. ;; (master-mode t)
  33. ;; (master-set-slave sql-buffer))))
  34. ;; (add-hook 'sql-set-sqli-hook
  35. ;; (function (lambda ()
  36. ;; (master-set-slave sql-buffer))))
  37. ;;; Thanks to all the people who helped me out:
  38. ;;
  39. ;; Rob Riepel <networking.stanford.edu>
  40. ;;; History:
  41. ;;
  42. ;;; Code:
  43. (defgroup master nil
  44. "Support for master/slave relationships between buffers."
  45. :version "22.1"
  46. :group 'convenience)
  47. ;; Variables that don't need initialization.
  48. (defvar master-of nil
  49. "Slave buffer of the current buffer. See `master-mode'.
  50. You can set this variable using `master-set-slave'.")
  51. (defvar master-set-slave-hook nil
  52. "Hook run after the slave is changed using \\[master-set-slave].")
  53. ;;; Define master mode.
  54. ;;;###autoload
  55. (define-minor-mode master-mode
  56. "Toggle Master mode.
  57. With a prefix argument ARG, enable Master mode if ARG is
  58. positive, and disable it otherwise. If called from Lisp, enable
  59. the mode if ARG is omitted or nil.
  60. When Master mode is enabled, you can scroll the slave buffer
  61. using the following commands:
  62. \\{master-mode-map}
  63. The slave buffer is stored in the buffer-local variable `master-of'.
  64. You can set this variable using `master-set-slave'. You can show
  65. yourself the value of `master-of' by calling `master-show-slave'."
  66. :group 'master
  67. :keymap
  68. '(("\C-c\C-n" . master-says-scroll-up)
  69. ("\C-c\C-p" . master-says-scroll-down)
  70. ("\C-c<" . master-says-beginning-of-buffer)
  71. ("\C-c>" . master-says-end-of-buffer)
  72. ("\C-c\C-l" . master-says-recenter)))
  73. ;; Initialize Master mode by setting a slave buffer.
  74. (defun master-set-slave (buffer)
  75. "Makes BUFFER the slave of the current buffer.
  76. Use \\[master-mode] to toggle control of the slave buffer."
  77. (interactive "bSlave: ")
  78. (make-local-variable 'master-of)
  79. (setq master-of buffer)
  80. (run-hooks 'master-set-slave-hook))
  81. (defun master-show-slave ()
  82. "Displays a message with the name of the slave buffer."
  83. (interactive)
  84. (message "This buffer is the master of %s. Master-mode is %S."
  85. (or master-of "none")
  86. master-mode))
  87. ;;; Functions that the master buffer can call for the slave buffer.
  88. (defun master-says-scroll-up (&optional arg)
  89. "Display and scroll the slave buffer up.
  90. See `scroll-up'."
  91. (interactive)
  92. (master-says 'scroll-up arg))
  93. (defun master-says-scroll-down (&optional arg)
  94. "Display and scroll the slave buffer down.
  95. See `scroll-down'."
  96. (interactive)
  97. (master-says 'scroll-down arg))
  98. (defun master-says-beginning-of-buffer (&optional arg)
  99. "Display and move to the beginning of the slave buffer.
  100. See `beginning-of-buffer'."
  101. (interactive)
  102. (master-says 'beginning-of-buffer arg))
  103. (defun master-says-end-of-buffer (&optional arg)
  104. "Display and move to the end of the slave buffer.
  105. See `end-of-buffer'."
  106. (interactive)
  107. (master-says 'end-of-buffer arg))
  108. (defun master-says-recenter (&optional arg)
  109. "Recenter the slave buffer.
  110. See `recenter'."
  111. (interactive)
  112. (master-says 'recenter arg))
  113. ;; The master function doing the stuff.
  114. (defun master-says (&optional command arg)
  115. "Display slave buffer and execute COMMAND with ARG in its window."
  116. (interactive)
  117. (if (null (buffer-live-p (get-buffer master-of)))
  118. (error "Slave buffer has disappeared")
  119. (let ((window (selected-window)))
  120. (if (not (eq (window-buffer window) (get-buffer master-of)))
  121. (switch-to-buffer-other-window master-of))
  122. (if command (condition-case nil (apply command arg) (error nil)))
  123. (select-window window))))
  124. (provide 'master)
  125. ;;; master.el ends here