2014-03-25 05:29:18 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;;
|
|
|
|
;;; ghc-process.el
|
|
|
|
;;;
|
|
|
|
|
|
|
|
;; Author: Kazu Yamamoto <Kazu@Mew.org>
|
|
|
|
;; Created: Mar 9, 2014
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'ghc-func)
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-05-13 20:25:37 +00:00
|
|
|
(defvar ghc-process-running nil)
|
|
|
|
|
2014-03-25 05:29:18 +00:00
|
|
|
(defvar-local ghc-process-process-name nil)
|
|
|
|
(defvar-local ghc-process-original-buffer nil)
|
|
|
|
(defvar-local ghc-process-original-file nil)
|
|
|
|
(defvar-local ghc-process-callback nil)
|
2014-04-22 03:33:26 +00:00
|
|
|
(defvar-local ghc-process-hook nil)
|
2014-03-25 05:29:18 +00:00
|
|
|
|
|
|
|
(defvar ghc-interactive-command "ghc-modi")
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-03-25 06:23:08 +00:00
|
|
|
(defun ghc-get-project-root ()
|
2014-04-21 02:37:33 +00:00
|
|
|
(ghc-run-ghc-mod '("root")))
|
2014-03-25 05:29:18 +00:00
|
|
|
|
2014-04-22 03:33:26 +00:00
|
|
|
(defun ghc-with-process (cmd callback &optional hook1 hook2)
|
2014-03-25 05:29:18 +00:00
|
|
|
(unless ghc-process-process-name
|
2014-03-25 06:23:08 +00:00
|
|
|
(setq ghc-process-process-name (ghc-get-project-root)))
|
2014-05-13 20:25:37 +00:00
|
|
|
(when (and ghc-process-process-name (not ghc-process-running))
|
|
|
|
(setq ghc-process-running t)
|
2014-04-22 03:33:26 +00:00
|
|
|
(if hook1 (funcall hook1))
|
2014-03-25 05:29:18 +00:00
|
|
|
(let* ((cbuf (current-buffer))
|
|
|
|
(name ghc-process-process-name)
|
|
|
|
(buf (get-buffer-create (concat " ghc-modi:" name)))
|
|
|
|
(file (buffer-file-name))
|
|
|
|
(cpro (get-process name)))
|
2014-05-13 20:05:49 +00:00
|
|
|
(ghc-with-current-buffer buf
|
2014-05-13 20:25:37 +00:00
|
|
|
(setq ghc-process-original-buffer cbuf)
|
|
|
|
(setq ghc-process-original-file file)
|
|
|
|
(setq ghc-process-callback callback)
|
|
|
|
(setq ghc-process-hook hook2)
|
|
|
|
(erase-buffer)
|
|
|
|
(let ((pro (ghc-get-process cpro name buf)))
|
|
|
|
(process-send-string pro cmd)
|
|
|
|
(when ghc-debug
|
|
|
|
(ghc-with-debug-buffer
|
|
|
|
(insert (format "%% %s" cmd))))
|
|
|
|
pro)))))
|
2014-03-25 05:29:18 +00:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defun ghc-get-process (cpro name buf)
|
|
|
|
(cond
|
|
|
|
((not cpro)
|
|
|
|
(ghc-start-process name buf))
|
|
|
|
((not (eq (process-status cpro) 'run))
|
|
|
|
(delete-process cpro)
|
|
|
|
(ghc-start-process name buf))
|
|
|
|
(t cpro)))
|
|
|
|
|
|
|
|
(defun ghc-start-process (name buf)
|
2014-08-01 07:56:44 +00:00
|
|
|
(let* ((opts (append '("-b" "\n" "-l") (ghc-make-ghc-options)))
|
|
|
|
(pro (apply 'start-file-process name buf ghc-interactive-command opts)))
|
2014-03-25 05:29:18 +00:00
|
|
|
(set-process-filter pro 'ghc-process-filter)
|
2014-05-13 21:39:50 +00:00
|
|
|
(set-process-sentinel pro 'ghc-process-sentinel)
|
2014-03-25 05:29:18 +00:00
|
|
|
(set-process-query-on-exit-flag pro nil)
|
|
|
|
pro))
|
|
|
|
|
|
|
|
(defun ghc-process-filter (process string)
|
2014-05-13 20:25:37 +00:00
|
|
|
(let ((pbuf (process-buffer process)))
|
|
|
|
(if (not (get-buffer pbuf))
|
|
|
|
(setq ghc-process-running nil) ;; just in case
|
|
|
|
(ghc-with-current-buffer (process-buffer process)
|
|
|
|
(goto-char (point-max))
|
|
|
|
(insert string)
|
|
|
|
(forward-line -1)
|
|
|
|
(cond
|
|
|
|
((looking-at "^OK$")
|
|
|
|
(if ghc-process-hook (funcall ghc-process-hook))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(funcall ghc-process-callback 'ok)
|
|
|
|
(when ghc-debug
|
|
|
|
(let ((cbuf (current-buffer)))
|
|
|
|
(ghc-with-debug-buffer
|
|
|
|
(insert-buffer-substring cbuf))))
|
|
|
|
(setq ghc-process-running nil))
|
|
|
|
((looking-at "^NG ")
|
|
|
|
(funcall ghc-process-callback 'ng)
|
|
|
|
(when ghc-debug
|
|
|
|
(let ((cbuf (current-buffer)))
|
|
|
|
(ghc-with-debug-buffer
|
|
|
|
(insert-buffer-substring cbuf))))
|
|
|
|
(setq ghc-process-running nil)))))))
|
2014-03-25 05:29:18 +00:00
|
|
|
|
2014-05-13 21:39:50 +00:00
|
|
|
(defun ghc-process-sentinel (process event)
|
|
|
|
(setq ghc-process-running nil))
|
|
|
|
|
2014-03-25 05:29:18 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-04-19 11:48:26 +00:00
|
|
|
(defvar ghc-process-rendezvous nil)
|
2014-04-21 08:35:32 +00:00
|
|
|
(defvar ghc-process-num-of-results nil)
|
2014-04-19 11:48:26 +00:00
|
|
|
(defvar ghc-process-results nil)
|
|
|
|
|
2014-04-22 03:33:26 +00:00
|
|
|
(defun ghc-sync-process (cmd &optional n hook)
|
2014-05-13 20:25:37 +00:00
|
|
|
(unless ghc-process-running
|
|
|
|
(setq ghc-process-rendezvous nil)
|
|
|
|
(setq ghc-process-results nil)
|
|
|
|
(setq ghc-process-num-of-results (or n 1))
|
|
|
|
(let ((pro (ghc-with-process cmd 'ghc-process-callback nil hook)))
|
|
|
|
(condition-case nil
|
|
|
|
(while (null ghc-process-rendezvous)
|
|
|
|
;; 0.01 is too fast for Emacs 24.4.
|
|
|
|
;; (sit-for 0.1 t) may get stuck when tooltip is displayed.
|
|
|
|
(sit-for 0.1)
|
|
|
|
;; (discard-input) avoids getting stuck.
|
|
|
|
(discard-input))
|
|
|
|
(quit
|
|
|
|
(setq ghc-process-running nil))))
|
|
|
|
ghc-process-results))
|
2014-04-19 11:48:26 +00:00
|
|
|
|
2014-04-27 12:51:44 +00:00
|
|
|
(defun ghc-process-callback (status)
|
|
|
|
(cond
|
|
|
|
((eq status 'ok)
|
|
|
|
(let* ((n ghc-process-num-of-results)
|
|
|
|
(ret (if (= n 1)
|
|
|
|
(ghc-read-lisp-this-buffer)
|
|
|
|
(ghc-read-lisp-list-this-buffer n))))
|
|
|
|
(setq ghc-process-results ret)))
|
|
|
|
(t
|
|
|
|
(setq ghc-process-results nil)))
|
|
|
|
(setq ghc-process-num-of-results nil)
|
|
|
|
(setq ghc-process-rendezvous t))
|
2014-04-19 11:48:26 +00:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-03-30 02:54:17 +00:00
|
|
|
(defun ghc-kill-process ()
|
|
|
|
(interactive)
|
|
|
|
(let* ((name ghc-process-process-name)
|
|
|
|
(cpro (if name (get-process name))))
|
|
|
|
(if (not cpro)
|
|
|
|
(message "No process")
|
|
|
|
(delete-process cpro)
|
|
|
|
(message "A process was killed"))))
|
|
|
|
|
2014-03-25 05:29:18 +00:00
|
|
|
(provide 'ghc-process)
|