I have a confession: I don’t know Lisp. Despite that fact, with a bit of help from some co-workers, I managed to write an emacs macro/script which:
- switched to shell mode (ie. M-x shell-mode)
- disabled truncating lines (ie. M-x toggle-truncate-lines)
- started a database console (ie. “mysql”)
I was then able to start emacs with that macro using the –script option, and suddenly I had a way to start mysql in a much friendlier environment with a single command 🙂
But here’s the problem: I changed jobs and left that script behind. Now I’d very much like to re-create that script at my new job, but I no longer have any emacs experts to help me write it like I did at the old job.
Now, I really hate SO posts where someone basically says “please write my code for me”, so I don’t want to do that. However, if any emacs macro experts could at least give me some pointers (like “here’s how you invoke a M-x command in a macro”), or point me to an emacs-macro-writing guide, or otherwise “teach me to fish” on this issue, I would greatly appreciate it.
… and if someone just happened to have a similar script already lying around that they wanted to post, I certainly wouldn’t complain 😉
Most emacs commands (i.e.,
M-x toggle-truncate-lines) can be translated directly to elisp by wrapping them in parentheses:The rumours are true, in lisp you just scatter parentheses around and they make magic.
Now in this case, you can do better. Toggling makes sense for an interactive function, but in a program you don’t really want to toggle truncate-lines, you want to turn on truncate-lines. Its the same thing if truncate-lines was turned off to begin with, but you don’t know when your program will be run next. Anyways, in Emacs, features are often controlled by a variable. In this case, the variable is
truncate-lines, and to turn that feature on, you set the variable tot(which means true).To do this, use:
We use
setqinstead of=for assignment, because they made lisp before=had been invented.For the real scoop you should take a look at Robert Chassel’s excellent “An introduction to to Programming in Emacs Lisp”. It comes built-in with your emacs, you can get to it with
C-h i m Emacs Lisp Intro.