Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Jun 2001 18:19:55 -0500
From:      Lucas Bergman <lucas@slb.to>
To:        j mckitrick <jcm@FreeBSD-uk.eu.org>
Cc:        questions@freebsd.org
Subject:   Re: emacs indentation question
Message-ID:  <20010614181955.A2100@billygoat.slb.to>
In-Reply-To: <20010614180524.A43569@dogma.freebsd-uk.eu.org>; from jcm@FreeBSD-uk.eu.org on Thu, Jun 14, 2001 at 06:05:24PM %2B0100
References:  <20010614180524.A43569@dogma.freebsd-uk.eu.org>

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 ()
  "  Align #define values in the current region."
  (interactive)
  (save-excursion
    (goto-char (mark))
    (let ((maxlen 0)
	  (defre "^#[ \t]*define[ \t]+\\([^ \t]+\\)[ \t]+\\(.*\\)$")
	  (padding "")
	  (numspaces 0))
      (while (re-search-forward defre nil t)
	(let ((elen (- (match-end 1) (match-beginning 1))))
	  (when (> elen maxlen)
	    (setq maxlen elen))))
      (goto-char (mark))
      (setq padding (make-string (+ 2 maxlen) ?\ ))
      ;;
      ;; taking two regexp searching passes through the block...  I'm
      ;; ashamed of myself.
      ;;
      (while (re-search-forward defre nil 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)

Put that monstrosity in your ~/.emacs, restart Emacs, then highlight
a block of #define's in a C file, and run M-x
slb-c-align-defines-region.  A similar trick could work for aligning
declaration blocks, but the regex won't be quite as straightforward.

I'm a terrible Lisp hacker, so please bear that in mind when composing
your flames, eh, guys?

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?20010614181955.A2100>