Search code examples
recursionfactor-lang

How to interrupt a Recursive Word in the Listener


I was playing with file system change monitors and typed in this example from the Factor manual

: watch-loop ( monitor -- )
    dup next-change path>> print flush watch-loop ;

: watch-directory ( path -- )
    [ t [ watch-loop ] with-monitor ] with-monitors ;

then called it with

"/tmp" watch-directory

This works nicely, and file system changes start coming in. But I was not able to stop it. Ctrl-C doesn't seem to work in the Listener, and it even survived hitting the "Restart Listener" button. Is hitting the "End" button of the listener the only way to terminate the monitoring? I understand the monitor is handled by the with-monitor combinator, so maybe it's just that watch-loop is recursive?!


Solution

  • I stumbled over this recent thread on factor-talk (the Factor mailing list), and am cooking here up what I've gathered from there. If somebody has corrections or amendments please edit.

    1. Run the listener on the command line, not in the GUI: factor -run=listener. This one is supposed to react on Ctrl-C. This throws you onto a debugger shell where you can type t to cancel out with an exception. A problem here seems to be that Ctrl-C might be caught in a sub-thread of your code (transitively), interrupting the wrong thing.

    2. In the GUI, start a second listener. Then you can

      2.1. Suspend the first listener that runs the recursive word. (How?)

      2.1. Suggestions to register keyboard hooks seem not to work as control keys are not forwarded to the listener.