home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
p2demo21.exe
/
PEL
/
BUFFERS.PEL
< prev
next >
Wrap
Text File
|
1995-03-23
|
46KB
|
1,704 lines
# $Header: P:\source\wmacros\buffers.pev 1.115 23 Mar 1995 10:02:52 WALKER $
## $Tabs:4 7$
##############################################################################
#
# Compuware Corporation
# 31440 Northwestern Highway
# Farmington Hills, Michigan 48334-2564
#
# This source code listing contains information that is
# proprietary to Compuware Corporation and may not be copied
# duplicated, translated, transmitted, stored, retrieved
# or in any manner or by any method conveyed or disclosed
# to a third party or parties without express written
# permission from Compuware Corporation.
#
#
##############################################################################
#### $Workfile: buffers.pel $: Support for Buffers
#
# buffer flags:
#
global BUFFER_SYSTEM = 0x0001
global BUFFER_IN_VIRTUAL_SPACE = 0x0002
global BUFFER_POSITION_IS_VIRTUAL = 0x0002 # This is the documented one
global BUFFER_CONTROL_Z_IS_EOF = 0x0004
#reserved = 0x0008
global BUFFER_READ_ONLY = 0x0010
global BUFFER_AUTOSAVE_MODIFIED = 0x0020
global BUFFER_MODIFIED = 0x0040
global BUFFER_OVERTYPE_MODE = 0x0080
global BUFFER_EXPAND_TABS = 0x0100
global BUFFER_WP_ENABLED = 0x0200
global BUFFER_REAL_SPACE_ONLY = 0x0400
global BUFFER_SNAP_TO_EOL = 0x0800
global BUFFER_NO_UNDO = 0x1000
global BUFFER_TABS_TO_SPACES = 0x2000
global BUFFER_WHOLE_LINES = 0x4000
global BUFFER_SCRATCH = 0x100000 # buffer is temp
global BUFFER_WP_WRAP_CONTINUOUS = 0x200000
global BUFFER_LOCKED = 0x400000 # R/O
global BUFFER_WP_CARRY_COMMENTS = 0x800000
# Analyzer Buffer Flags
global BUFFER_ANALYZER_RUNNING = 0x1000000
global BUFFER_ANALYZER_SUCCESSFUL = 0x2000000
global BUFFER_ANALYZER_DDE_TOPIC = 0x4000000
global BUFFER_ANALYZER_FILE_SAVED = 0x8000000
# not a buffer_flag, used to mark a buffer for "save_as" in the save_changes_list
local SAVE_AS_BUFFER = 0x200000
#user defined buffer flags
global DOS_BUFFER_FLAG = 0x00020000;
global save_changes_list = 0
global process_ok = 1
global buffer_list_index = 0
# flag that indicates a new window is to be created when
# a file is opened JAM: he is a primitive now (1-19-95)
# global create_new_bufwin = 0
##
## functions to interrogate/set bits in the buffer_flags word
##
global function buffer_is_modified()
{
return ( and(buffer_flags, BUFFER_MODIFIED) != 0 )
}
global function set_default_buffer_flag( mask, value )
{
if ( value )
default_buffer_flags = or( default_buffer_flags, mask )
else
default_buffer_flags = and( default_buffer_flags, not(mask) )
return value
}
############################################################################
##
## buffer_list() - pop up a dialog listing the active buffers
##
local bufferListId[] # array containing the buffer IDs
local bufferListFlag[] # array containing the buffer flags
local bufferListOrder[] # order in which filenames go in buffer
local bufferListCommand[] # array containing the buffer changes
local BLCMD_FLAGMASK = 0x50ffff
local BLCMD_CLOSED = 0x10000
global function buffer_list( systemFlag, old_list )
{
local firstBuffer
local fn
local retval
local wf
process_ok = 1
buffer_list_index = 0
# build the buffer list names and flags arrays
#
if ( and( buffer_flags, BUFFER_SYSTEM ) && !systemFlag )
{
# ensure that the starting buffer is not a system buffer
next_buffer()
}
# fill buffer listbox with the appropriate files
#
if ( save_changes_list )
{
fill_buffer_list( or(BUFFER_SCRATCH, BUFFER_MODIFIED), 1 )
fill_buffer_list( BUFFER_MODIFIED, 0 )
# if no buffers need saving, just quit
if ( buffer_list_index == 0 )
return TRUE
}
else
fill_buffer_list( BLCMD_FLAGMASK, 0 )
# invoke the dialog manager
fn = buffer_list_dialog(old_list)
if ( process_ok )
{
# processing all pending commands
if ( save_changes_list )
{
retval = bufferListProcessCommands()
# remove the buffer list arrays
delete bufferListId
delete bufferListFlag
delete bufferListCommand
delete bufferListOrder
return retval
}
else
bufferListProcessCommands()
}
# remove the buffer list arrays
delete bufferListId
delete bufferListFlag
delete bufferListCommand
delete bufferListOrder
# process the return value
if ( fn )
{
create_buf_and_win_key( fn );
}
}
local function fill_buffer_list( flag_mask, match_all )
{
local firstBuffer, systemFlag, fn
firstBuffer = current_buffer;
do {
# save the full file name, buffer id, and buffer flags
fn = buffer_filename
if ( fn == "" )
fn = "[" buffer_name "]"
if ( match_all && (flag_mask == and(buffer_flags, flag_mask)) )
{
if ( !(fn in bufferListId) )
{
bufferListId[ fn ] = current_buffer
bufferListFlag[ fn ] = and( buffer_flags, BLCMD_FLAGMASK )
bufferListOrder[ buffer_list_index ] = fn
buffer_list_index++
}
}
if ( !match_all && and(buffer_flags, flag_mask) )
{
if ( !(fn in bufferListId) )
{
bufferListId[ fn ] = current_buffer
bufferListFlag[ fn ] = and( buffer_flags, BLCMD_FLAGMASK )
bufferListOrder[ buffer_list_index ] = fn
buffer_list_index++
}
}
next_buffer( "", systemFlag, 1 )
} while ( current_buffer != firstBuffer )
}
# Update the list box to reflect the current buffer list.
# The input argument is the changed selection. If null, the
# entire list is updated.
#
global function bufferListUpdate( sel, fnSelected )
{
local attrib
local reselect
local index
# Clear the list box
#
if ( fnSelected )
{
set_dialog_item(dhBufferList, IDL_LIST, DAC_DELETE_INDEX, sel )
attrib = bufferListAttributes( fnSelected ) # bufferListOrder[index] )
set_dialog_item( dhBufferList, IDL_LIST, DAC_ADD_INDEX, attrib, sel )
set_dialog_item( dhBufferList, IDL_LIST, DAC_SELECT_INDEX, sel)
return
}
set_dialog_item(dhBufferList, IDL_LIST, DAC_DELETE_ITEM )
# Fill the list box with the names obtained above
#
for ( index in bufferListOrder )
{
attrib = bufferListAttributes( bufferListOrder[index] )
set_dialog_item( dhBufferList, IDL_LIST, DAC_ADD_ITEM, attrib)
# find updated selection
if ( bufferListOrder[index] == fnSelected )
reselect = attrib
}
# Reset the selection to where it was before the current change
#
if ( reselect )
set_dialog_item( dhBufferList, IDL_LIST, DAC_SELECT_ITEM, reselect, 0)
else
set_dialog_item( dhBufferList, IDL_LIST, DAC_SELECT_ITEM,
bufferListOrder[ 0 ], 0)
}
# determine the name of the currently selected buffer in the list box
#
global function bufferListSelected()
{
# local fn
# Is a buffer currently selected?
return query_dialog_item(dhBufferList, IDL_LIST, DAC_FIRST_INDEX )
}
global function bufferListText(index)
{
local fn
return bufferListOrder[ index ]
}
# determine the name of the next selected buffer in the list box
#
global function bufferListNextSelected()
{
local index
# Is another buffer currently selected?
index = query_dialog_item(dhBufferList, IDL_LIST, DAC_NEXT_INDEX )
return index
}
# given a buffer name, assemble a string which indicates the current
# attributes
#
local function bufferListAttributes( fn )
{
local flags
# get the buffer flags to use
#
if ( ! ( fn in bufferListFlag ))
error( "<" fn "> internal error in bufferListAttributes" )
flags = bufferListFlag[ fn ]
if ( fn in bufferListCommand )
flags = xor( flags, bufferListCommand[ fn ] )
# append selected attributes
#
if ( and( flags, BUFFER_MODIFIED ))
fn = fn " (MOD)"
if ( and( flags, BUFFER_READ_ONLY ))
fn = fn " (RO)"
if ( and( flags, BUFFER_SCRATCH ))
fn = fn " (TEMP)"
if ( and( flags, BUFFER_SYSTEM ))
fn = fn " (System Buffer)"
if ( and( flags, BLCMD_CLOSED ))
fn = fn " (CLOSE)"
if ( and( flags, BUFFER_LOCKED ))
fn = fn " (LOCKED)"
return fn
}
# Enable the buttons which are currently available.
#
global function bufferListActivateButtons()
{
local dac, flag, i
if ( save_changes_list )
{
if ( query_dialog_item(dhBufferList, IDL_LIST, DAC_COUNT_SELECTED) == 1 )
{
flag = bufferListFlag[ bufferListOrder[bufferListSelected()] ]
# if ( and(flag, BUFFER_SCRATCH) )
# {
# set_dialog_item( dhBufferList, IDB_SAVE_FILE, DAC_DISABLE, 1 )
# set_dialog_item( dhBufferList, IDB_SCL_SAVE_ALL, DAC_DISABLE, 1 )
# }
# else if ( and(flag, BUFFER_MODIFIED) )
# {
# set_dialog_item( dhBufferList, IDB_SAVE_FILE, DAC_ENABLE, 1 )
# set_dialog_item( dhBufferList, IDB_SCL_SAVE_ALL, DAC_ENABLE, 1 )
# }
}
}
else
{
# are any buffers currently selected?
#
if ( query_dialog_item(dhBufferList, IDL_LIST, DAC_COUNT_SELECTED ))
dac = DAC_ENABLE
else
dac = DAC_DISABLE
# enable or disable all buttons
#
for ( i = FIRST_BL_BUTTON; i <= LAST_BL_BUTTON; i++ )
set_dialog_item(dhBufferList, i, dac, "" )
}
}
# Register a buffer list command for subsequent processing if
# the buffer list "Ok" button is selected. The "cmd" argument
# is the index of the selected push button.
#
function bufferListRegisterCommand( cmd )
{
local i
local fn
local sel
local flags
local saved_buffer
local fmode
# Get the first selected buffer list (if any).
sel = bufferListSelected()
if ( sel < 0 )
return;
fn = bufferListText(sel)
# Save the Selected buffer (if only one is selected)
# if ( query_dialog_item(dhBufferList, IDL_LIST, DAC_COUNT_SELECTED ) == 1 )
# sel = fn
while ( sel >= 0 )
{
# Get the current command flags upon which to operate.
#
if ( ! ( fn in bufferListFlag ))
error( "<" fn "> internal error in bufferListRegisterCommand" )
flags = 0+bufferListCommand[ fn ]
# Update the appropriate command flag. An xor operation is used
# in order to toggle the current setting.
#
if ( (cmd == IDB_SAVE_FILE) && !and( bufferListFlag[ fn ], BUFFER_SCRATCH) )
{
fmode = filemode(fn)
if ( fmode != -1 && and(fmode, _READ_ONLY) )
warning("Unable to save %s, the file must be writable.", fn)
else
{
if ( and( bufferListFlag[ fn ], BUFFER_MODIFIED ))
bufferListCommand[ fn ] = xor( flags, BUFFER_MODIFIED )
}
}
else if ( cmd == IDB_DELETE_FILE )
{
bufferListCommand[ fn ] = xor( flags, BLCMD_CLOSED )
}
else if ( cmd == IDB_READONLY )
{
bufferListCommand[ fn ] = xor( flags, BUFFER_READ_ONLY )
}
else if ( cmd == IDB_LOCK_FILE )
{
bufferListCommand[ fn ] = xor( flags, BUFFER_LOCKED )
}
if ( (cmd == IDB_SCL_SAVE_AS) || ((cmd == IDB_SAVE_FILE) && and( bufferListFlag[ fn ], BUFFER_SCRATCH)) )
{
for ( i in bufferListOrder )
{
if ( bufferListOrder[i] == fn )
break
}
if ( prefix(fn, 1) == "{" )
bufferListOrder[i] = trim( ltrim( fn, "{"), "}" )
else
bufferListOrder[i] = "{" fn "}"
bufferListFlag[ bufferListOrder[i] ] = bufferListFlag[ fn ]
bufferListFlag[ fn ] = ""
bufferListId[ bufferListOrder[i] ] = bufferListId[ fn ]
bufferListId[ fn ] = ""
if ( and( bufferListFlag[ bufferListOrder[i] ], BUFFER_SCRATCH ))
bufferListCommand[ bufferListOrder[i] ] = xor( flags, BUFFER_SCRATCH )
else
bufferListCommand[ bufferListOrder[i] ] = xor( flags, SAVE_AS_BUFFER )
bufferListCommand[ bufferListOrder[i] ] = xor( bufferListCommand[ bufferListOrder[i] ],
BUFFER_MODIFIED )
bufferListCommand[ fn ] = ""
if ( prefix(fn, 1) == "{" )
{
bufferListCommand[ bufferListOrder[i] ] = and( bufferListCommand[ bufferListOrder[i] ],
not(BUFFER_MODIFIED) )
fn = trim( ltrim( fn, "{"), "}" )
}
else
{
bufferListCommand[ bufferListOrder[i] ] = or( bufferListCommand[ bufferListOrder[i] ],
BUFFER_MODIFIED )
fn = "{" fn "}"
}
}
# Update the list box to refect the new status.
#
bufferListUpdate( sel, fn )
# Get the Next Selected Item from the BufferList (if any).
#
sel = bufferListNextSelected()
if (sel >= 0 )
fn = bufferListText(sel)
}
}
# Process the list of buffer list commands registered above.
#
function bufferListProcessCommands()
{
local fn
local bufid
local flags
local bl_buffer
local fattr
local fmode, bmode
local modeMsg
local resp
local old_sb
local errors = 0
local start_win
local buffer_closed = FALSE
# Process the changes made to each modified buffer
#
for ( fn in bufferListCommand )
{
# get the current state of changes to this buffer
if ( ! ( fn in bufferListId ))
{
error( "<" fn "> internal error in bufferListProcessCommand" )
errors = TRUE
}
else if ( ! ( fn in bufferListFlag ))
{
error( "<" fn "> internal error in bufferListProcessCommand" )
errors = TRUE
}
bufid = bufferListId[ fn ]
flags = bufferListCommand[ fn ]
# "save" command
#
if ( and( flags, BUFFER_MODIFIED ))
{
fmode = filemode(fn)
if ( fmode != -1 && and(fmode, _READ_ONLY) )
{
warning("Unable to save %s, the file must be writable.", fn)
errors++
}
else
{
# write the buffer
bl_buffer = current_buffer
current_buffer = bufid
backup_file(buffer_filename)
if (!write_buffer())
errors++
current_buffer = bl_buffer
}
}
# "save as" command
#
if ( and(flags, BUFFER_SCRATCH) || and(flags, SAVE_AS_BUFFER) )
{
# write the buffer
bl_buffer = current_buffer
current_buffer = bufid
if ( !gui_saveas() )
errors++
current_buffer = bl_buffer
}
# "close" command
#
if ( and( flags, BLCMD_CLOSED ))
{
# delete the buffer
delete_buffer( bufid )
buffer_closed = TRUE
}
# "lock" command
#
if ( and( flags, BUFFER_LOCKED ))
{
bl_buffer = current_buffer
current_buffer = bufid
if ( and(buffer_flags, BUFFER_LOCKED) )
unlock_buffer( bufid )
else
lock_buffer( bufid )
current_buffer = bl_buffer
}
# "read-only" toggle command
#
if ( and( flags, BUFFER_READ_ONLY ))
{
# update the buffer's read-only bit
if ( !and( flags, BLCMD_CLOSED ))
{
bl_buffer = current_buffer
current_buffer = bufid
buffer_flags = xor(
buffer_flags,
BUFFER_READ_ONLY )
current_buffer = bl_buffer
}
# update the disk file's read-only bit
fattr = filemode( fn )
if ( fattr >= 0 )
{
# would the disk read-only mode match the buffer's?
fmode = !!and( fattr, _READ_ONLY )
bmode = !!and( bufferListFlag[ fn ], BUFFER_READ_ONLY )
# if so, make the change to the disk file
if ( fmode == bmode )
{
modeMsg = bmode ? "writable" : "read-only"
optional_function( "disable_status_bar_prompts" )
resp = toupper( confirm( \
"make file " fn " " modeMsg \
" also? [yn]", "yYnN" ))
optional_function( "restore_status_bar" )
if ( resp == "Y" ) {
message( "making file " fn " " modeMsg )
fattr = xor( fattr, _READ_ONLY )
filemode( fn, fattr )
} else
# replace misleading "canceled" message
message( "" )
}
}
}
}
if ( buffer_closed )
{
start_win = current_window
do {
update_current_caption();
if(and(buffer_flags, BUFFER_SYSTEM))
next_buffer();
}
while( next_window("", 0, 1) != start_win )
current_window = start_win
}
update_current_view()
if ( errors )
return FALSE
else
return TRUE
}
############################################################################
## prompt user for filename for edit_file() and read_file() operations
#
global function edit_file_key( fn )
{
if ( !fn )
fn = prompt_history( "EDITFILE", "File: ", "", 1, 1, "edit_file_dialog" )
if( fn )
{
# add prompt history here so even invalid fn arguments get added.
add_prompt_history( "EDITFILE", fn )
if( edit_file( fn ))
{
display_filename()
return TRUE
}
else
{
message( "Cannot open file '%s'", fn )
return FALSE
}
}
else
return FALSE
}
global function read_file_key( fn )
{
local result = FALSE
if ( !fn )
fn = prompt_history( "EDITFILE", "File to read: ", "", 1, 1, "insert_prompt" )
if ( fn )
{
result = read_file( fn )
if ( !result )
message( "Cannot read '%s'", fn )
else
add_prompt_history( "EDITFILE", fn )
}
return result
}
############################################################################
global function delete_buffer_without_asking( bufferToDelete )
{
local priorBuffer = current_buffer
if (argcount())
current_buffer = bufferToDelete
if ( current_buffer == bufferToDelete )
{
#
# if the buffer exists, clear the buffer modified bit first
# in order to avoid the "buffer had been modified" prompt
#
buffer_flags = and( buffer_flags, not( BUFFER_MODIFIED ) )
delete_buffer()
current_buffer = priorBuffer
}
}
global function display_filename( useNotify )
{
local fn = buffer_filename
# get the status of the modified bit in the current buffer
if ( and( buffer_flags, BUFFER_MODIFIED ))
fn = fn "*"
if ( useNotify )
notify( "File: " fn )
else
message( "File: " fn )
}
global function change_output_name( fn )
{
local newOutputName
if ( fn )
newOutputName = fn
else
newOutputName = prompt("Enter new output file name: ", "", "change_output_name_dialog" )
if (newOutputName)
{
buffer_filename = newOutputName
buffer_name = path_fname(buffer_filename) path_ext(buffer_filename)
buffer_flags = or( buffer_flags, BUFFER_MODIFIED )
buffer_flags = and( buffer_flags, not(BUFFER_SCRATCH) )
# display_redraw()
update_current_caption()
update_current_view()
new_edit_file();
syntax_new_buffer();
}
return newOutputName
}
#
# paste()
#
# a synonym for insert_scrap()
#
#global function paste() #PUBLIC #INT
#{
# return insert_scrap()
#}
# cut()
#
# a synonym for delete_to_scrap()
#
#global function cut() #PUBLIC #INT
#{
# return delete_to_scrap()
#}
#
# print_buffer() is now a primitive
#
#global function print_buffer() #PUBLIC #VOID
#{
# local expand_tabs = and( buffer_flags, BUFFER_EXPAND_TABS );
#
# message( "Printing..." );
#
# buffer_flags = or( buffer_flags, BUFFER_EXPAND_TABS );
#
# if ( selection_type() != 0 )
# write_marked_block( print_device )
# else
# write_buffer( print_device )
#
#
#
# if (!expand_tabs)
# buffer_flags = and( buffer_flags, not(BUFFER_EXPAND_TABS) );
#
# message( "Printing complete." )
#}
#
# Backups will be made in the appropriate directory according to the
# following rules:
#
#
#
# Using c:\foo\test.c as the same test file
# and editor_path() is c:\cpe
#
#
# \ backup_file_ext ==
# \
# \ │
# \ ".bak" │ "" | "*"
# backup_directory == ┌────────────────────────┼──────────────────────┐
# │ │ │
# "" │ c:\cpe\backup\test.bak │ c:\cpe\backup\test.c │
# │ │ │
# ───┼────────────────────────┼──────────────────────┤
# │ │ │
# "." | "*" │ c:\foo\test.bak │ c:\foo\test.bak │
# │ │ │
# ───┼────────────────────────┼──────────────────────┤
# │ │ │
# (any dir) "~~~\" │ ~~~\test.bak │ ~~~\test.c │
# │ │ │
# └────────────────────────┴──────────────────────┘
#
# Values for backup_directory:
# "*"|"." - means the directory where the file was originally loaded from
# "~~~\" - any valid drive and path name.
# "" - use the editor_path()\backup directory
#
# Values for backup_file_ext:
# ".bak" - and valid extension to be used as the new backup file extension
# ( if !("*" or "") always uses ".bak" for now
# "" - use the current file's extension as the backup file extension
# "*" - use the current file's extension as the backup file extension
#
global backup_directory
global backup_file_ext
global function backup_file( fname )
{
local pos, bakFilename
local pname
local new_ext = ".bak";
local new_path;
local dest_drive;
local fmode;
local cwd
if (length(fname) == 0)
fname = buffer_filename
fname = buildpath( fname );
if (!cindex(fname, "."))
fname = fname "."
if (!backup_files_enabled || (filemode( fname ) == -1))
# return TRUE;
# tell caller that the backup file was not created but
# that's ok....maybe?
return 2;
else
{
# change to fname's directory.
cwd = getcwd()
chdir(path_path(fname))
if (backup_file_ext == "*" || !backup_file_ext)
new_ext = path_ext( fname );
else
new_ext = backup_file_ext;
if (substr( new_ext, 1, 1) != ".")
new_ext = "." new_ext;
new_ext = substr( new_ext, 1, 4);
if (backup_directory && backup_directory != "*" && \
backup_directory != "." )
{
backup_directory = buildpath( backup_directory );
new_path = trim( backup_directory, "\\" );
if (filemode( new_path ) != _SUBDIR )
{
warning( "Invalid backup directory path: " backup_directory "." );
backup_directory = "";
}
}
if (!backup_directory)
{
new_path = editor_path()
if (length(new_path))
new_path = new_path "backup"
else
new_path = buildpath( "\\cpe\\backup" );
if (!mkpath(new_path))
{
warning( "Cannot create backup directory '%s'.", new_path );
backup_directory = "*";
}
}
if (backup_directory == "*" || backup_directory == ".")
new_path = path_path( fname )
pname = "\\" path_fname( fname );
new_path = trim( new_path, "\\" );
bakFilename = new_path pname new_ext;
if (bakFilename == fname )
bakFilename = new_path pname ".bak"
# change back to the original working directory.
chdir(cwd)
message( "Writing backup file '%s'.", bakFilename );
dest_drive = substr( bakFilename, 1, 1 );
if (bakFilename != fname )
{
fmode = filemode( bakFilename );
if ( (fmode != -1) && and( fmode, _READ_ONLY ))
{
if (tolower(confirm( "`" bakFilename "' is read-only, overwrite?",
"yYnN")) != "y")
{
return FALSE;
}
filemode( bakFilename, _NORMAL );
}
# DWM 5/23/94 ** Do not move for backup **
# Always copy file, otherwise extended attributes are lost
# if ( dest_drive == substr( fname, 1, 1 ) )
# {
# # backing up on same disk, just rename it.
# #
# if (buffer_size * 2 >= disk_free( dest_drive ))
# {
# warning( "Not enough disk space on drive " dest_drive \
# ": no backup created!");
#
# return FALSE;
# }
#
# unlink( bakFilename );
# return rename( fname, bakFilename )
# }
# else
return filecopy( fname, bakFilename )
}
}
return FALSE
}
## write_all_buffers()
#
# write all of the modified buffers to their output file but first
# check the read/write permission of the output file. Two passes are
# made at writing the files: first, save all of the r/w files, second,
# try to save the read-only files which will generate an error.
#
global function write_all_buffers() #PUBLIC #INT
{
local old_curnt_buffer = current_buffer
local fmask = BUFFER_MODIFIED + BUFFER_READ_ONLY
local buf = current_buffer;
local exitBuffers = 0;
if ( and(buffer_flags,BUFFER_SYSTEM) )
old_curnt_buffer = next_buffer();
#
# loop through the list of buffers and write all of the
# read/write files first
#
do {
#
# see if the buffer has changed
#
if ( and( buffer_flags, fmask ) == BUFFER_MODIFIED)
{
#
# don't save read-only files this pass
#
message( buffer_filename )
if (!backup_file( buffer_filename ))
{
warning( "Unable to create backup file for `" buffer_filename "'" );
exitBuffers = 1;
}
if (write_buffer_key() < 0 )
{
exitBuffers = 1;
}
}
next_buffer()
} while (old_curnt_buffer != current_buffer)
current_buffer = buf;
message( "" )
return !exitBuffers;
}
## alter current_buffer's tab settings
#
global function tabs( stops1, stops2 ) #PUBLIC #VOID
{
if( !stops1 )
stops1 = prompt( "Enter tab stop settings: ", buffer_tabs, "tabs_dialog" )
else if (argcount() > 1)
stops1 = stops1 " " stops2;
if( stops1 )
{
buffer_tabs = stops1
message( "" )
}
}
## delete_buffer_key()
#
# A replacement for the delete_buffer() function to be bound to keys.
# It protects the user from accidently having a system buffer (such as the
# DOS buffer or help buffer) become current.
#
# The bufid argument is an optional buffer id of the buffer to delete.
# If not specified, the current buffer will be deleted.
#
# The nextFlag argument is a flag which should be set to 1 (TRUE) to have the
# deleted buffer be replaced with next_buffer() rather than prev_buffer().
# The default is prev_buffer().
#
# The return value is FALSE if the specified buffer is the only buffer or if
# they answer no to the "really delete?" prompt.
#
global function delete_buffer_key( bufid, nextFlag )
{
local priorWindow = current_window
local priorBuffer = current_buffer
local replacementBuffer
local sentinel
local resp
local eid
if(!create_new_bufwin)
{
# make the specified buffer current
if ( bufid )
current_buffer = bufid
else
bufid = current_buffer
# find the buffer to use to replace the deleted buffer. Stop
# now if the buffer to delete is the last regular buffer.
if ( nextFlag )
next_buffer()
else
prev_buffer()
# if ( current_buffer == bufid )
# {
# current_buffer = priorBuffer
# warning( "Cannot delete last buffer" )
# return FALSE
# }
# else
# {
replacementBuffer = current_buffer
current_buffer = bufid
# }
# ask for confirmation if the buffer has been modified
if ( and( buffer_flags, BUFFER_MODIFIED ))
{
current_buffer = priorBuffer
resp = toupper( confirm(
buffer_filename " has been modified. Delete [ynw]? ",
"YyNnWw" ))
if ( resp == "Y" )
{
# yes, turn off the modified bit and proceed
current_buffer = bufid
buffer_flags = xor( buffer_flags, BUFFER_MODIFIED )
}
else if ( resp == "W" )
{
# write first, clearing the modified bit as we do
current_buffer = bufid
write_buffer()
}
else
{
return FALSE
}
}
# fix all non_system windows attached to this buffer
if ( and( window_flags, WINDOW_SYSTEM ) )
sentinel = next_window()
else
sentinel = current_window
do
{
if ( bufid == current_buffer )
attach_window_buffer( current_window, replacementBuffer )
next_window("", 0, 1)
} while ( current_window != sentinel )
# restore the original window
if ( priorWindow )
current_window = priorWindow
# delete the buffer, now that no windows (other than the current
# one) are attached
current_buffer = bufid
eid = "unlock_file" # avoids compiler warning
if (function_id( eid ))
execute_function( "unlock_file " buffer_filename );
delete_buffer()
if (bufid == current_buffer)
{
# failed to delete buffer
current_buffer = priorBuffer
return FALSE
}
# restore the original buffer if it was not the one deleted
if ( priorBuffer != bufid )
current_buffer = priorBuffer
else
{
# otherwise make the new current buffer the appropriate
# non-system buffer; and display the new filename
current_buffer = replacementBuffer
display_filename()
}
}
else
{
eid = "unlock_file" # avoids compiler warning
if (function_id( eid ))
execute_function( "unlock_file " buffer_filename );
delete_buffer();
}
return TRUE
}
#global function toggle_real_space( on )
#{
# if( argcount() < 1 )
# on = !and( buffer_flags, BUFFER_REAL_SPACE_ONLY );
# else
# on = 0+on
#
# #
# # toggle the setting
# #
# if (on)
# buffer_flags = or( buffer_flags, BUFFER_REAL_SPACE_ONLY )
# else
# buffer_flags = and( buffer_flags, not(BUFFER_REAL_SPACE_ONLY) )
#
#
# message( and( buffer_flags, BUFFER_REAL_SPACE_ONLY ) \
# ? "Real space only." \
# : "Virtual space allowed." )
# return on;
#}
## toggle insert mode
#
#global function toggle_insert_mode( on )
#{
# if( argcount() < 1 )
# on = and( buffer_flags, BUFFER_OVERTYPE_MODE )
# else
# on = 0+on
#
# if ( on )
# {
# buffer_flags = and(buffer_flags, not(BUFFER_OVERTYPE_MODE ))
# message( "Insert Mode" )
# }
# else
# {
# buffer_flags = or(buffer_flags, BUFFER_OVERTYPE_MODE )
# message( "Overtype Mode" )
# }
#}
## toggle tabs to spaces
#
#global function toggle_tabs_to_spaces( on )
#{
# if( argcount() < 1 )
# on = !and( buffer_flags, BUFFER_TABS_TO_SPACES )
# else
# on = 0+on
#
# toggle_buffer_flags( BUFFER_TABS_TO_SPACES, on )
# message( "Tabs to spaces " ( on ? "enabled." : "disabled." ))
#}
#global function toggle_buffer_expand_tabs( on )
#{
# if( argcount() < 1 )
# on = !and( buffer_flags, BUFFER_EXPAND_TABS );
# else
# on = 0+on
#
# #
# # toggle the setting
# #
# if (on)
# buffer_flags = or( buffer_flags, BUFFER_EXPAND_TABS )
# else
# buffer_flags = and( buffer_flags, not(BUFFER_EXPAND_TABS) )
#
#
# message( "Buffer expand tabs %s." , on ? "on" : "off");
# return on;
#}
#global function toggle_buffer_snap2eol( on )
#{
# if( argcount() < 1 )
# on = !and( buffer_flags, BUFFER_SNAP_TO_EOL );
# else
# on = 0+on
#
# #
# # toggle the setting
# #
# if (on)
# buffer_flags = or( buffer_flags, BUFFER_SNAP_TO_EOL )
# else
# buffer_flags = and( buffer_flags, not(BUFFER_SNAP_TO_EOL) )
#
#
# message( "Buffer snap to end of line %s." , on ? "on" : "off");
# return on;
#}
global function write_buffer_key()
{
local status
if (buffer_filename)
{
if ( !and( buffer_flags, BUFFER_MODIFIED ) )
message( buffer_name ": has not been modified. " );
else if ( is_scratch_file(current_buffer) )
status = write_buffer_as()
else
{
message( buffer_name ":" );
backup_file( buffer_filename );
status = write_buffer();
if ((status >= 0) && (!errno))
message( buffer_name ":" buffer_last_line " lines written" );
## The system will display the unable to write message
##
#else
# message( "Unable to write " buffer_name );
}
}
else
warning( "buffer_filename is not defined. " );
return status
}
function write_block_or_buffer_key()
{
local fileToWrite
local status = -1
if( selection_type() == NO_SELECTION )
write_buffer_key()
else
gui_write_block()
return status;
}
global function write_buffer_as( fileToWrite )
{
local flags
local status
local old_filename = ""
if ( !fileToWrite )
fileToWrite = prompt( "Write File as: ", "", "write_buffer_as_dialog" )
if (fileToWrite)
{
backup_file( fileToWrite );
# temporarily change filename so write_buffer will update
# buffer_original_filename if write is successful
old_filename = buffer_filename
buffer_filename = fileToWrite
status = write_buffer()
if ( status != -1 )
{
buffer_name = path_fname(buffer_filename) path_ext(buffer_filename)
flags = buffer_flags
flags = and(flags, not(BUFFER_SCRATCH) )
buffer_flags = flags
buffer_flags = and(buffer_flags, not(BUFFER_MODIFIED) )
update_current_caption()
message( "Write successful." )
}
else
buffer_filename = old_filename # restore old filename
update_current_view()
new_edit_file()
syntax_new_buffer()
}
return status;
}
global function next_buffer_key()
{
next_buffer()
display_filename()
}
global function prev_buffer_key()
{
prev_buffer()
display_filename()
}
global function next_buffer_mask()
{
if(create_new_bufwin)
next_window();
else
next_buffer(window_name)
display_filename()
}
global function prev_buffer_mask()
{
if(create_new_bufwin)
prev_window();
else
prev_buffer(window_name)
display_filename()
}
global function set_buffer_mask(mask)
{
# if all files are included then reset buffer mask so title
# bar doesn't display buffer mask
if ( mask == "*.*" || mask == "*" || mask == "*." )
reset_buffer_mask()
else
window_name = mask;
# now go to the next buffer that matches the mask
next_buffer( mask )
# get the title to update
display_redraw()
}
global function reset_buffer_mask()
{
window_name = ""
# get the title to update
display_redraw()
}
global function set_buffer_mask_key()
{
local fn
if ( !fn )
fn = prompt_history( "BUFFERMASK", "Buffer filter: ", "", 1, 1, "buffer_filter_prompt" )
if( fn )
set_buffer_mask(fn)
}
global function is_scratch_file( bufferId )
{
local retval = FALSE
local old_cur_buffer = current_buffer
current_buffer = bufferId
retval = and(buffer_flags, BUFFER_SCRATCH)
current_buffer = old_cur_buffer
return retval
}
#function toggle_read_only( on )
#{
# local resp, bmode, fmode, modeMsg, old_sb, fn, fattr
#
# if( argcount() < 1 )
# on = !and( buffer_flags, BUFFER_READ_ONLY );
# else
# on = 0+on
#
# #
# # toggle the setting
# #
# if (on)
# buffer_flags = or( buffer_flags, BUFFER_READ_ONLY )
# else
# buffer_flags = and( buffer_flags, not(BUFFER_READ_ONLY) )
#
#
# message( on ? "Buffer is read-only." : "Buffer is not read-only." )
#
# # update the disk file's read-only bit
# fn = buffer_filename
# fattr = filemode( fn )
#
# if ( fattr >= 0 )
# {
# # would the disk read-only mode match the buffer's?
# fmode = !!and( fattr, _READ_ONLY )
#
# bmode = !!and( buffer_flags, BUFFER_READ_ONLY )
#
# # if so, make the change to the disk file
# if ( fmode != bmode )
# {
# modeMsg = bmode ? "read-only" : "writable"
#
# optional_function( "disable_status_bar_prompts" )
#
# resp = toupper( confirm( \
# "make file " fn " " modeMsg \
# " also? [yn]", "yYnN" ))
#
# optional_function( "restore_status_bar" )
#
# if ( resp == "Y" ) {
# message( "making file " fn " " modeMsg )
# fattr = xor( fattr, _READ_ONLY )
# filemode( fn, fattr )
# } else
# # replace misleading "canceled" message
# message( "" )
# }
# }
# return on;
#}
global function create_buf_and_win_key(fn)
{
local rc
if ( argcount() < 1 || fn == "" || fn == 0 )
fn = prompt_history( "EDITFILE", "File: ", "", 1, 1, "create_buf_and_win_prompt" )
rc = create_buf_and_win(fn)
if ( message_level == 0 )
if ( rc )
message( buffer_filename )
else if ( fn )
warning("Unable to edit " fn)
}
global function toggle_create_new_win(on)
{
local first = current_window;
local old_create_new_bufwin = create_new_bufwin;
local id;
if( argcount() < 1 )
on = !create_new_bufwin
if(!mdi_mode && on)
{
warning("One buffer per window mode is not valid in SDI mode");
return;
}
# If the user turns this feature on, we need to set up the windows
# as if he/she opened all their buffers in this mode;
# temp_create_new_bufwin == 2 if files specified on the command line at startup;
if ( on && !old_create_new_bufwin )
{
cascade_buffers();
# assigning current_window to first sometimes doesn't work if cascade_buffers() deletes windows;
# current_window = first;
}
create_new_bufwin = on;
if (and(status_bar_flags, STB_MESSAGES))
message("One buffer per window is %s", create_new_bufwin ? "on" : "off")
return on;
}
function read_line( line )
{
local text
line = ( line ) ? line : current_line
save_position();
goto_line(line);
text = read_buffer();
restore_position(TRUE);
return text
}
#
# This file contains functions to keep the editor's current directory
# synchronized with the current buffer. To use, compile this file and
# execute the function toggle_dir_sync() with a 0 to disable, a 1 to enable
# or no argument to toggle the state.
#
function toggle_dir_sync(on)
{
local handler_id = function_id("buffer_chdir")
local dir_sync_active = handler_active(EVENT.NEW_CURNT_BUFFER, handler_id)
if( argcount() < 1 )
{
on = !dir_sync_active
}
if (on)
{
if (!dir_sync_active)
{
attach_event_handler(EVENT.NEW_CURNT_BUFFER, handler_id)
if (!editor_running)
attach_event_handler(EVENT.EDITOR_RUNNING, handler_id)
buffer_chdir()
}
notify("New buffer handler activated")
}
else
{
delete_event(EVENT.NEW_CURNT_BUFFER, handler_id)
delete_event(EVENT.EDITOR_RUNNING, handler_id)
notify("New buffer handler removed")
}
}
global function handler_active(event, handler)
{
local event_list = query_event( event )
local i
for (i in event_list)
if (event_list[i] == handler)
return TRUE
return FALSE
}
function buffer_chdir()
{
local path = path_path(buffer_filename)
if (and(buffer_flags, BUFFER_SYSTEM))
return
if (editor_running && path)
chdir(path)
}
#function remove_old()
#{
# local handler_id = function_id("delete_buffer")
# local event = EVENT.DELETING_WINDOW
# delete_event(event, handler_id)
#}
#function query_bind_buff_to_win()
#{
# return handler_active(EVENT.DELETING_WINDOW, function_id("delete_buffer_in_win"))
#}
global function toggle_buffer_flags(mask, value)
{
local fn, fattr, fmode, bmode, modeMsg, resp;
local msg, suffix = "disabled.";
# If no value supplied, toggle the old value;
if(argcount() < 2)
value = xor(mask, and(buffer_flags, mask));
if ( value )
{
buffer_flags = or( buffer_flags, mask )
suffix = "enabled.";
}
else
buffer_flags = and( buffer_flags, not(mask) )
if(mask == BUFFER_EXPAND_TABS)
msg = "Buffer expand tabs " suffix;
else if(mask == BUFFER_MODIFIED)
msg = "Buffer modified " suffix;
else if(mask == BUFFER_OVERTYPE_MODE)
msg = value ? "Buffer overtype mode enabled." : "Buffer insert mode enabled";
else if(mask == BUFFER_REAL_SPACE_ONLY)
msg = "Buffer real space only " suffix;
else if(mask == BUFFER_SNAP_TO_EOL)
msg = "Buffer snap to end of line " suffix;
else if(mask == BUFFER_TABS_TO_SPACES)
msg = "Buffer tabs to spaces " suffix;
else if(mask == BUFFER_WP_ENABLED)
msg = "Buffer word processing " suffix;
else if(mask == BUFFER_READ_ONLY)
{
# Special processing for read only toggle;
# update the disk file's read-only bit
fn = buffer_filename
fattr = filemode( fn )
if ( fattr >= 0 )
{
# would the disk read-only mode match the buffer's?
fmode = !!and( fattr, _READ_ONLY )
bmode = !!and( buffer_flags, BUFFER_READ_ONLY )
# if so, make the change to the disk file
if ( fmode != bmode )
{
modeMsg = bmode ? "read-only" : "writable"
optional_function( "disable_status_bar_prompts" )
resp = toupper( confirm( \
"make file " fn " " modeMsg \
" also? [yn]", "yYnN" ))
optional_function( "restore_status_bar" )
if ( resp == "Y" )
{
message( "making file " fn " " modeMsg )
fattr = xor( fattr, _READ_ONLY )
filemode( fn, fattr )
}
else
message( "" )
}
}
}
else
msg = "Buffer mode " suffix;
message(msg);
return value;
}