Emacs - Specific Linum Customization

4.1k views Asked by At

I am trying to apply a few rather specific customizations to Emacs' linum.el, using the section from this EmacsWiki page on Line Numbers (entitled Linum: Separating line numbers from text) as a guide.

I implemented the edit to linum.el that adds a space to the right of the line number. What I am trying to figure out now is how to add an additional space to the left of the line number, so that the line numbers will look something like this:

| 1 |#include <stdio.h>  (Vertical bars for visualization)
| 2 |int main()

Another customization I am trying to accomplish involves including the right space in coloring the background for the line numbers. When I set the background for the linum face, it doesn't color that new space on the right of the line number.

To illustrate, here is how the linum background covers currently:

(current) |1| #!/bin/bash   -- vs. -->   |1 |#!/bin/bash (ideal)
          |2| echo "123"    --------->   |2 |echo "123"

I did attempt to implement the former (space-on-left) by blindly tinkering with the line from the EmacsWiki that gives the right space, to no success. Unfortunately, since I am somewhat new to Emacs and Elisp, my skills aren't at the level yet where I can spot what I need to change easily on my own.


Links:

EmacsWiki: Line Numbers [Section - Linum: Separating line numbers from text]

http://www.emacswiki.org/emacs/LineNumbers

4

There are 4 answers

3
nickie On BEST ANSWER

If you really want to do it like you said, using spaces, and not as @lawlist suggested, find the line containing (number-to-string w) in function linum-update-window and change it to:

(number-to-string (+ w 1))

This number is the width of the box in which the line number is printed, right aligned. The original w is the number of digits in the last line number.

2
lawlist On

Does something like this give you any ideas on your own type of customization? You may be more interested in setting the fringe width, instead of altering spaces to the right of the line numbers.

(setq-default left-fringe-width  10)
(setq-default right-fringe-width  0)
(set-face-attribute 'fringe nil :background "white" :foreground "white")


;;    (setq linum-format "%d") ;; alternative solution to intermittent line numbers
(custom-set-faces '(linum ((t (:foreground "pink" :background "white" :box nil)))) ) 
(eval-after-load 'linum
  '(progn
     (defface linum-leading-zero
       `((t :inherit 'linum
            :foreground ,(face-attribute 'linum :background nil t)))
       "Face for displaying leading zeroes for line numbers in display margin."
       :group 'linum)
     (defun linum-format-func (line)
       (let ((w (length
                 (number-to-string (count-lines (point-min) (point-max))))))
         (concat
;;        (propertize (make-string (- w (length (number-to-string line))) ?0)
          (propertize (make-string (- w (length (number-to-string line))) ? ) ;; change leading zero to a soft space
                      'face 'linum-leading-zero)
          (propertize (number-to-string line) 'face 'linum))))
     (setq linum-format 'linum-format-func)))
3
tungd On

I believe setting (setq linum-format "%d ") will do what you want. If you also want the space in the left, use " %d " for the format.

0
Howard Abrams On

Perhaps an easier solution is just to fix the size of the linum face attribute, with something like:

(set-face-attribute 'linum nil :height 140)

You may need to hook this up after you actually use the linum-mode:

(defun fix-linum-size ()
  (interactive)  
  (set-face-attribute 'linum nil :height 140))

(add-hook 'linum-mode-hook 'fix-linum-size)