1 ;;; Copyright (C) 2010, 2014 Rocky Bernstein <rocky@gnu.org>
2 (eval-when-compile (require 'cl))
4 (require 'compile) ;; for compilation-find-file
5 (require 'load-relative)
6 (require-relative-list '("../../common/track"
11 (require-relative-list '("init") "realgud:trepan2-")
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)
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'.")
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)
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
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)
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)
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)
59 (and (remhash filename realgud-file-remap)) nil)
61 (let ((remapped-filename))
62 (setq remapped-filename
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)
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))
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
88 (defun trepan2-parse-cmd-args (orig-args)
89 "Parse command line ARGS for the annotate level and name of script to debug.
91 ARGS should contain a tokenized list of the command line to run.
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
99 For example for the following input
100 (map 'list 'symbol-name
101 '(python2.6 -O -Qold --emacs ./gcd.py a b))
104 ((python2.6 -O -Qold) (trepan2 --emacs) (./gcd.py a b) 't)
106 NOTE: the above should have each item listed in quotes.
109 ;; Parse the following kind of pattern:
110 ;; [python python-options] trepan2 trepan2-options script-name script-options
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"
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"
124 (trepan2-opt-two-args '())
126 (if (member system-type (list 'windows-nt 'cygwin 'msdos))
127 "^python[-0-9.]*\\(.exe\\)?$"
134 (interpreter-args '())
140 ;; Got nothing: return '(nil, nil)
141 (list interpreter-args debugger-args script-args annotate-p)
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)))
149 ;; Strip off Python-specific options
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))))
157 ;; Remove "trepan2" from "trepan2 --trepan2-options script
159 (setq debugger-name (file-name-sans-extension
160 (file-name-nondirectory (car args))))
161 (unless (string-match "^\\(trepan2\\|cli.py\\)$" debugger-name)
163 "Expecting debugger name `%s' to be `trepan2' or `cli.py'"
165 (setq debugger-args (list (pop args)))
167 ;; Skip to the first non-option argument.
168 (while (and args (not script-name))
169 (let ((arg (car args)))
171 ;; Annotation or emacs option with level number.
172 ((or (member arg '("--annotate" "-A"))
173 (equal arg "--emacs"))
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)) )
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))))
190 (list interpreter-args debugger-args script-args annotate-p))))
192 ;; To silence Warning: reference to free variable
193 (defvar realgud:trepan2-command-name)
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
201 (defun trepan2-reset ()
202 "Trepan2 cleanup - remove debugger's internal buffers (frame,
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)))
211 (kill-buffer buffer))))
213 ;; (defun trepan2-reset-keymaps()
214 ;; "This unbinds the special debugger keys of the source buffers."
216 ;; (setcdr (assq 'trepan2-debugger-support-minor-mode minor-mode-map-alist)
217 ;; trepan2-debugger-support-minor-mode-map-when-deactive))
220 (defun realgud:trepan2-customize ()
221 "Use `customize' to edit the settings of the `trepan2' debugger."
223 (customize-group 'realgud:trepan2))
225 (provide-me "realgud:trepan2-")