diff --git a/config.org b/config.org index 06349cb..266b822 100644 --- a/config.org +++ b/config.org @@ -1186,6 +1186,40 @@ (add-hook 'rust-mode-hook 'cargo-minor-mode) #+end_src +** clang-format + Most of the time, =lsp-mode= is fine for formatting, but sometimes + it doesn't work (mostly just because I haven't gone through the + effort to set it up) but I still want to be able to auto-format + code easily (that is to say, with a convenient keybinding). The + =clang-format= package provides Elisp functions for invoking it. + + #+begin_src emacs-lisp + (use-package clang-format) + #+end_src + + I want a keybinding that formats the region if its active, or the + whole buffer otherwise. It seems that there's no function which + does that out of the box, so that has to be defined first: + + #+begin_src emacs-lisp + (defun clang-format-region-or-buffer () + "Format the region if it's active, otherwise format the entire buffer." + (interactive) + (if (region-active-p) + (clang-format-region) + (clang-format-buffer))) + #+end_src + + With that defined, the keybinding can be added to C mode. + + #+begin_src emacs-lisp + (add-hook + 'c-mode-hook + (lambda () + (define-key c-mode-map (kbd "C-M-f") + 'clang-format-region-or-buffer))) + #+end_src + * Backup and Autosave ** Keep $PWD Tidy Emacs' default behaviour of dumping temporary files in the current