Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Semantic' movement across a line

Tags:

emacs

elisp

Consider the following line of Lisp code:

        (some-function 7 8 | 9) ;; some comment. note the extra indentation

The point is placed between '8' and '9'. If I perform (move-beginning-of-line), the point will be placed at the absolute beginning of the line, rather than at '('.

Same for move-end-of-line: I'd find it more desirable for it to place the point at ')' if I perform it once, and at the absolute end of the line if I perform it a second time. Some IDEs behave like that.

I tried to implement this but got stuck, my solution behaves particularly bad near the end of a buffer, and on the minibuffer as well. Is there a library that provides this functionality?

like image 751
deprecated Avatar asked Jan 14 '23 13:01

deprecated


2 Answers

I don't know of any library, but it can be done in a few lines of Elisp.

For the beginning of line part, the bundled functions beginning-of-line-text and back-to-indentation (M-m) move to the beginning of the “interesting” part of the line. back-to-indentation ignores only whitespace whereas beginning-of-line-text skips over the fill prefix (in a programming language, this is typically the comment marker, if in a comment). See Smart home in Emacs for how to flip between the beginning of the actual and logical line.

For the end of line part, the following function implements what you're describing. The function end-of-line-code moves to the end of the line, except for trailing whitespace and an optional trailing comment. The function end-of-line-or-code does this, except that if the point was already at the target position, or if the line only contains whitespace and a comment, the point moves to the end of the actual line.

(defun end-of-line-code ()
  (interactive "^")
  (save-match-data
    (let* ((bolpos (progn (beginning-of-line) (point)))
           (eolpos (progn (end-of-line) (point))))
      (if (comment-search-backward bolpos t)
          (search-backward-regexp comment-start-skip bolpos 'noerror))
      (skip-syntax-backward " " bolpos))))

(defun end-of-line-or-code ()
  (interactive "^")
  (let ((here (point)))
    (end-of-line-code)
    (if (or (= here (point))
        (bolp))
        (end-of-line))))
like image 79
Gilles 'SO- stop being evil' Avatar answered Jan 17 '23 02:01

Gilles 'SO- stop being evil'


Some suggestions that almost do what you ask:

In lisp code, you can sort-of do what you want, with the sexp movement commands. To get to the beginning of the expression from somewhere in the middle, use backward-up-list, which is bound to M-C-u. In your example, that would bring you to the open parenthesis. To move backwards over individual elements in the list, use backward-sexp, bound to M-C-b; forward-sexp moves the other way, and is bound to M-C-f. From the beginning of an sexp, you can skip to the next with M-C-n; reverse with M-C-p.

None of these commands are actually looking at the physical line you are on, so they'll go back or forward over multiple lines.

Other options include Ace Jump mode, which is a very slick way to quickly navigate to the beginning of any word visible on the screen. That might eliminate your need to use line-specific commands. For quick movement within a line, I usually use M-f and M-b to jump over words. Holding the M key down while tapping on b or f is quick enough that I end up using that by default most of the time.

Edit:

Forgot one other nice command - back-to-indentation, bound to M-m. This will back you up to the first non-whitespace character in a line. You could advice this to behave normally on the first call, and then to back up to the beginning of the line on the second call:

(defadvice back-to-indentation (around back-to-back)
  (if (eq last-command this-command)
      (beginning-of-line)
    ad-do-it))

(ad-activate 'back-to-indentation)
like image 40
Tyler Avatar answered Jan 17 '23 02:01

Tyler