
;**Last revision : Wednesday April 26, 1989 at 9:23 pm

;**   getf - Copyright 1988 by Marvin Hymowech.
;**        - Prompts for a function name, extracts the name of the file
;**        - in which it is defined from "funcs.txt" and presents the file
;**        - in the current window.
;**        - Returns: 0 if error, else 1.


#define NON_SYSTEM 0

( macro getf
   (
      (int start_buf_id func_buf_id)
      (string fn_name line file_name)

         ;** make fn_name a global so that it will retain its prior
         ;** value for the default display in the get_parm below
      (global fn_name)


         ;** get desired function name from user
      (if
         ((!
            (get_parm NULL fn_name "Enter function name: " NULL fn_name))
         )
         (return 0)
      )

         ;** verify that funcs.txt exists on disk in the current directory
      (if (!(exist "funcs.txt"))
         (
            (beep)
            (error "file funcs.txt not found")
            (return 0)
         )
      )

         ;** save current buff_id, so we can restore it if an error occurs
      ( = start_buf_id (inq_buffer))
         
         ;**create a buffer for funcs.txt without attaching it to a window
      (if
         (!( = func_buf_id
            (create_buffer "functions" "funcs.txt" NON_SYSTEM)
          )
         )
         (
            (beep)
            (error "Can't load funcs.txt file.")
            (return 0)
         )
      )

         ;** make it the current buffer
      (set_buffer func_buf_id)

         ;** Brief back-end buffer functions won't set tabs for us
         ;** like edit_file, so we do it explictly
      (tabs 9 17)

      (top_of_buffer)

         ;** search for the function name
      (while 1
         (
            ;** break if function anme not found
            (if ( <= (search_fwd fn_name)0)
               (
                  (beep)
                  (error "function %s not found" fn_name)
                     ;** restore the original buffer
                  (set_buffer start_buf_id)
                  (return 0)
               )
            )
               ;** get the current line into variable "line"
            (beginning_of_line)
            (sprintf line "%s" (read) )

               ;** if fn line, not file line
            (if (==(index line "\t")1)
               (

                  ;** read the line into "line" starting after the tab
               (next_char)
               (sprintf line "%s" (read) )

                  ;** make sure we have the exact function name, not
                  ;** part of a larger function name.

                  ;** if the line consists of \t,fn_name, and \n,
                  ;** then break
               (if (==(index line "\n") ( + 1 (strlen fn_name)))
                  (break)
               )

                  ;** if the line consists of \t,fn_name, ;, and \n
                  ;** then break
               (if(==(index line ";") ( + 1 (strlen fn_name)))
                  (break)
               )
            )
         )
         (end_of_line)     ;** else try next line
      )
   )
   (if (<= (search_back ":")0)  ;** done, get the file name
      (
         (beep)
         (error "fincs.txt corrupted")
         (set_buffer start_buf_id)
         (return 0)
      )
   )

   (beginning_of_line)
   (sprintf line "%s" (read))
   (sprintf file_name "%s"       ;** get rid of trailing ":"
      (substr line 1 (-(index line ":")1)))


      ;** verify that the file exists on disk
   (if(!(exist file_name))
      (
         (beep)
         (error "file %s not found" file_name)
         (set_buffer start_buf_id)
         (return 0)
      )
   )

      ;** and edit it
   (edit_file file_name)
      ;** try to place cursor on the function definition
   (return(funcsrch fn_name))
   )
)

;** funcsrch - Copyright 1988 Marvin Hymowech
;**          - Searches the current buffer for a string (function name)
;**          - by starting from the end of the buffer.
;**          - This is designed to position the cursor on
;**          - a function definition since most functions tend to be
;**          - used before thet are defined.
;**          - Returns: 0 if function not found, 1 if found
(macro funcsrch
   (
      (string s)
      (extern _s_pat _dir center_line)

      (get_parm 0 s)
      (message "locating function %s..." s)
      (end_of_buffer)
      (if(> (search_back s)0)
         (
            (message "function %s found" s)
            (center_line)
            (sprintf _s_pat "%s" s)
            (= _dir 0)
            (return 1)
         )
      ;else
         (
            (top_of_buffer)
            (beep)
            (error "function %s not found" s)
            (return 0)
         )
      )
   )
)




