An intelligent buffer switch for Slime/Lisp users on Emacs

Well, I can think of many reasons to modify this.
Probably its code should reside in site-lisp in its own .el file, and most certainly it can be done differently and better.
But it’s my first extension for Emacs, and it works!

  • What it is:

being a lazy asshole, I very often prefer to work in my living room rather than in my studio.
This means renouncing to my desktop PC’s huge monitor for a less confortable laptop with a 15.6 inch display.
Now this means much less real estate: working with Emacs, a code window, a REPL window, and a couple of HelpMesh and Firefox windows requires to be able to quickly switch. Quickly means keyboard, i.e alt+TAB and splitting the Emacs display. Unfortunately, on my laptop a horizzontally split Emacs means too few lines of code and REPL to be comfortable. So I thought that being able to quickly switch from REPL to code inside Emacs would have been nice.
C-x o on Emacs does it, if I have a split Emacs, but I want to view one window at a time and switch quickly. Again, C-x b does this, but I have to select my destination.
I never wrote Emacs Lisp code, but come on, how difficult can it be?

  • What it does:

Bound to C-tab. Allows me to switch intelligently from source to REPL. If I’m viewing anything that’s not the REPL window, stores a bookmark to the original buffer and switches to the REPL. From the REPL it switches back to the bookmarked buffer, if any.

  • The code I dumped into my ~/.emacs:
;;----- BEGIN slime switcher code 
(defun get-slime-buffer (&optional buflist)
  (when (null buflist) (setf buflist (buffer-list)))
  (if (equal 0 (length buflist)) nil
    (if (and (> (length (buffer-name (car buflist))) 10)
	     (string= "*slime-repl" (substring (buffer-name (car buflist)) 0 11 )))
	(car buflist)
      (get-slime-buffer (cdr buflist)))))

(defvar *buffer-bookmark* nil)

(defun switch-buffers ()
	(setf *temp-bookmark* (current-buffer))
    (switch-to-buffer *buffer-bookmark*)
    (setf *buffer-bookmark* *temp-bookmark*)
	(message (format "switching from %s to %s"
	(buffer-name *temp-bookmark*) 
	(buffer-name (current-buffer))))))

(defun switch-slime-buffer () (interactive)
 (let (
	(cur-buf (current-buffer))
	(slime-buf (get-slime-buffer)))
    (if (equal cur-buf slime-buf)
		(if (equal *buffer-bookmark* nil)
			(message "Don't know which buffer to switch to!")
      (if (equal slime-buf nil)
		(message "Can't find REPL buffer!")

(global-set-key (kbd "C-&#60tab>") 'switch-slime-buffer)

;;----- END slime switcher code