Advice for “kill-ring-save”

2020-07-27 03:50发布

问题:

I want to evaluate certain conditions before allowing a user to copy a text. As far as I know, I need an advice for "kill-ring-save". I need to ignore the user request to Copy that text if conditions are not met and allow it if are satisfied. How can I do this?

(UPDATE) -- MORE ABOUT CONSTRAINTS: only in specific mode of Emacs (e.g. NXML mode) this advice should be applied and only when one/more specific conditions are met.

回答1:

Quick proof of concept; you don't tell what your constraints are, so this is necessarily vague / useless.

(defvar moo nil)

(defadvice kill-ring-save (around kill-ring-check-constraints activate compile)
  "If in `nxml-mode', don't save to kill ring if `moo' is `nil'."
  (if (and (eq major-mode 'nxml-mode) (null moo))
    (message "Not copied.")
   ad-do-it) )

The additional major-mode constraint was added in response to comments below. If you want this in every mode, just take out the mode check.



标签: elisp