spacingissue
spacingissue

Reputation: 497

Emacs: line numbers that respect line-wrapping

Modes: I'm using linum for line numbers, the package linum-relative for relative line numbers. If it matters, I am also using visual-line-mode. These are flexible.

Currently, a single line (i.e. text without a newline) is numbered as only one line, regardless of how many times it is wrapped. I am wondering if there is a way to change the numbering to respect these wraps. So, for example,

263   This is all in
      a single line
      without newlines

might become:

263   This is all in
264   a single line
265   without newlines

and, in relative mode:

0     This is all in
      a single line
      without newlines

might become:

-1    This is all in
0     a single line
1     without newlines

I really only want the change in relative mode, but would not mind if it spills over into absolute mode.

A toggled change that works on both would be most useful - that way, the user can specifically select when, or with which modes, to turn it off or on.

Upvotes: 4

Views: 858

Answers (1)

assem
assem

Reputation: 2097

If the goal is navigation, I suggest a similar solution via the popular ace-jump-mode.

If the goal is just persistent line numbering, you might consider longlines-mode instead of visual-line-mode (but I would avoid this, personally).

ace-jump @ GitHub
https://github.com/winterTTr/ace-jump-mode

Demo:
http://dl.dropboxusercontent.com/u/3254819/AceJumpModeDemo/AceJumpDemo.htm

With it, you can jump to any line with as little as two keypresses.

In addition to lines, you can jump to the start of any word; there's also individual character-level jump precision. If desired, it can be configured to restrict jumps to the current window/buffer, or across all windows in the current frame, and even multi-frames.

It doesn't, however, recognize wrapped lines as jump-able locations. Again, you might consider longlines-mode as a fix for this if it is really important to you, but as I understand, it's considered hack'ish and deprecated in favor of visual-line-mode. Though, with longlines-mode, the lines are renumbered exactly as you want in your first example.

I'm assuming the goal is navigation, and as such, I think you'll find with just a little practice that word-based jumping or even jumping via incremental search to be a superior solution.

Update

Here's a simple solution to trick ace-jump to scan within N lines using emacs narrowing features; perhaps others can improve upon it. You could also do something similar for word and line modes.

(defun brian-ace-jump-to-char-within-N-lines (&optional n)
  (interactive "p")
  (let* ((N (or n 0))
     (query-char (read-char "Query Char:"))
     (start (save-excursion
          (forward-line (- N))
          (point)))
     (stop (save-excursion 
         (forward-line (1+ N))
         (point))))
    (unwind-protect
    (condition-case err 
        (progn
          (narrow-to-region start stop)
          (ace-jump-char-mode query-char))
      (error 
       (message (error-message-string err))))
      (widen))))

Upvotes: 4

Related Questions