home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
p2demo21.exe
/
PEL
/
BOOKMARK.PEL
< prev
next >
Wrap
Text File
|
1995-03-17
|
20KB
|
816 lines
# $Header: P:\source\wmacros\bookmark.pev 1.42 17 Mar 1995 09:50:04 NOBLE $
##############################################################################
#
# 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: bookmark.pel $: Support for Marked Blocks
## $Tabs:4 7$
# global markid assignments
#local MIN_BOOKMARK = 1 # window/buffer/pos bookmarks
#local MAX_BOOKMARK = 10
# selection types (constants):
global NO_SELECTION = 0
global NORMAL_SELECTION = 1
global COLUMN_SELECTION = 2
global LINE_SELECTION = 3
global INCLUSIVE_SELECTION = 4
local MULTIPLE_SELECTION = 1
local event_defined = 0
### system-wide bookmarks
global TEMP_MARK = 999 # generic temporary
# values above this are illegal
global bookmark_table # bookmark_table[i].file - filename of bookmark "i"
# bookmark_table[i].number - markid of bookmark "i"
# bookmark_table[i].buff_id - buffer id of bookmark "i"
global function goto_bookmark( name )
{
local num
local bookmarksInUse
local n = 0
local i
local filename;
local success = FALSE
for ( i in bookmark_table )
{
n++
# bookmarksInUse = bookmarksInUse " " i
# else
# bookmarksInUse = num = i
}
if ( n == 0 )
{
warning( "No bookmarks defined." )
return
}
# if ( n >= 1 )
# {
# name = prompt_history( "BOOKMARKS", "Enter bookmark to jump to [" \
# bookmarksInUse "]: ", num )
# }
if ( name == "" )
name = prompt_history("BOOKMARKS", "Enter bookmark to jump to : ", "", 0, 0, "goto_bookmark_prompt");
if ( name in bookmark_table )
{
filename = bookmark_table[name].file;
if ( create_buf_and_win(filename) )
{
goto_mark(bookmark_table[name].number)
display_filename()
success = TRUE
}
else
warning( "Error: could not access file: %s", filename )
}
else
message( "Bookmark %s is undefined.", name )
return success
}
global function bookmark_defined(name)
{
local rc = 0
local start_buff = current_buffer
if (name in bookmark_table)
{
current_buffer = bookmark_table[name].buff_id
rc = mark_defined(bookmark_table[name].number);
}
current_buffer = start_buff
return rc;
}
global function delete_bookmark(name)
{
local start_buff = current_buffer
# delete old mark if one exists.
if (name in bookmark_table)
{
current_buffer = bookmark_table[name].buff_id
delete_mark(bookmark_table[name].number);
delete(bookmark_table[name])
}
current_buffer = start_buff
}
# place a bookmark at the cursor (companion to goto_bookmark)
#
global function place_bookmark( name, noPrompt, system_mark )
{
local i, num, mark = TRUE;
local max_name = 0; # highest current mark name
# If no argument was given, prompt for a bookmark name to
# use, offering the next numerically available bookmark as a default.
if(name == "")
{
name = 0;
for(name in bookmark_table)
if (name+0 > max_name)
max_name = name+0
max_name += 1;
name = max_name "";
name = prompt_history("BOOKMARKS", "Bookmark name to drop: ", name, 1, 1, "place_bookmark_prompt")
if(!name)
mark = FALSE;
}
else
add_prompt_history("BOOKMARKS", name)
if(!noPrompt)
{
if( (name in bookmark_table) &&
(tolower(confirm("Reuse bookmark '" name "'? [yn]", "yYnN")) != "y"))
{
mark = FALSE;
}
}
if(mark)
{
# delete old mark if one exists.
if (name in bookmark_table)
delete_mark(bookmark_table[name].number);
num = pick_unused_mark();
create_mark( num, current_line, current_column, TRUE );
bookmark_table[name].number = num;
bookmark_table[name].file = buffer_filename;
bookmark_table[name].system = system_mark;
bookmark_table[name].buff_id = current_buffer;
message( "Dropped bookmark %s", name )
display_update();
# the next pointless looking line forces the line numbers to be redrawn
# which shows a bookmark marker immediately
linenumber_format = linenumber_format;
if(!event_defined)
{
event_defined = TRUE;
attach_event_handler(EVENT.DELETING_BUFFER, function_id("buffer_delete_bookmarks"));
}
}
return mark
}
global function buffer_delete_bookmarks()
{
local i;
if(!and(buffer_flags, BUFFER_SYSTEM))
for(i in bookmark_table)
if(bookmark_table[i].buff_id == current_buffer)
{
delete_mark(bookmark_table[i].number)
delete bookmark_table[i];
}
}
global function bookmark_delete_pressed(list_line, data_line)
{
local mark, pos, msg, name, array, start_buff;
start_buff = current_buffer;
pos = match(list_line, ":");
name = substr(list_line, 1, pos-1);
split(data_line, array);
if(name in bookmark_table)
{
edit_file(array[1]);
mark = bookmark_table[name].number;
delete_mark(mark);
delete bookmark_table[name];
}
else
{
# restore the deleted bookmark when the dialog is still visible
# (the user pressed the Delete button twice)
edit_file(array[1]);
goto_pos(array[2], array[3]);
place_bookmark(name);
}
current_buffer = start_buff;
# this is that same goofy thing we have to do in place_bookmark to redraw line numbers.
linenumber_format = linenumber_format;
update_current_view();
}
global function build_bookmark_list()
{
local list[]
local name
local start_buff = current_buffer
local line, column, filename, mark, text;
for(name in bookmark_table)
if ( !bookmark_table[name].system )
if(bookmark_table[name].buff_id)
{
current_buffer = bookmark_table[name].buff_id
mark = bookmark_table[name].number;
line = mark_line(mark);
column = mark_column(mark);
filename = bookmark_table[name].file;
save_position();
goto_line(line);
goto_bol();
text = read_buffer();
restore_position(TRUE);
list[name] = name ": " filename " ( " line " , " column " ) : " text;
}
current_buffer = start_buff
return list
}
global function bookmark_list()
{
local dhList, line, column, filename, name, mark, data, text,
found = FALSE, start_buff;
dhList = create_list_dialog( "Bookmark List", editor_helpfile, "bookmark_list_callback" );
start_buff = current_buffer;
for(name in bookmark_table)
{
if ( !bookmark_table[name].system )
if(bookmark_table[name].buff_id)
{
current_buffer = bookmark_table[name].buff_id
mark = bookmark_table[name].number;
line = mark_line(mark);
column = mark_column(mark);
filename = bookmark_table[name].file;
save_position();
goto_line(line);
goto_bol();
text = read_buffer();
restore_position(TRUE);
data = name ": " filename " ( " line " , " column " ) : " text;
add_item_to_list(dhList, data, filename, line, column, -1);
found = TRUE;
}
}
current_buffer = start_buff;
if(found)
begin_list_dialog(dhList);
else
{
delete_list_dialog(dhList);
warning("No bookmarks set");
}
}
function write_block_key()
{
local fileToWrite
local status
if( selection_type() == NO_SELECTION )
{
if (buffer_filename)
{
if ( !and( buffer_flags, BUFFER_MODIFIED ) )
message( buffer_name ": has not been modified. " );
else if ( is_scratch_file(current_buffer) )
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" );
}
}
else
warning( "buffer_filename is not defined. " );
}
else
{
fileToWrite = prompt( "Write block as: ", "" )
if (fileToWrite)
{
backup_file( fileToWrite );
if (write_marked_block( fileToWrite ))
message("Write successful.")
}
}
}
local function set_anchor_mark( mtype )
{
local otype = selection_type()
if ( otype == mtype )
{
# There is a currently a selection of the specified type.
# If the selection is "floating" with the cursor as one
# endpoint, make the selection cursor independent if multiple
# selections are enabled.
# Otherwise remove the existing selection.
#
if ( ( selection_mode == MULTIPLE_SELECTION ) &&
(( selection_mark_top() == 0 ) ||
( selection_mark_bottom() == 0 )) )
end_selection()
else if ( selection_mode == MULTIPLE_SELECTION )
begin_selection(mtype)
else
remove_selection()
}
else
{
# Create a new selection
#
remove_selection()
begin_selection( mtype )
}
}
global function set_exclusive_mark()
{
set_anchor_mark( NORMAL_SELECTION );
}
global function set_column_mark()
{
set_anchor_mark( COLUMN_SELECTION );
}
global function set_line_mark()
{
set_anchor_mark( LINE_SELECTION );
}
global function set_inclusive_mark()
{
set_anchor_mark( INCLUSIVE_SELECTION );
}
global function copy_to_scrap_key( append )
{
local status;
if (argcount() && append)
status = append_to_scrap();
else
status = copy_to_scrap();
}
#
# mark_paragraph()
#
# mark the current paragraph by first searching backward to the
# beginning of the paragraph (a blank line) and then searching forwards
# to the end of the paragraph ( a blank line ).
#
global function mark_paragraph() #PUBLIC #INT
{
# find beginning of paragraph
#
if ( !search("^[ \t]*$", SEARCH_BKWD_REGEX_MAX) )
goto_buffer_top();
search("[ \t]*\\c[^ \t]", SEARCH_FWD_REGEX_MAX)
begin_selection( NORMAL_SELECTION )
# find end of paragraph
#
if ( !search("^[ \t]*$", SEARCH_FWD_REGEX_MAX) )
goto_buffer_bottom()
end_selection()
}
## marked_region_size()
#
# Returns the number of characters included in the current highlight.
# If the current selection is a column block, return the area.
# (Note that column block may include virtual space, so the area may
# differ from the number of characters in the highlight.)
#
global function marked_region_size()
{
local st
local height, width
local begin
local markid
local ret
# ensure that a highlight exists
st = selection_type()
if ( !st )
return 0
save_position()
# get the position at the top of the selected region
#
goto_mark( selection_mark_top() )
if ( st == COLUMN_SELECTION )
{
height = current_line
width = current_column
}
else
{
if ( st == LINE_SELECTION )
goto_bol() # include all of the first line
begin = buffer_offset
}
# get the position at the bottom of the selected region
#
markid = selection_mark_bottom()
if ( markid )
goto_mark( markid )
else
{
restore_position( 1 )
save_position()
}
if ( st == COLUMN_SELECTION )
{
height = current_line - height + 1
width = ( width > current_column ) \
? width - current_column + 1 \
: current_column - width + 1
ret = height * width # area
}
else
{
if ( st == LINE_SELECTION )
{
goto_eol() # include all of the last line
next_char()
}
else if ( st == INCLUSIVE_SELECTION )
{
# include character at cursor
next_char()
}
ret = buffer_offset - begin
}
restore_position( 1 )
return ret
}
#===========================================================================
# primitives which extend the current selection
global function extend_selection_init()
{
# make sure selection is active
if ( !selection_type() )
begin_selection( NORMAL_SELECTION )
}
global function extend_selection()
{
local mark1, mark2
local diff1, diff2
# Get current selection endpoints
#
mark1 = selection_mark_top()
mark2 = selection_mark_bottom()
if ( !mark1 || !mark2 )
{
end_selection()
return
}
# Find mark closest to cursor
#
diff1 = distance_between_marks( mark1, 0 )
diff2 = distance_between_marks( mark2, 0 )
if ( diff1 < 0 )
diff1 = 0 - diff1
if ( diff2 < 0 )
diff2 = 0 - diff2
# Move the closer mark to the current cursor position
#
create_mark( ((diff1 < diff2) ? mark1 : mark2) )
}
#===========================================================================
# functions to save and restore the current selection (stack of 1)
#
local selectionArray
global function save_selection()
{
selectionArray.type = selection_type()
if ( selectionArray.type )
{
# Save the original cursor position.
#
selectionArray.curpos = create_mark( pick_unused_mark() )
# Save the id numbers of the two bookmarks now in use, in
# case they are permanent bookmarks assigned by the user.
#
selectionArray.m1 = selection_mark_top()
selectionArray.m2 = selection_mark_bottom()
if ( !selectionArray.m1 )
{
selectionArray.m1 = selectionArray.m2
selectionArray.m2 = 0
}
# Make copies of the two selection endpoints
#
goto_mark( selectionArray.m2 )
selectionArray.temp2 = create_mark( pick_unused_mark() )
goto_mark( selectionArray.m1 )
selectionArray.temp1 = create_mark( pick_unused_mark() )
# Now it is safe to remove the selection.
remove_selection()
# Return to the original cursor position
goto_mark( selectionArray.curpos )
update_current_view()
}
}
global function restore_selection()
{
if ( selectionArray )
{
if ( selectionArray.type )
{
# restore the first endpoint
#
if ( !begin_selection( selectionArray.type, selectionArray.m1 ))
{
# m1 no longer exists, create a new one
begin_selection( selectionArray.type, selectionArray.temp1 )
}
else
delete_mark( selectionArray.temp1 )
# Restore the second endpoint.
#
if ( selectionArray.m2 )
{
if ( !end_selection( selectionArray.m2 ))
{
# m2 no longer exists, create a new one
end_selection( selectionArray.temp2 )
}
}
else
delete_mark( selectionArray.temp2 )
# Return the cursor to the original location.
goto_mark( selectionArray.curpos )
delete_mark( selectionArray.curpos )
update_current_view()
}
delete( selectionArray )
}
}
#===========================================================================
#
# The following functions are retained from SPE v1.1 for forward
# compatibility.
#
global function region_type()
{
return selection_type()
}
global function swap_selection_marks()
{
swap_marks( selection_mark_top(), selection_mark_bottom() )
}
local anchorStack = initStack(anchorStack);
local selectionStack = initStack(selectionStack);
# CFV redesigned so that it matched the documentation from pel manuals
# 9/24/93
global function drop_anchor( type, mark1, mark2 )
{
local retval = 0
# message("drop anchor: type = %d; mark1 = %d; mark2 = %d",type, mark1, mark2);
# save previous region type
selectionStack = pushStack( selectionStack, selection_type() )
if ( argcount() == 0 )
type = NORMAL_SELECTION
if ( argcount() == 3 )
{
if (mark_defined(mark1))
{
if (mark_defined(mark2))
{
goto_mark(mark1);
save_position(); # saves cursor postion of mark1
begin_selection( type, mark1 )
goto_mark(mark2);
return TRUE;
}
else
return FALSE;
}
else
return FALSE;
}
save_position(); # save current cursor position
if ( argcount() == 2 ) {
if ( mark_defined(mark1) ) {
begin_selection( type, mark1 )
return TRUE;
}
else
return FALSE
}
else {
begin_selection( type )
return TRUE
}
}
# CFV changed raise anchor so that it works like the manual says it should
# 9/24/93
global function raise_anchor()
{
local mark1;
local cur_col = current_column;
local cur_line = current_line;
# local mark2 = create_mark( pick_unused_mark() );
mark1 = restore_position( 0 );
# message("raise anchor");
if ( mark1 )
{
# if (restore_position( 0 ) ) {
mark1 = restore_position( 1 );
# warning("first restore pos = %d", mark1);
if ( mark1 )
{
# if ( restore_position( 1 ) ) {
save_position();
begin_selection( topOfStack( selectionStack ) );
goto_pos(cur_line,cur_col);
}
else
{
remove_selection();
}
selectionStack = popStack( selectionStack );
return TRUE
} # if mark1
else
{
# warning("removing selection")
remove_selection();
return FALSE
}
}
# CFV changed so that it works like the manual says it should
# 9/24/93
global function toggle_anchor( type )
{
if( selection_type() == NO_SELECTION )
{
if ( argcount() == 0 )
type = NORMAL_SELECTION
drop_anchor( type )
return TRUE
}
else
{
raise_anchor()
return FALSE;
}
}
local function initStack( stack )
{
delete( stack )
stack[0] = 0;
return stack;
}
local function pushStack( stack, element )
{
local top = ++stack[0];
stack[top] = element;
return stack;
}
local function topOfStack( stack )
{
local top = stack[0];
if (top)
{
return stack[top];
}
else
return FALSE;
}
local function popStack( stack )
{
local top = stack[0];
local retval
if (top)
{
delete(stack[top] );
--stack[0];
return stack;
}
}
global function resetAnchorStacks()
{
local i= 0;
while (++i <= anchorStack[0])
delete_mark( anchorStack[i] );
anchorStack = initStack( anchorStack );
selectionStack = initStack( selectionStack );
remove_selection();
}