Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add widgets in Emacs dynamically?

I am a beginner at Emacs/Elisp. I want the following:

  • I want to place a dropdown menu
  • I want to fill it with values given to me in a list (a string as a label for it and a symbol as a value)
  • according to the selected list item I want to generate widgets in the same buffer
  • the widgets that should be generated (of course dynamically) are given as a symbol list, so a specific symbol means that a specific type of widget should be put to there
  • cause of the dynamic behaviour of my buffer it is also needed sometimes to remove the old widgets (in order to be able to add new ones)

How is this possible to do this? I was searching the Emacs docs but I found just some crumbs of information. Please include example code if possible.

like image 882
user1724641 Avatar asked Sep 19 '25 02:09

user1724641


1 Answers

IMHO you can find pretty good information here. As starting point this is an example of a dropdown menu I adapted from that manual:

(require 'widget)
(require 'wid-edit)

(defun widget-example ()
  "Create the widgets from the Widget manual."
  (interactive)
  (switch-to-buffer "*Example*")
  (kill-all-local-variables)
  (make-local-variable 'widget-example-repeat)
  (let ((inhibit-read-only t))
    (erase-buffer))
  (remove-overlays)
  (widget-create 'menu-choice
                 :value "Funny option"
                 :help-echo "Choose me, please!"
                 :notify (lambda (widget &rest ignore)
                           (message "%s is a good choice!"
                                    (widget-value widget)))
                 '(choice-item "Example option")
                 '(choice-item "Funny option")
                 '(choice-item "Another Example option"))
  (widget-insert "\n")
  (use-local-map widget-keymap)
  (widget-setup))
like image 111
juanleon Avatar answered Sep 21 '25 15:09

juanleon