1

This is my C style:

(defconst mira-c-style
  '((c-basic-offset . 4)
    (indent-tabs-mode . nil)
    (tab-width . 7)
    (c-comment-only-line-offset . (0 . 0))
    (c-lineup-C-comments . 0)
    (c-electric-pound-behavior. alignleft) ; Pre-processor macros go to column 0
    (paragraph-start . "[   ]*\\(//+\\|\\**\\)[     ]*$\\|@param\\)\\|^")
    (c-offsets-alist . ((topmost-intro-cont    . 0)
                        (statement-block-intro . +)
                        (knr-argdecl-intro     . 5)
                        (substatement-open     . 0)
                        (substatement-label    . +)
                        (label                 . +)
                        (statement-case-open   . +)
                        (statement-cont        . +)
                        (arglist-intro  . +)
                        (arglist-close  . 0)
                        (arglist-cont-nonempty . +)
                        (access-label   . 0)
                        (arglist-cont-nonempty . +)
                        (cpp-macro . [0])
                        )))
  "Mira C Programming Style")
(c-add-style "Mira C" mira-c-style)
(setq c-default-style "Mira C")

I then use this fake function for testing:

int func(void) {
    sub_function(quite_a_long_arg, very_very_very_very_very_very_very_very_very_very_long_arg);
}

If I add a newline between the two arguments, I get what I expect:

int func(void) {
    sub_function(quite_a_long_arg,
        very_very_very_very_very_very_very_very_very_very_long_arg);
}

If I run indent-region on the whole function, I get this:

int func(void) {
  sub_function(quite_a_long_arg,
               very_very_very_very_very_very_very_very_very_very_long_arg);
}

I do not want this alignment. It does not respect the style definition above (I think?).

I suspect that clangd might be involved in this, I am using it through lsp mode. In real-life code it also breaks strings into sub-strings for the sake of column requirements, which is a big no-no for me.

What is causing this? How do I get control about it? How do I force indent-region to respect my C style?

Emacs 28.1 with lsp, clangd.

1
  • If you are using lsp, you should first douible–check that lsp-mode isn’t sending your code off to clangd to be formatted.
    – db48x
    Aug 15, 2022 at 7:24

1 Answer 1

1

I added this to the :init section of my init file, so far so good:

(setq lsp-enable-indentation nil)
(setq lsp-enable-on-type-formatting nil)

I'm not sure yet if there are other formatting options to turn off.

My current (working?) snippet from init.el:

(use-package lsp-mode
  :init
  ;; set prefix for lsp-command-keymap (few alternatives - "C-l", "C-c l")
  (setq lsp-keymap-prefix "C-c l")
  (setq lsp-enable-indentation nil)
  (setq lsp-enable-on-type-formatting nil)
  :hook (
         (c-mode . lsp-deferred)
         (lsp-mode . lsp-enable-which-key-integration))
  :commands (lsp lsp-deferred)
  :ensure t)
4
  • If you have a good file respecting Mira style, then open it (without lsp) and from menu Cc-mode select Guess style from file and then View style - you can further save the style and name it Mira.
    – Ian
    Aug 16, 2022 at 8:38
  • @Ian This would be to create a C style, but without the lsp setup above, lsp would still try to correct it, right?
    – Gauthier
    Aug 17, 2022 at 8:16
  • Yes, but lsp is only the interface for clang-format - the parameters can be trimmed with the variable lsp-clients-clangd-args. No - so why not really use clang-format to work for you - the clang-format docs show how to get a formatting style and use it as template for your case - here the clang-format style is detailed, including veryveryverylong indentation case.
    – Ian
    Aug 18, 2022 at 8:04
  • @Ian I tried to make clang format like my company wants it, a few years ago. I was missing some options, and uncrustify allowed me to do what was needed. Now we run uncrustify before commit anyway, but I want to format approximately correctly while writing, it's easier to read. It's awkward to have clangd auto-format with its default config, which looks vastly different, when what I wrote already looks like the correct format.
    – Gauthier
    Aug 19, 2022 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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