;# BJP  h:\u\bjp\psicor.m  Saturday October 31, 1987  2:09:07
;     
; PSICOR.M
; Date/Time key and File Time Stamping Macros for Brief 1.33
; (Read the macro headers for descriptions.)
;
; To add these macros to your copy of Brief (version 1.33 ONLY):
;
; 1) At the very least, make copies of 
;       STARTUP.M, STARTUP.CM, BUF_LIST.M, BUF_LIST.CM, SYSTEM.M
;    Preferably, you will make a copy of Brief and do this surgery
;    on that copy.
;    Remember: if you have a problem & mess up these critical macro files,
;    you won't have Brief around to help you edit out the mistakes!
;
; 2) Copy PSICOR.M into \BRIEF\MACROS
;    (Note: I tried to keep the modifications as separate from the rest
;     of Brief as possible, that's why the macros are in PSICOR.M instead
;     of one of the standard Brief macro files.
;
; 3) Put
;       #include "psicor.m"
;    into STARTUP.M (after the other #includes)
;
; 4) Put
;      (psicor)
;    after '(extended)' in STARTUP.M
;
; 5) Put
;         (  (timestamp)
;            (write_buffer)
;         )
;    instead of just the '(write_buffer)' in macro write_it in SYSTEM.M
;
; 6) Put
;        (timestamp)
;
;    above	'(if (>= (write_buffer) 0)' in macro _buf_write in BUF_LIST.M
;
; 7) Compile STARTUP.M and BUF_LIST.M
;    (SYSTEM.M and PSICOR.M get included into STARTUP.M)

; Since I don't have Brief Version 2 yet, I don't know how these macros
; would be adapted to work with it.

; A little history: the COMMANDER II system EDITOR did file timestamps
; much like these.  COMMANDER II is a proprietary operating system
; running on the Xerox Sigma 9 systems of Comshare, Inc., Ann Arbor, Mich.
; I used to work there and got very accustomed to seeing that information
; in my source files...

; These macros have been placed in the public domain (I wanted them
; for my work, if you can benefit from them, that would be nice.)
; No warranties, no promises, no guarantees (Sorry, but that's the way
; the cookie crumbles.)  

;         Brad Phillips
;         Information Systems Department
;         PSICOR, Inc.
;         16818 Via Del Campo
;         San Diego, CA 92127

; Note: PSICOR is a national provider of services in the field of open heart
; surgery.  The technical name for the specialty is 'perfusion', and the
; company used to be called 'Perfusion Services Inc.', or commonly PSI.
; As we expanded and added more services, the name was changed to PSICOR.
; So now you know that a name like that can have a perfectly reasonable
; origin, and have nothing to do with Silicon Valley.

; 
(macro psicor
 (  
   (assign_to_key "%#104" "timedate")    ;** Alt-F1 long format time/date
   (assign_to_key "%#105" "timedate2")   ;** Alt-F2 short format time/date
   (assign_to_key "%#106" "timeonly")    ;** Alt-F3 hh:mm:ss
 )
)  
   
; timedate.m
;
; This macro will insert the current time and date at the current location
; in the buffer when you press Alt-F1.
; Format: 'dayname monthname day, year hh:mm:ss'
; Example: Friday October 30, 1987  0:31:28

(macro timedate
 (
   (int year month day hours minutes seconds)
   (string month_string weekday_string output_mdy output_time)
	(string output_hours output_minutes output_seconds)

   (date year month day month_string weekday_string)
   (time hours minutes seconds)

	(sprintf output_mdy " %s %d, %d " month_string day year)

   (sprintf output_hours " %d" hours) 

   (if (< minutes 10)
       (sprintf output_minutes ":0%d" minutes)
   ;else
       (sprintf output_minutes ":%d" minutes)
   )

   (if (< seconds 10)
       (sprintf output_seconds ":0%d" seconds)
   ;else
       (sprintf output_seconds ":%d" seconds)
   )

   (= output_time (+ (+ output_hours output_minutes) output_seconds) )

   (insert weekday_string)
   (insert output_mdy) 
   (insert output_time)

 )
)

; This macro will insert the current time and date at the current location
; in the buffer when you press Alt-F2.
; Format: 'dd-mmm-yy hh:mm:ss'
; Example:  30-Oct-87  0:31:44

(macro timedate2
 (
   (int year month day hours minutes seconds)
   (string month_string weekday_string output_mdy output_time)
	(string output_hours output_minutes output_seconds)

   (date year month day month_string weekday_string)
   (time hours minutes seconds)

	(sprintf output_mdy " %d-%s-%d " day
                                   (substr month_string 1 3)
                                   (% year 100) )

   (sprintf output_hours " %d" hours) 

   (if (< minutes 10)
       (sprintf output_minutes ":0%d" minutes)
   ;else
       (sprintf output_minutes ":%d" minutes)
   )

   (if (< seconds 10)
       (sprintf output_seconds ":0%d" seconds)
   ;else
       (sprintf output_seconds ":%d" seconds)
   )

   (= output_time (+ (+ output_hours output_minutes) output_seconds) )

   (insert output_mdy) 
   (insert output_time) 

 )
)

; timeonly.m
;
; This macro will insert the current time at the current location
; in the buffer when you press Alt-F3.
; Format: 'hh:mm:ss'
; Example: 0:31:28

(macro timeonly
 (
   (int hours minutes seconds)
   (string output_time)
	(string output_hours output_minutes output_seconds)

   (time hours minutes seconds)

   (sprintf output_hours " %d" hours) 

   (if (< minutes 10)
       (sprintf output_minutes ":0%d" minutes)
   ;else
       (sprintf output_minutes ":%d" minutes)
   )

   (if (< seconds 10)
       (sprintf output_seconds ":0%d" seconds)
   ;else
       (sprintf output_seconds ":%d" seconds)
   )

   (= output_time (+ (+ output_hours output_minutes) output_seconds) )

   (insert output_time)

 )
)



; The timestamp macro will put the user name, file name, and date/time
; on line 1 of any file that contains one of the following sequences
; (I call it a stamp flag) starting at line 1, column 1:
;              !#       for Knowledgeman
;              ;#       for Brief
;              :#       for DOS Batch files
;              '#       for Basic
;              /*#      for C
; The line will have the following items placed on it:
;   username drive:directory/file.ext dayname monthname day, year hh:mm:ss 
;
; The 'username' is taken from a DOS environment variable, USER_NAME.
; One-user, standalone PCs probably don't care about this, but could put
;       SET USER_NAME=FRED
; in their AutoExec.Bat files.
; On a Novell network, put
;       DOS SET USER_NAME="%LOGIN_NAME"
; in the login script.
; If you don't set USER_NAME, it just won't show up on the timestamp.
;
; If you immediately follow the stamp flag with a '+', it will insert
; a new line with the new timestamp, leaving an audit trail at the top
; of the file:  Note: this can add up to a lot of lines to be deleted
; later on, but can be useful in some applications...
;
; If you use the '/*' stamp flag, the timestamp will be terminated with '*/'.
; 
; Note, the ':#' construct is indeed a valid way to get a timestamp
; into a DOS Batch file -- it is just a label that you will never use!
; I don't know of any side effects for Dos 3.1 or 3.2, and sometimes
; use ':::' instead of 'REM' to lead off a comment.
; If this is not satisfactory, one could code in REM# as an alternative
; stamp flag.
;
; Known bugs: Brief sometimes still shows the '*' modified flag on the 
; file name after the file is saved, but the file HAS been saved.

(macro timestamp
   (
		(save_position)
      (move_abs 1 1)
      (string stamp_flag)
      (string stamp_roll)
      (= stamp_flag (read 4) )
;      (= stamp_flag (substr stamp_flag 1 3) )

      (if (search_string "<[!\;:']|{/*}#" stamp_flag)
			(
      	   (string user_name file_name)
   
            (= user_name (inq_environment "USER_NAME") )
            (inq_names file_name)

            (int stamp_len)   
            (if (search_string "<[!\;:']|{/*}#+" stamp_flag stamp_len)
               (
;                 (string temp_str)
;                 (sprintf temp_str "String Len=%d" (strlen(read)))
;                 (insert temp_str)
                 (if (> (strlen (read) ) 7 )  ; Is the rest of this line empty?
                    (                         ; No, so put in a new line.
                       (insert (substr stamp_flag 1 stamp_len) )
                       (insert " \n")
                    )
                 )
                 (move_abs 1 (+ stamp_len 2) )
               )
               ;else
               (
                 (move_rel 0 3)
                 (delete_to_eol)
               )
            )
            (insert user_name)
            (insert "  ")
            (insert file_name)
            (insert "  ")
            (timedate)
            (if (search_string "/*#" stamp_flag)
                (insert " */")
            )
			)
      )
		(restore_position 1)
   )
)



