]> code.delx.au - gnu-emacs-elpa/blob - packages/realgud/realgud/debugger/nodejs/core.el
9cc5aa2878469f8d24ee5e5963a213dd038b7ccc
[gnu-emacs-elpa] / packages / realgud / realgud / debugger / nodejs / core.el
1 ;;; Copyright (C) 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")
6 "realgud-")
7 (require-relative-list '("init") "realgud:nodejs-")
8
9 (declare-function realgud:expand-file-name-if-exists 'realgud-core)
10 (declare-function realgud-parse-command-arg 'realgud-core)
11 (declare-function realgud-query-cmdline 'realgud-core)
12 (declare-function realgud-suggest-invocation 'realgud-core)
13
14 ;; FIXME: I think the following could be generalized and moved to
15 ;; realgud-... probably via a macro.
16 (defvar realgud:nodejs-minibuffer-history nil
17 "minibuffer history list for the command `nodejs'.")
18
19 (easy-mmode-defmap realgud:nodejs-minibuffer-local-map
20 '(("\C-i" . comint-dynamic-complete-filename))
21 "Keymap for minibuffer prompting of nodejs startup command."
22 :inherit minibuffer-local-map)
23
24 ;; FIXME: I think this code and the keymaps and history
25 ;; variable chould be generalized, perhaps via a macro.
26 (defun nodejs-query-cmdline (&optional opt-debugger)
27 (realgud-query-cmdline
28 'realgud:nodejs-suggest-invocation
29 realgud:nodejs-minibuffer-local-map
30 'realgud:nodejs-minibuffer-history
31 opt-debugger))
32
33 ;;; FIXME: DRY this with other *-parse-cmd-args routines
34 (defun nodejs-parse-cmd-args (orig-args)
35 "Parse command line ORIG-ARGS for the annotate level and name of script to debug.
36
37 ORIG-ARGS should contain a tokenized list of the command line to run.
38
39 We return the a list containing
40 * the name of the debugger given (e.g. nodejs) 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 '(node --interactive --debugger-port 5858 /tmp nodejs ./gcd.js a b))
47
48 we might return:
49 ((\"node\" \"--interactive\" \"--debugger-port\" \"5858\") nil (\"/tmp/gcd.js\" \"a\" \"b\"))
50
51 Note that path elements have been expanded via `expand-file-name'.
52 "
53
54 ;; Parse the following kind of pattern:
55 ;; node nodejs-options script-name script-options
56 (let (
57 (args orig-args)
58 (pair) ;; temp return from
59 (node-two-args '("-debugger_port" "C" "D" "i" "l" "m" "-module" "x"))
60 ;; node doesn't have any optional two-arg options
61 (node-opt-two-args '())
62
63 ;; One dash is added automatically to the below, so
64 ;; h is really -h and -debugger_port is really --debugger_port.
65 (nodejs-two-args '("-debugger_port"))
66 (nodejs-opt-two-args '())
67
68 ;; Things returned
69 (script-name nil)
70 (debugger-name nil)
71 (interpreter-args '())
72 (script-args '())
73 )
74 (if (not (and args))
75 ;; Got nothing: return '(nil, nil, nil)
76 (list interpreter-args nil script-args)
77 ;; else
78 (progn
79 ;; Remove "nodejs" (or "nodemon" or "node") from invocation like:
80 ;; nodejs --nodejs-options script --script-options
81 (setq debugger-name (file-name-sans-extension
82 (file-name-nondirectory (car args))))
83 (unless (string-match "^node\\(?:js\\|mon\\)$" debugger-name)
84 (message
85 "Expecting debugger name `%s' to be `node', `nodemon', or `nodejs'"
86 debugger-name))
87 (setq interpreter-args (list (pop args)))
88
89 ;; Skip to the first non-option argument.
90 (while (and args (not script-name))
91 (let ((arg (car args)))
92 (cond
93 ((equal "debug" arg)
94 (nconc interpreter-args (list arg))
95 (setq args (cdr args))
96 )
97
98 ;; Options with arguments.
99 ((string-match "^-" arg)
100 (setq pair (realgud-parse-command-arg
101 args nodejs-two-args nodejs-opt-two-args))
102 (nconc interpreter-args (car pair))
103 (setq args (cadr pair)))
104 ;; Anything else must be the script to debug.
105 (t (setq script-name (realgud:expand-file-name-if-exists arg))
106 (setq script-args (cons script-name (cdr args))))
107 )))
108 (list interpreter-args nil script-args)))
109 ))
110
111 ;; To silence Warning: reference to free variable
112 (defvar realgud:nodejs-command-name)
113
114 (defun realgud:nodejs-suggest-invocation (debugger-name)
115 "Suggest a nodejs command invocation via `realgud-suggest-invocaton'"
116 (realgud-suggest-invocation realgud:nodejs-command-name
117 realgud:nodejs-minibuffer-history
118 "js" "\\.js$"))
119
120 (defun realgud:nodejs-remove-ansi-shmutz()
121 "Remove ASCII escape sequences that node.js 'decorates' in
122 prompts and interactive output with"
123 (add-to-list
124 'comint-preoutput-filter-functions
125 (lambda (output)
126 (replace-regexp-in-string "\033\\[[0-9]+[GKJ]" "" output)))
127 )
128
129 (defun realgud:nodejs-reset ()
130 "Nodejs cleanup - remove debugger's internal buffers (frame,
131 breakpoints, etc.)."
132 (interactive)
133 ;; (nodejs-breakpoint-remove-all-icons)
134 (dolist (buffer (buffer-list))
135 (when (string-match "\\*nodejs-[a-z]+\\*" (buffer-name buffer))
136 (let ((w (get-buffer-window buffer)))
137 (when w
138 (delete-window w)))
139 (kill-buffer buffer))))
140
141 ;; (defun nodejs-reset-keymaps()
142 ;; "This unbinds the special debugger keys of the source buffers."
143 ;; (interactive)
144 ;; (setcdr (assq 'nodejs-debugger-support-minor-mode minor-mode-map-alist)
145 ;; nodejs-debugger-support-minor-mode-map-when-deactive))
146
147
148 (defun realgud:nodejs-customize ()
149 "Use `customize' to edit the settings of the `nodejs' debugger."
150 (interactive)
151 (customize-group 'realgud:nodejs))
152
153 (provide-me "realgud:nodejs-")