1

我想让我在 eshell 中的向上箭头键是 eshell-previous-matching-input-from-input,因为它是,当点位于点最大时,否则是上一行。我写过

    (defun my-up-arrow-in-eshell() (交互式)
      (如果(=(点)(点最大))
          (eshell-previous-matching-input-from-input)
      ; 别的
        (上一行)
      )
    )

    (add-hook 'eshell-mode-hook
      (拉姆达 ()
        (define-key eshell-mode-map (kbd "<up>") 'my-up-arrow-in-eshell)))

但这不对,因为 eshell-previous-matching-input-from-input 需要一个参数。我可以将其硬编码为 0,但这适用于单次按下向上箭头键(在最大点时)。我希望它在最高点时像开箱即用一样工作。我给论点什么?

4

2 回答 2

2

eshell-previous-matching-input-from-input以依赖于last-command正确浏览输入历史的方式实现。绑定up到然后调用的新函数eshell-previous-matching-input-from-input因此在当前实现中无法按预期工作。

如果您不想完全重新实现eshell-previous-matching-input-from-input,您还可以建议现有功能如下:

(advice-add 'eshell-previous-matching-input-from-input
        :before-until
        (lambda (&rest r)
          (when (and (eq this-command 'eshell-previous-matching-input-from-input)
             (/= (point) (point-max)))
        (previous-line) t)))
于 2019-11-26T23:37:40.083 回答
1

您可以使用(call-interactively #'eshell-previous-matching-input-from-input)根据其interactive形式解释参数,例如。

(defun my-up-arrow-in-eshell ()
  (interactive) 
  (if (/= (point) (point-max))
      (previous-line)
    (setq this-command 'eshell-previous-matching-input-from-input)
    (call-interactively #'eshell-previous-matching-input-from-input)))

或者,您可以添加自己的参数并将其传递,例如。

(defun my-up-arrow-in-eshell (arg)
  (interactive "p") 
  (if (= (point) (point-max)) 
      (progn
        (setq this-command 'eshell-previous-matching-input-from-input)
        (eshell-previous-matching-input-from-input arg))
    (previous-line arg)))

最后一个选项可以是条件绑定(参见 (elisp)Extended Menu Items),eshell-previous-matching-input-from-input当点位于point-max

(define-key eshell-hist-mode-map (kbd "<up>")
  '(menu-item "maybe-hist"
              nil
              :filter
              (lambda (&optional _)
                (when (= (point) (point-max))
                  'eshell-previous-matching-input-from-input))))
于 2019-11-26T22:34:55.933 回答