1 ;;; Copyright (C) 2010, 2013 Rocky Bernstein <rocky@gnu.org>
2 (eval-when-compile (require 'cl))
4 (require 'load-relative)
5 (require-relative-list '("../../common/track"
9 (require-relative-list '("init") "realgud-pydbgr-")
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)
16 ;; FIXME: I think the following could be generalized and moved to
17 ;; realgud-... probably via a macro.
18 (defvar realgud:pydbgr-minibuffer-history nil
19 "minibuffer history list for the command `pydbgr'.")
21 (easy-mmode-defmap pydbgr-minibuffer-local-map
22 '(("\C-i" . comint-dynamic-complete-filename))
23 "Keymap for minibuffer prompting of gud startup command."
24 :inherit minibuffer-local-map)
26 ;; FIXME: I think this code and the keymaps and history
27 ;; variable chould be generalized, perhaps via a macro.
28 (defun pydbgr-query-cmdline (&optional opt-debugger)
29 (realgud-query-cmdline
30 'pydbgr-suggest-invocation
31 pydbgr-minibuffer-local-map
32 'realgud:pydbgr-minibuffer-history
35 (defun pydbgr-parse-cmd-args (orig-args)
36 "Parse command line ARGS for the annotate level and name of script to debug.
38 ARGS should contain a tokenized list of the command line to run.
40 We return the a list containing
41 - the command processor (e.g. python) and it's arguments if any - a list of strings
42 - the name of the debugger given (e.g. pydbgr) 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
46 For example for the following input
47 (map 'list 'symbol-name
48 '(python2.6 -O -Qold --emacs ./gcd.py a b))
51 ((python2.6 -O -Qold) (pydbgr --emacs) (./gcd.py a b) 't)
53 NOTE: the above should have each item listed in quotes.
56 ;; Parse the following kind of pattern:
57 ;; [python python-options] pydbgr pydbgr-options script-name script-options
60 (pair) ;; temp return from
61 (python-opt-two-args '("c" "m" "Q" "W"))
62 ;; Python 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"
66 ;; One dash is added automatically to the below, so
67 ;; h is really -h and -host is really --host.
68 (pydbgr-two-args '("x" "-command" "e" "-execute"
69 "o" "-output" "t" "-target"
71 (pydbgr-opt-two-args '())
73 (if (member system-type (list 'windows-nt 'cygwin 'msdos))
74 "^python[-0-9.]*\\(.exe\\)?$"
81 (interpreter-args '())
87 ;; Got nothing: return '(nil, nil)
88 (list interpreter-args debugger-args script-args annotate-p)
90 ;; Strip off optional "python" or "python182" etc.
91 (when (string-match interp-regexp
92 (file-name-sans-extension
93 (file-name-nondirectory (car args))))
94 (setq interpreter-args (list (pop args)))
96 ;; Strip off Python-specific options
98 (string-match "^-" (car args)))
99 (setq pair (realgud-parse-command-arg
100 args python-two-args python-opt-two-args))
101 (nconc interpreter-args (car pair))
102 (setq args (cadr pair))))
104 ;; Remove "pydbgr" from "pydbgr --pydbgr-options script
106 (setq debugger-name (file-name-sans-extension
107 (file-name-nondirectory (car args))))
108 (unless (string-match "^\\(pydbgr\\|cli.py\\)$" debugger-name)
110 "Expecting debugger name `%s' to be `pydbgr' or `cli.py'"
112 (setq debugger-args (list (pop args)))
114 ;; Skip to the first non-option argument.
115 (while (and args (not script-name))
116 (let ((arg (car args)))
118 ;; Annotation or emacs option with level number.
119 ((or (member arg '("--annotate" "-A"))
120 (equal arg "--emacs"))
122 (nconc debugger-args (list (pop args))))
123 ;; Combined annotation and level option.
124 ((string-match "^--annotate=[0-9]" arg)
125 (nconc debugger-args (list (pop args)) )
127 ;; Options with arguments.
128 ((string-match "^-" arg)
129 (setq pair (realgud-parse-command-arg
130 args pydbgr-two-args pydbgr-opt-two-args))
131 (nconc debugger-args (car pair))
132 (setq args (cadr pair)))
133 ;; Anything else must be the script to debug.
134 (t (setq script-name arg)
135 (setq script-args args))
137 (list interpreter-args debugger-args script-args annotate-p))))
139 ;; To silence Warning: reference to free variable
140 (defvar realgud:pydbgr-command-name)
142 (defun pydbgr-suggest-invocation (debugger-name)
143 "Suggest a pydbgr command invocation via `realgud-suggest-invocaton'"
144 (realgud-suggest-invocation realgud:pydbgr-command-name
145 realgud:pydbgr-minibuffer-history
148 (defun pydbgr-reset ()
149 "Pydbgr cleanup - remove debugger's internal buffers (frame,
152 ;; (pydbgr-breakpoint-remove-all-icons)
153 (dolist (buffer (buffer-list))
154 (when (string-match "\\*pydbgr-[a-z]+\\*" (buffer-name buffer))
155 (let ((w (get-buffer-window buffer)))
158 (kill-buffer buffer))))
160 ;; (defun pydbgr-reset-keymaps()
161 ;; "This unbinds the special debugger keys of the source buffers."
163 ;; (setcdr (assq 'pydbgr-debugger-support-minor-mode minor-mode-map-alist)
164 ;; pydbgr-debugger-support-minor-mode-map-when-deactive))
167 (defun realgud:pydbgr-customize ()
168 "Use `customize' to edit the settings of the `pydbgr' debugger."
170 (customize-group 'realgud:pydbgr))
172 (provide-me "realgud-pydbgr-")