diff --git a/gists.org b/gists.org index 7ec35ee..b145e64 100644 --- a/gists.org +++ b/gists.org @@ -623,6 +623,21 @@ An improved version was [[https://github.com/xenodium/dwim-shell-command/commit/ "\n") #+end_src +* Run code when opening a file with a certain name :emacs: + +Based on an answer at the [[https://emacs.stackexchange.com/a/77480/34645][Emacs StackExchange]], to have some code executed for files that have no specific extension or mode set. For example, to have =yaml-mode= enabled when opening a /.clang-format/ file: + +#+begin_src elisp :tangle gists/find-file-hook-for-certain-filenames.el + (setq my/setup-functions-alist '((".clang-format" . yaml-mode))) + + (defun my/file-setup () + (when-let* ((filename (file-name-nondirectory (buffer-file-name))) + (f (map-elt my/setup-functions-alist filename))) + (funcall f))) + + (add-hook 'find-file-hook #'my/file-setup) +#+end_src + * Meta ** License diff --git a/gists/find-file-hook-for-certain-filenames.el b/gists/find-file-hook-for-certain-filenames.el new file mode 100644 index 0000000..9301318 --- /dev/null +++ b/gists/find-file-hook-for-certain-filenames.el @@ -0,0 +1,8 @@ +(setq my/setup-functions-alist '((".clang-format" . yaml-mode))) + +(defun my/file-setup () + (when-let* ((filename (file-name-nondirectory (buffer-file-name))) + (f (map-elt my/setup-functions-alist filename))) + (funcall f))) + +(add-hook 'find-file-hook #'my/file-setup)