1

On a mouseclick to one side of the text, it can be convenient to make the cursor jump to:

  1. the beginning of the line (when clicking left of the text)
  2. the end of the line (when clicking right of the text)

This is the default behaviour of most text editors (also the one used for editing this very entry), so it can help to make emacs more beginner (user?) friendly.

Currently, the cursor either doesn't move (1.) or it jumps to the beginning of the next line (2.). Most people probably use C-E instead, but it would be good to be able to leave that out.

3
  • Please remove the second question, about the "related issue" - post it as a separate question. One question per question. Thx.
    – Drew
    Apr 2, 2021 at 15:39
  • OK, moved here: emacs.stackexchange.com/questions/64277/…
    – telephon
    Apr 3, 2021 at 13:30
  • No. You posed two questions there. One question per question, please.
    – Drew
    Apr 3, 2021 at 14:11

1 Answer 1

1

This should do it. Bind commands to keys [left-fringe mouse-l] and [right-fringe mouse-l]. The commands pick up the click position and move point to the beginning or end of the text for that line.

(defun mouse-goto-bol (click)
  "Move to beginning of line for mouse-1 click in left fringe."
  (interactive "e")
  (mouse-goto-line click 'left))

(defun mouse-goto-eol (click)
  "Move to beginning of line for mouse-1 click in left fringe."
  (interactive "e")
  (mouse-goto-line click 'right))

(defun mouse-goto-line (click left/right)
  "Helper for `mouse-goto-(bol|eol)'."
  (let* ((posn      (event-start click))
         (click-pt  (posn-point posn))
         (window    (posn-window posn))
         (buf       (window-buffer window))
         (clicks    (if (eq mouse-selection-click-count-buffer buf)
                        (event-click-count click)
                      0)))
    (when (= clicks 1)                  ; No-op if not single-click.
      (with-current-buffer buf
        (goto-char click-pt)
        (if (eq 'left left/right)
            (line-beginning-position)
          (line-end-position))))))
 
(global-set-key [left-fringe mouse-1]  'mouse-goto-bol)
(global-set-key [right-margin mouse-1] 'mouse-goto-eol)
;; (global-set-key [right-fringe mouse-1] 'mouse-goto-eol) ; To use the right fringe.
;; (global-set-key (kbd "<mouse-1>") 'mouse-goto-eol) ; Useless, since click in text area does it by default.

7
  • 1. There was a typo - I wrote mouse-l (letter l) instead of mouse-1 (numeral 1). Fixed now. 2. I also fixed the click count. 3. Your kbd sexp just returns [left-fringe mouse-1], so it's the same thing. 4. If you want the click on the right to be in the text area then just bind the eol command to (kbd "<mouse-1>") instead of [right-fringe mouse-1].
    – Drew
    Apr 2, 2021 at 18:32
  • Setting a click in the right text area to move to eol is useless, as that's the default click behavior there. And that would interfere with setting point at an arbitrary click position in the text. But if you want to handle a click in the right margin then do (global-set-key [right-margin mouse-1] 'mouse-goto-eol), as shown.
    – Drew
    Apr 2, 2021 at 18:45
  • Huh? I wrote [right-fringe mouse-1] to begin with, and you then said you wanted to click to the right of the text but not in the fringe. So I used [right-margin mouse-1]. You can define keys for any of the three areas: text area, fringe, and margin. You can bind the same command to all three if you like. It's up to you. HTH.
    – Drew
    Apr 2, 2021 at 21:42
  • See follow up: emacs.stackexchange.com/questions/64277/…
    – telephon
    Apr 3, 2021 at 13:05
  • It works for me. Start Emacs with emacs -Q and evaluate the code, together with the key bindings in your last comment. Works 100% of the time, for me. Clicking `mouse-1' in the right fringe puts the cursor at the end of the line.
    – Drew
    Jan 17 at 17:27

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.