Skip to content

Commit ba8a738

Browse files
Check for user interruptions via C-g
1 parent fa71f8e commit ba8a738

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Source/Swift/Defun.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ extension Environment {
9191
} catch (EmacsError.thrown(let tag, let value)) {
9292
env.throwForTag(tag, with: value)
9393

94+
} catch EmacsError.interrupted {
95+
// do nothing, just return
96+
9497
} catch {
9598
// As mentioned earlier, we cannot let any of the Swift exceptions to get
9699
// away or it'll crash Emacs.

Source/Swift/Error.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)