home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
p2demo21.exe
/
PEL
/
EMACS.PEL
< prev
next >
Wrap
Text File
|
1995-03-22
|
70KB
|
2,746 lines
# $Header: P:\source\wmacros\emacs.pev 1.55 22 Mar 1995 11:01:50 PFHJXM0 $
## $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: emacs.pel $: Key bindings for emacs compatibility
# local variables:
local markx;
local marky;
local last_column;
local last_command = 0;
local KILL = 1;
local VERT = 2;
local YANK = 3;
local search_strings;
local search_string_count = 0;
local current_search_string = 0;
local line_prefix = "";
local emacc = 0;
local keymac;
local savedmacs;
local fid, tid;
local NUMKILLBUFFERS = 30;
local killbuffers;
local currentkill=0;
local lastyankx1 = (-1);
local lastyanky1 = (-1);
local lastyankx2 = (-1);
local lastyanky2 = (-1);
local errfile;
local makebuffer = 0;
local em_sta_file = 0;
local em_sta_name = "emem.sta";
# local old_default_buffer_flags;
local old_search_flags;
global ctrlxr_keymap;
global ctrlx_keymap;
global emacs_keymap = -1;
global emacs_music = 0;
local comments;
local matching;
# establish emacs mode:
global function emacs()
{
local prevPauseOnError, first;
if (emulation_mode != "emacs")
{
# attach_event_handler(EVENT.EDITOR_RUNNING, function_id("em_change_buffer_flags"));
emulation_mode = "emacs";
execute_event_handler(EVENT.EMULATION_CHANGED);
attach_event_handler(EVENT.EMULATION_CHANGED, function_id("emacs_exit"));
attach_event_handler(EVENT.MOUSE_LEFT_DOWN, function_id("em_mouse_set_mark"));
# attach_event_handler(EVENT.EMULATION_CHANGED, function_id("em_change_buffer_flags"));
# pause if an error or warning occurs during initialization
prevPauseOnError = pause_on_error
pause_on_error = 1
# old_default_buffer_flags = default_buffer_flags;
old_search_flags = search_flags;
markx = 0;
marky = 0;
# toggle_dialog(1);
if (!em_read_state())
emacs_set_keymap()
# default_buffer_flags = 0 + BUFFER_EXPAND_TABS + BUFFER_REAL_SPACE_ONLY \
# + BUFFER_SNAP_TO_EOL + BUFFER_TABS_TO_SPACES;
emacs_menu();
em_fix_toolbar();
pause_on_error = prevPauseOnError;
}
return 23;
}
global function emacs_set_keymap()
{
local i, name;
# Enable the keymap. Subsequent mods to the keymap will persist
# through the end of the session.
# Only one-time initialization should follow this point.
if( emacs_keymap >= 0 )
{
current_keymap = emacs_keymap
return
}
else
{
em_comments(); # Initialize comment structure array
current_keymap = ctrlxr_keymap = create_keymap(empty_keymap);
assign_ctrlxr_keys()
# assert: ctrlxr_keymap exists and has the C-X r keys mapped to it.
current_keymap = ctrlx_keymap = create_keymap(empty_keymap);
assign_ctrlx_keys()
# assert: ctrlx_keymap exists and has the C-X keys mapped to it.
current_keymap = emacs_keymap = create_keymap(ascii_keymap);
assign_emacs_keys()
# assert: emacs_keymap exists and has the emacs keys mapped to it.
# assign_mouse_buttons()
optional_function("local_keys")
for (i = 0; i < NUMKILLBUFFERS; i++)
{
name = "killbufer" i "";
killbuffers[i].bufid = create_buffer( name, name, BUFFER_SYSTEM+BUFFER_NO_UNDO);
}
# assert: NUMKILLBUFFERS empty system buffers exist in the killbuffers array.
}
}
#global function em_change_buffer_flags()
#{
# first = current_buffer;
# do {
# current_buffer = next_buffer();
# if(!and(buffer_flags, BUFFER_SYSTEM))
# buffer_flags = default_buffer_flags;
# } while(current_buffer != first);
#
#}
local function em_comments()
{
comments[".c"] = "/* */";
comments[".h"] = "/* */";
comments[".cpp"] = "// ";
comments[".hpp"] = "// ";
comments[".pel"] = "# ";
comments[".pas"] = "(* *)";
matching["("] = ")";
matching["{"] = "}";
matching["["] = "]";
}
global function emacs_menu()
{
local prefix;
local menu = menu_info( 0, MI_MENUHANDLE )
local temp_array
# modify what functions are called by the cut, copy and paste functions
if(!(fid[IDM_CUT] || fid[IDM_COPY] || fid[IDM_PASTE]))
{
fid[IDM_CUT] = menu_functions[IDM_CUT]
fid[IDM_COPY] = menu_functions[IDM_COPY]
fid[IDM_PASTE] = menu_functions[IDM_PASTE]
}
menu_functions[IDM_CUT] = "em_kill_region"
menu_functions[IDM_COPY] = "em_copy_to_scrap"
menu_functions[IDM_PASTE] = "em_yank"
if ( menu )
{
# add keys to menu that contain non-special-dialog box functions
# set the default keymap
set_default_keymap( emacs_keymap )
temp_array[ IDM_UNDO ].mapped = "em_undo"
temp_array[ IDM_CUT ].mapped = "em_kill_region"
temp_array[ IDM_COPY ].mapped = "em_copy_to_scrap"
temp_array[ IDM_PASTE ].mapped = "em_yank"
temp_array[ IDM_UPPER ].mapped = "em_upper"
temp_array[ IDM_LOWER ].mapped = "em_lower"
temp_array[ IDM_ISEARCH ].mapped = "em_i_search"
temp_array[ IDM_CHANGE ].mapped = "em_query_replace"
temp_array[ IDM_COMMAND ].mapped = "em_invoke_function"
prefix = "Ctrl+x-";
temp_array[ IDM_ADDFILE ] = set_menu_mappings( "em_find_file", prefix, ctrlx_keymap )
temp_array[ IDM_INSERTFILE ] = set_menu_mappings( "em_insert_file", prefix, ctrlx_keymap )
temp_array[ IDM_SAVE ] = set_menu_mappings( "em_save_file", prefix, ctrlx_keymap )
temp_array[ IDM_SAVEAS ] = set_menu_mappings( "em_write_file", prefix, ctrlx_keymap )
temp_array[ IDM_SAVEALL ] = set_menu_mappings( "em_save_all", prefix, ctrlx_keymap )
temp_array[ IDM_EXIT ] = set_menu_mappings( "em_exit", prefix, ctrlx_keymap )
temp_array[ IDM_UNDO ] = set_menu_mappings( "em_undo", prefix, ctrlx_keymap )
temp_array[ IDM_REDO ] = set_menu_mappings( "em_redo", prefix, ctrlx_keymap )
temp_array[ IDM_GOTOLINE ] = set_menu_mappings( "em_goto_line", prefix, ctrlx_keymap )
temp_array[ IDM_NEXT ] = set_menu_mappings( "em_next_buffer", prefix, ctrlx_keymap )
temp_array[ IDM_PREV ] = set_menu_mappings( "em_prev_buffer", prefix, ctrlx_keymap )
temp_array[ IDM_CLOSE ] = set_menu_mappings( "em_kill_buffer", prefix, ctrlx_keymap )
temp_array[ IDM_BUFFERLIST ] = set_menu_mappings( "em_bufed", prefix, ctrlx_keymap )
temp_array[ IDM_HSPLIT_WINDOW ] = set_menu_mappings( "em_horizontal_window", prefix, ctrlx_keymap )
temp_array[ IDM_VSPLIT_WINDOW ] = set_menu_mappings( "em_vertical_window", prefix, ctrlx_keymap )
mapped_menu_functions = temp_array
}
}
global function em_fix_toolbar()
{
local tbhand;
if ( !( tbhand = toolbar_info(0) ) )
return FALSE
if(!(tid[IDM_CUT] || tid[IDM_COPY] || tid[IDM_PASTE]))
{
tid[IDM_CUT] = toolbar_info(tbhand, TI_FUNCTIONID, IDM_CUT, 1);
tid[IDM_COPY] = toolbar_info(tbhand, TI_FUNCTIONID, IDM_COPY, 1);
tid[IDM_PASTE] = toolbar_info(tbhand, TI_FUNCTIONID, IDM_PASTE, 1);
}
modify_toolbaritem(tbhand, IDM_CUT, TI_FUNCTIONID, function_id("em_kill_region"));
modify_toolbaritem(tbhand, IDM_COPY, TI_FUNCTIONID, function_id("em_copy_to_scrap"));
modify_toolbaritem(tbhand, IDM_PASTE, TI_FUNCTIONID, function_id("em_yank"));
}
###############################################################################
# pre: current_keymap == emacs_keymap == ascii_keymap
# post: current_keymap == emacs_keymap == emacs standard keymappings
###############################################################################
global function assign_emacs_keys()
{
local i;
for(i = 32; i < 128; i++) # perform normal operations
assign_key(int_to_key(i), "donormal");
# Normal keys that do extra things
assign_key( "]", "paren" ); # find matching character
assign_key( "}", "paren" );
assign_key( ")", "paren" );
# Main keyboard special keys that do extra things
assign_key( "<Backspace>", "em_bksp" ); # delete previous character
assign_key( "<Enter>", "em_insert_newline" ); # insert a new line
assign_key( "<Esc>", "em_get_arg 0"); # allows user to enter integers into the argument
assign_key( "<Num-5>", "em_scroll_window_middle" ); # centers the current window around current line
# Alt (meta) functions
assign_key( "<Alt-Backspace>", "em_delete_prev_word" ); #
assign_key( "<Alt-Delete>", "em_delete_prev_word"); # deletes word to the left
assign_key( "<Alt-\\>", "em_remove_white_space" ); #
# assign_key( "<Alt-,>", "em_goto_window_top"); # moves cursor to the top of the window
assign_key( "<Alt-.>", "em_tags"); # finds the first occurence of the tag under the cursor
# assign_key( "<Alt-/>", "expand_template" ); #
set_expand_template_key("<Alt-/>");
assign_key( "<Alt-;>", "em_start_comment"); # moves cursor to the bottom of the window
assign_key( "<Alt-A>", "em_prev_sentence" ); # goes to the beginning of previous sentence
assign_key( "<Alt-B>", "em_prev_word" ); # goes to the beginning of word
assign_key( "<Alt-C>", "em_capitalize" );
assign_key( "<Alt-D>", "em_delete_next_word"); # deletes word to the right
assign_key( "<Alt-E>", "em_next_sentence" ); # goes to beginning of next sentence
assign_key( "<Alt-F>", "em_next_word" ); # goes to the beginning of the next word
assign_key( "<Alt-H>", "em_mark_paragraph" );
assign_key( "<Alt-K>", "em_delete_next_sentence"); # deletes next sentence
assign_key( "<Alt-L>", "em_lower" );
assign_key( "<Alt-Q>", "em_wrap_paragraph" );
assign_key( "<Alt-S>", "em_center" );
assign_key( "<Alt-T>", "em_transpose_words" );
assign_key( "<Alt-U>", "em_upper" );
assign_key( "<Alt-V>", "em_page_up" ); # scrolls the window up one page
assign_key( "<Alt-W>", "em_copy_to_scrap" ); # copies the marked region to the next kill buffer
assign_key( "<Alt-X>", "em_invoke_function"); # brings up the command dialog
assign_key( "<Alt-Y>", "em_yankpop" );
assign_key( "<Alt-Z>", "em_delete_to_char" );
assign_key( "<Alt-F4>", "em_exit" );
# alt-shift functions
assign_key( "<Alt-Shift->>", "em_goto_buffer_bottom" ); # moves cursor to the bottom of the buffer
assign_key( "<Alt-Shift-<>", "em_goto_buffer_top"); # moves cursor to the top of the buffer
assign_key( "<Alt-Shift-{>", "em_prev_paragraph"); # moves cursor to the previous blank line
assign_key( "<Alt-Shift-}>", "em_next_paragraph"); # moves cursor to the next blank line
assign_key( "<Alt-Shift-%>", "em_query_replace"); # moves cursor to the next blank line
assign_key( "<Alt-Shift-!>", "em_shell" );
assign_key( "<Alt-Shift-|>", "em_filter" );
assign_key( "<Alt-Shift-@>", "em_set_far_mark" );
# normal control keys
assign_key( "<Ctrl-A>", "em_goto_bol" ); # move to beginning of the current line
assign_key( "<Ctrl-B>", "em_prev_char" ); # move to the previous character in the buffer
assign_key( "<Ctrl-D>", "em_delete_chars" ); # delete character to the right
assign_key( "<Ctrl-E>", "em_goto_eol" ); # move to the end of the current line
assign_key( "<Ctrl-F>", "em_next_char" ); # move to the next character in the buffer
assign_key( "<Ctrl-G>", "em_cancel" ); # cancels command and restores emacs keymap
assign_key( "<Ctrl-H>", "em_help" ); # pull up the help index
assign_key( "<Ctrl-K>", "em_kill_to_eol" ); # kills to end of line
assign_key( "<Ctrl-L>", "em_scroll_window_middle" ); # scrolls the current line to the center of the screen
assign_key( "<Ctrl-N>", "em_down" ); # moves the cursor down one line
assign_key( "<Ctrl-O>", "em_open_line" ); # inserts a new line after the cursor
assign_key( "<Ctrl-P>", "em_up" ); # moves the cursor up one line
assign_key( "<Ctrl-R>", "em_i_search" ); # reverse incremental search
assign_key( "<Ctrl-S>", "em_i_search" ); # forward incremental search
assign_key( "<Ctrl-T>", "em_transpose_characters" ); # transposes the two characters following the cursor
assign_key( "<Ctrl-U>", "em_quad_arg" ); # multiply the argument by 4
assign_key( "<Ctrl-V>", "em_page_down" ); # scrolls to next page down
assign_key( "<Ctrl-W>", "em_kill_region" );
assign_key( "<Ctrl-X>", "ctrlx" ); # invoke C-X key map
assign_key( "<Ctrl-Y>", "em_yank" ); # yanks last thing killed
assign_key( "<Ctrl-Z>", "em_iconify"); # minimize the editor
assign_key( "<Ctrl-1>", "em_redraw" ); # redraws the current screen
assign_key( "<Ctrl-2>", "em_revert_buffer" ); # restores the buffer to its original contents
assign_key( "<Ctrl-Shift-@>", "em_set_mark" ); # set mark
assign_key( "<Ctrl-Shift-_>", "em_undo"); # undoes the previous edit
assign_key( "<Ctrl-Space>", "em_set_column_mark" ); # set mark
# ctrl function keys
assign_key( "<Ctrl-F1>", "display_help_item" );
# ctrl-alt keys
assign_key("<Ctrl-Alt-A>", "em_prev_function"); # move to the previous function
assign_key("<Ctrl-Alt-E>", "em_next_function"); # moce to the next function
assign_key("<Ctrl-Alt-H>", "em_mark_function"); # mark a function
assign_key("<Ctrl-Alt-K>", "em_kill_to_bol"); # kills from the current position to beginning of line
assign_key("<Ctrl-Alt-R>", "em_i_search"); # invokes reverse regular expression incremental search
assign_key("<Ctrl-Alt-S>", "em_i_search"); # invokes regular expression incremental search
assign_key("<Ctrl-Alt-Shift-@>", "em_mark_sexp"); # marks an s expression
# normal function keys
# assign_key( "<F1>", "em_help" );
assign_key( "<F2>", "expand_template" );
assign_key( "<F4>", "bind_to_key" );
assign_key( "<F6>", "whatiskey" ); # find what function is bound to a key
assign_key( "<F7>", "em_cd" ); # change directories
# assign_key( "<F8>", "em_get_arg 0"); #setvar" );
assign_key( "<F9>", "em_undo" );
assign_key( "<F10>", "em_redo" );
# normal cursor keys
assign_key( "<Home>", "em_goto_window_top" );
assign_key( "<End>", "em_goto_window_bottom" );
assign_key( "<Left>", "em_prev_char" );
assign_key( "<PageDown>", "em_page_down" );
assign_key( "<PageUp>", "em_page_up" );
assign_key( "<Right>", "em_next_char" );
assign_key( "<Up>", "em_up" );
assign_key( "<Down>", "em_down" );
assign_key( "<Insert>", "em_toggle_insert_mode" );
assign_key( "<Delete>", "em_delete_chars" );
assign_key( "<Tab>", "em_indent_tab_maybe" );
}
################################################################################
#
################################################################################
global function assign_ctrlx_keys()
{
assign_key( "<Delete>", "em_delete_prev_sentence" ); # deletes the sentence before the cursor
assign_key( "<Backspace>", "em_delete_prev_sentence" ); # deletes the sentence before the cursor
assign_key( "<Ctrl-B>", "em_bufed" ); # displays the buffer list
assign_key( "<Ctrl-C>", "em_exit" ); # exits the editor
#?? assign_key( "<Ctrl-E>", "em_push" );
assign_key( "<Ctrl-F>", "em_find_file" ); # loads a buffer from a file
assign_key( "<Ctrl-G>", "em_cancel" ); # cancels command and restores emacs keymap
#?? assign_key( "<Ctrl-I>", "em_indent_rigidly" );
assign_key( "<Ctrl-L>", "em_lower_region" ); # lower cases region
assign_key( "<Ctrl-M>", "em_start_process" );
#?? assign_key( "<Ctrl-N>", "em_next_error" );
#?? assign_key( "<Ctrl-O>", "em_delete_blank_lines" );
assign_key( "<Ctrl-S>", "em_save_file" ); # saves the current buffer to disk
assign_key( "<Ctrl-T>", "em_transpose_lines" ); # transposes current line with line above
assign_key( "<Ctrl-U>", "em_upper_region" ); # upper cases region
assign_key( "<Ctrl-V>", "em_replace" ); # replace buffer with specified file
assign_key( "<Ctrl-W>", "em_write_file" ); # writes buffer to file
assign_key( "<Ctrl-X>", "em_exchange_point_and_mark" ); # echanges point and mark
#?? assign_key( "<Ctrl-Z>", "em_exit" );
assign_key( "(", "em_start_kbd_macro" ); # starts recording a macro
assign_key( ")", "em_end_kbd_macro" ); # stops recording a macro
assign_key( "[", "em_page_up" ); # move back in the the buffer by one page
assign_key( "]", "em_page_down" ); # move forward in the the buffer by one page
assign_key( "0", "em_delete_window" ); # deletes the current window
assign_key( "1", "em_one_window" ); # makes the current window the only
assign_key( "2", "em_horizontal_window" ); # splits the current window in two horizontally
assign_key( "3", "em_vertical_window" ); # splits the current window in two vertically
assign_key( "=", "em_show_point" );
assign_key( "<", "em_scroll_right" ); # scroll window right
assign_key( ">", "em_scroll_left" ); # scroll window left
assign_key( ".", "em_set_line_prefix" ); # sets the string that prefixes each new line
assign_key( "b", "em_bufed" ); # invoke buffer list
assign_key( "e", "em_last_kbd_macro" ); # play last keyboard macro
assign_key( "f", "em_set_fill_column" );
assign_key( "g", "em_goto_line" ); # go to specified line number
assign_key( "h", "em_mark_buffer" ); # mark entire buffer
assign_key( "i", "em_insert_file" ); # unserts a file into the current buffer
assign_key( "j", "em_redo" ); # redoes the last undo
assign_key( "k", "em_kill_buffer" ); # delete the buffer from the buffer list
assign_key( "l", "em_count_lines" );
assign_key( "n", "em_next_buffer" ); # make next buffer current
assign_key( "o", "em_next_window" ); # make next window current
assign_key( "p", "em_prev_buffer" ); # make previous buffer current
assign_key( "r", "em_ctrlxr" ); # switch to the ctrlxr keymap
assign_key( "s", "em_save_all" ); # saves all buffers to disk
assign_key( "u", "em_undo" ); # undoes the previous edit
assign_key( "w", "em_write_region" ); # write the specified region to disk
assign_key( "B", "em_bufed" );
assign_key( "E", "em_last_kbd_macro" );
assign_key( "F", "em_set_fill_column" );
assign_key( "G", "em_goto_line" ); # go to specified line number
assign_key( "H", "em_mark_buffer" );
assign_key( "I", "em_insert_file" ); # unserts a file into the current buffer
assign_key( "J", "em_redo" ); # redoes the last undo
assign_key( "K", "em_kill_buffer" );
assign_key( "L", "em_count_lines" );
assign_key( "N", "em_next_buffer" );
assign_key( "O", "em_next_window" );
assign_key( "P", "em_prev_buffer" );
assign_key( "R", "em_ctrlxr" );
assign_key( "S", "em_save_all" ); # saves all buffers to disk
assign_key( "U", "em_undo" ); # undoes the previous edit
assign_key( "W", "em_write_region" );
assign_key( "<Alt-N>", "name_last_kbd_macro" );
assign_key( "<Esc>", "em_cancel" );
assign_key( "<F6>", "whatiskey" );
# assign_key( "<Esc>b", "em_bufed" );
# assign_key( "<Esc>n", "name_macro" );
}
global function assign_ctrlxr_keys()
{
assign_key( "b", "em_clear_rectangle" ); # clears the defined rectangle
assign_key( "k", "em_rect_kill" ); # kills rectangle to current killbuffer
assign_key( "o", "em_rect_open" ); # shifts rectangle right by the width of the rectangle
assign_key( "p", "em_string_rectangle" ); # prefixes each line of the rectangle with the prompted string
assign_key( "r", "em_rect_copy_to_register" );
assign_key( "y", "em_rect_yank" );
assign_key( "B", "em_clear_rectangle" ); # clears the defined rectangle
assign_key( "K", "em_rect_kill" ); # kills rectangle to current killbuffer
assign_key( "O", "em_rect_open" ); # shifts rectangle right by the width of the rectangle
assign_key( "P", "em_string_rectangle" ); # prefixes each line of the rectangle with the prompted string
assign_key( "R", "em_rect_copy_to_register" );
assign_key( "Y", "em_rect_yank" );
assign_key( "<Ctrl-G>", "em_cancel" ); # cancels command and restores emacs keymap
assign_key( "<Esc>", "em_cancel" );
assign_key( "<F6>", "whatiskey" );
}
global function em_always(command, new_message)
{
local val, msg, response;
while(current_keymap != emacs_keymap)
pop_keymap();
last_command = command;
val = em_num();
if(val > 100)
{
msg = sprintf("Argument is very large (%d). Are you sure you want to use it [yn]?", val);
response = confirm(msg, "YyNn");
if(tolower(response) == "n")
val = 1;
}
if((last_message != " ") || (!new_message))
message(" ");
else
message(new_message);
return val;
}
global function ctrlx()
{
message("C-X ");
push_keymap(ctrlx_keymap);
}
global function em_ctrlxr()
{
message("C-X r");
push_keymap(ctrlxr_keymap);
}
###############################################################################
# post: if (last_command == KILL) em_num lines, including the current line from
# the current position is the last block in the current kill buffer and are
# removed from the current buffer.
# if !(last_was_kill == KILL) em_num lines, including the current line from
# the current position is the only block in the current kill buffer and are
# removed from the current buffer.
###############################################################################
global function em_kill_to_bol()
{
local normal, last = last_command;
save_position();
begin_selection(NORMAL_SELECTION);
if(emacc)
up(em_always(KILL) - 1);
else
em_always(KILL);
normal = goto_bol();
if (last == KILL)
{
em_append_to_scrap(!normal);
if(!normal)
delete_chars();
}
else
em_delete_to_scrap();
restore_position(TRUE);
end_selection();
}
###############################################################################
# post: if (last_command == KILL) em_num lines, including the current line from
# the current position is the last block in the current kill buffer and are
# removed from the current buffer.
# if !(last_was_kill == KILL) em_num lines, including the current line from
# the current position is the only block in the current kill buffer and are
# removed from the current buffer.
###############################################################################
global function em_kill_to_eol()
{
local normal, last = last_command;
save_position();
begin_selection(NORMAL_SELECTION);
if(emacc)
down(em_always(KILL) - 1);
else
em_always(KILL);
normal = goto_eol();
if (last == KILL)
{
em_append_to_scrap(!normal);
if(!normal)
delete_chars();
}
else
em_delete_to_scrap();
restore_position(TRUE);
end_selection();
}
global function em_kill_region()
{
if (last_command == KILL)
em_append_to_scrap();
else
em_delete_to_scrap();
end_selection();
em_always(KILL, "Region killed");
}
global function em_delete_next_word()
{
local last = last_command;
begin_selection(NORMAL_SELECTION);
next_word(em_always(), ">");
if (last == KILL)
em_append_to_scrap();
else em_delete_to_scrap();
end_selection();
last_command = KILL;
}
local function massage_string(instr, direction, regex)
{
local outstr = instr;
if(!regex)
outstr = quote_regex(instr);
if(direction)
outstr = outstr "\\c"
return outstr;
}
global function em_i_search()
{
local ch,
savex, savey,
char,
key_to_do,
found,
search_name,
old_current_search_string = current_search_string,
ESC = key_to_int("<Esc>"),
RET = key_to_int("<Enter>"),
BKSP = key_to_int("<Backspace>"),
CTRLR = key_to_int("<Ctrl-R>"),
CTRLS = key_to_int("<Ctrl-S>"),
CTRLG = key_to_int("<Ctrl-G>"),
CAR = key_to_int("<Ctrl-Alt-R>"),
CAS = key_to_int("<Ctrl-Alt-S>"),
MN = key_to_int("<Alt-N>"),
MP = key_to_int("<Alt-P>"),
level = 0,
pattern = "",
spattern = "",
dokey = FALSE,
direction = 1,
finished = FALSE,
wrap = FALSE,
regex = FALSE,
restore = FALSE,
flags = SEARCH_MAXIMAL_MATCH + SEARCH_FORWARD + SEARCH_REGEX
em_always();
search_name[0] = "Reverse I-Search";
search_name[1] = "I-Search";
# Set the direction and determine regular expressions for the search;
if((current_key == CTRLR) || (current_key == CAR))
{
direction = 0;
flags = and(flags,not(SEARCH_FORWARD));
}
if((current_key == CAR) || (current_key == CAS))
flags = or(flags, SEARCH_REGEX);
message(search_name[direction] " for:");
save_position();
while (!finished)
{
ch = getkey();
if (ch == BKSP)
{
# if (level) {
# restore_position(TRUE);
# restore_position(TRUE);
# level --;
# save_position();
# }
# if(direction)
# prev_char(length(pattern));
# else
# next_char(length(pattern));
# pattern = substr( pattern, 0, length( pattern ) - 1)
#
# spattern = massage_string(pattern, direction, regex);
#
# if (search(spattern, flags)) {
# message(search_name[direction] " for: %s",pattern);
# save_position();
# level++;
# }
# else {
# message("Failing " search_name[direction] " for: %s",pattern);
# restore_position(TRUE);
# save_position();
# }
savex = current_column;
savey = current_line;
if (level)
{
restore_position(TRUE);
level --;
}
if(direction)
prev_char(length(pattern));
else
next_char(length(pattern));
pattern = substr( pattern, 0, length( pattern ) - 1)
spattern = massage_string(pattern, direction, regex);
if (search(spattern, flags))
{
message(search_name[direction] " for: %s",pattern);
save_position();
level++;
}
else
{
message("Failing " search_name[direction] " for: %s",pattern);
goto_pos(savey, savex);
save_position();
level++
}
}
else if((ch == CTRLS) || (ch == CTRLR) || (ch == CAR) || (ch == CAS))
{
if (pattern == "")
pattern = search_strings[current_search_string];
# pattern = search_strings[(current_search_string + search_string_count - 1) % search_string_count];
direction = ((ch == CTRLS) || (ch == CAS));
regex = ((ch == CAS) || (ch == CAR));
spattern = massage_string(pattern, direction, regex);
if(direction)
{
flags = or(flags, SEARCH_FORWARD);
found = search(spattern, flags);
}
else
{
flags = and(flags, not(SEARCH_FORWARD));
found = search(spattern, flags+SEARCH_ADVANCE);
}
if(found)
{
if(wrap)
{
message("Wrapped " search_name[direction] " for: %s",pattern);
wrap = FALSE;
flags = and(flags, not(SEARCH_WRAPS));
}
else
message(search_name[direction] " for: %s",pattern);
save_position();
level++;
}
else
{
if(!wrap)
{
wrap = TRUE;
flags = or(flags, SEARCH_WRAPS);
message("Failing " search_name[direction] " for: %s",pattern);
}
else
{
wrap = FALSE;
flags = and(flags, not(SEARCH_WRAPS));
message("Failing Wrapped " search_name[direction] " for: %s",pattern);
}
}
}
else if(ch == MN)
{
current_search_string++;
if(current_search_string >= search_string_count)
current_search_string = 0;
pattern = search_strings[current_search_string];
message("Pattern: " search_strings[current_search_string]);
}
else if(ch == MP)
{
current_search_string--;
if(current_search_string < 0)
current_search_string = search_string_count - 1;
pattern = search_strings[current_search_string];
message("Pattern: " search_strings[current_search_string]);
}
else if(ch == CTRLG)
{
finished = TRUE;
restore = TRUE;
current_search_string = old_current_search_string;
}
else if(ch == RET)
{
finished = TRUE;
}
else if (isprint( chr(int_to_ascii(ch) )) )
{
char = chr(int_to_ascii(ch));
pattern = pattern char;
spattern = massage_string(pattern, direction, regex);
current_search_string = search_string_count;
if(direction)
prev_char(length(pattern) - 1);
else
next_char(length(pattern));
if (search(spattern, flags))
{
message(search_name[direction] " for: %s",pattern);
level++;
}
else
{
restore_position(TRUE);
# save_position();
# level++;
message("Failing " search_name[direction] " for: %s",pattern);
}
save_position();
}
else
{
finished = TRUE;
# message("key not recognized");
dokey = TRUE;
key_to_do = ch;
}
display_update();
# message(spattern);
}
# This is commented out while I try to find the goofy character bug.
while (level)
{
restore_position(FALSE);
level --;
}
restore_position(restore);
if( (!restore) && (pattern != "") &&
(current_search_string == search_string_count) )
{
search_strings[search_string_count] = pattern;
search_string_count++;
# message("getting here");
# message("ss[css] = " search_strings[search_string_count-1]);
}
message("Search complete.");
if (dokey)
invoke_function(keymap_binding(int_to_key(key_to_do)));
#execute_key_action(int_to_key(key_to_do));
}
global function em_iconify()
{
em_always();
set_editwin_property(EWC_MINIMIZE);
}
global function em_redraw()
{
em_always();
display_redraw();
message("Window redrawn");
}
global function em_revert_buffer()
{
em_always();
if(undo_index())
{
undo(1); # this is done to preserve the first cursor movement performed by the config file
if(and(buffer_flags, BUFFER_MODIFIED))
undo();
}
message("Buffer restored");
}
global function em_mouse_set_mark()
{
em_always();
marky = current_line;
markx = current_column;
message("Mark set");
}
global function em_set_far_mark()
{
marky = current_line;
markx = current_column;
save_position();
em_next_word();
begin_selection(NORMAL_SELECTION);
restore_position(TRUE);
em_always();
message("Mark set");
}
global function em_set_mark()
{
em_always();
marky = current_line;
markx = current_column;
begin_selection(NORMAL_SELECTION);
message("Mark set");
}
global function em_set_column_mark()
{
em_always();
marky = current_line;
markx = current_column;
set_column_mark();
message("Column mark set");
}
global function em_unset_mark()
{
em_always();
marky = 0;
markx = 0;
end_selection();
remove_selection();
message("Mark unset");
}
global function em_next_word()
{
next_word(em_always());
}
global function em_prev_word()
{
prev_word(em_always());
}
global function em_transpose_characters()
{
local char, goleft;
em_always();
if (current_line_length < 2)
return;
goleft = !left(1);
char = read_buffer(1);
delete_chars(1);
right(1);
insert_string(char);
left(1);
if (goleft) left(1);
}
global function em_delete_prev_word()
{
local count = em_num();
begin_selection(NORMAL_SELECTION);
prev_word(count);
if (last_command == KILL)
em_append_to_scrap();
else
em_delete_to_scrap();
end_selection();
em_always(KILL);
}
global function em_delete_prev_sentence()
{
local count = em_num();
begin_selection(NORMAL_SELECTION);
prev_sentence(count);
end_selection();
if (last_command == KILL)
em_append_to_scrap();
else
em_delete_to_scrap();
em_always(KILL);
}
global function em_capitalize()
{
local count;
count = em_always();
do
{
capitalize();
next_word();
count--;
} while(count > 0);
}
global function em_upper()
{
save_position();
begin_selection(NORMAL_SELECTION);
next_word(em_always());
upper();
restore_position(TRUE);
}
global function em_upper_region()
{
em_always();
upper();
}
global function em_delete_next_sentence()
{
begin_selection(NORMAL_SELECTION);
next_sentence(em_num());
em_kill_region();
message("Sentence deleted");
}
global function em_lower()
{
save_position();
begin_selection(NORMAL_SELECTION);
next_word(em_always());
lower();
restore_position(TRUE);
}
global function em_lower_region()
{
em_always();
lower();
}
global function em_center()
{
local s,e,c;
local ft;
em_num();
if (and(keyboard_flags,4))
{
toggle_search_flags( SEARCH_REGEX, TRUE);
search_forward();
return;
}
goto_bol();
if (!search("[^ \t]",SEARCH_FORWARD+SEARCH_REGEX))
return;
s = current_column;
goto_eol();
search("[^ \t]",SEARCH_REGEX);
e = current_column;
c = 40-((s + e)/2);
current_column = s;
ft = first_tab_distance();
indent_tabs(c/ft);
indent_columns(c%ft);
last_command = FALSE;
}
global function em_remove_white_space()
{
local col = current_column;
em_always();
search("[^ \t]|^", SEARCH_REGEX);
if(!(read_buffer(1) ~ "[ \t]"))
next_char();
col = (current_column < col) ? current_column : col;
begin_selection(NORMAL_SELECTION);
search("[^ \t]|$", SEARCH_FORWARD+SEARCH_REGEX);
delete_chars();
goto_pos(0, col);
}
global function em_goto_buffer_top()
{
em_always();
goto_buffer_top();
message("Top of buffer");
}
global function em_goto_buffer_bottom()
{
em_always();
goto_buffer_bottom();
message("Bottom of buffer");
}
global function em_shell()
{
local command;
em_always();
command = prompt("OS/2 command: ", "" );
if(command)
system(command);
}
global function em_filter()
{
local command, code;
em_always();
command = prompt_history("XMACRO", "Enter command: ", "", 1, 1, "em_filter_prompt" );
if(command)
{
code = filter(command);
if(!code)
message("Command executed successfully");
else
warning("Command encountered an error. Return code %d", code);
}
else
message("Command canceled");
}
global function bind_to_key()
{
local key, comm, ch;
comm = prompt_history("XMACRO", "Bind command: ", "", 1, 1, "em_bind_to_key_prompt" );
if (comm != "")
{
message("To key: ");
ch = getkey();
if (comm in savedmacs)
assign_key(int_to_key(ch), "replay " comm);
else
assign_key(int_to_key(ch),comm);
}
em_always();
}
global function paren()
{
local i,j, ch;
j = em_always();
ch = chr(int_to_ascii(current_key));
for (i=0; i<j; i++)
{
insert_string(ch);
save_position();
prev_char();
goto_matching();
create_linecol_selection(COLUMN_SELECTION, current_line, current_column, current_line, current_column);
play("P1P1P1P1P1");
end_selection();
if(ch == "}")
move_bracket();
restore_position(TRUE);
}
}
global function first_nonwhite()
{
local ch;
goto_bol();
do
{
ch = read_buffer(1);
if(ch)
next_char();
} while(((ch == " ") || (ch == "\t")) && (ch != ""));
return (ch == "") ? 0 : (current_column - 1);
}
local function move_bracket()
{
local start, col = first_nonwhite(), offset = "";
restore_position(TRUE);
prev_char();
start = current_column;
if(start < col)
offset = strrepeat(" ", col - start);
insert_string(offset);
next_char();
}
global function em_transpose_words()
{
local s1,e1,s2,e2, save1, save2;
em_num();
next_word();
e1 = current_column;
prev_word();
s1 = current_column;
save1 = read_buffer(e1-s1);
prev_word();
next_word();
e2 = current_column;
prev_word();
s2 = current_column;
save2 = read_buffer(e2-s2);
current_column = s1;
delete_chars(e1-s1);
insert_string(save2);
current_column = s2;
delete_chars(e2-s2);
insert_string(save1);
last_command = FALSE;
}
global function em_mark_paragraph()
{
local i,j;
j = em_always();
prev_paragraph();
em_set_mark();
for (i=0; i<j; i++)
next_paragraph();
}
global function em_query_replace()
{
em_always();
sf_replace();
}
global function em_delete_chars()
{
delete_chars(em_always());
}
global function em_down()
{
local count = em_always(last_command);
if(last_command == VERT)
goto_pos(current_line + count, last_column);
else
{
last_column = current_column;
last_command = VERT;
down(count);
}
}
global function em_insert_newline()
{
local i, j;
j = em_always();
for (i = 0; i < j; i ++)
{
auto_indent_cr()
insert_string(line_prefix);
}
}
global function em_open_line()
{
save_position()
em_insert_newline()
restore_position(TRUE)
}
global function em_insert_scrap()
{
local i, j;
j = em_always();
lastyankx1 = current_column;
lastyanky1 = current_line;
for (i = 0; i < j; i ++)
insert_scrap()
lastyankx2 = current_column;
lastyanky2 = current_line;
}
###############################################################################
# pre: killbuffers[currentkill].bufid exists
# post: killbuffers[currentkill].bufis is in the current buffer at current_position'
# current_position is next character after killbuffers[currentkill]
###############################################################################
global function em_yank()
{
local buffer = current_buffer,
count = em_always(YANK), i;
lastyankx1 = current_column;
lastyanky1 = current_line;
current_buffer = killbuffers[currentkill].bufid;
goto_buffer_top();
begin_selection(killbuffers[currentkill].type);
goto_buffer_bottom();
if(killbuffers[currentkill].type == COLUMN_SELECTION)
prev_char();
copy_to_scrap();
end_selection();
current_buffer = buffer;
for(i=0; i<count; i++)
insert_scrap();
lastyankx2 = current_column;
lastyanky2 = current_line;
}
global function em_next_char()
{
next_char(em_always());
}
global function em_next_paragraph()
{
next_paragraph(em_always())
}
global function em_next_function()
{
next_section(em_always())
}
global function em_next_sentence()
{
next_sentence(em_always());
}
global function em_page_down()
{
local i, count;
count = em_always();
for (i = 0; i < count; i++)
page_down()
}
global function em_page_up()
{
local i, count;
count = em_always();
for (i = 0; i < count; i++)
page_up()
}
global function em_prev_char()
{
prev_char(em_always())
}
global function em_prev_paragraph()
{
prev_paragraph(em_always())
}
global function em_prev_function()
{
prev_section(em_always())
}
global function em_prev_sentence()
{
prev_sentence(em_always())
}
global function em_scroll_vertical_up()
{
scroll_vertical(-em_always())
}
global function em_scroll_vertical_down()
{
scroll_vertical(em_always())
}
global function em_up()
{
local count = em_always(last_command);
if(last_command == VERT)
goto_pos(max(1, current_line - count), last_column);
else
{
last_column = current_column;
last_command = VERT;
up(count);
}
}
global function em_bksp()
{
local i,j;
j = em_always();
for (i=0; i<j; i++)
backspace();
}
global function em_num()
{
local i;
i = ((emacc) ? emacc : 1);
emacc = 0;
return i;
}
global function em_quad_arg()
{
if (!emacc) emacc++;
emacc *= 4;
message("Argument %d",emacc);
}
global function em_get_arg(n)
{
local ch;
do
{
emacc *= 10;
emacc += n;
message("Arg: %d", emacc);
ch = getkey();
n = int_to_ascii(ch) - ord("0");
} while (isdigit( chr(int_to_ascii(ch))));
ungetkey(ch);
}
global function donormal()
{
local ch, count = em_always(), note, tune;
ch = chr(int_to_ascii(current_key));
if(count > 1)
{
insert_string(strrepeat(ch, count));
if(and(buffer_flags, BUFFER_OVERTYPE_MODE))
{
if((count + current_column) > current_line_width)
delete_to_eol();
else
delete_chars(count);
}
}
else
insert_key();
if(emacs_music)
{
note = (( (ord(ch) - 32) * 83 ) / 95) + 16;
tune = "L16N" note;
message(tune);
play(tune);
}
}
global function em_cd()
{
em_always();
chdir(prompt_history("EDITFILE", "Change directory to: ", getcwd(), 1, 1, "em_cd_dialog" ));
}
global function em_help()
{
em_always();
gui_help_index();
}
global function em_delete_to_char()
{
local ch;
em_always();
message("Enter search character: ");
ch = chr(int_to_ascii(getkey()));
save_position();
begin_selection(NORMAL_SELECTION);
if(search(ch, SEARCH_FORWARD+SEARCH_CASE))
{
em_kill_region();
message("Text deleted");
}
else
message("Character not found");
restore_position(TRUE);
end_selection();
}
global function em_invoke_function()
{
local comm;
em_always();
comm = prompt_history("XMACRO","Command: ","em_", 1, 1, "command" );
if (comm in savedmacs)
replay(comm);
else if(comm)
invoke_function(comm);
}
global function em_redo()
{
em_always();
redo();
}
global function em_scroll_window_middle()
{
em_always();
scroll_window_middle();
}
global function em_toggle_insert_mode()
{
em_always();
toggle_buffer_flags( BUFFER_OVERTYPE_MODE );
}
global function em_undo()
{
em_always();
undo();
}
global function em_goto_bol()
{
em_always();
goto_bol();
}
global function em_goto_eol()
{
em_always();
goto_eol();
}
global function em_goto_window_bottom()
{
em_always();
goto_window_bottom();
}
global function em_tags()
{
local symbol;
em_always();
symbol = symbol_under_cursor();
tags(symbol);
}
global function em_start_comment()
{
local key;
em_always();
key = path_ext(buffer_filename);
message(key);
if(key in comments)
{
save_position();
insert_string(comments[key]);
restore_position(TRUE);
current_column += 3;
}
else
warning("Unrecognized file extension.");
}
global function em_goto_window_top()
{
em_always();
goto_window_top();
}
global function name_last_kbd_macro()
{
local s;
if (current_keymap == ctrlx_keymap)
pop_keymap();
if (keymac == "")
return;
s = prompt("Macro name: ", "", "em_name_kbd_macro_prompt" );
if (s == "")
return;
savedmacs[s] = keymac;
message("");
}
global function replay(comm)
{
local s;
em_always();
s = savedmacs[comm];
playback(substr(s, 1, length(s) -1));
}
################################################################################
# C-x r keymap functions.
# These functions handle all rectangle commands.
################################################################################
global function em_clear_rectangle()
{
local prefix, i, flags,
start = min(marky, current_line),
end = max(marky, current_line),
col = min(markx, current_column),
len = abs(markx - current_column) + 1,
old_buffer = buffer_flags;
em_always();
if(em_rectangle_defined())
{
em_rect_kill();
prefix = strrepeat(" ", len);
for(i = start; i <= end; i++)
{
goto_pos(i, col);
insert_string(prefix);
}
remove_selection();
message("Rectangle blanked out");
}
}
global function em_rect_kill()
{
em_always();
if(em_rectangle_defined())
{
create_linecol_selection(COLUMN_SELECTION, marky, markx, current_line, current_column);
em_delete_to_scrap();
remove_selection();
message("Rectangle killed");
}
}
global function em_rect_open()
{
local col = current_column, scol;
em_always();
if(em_rectangle_defined())
{
scol = min(markx, col);
create_linecol_selection(COLUMN_SELECTION, min(marky, current_line), scol, max(marky, current_line), scol);
indent_columns(abs(col - markx)+1);
message("Rectangle shifted right");
remove_selection();
}
}
global function em_string_rectangle()
{
local prefix, i, start, end, col;
em_always();
if(em_rectangle_defined())
{
start = min(marky, current_line);
end = max(marky, current_line);
col = min(markx, current_column);
prefix = prompt_history("PREFIX", "String to prefix rectangle: ", "", 1, 1, "em_prefix_rectangle_prompt");
for(i = start; i <= end; i++)
{
goto_pos(i, col);
insert_string(prefix);
}
remove_selection();
message("Rectangle prefixed");
}
}
global function em_rect_copy_to_register()
{
em_always();
message("Find out what a register is");
}
global function em_rect_yank()
{
local this_buffer;
em_always();
this_buffer = current_buffer;
current_buffer = killbuffers[currentkill].bufid;
goto_buffer_top();
begin_selection(killbuffers[currentkill].type);
goto_buffer_bottom();
if(killbuffers[currentkill].type == COLUMN_SELECTION)
prev_char();
copy_to_scrap();
current_buffer = this_buffer;
insert_scrap();
message("Yank rectangle from kill buffer");
}
global function em_rectangle_defined()
{
local markset = !((markx + marky) == 0);
if(!markset)
warning("Rectangle not defined");
return markset;
}
######## end rectangle functions
global function em_indent_tab_maybe()
{
local i, cnt = em_always();
for(i = 0; i < cnt; i++)
indent_tab_maybe();
}
#
# Current line is adjusted to line up with line above or indented one stop if required
#
# Indent if line above has an open structure ( ([{" )
# or does not end in a semi-colon followed by white space
#
global function em_indent()
{
local ch, pos = 0, fnw = 0, fnw2 = 0, line, level = 0, quote = 0, tablevel = FALSE;
local savex = current_column, savey = current_line, newx;
local save_buffer_flags = buffer_flags;
if(and(save_buffer_flags, BUFFER_OVERTYPE_MODE))
buffer_flags = xor(buffer_flags, BUFFER_OVERTYPE_MODE);
em_always();
do
{
pos = up(1);
fnw = first_nonwhite();
} while(!fnw && pos)
if(!pos)
{
tablevel = TRUE;
fnw = 1;
}
else
{
goto_pos(0, fnw);
ch = read_buffer(1);
pos = 0;
while(ch != "")
{
# Only count characters that are outside of quotes
if(ch == "\"")
quote = xor(quote, TRUE);
else if(!quote)
{
if(match(ch, "[\\{\\(\\[]"))
level++;
else if(match(ch, "[\\}\\)\\]]"))
level--;
else if(match(ch, "[ \t]") && pos)
pos++;
else if(ch == ";")
pos = current_column;
}
next_char();
ch = read_buffer(1);
}
if( ((pos != current_line_width) && (level >= 0)) || (level > 0) || quote )
tablevel = TRUE;
}
goto_pos(savey, 0);
fnw2 = first_nonwhite();
goto_pos(0, fnw2);
line = read_buffer();
delete_line();
goto_bol();
insert_string(strrepeat(" ", fnw-1));
if(tablevel)
{
fnw += distance_next_tab;
insert_string(strrepeat(" ", distance_next_tab));
}
newx = current_column;
savex = savex - fnw2 + fnw;
insert_string(line "\n");
if(savex > newx)
goto_pos(savey, savex);
else
goto_pos(savey, newx);
buffer_flags = save_buffer_flags;
}
global function whatiskey()
{
local ch, func, bound, name;
message("What is key: ");
ch = getkey();
name = int_to_key(ch);
bound = keymap_binding(name);
em_always();
if(bound)
func = "Mapped to: " "\"" bound "\"";
else
func = "Key not mapped."
message("Key ID: " ch "; String: " name "; " func);
}
global function em_wrap_paragraph()
{
em_always();
wrap_paragraph();
}
global function em_yankpop()
{
local width;
local thisbuffer = current_buffer;
local type = killbuffers[currentkill].type;
if(last_command == YANK)
{
if(type == COLUMN_SELECTION)
{
current_buffer = killbuffers[currentkill].bufid;
save_position();
goto_buffer_top();
restore_position(TRUE);
width = current_line_width;
current_buffer = thisbuffer;
current_line --;
current_column = lastyankx1 + width - 1;
}
begin_selection(type);
current_column = lastyankx1;
current_line = lastyanky1;
delete_chars();
end_selection();
prevkillbuf(1);
em_yank();
}
else
warning("Last command was not a yank");
}
global function em_delete_to_scrap()
{
local thisbuffer,
name,
type = selection_type();
thisbuffer = current_buffer;
delete_to_scrap();
nextkillbuf(1);
delete_buffer(killbuffers[currentkill].bufid);
name = "killbuffer" currentkill "";
killbuffers[currentkill].bufid = create_buffer(name, name, BUFFER_SYSTEM+BUFFER_NO_UNDO);
killbuffers[currentkill].type = type;
current_buffer = killbuffers[currentkill].bufid;
insert_scrap();
current_buffer = thisbuffer;
}
global function em_append_to_scrap(newline)
{
local thisbuffer;
thisbuffer = current_buffer;
delete_to_scrap();
current_buffer = killbuffers[currentkill].bufid;
if(newline)
insert_newline();
else
insert_scrap();
current_buffer = thisbuffer;
}
global function em_copy_to_scrap()
{
local thisbuffer,
name,
type = selection_type();
thisbuffer = current_buffer;
copy_to_scrap();
nextkillbuf(1);
delete_buffer(killbuffers[currentkill].bufid);
name = "killbuffer" currentkill "";
killbuffers[currentkill].bufid = create_buffer(name, name, BUFFER_SYSTEM);
killbuffers[currentkill].type = type;
current_buffer = killbuffers[currentkill].bufid;
insert_scrap();
current_buffer = thisbuffer;
em_unset_mark();
}
global function nextkillbuf(adv)
{
currentkill += adv;
currentkill %= NUMKILLBUFFERS;
}
global function prevkillbuf(adv)
{
adv %= NUMKILLBUFFERS;
currentkill += (NUMKILLBUFFERS - adv);
currentkill %= NUMKILLBUFFERS;
}
global function em_display_next_kill()
{
# local old_flags = window_flags;
nextkillbuf(em_always());
window_flags = or(window_flags, WINDOW_SHOW_SYSTEM);
current_buffer = killbuffers[currentkill].bufid;
# window_flags = old_flags;
}
global function em_display_prev_kill()
{
# local old_flags = window_flags;
prevkillbuf(em_always());
window_flags = or(window_flags, WINDOW_SHOW_SYSTEM);
current_buffer = killbuffers[currentkill].bufid;
# window_flags = old_flags;
}
global function em_modified()
{
em_always();
buffer_flags = xor(buffer_flags, BUFFER_MODIFIED);
if (and(buffer_flags,BUFFER_MODIFIED))
message("Buffer is marked changed");
else
message("Buffer is marked unchanged");
}
global function em_showvar()
{
local showme;
em_always();
showme = prompt_history("XMACRO","Show variable: ","", 1, 1);
if(showme)
message(showme " == " execute_function(showme));
}
global function setvar()
{
local setme;
local to;
em_always();
setme = prompt_history("XMACRO","set variable: ","", 1, 1);
to = prompt("to: ");
message(setme " == " execute_function(setme"="to));
}
global function em_copytofile()
{
local t;
em_always();
t = and(buffer_flags,BUFFER_MODIFIED);
write_buffer(prompt_history("EDITFILE","Copy to file: ", getcwd(), 1, 1, "em_copytofile_dialog"));
buffer_flags = set_flag_bits(buffer_flags, BUFFER_MODIFIED, t);
}
global function em_set_line_prefix()
{
local prefix_string;
prefix_string = prompt_history("PREFIX", "New line prefix: " , line_prefix, 1, 1, "em_set_line_prefix_prompt" );
line_prefix = prefix_string;
em_always();
}
global function em_bufed()
{
em_always();
buffer_list();
}
global function em_count_lines()
{
em_always();
message("%d lines, point on line %d",buffer_last_line, current_line);
}
global function em_cancel()
{
em_always();
message("Command canceled");
}
global function em_delete_blank_lines()
{
em_always();
goto_bol();
save_position();
while (up() && isblank(read_buffer()))
{
restore_position(FALSE);
save_position();
}
restore_position(TRUE);
begin_selection(NORMAL_SELECTION);
save_position();
while (isblank(read_buffer()) && down())
{
restore_position(FALSE);
save_position();
}
restore_position(TRUE);
delete_chars();
}
global function em_end_kbd_macro()
{
em_always();
keymac=record(0);
message("Done Recording");
}
global function em_exchange_point_and_mark()
{
local t;
em_always();
end_selection();
update_current_view();
begin_selection(NORMAL_SELECTION);
t = markx;
markx = current_column;
current_column = t;
t = marky;
marky = current_line;
current_line = t;
}
global function em_exit()
{
em_always();
# em_write_state();
done();
}
global function em_find_file()
{
local fn
em_always();
fn = prompt_history("EDITFILE", "Find file: ", getcwd(), 1, 1, "create_buf_and_win_prompt" )
if ( fn )
create_buf_and_win(fn);
}
global function em_goto_line()
{
local t=0, j, response;
t = emacc;
j = em_always();
if(t)
goto_line(j);
else
goto_line_key();
}
global function em_mark_sexp()
{
em_always();
message("mark sexp");
}
global function em_mark_function()
{
em_always();
message("mark function");
}
global function em_mark_buffer()
{
em_always();
goto_buffer_top();
begin_selection(NORMAL_SELECTION);
goto_buffer_bottom();
}
global function em_indent_rigidly()
{
local i, j, t;
t = emacc;
j = em_always();
begin_selection(NORMAL_SELECTION);
current_column = markx;
current_line = marky;
indent_columns((t) ? j : first_tab_distance());
end_selection();
}
global function em_insert_file(file)
{
em_always();
markx = current_column;
marky = current_line;
if(!file)
read_file(prompt_history("EDITFILE", "Insert file: ", getcwd(), 1, 1, "insert_prompt" ));
else
read_file(file);
}
global function em_kill_buffer()
{
em_always();
delete_buffer_key();
}
global function em_kill_window()
{
em_always();
delete_window();
}
global function em_last_kbd_macro()
{
local i, j;
j = em_always();
for (i=0; i<j; i++)
playback(substr(keymac,1,length(keymac)-1));
}
global function em_make()
{
local status;
local old_window = current_window, old_buffer = current_buffer;
em_always();
if (makebuffer)
delete_buffer(makebuffer);
if (buffers_modified && toupper(prompt("Save buffers?")) == "Y")
write_all_buffers();
errfile=create_temp_name();
current_window = create_window(3,8,display_width-6,display_height-16)
# WINDOW_MENU0);
# window_cursor_x = 0;
# window_cursor_y = 0;
display_update();
status = dos_window("make >& "errfile);
makebuffer = current_buffer =
create_buffer("", errfile, BUFFER_SYSTEM + BUFFER_NO_UNDO);
attach_window_buffer(current_window, current_buffer);
goto_buffer_bottom();
goto_eol();
display_update();
confirm( "Press <ENTER> to continue.", "" );
goto_buffer_top();
goto_bol();
delete_window();
current_buffer = old_buffer;
if (old_window)
current_window = old_window;
display_redraw();
unlink(errfile);
message("make returns %d",status);
}
global function em_next_error()
{
em_always();
# goto_next_error( errorSrcName );
}
global function em_next_buffer()
{
local i, count;
count = em_always();
for(i = 0; i < count; i++)
next_buffer_mask();
}
global function em_prev_buffer()
{
local i, count;
count = em_always();
for(i = 0; i < count; i++)
prev_buffer_mask();
}
global function em_next_window()
{
em_always();
next_window();
}
# post: mdi_mode = FALSE
global function em_previous_window()
{
em_always();
prev_window();
}
global function em_push()
{
em_always();
system("p");
}
global function em_save_file()
{
em_always();
write_buffer_key();
# message("File saved.");
}
global function em_save_all()
{
em_always();
gui_saveall();
}
global function em_set_fill_column()
{
local t, j;
t = emacc;
j = em_always();
wp_right_margin = (t) ? j : current_column;
}
global function em_show_point()
{
em_always();
message("%d Characters, point at %d",buffer_size, buffer_offset);
}
global function em_scroll_right()
{
scroll_horizontal(-em_always());
}
global function em_scroll_left()
{
scroll_horizontal(em_always());
}
global function em_one_window()
{
em_always();
if(mdi_mode)
toggle_window_mode(0);
}
global function em_delete_window()
{
em_always();
if(mdi_mode && current_window)
delete_window();
}
###############################################################################
# pre: There is at least one window open.
# post: mdi_mode = TRUE. current_window' is split horizontally
###############################################################################
global function em_horizontal_window()
{
em_always();
if(!mdi_mode)
toggle_window_mode(1);
split_window_horizontal();
}
###############################################################################
# pre: There is at least one window open.
# post: mdi_mode = TRUE. current_window' is split vertically
###############################################################################
global function em_vertical_window()
{
em_always();
if(!mdi_mode)
toggle_window_mode(1);
split_window_vertical();
}
global function em_start_kbd_macro()
{
em_always();
record(1);
message("Recording");
}
global function em_start_process()
{
em_always();
system();
}
global function em_transpose_lines()
{
em_always();
up(1);
goto_bol();
begin_selection(NORMAL_SELECTION);
down(1);
delete_to_scrap();
end_selection();
down(1);
insert_scrap();
up(1);
}
global function em_replace()
{
local fn, old_buffer = current_buffer;
em_always();
fn = prompt_history("EDITFILE", "File to open: ", getcwd(), 1, 1, "em_replace_prompt" )
if(fn && (fn != buffer_filename) && create_buf_and_win(fn))
delete_buffer(old_buffer);
}
#global function em_visit_file()
#{
# local name;
# local fn;
#
# em_always();
# fn = prompt_history("EDITFILE","Visit file :",buffer_filename)
#
# create_buf_and_win(fn);
#
# if (and(buffer_flags,BUFFER_MODIFIED))
# {
# if (toupper(prompt("Write changes to disk first?"))=="Y")
# write_buffer();
# else
# {
# name = buffer_filename;
# buffer_flags = xor(buffer_flags,BUFFER_MODIFIED);
# delete_buffer();
# edit_file(name);
# }
# }
#}
global function em_write_file(file)
{
local msg;
em_always();
if(!file)
file = prompt("Write to file: ", getcwd(), "write_buffer_as_dialog");
if(file)
{
change_output_name(file);
if(write_buffer())
message("File written.");
else
{
msg = sprintf("Zero bytes written to '%s'", file);
warning(msg);
}
}
}
global function em_write_region()
{
local file;
save_position();
if(!selection_type())
em_mark_buffer();
file = prompt("Write region to file: ", getcwd());
if(file)
write_marked_block(file);
remove_selection();
# raise_anchor();
restore_position(TRUE);
}
local function isblank(s)
{
local i;
if (s == "")
return TRUE;
for (i=1; i<=length(s); i++)
{
if (substr(s,i,1) !~ "[ \t]")
return FALSE;
}
return TRUE;
}
global function indent_region()
{
local oldcurrent, swapt = FALSE;
em_always();
if (current_line < marky)
{
em_exchange_point_and_mark();
swapt = TRUE;
}
save_position();
oldcurrent = current_line;
for (current_line=marky; current_line<=oldcurrent; current_line++)
{
goto_bol();
em_indent();
}
restore_position(TRUE);
if (swapt)
em_exchange_point_and_mark();
}
global function em_write_state()
{
local i;
em_always();
em_sta_file = fopen(em_sta_name,1)
if (!em_sta_file)
{
message("Cannot open state file");
return;
}
if (search_strings[search_string_count] != "")
fprintf(em_sta_file,"lastsearch="search_strings[search_string_count]"\n");
if (keymac != "")
{
fprintf(em_sta_file,"keymac="keymac"\n");
# !! this doesn't always work right
}
for (i in savedmacs)
{
fprintf(em_sta_file,"savedmacs["i"]="i"\n");
# !! this also doesn't always work right
}
# !! fprintf(em_sta_file,"push_keymap ctrlx_keymap\n");
# !! save ctrlx keymap here as a set of assign_key commands
# !! fprintf(em_sta_file,"pop_keymap\n");
# !! save normal keymap here as a set of assign_key commands
fclose(em_sta_file);
message("State file written.");
}
global function em_read_state()
{
local oldbuffer = current_buffer,cf,cmd;
cf = create_buffer("",em_sta_name, BUFFER_SYSTEM+BUFFER_NO_UNDO);
# !! create the ctrlx keymap (should be put in when keymaps are saved)
# !!
# !! ctrlx_keymap=create_keymap(empty_keymap);
# !!
# !! create and activate the main keymap (should be put in when
# !! keymaps are saved)
# !!
# !! push_keymap(create_keymap(empty_keymap));
if (cf)
{
current_buffer = cf;
goto_buffer_top();
do
{
cmd = read_buffer();
if (cmd)
execute_function(cmd);
next_line();
} while (cmd);
delete_buffer(cf);
current_buffer = oldbuffer;
}
else
return FALSE;
return FALSE; # !! this should return true when the keymap stuff is
# !! built. My problem is saving all the maps including
# !! multiple keystroke sequences
}
local em_tab_distance;
local em_tab_string;
## first_tab_distance()
#
# Determine the column where the first Tab in a row would be positioned.
#
global function first_tab_distance()
{
if (em_tab_string != buffer_tabs)
{
em_tab_string = buffer_tabs;
buffer_flags = and(buffer_flags, not(BUFFER_REAL_SPACE_ONLY))
save_position()
goto_bol()
goto_next_tab();
em_tab_distance = current_column - 1;
restore_position(1);
buffer_flags = or(buffer_flags, BUFFER_REAL_SPACE_ONLY)
}
return em_tab_distance;
}
global function emacs_exit()
{
local item, name, menu, tbhand;
search_flags = old_search_flags;
# Reset the functions called by the menu items
menu = menu_info(0, MI_MENUHANDLE);
for(item in fid)
{
if ( menu )
menu_functions[item] = fid[item];
}
# Reset the functions called by the toolbar buttons
tbhand = toolbar_info(0);
for(item in tid)
{
if ( tbhand )
modify_toolbaritem(tbhand, item, TI_FUNCTIONID, tid[item]);
}
delete_event(EVENT.EMULATION_CHANGED, function_id("emacs_exit"));
delete_event(EVENT.MOUSE_LEFT_DOWN, function_id("em_mouse_set_mark"));
}