2014-09-03 07:35:50 +00:00
|
|
|
;; -*- lexical-binding: t; -*-
|
2014-08-14 00:02:49 +00:00
|
|
|
;; Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
;; Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
;;
|
|
|
|
;; Author: Soonho Kong
|
|
|
|
;;
|
|
|
|
(require 'cl-lib)
|
|
|
|
(require 'lean-variable)
|
|
|
|
(require 'lean-cmd)
|
|
|
|
(require 'lean-info)
|
|
|
|
(require 'lean-util)
|
2014-08-25 18:40:37 +00:00
|
|
|
(require 'flycheck)
|
2014-08-14 00:02:49 +00:00
|
|
|
|
|
|
|
;; Parameters
|
|
|
|
;; ==========
|
2014-08-30 14:35:00 +00:00
|
|
|
(defvar-local lean-server-process-name "lean-server")
|
|
|
|
(defvar-local lean-server-buffer-name "*lean-server*")
|
|
|
|
(defvar-local lean-server-trace-buffer-name "*lean-server-trace*")
|
|
|
|
(defvar-local lean-server-option "--server")
|
2014-08-14 00:02:49 +00:00
|
|
|
|
2014-08-25 18:40:37 +00:00
|
|
|
;; Log Function
|
|
|
|
;; ============
|
|
|
|
(defun lean-server-log (format-string &rest args)
|
|
|
|
"Display a message at the bottom of the *lean-server* buffer."
|
|
|
|
(with-current-buffer (lean-server-get-buffer)
|
|
|
|
(goto-char (point-max))
|
2014-08-30 14:35:00 +00:00
|
|
|
(insert (apply 'format (concat format-string "\n") args))))
|
|
|
|
|
|
|
|
;; Trace Function
|
|
|
|
;; ============
|
|
|
|
(defun lean-server-trace (format-string &rest args)
|
|
|
|
"Display a message at the bottom of the *lean-server* buffer."
|
|
|
|
(with-current-buffer
|
|
|
|
(get-buffer-create lean-server-trace-buffer-name)
|
|
|
|
(goto-char (point-max))
|
2014-08-30 14:51:53 +00:00
|
|
|
(when lean-global-server-last-time-sent
|
|
|
|
(let ((time-diff (- (float-time) lean-global-server-last-time-sent)))
|
|
|
|
(insert (format "SLEEP %i\n" (* 1000 time-diff)))))
|
|
|
|
(setq lean-global-server-last-time-sent (float-time))
|
2014-08-30 14:35:00 +00:00
|
|
|
(insert (apply 'format format-string args))))
|
2014-08-25 18:40:37 +00:00
|
|
|
|
2014-08-14 00:02:49 +00:00
|
|
|
;; How to read data from an async process
|
|
|
|
;; ======================================
|
2014-08-25 18:40:37 +00:00
|
|
|
(defconst lean-server-syntax-pattern
|
2014-09-02 17:36:38 +00:00
|
|
|
`((INFO ,(rx line-start "-- BEGININFO" (* not-newline) line-end)
|
2014-08-25 18:40:37 +00:00
|
|
|
,(rx line-start (group "-- ENDINFO") line-end))
|
|
|
|
(SET ,(rx line-start "-- BEGINSET" line-end)
|
|
|
|
,(rx line-start (group "-- ENDSET") line-end))
|
|
|
|
(EVAL ,(rx line-start "-- BEGINEVAL" line-end)
|
2014-09-01 00:46:40 +00:00
|
|
|
,(rx line-start (group "-- ENDEVAL") line-end))
|
|
|
|
(OPTIONS ,(rx line-start "-- BEGINOPTIONS" line-end)
|
|
|
|
,(rx line-start (group "-- ENDOPTIONS") line-end))
|
2014-09-03 07:45:16 +00:00
|
|
|
(SHOW ,(rx line-start "-- BEGINSHOW" line-end)
|
|
|
|
,(rx line-start (group "-- ENDSHOW") line-end))
|
2014-08-25 18:40:37 +00:00
|
|
|
(ERROR ,(rx line-start "-- " (0+ not-newline) line-end)
|
2014-08-26 23:02:40 +00:00
|
|
|
,(rx line-start (group "-- ERROR" (0+ not-newline)) line-end)))
|
2014-08-25 18:40:37 +00:00
|
|
|
"Regular expression pattern for lean-server message syntax")
|
|
|
|
|
|
|
|
(defun lean-server-split-buffer (buf-str beg-regex end-regex)
|
|
|
|
""
|
|
|
|
(let ((beg (string-match beg-regex buf-str))
|
|
|
|
(end (string-match end-regex buf-str))
|
|
|
|
pre body post)
|
|
|
|
(when (and beg end)
|
|
|
|
(setq end (match-end 1))
|
|
|
|
(setq pre (substring-no-properties buf-str 0 beg))
|
|
|
|
(setq body (substring-no-properties buf-str beg end))
|
|
|
|
(setq post (substring-no-properties buf-str end))
|
|
|
|
`(,pre ,body ,post))))
|
|
|
|
|
|
|
|
(defun lean-server-check-buffer-and-partition (buf-str)
|
|
|
|
"Return the status of buffer."
|
|
|
|
(let (result)
|
2014-09-03 07:39:54 +00:00
|
|
|
(when buf-str
|
|
|
|
(cl-loop for (type beg-regex end-regex) in lean-server-syntax-pattern
|
|
|
|
do (setq partition-result (lean-server-split-buffer buf-str beg-regex end-regex))
|
|
|
|
if partition-result
|
|
|
|
return `(,type ,partition-result)))))
|
2014-08-14 00:02:49 +00:00
|
|
|
|
|
|
|
(defun lean-server-process-received-message (buf str)
|
|
|
|
"Process received message from lean-server"
|
2014-08-25 18:40:37 +00:00
|
|
|
(lean-server-log "Received String: %s" str)
|
2014-09-03 07:39:54 +00:00
|
|
|
(setq lean-global-server-buffer (concat lean-global-server-buffer str)))
|
2014-08-14 00:02:49 +00:00
|
|
|
|
|
|
|
(defun lean-server-output-filter (process string)
|
|
|
|
"Filter function attached to lean-server process"
|
|
|
|
(lean-server-process-received-message (process-buffer process) string))
|
|
|
|
|
2014-08-26 23:21:48 +00:00
|
|
|
(defun lean-server-handle-signal (process event)
|
|
|
|
"Handle signals for lean-server-process"
|
|
|
|
(cond
|
|
|
|
((string-prefix-p "hangup" event)
|
2014-09-01 00:46:40 +00:00
|
|
|
(lean-server-initialize-global-vars))
|
|
|
|
((string-prefix-p "killed" event)
|
2014-08-26 23:21:48 +00:00
|
|
|
(lean-server-initialize-global-vars))))
|
|
|
|
|
2014-08-14 00:02:49 +00:00
|
|
|
;; How to create an async process
|
|
|
|
;; ==============================
|
2014-08-26 23:21:48 +00:00
|
|
|
|
|
|
|
(defun lean-server-initialize-global-vars ()
|
|
|
|
"Initialize lean-server related global variables"
|
|
|
|
(setq lean-global-server-buffer nil)
|
|
|
|
(setq lean-global-server-current-file-name nil)
|
2014-08-27 07:48:55 +00:00
|
|
|
(setq lean-global-server-message-to-process nil)
|
2014-08-30 14:51:53 +00:00
|
|
|
(setq lean-global-server-last-time-sent nil)
|
2014-09-03 07:39:54 +00:00
|
|
|
(when (timerp lean-global-retry-timer)
|
|
|
|
(cancel-timer lean-global-retry-timer))
|
|
|
|
(setq lean-global-retry-timer nil))
|
2014-08-26 23:21:48 +00:00
|
|
|
|
2014-08-14 00:02:49 +00:00
|
|
|
(defun lean-server-create-process ()
|
2014-08-14 15:56:07 +00:00
|
|
|
"Create lean-server process."
|
2014-08-21 16:09:54 +00:00
|
|
|
;; (when (buffer-modified-p)
|
|
|
|
;; (error "Please save the buffer before start lean-server."))
|
2014-08-14 00:02:49 +00:00
|
|
|
(let ((process-connection-type nil)
|
|
|
|
(lean-server-process
|
|
|
|
(start-process lean-server-process-name
|
|
|
|
lean-server-buffer-name
|
|
|
|
(lean-get-executable lean-executable-name)
|
|
|
|
lean-server-option)))
|
|
|
|
(set-process-coding-system lean-server-process 'utf-8 'utf-8)
|
|
|
|
(set-process-filter lean-server-process 'lean-server-output-filter)
|
2014-08-26 23:21:48 +00:00
|
|
|
(set-process-sentinel lean-server-process 'lean-server-handle-signal)
|
|
|
|
(lean-server-initialize-global-vars)
|
2014-08-25 18:40:37 +00:00
|
|
|
(setq lean-global-server-process lean-server-process)
|
2014-08-14 00:02:49 +00:00
|
|
|
lean-server-process))
|
|
|
|
|
2014-08-14 15:56:07 +00:00
|
|
|
(defun lean-server-kill-process ()
|
2014-08-25 18:40:37 +00:00
|
|
|
"Kill lean-server process. Return t if killed, nil if nothing to kill"
|
2014-08-14 15:56:07 +00:00
|
|
|
(interactive)
|
2014-08-25 18:40:37 +00:00
|
|
|
(cond
|
2014-08-26 23:21:48 +00:00
|
|
|
((and lean-global-server-process
|
|
|
|
(not (= 0 (process-exit-status lean-global-server-process))))
|
2014-08-25 23:50:16 +00:00
|
|
|
(setq lean-global-server-process nil)
|
|
|
|
t)
|
2014-08-25 23:27:33 +00:00
|
|
|
(lean-global-server-process
|
|
|
|
(when (interactive-p)
|
|
|
|
(message "lean-server-kill-process: %S killed" lean-global-server-process))
|
|
|
|
(kill-process lean-global-server-process)
|
|
|
|
(setq lean-global-server-process nil)
|
|
|
|
t)
|
2014-08-26 23:21:48 +00:00
|
|
|
(t
|
|
|
|
(when (interactive-p)
|
|
|
|
(message "lean-server-kill-process: no process to kill"))
|
|
|
|
nil)))
|
2014-08-14 15:56:07 +00:00
|
|
|
|
|
|
|
(defun lean-server-restart-process ()
|
|
|
|
"Restart lean-server process."
|
|
|
|
(interactive)
|
2014-08-25 18:40:37 +00:00
|
|
|
(and (lean-server-kill-process)
|
|
|
|
(lean-server-create-process)))
|
2014-08-14 00:02:49 +00:00
|
|
|
|
2014-08-14 15:56:07 +00:00
|
|
|
(defun lean-server-get-process ()
|
|
|
|
"Get lean-server process. If needed, create a one."
|
2014-08-25 18:40:37 +00:00
|
|
|
(cond ((not lean-global-server-process)
|
2014-08-25 23:27:33 +00:00
|
|
|
(lean-server-create-process))
|
2014-08-25 18:40:37 +00:00
|
|
|
((not (process-live-p lean-global-server-process))
|
2014-08-25 23:27:33 +00:00
|
|
|
(when (interactive-p)
|
|
|
|
(message "lean-server-get-process: %S is not live, kill it"
|
|
|
|
lean-global-server-process))
|
2014-08-25 23:50:16 +00:00
|
|
|
(lean-server-restart-process))
|
2014-08-25 18:40:37 +00:00
|
|
|
(t lean-global-server-process)))
|
2014-08-14 00:02:49 +00:00
|
|
|
|
|
|
|
(defun lean-server-get-buffer ()
|
2014-08-14 15:56:07 +00:00
|
|
|
"Get lean-server buffer."
|
2014-08-14 00:02:49 +00:00
|
|
|
(process-buffer (lean-server-get-process)))
|
|
|
|
|
|
|
|
;; How to send data to an async process
|
|
|
|
;; ====================================
|
|
|
|
(defun lean-flush-changed-lines ()
|
|
|
|
"Flush lean-changed-lines.
|
|
|
|
|
|
|
|
Send REPLACE commands to lean-server, reset lean-changed-lines to nil."
|
|
|
|
(cl-loop for n in lean-changed-lines
|
|
|
|
do (lean-server-send-cmd (lean-cmd-replace n (lean-grab-line n)))
|
|
|
|
finally (setq lean-changed-lines nil)))
|
|
|
|
|
2014-08-25 23:27:33 +00:00
|
|
|
(defun lean-server-check-current-file (&optional file-name)
|
2014-08-14 00:02:49 +00:00
|
|
|
"Check lean-global-server-current-file-name.
|
|
|
|
|
2014-08-25 23:27:33 +00:00
|
|
|
If it's not the same with file-name (default: buffer-file-name), send VISIT cmd."
|
|
|
|
(let ((current-file-name (or file-name (buffer-file-name))))
|
2014-08-14 00:02:49 +00:00
|
|
|
(unless (string= lean-global-server-current-file-name
|
|
|
|
current-file-name)
|
|
|
|
(lean-server-send-cmd (lean-cmd-visit current-file-name)))))
|
|
|
|
|
|
|
|
(defun lean-server-before-send-cmd (cmd)
|
2014-08-14 15:56:07 +00:00
|
|
|
"Operations to perform before sending a command."
|
2014-08-14 00:02:49 +00:00
|
|
|
(cl-case (lean-cmd-type cmd)
|
2014-08-25 18:40:37 +00:00
|
|
|
('LOAD (setq lean-global-server-current-file-name
|
|
|
|
(lean-cmd-load-get-file-name cmd)))
|
|
|
|
('VISIT (setq lean-global-server-current-file-name
|
|
|
|
(lean-cmd-visit-get-file-name cmd)))
|
2014-08-14 00:02:49 +00:00
|
|
|
('REPLACE (lean-server-check-current-file))
|
|
|
|
('INSERT (lean-server-check-current-file))
|
|
|
|
('REMOVE (lean-server-check-current-file))
|
2014-08-25 18:40:37 +00:00
|
|
|
('INFO (lean-flush-changed-lines))
|
2014-09-01 00:46:40 +00:00
|
|
|
('CHECK ())
|
2014-08-25 18:40:37 +00:00
|
|
|
('SET ())
|
2014-09-01 00:46:40 +00:00
|
|
|
('EVAL (lean-server-check-current-file))
|
2014-09-03 07:45:16 +00:00
|
|
|
('OPTIONS ())
|
|
|
|
('SHOW ())
|
|
|
|
('VALID ())))
|
2014-08-14 00:02:49 +00:00
|
|
|
|
|
|
|
(defun lean-server-after-send-cmd (cmd)
|
2014-08-14 15:56:07 +00:00
|
|
|
"Operations to perform after sending a command."
|
2014-08-14 00:02:49 +00:00
|
|
|
(cl-case (lean-cmd-type cmd)
|
2014-08-25 18:40:37 +00:00
|
|
|
('LOAD ())
|
|
|
|
('VISIT ())
|
2014-08-14 00:02:49 +00:00
|
|
|
('REPLACE ())
|
|
|
|
('INSERT ())
|
|
|
|
('REMOVE ())
|
|
|
|
('INFO ())
|
2014-08-25 18:40:37 +00:00
|
|
|
('CHECK ())
|
|
|
|
('SET ())
|
2014-09-01 00:46:40 +00:00
|
|
|
('EVAL ())
|
2014-09-03 07:45:16 +00:00
|
|
|
('OPTIONS ())
|
|
|
|
('SHOW ())
|
|
|
|
('VALID ())))
|
2014-08-14 00:02:49 +00:00
|
|
|
|
2014-09-03 07:39:54 +00:00
|
|
|
(defun lean-server-send-cmd (cmd &optional cont)
|
2014-08-14 15:56:07 +00:00
|
|
|
"Send string to lean-server."
|
2014-08-14 00:02:49 +00:00
|
|
|
(let ((proc (lean-server-get-process))
|
|
|
|
(string-to-send (concat (lean-cmd-to-string cmd) "\n")))
|
|
|
|
(lean-server-before-send-cmd cmd)
|
|
|
|
;; Logging
|
2014-08-25 18:40:37 +00:00
|
|
|
(lean-server-log
|
2014-09-03 07:39:54 +00:00
|
|
|
(string-join
|
|
|
|
'("Send"
|
|
|
|
"================"
|
|
|
|
"%s"
|
|
|
|
"================") "\n") string-to-send)
|
2014-08-30 14:35:00 +00:00
|
|
|
;; Trace
|
|
|
|
(lean-server-trace
|
|
|
|
(format "%s" string-to-send))
|
|
|
|
(process-send-string proc string-to-send)
|
2014-09-03 07:39:54 +00:00
|
|
|
(lean-server-after-send-cmd cmd)
|
|
|
|
(when cont
|
|
|
|
(lean-server-event-handler cont))))
|
|
|
|
|
2014-09-03 07:45:16 +00:00
|
|
|
(defun lean-show-parse-string (str)
|
|
|
|
"Parse the output of eval command."
|
|
|
|
(let ((str-list (split-string str "\n")))
|
|
|
|
;; Drop the first line "-- BEGINSHOW" and
|
|
|
|
;; the last line "-- ENDSHOW"
|
|
|
|
(setq str-list
|
|
|
|
(-take (- (length str-list) 2)
|
|
|
|
(-drop 1 str-list)))
|
|
|
|
(string-join str-list "\n")))
|
|
|
|
|
|
|
|
(defun lean-server-show ()
|
|
|
|
(interactive)
|
|
|
|
(lean-server-send-cmd (lean-cmd-show) 'message))
|
|
|
|
|
|
|
|
(defun lean-server-valid ()
|
|
|
|
(interactive)
|
|
|
|
(lean-server-send-cmd (lean-cmd-valid) 'message))
|
|
|
|
|
2014-09-03 07:39:54 +00:00
|
|
|
(defun lean-server-set-timer-for-event-handler (cont)
|
|
|
|
(setq lean-global-retry-timer
|
|
|
|
(run-with-idle-timer
|
|
|
|
(if (current-idle-time)
|
|
|
|
(time-add (seconds-to-time lean-server-retry-time) (current-idle-time))
|
|
|
|
lean-server-retry-time)
|
|
|
|
nil
|
|
|
|
'lean-server-event-handler cont)))
|
2014-08-14 00:02:49 +00:00
|
|
|
|
2014-09-03 07:39:54 +00:00
|
|
|
(defun lean-server-get-info-record-at-pos (body)
|
|
|
|
(let* ((file-name (buffer-file-name))
|
|
|
|
(column (current-column))
|
|
|
|
(cur-char (char-after (point))))
|
|
|
|
(when (and cur-char
|
|
|
|
(or (char-equal cur-char ?\s)
|
|
|
|
(char-equal cur-char ?\t)
|
|
|
|
(char-equal cur-char ?\t)
|
|
|
|
(char-equal cur-char ?\,)
|
|
|
|
(char-equal cur-char ?\))
|
|
|
|
(char-equal cur-char ?\})
|
|
|
|
(char-equal cur-char ?\]))
|
|
|
|
(> column 1))
|
|
|
|
(setq column (1- column)))
|
|
|
|
(lean-info-record-parse body file-name column)))
|
|
|
|
|
|
|
|
(defun lean-server-event-handler (cont)
|
|
|
|
(let ((partition-result (lean-server-check-buffer-and-partition lean-global-server-buffer)))
|
|
|
|
(pcase partition-result
|
|
|
|
(`(,type (,pre ,body ,post))
|
|
|
|
(lean-server-log "The following pre-message will be thrown away:")
|
|
|
|
(lean-server-log "%s" pre)
|
|
|
|
(setq lean-global-server-buffer post)
|
|
|
|
(cl-case type
|
|
|
|
(INFO
|
|
|
|
(let ((info-record (lean-server-get-info-record-at-pos body)))
|
|
|
|
(cond
|
|
|
|
((lean-info-record-nay info-record)
|
|
|
|
(lean-server-send-cmd (lean-cmd-info (line-number-at-pos))
|
|
|
|
cont))
|
|
|
|
(t
|
|
|
|
(funcall cont info-record)))))
|
|
|
|
(SET
|
|
|
|
(funcall cont (lean-set-parse-string body)))
|
|
|
|
(EVAL
|
|
|
|
(funcall cont (lean-eval-parse-string body)))
|
|
|
|
(OPTIONS
|
|
|
|
(funcall cont (lean-options-parse-string body)))
|
|
|
|
(SHOW
|
|
|
|
(funcall cont (lean-show-parse-string body)))
|
|
|
|
(ERROR
|
|
|
|
(lean-server-log "Error detected:\n%s" body))))
|
|
|
|
(`()
|
|
|
|
(lean-server-set-timer-for-event-handler cont)
|
|
|
|
nil))))
|
2014-09-03 07:45:16 +00:00
|
|
|
|
2014-08-14 00:02:49 +00:00
|
|
|
(provide 'lean-server)
|