all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to whip Emacs into weird indentation rules with tabs+spaces?
@ 2008-07-18  0:35 Juanma
  2008-07-18 16:51 ` Alan Mackenzie
  0 siblings, 1 reply; 7+ messages in thread
From: Juanma @ 2008-07-18  0:35 UTC (permalink / raw)
  To: help-gnu-emacs

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)

Many thanks.
-- 
Juanma

"Having a smoking section in a restaurant is like
 having a peeing section in a swimming pool."
       -- Edward Burr








^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to whip Emacs into weird indentation rules with tabs+spaces?
       [not found] <mailman.14916.1216341367.18990.help-gnu-emacs@gnu.org>
@ 2008-07-18 10:04 ` Xah
  0 siblings, 0 replies; 7+ messages in thread
From: Xah @ 2008-07-18 10:04 UTC (permalink / raw)
  To: help-gnu-emacs

> The question is, for C++ code, how to make Emacs insert tabs for "main
> indentation" and spaces for "sub-indentation". Example: ..

Maybe someone has thought this up before... but if it comes down to
write your own, here's a suggestion.

Tab key runs the c-indent-command in c mode. So, you might define tab
to run my-own-c-indent.  The command will first check if the beginning
of line is a tab. If so, move to the end of white spaces and insert
spaces. If the beginning of line is a not white-space, then your
command can call the c indent command. you might look up the source
code and borrow pieces...

  Xah
∑ http://xahlee.org/

☄

On Jul 17, 5:35 pm, Juanma <juanma_bel...@yahoo.es> 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)
>
> Many thanks.
> --
> Juanma
>
> "Having a smoking section in a restaurant is like
>  having a peeing section in a swimming pool."
>        -- Edward Burr



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to whip Emacs into weird indentation rules with tabs+spaces?
  2008-07-18  0:35 Juanma
@ 2008-07-18 16:51 ` Alan Mackenzie
  2008-07-18 21:53   ` Nikolaj Schumacher
  2008-07-19 18:18   ` Juanma
  0 siblings, 2 replies; 7+ messages in thread
From: Alan Mackenzie @ 2008-07-18 16:51 UTC (permalink / raw)
  To: Juanma; +Cc: help-gnu-emacs

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:
<a2dd74f90801090004r347ae528oc274782f5ae91f5c@mail.gmail.com>).  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).




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to whip Emacs into weird indentation rules with tabs+spaces?
  2008-07-18 16:51 ` Alan Mackenzie
@ 2008-07-18 21:53   ` Nikolaj Schumacher
  2008-07-19  1:56     ` Juanma
  2008-07-19 18:18   ` Juanma
  1 sibling, 1 reply; 7+ messages in thread
From: Nikolaj Schumacher @ 2008-07-18 21:53 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: help-gnu-emacs

Alan Mackenzie <acm@muc.de> wrote:

> On Fri, Jul 18, 2008 at 02:35:57AM +0200, Juanma wrote:
>
>> |---| = 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)

The way I see it, that would be the only /sane/ way to use tabs at all. :)
Because no matter what tab-width is set to, the code will always look
right...  Unfortunately there's probably not a single editor out there that
supports the scheme, so it's even less portable.

> (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))))))))

Great, but it leaves the point at the beginning of space-only lines.
A quick fix would be to replace the last two lines with this:

(insert-before-markers (make-string (/ anchor-col tab-width) ?\t))
(insert-before-markers (make-string (- indent-col (current-column)) ?\ )))))))


regards,
Nikolaj Schumacher




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to whip Emacs into weird indentation rules with tabs+spaces?
  2008-07-18 21:53   ` Nikolaj Schumacher
@ 2008-07-19  1:56     ` Juanma
  0 siblings, 0 replies; 7+ messages in thread
From: Juanma @ 2008-07-19  1:56 UTC (permalink / raw)
  To: help-gnu-emacs

On Friday 18 July 2008, Nikolaj Schumacher wrote:
> Alan Mackenzie <acm@muc.de> wrote:
> 
> > On Fri, Jul 18, 2008 at 02:35:57AM +0200, Juanma wrote:
> >
> >> |---| = 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)
> 
> The way I see it, that would be the only /sane/ way to use tabs at all. :)
> Because no matter what tab-width is set to, the code will always look
> right...

Well, actually I agree with you. I'd prefer no tabs, but if there are tabs,
this way is the only good one I can think of. But I thought it would be an
eLisp insanity.

> Unfortunately there's probably not a single editor out there that
> supports the scheme, so it's even less portable.

That supports it *automatically*. My colleagues, to whom I owe this
pleasure, use editors that insert actual tab characters when they press TAB
key. Go figure  :-)

Many thanks to everybody for help. I'll try it and tell you how good it is.
-- 
Juanma

"Having a smoking section in a restaurant is like
 having a peeing section in a swimming pool."
       -- Edward Burr







^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to whip Emacs into weird indentation rules with tabs+spaces?
  2008-07-18 16:51 ` Alan Mackenzie
  2008-07-18 21:53   ` Nikolaj Schumacher
@ 2008-07-19 18:18   ` Juanma
  2008-07-20 22:13     ` Nikolaj Schumacher
  1 sibling, 1 reply; 7+ messages in thread
From: Juanma @ 2008-07-19 18:18 UTC (permalink / raw)
  To: help-gnu-emacs

On Friday, 18 July 2008 6:51:12 pm Alan wrote:
>  M-: (add-hook 'c-special-indent-hook 'ms-space-for-alignment nil t)

I added that sexp to .emacs and I eveluated it. Then, on failing, I removed
the 4th argument to force the value globally. Nothing changed. The function
is not being called.

I checked the documentation of c-special-indent-hook:
----------------------------------------
c-special-indent-hook is a variable defined in `cc-vars.el'.
Its value is 
(ms-space-for-alignment)
[...] It is only called if `c-syntactic-indentation' is non-nil.
----------------------------------------

So let's see that variable:
----------------------------------------
c-syntactic-indentation is a variable defined in `cc-vars.el'.
Its value is t
----------------------------------------

I instrumented ms-space-for-alignment for Edebug, but it's not being called
at all, either when I press TAB or when I type C-j, or during region
reformatting, or anything. Can anyone figure why?

Best regards,
-- 
Juan

"Having a smoking section in a restaurant is like
 having a peeing section in a swimming pool."
       -- Edward Burr






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to whip Emacs into weird indentation rules with tabs+spaces?
  2008-07-19 18:18   ` Juanma
@ 2008-07-20 22:13     ` Nikolaj Schumacher
  0 siblings, 0 replies; 7+ messages in thread
From: Nikolaj Schumacher @ 2008-07-20 22:13 UTC (permalink / raw)
  To: Yo mismo; +Cc: help-gnu-emacs

Juanma <juanma_bellon@yahoo.es> wrote:

> I instrumented ms-space-for-alignment for Edebug, but it's not being called
> at all, either when I press TAB or when I type C-j, or during region
> reformatting, or anything. Can anyone figure why?

Try instrumenting `c-indent-line'.  That's where the hook should be run.
And check what `indent-line-function' is set to.

regards,
Nikolaj Schumacher




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-07-20 22:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.14916.1216341367.18990.help-gnu-emacs@gnu.org>
2008-07-18 10:04 ` How to whip Emacs into weird indentation rules with tabs+spaces? Xah
2008-07-18  0:35 Juanma
2008-07-18 16:51 ` Alan Mackenzie
2008-07-18 21:53   ` Nikolaj Schumacher
2008-07-19  1:56     ` Juanma
2008-07-19 18:18   ` Juanma
2008-07-20 22:13     ` Nikolaj Schumacher

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.