Skip to content

Commit

Permalink
tweak(core): more robust minemacs-first-*-file-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
abougouffa committed Nov 4, 2023
1 parent 4283c1f commit 6fb7628
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions elisp/+minemacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -208,30 +208,35 @@ DEPTH and LOCAL are passed as is to `add-hook'."
"Make a hook which runs on the first FILETYPE file which with an extension
that matches EXT-REGEXP.
This will creates a function named `+first-file--FILETYPE-h' which and adds it
to `first-file-hook', this function will run on the first file that matches
This will creates a function named `+first-file--FILETYPE-h' which gets executed
before `after-find-file'. This function will run on the first file that matches
EXT-REGEXP. When it runs, this function provides a feature named
`minemacs-first-FILETYPE-file' and a run all hooks in
`minemacs-first-FILETYPE-file-hook'."
(let* ((filetype (+unquote filetype))
(fn-name (intern (format "+first-file-%s-h" (if filetype (format "-%s" filetype) ""))))
(hook-name (intern (format "minemacs-first%s-file-hook" (if filetype (format "-%s" filetype) ""))))
(feature-name (intern (format "minemacs-first%s-file" (if filetype (format "-%s" filetype) ""))))
(hook-docs (format "This hook will be run after opening the first %s file.\n\nExecuted inside `find-file-hook', it runs all hooks in `%s' and provide the `%s' feature."
filetype hook-name feature-name)))
(hook-docs (format "This hook will be run after opening the first %s file (files that matches \"%s\").\n\nExecuted before `after-find-file', it runs all hooks in `%s' and provide the `%s' feature."
filetype ext-regexp hook-name feature-name)))
`(progn
(+log! "Setting up hook `%s' -- function `%s' -- feature `%s'."
',hook-name ',fn-name ',feature-name)
(defcustom ,hook-name nil ,hook-docs :group 'minemacs-core :type 'hook)
(add-hook 'find-file-hook
(defun ,fn-name ()
(+log! "Hook `find-file-hook' invoked, checking for `%s'." ',hook-name)
(when (and buffer-file-name (string-match-p ,ext-regexp buffer-file-name))
(+log! "Running %d `%s' hooks." (length ,hook-name) ',hook-name)
(remove-hook 'find-file-hook #',fn-name)
(provide ',feature-name)
(run-hooks ',hook-name)))
-101))))
(defun ,fn-name (&rest _)
(when (and
after-init-time ; after Emacs initialization
(featurep 'minemacs-loaded) ; after MinEmacs is loaded
(buffer-file-name) ; for named files
(string-match-p ,ext-regexp (buffer-file-name))) ; file name matches the regexp
(+log! "Running %d `%s' hooks." (length ,hook-name) ',hook-name)
(advice-remove 'after-find-file #',fn-name)
(provide ',feature-name)
(run-hooks ',hook-name)))
(if (daemonp)
;; Load immediately after init when in daemon mode
(add-hook 'after-init-hook #',fn-name 90)
(advice-add 'after-find-file :before #',fn-name '((depth . -101)))))))

;; From Doom Emacs
(defun +resolve-hook-forms (hooks)
Expand Down

0 comments on commit 6fb7628

Please sign in to comment.