Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Comments</h1> <p>I think <b>part of</b> the problem you experience is that when you instantiate templates, emacs CC mode views it with the same <code>template-args-cont</code> structure. So, taking this into account, I expanded on your original idea and tried to make it suit my liking; I made the code verbose so that hopefully everyone can understand my intention. :) This should not cause problems when you instantiate, and it also appears to work for template template parameters! Try this out until someone with more Elisp skills can provide a better solution!</p> <p>If you experience any 'fighting' (i.e. alternating or broken indentation), try reloading the cpp file <kbd>C-x</kbd><kbd>C-v</kbd><kbd>Enter</kbd> and indenting again. Sometimes with template template parameters emacs shows the inner arguments as <code>arglist-cont-nonempty</code> and even alternates back and forth with <code>template-args-const</code>, but the reload always restored state.</p> <h1> Usage </h1> <p>To do what you want try this out by using the code below and adding to your <code>c-offsets-alist</code> an entry:</p> <p><code>(template-args-cont . brian-c-lineup-template-args)</code></p> <p>and set the variable </p> <pre><code>(setq brian-c-lineup-template-closebracket t) </code></pre> <p>I actually prefer a slightly different alignment:</p> <pre><code>(setq brian-c-lineup-template-closebracket 'under) </code></pre> <h1>Code</h1> <pre><code>(defvar brian-c-lineup-template-closebracket 'under "Control the indentation of the closing template bracket, &gt;. Possible values and consequences: 'under : Align directly under (same column) the opening bracket. t : Align at the beginning of the line (or current indentation level. nil : Align at the same column of previous types (e.g. col of class T).") (defun brian-c-lineup-template--closebracket-p () "Return t if the line contains only a template close bracket, &gt;." (save-excursion (beginning-of-line) ;; Check if this line is empty except for the trailing bracket, &gt; (looking-at (rx (zero-or-more blank) "&gt;" (zero-or-more blank))))) (defun brian-c-lineup-template--pos-to-col (pos) (save-excursion (goto-char pos) (current-column))) (defun brian-c-lineup-template--calc-open-bracket-pos (langelem) "Calculate the position of a template declaration opening bracket via LANGELEM." (save-excursion (c-with-syntax-table c++-template-syntax-table (goto-char (c-langelem-pos langelem)) (1- (re-search-forward "&lt;" (point-max) 'move))))) (defun brian-c-lineup-template--calc-indent-offset (ob-pos) "Calculate the indentation offset for lining up types given the opening bracket position, OB-POS." (save-excursion (c-with-syntax-table c++-template-syntax-table ;; Move past the opening bracket, and check for types (basically not space) ;; if types are on the same line, use their starting column for indentation. (goto-char (1+ ob-pos)) (cond ((re-search-forward (rx (or "class" "typename" (one-or-more (not blank)))) (c-point 'eol) 'move) (goto-char (match-beginning 0)) (current-column)) (t (back-to-indentation) (+ c-basic-offset (current-column))))))) (defun brian-c-lineup-template-args (langelem) "Align template arguments and the closing bracket in a semi-custom manner." (let* ((ob-pos (brian-c-lineup-template--calc-open-bracket-pos langelem)) (ob-col (brian-c-lineup-template--pos-to-col ob-pos)) (offset (brian-c-lineup-template--calc-indent-offset ob-pos))) ;; Optional check for a line consisting of only a closebracket and ;; line it up either at the start of indentation, or underneath the ;; column of the opening bracket (cond ((and brian-c-lineup-template-closebracket (brian-c-lineup-template--closebracket-p)) (cond ((eq brian-c-lineup-template-closebracket 'under) (vector ob-col)) (t 0))) (t (vector offset))))) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload