【问题标题】:Emacs: how to evaluate Haskell expressions in the comment section within the source fileEmacs:如何在源文件的注释部分评估 Haskell 表达式
【发布时间】:2021-11-05 09:37:26
【问题描述】:

在 Haskell 源代码文件中:

-- >>> sin 5

键入一个快捷键,你会得到以下结果:

-- λ> sin 5
-- -0.9589242746631385
-- it :: Floating a => a
-- (0.03 secs, 133,480 bytes)

这个功能非常方便。 有谁知道如何用 Emacs 做到这一点?

【问题讨论】:

  • "结果如下" 下面没有任何内容。请在您的问题中包含这一点。
  • @Franky 更新
  • 你在问什么?如何使用 Emacs 计算 sin(5)?问题不清楚。
  • 有可能,但haskell-mode 似乎没有。您可以查看haskell-repl.el 并尝试在其之上构建。您也可以生成一个ghci 进程并手动与之交互。
  • @rajashekar 谢谢。我设法找到了函数:(haskell-interactive-mode-run-expr)。将(defun haskell-interactive-mode-expr-result (state response) 修改为仅在当前缓冲区中打印

标签: haskell emacs


【解决方案1】:

我设法修改了haskell-mode代码:

(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 绑定到快捷方式即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2010-12-06
    • 2018-03-10
    • 2010-09-19
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    相关资源
    最近更新 更多