August 6, 2008

Drupal, Emacs, and templates: Module update functions

August 6, 2008 - Categories: drupal, emacs

Drupal’s coding conventions make it easier to hook into system behavior, but they also result in a lot of repetitive typing. For example, you can run code when upgrading a module by putting the code in a function named modulename_update_N() in your module’s install file. I found myself scrolling up and copy-pasting stuff too many times, so I decided to automate it instead.

I’ve been using yasnippet for my Emacs templates. All I needed to do to automate that little update bit was to write some code that figured out what the next update number should be. Here’s the snippet file I’ve just added (~/elisp/snippets/php-mode/drupal-mode/_update):

function `(sacha/drupal-module-name)`_update_`(sacha/drupal-module-update-number)`() {
  $ret = array();
  $0
  $ret[] = array(
    'success' => true,
    'query' => '$1',
  );
  return $ret;
}

The relevant functions from my ~/.emacs:

(defun sacha/drupal-module-update-number ()
  "Return the number of the next module update function.
This is one more than the highest number used so far.
This function should be called in a module's .install file."
  (save-excursion
    (save-restriction
      (widen)
      (goto-char (point-min))
      (let ((module-name (sacha/drupal-module-name))
	    (max 0))
	(while (re-search-forward
		(concat "function[ \t\n]+" module-name "_update_\\([0-9]+\\)") nil t)
	  (setq max (max (string-to-number (match-string 1)) max)))
	(number-to-string (1+ max))))))

(defun sacha/drupal-module-name ()
  "Return the Drupal module name for .module and .install files."
  (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))

I can’t think of how I’d do that in Eclipse. =) Don’t get me wrong–I still like Eclipse–but I heart being able to hack my editor on the fly.

Lifehacking: Switching to a rolling laptop bag

August 6, 2008 - Categories: life

I’ve been paying attention to the preventive advice I picked up during my last session with a registered massage therapist, and I thought I’d post an update on how things are going with this life-hacking.

There’s only one thing I’m having a hard time doing: leaning back. The massage therapist said that some of my back and neck muscles are tenser than they need to be because I lean forward instead of using the chair back. I’m not used to the idea of leaning back against the chair. It feels casual, and it sometimes means that I’m not in quite the right position to type.

Hmm…

Making things tangible: agile bug-tracking with LEGO!

August 6, 2008 - Categories: geek

What a creative way to make things concrete… <laugh>

Do what works for you; It’s not about being Gen Y, it’s about being new

August 6, 2008 - Categories: geek, learning, life

Luis Suarez has just realized that being a Web 2.0 evangelist doesn’t mean getting into every new Web 2.0 tool, and he linked to Chris Brogan’s post on doing what works for you.

This reminds me of a point I’ve been wanting to think about for a while. =)

When people look at the way new generations or new hires immerse themselves in learning, they often think that they themselves could never do that, could never find the time to try out and learn all these different things.

If you’ve ever told yourself that or heard someone say that, I’d like to help you remember what it was like to be on the other side of the fence, and I’d like to remind my future self what it’s like to be here.

Yes, being new means I have no bad habits to unlearn. That’s part of it. But it’s also true that I don’t have as many good habits as you do, or even “good enough” habits that can get me through. That’s one of the things that drives me to go out there and experiment. Because I don’t have a “good” way of dealing with so many things, I need to try lots of different ways and see which ways work for me.

In other areas, where I’ve found things that work for me, I stick with them and get deeper and deeper into them. Someday, my mind might have been so shaped by the tools I’ve used that I won’t see any need or potential beyond them, and it might be someone else’s turn to explore even further.

I think that the trick, then, is to keep “breaking” things, keep stepping out of my comfort zone, so that I always feel the urge to learn something. For example, even though I’ve used Emacs for years, I’m always pushing it to do something new, and I’m always tinkering with other editors and other systems to see if I can steal any good ideas.

Helping other people also stops me from becoming complacent, becoming too comfortable with my current toolkit. When I’m helping people brainstorm, answering their questions, customizing systems to fit their needs, I learn more about them, the system, and myself. I learn in the process of teaching.

I’m not going to adopt every single thing that comes along, especially when I’ve already got something that works out quite well. (Look at how old my text editor is!) That’s okay. That kind of exploration will naturally fit other people. What I can do, though, is:

That’s how I plan to mix being both new and experienced. =)

How do you keep yourself new?