help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to whip Emacs into weird indentation rules with tabs+spaces?


From: Alan Mackenzie
Subject: Re: How to whip Emacs into weird indentation rules with tabs+spaces?
Date: Fri, 18 Jul 2008 16:51:12 +0000
User-agent: Mutt/1.5.9i

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 address@hidden 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:
<address@hidden>).  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).




reply via email to

[Prev in Thread] Current Thread [Next in Thread]