简体   繁体   中英

emacs C/C++ indentation: different for pre and post comments // or: how can I make certain comments like //^ be indented extra?

BRIEFEST:

The original question boiled down to:

Many if not most emacs c indentation styles make comments on a line by themselves, eg

// ...

indent to match the surrounding code.

I want to make comments that look like //+ or //- be indented one or more indent settings more than the surrounding code.

I usually think of this in terms of having same-indentation-level comments apply to the next lines of code, and extra-indentation-level comments apply to preceding lines of code. But that's just style.

BRIEF:

Are there (reasonably standard) emacs settings to obtain different indentation for C/C++ comments, depending on whether the comment is associated with the next line (in which case I want it indented appropriately for the syntax) or whether the comment is associated with the previous line (in which case I want it indented an additional level)?

Eg

int
foo() 
{
     // this comment is for the variable bazz below
     int bazz;
         //- this comment is for the variable bazz,on the line above.
         //- I want it indented an extra level, which I will do by hand here.

     // this comment is for the next variable, biff
     int biff;

     int example; /* comment
               * on multiple lines
               * but this can be too much indented
               */

     int example_of_a_really_long_name_that_can_produce_excessive_indentation; /* comment
                                            * on multiple lines
                                            * but this can be too much indented
                                            */

}

The comments beginning //and //- are pretty much what I want. // is indented along with the surrounding code. This is what emacs gives me.

I would like //- to be indented an extra indentation level. Or //+,or whatever, if there is a convention. Sounds like a job for a regexp.

I provide examples of what I know how to do for // and / . The / comments get spread over multiple lines, but can be excessively indented.

DETAIL:

I am just now installing emacs 24.1 - or at least I hope I am installing it, the systems at work have very old and crankly distros. But if something works on older emacs, such as 21.4.1, I'd e even happier - avoiding the hassle of having to install emacs on many different systems.

================================

By the way, I already know about c-indent-comment-syntactically, and much of cc-mode.el.

In particular, c-indent-comment-syntactically gives me:

int
foo()
{
  // this comment is for the variable bazz below
  int bazz;
  //- this comment is for the variable bazz,on the line above.
  //- I want it indented an extra level, which I will do by hand here.

  // this comment is for the next variable, biff
  int biff;

  int example; /* comment
            * on multiple lines
            * but this can be too much indented
            */

  int example_of_a_really_long_name_that_can_produce_excessive_indentation; /* comment
                                             * on multiple lines
                                             * but this can be too much indented
                                             */

  if( foo )
    // comment
    bar();

  if( foo ) {
    // comment
    bar();
  }

}

This is not what I want. I want the //- comment lines to be indented an extra indent level.

Heck, why not something like elisp:

/// <-- start in columno 0

// indented syntactically (pertaining to next line of code)

or perhaps //^ (the ^ indicates "above".

//- or //^ - indented an extra indent level (pertaining to previous line of code.

Hmm, have you tried setting this ?

— User Option: c-indent-comments-syntactically-p

Normally, when this style variable is nil, M-; will indent comment-only lines according to c-indent-comment-alist, just as it does with lines where other code precede the comments. However, if you want it to act just like for comment-only lines you can get that by setting c-indent-comments-syntactically-p to non-nil.

If c-indent-comments-syntactically-p is non-nil then c-indent-comment-alist won't be consulted at all for comment-only lines.

So, if I understand what you want, you just want the capability to add one extra level of indentation based on some token. This is quite possible by using your own lineup function, you can find more info in the "CC Mode" Manual - specifically check out the section:

(ccmode) Line-Up Functions

Here's the custom lineup function, it checks if the comment starts with your special sequence (I used //+), if so it adds an extra level (returning the plus symbol). Otherwise, we ignore it and treat it as the comment is normally indented (returning nil). As you can probably imagine, using a lineup function, we can get as tricky as one likes - such as multiple tokens or nested levels.

(defun brian-comment-offset (langelem)
  (save-excursion
    (back-to-indentation)
    (cond 
      ((re-search-forward (regexp-quote "//+") (point-at-eol) t)
       '+)
      (t
       nil))))

To use it, you need to set the offset alias variable comment-intro to run this function. I do this by creating my own c style as shown below. I would recomend you copy whatever your current style is, and just modify/add the comment-intro alist only.

(c-add-style 
 "briancpp" '((c-basic-offset . 2)
        (c-comment-only-line-offset . 0)
        (c-offsets-alist
         (comment-intro . brian-comment-offset)  ; Here's our workhorse
         (defun-open . 0)
         (defun-close . 0)
         (statement-block-intro . +)
         (substatement-open . 0)
         (substatement-label . 0)
         (label . 0)
         (statement-cont . +)
         (inline-open . 0)
         (inline-close . 0)
         (innamespace . 0))))

(add-hook 'c++-mode-hook (lambda () 
               (c-set-style "briancpp")))

And here's what it looks like in action:

CC模式额外缩进ScreenShot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM