Monday, March 24, 2008

Emacs Tip #15: indent yanked code

I'm a bit apprehensive about this chunk of code, mainly because it facilitates cut/paste coding, which I abhor. Nevertheless, it does come in handy.

When you (shudder) cut/paste code, one of the first things you do is immediately indent the code appropriately. Well, why not have that done automatically for you? This chunk of emacs lisp does the trick rather nicely.

It will not indent regions that are too large (see yank-advised-indent-threshold) and given a prefix argument, it will not indent.

;; automatically indenting yanked text if in programming-modes
(defvar yank-indent-modes '(emacs-lisp-mode
c-mode c++-mode
tcl-mode sql-mode
perl-mode cperl-mode
java-mode jde-mode
LaTeX-mode TeX-mode)
"Modes in which to indent regions that are yanked (or yank-popped)")

(defvar yank-advised-indent-threshold 1000
"Threshold (# chars) over which indentation does not automatically occur.")

(defun yank-advised-indent-function (beg end)
"Do indentation, as long as the region isn't too large."
(if (<= (- end beg) yank-advised-indent-threshold)
(indent-region beg end nil)))

(defadvice yank (after yank-indent activate)
"If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
(if (and (not (ad-get-arg 0))
(member major-mode yank-indent-modes))
(let ((transient-mark-mode nil))
(yank-advised-indent-function (region-beginning) (region-end)))))

(defadvice yank-pop (after yank-pop-indent activate)
"If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
(if (and (not (ad-get-arg 0))
(member major-mode yank-indent-modes))
(let ((transient-mark-mode nil))
(yank-advised-indent-function (region-beginning) (region-end)))))

Thursday, March 20, 2008


I've written before about interviewing, but I think there's a topic people have ignored, namely that people conducting interviews forget (in my vast experience of employment in 2 companies) is that a part of the purpose of the interview is to sell the group/company to the interviewee.

Shortly after the post in August, a friend of mine started looking for jobs, he interviewed at companies X and Y, and his experiences were very different.

Let me compare and contrast the two interviews:

At company X, he was underwhelmed by the technical lead for the job (the guy is nice, but somewhat soft-spoken, so not a lot of energy/excitement), and the questions posed by all the interviewers were pretty shallow. In other words, the questions were like, "write a routine to do sorting", which he did, and then they moved on to the next question - no further investigation of why he chose that, how might he optimize it, etc. etc. etc. It sounded kind of like watching an interview on TV where the questions were all 'yes' or 'no' questions.

At company Y, the questions were very often 'deep' questions, where the initial answer was picked apart and explored. The questions also seemed to be a little more interesting and challenging - one of the guys asked my friend how he might approach a problem that the guy had been working on that entire week.

Needless to say, my friend decided against working at company X and was very tempted by company Y.

I recall my interview at Mentor, and it was somewhat similar to my friend's at company X, only I wasn't asked to write any code. I did bring some samples of what I'd written and offered them to the hiring manager. I presume the interviewers liked what they saw in me, but I was concerned because I expected to be asked to show some level of competence in the job for which I was interviewing (a programmer).

I always figured that if you're interviewing for a job at an auto-repair shop, you'd be expected to poke around an engine and show some competence. The same thing should go for a programming job. And, wouldn't you be a little concerned if you knew your mechanic didn't require a competency exam before hiring his mechanics? (See raganwald's hiring a juggler post.)

So remember, the quality of the interview you provide influences how the interviewee views the company.

Monday, March 17, 2008

Emacs Tip #14: caps lock on windows

In my previous job, everyone had Windows laptops, and worked using VNC. But they never learned the trick to change the Caps Lock key into a Ctrl key.

From the GNU Emacs FAQ for Windows: "Caps"

Download caps-as-ctrl.reg to make CapsLock a Control key (leaving your original control keys as they were), or caps-ctrl-swap.reg to swap CapsLock and the left Control key. Once you've downloaded them, double-click on the .reg files in Explorer or "run" them from a command prompt to have them update your registry.
If you use Emacs and you are using Windows and your Caps Lock key is still functioning as Caps Lock, just download the registry change and install it. If you really think you need to type in all caps, learn M-x upcase-region

Monday, March 10, 2008

Emacs Tip #13: browse-kill-ring

The kill-ring. You know about the kill-ring, right?

C-h i m emacs RET kill ring RET

Ok, now you know about the kill-ring, it is basically a list of all the chunks of text that have been cut (C-w) (or just saved using M-w). The basic interaction with the kill-ring is:

C-y (aka "yank" aka "paste")

After you've pasted text, if you didn't want that, but an earlier chunk of text, the key M-y will cycle through the earlier chunks, replacing what was just pasted with the earlier text.

If that's too much for you to handle, you can browse the entries in the kill-ring and paste them in (more than one if you want).

To get it, go to the wiki and download it.

To install and use, do the standard download, load-path manipulation, and:

(require 'browse-kill-ring)
M-x browse-kill-ring

[edited to correct the cut/paste commands]
[edited to fix broken link]

Monday, March 3, 2008

Emacs Tip #12: show-trailing-whitespace

If you don't like having lines of code/text with whitespace at the
ends, you can turn on the variable 'show-trailing-whitespace' to have
Emacs highlight the offending whitespace.

When set, the variable's value becomes buffer local, so set it to true
in the mode-hooks for your preferred modes. Or, if you want it on all
the time, change the default value with:

(setq-default show-trailing-whitespace t)