xahlee / xah-fly-keys

the most efficient keybinding for emacs
http://xahlee.info/emacs/misc/xah-fly-keys.html
469 stars 80 forks source link

Some comments on minibuffer setup and exit hook logic #174

Open akashpal-21 opened 2 weeks ago

akashpal-21 commented 2 weeks ago

Currently the following logic is implemented in xah-fly-keys mode

;; Construction:
(add-hook 'minibuffer-setup-hook 'xah-fly-insert-mode-activate)
(add-hook 'minibuffer-exit-hook 'xah-fly-command-mode-activate)

;; ;; Teardown:
(remove-hook 'minibuffer-setup-hook 'xah-fly-insert-mode-activate)
(remove-hook 'minibuffer-exit-hook 'xah-fly-command-mode-activate)

Problem description:

  1. If minibuffer is called when insert-mode is active - it must not revert to command mode when minibuffer exits

  2. Some workflow such as when using consult-completions-in-region as the completion-in-region-function requires calling another minibuffer session to handle when completions is called. This causes the command-mode to be activated prematurely after the completions has completed -- instead when the minibuffer has actually exited.

Possible solution

Define a logic to determine when minibuffer session has actually exited using a post-command-hook when switchover from command to insert is required -- this adds some extra overheads.

(defun xah-fly--minibuffer-setup ()
  "Activate `xah-fly-insert-mode` in the minibuffer."
  (unless xah-fly-insert-state-p
    (xah-fly-insert-mode-activate)
    (add-hook 'post-command-hook #'xah-fly--check-minibuffer-exit)))

(defun xah-fly--check-minibuffer-exit ()
  "Check if the minibuffer has been exited and activate command mode."
  (unless (active-minibuffer-window)
    (xah-fly-command-mode-activate)
    (remove-hook 'post-command-hook #'xah-fly--check-minibuffer-exit)))
;; Construction:
(add-hook 'minibuffer-setup-hook #'xah-fly--minibuffer-setup)

;; Teardown:
(remove-hook 'minibuffer-setup-hook #'xah-fly--minibuffer-setup)
(remove-hook 'post-command-hook #'xah-fly--check-minibuffer-exit)

Please let me know if some other trivial solution exists.

Best,

akashpal-21 commented 2 weeks ago

I wish to clarify one point,

Some workflow such as when using consult-completions-in-region as the completion-in-region-function requires calling another minibuffer session to handle when completions is called. This causes the command-mode to be activated prematurely after the completions has completed -- instead when the minibuffer has actually exited.

This is relevant only when using eval-expression M-: (default overwritten by xah-fly-keys as <space> , r) - we can use autocompletions (minibuffer on top of another minibuffer) in consult-completions-in-region here.

But also as implied - this type of autocompletions constantly reverts xah-fly-keys to command-mode due to the minibuffer-exit-hook even when called when insert-mode is currently active (during normal data entry operations).

Sorry if I were not too clear.