;**
;**		BRIEF -- Basic Reconfigurable Interactive Editing Facility
;**
;**		Written by Dave Nanian and Michael Strickman.
;**
;**		Revision history:
;**		-----------------
;**		29 January 1986		Fixed incorrect search RE parameter so regular
;**									expressions in filenames are ignored.
;**		9 October 1985			Now puts autosave files in same directory as
;**									originals.
;**		6 October 1985			Removed filename from autosave buffer.
;**

;**
;**		autosave.m:
;**
;**		This macro implements a simple autosave feature using the BRIEF
;**	idle action registered macro facility.  Every time the user is idle
;**	for the amount of time specified by -i<num>, the buffer list is checked
;**	and modified files are automatically written to disk as "filename.asv"
;**	(stands for AutoSaVe).
;**

(macro autosave
	(
		(register_macro 4 "save_files")
		(register_macro 5 "save_exit")
	)
)

;**
;**		save_files:
;**
;**		This macro loops through the buffer list, writing all of the modified
;**	buffers.  Note that a special buffer is created to keep track of the
;**	autosaved files.
;**

(macro save_files
	(
		(int		save_buf
					curr_buf
					old_buf
					saved
		)
		(global	save_buf)

		(= curr_buf (inq_buffer))

		;**
		;**		This loop goes through the buffer list, and stops when all
		;**	buffers have been checked.  Note that if the user types a
		;**	character, the loop stops -- this prevents the autosave from
		;**	continuing through all of the remaining buffers when a person
		;**	wants to edit.
		;**

		(while (&& (!= curr_buf old_buf) (! (inq_kbd_char)))
			(
				(if (! old_buf)
					(= old_buf curr_buf)
				)
				(if (&& (inq_modified) (! (inq_system)))
					(
					;**
					;**		If the current buffer is modified, and is not a system
					;**	buffer, we have to write it to disk.
					;**

						(string	name
									extension
						)
						(message "Autosaving files...")
						(= saved 1)
						(inq_names name extension)

						(if (strlen extension)
							(= name (substr name 1 (- (rindex name (+ "." extension)) 1)))
						)
						(+= name ".asv")

						(if (! save_buf)
							(= save_buf (create_buffer "Autosaves" NULL 1))
						)
						(set_buffer save_buf)
						(top_of_buffer)

						;**
						;**		If the file has been saved already, delete the
						;**	one that is on the disk.  This way, no backup file
						;**	will be created.
						;**

						(if (search_fwd name 0)
							(del name)
						;else
							(insert (+ name "\n"))
						)
						(set_buffer curr_buf)
						(save_position)
						(top_of_buffer)
						(drop_anchor)
						(end_of_buffer)

						(if (< (write_block name) 0)
							(raise_anchor)
						)
						(restore_position)
					)
				)
				;**
				;**		When we go to the next buffer, we ask next buffer for
				;**	all of the buffers, not just non-system.  This way, if we
				;**	started with a system buffer, we don't infinite loop.
				;**

				(= curr_buf (set_buffer (next_buffer 1)))
			)
		)
		(if old_buf
			(set_buffer old_buf)
		)
		(if saved
			(message "Autosave complete.")
		)
	)
)

;**
;**		save_exit:
;**
;**		This routine is called just before BRIEF is going to exit for real.
;**	At that time, all of the autosaved files are deleted from the disk
;**	since they are no longer needed.
;**

(macro save_exit
	(
		(string		name)

		(if save_buf
			(
				(set_buffer save_buf)
				(top_of_buffer)

				(while (! (inq_position))
					(
						(= name (read))
						(del (substr name 1 (- (strlen name) 1)))
						(move_rel 1 0)
					)
				)
			)
		)
	)
)
