CLIM mail archive


Sorting Command Menus

I think that a few weeks ago someone asked for a facility to sort the menu items 
of command tables, and furthermore, I believe that SWM answered that
there isn't any predefined CLIM function for this purpose.

However, I now have an application where there are command tables
with up to about 10  menu items in it.
>From the software ergonomics' point of view, it is absolutely necessary 
to have the possibility to group or sort items in a menu.

So I decided to write one myself, which I publish here for everyone.
Please note, though, that this code is NOT tested for all cases,
but instead only for the specific case of my applications; just take it
as a basis for your ideas.

(defun sort-command-table-menu-items (command-table sorting-predicate
				      &optional (sorting-key #'identity))
  "removes all menu items and re-inserts them in a sorted way"
  (let ((menu-items NIL))
      #'(lambda (menu-item-name keystroke type-and-value)
	  ;; create a list that can be used as argument list to add-menu-item-to-command-table
	  (push (list menu-item-name (first type-and-value) (second type-and-value)
		      :keystroke keystroke)
    (setq menu-items (sort menu-items sorting-predicate
			   :key #'(lambda (menu-item-description)
				    (funcall sorting-key (first menu-item-description)))))
    (dolist (menu-item-description menu-items)
      (remove-menu-item-from-command-table command-table (first menu-item-description))
      (apply 'add-menu-item-to-command-table command-table menu-item-description))))

an example call:

  (sort-command-table-menu-items 'foo 'string-lessp)

when foo is the name of a command table in the current package.

The code only works, if CLIM ensures that the order of command menu item adding
is also the order to appear in the menu. This behavior is not documented, but I think
it is rather obvious.

Markus Fischer
Consulting Services
Symbolics Systemhaus GmbH
Mergenthaler Allee 77-81
6236 Eschborn
West Germany
Tel (from the US): 011-6196-47220

Main Index | Thread Index