10

In Emacs, how to set a different background color after 80 columns?

So that if the window is wider than 80 columns, all the columns after 80 would have a different background color.

Drew
  • 29,895
  • 7
  • 74
  • 104
Sampo Smolander
  • 1,605
  • 2
  • 15
  • 33
  • 1
    [This](http://www.emacswiki.org/emacs/FindLongLines) EmacsWiki page mentions several approaches to highlighting a limit column etc. None of the respond directly to what you request (background different past a limit column), but you might find some of them useful. – Drew Feb 27 '15 at 21:41
  • 1
    If you don't find a solution ready-made, it should be pretty simple to iterate over the lines in the buffer and add an overlay with the background color for columns past the limit. – Drew Feb 27 '15 at 21:42
  • If this has not yet been invented, then `fill-column-indicator` would be a likely candidate for modification by adding an overlay with color when column at end of vertical line is greater than the fill-column. https://github.com/alpaker/Fill-Column-Indicator See also the built-in `whitespace-mode` to see what it can offer. – lawlist Feb 27 '15 at 23:45
  • Related question: http://stackoverflow.com/questions/6344474/how-can-i-make-emacs-highlight-lines-that-go-over-80-chars – Juancho Feb 28 '15 at 14:39
  • 1
    @lawlist I don't recommend `fill-column-indicator` as it causes more issues than benefit. – xuhdev Oct 19 '16 at 04:21
  • @xuhdev -- my idea was to *modify* `fill-column-indicator` because it teaches us how to use overlays with the `after-string` property. This is the only method Emacs offers to create a floating object/color *after* the end of a line, other than the standard `background` property for a face that spans multiple lines. – lawlist Oct 19 '16 at 04:46
  • 1
    @lawlist I see. Sorry for the misunderstanding. But the OP does not ask about "one line" either -- the OP asks for a different background color for all lines per my understanding. – xuhdev Oct 19 '16 at 06:09
  • @xuhdev -- correct -- just like `fill-collumn-indicator`, the overlays would need to be redrawn on the visible window as things change -- 50+ lines depending upon the screen resolution. The reason I am familiar with that library is because I have a custom minor mode that tracks the cursor position with a cross-hairs spanning the vertical and horizontal lengths of the screen -- it uses the same principle as `fill-column-indicator`, and the cross-hairs extend beyond the ends of the lines using the overlay `after-string` property. What the OP seeks is doable, but involves an investment of time. – lawlist Oct 19 '16 at 11:26
  • @xuhdev -- the `vline.el` library is another example that could be modified to create the visual effect that the OP seeks -- for each visible line. https://www.emacswiki.org/emacs/vline.el In that case, the vertical line would begin at line 80 (for example) and be just a background color -- that color can extend as far to the right as so desired (e.g., all the way to the right edge of the window). – lawlist Oct 19 '16 at 11:32

1 Answers1

0

I think you can do this using whitespace-mode and then suitably customizing whitespace-style to just do 80-column highlighting, and then customizing the face that is used to set the background color.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
  • 3
    This technique only sets the background color on characters that are after column 80. If there are no characters after column 80, the remainder of the visual line will not be colored differently. If there is 1 character after column 80, only it will be colored differently, but not the remainder of the visual line. The question asks how to set the background color for the entire window after a certain column number. – WalterGR Jul 09 '18 at 23:48
  • 1
    Additionally, this technique will make the foreground color the same for any characters after 80 columns. When in a buffer that uses different foreground colors to distinguish things (for example, distinguishing code comments from variable names from control statements,) they would all look the same after column 80. If only the background color could be changed, this unfortunate detail would be avoided. – WalterGR Jul 09 '18 at 23:50