Date: Mon, 18 Jun 2001 09:17:03 -0500 From: Lucas Bergman <lucas@slb.to> To: jcm@FreeBSD-uk.eu.org Cc: questions@freebsd.org Subject: Re: emacs indentation question Message-ID: <20010618091703.B19099@billygoat.slb.to> In-Reply-To: <20010614181955.A2100@billygoat.slb.to>; from lucas@slb.to on Thu, Jun 14, 2001 at 06:19:55PM -0500 References: <20010614180524.A43569@dogma.freebsd-uk.eu.org> <20010614181955.A2100@billygoat.slb.to>
next in thread | previous in thread | raw e-mail | index | archive | help
> > When in C/C++ mode, TAB indents to where emacs thinks the text
> > should go. How do you add extra tabs at the end of a line, say,
> > to line up a group of variable names or #define values?
>
> Emacs doesn't line up that sort of thing automatically, AFAIK. You
> can run your program through indent(1), of course, or you can write
> Emacs functions to do what you want. For example, I banged this out
> to align a block of #define's:
>
> (defun slb-c-align-defines-region ()
> [... schnipp ...]
Mea culpa. I just realized that the slb-c-align-defines-region
function I posted was an intermediate version that had bugs. Here's
the real one:
(defun slb-c-align-defines-region (start end)
" Align #define values in the current region."
(interactive "r")
(save-excursion
(let ((maxlen 0)
(defre "^#[ \t]*define[ \t]+\\([^ \t]+\\)[ \t]+\\(.*\\)$")
(padding "")
(numspaces 0))
(goto-char start)
(while (re-search-forward defre end t)
(let ((elen (- (match-end 1) (match-beginning 1))))
(when (> elen maxlen)
(setq maxlen elen))))
(goto-char start)
(setq padding (make-string (+ 2 maxlen) ?\ ))
;;
;; Taking two regexp searching passes through the region... I'm
;; ashamed of myself.
;;
(while (re-search-forward defre end t)
(setq numspaces (- maxlen (- (match-end 1) (match-beginning 1))))
(replace-match
(concat "#define " (match-string 1)
(substring padding 0 (1+ numspaces))
(match-string 2))))))
nil)
I'm not trying to contribute gratuitously to off-topic discussion, but
I'm trying to keep incorrect stuff out of the archives even more...
Lucas
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010618091703.B19099>
