Is it possible to provide an alias (e.g. m or mes) for a name like *Messages*?

Ideally, I'd like to be able to write some kind of hook that gets called when a buffer doesn't exist and resolves to another name. Transferring control to an arbitrary function lets the user write interesting behavior like bailing out when presented with an ambiguous abbreviation.

Strawman code:

(defun messages-abbrev (ignored-string)

(install-buffer-abbreviation '("m" "mes") #'messages-abbrev)

Ideally, I'd like something that works at a lower level than ido or helm or equivalent, so it can be composed with either of them or none of them. But it is also interesting if those libraries provide a solution to this problem.

An emacs configuration option that globally enables some kind of fuzzy buffer resolution would also be neat if such a thing exists.

  • 1
    It's not clear to me what you're asking, e.g., how you would use your install-buffer-abbreviation, if it were defined. (BTW, you need to close your first string.) Please consider describing the actual problem that you want to solve, more clearly. – Drew Nov 15 '18 at 23:20
  • @Drew I want to be able to navigate to predictably-named buffers *Messages* *eshell*, *eshell*<2>, *shell*, *shell2* using switch-to-buffer or list-buffers or similar with a short name that's customizable without affecting how the name is displayed. I like the *name* convention for non-file buffers. As a stretch goal, I'd also like to be able to navigate to associated files (e.g. C-x b .h C-m for navigating to the corresponding header file). I guess I'm just wondering what hooks are available for making a file-navigation DSL. There might not be any. It is a strange question. – Gregory Nisbet Nov 15 '18 at 23:44

I'm not sure that I recommend this (purely on the basis that messing with such a low-level routine as get-buffer seems like it has the potential to go fairly wrong -- at minimum I recommend that you experiment in a separate instance of Emacs), however testing suggests that you can do something like this:

(defadvice get-buffer (after my-get-buffer-alias)
  "Look for a buffer name alias, if no buffer was found."
  (unless ad-return-value
    (when (stringp (ad-get-arg 0))
      (setq ad-return-value
            (my-get-buffer-alias (ad-get-arg 0))))))

(ad-activate 'get-buffer)

(defun my-get-buffer-alias (alias)
  "Return a buffer for ALIAS, or nil."
  (unless (bound-and-true-p my-get-buffer-alias)
    (let ((my-get-buffer-alias t))
      (get-buffer ...))))

Then implement my-get-buffer-alias using whatever logic you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.