From owner-freebsd-questions Thu Jun 14 16:19:46 2001 Delivered-To: freebsd-questions@freebsd.org Received: from dsl-64-193-218-89.telocity.com (dsl-64-193-218-89.telocity.com [64.193.218.89]) by hub.freebsd.org (Postfix) with SMTP id 6B25337B406 for ; Thu, 14 Jun 2001 16:19:42 -0700 (PDT) (envelope-from lucas@slb.to) Received: (qmail 21918 invoked by uid 1000); 14 Jun 2001 23:19:55 -0000 Date: Thu, 14 Jun 2001 18:19:55 -0500 From: Lucas Bergman To: j mckitrick Cc: questions@freebsd.org Subject: Re: emacs indentation question Message-ID: <20010614181955.A2100@billygoat.slb.to> Reply-To: lucas@slb.to References: <20010614180524.A43569@dogma.freebsd-uk.eu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i 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 +0100 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG > 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