Writing Web apps in Emacs Lisp

There are several solutions for running a webserver in Emacs like Elnode from Nic Ferrier, httpd.el from Joe Schafer, simple-httpd from Christopher Wellons or Emacs Web Server from Eric Schulte.

For this howto I will use simple-httpd so you will have to install it with M-x package-list-packages as well as mustache as templating engine.

Simple-httpd can be used together with impatient-mode for live serving a Emacs HTML buffer, but it can also used standalone to program a web application in style of Python Flask.

For the start let’s try the famous hello world.

(require 'simple-httpd)

(defservlet hello-world text/plain (path)
(insert “Hello world!”))

Point your browser to localhost:8080/hello-world and it should display the string “Hello world!”

If you want to get your hello world as index you have to evaluate the following snippet

(defun httpd/ (proc path &rest args)
(with-httpd-buffer proc "text/plain"
(insert "hello, let's start writing web apps in Emacs!")))

Now what about parameters to get a bit dynamically? Just use defservlet* instead of defservlet.

You can either have parameters in path or via GET/POST

(defservlet* goto/:path text/plain ()
(insert (format "cd %s" path)))

(defservlet* greet text/plain (name)
(insert “Hello ” name))

Last but not least it’s good style to separate the HTML code from the rest of the functionality so we use the mustache template engine to load and render a template file

(require 'mustache)

(defun template (file context)
“Return rendered template as string
Some code happily copied from http://ergoemacs.org/emacs/elisp_read_file_content.html”

(insert-file-contents file)

(defservlet* greet text/plain (name)
(let ((context (ht (“name” name))))
(insert (template “/home/basti/emacs-web.html” context))))

The content of the emacs-web.html file could be

Hello, my name is {{name}}

For more information about the mustache template engine refer to https://github.com/Wilfred/mustache.el

Happy web hacking in Emacs! 🙂


Sending files from Dired to Android

Ever wanted to send marked files in dired to a connected Android device?

I wrote a tiny extension that uses the external tool mtp-sendfile to do so.

Just install dired-mtp from MELPA or have a look at the Github repository

As someone stated out on Reddit one could also use adb in TRAMP mode, but for my Android device I can’t get it to work :/

Don’t know what to eat? Ask Emacs!

Help me I am hungry and don’t know what to eat!

Your stomache growls, tells you “I am hungry! Go get and hunt anything”, but you are sitting in front of your computer screen head in revolutionary code and dont want to waste energy thinking about what or where to eat now? Dont panic. Relax. Just let Emacs help you.

You can install the fooddice package either from marmalade repo or from https://github.com/balle/fooddice, put it somewhere in your `load-path’ and add (require 'fooddice) to your ~/.emacs file.

Now ask Emacs what to do

M-x what-to-eat

M-x where-to-eat

M-x what-to-drink

For better results set or add to the following lists

(setq fooddice/meals '(pizza pommes burger noodles bread soup chinese sushi))

(setq fooddice/drinks '(cola water coffee tea beer whisky milk cacao icetea energydrink))

(setq fooddice/restaurants '(italian chinese thai american japanese))

Emacs for creative writing

For those of you that are totally new to Emacs I would recommend reading Woodnotes Guide to Emacs for Writers as well as his cheatsheet.

To plan your writing and keep notes of ideas I suggest using org-mode.org. In the beginning it’s very easy to learn, but it grows together with your need to a full publishing machine that can help you writing your book which you can export in HTML, PDF, Latex and even to MS Word and LibreOffice (see http://vimeo.com/31564708).

To get better and practice more often tasks and writing games are a good idea such as “Give me a word and I write 15 minutes what comes to my mind” and if you dont keep up writing Emacs will automatically start deleting the last words. This is what Write or die! mode is for.

Now that you’ve written something Emacs can (to some degree) tell you how “good” e.g. complicated style your text is – like this sentence – by performing the Flesch-Kincaid scoring on it implemented by the writegood-mode

Another barometer for good writing style is the usage of weasel words als well as telling something in passive voice. artbollocks-mode can help you to avoid this (at least if you’re writing in English or are willing to modify the variables weasel-words-regex and passive-voice-regex).

Latex – quick & beautiful

Beside the latest Git source of auctex (due to compatibility issues with the installed version of texlive on Fedora 20, Debian Jessie and Arch Linux) I use the external packages auto-complete, ac-math, yasnippet and tex-smart-umlauts. The last one automatically converts German umlauts to the format \”a and vice versa. You always see and type umlaut character and Emacs does the rest for you.

Additionally I use yasnippets and auto-completion mode thus I am lazy and want to write as quick as my mind flows.

For spell checking flyspell and hunspell via ispell.el jumps in just because hunspell (from Libreoffice) is the most advanced spell checker available on GNU/Linux.

Last but not least I like the preview feature from auctex. It get’s you a WYSIWYG editor for Latex and by pressing C-c you can compile the current file and view it in an external program like Adobe Acrobat reader.

Here’s my complete config for copy & paste

(add-to-list 'load-path "~/.emacs.d/extensions/auctex")
(add-to-list 'load-path "~/.emacs.d/extensions/ac-math")
(load "~/.emacs.d/extensions/auctex/tex-site.el" nil t t)
(require 'reftex)
(require 'preview-latex)

(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-electric-escapy t)
(setq reftex-plug-into-AUCTeX t)
(setq TeX-PDF-mode t)
(setq TeX-show-compilation nil)
(setq TeX-toggle-debug-boxes nil)
(setq file_line_error_style t)
(setq TeX-view-program-list '(("Acrobat" "acroread %o")))
(setq TeX-view-program-selection '((output-pdf "Acrobat")))

(add-hook 'LaTeX-mode-hook 'visual-line-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)

; snippets
(require 'yasnippet)

; auto-completion
(require 'ac-math)
(add-to-list 'ac-modes 'LaTeX-mode)
(add-to-list 'ac-modes 'latex-mode)

(defun ac-LaTeX-mode-setup () ; add ac-sources to default ac-sources
(setq ac-sources
(append '(ac-source-math-unicode ac-source-math-latex ac-source-latex-commands ac-source-yasnippet)
(add-hook 'LaTeX-mode-hook 'ac-LaTeX-mode-setup)

;; Make RefTeX faster
(setq reftex-enable-partial-scans t)
(setq reftex-save-parse-info t)
(setq reftex-use-multiple-selection-buffers t)

(require 'tex-smart-umlauts)
(add-hook 'LaTeX-mode-hook #'tex-smart-umlauts-decode)

(setq latex-mode-hook
'(lambda ()
(setq ac-delay 0.2)
(define-key LaTeX-mode-map "\C-cu" 'LaTeX-find-matching-begin)
(define-key LaTeX-mode-map "\C-cd" 'LaTeX-find-matching-end)
(define-key LaTeX-mode-map "\C-ci" 'LaTeX-insert-item)

; spell checking
(require 'ispell)

(setq ispell-dictionary-base-alist
"[a-zäöüßA-ZÄÖÜ]" "[^a-zäöüßA-ZÄÖÜ]" "[']" nil
("-d" "de_DE" "-i" "iso-8859-1") nil iso-8859-1)

"[a-zA-Z]" "[^a-zA-Z]" "[']" nil
("-d" "en_US" "-i" "iso-8859-1") nil iso-8859-1)

"[a-zA-Z]" "[^a-zA-Z]" "[']" nil
("-d" "en_GB" "-i" "iso-8859-1") nil iso-8859-1)


(eval-after-load "ispell"
(setq ispell-dictionary "de_DE")
;(setq ispell-extra-args '("-t")) ; input is tex or latex
(setq ispell-silently-savep t) ; save personal dict without confirmation

(setq-default ispell-program-name "hunspell")
(setq ispell-really-hunspell t)

And here’s a tiny cheat sheet (not complete but the most usable keybindings)

C-c C-s insert section with label
C-c C-e insert new environment
C-u C-c C-e change current environment
C-c u jump to begin block
C-c d jump to end block
C-c * mark current section
C-c . mark current environment
C-c i insert \item
C-c < insert \index
C-c C-f C-b font bold
C-c C-f C-i font italics
C-c C-l show compiler output
C-c C-r compile region
C-c ` goto error
C-c ( insert a label
C-c ) list all labels and make ref entry
C-c = show toc to navigation
C-c return insert macro
M-x reftex-change-label globally rename a label
M-x reftex-goto-label goto label definition
M-x reftex-search-document Grep a label through all files

Emacs as an advanced terminal multiplexer

I really enjoy using Tmux and I love zsh, but I am missing something like ace-jump-mode to quickly jump to a line in my terminal and I would like to have snippets for everyday tasks like a for loop so some day I thought why not try using zsh in Emacs? And voila here is the config that let you jump around in your terminal by pressing “jj” and a head character as well as write you Yasnippets.
To use it you have to install multi-term, ace-jump-mode, key-chord and yasnippet.

(require 'multi-term)
(require 'ace-jump-mode)
(require 'key-chord)
(require 'yasnippet)

(key-chord-mode 1)
(setq key-chord-one-key-delay 0.15)
(key-chord-define-global "jj" 'ace-jump-mode)

(add-hook 'term-mode-hook (lambda ()
(setq yas/dont-activate nil)
(add-to-list 'term-bind-key-alist '("C-c C-n" . multi-term-next))
(add-to-list 'term-bind-key-alist '("C-c C-p" . multi-term-prev))
(add-to-list 'term-bind-key-alist '("C-c C-j" . term-line-mode))
(add-to-list 'term-bind-key-alist '("C-c C-k" . term-char-mode))

(global-set-key (kbd "C-c t") 'multi-term-next)
(global-set-key (kbd "C-c T") 'multi-term)

Now you can open a new terminal by pressing C-c T, switch between them with C-c C-n and C-c C-p and if you would like to copy some text in the middle of your screen first press C-c C-j to enable line-mode than jj to jump. The same applies for yasnippets first switch from char to line-mode (it makes your terminal a normal emacs buffer), type your keyword and expand it with tab. Here’s an example

# key: for
# --
for X in $1; do $0; done