Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 526c58b19f
Fetching contributors…

Cannot retrieve contributors at this time

973 lines (866 sloc) 35.056 kb
;;; kite-console.el --- Kite console module implementation
;; Copyright (C) 2012 Julian Scheid
;; Author: Julian Scheid <>
;; Keywords: tools
;; Package: kite
;; Compatibility: GNU Emacs 24
;; This file is not part of GNU Emacs.
;; Kite is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; Kite is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; License for more details.
;; You should have received a copy of the GNU General Public License
;; along with Kite. If not, see <>.
;;; Commentary:
;; This package implements the WebKit inspector console.
;; It is part of Kite, a WebKit inspector front-end.
;;; Code:
(require 'cl)
(require 'kite-debug)
(require 'kite-dom)
(require 'kite-global)
(require 'kite-object)
(require 'kite-util)
(require 'font-lock)
(require 'comint)
(require 'widget)
(require 'js) ; for syntax highlighting
(require 'rx))
(defface kite-log-warning
'((t :inherit warning))
"Basic face used to highlight warnings."
:version "24.1"
:group 'kite-faces)
(defface kite-log-error
'((t :inherit error))
"Basic face used to highlight errors."
:version "24.1"
:group 'kite-faces)
(defface kite-log-debug
'((t :inherit font-lock-comment))
"Basic face used to highlight debug-level messages."
:version "24.1"
:group 'kite-faces)
(defface kite-log-log
'((t :inherit default))
"Basic face used to highlight regular messages."
:version "24.1"
:group 'kite-faces)
(defface kite-object
'((t :inherit font-lock-variable-name))
"Face used to highlight object references."
:version "24.1"
:group 'kite-faces)
(defface kite-number
'((t :inherit nxml-char-ref-number))
"Face used to highlight numbers."
:version "24.1"
:group 'kite-faces)
(defface kite-boolean
'((t :inherit nxml-char-ref-number))
"Face used to highlight boolean values."
:version "24.1"
:group 'kite-faces)
(defface kite-null
'((t :inherit nxml-char-ref-number))
"Face used to highlight null values."
:version "24.1"
:group 'kite-faces)
(defface kite-undefined
'((t :inherit nxml-char-ref-number))
"Face used to highlight undefined values."
:version "24.1"
:group 'kite-faces)
(defface kite-string
'((t :inherit font-lock-string))
"Face used to highlight strings."
:version "24.1"
:group 'kite-faces)
(defface kite-quote
'((t :inherit font-lock-keyword))
"Face used to highlight quotes around strings."
:version "24.1"
:group 'kite-faces)
(defface kite-loading
'((t :inherit font-lock-comment))
"Face used to highlight loading indicator."
:version "24.1"
:group 'kite-faces)
(defface kite-property-name
'((t :inherit default))
"Face used to highlight null values."
:version "24.1"
:group 'kite-faces)
(defface kite-proto-property-name
`((t :inherit default
:foreground ,(kite--dimmed-face-foreground 'default 0.5)))
"Face used to highlight null values."
:version "24.1"
:group 'kite-faces)
(defface kite-console-prompt-face
'((t :inherit default))
"Face used to highlight the prompt."
:version "24.1"
:group 'kite-faces)
(defface kite-stack-error-type
'((t :inherit error))
"Face used to highlight error types in stack traces."
:version "24.1"
:group 'kite-faces)
(defface kite-stack-error-message
'((t :inherit default))
"Face used to highlight error messages in stack traces."
:version "24.1"
:group 'kite-faces)
(defface kite-stack-function-name
'((t :inherit font-lock-function-name-face))
"Face used to highlight function names in stack traces."
:version "24.1"
:group 'kite-faces)
(defface kite-stack-pseudo-file-name
'((t :inherit default))
"Face used to highlight file names in stack traces."
:version "24.1"
:group 'kite-faces)
(defface kite-stack-file-name
'((t :inherit link))
"Face used to highlight file names in stack traces."
:version "24.1"
:group 'kite-faces)
(defface kite-stack-line-number
'((t :inherit kite-number))
"Face used to highlight line numbers in stack traces."
:version "24.1"
:group 'kite-faces)
(defface kite-stack-column-number
'((t :inherit kite-number))
"Face used to highlight column numbers in stack traces."
:version "24.1"
:group 'kite-faces)
(defconst kite-level-prefixes
'(("warning" . "WARNING: ")
("error" . "ERROR: "))
"Prefix strings by message log level")
(defcustom kite-console-log-max 1000
"Maximum number of lines to keep in the kite console log buffer.
If nil, disable console logging. If t, log messages but don't
truncate the buffer when it becomes large."
:group 'kite)
(defcustom kite-console-prompt "JS> "
"Prompt used in kite-console."
:group 'kite)
(defcustom kite-console-on-reload-function
(function kite-insert-page-break)
"A function called with no arguments when the page is reloaded,
with the message buffer as the current buffer, point placed at
the end of the buffer, and read-only-ness inhibited. The
default value `kite-insert-page-break' does just that, insert a
page break. To mimic the behaviour of the WebKit debugger
frontend, set this function to `erase-buffer'."
:group 'kite)
(defvar kite-console-prompt-internal
(propertize "JS> " 'font-lock-face 'kite-console-prompt-face)
"Stored value of `kite-console-prompt' in the current kite-console
This is an internal variable used by kite-console. Its purpose
is to prevent a running kite-console process from being messed up
when the user customizes `kite-console-prompt'.")
(defvar kite-console-mode-map
(let ((map (copy-keymap widget-keymap))
(menu-map (make-sparse-keymap)))
;;(suppress-keymap map t)
(kite--define-global-mode-keys map)
(define-key map "\t" 'kite-async-completion-at-point)
(define-key map "\C-cX" 'kite-clear-console)
(define-key map "\C-cg" 'kite-console-visit-source)
(define-key map "\C-ci" 'kite-show-log-entry)
(define-key map "\C-j" 'kite-console-send-input)
(define-key map (kbd "RET") 'kite-console-send-input-or-visit-object)
"Local keymap for `kite-console-mode' buffers.")
(defvar kite-console-input)
(defvar kite-message-group-level)
(defvar kite-console-line-count)
(defvar kite-console-header
"// Welcome to Kite Console. `M-x describe-mode' for help.\n"
"Message to display when kite-console is started.")
(define-derived-mode kite-console-mode comint-mode "kite-console"
"Toggle kite console mode."
:group 'kite
(add-hook 'kill-buffer-hook 'kite--kill-console nil t)
(set (make-local-variable 'kite-message-group-level) 0)
(set (make-local-variable 'kite-console-line-count) 0)
(setq case-fold-search nil)
(set (make-local-variable 'widget-link-prefix) "")
(set (make-local-variable 'widget-link-suffix) "")
;; Below code is adapted from ielm.el
(setq comint-prompt-regexp
(concat "^" (regexp-quote kite-console-prompt)))
(setq comint-input-sender 'kite-console-input-sender)
(setq comint-get-old-input 'kite-console-get-old-input)
(set (make-local-variable 'comint-prompt-read-only) t)
;; A dummy process to keep comint happy. It will never get any input
(unless (comint-check-proc (current-buffer))
(condition-case nil
(start-process "kite-console"
(file-error (start-process "kite-console"
(set-process-query-on-exit-flag (kite-console-process) nil)
(goto-char (point-max))
;; JavaScript output can include raw characters that confuse
;; comint's carriage control code.
(set (make-local-variable 'comint-inhibit-carriage-motion) t)
(set (make-local-variable 'font-lock-defaults)
(list js--font-lock-keywords))
;; Add a silly header
(insert (propertize kite-console-header
'face 'font-lock-comment
'font-lock-face 'font-lock-comment))
(kite-console-set-pm (point-max))
(unless comint-use-prompt-regexp
(let ((inhibit-read-only t))
(point-min) (point-max)
(list 'rear-nonsticky t
'field 'output
'inhibit-line-move-field-capture t))))
(comint-output-filter (kite-console-process)
(set-marker comint-last-input-start (kite-console-pm))
(set-process-filter (get-buffer-process (current-buffer))
(kite-send "Console.enable")
(run-mode-hooks 'kite-console-mode-hook))
(defun kite--kill-console ()
"Called when a session's console buffer is closed. Disables
console logging in the remote debugger by sending the
`Console.disable' message."
(kite-send "Console.disable")))
(defun kite--message-repeat-text (repeat-count)
"Return a string to be used as a suffix for messages with the
given REPEAT-COUNT. Return nil for a repeat count less than or
equal to 1, a human-readable string otherwise. The returned
string has the `kite-repeat-count' text property set so that it
can be updated later on."
(and repeat-count
(> repeat-count 1)
(format " [message repeated %d times]" repeat-count)
'kite-repeat-count t)))
(defun kite--console-replace-object-async
(result object-plist buffer-point longp)
"Replace a previously inserted simple object representation
with a more detailed representation after receiving additional
data from the server. RESULT is the JSON-RPC result received
from the server. OBJECT-PLIST is a plist describing the message
parameter for which the request was sent. BUFFER-POINT is a
marker at which the temporary placeholder is located."
(let* ((text-prop-start (text-property-any
(plist-get object-plist :objectId)))
(text-prop-end (next-single-property-change
(log-message (get-text-property text-prop-start 'log-message)))
(when (and text-prop-start text-prop-end)
(let ((inhibit-read-only t))
(delete-region text-prop-start text-prop-end)
(goto-char text-prop-start)
((string= (plist-get object-plist :subtype) "array")
(insert (kite--format-array
(plist-get result :result))))
((string= (plist-get object-plist :subtype) "node")
:kite-object-id (plist-get object-plist :objectId)
:button-face 'kite-object
:notify (lambda (widget &rest ignore)
(widget-get widget :kite-object-id)))
(plist-get object-plist :description)))
((null (plist-get object-plist :subtype))
:kite-object-id (plist-get object-plist :objectId)
:kite-object-description (plist-get
:button-face 'kite-object
:notify (lambda (widget &rest ignore)
(widget-get widget :kite-object-id)
(widget-get widget :kite-object-description)))
(if longp
(plist-get result :result))
(plist-get object-plist :description))))
(insert "UNKNOWN")))
(put-text-property text-prop-start
(defun kite--console-format-object (object-plist &optional longp)
"Return a propertized string representation of OBJECT-PLIST,
where OBJECT-PLIST is a raw short object description plist as
sent by the remote debugger, for example as part of a log message
If LONGP is t, show a more detailed description of the object by
including its properties.
For JavaScript objects and arrays, additional data is fetched
from the remote debugger asynchronously and the returned
representation will eventually be replaced with a more detailed
one by calling `kite--console-replace-object-async'.
The returned string must be inserted into the current buffer so
that `kite--console-replace-object-async' can locate it for
(when (and (not (plist-member object-plist :result))
(plist-member object-plist :objectId)
(string= (plist-get object-plist :type) "object"))
(lexical-let ((object-plist object-plist)
(longp longp)
(buffer-point (point-marker)))
;; Optimization: receiving an array with thousands or even
;; millions of elements, or an object with thousands of
;; properties can take a very long time, most of all because of
;; JSON serialization. Therefore, only fetch the
;; elements/properties necessary for printing a compact
;; representation.
(list :objectId (plist-get object-plist :objectId)
(format "\
function f() { \
if (this instanceof Array) { \
return this.slice(0, %d); \
} \
else if (this instanceof Object) {
obj = {}; \
count = 0; \
for (key in this) { \
obj[key] = this[key]; \
if (++count >= %d) break; \
}; \
return obj; \
} \
else { \
return this; \
} \
;; Fetch one item more than necessary so that
;; kite--format-array and
;; kite--format-object-with-props know when to
;; insert an ellipsis at the end.
(1+ kite-short-array-max-elements)
(1+ kite-short-object-max-properties))
:arguments [])
(lambda (result)
(list :objectId (kite--get result
:ownProperties t)
(lambda (result)
(kite--console-replace-object-async result
(kite--format-object object-plist))
(defun kite--console-format-message (message)
"Format the console message described by MESSAGE at point.
MESSAGE is the raw message plist as received from the remote
(let* ((parameters (plist-get message :parameters))
(arg-index (if (string= (kite--get parameters 0 :type)
1 0)))
(make-string (* 2 kite-message-group-level) 32)
(let ((kite-level-prefix
(cdr (assoc (plist-get message :level)
(when kite-level-prefix
'font-lock-face (intern
(format "kite-log-%s"
(plist-get message :level))))))
(when (or (> arg-index 0) (null parameters))
(lambda (string)
(let ((object (elt parameters arg-index)))
(if object
(kite--console-format-object object)
(setq arg-index (1+ arg-index))))
(plist-get message :text)
t ; fixed-case
t ; literal
2)) ; subexp
(let (extra-args)
(while (< arg-index (length parameters))
(setq extra-args
(concat extra-args
(when (> arg-index 0) " ")
(elt parameters arg-index)
(setq arg-index (1+ arg-index)))
(when (plist-get message :repeatCount)
(plist-get message :repeatCount)))))
'log-message message))
(defun kite--console-messageAdded (websocket-url packet)
"Callback invoked for `Console.messageAdded' notifications
received from the remote debugger."
(let* ((buf (kite--find-buffer websocket-url 'console))
(message (plist-get packet :message))
(message-type (plist-get message :type)))
((string= message-type "startGroup")
(setq kite-message-group-level
(+ kite-message-group-level 1)))
((string= message-type "endGroup")
(setq kite-message-group-level
(- kite-message-group-level 1))))
(when (and kite-console-log-max
(> (length (plist-get message :text)) 0))
(with-current-buffer buf
(let ((inhibit-read-only t)
(keep-at-end (and (eq (point) (point-max))
(not (eq (point) (point-min))))))
(when (numberp kite-console-log-max)
(while (>= kite-console-line-count kite-console-log-max)
(goto-char (point-min))
(delete-region (point-min) (point))
(setq kite-console-line-count
(- kite-console-line-count 1))))
(insert (concat "\n"
(kite--console-format-message message))))
(setq kite-console-line-count (1+ kite-console-line-count)))
(kite--log "message added, url is %s, packet is %s"
(defun kite-clear-console ()
"Clear the console locally and in the remote debugger, the
latter by sending a `Console.clearMessages' message."
(let ((inhibit-read-only t))
(comint-output-filter (kite-console-process)
(goto-char (kite-console-pm))
(kite-send "Console.clearMessages"))
(defun kite-console-visit-source ()
"Visit the JavaScript source where the console message at point
originated, or raise an error if the source is unknown or
(unless (eq kite-buffer-type 'console)
(error "Not in a kite console buffer"))
(let ((stack-frame
(or (elt (plist-get
(get-text-property (point) 'log-message)
(get-text-property (point) 'kite-source))))
(if stack-frame
(kite-visit-stack-frame stack-frame)
(error "No source location available for this log message")))))
(defun kite-show-log-entry ()
"Show details about the console message under cursor in a
temporary buffer.
FIXME: this should print the message using the same code as
output into the main console buffer.
FIXME: this could use nicer formatting."
(let ((log-message (get-text-property (point) 'log-message)))
(with-output-to-temp-buffer "*kite log message*"
(princ (format (concat
"Origin: %s:%s\n"
"Source: %s\n"
"Level: %s\n"
"Repeat Count: %s\n"
"Message:\n\n%s\nStack Trace:\n\n%s")
(plist-get log-message :url)
(plist-get log-message :line)
(plist-get log-message :source)
(plist-get log-message :level)
(plist-get log-message :repeatCount)
(kite--console-format-message log-message)
(plist-get log-message :stackTrace))
(defun kite-insert-page-break ()
"Insert a newline and page break character. Default for
(insert "\n\f"))
(defun kite--console-goto-last-message ()
"Put point after the last message printed"
(goto-char (kite-console-pm))
(let ((inhibit-field-text-motion t))
(defun kite--console-globalObjectCleared (websocket-url packet)
"Callback invoked for `Console.globalObjectCleared'
notifications received from the remote debugger."
(let ((buf (kite--find-buffer websocket-url 'console)))
(when buf
(with-current-buffer buf
(let ((inhibit-read-only t))
(funcall kite-console-on-reload-function)))))))
(defun kite--console-messageRepeatCountUpdated (websocket-url packet)
"Callback invoked for `Console.messageRepeatCountUpdated'
notifications received from the remote debugger."
(let ((buf (kite--find-buffer websocket-url 'console)))
(when buf
(with-current-buffer buf
(goto-char (kite-console-pm))
(let ((inhibit-field-text-motion t))
(while (and (null (get-text-property (point) 'log-message))
(> (point) (point-min)))
(forward-line -1)))
(let ((inhibit-read-only t)
(text-prop-start (text-property-any
(if text-prop-start
(let ((text-prop-end (next-single-property-change
(delete-region text-prop-start text-prop-end)
(goto-char text-prop-start))
(insert (kite--message-repeat-text
(plist-get packet :count)))))))))
(defun kite--format-stacktrace (stacktrace)
"Return the given STACKTRACE as a formatted string. STACKTRACE
should be the raw stack trace plist as received from the remote
FIXME: this is incomplete.
FIXME: this should be consolidated with
(let ((formatted "") (index 0))
(while (< index (length stacktrace))
(let ((stackframe (elt stacktrace index)))
(setq formatted
(concat formatted
(format "%s:%s:%s(%s)"
(plist-get stackframe :url)
(plist-get stackframe :lineNumber)
(plist-get stackframe :columnNumber)
(plist-get stackframe :functionName))))
(setq index (1+ index))))
(defun kite-console-input-sender (_proc input)
;; Just sets the variable kite-console-input, which is in the scope
;; of `kite-console-send-input's call.
(setq kite-console-input input))
(defun kite-console-send-input ()
"Evaluate the current console prompt input."
(let (kite-console-input) ; set by
; kite-console-input-sender
(comint-send-input) ; update history, markers etc.
(kite-console-eval-input kite-console-input)))
(defun kite--is-whitespace-or-comment (string)
"Return non-nil if STRING is all whitespace or a comment."
(or (string= string "")
(string-match-p "\\`[ \t\n]*\\(?:\/\/.*\\)*\\'" string)))
(defun kite-console-eval-input (input)
"Evaluate console input: send it to the remote debugger and
insert the result or error message, along with a new prompt, into
the buffer."
(unless (kite--is-whitespace-or-comment input)
(list :expression input
:contextId (plist-get (kite-session-current-context
(lambda (result)
(if (eq :json-false (plist-get result :wasThrown))
(concat (kite--console-format-object
(plist-get result :result)
(kite--get result :result :objectId)
(lambda (stack-trace)
(concat stack-trace
(let ((object-id
(kite--get result :result :objectId)))
(when object-id
(kite--release-object object-id)))))))
(defun kite--contexts-by-unique-name (context-and-frame-list)
. CONTEXT-PLIST FRAME-PLIST), return an alist
FIXME: This does not yet ensure that the returned name is
(lambda (context-and-frame)
(let* ((context-id (car context-and-frame))
(context (nth 0 (cdr context-and-frame)))
(frame (nth 1 (cdr context-and-frame)))
(context-name (plist-get context :name)))
;; Use given context name if available
((not (string= context-name ""))
;; Otherwise, fall back to frame URL
(plist-get frame :url)))
(defun kite--session-contexts-by-unique-name ()
(lambda (context)
(cons context
(list context (kite--frame-by-id
(plist-get context :frameId)))))
(kite-session-execution-context-list kite-session))))
(defvar kite-context-history)
(defun kite-set-context ()
"Interactively select a JavaScript execution context for the
(let* ((contexts-by-unique-name
(selection (completing-read "Context: "
(mapcar (function car)
(new-context (cdr (assoc selection
(when new-context
(setf (kite-session-current-context kite-session)
;;; Process and marker utilities
(defun kite-console-process ()
;; Return the current buffer's process.
(get-buffer-process (current-buffer)))
(defun kite-console-pm ()
;; Return the process mark of the current buffer.
(process-mark (get-buffer-process (current-buffer))))
(defun kite-console-set-pm (pos)
;; Set the process mark in the current buffer to POS.
(set-marker (process-mark (get-buffer-process (current-buffer))) pos))
(defun kite-console-get-old-input nil
;; Return the previous input surrounding point
(unless (looking-at-p comint-prompt-regexp)
(re-search-backward comint-prompt-regexp))
(buffer-substring (point) (progn (forward-sexp 1) (point)))))
(defun kite--console-update-mode-line ()
"Update the console buffer mode line display. Should be
invoked after execution context changes."
(let ((buf (kite--find-buffer
(kite-session-websocket kite-session))
(when buf
(with-current-buffer buf
(setq mode-line-process nil)
(let ((current-context (kite-session-current-context
(unique-names (kite--session-contexts-by-unique-name)))
(propertized-buffer-identification "%b")
(when current-context
"%" "%%"
" (%s)"
(car (rassq current-context
(defun kite--console-execution-context-created (websocket-url packet)
(defun kite--get-properties-fast (object-expr js-regex callback)
"Efficiently and asynchronously fetch matching property names
for the object resulting from evaluating OBJECT-EXPR, a
JavaScript expression. Only properties matching JS-REGEX, a
regular expression using JavaScript syntax, are fetched. The
resulting property names are passed as an unsorted list of
strings to CALLBACK, which should accept a single parameter.
FIXME: no error handling."
(lexical-let ((lex-js-regex js-regex)
(lex-callback callback))
(list :expression object-expr
:contextId (plist-get (kite-session-current-context
(lambda (result)
(lexical-let ((object-id (kite--get result
(list :objectId object-id
:functionDeclaration "\
function f(regex_str) {
result = [];
regex = new RegExp(regex_str);
for (key in this) {
if (regex.test(key)) {
return result.join(\";\");
:arguments `[ ( :value ,lex-js-regex ) ])
(lambda (result)
(kite--release-object object-id)
(funcall lex-callback
(split-string (kite--get result
(defun kite-async-completion-at-point ()
"Asynchronously fetch completions for the JavaScript expression
at point and, once results have arrived, perform completion using
Note: we can't use the usual mechanism of hooking into the
completions API (`completion-at-point-functions') because it
doesn't support asynchronicity."
(let (completion-begin)
;; Find the dotted JavaScript expression (consisting of
;; identifiers only) before point. Note that we can't use just a
;; single regex because greedy regexes don't work when searching
;; backwards.
(while (re-search-backward kite--identifier-part-regexp nil t))
(setq completion-begin (point))))
;; FIXME: the previous step is too broad, it will find identifiers
;; starting with a digit. Could do a second pass here to make
;; sure that we're looking at a valid expression, or improve error
;; handling in `kite--get-properties-fast' to ensure that we do
;; the right thing when the JavaScript side gets back to us with a
;; complaint.
(when (< completion-begin (point))
(let* ((components (split-string (buffer-substring-no-properties
(last-component (car (last components))))
(lexical-let ((lex-completion-begin (- (point)
(length last-component)))
(lex-completion-end (point)))
(if (> (length components) 1)
(mapconcat 'identity
(subseq components
(- (length components) 1))
(concat "^" (regexp-quote last-component))
(lambda (completions)
(let* (completion-extra-properties
(defun kite-console-send-input-or-visit-object ()
"If point is on a widget, activate the widget. Otherwise,
evaluate console input."
(let ((widget (widget-at (point))))
(if widget
(widget-apply-action widget nil)
(defun kite-visit-dom-node (object-id)
"Open the DOM buffer and move point to the node corresponding
to the given OBJECT-ID."
(kite-send "DOM.requestNode"
(list :objectId object-id)
(lambda (result)
(lexical-let ((lex-node-id (plist-get result :nodeId)))
(websocket-url (kite-session-websocket kite-session))
(lambda ()
(add-hook 'kite-Console-messageAdded-hooks
(add-hook 'kite-Console-messageRepeatCountUpdated-hooks
(add-hook 'kite-Debugger-globalObjectCleared-hooks
(add-hook 'kite-Runtime-executionContextCreated-hooks
(provide 'kite-console)
;;; kite-console.el ends here
Jump to Line
Something went wrong with that request. Please try again.