【发布时间】:2014-12-05 02:23:26
【问题描述】:
在低级模式下使用 ESS[R] 时,我可以使用 C-c C-p 检索最新的命令输出,这会将光标移动到上一个命令输出。或者,我可以使用 C-up ,它基本上从下级进程复制最近输入的命令(类似于 readline 的工作方式)。我更喜欢 C-up 方法,但不幸的是,它不会检索使用任何 ess-eval 逗号从脚本输入的命令。对于在低级模式和 ess-eval 中输入的命令,是否可以获得 C-up 的功能?
【问题讨论】:
在低级模式下使用 ESS[R] 时,我可以使用 C-c C-p 检索最新的命令输出,这会将光标移动到上一个命令输出。或者,我可以使用 C-up ,它基本上从下级进程复制最近输入的命令(类似于 readline 的工作方式)。我更喜欢 C-up 方法,但不幸的是,它不会检索使用任何 ess-eval 逗号从脚本输入的命令。对于在低级模式和 ess-eval 中输入的命令,是否可以获得 C-up 的功能?
【问题讨论】:
您的解决方案适用于单行命令,但需要稍作调整才能处理多行语句:
(defun ess-readline ()
"Move to previous command entered from script *or* R-process and copy
to prompt for execution or editing"
(interactive)
;; See how many times function was called
(if (eq last-command 'ess-readline)
(setq ess-readline-count (1+ ess-readline-count))
(setq ess-readline-count 1))
;; Move to prompt and delete current input
(comint-goto-process-mark)
(end-of-buffer nil) ;; tweak here
(comint-kill-input)
;; Copy n'th command in history where n = ess-readline-count
(comint-previous-prompt ess-readline-count)
(comint-copy-old-input)
;; Below is needed to update counter for sequential calls
(setq this-command 'ess-readline)
)
(global-set-key (kbd "\C-cp") 'ess-readline)
这使您可以向上移动之前的命令;以下功能使您可以再次向下移动,因此您可以上下查找您要执行的命令:
(defun ess-readnextline ()
"Move to next command after the one currently copied to prompt and copy
to prompt for execution or editing"
(interactive)
;; Move to prompt and delete current input
(comint-goto-process-mark)
(end-of-buffer nil)
(comint-kill-input)
;; Copy (n - 1)'th command in history where n = ess-readline-count
(setq ess-readline-count (max 0 (1- ess-readline-count)))
(when (> ess-readline-count 0)
(comint-previous-prompt ess-readline-count)
(comint-copy-old-input))
;; Update counter for sequential calls
(setq this-command 'ess-readline)
)
(global-set-key (kbd "\C-cn") 'ess-readnextline)
【讨论】:
似乎没有任何内置函数可以做到这一点,所以我编写了下面的函数。本质上,此函数自动使用按键 C-c C-p 将光标移动到 R 进程缓冲区中的前一个命令,然后 C-c RET 将该命令复制到提示符以进行编辑
(defun ess-readline ()
"Move to previous command entered from script *or* R-process and copy
to prompt for execution or editing"
(interactive)
;; See how many times function was called
(if (eq last-command 'ess-readline)
(setq ess-readline-count (1+ ess-readline-count))
(setq ess-readline-count 1))
;; Move to end of buffer and delete current input
(end-of-buffer nil)
(comint-kill-input)
;; Copy n'th command in history where n = ess-readline-count
(comint-previous-prompt ess-readline-count)
(comint-copy-old-input)
;; Below is needed to update counter for sequential calls
(setq this-command 'ess-readline)
)
(global-set-key (kbd "\C-cp") 'ess-readline)
【讨论】:
(define-key comint-mode-map [C-up] 'ess-readline)将此绑定到C-up