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 / boot / edit.pl < prev    next >
Text File  |  1993-02-17  |  4KB  |  164 lines

  1. /*  edit.pl,v 1.3 1993/02/17 12:45:46 jan Exp
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     jan@swi.psy.uva.nl
  5.  
  6.     Purpose: editor interface
  7. */
  8.  
  9. :- module($edit,
  10.     [ edit/1
  11.     , edit/0
  12.     , ed/1
  13.     , ed/0
  14.     , make/0
  15.     ]).
  16.  
  17. :- user:dynamic
  18.     edit_source/1.
  19. :- user:multifile
  20.     edit_source/1.
  21.  
  22. :- module_transparent
  23.     edit/1, 
  24.     ed/1.
  25.  
  26. edit(File) :-
  27.     $break($check_file(File, _)), !, 
  28.     $record_last($edit_file, File), 
  29.     $edit_load(File).
  30. edit(File) :-
  31.     $confirm('Edit new file ~w', [File]),
  32.     $record_last($edit_file, File), 
  33.     $edit(File).
  34.  
  35. edit :-
  36.     recorded($edit_file, File), 
  37.     $confirm('Edit file `~w''', [File]), !,
  38.     edit(File).
  39.  
  40. ed(Spec) :-
  41.     $find_predicate(Spec, Preds),
  42.     (   Preds = [Head]
  43.     ;   member(Head, Preds), 
  44.         $predicate_name(Head, PredName),
  45.         $confirm('Edit predicate `~w''', [PredName])
  46.     ), !, 
  47.     source_file(Head, File), 
  48.     $strip_module(Head, Module, Term), 
  49.     functor(Term, Name, Arity), 
  50.     $record_last($edit_predicate, Module:Name/Arity), 
  51.     $edit_load(File:Name/Arity).
  52.  
  53. ed :-
  54.     $module(TypeIn, TypeIn), 
  55.     recorded($edit_predicate, TypeIn:Name/Arity), !, 
  56.     $confirm('Edit predicate `~w/~w''', [Name, Arity]), !, 
  57.     ed(TypeIn:Name/Arity).
  58. ed :-
  59.     recorded($edit_predicate, Module:Name/Arity), !, 
  60.     $confirm('Edit predicate `~w:~w/~w''', [Module, Name, Arity]), !, 
  61.     ed(Module:Name/Arity).
  62. ed :-
  63.     $break($warning('ed/0: You can only use ed/0 after ed/1!')).
  64.  
  65. $record_last(Key, Term) :-
  66.     recorded(Key, Last) -> 
  67.     Last = Term, !.
  68. $record_last(Key, Term) :-
  69.     recorda(Key, Term).
  70.  
  71. $edit_load(File:Predicate) :-
  72.     $check_file(File, Path), 
  73.     $edit(Path:Predicate), !.
  74. $edit_load(File) :-
  75.     File \= _:_,
  76.     $check_file(File, Path), 
  77.     $edit(Path), !.
  78. $edit_load(_, _).
  79.  
  80. $edit(Spec) :-
  81.     user:edit_source(Spec), !.
  82. $edit(File:Name/_Arity) :- !,
  83.     (   getenv('EDITOR', Editor)
  84.     ;   $default_editor(Editor)
  85.     ) ->
  86.     edit_command(Editor, File, Name, Command), !, 
  87.     shell(Command),
  88.     make.
  89. $edit(File) :-
  90.     (   getenv('EDITOR', Editor)
  91.     ;   $default_editor(Editor)
  92.     ) ->
  93.     edit_command(Editor, File, $nopredicate, Command), !, 
  94.     shell(Command),
  95.     make.
  96.  
  97. edit_command(Editor, File, $nopredicate, Command) :-
  98.     $file_base_name(Editor, Base),
  99.     (   edit_command(Base, nosearch, Cmd)
  100.     ->  name(Cmd, S0),
  101.         substitute("%e", Editor, S0, S1),
  102.         substitute("%f", File, S1, S2),
  103.         name(Command, S2)
  104.     ;   $warning('Don''t know how to use editor `~w''', [Editor])
  105.     ).
  106. edit_command(Editor, File, Pred, Command) :-
  107.     $file_base_name(Editor, Base),
  108.     (   edit_command(Base, search, Cmd)
  109.     ->  name(Cmd, S0),
  110.         substitute("%e", Editor, S0, S1),
  111.         substitute("%f", File, S1, S2),
  112.         substitute("%s", Pred, S2, S3),
  113.         name(Command, S3)
  114.     ;   $warning('Don''t know how to use editor `~w''', [Editor])
  115.     ).
  116.  
  117. edit_command(top, search,   '%e ''%f'' ''-^%s''').
  118. edit_command(top, nosearch, '%e ''%f''').
  119. edit_command(vi,  search,   '%e ''+/^%s'' ''%f''').
  120. edit_command(vi,  nosearch, '%e ''%f''').
  121.  
  122. substitute(From, ToAtom, Old, New) :-
  123.     name(ToAtom, To),
  124.     append(Pre, S0, Old),
  125.     append(From, Post, S0) ->
  126.     append(Pre, To, S1),
  127.     append(S1, Post, New), !.
  128.  
  129.  
  130.         /********************************
  131.         *              MAKE             *
  132.         *********************************/
  133.  
  134. %    make
  135. %    Reload all source files that have been changed since they were
  136. %    loaded.
  137.  
  138. make :-
  139.     $update_library_index,
  140.     $time_source_file(File, LoadTime),
  141.     time_file(File, Modified),
  142.     Modified @> LoadTime,
  143.     reload(File),
  144.     fail.
  145. make.
  146.  
  147.  
  148. %    reload(File)
  149. %
  150. %    Reload file in proper module.  Note that if the file is loaded
  151. %    into multiple modules this should be handled more carefully.
  152.  
  153. reload(File) :-
  154.     findall(Context, $load_context_module(File, Context), Modules),
  155.     (   Modules = []
  156.     ->  consult(user:File)
  157.     ;   Modules = [Module]
  158.     ->  consult(Module:File)
  159.     ;   Modules = [First|_Rest],
  160.         consult(First:File)
  161.     ).
  162.     ).
  163.     
  164.