condition-case and unwind-protect are useful here. condition-case allows you to catch exceptions, of which quit is one:
(condition-case (while t) ; never terminates (quit (message "Cg was pressed")))
You can also catch other errors, for example "error".
unwind-protect as final; he will perform "body forms" and then "unwind forms". However, "unwinding forms" are executed regardless of whether the "body forms" were successfully executed:
(unwind-protect (while t) (message "Done with infinite loop"))
You want unwind-protect in your case.
jrockway
source share