help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Incorporating new things into completions


From: Emanuel Berg
Subject: Re: Incorporating new things into completions
Date: Fri, 02 Feb 2024 07:48:08 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

Psionic K wrote:

> I want to augment `switch-to-buffer' candidates to contain
> a list of exotic fruits. Presumably the downstream handler
> for buffers has no idea what to do with fruits, so I need to
> provide some alternative handler.
>
> Has this infrastructure been built into Emacs or is it
> exclusive to completion packages at this time? If Emacs
> provides facilities for this kind of thing, what vocabulary
> should I become familiar with?

I have something to that extent, but instead of a list, it
matches a regexp. Don't know if this is already available
somewhere else but that wouldn't come as a surprise.

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/switch-to-buffer-regexp.el

(require 'cl-lib)
(require 'buc)

(defun switch-to-buffer-regexp (buf-re)
  (let*((ori-buf (buffer-name (current-buffer)))
        (hits    (cl-remove-if-not
                   (lambda (b) (string-match buf-re b)) (buffer-names)) ))
    (when hits
      (let ((new-hits (cl-remove ori-buf hits)))
        (when new-hits
          (when (string-match buf-re ori-buf)
            (bury-buffer ori-buf) )
          (switch-to-buffer (car new-hits)) )))))

;; try it!
;; (require 'gnus)
;; (dotimes (i 4) (gnus-post-news 'post "")) ; create 4 posts

(global-set-key "\C-cu" (lambda () (interactive) (switch-to-buffer-regexp 
"\*unsent posting\*.*")))
;; hit again to iterate

(provide 'switch-to-buffer-regexp)

Some more example use cases, some of which I use every day:

  https://dataswamp.org/~incal/emacs-init/switch-to-buffer.el

Ugh, for it to work this is needed as well from yet another file

(defun buffer-names ()
  "Get the names of all open buffers, as strings."
  (mapcar #'buffer-name (buffer-list)) )

https://dataswamp.org/~incal/emacs-init/buc.el

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]