Emacs: Is there a way to create a interactive script using Emacs?

别来无恙 提交于 2019-12-05 05:37:25

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).

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

You can use read-from-minibuffer, using Emacs Lisp, aka 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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!