forked from abo-abo/lispy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
le-hy.el
86 lines (73 loc) · 2.94 KB
/
le-hy.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
;;; le-hy.el --- lispy support for Hy. -*- lexical-binding: t -*-
;; Copyright (C) 2016 Oleh Krehel
;; This file is not part of GNU Emacs
;; This file 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, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'hy-mode nil t)
(require 'inf-lisp)
(defun lispy--hy-proc ()
(let ((proc-name "hy"))
(if (process-live-p proc-name)
(get-process proc-name)
(get-buffer-process
(make-comint proc-name "hy")))))
(defun lispy--comint-eval (command)
"Collect output of COMMAND without changing point."
(let ((command-output-begin nil)
(str nil)
(last-cmd nil)
(last-cmd-with-prompt nil)
(buffer (process-buffer (lispy--hy-proc))))
(with-current-buffer buffer
(let ((inhibit-field-text-motion t)
(inhibit-read-only t))
;; save the last command and delete the old prompt
(beginning-of-line)
(setq last-cmd-with-prompt
(buffer-substring (point) (line-end-position)))
(setq last-cmd (replace-regexp-in-string
"=> " "" last-cmd-with-prompt))
(delete-region (point) (line-end-position))
;; send the command
(setq command-output-begin (point))
(comint-simple-send (get-buffer-process (current-buffer))
command)
;; collect the output
(while (null (save-excursion
(let ((inhibit-field-text-motion t))
(goto-char command-output-begin)
(re-search-forward "^[. ]*=> \\s-*$" nil t))))
(accept-process-output (get-buffer-process buffer))
(goto-char (point-max)))
(goto-char (point-max))
(when (looking-back "^[. ]*=> *" (line-beginning-position))
(goto-char (1- (match-beginning 0))))
;; save output to string
(setq str (buffer-substring-no-properties command-output-begin (point)))
;; delete the output from the command line
(delete-region command-output-begin (point-max))
;; restore prompt and insert last command
(goto-char (point-max))
(comint-send-string (get-buffer-process (current-buffer)) "\n")
(insert last-cmd)
;; return the shell output
str))))
(defun lispy--eval-hy (str)
"Eval STR as Hy code."
(let ((res (lispy--comint-eval str)))
(if (member res '("" "\n"))
"(ok)"
res)))
(provide 'le-hy)
;;; le-hy.el ends here