]> code.delx.au - gnu-emacs-elpa/blob - realgud/debugger/trepan2/core.el
{trepan2,jdb}/{core,init}.el: Start adding debugger-specific find-file routines....
[gnu-emacs-elpa] / realgud / debugger / trepan2 / core.el
1 ;;; Copyright (C) 2010, 2014 Rocky Bernstein <rocky@gnu.org>
2 (eval-when-compile (require 'cl))
3
4 (require 'compile) ;; for compilation-find-file
5 (require 'load-relative)
6 (require-relative-list '("../../common/track"
7 "../../common/core"
8 "../../common/file"
9 "../../common/lang")
10 "realgud-")
11 (require-relative-list '("init") "realgud:trepan2-")
12
13 (declare-function realgud:strip 'realgud)
14 (declare-function realgud:expand-file-name-if-exists 'realgud-core)
15 (declare-function realgud-parse-command-arg 'realgud-core)
16 (declare-function realgud-query-cmdline 'realgud-core)
17 (declare-function realgud-suggest-invocation 'realgud-core)
18 (declare-function realgud-file-loc-from-line 'realgud-file)
19
20 ;; FIXME: I think the following could be generalized and moved to
21 ;; realgud-... probably via a macro.
22 (defvar realgud:trepan2-minibuffer-history nil
23 "minibuffer history list for the command `realgud:trepan2'.")
24
25 (easy-mmode-defmap trepan2-minibuffer-local-map
26 '(("\C-i" . comint-dynamic-complete-filename))
27 "Keymap for minibuffer prompting of gud startup command."
28 :inherit minibuffer-local-map)
29
30 (defvar realgud:trepan2-file-remap (make-hash-table :test 'equal)
31 "How to remap Python files in treapn2 when we otherwise can't
32 find in the filesystem. The hash key is the file string we saw,
33 and the value is associated filesystem string presumably in the
34 filesystem")
35
36 ;; FIXME: this code could be generalized and put in a common place.
37 (defun realgud:trepan2-find-file(filename)
38 "A find-file specific for python/trepan. We strip off trailing
39 blanks. Failing that we will prompt for a mapping and save that
40 in variable `realgud:trepan2-file-remap' when that works. In the future,
41 we may also consult PYTHONPATH."
42 (let* ((transformed-file)
43 (stripped-filename (realgud:strip filename))
44 (ignore-file-re realgud-python-ignore-file-re)
45 )
46 (cond
47 ((file-exists-p filename) filename)
48 ((file-exists-p stripped-filename) stripped-filename)
49 ((string-match ignore-file-re filename)
50 (message "tracking ignored for psuedo-file: %s" filename) nil)
51 ('t
52 ;; FIXME search PYTHONPATH if not absolute file
53 (if (gethash filename realgud-file-remap)
54 (let ((remapped-filename))
55 (setq remapped-filename (gethash filename realgud:trepan2-file-remap))
56 (if (file-exists-p remapped-filename)
57 remapped-filename
58 ;; else
59 (and (remhash filename realgud-file-remap)) nil)
60 ;; else
61 (let ((remapped-filename))
62 (setq remapped-filename
63 (buffer-file-name
64 (compilation-find-file (point-marker) stripped-filename nil)))
65 (when (and remapped-filename (file-exists-p remapped-filename))
66 (puthash filename remapped-filename realgud-file-remap)
67 remapped-filename
68 ))
69 ))
70 ))
71 ))
72
73 (defun realgud:trepan2-loc-fn-callback(text filename lineno source-str
74 ignore-file-re cmd-mark)
75 (realgud-file-loc-from-line filename lineno
76 cmd-mark source-str nil
77 'realgud:trepan2-find-file))
78
79 ;; FIXME: I think this code and the keymaps and history
80 ;; variable chould be generalized, perhaps via a macro.
81 (defun trepan2-query-cmdline (&optional opt-debugger)
82 (realgud-query-cmdline
83 'trepan2-suggest-invocation
84 trepan2-minibuffer-local-map
85 'realgud:trepan2-minibuffer-history
86 opt-debugger))
87
88 (defun trepan2-parse-cmd-args (orig-args)
89 "Parse command line ARGS for the annotate level and name of script to debug.
90
91 ARGS should contain a tokenized list of the command line to run.
92
93 We return the a list containing
94 - the command processor (e.g. python) and it's arguments if any - a list of strings
95 - the name of the debugger given (e.g. trepan2) and its arguments - a list of strings
96 - the script name and its arguments - list of strings
97 - whether the annotate or emacs option was given ('-A', '--annotate' or '--emacs) - a boolean
98
99 For example for the following input
100 (map 'list 'symbol-name
101 '(python2.6 -O -Qold --emacs ./gcd.py a b))
102
103 we might return:
104 ((python2.6 -O -Qold) (trepan2 --emacs) (./gcd.py a b) 't)
105
106 NOTE: the above should have each item listed in quotes.
107 "
108
109 ;; Parse the following kind of pattern:
110 ;; [python python-options] trepan2 trepan2-options script-name script-options
111 (let (
112 (args orig-args)
113 (pair) ;; temp return from
114 (python-opt-two-args '("c" "m" "Q" "W"))
115 ;; Python doesn't have mandatory 2-arg options in our sense,
116 ;; since the two args can be run together, e.g. "-C/tmp" or "-C /tmp"
117 ;;
118 (python-two-args '())
119 ;; One dash is added automatically to the below, so
120 ;; h is really -h and -host is really --host.
121 (trepan2-two-args '("x" "-command" "e" "-execute"
122 "o" "-output" "t" "-target"
123 "a" "-annotate"))
124 (trepan2-opt-two-args '())
125 (interp-regexp
126 (if (member system-type (list 'windows-nt 'cygwin 'msdos))
127 "^python[-0-9.]*\\(.exe\\)?$"
128 "^python[-0-9.]*$"))
129
130 ;; Things returned
131 (annotate-p nil)
132 (debugger-args '())
133 (debugger-name nil)
134 (interpreter-args '())
135 (script-args '())
136 (script-name nil)
137 )
138
139 (if (not (and args))
140 ;; Got nothing: return '(nil, nil)
141 (list interpreter-args debugger-args script-args annotate-p)
142 ;; else
143 ;; Strip off optional "python" or "python182" etc.
144 (when (string-match interp-regexp
145 (file-name-sans-extension
146 (file-name-nondirectory (car args))))
147 (setq interpreter-args (list (pop args)))
148
149 ;; Strip off Python-specific options
150 (while (and args
151 (string-match "^-" (car args)))
152 (setq pair (realgud-parse-command-arg
153 args python-two-args python-opt-two-args))
154 (nconc interpreter-args (car pair))
155 (setq args (cadr pair))))
156
157 ;; Remove "trepan2" from "trepan2 --trepan2-options script
158 ;; --script-options"
159 (setq debugger-name (file-name-sans-extension
160 (file-name-nondirectory (car args))))
161 (unless (string-match "^\\(trepan2\\|cli.py\\)$" debugger-name)
162 (message
163 "Expecting debugger name `%s' to be `trepan2' or `cli.py'"
164 debugger-name))
165 (setq debugger-args (list (pop args)))
166
167 ;; Skip to the first non-option argument.
168 (while (and args (not script-name))
169 (let ((arg (car args)))
170 (cond
171 ;; Annotation or emacs option with level number.
172 ((or (member arg '("--annotate" "-A"))
173 (equal arg "--emacs"))
174 (setq annotate-p t)
175 (nconc debugger-args (list (pop args))))
176 ;; Combined annotation and level option.
177 ((string-match "^--annotate=[0-9]" arg)
178 (nconc debugger-args (list (pop args)) )
179 (setq annotate-p t))
180 ;; Options with arguments.
181 ((string-match "^-" arg)
182 (setq pair (realgud-parse-command-arg
183 args trepan2-two-args trepan2-opt-two-args))
184 (nconc debugger-args (car pair))
185 (setq args (cadr pair)))
186 ;; Anything else must be the script to debug.
187 (t (setq script-name (realgud:expand-file-name-if-exists arg))
188 (setq script-args (cons script-name (cdr args))))
189 )))
190 (list interpreter-args debugger-args script-args annotate-p))))
191
192 ;; To silence Warning: reference to free variable
193 (defvar realgud:trepan2-command-name)
194
195 (defun trepan2-suggest-invocation (debugger-name)
196 "Suggest a trepan2 command invocation via `realgud-suggest-invocaton'"
197 (realgud-suggest-invocation realgud:trepan2-command-name
198 realgud:trepan2-minibuffer-history
199 "python" "\\.py"))
200
201 (defun trepan2-reset ()
202 "Trepan2 cleanup - remove debugger's internal buffers (frame,
203 breakpoints, etc.)."
204 (interactive)
205 ;; (trepan2-breakpoint-remove-all-icons)
206 (dolist (buffer (buffer-list))
207 (when (string-match "\\*trepan2-[a-z]+\\*" (buffer-name buffer))
208 (let ((w (get-buffer-window buffer)))
209 (when w
210 (delete-window w)))
211 (kill-buffer buffer))))
212
213 ;; (defun trepan2-reset-keymaps()
214 ;; "This unbinds the special debugger keys of the source buffers."
215 ;; (interactive)
216 ;; (setcdr (assq 'trepan2-debugger-support-minor-mode minor-mode-map-alist)
217 ;; trepan2-debugger-support-minor-mode-map-when-deactive))
218
219
220 (defun realgud:trepan2-customize ()
221 "Use `customize' to edit the settings of the `trepan2' debugger."
222 (interactive)
223 (customize-group 'realgud:trepan2))
224
225 (provide-me "realgud:trepan2-")