ghc-mod/elisp/ghc-process.el

111 lines
3.1 KiB
EmacsLisp
Raw Normal View History

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)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar-local ghc-process-running nil)
(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)
(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
(defun ghc-with-process (cmd callback)
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-03-25 05:29:18 +00:00
(when ghc-process-process-name
(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)))
(with-current-buffer buf
(unless ghc-process-running
(setq ghc-process-running t)
(setq ghc-process-original-buffer cbuf)
(setq ghc-process-original-file file)
(setq ghc-process-callback callback)
(erase-buffer)
(let ((pro (ghc-get-process cpro name buf)))
2014-03-27 04:10:43 +00:00
(process-send-string pro cmd)
2014-03-28 06:05:14 +00:00
(when ghc-debug
(ghc-with-debug-buffer
(insert (format "%% %s" cmd))))))))))
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-04-19 07:14:02 +00:00
(let ((pro (start-file-process name buf ghc-interactive-command "-b" "\n" "-l")))
2014-03-25 05:29:18 +00:00
(set-process-filter pro 'ghc-process-filter)
(set-process-query-on-exit-flag pro nil)
pro))
(defun ghc-process-filter (process string)
(with-current-buffer (process-buffer process)
(goto-char (point-max))
(insert string)
(forward-line -1)
(when (looking-at "^\\(OK\\|NG\\)$")
(goto-char (point-min))
(funcall ghc-process-callback)
2014-03-28 06:05:14 +00:00
(when ghc-debug
(let ((cbuf (current-buffer)))
(ghc-with-debug-buffer
(insert-buffer-substring cbuf))))
2014-03-25 05:29:18 +00:00
(setq ghc-process-running nil))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2014-04-19 11:48:26 +00:00
(defvar ghc-process-rendezvous nil)
(defvar ghc-process-results nil)
(defun ghc-sync-process (cmd)
2014-04-19 11:48:26 +00:00
(setq ghc-process-rendezvous nil)
(setq ghc-process-results nil)
(ghc-with-process cmd 'ghc-process-callback)
2014-04-19 11:48:26 +00:00
(while (null ghc-process-rendezvous)
(sit-for 0.01))
ghc-process-results)
(defun ghc-process-callback ()
(let ((mods (ghc-read-lisp-this-buffer)))
(setq ghc-process-results mods)
(setq ghc-process-rendezvous t)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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)