Skip to content

Instantly share code, notes, and snippets.

@miyamuko
Created November 10, 2011 02:27
Show Gist options
  • Save miyamuko/1353916 to your computer and use it in GitHub Desktop.
Save miyamuko/1353916 to your computer and use it in GitHub Desktop.
バッファ内に書いてある数式を計算して答えを挿入 #xyzzy
;;
;; バッファ内に書いてある数式を計算して答えを挿入
;;
;; 例
;; 1 + 1 ってバッファに書いておいてリージョンで囲って M-x eval-calc-region
;;
(require "calc")
(defun eval-calc-region (s e)
(interactive "*r")
(if (< e s) (rotatef s e))
(progn
(goto-char e)
(let ((skip-equal (skip-chars-backward "[ =]"))
(expr (buffer-substring s (point))))
(let ((ans (ed::calc-string expr)))
(goto-char e)
(unless skip-equal
(insert "="))
(insert (format nil "~A" ans))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment