Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs indent an extra level for every continued line

How do you tell emacs to indent the current continued line (e.g. after a dot or indirection operator) one level deeper than the previous one? Arguments about which one is prettier are irrelevant here since this is the style we use at my work, so I don't really have a choice.

I'm guessing it's an offset (maybe statement-cont?), but I'm not sure how to do it dynamically like that...

Kind of related: How to control indentation after an open parenthesis in Emacs

Have

#include <stdio.h>

struct thing {
    int the_first_piece_of_data;
};

struct stuff {
    struct thing the_first_thing_in_this_stuff;
    struct thing *pointer_to_the_first_thing_in_this_stuff;
};

int main(int argc, char *argv[])
{
    struct stuff some_stuff_to_work_with;
    struct stuff *pointer_to_stuff = &some_stuff_to_work_with;
    some_stuff_to_work_with.
        pointer_to_the_first_thing_in_this_stuff =
        &(some_stuff_to_work_with.
          the_first_thing_in_this_stuff);

    some_stuff_to_work_with.
        the_first_thing_in_this_stuff.
        the_first_piece_of_data = 42;

    printf("The piece of data is => %d\n",
           some_stuff_to_work_with.
           the_first_thing_in_this_stuff.
           the_first_piece_of_data);

    pointer_to_stuff->
        pointer_to_the_first_thing_in_this_stuff->
        the_first_piece_of_data++;

    printf("The piece of data is => %d\n",
           pointer_to_stuff->
           pointer_to_the_first_thing_in_this_stuff->
           the_first_piece_of_data);

    return 0;
}

Want

#include <stdio.h>

struct thing {
    int the_first_piece_of_data;
};

struct stuff {
    struct thing the_first_thing_in_this_stuff;
    struct thing *pointer_to_the_first_thing_in_this_stuff;
};

int main(int argc, char *argv[])
{
    struct stuff some_stuff_to_work_with;
    struct stuff *pointer_to_stuff = &some_stuff_to_work_with;
    some_stuff_to_work_with.
        pointer_to_the_first_thing_in_this_stuff =
            &(some_stuff_to_work_with. /*exra indent*/
              the_first_thing_in_this_stuff);

    some_stuff_to_work_with.
        the_first_thing_in_this_stuff.
            the_first_piece_of_data = 42; /*exra indent*/

    printf("The piece of data is => %d\n",
           some_stuff_to_work_with.
               the_first_thing_in_this_stuff. /*exra indent*/
                   the_first_piece_of_data); /*exra indent*/

    pointer_to_stuff->
        pointer_to_the_first_thing_in_this_stuff->
            the_first_piece_of_data++; /*exra indent*/

    printf("The piece of data is => %d\n",
           pointer_to_stuff->
               pointer_to_the_first_thing_in_this_stuff-> /*exra indent*/
                   the_first_piece_of_data); /*exra indent*/

    return 0;
}
like image 665
mgalgs Avatar asked Nov 18 '25 22:11

mgalgs


1 Answers

This is not possible with the built-in indentation options.

You can verify this by using C-c C-s (aka c-show-syntactic-information) on the lines where you want the /* extra indent */ and the line above them, and you'll see that the syntactic information for those lines is always the same. In other words, as far as the indentation engine knows, those lines are the same syntactically.

Check out the documentation for interactive customization.

It might be possible to do what you want by customizing c-special-indent-hook.

like image 116
Trey Jackson Avatar answered Nov 20 '25 13:11

Trey Jackson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!