Module: Mesa Branch: main Commit: 270d898e754f7ea8e4a55c138f5b59b47219f009 URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=270d898e754f7ea8e4a55c138f5b59b47219f009
Author: Eric Engestrom <[email protected]> Date: Mon Jun 19 12:42:26 2023 +0100 docs/coding-style: add example emacs config for clang-format Signed-off-by: Eric Engestrom <[email protected]> Reviewed-by: Alyssa Rosenzweig <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23722> --- docs/codingstyle.rst | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/codingstyle.rst b/docs/codingstyle.rst index 89cafd11642..ffa5b4d0c15 100644 --- a/docs/codingstyle.rst +++ b/docs/codingstyle.rst @@ -49,6 +49,38 @@ If ``/usr/share/clang/clang-format.py`` doesn't exist, try has put the file somewhere else, look through the files in the package providing ``clang-format``. +Emacs +***** + +Add this to your ``.emacs`` to automatically format any C & C++ file +(that has a .clang-format config) when you save it: + +.. code:: emacs + + (load "/usr/share/clang/clang-format.el") + + (defun clang-format-save-hook-for-this-buffer () + "Create a buffer local save hook." + (add-hook 'before-save-hook + (lambda () + (when (locate-dominating-file "." ".clang-format") + (clang-format-buffer)) + ;; Continue to save. + nil) + nil + ;; Buffer local hook. + t)) + + ;; Run this for each mode you want to use the hook. + (add-hook 'c-mode-hook (lambda () (clang-format-save-hook-for-this-buffer))) + (add-hook 'c++-mode-hook (lambda () (clang-format-save-hook-for-this-buffer))) + +If ``/usr/share/clang/clang-format.el`` doesn't exist, look through the +files in the package providing ``clang-format`` in your distro. If you +can't find anything (eg. on Debian/Ubuntu), refer to `this StackOverflow +answer <https://stackoverflow.com/a/59850773>`__ to install clang-format +through Emacs instead. + Basic formatting guidelines ---------------------------
