

                          Codewright V1.1
                      Pre-defined Event Handlers


File               Event                  Mode          Function
startup/autosave.c EVENT_EXIT_SHUTDOWN    EVENT_NORMAL  _AutosaveExit
    Cleans up the autosave files.

startup/c_lang.c   EVENT_BUFFER_CURRENT   EVENT_NORMAL  _c_current_buffer
    Enables template expansion for .C files.

startupcursor.c    EVENT_WINDOW_UNCURRENT EVENT_NORMAL  _saveLastCurWinWithSel
    Tracks changes to the current window to facilitate mouse copy/move.

startup/language.c EVENT_BUFFER_CURRENT   EVENT_NORMAL  _ext_current_buffer
startup/language.c EVENT_FILENAME_CHANGED EVENT_NORMAL  _ext_current_buffer
    Enables template expansion and ChromaCoding based on the file's extension.

startup/language.c EVENT_BUFFER_DELETE    EVENT_NORMAL  _ext_buffer_delete
    Cleans up data structures used to handle coloring.

startup/language.c EVENT_BUFFER_RELOADED  EVENT_NORMAL  _ext_buffer_reload
    Re-colors a buffer.

startup/language.c EVENT_LINES_CHANGED    EVENT_NORMAL  _ext_lines_changed
startup/language.c EVENT_LINES_INSERTED   EVENT_NORMAL  _ext_lines_changed
startup/language.c EVENT_LINES_DELETED    EVENT_NORMAL  _ext_lines_changed
    Properly colors a line pursuant to user action.

startup/prompt.c   EVENT_INVALID_RESPONSE EVENT_NORMAL  _MsgBadPromptKey
    Navigates through the history in a prompt box.  Handles <up>,
    <down>, <tab>.

startup/state.c    EVENT_EXIT_SHUTDOWN    EVENT_FIRST   _StateSave
    Saves the edit state at exit-time.

startup/tabset.c   EVENT_BUFFER_CREATED   EVENT_NORMAL  _FileTabEvent
    Sets the tab string for a buffer according to its extension.

dialogs/filemenu.c EVENT_BUFFER_CREATED   EVENT_NORMAL  _FileOpenReadOnly
    Sets the buffer status to read only pursuant to a checkbox in the
    file open dialog.

<kernel>           EVENT_EOL_INSERTED     EVENT_ONLY    bufAutoIndent (below)
    Moves the cursor to the column position of the first non-whitespace
    character of the previous line.

<kernel>           EVENT_READY_TO_WRITE   EVENT_ONLY    bufBackup (below)
    If backup is on, the existing file is moved/renamed.  Otherwise the
    existing file is deleted.

<kernel>           EVENT_EDIT_READ_ONLY   EVENT_ONLY    bufEditReadOnly (below)
    Emits a beep.

----------------------------------------------------------------------------

Equivalent source code for internal event handlers.


#include "exports.h"

/*
 ** bufAutoIndent
 *
 * Default event handler for the "EOL inserted" event.  Two cases are
 * handled: 1) the cursor is in column 1 of a blank line, 2) the cursor
 * is in column 1 of a non-blank line.
 *
 * For case 1, if the immediately preceding line has leading whitespace,
 * the cursor is positioned below the first non-whitespace character.  Note
 * that the cursor ends up in virtual space, no actual characters are
 * inserted.  When a character other than an EOL is typed, the virtual space
 * is filled (by either tabs and spaces or just spaces depending on the
 * UseTabs setting for the buffer).  If an EOL is typed, the virtual space
 * is not filled.
 *
 * For case 2, if the immediately preceding line has leading whitespace,
 * that identical whitespace string is inserted so that the remainded of
 * the line is indented in the same manner.
 *
 */
PUBLIC int SYSFUNC
bufAutoIndent(EVENT_HANDLER_ARGS)
{
	WORD ch;
	long col;
	WORD cnt;
	WORD i;
	int atEOL;
	int atEOF;
	LPSTR buf;
	
	if ((eventID == EVENT_EOL_INSERTED) &&
			(BufQSysFlags() & BUFFER_AUTO_INDENT))
	{
		/* see if we're positioned at EOF or EOL */
		_PosInit(-1);
		ch = (int)_PosCurrentCharNL();
		atEOL = ((ch == EOL1_CHAR) || (ch == EOL2_CHAR));
		atEOF = (ch == EOF_CHAR);

		/* compute length of indentation on the previous line */
		_PosPrevLine(1);
		for (col = 1, cnt = 0; ; _PosNextChar(), cnt++)
		{
			if ((ch = _PosCurrentChar()) == ' ')
				col++;
			else if (ch == '\t')
				col = BufQNextTab(col);
			else
				break;
		}

		/* see if there was whitespace and move to indented col */
		if (col > 1)
		{
			if (atEOL || atEOF)
				MovRight(col - 1);
			else
			{
				/* extract previous indentation for insertion */
				buf = MemAlloc(cnt);
				_PosInit(-1);
				_PosPrevLine(1);
				for (i = 0; i < cnt; _PosNextChar(), i++)
					buf[i] = (char)_PosCurrentChar();

				/* insert the character */
				BufInsertStrN(buf, cnt);

				MemFree(buf);
			}
		}
	}
	return(0);
}

/*
 ** backBackup
 *
 * Event handler for backing up files, attached to the 'Ready to Write' event.
 * The handler simply creates a backup file, if backups are enabled, after
 * first deleting any existing backup file.
 *
 */
PUBLIC int DLL
bufBackup(EVENT_HANDLER_ARGS)
{
	int stat = 0;
	LPSTR file = NIL;
	LPSTR spec;
	LPSTR backup = NIL;
	char msg[512];

	/* check for the event of interest */
	if (eventID != EVENT_READY_TO_WRITE)
		goto done;

	/* pull out the name of the file that may need to be backed up */
	if (data != NIL)
		file = *(LPSTR XFAR *)data;

	/* no need for backup unless the file already exists */
	if ((file == NIL) || !FileExists(file))
		goto done;

	/* get the backup specification in case it's needed */
	if ((spec = BufQBackupSpec()) == NIL)
		spec = BufQGlobalBackupSpec();

	/* check for the backups being enabled */
	if ((BufQSysFlags() & BUFFER_BACKUP) && (spec != NIL) &&
			((backup = TransformFilename(file, spec)) != NIL))
	{
		/* check for the backup file name being used already */
		if (BufFindBuffer(backup, FALSE, FALSE) != NIL)
		{
			wsprintf(msg,
					"Can't use backup file \"%s\" because it is being edited.",
					backup);
			MessageBox(NULL, msg, SysQAppName(),
					MB_OK | MB_TASKMODAL | MB_ICONINFORMATION);
			stat = -1;
		}
		else if (FileExists(backup) && (FileDelete(backup) != 0))
		{
			wsprintf(msg, "Can't delete the existing backup file \"%s\".",
					backup);
			MessageBox(NULL, msg, SysQAppName(),
					MB_OK | MB_TASKMODAL | MB_ICONINFORMATION);
			stat = -1;
		}
		/* move file to destination */
		else if (FileMove(backup, file) != 0)
		{
			wsprintf(msg, "Can't copy/move file \"%s\" to backup file \"%s\".",
					file, backup);
			MessageBox(NULL, msg, SysQAppName(),
					MB_OK | MB_TASKMODAL | MB_ICONINFORMATION);
			stat = -1;
		}
	}
	else if (FileDelete(file) != 0)
	{
		/* couldn't delete the file */
		wsprintf(msg, "Can't delete the existing file \"%s\".", file);
		MessageBox(NULL, msg, SysQAppName(),
				MB_OK | MB_TASKMODAL | MB_ICONINFORMATION);
		stat = -1;
	}
done:
	if (backup != NIL)
		StrFree(backup);
	return(stat);
}

/*
 ** bufEditReadOnly
 *
 * Default event handler for the "Edit read-only file" event.
 *
 */
PUBLIC int DLL
bufEditReadOnly(EVENT_HANDLER_ARGS)
{
	SysBeep();
	return(0);
}
