home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / p / plbin.zip / pl / library / help.pl < prev    next >
Text File  |  1992-05-26  |  4KB  |  181 lines

  1. /*  help.pl,v 1.1.1.1 1992/05/26 11:51:36 jan Exp
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     jan@swi.psy.uva.nl
  5.  
  6.     Purpose: Give online help
  7. */
  8.  
  9. :- module(online_help,
  10.     [ help/1
  11.     , help/0
  12.     , apropos/1
  13.     ]).
  14.  
  15. :- use_module(library(help_index)).
  16.  
  17. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  18. This module  defines the  online  help  facility of   SWI-Prolog.   It
  19. assumes  (Prolog) index file  at library(help_index)   and  the actual
  20. manual  at library(online_manual).   Output  is piped through  a  user
  21. defined pager, which defaults to `more'.
  22.  
  23. BUGS:
  24. If the pager  is quit prematurely Prolog will  abort  on  noticing the
  25. broken pipe.
  26. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  27.  
  28. %    help/0
  29.  
  30. help :-
  31.     help(help/1).
  32.  
  33. %    help(+Subject)
  34. %    Display online help on specified subject.
  35.  
  36. help(What) :-
  37.     give_help(What).
  38.  
  39. %    apropos(Pattern)
  40. %    Give a list of subjects that might be appropriate.
  41.  
  42. apropos(What) :-
  43.     give_apropos(What).
  44.  
  45. give_help(Name/Arity) :- !,
  46.     predicate(Name, Arity, _, From, To), !,
  47.     show_help([From-To]).
  48. give_help(Section) :-
  49.     user_index(Index, Section), !,
  50.     section(Index, _, From, To),
  51.     show_help([From-To]).
  52. give_help(Function) :-
  53.     atom(Function),
  54.     concat('PL_', _, Function),
  55.     function(Function, From, To), !,
  56.     show_help([From-To]).
  57. give_help(Name) :-
  58.     findall(From-To, predicate(Name, _, _, From, To), Ranges),
  59.     Ranges \== [], !,
  60.     show_help(Ranges).
  61. give_help(What) :-
  62.     format('No help available for ~w~n', What).
  63.  
  64. %    show_help(+ListOfRanges)
  65. %    Pipe specified ranges of the manual through the user defined pager
  66.  
  67. show_help([Start-End]) :-
  68.     End - Start > 4000, !,
  69.     find_manual(Manual),
  70.     find_pager(Pager),
  71.     sformat(Cmd, 'pl-bite ~d:~d ~a | ~a', [Start, End, Manual, Pager]),
  72.     shell(Cmd).    
  73. show_help(Ranges) :-
  74.     online_manual_stream(Manual),
  75.     pager_stream(Pager),
  76.     show_ranges(Ranges, Manual, Pager),
  77.     close(Manual),
  78.     close(Pager).
  79.  
  80. show_ranges([], _, _) :- !.
  81. show_ranges([From-To|Rest], Manual, Pager) :-
  82.     stream_position(Manual, _, '$stream_position'(From, 0, 0)),
  83.     Range is To - From,
  84.     copy_chars(Range, Manual, Pager),
  85.     nl(Pager),
  86.     show_ranges(Rest, Manual, Pager).
  87.  
  88. copy_chars(0, _, _) :- !.
  89. copy_chars(N, _, To) :-
  90.     0 =:= N mod 4096,
  91.     flush_output(To),
  92.     fail.
  93. copy_chars(N, From, To) :-
  94.     get0(From, C),
  95.     put_printable(To, C),
  96.     NN is N - 1,
  97.     copy_chars(NN, From, To).
  98.  
  99. put_printable(_, 12) :- !.
  100. put_printable(_, -1) :- !.
  101. put_printable(To, C) :-
  102.     put(To, C).
  103.  
  104. online_manual_stream(Stream) :-
  105.     find_manual(Manual),
  106.     open(Manual, read, Stream).
  107.  
  108. pager_stream(Stream) :-
  109.     find_pager(Pager),
  110.     open(pipe(Pager), write, Stream).
  111.  
  112. find_manual(Path) :-
  113.     '$chk_file'(library('MANUAL'), Path, [''], ['']).
  114.  
  115. find_pager(Pager) :-
  116.     getenv('PAGER', Pager), !.
  117. find_pager(more).
  118.  
  119. %    APROPOS
  120.  
  121. give_apropos(Atom) :-
  122.     ignore(predicate_apropos(Atom)),
  123.     ignore(function_apropos(Atom)),
  124.     ignore(section_apropos(Atom)).
  125.  
  126. apropos_predicate(Pattern, Name, Arity, Summary) :-
  127.     predicate(Name, Arity, Summary, _, _),
  128.     (   apropos_match(Pattern, Name)
  129.     ->  true
  130.     ;   apropos_match(Pattern, Summary)
  131.     ).
  132.  
  133. predicate_apropos(Pattern) :-
  134.     findall(Name-Arity-Summary,
  135.         apropos_predicate(Pattern, Name, Arity, Summary),
  136.         Names),
  137.     Names \== [],
  138.     forall(member(Name-Arity-Summary, Names),
  139.             format('~w/~w~t~22|~w~n', [Name, Arity, Summary])).
  140.  
  141. function_apropos(Pattern) :-
  142.     findall(Name, (function(Name, _, _),
  143.                apropos_match(Pattern, Name)), Names),
  144.     Names \== [],
  145.     forall(member(Name, Names),
  146.             format('Interface Function~t~22|~w()~n', Name)).
  147.  
  148. section_apropos(Pattern) :-
  149.     findall(Index-Name, (section(Index, Name, _, _),
  150.                apropos_match(Pattern, Name)), Names),
  151.     Names \== [],
  152.     forall(member(Index-Name, Names),
  153.             (user_index(Index, UserIndex),
  154.             format('Section ~w~t~22|"~w"~n', [UserIndex, Name]))).
  155.  
  156. apropos_match(A, B) :-
  157.     '$apropos_match'(A, B).            % C defined for performance
  158.  
  159. user_index(List, Index) :-
  160.     is_list(List), !,
  161.     to_user_index(List, S),
  162.     name(Index, S).
  163. user_index(List, Index) :-
  164.     to_system_index(Index, List).
  165.  
  166. to_user_index([], "").
  167. to_user_index([A], S) :- !,
  168.     name(A, S).
  169. to_user_index([A|B], S) :-
  170.     name(A, S0),
  171.     append(S0, "-", S1),
  172.     append(S1, Rest, S),
  173.     to_user_index(B, Rest).
  174.  
  175. to_system_index(A-B, I) :- !,
  176.     to_system_index(A, C),
  177.     integer(B),
  178.     append(C, [B], I).
  179. to_system_index(A, [A]) :-
  180.     integer(A).
  181.