Package ibuffer per organizzazione i buffer

Questo pomeriggio stavo configurando ibuffer per organizzare meglio i buffer.

La config è la seguente

(use-package ibuffer
  :straight t
  :config
  ;; don't ask for confirmation of "dangerous" operations such as
  ;; deleting buffers
  (setq ibuffer-expert t)

  ;; define a group-organized view where buffers are organized into
  ;; groups depending on whether they match a given regex pattern or
  ;; not. This structure is dynamically modified by the function
  ;; 'work/compute-ibuffer-group' using information taken from the
  ;; list of currently active buffers.
  (setq default-ibuffer-saved-filter-groups
	(quote (("default"
		 ("org" (mode . org-mode))
		 ("chromium" (name . "^Chromium"))	       
		 ("vterminal" (name . "^\\*vterminal"))
		 ;;
		 ;; TODO: learn how to recognize when a buffer is an
		 ;; external application handled by EXWM
		 ;; 
		 ("exwm" (mode . exwm-mode))
		 ("erc" (mode . erc-mode))
		 ("emacs" (or
			   (name . "^\\*scratch\\*$")
			   (name . "^\\*Messages\\*$")))	       
		 ))))

  ;; items for each group are sorted alphabetically using the buffer name
  (setq ibuffer-default-sorting-mode 'alphabetic)

  ;; as soon as you enter or refresh ibuffer, switch to a
  ;; group-organized view using a group configuration computed on the
  ;; fly depending on currently open buffers.
  (add-hook 'ibuffer-hook
	    (lambda ()
	      (setq ibuffer-saved-filter-groups (home/compute-ibuffer-group))
	      (ibuffer-switch-to-saved-filter-groups "default"))
	    )
  )

Dove la funzione home/compute-ibuffer-group è definita nel seguente modo

(defun home/compute-ibuffer-group ()

  ;; list out the names of activity based on currently opened buffers
  (defun get-buffers ()
    (delete-dups
     (remove "" 
	     (remove nil
		     (mapcar
		      (lambda (buffer)
			(with-current-buffer buffer
			  (let* ((directory (split-string default-directory "/"))
				 (is-minded? (and (> (length directory) 6)
						  (string-equal (nth 4 directory) "PROGRAMMING"))))
			    (when is-minded?
			      (nth 5 directory)
			      )
			    )
			  )
			)
		      (buffer-list))
		     )
	     )
     ))

  ;; escape certain characters in order to have proper regex within
  ;; the group configuration
  ;; 
  (defun prepare-name (string)
    (concat ".*" (s-replace "+" "\\+" string) ".*")
    )

  ;; compute the new group filter by appending the new filters
  ;; computed based on activity names and the default filters saved
  ;; within the 'default-ibuffer-saved-filter-groups' variable.
  ;; 
  (let* ((new-filters (mapcar (lambda (x) `(,x (filename . ,(prepare-name x)))) (get-buffers)))
	 (default-filters (cdar default-ibuffer-saved-filter-groups))
	 (new-default-group `( ,(append '("default") new-filters default-filters))))
    new-default-group
    )
  )

Fatemi sapere cosa ne pensate!

1 Like

Un piccolo trick, anziché mettere (quote ...) potevi usare il “quasiquote” ` ovveronil backtick.
Ha la stessa funzione del quote normale ’ l’apice per intenderci, solo che puoi fare l’unquote dei termini nella lista prefissandoli con un comma ,

1 Like

Y lo conosco, in questo caso la config veniva con il quote quindi ho lasciato quello xD

Però si molto comodo anche il backtick.

Elisp sarà pure minimale, ma come tutti i lisp poi aggiunge una quantità di “zucchero sintattico” che ti permette di essere molto espressivo e di fare la stessa cosa in N modi diversi.