home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 16 / 16.iso / w / w055 / 4.ddi / SOURCES.LIF / MSWORD.PEL < prev    next >
Encoding:
Text File  |  1990-09-27  |  27.5 KB  |  1,052 lines

  1. # $Header:   P:/source/ppee/macros/msword.pev   1.36   20 Aug 1990 18:13:28   skipr  $
  2.  
  3. ##############################################################################
  4. #
  5. #           Sage Software - POLYTRON Division
  6. #             1700 NW 167th Place
  7. #               Beaverton, OR 97006
  8. #
  9. #   Copyright 1990, Sage Software, Inc.
  10. #
  11. #   Permission is hereby granted for licensed users of Sage Professional
  12. #   Editor and PolyAwk to copy and modify this source code for their own
  13. #   personal use.  These derivative works may be distributed only to other
  14. #   licensed Sage Professional Editor and PolyAwk users.  All other usage
  15. #   is prohibited without express written permission from Sage Software.
  16. #
  17. ##############################################################################
  18.  
  19. #### $Workfile:   msword.pel  $: Key bindings for Microsoft Word compatability
  20.  
  21. ##
  22. ## msword() - define MS Word key bindings
  23. ##
  24. local extended = FALSE
  25. local wordDefStr = "<|\\(|\\)|,"
  26. local previous_mst, previous_win_flags
  27.  
  28.  
  29. global function msword() {
  30.  
  31.     emulation_mode = "msword"
  32.     execute_event_handler(EVENT_EMULATION_CHANGED)
  33.  
  34.     current_keymap = create_keymap(factory_keymap)
  35.     toggle_file_backup( TRUE )
  36.     previous_win_flags = default_window_flags
  37.     default_buffer_tabs = "5 10"
  38.     default_window_flags = 0x6091
  39.     if ( !and( window_flags, WINDOW_SYSTEM )) {
  40.         window_flags = 0x6091
  41.     search_flags =             \
  42.         + SEARCH_MAXIMAL_MATCH            \
  43.         + SEARCH_FORWARD            \
  44.         + SEARCH_REGEX                \
  45.         + SEARCH_ADVANCE        \
  46.         + SEARCH_HIGHLIGHT
  47.     buffer_tabs = "5 10"
  48.     }
  49.     toggle_dialog( TRUE )
  50.     default_wp_left_margin = 9
  51.     default_wp_right_margin = 75
  52.     reset_visibles()
  53.     msw_visibles()
  54. #    attach_event_handler(EVENT_KEYPRESS,function_id("msw_check_key"))
  55.     previous_mst = mouse_selection_type
  56.     mouse_selection_type = INCLUSIVE_SELECTION
  57.     attach_event_handler(EVENT_EMULATION_CHANGED,function_id("msw_restore"))
  58.  
  59.     #--------------------------------------------------------------
  60.     # Mouse commands
  61.     #--------------------------------------------------------------
  62.     assign_mouse_buttons()
  63.  
  64.     #--------------------------------------------------------------
  65.     # Arrow Keys
  66.     #--------------------------------------------------------------
  67.     assign_key( "<Left>",        "msw_left")
  68.     assign_key( "<Right>",        "msw_right")
  69.     assign_key( "<Up>",        "msw_up")
  70.     assign_key( "<Down>",        "msw_down")
  71.     assign_key( "<Ctrl-Left>",        "prev_word")
  72.     assign_key( "<Ctrl-Right>",        "next_word")
  73.  
  74.     #--------------------------------------------------------------
  75.     # Function Keys
  76.     #--------------------------------------------------------------
  77.     assign_key( "<F1>",            "next_window")
  78.     assign_key( "<F2>",            "prev_window")        # non-standard
  79.     assign_key( "<F3>",            "buffer_list")        # non-standard
  80.     assign_key( "<F4>",            "playback")        # non-standard
  81.     assign_key( "<F5>",            "toggle_insert_mode")
  82.     assign_key( "<F6>",            "msw_toggle_inclusive_block")
  83.     assign_key( "<F7>",            "msw_prev_word")
  84.     assign_key( "<F8>",            "msw_next_word")
  85.     assign_key( "<F9>",            "mark_paragraph")
  86.     assign_key( "<F10>",        "invoke_function")
  87.     assign_key( "<Shift-F1>",    "undo")
  88.     assign_key( "<Shift-F2>",    "redo")            # non-standard
  89.     assign_key( "<Shift-F3>",    "record_key")
  90.     assign_key( "<Shift-F4>",    "msw_search_again")
  91.     assign_key( "<Shift-F5>",    "replace_again")    # non-standard
  92.     assign_key( "<Shift-F6>",    "msw_toggle_column_block")
  93.     assign_key( "<Shift-F7>",    "wp")            # non-standard
  94.     assign_key( "<Shift-F8>",    "compile_buffer")    # non-standard
  95.     assign_key( "<Shift-F9>",    "display_errors")    # non-standard
  96.     assign_key( "<Shift-F10>",    "goto_next_error")     # non-standard
  97.     assign_key( "<Ctrl-F1>",        "toggle_window_zoom")
  98.     assign_key( "<Ctrl-F2>",        "next_buffer_key")    # non-standard
  99.     assign_key( "<Ctrl-F3>",        "prev_buffer_key")    # non-standard
  100.     assign_key( "<Ctrl-F4>",        "msw_reverse")
  101.     assign_key( "<Ctrl-F5>",        "optional_function toggle_drawing")
  102.     assign_key( "<Ctrl-F6>",        "delete_tiled_window")
  103.     assign_key( "<Ctrl-F7>",        "edit_file_key")
  104.     assign_key( "<Ctrl-F8>",        "print_buffer")
  105.     assign_key( "<Ctrl-F9>",        "delete_buffer_key")
  106.     assign_key( "<Ctrl-F10>",    "msw_save_buffer")
  107.     assign_key( "<Alt-F1>",        "tabs")
  108. #    assign_key( "<Alt-F2>",        "beep")
  109.     assign_key( "<Alt-F3>",        "copy_to_scrap_key")
  110. #    assign_key( "<Alt-F4>",        "beep")
  111.     assign_key( "<Alt-F7>",        "learn_key" )
  112.     assign_key( "<Alt-F9>",        "delete_window_and_buffer" )
  113.  
  114.     #--------------------------------------------------------------
  115.     # Misc. Keys
  116.     #--------------------------------------------------------------
  117.     assign_key( "<Space>",        "indent_outdent_space" )
  118.     assign_key( "#10",    "auto_indent_nl" )    # Ctrl-J or Ctrl-Enter
  119.     assign_key( "#13",    "auto_indent_cr" )    # Ctrl-M or Enter
  120.     assign_key( "<Esc>",            "escape_hit")
  121.     assign_key( "<Alt-H>",        "help")
  122.     assign_key( "<Alt-M>",        "mark_matching")
  123.     assign_key( "<Alt-P>",        "insert_quoted_key")
  124.     assign_key( "<Alt-V>",        "print_version")
  125.     assign_key( "<Alt-X>",        "beep")
  126.     assign_key( "<Del>",        "cut")
  127.     assign_key( "<Ins>",        "insert_scrap")
  128.     assign_key( "<Shift-Tab>",    "outdent_tab_maybe")
  129.     assign_key( "<Tab>",            "indent_tab_maybe")
  130.     assign_key( "`",            "expand_template")
  131. }
  132.  
  133.  
  134. #        escape_hit()
  135. #
  136. #        This is the function that presents the "main" Word menu to the 
  137. #        user when the Ecs key is pressed.  It serves as a central 
  138. #        jumping-off point to the various Word features
  139.  
  140. function escape_hit(){
  141.     local selected
  142.  
  143.     selected = tolower(mouse_confirm( \
  144.     "CMD: Help Jump Library Options Print Quit Replace Search Transfer Undo Window", \
  145.         "HhJjLlOoPpQqRrSsTtUuWw"))
  146.     message("")
  147.     if (selected == "h")
  148.         help()
  149.     else
  150.     if (selected == "j")
  151.         jump_menu()
  152.     else
  153.     if (selected == "l")
  154.         library_menu()
  155.     else
  156.     if (selected == "o")
  157.         option_menu()
  158.     else
  159.     if (selected == "p")
  160.         print_menu()
  161.     else
  162.     if (selected == "q")
  163.         msw_quit()
  164.     else
  165.     if (selected == "r")
  166.         replace_menu()
  167.     else
  168.     if (selected == "s")
  169.         search_menu()
  170.     else
  171.     if (selected == "t")
  172.         transfer_menu()
  173.     else
  174.     if (selected == "u")
  175.         undo()
  176.     else
  177.     if (selected == "w")
  178.         window_menu()
  179. }
  180.  
  181.  
  182. #        jump_menu()
  183. #
  184. #        This function allows the user to move directly to a line, page or 
  185. #        mark by number.  Pages for our purposes here are units of 66 
  186. #        lines.
  187.  
  188. local function jump_menu(){
  189.     local selected, jumpNum, escaped
  190.  
  191.     selected = tolower(mouse_confirm("Jump to:  Line  Page  Mark","LlPpMm"))
  192.     if (selected == "l"){
  193.         do {
  194.             jumpNum = prompt("Line Number:  ",current_line)
  195.             escaped = (and(current_key,255) == 27)
  196.             jumpNum = atoi(jumpNum)
  197.         } while( jumpNum == 0 && !escaped )
  198.         if( !escaped ){
  199.             goto_line(jumpNum)
  200.         }
  201.     } else if (selected == "p"){
  202.         do {
  203.             jumpNum = prompt("Page Number:  ",current_line / 66 + 1)
  204.             escaped = (and(current_key,255) == 27)
  205.             jumpNum = atoi(jumpNum) * 66
  206.         } while( jumpNum == 0 && !escaped )
  207.         if( !escaped ){
  208.             goto_line(jumpNum)
  209.         }
  210.     } else if (selected == "m"){
  211.         do {
  212.             jumpNum = prompt("Mark Number:  ",pick_unused_mark() - 1)
  213.             escaped = (and(current_key,255) == 27)
  214.             jumpNum = atoi(jumpNum)
  215.         } while( jumpNum == 0 && !escaped )
  216.         if( !escaped ){
  217.             goto_mark(jumpNum)
  218.         }
  219.     }
  220. }
  221.  
  222.  
  223. #        library_menu()
  224. #
  225. #        This function presents the library menu to the user.  Most of 
  226. #        Word's library functions have no logical equivalent and the menu 
  227. #        is therefore rather spartan.  The "Run" command, unlike Word's, 
  228. #        ALWAYS defaults to the value of the comspec variable rather than 
  229. #        the last value used.  All previous values used are available, 
  230. #        however, using the prompt history mechanism (up arrow).
  231.  
  232. local function library_menu(){
  233.     local selected, cmdStr, escaped
  234.  
  235.     selected = tolower(mouse_confirm("Library:  Autosort  Run","AaRr"))
  236.     if (selected == "a"){
  237.         if (region_type()){
  238.             filter("sort")
  239.         } else {
  240.             warning("No selection defined.")
  241.         }
  242.     } else if (selected == "r") {
  243.         cmdStr = prompt_history("SYSTEM","Command Line:  ",ENV["COMSPEC"])
  244.         if (and(current_key,0x00FF) != 0x1b)
  245.             system(cmdStr)
  246.     }
  247. }
  248.  
  249.  
  250. #        option_menu()
  251. #
  252. #        This function allows setting of options for windows, and various 
  253. #        other features.  Rather than presenting these as a screen full of 
  254. #        selections, a series of menus allows selecting of the desired 
  255. #        option.  If the categories, window and general do not seem 
  256. #        logically organized, it is because it follows Word's organization 
  257. #        as much as possible.  The setup() function is also thrown in here 
  258. #        rather than duplicating Word's color selection and other odds and 
  259. #        ends.  Most of the options selected outside of the setup() 
  260. #        function are transient and apply only to the current session.  The 
  261. #        exceptions are those things that affect window_flags when state 
  262. #        saving is in effect.
  263.  
  264. local function option_menu(){
  265.     local selected
  266.  
  267.     selected = tolower(mouse_confirm("OPTIONS:  Window  General  Setup","WwGgSs"))
  268.     if (selected == "s") {
  269.         setup()
  270.     } else if( selected == "g" ) {
  271.         general_option_menu()
  272.     } else if( selected == "w" ) {
  273.         window_option_menu()
  274.     }
  275. }
  276.  
  277.  
  278. #        general_option_menu()
  279. #
  280. #        This function, called by option_menu(), allows switching between 
  281. #        25 and 43/50 line mode, setting cursor speed, as well as making 
  282. #        line numbers and borders visible.
  283.  
  284. local function general_option_menu(){
  285.     local selected
  286.  
  287.     selected = tolower(mouse_confirm("GENERAL OPTIONS:  Display mode  Cursor speed  Line numbers  Show borders","SsCcDdLl"))
  288.     if (selected == "s") {
  289.         show_borders()
  290.     } else if( selected == "d" ) {
  291.         set_display()
  292.     } else if( selected == "c" ) {
  293.         set_cursor()
  294.     } else if( selected == "l" ) {
  295.         show_numbers()
  296.     }
  297. }
  298.  
  299.  
  300. #        set_display()
  301. #
  302. #        This function allows the user to select numerically between 
  303. #        display modes.  No attempt is made to ensure that these modes are 
  304. #        available.
  305.  
  306. local function set_display() {
  307.     local selected
  308.  
  309.     selected = mouse_confirm("DISPLAY MODE:  1)  25 lines,  2)  43/50 lines","12")
  310.     toggle_display(atoi(selected) - 1)
  311. }
  312.  
  313.  
  314. #        set_cursor()
  315. #
  316. #        This function allows selecting a cursor speed from 1 to 6.  This 
  317. #        number is used to set the keyboard repeat rate on AT class 
  318. #        machines.  The delay is always set to 2.  Since Word allows cursor 
  319. #        speeds from 1 to 9 with 9 being the fastest, the sense of the 
  320. #        value normally supplied to keyboard_speed() is reversed.  6 is the 
  321. #        fastest and 1 is the slowest.
  322.  
  323. local function set_cursor() {
  324.     local selected
  325.  
  326.     selected = mouse_confirm("CURSOR SPEED:  ( 1-6 )","123456")
  327.     keyboard_speed(7-selected,2)
  328. }
  329.  
  330.  
  331. #        show_borders()
  332. #
  333. #        This function allows toggling on and off borders using a yes or no 
  334. #        prompt.  The change is immediately visible.  When the desired 
  335. #        state is reached, pressing [Enter] accepts that setting.
  336.  
  337. local function show_borders() {
  338.     local selected
  339.  
  340.     do {
  341.         selected = tolower(mouse_confirm("SHOW BORDERS: " \
  342.             yesno_prompt_string(and(window_flags,WINDOW_BORDER)),"YyNn\r"))
  343.         if (and(current_key,0x00ff) != 0x1b && selected != "\r") {
  344.             if (selected == "y") {
  345.                 toggle_borders(TRUE)
  346.             } else if (selected == "n"){
  347.                 toggle_borders(FALSE)
  348.             }
  349.         }
  350.     } while ( and(current_key,0x00ff) != 0x1b && selected != "\r")
  351. }
  352.  
  353.  
  354. #        show_numbers()
  355. #
  356. #        This function allows toggling on and off line numbers using a yes 
  357. #        or no prompt.  The change is immediately visible.  When the 
  358. #        desired state is reached, pressing [Enter] accepts that setting.
  359.  
  360. local function show_numbers() {
  361.     local selected
  362.  
  363.     do {
  364.         selected = tolower(mouse_confirm("SHOW LINE NUMBERS: " \
  365.             yesno_prompt_string(linenumber_format),"YyNn\r"))
  366.         if (and(current_key,0x00ff) != 0x1b && selected != "\r") {
  367.             if (selected == "y") {
  368.                 toggle_linenumbers(TRUE)
  369.             } else if (selected == "n"){
  370.                 toggle_linenumbers(FALSE)
  371.             }
  372.         }
  373.     } while ( and(current_key,0x00ff) != 0x1b && selected != "\r")
  374. }
  375.  
  376.  
  377. #        yesno_prompt_string()
  378. #
  379. #        This is the function that produces the yes or no string used by 
  380. #        various other prompts.  A value is passed as an argument, 
  381. #        evaluates it to TRUE or FALSE and returns the appropriate string.
  382.  
  383. local function yesno_prompt_string(value) {
  384.     local    yesStr = "(Yes) No  ", noStr = " Yes (No) "
  385.  
  386.     return (value ? yesStr :noStr)
  387. }
  388.  
  389.  
  390. #        window_option_menu()
  391. #
  392. #        This function allows turning on and off the display of the ruler 
  393. #        and setting which of the non-printing symbols are made visible.
  394.  
  395. local function window_option_menu(){
  396.     local selected
  397.  
  398.     selected = tolower(mouse_confirm("WINDOW OPTIONS:  Show ruler  Non-printing symbols", \
  399.                 "NnSs"))
  400.     if (selected == "s") {
  401.         show_ruler()
  402.     } else if( selected == "n" ) {
  403.         set_symbols()
  404.     }
  405. }
  406.  
  407.  
  408. #        show_ruler()
  409. #
  410. #        This function allows toggling on and off the ruler using a yes or 
  411. #        no prompt.  The change is immediately visible.  When the desired 
  412. #        state is reached, pressing [Enter] accepts that setting.
  413.  
  414. local function show_ruler() {
  415.     local selected
  416.  
  417.     do {
  418.         selected = tolower(mouse_confirm("SHOW RULER: " \
  419.             yesno_prompt_string(and( window_flags, WINDOW_RULER )),"YyNn\r"))
  420.         if (and(current_key,0x00ff) != 0x1b && selected != "\r") {
  421.             if (selected == "y") {
  422.                 window_flags = set_flag_bits(window_flags, \
  423.                     WINDOW_RULER, WINDOW_RULER )
  424.             } else if (selected == "n"){
  425.                 window_flags = set_flag_bits(window_flags, \
  426.                     WINDOW_RULER, 0)
  427.             }
  428.         }
  429.     } while ( and(current_key,0x00ff) != 0x1b && selected != "\r")
  430. }
  431.  
  432.  
  433. #        set_symbols()
  434. #
  435. #        This function allows selecting whether All Partial or None of the 
  436. #        non-printing symbols show on the screen using Word's symbol set.
  437.  
  438. local function set_symbols() {
  439.     local selected
  440.  
  441.     selected = tolower(mouse_confirm("NON-PRINTING SYMBOLS:  None  Partial  All", \
  442.         "NnPpAa"))
  443.     if (selected == "n") {
  444.         reset_visibles()
  445.         visible_end_buffer = ""
  446.         default_visible_end_buffer = ""
  447.     } else if( selected == "p" ) {
  448.         reset_visibles()
  449.         msw_part_visibles()
  450.     } else if( selected == "a" ) {
  451.         reset_visibles()
  452.         msw_visibles()
  453.     }
  454. }
  455.  
  456.  
  457. #        print_menu()
  458. #
  459. #        This function allows the user to specify whether to print the file 
  460. #        to the print device, print it to a file or to change the print 
  461. #        device.
  462.  
  463. local function print_menu() {
  464.     local selected
  465.  
  466.     selected = tolower(mouse_confirm("Print:  Print  File  Options","PpFfOo"))
  467.     if (selected == "p") {
  468.         print_buffer()
  469.     } else if( selected == "f" ) {
  470.         msw_write_file()
  471.     } else if( selected == "o" ) {
  472.         print_options()
  473.     }
  474. }
  475.  
  476.  
  477. #        print_options()
  478. #
  479. #        This function displays and allows selection of the print options.  
  480. #        The only option available is to change the print device.  If a 
  481. #        selection has been defined, SPE will always print just the 
  482. #        selection.
  483.  
  484. local function print_options() {
  485.     print_device = prompt("PRINT DEVICE:  ",print_device)
  486. }
  487.  
  488.  
  489. #        msw_quit()
  490. #
  491. #        This is the function that executes when Quit has been selected 
  492. #        from the main menu.  It checks to see that each file has been 
  493. #        written to a file, if the user desires, before exiting.
  494.  
  495. function msw_quit(){
  496.     local    cw = current_window;
  497.     local    win = current_window
  498.     
  499.     win = next_window();
  500.  
  501.     do {
  502.         next_window()
  503.         if (buffer_is_modified()) {
  504.             confirm_save()
  505.         } else {
  506.             buffer_flags = and(buffer_flags,not(BUFFER_MODIFIED))
  507.         }
  508.         if (and(current_key,0x00FF) == 0x1b)
  509.             return
  510.     } while (win != current_window)
  511.     if (cw)
  512.         current_window = cw;
  513.     while (buffers_modified) {
  514.         next_buffer()
  515.         if (buffer_is_modified()) {
  516.             confirm_save()
  517.         } else {
  518.             buffer_flags = and(buffer_flags,not(BUFFER_MODIFIED))
  519.         }
  520.         if (and(current_key,0x00FF) == 0x1b)
  521.             return
  522.     }
  523.     quit()
  524. }
  525.  
  526.  
  527. #        replace_menu()
  528. #
  529. #        This function obtains the search and replacement strings and 
  530. #        options.
  531.  
  532. local function replace_menu(){
  533.     local searchStr, replaceStr
  534.  
  535.     searchStr = prompt_history("SEARCH","Replace:  ","")
  536.     if ( and(current_key,0x00FF) != 0x1b && searchStr != "") {
  537.         replaceStr = prompt_history("REPLACE","With:  ","")
  538.         if ( and(current_key,0x00FF) != 0x1b) {
  539.             if (search_options()){
  540.                 if (and(search_flags,SEARCH_FORWARD))
  541.                     replace_forward(searchStr,replaceStr)
  542.                 else
  543.                     replace_backward(searchStr,replaceStr)
  544.             }
  545.         }
  546.     }
  547. }
  548.  
  549.  
  550. #        search_prompt_string()
  551. #
  552. #        This function returns a string for display which indicates case 
  553. #        sensitivity and direction of such as currently specified by the 
  554. #        search_flags variable.
  555.  
  556. local function search_prompt_string(){
  557.     local    forwardStr = "(Forward) Backward  ", backStr = " Forward (Backward) ", \
  558.         ignoreStr = "(Ignore) Sensitive  ", sensitiveStr = " Ignore (Sensitive) ", \
  559.         promptStr = "Direction: "
  560.  
  561.     promptStr = promptStr (and(search_flags,SEARCH_FORWARD) \
  562.             ? forwardStr            \
  563.             : backStr) " Case: "        \
  564.                 (and(search_flags,SEARCH_IGNORE_CASE)    \
  565.                     ? ignoreStr            \
  566.                     : sensitiveStr)
  567.     return promptStr
  568. }
  569.  
  570.  
  571. #        search_options()
  572. #
  573. #        This function displays the current settings of the search flags 
  574. #        with regard to direction and case sensitivity, and allows the user 
  575. #        to change the settings.  The user accepts the current settings by 
  576. #        pressing [Enter].
  577.  
  578. local function search_options(){
  579.     local    selected, save_level = message_level
  580.  
  581.     message_level = 2
  582.     do {
  583.         selected = tolower(confirm(search_prompt_string(),"SsBbIiFf\r"))
  584.         if (and(current_key,0x00ff) != 0x1b && selected != "\r") {
  585.             if (selected == "f") {
  586.                 toggle_search_forward(TRUE)
  587.             } else if (selected == "b"){
  588.                 toggle_search_forward(FALSE)
  589.             } else if (selected == "i"){
  590.                 toggle_search_case(TRUE)
  591.             } else if (selected == "s"){
  592.                 toggle_search_case(FALSE)
  593.             }
  594.         }
  595.     } while ( and(current_key,0x00ff) != 0x1b && selected != "\r")
  596.     if (region_type())
  597.         toggle_search_block(TRUE)
  598.     else
  599.         toggle_search_block(FALSE)
  600.     message_level = save_level
  601.     return ( and(current_key,0x00ff) != 0x1b )
  602. }
  603.  
  604.  
  605. #        search_menu()
  606. #
  607. #        This function collects the search string and options.
  608.  
  609. local function search_menu(){
  610.     local searchStr, repaceStr, found
  611.  
  612.     searchStr = prompt_history("SEARCH","Search for:  ","")
  613.     if ( and(current_key,0x00FF) != 0x1b && searchStr != "") {
  614.         if (search_options()) {
  615.             if (and(search_flags,SEARCH_FORWARD))
  616.                 found = search_forward(searchStr)
  617.             else
  618.                 found = search_backward(searchStr)
  619.             if ( found )
  620.                 attach_event_handler(EVENT_KEYPRESS, \
  621.                     function_id("msw_search_restore"))
  622.         }
  623.     }
  624. }
  625.  
  626.  
  627. #        transfer_menu()
  628. #
  629. #        This function presents the user with the transfer menu to allow 
  630. #        loading saving and merging files.
  631.  
  632. local function transfer_menu(){
  633.     local selected, escaped, OutputName
  634.  
  635.     selected = tolower(mouse_confirm("Transfer:  Load  Merge  Save","LlMmSs"))
  636.     if (selected == "l"){
  637.         edit_file_key()
  638.     } else if( selected == "m" ){
  639.         read_file_key()
  640.     }
  641.     if( selected == "s" ){
  642.         OutputName = prompt_history("TRANSFER","Transfer Save:  ", buffer_filename)
  643.         if ( and(current_key,0x00FF) != 0x1b ) {
  644.             buffer_filename = OutputName
  645.             buffer_name = path_fname(buffer_filename) \
  646.                     path_ext(buffer_filename)
  647.             msw_save_buffer()
  648.         }
  649.     }
  650. }
  651.  
  652.  
  653. #        window_menu()
  654. #
  655. #        This function provides access to various windowing functions.  The 
  656. #        user can split the current window to create a new one, close it or 
  657. #        move it.
  658.  
  659. local function window_menu(){
  660.     local selected, escaped, orient, where, pstr
  661.  
  662.     selected = tolower(mouse_confirm("Window:  Split  Close  Move","CcSsMm"))
  663.     if (selected == "c"){
  664.         if (buffer_views() == 1) {
  665.             if (buffer_is_modified())
  666.                 confirm_save()
  667.             if ( and(current_key,0x00FF) != 0x1b )
  668.                 delete_buffer()
  669.         }
  670.         if ( and(current_key,0x00FF) != 0x1b )
  671.             delete_tiled_window()
  672.     } else if( selected == "s" ){
  673.         orient = tolower(mouse_confirm("Window Split:  Horizontal  Vertical","HhVv"))
  674.         escaped = ( and(current_key,0x00FF) == 0x1b )
  675.         if( !escaped ){
  676.             pstr = (orient == "h") ? "At line:  " : "At column:  "
  677.             do {
  678.                 where = prompt(pstr,"")
  679.                 escaped = ( and(current_key,0x00FF) == 0x1b )
  680.                 where = atoi(where)
  681.             } while (where == 0 && !escaped)
  682.             if( !escaped ){
  683.                 if (orient == "h"){
  684.                     split_window_horizontal(where)
  685.                 } else
  686.                     split_window_vertical(where)
  687.             }
  688.         }
  689.     } else
  690.     if (selected == "m") {
  691.         adjust_window()
  692.     }
  693. }
  694.  
  695.  
  696. #        msw_toggle_inclusive_block
  697. #
  698. #        This is the function that allows the user to make EXTENDED 
  699. #        selctions.  
  700.  
  701. function msw_toggle_inclusive_block(){
  702.     if( region_type() == INCLUSIVE_SELECTION){
  703.         if (extended) {
  704.             raise_anchor()
  705.             extended = FALSE
  706.         } else {
  707.             extended = TRUE
  708.         }
  709.     } else {
  710.         if (!region_type()) {
  711.             set_inclusive_mark()
  712.             extended = TRUE
  713.         } else
  714.             raise_anchor()
  715.     }
  716. }
  717.  
  718.  
  719. #        msw_toggle_column_block()
  720. #
  721. #        This function allows toggling a column selection on or off.
  722.  
  723. function msw_toggle_column_block(){
  724.     if( region_type() == COLUMN_SELECTION){
  725.         raise_anchor()
  726.         extended = FALSE
  727.     } else {
  728.         set_column_mark()
  729.         extended = TRUE
  730.     }
  731. }
  732.  
  733.  
  734. #        msw_reverse()
  735. #
  736. #        This function allows changing the case of a character or 
  737. #        selection.  
  738.  
  739. function msw_reverse() {
  740.  
  741.     if ( region_type() ) {
  742.         reverse()
  743.     } else {
  744.         drop_anchor(INCLUSIVE_SELECTION)
  745.         reverse()
  746.         raise_anchor()
  747.     }
  748. }
  749.  
  750.  
  751. #        confirm_save()
  752. #
  753. #        This function asks the user if he or she wishes to save the 
  754. #        changes to the current buffer.  The user may elect to save, not 
  755. #        save, or bail out.
  756.  
  757. function confirm_save()    {
  758.     if (toupper(mouse_confirm(sprintf("%s not saved. Enter Y to save, N to lose changes, Esc to cancel",
  759.         buffer_name),"YyNn")) == "Y") {
  760.         msw_save_buffer()
  761.     } else {
  762.             if (and(current_key,0x00FF) != 0x1b)
  763.                 buffer_flags = and(buffer_flags,not(BUFFER_MODIFIED))
  764.             else
  765.                 return FALSE
  766.     }
  767.     return TRUE
  768. }
  769.  
  770.  
  771. #        msw_save_buffer()
  772. #
  773. #        This function displays the file size after saving the file to 
  774. #        disk.
  775.  
  776. function msw_save_buffer() {
  777.     if (filemode(buffer_filename) != -1 && buffer_is_modified())
  778.         backup_file(buffer_filename)
  779.     write_buffer()
  780.     message("%d bytes saved",filesize(buffer_filename))
  781. }
  782.  
  783.  
  784. #        msw_part_visibles()
  785. #
  786. #        This function sets the primary non-printing characters
  787.  
  788. function msw_part_visibles()    {
  789.     default_visible_newlines = ""
  790.     default_visible_end_buffer = ""
  791.     visible_newlines = ""
  792.     visible_end_buffer = ""
  793. }
  794.  
  795.  
  796. #        msw_visibles()
  797. #
  798. #        This function sets all of the non-printing characters displayed by 
  799. #        Word.
  800.  
  801. function msw_visibles(){
  802.     msw_part_visibles()
  803.     default_visible_spaces = "·"
  804.     default_visible_tabs = ""
  805.     visible_spaces = "·"
  806.     visible_tabs = ""
  807. }
  808.  
  809.  
  810. #        msw_check_key()
  811. #
  812. #        This function is an event handler to check each key that is 
  813. #        pressed.  If the key is bound to the insert_key() function the 
  814. #        block marking is removed.  This means largely ASCII keys will have 
  815. #        this action.
  816.  
  817. function msw_check_key(){
  818.     if (region_type() == 1)
  819.         if (keymap_binding(int_to_key(current_key)) == "insert_key")
  820.             raise_anchor()
  821. }
  822.  
  823.  
  824. #        msw_next_word()
  825. #
  826. #        This function not only moves to the next word, it also highlights 
  827. #        it.  It honors an extended selection, as well.
  828.  
  829. function msw_next_word(){
  830.     local words = 2
  831.  
  832.     if (!region_type()){
  833.         words = 1
  834.     }
  835.     else
  836.     if (!extended) {
  837.         if ( prev_command == function_id("msw_prev_word"))
  838.             words = 1
  839.         raise_anchor()
  840.     }
  841.     if (!region_type()) {
  842.         next_word(words,wordDefStr)
  843.         set_exclusive_mark()
  844.         prev_word(1,wordDefStr)
  845.     } else
  846.         next_word(1,wordDefStr)
  847. }
  848.  
  849.  
  850. #        msw_next_word()
  851. #
  852. #        This function not only moves to the next word, it also highlights 
  853. #        it.  It honors an extended selection, as well.
  854.  
  855. function msw_prev_word(){
  856.     local words = 2
  857.  
  858.     if (!region_type() ){
  859.         words = 1
  860.     }
  861.     else
  862.     if (!extended) {
  863.         if (prev_command == function_id("msw_next_word"))
  864.             words = 1
  865.         raise_anchor()
  866.     }
  867.     if (!region_type()) {
  868.         prev_word(words,wordDefStr)
  869.         set_exclusive_mark()
  870.         next_word(1,wordDefStr)
  871.     } else
  872.         prev_word(1,wordDefStr)
  873. }
  874.  
  875.  
  876. #        msw_restore()
  877. #
  878. #        This function is an event handler to restore things to their 
  879. #        previous or vanilla state when the user changes emulations.
  880.  
  881. function msw_restore(){
  882.     mouse_selection_type = previous_mst
  883.     default_window_flags = previous_win_flags
  884.     delete_event(EVENT_KEYPRESS,function_id("msw_check_key"))
  885.     delete_event(EVENT_EMULATION_CHANGED,function_id("msw_restore"))
  886.     reset_visibles()
  887. }
  888.  
  889.  
  890. #        msw_left()
  891. #
  892. #        This function moves left unless it is at the beginning of the 
  893. #        line.  In that case it moves to the last character of the 
  894. #        preceding line.
  895.  
  896. function msw_left(){
  897.      if (current_column == 1)
  898.         prev_char()
  899.     else
  900.         left()
  901. }
  902.  
  903.  
  904. #        msw_right()
  905. #
  906. #        This function moves right unless it is at the end of the line.  In 
  907. #        that case it moves to the first character of the preceding line.
  908.  
  909. function msw_right(){
  910.     if (current_column > current_line_width)
  911.         next_char()
  912.     else
  913.         right()
  914. }
  915.  
  916.  
  917. #        msw_up()
  918. #
  919. #        This function moves the cursor to the preceding line.  If the 
  920. #        cursor is positioned beyond the end of the line, it moves to the 
  921. #        end of the line.
  922.  
  923. local line_position = 0
  924.  
  925. function msw_up(){
  926.     local line_length
  927.  
  928.     up()
  929.     line_length = current_line_width + 1
  930.     if (prev_key == current_key){
  931.         current_column = ( line_position > line_length) ? \
  932.             line_length : line_position
  933.     } else {
  934.         line_position = current_column
  935.         if (current_column > line_length)
  936.             current_column = line_length
  937.     }
  938. }
  939.  
  940.  
  941. #        msw_down()
  942. #
  943. #        This function moves the cursor to the following line.  If the 
  944. #        cursor is positioned beyond the end of the line, it moves to the 
  945. #        end of the line.
  946.  
  947. function msw_down(){
  948.     local line_length
  949.  
  950.     down()
  951.     line_length = current_line_width + 1
  952.     if (prev_key == current_key){
  953.         current_column = ( line_position > line_length) ? \
  954.             line_length : line_position
  955.     } else {
  956.         line_position = current_column
  957.         if (current_column > line_length)
  958.             current_column = line_length
  959.     }
  960. }
  961.  
  962.  
  963. #        mouse_confirm()
  964. #
  965. #        This function is like the confirm() function except that it allows 
  966. #        mouse input to indicate the desired response. 
  967.  
  968. function mouse_confirm(promptStr, keyStr)
  969. {
  970.     local ch, ok, old_win
  971.  
  972.     message(promptStr)
  973.     do {
  974.         ch = getkey()
  975.         if ((ch == LEFT_PRESS || ch == LEFT_CLICK) && dialog_window){
  976.             old_win = current_window
  977.             current_window = dialog_window
  978.             if (mouse_event_y == window_y0){
  979.                 ch = cap_near_mouse()
  980.             }
  981.             if (old_win)
  982.                 current_window = old_win
  983.             flush_keyboard()
  984.         } else {
  985.             ch = chr(and(ch,0x00ff))
  986.         }
  987.         if (!(ok = cindex(keyStr chr(0x1b),ch)))
  988.             beep()
  989.     } while (!ok)
  990.     if (ch == chr(0x1b))
  991.         message("Command canceled.")
  992.     else
  993.         message("")
  994.     return ch
  995. }
  996.  
  997.  
  998. #        cap_near_mouse()
  999. #
  1000. #        This function looks for an uppercase character or digit at or 
  1001. #        preceding the position where the mouse was clicked.  It returns 
  1002. #        that character if found and gives up (returning null string) if a 
  1003. #        colon or beginning or the screen is reached.
  1004.  
  1005. function cap_near_mouse(){
  1006.     local ch, x_loc
  1007.  
  1008.     x_loc = mouse_event_x
  1009.     do {
  1010.         window_cursor_x = x_loc
  1011.         ch = read_window(1)
  1012.         x_loc--
  1013.     } while (ch != ":" && x_loc > window_x0 && !isupper(ch) && !isdigit(ch))
  1014.     if (isupper(ch) || isdigit(ch))
  1015.         return ch
  1016.     else
  1017.         return ""
  1018. }
  1019.  
  1020.  
  1021. #        msw_write_file()
  1022. #
  1023. #        This function is used by the Print File option to allow printing 
  1024. #        the current contents of the buffer to a different filename without 
  1025. #        saving the buffer to its original file or changing the original 
  1026. #        filename.
  1027.  
  1028. function msw_write_file(){
  1029.     local filename, ok
  1030.  
  1031.     filename = prompt("Write to file:  ")
  1032.     if (filemode(filename) == -1) {
  1033.         ok = mouse_confirm("File exists.  Enter Y to overwrite N to abandon." \
  1034.                 "YyNn")
  1035.         if (tolower(ok) == "n")
  1036.             return
  1037.     }
  1038.     message("%d bytes written.",write_buffer(filename))
  1039. }
  1040.  
  1041. function msw_search_restore(){
  1042.     delete_event(EVENT_KEYPRESS,function_id("msw_search_restore"))
  1043.     raise_anchor()
  1044. }
  1045.  
  1046.  
  1047. function msw_search_again() {
  1048.     if ( search_again() )
  1049.         display_update()
  1050.         attach_event_handler(EVENT_KEYPRESS, \
  1051.             function_id("msw_search_restore"))
  1052. }