Commit | Line | Data |
---|---|---|
1ae6b8f1 GM |
1 | ;;; master.el --- make a buffer the master over another buffer |
2 | ||
3 | ;; Copyright (C) 1999, 2000, 2001 Alexander Schroeder | |
4 | ||
5 | ;; Author: Alex Schroeder <alex@gnu.org> | |
6 | ;; Maintainer: Alex Schroeder <alex@gnu.org> | |
7 | ;; Version: 1.0.2 | |
8 | ;; Keywords: comm | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; master.el is free software; you can redistribute it and/or modify it | |
13 | ;; under the terms of the GNU General Public License as published by the | |
14 | ;; Free Software Foundation; either version 2, or (at your option) any | |
15 | ;; later version. | |
16 | ||
17 | ;; master.el is distributed in the hope that it will be useful, but | |
18 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | ;; General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | ;; MA 02111-1307, USA. | |
26 | ||
27 | ;;; Commentary: | |
28 | ||
29 | ;; master-mode is a minor mode which enables you to scroll another | |
30 | ;; buffer (the slave) without leaving your current buffer (the master). | |
31 | ||
32 | ;; It can be used by sql.el, for example: The SQL buffer is the master | |
33 | ;; and its SQLi buffer is the slave. This allows you to scroll the SQLi | |
34 | ;; buffer containing the output from the SQL buffer containing the | |
35 | ;; commands. | |
36 | ;; | |
37 | ;; This is how to use sql.el and master.el together: The variable | |
38 | ;; sql-buffer contains the slave buffer. It is a local variable in the | |
39 | ;; SQL buffer. | |
40 | ;; | |
41 | ;; (autoload 'master-mode "master" "Master mode minor mode." t) | |
42 | ;; (add-hook 'sql-mode-hook | |
43 | ;; (function (lambda () | |
44 | ;; (master-mode t) | |
45 | ;; (master-set-slave sql-buffer)))) | |
46 | ;; (add-hook 'sql-set-sqli-hook | |
47 | ;; (function (lambda () | |
48 | ;; (master-set-slave sql-buffer)))) | |
49 | ||
50 | ;;; Thanks to all the people who helped me out: | |
51 | ;; | |
52 | ;; Rob Riepel <networking.stanford.edu> | |
53 | ||
54 | \f | |
55 | ||
56 | ||
57 | ;;; History: | |
58 | ;; | |
59 | ||
60 | ;;; Code: | |
61 | ||
62 | (require 'easy-mmode) | |
63 | ||
64 | ;; Variables that don't need initialization. | |
65 | ||
66 | (defvar master-of nil | |
67 | "Slave buffer of the current buffer. See `master-mode'. | |
68 | You can set this variable using `master-set-slave'.") | |
69 | ||
70 | (defvar master-set-slave-hook nil | |
71 | "Hook run after the slave is changed using \\[master-set-slave].") | |
72 | ||
73 | ;;; Define master mode. | |
74 | ||
75 | (easy-mmode-define-minor-mode master-mode | |
76 | "Toggle Master mode. | |
77 | With no argument, this command toggles the mode. | |
78 | Non-null prefix argument turns on the mode. | |
79 | Null prefix argument turns off the mode. | |
80 | ||
81 | When Master mode is enabled, you can scroll the slave buffer using the | |
82 | following commands: | |
83 | ||
84 | \\{master-mode-map} | |
85 | ||
86 | The slave buffer is stored in the buffer-local variable `master-of'. | |
87 | You can set this variable using `master-set-slave'. You can show | |
88 | yourself the value of `master-of' by calling `master-show-slave'." | |
89 | ;; The initial value. | |
90 | nil | |
91 | ;; The indicator for the mode line. | |
92 | nil | |
93 | ;; The minor mode bindings. | |
94 | '(("\C-c\C-n" . master-says-scroll-up) | |
95 | ("\C-c\C-p" . master-says-scroll-down) | |
96 | ("\C-c<" . master-says-beginning-of-buffer) | |
97 | ("\C-c>" . master-says-end-of-buffer) | |
98 | ("\C-c\C-l" . master-says-recenter))) | |
99 | ||
100 | ;; Initialize Master mode by setting a slave buffer. | |
101 | ||
102 | (defun master-set-slave (buffer) | |
103 | "Makes BUFFER the slave of the current buffer. | |
104 | Use \\[master-mode] to toggle control of the slave buffer." | |
105 | (interactive "bSlave: ") | |
106 | (make-local-variable 'master-of) | |
107 | (setq master-of buffer) | |
108 | (run-hooks 'master-set-slave-hook)) | |
109 | ||
110 | (defun master-show-slave () | |
111 | "Displays a message with the name of the slave buffer." | |
112 | (interactive) | |
113 | (message "This buffer is the master of %s. Master-mode is %S." | |
114 | (or master-of "none") | |
115 | master-mode)) | |
116 | ||
117 | \f | |
118 | ||
119 | ;;; Functions that the master buffer can call for the slave buffer. | |
120 | ||
121 | (defun master-says-scroll-up (&optional arg) | |
122 | "Display and scroll the slave buffer up. | |
123 | See `scroll-up'." | |
124 | (interactive) | |
125 | (master-says 'scroll-up arg)) | |
126 | ||
127 | (defun master-says-scroll-down (&optional arg) | |
128 | "Display and scroll the slave buffer down. | |
129 | See `scroll-down'." | |
130 | (interactive) | |
131 | (master-says 'scroll-down arg)) | |
132 | ||
133 | (defun master-says-beginning-of-buffer (&optional arg) | |
134 | "Display and move to the beginning of the slave buffer. | |
135 | See `beginning-of-buffer'." | |
136 | (interactive) | |
137 | (master-says 'beginning-of-buffer arg)) | |
138 | ||
139 | (defun master-says-end-of-buffer (&optional arg) | |
140 | "Display and move to the end of the slave buffer. | |
141 | See `end-of-buffer'." | |
142 | (interactive) | |
143 | (master-says 'end-of-buffer arg)) | |
144 | ||
145 | (defun master-says-recenter (&optional arg) | |
146 | "Recenter the slave buffer. | |
147 | See `recenter'." | |
148 | (interactive) | |
149 | (master-says 'recenter arg)) | |
150 | ||
151 | ;; The master function doing the stuff. | |
152 | ||
153 | (defun master-says (&optional command arg) | |
154 | "Display slave buffer and execute COMMAND with ARG in its window." | |
155 | (interactive) | |
156 | (if (null (buffer-live-p (get-buffer master-of))) | |
157 | (error "Slave buffer has disappeared") | |
158 | (let ((window (selected-window))) | |
159 | (if (not (eq (window-buffer window) (get-buffer master-of))) | |
160 | (switch-to-buffer-other-window master-of)) | |
161 | (if command (condition-case nil (apply command arg) (error nil))) | |
162 | (select-window window)))) | |
163 | ||
164 | (provide 'master) | |
165 | ||
166 | ;;; master.el ends here | |
167 |