An open API service indexing awesome lists of open source software.

https://github.com/ailisp/exit-hooks

Call functions automatically when Common Lisp exit
https://github.com/ailisp/exit-hooks

common-lisp portable utility

Last synced: 3 months ago
JSON representation

Call functions automatically when Common Lisp exit

Awesome Lists containing this project

README

        

* exit-hooks
exit-hooks provide a portable way to automatically call some user defined function when exit Common Lisp (Both ~(quit)~ from REPL or kill in shell). Like ~atexit~ in C and Python or Java's ~Runtime.addShutdownHook()~. It currently support SBCL, CCL, ECL, ABCL, Allegro CL, clisp and CMUCL. Before exit-hooks, there is no portable way to doing so and no staightforward way to use an exit hook on ABCL. It can be used for tasks like parmenantly save something when exit Lisp.

** Installation
#+BEGIN_SRC lisp
(ql:quickload :exit-hooks)

#+END_SRC

** Usage
To add a function FUNC called when Common Lisp exits:
#+BEGIN_SRC lisp
(exit-hooks:add-exit-hook #'func)
#+END_SRC
The function FUNC must be of zero arguments. If several functions are added this way, their calling sequence is not specified. If you want to call them in a specific sequence, you can simply wrap them into a single function and add it.

To visit current exit hooks or delete one, you can visit or delete elements from variable ~exit-hooks:*exit-hooks*~, it's a list contains current exit hooks. But you should not add exit hooks by adding elements to ~*exit-hooks*~, use ~add-exit-hook~ only, because elements in ~*exit-hooks*~ is implementation specific.

** Acknowledgements
Thanks to Vibhu Mohindra from ABCL mail list and R. Matthew Emerson from OpenMCL mail list. They provide methods for exit-hooks for ABCL and CCL. And Jocelyn Fréchot gives many useful suggestions and support for clisp, Allegro CL and CMUCL.

** Limitations
For some implementation, for example SBCL, when you are using SLIME, you need to exit Lisp in ~*inferior-lisp*~, not ~*slime-repl*~. If you directly run Lisp from shell, not in SLIME, there is no problem. Currently exit-hooks support most of popular Lisp implementations, but not all. If your implementation is not supported and you know how to do that, please tell me. Your work maybe helpful to other fellow Lispers. And this Library only provide a calling function when exit Lisp. Previous version of exit-hooks provide ability to add exit hooks with calling priority, but this can be easily done by wrap several functions into a single one and most people only want a portable layer so this is removed. If you want something to deal all kinds of OS signal handling capability you may have a look at [[https://github.com/guicho271828/trivial-signal][trivial-signal]].