home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / OUT18.ZIP / OUTMISC.INC < prev    next >
Encoding:
Text File  |  1986-11-24  |  12.3 KB  |  473 lines

  1.  
  2. (*
  3.  * outline - a simple "outline" oriented document generator
  4.  *
  5.  * outmisc.inc - this module contains various support procedures
  6.  *               used by the rest of the outline processor.
  7.  *
  8.  * Author:  Samuel H. Smith,  11-Jan-86
  9.  *
  10.  *)
  11.  
  12.  
  13. const                      {single character codes for each of
  14.                             the special keys on the keyboard}
  15.    BACKSPC  = #8;
  16.    TAB      = #9;
  17.    NEWLINE  = #13;
  18.    ESC      = #27;
  19.  
  20.    F1       = #128;
  21.    F2       = #129;
  22.    F3       = #130;
  23.    F4       = #131;
  24.    F5       = #132;
  25.    F6       = #133;
  26.    F7       = #134;
  27.    F8       = #135;
  28.    F9       = #136;
  29.    F10      = #137;
  30.  
  31.    HOME     = #140;
  32.    UP       = #141;
  33.    PGUP     = #142;
  34.    LEFT     = #144;
  35.    RIGHT    = #146;
  36.    ENDLINE  = #148;
  37.    DOWN     = #149;
  38.    PGDN     = #150;
  39.    INS      = #151;
  40.    DEL      = #152;
  41.  
  42.  
  43.  
  44. function getkey: char;      {get a key; map special keys into the
  45.                              matching symbolic constant}
  46. var
  47.    c:  char;
  48.  
  49. begin
  50.    read(kbd,c);                           {read a key}
  51.  
  52.    if (c = ESC) and keypressed then       {if this is a funtion key, then
  53.                                            read the second byte and convert
  54.                                            it into a special key code}
  55.    begin
  56.       read(kbd,c);
  57.       c := chr(ord(c) + 69);   {this makes F1=#128}
  58.    end;
  59.  
  60.    getkey := c;
  61. end;
  62.  
  63.  
  64.  
  65. function new_section: section_ptr;      {allocate a new section and
  66.                                          return a pointer to it}
  67. var
  68.    i:   integer;
  69.    sec: section_ptr;
  70.  
  71. begin
  72.    new(sec);                         {make a new section record}
  73.  
  74.    with sec^ do
  75.    begin
  76.       title := '';                    {initialize all of the fields}
  77.       estimate := 0;
  78.       onpage := 0;
  79.  
  80.       text := emptytext;
  81.  
  82.       for i := 1 to max_subsects do
  83.          subsect[i] := nil;
  84.  
  85.       refcount := 1;
  86.    end;
  87.  
  88.    new_section := sec;
  89. end;
  90.  
  91.  
  92. procedure allocate_text(var sec: section_ptr);     {allocate memory for the
  93.                                                     text array of this section}
  94. var
  95.    i:  integer;
  96.  
  97. begin
  98.    with sec^ do
  99.    begin
  100.       if text = emptytext then
  101.       begin
  102.          new(text);
  103.          for i := 1 to max_text do
  104.             text^[i] := '';
  105.       end;
  106.    end;
  107. end;
  108.  
  109.  
  110. procedure delete_section(var sec: section_ptr);    {delete a section and
  111.                                                     all sub-sections unless
  112.                                                     the section is still
  113.                                                     referenced by someone
  114.                                                     else}
  115. var
  116.    i:  integer;
  117.  
  118. begin
  119.    with sec^ do
  120.    begin
  121.  
  122.       refcount := refcount - 1;      {decrement the reference count for
  123.                                       this section}
  124.  
  125.       if refcount = 0 then           {if nobody references this any more,
  126.                                       then release all subordinates and
  127.                                       dispose of myself!}
  128.       begin
  129.          for i := 1 to max_subsects do
  130.             if subsect[i] <> nil then
  131.                delete_section(subsect[i]);
  132.  
  133.          if text <> emptytext then
  134.             dispose(text);
  135.  
  136.          dispose(sec);
  137.       end;
  138.  
  139.       sec := nil;                    {set this reference pointer to
  140.                                       nil even if it is not the last ref}
  141.    end;
  142. end;
  143.  
  144.  
  145.  
  146. procedure warning(msg:  anystring);   {display a warning message for a
  147.                                        couple of seconds}
  148. var
  149.    i:  integer;
  150.    c:  char;
  151.  
  152. begin
  153.    msg := '<<< '+msg+' >>>';
  154.    sound(430);
  155.  
  156.    for i := 1 to 5 do
  157.    begin
  158.       lowvideo;
  159.       gotoxy(79-length(msg), 3);
  160.       disp(msg);
  161.       delay(125);                     {flash a dim message}
  162.  
  163.       normvideo;
  164.       gotoxy(79-length(msg), 3);
  165.       disp(msg);
  166.       delay(125);                     {flash a bright message}
  167.  
  168.       gotoxy(79-length(msg),3);
  169.       clreol;
  170.       nosound;                        {remove the message and stop beeping}
  171.    end;
  172.  
  173.  
  174.    while keypressed do
  175.       read(kbd,c);             {throw away all key-aheads after a warning}
  176.  
  177. end;
  178.  
  179.  
  180.  
  181. procedure edit_string(x,y:      integer;
  182.                       var col:  integer;
  183.                       var str:  anystring;
  184.                       var c:    char;
  185.                       maxlen:   integer);   {provide full text editing
  186.                                              on the contents of a string
  187.                                              variable; processes most
  188.                                              editing keys.  this is where
  189.                                              all the time is spent while
  190.                                              waiting on the user}
  191. var
  192.    i:   integer;
  193.  
  194. begin
  195.  
  196.    gotoxy(x+length(str),y);
  197.  
  198.    lowvideo;
  199.    disp(make_string('_',maxlen-length(str)));
  200.                                           {display underscores out to the
  201.                                            end of the field.  this lets
  202.                                            the user know when he is near
  203.                                            the end of line}
  204.    normvideo;
  205.  
  206.    repeat
  207.       if col > length(str) then
  208.          col := length(str)+1;            {if i am beyond end of string,
  209.                                            move cursor back to first legal
  210.                                            position}
  211.  
  212.       gotoxy(x+col-1,y);
  213.       c := getkey;                        {position the cursor and wait for an
  214.                                            input from the user}
  215.  
  216.  
  217.       case c of                           {switch on the key and do what
  218.                                            ever is needed}
  219.  
  220.          HOME:      col := 1;
  221.  
  222.  
  223.          ENDLINE:   col := length(str) + 1;
  224.  
  225.  
  226.          TAB:       repeat
  227.                        col := col + 1;
  228.                     until (col mod 4) = 0;
  229.  
  230.  
  231.          ^S,LEFT:   if col > 1 then
  232.                        col := col - 1
  233.                     else
  234.                        write(^G);
  235.  
  236.  
  237.          ^A:        begin
  238.                        while (col > 1) and (str[col] <> ' ') do
  239.                           col := col -1;
  240.                        while (col > 1) and (str[col] = ' ') do
  241.                           col := col -1;
  242.                        while (col > 1) and (str[col] <> ' ') do
  243.                           col := col -1;
  244.                        if str[col] = ' ' then
  245.                           col := col + 1;
  246.                     end;
  247.  
  248.  
  249.          ^D,RIGHT:  col := col + 1;
  250.  
  251.  
  252.          ^F:        begin
  253.                        while (col < length(str)) and (str[col] <> ' ') do
  254.                           col := col +1;
  255.                        while (col < length(str)) and (str[col] = ' ') do
  256.                           col := col +1;
  257.                     end;
  258.  
  259.  
  260.          INS:       if col <= length(str) then
  261.                     begin
  262.                        insert(' ',str,col);
  263.                        disp(copy(str,col,99));
  264.                        saved := false;
  265.                     end;
  266.  
  267.  
  268.          ^G,DEL:    if col <= length(str) then
  269.                     begin
  270.                        delete(str,col,1);
  271.                        disp(copy(str,col,99));
  272.                        lowvideo;
  273.                        disp('_');
  274.                        normvideo;
  275.                        saved := false;
  276.                     end
  277.                     else
  278.                        write(^G);
  279.  
  280.  
  281.          BACKSPC:   if col > 1 then
  282.                     begin
  283.                        col := col - 1;
  284.                        delete(str,col,1);
  285.                        disp(^H+copy(str,col,99));
  286.                        lowvideo;
  287.                        disp('_');
  288.                        normvideo;
  289.                        saved := false;
  290.                     end
  291.                     else
  292.                        write(^G);
  293.  
  294.  
  295.          NEWLINE:   col := 1;
  296.  
  297.  
  298.          ^Y:        begin
  299.                        str := '';
  300.                        gotoxy(x,y);
  301.                        lowvideo;
  302.                        disp(make_string('_',maxlen));
  303.                        normvideo;
  304.                        saved := false;
  305.                     end;
  306.  
  307.          ^E:        c := UP;
  308.          ^X:        c := DOWN;
  309.          ^R:        c := PGUP;
  310.          ^C:        c := PGDN;
  311.  
  312.          UP,
  313.          DOWN,
  314.          PGUP,
  315.          PGDN,
  316.          ESC,
  317.          F1..F10:   ;
  318.  
  319.  
  320.          else       begin
  321.                        if col = (maxlen-7) then
  322.                        begin
  323.                           sound(1200);         {make a quick beep when
  324.                                                 getting close to the end
  325.                                                 of a line}
  326.                           delay(100);
  327.                           nosound;
  328.                        end;
  329.  
  330.                        if col > length(str) then
  331.                           if length(str) < maxlen then
  332.                           begin
  333.                              str := str + c;
  334.                              col := col + 1;
  335.                              write(c);           {add to end of string
  336.                                                   if there is room}
  337.                           end
  338.                           else
  339.                              warning('Line full')     {beep if no more room}
  340.                        else
  341.                        begin
  342.                           str[col] := c;
  343.                           col := col + 1;       {replace in middle of string}
  344.                           write(c);
  345.                           saved := false;
  346.                        end;
  347.  
  348.                     end;
  349.       end;
  350.  
  351.    until c in [UP,DOWN,PGUP,PGDN,NEWLINE,ESC,F1..F10];
  352.  
  353.  
  354.    gotoxy(x+length(str),y);
  355.    clreol;                             {input is finished, remove the extra
  356.                                         underscores from the screen}
  357. end;
  358.  
  359.  
  360.  
  361. procedure change_dir;      {user interface to change working subdirectory }
  362. var
  363.    cd:  anystring;
  364.  
  365. begin
  366.  
  367.    getdir(0,cd);
  368.    gotoxy(1,23);
  369.    normvideo;
  370.    disp('Current directory = '+cd);
  371.    clreol;
  372.  
  373.    gotoxy(1,24);
  374.    clreol;
  375.    disp('Enter new current directory: ');
  376.    cd := '';
  377.    readln(cd);
  378.    if cd = '' then
  379.       exit;
  380.  
  381. {$I-}
  382.    chdir(cd);
  383. {$I+}
  384.    if ioresult <> 0 then
  385.       write(^G);
  386. end;
  387.  
  388.  
  389.  
  390. procedure check_page_header(var fd: textfile;
  391.                             format: print_formats;
  392.                             sec:    section_ptr);    {check to see if a page
  393.                                                       header is needed. print
  394.                                                       one if it is needed.}
  395. var
  396.    dir: anystring;
  397.  
  398. begin
  399.    if break_into_pages and (prnline = 1) then
  400.    begin
  401.       page_number := page_number + 1;
  402.       if page_number = 1 then
  403.       begin
  404.          getdir(0,dir);
  405.          if dir[length(dir)] <> '\' then
  406.             dir := dir + '\';
  407.  
  408.          write(fd,dir,docfile,'':right_margin-10-length(dir+docfile));
  409.       end
  410.       else
  411.          write(fd,document^.title,'':right_margin-10-length(document^.title));
  412.  
  413.       if format = contents_format then
  414.          writeln(fd,'Contents ',page_number)
  415.       else
  416.       if format = index_format then
  417.          writeln(fd,'   Index ',page_number)
  418.       else
  419.          writeln(fd,'    Page ',page_number);
  420.  
  421.       writeln(fd);
  422.       prnline := 2;
  423.  
  424.       if (prnfile <> 'CON') then
  425.       begin
  426.          gotoxy(1,wherey);
  427.          write('Page: ',page_number);
  428.          gotoxy(10,wherey);
  429.          disp('Section: '+sec^.title);
  430.          clreol;
  431.       end;
  432.    end;
  433. end;
  434.  
  435.  
  436.  
  437. procedure pflush(var fd: textfile);
  438. var
  439.    start,stop: real;
  440.    time:       real;
  441.    c:          char;
  442. begin
  443.    start := get_time;
  444.    flush(fd);
  445.    stop := get_time;
  446.  
  447.    if prnfile <> 'PRN' then exit;
  448.  
  449.    if (stop-start) > 0.5 then
  450.    begin
  451.       stop := get_time + 10.0;
  452.       write(' DELAY');
  453.  
  454.       while (get_time < stop) and (not keypressed) do
  455.          give_up_time;
  456.  
  457.       write(^H^H^H^H^H^H'      '^H^H^H^H^H^H);
  458.  
  459.       if keypressed then
  460.          read(kbd,c);
  461.    end;
  462. end;
  463.  
  464.  
  465. function itoa(i: integer): anystring;
  466. begin
  467.    if i < 10 then
  468.       itoa := chr(i + ord('0'))
  469.    else
  470.       itoa := '1' + chr(i + ord('0') - 10);
  471. end;
  472.  
  473.