gpt4 book ai didi

Emacs 更快的书签跳转?

转载 作者:行者123 更新时间:2023-12-01 01:07:31 25 4
gpt4 key购买 nike

我的大部分书签都以字母为前缀
第一个字母几乎总是唯一地确定书签。
例如,我可以通过这种方式,
使用 M-x bookmark-jump RET s RET 跳转到我的源文件夹(标记为“s:源”)。
我把它放在一个快捷方式上,所以它实际上是 ~ s RET。
我想最终摆脱 RET,
即得到 M-x bookmark-quick-jump RET s 或 ~ s
做上述工作。
我还希望它回退到默认行为:向我显示所有书签
以给定的字母开头,以防只有一个变体。
到目前为止,我有:

(defun bookmark-do-quick-jump (str)
(let ((completions (all-completions str bookmark-alist)))
(bookmark-jump
(if (eq 1 (length completions))
(car completions)
(completing-read "Jump to bookmark: " bookmark-alist nil t str)))))
还有两个小问题:
首先,我需要以某种方式跳入 minibuffer 并坚持在那里这张 map (不知道如何做到这一点):
(setq bookmark-quick-jump-map
(let ((map (make-sparse-keymap)))
(mapcar (lambda (key)
(define-key map key
(lambda()
(interactive)
(bookmark-do-quick-jump key))))
(loop for c from ?a to ?z
collect (string c)))
map))
其次,当我打电话时
(bookmark-do-quick-jump "o")
它带有 3 个变体(org-capture-last-stored、org-capture-last-stored-marker...)。
我现在在 minibuffer 中,但我仍然需要按 RET RET
查看这 3 个变体。我希望这能自动完成。
我很感激任何直接回答我的两个子问题的回答,
或者完全不同的方法,只要我能得到行为和可用性
我描述的。
更新:
我通过从 completing-read 切换解决了第二件事至 ido-completing-read :
(defun bookmark-do-quick-jump (str)
(let ((completions (all-completions str bookmark-alist)))
(bookmark-jump
(if (eq 1 (length completions))
(car completions)
(ido-completing-read "Jump to bookmark: " completions nil t str)))))
顺便说一句,我忘了提到我使用 bookmark+ .我不确定是否跳到 dired
默认支持 bookmark-jump .

最佳答案

我们可以重新映射 self-insert-command在完成读取期间触发自动完成和自动接受行为。

我原来用的(or (minibuffer-complete-and-exit) (minibuffer-completion-help))乍一看效果很好,但正如评论中所指出的那样,当一个书签的名称是另一个书签的前缀时,它就不那么理想了,因为它会立即接受较短的名称,从而使较长的名称无法访问。

调用 minibuffer-completeminibuffer-completion-help然而,一起破坏了完成功能,所以我复制了 minibuffer-complete-and-exit 的相关部分。到一个新功能。使用它可以解决所有早期的问题。

(require 'bookmark)

(defvar bookmark-do-quick-jump-map (copy-keymap minibuffer-local-must-match-map)
"Keymap for `bookmark-do-quick-jump'.

`minibuffer-local-must-match-map' is used by `completing-read' when its
REQUIRE-MATCH argument is t.

In `bookmark-do-quick-jump' we bind this modified copy to use in its place.")

(define-key bookmark-do-quick-jump-map
[remap self-insert-command] 'my-self-insert-complete-and-exit)

(defun bookmark-do-quick-jump ()
"Jump to specified bookmark with auto-completion and auto-acceptance."
(interactive)
(bookmark-maybe-load-default-file)
(let ((minibuffer-local-must-match-map bookmark-do-quick-jump-map))
(bookmark-jump
(completing-read "Jump to bookmark: " bookmark-alist nil t))))

(defun my-self-insert-complete-and-exit (n)
"Insert the character, then attempt to complete the current string,
automatically exiting when only one option remains, and displaying the
completion options otherwise."
(interactive "p")
(self-insert-command n)
(my-minibuffer-complete)
(let ((my-completions (completion-all-sorted-completions)))
(if (and my-completions (eq 0 (cdr my-completions)))
(exit-minibuffer)
(minibuffer-completion-help))))

(defun my-minibuffer-complete ()
"Copied from `minibuffer-complete-and-exit'."
(interactive)
(condition-case nil
(completion--do-completion nil 'expect-exact)
(error 1)))

编辑:

我使用 ido 对此进行了另一次尝试。有点不幸的是,您没有像使用常规迷你缓冲区完成那样突出显示下一个“重要字符”(因为这是下一步输入内容的一个很好的指示),但这在其他方面似乎很好用.

(require 'bookmark)
(require 'ido)

(defvar bookmark-ido-quick-jump-map (copy-keymap minibuffer-local-map)
"Keymap for `bookmark-ido-quick-jump'.

Every time `ido-completing-read' is called it re-initializes
`ido-common-completion-map' and sets its parent to be `minibuffer-local-map'.

In `bookmark-ido-quick-jump' we provide this modified copy as a replacement
parent.")

(define-key bookmark-ido-quick-jump-map
[remap self-insert-command] 'my-self-insert-and-ido-complete)

(defun bookmark-ido-quick-jump ()
"Jump to selected bookmark, using auto-completion and auto-acceptance."
(interactive)
(bookmark-maybe-load-default-file)
(let ((minibuffer-local-map bookmark-ido-quick-jump-map)
(ido-enable-prefix t))
(bookmark-jump
(ido-completing-read "Jump to bookmark: "
(loop for b in bookmark-alist collect (car b))))))

(defun my-self-insert-and-ido-complete (n)
"Insert the character, then attempt to complete the current string,
automatically exiting when only one option remains."
(interactive "p")
(self-insert-command n)
;; ido uses buffer-local pre- and post-command hooks, so we need to
;; co-operate with those. We append our post-command function so that
;; it executes after ido has finished processing our self-insert.
(add-hook 'post-command-hook
'my-self-insert-and-ido-complete-post-command t t))

(defun my-self-insert-and-ido-complete-post-command ()
(remove-hook 'post-command-hook
'my-self-insert-and-ido-complete-post-command t)
;; Now that ido has finished its normal processing for the current
;; command, we simulate a subsequent `ido-complete' command.
(ido-tidy) ;; pre-command-hook
(ido-complete)
(ido-exhibit)) ;; post-command-hook

关于Emacs 更快的书签跳转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17496021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com