11

I love the ability to select word/symbol at point which I currently do with double mouse click. I would like to be able to do it from the keyboard, and so I'd like to bind it to a keystroke (or maybe it already is?), so I need the name of the function. I assume the function must already exist, but I cannot figure out what it's name is.

I tried C-h k but then when I double click the mouse the help for <down-mouse-1> shows up (I assume the help system doesn't wait for the second click or something). I also searched the documentation where this functionality is defined in section 21.2, but it does not say the name of the function.

Update: For typical keybinding I suppose it is the equivalent to <C-left> followed by <C-S-right> which are left-word and right-word respectively, although the shift key is held for the latter. (I do not fully understand the relationship between keyboard designations with the shift key and their mapping to functions, the help for <C-S-right> translates it to <C-right> before it identifies it as right-word)

  • Using C-h b and searching through that buffer shows that <double-mouse-1> calls mouse-set-point (from "mouse.el") with some specific event. Not sure how much this helps :) – VanLaser Apr 27 '16 at 22:43
  • 1
    For the relationship between the shifted and the non-shifted version of C-right and C-left, it's the effect of shift-selection-mode (see (info "(emacs) Shift Selection")). See also emacs.stackexchange.com/questions/12858/… – YoungFrog Apr 28 '16 at 11:02
  • @YoungFrog that kind of complication is what often keeps most of my emacs programming as keyboard macros. A simple kbd macro based on <right> <C-left> <C-S-right> means I don't need to fully address subtle issues discussed at length in that link. – Chip Grandits Apr 28 '16 at 18:56
9

It's actually a bit complicated. When you double-click mouse-1 there are multiple actions. <down-mouse-1> occurs first, when you press the button. Then mouse-1 occurs, when you release the button.

<down-mouse-1> is bound to command mouse-drag-region, and that command calls mouse-drag-track. That function counts the mouse-button clicks and keeps track of any mouse movement (if you double-click that means that you did not move the mouse).

mouse-drag-track calls function mouse-start-end to determine the range of text to select. It passes to mouse-start-end the position where you clicked for, the first two args, and the click count, as the third arg. This is mouse-start-end:

mouse-start-end is a compiled Lisp function in `mouse.el'.

(mouse-start-end START END MODE)

Return a list of region bounds based on START and END according to MODE.
If MODE is 0 then set point to (min START END), mark to (max START END).
If MODE is 1 then set point to start of word at (min START END),
mark to end of word at (max START END).
If MODE is 2 then do the same for lines.

The click-count passed as the third arg (MODE) is 1 for a double-click.

You won't be able to just call some of this code from a keyboard key sequence, but examining the code might help you write a keyboard key-bindable command to do what you want.

  • Thanks for explaining why C-h k does not work for mouse events. The best way I've figured out to investigate how Emacs "is handling the mouse" is to record it as a keyboard macro, name it, and then look at the code generated by insert-kbd-macro Generally I would not try to use a mouse event in a macro, so these temporary macros are just useful to see what Emacs is doing. – Chip Grandits Apr 28 '16 at 19:08
6

@Drew has explained some of the complexity associated with events bound to mouse actions. As for your ultimate goal of marking the word at point using the keyboard, you might be interested in expand-region.

expand-region allows you to manipulate the region by semantic units. For example, when no region is active, calling er/expand-region (typically bound to C-=) will mark the current word. Calling it again expands the region to the current sentence. And then the paragraph, section etc. The actual units will depend on the mode you're in, so in lisp modes they would be symbol, expression enclosing the symbol, expression enclosing that expression etc.

The linked page has further explanation and a video demonstrating this.

  • 1
    yes I see the expand-region package in the package list from melpa. Very useful, I am surprised it is not default in Emacs or at least bundled in with CEDET. Thanks for the tip! – Chip Grandits Apr 28 '16 at 18:44
  • Chip Grandits: That's dependent upon the author(s) submitting their package to the FSF with appropriate copyright assignment. It can't be a default part of Emacs if the authors haven't submitted it. – phils Apr 29 '16 at 10:30
2

Here's a function that I extracted from mouse-set-region that does the marking when you double click:

(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))))

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.