Handy emacs printing trick

emacs -nw myfile.c -e “ps-print-buffer” -e “save-buffers-kill-terminal”

By extension, you can keep stacking -e’s with interactive functions that take no arguments for as many commands as you want. I would imagine there is a way to pass arguments to the functions you list, but I haven’t found that yet. Also, it might be nice to be able to do something like -e “(+ 5 9)”, but I haven’t found the proper way to do that either.

Emacs Mode for Protobuf editing

I like using Google’s Protocol Buffers (aka protobuf). It is faster and more bandwidth/disk efficient than JSON, but perhaps not quite as simple or flexible.

In protobuf you have messages. In messages, everything is a key/value pair. Keys are tagged to show the type of the value (int, float, string, sub-message), then followed by the value. Keys can be used repeatedly to create something like arrays. The big difference between a protobuf message and a JSON associative array is that in JSON the keys are strings, while in protobuf, the key is a number. Protobuf uses .proto files to describe the messages, primarily to map a text name for keys to a number, and also to describe the type of the field, specify a default value, and to flag it as required/optional/repeated. The messages in the files look something like this:


message Person {
        required int32 id = 1;
        required string name = 2;
        optional string email = 3;
}

I also like Emacs a lot, and so obviously, I will use Emacs to edit these .proto files. Here is the start of a major mode for editing proto files. Just paste it into a file named protobuf.el, then (require ‘protobuf) in you .emacs file.


(define-derived-mode protobuf-mode c-mode
  "Protocol Buffer" "Major mode for editing Google Protocol Buffer files."
  (setq fill-column 80
          tab-width 4))

(add-to-list 'auto-mode-alist '("\\.proto$" . protobuf-mode))
(provide 'protobuf)

This is my first major mode. It is derived from C mode, and I still haven’t figured out how to add syntax rules for the =1 type stuff required by every line. I hope to get back and flesh this out further eventually.

Must have Emacs Mode

If you ever edit javascript code, the only emacs mode to consider is Steve Yegge’s JS2 mode.  The official page for it is here.  The annoucement page is here.  This javascript mode is an example of what more modes should aspire to.  It parses your buffer into a javascript AST and uses that for syntax highlighting, and also to check for syntactic correctness, helping to prevent you from having bugs caused by typos.  This is a lot faster than having to load the code into a web browser to find out that you missed a ‘;’.

It also provides Javascript code navigation.  Some other modes have this, like python mode (but not C-mode, at least not by default), but I wish all modes had it.

I wish someone would copy this strategy for a new C mode or Python mode.

eval-exp-insert

I often want to calculate something quickly then stick it into the buffer I am currently editing. Previously, I would do something like type (+ 3 5) into the buffer, Type C-x C-e, see the result in the minibuffer at the bottom of the screen (are re-evaluate repeatedly if I didn’t quite catch it, say for a long result), type the result into the buffer, then delete the expression (although sometimes in C code, I might leave the expression in a comment).

Eventually, I realized that Esc-: brought up an eval prompt in the minibuffer and that I could use it instead of the above. But I still had to be fast at reading the result before it was taken of the screen, and now I had to retype the expression to re-evaluate it. Sometimes this was an improvement, but not always.

Alternatively, sometimes I would type (insert (format “%s” (+ 3 5))), and evaluate it, but that was quite a lot of typing, and so I’d only use that when I wanted a long result to be pasted into my buffer. And then I still had to delete that long expression.

That led me to do the following in my .emacs files:

(defun eval-exp-insert (EVAL-EXPRESSION-ARG)
  "Evaluate EVAL-EXPRESSION-ARG and insert the result into the current buffer."
  (interactive "xEval: \n")
  (insert (format "%s" (eval EVAL-EXPRESSION-ARG))))

(global-set-key "\M-:" 'eval-exp-insert)

With that, when I type Esc-:, and then enter (+ 3 5), 8 is inserted into the buffer at the current cursor position.