1

Usually a mouse selected region is highlighted and a region set by (set-mark-command) is not highlighted. But I don't know a programmatic way to check it.

Could anyone help?

  • Can you be a little more specific about what it is you want to check? Do you just want to know if the mouse has selected a region and do something if so? – RichieHH Feb 25 at 19:04
  • @RichieHH Yes, I'd like to have a global flag that is true when the region is selected by mouse and is false when the region is selected by running commands from M-x. – DataHungry Feb 25 at 19:42
1

I think you have transient-mark-mode switched off. Without transient-mark-mode you do not get a highlighted region after setting mark with C-SPC and successive motion commands.

If the region is not highlighted, it is not active and (region-active-p) returns nil.

Even if the region is not active (point) and (mark) can give differing positions. That is the situation without transient-mark-mode when you have set mark with C-SPC and moved point away from that position.

See also use-region-p to neglect empty active regions.

|improve this answer|||||
  • I'm able to use (region-active-p) to achieve my goal with transient-mark-mode switched off. (region-active-p) will return t if it's selected by mouse (and there's also highlighting) and will return nil if the region is set by keyboard. – DataHungry Feb 25 at 22:02
2

Use (mouse-region-match). C-h f mouse-region-match says:

mouse-region-match is a compiled Lisp function in mouse.el.

(mouse-region-match)

Return non-nil if there's an active region that was set with the mouse.

|improve this answer|||||
  • heh. helm should have found that for me. – RichieHH Feb 25 at 21:33
  • @RichieHH: (Icicles finds it right away.) Even M-x apropos finds it, as the first hit, with the highest score, for input keywords region and mouse. – Drew Feb 25 at 21:45
  • Somehow this doesn't work for me. (mouse-region-match) always gives me nil regardless of whether set by mouse. – DataHungry Feb 25 at 22:08
  • 1
    Put (mouse-region-mouse) in your Scratch buffer than mouse select to the end of it and press C-x e while its still selected with the mouse selection just past the closing ")". – RichieHH Feb 25 at 22:31
  • @Drew yes, I definitely need to use apropos and helm would have shown it had I been rigorous enough. Helm has a super helm-apropos and I should use it more. – RichieHH Feb 25 at 22:32
1

Not 100% sure what you want to check for but if you wish to do something when the mouse has selected a region then here is one way to do it using Emacs advice functionality.

(defun register-mouse-select(&rest args)
  (message "mouse select"))

(advice-add 'mouse-set-region :after #'register-mouse-select)

 ;;(advice-remove 'mouse-set-region #'register-mouse-select)

|improve this answer|||||
  • Thanks! Yet after evaluating these lines, I still don't see any message when I select a region by a mouse. – DataHungry Feb 25 at 19:47
  • sorry, but the dreaded "works for me" with emacs gtk 26 on Debian. – RichieHH Feb 25 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.