@@ -45,6 +45,13 @@ public enum EmacsError: Error {
4545 /// - tag: the symbol categorizing this exception.
4646 /// - value: supplementary value.
4747 case thrown( tag: EmacsValue , value: EmacsValue )
48+ /// Emacs user interruption (C-g).
49+ ///
50+ /// User can request execution interruption at any time, if you are trying to do something
51+ /// with environment after that, this exception will be thrown.
52+ ///
53+ /// It is recommended to stop any work as soon as possible upon receiving user interruptions.
54+ case interrupted
4855 /// If everything went to hell...
4956 case unknown
5057}
@@ -65,6 +72,10 @@ extension Environment {
6572 /// - Returns: the checked value if we encountered no errors.
6673 /// - Throws: an instance of `EmacsError` if Emacs was in error state.
6774 func check< T> ( _ result: T ) throws -> T {
75+ if ( interrupted ( ) ) {
76+ throw EmacsError . interrupted
77+ }
78+
6879 var symbolOrTag : emacs_value ?
6980 var dataOrValue : emacs_value ?
7081
@@ -112,4 +123,14 @@ extension Environment {
112123 func throwForTag( _ tag: EmacsValue , with value: EmacsValue ) {
113124 raw. pointee. non_local_exit_throw ( raw, tag. raw, value. raw)
114125 }
126+
127+ /// Checks whether the user interrupted execution of the current function.
128+ ///
129+ /// It is recommended to stop work as soon as possible, and check this condition
130+ /// in long-running tasks.
131+ ///
132+ /// - Returns: `true` if the environment received interruption signal from the user.
133+ public func interrupted( ) -> Bool {
134+ return raw. pointee. should_quit ( raw)
135+ }
115136}
0 commit comments