Skip to content

Conversation

miseran
Copy link
Contributor

@miseran miseran commented Sep 23, 2018

In order to save horizontal space, this patch allows you to display signs in place of line numbers in the number column by setting signcolumn=number. Additionally, setting signcolumn=number_hl will display the line number, but highlighted according to the sign.

I'm not an experienced C programmer, so please forgive any rookie mistakes.

Some comments:

  • The code is currently broken with numberwidth <= 2. This can either be fixed by forcing the number column to be wider in this case, or by truncating or hiding the sign. It now eats into the padding to get enough room.
  • There is a bug in the old code where the extra buffer can be overflown with a sign text with unreasonable amounts of combining characters. I switched to using p_extra_free to fix this.
  • I tried to minimize code duplication by moving most of the drawing to the draw_sign function, although it's still not very elegant.
  • I currently have the highlight of the sign win out over cursorline, but that can easily be changed.
  • The code currently "abuses" the fact that number starts with the letter n, and hence is interpreted as a no by the part of the code that hides the regular sign column. Maybe this is a bit too hacky, but easy to fix.
  • There is a WIP pull request ([WIP] Variable signcolumn #6196) to allow showing multiple signs in a line. That would have to be adjusted to work with this.

if ((wp->w_p_cul || wp->w_p_rnu)
&& lnum == wp->w_cursor.lnum) {

if (text_sign != 0 && strstr((char *)wp->w_p_scl, "number") != NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General note: win_line is a hot path, so it needs to stay fast. It's probably negligible, or maybe compilers optimize this strstr, but the general practice in win_line is avoid the risk.

Typical "Vim style" would be to check (*wp->w_p_scl == 'n' && *wp->w_p_scl == 'u'). But that's a bit much, rather store a boolean at the top of win_line.

For the same reason we might want to mark draw_sign as inline (it should also be static).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a concern, the value could be parsed into an enum by option.c at the time of setting, as is done for other "hot-path" string options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this code is run only once per call to win_line anyway, except in the case of wrapping lines. So I don't think putting it at the beginning of win_line would do much. For now, I did the char-wise check.

As for using an enum, I haven't found any such cases for other options. Do you have an example?

@marvim marvim added the WIP label Sep 23, 2018
@bfredl
Copy link
Member

bfredl commented Sep 24, 2018

I find the signcolumn=number_hl feature quite interesting! But I wonder if really should be entangled with the elimination of the signcolumn, because I would find it useful to use both, say, the signcolumn for gitgutter, and colored numbers to highlight warnings/errors. Perhaps numhl= option could be added to sign define, in analogy with linehl=.

@miseran
Copy link
Contributor Author

miseran commented Sep 24, 2018

I hadn't thought of that possibility. I think adding numhl= as you suggested would be outside the scope of this PR, but perhaps I should remove the number_hl option in order to keep this possibility open. I mostly just added it because it came almost for free with the rest of the implementation.

@miseran
Copy link
Contributor Author

miseran commented Sep 24, 2018

I dealt with the numberwidth <= 2 case by removing the padding if the width is too small. This should always be enough currently, since the number column has to be at least 2 characters (a digit and the empty column). This part will have to be rewritten anyway for variable width sign columns, so I didn't take that into account.

I also removed number_hl as discussed.

@miseran
Copy link
Contributor Author

miseran commented Sep 29, 2018

I added a test, but I'm not sure if I did it right. Perhaps I should split it up into smaller tests?
Apart from that, the code seems ready to me. I will add documentation once the rest is approved.

@miseran miseran changed the title [WIP] Allow displaying signs in the number column [RFC] Allow displaying signs in the number column Sep 29, 2018
@marvim marvim added RFC and removed WIP labels Sep 29, 2018
@justinmk justinmk added the needs:discussion issue needs attention from an expert, or PR proposes significant changes to architecture or API label Oct 13, 2018
@justinmk
Copy link
Member

justinmk commented Oct 13, 2018

Status/Resolution

From #9113:

As far as I'm concerned, this removes the need for #9040, so I'll put that on hold unless other people are interested in it.

If anyone still wants this, explain the use-case and explain why #9113 doesn't resolve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs:discussion issue needs attention from an expert, or PR proposes significant changes to architecture or API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants