home *** CD-ROM | disk | FTP | other *** search
- # $Header: P:/source/ppee/macros/wordstar.pev 1.32 03 Jul 1990 14:56:22 milow $
-
- ##############################################################################
- #
- # Sage Software - POLYTRON Division
- # 1700 NW 167th Place
- # Beaverton, OR 97006
- #
- # Copyright 1990, Sage Software, Inc.
- #
- # Permission is hereby granted for licensed users of Sage Professional
- # Editor and PolyAwk to copy and modify this source code for their own
- # personal use. These derivative works may be distributed only to other
- # licensed Sage Professional Editor and PolyAwk users. All other usage
- # is prohibited without express written permission from Sage Software.
- #
- ##############################################################################
-
- #### $Workfile: wordstar.pel $: Key bindings for WordStar compatability
-
- ##
- ## wordstar() - define WordStar key bindings
- ##
-
- local wordDefStr = "<|\\(|\\)|,|$"
- local previous_mst
-
- local wordstar_keymap = -1
-
- global function wordstar() {
-
- emulation_mode = "wordstar"
- execute_event_handler(EVENT_EMULATION_CHANGED)
-
- toggle_dialog( TRUE )
- toggle_file_backup( TRUE )
- reset_visibles()
- default_visible_virtual_lines = "~"
- visible_virtual_lines = "~"
-
- default_window_flags = \
- nominal_border_flags = \
- WINDOW_STANDARD
-
- if ( !and( window_flags, WINDOW_SYSTEM ))
- window_flags = default_window_flags
-
- search_flags = \
- + SEARCH_MAXIMAL_MATCH \
- + SEARCH_FORWARD \
- + SEARCH_REGEX \
- # + SEARCH_ADVANCE \
- # + SEARCH_HIGHLIGHT
-
- previous_mst = mouse_selection_type
- mouse_selection_type = NORMAL_SELECTION
- attach_event_handler(EVENT_EMULATION_CHANGED,function_id("ws_restore"))
-
- # 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( wordstar_keymap >= 0 ){
- current_keymap = wordstar_keymap
- return
- }
-
- wordstar_keymap = current_keymap = create_keymap( factory_keymap )
-
- #--------------------------------------------------------------
- # Mouse commands
- #--------------------------------------------------------------
- assign_mouse_buttons()
-
- #--------------------------------------------------------------
- # Basic Text commands
- #--------------------------------------------------------------
- assign_key("<Ctrl-N>", "ws_open_newline")
- assign_key("<Ctrl-Q><Ctrl-Y>", "delete_to_eol")
- assign_key("<Ctrl-Q>y", "delete_to_eol")
- assign_key("<Ctrl-Q>Y", "delete_to_eol")
- assign_key("<Ctrl-Y>", "delete_line")
- assign_key("<Shift-Tab>", "outdent_tab_maybe")
- assign_key("<Tab>", "indent_tab_maybe")
- assign_key("<Ctrl-G>", "delete_chars")
-
- #--------------------------------------------------------------
- # Macros and keystroke recordings
- #--------------------------------------------------------------
- assign_key("<F7>", "record_key")
- assign_key("<F8>", "playback")
-
- #--------------------------------------------------------------
- # Manipulating Buffers
- #--------------------------------------------------------------
- assign_key("<Alt-N>", "next_buffer_key")
- assign_key("<Alt-P>", "prev_buffer_key")
- assign_key("<F9>", "compile_buffer")
- assign_key("<Ctrl-Q>y", "delete_to_eol")
- assign_key("<Ctrl-Q>Y", "delete_to_eol")
- assign_key("<Ctrl-K>s", "save_buffer")
- assign_key("<Ctrl-K>S", "save_buffer")
- assign_key("<Ctrl-K><Ctrl-S>", "save_buffer")
- assign_key("<Ctrl-K>d", "ws_quit 1 0 1")
- assign_key("<Ctrl-K>D", "ws_quit 1 0 1")
- assign_key("<Ctrl-K><Ctrl-D>", "ws_quit 1 0 1")
- assign_key("<Ctrl-K>q", "ws_abandon")
- assign_key("<Ctrl-K>Q", "ws_abandon")
- assign_key("<Ctrl-K><Ctrl-Q>", "ws_abandon")
- assign_key("<Ctrl-K>x", "ws_quit 1")
- assign_key("<Ctrl-K>X", "ws_quit 1")
- assign_key("<Ctrl-K><Ctrl-X>", "ws_quit 1")
- assign_key("<Alt-Minus>", "delete_buffer_key")
-
- #--------------------------------------------------------------
- # Window Selection
- #--------------------------------------------------------------
- assign_key("<F4>", "delete_window_key")
- assign_key("<Alt-F1>", "next_window")
- assign_key("<Alt-F2>", "prev_window")
- assign_key("<Shift-F5>", "larger_window")
- assign_key("<Ctrl-F5>", "smaller_window")
- assign_key("<Shift-F9>", "make_window")
- assign_key("<Alt-F4>", "delete_tiled_window")
- assign_key("<Alt-F5>", "create_tiled_window")
-
- #--------------------------------------------------------------
- # Cursor Movement
- #--------------------------------------------------------------
- assign_key("<Ctrl-E>", "up")
- assign_key("<Ctrl-X>", "down")
- assign_key("<Ctrl-S>", "left")
- assign_key("<Ctrl-D>", "right")
- assign_key("<Ctrl-R>", "page_up")
- assign_key("<Ctrl-C>", "page_down")
- assign_key("<Ctrl-Q>s", "goto_bol")
- assign_key("<Ctrl-Q>S", "goto_bol")
- assign_key("<Ctrl-Q><Ctrl-S>", "goto_bol")
- assign_key("<Ctrl-Q>d", "goto_eol")
- assign_key("<Ctrl-Q>D", "goto_eol")
- assign_key("<Ctrl-Q><Ctrl-D>", "goto_eol")
- assign_key("<Ctrl-Q>r", "goto_buffer_top")
- assign_key("<Ctrl-Q>R", "goto_buffer_top")
- assign_key("<Ctrl-Q><Ctrl-R>", "goto_buffer_top")
- assign_key("<Ctrl-Q>c", "goto_buffer_bottom")
- assign_key("<Ctrl-Q>C", "goto_buffer_bottom")
- assign_key("<Ctrl-Q><Ctrl-C>", "goto_buffer_bottom")
- assign_key("<Ctrl-Q>e", "topLeft")
- assign_key("<Ctrl-Q>E", "topLeft")
- assign_key("<Ctrl-Q><Ctrl-E>", "topLeft")
- assign_key("<Ctrl-A>", "ws_prev_word")
- assign_key( "#8", "outdent_space_maybe" ) # Ctrl-H or Backspace
- assign_key("<Ctrl-F>", "ws_next_word")
- assign_key("<Ctrl-W>", "scroll_up_1")
- assign_key("<Ctrl-Z>", "scroll_down_1")
- assign_key("<Ctrl-Q>x", "bottomRight")
- assign_key("<Ctrl-Q>X", "bottomRight")
- assign_key("<Ctrl-Q><Ctrl-X>", "bottomRight")
- assign_key("<Ctrl-Q>l", "goto_line_key")
- assign_key("<Ctrl-Q>L", "goto_line_key")
- assign_key("<Ctrl-Q><Ctrl-L>", "goto_line_key")
- assign_key("<Ctrl-Left>", "ws_prev_word")
- assign_key("<Ctrl-Right>", "ws_next_word")
-
- #--------------------------------------------------------------
- # Marked Blocks
- #--------------------------------------------------------------
- assign_key("<Ctrl-K>i", "ws_begin_block 4")
- assign_key("<Ctrl-K>I", "ws_begin_block 4")
- assign_key("<Ctrl-K><Ctrl-I>", "ws_begin_block 4")
- assign_key("<Ctrl-K>l", "ws_begin_block 3")
- assign_key("<Ctrl-K>L", "ws_begin_block 3")
- assign_key("<Ctrl-K><Ctrl-L>", "ws_begin_block 3")
- assign_key("<Ctrl-K>n", "ws_begin_block 2")
- assign_key("<Ctrl-K>N", "ws_begin_block 2")
- assign_key("<Ctrl-K><Ctrl-N>", "ws_begin_block 2")
- assign_key("<Ctrl-K>b", "ws_begin_block")
- assign_key("<Ctrl-K>B", "ws_begin_block")
- assign_key("<Ctrl-K><Ctrl-B>", "ws_begin_block")
- assign_key("<Keypad-+>", "ws_copy_block")
- assign_key("<Ctrl-K>k", "ws_copy_block")
- assign_key("<Ctrl-K>K", "ws_copy_block")
- assign_key("<Ctrl-K><Ctrl-K>", "ws_copy_block")
- assign_key("<Ctrl-K>c", "ws_insert_scrap")
- assign_key("<Ctrl-K>C", "ws_insert_scrap")
- assign_key("<Ctrl-K><Ctrl-C>", "ws_insert_scrap")
- assign_key("<Ctrl-K>v", "ws_move_block")
- assign_key("<Ctrl-K>V", "ws_move_block")
- assign_key("<Ctrl-K><Ctrl-V>", "ws_move_block")
- assign_key("<Ctrl-K>p", "print_buffer")
- assign_key("<Ctrl-K>P", "print_buffer")
- assign_key("<Ctrl-K><Ctrl-P>", "print_buffer")
- assign_key("<Ctrl-K>w", "ws_write_file")
- assign_key("<Ctrl-K>W", "ws_write_file")
- assign_key("<Ctrl-K><Ctrl-W>", "ws_write_file")
- assign_key("<Ctrl-K>r", "ws_read_file")
- assign_key("<Ctrl-K>R", "ws_read_file")
- assign_key("<Ctrl-K><Ctrl-R>", "ws_read_file")
- assign_key("<Ctrl-K>h", "ws_toggle_marking")
- assign_key("<Ctrl-K>H", "ws_toggle_marking")
- assign_key("<Ctrl-K><Ctrl-H>", "ws_toggle_marking")
- assign_key("<Ctrl-K>y", "ws_delete_block")
- assign_key("<Ctrl-K>Y", "ws_delete_block")
- assign_key("<Ctrl-K><Ctrl-Y>", "ws_delete_block")
- assign_key("<Ctrl-K>f", "system_key")
- assign_key("<Ctrl-K>F", "system_key")
- assign_key("<Ctrl-K><Ctrl-F>", "system_key")
- assign_key("<Ins>", "ws_insert_scrap")
- assign_key("<Minus>", "ws_delete_block")
-
- #--------------------------------------------------------------
- # Bookmarks
- #--------------------------------------------------------------
- assign_key("<Ctrl-K>1", "place_bookmark 1")
- assign_key("<Ctrl-K>2", "place_bookmark 2")
- assign_key("<Ctrl-K>3", "place_bookmark 3")
- assign_key("<Ctrl-K>4", "place_bookmark 4")
- assign_key("<Ctrl-K>5", "place_bookmark 5")
- assign_key("<Ctrl-K>6", "place_bookmark 6")
- assign_key("<Ctrl-K>7", "place_bookmark 7")
- assign_key("<Ctrl-K>8", "place_bookmark 8")
- assign_key("<Ctrl-K>9", "place_bookmark 9")
- assign_key("<Ctrl-K>0", "place_bookmark 10")
-
- assign_key("<Ctrl-Q>1", "goto_bookmark 1")
- assign_key("<Ctrl-Q>2", "goto_bookmark 2")
- assign_key("<Ctrl-Q>3", "goto_bookmark 3")
- assign_key("<Ctrl-Q>4", "goto_bookmark 4")
- assign_key("<Ctrl-Q>5", "goto_bookmark 5")
- assign_key("<Ctrl-Q>6", "goto_bookmark 6")
- assign_key("<Ctrl-Q>7", "goto_bookmark 7")
- assign_key("<Ctrl-Q>8", "goto_bookmark 8")
- assign_key("<Ctrl-Q>9", "goto_bookmark 9")
- assign_key("<Ctrl-Q>0", "goto_bookmark 10")
-
-
- #--------------------------------------------------------------
- # Search and Replace
- #--------------------------------------------------------------
- assign_key("<Ctrl-Q>f", "ws_search_find")
- assign_key("<Ctrl-Q>F", "ws_search_find")
- assign_key("<Ctrl-Q><Ctrl-F>", "ws_search_find")
- assign_key("<Ctrl-Q>a", "ws_search_replace")
- assign_key("<Ctrl-Q>A", "ws_search_replace")
- assign_key("<Ctrl-Q><Ctrl-A>", "ws_search_replace")
- assign_key("<Ctrl-Q>i", "search_i")
- assign_key("<Ctrl-Q>I", "search_i")
- assign_key("<Ctrl-Q><Ctrl-I>", "search_i")
- assign_key("<Ctrl-L>", "ws_search_again")
-
- #--------------------------------------------------------------
- # Miscellaneous
- #--------------------------------------------------------------
- assign_key("<Ctrl-T>", "del_next_word")
- assign_key("<Ctrl-P>", "insert_quoted_key")
- assign_key("<F10>", "invoke_function")
- assign_key("<Ctrl-V>", "toggle_insert_mode")
- assign_key("<Alt-U>", "undo")
- assign_key("<Keypad-*>", "undo")
- assign_key("<Alt-Y>", "redo")
- assign_key("<Alt-V>", "print_version")
- assign_key("<Ctrl-F7>", "display_errors")
- assign_key("<Alt-=>", "goto_next_error")
- assign_key("<Ctrl-B>", "wrap_paragraph")
- assign_key("<Alt-B>", "buffer_list")
- assign_key("<Alt-H>", "help")
- assign_key("`", "expand_template")
- assign_key("<Alt-M>", "mark_matching")
- assign_key( "<Space>", "indent_outdent_space" )
- assign_key( "<Alt-F7>", "learn_key" )
- assign_key( "#10", "auto_indent_nl" ) # Ctrl-J or Ctrl-Enter
- assign_key( "#13", "auto_indent_cr" ) # Ctrl-M or Enter
-
- }
-
- ##
- ## turbo_local() - define Borland extensions to WordStar
- ##
-
- global function turbo_local() {
- assign_key("<Alt-F3>", "buffer_list")
- assign_key("<F2>", "save_buffer")
- assign_key("<F3>", "edit_file_key")
- assign_key("<Alt-X>", "ws_quit 1 1")
- assign_key("<Alt-F>o", "system")
- assign_key("<Alt-F>w", "writeTo")
- assign_key("<Alt-F>c", "changeDir")
- assign_key("<Alt-F>d", "showDir")
- assign_key("<F5>", "toggle_window_zoom")
- assign_key("<Ctrl-O>i", "ai")
- toggle_auto_indent(TRUE)
- message_level = 0
- }
-
- #---------------------------------------------------------------#
- # #
- # Functions required to support the WordStar calls #
- # #
- #---------------------------------------------------------------#
-
-
- # topLeft()
- #
- # This function moves the cursor to the top left of the current
- # window.
-
- function topLeft() {
- goto_window_top()
- goto_bol()
- }
-
-
- # bottomRight()
- #
- # This function moves the cursor to the bottom right of the current
- # window.
-
- function bottomRight() {
- goto_window_bottom()
- goto_eol()
- }
-
-
-
- # writeTo()
- #
- # This function prompts the user for a new output filename and then
- # saves the current buffer to that name.
-
- function writeTo() {
- change_output_name()
- save_buffer()
- }
-
-
- # ws_open_newline()
- #
- # This function opens a new line at the cursor position. The cursor
- # does NOT move down to the new line.
-
- function ws_open_newline() {
- insert_newline()
- up()
- goto_eol()
- }
-
- # ws_abandon()
- #
- # This is the WordStar function to abandon the current buffer. If
- # there have been no modifications, the editor exits without further
- # adieu. If there have been mods, the user is asked his/her
- # intentions. If the user elects not to abandon the edits, the
- # editor returns to editing and does not exit or save.
-
- function ws_abandon() {
- while (buffers_modified) {
- next_buffer()
- if (buffer_is_modified()) {
- if (toupper(confirm(sprintf("Buffer <%s> not saved. Abandon? (Y/N)",
- buffer_name),"YyNn")) == "N")
- return
- else
- buffer_flags = set_flag_bits(buffer_flags, \
- BUFFER_MODIFIED,0)
- }
- }
- if (and(current_key,0x00FF) != 0x1b)
- quit()
- }
-
-
- # ws_quit()
- #
- # This function handles WordStar's various other methods of exiting,
- # other than abandoning. It allows saving all buffers and exiting
- # (a la <Ctrl-K>x), saving and moving on to the next file (a la
- # <Ctrl-K>d), or prompting to save each modified buffer and then
- # exiting (a la Borland's <Alt-X>.
-
- function ws_quit(saving,prompting,newfile) {
- if (buffers_modified && saving && !prompting)
- message(sprintf("Saving %ld buffers... ",buffers_modified))
- while (buffers_modified) {
- next_buffer()
- if (buffer_is_modified()) {
- if (saving && (!(0+prompting) ||
- toupper(confirm(sprintf( \
- "Verify: Buffer <%s> not saved. Save? (Y/N)", \
- buffer_name),"YyNn")) == "Y")) {
- if (filemode(buffer_filename) != -1)
- backup_file(buffer_filename)
- write_buffer()
- } else {
- if (and(current_key,0x00FF) == 0x1b)
- return
- buffer_flags = set_flag_bits(buffer_flags,BUFFER_MODIFIED,0)
- }
- }
- }
- if (newfile)
- edit_file_key()
- else
- if (and(current_key,0x00FF) != 0x1b)
- quit()
- }
-
-
- # del_next_word()
- #
- # This function deletes the word following the cursor position. The
- # characters that are used to define the beginning and end of a word
- # may be defined.
-
- function del_next_word() {
- if (drop_anchor()) {
- ws_next_word()
- delete_chars()
- } else
- beep()
- }
-
-
- # ws_write_file()
- #
- # This function allows writing a block to a file. If the file
- # already exists the user may elect to overwrite it or abort the
- # write.
-
- function ws_write_file() {
- local filenameStr
-
- if ( region_type() != 0 || ws_toggle_marking()) {
- filenameStr = prompt("File name: ")
- if (filemode(filenameStr) != -1) {
- if (toupper(confirm("File " filenameStr "exists. Overwrite? (Y/N) ",
- "YyNn")) == "Y")
- if (write_marked_block(filenameStr))
- message("Block written")
- else
- message("Error writing file.")
- } else if (write_marked_block(filenameStr))
- message("Block written")
- } else
- warning( "No block marked.")
- }
-
-
- # ws_read_file()
- #
- # This function allows reading a block from a file. The user is
- # prompted for the name of the file to be read.
-
- function ws_read_file() {
- local start_line, start_col
-
- start_line = current_line
- start_col = current_column
- drop_anchor()
- if (!read_file_key())
- raise_anchor()
- else {
- goto_pos(start_line,start_col)
- copy_to_scrap()
- ws_toggle_marking()
- }
- }
-
- local sel_line = 0, sel_col = 0, pos_line = 0, pos_col = 0, sel_buf, sel_type
-
-
- # ws_toggle_marking()
- #
- # This function hides or reveals a marked block, if any. This
- # function is very important to simulating the type of block marking
- # used by WordStar. It is called by other functions that expect
- # there to be a marked block but find none.
-
- function ws_toggle_marking() {
- if (region_type() == 0) {
- if (sel_line + sel_col) {
- current_buffer = sel_buf
- current_line = sel_line
- current_column = sel_col
- drop_anchor(sel_type)
- goto_pos(pos_line,pos_col);
- message("Block is revealed.")
- } else
- message("No hidden block defined.")
- } else {
- sel_line = mark_line()
- sel_col = mark_column()
- pos_line = current_line
- pos_col = current_column
- sel_buf = current_buffer
- sel_type = region_type()
- raise_anchor()
- message("Block is hidden.")
- }
- return sel_line
- }
-
-
- # reinit_selvars()
- #
- # This function reinitializes the variables used to record the
- # nature and position of the hidden block.
-
- function reinit_selvars() {
- sel_line = sel_col = pos_line = pos_col = sel_type = 0;
- }
-
-
- # ws_copy_block()
- #
- # This is the function that copies a block to scrap when the end of
- # the block is defined (<Ctrl-K>k).
-
- function ws_copy_block() {
- if (region_type() != 0) {
- copy_to_scrap()
- ws_toggle_marking()
- message("Block copied.")
- } else {
- if ((sel_line + sel_col) && sel_buf == current_buffer ) {
- save_position()
- current_line = sel_line
- current_column = sel_col
- drop_anchor(sel_type)
- restore_position(TRUE)
- } else
- message("Beginning of block not defined.");
- }
- }
-
-
- # ws_move_block()
- #
- # This function simulates the WordStar block move (<Ctrl-K>v). It
- # restores a hidden block, deletes it and then inserts scrap at the
- # current position.
-
- function ws_move_block() {
- local save_buf
-
- save_buf = current_buffer
- save_position()
- if (ws_toggle_marking()) {
- delete_chars()
- current_buffer = save_buf
- restore_position(TRUE)
- ws_insert_scrap()
- message("Block moved.")
- } else {
- restore_position(TRUE)
- warning("No block to move.")
- }
- }
-
-
- # ws_delete_block()
- #
- # This function simulates the WordStar block delete (<Ctrl-K>y. The
- # hidden block is restored and then deleted to scrap.
-
- function ws_delete_block() {
- save_position()
- if (ws_toggle_marking()) {
- delete_to_scrap()
- message("Block deleted to scrap")
- } else
- warning("No block defined.")
- restore_position(TRUE)
- reinit_selvars()
- }
-
-
- # ws_insert_scrap()
- #
- # This function inserts scrap and then restores the cursor position
- # to the beginning of the insertion. (<Ctrl-K>c)
-
- function ws_insert_scrap() {
- local start_line, start_col
-
- start_line = current_line
- start_col = current_column
- insert_scrap()
- reinit_selvars()
- goto_pos(start_line,start_col)
- }
-
-
- # ws_begin_block()
- #
- # This is the function to begin a WordStar block. If a block has
- # already been started, it is abandoned in favor of a new block.
-
- function ws_begin_block( marktype ) {
- if (!marktype)
- marktype = NORMAL_SELECTION
- if (region_type() != 0)
- raise_anchor()
- drop_anchor( marktype )
- }
-
-
- # save_buffer()
- #
- # This is the function to save a buffer and continue (<Ctrl-K>s)
-
- function save_buffer() {
- message("Saving buffer...")
- if (filemode(buffer_filename) != -1 && buffer_is_modified())
- backup_file(buffer_filename)
- write_buffer()
- message("Buffer saved.")
- }
-
-
- local dirStr = getcwd()
-
-
- # changeDir()
- #
- # This function provides the Borland extension to WordStar that
- # allows changing directories.
-
- function changeDir() {
- dirStr = prompt("Enter the new directory: ",dirStr)
- if (!chdir(dirStr))
- warning("Cannot change to directory " dirStr)
- else
- message("Logged to: " dirStr)
- }
-
- local dirMask = "*.*"
-
-
- # showDir()
- #
- # This is the function that provides the Borland extension to
- # WordStar that allows getting a directory.
-
- function showDir() {
- dirMask = prompt("Enter the directory file mask: ",dirMask)
- system("dir " dirMask " /w")
- }
-
-
- # ws_next_word()
- #
- # This function moves the cursor to the following word, using our
- # own definition of the word's delimiting characters.
-
- function ws_next_word() {
- if (!next_word(1,wordDefStr))
- goto_eol()
- }
-
-
-
- # ws_prev_word()
- #
- # This function moves the cursor to the preceding word, using our
- # own definition of the word's delimiting characters.
-
- function ws_prev_word() {
- prev_word(1,wordDefStr)
- }
-
-
- #----------- Supporting search and replace functions ------------#
-
- # reserved global variables
-
- #local SEARCH_MARK
- local search_line, search_column, search_prompt
- local SEARCH_GLOBALLY = 99999
-
-
-
- # search_is_local()
- #
- # This function tests to see if searches are currently set to be
- # limited to the defined block.
-
- function search_is_local() {
- return and(search_flags,SEARCH_BLOCK)
- }
-
-
- # ws_replacing()
- #
- # This function is the core of the replacement engine. It takes
- # note of various flags and settings to search globally, locally
- # (within a block), and with or without prompting.
-
- function ws_replacing(serStr,replStr) {
- local found_replacement = FALSE, ch, change_count = 0, save_count
- local local_offset, end_offset, lsearch_flags
-
- if (search_count == SEARCH_GLOBALLY) {
- # save current position and restore later if successful
- save_position()
- if (search_is_local()) {
- if (!region_type())
- if (!ws_toggle_marking()) {
- error("Block must be marked!")
- return
- }
-
- # make sure the cursor is at the beginning of the block
- local_offset = buffer_offset
- swap_marks()
- if (local_offset < buffer_offset) {
- end_offset = buffer_offset
- swap_marks()
- } else
- end_offset = local_offset
- save_position()
- }
- search_count = 0;
-
- # turn search advance off so we find match at cursor.
- lsearch_flags = set_flag_bits(search_flags,SEARCH_ADVANCE,0)
- while ( search( serStr, lsearch_flags )) {
- #
- # highlight the search string found and update
- # the screen
- #
- next_char( search_string_length )
- drop_anchor()
- prev_char( search_string_length )
- display_update()
- found_replacement = TRUE
- if (search_prompt) {
- ch = tolower(confirm("Replace? (Y/N) ","YyNn"))
- raise_anchor()
- display_update()
- } else {
- raise_anchor()
- while (replace(serStr, replStr, search_flags)) {
- if (!change_count)
- # if we found something, turn search advance on.
- search_flags = \
- set_flag_bits(search_flags,SEARCH_ADVANCE,SEARCH_ADVANCE)
- change_count++
- }
- setSearchPos()
- break
- }
- if (ch == "y") {
- # replace and prompt again for next
- search_flags = \
- set_flag_bits(search_flags,SEARCH_ADVANCE,0)
- _replace_current_item(serStr, replStr, search_flags)
- change_count++
- } else if (ch == "n") {
- if (and( search_flags, SEARCH_FORWARD ))
- next_char( search_string_length )
- else
- prev_char( 1 )
- }
- if (ch == "" || ch == "") {
- warning("INTERRUPTED!")
- return
- }
- # search_flags = set_flag_bits( \
- # search_flags,
- # SEARCH_ADVANCE,
- # SEARCH_ADVANCE)
- }
- restore_position( TRUE )
- if ( search_is_local() ) {
- if (local_offset == end_offset) {
- swap_marks()
- restore_position()
- }
- }
- } else {
- if (search( serStr, search_flags )) {
- next_char( search_string_length )
- drop_anchor()
- prev_char( search_string_length )
- display_update()
- found_replacement = TRUE
- ch = tolower(confirm("Replace? (Y/N) ","YyNn"))
- raise_anchor()
- if (ch == "y") {
- _replace_current_item(serStr, replStr, search_flags)
- change_count++
- } else if (ch == "" || ch == "") {
- warning("INTERRUPTED!")
- return
- }
- }
- }
- if (found_replacement == TRUE) {
- message(sprintf("Translation complete; %d occurrence%s changed.",\
- change_count, \
- change_count == 1 ? "" : "s"))
- } else {
- message("Pattern not found." )
- }
- search_count = 0;
- return found_replacement
- }
-
-
- #----------- Supporting search and replace functions ------------#
-
- local optionStr = "", searchStr = "", searched = TRUE
-
- # set_search_options()
- #
- # This local function is the one that processes the search option
- # characters obtained by the ws_search_find() and
- # ws_search_replace() functions. Valid options include B)ackward
- # G)lobal, L)ocal, N)o-prompt, R)eg-ex and U)pper/lower-case. The R
- # option is not standard to WordStar.
-
- local function set_search_options(options) {
- if ( options ~ /[^bglnru]/ ) {
- return FALSE
- }
-
- search_count = (options ~ /g/) \
- ? SEARCH_GLOBALLY \
- : 0
-
- # search_flags = (options ~ /g/) \
- # or(search_flags,SEARCH_ADVANCE)
- # and(search_flags,not(SEARCH_ADVANCE))
-
- search_flags = (options ~ /u/) \
- ? or(search_flags,SEARCH_IGNORE_CASE) \
- : and(search_flags,not(SEARCH_IGNORE_CASE))
-
- search_prompt = (options ~ /n/) \
- ? FALSE \
- : TRUE
-
- search_flags = (options ~ /r/) \
- ? or(search_flags,SEARCH_REGEX) \
- : and(search_flags,not(SEARCH_REGEX))
-
- search_flags = (options ~ /b/) \
- ? and(search_flags,not(SEARCH_FORWARD)) \
- : or(search_flags,SEARCH_FORWARD)
-
- search_flags = (options ~ /l/) \
- ? or(search_flags,SEARCH_BLOCK) \
- : and(search_flags,not(SEARCH_BLOCK))
-
- return TRUE
- }
-
-
- # ws_search_find()
- #
- # This function does searching WordStar style. It prompts for a
- # search string and then prompts for option characters.
-
- function ws_search_find() {
- searched = TRUE
- searchStr = prompt_history("SEARCH","Find: ",searchStr)
- if (searchStr !~ // && searchStr != "" && and(current_key,0x00FF) != 0x1b) {
- optionStr = tolower(prompt("Options: ",optionStr))
- if (optionStr !~ // && and(current_key,0x00FF) != 0x1b)
- if (set_search_options(optionStr)) {
- message("Searching...")
- if (!search(searchStr,search_flags))
- warning("Pattern not found." )
- else
- message("Search completed.")
- } else
- message("Unrecognized option string <" optionStr ">" )
- else
- warning("Interrupted!")
- }
- }
-
- local replacementStr = ""
-
- # ws_search_replace()
- #
- # This function does search and replacement WordStar style. It
- # prompts for search and replacement strings and then option
- # characters. It uses the prompt history mechanism to allow
- # previous responses to be recalled with the up arrow.
-
- function ws_search_replace() {
- searched = FALSE
- searchStr = prompt_history("SEARCH","Find: ",searchStr)
- if (searchStr !~ // && searchStr != "") {
- replacementStr = prompt_history("REPLACE","Replace with: ",replacementStr)
- if (replacementStr !~ // && and(current_key,0x00FF) != 0x1b) {
- optionStr = tolower(prompt("Options: ",optionStr))
-
- if (optionStr !~ // && and(current_key,0x00FF) != 0x1b)
-
- if (set_search_options(optionStr)) {
- ws_replacing(searchStr,replacementStr)
- } else
- warning("Unrecognized option string <" optionStr ">" )
- }
- }
- }
-
- # ws_search_again()
- #
- # This function repeats a search using the same parameters
- # previously specified. If a block has been marked, the function
- # searches for the text found in the marked block.
-
- function ws_search_again() {
- local direction, local_offset, end_offset
-
- if ( region_type() ) {
- local_offset = buffer_offset
- swap_marks()
- if (local_offset < buffer_offset) {
- local_offset = buffer_offset
- swap_marks()
- }
- searchStr = read_buffer(local_offset - buffer_offset)
- raise_anchor()
- }
- if (searchStr) {
- direction = (and(search_flags,SEARCH_FORWARD)) ? 1 : -1
- if (searched) {
- if (next_char(direction) && search(searchStr,search_flags)) {
- message( "Search completed." )
- setSearchPos()
- } else {
- warning("Pattern not found." )
- prev_char(direction)
- }
- } else {
- if (next_char(direction) && ws_replacing(searchStr,replacementStr)) {
- warning( "Search completed." )
- setSearchPos()
- } else {
- warning("Pattern not found." )
- prev_char(direction)
- }
- }
- } else {
- beep()
- warning("No previous search pattern.")
- }
- }
-
- # setSearchPos()
- #
- # This function remembers the current position for restoration
- # later. It is primarily used when doing multiple replaces.
-
- local function setSearchPos() {
- search_line = current_line
- search_column = current_column
- }
-
-
- # ws_restore()
- #
- # This function restores various changes that this emulation makes
- # in the event that the user invokes another emulation.
-
- function ws_restore(){
- mouse_selection_type = previous_mst
- delete_event(EVENT_EMULATION_CHANGED,function_id("ws_restore"))
- }
-