2

I am trying to configure my cc-styles to use a single basic offset for the braces of an inline defined lambda.

I have a function template which takes a functor as an argument

template<typename Func>
void some_function(Func f)
{
    // ...
}   

I am calling this function with a lambda, defined inline at the call site:

some_function([](int foo)
              {
                  // ...
              });

Notice how the opening and closing braces of the body of the lambda are aligned with the capture statement.

Our coding standards require that the braces are indented by one indent. As such, I am required to have the code look like the following:

some_function([](int foo)
    {
        // ...
    });

Question:

What do I need to specify in c-offsets-alist to get the indent I'm looking for?

Notes:

Here is the c-add-style call I'm making, in case that's of some use:

  (c-add-style "work"
               '((indent-tabs-mode . nil)                   
                 (c-basic-offset . 4)                       
                 (c-offsets-alist
                  (substatement-open . 0)                  
                  (case-label . +)                         
                  (inline-open . 0)                        
                  (block-open . 0)                         
                  (statement-cont . +)                     
                  (inextern-lang . 0)                      
                  (innamespace . 0))))                     
4

You can add a function for that to arglist-cont-nonempty. In the following example my-c-lineup-arglist-lambda should do what you want:

(c-add-style "work"
             '((indent-tabs-mode . nil)                   
               (c-basic-offset . 4)                       
               (c-offsets-alist
                (substatement-open . 0)                  
                (case-label . +)                         
                (inline-open . 0)                        
                (block-open . 0)                         
                (statement-cont . +)                     
                (inextern-lang . 0)                      
                (innamespace . 0)
                (arglist-cont-nonempty (my-c-lineup-arglist-lambda c-lineup-arglist)))))   


(defun my-c-lineup-arglist-lambda (langelem)
  "Line up lambda."
  (save-excursion
    (back-to-indentation)
    (when (looking-at "{")
      '+)))

Note that this approach does not touch indentation of other function arguments.

  • I have found that (arglist-cont-nonempty . +) achieves the same result. Would you agree, or is there something else you're doing in your function which I should be aware of? – Steve Lorimer Aug 10 '16 at 16:20
  • Of course, this would work for your example, but then all arguments would be indented that way. If thats what you want, ok, but probably you want the arguments spread over lines to be aligned. – theldoria Aug 10 '16 at 17:09
  • Ah, ok, I think I understand - that's the when looking-at "{" statement right? – Steve Lorimer Aug 10 '16 at 18:12
  • Yes, the indentation for lambda is returned only if { is seen (the back-to-indentation would position right before {), otherise nil is returned and c-lineup-arglist is executed and asked to return indent value. – theldoria Aug 10 '16 at 18:15
  • @theldoria This is great. However it doesn't work with lambda assigning statement. What OFFSET symbol should I use? thanks! – Amos Apr 12 '18 at 7:07
1

In some recent versions of Emacs, there's actually a new syntactic element available to handle lambdas: inlambda. This might be a bit clearner to use rather than a custom specifier function. As an example, I believe the following snippet would implement your desired lambda indentation:

(defconst my-c++-style
  '("stroustrup"
    (c-basic-offset   . 4)
    (c-offsets-alist  . ((inline-open         . 0)
                         (brace-list-open     . 0)
                         (inextern-lang       . 0)
                         (innamespace         . 0)
                         (inlambda            . 4)
                         (statement-case-open . +)))))

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.