1 ;;; Copyright (C) 2014 Rocky Bernstein <rocky@gnu.org>
2 (eval-when-compile (require 'cl))
4 (require 'load-relative)
5 (require-relative-list '("../../common/track" "../../common/core")
7 (require-relative-list '("init") "realgud:nodejs-")
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)
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'.")
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)
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
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.
37 ORIG-ARGS should contain a tokenized list of the command line to run.
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
44 For example for the following input:
45 (map 'list 'symbol-name
46 '(node --interactive --debugger-port 5858 /tmp nodejs ./gcd.js a b))
49 ((\"node\" \"--interactive\" \"--debugger-port\" \"5858\") nil (\"/tmp/gcd.js\" \"a\" \"b\"))
51 Note that path elements have been expanded via `expand-file-name'.
54 ;; Parse the following kind of pattern:
55 ;; node nodejs-options script-name script-options
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 '())
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 '())
71 (interpreter-args '())
75 ;; Got nothing: return '(nil, nil, nil)
76 (list interpreter-args nil script-args)
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)
85 "Expecting debugger name `%s' to be `node', `nodemon', or `nodejs'"
87 (setq interpreter-args (list (pop args)))
89 ;; Skip to the first non-option argument.
90 (while (and args (not script-name))
91 (let ((arg (car args)))
94 (nconc interpreter-args (list arg))
95 (setq args (cdr args))
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))))
108 (list interpreter-args nil script-args)))
111 ;; To silence Warning: reference to free variable
112 (defvar realgud:nodejs-command-name)
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
120 (defun realgud:nodejs-remove-ansi-shmutz()
121 "Remove ASCII escape sequences that node.js 'decorates' in
122 prompts and interactive output with"
124 'comint-preoutput-filter-functions
126 (replace-regexp-in-string "\033\\[[0-9]+[GKJ]" "" output)))
129 (defun realgud:nodejs-reset ()
130 "Nodejs cleanup - remove debugger's internal buffers (frame,
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)))
139 (kill-buffer buffer))))
141 ;; (defun nodejs-reset-keymaps()
142 ;; "This unbinds the special debugger keys of the source buffers."
144 ;; (setcdr (assq 'nodejs-debugger-support-minor-mode minor-mode-map-alist)
145 ;; nodejs-debugger-support-minor-mode-map-when-deactive))
148 (defun realgud:nodejs-customize ()
149 "Use `customize' to edit the settings of the `nodejs' debugger."
151 (customize-group 'realgud:nodejs))
153 (provide-me "realgud:nodejs-")