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

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

Notes: Auto-Generation of Sub-Menu Items (Keys) from a list of lists


From: Nordlöw
Subject: Notes: Auto-Generation of Sub-Menu Items (Keys) from a list of lists
Date: Wed, 17 Sep 2008 04:21:22 -0700 (PDT)
User-agent: G2/1.0

On 17 Sep, 12:41, Nordlöw <per.nord...@gmail.com> wrote:
> I am trying to automatically create a Sub-menu to "C++"-menu from the
> list c++-iostream-objects in the following way:
>
> (defvar c++-iostream-objects
>   '(
>     ("cin" [cin] "Standard input stream." 'object)
>     ("cout" [cout] "Standard output stream." 'object)
>     ("cerr" [cerr] "Standard output stream for errors." 'object)
>     ("clog" [clog] "Standard output stream for logging." 'object)
>     )
>   "List of C++ IOstream Objects.")
>
> (defun generate-keymap-menu (keymap-menu-name insert-prefix alist)
>   (let ((m (make-sparse-keymap keymap-menu-name)))
>     (dolist (elm (reverse alist))
>       (let* ((str (concat insert-prefix (elt elm 0)))
>              (key (elt elm 1))
>              (doc (elt elm 2))
>              )
>         (define-key m key `(menu-item ,str '(lambda () (interactive)
> (insert ,str)) :help ,doc))))
>     m))
>
> (defvar c++-iostream-objects-menu)
> (setq c++-iostream-objects-menu
>       (generate-keymap-menu "IOstream Objects" "std::" c++-iostream-
> objects))
>
> (add-hook 'c++-mode-hook
>           '(lambda () (define-key-after c-c++-menu [iostream-objects]
>                         (cons "IOstream Objects" c++-iostream-objects-
> menu) t))
>           t)
>
> The value of c++-iostream-objects-menu after evaluating the above is:
>
> (keymap
>  (cin menu-item "std::cin"
>       '(lambda nil
>          (interactive)
>          (insert "std::cin"))
>       :help "Standard input stream.")
>  (cout menu-item "std::cout"
>        '(lambda nil
>           (interactive)
>           (insert "std::cout"))
>        :help "Standard output stream.")
>  (cerr menu-item "std::cerr"
>        '(lambda nil
>           (interactive)
>           (insert "std::cerr"))
>        :help "Standard output stream for errors.")
>  (clog menu-item "std::clog"
>        '(lambda nil
>           (interactive)
>           (insert "std::clog"))
>        :help "Standard output stream for logging.")
>  "IOstream Objects")
>
> and the sub-menu elements gets created but when I press on one of them
> I get the error:
>
> Debugger entered--Lisp error: (wrong-type-argument commandp (quote
> (lambda nil (interactive) (insert "std::cin"))))
>   call-interactively((quote (lambda nil (interactive) (insert
> "std::cin"))) nil nil)
>
> This is really strange since the evaluation of
>
> (call-interactively (quote (lambda nil (interactive) (insert
> "std::cin"))))
>
> works correctly and
>
> (commandp (quote (lambda nil (interactive) (insert "std::cin"))))
>
> evaluates to t.
>
> What on earth have I missed? This nut is really hard to crack!
>
> Many thanks in advance,
> Nordlöw

First Note: All of the above code snippets can be evaluated in Vanilla
Emacs if you want to test or integrate these concepts into other Emacs
add-ons.
Second Note: The fourth element 'object is currently not used for
anything.

/Nordlöw


reply via email to

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