]> code.delx.au - gnu-emacs-elpa/blob - packages/realgud/realgud/debugger/rdebug/core.el
Add 'packages/realgud/' from commit 'd811316e6a0f4eeee8a1347f504c196c86baa2cb'
[gnu-emacs-elpa] / packages / realgud / realgud / debugger / rdebug / core.el
1 ;;; Copyright (C) 2010, 2015 Rocky Bernstein <rocky@gnu.org>
2 (eval-when-compile (require 'cl))
3
4 (require 'load-relative)
5 (require-relative-list '("../../common/track"
6 "../../common/core"
7 "../../common/lang")
8 "realgud-")
9 (require-relative-list '("init") "realgud-rdebug-")
10
11 (declare-function realgud-lang-mode? 'realgud-lang)
12 (declare-function realgud-parse-command-arg 'realgud-core)
13 (declare-function realgud-query-cmdline 'realgud-core)
14 (declare-function realgud-suggest-invocation 'realgud-core)
15
16 ;; FIXME: I think the following could be generalized and moved to
17 ;; realgud-... probably via a macro.
18 (defvar realgud:rdebug-minibuffer-history nil
19 "minibuffer history list for the command `rdebug'.")
20
21 (easy-mmode-defmap rdebug-minibuffer-local-map
22 '(("\C-i" . comint-dynamic-complete-filename))
23 "Keymap for minibuffer prompting of gud startup command."
24 :inherit minibuffer-local-map)
25
26 ;; FIXME: I think this code and the keymaps and history
27 ;; variable chould be generalized, perhaps via a macro.
28 (defun rdebug-query-cmdline (&optional opt-debugger)
29 (realgud-query-cmdline
30 'rdebug-suggest-invocation
31 rdebug-minibuffer-local-map
32 'realgud:rdebug-minibuffer-history
33 opt-debugger))
34
35 (defun rdebug-parse-cmd-args (orig-args)
36 "Parse command line ARGS for the annotate level and name of script to debug.
37
38 ARGS should contain a tokenized list of the command line to run.
39
40 We return the a list containing
41 - the command processor (e.g. ruby) and it's arguments if any - a list of strings
42 - the name of the debugger given (e.g. rdebug) and its arguments - a list of strings
43 - the script name and its arguments - list of strings
44 - whether the annotate or emacs option was given ('-A', '--annotate' or '--emacs) - a boolean
45
46 For example for the following input
47 (map 'list 'symbol-name
48 '(ruby1.9 -W -C /tmp rdebug --emacs ./gcd.rb a b))
49
50 we might return:
51 ((ruby1.9 -W -C) (rdebug --emacs) (./gcd.rb a b) 't)
52
53 NOTE: the above should have each item listed in quotes.
54 "
55
56 ;; Parse the following kind of pattern:
57 ;; [ruby ruby-options] rdebug rdebug-options script-name script-options
58 (let (
59 (args orig-args)
60 (pair) ;; temp return from
61 (ruby-opt-two-args '("0" "C" "e" "E" "F" "i"))
62 ;; Ruby doesn't have mandatory 2-arg options in our sense,
63 ;; since the two args can be run together, e.g. "-C/tmp" or "-C /tmp"
64 ;;
65 (ruby-two-args '())
66 ;; One dash is added automatically to the below, so
67 ;; h is really -h and -host is really --host.
68 (rdebug-two-args '("h" "-host" "p" "-port"
69 "I" "-include" "-r" "-require"))
70 (rdebug-opt-two-args '())
71
72 ;; Things returned
73 (script-name nil)
74 (debugger-name nil)
75 (interpreter-args '())
76 (debugger-args '())
77 (script-args '())
78 (annotate-p nil))
79
80 (if (not (and args))
81 ;; Got nothing: return '(nil, nil)
82 (list interpreter-args debugger-args script-args annotate-p)
83 ;; else
84 ;; Strip off optional "ruby" or "ruby182" etc.
85 (when (string-match "^ruby[-0-9]*$"
86 (file-name-sans-extension
87 (file-name-nondirectory (car args))))
88 (setq interpreter-args (list (pop args)))
89
90 ;; Strip off Ruby-specific options
91 (while (and args
92 (string-match "^-" (car args)))
93 (setq pair (realgud-parse-command-arg
94 args ruby-two-args ruby-opt-two-args))
95 (nconc interpreter-args (car pair))
96 (setq args (cadr pair))))
97
98 ;; Remove "rdebug" from "rdebug --rdebug-options script
99 ;; --script-options"
100 (setq debugger-name (file-name-sans-extension
101 (file-name-nondirectory (car args))))
102 (unless (string-match "^rdebug$" debugger-name)
103 (message
104 "Expecting debugger name `%s' to be `rdebug'"
105 debugger-name))
106 (setq debugger-args (list (pop args)))
107
108 ;; Skip to the first non-option argument.
109 (while (and args (not script-name))
110 (let ((arg (car args)))
111 (cond
112 ;; Annotation or emacs option with level number.
113 ((or (member arg '("--annotate" "-A"))
114 (equal arg "--emacs"))
115 (setq annotate-p t)
116 (nconc debugger-args (list (pop args))))
117 ;; Combined annotation and level option.
118 ((string-match "^--annotate=[0-9]" arg)
119 (nconc debugger-args (list (pop args)) )
120 (setq annotate-p t))
121 ;; Options with arguments.
122 ((string-match "^-" arg)
123 (setq pair (realgud-parse-command-arg
124 args rdebug-two-args rdebug-opt-two-args))
125 (nconc debugger-args (car pair))
126 (setq args (cadr pair)))
127 ;; Anything else must be the script to debug.
128 (t (setq script-name arg)
129 (setq script-args args))
130 )))
131 (list interpreter-args debugger-args script-args annotate-p))))
132
133 (defvar realgud:rdebug-command-name)
134 (defun rdebug-suggest-invocation (debugger-name)
135 "Suggest a rdebug command invocation via `realgud-suggest-invocaton'"
136 (realgud-suggest-invocation realgud:rdebug-command-name
137 realgud:rdebug-minibuffer-history
138 "ruby" "\\.rb$"
139 realgud:rdebug-command-name))
140
141 (defun rdebug-reset ()
142 "Rdebug cleanup - remove debugger's internal buffers (frame,
143 breakpoints, etc.)."
144 (interactive)
145 ;; (rdebug-breakpoint-remove-all-icons)
146 (dolist (buffer (buffer-list))
147 (when (string-match "\\*rdebug-[a-z]+\\*" (buffer-name buffer))
148 (let ((w (get-buffer-window buffer)))
149 (when w
150 (delete-window w)))
151 (kill-buffer buffer))))
152
153 ;; (defun rdebug-reset-keymaps()
154 ;; "This unbinds the special debugger keys of the source buffers."
155 ;; (interactive)
156 ;; (setcdr (assq 'rdebug-debugger-support-minor-mode minor-mode-map-alist)
157 ;; rdebug-debugger-support-minor-mode-map-when-deactive))
158
159
160 (defun realgud:rdebug-customize ()
161 "Use `customize' to edit the settings of the `rdebug' debugger."
162 (interactive)
163 (customize-group 'realgud:rdebug))
164
165 (provide-me "realgud-rdebug-")