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

I am new to emacs, but shocked by what I really can do and how much time it saves (Macros save a lot of time). But I was wondering if it is possible to create scripts based on steps, where it asks the user for input and executes based on this code. For example, maybe I want to create an SQL query so that it calls 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 interested, because something like this would be useful to have. Someone mentioned AWK scripts, but I was not sure if it was the right tree to bark or not. I am on Windows, but I do not think it matters a lot.

I definitely appreciate any information about this, thanks

+7
macros awk emacs editing
source share
6 answers

see this little hack on emacswiki: Querying while executing a keyboard macro . Otherwise, you can always pause the macro and paste the text in the place where you give Cx q during the definition, see Running 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 can put this function in your ~/.emacs and execute it with Mx: my-build-query .

Hope this gives you some pointers to get started!

PS: Ahh and another idea. Probably a simpler approach for this kind of material is to use YASnippet (look at the screencast on the page).

+7
source share

You can use read-from-minibuffer using Emacs Lisp, aka elisp.

+2
source share

e.g. 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 the command line

  c:\test> gawk -f myscript.awk 
+2
source share

That's right, I think, is to write a function like readline, which allows you to query and enter the user into the buffer.

This is one of those things that is easy enough to implement, but it is very nice to do it. There is probably a good elisp reuse code for this, but I don't know about that.

+1
source share

Here is the basic implementation 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 Mx prompt-for-sql-statement (or enter the key sequence to which you attached the command), you will 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 tab by operator type, and an empty field will terminate the list. The function will then insert the constructed SQL statement, wherever it is, when you invoke the command.

The command written in the command will generate SQL statements, all of which look like SELECT ("select ... from table", "insert ... from table", etc.). A more reasonable implementation would know how to create the correct syntax for each type of SQL query.

+1
source share

another option might be skeleton or another emacs template (maybe a pace?), perhaps in combination with abbrevs

+1
source share

All Articles