;**************************************************************************
;*																								  *
;* 	If you are a previous user of ABBREV, you will need to				  *
;* 	replace the autoload's that were provided with that version			  *
;* 	with the ones specified below.												  *
;*																								  *
;**************************************************************************
;*																								  *
;* 	 ABBREV.CB																			  *
;* 																							  *
;* 	 This macro provides a replacement for edit_file and read_file.  It *
;* 	 allows arbitrary abbreviation of drive and path combinations in	  *
;* 	 file names (and may even be used to abbreviate full file names).	  *
;* 																							  *
;* 	 The if there is a slash or backslash in the path (after column 1), *
;* 	 then abbrev looks up a replacement for what preceeds the			  *
;* 	 1st slash (backslash) and passes it on to the next					  *
;* 	 program in the chain.	 If there is no slash or backslash,			  *
;* 	 abbrev checks for a replacement of the entire name					  *
;* 	 specified.  (This is for using an abbreviation for a					  *
;* 	 file name.)  It is not necessary to use the last						  *
;* 	 directory name as the abbreviation, and you may						  *
;* 	 supply several abbreviations for a single directory.					  *
;* 																							  *
;* 	 Finally, if you wish to pass a name through without being			  *
;* 	 altered by abbrev, then precede it with an exclaimation point.	  *
;* 																							  *
;* 	 If "foo" was the abbreviation for "c:\new\dos\foo"					  *
;* 																							  *
;* 	 then (edit_file "foo\filename") would pass on:							  *
;* 																							  *
;* 			 (edit_file "c:\new\dos\foo\filename").							  *
;* 																							  *
;* 	 The abbreviations are kept in a file called "abbrev.dat".			  *
;* 	 This file goes in the last directory specified by your				  *
;* 	 BPATH environment variable.	If you would like it in a				  *
;* 	 different directory, then you may specify this with the				  *
;* 	 BABBREV environment variable.  The macro "abbrev" (invoked with	  *
;* 	 F10) will bring the abbrev.dat file into a Brief buffer to be		  *
;* 	 edited.																				  *
;* 																							  *
;* 	 Each abbreviation is specified on 1 line of the file.				  *
;* 	 The line is of the form:														  *
;* 																							  *
;* 	 <abbreviation> <whitespace> <substitution>								  *
;* 																							  *
;* 	 The <abbreviation> must begin in column 1.	The line					  *
;* 	 for the above example would be:												  *
;* 																							  *
;* 	 foo		 c:\new\dos\foo														  *
;* 																							  *
;* 	 To inspire you of its usefulness, part of my current file is		  *
;* 	 setup as follows:																  *
;* 																							  *
;* 	 autoexec c:\autoexec.bat														  *
;* 	 exec 	 c:\autoexec.bat														  *
;* 	 config	 c:\config.sys															  *
;* 	 ap		 d:\pinball\apple														  *
;* 	 apple	 d:\pinball\apple														  *
;* 	 util 	 d:\util																	  *
;* 	 u 		 d:\util																	  *
;* 	 uni		 d:\util\uni															  *
;* 	 mine 	 c:\soft\brief\macros\mine											  *
;* 	 m 		 c:\soft\brief\macros\mine											  *
;* 	 macros	 c:\soft\brief\macros												  *
;* 	 mac		 c:\soft\brief\macros												  *
;* 	 macs 	 c:\soft\brief\macros												  *
;* 	 inc		 c:\soft\c\include													  *
;* 	 include  c:\soft\c\include													  *
;* 																							  *
;* 	 To install, add the following to your initials macro					  *
;* 	 (after any other replacements to edit_file or read_file):			  *
;* 																							  *
;*     (autoload "abbrev.cm" "edit_file" "read_file" "abbrev")			  	  *
;* 	 (autoload "ablookup.cm" "ab_lookup" "get_abbrev_file")			     *
;* 																							  *
;* 	 Place "abbrev.cm" and "ablookup.cm" in a BPATH directory			  *
;* 	 (e.g. c:\brief\macros).														  *
;* 																							  *
;* 	 Abbrev keeps the abbreviation file in a system buffer.				  *
;* 	 The 1st call will create the system buffer.   Subsequent calls	  *
;* 	 will run faster.																	  *
;* 																							  *
;* 	 Larry DeMar	 July, 1989														  *
;* 																							  *
;* 	 Updated for FILE_ED3 compatability,  August, 1989.   -Led			  *
;* 																							  *
;* 	 Updated to allow suffix.cb to use the utilities without loading	  *
;* 	 replacement macros October, 1989  -Led.									  *
;*																								  *
;**************************************************************************

(extern get_abbrev_file)
(extern ab_lookup)

(replacement edit_file
   (
      (string e_parm)
      (if (!= (inq_called) "")
         (return (edit_file))
      )
      (if (! (get_parm 0 e_parm "File: "))
         (return)
      )
      (edit_file (abbreviate e_parm))
   )
)
(replacement read_file
   (
      (string r_parm)
      (if (!= (inq_called) "")
         (return (read_file))
      )
      (if (! (get_parm 0 r_parm "File to read: "))
         (return)
      )
      (read_file (abbreviate r_parm))
   )
)
;**************************************************************************
;*																								  *
;* 	 ABBREVIATE																			  *
;* 																							  *
;* 	 This is called to expand an abbreviation if necessary.				  *
;* 																							  *
;* 	 It returns the string that should be passed on.						  *
;*																								  *
;**************************************************************************
(macro abbreviate
   (
      (int slash_index)
      (int abbrev_buffer_id)
      (global abbrev_buffer_id)
      (string abbreviation
              file_name
              first_char   ; allow leading slash or backslash as a courtesy
      )
      (get_parm 0 file_name)
      (if (== (substr file_name 1 1) "!")   ; check for bang
         (return (substr file_name 2))   ; strip it and we're done.
      )
      ; 
      ;   As a courtesy, if the user preceeded his abbreviation
      ;   with slash or backslash (from using dos too much)
      ;   lookup what follows the leading seperator
      ; 
      ;   If you want to use that leading slash or backslash
      ;   to shut off abbrev, then comment out the next 2
      ;   Brief statements (next 6 lines).
      ; 
      (= first_char (substr file_name 1 1))    ; get 1st char
      (if (|| (== first_char "/") (== first_char "\\"))
         (= file_name (substr file_name 2))   ; lookup rest
      ;else
         (= first_char "")   ; nothing stripped
      )
      ; 
      ;   now we have to see if there is a path separator.
      ; 
      (= slash_index (get_first_seperator file_name))
      ; 	  slash_index is now zero if there are now separators
      ; 	  and has the position of the 1st seperator if non-zero.
      ; 
      (if slash_index
         (= abbreviation (substr file_name 1 (- slash_index 1)))
      ;else
         (= abbreviation file_name)
      )
      (if (|| (index abbreviation "*") (index abbreviation "?"))   ; for FILE_ED3 if it has a star
         (= abbreviation (+ first_char abbreviation))   ; don't look up!
      ;else
         (if (! (ab_lookup "abbrev.dat" abbreviation abbrev_buffer_id "abbrev.$b$"))
            (= abbreviation (+ first_char abbreviation))   ; not found..put 1st char back.
         )
         ; 
         ;   now....if there was a path...we must concatonate whats beyond 1st seperator
         ; 
      )
      (if slash_index
         (= abbreviation (+ abbreviation (substr file_name slash_index)))
      )
      (return abbreviation)
   )
)
;**************************************************************************
;*																								  *
;* 	 ABBREV																				  *
;* 																							  *
;* 	 This is called to bring the abbreviation file into the				  *
;* 	 editor.  It first deletes the system buffer such that				  *
;* 	 changes (that are written) will be used on the next					  *
;* 	 request.																			  *
;*																								  *
;**************************************************************************
(macro abbrev
   (
      (if abbrev_buffer_id
         (delete_buffer abbrev_buffer_id)
      )
      (= abbrev_buffer_id 0)
      (edit_file (get_abbrev_file "abbrev.dat"))
   )
)
;**************************************************************************
;*																								  *
;* 	 GET_FIRST_SEPERATOR																  *
;* 																							  *
;* 	 This is called to return the position of the 1st seperator			  *
;* 	 (slash or backslash) in a file name.	It returns 0 if				  *
;* 	 no seperators exist, or the index of the 1st one it					  *
;* 	 finds.																				  *
;*																								  *
;**************************************************************************
(macro get_first_seperator
   (
      (int slash_index
           bslash_index
      )
      (string file_name)
      (get_parm 0 file_name)
      (= slash_index (index file_name "/"))
      (= bslash_index (index file_name "\\"))
      (if (&& slash_index bslash_index)   ; if both seperators are there
         (if (< bslash_index slash_index)
            (= slash_index bslash_index)   ; then take the lower
         ;else
         )
      ;else
         (if bslash_index   ; is 1st zero?
            (= slash_index bslash_index)   ; yep...use 2nd.
         )
      )
      (return slash_index)
   )
)
