From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.help Subject: Re: How to whip Emacs into weird indentation rules with tabs+spaces? Date: Fri, 18 Jul 2008 16:51:12 +0000 Message-ID: <20080718165112.GA3171@muc.de> References: <200807180235.57978.juanma_bellon@yahoo.es> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1216399997 697 80.91.229.12 (18 Jul 2008 16:53:17 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 18 Jul 2008 16:53:17 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Juanma Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jul 18 18:54:05 2008 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KJtDM-0004SE-Hp for geh-help-gnu-emacs@m.gmane.org; Fri, 18 Jul 2008 18:53:52 +0200 Original-Received: from localhost ([127.0.0.1]:33709 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KJtCT-0000os-U3 for geh-help-gnu-emacs@m.gmane.org; Fri, 18 Jul 2008 12:52:57 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KJtBd-0000fV-8e for help-gnu-emacs@gnu.org; Fri, 18 Jul 2008 12:52:05 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KJtBb-0000eT-2q for help-gnu-emacs@gnu.org; Fri, 18 Jul 2008 12:52:04 -0400 Original-Received: from [199.232.76.173] (port=48464 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KJtBa-0000eG-J1 for help-gnu-emacs@gnu.org; Fri, 18 Jul 2008 12:52:02 -0400 Original-Received: from colin.muc.de ([193.149.48.1]:2624 helo=mail.muc.de) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KJtBa-0007ep-1k for help-gnu-emacs@gnu.org; Fri, 18 Jul 2008 12:52:02 -0400 Original-Received: (qmail 59000 invoked by uid 3782); 18 Jul 2008 16:51:56 -0000 Original-Received: from acm.muc.de (pD9E23E70.dip.t-dialin.net [217.226.62.112]) by colin2.muc.de (tmda-ofmipd) with ESMTP; Fri, 18 Jul 2008 18:51:54 +0200 Original-Received: (qmail 3367 invoked by uid 1000); 18 Jul 2008 16:51:12 -0000 Content-Disposition: inline In-Reply-To: <200807180235.57978.juanma_bellon@yahoo.es> User-Agent: Mutt/1.5.9i X-Delivery-Agent: TMDA/1.1.5 (Fettercairn) X-Primary-Address: acm@muc.de X-detected-kernel: by monty-python.gnu.org: FreeBSD 4.6-4.9 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:55705 Archived-At: Hi, Juanma, On Fri, Jul 18, 2008 at 02:35:57AM +0200, Juanma wrote: > Hi ... again. > The question is, for C++ code, how to make Emacs insert tabs for "main > indentation" and spaces for "sub-indentation". Example: > |---| = tab . = space > |---|while ( something && > |---|........some_else ) > |---|---|then_do_something(); > The idea is that tab goes one level further, but for same-level adjustments > only spaces should be used. > How to force Emacs into that insanity? (no, it's not my personal choice) I got precisely this request on bug-cc-mode@gnu.org in January, from a Mike Sullivan (Subject: Supporting tabs for indentation, spaces for alignment, Date: Wed, 9 Jan 2008 02:04:40 -0600, Message-ID: ). I replied to the effect that I would consider such a contribution for incorporation into CC Mode. :-) However, I also supplied a quick hack which can be installed on c-special-indent-hook. (Don't anybody ever accuse CC Mode of lack of flexibility!). It's probably not production quality code, but try it and see what you think: ######################################################################### (defun ms-space-for-alignment () "Make the current line use tabs for indentation and spaces for alignment. It is intended to be called from the hook `c-special-indent-hook'. It assumes that `indent-tabs-mode' is non-nil and probably assumes that `c-basic-offset' is the same as `tab-width'." (save-excursion (let* ((indent-pos (progn (back-to-indentation) (point))) (indent-col (current-column)) (syn-elt (car c-syntactic-context)) (syn-sym (c-langelem-sym syn-elt))) (when (memq syn-sym '(arglist-cont-nonempty)) ;; <============== (let* ((syn-anchor (c-langelem-pos syn-elt)) (anchor-col (progn (goto-char syn-anchor) (back-to-indentation) (current-column))) num-tabs) ;; (goto-char indent-pos) (delete-horizontal-space) (insert-char ?\t (/ anchor-col tab-width)) (insert-char ?\ (- indent-col (current-column)))))))) To enable it, do this, or similar: M-: (setq indent-tabs-mode t) M-: (setq tab-width 3) M-: (setq c-basic-offset tab-width) M-: (add-hook 'c-special-indent-hook 'ms-space-for-alignment nil t) ######################################################################### > Many thanks. > -- > Juanma -- Alan Mackenzie (Nuremberg, Germany).