Bad-ptr / persp-mode.el

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

[BUG?] persp-add-buffer-on-after-change-major-mode does not support function choice #147

Open sfavazza opened 10 months ago

sfavazza commented 10 months ago

Hello,

I wanted to create a function to add files to a perspective, only if belonging to a project or a list of projects. I thought that the best way to introduce this custom feature would have been persp-add-buffer-on-after-change-major-mode, as according to its documentation it would support a custom function.

Though in the implementation it does not look like it.

Is this intended? Am I missing something?

As a workaround I am currently implementing my functionality as a persp-add-buffer-on-after-change-major-mode-filter-functions function.

miguebox commented 8 months ago

I am facing this same issue @sfavazza .

As a workaround, instead of using persp-add-buffer-on-after-change-major-mode-filter-functions, which exlcudes buffers

Additional filters to know which buffers we dont want to add to the current perspective after the after-change-major-mode-hook is fired.

you can implement a hook similar to persp-after-change-major-mode-h, which is what persp-add-buffer-on-after-change-major-mode enables when set.

This is what I use:

(defcustom migue/persp-major-mode-change-functions nil
  "List of functions to run when changing major-modes. If they return true, add the buffer to the current
perspective."
  :group 'persp-mode
  :type 'hook
  )
(defun migue/persp-major-mode-check ()
  "Check functions in `migue/persp-major-mode-change-functions', if they return true, add them to the
current perspective."
  (let ((buf (current-buffer)))
    (when (cl-find-if (lambda (filter)
                        (if (functionp filter)
                            (funcall filter buf)
                          ))
                      migue/persp-major-mode-change-functions)
      (persp-add-buffer buf (get-current-persp) nil nil)
      )
    )
  )
(add-hook 'after-change-major-mode-hook #'migue/persp-major-mode-check)

It's all basically a stripped down version of persp-after-change-major-mode-h, like I said, and persp-buffer-filtered-out-p; but it allows you to choose what types of buffers you want on a case-by-case basis.

For instance, to add dired buffers:

(with-eval-after-load 'persp-mode
  (add-hook 'migue/persp-major-mode-change-functions
            #'(lambda (buffer) (equal (with-current-buffer buffer major-mode) 'dired-mode)))
  )

I don't think this would be too hard to implement, but my way is probably lacking in some way, which is why I haven't refined it and made a pull request :sweat_smile:.

Bad-ptr commented 2 months ago

must be fixed 84bc787