1

I'm overriding double-mouse-1 to do additional stuff on top of its default behavior. To this end, I have a function that (1) does my additional stuff and then (2) calls another function, ora-mark-like-double-mouse-1, which simulates normal double-mouse-1 behavior (copied from here).

(transient-mark-mode t)

(defun ora-mark-like-double-mouse-1 ()
  (interactive)
  (let ((bnd (mouse-start-end (point) (point) 1)))
    (goto-char (car bnd))
    (push-mark)
    (set-mark (point))
    (goto-char (cadr bnd))

    ; The addition of this line produces a different,
    ; but also wrong effect, in which the selection vanishes
    ; immediately after appearing.
    (setq deactivate-mark t)
  )
)

(global-set-key [(double-mouse-1)] 'ora-mark-like-double-mouse-1)

However, this function is slightly flawed. It selects the clicked-on word as the region, but afterwards, any non-shifted cursor movement I do with the arrow keys causes the existing region to be expanded, rather than deactivated. This is different from normal double-mouse-1 behavior. This happens even if I just bind this function directly to double-mouse-1, as I do in the code above (in which I have temporarily removed my own extra functionality, and I have added a call to (setq deactivate-mark t), which causes the selection to vanish immediately after appearing -- which is not the correct behavior either).

Is there a way to set it so that, after calling this function, which selects a region, subsequent non-shifted cursor movement will cause the region to be deactivated?

Alternatively, if there some way to set up a hook to run after a double-click event, I wouldn't need to override and reimplement the default functionality at all. (It would also solve the problem that the linked-to function doesn't work when I double-click on the opening quote.)

3

End your command with this:

(setq deactivate-mark  t)

or this:

(deactivate-mark)

C-h v deactivate-mark:

deactivate-mark is a variable defined in C source code.

Its value is nil

Automatically becomes buffer-local when set.

Documentation:

If an editing command sets this to t, deactivate the mark afterward.

The command loop sets this to nil before each command, and tests the value when the command returns.

Buffer modification stores t in this variable.

C-h f deactivate-mark:

deactivate-mark is a compiled Lisp function in simple.el.

(deactivate-mark &optional FORCE)

Deactivate the mark.

If Transient Mark mode is disabled, this function normally does nothing; but if FORCE is non-nil, it deactivates the mark anyway.

Deactivating the mark sets mark-active to nil, updates the primary selection according to select-active-regions, and runs deactivate-mark-hook.

If Transient Mark mode was temporarily enabled, reset the value of the variable transient-mark-mode; if this causes Transient Mark mode to be disabled, don’t change mark-active to nil or run deactivate-mark-hook.

The variable and the function do not have the same effect. You can decide which effect you want/need.

  • Adding (deactivate-mark) to the end of ora-mark-like-double-mouse-1 causes the selection to appear and then very quickly vanish entirely after I double-click on a word. I want the selection to remain until the user presses a non-shifted cursor key (as normally happens after double-clicking a word). I do have transient mark mode enabled – std_answ Apr 7 '17 at 13:38
  • 1
    Did you try setting the variable? The two do not have exactly the same effect, as their doc makes clear. – Drew Apr 7 '17 at 17:50
  • Yes, tried both ways; same effect in this case. – std_answ Apr 7 '17 at 19:16
  • You don't show all your code, so it's unclear what you are doing. Deactivating the mark should be the last thing that the command that is invoked as a command does. You are apparently invoking a command non-interactively from another command. Did you deactivate the mark in the command that is invoked interactively? And why are you calling push-mark and then immediately calling set-mark. Unclear (to me). – Drew Apr 8 '17 at 0:26
  • For simplicity's sake, at the moment I'm simply adding (setq deactivate-mark t) to the end of the ora-mark-like-double-mouse-1 function I already show, and binding that function to double-mouse-1. I will re-introduce my own external functionality once I get this working. Edited original post to show this. – std_answ Apr 9 '17 at 6:41

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.