Stops
Better Guards in Emacs Lisp
Stops provides two small guard macros for Emacs Lisp:
stops-if!signals an error when a condition is non-nil.stops-if-not!signals an error when a condition is nil.
Both macros return t when the guard passes. Both accept :error-type and
:error-message keyword arguments. The error message may be a literal string
or any Emacs Lisp expression that evaluates to a string.
Installation
Clone the repository or download stops.el and place it in your Emacs
load-path. Then add this to your Emacs configuration:
(require 'stops)
Quick Start
Use stops-if! when a true condition should stop execution.
(defun divide (x y)
(stops-if! (zerop y)
:error-message (format "Cannot divide %S by zero" x))
(/ x y))
Use stops-if-not! when a false condition should stop execution.
(defun first-item (xs)
(stops-if-not! (consp xs)
:error-message (format "Expected a non-empty list, got %S" xs))
(car xs))
When no error message is provided, Stops creates a message from the guard condition.
(stops-if-not! (integerp x))
This signals an error with a message like:
stops-if-not!: condition was nil: (integerp x)
Formatted Messages
Stops does not need a separate formatting keyword. Because :error-message is
ordinary Emacs Lisp, pass a call to format when the message should include
runtime values.
(defun require-user-id (user-id)
(stops-if-not! (natnump user-id)
:error-type 'wrong-type-argument
:error-message (format "Expected a natural-number user id, got %S" user-id))
user-id)
The message expression is evaluated only when the guard fails. In this example,
format is not called when user-id is a natural number.
(defun lookup-user (user-id users)
(stops-if-not! (hash-table-p users)
:error-message (format "Expected a hash table, got %S" users))
(stops-if-not! (gethash user-id users)
:error-message (format "No user found for id %S" user-id))
(gethash user-id users))
Literal strings still work when no formatting is needed.
(stops-if! (zerop y) :error-message "Division by zero")
Reference
stops-if!
(stops-if! CONDITION &key ERROR-TYPE ERROR-MESSAGE)
Signal an error if CONDITION is non-nil. Return t otherwise.
ERROR-TYPE defaults to error. ERROR-MESSAGE, when non-nil, is used as the
error message. It may be a literal string or an expression that evaluates to a
string.
(stops-if! (member user blocked-users) :error-type 'user-error :error-message (format "User %S is blocked" user))
If ERROR-MESSAGE is omitted, Stops generates a message from CONDITION.
(stops-if! (member user blocked-users))
This signals an error with a message like:
stops-if!: condition was non-nil: (member user blocked-users)
stops-if-not!
(stops-if-not! CONDITION &key ERROR-TYPE ERROR-MESSAGE)
Signal an error if CONDITION is nil. Return t otherwise.
ERROR-TYPE defaults to error. ERROR-MESSAGE, when non-nil, is used as the
error message. It may be a literal string or an expression that evaluates to a
string.
(stops-if-not! (natnump x) :error-type 'wrong-type-argument :error-message (format "Expected a natural number, got %S" x))
If ERROR-MESSAGE is omitted, Stops generates a message from CONDITION.
(stops-if-not! (natnump x))
This signals an error with a message like:
stops-if-not!: condition was nil: (natnump x)
Development
Run the test suite with:
make test
Run byte-compilation, tests, and package linting with:
make check
Build this documentation site with:
make docs
The generated site is written to public/. The build-main workflow uploads
that directory as a GitHub Pages artifact.
License
Stops is released under the MIT License.