1

I love emacs and its modes but sometimes I do not agree with its indentation. In this specific case I am talking about glsl-mode . When I type an array inside a function, I want the square brackes to be indented past the curly braces. Like so:

void main () {
    vec4 thing = [
     1, 2, 3, 4;
    ]
}

What I get by default is this:

void main () {
    vec4 thing = [
     1, 2, 3, 4;
] // I do not want this
}

and I am wondering if there is a simple way to fix this. Can someone explain to me a possible solution?

  • If you selection the whole buffer with mark-whole-buffer (C-x h), then indent it with indent-region (C-M-\ ), does the closing bracket still fail to line up? Emacs often does not indent text automatically. Check out aggressive-indent for more "automatic" indentation. – Tianxiang Xiong Jul 28 '16 at 4:59
  • 2
    This looks like a bug to me. If you for whatever reason are forced to keep using a buggy version, a possible workaround is to enable something like c-mode instead (GLSL is after all, a specialized version of C...) and use its indentation setup. – wasamasa Jul 28 '16 at 6:44
  • @wasamasa Not, it is c-mode behavior (glsl-mode just uses c-mode here). The bracket-block is reckognized as syntax arglist-close. – Tobias Oct 26 '16 at 9:22
  • Is it right that you do not have a semi-colon after the closing bracket? (I do not know glsl-syntax.) @wasamasa: Did you try what you wrote in your comment? I do get the same bad indenting from the second code-snippet with unmodified c-mode. – Tobias Oct 26 '16 at 12:29
1

You can adjust the indentation of cc-mode used for glsl-mode. Put the following elisp lines into your initialization files.

(defun glsl-mode-adjust-closing-bracket ()
  (setf (cdr (assoc 'arglist-close c-offsets-alist)) 0))
(add-hook 'glsl-mode-hook #'glsl-mode-adjust-closing-bracket)

See the doc for variable c-offsets-alist in the manual for cc-mode: https://www.gnu.org/software/emacs/manual/html_node/ccmode/c_002doffsets_002dalist.html#c_002doffsets_002dalist

With the above modification you get the following indentation if you add a semi-colon after the closing bracket.

void main () {
    vec4 thing = [
        1, 2, 3, 4;
    ];
}

Remaining question: Is the semicolon missing in your example or is this valid glsl-syntax?

  • That was actually a syntax error on my part. It's actually helpful that it indents it "wrong" when there's a missing semicolon like this. – Startec Oct 31 '16 at 2:56
  • @Startec Does the hook resolve your problem with the indentation of the brackets? – Tobias Oct 31 '16 at 5:31
0

I don't know the glsl language or have that mode available on this machine for me to examine, but most modes that have indentation like this have variables that control it (often in very fine detail). So, if you start with C-h f glsl-mode it may tell you there. If not, C-h a glsl might reveal something interesting. Otherwise, you may be forced to follow the link in the help for glsl-mode to the source, and then just do some scrolling up and down to find all the variables.

Of course for individual cases, you can just change it yourself, after all you are in an editor. There is one of the C indentations I don't like, but it's for something I use seldom, so when it happens I just fix it after the fact.

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.