ESS:从ESS [R]下级模式或脚本中输入的命令中检索命令历史

时间:2014-12-05 02:23:26

标签: r emacs ess

在劣质模式下使用ESS [R]时,我可以使用C-c C-p检索最新的命令输出,将光标移动到上一个命令输出。或者,我可以使用C-up,它基本上复制了来自下级进程的最近输入的命令(类似于readline的工作原理)。我更喜欢C-up方法,但遗憾的是它不会检索使用任何ess-eval commads从脚本输入的命令。对于在劣质模式和ess-eval中输入的命令,是否可以获得C-up的功能?

2 个答案:

答案 0 :(得分:1)

似乎没有任何内置函数可以执行此操作,因此我编写了下面的函数。本质上,此函数自动执行使用键盘C-c C-p将光标移动到R-process缓冲区中的上一个命令,然后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)

答案 1 :(得分:1)

您的解决方案适用于单行命令,但需要一些小的调整来处理多行语句:

(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)