Function: easy-menu-convert-item-1

easy-menu-convert-item-1 is a byte-compiled function defined in easymenu.el.gz.

Signature

(easy-menu-convert-item-1 ITEM)

Documentation

Parse an item description and convert it to a menu keymap element.

ITEM defines an item as in easy-menu-define.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/easymenu.el.gz
(defun easy-menu-convert-item-1 (item)
  "Parse an item description and convert it to a menu keymap element.
ITEM defines an item as in `easy-menu-define'."
  (let (name command label prop remove)
    (cond
     ((stringp item)			; An item or separator.
      (setq label item))
     ((consp item)			; A sub-menu
      (setq label (setq name (car item)))
      (setq command (cdr item))
      (if (not (keymapp command))
	  (setq command (easy-menu-create-menu name command)))
      (if (null command)
	  ;; Invisible menu item. Don't insert into keymap.
	  (setq remove t)
	(when (and (symbolp command) (setq prop (get command 'menu-prop)))
	  (when (eq :label (car prop))
	    (setq label (cadr prop))
	    (setq prop (cddr prop)))
	  (setq command (symbol-function command)))))
     ((vectorp item)			; An item.
      (let* ((ilen (length item))
	     (active (if (> ilen 2) (or (aref item 2) ''nil) t))
	     (no-name (not (symbolp (setq command (aref item 1)))))
	     cache cache-specified)
	(setq label (setq name (aref item 0)))
	(if no-name (setq command (easy-menu-make-symbol command)))
	(if (keywordp active)
	    (let ((count 2)
		  keyword arg suffix visible style selected keys)
	      (setq active nil)
	      (while (> ilen count)
		(setq keyword (aref item count))
		(setq arg (aref item (1+ count)))
		(setq count (+ 2 count))
		(pcase keyword
                  ((or :included :visible) (setq visible (or arg ''nil)))
                  (:key-sequence (setq cache arg cache-specified t))
                  (:keys (setq keys arg no-name nil))
                  (:label (setq label arg))
                  ((or :active :enable) (setq active (or arg ''nil)))
                  (:help (setq prop (cons :help (cons arg prop))))
                  (:suffix (setq suffix arg))
                  (:style (setq style arg))
                  (:selected (setq selected (or arg ''nil)))))
	      (if suffix
		  (setq label
			(if (stringp suffix)
			    (if (stringp label) (concat label " " suffix)
			      `(concat ,label ,(concat " " suffix)))
			  (if (stringp label)
			      `(concat ,(concat label " ") ,suffix)
			    `(concat ,label " " ,suffix)))))
	      (cond
	       ((eq style 'button)
		(setq label (if (stringp label) (concat "[" label "]")
			      `(concat "[" ,label "]"))))
	       ((and selected
		     (setq style (assq style easy-menu-button-prefix)))
		(setq prop (cons :button
				 (cons (cons (cdr style) selected) prop)))))
	      (when (stringp keys)
                (if (string-match "^[^\\]*\\(\\\\\\[\\([^]]+\\)]\\)[^\\]*$"
                                  keys)
                    (let ((prefix
                           (if (< (match-beginning 0) (match-beginning 1))
                               (substring keys 0 (match-beginning 1))))
                          (postfix
                           (if (< (match-end 1) (match-end 0))
                               (substring keys (match-end 1))))
                          (cmd (intern (match-string 2 keys))))
                      (setq keys (and (or prefix postfix)
                                      (cons prefix postfix)))
                      (setq keys
                            (and (or keys (not (eq command cmd)))
                                 (cons cmd keys))))
                  (setq cache-specified nil))
                (if keys (setq prop (cons :keys (cons keys prop)))))
	      (if (and visible (not (easy-menu-always-true-p visible)))
		  (if (equal visible ''nil)
		      ;; Invisible menu item. Don't insert into keymap.
		      (setq remove t)
		    (setq prop (cons :visible (cons visible prop)))))))
	(if (and active (not (easy-menu-always-true-p active)))
	    (setq prop (cons :enable (cons active prop))))
	(if (and (or no-name cache-specified)
		 (or (null cache) (stringp cache) (vectorp cache)))
	    (setq prop (cons :key-sequence (cons cache prop))))))
     (t (error "Invalid menu item in easymenu")))
    ;; `intern' the name so as to merge multiple entries with the same name.
    ;; It also makes it easier/possible to lookup/change menu bindings
    ;; via keymap functions.
    (let ((key (easy-menu-intern name)))
      (cons key
            (and (not remove)
                 (if (and (stringp label)
                          (seq-every-p (lambda (c) (char-equal c ?-)) label))
                     menu-bar-separator
                   (cons 'menu-item
                         (cons label
                               (and name
                                    (cons command prop))))))))))