How to watch minibuffer input?

hello everyone

i want to write a function to watch minibuffer input which is similar like company.

here is the details:

when i type four chars in minibuffer, then run a function named “test” automatically.

i’m a emacs beginner and i hope get help for this.

thank you all!

I’m not entirely sure what you mean, but I wonder if you’re looking for a minibuffer completion framework like vertico?

sorry, it’s not about completion in minibuffer.

actually my need is:

minibuffer runs a custom function which is named “test or whatever” automatically after i type four chars in it.

I’m not sure if there is a more targeted hook, but post-command hook will probably work, i.e.

(defun run-test-if-four-chars ()
  (when (and (minibufferp)
             (= (length (minibuffer-contents)) 4))
    (test)))

(defun test ()
  (message "Foo"))

(add-hook 'post-command-hook 'run-test-if-four-chars)


1 Like

thank you so much firstly.

i’ll have a try at once.

you are right and the code works well using emacs -Q

one more thing, i add this

(defun run-test-if-four-chars ()
  (when (and (minibufferp)
             (= (length (minibuffer-contents)) 4))
    (expand-abbrev)
    (read-only-mode 1)
    (exit-minibuffer)))

now how to limit the four chars only inside abbrev file to activate the function?

thanks for BigEatie’s great help firstly.
i use these codes to get it work:

(defun run-test-if-four-chars ()
  (when (and (minibufferp)
             (= (length (minibuffer-contents)) 4))
    (expand-abbrev)
    (read-only-mode 1)
    (exit-minibuffer)))

(defun custom-execute-extended-command ()
  (interactive)
  (add-hook 'post-command-hook 'run-test-if-four-chars)
  (execute-extended-command nil)
  (remove-hook 'post-command-hook 'run-test-if-four-chars)

(global-set-key (kbd "C-c s c") 'custom-execute-extended-command)

and is there a better way to do this?