1:- module(pager, [with_pager/1]).
 with_pager(:Goal)
Send output of Goal through the user's preferred pager. The pager is only used if current_output is connected to a terminal. Otherwise, send output directly to current_output.
   10:- meta_predicate with_pager(0).   11with_pager(Goal) :-
   12    stream_property(current_output,tty(true)),
   13    !,
   14    setup_call_cleanup(
   15        with_pager_setup(PagerIn,Pid),
   16        with_output_to(PagerIn,Goal),
   17        with_pager_cleanup(PagerIn,Pid)
   18    ).
   19with_pager(Goal) :-
   20    call(Goal).
   21
   22
   23% set up a pager
   24with_pager_setup(PagerIn,Pid) :-
   25    once( getenv('PAGER',Pager)
   26        ; Pager=less
   27        ),
   28    process_create(
   29        path(Pager),
   30        [],
   31        [
   32            stdin(pipe(PagerIn)),
   33            stdout(std),
   34            process(Pid)
   35        ]
   36    ).
   37
   38% clean up after a pager
   39with_pager_cleanup(PagerIn,Pid) :-
   40    close(PagerIn),
   41    process_wait(Pid,_Status)