2

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.

Daniel
  • 611
  • 5
  • 15

1 Answers1

3

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • The edit iss exactly what I needed. I will also add it to the question. – Daniel Sep 04 '12 at 12:00
  • One small issue, If I want to read the value that is copied and check for example if it doesn't have "JACK" in it, can I do it with advice too? I want it to be dynamic though, so for example I can check for different Strings.. I can update the question or write another one if you can't answer here. it is related to this question though. – Daniel Sep 04 '12 at 14:32
  • You could examine the current region, either by searching in the buffer between point and mark, or by copying the region to a string. Quick googling turns up http://stackoverflow.com/questions/605846/how-do-i-access-the-contents-of-the-current-region-in-emacs-lisp – tripleee Sep 04 '12 at 14:45