emacs-lisp quick-hacks

by debianjoe

lambda

 

…that’s just cool.

 

As I’ve mentioned previously, and will probably do so again, I am an avid (rabid) emacs user.  When I’m setting up a new *nix system, it will be one of the first things that I put on, as I am accustomed to using it.  That being said, despite having all of the fantastically complex init files and loadable foobar.el options that I may have, some times I end up saying, “You know, it sure would be cool if I didn’t have to repeat this same action over and over.”

Let’s use a metaphorical situation that might warrant this.  Let’s assume we’re looking over and proofreading someone else’s poorly written C coding (as opposed to my own poorly written C), and I want to call attention politely to certain lines in a commit.  I’d like for it to always appear as a properly commented code function, be tied to a single button press (we’re assuming that this will need to be repeated quite often), and return the cursor to the previous position.  I’m going to go out on a limb and assume this isn’t the kind of action that we’ll be performing so often that it needs to be a permanent function, so rather than put it in our init, we can simply pop over to a *scratch* buffer, write out something short to make…oh, let’s use “F12″…print out our friendly comment, and then evaluate it for use in only this session.

(defun whattheflip ()
 "make automatic comment"
  (interactive)
  (save-excursion
    (goto-char (line-end-position)) 
    (insert "\n /*^^^wtf is this? It's horrible!!!*/ \n")
    ))
(global-set-key [f12] 'whattheflip)

Now, a couple of C^j’s later, we can flip back over to the code we’re proofreading and every time we press our F12 button, we get an automatic comment inserted on the next line that should hopefully serve as a friendly notice to our beloved teammate that we aren’t 100% clear as to why they’ve chosen to do something in a particular manner.

While this exact use may not perfectly suit your needs, I’ve often found that it’s the ideas that serve us better than the exact application.  For those interested in other cool hacks, I’d at least check out http://ergoemacs.org/emacs/elisp.html for other quick uses for emacs-lisp in text processing.

Advertisements