Function: easy-menu-get-map

easy-menu-get-map is a byte-compiled function defined in easymenu.el.gz.

Signature

(easy-menu-get-map MAP PATH &optional TO-MODIFY)

Documentation

Return a sparse keymap in which to add or remove an item.

MAP and PATH are as defined in easy-menu-add-item.

TO-MODIFY, if non-nil, is the name of the item the caller wants to modify in the map that we return. In some cases we use that to select between the local and global maps.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/easymenu.el.gz
(defun easy-menu-get-map (map path &optional to-modify)
  "Return a sparse keymap in which to add or remove an item.
MAP and PATH are as defined in `easy-menu-add-item'.

TO-MODIFY, if non-nil, is the name of the item the caller
wants to modify in the map that we return.
In some cases we use that to select between the local and global maps."
  (setq map
	(catch 'found
	  (if (and map (symbolp map) (not (keymapp map)))
	      (setq map (symbol-value map)))
	  (let ((maps (if map (if (keymapp map) (list map) map)
			(current-active-maps))))
	    ;; Look for PATH in each map.
	    (unless map (push 'menu-bar path))
	    (dolist (name path)
	      (setq maps
		    (delq nil (mapcar (lambda (map)
					(setq map (easy-menu-lookup-name
						   map name))
					(and (keymapp map) map))
				      maps))))

	    ;; Prefer a map that already contains the to-be-modified entry.
	    (when to-modify
	      (dolist (map maps)
		(when (easy-menu-lookup-name map to-modify)
		  (throw 'found map))))
	    ;; Use the first valid map.
	    (when maps (throw 'found (car maps)))

	    ;; Otherwise, make one up.
	    ;; Hardcoding current-local-map is lame, but it's difficult
	    ;; to know what the caller intended for us to do ;-(
	    (let* ((name (if path (format "%s" (car (reverse path)))))
		   (newmap (make-sparse-keymap name)))
	      (define-key (or map (current-local-map))
		(apply #'vector (mapcar #'easy-menu-intern path))
		(if name (cons name newmap) newmap))
	      newmap))))
  (or (keymapp map) (error "Malformed menu in easy-menu: (%s)" map))
  map)