0

In c-mode, how to align following code so, that the second variable name's starting character (A) is aligned below the first variable name's starting character (V)?

static unsigned int V_name_1,
                    A_name_2;

c-show-syntactic-information shows, that that indentation can be justified with topmost-intro-cont.
(I am unable to find a setting to achieve the wanted indentation.)

0

There seems to be no valid function or value to set the indentation offset. So I came up with a new function which needs to be assigned to topmost-intro-cont. Use M-: c-set-offset to do it temporarily in a buffer with c-mode.

Here is the function:

(defun c-lineup-topmost-intro-cont-symbol (elem)
    "calculate offset to previous declared symbol or
function name to align variable names on different lines."
    (save-excursion
        (beginning-of-line)
        (c-beginning-of-statement-1)
        (when (search-forward-regexp  "[,(\[]" (line-end-position) t)
            (progn
                (search-backward-regexp "=" (line-beginning-position) t)
                (search-backward-regexp "\\<" (line-beginning-position) t)))
        (current-column)))

Short explanation: go to beginning of this statement, and search for , on current line. If it is not found return column of current indentation, else search backward for'=' and then for beginning of word and return column of beginning of this first symbol name.

Set this function with:

(add-to-list 'c-style-alist '( ;; your own stuff here
                              (c-offsets-alist . ( ;; your own stuff here
                                                  (topmost-intro-cont . (c-lineup-topmost-intro-cont-symbol))))))

Test case:

unsigned int foo = 5,
         blah;

unsigned int foo,
         blah;

unsigned int foo[],
         blah;

unsigned int foo[]={1,2},
         blah;


unsigned int foo= 5,
         blah;

unsigned int foo =5,
         blah;


unsigned int foo[] ={1,2},
         blah;

unsigned int foo[]= {1,2},
         blah;


unsigned int foo[]  = {1,2},
         blah;


unsigned int foo   = 1,
         blah;

unsigned int    foo,
        blah;

unsigned int    foo   = 1,
        blah;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.