lisp as a shebang script vs lisp running in SLIME - lisp

I just started with common-lisp, having come from C++ and Python. I'm trying to run a simple SDL program that does nothing other than show an image on-screen. I can get it working from within SLIME. The problem is, it won't work when run from the shell as a script.
My program looks like this:
#!/usr/bin/sbcl --script
(asdf:operate 'asdf:load-op :lispbuilder-sdl)
(defun main ()
(sdl:with-init ()
(sdl:window 320 240)
(sdl:draw-surface (sdl:load-image "image.png"))
(sdl:update-display)
(sdl:with-events ()
(:quit-event () t)
(:video-expose-event () (sdl:update-display)))))
(main)
When I run this as a script, I get the following error:
mkg#chisel:~/projects/common-lisp/sandbox$ ./hello-world.lisp
unhandled ASDF:MISSING-COMPONENT in thread #<SB-THREAD:THREAD "initial thread" RUNNING {AA5E849}>:
component "lispbuilder-sdl" not found
0: (SB-DEBUG::MAP-BACKTRACE #<CLOSURE (LAMBDA #) {AAF1EF5}>)[:EXTERNAL]
(... long backtrace omitted)
Oddly, this program works fine if I do the following. I open the program in Emacs, start SLIME in another window, and in the SLIME window, I enter the first line of the program:
(asdf:operate 'asdf:load-op :lispbuilder-sdl)
Then, in the editor window, I hit C-c C-k (compile/load file). This pops up a window showing image.png, as expected.
Why does this not work when run as a shebang script? How can I fix it?

As the man page for sbcl says, --script implies --no-sysinit --no-userinit --disable-debugger --end-toplevel-options, which means that initialization files are not read, and so if you set up ASDF registry there it is not set up, and so it cannot find the lispbuilder-sdl system. You need to either set up the registry in the script itself, or save an executable core with the registry already set up and call that instead of the default sbcl. Usually you can also save libraries in the core instead of loading them in the script, but I am not quite sure how that interacts with non-Lisp libraries and resources.

The usual way when developing in lisp is to use ASDF to describe project and its dependencies. Then, you can easily (asdf:oos 'asdf:load-op :yourapp).
For most implementations there is a way to generate executable form asdf definition.

Related

How can I write to standard output in Emacs

I am trying to debug an Emacs program performance wise. In particular, I suffer an extremely long startup time (~5' compared to ~1' for a bare Emacs) on a remote connection via WLAN, cellphone etc. In this context, any message written is no help, for the display is not refreshed at all.
What I would like to do is to write onto the "standard output" of the Linux process. I am aware of the --batch mode but this is no help to me because I want to use Emacs interactively.
So how can I write messages out to the Linux-standard output (as opposed to the Emacs standard output)?
You can output to standard error like this:
(print "hello world" #'external-debugging-output)
or
(princ "hello world" #'external-debugging-output)
This can buffer, so be careful.It's not possible to output to standard out at the moment. I'm going to add that, I think!
Start emacs as a daemon (emacs --daemon) and any messages during the start-up sequence will be sent to stdout or stderr, as described by lunaryorn.
Connect to the server with emacsclient
The simplest way to kill the server is M-x kill-emacs RET
For details see C-hig (emacs) Emacs Server RET
Works for me in centos 6.8 (GNU Emacs 23.1.1):
(append-to-file "here I come to save the day\n" nil "/dev/stdout")
Try also using "/dev/tty" in place of "/dev/stdout":
Unclear from question if you intend to redirect "emacs -nw" stdout to a file and monitor that file externally (then use "/dev/stdout"); or are ok with writing to "/dev/tty" thus polluting the self-same tty of the main "emacs -nw" display.
If starting a GUI version of emacs, in such a way it may lose attachment to originating tty, can abuse environment variables to communicate an originating shell's tty to elisp.
This works for me using Aquamacs in Mac OS X. Launching from a bash shell:
$ MY_TTY=$(tty) open /Applications/Aquamacs\ Emacs.app &
then in emacs:
(append-to-file "here I come to save the day\n" nil (getenv "MY_TTY"))

Accessing custom bash script from SBCL

I've seen this question but it doesn't seem to apply here.
Using SBCL, this works fine:
(run-program "/bin/ls" () :output *standard-output*)
So does this:
(run-program "/Applications/Safari.app/Contents/MacOS/Safari" ())
It launches a Safari window.
I can create a bash script in my bin directory that just has this in it:
/Applications/Safari.app/Contents/MacOS/Safari
When I run this bash script from the Terminal, Safari opens.
But I cannot run this script from inside SBCL:
(run-program "/Users/myhome/bin/safariscript" ())
REPL reports:
Couldn't execute "/Users/myhome/bin/safariscript": Exec format error
[Condition of type SIMPLE-ERROR]
The script certainly works fine on its own. I've searched ad nauseum for the meaning of this error without any help that would apply to a lisp environment, so I wonder if there is a broader issue at play here?
Shell scripts need a shell-bang line in it in order to be run via execve or anything that uses it, such as run-program. So you should use this as your file's content:
#!/bin/sh
exec /Applications/Safari.app/Contents/MacOS/Safari

SLIME on Windows 7

I'm trying to get set up with SLIME on a Windows 7 box, but running M-x slime gives me the error
Spawning child process: invalid argument
I have inferior-lisp-program set to "C:\\Program Files\\ccl\\wx86cl.exe" (which is factually correct, and running (comint-run inferior-lisp-program) gives me a working CCL prompt), and the slime directory added to my 'load-path.
What am I doing wrong?
EDIT: Tried loading up the same environment through the Windows edition of lispbox, and it runs SLIME fine. I'd prefer not to use that one because it packages an older Emacs, CCL and SLIME than I want.
The message you received means that there's a high chance that there was a syntax problem with the command given to shell. This would be caused by having characters in the file name, which can be interpreted as doing something special. So, it looks like Emacs was trying to call C:\\Program "program" with an argument Files\\ccl\\wx86cl.exe.
There are several ways to address the error:
There has to be an escaping function, something like:
(shell-quote-argument "C:\\Program Files\\ccl\\wx86cl.exe")
But since you cannot affect how the file name is passed to the function which creates the process, this isn't going to work.
You can move the program you want to call to a directory with "safe" name.
You can move the executable to be on the system path (%PATH% variable in Windows) - through changing environment variables and appending the directory with the executable to it.
One more option is to add the directory with the executable to exec-path variable in Emacs. This variable holds a list of all directories looked up for programs to run, if you just call a program by name, rather then by full path. This also (at least for me) makes my .emacs file easier to port between different systems.

How do you run iex from Emacs?

I keep on getting this warning when I run iex using elixir-mode-iex from Emacs:
Warning: could not run smart terminal, falling back to dumb one
I think that this just means that I don't get tab completion, which I'm fine with. But I'd like a smart terminal if it's possible with elixir-mode in Emacs.
elixir-mode-iex uses the comint-mode major mode to interactive with iex. That also means that it's acting just like a dumb terminal (doesn't have the ability to process special escape sequences etc see here).
As a workaround you just could use term which sends any key press directly to the subprocess itself. You could write a function like the following:
(defun my-elixir-iex ()
(interactive)
(term "iex"))
I'm working on an iex Alchemist.el integration, which brings functionality like Inf-Ruby has. But until it's done try to just use iex via term
Cheers
Samuel
It looks like that warning occurs when IEX can't find tty support. You can enable tty mode in emacs by invoking it with -nw.

Automatically saving shell history in Emacs

Is there an easy way to automatically save every command I execute in shell-mode buffer? I'm running things like python and lua from Emacs' shell buffer and want to save those in addition to regular bash commands.
Default behavior saves history in in .history or .bash_history, but it does not save input to subprocesses. As an example, if I do the following
ls /export/hda3/tmp
python
a=2+3
import sys
sys.exit()
ls /export/hda3/tmp
the following gets saved
#1328903075
ls /export/hda3/tmp
#1328903081
python
#1328903087
ls /export/hda3/tmp
Commands are saved automatically, only you need to make sure to actually exit the shell. If you simply kill the shell buffer then no commands will be saved.
I added a check to emacs exit to warn me if I have an open shell buffer, so that I can exit it manually:
(defun my-check-if-no-shell-buffer-exists ()
(if (not (get-buffer "*shell*"))
t
(message "you have a shell buffer, make sure you exit it manually")
nil))
(add-hook 'kill-emacs-query-functions 'my-check-if-no-shell-buffer-exists)
It would even be better if the shell buffer would do it automatically when killing the buffer. I think it's a bug in emacs that it fails to do that.
Edit: I noticed I have a setting which prevented running process warnings when emacs exits and that's why I needed the above function. If you get a warning about a running shell already when exiting, then you don't need it, you only need to exit the shell manually to save history.