我正在使用本地emacs实例(aquamacs)在远程服务器上运行R进程,我想自动连接到我的服务器的过程.过程如下:
[在emacs中]
M-x shell
Run Code Online (Sandbox Code Playgroud)
[在结果控制台中]
TERM=xterm
ssh -Y -C <my remote server>
screen -rd [and/or] R
Run Code Online (Sandbox Code Playgroud)
[在emacs中]
M-x ess-remote
r
Run Code Online (Sandbox Code Playgroud)
我在这里发现了这种一般方法:http://blog.nguyenvq.com/2010/07/11/using-r-ess-remote-with-screen-in-emacs/.-Y -C选项允许您使用xterm查看绘图.我不知道lisp和我已经google了一下,我似乎无法拼凑如何实际定义一个函数来自动化这个(例如,在.emacs.el中).有没有人实现过这样的事情?
我们假设您只想调用shell代码.在Lisp中,所有内容都是括号括起来的前缀表示法.所以我们将它输入缓冲区(比如临时缓冲区):
(shell)
Run Code Online (Sandbox Code Playgroud)
将指针移动到close-paren之后的行尾,然后键入<C-x C-e>以执行Lisp代码.您应该看到该shell函数被调用.
现在,让我们把它变成一个函数,这样我们就可以添加其他东西了.创建函数的命令是defun,它接受函数的名称,参数列表(在括号中),然后是函数体:
(defun automate-connection ()
(shell))
Run Code Online (Sandbox Code Playgroud)
将光标移动到代码的末尾,点击<C-x C-e>,然后定义函数.你可以通过执行从Lisp中调用它
(automate-connection)
Run Code Online (Sandbox Code Playgroud)
好的,现在我们只需要将一些文本放入shell缓冲区.
(defun automate-connection ()
(shell)
(insert "TERM=xterm"))
Run Code Online (Sandbox Code Playgroud)
现在,当我们运行它时,我们将"TERM = xterm"放入shell缓冲区.但它实际上并没有发送命令.我们试着换一个新行.
(defun automate-connection ()
(shell)
(insert "TERM=xterm\n"))
Run Code Online (Sandbox Code Playgroud)
这会产生换行符,但实际上并不会使命令运行.为什么不?让我们看看输入键的作用.转到*shell*缓冲区,然后键入<C-h c>,然后按返回键.(<C-h c>运行describe-key-briefly,打印通过命中给定键调用的函数的名称).这就是说当你点击RET时,它不是换行,而是实际调用comint-send-input.所以我们这样做:
(defun automate-connection ()
(shell)
(insert "TERM=xterm")
(comint-send-input))
Run Code Online (Sandbox Code Playgroud)
现在,当你从任何Lisp代码运行`(自动连接)时,你应该得到给定的东西.我把它作为练习留给读者添加你的其他命令.
可是等等!我们还没有真的完成,是吗?我假设您不想移动到Lisp临时缓冲区,输入(automate-connection),然后评估该代码.你可能只想打字,并称它为一天.默认情况下,您不能使用我们刚刚创建的函数执行此操作.幸运的是,允许这样做很简单:只需(interactive)在函数中添加一个调用:
(defun automate-connection ()
(interactive)
(shell)
(insert "TERM=xterm")
(comint-send-input))
Run Code Online (Sandbox Code Playgroud)
现在你可以根据需要调用它,它将打开*shell*缓冲区,放入文本,并告诉Emacs告诉shell运行该文本.