home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1988 / 12 / func.asc < prev    next >
Text File  |  1988-12-31  |  5KB  |  176 lines

  1. _FIND THAT FUNCTION -- FROM INSIDE BRIEF!_
  2. by Marvin Hymowech
  3.  
  4. [LISTING 1]
  5.  
  6. ;**   getf - Copyright 1988 by Marvin Hymowech.
  7. ;**        - Prompts for a function name, extracts the name of the file
  8. ;**        - in which it is defined from "funcs.txt" (see bldfuncs.c)
  9. ;**        - and presents the file in the current window.
  10. ;**        - Returns: 0 if error, else 1.
  11.  
  12. #define NON_SYSTEM 0
  13.  
  14. ( macro getf
  15.    (
  16.       ( int start_buf_id func_buf_id )
  17.       ( string fn_name line file_name )
  18.  
  19.          ;** make fn_name a global so that it will retain its prior
  20.          ;** value for the default display in the get_parm below
  21.       ( global fn_name )   
  22.  
  23.       
  24.          ;** get desired function name from user
  25.       ( if
  26.          ( ( !
  27.             ( get_parm NULL fn_name "Enter function name: " NULL fn_name ) )
  28.          )
  29.          (return 0)
  30.       )
  31.  
  32.          ;** verify that funcs.txt exists on disk in the current directory
  33.       ( if ( ! ( exist "funcs.txt" ) )
  34.          (
  35.             ( beep )
  36.             ( error "file funcs.txt not found" )
  37.             ( return 0 )
  38.          )
  39.       )
  40.  
  41.          ;** save current buf id, so we can restore it if an error occurs
  42.       ( = start_buf_id ( inq_buffer ) )
  43.  
  44.          ;** create a buffer for funcs.txt without attaching it to a window
  45.       ( if
  46.          ( ! ( = func_buf_id
  47.                ( create_buffer "functions" "funcs.txt" NON_SYSTEM )
  48.             )
  49.          )
  50.          (
  51.             ( beep )
  52.             ( error "Can't load funcs.txt file." )
  53.             ( return 0 )
  54.          )
  55.       )
  56.  
  57.          ;** make it the current buffer
  58.       ( set_buffer func_buf_id )
  59.  
  60.          ;** Brief back-end buffer functions won't set tabs for us 
  61.          ;** like edit_file, so we do it explicitly 
  62.       ( tabs 9 17 )
  63.  
  64.       ( top_of_buffer )
  65.  
  66.          ;** search for the function name
  67.       ( while 1
  68.          (     
  69.                ;** break if function name not found
  70.             ( if ( <= ( search_fwd fn_name ) 0 )   
  71.                (
  72.                   ( beep )
  73.                   ( error "function %s not found" fn_name )
  74.                      ;** restore the original buffer
  75.                   ( set_buffer start_buf_id )
  76.                   ( return 0 )
  77.                )
  78.             )
  79.                ;** get the current line into variable "line"
  80.             ( beginning_of_line )
  81.             ( sprintf line "%s" ( read ) )
  82.  
  83.                ;** if fn line, not file line
  84.             ( if ( == ( index line "\t" ) 1 )         
  85.                (
  86.                      ;** read the line into "line" starting after the tab
  87.                   ( next_char )
  88.                   ( sprintf line "%s" ( read ) )      
  89.  
  90.                      ;** make sure we have the exact function name, not
  91.                      ;** part of a larger function name
  92.  
  93.                      ;** if the line consists of \t,fn_name, and \n,
  94.                      ;**    then break
  95.                   ( if ( == ( index line "\n" ) ( + 1 ( strlen fn_name) ) )
  96.                      ( break )
  97.                   )
  98.  
  99.                      ;** if the line consists of \t,fn_name, ;, and \n
  100.                      ;**    then break
  101.                   ( if ( == ( index line ";" ) ( + 1 ( strlen fn_name ) ) )
  102.                      ( break )
  103.                   )
  104.                )
  105.             )
  106.             ( end_of_line )               ;** else try next line           
  107.          )
  108.       )
  109.       
  110.       ( if ( <= ( search_back ":" ) 0 )   ;** done, get the file name
  111.          (
  112.             ( beep )
  113.             ( error "funcs.txt corrupted" )
  114.             ( set_buffer start_buf_id )
  115.             ( return 0 )
  116.          )
  117.       )
  118.  
  119.       ( beginning_of_line )
  120.       ( sprintf line "%s" ( read ) )
  121.       ( sprintf file_name "%s"            ;** get rid of trailing ":"
  122.          ( substr line 1 ( - (index line ":" ) 1 ) ) )
  123.  
  124.           ;** verify that the file exists on disk
  125.       ( if ( ! ( exist file_name ) ) 
  126.          (
  127.             ( beep )
  128.             ( error "file %s not found" file_name )
  129.             ( set_buffer start_buf_id )
  130.             ( return 0 )
  131.          )
  132.       )
  133.  
  134.          ;** and edit it
  135.       ( edit_file file_name )
  136.          ;** try to place cursor on the function defn
  137.       ( return ( funcsrch fn_name ) )
  138.    )
  139. )
  140.  
  141. ;** funcsrch - Copyright 1988 by Marvin Hymowech.
  142. ;**          - Searches the current buffer for a string (function name)
  143. ;**          - by starting from the end of the buffer.
  144. ;**          - This is designed to position the cursor on
  145. ;**          - a function definition since most functions tend to be
  146. ;**          - used before they are defined.
  147. ;**          - Returns: 0 if function not found, 1 if found.
  148. (macro funcsrch
  149.    (
  150.       ( string s )
  151.       ( extern _s_pat _dir center_line )
  152.  
  153.       ( get_parm 0 s )
  154.       ( message "locating function %s..." s )
  155.       ( end_of_buffer )
  156.       ( if ( > (search_back s) 0 )
  157.          (
  158.             ( message "function %s found" s )
  159.             ( center_line )
  160.             ( sprintf _s_pat "%s" s )
  161.             ( = _dir 0 )
  162.             ( return 1 )
  163.          )
  164.       ;else
  165.          (
  166.             ( top_of_buffer )
  167.             ( beep )
  168.             ( error "function %s not found" s )
  169.             ( return 0 )
  170.          )
  171.       )
  172.    )
  173. )
  174.  
  175.  
  176.