Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.9 KiB

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