I'm modifying some elisp code from linum.el:
(custom-set-variables '(linum-format 'dynamic))
(defadvice linum-update-window (around linum-dynamic activate)
(let* ((w (length (number-to-string
(+ (count-lines (point-min) (point-max)) 1))))
(linum-format (concat " %" (number-to-string w) "d ")))
ad-do-it))
I was able to fix a bug where the indentation was off-by-one by modifying (count-lines (point-min) (point-max))
to (+ (count-lines (point-min) (point-max)) 1)
. That was easy.
But now I want to modify it so that the minimum width is 2 by adding an if-conditional where (concat " %" (number-to-string w) "2d ")
if the line number count is < 10.
This should be easy! Add one conditional and copy/paste the concat. Piece of cake, right? I mean, I know what I'm supposed to do but I rarely touch elisp and I'm always daunted when I have to modify anything with lots of parenthesis.
The "correct" style, from what I understand, is to structure the code based on indentation and wrap the trailing parenthesis at the end of a line rather than on its own. Coming from other 'C' style languages, I struggle with both reading and writing code in this way. So my question is: what am I doing wrong? How am I supposed to edit elisp and navigate around the code such that I don't have to sit there and count every parenthesis?
When I work with something in elisp that gets too deep I have to shut my door, pull the blinds, and start positioning the parenthesis K&R-style so that I can not only read but modify the darn thing without freaking out.
Obviously I'm doing this all wrong. How can I touch elisp like this without fear?
Please note that my question is how to navigate and edit elisp not as a question on style. I am using the following as a style guide already: https://github.com/bbatsov/emacs-lisp-style-guide
Updates:
How to properly format elisp before embarassing yourself on emacs.stackexchange:
Mark your elisp and perform M-x indent-region
.
The Problem Solution:
For those wanting to know how to perform a right-justify for linum with a minimum width of two, here is the solution:
(defadvice linum-update-window (around linum-dynamic activate)
(let* ((w (length (number-to-string
(+ (count-lines (point-min) (point-max)) 1))))
(linum-format (if (> w 1)
(concat " %" (number-to-string w) "d ")
" %2d ")))
ad-do-it))