crux
crux copied to clipboard
crux-start-or-switch-to
When trying to use crux-start-or-switch-to with slime-connect function there is problem that slime-connect names its buffer according to the type of common lisp implementation that it connected to, so the resulting buffer can be named "slime-repl sbcl", "slime-repl ccl" etc. I slightly changed the function, adding a helper get-buffer-extended function:
(defun get-buffer-extended (buffer-name &optional match)
"Finds a buffer by its name. If the optional parameter MATCH is then call the standard
GET-BUFFER, if MATCH is :PREFIX, then it looks for buffers with names starting with BUFFER-NAME
and returns the first one that matches. When MATCH is :REGEXP does the same but BUFFER-NAME
is a regular expression."
(cond ((null match) (get-buffer buffer-name))
((eq match :regexp)
(catch 'done
(dolist (buffer (buffer-list))
(let ((name (buffer-name buffer)))
(when (string-match buffer-name name)
(throw 'done buffer))))))
((eq match :prefix)
(catch 'done
(dolist (buffer (buffer-list))
(let ((name (buffer-name buffer)))
(when (string-prefix-p buffer-name name)
(throw 'done buffer))))))))
(defun start-or-switch-to (function buffer-name &optional match)
"Modified crux-start-or-switch-to from https://github.com/bbatsov/crux/blob/master/crux.el
Add MATCH parameter that can be :prefix to find any buffer that starts with BUFFER-NAME or
:REGEXP to use regular expressions in the search string."
(let ((buf (get-buffer-extended buffer-name match)))
(if (not buf)
(progn
(split-window-sensibly (selected-window))
(other-window 1)
(funcall function))
(switch-to-buffer-other-window buf))))
There probably are other cases like this, where the names of the buffers can vary, so maybe such generalization may be of use.
Yeah, you're probably right. I'm a bit busy these days but I'll think a bit more about this next week.