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.

telephon
  • 23
  • 6
  • Please remove the second question, about the "related issue" - post it as a separate question. One question per question. Thx. – Drew Apr 02 '21 at 15:39
  • OK, moved here: https://emacs.stackexchange.com/questions/64277/how-to-make-two-fixes-to-mouse-click-positioning-compatible-with-each-other – telephon Apr 03 '21 at 13:30
  • No. You posed two questions there. One question per question, please. – Drew Apr 03 '21 at 14:11

1 Answers1

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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 "")` instead of `[right-fringe mouse-1]`. – Drew Apr 02 '21 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 02 '21 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 02 '21 at 21:42
  • See follow up: https://emacs.stackexchange.com/questions/64277/how-to-make-two-fixes-to-mouse-click-positioning-compatible-with-each-other – telephon Apr 03 '21 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 '22 at 17:27
  • Based on your comments, maybe you want to delete this question? And perhaps post another one? Or keep this and provide an answer for it? Comments can be deleted at any time. Q & A need to stand on their own (comments can't be searched etc.). Thx. – Drew Jan 22 '22 at 16:50
  • ok, I did so. Your helpful comments now are out of context, so you may also delete them if you like. – telephon Jan 23 '22 at 17:33