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