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 at 15:39
  • OK, moved here: emacs.stackexchange.com/questions/64277/… – telephon Apr 3 at 13:30
  • No. You posed two questions there. One question per question, please. – Drew Apr 3 at 14:11
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.

6
  • Thank you, this looks very promising. There are two issues remaining, maybe you have an idea: I had to use ``` (global-set-key (kbd "<left-fringe> <mouse-1>") 'mouse-goto-bol) (global-set-key (kbd "<left-fringe> <mouse-1>") 'mouse-goto-eol) ``` But still it doesn't yet do anything. Maybe you can give me a hint how I could provide more information (I am not fluent in emacs). Also the click to the right side is not necessarily on the right margin, but just behind the last newline character. This is the most common one in my experience. – telephon Apr 2 at 18:05
  • 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 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 at 18:45
  • thank you! One more thing was: It should be [right-fringe mouse-1], not [right-margin mouse-1] – telephon Apr 2 at 21:11
  • 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 at 21:42

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.