Blog

July 2, 2009
Categories:

aquamacs 2.0 elisp fun

While waiting for the white smoke to emerge on Textmate 2.0, I decidedto revisit an old friend. It turns out the emacsen have been busysince I've been using TextMate. Aquamacs 2.0 is a great Cocoa port ofEmacs 23. Not sure whether I should call it a port since emacsofficially supports the OS X operating system.The first thing I did was check out some of the old modes I used touse. AucTeX, GNUs are both packaged with emacs now. The SQL mode that comes withis great and "just works" with mysql and postgresql. There is goodsupport for Python and of course HTML. Two minor gliches in thoseareas that I wish I could fix:
  • I cannot get MMM (multi-major-mode) to work properly. I wouldreally like to have both HTML and Javascript colorized properly whenI'm working on my Django projects. If anyone has any advice ongetting MMM to work right in emacs 23 I would really appreciate apointer.
  • Completion in iPython does not work right. Instead of completingin the ipython shell it just indents more.
In addition to some old friends I discovered a bunch of new modes thatare really nice. JDE for Java development, Muse for creating lecturenotes in a personal wiki style. In addition Muse lets you publish amarkup style format really easily in lots of differnt output formats.I've been a big fan of using Pandoc with TextMate over the last fewyears, but Muse is every bit as nice.One thing lacking in Muse was the ability to quickly bold or ital ortt some part of the text I'm working on. So I decided to dust off mylisp coding ability and cook up my own. The result is shown below. Ithink it is a nice example of a lot of what you need to do in writingbasic lisp extensions for any text processing mode. I'd be happy toget comments on how to improve this.The idea of these simple commands is that pressing cmd-b should boldthe word the cursor is currently on, or the region (if there is one)or simply insert **|** when the cursor is not on a word. It alsotook some research to figure out how to bind a function to the commandkey. (side note: I've finally gotten comfortable with using the optionkey for meta in emacs.)


(defun muse-bold-word ()
(interactive)
(muse-wrap-with-string "**" 2)
)

(defun muse-ital-word ()
(interactive)
(muse-wrap-with-string "*" 1)
)

(defun muse-tt-word ()
(interactive)
(muse-wrap-with-string "=" 1)
)

(defun muse-wrap-with-string (str len)
(if mark-active
(progn
(kill-region (point) (mark))
(let ((myword (car kill-ring)))
(insert (concat str myword str) )))
(let ((myStr (thing-at-point 'word)))
(if myStr
(let ((myBounds (bounds-of-thing-at-point 'word)))
(kill-region (car myBounds) (cdr myBounds))
(insert (concat str myStr str)))
(progn
(insert (concat str str))
(goto-char (- (point) len)) ) ) ))
)

(define-key muse-mode-map `[(,osxkeys-command-key b)] 'muse-bold-word)
(define-key muse-mode-map `[(,osxkeys-command-key i)] 'muse-ital-word)
(define-key muse-mode-map `[(,osxkeys-command-key k)] 'muse-tt-word)