2

In a buffer in Emacs, a line is the text you get from the first character after you press "Enter" to the last character before the next "Enter". And so, this is the line numbers one gets to the left of the buffer when you are in the display-line-numbers-mode.

Is there any way to get not these line numbers, but the "number of line" you get by counting the number of times you press C-n when moving down the buffer from the beginning? Namely, counting the numbers of lines (as I see in the screen) from the beginning of the buffer till the current position, and showing them in the screen as display-line-numbers-mode does.

I have tried using M-q on the line, but this is not what I am looking for as this command truncates the line into multiple lines.

[Edited in order to make the question clearer.]

I'd like to get the kind of thing you get when (in the windowed version of Emacs) you activate "Options → Show/Hide → Line Numbers for All Lines → Visual Line Numbers". But I don't quite understand this option, because it's not counting the lines from the first one in the buffer. Instead, the number in front of each line changes depending on the line in which the cursor is.

[Edit 2]

Example: enter image description here

In this image I have two lines (I am using word wrapping). Well, the idea is that I'd like to show a number in each line I see, that is, from 1 (at the beginning) to 12 (at the end of the buffer). Of course, if I change the width of the window I'd get a different number of lines.

  • 1
    I don't understand what you mean. Maybe you can provide a short example of a buffer with a few lines and what it should look like? – NickD Oct 22 '20 at 16:30
  • 1
    Like @NickD, I don't understand what you mean. But I have some inspired guess. You can toggle between lines showing with a virtual newline breaking at the end of your display to show the rest of the line in a new display line, rather than hiding it somewhere to the left or right of your monitor. Is it that what your question is about? – Gyro Gearloose Oct 22 '20 at 16:56
  • 1
    @NickD, I edited the question trying to make it clearer. – Matematikisto Oct 22 '20 at 17:02
  • When in dired-mode you can M-x dired-find-file-literally. This way you can see the characters "naked". This is a great way to see *alien* characters by eiy-sight; not direcly what you asked but maybe this helps. – Gyro Gearloose Oct 22 '20 at 17:03
  • @GyroGearloose, that is what I meant. Showing the "virtual line number", so to speak. – Matematikisto Oct 22 '20 at 17:03
  • I'm not aware of any (per-factured) code that would do that. But then, emacs (with its vast resort of supporters), is always good for a surprise. I hope this comments will lead to a better understanding of the question and finally to an answer. – Gyro Gearloose Oct 22 '20 at 17:24
  • 1
    You basically want `linum-mode` for the visual lines in `visual-line-mode`. – Zeta Oct 23 '20 at 17:01
  • That's it, @Zeta. Is there any way to get it? – Matematikisto Oct 23 '20 at 17:46
  • I'm curious what you then want to use those visual line numbers for! – pst Oct 24 '20 at 06:08
  • @Matematikisto oof, not yet. However, there's an issue. Counting "visual" lines is hard, as you need to traverse the whole buffer. Would it be fine if the counter always started at `1` for the parts that are currently shown in the window? I *guess* this is about discussing text/code efficiently (e.g. "there's a typo in line 3") and not about persistent/cross-editor discussions (e.g. "there's a bug in lines 15-20 of main.c"), so that should be fine, right? – Zeta Oct 24 '20 at 06:30

1 Answers1

2

There is no built-in functionality to provide line numbers based on visual lines. First of all, compared to buffer lines (e.g. \n, \r\n), visual lines need to get recounted on every window width-change, which can bring Emacs down to its knees. Compared to normal buffer lines, visual lines are only second-class citizens in that regard.

However, the old linum-mode provides a starting point to get your desired behaviour. We can change linum-mode in a way that counts visual line modes, but we will disregard some warnings.

Here are the necessary steps:

  1. Copy linum.el to another file. For this demonstration, let's call it vinum.el
  2. Open vinum.el and M-x replace-string RET linum RET vinum RET
  3. Replace forward-line with next-line (warning: next-line is only meant for interactive use, however, C-n is next-line and you want to use its behaviour)
  4. M-x eval-buffer
  5. Open a buffer with long lines and enable both visual-line-mode and vinum-mode.

This will still use the correct line number for the first line. If you always want to start the numbers by 1, then replace (line-number-at-pos) by 1. If you want to count all visual lines, then replace (window-start win) by (point-min) and (window-end win t) by (point-max) instead.

Zeta
  • 1,045
  • 9
  • 18
  • This is working as I expected. Thanks! The only (minor) detail is that in order to refresh the line numbers (for example, if I keep writing or if I resize the window) is disabling and enabling again the vinum-mode. – Matematikisto Oct 25 '20 at 16:40
  • 1
    @Matematikisto you can probably add some more hooks to also refresh on those changes, but the whole thing is rather wonky. Someone else might be able to create a proper minor-mode, but I don't have enough elisp experience yet. – Zeta Oct 25 '20 at 17:54