[英]How can I automate these emacs ESS (ess-remote) commands?
I'm using a local emacs instance (aquamacs) to run R processes on a remote server, and I'd like to automate the process of connecting to my server. 我正在使用本地emacs实例(aquamacs)在远程服务器上运行R进程,我想自动连接到我的服务器的过程。 The process is as follows:
过程如下:
[in emacs] [在emacs中]
M-x shell
[in the resulting console] [在结果控制台中]
TERM=xterm
ssh -Y -C <my remote server>
screen -rd [and/or] R
[in emacs] [在emacs中]
M-x ess-remote
r
I discovered this general approach here: http://blog.nguyenvq.com/2010/07/11/using-r-ess-remote-with-screen-in-emacs/ . 我在这里发现了这种一般方法: http : //blog.nguyenvq.com/2010/07/11/using-r-ess-remote-with-screen-in-emacs/ 。 The -Y -C options allow you use xterm to view plots.
-Y -C选项允许您使用xterm查看绘图。 I don't know lisp and tho I've googled around a bit, I can't seem to piece together how to actually define a function to automate this (eg, in .emacs.el).
我不知道lisp和我已经google了一下,我似乎无法拼凑如何实际定义一个函数来自动化这个(例如,在.emacs.el中)。 Has anyone implemented anything like this?
有没有人实现过这样的事情?
Let's assume you just want to call shell
in code. 我们假设您只想在代码中调用
shell
。 In Lisp, everything is prefix notation surrounded by parentheses. 在Lisp中,所有内容都是括号括起来的前缀表示法。 So we enter this into a buffer (say, the scratch buffer):
所以我们将它输入缓冲区(比如临时缓冲区):
(shell)
Move your pointer to the end of the line after the close-paren, and type <Cx Ce>
to execute the Lisp code. 将指针移动到close-paren之后的行尾,然后键入
<Cx Ce>
以执行Lisp代码。 You should see that the shell
function is called. 您应该看到调用了
shell
函数。
Now, let's make it a function, so we can add other things to it. 现在,让我们把它变成一个函数,这样我们就可以添加其他东西了。 The command to create a function is
defun
, and it takes the name of the function, the argument list (in parentheses), and then the body of the function: 创建函数的命令是
defun
,它接受函数的名称,参数列表(在括号中),然后是函数体:
(defun automate-connection ()
(shell))
Move your cursor to the end of the code, hit <Cx Ce>
, and the function will be defined. 将光标移动到代码的末尾,按
<Cx Ce>
,将定义该函数。 You can call it from Lisp by executing 你可以通过执行从Lisp中调用它
(automate-connection)
Ok, now we just need to put some text into the shell buffer. 好的,现在我们只需要将一些文本放入shell缓冲区。
(defun automate-connection ()
(shell)
(insert "TERM=xterm"))
Now, when we run that, we get "TERM=xterm" put into the shell buffer. 现在,当我们运行它时,我们将“TERM = xterm”放入shell缓冲区。 But it doesn't actually send the command.
但它实际上并没有发送命令。 Let's try putting a newline.
我们试着换一个新行。
(defun automate-connection ()
(shell)
(insert "TERM=xterm\n"))
That puts in a newline, but doesn't actually make the command run. 这会产生换行符,但实际上并不会使命令运行。 Why not?
为什么不? Let's see what the enter key does.
让我们看看输入键的作用。 Go to your
*shell*
buffer, and type <Ch c>
, then hit the return key. 转到
*shell*
缓冲区,然后键入<Ch c>
,然后单击返回键。 ( <Ch c>
runs describe-key-briefly
, which prints the name of the function invoked by hitting the given key). (
<Ch c>
运行describe-key-briefly
,它打印通过命中给定键调用的函数的名称)。 That says that when you hit RET, it's not putting a newline, but actually calling comint-send-input
. 这说明当你点击RET时,它不是换行,而是实际调用
comint-send-input
。 So let's do that: 所以我们这样做:
(defun automate-connection ()
(shell)
(insert "TERM=xterm")
(comint-send-input))
Now, when you run `(automate-connection) from any Lisp code, you should get the given thing sent. 现在,当你从任何Lisp代码运行`(自动连接)时,你应该得到给定的东西发送。 I leave it as an exercise to the reader to add your other commands.
我把它作为练习留给读者添加你的其他命令。
But wait! 可是等等! We're not really done, are we?
我们还没有真的完成,是吗? I assume you don't want to have to move to a Lisp scratch buffer, type in
(automate-connection)
, then evaluate that code. 我假设您不想移动到Lisp临时缓冲区,键入
(automate-connection)
,然后评估该代码。 You probably just want to type , and call it a day. 你可能只想打字,并称它为一天。 You can't do that by default with the function we just created.
默认情况下,您不能使用我们刚刚创建的函数执行此操作。 Luckily, it's simple to allow that: just add a call to
(interactive)
in your function: 幸运的是,允许这样做很简单:只需在函数中添加对
(interactive)
的调用:
(defun automate-connection ()
(interactive)
(shell)
(insert "TERM=xterm")
(comint-send-input))
Now you can call it as you want, and it'll open the *shell*
buffer, put in the text, and tell Emacs to tell the shell to run that text. 现在你可以根据需要调用它,它将打开
*shell*
缓冲区,放入文本,并告诉Emacs告诉shell运行该文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.