Skip to content

Instantly share code, notes, and snippets.

@artagnon
Created July 22, 2010 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artagnon/486320 to your computer and use it in GitHub Desktop.
Save artagnon/486320 to your computer and use it in GitHub Desktop.
Path-specific style switching for Emacs
((defmacro define-new-c-style (name derived-from style-alists tabs-p path-list)
`(progn
(add-hook 'c-mode-common-hook
(lambda ()
(c-add-style ,name
'(,derived-from (c-offsets-alist
,style-alists)))))
(add-hook 'c-mode-hook
(lambda ()
(let ((filename (buffer-file-name)))
(when (and filename
(delq nil
(mapcar (lambda (path)
(string-match (expand-file-name path)
filename))
',path-list)))
(setq indent-tabs-mode ,tabs-p)
(c-set-style ,name)))))))
(defun c-lineup-arglist-tabs-only (ignored)
"Line up argument lists with tabs, not spaces"
(let* ((anchor (c-langelem-pos c-syntactic-element))
(column (c-langelem-2nd-pos c-syntactic-element))
(offset (- (1+ column) anchor))
(steps (floor offset c-basic-offset)))
(* (max steps 1)
c-basic-offset)))
;; Syntax for define-new-c-style:
;; <style name> <derived from> <style alist> <tabs-p> <list of paths to apply to>
(define-new-c-style "linux-tabs-only" "linux" (arglist-cont-nonempty
c-lineup-gcc-asm-reg
c-lineup-arglist-tabs-only) t
("~/src/linux" "~/src/git"))
(define-new-c-style "subversion" "gnu" (inextern-lang 0) nil ("~/svn"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment