簡體   English   中英

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-get-haskell-expr ()
  "Get haskell expression"
  (interactive)
  (setq myLine
        (buffer-substring-no-properties
         (line-beginning-position)
         (line-end-position)
         ))
  (my-haskell-interactive-mode-run-expr (string-remove-prefix "-- >>> " myLine))
  )
(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")))

只需將函數my-get-haskell-expr綁定到快捷方式即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM