Simple vs complex user entry functions in Lisp - lisp

At this site: http://www.gigamonkeys.com/book/practical-a-simple-database.html there is user entry function listed as follows:
(defun prompt-read (prompt)
(format *query-io* "~%~a: " prompt)
(force-output *query-io*)
(read-line *query-io*))
Are there any major advantages of above function as compared to following simpler form:
(defun prompt-read2 (prompt)
(format t "~%~a: " prompt)
(setf answer (read-line)))
Is it recommended to always use force-output and *query-io* all the time?

Setting the answer to a global variable like that is bad. You should just return the answer and let the caller do what it wants with it. If you do use special (~global) variables, you should put asterisks around the name (*ANSWER* instead of ANSWER).
FORCE-OUTPUT is needed to ensure that the user actually sees the prompt before having to answer. If I run the second version using SBCL in a terminal, the program just freezes to wait for input without saying anything.
*QUERY-IO* should be used for querying things from the user, because some environment might want to handle that differently from other output. For example, someone might write a GUI wrapper for your program that turns the queries into graphical dialogs. Or maybe they want to run it as a part of a script, providing the input from a string.
(defun prompt-read (prompt)
(format *query-io* "~%~a: " prompt)
(force-output *query-io*)
(read-line *query-io*))
(defun hello ()
(format t "~&Hello ~a!~%" (prompt-read "What's your name")))
(defmacro with-input ((input) &body body)
`(let ((*query-io* (make-two-way-stream (make-string-input-stream ,input)
(make-string-output-stream))))
,#body))
(defun test ()
(with-input ("jkiiski")
(hello))
(with-input ("rnso")
(hello)))
(test)
; Hello jkiiski!
; Hello rnso!
Edit
A more complex example using SBCLs gray streams.
(defclass foo-stream (sb-gray:fundamental-character-input-stream)
((output-input-script :initarg :script :accessor foo-stream-script)
(output-stream :initarg :out :accessor foo-stream-out)
(current-input :initform nil :accessor foo-stream-current-input)))
(defmethod sb-gray:stream-read-char ((stream foo-stream))
(with-accessors ((input foo-stream-current-input)
(out foo-stream-out)
(script foo-stream-script)) stream
(when (or (null input)
(not (listen input)))
(let ((output (string-trim '(#\space #\newline)
(get-output-stream-string out))))
(setf input (make-string-input-stream
(format nil "~a~%"
(cdr (assoc output script :test #'string=)))))))
(read-char input)))
(defun prompt-read (prompt)
(format *query-io* "~%~a: " prompt)
(force-output *query-io*)
(read-line *query-io*))
(defun hello ()
(format t "~&Hello ~a!~%" (prompt-read "What's your name"))
(format t "~&I'm ~a too!" (prompt-read "How are you"))
(format t "~&~a~%" (if (string-equal (prompt-read
"Do you want to delete all your files")
"yes")
"Deleting all files... (not really)"
"Not deleting anything.")))
(defmacro with-input-script ((script) &body body)
(let ((out-sym (gensym "out")))
`(let* ((,out-sym (make-string-output-stream))
(*query-io* (make-two-way-stream
(make-instance 'foo-stream
:out ,out-sym
:script ,script)
,out-sym)))
,#body)))
(defun test ()
(with-input-script ('(("What's your name:" . "jkiiski")
("How are you:" . "great")
("Do you want to delete all your files:" . "No")))
(hello))
(with-input-script ('(("What's your name:" . "Foo Bar")
("How are you:" . "fine")
("Do you want to delete all your files:" . "Yes")))
(hello)))
(test)
; Hello jkiiski!
; I'm great too!
; Not deleting anything.
; Hello Foo Bar!
; I'm fine too!
; Deleting all files... (not really)

Yes you code is easy but the first is more clarifying what are you doing:
*query-io* is a global variable (which you can tell because of the * naming convention for global variables) that contains the input stream
connected to the terminal. The return value of prompt-read will be the
value of the last form, the call to READ-LINE, which returns the
string it read (without the trailing newline.)
This is what they said about *query-io*
And about the streams that you can put there works as follow:
most other I/O functions also accept T and NIL as stream designators
but with a different meaning: as a stream designator, T designates the
bidirectional stream *TERMINAL-IO*, while NIL designates
*STANDARD-OUTPUT* as an output stream and *STANDARD-INPUT* as an input stream
in this case it seems that this is only pointing to *standard-input* and not to the bidirectional stream t

Related

How to provide completion of interactive function from a request?

I would like to provide completion for an emacs interactive function whose content would be based on a HTTP request's content.
Here, the TODO explains it all.
(defun sensei-record-flow (flow-type)
"Interactive function to record change in flow."
(interactive (list (completing-read
"Flow: "
;; TODO need to get the completion from a variable filled with user's
;; flow types
'(("Foo" Foo) ("Bar" Bar) ("Baz" Baz))
nil t)))
(let ((directory (projectile-project-root)))
(setq sensei-cur-directory directory)
(sensei-send-event-flow directory flow-type))
)
How can I do that? request.el provides a :completion key to block until completion of a request, but it's not clear to me how to do that? I think what I need is to make the interactive function a continuation of the call to senseiilist-flows but I don't know how to do that in elisp.
EDIT: Here is the code of sensei-list-flows based on request.el
(defun sensei-list-flows (on-success)
"List available flow types for the current user.
ON-SUCCESS is a function that's called upon successful completion of the call
and is passed a list of symbols listing user-defined flow names."
(let* ((config (sensei-read-config))
(auth-token (cdr (assoc 'authToken config)))
(username (cdr (assoc 'configUser config)))
(server-uri (cdr (assoc 'serverUri config))))
(request (concat server-uri "api/users/" username)
:headers `(("Content-Type" . "application/json")
("X-API-Version" . "0.38.0")
("Authorization" . ,(concat "Bearer " auth-token)))
:parser 'json-read
:error (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
(message "Got error: %S" error-thrown)))
:success (cl-function (lambda (&key data &allow-other-keys)
(funcall on-success
(map 'list 'car (cdr (assoc 'userFlowTypes data)))))))))
I don't know anything about request.el and you haven't included any call to a list-flows in your quoted code and so that's impossible to comment on; but OOTB you would use C-hf url-retrieve-synchronously to fetch something via http like that.
In the resulting buffer, the url-http-end-of-headers variable is of particular note (+1 to get to the start of the content); and you may wish to check the url-http-* vars in that buffer in general.
I was able to achieve what I want using url-insert-file-contents which is similar to url-retrieve-synchronously:
(defun sensei-list-flows ()
"List available flow types for the current user."
(let* ((config (sensei-read-config))
(auth-token (cdr (assoc 'authToken config)))
(username (cdr (assoc 'configUser config)))
(server-uri (cdr (assoc 'serverUri config)))
(url-request-extra-headers `(("Content-Type" . "application/json")
("X-API-Version" . "0.38.0")
("Authorization" . ,(concat "Bearer " auth-token)))))
(with-temp-buffer
(url-insert-file-contents (concat server-uri "api/users/" username))
(let ((flows (cdr (assoc 'userFlowTypes (json-parse-buffer :object-type 'alist)))))
(map 'list 'car flows)))))
Then interactive completion is trivial:
(defun sensei-record-flow (flow-type)
"Interactive function to record change in flow."
(interactive (list (completing-read
"Flow: "
(sensei-list-flows)
nil t)))
(let ((directory (projectile-project-root)))
(setq sensei-cur-directory directory)
(sensei-send-event-flow directory flow-type))
)

Lisp basic print function getting user input

I am supposed to write a program that gets simple user input as a string and the code supposed to writes back a response (name, are you a person etc.) The program suppose to terminate when word 'bye' is typed. The code is below:
(defun x()
(setq words "empty")
(loop while (string/= words "BYE")
(setq words (read-delimited-list #\~)
(write-line words)
(format t "ROBBIE%: Hello, who are you?")
(case (string-include "I'm" words)
(format t "ROBBIE%: Nice to see you, how are you?")
((string-include "Hi" words)
(format t "ROBBIE%: How are you?")
(or (string-include "fine" words) (string-include "person" words))
(format t "ROBBIE%: No I'm a computer")))
(format t "BYE"))
(x)
However, when I compile this on program 2 errors pop up:
Line2:3 warning: undefined variable: COMMON-LISP-USER:: WORDS
Line3:3 error: during macroexpansion of (LOOP WHILE (STRING/= WORDS "BYE") ...). Use BREAK-ON-SIGNALS to intercept.
I've done programming in python but this is extremely complicated lang for me and I need some help understanding why this isn't working? Any advice is greatly appreciated!
When you do this:
(defun x ()
(setf words "foo"))
then words is not defined. It references some global variable, and if that doesn't exist, it will create it, but possibly with some strange behaviour regarding scope and extent. This is not portable code.
In order to introduce a local variable, use let:
(defun x ()
(let ((words "foo"))
;; words is is scope here
)
;; but not here
)
Loop (in the more usual »extended« form) uses loop keywords for all its clauses. There is no implicit body. In order to do something, you might use do, which allows multiple forms to follow:
(defun x ()
(let ((words "foo"))
(loop while (string/= words "bye")
do (setf words (read-line …))
(format …))))
Case uses compile-time values to compare using eql:
(case something
(:foo (do-a-foo))
((:bar :baz) (do-a-bell))
(t (moooh)))
This doesn't work with strings, because strings are not eql unless they are the same object (i. e. they are eq). In your case, you want a cond:
(cond ((string-include-p words "Whatever")
…)
((string-include-p words "yo man")
…))
For interaction with the user, you'd maybe want to use the bidirectional *query-io* stream:
(format *query-io* "Who am I?")
and
(read-line *query-io*)
Read-line gives you strings, and seems much better suited to your task than read-delimited-list, which has other use cases.
Let me focus on aspects of your code not already covered by other solutions.
Loop
Here is your loop structure:
(let ((words "empty"))
(loop
while (string/= words "BYE")
do
(progn
(setq words (read-line)))))
First of all, after do you don't need (progn ...). You could write equally:
(let ((words "empty"))
(loop
while (string/= words "BYE")
do (setq words (read-line))))
Having to initialize words to some arbitrary value (called sometime a sentinel value) is a code smell (not always a bad thing, but there might be better alternatives). Here you can simplify the loop by using a for clause.
(loop
for words = (read-line)
while (string/= words "BYE")
do ...)
Also, you may want to use until with a string= test, this might be more readable:
(loop
for words = (read-line)
until (string= words "BYE")
do ...)
Search
You can test for string inclusion with SEARCH. Here is a little commented snippet of code to showcase how string manipulation function could work:
(defun test-I-am (input)
(let ((start (search "I'am" input)))
(when start
;; we found an occurrence at position start
;; let's find the next space character
(let ((space (position #\space input :start start)))
(when space
;; we found a space character, the name starts just after
(format nil "Hello ~a!" (subseq input (1+ space))))))))
With this simple algorithm, here is a test (e.g. in your REPL):
> (test-i-am "I'am tired")
"Hello tired!"
Replace read-delimited-list with read-line, case with cond and balance some parentheses. Here is working solution, including some function for string-inclusion:
(defun string-include (string1 string2)
(let* ((string1 (string string1)) (length1 (length string1)))
(if (zerop length1)
nil
(labels ((sub (s)
(cond
((> length1 (length s)) nil)
((string= string1 s :end2 (length string1)) string1)
(t (sub (subseq s 1))))))
(sub (string string2))))))
(defun x ()
(let ((words "empty"))
(format t "ROBBIE%: Hello, who are you?~%")
(loop while (string/= words "BYE") do
(progn
(finish-output)
(setq words (read-line))
(cond ((string-include "I'm" words)
(format t "ROBBIE%: Nice to see you, how are you?~%"))
((string-include "Hi" words)
(format t "ROBBIE%: How are you?~%"))
((or (string-include "fine" words)
(string-include "person" words))
(format t "ROBBIE%: No I'm a computer~%")))))
(format t "BYE")))
Then you just call it:
(x)

Is there a way to not pass an argument in Common-Lisp instead of passing "NIL"?

I'm calling functions according to user input, but some have two parameters and others just one. Instead of using &optional parameter on every function (and never using it), is there a way to simply not pass an argument when it's value is "NIL"?
This is for an interactive fiction game, in which the user type some commands and these are converted into function calls.
(defun inputs (state)
(format *query-io* "> ")
(force-output *query-io*)
(let* ((entry (cl-ppcre:split "\\s+" (string-downcase (read-line *query-io*))))
(function (car entry))
(args (cdr entry)))
(if (valid-call function)
(funcall (symbol-function (read-from-string function))
state
args)
(progn
(format *query-io* "Sorry, I don't know the command '~a'~%~%" function)
(inputs state)))))
If the user input is "equip sword", I need to call the function "equip" passing the '("Sword") as argument, but if the user input is "status", I need to call the function "status" without passing the 'args', instead of passing them as "NIL"
I think you want to use apply
instead of funcall,
find-symbol instead of
read-from-string (this is actually important for security reasons!)
and destructuring-bind
instead of let*:
(defun inputs (state)
(format *query-io* "> ")
(force-output *query-io*)
(destructuring-bind (command &rest args)
(cl-ppcre:split "\\s+" (string-downcase (read-line *query-io*)))
(if (valid-call command)
(apply (find-symbol command) state args)
(progn
(format *query-io* "Sorry, I don't know the command '~a'~%~%" command)
(inputs state)))))
Using apply lets your commands accept an arbitrary number of arguments instead of one.
In fact, your valid-call should probably return the function to be called:
(let ((f (valid-call function)))
(if f
(apply f state args)
...)
You can also use a simple LOOP instead of a recursive call:
(defun inputs (state)
(loop
(format *query-io* "> ")
(force-output *query-io*)
(let* ((entry (cl-ppcre:split "\\s+" (string-downcase (read-line *query-io*))))
(function (car entry))
(args (cdr entry)))
(when (valid-call function)
(apply (symbol-function (find-symbol function))
state
args)
(return))
(format *query-io* "Sorry, I don't know the command '~a'~%~%" function))))

Asking emacs for default directory path "once"

I want to have a variable that keeps the default directory a user enters and keep using it throughout the run of emacs.
Basically, when the user executes a custom command, the prompt will ask for a default directory path to execute the command (only once) and whenever the user calls the same command emacs uses the same path onward.
How can I program that snippet of code in lisp?
I basically want this code in the igrep library to accept the input from user once and not ask again:
(defvar default-files-string-new "*.[sch]")
(defun igrep-read-files (&optional prompt-prefix)
"Read and return a file name pattern from the minibuffer.
If `current-prefix-arg' is '(16) or '(64), read multiple file name
patterns and return them in a list. Optional PROMPT-PREFIX is
prepended to the \"File(s): \" prompt."
(let* ((default-files (igrep-default-files))
(default-files-string (mapconcat 'identity default-files " "))
(insert-default-directory igrep-insert-default-directory)
(file (igrep-read-file-name
(igrep-prefix prompt-prefix
(if default-files
(format "File(s) [default: %s]: "
default-files-string)
"File(s): "))
nil (if default-files default-files-string "") nil nil
'igrep-files-history))
(files (list file)))
(if (or igrep-read-multiple-files
(and (consp current-prefix-arg)
(memq (prefix-numeric-value current-prefix-arg)
'(16 64))))
(let* ((key (igrep-default-key 'exit-minibuffer
minibuffer-local-completion-map
"\r"))
(prompt
(igrep-prefix prompt-prefix
(if igrep-verbose-prompts
(format "File(s): [Type `%s' when done] "
(key-description key))
"File(s): "))))
(while (and (setq file
(igrep-read-file-name prompt
nil "" nil nil
'igrep-files-history))
(not (equal file "")))
(setq files (cons file files)))))
(mapcar (lambda (file)
(if (file-directory-p file)
;; really should map expand-file-name over default-files:
(expand-file-name (if default-files default-files-string-new "*")
file)
file))
(nreverse files))))
You could use advices to do that:
(defvar wd-alist nil)
(mapc
(lambda (function)
(eval
`(defadvice ,function (around ,(intern (format "%s-wd" function)) activate)
(let ((wd (cdr (assoc ',function wd-alist))))
(unless wd
(setq wd (read-file-name "Default directory: "))
(push (cons ',function wd) wd-alist))
(let ((default-directory wd))
ad-do-it)))))
'(grep-find))
The variable wd-list stores the association (FUNCTION . PATH). The list mapc iterate over are the advised functions. Now, when calling find-grep, it asks for the working directory (after interactive arguments, so you first have to type the pattern and enter...) and stores it in wd-list for further use. Now your find-grep are always done in that directory.
You could have a custom variable for the sane default, and then have the user enter the path or accept the default on the first call.
(defcustom default-path "/tmp/foo" "Path")
(setq current-path nil)
(defun foo ()
(interactive)
(unless current-path
(setq current-path
(read-from-minibuffer
(format "Path [%s]" default-path) nil nil t nil default-path)))
(message "Path is: %s" current-path))
The first time you do M-x foo, it prompts for the path. A common idiom is to allow the user to specify a prefix argument when they want to change the value (after the first time.) This code will have the desired effect:
(defun foo (choose)
(interactive "P")
(when (or choose (not current-path))
(setq current-path
(read-from-minibuffer
(format "Path [%s]" default-path) nil nil t nil default-path)))
(message "Path is: %s" current-path))
Now doing M-x foo is the same as before, but C-0 M-x foo will prompt for a new value.
In your example, something like this will work.
(defun igrep-read-files (&optional prompt-prefix)
(interactive "P")
(when (or prompt-prefix (not current-path ))
(setq current-path
(read-file-name "Dir: " default-path nil t)))
(message (expand-file-name default-files-string-new current-path)))
Have a look at the code of sendmail-query-once.
Although it's not very fashionable to do this sort of thing.
Usually package writers pick a sane default and let the user
customize it as they want.

Wrong type error in dbus method (GNU Emacs)

I am writing an elisp file to integrate GNU Emacs with Zeitgeist over dbus. Because of the lack of good documentation on dbus in emacs and my lack of experience with advanced elisp, I am coming up with the following error in my method zeitgeist-send:
Wrong type argument: D-Bus, (zeitgeist-event-timestamp)
I have tried correcting the issue by placing :string before all the arguments, but that gave me the error:
Wrong type argument: stringp, (zeitgeist-event-timestamp)
This makes no sense, as I am positive that the value of (zeitgeist-event-timestamp) is a string.
If you need, the dbus documentation for zeitgeist is here. The format for it is asaasay.
Here is the code:
(require 'dbus)
(defun zeitgeist-call (method &rest args)
"Call the zeitgeist method METHOD with ARGS over dbus"
(apply 'dbus-call-method
:session ; use the session (not system) bus
"org.gnome.zeitgeist.Engine" ; service name
"/org/gnome/zeitgeist/log/activity" ; path name
"org.gnome.zeitgeist.Log" ; interface name
method args))
(defun zeitgeist-event-timestamp ()
"Get the timestamp in zeitgeist format."
(let* ((now-time (current-time))
(hi (car now-time))
(lo (car (cdr now-time)))
(msecs (car (cdr (cdr now-time))))) ; This is *micro*seconds.
(number-to-string (+ (/ msecs 1000)
(* (+ lo (* hi 65536)) 1000))))) ; Convert system time to milliseconds.
(defun zeitgeist-event-interpretation (event)
"Get the Event Interpretation of EVENT."
(case event
('zeitgeist-open-event
"http://zeitgeist-project.com/ontologies/2010/01/27/zg#AccessEvent")
('zeitgeist-close-event
"http://zeitgeist-project.com/schema/1.0/core#CloseEvent")
('zeitgeist-create-event
"http://zeitgeist-project.com/schema/1.0/core#CreateEvent")
('zeitgeist-modify-event
"http://zeitgeist-project.com/schema/1.0/core#ModifyEvent")
(otherwise nil)))
(defun zeitgeist-send (event fileurl filemime)
"Send zeitgeist an event EVENT using the list FILEINFO."
(let ((event-interpretation (zeitgeist-event-interpretation event)))
(if (eq nil event-interpretation)
(message "YOU FAIL")
(zeitgeist-call "InsertEvents"
'(""
(zeitgeist-event-timestamp)
event-interpretation
"http://zeitgeist-project.com/schema/1.0/core#UserActivity"
"app://emacs.desktop")
'((fileurl
"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo/#Document"
"http://www.semanticdesktop.org/ontologies/nfo/#FileDataObject"
fileurl
filemime
(file-name-sans-versions fileurl)
"")) ; Some black magic later
'(:array)))))
(defun zeitgeist-open-file ()
"Tell zeitgeist we openned a file!"
(if (eq nil (buffer-file-name))
(message "You are not on a file.")
(zeitgeist-send 'zeitgeist-open-event buffer-file-name "text/plain")))
(zeitgeist-open-file)
Thanks for any help!
Patrick Niedzielski
I asked on the mailing list, and I found out that since I was using (quote ...) instead of (list ...), the variables and functions were not evaluated. A stupid LISP mistake.
Also, if anyone is interested, I will send this patch into the Zeitgeist project.
Cheers,
Patrick