Advertisement
Guest User

Defining a custom C/C++ formatting style in ELisp

a guest
Dec 9th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.65 KB | None | 0 0
  1. (defun my-c-and-c++-mode-hook ()
  2.   "Added to c-mode-hook and c++-mode-hook (as defined by cc-mode.el).  NOTE:
  3. This hook is designed to be used with both C and C++ files.  To distinguish
  4. the two, test the value of the symbol major-mode against 'c-mode or 'c++-mode."
  5.   (local-set-key (kbd "TAB") 'tab-to-tab-stop)
  6.   (setq indent-tabs-mode nil)  ;; Replace tabs with spaces.
  7.  
  8.   (let ((my-style (assoc-string "franl" c-style-alist 'ignore-case)))
  9.  
  10.     ;; First, delete my custom formatting style from c-style-alist.
  11.     (if my-style
  12.         (setq c-style-alist (delete my-style c-style-alist)))
  13.  
  14.     ;; Next, add my custom style.  We do this so re-executing this hook
  15.     ;; updates c-style-alist to my latest definition of this style.
  16.     (add-to-list 'c-style-alist
  17.                  `("franl"
  18.                    (c-basic-offset . ,tab-width)
  19.                    (c-comment-only-line-offset . (0 . 0))
  20.                    (c-double-slash-is-comments-p . t)
  21.                    (c-echo-syntactic-information-p . nil)
  22.                    (c-electric-pound-behavior . (alignleft))
  23.                    (c-indent-comments-syntactically-p . t)
  24.                    (c-hanging-comment-ender-p . nil)
  25.                    (c-recognize-knr-p . nil)
  26.                    (c-offsets-alist . ((substatement-open . 0)
  27.                                        (brace-list-open . 0)
  28.                                        (inline-open . 0)
  29.                                        ;;(c . 1)
  30.                                        (arglist-close . ++)))))
  31.     (c-set-style "franl"))
  32.   )
  33.  
  34. (add-hook 'c++-mode-hook 'my-c-and-c++-mode-hook)
  35. (add-hook 'c-mode-hook 'my-c-and-c++-mode-hook)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement