Skip to content
Newer
Older
100644 261 lines (217 sloc) 9.16 KB
0768eb0 @mark-moseley SVN RubyForge pull
mark-moseley authored
1 ;;; rdebug-secondary.el --- Rdebug support windows.
2
3 ;; Copyright (C) 2008 Rocky Bernstein (rocky@gnu.org)
4 ;; Copyright (C) 2008 Anders Lindgren
5
6 ;; $Id: rdebug-secondary.el 733 2008-02-29 04:34:44Z rockyb $
7
8 ;; This program 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 2, or (at your option)
11 ;; any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;; See the manual and the file `rdebug.el' for more information.
26
27 ;; Key bindings and menu for secondary buffers.
28
29 ;;; Code:
30
31 (require 'rdebug)
32
33 (defun rdebug-populate-secondary-buffer-map (map)
34 "Bind all common keys and menu used in the rdebug secondary buffers.
35 This includes the keys bound to `gud-key-prefix' (typically C-x
36 C-a)."
37 (rdebug-populate-secondary-buffer-map-plain map)
38 (rdebug-populate-common-keys map)
39 (rdebug-populate-debugger-menu map)
40 (let ((prefix-map (make-sparse-keymap)))
41 (rdebug-populate-secondary-buffer-map-plain prefix-map)
42 (define-key map gud-key-prefix prefix-map)))
43
44
45 (defun rdebug-display-cmd-buffer ()
46 "Display the rdebug debugger command buffer."
47 (interactive)
48 (rdebug-display-secondary-buffer "cmd"))
49
50 (defun rdebug-pick-secondary-window-categorize (win name orig-win)
51 "Return how suiteable this window is to display the a secondary buffer.
52 The higher score the better."
53 (let ((buffer (window-buffer win)))
54 (save-current-buffer
55 (set-buffer buffer)
56 (cond (rdebug-secondary-buffer
57 (cond ((eq win orig-win)
58 ;; If the user issued the command inside a
59 ;; secondary window, use that window.
60 5)
61 ((and (member name '("variables" "watch"))
62 (memq major-mode '(rdebug-variables-mode
63 rdebug-watch-mode)))
64 ;; Let "Watch" and "Variables" switch content.
65 4)
66 (t
67 ;; Any other secondary window.
68 3)))
69 ((eq major-mode 'ruby-mode)
70 ;; Avoid source windows.
71 0)
72 ((eq major-mode 'gud-mode)
73 ;; Avoid the debugger shell window.
74 1)
75 (t
76 ;; Just any other window.
77 2)))))
78
79
80 (defun rdebug-display-secondary-buffer (name)
81 "Display one of the rdebug secondary buffers.
82 If the buffer doesn't exist, do nothing. If the buffer is already
83 displayed, switch to it. Otherwise if the current buffer is a
84 secondary buffer, bury it replacing with the requested
85 buffer. Failing that, if there is secondary buffer visible, that
86 is replaced instead. And finally failing all of the preceding,
87 we'll just pick a visible buffer to bury and replace."
88 (let* ((buf-name (rdebug-get-secondary-buffer-name name))
89 (buf (get-buffer buf-name))
90 (orig-win (selected-window)))
91 (if (null buf)
92 (message "Buffer %s not found" buf-name)
93 ;; Find a suitable window to display the buffer in.
94 (let ((win (get-buffer-window buf (selected-frame))))
95 (if win
96 ;; Buffer already displayed, switch to it.
97 (select-window win)
98 ;;
99 (let ((candidate nil)
100 (candidate-score -1))
101 (dolist (win (window-list (selected-frame)))
102 (let ((score (rdebug-pick-secondary-window-categorize
103 win name orig-win)))
104 (if (> score candidate-score)
105 (progn
106 (setq candidate win)
107 (setq candidate-score score)))))
108 (select-window candidate)))))
109 (switch-to-buffer buf)))
110
111
112 ;; Note: The generic `gud' framework contains special code to handle
113 ;; this for GDB (see `gud-display-line') which we, unfortuately can't
114 ;; use. Instead, we call `rdebug-pick-source-window' from
115 ;; `gud-rdebug-marker-filter'. When gud becomes more generic we could
116 ;; hopefully solve this in another way.
117 ;;
118 ;; The machanism is that `rdebug-pick-source-window' displays the
119 ;; source file in the window of our choice, and gud kindly re-uses
120 ;; that window.
121
122
123 (defun rdebug-display-source-window-categorize (win)
124 "Return how suitable this window WIN is to display the source buffer.
125 The higher score the better."
126 (let ((buffer (window-buffer win)))
127 (cond ((eq buffer gud-comint-buffer)
128 0)
129 ((buffer-local-value 'rdebug-secondary-buffer buffer)
130 1)
131 ((eq (buffer-local-value 'major-mode buffer) 'ruby-mode)
132 3) ; Pick me! Pick me!
133 (t
134 2))))
135
136 (defun rdebug-display-pick-source-window ()
137 "Return the window that should get replaced by the source window."
138 (rdebug-debug-enter "rdebug-display-pick-source-window"
139 (let ((candidate nil)
140 (candidate-score -1))
141 (dolist (win (window-list (selected-frame)))
142 (let ((score
143 (rdebug-display-source-window-categorize win)))
144 (if (> score candidate-score)
145 (progn
146 (setq candidate win)
147 (setq candidate-score score)))))
148 candidate)))
149
150 (defun rdebug-frame-source-buffer (frame)
151 "Return the buffer corresponding to the source file given in FRAME, or nil if none."
152 (and frame
153 gud-comint-buffer
154 (save-current-buffer
155 (set-buffer gud-comint-buffer)
156 (gud-find-file (car frame)))))
157
158
159 (defun rdebug-current-source-buffer ()
160 "Return the latest source buffer, or nil."
161 (or (rdebug-frame-source-buffer gud-last-frame)
162 (rdebug-frame-source-buffer gud-last-last-frame)))
163
164
165 (defun rdebug-display-source-buffer ()
166 "Display the current source buffer."
167 (interactive)
168 (rdebug-debug-enter "rdebug-display-source-buffer"
169 (let ((buffer (rdebug-current-source-buffer))
170 (last-buffer (rdebug-frame-source-buffer gud-last-last-frame)))
171 (if buffer
172 (let ((window
173 (or
174 ;; Buffer is already visible, re-use the window.
175 (get-buffer-window buffer)
176 ;; Re-use the last window
177 (and last-buffer
178 (get-buffer-window last-buffer))
179 ;; Find a non-rdebug window.
180 (rdebug-display-pick-source-window))))
181 (select-window window)
182 (switch-to-buffer buffer))))))
183
184
185 (defun rdebug-pick-source-window ()
186 "Display the source file, but do not switch window."
187 (save-selected-window
188 (rdebug-display-source-buffer)))
189
190
191 (defun rdebug-display-source-buffer-resync ()
192 "Resync output and display the source buffer."
193 (interactive)
194 (call-interactively 'gud-source-resync)
195 (rdebug-display-source-buffer))
196
197
198 (defun rdebug-delete-frame-or-window ()
199 "Delete frame if there is only one window. Otherwise delete the window."
200 (interactive)
201 (if (one-window-p) (delete-frame)
202 (delete-window)))
203
204 (defun rdebug-goto-entry-try (str)
205 "See if thre is an entry with number STR. If not return nil."
206 (goto-char (point-min))
207 (if (re-search-forward (concat "^[^0-9]*\\(" str "\\)[^0-9]") nil t)
208 (progn
209 (goto-char (match-end 1))
210 t)
211 nil))
212
213
214 ;; The following is split in two to facilitate debugging.
215 (defun rdebug-goto-entry-n-internal (keys)
216 (if (and (stringp keys)
217 (= (length keys) 1))
218 (progn
219 (setq rdebug-goto-entry-acc (concat rdebug-goto-entry-acc keys))
220 ;; Try to find the longest suffix.
221 (let ((acc rdebug-goto-entry-acc)
222 (p (point)))
223 (while (not (string= acc ""))
224 (if (not (rdebug-goto-entry-try acc))
225 (setq acc (substring acc 1))
226 (setq p (point))
227 ;; Break loop.
228 (setq acc "")))
229 (goto-char p)))
230 (message "`rdebug-goto-entry-n' must be bound to a number key")))
231
232
233 (defun rdebug-goto-entry-n ()
234 "Go to an entry number.
235
236 Breakpoints, Display expressions and Stack Frames all have
237 numbers associated with them which are distinct from line
238 numbers. In a secondary buffer, this function is usually bound to
239 a numeric key which will position you at that entry number. To
240 go to an entry above 9, just keep entering the number. For
241 example, if you press 1 and then 9, you should jump to entry
242 1 (if it exists) and then 19 (if that exists). Entering any
243 non-digit will start entry number from the beginning again."
244 (interactive)
245 (if (not (eq last-command 'rdebug-goto-entry-n))
246 (setq rdebug-goto-entry-acc ""))
247 (rdebug-goto-entry-n-internal (this-command-keys)))
248
249
250 ;; -------------------------------------------------------------------
251 ;; The end.
252 ;;
253
254 (provide 'rdebug-secondary)
255
256 ;;; Local variables:
257 ;;; eval:(put 'rdebug-debug-enter 'lisp-indent-hook 1)
258 ;;; End:
259
260 ;;; rdebug-secondary.el ends here
Something went wrong with that request. Please try again.