!Last Modified: 3-APR-1989 15:13:57.48, By: FLEMING ! Modify so that upon ctrl-C it toggles back to white map procedure eve_replace (target_arg, ! EVE Replace replacement_arg) local target, ! Local copy of replace_parameter_1 replacement, ! Local copy of replace_parameter_2 this_buffer, ! Current buffer saved_mode, ! Keyword for current mode lowercase_target, ! Lowercase version of target string lowercase_replacement, ! Lowercase version of replacement string uppercase_target, ! Uppercase version of target string uppercase_replacement, ! Uppercase version of replacement string capital_target, ! Capitalized version of target string capital_replacement, ! Capitalized version of replacement string how_exact, ! Keyword to indicate replace's case-sensitivity search_exact, ! Keyword to indicate search's case-sensitivity repeat_find_range, ! Range of found string after reverse directn here, ! Position marker found_forward, ! Flag if found in forward found_reverse, ! Flag if found in reverse pivot_point, ! Marker on position where direction is changed this_direction, ! Current direction before find saved_direction, ! Current direction upon entry other_direction, ! Opposite direction for reverse search erasing_pivot_point, ! Flag if pivot_point marker text is replaced last_found, ! Marker at last string found in one dir find_reply, ! User's reply to find prompt change_direction_key, ! Key terminating reply switched_pivot_point, ! Flag if direction toggled temp_mark, ! Temporary marker replace_range, ! Range of current occurrence highlight_range, ! Reverse-video version of replace_range replace_action, ! String reply to prompt action_length, ! Length of replace_action colon_index, ! Index of : in 'all:' asking, ! True unless eve$x_all option has been chosen this_occurrence, ! String of replace_range saved_mark, ! Marker at start of procedure occurrences; ! Number of replacements made so far on_error [TPU$_CONTROLC]: eve$message (EVE$_REPLCTRLC, 0, occurrences); eve$$restore_position (saved_mark); set (saved_direction, current_buffer); set (saved_mode, current_buffer); set (SCREEN_UPDATE, ON); eve_toggle_white_map; ! if aborting then toggle back to white map eve$learn_abort; abort; [OTHERWISE]: eve$$restore_position (saved_mark); set (saved_direction, current_buffer); set (saved_mode, current_buffer); set (SCREEN_UPDATE, ON); endon_error; if not eve$test_if_modifiable (current_buffer) then eve$learn_abort; return (FALSE); endif; if not (eve$prompt_string (target_arg, target, message_text (EVE$_OLDPROMPT, 1), message_text (EVE$_NOREPLSTR, 0))) then eve$learn_abort; return (FALSE); endif; saved_mark := mark (FREE_CURSOR); position (search (ANCHOR, FORWARD)); ! snap cursor to prevent padding this_buffer := current_buffer; asking := TRUE; !+ ! Initialization !- saved_mode := get_info (current_buffer, "mode"); set (INSERT, this_buffer); switched_pivot_point := FALSE; pivot_point := mark (FREE_CURSOR); ! here's where we'll look in other_direction saved_direction := current_direction; if saved_direction = FORWARD then other_direction := REVERSE; else other_direction := FORWARD; endif; replacement := replacement_arg; if replacement = "" then replacement := eve$prompt_line (message_text (EVE$_NEWPROMPT, 1), eve$$x_prompt_terminators); if replacement = 0 then set (saved_mode, this_buffer); eve$learn_abort; return (FALSE); endif; endif; !+ ! Set up case of targets !- lowercase_target := target; if get_info (lowercase_target, "type") = STRING then change_case (lowercase_target, LOWER); endif; if (lowercase_target = target) then search_exact := eve$x_find_no_exact; else search_exact := eve$x_find_exact; endif; lowercase_replacement := replacement; change_case (lowercase_replacement, LOWER); if (lowercase_target = target) and (lowercase_replacement = replacement) then how_exact := eve$x_find_no_exact; uppercase_target := target; if get_info (uppercase_target, "type") = STRING then change_case (uppercase_target, UPPER); endif; capital_target := target; if get_info (capital_target, "type") = STRING then eve$capitalize_string (capital_target); endif; uppercase_replacement := replacement; change_case (uppercase_replacement, UPPER); capital_replacement := replacement; eve$capitalize_string (capital_replacement); else how_exact := eve$x_find_exact; endif; eve$x_target := target; ! eve$$find searches for eve$x_target found_forward := FALSE; found_reverse := FALSE; !%IF eve$x_option_fonts !%THEN !%eve$save_font; !%ENDIF !%IF eve$x_option_evej !%THEN !% eve$conversion_cancel; !%ENDIF eve$$remove_found_range; ! remove range so current position is searched !+ ! Search for target !- loop repeat_find_range := 0; erasing_pivot_point := FALSE; this_direction := current_direction; replace_range := eve$$find (search_exact, 1, 1); set (this_direction, current_buffer); if replace_range = 0 then if learn_abort ! Don't look in opposite dir if in LEARN SEQ then !%IF eve$x_option_fonts !%THEN !%eve$restore_font; !%ENDIF if not asking then position (saved_mark); ! return to old spot if 'all' endif; set (saved_direction, current_buffer); ! always restore original set (saved_mode, this_buffer); ! direction and mode set (SCREEN_UPDATE, ON); eve$message (EVE$_FINDFAIL); eve$message (EVE$_REPLCOUNT, 0, occurrences); eve$message (EVE$_LEARNABORTBIG); !%IF eve$x_option_evej !%THEN !% eve$conversion_start; !%ENDIF return (FALSE); endif; here := mark (FREE_CURSOR); ! remember last found string in case user ! doesn't want to go to next one !+ ! Search in other direction from pivot point. !- last_found := mark (FREE_CURSOR); ! (maybe = pivot_point) position (pivot_point); set (other_direction, current_buffer); this_direction := current_direction; replace_range := eve$$find (search_exact, 1, 0); set (this_direction, current_buffer); if (replace_range = 0) then !+ ! Couldn't find one on other side of pivot_point; so ! go to last one found, and search in other direction. !- position (last_found); pivot_point := mark (FREE_CURSOR); switched_pivot_point := TRUE; ! see if ANY other occurrences still exist this_direction := current_direction; repeat_find_range := eve$$find (search_exact, 1, 0); set (this_direction, current_buffer); exitif (repeat_find_range = 0); ! no more occurrences endif; if not asking ! We're in 'all' loop: during prompts, then ! return to location that's showing on temp_mark := mark (FREE_CURSOR); ! screen so the screen doesn't position (saved_mark); ! change (eve$prompt_line doesn't use set (SCREEN_UPDATE, ON); ! read_lines -> must turn on update) endif; if (repeat_find_range <> 0) or ((replace_range <> 0) and switched_pivot_point) or ((replace_range <> 0) and found_forward and found_reverse) then !+ ! Ask if ok to re-replace in another pass thru the buffer. !- if other_direction = FORWARD then find_reply := eve$prompt_line (message_text (EVE$_REPLACEFWDAGAIN, 1), eve$$x_prompt_terminators); else find_reply := eve$prompt_line (message_text (EVE$_REPLACEREVAGAIN, 1), eve$$x_prompt_terminators); endif; else if other_direction = FORWARD then ! ask if want to go in other dir find_reply := eve$prompt_line (message_text (EVE$_REPLACEFWD, 1), eve$$x_prompt_terminators); else find_reply := eve$prompt_line (message_text (EVE$_REPLACEREV, 1), eve$$x_prompt_terminators); endif; endif; if find_reply = 0 then set (saved_direction, current_buffer); ! always restore original set (saved_mode, this_buffer); ! direction and mode set (SCREEN_UPDATE, ON); eve$learn_abort; return (FALSE); endif; if not asking ! turn screen update off again, then return then ! to last found string and continue set (SCREEN_UPDATE, OFF); position (temp_mark); endif; ! Hitting return or do means yes; hitting another non-typing ! key is probably a mistake, so interpret as no. if find_reply = "" then change_direction_key := eve$$lookup_comment (last_key, ""); if eve$test_synonym ("do", change_direction_key) or eve$test_synonym ("return", change_direction_key) then if (repeat_find_range <> 0) or ((replace_range <> 0) and switched_pivot_point) or ((replace_range <> 0) and found_forward and found_reverse) then find_reply := eve$x_no; ! 2nd pass starting else find_reply := eve$x_yes; ! more new ones endif; else find_reply := eve$x_yes; ! not default key = yes endif; else change_case (find_reply, LOWER); endif; !+ ! test the reply and stop if 'no' !- position (here); exitif substr (eve$x_no, 1, length (find_reply)) = find_reply; if (replace_range = 0) and (repeat_find_range <> 0) then replace_range := repeat_find_range; ! new replace range endif; ! go to the find string (last find did not position to it) position (replace_range); if other_direction = FORWARD ! flip the direction for next find then other_direction := REVERSE; else other_direction := FORWARD; endif; endif; if replace_range <> 0 then ! any ELSE?? !+ ! Hilight the occurrence and ask user what to do with it. !- if current_direction = FORWARD then found_forward := TRUE; else found_reverse := TRUE; endif; if asking then highlight_range := create_range (beginning_of (replace_range), end_of (replace_range), eve$x_highlighting); endif; position (beginning_of (replace_range)); if mark (NONE) = pivot_point then erasing_pivot_point := TRUE; endif; if asking then update (current_window); endif; loop if asking then replace_action := eve$prompt_line (message_text (EVE$_REPLPROMPT, 1), eve$$x_prompt_terminators); if replace_action = 0 then ! always restore original direction and mode set (saved_direction, current_buffer); set (saved_mode, this_buffer); set (SCREEN_UPDATE, ON); eve$learn_abort; return (FALSE); endif; if eve$test_synonym ("exit", eve$$lookup_comment (last_key, eve$x_key_map_list)) then ! EXIT = quit replacing replace_action := eve$x_quit; endif; change_case (replace_action, LOWER); else replace_action := eve$x_yes; endif; action_length := length (replace_action); if (replace_action = substr (eve$x_yes, 1, action_length)) or (replace_action = substr (eve$x_all, 1, action_length)) or (replace_action = substr (eve$x_last, 1, action_length)) or (action_length = 0) then highlight_range := 0; !%IF eve$x_option_fonts !%THEN !% eve$match_font (replace_range); !%ENDIF this_occurrence := erase_character (length (replace_range)); if how_exact = eve$x_find_exact then copy_text (replacement); else ! Make sure non-alphabetic target is replaced by lowercase if this_occurrence = lowercase_target then copy_text (lowercase_replacement); else if this_occurrence = uppercase_target then copy_text (uppercase_replacement); else if this_occurrence = capital_target then copy_text (capital_replacement); else copy_text (lowercase_replacement); endif; endif; endif; endif; if erasing_pivot_point ! 'pivot_point' marker text erased, then ! redo the marker temp_mark := mark (FREE_CURSOR); move_horizontal (-length (replacement)); pivot_point := mark (FREE_CURSOR); position (temp_mark); endif; if current_direction = REVERSE then move_horizontal (-length (replacement)); endif; occurrences := occurrences + 1; if asking then update (current_window); if (replace_action = substr (eve$x_all, 1, action_length)) and (action_length > 0) then asking := FALSE; eve$message (EVE$_REPLALL, 0, target); saved_mark := mark (FREE_CURSOR);! return here when done set (SCREEN_UPDATE, OFF); endif; endif; exitif 1; else if (replace_action = substr (eve$x_no, 1, action_length)) or (replace_action = substr (eve$x_quit, 1, action_length)) then highlight_range := 0; if current_direction = FORWARD then position (end_of (replace_range)); move_horizontal (1); endif; update (current_window); exitif 1; endif; endif; endloop; exitif (action_length > 0) and ((replace_action = substr (eve$x_quit, 1, action_length)) or (replace_action = substr (eve$x_last, 1, action_length))); endif; endloop; !%IF eve$x_option_fonts !%THEN !%eve$restore_font; !%ENDIF if not asking then position (saved_mark); ! return to original spot only if 'all' endif; set (saved_direction, current_buffer); ! always restore original direction set (saved_mode, this_buffer); ! and mode set (SCREEN_UPDATE, ON); eve$message (EVE$_REPLCOUNT, 0, occurrences); !%IF eve$x_option_evej !%THEN !% eve$conversion_start; !%ENDIF return (TRUE); endprocedure;