Hi everybody,
I'd like to know if it's possible to implement a kind of menu in
lisp, something like this :
1 --> choice 1
2 --> choice 2
3 --> choice 3
What is your choice ?
If it's possible, what are the fonctions I have to use ?
Thanks.
Jybo.
Sent via Deja.com http://www.deja.com/
Before you buy.
In article <············@nnrp1.deja.com>, Jybo. <····@netcourrier.com>
wrote:
> Hi everybody,
>
> I'd like to know if it's possible to implement a kind of menu in
> lisp, something like this :
Sure.
> 1 --> choice 1
> 2 --> choice 2
> 3 --> choice 3
> What is your choice ?
>
> If it's possible, what are the fonctions I have to use ?
Do you want a graphical menu (2d or 3d) or
just a simple textual menu?
A simple textual version using CLIM (Common Lisp Interface Manager):
(defun choose-one (&rest items)
(clim:accept `(clim:member-sequence ,items)))
A more graphical version:
(defun choose-one (&rest items)
(clim:accepting-values (*standard-output*)
(clim:accept `(clim:member-sequence ,items)
:view clim:+radio-box-view+
:stream *standard-output*)))
--
Rainer Joswig, Hamburg, Germany
Email: ·············@corporate-world.lisp.de
Web: http://corporate-world.lisp.de/
Rainer Joswig <······@corporate-world.lisp.de> writes:
> In article <············@nnrp1.deja.com>, Jybo. <····@netcourrier.com>
> wrote:
>
> > Hi everybody,
> >
> > I'd like to know if it's possible to implement a kind of menu in
> > lisp, something like this :
>
> Sure.
>
> > 1 --> choice 1
> > 2 --> choice 2
> > 3 --> choice 3
> > What is your choice ?
> >
> > If it's possible, what are the fonctions I have to use ?
>
> Do you want a graphical menu (2d or 3d) or
> just a simple textual menu?
>
> A simple textual version using CLIM (Common Lisp Interface Manager):
>
> (defun choose-one (&rest items)
> (clim:accept `(clim:member-sequence ,items)))
>
> A more graphical version:
>
> (defun choose-one (&rest items)
> (clim:accepting-values (*standard-output*)
> (clim:accept `(clim:member-sequence ,items)
> :view clim:+radio-box-view+
> :stream *standard-output*)))
Or without CLIM, something like:
(DEFUN CHOOSE-FROM (ITEMS)
;; Returns two values: (1) the choice and (2) whether the choice is valid.
;; In the case that ITEMS is NIL, two NILs are returned, to
;; distinguish from a case where NIL was among the ITEMS and selected,
;; in which case NIL, T is returned.
(COND ((NOT ITEMS) (RETURN-FROM CHOOSE-FROM (VALUES NIL NIL)))
((NOT (REST ITEMS))
(RETURN-FROM CHOOSE-FROM (VALUES (FIRST ITEMS) T))))
(LET ((*READ-EVAL* NIL))
(FLET ((SHOW-MENU ()
(LOOP FOR I FROM 1
FOR ITEM IN ITEMS
DO (FORMAT T "~&~D: ~A~%"))))
(SHOW-MENU)
(LOOP WITH N = (LENGTH ITEMS)
FOR CHOICE = (PROGN (FORMAT T "~&Choice: ")
(FORCE-OUTPUT)
(READ))
DO (COND ((NOT (INTEGERP CHOICE))
(FORMAT T "~&Please type a number.~%"))
((OR (NOT (PLUSP CHOICE)) (> CHOICE N))
(FORMAT T "~&Try a number between 1 and ~D." N))
(T
(RETURN (VALUES
(NTH ;; People count from 1, machines from 0
(- CHOICE 1)
ITEMS)
T))))
(SHOW-MENU)))))
No, I didn't test this code. But it's probably at least close to right.
Caveat emptor.
Of course, one could just do
(DEFUN CHOOSE-FROM (ITEMS)
(LOOP FOR I FROM 1 FOR ITEM IN ITEMS DO (PRINT (LIST I ITEM)))
(PRINT 'CHOICE)
(NTH (1- (READ)) ITEMS))
if you prefer less code and less error checking.
No, I didn't test that one either.
Kent M Pitman <······@world.std.com> writes:
Thomas Russ spotted this typo in my code, btw:
> (FLET ((SHOW-MENU ()
> (LOOP FOR I FROM 1
> FOR ITEM IN ITEMS
;; Replace: DO (FORMAT T "~&~D: ~A~%"))))
;; With:
DO (FORMAT T "~&~D: ~A~%" I ITEM))))
And if you don't have CLIM, try the graphical stuff you, can find on the clx
help, or we did it in a first window manager version (that I'll try to post
soon)
http://www.emi.u-bordeaux.fr/~boninfan/TER.
Otherwise, I'm actually working on the Free-CLIM menu, so it may interrest you.
I guess it will be available, in one or two weeks.