Need a function to remove an advice, when point is on the code, where the advice is added

编辑调试的 elisp configuration 时候,假如光标在下面这一行,

(advice-add 'kill-new :around #'my-kill-new)

How to make an interactive command to achieve the following behaviour when executed:

(advice-remove 'kill-new #'my-kill-new)

更进一步,如果事先没有这个advice则 act like advice-add, 有则如上所述,那就更好了。

(defun my-goto-left-paren ()
  "a(...|....) => a|(...)"
  (interactive)
  (or (eq (char-after) ?\()
      (let ((innermost-paren (nth 1 (syntax-ppss))))
        (if innermost-paren
            (goto-char innermost-paren)
          (beginning-of-defun)))))


(defun rm-at-point:hook|advice|alist ()
  (interactive)
  (save-excursion
    (let ((form (read (progn
                        (my-goto-left-paren)
                        (buffer-substring-no-properties (point)
                                                        (progn
                                                          (forward-sexp)
                                                          (point)))))))
      (pcase (car form)
        (`add-hook
         (pcase (length form)
           (5
            ;; (add-hook 'x-hook 'fn nil-or-append :local)
            (eval `(remove-hook ,(cadr form)
                                ,(nth 2 form)
                                :local)))
           (_ (eval `(remove-hook ,(cadr form)
                                  ,(nth 2 form)))))
         (prin1 (symbol-value (eval (cadr form)))))
        ;; (advice-add 'ffx :before 'f1)
        (`advice-add
         (eval `(advice-remove ,(cadr form) ,(nth 3 form)))
         (message "rm %S advice => %S" (cadr form) (nth 3 form)))
        ;; (add-to-list 'xx-alist '("a" . 1))
        (`add-to-list
         (cl-symbol-macrolet ((al (eval (cadr form))))
           (eval `(setq ,al
                        (delete ,(nth 2 form) ,al)))
           (prin1 (eval al))))
        (_
         (error "unkown %S" form))))))
1 个赞