defmain
defmain copied to clipboard
Proposal to not `quit` when we are working inside Slime
Hello there,
I tried defmain, and I tried to call the main function from Emacs & Slime. It should be possible, since what defmain does is creating a defun
called main
which accepts a &rest argv
. My goal was to call the main function from a run.lisp script. That way I could avoid writing another function that would be called by main and that would accept the same key arguments from main.
So I evaluated (C-x C-e) or compiled a line like (mypackage::main :debug t)
to see what it does, but it closes the Lisp connection.
I suggest we check if we are on Slime and do not quit in that case.
A very simple way is to check the TERM
environment variable. On Slime, it's "dumb". I made a trivial system for that: termp.
This is a good idea. Ideally, we should call this helper system like in-ide
instead of termp
, because there are other environments like SLY, Lem, Closure IDE, LispWorks, etc. I'd create a single function there which will return a keyword for IDE kind or NIL, if this script is running standalone.
By the way, a function defined with defmain
will not work like this (mypackage::main :debug t)
, because it defines a function which accepts args
which are unix args – a list of strings.
To solve this issue really well, we need to separate defined body where variables names a used as keywords from the function which parses command-line arguments. And a version to be called should be chosen depending on IDE or non-IDE environent.
How would you find the IDE kind? (is there an environment variable for this?)
I don't see any special environment variables in LispWorks listener. But probably the best way will be to figure out IDE from backtrace stack frames.
For example, in lispworks it contains a call to INTERACTIVE-PANE-TOP-LOOP
:
CL-USER 33 > (dbg:output-backtrace :bried)
Call to EVAL
Call to CAPI::CAPI-TOP-LEVEL-FUNCTION
Call to CAPI::INTERACTIVE-PANE-TOP-LOOP
Call to MP::PROCESS-SG-FUNCTION
In SBCL running under the SLYNK:
...
/Users/art/projects/lisp/sly/slynk/slynk.lisp:tlf73fn4: CALL-WITH-LISTENER:
FN = #<FUNCTION (LAMBDA () :IN SLYNK::SPAWN-CHANNEL-THREAD) {1003B4802B}>
LISTENER = #<SLYNK-MREPL::MREPL mrepl-1-1>
OBJECT = #<SLYNK-MREPL::MREPL mrepl-1-1>
SAVING = NIL
/Users/art/projects/lisp/sly/slynk/slynk.lisp:tlf131fn4: (FLET FORM-FUN-5 IN SPAWN-CHANNEL-THREAD):
CONNECTION = #<SLYNK::MULTITHREADED-CONNECTION {10027A5C93}>
G6 = #<SLYNK-MREPL::MREPL mrepl-1-1>
...
The guesser should search for packages such as CAPI
, SLYNK
, SWANK
, etc and then check if some symbols are present in the stackframes.
mmh good ideas, though would you maybe accept a first patch that does like termp
? "make it work, make it right"…