The problem

Many useful emacs apps can only have one instance running at a time due to working with a fixed buffer name.

  • Examples of useful apps which you can only run one of
    • eww
    • Perl-REPL
    • dictionary-search
    • calculator

The fix: rename the buffer to tack on a hash after it is created

Create the advice function

(defun unique-buffer-generic-after-advice (&rest args)
  "Give the buffer a unique name"
  (ignore-errors (let* ((hash (short-hash (str (time-to-seconds))))
                        (new-buffer-name (pcre-replace-string "(\\*?)$" (concat "-" hash "\\1") (current-buffer-name))))
                   (rename-buffer new-buffer-name))))

Apply the function

(advice-add 'dictionary-search :after 'unique-buffer-generic-after-advice)
(advice-add 'calculator :after 'unique-buffer-generic-after-advice)

But things fell apart with the Perl-REPL buffer

perl-repl required me to specify with-current-buffer as it does some musical chairs with buffers.

Happily, it didn’t take much more work to get that going as well

I made a new advice function which uses the generic function above but adds some extra functionality.

(defun perl-repl-after-advice (&rest args)
  "Give the buffer a unique name"
  (with-current-buffer "*Perl-REPL*" (eval `(unique-buffer-generic-after-advice ,@args))
                       t))

(advice-add 'perl-repl :after 'perl-repl-after-advice)