Add gist for executing code for certain files with find-file

This commit is contained in:
Bram Schoenmakers 2023-07-04 09:47:52 +02:00
parent ee5e3b6563
commit ad450efe20
2 changed files with 23 additions and 0 deletions

View File

@ -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

View File

@ -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)