Emacs:有没有办法使用 Emacs 创建交互式脚本?

发布于 2024-08-21 02:15:19 字数 427 浏览 11 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烂柯人 2024-08-28 02:15:19

请参阅 emacswiki 上的这个小技巧:在键盘宏执行期间提示。否则,您始终可以暂停宏并在定义期间给出 Cx q 的位置插入文本执行,请参阅 执行具有变体的宏。最后,您可以定义一个函数并使用 交互式 来获取所需的参数,即:

(defun my-build-query (table type field)
  (interactive "sTable name: \nsType of query: \nsFields to get: ")
  (message "%s (%s) from %s" type fields table)
)

您可以将此函数放入 ~/.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.:

(defun my-build-query (table type field)
  (interactive "sTable name: \nsType of query: \nsFields to get: ")
  (message "%s (%s) from %s" type fields table)
)

You could put this function in your ~/.emacs and execute it with M-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).

音盲 2024-08-28 02:15:19

例如在 awk 中。

BEGIN{
while (1){
    printf "Enter table name: "
    getline tablename
    printf "Enter type of query: (s)elect, (i)nsert, (u)pdate, (d)elete, (q)uit: "
    getline querytype
    if ( querytype ~ /^q|Q$/) { exit}
    printf "Enter fields to get (field1,..): "
    getline fields
    sql=querytype" ("fields") from " tablename
    print "Result query is " sql
    printf "Do you want to execute query??: (yY)es, (nN)o"
    getline choice
    if ( choice ~ /^y|Y$/) {
    # use sql cmd here
    }
 }
}

另存为 myscript.awk 并在命令行上

 c:\test> gawk -f myscript.awk

eg in awk.

BEGIN{
while (1){
    printf "Enter table name: "
    getline tablename
    printf "Enter type of query: (s)elect, (i)nsert, (u)pdate, (d)elete, (q)uit: "
    getline querytype
    if ( querytype ~ /^q|Q$/) { exit}
    printf "Enter fields to get (field1,..): "
    getline fields
    sql=querytype" ("fields") from " tablename
    print "Result query is " sql
    printf "Do you want to execute query??: (yY)es, (nN)o"
    getline choice
    if ( choice ~ /^y|Y$/) {
    # use sql cmd here
    }
 }
}

save as myscript.awk and on command line

 c:\test> gawk -f myscript.awk
和影子一齐双人舞 2024-08-28 02:15:19

我认为正确的做法是编写一个类似 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.

北渚 2024-08-28 02:15:19

以下是帮助您入门的基本实现:

(defun prompt-for-sql-statement (table type fields)
  (interactive
   (list
    (read-from-minibuffer "Table name? ")
    (completing-read "Type of statement? " '("select" "insert" "update" "delete"))
    (let (field fields (index 1))
      (while (not (string= "" (setq field (read-from-minibuffer (format "Field #%d: " index)))))
        (setq fields (cons field fields) index (1+ index)))
      (mapconcat 'identity (nreverse fields) ", "))))
  (insert type " (" fields ") from " table))

当您键入 Mx Prompt-for-sql-statement(或键入已将命令绑定到的按键序列)时,您将收到一系列提示:

Table name? myTable
Type of statement? select
Field #1: foo
Field #2: bar
Field #3: baz
Field #4:

您可以对语句类型进行制表符补全,空字段将终止列表。然后,该函数将在您调用该命令时的任何位置插入构造的 SQL 语句。

编写的命令将生成看起来像 SELECT 的 SQL 语句(“select ... from table”、“insert ... from table”等)。更智能的实现会知道如何为每种类型的 SQL 语句生成正确的语法。

Here's a basic implementation to get you started:

(defun prompt-for-sql-statement (table type fields)
  (interactive
   (list
    (read-from-minibuffer "Table name? ")
    (completing-read "Type of statement? " '("select" "insert" "update" "delete"))
    (let (field fields (index 1))
      (while (not (string= "" (setq field (read-from-minibuffer (format "Field #%d: " index)))))
        (setq fields (cons field fields) index (1+ index)))
      (mapconcat 'identity (nreverse fields) ", "))))
  (insert type " (" fields ") from " table))

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:

Table name? myTable
Type of statement? select
Field #1: foo
Field #2: bar
Field #3: baz
Field #4:

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.

乖乖哒 2024-08-28 02:15:19

另一种可能性可能是 骨架 或其他 emacs 模板(也许是节奏?)可能与缩写组合

another possiblity might be a skeleton or other emacs template (maybe tempo?) possibly combined with abbrevs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文