Is emacs the only elisp interpreter? - emacs

I recently started using emacs and extending it with elisp. That makes me wonder: is emacs the only program that can evaluate elisp expressions or do dedicated elisp-interpreters exist?

You can use Elisp outside emacs if you use Guile
here is de documentation:
https://www.gnu.org/software/guile/manual/html_node/Using-Other-Languages.html
☸ sandas-nonpro (qa) in ~ via ⬢ v12.4.0 took 11s
❯ guile ~
GNU Guile 3.0.7
Copyright (C) 1995-2021 Free Software Foundation, Inc.
Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.
Enter `,help' for help.
scheme#(guile-user)> ,language elisp
Happy hacking with Emacs Lisp! To switch back, type `,L scheme'.
elisp#(guile-user)> (printf "Hello World!)
You can also write scripts in emacs lisp like this:
☸ sandas-nonpro (qa) in ~ via ⬢ v8.9.4
❯ cat > test.el ~
:;exec emacs -batch -l "$0" -f main "$#"
(defun main ()
(print (version))
(print (format "I did it. you passed in %s" command-line-args-left)))
;; Local Variables:
;; mode: emacs-lisp
;; End:
☸ sandas-nonpro (qa) in ~ via ⬢ v12.4.0
❯ chmod +x test.el ~
☸ sandas-nonpro (qa) in ~ via ⬢ v12.4.0
❯ ./test.el cat dog ~
"GNU Emacs 28.0.50 (build 2, x86_64-apple-darwin20.6.0, NS appkit-2022.60 Version 11.5.2 (Build 20G95))
of 2021-09-15"
"I did it. you passed in (cat dog)"
But I encourage to use it inside emacs, use ielm buffer alot or eshell which is a shell that you can combine emacs lisp s-expressions with shell commands. and obviously gccemacs, if you pan to do intensive work in emacs.

CLOCC/CLLIB/elisp.lisp allows running Emacs-Lisp code from Common Lisp.

Related

Build C program in gcc and run it in valgrind using Emacs

I am studying programming, which requires me to write and run many small programs, so I cannot make a makefile for each one. I want to make a command that would take current open file, compile it with gcc (with arguments) and run it with valgrind (with arguments as well), while allowing me to interact with it (possible by opening another shell window). Is it possible? If yes, how to do it?
You can store your compile-command in the buffer by writing
// -*- compile-command: "gcc -o foo foo.c && valgrind foo" -*-
in the first line.
You may want to do (put 'compile-command 'safe-local-variable 'stringp) to prevent Emacs from warning you about the file local variable (but realize that untrusted files can potentially do malicious things).
I have a yasnippet snippet for adding this to one-off files.
# -*- mode: snippet -*-
# name: compile
# key: compile
# expand-env: ((file (if (buffer-file-name) (file-name-nondirectory (buffer-file-name)) (buffer-name))))
# --
// -*- compile-command: "gcc -DDEBUG=9 -ansi -Wall -g3 -std=c99 -o ${1:$(file-name-sans-extension yas-text)} ${1:`file`}" -*-
I am studying programming, which requires me to write and run many small programs, so I cannot make a makefile for each one.
As I mentioned in a comment I don't understand this logic, but if you insist on typing the commands each time you could run M-x compile and change the command to whatever you want (e.g. gcc -Wall --foo file.c or valgrind myapp arg1 arg2), then hit RET.
The next time you run M-x compile in the same Emacs session it will suggest the previous command. This should hopefully save you a few keystrokes.

Batch export of org-mode files from the command line

Assume that I have in a certain directory several org-mode files: foo1.org, foo2.org, etc. I would like to have a script (maybe a makefile) that I could invoke something like
$ generate-pdfs
and foo1.pdf, foo2.pdf, etc. will be generated.
I thought that something like emacs --batch --eval <MAGIC> is a good start, but I don't know the magic.
A solution that is solely inside emacs could be of interest as well.
As you said, Emacs has the --batch option to perform operations with Emacs from the shell. In addition to that, you can use the -l flag to load Emacs Lisp code from a file and execute it, and the -f flag to execute a single Lisp function.
Here is a basic example, which exports a single org-mode file to HTML:
emacs myorgfile.org --batch -f org-html-export-to-html --kill
Perhaps you want something more advanced like exporting/publishing a full org-mode project. I do not have sample code for that, but it should not be too complicated.
I also have a sample Makefile I wrote some time ago to export all org-mode files in the directory to HTML (and also copy the HTML files to another directory):
OUT_DIR=/some/output/dir/html
# Using GNU Make-specific functions here
FILES=$(patsubst %.org,$(OUT_DIR)/%.html,$(wildcard *.org))
.PHONY: all clean install-doc
all: install-doc
install-doc: $(OUT_DIR) $(FILES)
$(OUT_DIR):
mkdir -v -p $(OUT_DIR)
%.html: %.org
emacs $< --batch -f org-html-export-to-html--kill
$(OUT_DIR)/%.html: %.html
install -v -m 644 -t $(OUT_DIR) $<
rm $<
clean:
rm *.html
EDIT:
With Org-mode 8 and the new export engine the function for HTML export has changed.
To make the previous examples work with Org 7 or older, replace org-html-export-to-html with org-export-as-html.
I expect to publish (by the end of this week-end) OrgMk, a suite of Makefile and standalone Bash scripts (usable as well under Cygwin) just to do that! Even more: generation of HTML, Ascii, Beamer, etc.
You'll find it on my GitHub account: https://github.com/fniessen/ (where I already have Emacs configuration files, color themes and other stuff such as an Org Babel refcard -- in progress).
Mark a few org files in dired and call this:
(defun dired-org-to-pdf ()
(interactive)
(mapc
(lambda (f)
(with-current-buffer
(find-file-noselect f)
(org-latex-export-to-pdf)))
(dired-get-marked-files)))
If you know what async is, wrap the call as it can take a while.
update:
Here's a version that combines the awesome dired approach with the lame
other one:)
(defun dired-org-to-pdf ()
(interactive)
(let ((files
(if (eq major-mode 'dired-mode)
(dired-get-marked-files)
(let ((default-directory (read-directory-name "dir: ")))
(mapcar #'expand-file-name
(file-expand-wildcards "*.org"))))))
(mapc
(lambda (f)
(with-current-buffer
(find-file-noselect f)
(org-latex-export-to-pdf)))
files)))

emacs lisp will not start

I am trying to setup the slime mode in emacs for using common lisp. When I attemp to start slime with M-x slime I get an error message saying:
process inferior-lisp not running.
So, I checked the value of the variable inferior-lisp-program which turned out to be "/opt/sbcl/bin/sbcl". sbcl is an acronym for an implementation of common lisp known as steel bank common lisp. Note that this variable is defined in file slime.el. As I do not have sbcl (the previous directory does not even exist on my machine) installed on my machine (which runs os x 10.8.3) this will not work.
I have the clisp implementation which is located in the directory: /opt/local/bin/. I tried to change the value of the variable inferior-lisp-program by:
(setq inferior-lisp-program '/opt/local/bin/clisp/)
However, this did not work and I do not know what else to try.
How can I get inferior-lisp to run and hence get slime to work?
EDIT: Here is some extra information I believe that could be helpful. If I try to just start common lisp in emacs by executing M-x run-lisp I get the following output from emacs:
(progn (load "/Users/s2s2/.emacs.d/slime/swank-loader.lisp" :verbose t) (funcall \
(read-from-string "swank-loader:init")) (funcall (read-from-string "swank:start-s\
erver") "/var/folders/wf/yjgymt8j14v2tqwjnny68wq00000gn/T/slime.28222"))
Can't exec program: /opt/sbcl/bin/sbcl
Process inferior-lisp exited abnormally with code 1
Can't exec program: /opt/sbcl/bin/sbcl
Process inferior-lisp exited abnormally with code 1
Hope this helps! All help is greatly appreciated!
The variable slime-lisp-implementations has higher priority than inferior-lisp-program for slime if set; try this instead (adjust parameters accordingly):
(setq slime-lisp-implementations
'((clisp ("/opt/local/bin/clisp" "-q -I"))
(sbcl ("/usr/local/bin/sbcl") :coding-system utf-8-unix)))
The first thing to try is to run the command in a regular shell window - just type or copy and paste the executable path there and see what bash tells you:
$ sbcl < /dev/null
bash: sbcl: command not found
$ clisp < /dev/null
<<clisp splash screen>>
$ which clisp
/usr/bin/clisp
Once you find out what the correct executable is, you set inferior-lisp to it:
(setq inferior-lisp "/usr/bin/clisp")
Notes:
It should be a string, not a symbol, so you need the quotes ".
It should point to a file, not a directory, so your trailing slash / is wrong

Honor in-file settings for batch export of org files as HTML

I would like to use Emacs in batch mode to export a number of org files to HTML from the command-line. And I would like to get the same result than interactively using C-cC-eh, in particular:
honor file-local variables (such as org-export-publishing-directory)
honor all options specified through #+KEYWORD: headlines
Starting from the example given in org-export-as-html-batch, I got to this point:
emacs --batch \
--visit=/tmp/foo.org \
--eval "(defun safe-local-variable-p (sym val) t)" \
--funcall hack-local-variables \
--eval "(setq org-export-headline-levels 4)" \
--funcall org-export-as-html-batch
However, some problems remain:
I need to explicitly specify the headline level and I fail to see why all other #+OPTIONS are honored (like toc:nil) but not this one
I had to manually trigger file-local variables parsing using hack-local-variables (I guess it is not automatically done in batch mode) but more importantly I had to resort to a hack to mark all local variables as safe (I'm sure there is much space for improvement here).
NB:
In case it matters, I'm using emacs 23.2.1 (Debian Squeeze flavour)
Here is a sample org file on which I tested this:
#+TITLE: Foo
#+OPTIONS: H:4 toc:nil author:nil
* 1
** 2
*** 3
**** 4
# Local Variables:
# org-export-publishing-directory: "/some/where";
# End:
I eventually got the following script, which seems to fulfill all my requirements:
#!/bin/sh
":"; exec emacs --script "$0" -- "$#" # -*-emacs-lisp-*-
;;
;; Usage:
;; org2html FILE1 [FILE2 ...]
;; Mark org-related variables as safe local variables,
;; regardless of their value.
(defun my/always-safe-local-variable (val) t)
(dolist (sym '(org-export-publishing-directory
org-export-html-preamble
org-export-html-postamble))
(put sym 'safe-local-variable 'my/always-safe-local-variable))
(defun my/org-export-as-html (filename)
"Export FILENAME as html, as if `org-export-to-html' had been called
interactively.
This ensures that `org-export-headline-levels' is correctly read from
the #+OPTIONS: headline."
(save-excursion
(find-file filename)
(message "Exporting file `%s' to HTML" filename)
(call-interactively 'org-export-as-html)))
(mapcar 'my/org-export-as-html
(cdr argv)) ;; "--" is the first element of argv
A few notes on this script:
The executable emacs-lisp script trick comes from this question.
The only way I found to use the org-export-headline-levels value from the #+OPTIONS: headline is to call org-export-as-html interactively, instead of org-export-as-html-batch.
hack-local-variables does not need to be explicitly called, provided that local variables are marked as safe before the file is opened.
I think it is better to only mark org-related variables as safe, using the safe-local-variable symbol property.

Something wrong with Emacs shell

I use Ubuntu8.10 and emacs-snapshot. Running shell-mode on emacs and input "ls" shows escape codes:
screenshot http://lh3.ggpht.com/_os_zrveP8Ns/SdMmohKNjmI/AAAAAAAADB4/VlKpr5H_7ZA/s512/screen.png
How can I get the output I expect?
You can use AnsiTerm which does support colors or you can enable AnsiColor for the normal shell:
(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
Furthermore, you may choose another shell: M-x term or M-x eshell. The former provides an interface that is much closer to a real terminal emulator than shell-mode (once you start it, you can get out of the mode with C-c C-j and get in again with C-c C-k). The latter is a shell implementation written in Elisp (you can use the common shell commands as well as evaluating Lisp code).
Expanding on vatine's answer, you can add that inside your .cshrc (.tcshrc/.bashrc) wrapped with a check for the environment variable INSIDE_EMACS.
For example (from my .tcshrc):
if ( $?INSIDE_EMACS ) then
alias l 'ls --color=never'
endif
M-x ansi-color-for-comint-mode-on
The problem is that "l" is trying to colorise the output and emacs isn't having any of it. Try the following:
$ unalias l
$ alias l ls --color=never
I wrapped my alias ls ='ls --color=auto' in ~/.bashrc:
case "$TERM" in
xterm*|rxvt*)
if [ -x /usr/bin/dircolors ]; then
alias ls='ls --color=auto'
...
fi
;;
*)
;;
esac
This disables using color=auto in emacs.