lawlist
lawlist

Reputation: 13457

Select to beginning / ending of word-wrapped visual line

The default behavior of the latest nightly builds of Aquamacs24 and Emacs-Trunk both appear to function differently than I'm accustomed -- i.e., command+shift+right or command+shift+left jumps to the beginning or end of the visual line without selecting the region. Instead, it is necessary to set the mark by moving the shift+left or shift+right or Ctrl+SPC to activate the mark and then go to the end or beginning of the visual line. In other words, it is a two step approach that needs to be combined into one-fell-swoop.

The following code almost does what I'm looking for, except that I'd like the selection to automatically cancel if I change my mind and release the shift key and move the arrow key. The way the code is presently written keeps the selection mode active and does not cancel unless I use a Ctrl+g.

Can anyone please suggest a modification to my code or an alternative way to achieve the desired behavior?

(defun beginning-of-visual-line (&optional n)
  "Move point to the beginning of the current line.
If `word-wrap' is nil, we move to the beginning of the buffer
line (as in `beginning-of-line'); otherwise, point is moved to
the beginning of the visual line."
  (interactive)
  (if word-wrap
      (progn 
    (if (and n (/= n 1))
        (vertical-motion (1- n))
      (vertical-motion 0))
    (skip-read-only-prompt))
    (beginning-of-line n)))


(defun end-of-visual-line (&optional n)
  "Move point to the end of the current line.
If `word-wrap' is nil, we move to the end of the line (as in
`beginning-of-line'); otherwise, point is moved to the end of the
visual line."
  (interactive)
  (if word-wrap
      (unless (eobp)
    (progn
      (if (and n (/= n 1))
          (vertical-motion (1- n))
        (vertical-motion 1))
      (skip-chars-backward " \r\n" (- (point) 1))))
    (end-of-line n)))


(defun command-shift-right ()
  ""
  (interactive) ;; this is a command (i.e. can be interactively used)
  (when (not (region-active-p))  ;; if the region is not active...
    (push-mark (point) t t))     ;; ... set the mark and activate it
  (end-of-visual-line)) ;; move point defined


(defun command-shift-left ()
  ""
  (interactive) ;; this is a command (i.e. can be interactively used)
  (when (not (region-active-p))  ;; if the region is not active...
    (push-mark (point) t t))     ;; ... set the mark and activate it
  (beginning-of-visual-line)) ;; move point defined

Upvotes: 1

Views: 322

Answers (1)

Stefan
Stefan

Reputation: 28541

Emacs has built-in support for shift-select-mode: just use (interactive "^") in your functions and they'll select when triggered with a shift.

Upvotes: 2

Related Questions