I'm making my first steps in ELisp programming and I want to know is there a better solution for this problem:
I have a file with two columns, that are separated by one or more spaces, ex.:
One 1
Two 2
Three 3
Four 4
I'd like to make some alignment (columns are separated by at least two spaces):
One 1
Two 2
Three 3
Four 4
My solution is:
(defun align-config-file ()
"Align two-columned config file in a pretty manner"
(interactive)
(let* ((strings (split-string (buffer-string) "\n"))
(data (mapcar (lambda (line) (split-string line " ")) strings))
(first-col-lengths (mapcar (lambda (item) (length (car item))) data))
(first-col-length (+ 2 (apply 'max first-col-lengths))))
(erase-buffer)
(while data
(let* ((this-line (car data))
(first-col (car this-line))
(last-col (car (nthcdr (- (length this-line) 1) this-line)))
(amount-of-spaces (- first-col-length (length first-col)))
(spaces-inserted 0))
(insert first-col)
(while (< spaces-inserted amount-of-spaces)
(insert " ")
(set 'spaces-inserted (+ 1 spaces-inserted)))
(insert last-col)
(insert "\n"))
(set 'data (cdr data)))))
It is not beautiful, but it works. Is it ok?
align
command, that comes with Emacs? You seem to be addressing a solved problem. \$\endgroup\$