【问题标题】:ESS: retrieving command history from commands entered in ESS[R] inferior mode or scriptESS:从以 ESS[R] 次等模式或脚本输入的命令检索命令历史记录
【发布时间】:2014-12-05 02:23:26
【问题描述】:

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

【问题讨论】:

    标签: r emacs ess


    【解决方案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)
    

    【讨论】:

    • 谢谢!还值得指出的是,这两种解决方案都不适用于旧版本的 ESS,即使在最新版本 (14.9) 中,这也不适用于重复调用多行命令(这可能是当前版本 ESS 中的错误) )。见stat.ethz.ch/pipermail/ess-help/2014-December/010373.html
    • 谢谢!优秀的解决方案。这并不能解决搜索先前执行的命令的问题。例如,comint-history-isearch-backward-regexp (M-r) 仅搜索 comint 历史。为什么 ess 不将评估的命令放入 comint 历史记录中?
    【解决方案2】:

    似乎没有任何内置函数可以做到这一点,所以我编写了下面的函数。本质上,此函数自动使用按键 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
    猜你喜欢
    • 1970-01-01
    • 2012-01-25
    • 2017-09-24
    • 1970-01-01
    • 2012-01-23
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多