1

I recently switched from ido to helm. Now I'm using helm-buffers-list to select buffers.

Additionally I want to create a customised helm-buffers-list function which allows me only to select from shell buffers; let's call this new function helm-shell-buffers-list. From earlier times where I used ido I still have a function populating a list with all shell buffers - how can I use this list to create the function helm-shell-buffers-list?

I looked at the source for helm-buffers-list:

(defun helm-buffers-list ()
  (interactive)
  (unless helm-source-buffers-list
    (setq helm-source-buffers-list
          (helm-make-source "Buffers" 'helm-source-buffers)))
  (helm :sources '(helm-source-buffers-list
                   helm-source-ido-virtual-buffers
                   helm-source-buffer-not-found)
        :buffer "*helm buffers*"
        :keymap helm-buffer-map
        :truncate-lines helm-buffers-truncate-lines))

This leads me to believe I have to provide a new class similar to helm-source-buffers:

(defclass helm-source-buffers (helm-source-sync helm-type-buffer)
  ((buffer-list
    :initarg :buffer-list
    :initform #'helm-buffer-list
    :custom function
    :documentation
    "  A function with no arguments to create buffer list.")
   (init :initform 'helm-buffers-list--init)
   (multimatch :initform nil)
   (match :initform 'helm-buffers-match-function)
   (persistent-action :initform 'helm-buffers-list-persistent-action)
   (keymap :initform helm-buffer-map)
   (migemo :initform 'nomultimatch)
   (volatile :initform t)
   (nohighlight :initform t)
   (resume :initform (lambda () (setq helm-buffers-in-project-p nil)))
   (help-message :initform 'helm-buffer-help-message)))

However in that class I have no idea where to provide the list with buffers...

Any help greatly appreciated!

1 Answer 1

2

Assuming "shell buffers" means its major-mode is shell-mode, you can supply "shell buffers" via :buffer-list argument while calling helm-make-source:

(defvar helm-source-shell-buffers-list
  (helm-make-source "Shell Buffers" 'helm-source-buffers
    :buffer-list
    (lambda ()
      (mapcar #'buffer-name
              (cl-remove-if-not
               (lambda (buf)
                 (with-current-buffer buf
                   (eq major-mode 'shell-mode)))
               (buffer-list))))))

(defun helm-shell-buffers-list ()
  (interactive)
  (helm :sources helm-source-shell-buffers-list
        :keymap helm-buffer-map
        :truncate-lines helm-buffers-truncate-lines))

BTW, you can filter buffer by their major mode in helm-buffers-list, for example, type "*shell" for all buffers in shell-mode.

2
  • Works great. Thanks. - The only strange thing is that the helm buffer (with the list of shell buffers) is not truncated. How can i make it trubcate the list? - helm-buffers-truncate-lines has no effect.
    – halloleo
    Commented Jan 16, 2018 at 7:24
  • @halloleo Add :truncate-lines helm-buffers-truncate-lines while calling helm. The answer is updated accordingly.
    – xuchunyang
    Commented Jan 16, 2018 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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