何必东奔西走 Emacs应有尽有

之前还写了一个bc-mode 但今天突然发现Emacs的calc就可以进行我需要的计算 也提供了一个快捷函数quick-calc 我还是觉得不方便 所以可以把给bc-mode写的函数改一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(defun run-calc (expr &optional replace)
"Calculate the `expr' using GNU bc.
If called interactively, the user is asked for input. If called on
region the selected expression is used as input. By default display
output in temp buffer `*BC Output*'. With prefix, insert the output."
(interactive
(list (if (use-region-p)
(buffer-substring-no-properties
(region-beginning)
(region-end))
(quick-calc current-prefix-arg))
current-prefix-arg))
(require 'calc)
(let ((output (calc-eval expr)))
(if (and replace output)
(when (use-region-p)
(delete-region (region-beginning) (region-end))
(insert output))
(message "Result: %s" output))))

和bc相比缺点有:

  • 运算速度慢
  • bc支持简单的编程而这个只能用elisp了