From e20f6f334a2c93c88c3740a1bb26a061c0142aa8 Mon Sep 17 00:00:00 2001 From: JenChieh Date: Sun, 21 Apr 2024 16:36:32 -0700 Subject: [PATCH] better detect read file --- mbs.el | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/mbs.el b/mbs.el index 8b4acd7..0b95e26 100644 --- a/mbs.el +++ b/mbs.el @@ -33,6 +33,19 @@ (require 's) +(defgroup mbs nil + "Minibuffer Stats." + :prefix "mbs-" + :group 'convenience + :group 'tools + :link '(url-link :tag "Repository" "https://github.com/jcs-elpa/mbs")) + +(defconst mbs-read-file-name-commands '(read-file-name) + "List of command to detect find file action.") + +(defvar mbs-reading-file-name-p nil + "Return non-nil if reading file name.") + ;; ;;; Util @@ -44,6 +57,31 @@ (contents (minibuffer-contents))) ,@body)) +;; +;;; Entry + +(defun mbs-mode--adv-around (fnc &rest args) + "Advice bind FNC and ARGS." + (let ((mbs-reading-file-name-p t)) (apply fnc args))) + +(defun mbs-mode--enable () + "Enable function `recentf-excl-mode'." + (dolist (command mbs-read-file-name-commands) + (advice-add command :around #'mbs-mode--adv-around))) + +(defun mbs-mode--disable () + "Disable function `recentf-excl-mode'." + (dolist (command mbs-read-file-name-commands) + (advice-remove command #'mbs-mode--adv-around))) + +;;;###autoload +(define-minor-mode mbs-mode + "Minor mode `mbs-mode'." + :global t + :require 'mbs-mode + :group 'mbs + (if mbs-mode (mbs-mode--enable) (mbs-mode--disable))) + ;; ;;; Core @@ -54,12 +92,16 @@ (string-prefix-p "M-x" prompt))) ;;;###autoload -(defun mbs-finding-file-p () +(defun mbs-reading-file-name-p () "Return non-nil if current minibuffer finding file." (mbs-with-minibuffer-env - (and (not (mbs-M-x-p)) - (not (string-empty-p contents)) - (ignore-errors (expand-file-name contents))))) + (or mbs-reading-file-name-p + (and (not (mbs-M-x-p)) + (not (string-empty-p contents)) + (ignore-errors (expand-file-name contents)))))) + +;;;###autoload +(defalias 'mbs-finding-file-p #'mbs-reading-file-name-p) ;;;###autoload (defun mbs-renaming-p ()