糊了一个一键eval function 并运行的功能

以前写elisp很勤快, 都是写完函数后手动移动函数的最后一个括号C-x C-e, 直到最近开始写一个自动生成UVM平台的测试代码的工具, 由于要写的函数太多了, 老是按照老办法感觉效率好低, 于是研究了一下有没有更自动化的办法, 最终糊了下面三个函数, 用起来爽爆了!

(defun my/eval-current-elisp-func (&optional run)
  " eval-last-sexp 当前光标处的function
根据run 来决定是否要运行"
  (interactive)
  (let* ((current-pos (point)) func-start-pos func-end-pos fun-name)
    (when (string= major-mode "emacs-lisp-mode")
      (save-excursion
        (beginning-of-defun) 
        (setq func-start-pos (point))
        (end-of-defun)
        (setq func-end-pos (point)))
      (when (and (>= current-pos func-start-pos) (< current-pos func-end-pos))
                 (evil-backward-section-begin)
                 (evil-jump-item)
                 (eval-last-sexp nil)   ;; 不把执行的结果插入到当前buffer 中
                 (goto-char current-pos)
                 (when run
                   (setq fun-name (format "(%s)" (lisp-current-defun-name)))
                   (eval (read fun-name)))))))

(defun my/eval-current-elisp-func-only ()
  " eval-last-sexp 当前光标处的function "
  (interactive)
  (my/eval-current-elisp-func))

(defun my/eval-current-elisp-func-and-run ()
  " eval-last-sexp 当前光标处的function 并运行"
  (interactive)
  (my/eval-current-elisp-func t))

绑定到快捷键

(define-key emacs-lisp-mode-map (kbd "C-c e") #'my/eval-current-elisp-func-only)
(define-key emacs-lisp-mode-map (kbd "C-c r") #'my/eval-current-elisp-func-and-run)

2 个赞