[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 3dfd515 42/79: Messy server implementation.
From: |
Jackson Ray Hamilton |
Subject: |
[elpa] master 3dfd515 42/79: Messy server implementation. |
Date: |
Sun, 14 Jun 2015 00:05:34 +0000 |
branch: master
commit 3dfd515c2d4bae593c155aedf44288840bd4d353
Author: Jackson Ray Hamilton <address@hidden>
Commit: Jackson Ray Hamilton <address@hidden>
Messy server implementation.
---
context-coloring.el | 86 ++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 79 insertions(+), 7 deletions(-)
diff --git a/context-coloring.el b/context-coloring.el
index 0f823d8..b1f5963 100644
--- a/context-coloring.el
+++ b/context-coloring.el
@@ -748,14 +748,22 @@ to colorize the buffer."
(with-silent-modifications
(context-coloring-apply-tokens braceless)))))
+(defvar-local context-coloring-scopifier-cancel-function nil
+ "Kills the current scopification process.")
+
(defvar-local context-coloring-scopifier-process nil
"The single scopifier process that can be running.")
(defun context-coloring-kill-scopifier ()
"Kill the currently-running scopifier process."
- (when (not (null context-coloring-scopifier-process))
- (delete-process context-coloring-scopifier-process)
- (setq context-coloring-scopifier-process nil)))
+ (cond
+ (context-coloring-scopifier-cancel-function
+ (funcall context-coloring-scopifier-cancel-function)
+ (setq context-coloring-scopifier-cancel-function nil))
+ ;; ((not (null context-coloring-scopifier-process))
+ ;; (delete-process context-coloring-scopifier-process)
+ ;; (setq context-coloring-scopifier-process nil))
+ ))
(defun context-coloring-scopify-shell-command (command callback)
"Invoke a scopifier via COMMAND, read its response
@@ -795,6 +803,72 @@ scopifying."
(process-send-eof
context-coloring-scopifier-process))
+(defcustom context-coloring-js-scopifier-server-host "localhost"
+ "Host for the JavaScript scopifier server."
+ :group 'context-coloring)
+
+(defcustom context-coloring-js-scopifier-server-port 6969
+ "Port for the JavaScript scopifier server."
+ :group 'context-coloring)
+
+(defun context-coloring-js-start-scopifier-server (callback)
+ (let (stream)
+ (condition-case nil
+ (progn
+ (setq stream (open-network-stream
+ "scopifier" nil
+ context-coloring-js-scopifier-server-host
+ context-coloring-js-scopifier-server-port))
+ (funcall callback stream))
+ (error
+ (context-coloring-scopify-shell-command
+ (context-coloring-join
+ (list "scopifier"
+ "--server"
+ "--host" context-coloring-js-scopifier-server-host
+ "--post" (number-to-string
+ context-coloring-js-scopifier-server-port))
+ " ")
+ (lambda (_output)
+ (setq stream (open-network-stream
+ "scopifier" nil
+ context-coloring-js-scopifier-server-host
+ context-coloring-js-scopifier-server-port))
+ (funcall callback stream)))))))
+
+(defun context-coloring-send-buffer-to-scopifier-server (callback)
+ ;; TODO: Dispatch configuration.
+ (context-coloring-js-start-scopifier-server
+ (lambda (process)
+ (let* ((body (buffer-substring-no-properties (point-min) (point-max)))
+ (header (concat "POST / HTTP/1.0\r\n"
+ "Host: localhost\r\n"
+ "Content-Type: application/x-www-form-urlencoded"
+ "; charset=UTF8\r\n"
+ (format "Content-Length: %d\r\n" (length body))
+ "\r\n"))
+ (output "")
+ (active t))
+ (set-process-filter
+ process
+ (lambda (_process chunk)
+ (setq output (concat output chunk))))
+ (set-process-sentinel
+ process
+ (lambda (_process event)
+ (when (and (equal "connection broken by remote peer\n" event)
+ active)
+ ;; Strip the response headers.
+ (string-match "\r\n\r\n" output)
+ (setq output (substring-no-properties output (match-end 0)))
+ (funcall callback output))))
+ (process-send-string process (concat header body "\r\n"))
+ (setq context-coloring-scopifier-cancel-function
+ (lambda ()
+ "Cancel this scopification."
+ (setq active nil)))))))
+
+;; TODO: Maybe have a function dedicated to servers instead.
(defun context-coloring-scopify-and-colorize (command &optional callback)
"Invoke a scopifier via COMMAND with the current buffer's contents,
read the scopifier's response asynchronously and apply a parsed
@@ -802,14 +876,12 @@ list of tokens to `context-coloring-apply-tokens'.
Invoke CALLBACK when complete."
(let ((buffer (current-buffer)))
- (context-coloring-scopify-shell-command
- command
+ (context-coloring-send-buffer-to-scopifier-server
(lambda (output)
(with-current-buffer buffer
(context-coloring-parse-array output))
(setq context-coloring-scopifier-process nil)
- (when callback (funcall callback)))))
- (context-coloring-send-buffer-to-scopifier))
+ (when callback (funcall callback))))))
;;; Dispatch
- [elpa] master 5a8186b 34/79: Color comments and strings inside defuns., (continued)
- [elpa] master 5a8186b 34/79: Color comments and strings inside defuns., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 4b25191 28/79: Refactor benchmarks. Improve benchmark reliability., Jackson Ray Hamilton, 2015/06/13
- [elpa] master f3697c0 35/79: Strings are sexps., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 87893df 32/79: Support comma splicing., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 7167e93 19/79: Pass let and let* tests with recursive colorizer., Jackson Ray Hamilton, 2015/06/13
- [elpa] master af4f773 33/79: Colorize comments and strings inside backquotes., Jackson Ray Hamilton, 2015/06/13
- [elpa] master acaaf39 38/79: Add file statistics to benchmarks., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 8836df9 37/79: Add colorization times to logs., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 5661ed0 44/79: Document `:host' and `:port' properties., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 16fa835 40/79: Optimize JSON parser., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 3dfd515 42/79: Messy server implementation.,
Jackson Ray Hamilton <=
- [elpa] master 872bf5f 46/79: Bump required scopifier version., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 2258572 39/79: Optimize. Refactor., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 59492ab 41/79: Refactor token application., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 233ea1a 47/79: Kill any scopifier server before running the tests., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 0079778 48/79: Set `:send-report' to nil., Jackson Ray Hamilton, 2015/06/13
- [elpa] master cacf5de 49/79: Add cond support., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 8d15938 43/79: Cleaner server implementation., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 89286b6 54/79: Cleanup., Jackson Ray Hamilton, 2015/06/13
- [elpa] master 5f9bd49 50/79: Add comments and strings to the cond test., Jackson Ray Hamilton, 2015/06/13
- [elpa] master c2834f0 51/79: Add condition-case support., Jackson Ray Hamilton, 2015/06/13