Emacs:有没有办法使用 Emacs 创建交互式脚本?
我是 emacs 的新手,但对我真正能做的事情以及它节省了多少时间感到震惊(宏节省了大量时间)。但我想知道是否可以创建基于步骤的脚本,其中它要求用户输入并基于该输入执行代码。例如,也许我想创建一个 SQL 查询,因此它会提示类似以下内容:
>table name?
myTable
>type of query (select, insert, update, delete)
select
>fields to get
name, id
>Result query is "select (name, id) from myTable"
这只是一个想法的概述,但我很好奇,因为这样的东西会有用。有人提到了 AWK 脚本,但我不确定这是否是正确的树。我使用的是 Windows,但我认为这并不重要。
我非常感谢任何有关此的信息,谢谢
I am new to emacs, but shocked at what I can really do and how much time it saves (Macros save A LOT of time). But I was wondering it was possible to create step based scripts where it asks the user for input and executes code based on that. For example maybe I want to create a SQL query so it would prompt something like:
>table name?
myTable
>type of query (select, insert, update, delete)
select
>fields to get
name, id
>Result query is "select (name, id) from myTable"
This is just an outline of an idea but I was wonder because something like this would be useful to have. Someone mentioned AWK scripts but I wasn't sure if that was the right tree to bark up or not. I am on Windows but I don't think that matters a whole lot.
I definitely appreciate any info on this, Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅 emacswiki 上的这个小技巧:在键盘宏执行期间提示。否则,您始终可以暂停宏并在定义期间给出
Cx q
的位置插入文本执行,请参阅 执行具有变体的宏。最后,您可以定义一个函数并使用 交互式 来获取所需的参数,即:您可以将此函数放入
~/.emacs
中,并使用Mx: my-build-query
执行它。希望这能为您提供一些入门指导!
PS:啊,还有一个想法。对于此类内容,可能更简单的方法是使用 YASnippet (看看上面的截屏视频)页面)。
see this little hack on emacswiki: Prompting During Keyboard Macro Execution. Otherwise you can always pause a macro and insert you text execution at the points where you give
C-x q
during definition, see Executing Macros with Variations. Finally you can define a function and use interactive to get the required parameters, i.e.:You could put this function in your
~/.emacs
and execute it withM-x: my-build-query
.Hope this gives you some pointers to get started!
P.S.: Ahh, and one more idea. The probably easier approach for this kind of stuff is to use YASnippet (have a look at the screencast on the page).
例如在 awk 中。
另存为 myscript.awk 并在命令行上
eg in awk.
save as myscript.awk and on command line
您可以使用 read-from-minibuffer,使用 Emacs Lisp,又名 elisp。
You can use read-from-minibuffer, using Emacs Lisp, aka elisp.
我认为正确的做法是编写一个类似 readline 的函数,允许在缓冲区内进行提示和用户输入。
这是很容易实现但很难以真正令人满意的方式完成的事情之一。可能有很好的可重用 elisp 代码可以做到这一点,但我不知道。
The right thing, I think, is to write a readline-like function that allows prompting and user input within the buffer.
This is one of those things that is easy enough to implement, but hard to do in a really pleasing way. There's probably good reusable elisp code out there to do this, but I don't know of it.
以下是帮助您入门的基本实现:
当您键入
Mx Prompt-for-sql-statement
(或键入已将命令绑定到的按键序列)时,您将收到一系列提示:您可以对语句类型进行制表符补全,空字段将终止列表。然后,该函数将在您调用该命令时的任何位置插入构造的 SQL 语句。
编写的命令将生成看起来像 SELECT 的 SQL 语句(“select ... from table”、“insert ... from table”等)。更智能的实现会知道如何为每种类型的 SQL 语句生成正确的语法。
Here's a basic implementation to get you started:
When you type
M-x prompt-for-sql-statement
(or type a key sequence you've bound the command to), you'll get a series of prompts:You can do tab-completion on the statement type, and an empty field will terminate the list. Then the function will insert the constructed SQL statement wherever point was when you invoked the command.
The command as written will generate SQL statements that all look like a SELECT ("select ... from table", "insert ... from table", etc). A smarter implementation would know how to produce the correct syntax for each type of SQL statement.
另一种可能性可能是 骨架 或其他 emacs 模板(也许是节奏?)可能与缩写组合
another possiblity might be a skeleton or other emacs template (maybe tempo?) possibly combined with abbrevs