PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/rdebug/rdebug-secondary.el

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