Last active
January 23, 2025 01:23
-
-
Save kobapan/329eb42f3709928c18eabe0143080fe2 to your computer and use it in GitHub Desktop.
リージョン選択範囲、もしくは直前のS式を読み込み、評価した結果を現在のバッファに挿入する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun eval-print-rs () | |
"S式を実行して結果を現在のバッファに挿入する | |
先頭の関数が以下の算術関数だったら行末で ' = ' に続けて印字。それ以外は改行して印字。 | |
abs % mod truncate round floor ceiling sin cos tan expt sqrt | |
リージョン選択している場合は、リージョン全体を評価。それ以外は、直前のS式を評価。 | |
数字の中に,があるとエラーになるので、削除してから評価する。 | |
" | |
(interactive) | |
(let ((s-begin) (s-end) | |
(math-list '("-" "+" "*" "/" "1+" "1-" "abs" "%" "mod" "truncate" "round" "floor" "ceiling" "sin" "cos" "tan" "expt" "sqrt")) | |
(math?) | |
(f (lambda (s e m) | |
(if m (princ '\ =\ (current-buffer)) (newline)) | |
(princ (eval (read (replace-regexp-in-string "," "" (buffer-substring s e)))) (current-buffer)) | |
(unless m (newline))))) | |
(if (region-active-p) | |
;; リージョン全体の場合 | |
(progn | |
;; 先頭の関数 | |
(save-excursion | |
(goto-char (region-beginning)) | |
(forward-char) | |
(setq math? (thing-at-point 'sexp))) | |
;; 算術関数だったら、行末で ' = ' に続けて印字。それ以外は、改行して印字 | |
(funcall f (region-beginning) (region-end) (member math? math-list))) | |
;; カーソル位置直前のS式全体の場合 | |
(progn | |
(backward-sexp) (setq s-begin (point)) | |
(save-excursion | |
(forward-char) | |
(setq math? (thing-at-point 'sexp))) | |
(forward-sexp) (setq s-end (point)) | |
(funcall f s-begin s-end (member math? math-list)))))) | |
(define-key global-map (kbd "C-=") 'eval-print-rs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
テキストモードでふと「この計算式を評価したい」というときがある。
M-:としてミニバッファで評価しても、結果は見れるだけで、コピーされない。
そこで