new .emacs snippet

for the non lisp hackers – this sets some c mode options depending on the name of the path to the source file.


;; run this for mysql source
(defun mysql-c-mode-common-hook () (setq indent-tabs-mode nil))

;; linux kernel style
(defun linux-c-mode-common-hook () linux-c-mode)

(setq my-c-mode-common-hook '(lambda ()
(turn-on-font-lock)
(setq comment-column 48)
)
)

;; predicates to check
(defvar my-style-selective-mode-hook nil)

(add-hook 'my-style-selective-mode-hook
'((string-match "MySQL" (buffer-file-name)) . mysql-c-mode-common-hook)
)

(add-hook 'my-style-selective-mode-hook
'((string-match "linux" (buffer-file-name)) . linux-c-mode-common-hook)
)

;; default hook
(add-hook 'my-style-selective-mode-hook
'(t . my-c-mode-common-hook) t)

;; find which hook to run depending on predicate
(defun my-style-selective-mode-hook-function ()
"Run each PREDICATE in `my-style-selective-mode-hook' to see if the
HOOK in the pair should be executed. If the PREDICATE evaluate to non
nil HOOK is executed and the rest of the hooks are ignored."
(let ((h my-style-selective-mode-hook))
(while (not (eval (caar h)))
(setq h (cdr h)))
(funcall (cdar h))))

;; Add the selective hook to the c-mode-common-hook
(add-hook 'c-mode-common-hook 'my-style-selective-mode-hook-function)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.