简体   繁体   中英

Emacs list file buffers in clickable text

I am new to emacs lisp. Today I want to write a emacs lisp function to list my opening files (that is buffer related to a file) and make them clickable, but i get question in understanding insert-button function.

Here is my code.

(require 'dash)
(require 'button)
(defun insert-button-for-buffer (buf)
 (insert-button (buffer-name buf)
            'action  (lambda (x) (display-buffer (get-buffer buf)))))
(-map 'insert-button-for-buffer
  (-filter (lambda (buf) (buffer-file-name buf))
           (buffer-list)))

this piece of code just don't work. I guess (display-buffer (get-buffer buf)). The variable in an lambda function just don't get the right value. I know the x argument in lambda in a Overlay.But how could I get buffer-name from x variable? Or is there a better way to achieve this goal? This quetion may seem silly. I hope you guys could help.

You fell for the good ol' lexical binding trap. Here's a fix:

(require 'button)
(defun insert-button-for-buffer (buf)
  (insert-button
   (buffer-name buf)
   'action
   `(lambda (x) (display-buffer ,(get-buffer buf))))
  (insert "\n"))
(mapc #'insert-button-for-buffer
      (cl-remove-if-not
       #'buffer-file-name
       (buffer-list)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM