Skip to content

Instantly share code, notes, and snippets.

@Pitometsu
Created May 3, 2014 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Pitometsu/34a5ae0b9998113e1c67 to your computer and use it in GitHub Desktop.
Save Pitometsu/34a5ae0b9998113e1c67 to your computer and use it in GitHub Desktop.
;; hack that enable major mode for newly created buffer according to `auto-mode-alist'
(defadvice set-buffer-major-mode (after new-buffer-auto-mode activate compile)
"Select major mode for newly created buffer.
Compare the `buffer-name' the entries in `auto-mode-alist'."
(with-current-buffer (ad-get-arg 0)
(if (and (buffer-name) (not buffer-file-name))
(let ((name (buffer-name)))
;; Remove backup-suffixes from file name.
(setq name (file-name-sans-versions name))
;; Do not handle service buffers
(while (and name (not (string-match "^\\*.+\\*$" name)))
;; Find first matching alist entry.
(setq mode
(if (memq system-type '(windows-nt cygwin))
;; System is case-insensitive.
(let ((case-fold-search t))
(assoc-default name auto-mode-alist
'string-match))
;; System is case-sensitive.
(or
;; First match case-sensitively.
(let ((case-fold-search nil))
(assoc-default name auto-mode-alist
'string-match))
;; Fallback to case-insensitive match.
(and auto-mode-case-fold
(let ((case-fold-search t))
(assoc-default name auto-mode-alist
'string-match))))))
(if (and mode
(consp mode)
(cadr mode))
(setq mode (car mode)
name (substring name 0 (match-beginning 0)))
(setq name nil))
(when mode
(set-auto-mode-0 mode t)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment