Skip to content

Instantly share code, notes, and snippets.

@ympbyc
Last active January 10, 2019 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ympbyc/fe275414afa15b2053ac139bd7bf9253 to your computer and use it in GitHub Desktop.
Save ympbyc/fe275414afa15b2053ac139bd7bf9253 to your computer and use it in GitHub Desktop.
Use restart mechanism to let user select from options.
(define-condition selection-required (simple-error)
((message :initarg :message :reader message))
(:report (lambda (c s) (format s "Selection required: ~A" (message c)))))
(defmacro selection-restarts (options message)
"Generates restart-case from given list of options. Useful for interactive selection."
(let ((x (gensym))
(rpt (gensym))
(i (gensym))
(name (gensym)))
`(eval
`(restart-case
(error 'selection-required :message ,,message)
,@(loop for ,x in ,options
for ,rpt = (format nil "~A" ,x)
for ,i from 0
for ,name = (intern (format nil "option-~A" ,i))
collect `(,,name () :report ,,rpt
(quote ,,x)))))))
;;Usage
(selection-restarts (mapcar #'identity (loop for x from 0 to 5 collect (* x 3)))
"Choose which one?")
Selection required: Please choose which value to use:
[Condition of type SELECTION-REQUIRED]
Restarts:
0: [option-0] 0
1: [option-1] 3
2: [option-2] 6
3: [option-3] 9
4: [option-4] 12
5: [option-5] 15
6: [RETRY] Retry SLIME REPL evaluation request.
7: [*ABORT] Return to SLIME's top level.
8: [ABORT] abort thread (#<THREAD "repl-thread" RUNNING {10045D8453}>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment