Bad-ptr / persp-mode.el

named perspectives(set of buffers/window configs) for emacs
401 stars 44 forks source link

with-persp-buffer-list changes buffer ordering #38

Open mz-pdm opened 8 years ago

mz-pdm commented 8 years ago

When I compare outputs of (buffer-list) and (with-persp-buffer-list () (buffer-list)), I can see that the buffer orderings in the results are different. In some cases it doesn't matter but in other cases, like when applying with-persp-buffer-list on (helm-buffer-list), it's very annoying. I can overcome it by defining my own function to restrict a buffer list to the current perspective, but it would be nice if with-persp-buffer-list retained the buffer order or, if it's not possible for some reason, another version of the macro with stable ordering was provided.

Bad-ptr commented 8 years ago

Yes, buffer ordering and perspectives ordering is something that I don't know how to do right. For now I have added the sortp (d041b33c18c73cb44d0b180f8830d867e8b7a915 and 6b82c31ee56a95e6aae5299c378980ecee7c6ce6) argument to the with-persp-buffer-list macro. It could be used like this:

(defvar persp-buffer-list-ordering-index nil)
(defun build-persp-buffer-list-ordering-index ()
  (setq persp-buffer-list-ordering-index (make-hash-table))
  (let ((n 0))
    (mapc #'(lambda (b)
              (puthash (buffer-name b) n persp-buffer-list-ordering-index)
              (setq n (1+ n)))
          (funcall persp-buffer-list-function))))

(defun persp-emacs-buffers-ordering-cmp (b bb)
  (unless persp-buffer-list-ordering-index
    (build-persp-buffer-list-ordering-index))
  (let ((b-i (gethash (buffer-name b) persp-buffer-list-ordering-index 1000000))
        (bb-i (gethash (buffer-name bb) persp-buffer-list-ordering-index 1000001)))
    (< b-i bb-i)))

(with-persp-buffer-list (:sortp #'persp-emacs-buffers-ordering-cmp)
                        (build-persp-buffer-list-ordering-index)
                        (buffer-list))

If you have any thoughts of how it could be implemented in a better way, please share.)

psdp commented 4 years ago

@Bad-ptr the above code doesn't seem to work now, getting error Invalid function: (function persp-emacs-buffers-ordering-cmp)