0

In my .emacs i have put following for cuda highlighting:

(setq c-default-style '(("c++" . "linux")
                        (java-mode . "java")
                        (awk-mode . "awk")
                        (other . "gnu")))

(setq c-default-style "k&r"
      c-basic-offset 4)

;; Use C-Mode for CUDA
(add-to-list 'auto-mode-alist '("\\.cu\\'" . c-mode))

Indentation 4 spaces works for c-mode or c files but for cuda files indentation is set to 2 spaces.
Please help me in setting indentation for cuda files to 4 spaces. Thanks!

phils
  • 48,657
  • 3
  • 76
  • 115
skaushal
  • 101
  • 1

1 Answers1

1

The "gnu" style sets c-basic-offset to 2, so that's where that's coming from.

As you have specific needs for a specific filename extension, I'd probably just define a derived mode to use with auto-mode-alist:

(define-derived-mode cuda-mode c-mode "CUDA"
  "CUDA mode."
  (setq c-basic-offset 4))

(add-to-list 'auto-mode-alist '("\\.cu\\'" . cuda-mode))

You might alternatively call c-set-style in the mode body, for some appropriate style, or even have an empty mode body and add an entry to c-default-style for the new mode, depending on which approach you think will be the easiest to maintain.


Note also that c-basic-offset is automatically buffer-local, so the following isn't doing anything useful for that variable:

(setq c-default-style "k&r"
      c-basic-offset 4)

You could use (setq-default c-basic-offset 4) if you want to, but it's probably sensible to leave the default value alone.


Lastly, ("c++" . "linux") looks to me to be invalid as an entry in the c-default-style alist. That list maps major mode symbols to style names, and "c++" is not a symbol (nor even the name of a major mode symbol). I doubt that entry is doing anything?

phils
  • 48,657
  • 3
  • 76
  • 115
  • Thanks @phils for quick reply! I removed the code that I mention in the question and added you code in .emacs. Still indentation for cuda .cu files remains 2 spaces. I wonder why it is not 4 spaces indentation. Please help – skaushal Aug 09 '18 at 09:48
  • My suggested code works for me. – phils Aug 09 '18 at 11:53
  • Any suggestion to debug this issue? – skaushal Aug 09 '18 at 14:44
  • Run `emacs -Q` to disable non-standard libraries, and then evaluate my suggestion in the `*scratch*` buffer, and then test and confirm whether or not it works. – phils Aug 09 '18 at 21:16
  • If it still doesn't work under `emacs -Q`, then let me know which version of Emacs you're using. Confirm that the "CUDA" lighter is displayed in the mode line (or that `C-h m` indicates the *.cu buffer is in `cuda-mode`), and check what `C-h v c-basic-offset` shows you (when used in the *.cu buffer). – phils Aug 09 '18 at 21:16
  • If it *does* work in `emacs -Q` but not under `emacs` otherwise, then something elsewhere in your config is conflicting, and you'll need to track that down. If it works with `-Q` but fails under `emacs -q` (lower-case) then the problem lies in the system-wide site-lisp libraries (probably unlikely). If it works under both `-Q` and `-q` then it's definitely your personal config causing the conflict. – phils Aug 09 '18 at 21:20
  • You can use the recursive bisection technique to narrow down an unknown problem in your config in an efficient manner. https://github.com/Malabarba/elisp-bug-hunter might help you semi-automate the process. – phils Aug 09 '18 at 21:22