Emacs:如何计算源文件中注释部分中的 Haskell 表达式
Emacs: how to evaluate Haskell expressions in the comment section within the source file
在Haskell源代码文件中:
-- >>> sin 5
键入快捷键,您将得到以下结果:
-- λ> sin 5
-- -0.9589242746631385
-- it :: Floating a => a
-- (0.03 secs, 133,480 bytes)
这个功能非常方便。
有谁知道如何用 Emacs 做到这一点?
我设法修改了 haskell-模式代码:
(require 'subr-x)
(defun my-run-haskell-expr ()
"Get haskell expression"
(interactive)
(search-backward "-- >>>")
(setq my-expr
(string-remove-prefix "-- >>>" (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
(my-haskell-interactive-mode-run-expr my-expr)
)
(defun my-haskell-interactive-mode-run-expr (expr)
"Run the given expression."
(let ((session (haskell-interactive-session))
(process (haskell-interactive-process)))
(haskell-process-queue-command
process
(make-haskell-command
:state (list session process expr 0)
:go (lambda (state)
;; (goto-char (point-max))
;; (insert "\n")
(end-of-line)
(insert "\n")
(beginning-of-line)
(setq haskell-interactive-mode-result-end
(point-max))
(haskell-process-send-string (cadr state)
(haskell-interactive-mode-multi-line (cl-caddr state)))
(haskell-process-set-evaluating (cadr state) t))
:complete
(lambda (state response)
(haskell-process-set-evaluating (cadr state) nil)
(unless (haskell-interactive-mode-trigger-compile-error state response)
(my-haskell-interactive-mode-expr-result state response)))))))
(defun my-haskell-interactive-mode-expr-result (state response)
"Print the result of evaluating the expression."
;; (mapc 'insert (split-string-and-unquote response))
(mapc (lambda (str) (progn
(insert "-- ")
(insert str)
(insert "\n")))
(split-string-and-unquote response "\n")))
(global-set-key (kbd "C-c C-e") 'my-run-haskell-expr)
;; end of haskell inline evaluation
只需将函数 my-get-haskell-expr
绑定到快捷方式即可。
在Haskell源代码文件中:
-- >>> sin 5
键入快捷键,您将得到以下结果:
-- λ> sin 5
-- -0.9589242746631385
-- it :: Floating a => a
-- (0.03 secs, 133,480 bytes)
这个功能非常方便。 有谁知道如何用 Emacs 做到这一点?
我设法修改了 haskell-模式代码:
(require 'subr-x)
(defun my-run-haskell-expr ()
"Get haskell expression"
(interactive)
(search-backward "-- >>>")
(setq my-expr
(string-remove-prefix "-- >>>" (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
(my-haskell-interactive-mode-run-expr my-expr)
)
(defun my-haskell-interactive-mode-run-expr (expr)
"Run the given expression."
(let ((session (haskell-interactive-session))
(process (haskell-interactive-process)))
(haskell-process-queue-command
process
(make-haskell-command
:state (list session process expr 0)
:go (lambda (state)
;; (goto-char (point-max))
;; (insert "\n")
(end-of-line)
(insert "\n")
(beginning-of-line)
(setq haskell-interactive-mode-result-end
(point-max))
(haskell-process-send-string (cadr state)
(haskell-interactive-mode-multi-line (cl-caddr state)))
(haskell-process-set-evaluating (cadr state) t))
:complete
(lambda (state response)
(haskell-process-set-evaluating (cadr state) nil)
(unless (haskell-interactive-mode-trigger-compile-error state response)
(my-haskell-interactive-mode-expr-result state response)))))))
(defun my-haskell-interactive-mode-expr-result (state response)
"Print the result of evaluating the expression."
;; (mapc 'insert (split-string-and-unquote response))
(mapc (lambda (str) (progn
(insert "-- ")
(insert str)
(insert "\n")))
(split-string-and-unquote response "\n")))
(global-set-key (kbd "C-c C-e") 'my-run-haskell-expr)
;; end of haskell inline evaluation
只需将函数 my-get-haskell-expr
绑定到快捷方式即可。