Skip to content

Emacs C style

Erik Massop edited this page Nov 4, 2017 · 1 revision

Cleaner and Correct Version of the Emacs Style Below

Remember to get smart tabs, the implementation below may be broken on your emacs (it was on mine). When you have smarttabs installed c-basic-offset becomes an alias for tab-width, but we'll set it just in case.

There is no reason to treat or call the xmms2 style a mode (and the below aren't modes anyway). Instead create a file named xmms2-style.el and insert the following:

(c-add-style "xmms2"
             '("K&R"
               (c-basic-offset . 4)
               (c-offsets-alist
                (case-label . +)
                (arglist-close . c-lineup-arglist-intro-after-paren)
                (inextern-lang . 0))))

(defun maybe-xmms2-style ()
  "Set the style used in xmms2, requires smart tabs."
  (when (string-match "xmms2" buffer-file-name)
        (setq show-trailing-whitespace t)
        (setq indent-tabs-mode t)
        (setq tab-width 4)
        (c-set-style "xmms2")))

(add-hook 'c-mode-common-hook 'maybe-xmms2-style)

(provide 'xmms2-style)

and

(require 'xmms2-style)

in your .emacs.

Configuration for XMMS2 Source Editing

Put this at the end of your .emacs to follow style guidelines. (For correct indentation of multi-line statements, see below.)

(defun xmms2-c-mode ()   "C mode with adjusted defaults for use with the xmms2."   (interactive)   (c-mode)   (c-set-style "K&R")   (setq tab-width 4)   (setq indent-tabs-mode t)   (setq c-basic-offset 4)) (setq auto-mode-alist        (cons '("^.*/xmms2.*/.*\\.[ch]$" . xmms2-c-mode)          auto-mode-alist))

Correct Indentation and Alignment of Multi-Line Statements

Add the following to your .emacs file to use tabs for indentation and spaces for alignment. Be sure to add the hook to the C-mode you use for coding on XMMS2, and to keep the c-set-offset function.

; But special coding guidelines for different projects (defun xmms2-c-mode ()   "C mode with adjusted defaults for use with the xmms2."   (interactive)   (c-mode)   (c-set-style "K&R")   (setq tab-width 4)   (setq indent-tabs-mode t)   (setq c-basic-offset 4)   ; Align closing paren with opening paren   (c-set-offset 'arglist-close 'c-lineup-arglist-intro-after-paren)   (add-hook 'c-special-indent-hook 'smart-tab-indent-hook)) (defun get-nonempty-context ()   (let ((curr-context (car (c-guess-basic-syntax))))     (if (or (eq (car curr-context) 'arglist-intro)             (eq (car curr-context) 'arglist-cont)             (eq (car curr-context) 'arglist-cont-nonempty)             (eq (car curr-context) 'arglist-close))         curr-context       nil))) (defun smart-tab-indent-hook ()   "Fixes indentation so it pads with spaces in arglists."   (let ((nonempty-ctx (get-nonempty-context)))     (if nonempty-ctx         (let ((tabbed-columns (+ (point-at-bol)                                  (/ (c-langelem-col nonempty-ctx t)                                     tab-width)))               (orig-column (current-column)))           (tabify (point-at-bol) tabbed-columns)           (untabify tabbed-columns (point-at-eol))           ; editing tabs screws the pointer position           (move-to-column orig-column)))))

For a more language-agnostic and general way to do this, see http://www.emacswiki.org/emacs/SmartTabs.

Or you can run a [http://code.l3ib.org/?p=.users/syscrash/indent.git;a=summary Haskell script]. It only touches multi-line statements, and preserves whatever indentation you were using at the beginning of the statement.

Clone this wiki locally