home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / p2demo21.exe / PEL / BUFFERS.PEL < prev    next >
Text File  |  1995-03-23  |  46KB  |  1,704 lines

  1. # $Header:   P:\source\wmacros\buffers.pev   1.115   23 Mar 1995 10:02:52   WALKER  $
  2. ## $Tabs:4 7$
  3.  
  4. ##############################################################################
  5. #
  6. #       Compuware Corporation
  7. #         31440 Northwestern Highway
  8. #           Farmington Hills, Michigan 48334-2564
  9. #
  10. #   This source code listing contains information that is
  11. #   proprietary to Compuware Corporation and may not be copied
  12. #   duplicated, translated, transmitted, stored, retrieved
  13. #   or in any manner or by any method conveyed or disclosed
  14. #   to a third party or parties without express written
  15. #   permission from Compuware Corporation.
  16. #
  17. #  
  18. ##############################################################################
  19.  
  20.  
  21. #### $Workfile:   buffers.pel  $: Support for Buffers
  22.  
  23.  
  24. #
  25. # buffer flags:
  26. #
  27. global  BUFFER_SYSTEM                   = 0x0001
  28. global  BUFFER_IN_VIRTUAL_SPACE         = 0x0002
  29. global  BUFFER_POSITION_IS_VIRTUAL      = 0x0002 # This is the documented one
  30. global  BUFFER_CONTROL_Z_IS_EOF         = 0x0004
  31. #reserved                               = 0x0008
  32. global  BUFFER_READ_ONLY                = 0x0010
  33. global  BUFFER_AUTOSAVE_MODIFIED        = 0x0020
  34. global  BUFFER_MODIFIED                 = 0x0040
  35. global  BUFFER_OVERTYPE_MODE            = 0x0080
  36. global  BUFFER_EXPAND_TABS              = 0x0100
  37. global  BUFFER_WP_ENABLED               = 0x0200
  38. global  BUFFER_REAL_SPACE_ONLY          = 0x0400
  39. global  BUFFER_SNAP_TO_EOL              = 0x0800
  40. global  BUFFER_NO_UNDO                  = 0x1000
  41. global  BUFFER_TABS_TO_SPACES           = 0x2000
  42. global  BUFFER_WHOLE_LINES              = 0x4000
  43. global  BUFFER_SCRATCH                  = 0x100000    # buffer is temp
  44. global  BUFFER_WP_WRAP_CONTINUOUS       = 0x200000
  45. global  BUFFER_LOCKED                   = 0x400000    # R/O
  46. global  BUFFER_WP_CARRY_COMMENTS        = 0x800000 
  47. # Analyzer Buffer Flags
  48. global  BUFFER_ANALYZER_RUNNING         = 0x1000000 
  49. global  BUFFER_ANALYZER_SUCCESSFUL      = 0x2000000 
  50. global  BUFFER_ANALYZER_DDE_TOPIC       = 0x4000000 
  51. global  BUFFER_ANALYZER_FILE_SAVED      = 0x8000000
  52.  
  53. # not a buffer_flag, used to mark a buffer for "save_as" in the save_changes_list
  54. local   SAVE_AS_BUFFER                  = 0x200000                                                       
  55.  
  56. #user defined buffer flags
  57. global  DOS_BUFFER_FLAG =               0x00020000;
  58.  
  59. global save_changes_list                = 0
  60. global process_ok                       = 1
  61. global   buffer_list_index               = 0
  62.  
  63. # flag that indicates a new window is to be created when
  64. # a file is opened JAM: he is a primitive now (1-19-95)
  65. #  global create_new_bufwin = 0
  66.  
  67. ##
  68. ## functions to interrogate/set bits in the buffer_flags word
  69. ##
  70.  
  71. global function buffer_is_modified()
  72. {
  73.    return ( and(buffer_flags, BUFFER_MODIFIED) != 0 )
  74. }
  75.  
  76. global function set_default_buffer_flag( mask, value )
  77. {
  78.    if ( value )
  79.       default_buffer_flags = or( default_buffer_flags, mask )
  80.    else
  81.       default_buffer_flags = and( default_buffer_flags, not(mask) )
  82.  
  83.    return value
  84. }
  85.  
  86.  
  87. ############################################################################
  88.  
  89. ##
  90. ## buffer_list() - pop up a dialog listing the active buffers
  91. ##
  92.  
  93. local   bufferListId[]          # array containing the buffer IDs
  94. local   bufferListFlag[]        # array containing the buffer flags
  95. local   bufferListOrder[]       # order in which filenames go in buffer
  96. local   bufferListCommand[]     # array containing the buffer changes
  97.  
  98. local   BLCMD_FLAGMASK  = 0x50ffff
  99. local   BLCMD_CLOSED    = 0x10000
  100.  
  101. global function buffer_list( systemFlag, old_list )
  102. {
  103.    local   firstBuffer
  104.    local   fn
  105.    local   retval
  106.    local   wf
  107.  
  108.    process_ok        = 1
  109.    buffer_list_index = 0
  110.  
  111.    # build the buffer list names and flags arrays
  112.    #
  113.    if ( and( buffer_flags, BUFFER_SYSTEM ) && !systemFlag )
  114.    {
  115.       # ensure that the starting buffer is not a system buffer
  116.       next_buffer()
  117.    }
  118.  
  119.    # fill buffer listbox with the appropriate files
  120.    #
  121.    if ( save_changes_list )
  122.    {
  123.       fill_buffer_list( or(BUFFER_SCRATCH, BUFFER_MODIFIED), 1 )
  124.       fill_buffer_list( BUFFER_MODIFIED, 0 )
  125.  
  126.       # if no buffers need saving, just quit
  127.       if ( buffer_list_index == 0 )
  128.          return TRUE
  129.    }
  130.    else
  131.       fill_buffer_list( BLCMD_FLAGMASK, 0 )
  132.  
  133.    # invoke the dialog manager
  134.    fn = buffer_list_dialog(old_list)
  135.  
  136.    if ( process_ok )
  137.    {
  138.       # processing all pending commands
  139.       if ( save_changes_list )
  140.       {
  141.          retval = bufferListProcessCommands()
  142.  
  143.          # remove the buffer list arrays
  144.          delete bufferListId
  145.          delete bufferListFlag
  146.          delete bufferListCommand
  147.          delete bufferListOrder
  148.  
  149.          return retval
  150.       }
  151.       else 
  152.          bufferListProcessCommands()
  153.    }
  154.  
  155.    # remove the buffer list arrays
  156.    delete bufferListId
  157.    delete bufferListFlag
  158.    delete bufferListCommand
  159.    delete bufferListOrder
  160.  
  161.    # process the return value
  162.    if ( fn )
  163.    {
  164.       create_buf_and_win_key( fn );
  165.    }
  166. }
  167.  
  168. local function fill_buffer_list( flag_mask, match_all )
  169. {
  170.    local   firstBuffer, systemFlag, fn
  171.  
  172.    firstBuffer = current_buffer;
  173.    do {
  174.       # save the full file name, buffer id, and buffer flags
  175.       fn = buffer_filename
  176.       if ( fn == "" )
  177.          fn = "[" buffer_name "]"
  178.  
  179.       if ( match_all && (flag_mask == and(buffer_flags, flag_mask)) )
  180.          {
  181.          if ( !(fn in bufferListId) )
  182.             {
  183.             bufferListId[ fn ] = current_buffer
  184.             bufferListFlag[ fn ] = and( buffer_flags, BLCMD_FLAGMASK )
  185.             bufferListOrder[ buffer_list_index ] = fn
  186.             buffer_list_index++
  187.             }
  188.          }
  189.       if ( !match_all && and(buffer_flags, flag_mask) )
  190.          {
  191.          if ( !(fn in bufferListId) )
  192.             {
  193.             bufferListId[ fn ] = current_buffer
  194.             bufferListFlag[ fn ] = and( buffer_flags, BLCMD_FLAGMASK )
  195.             bufferListOrder[ buffer_list_index ] = fn
  196.             buffer_list_index++
  197.             }
  198.          }
  199.  
  200.       next_buffer( "", systemFlag, 1 )
  201.    } while ( current_buffer != firstBuffer )
  202.  
  203. }
  204.  
  205.  
  206. # Update the list box to reflect the current buffer list.
  207. # The input argument is the changed selection.  If null, the
  208. # entire list is updated.
  209. #
  210. global function bufferListUpdate( sel, fnSelected )
  211. {
  212.    local   attrib
  213.    local   reselect
  214.    local   index
  215.  
  216.    # Clear the list box
  217.    #
  218.    if ( fnSelected )
  219.    {
  220.       set_dialog_item(dhBufferList, IDL_LIST, DAC_DELETE_INDEX, sel )
  221.       attrib = bufferListAttributes( fnSelected ) # bufferListOrder[index] )
  222.       set_dialog_item( dhBufferList, IDL_LIST, DAC_ADD_INDEX, attrib, sel )
  223.       set_dialog_item( dhBufferList, IDL_LIST, DAC_SELECT_INDEX, sel)
  224.       return
  225.    }
  226.  
  227.    set_dialog_item(dhBufferList, IDL_LIST, DAC_DELETE_ITEM )
  228.  
  229.    # Fill the list box with the names obtained above
  230.    #
  231.    for ( index in bufferListOrder )
  232.    {
  233.       attrib = bufferListAttributes( bufferListOrder[index] )
  234.       set_dialog_item( dhBufferList, IDL_LIST, DAC_ADD_ITEM, attrib)
  235.  
  236.       # find updated selection
  237.       if ( bufferListOrder[index] == fnSelected )
  238.          reselect = attrib
  239.    }
  240.  
  241.    # Reset the selection to where it was before the current change
  242.    #
  243.    if ( reselect )
  244.       set_dialog_item( dhBufferList, IDL_LIST, DAC_SELECT_ITEM, reselect, 0)
  245.    else
  246.       set_dialog_item( dhBufferList, IDL_LIST, DAC_SELECT_ITEM,
  247.                        bufferListOrder[ 0 ], 0)
  248. }
  249.  
  250. # determine the name of the currently selected buffer in the list box
  251. #
  252. global function bufferListSelected()
  253. {
  254. #   local fn
  255.  
  256.    # Is a buffer currently selected?
  257.    return query_dialog_item(dhBufferList, IDL_LIST, DAC_FIRST_INDEX )
  258. }
  259.  
  260. global function bufferListText(index)
  261. {
  262.    local fn
  263.    
  264.    return bufferListOrder[ index ]
  265. }
  266.  
  267. # determine the name of the next selected buffer in the list box
  268. #
  269. global function bufferListNextSelected()
  270. {
  271.    local index
  272.  
  273.    # Is another buffer currently selected?
  274.    index = query_dialog_item(dhBufferList, IDL_LIST, DAC_NEXT_INDEX )
  275.    return index
  276. }
  277.  
  278. # given a buffer name, assemble a string which indicates the current
  279. # attributes
  280. #
  281. local function bufferListAttributes( fn )
  282. {
  283.    local   flags
  284.  
  285.    # get the buffer flags to use
  286.    #
  287.    if ( ! ( fn in bufferListFlag ))
  288.       error( "<" fn "> internal error in bufferListAttributes" )
  289.  
  290.    flags = bufferListFlag[ fn ]
  291.  
  292.    if ( fn in bufferListCommand )
  293.       flags = xor( flags, bufferListCommand[ fn ] )
  294.  
  295.    # append selected attributes
  296.    #
  297.    if ( and( flags, BUFFER_MODIFIED ))
  298.       fn = fn " (MOD)"
  299.  
  300.    if ( and( flags, BUFFER_READ_ONLY ))
  301.       fn = fn " (RO)"
  302.  
  303.    if ( and( flags, BUFFER_SCRATCH ))
  304.       fn = fn " (TEMP)"
  305.  
  306.    if ( and( flags, BUFFER_SYSTEM ))
  307.       fn = fn " (System Buffer)"
  308.  
  309.    if ( and( flags, BLCMD_CLOSED ))
  310.       fn = fn " (CLOSE)"
  311.  
  312.    if ( and( flags, BUFFER_LOCKED ))
  313.       fn = fn " (LOCKED)"
  314.  
  315.    return fn
  316. }
  317.  
  318. # Enable the buttons which are currently available.
  319. #
  320. global function bufferListActivateButtons()
  321. {
  322.    local   dac, flag, i
  323.  
  324.    if ( save_changes_list )
  325.    {
  326.       if ( query_dialog_item(dhBufferList, IDL_LIST, DAC_COUNT_SELECTED) == 1 )
  327.       {
  328.          flag = bufferListFlag[ bufferListOrder[bufferListSelected()] ]
  329.  
  330. #        if ( and(flag, BUFFER_SCRATCH) )
  331. #        {
  332. #           set_dialog_item( dhBufferList, IDB_SAVE_FILE, DAC_DISABLE, 1 )
  333. #           set_dialog_item( dhBufferList, IDB_SCL_SAVE_ALL, DAC_DISABLE, 1 )
  334. #        }
  335. #        else if ( and(flag, BUFFER_MODIFIED) )
  336. #        {
  337. #           set_dialog_item( dhBufferList, IDB_SAVE_FILE, DAC_ENABLE, 1 )
  338. #           set_dialog_item( dhBufferList, IDB_SCL_SAVE_ALL, DAC_ENABLE, 1 )
  339. #        }
  340.       }
  341.    }
  342.    else
  343.    {
  344.       # are any buffers currently selected?
  345.       #
  346.       if ( query_dialog_item(dhBufferList, IDL_LIST, DAC_COUNT_SELECTED ))
  347.          dac = DAC_ENABLE
  348.       else
  349.          dac = DAC_DISABLE
  350.    
  351.       # enable or disable all buttons
  352.       #
  353.       for ( i =  FIRST_BL_BUTTON; i <= LAST_BL_BUTTON; i++ )
  354.          set_dialog_item(dhBufferList, i, dac, "" )
  355.    }
  356. }
  357.  
  358.  
  359. # Register a buffer list command for subsequent processing if
  360. # the buffer list "Ok" button is selected.  The "cmd" argument
  361. # is the index of the selected push button.
  362. #
  363. function bufferListRegisterCommand( cmd )
  364. {
  365.    local   i
  366.    local   fn
  367.    local   sel
  368.    local   flags
  369.    local   saved_buffer
  370.    local   fmode
  371.  
  372.    # Get the first selected buffer list (if any).
  373.    sel = bufferListSelected()
  374.    if ( sel < 0 )
  375.       return;
  376.  
  377.    fn = bufferListText(sel)
  378.  
  379.    # Save the Selected buffer (if only one is selected)
  380. #   if ( query_dialog_item(dhBufferList, IDL_LIST, DAC_COUNT_SELECTED ) == 1 )
  381. #     sel = fn
  382.  
  383.    while ( sel >= 0 )
  384.    {
  385.       # Get the current command flags upon which to operate.
  386.       #
  387.       if ( ! ( fn in bufferListFlag ))
  388.          error( "<" fn "> internal error in bufferListRegisterCommand" )
  389.    
  390.       flags = 0+bufferListCommand[ fn ]
  391.    
  392.       # Update the appropriate command flag.  An xor operation is used
  393.       # in order to toggle the current setting.
  394.       #
  395.       if ( (cmd == IDB_SAVE_FILE) && !and( bufferListFlag[ fn ], BUFFER_SCRATCH) )
  396.       {
  397.          fmode = filemode(fn)
  398.          if ( fmode != -1 && and(fmode, _READ_ONLY) )
  399.             warning("Unable to save %s, the file must be writable.", fn)
  400.          else
  401.          {
  402.             if ( and( bufferListFlag[ fn ], BUFFER_MODIFIED ))
  403.                bufferListCommand[ fn ] = xor( flags, BUFFER_MODIFIED )
  404.          }
  405.       }
  406.       else if ( cmd == IDB_DELETE_FILE )
  407.       {
  408.          bufferListCommand[ fn ] = xor( flags, BLCMD_CLOSED )
  409.       }
  410.       else if ( cmd == IDB_READONLY )
  411.       {
  412.          bufferListCommand[ fn ] = xor( flags, BUFFER_READ_ONLY )
  413.       }
  414.       else if ( cmd == IDB_LOCK_FILE )
  415.       {
  416.          bufferListCommand[ fn ] = xor( flags, BUFFER_LOCKED )
  417.       }
  418.       
  419.       if ( (cmd == IDB_SCL_SAVE_AS) || ((cmd == IDB_SAVE_FILE) && and( bufferListFlag[ fn ], BUFFER_SCRATCH)) )
  420.       {
  421.          for ( i in bufferListOrder )
  422.          {
  423.             if ( bufferListOrder[i] == fn )
  424.                break
  425.          }
  426.  
  427.          if ( prefix(fn, 1) == "{" )
  428.             bufferListOrder[i] = trim( ltrim( fn, "{"), "}" )  
  429.          else
  430.             bufferListOrder[i] = "{" fn "}"
  431.  
  432.          bufferListFlag[ bufferListOrder[i] ] = bufferListFlag[ fn ]
  433.          bufferListFlag[ fn ] = ""
  434.  
  435.          bufferListId[ bufferListOrder[i] ] = bufferListId[ fn ]
  436.          bufferListId[ fn ] = ""
  437.          
  438.          if ( and( bufferListFlag[ bufferListOrder[i] ], BUFFER_SCRATCH ))
  439.             bufferListCommand[ bufferListOrder[i] ] = xor( flags, BUFFER_SCRATCH )
  440.          else
  441.             bufferListCommand[ bufferListOrder[i] ] = xor( flags, SAVE_AS_BUFFER )
  442.  
  443.          bufferListCommand[ bufferListOrder[i] ] = xor( bufferListCommand[ bufferListOrder[i] ], 
  444.                                                             BUFFER_MODIFIED )
  445.  
  446.          bufferListCommand[ fn ] = ""
  447.  
  448.  
  449.          if ( prefix(fn, 1) == "{" )
  450.          {
  451.             bufferListCommand[ bufferListOrder[i] ] = and( bufferListCommand[ bufferListOrder[i] ], 
  452.                                                            not(BUFFER_MODIFIED) )
  453.          
  454.             fn = trim( ltrim( fn, "{"), "}" )   
  455.          }
  456.          else
  457.          {
  458.             bufferListCommand[ bufferListOrder[i] ] = or( bufferListCommand[ bufferListOrder[i] ], 
  459.                                                                BUFFER_MODIFIED )
  460.             fn = "{" fn "}"
  461.          }
  462.       }
  463.    
  464.       # Update the list box to refect the new status.
  465.       #
  466.       bufferListUpdate( sel, fn )
  467.  
  468.       # Get the Next Selected Item from the BufferList (if any).
  469.       #
  470.       sel = bufferListNextSelected()
  471.       if (sel >= 0 )
  472.          fn = bufferListText(sel)
  473.    }
  474. }
  475.  
  476. # Process the list of buffer list commands registered above.
  477. #
  478. function bufferListProcessCommands()
  479. {
  480.    local   fn
  481.    local   bufid
  482.    local   flags
  483.    local   bl_buffer
  484.    local   fattr
  485.    local   fmode, bmode
  486.    local   modeMsg
  487.    local   resp
  488.    local   old_sb
  489.    local   errors = 0
  490.    local   start_win
  491.    local   buffer_closed = FALSE
  492.  
  493.    # Process the changes made to each modified buffer
  494.    #
  495.    for ( fn in bufferListCommand )
  496.    {
  497.       # get the current state of changes to this buffer
  498.       if ( ! ( fn in bufferListId ))
  499.       {
  500.          error( "<" fn "> internal error in bufferListProcessCommand" )
  501.          errors = TRUE
  502.       }
  503.       else if ( ! ( fn in bufferListFlag ))
  504.       {
  505.          error( "<" fn "> internal error in bufferListProcessCommand" )
  506.          errors = TRUE
  507.       }
  508.  
  509.       bufid = bufferListId[ fn ]
  510.       flags = bufferListCommand[ fn ]
  511.  
  512.       # "save" command
  513.       #
  514.       if ( and( flags, BUFFER_MODIFIED ))
  515.       {
  516.          fmode = filemode(fn)
  517.          if ( fmode != -1 && and(fmode, _READ_ONLY) )
  518.          {
  519.             warning("Unable to save %s, the file must be writable.", fn)
  520.             errors++
  521.          }
  522.          else
  523.          {
  524.             # write the buffer
  525.             bl_buffer = current_buffer
  526.             current_buffer = bufid
  527.  
  528.             backup_file(buffer_filename)
  529.             if (!write_buffer())
  530.                errors++
  531.  
  532.             current_buffer = bl_buffer
  533.          }
  534.       }
  535.  
  536.       # "save as" command
  537.       #
  538.       if ( and(flags, BUFFER_SCRATCH) || and(flags, SAVE_AS_BUFFER) )
  539.       {
  540.          # write the buffer
  541.          bl_buffer = current_buffer
  542.          current_buffer = bufid
  543.          if ( !gui_saveas() )
  544.             errors++
  545.  
  546.          current_buffer = bl_buffer
  547.       }
  548.  
  549.       # "close" command
  550.       #
  551.       if ( and( flags, BLCMD_CLOSED ))
  552.       {
  553.          # delete the buffer
  554.          delete_buffer( bufid )
  555.          buffer_closed = TRUE
  556.       }
  557.  
  558.       # "lock" command
  559.       #
  560.       if ( and( flags, BUFFER_LOCKED ))
  561.       {
  562.          bl_buffer = current_buffer
  563.          current_buffer = bufid
  564.          
  565.          if ( and(buffer_flags, BUFFER_LOCKED) )
  566.            unlock_buffer( bufid )
  567.          else
  568.            lock_buffer( bufid )
  569.            
  570.          current_buffer = bl_buffer
  571.       }
  572.  
  573.       # "read-only" toggle command
  574.       #
  575.       if ( and( flags, BUFFER_READ_ONLY ))
  576.       {
  577.          # update the buffer's read-only bit
  578.          if ( !and( flags, BLCMD_CLOSED ))
  579.          {
  580.             bl_buffer       = current_buffer
  581.             current_buffer  = bufid
  582.             buffer_flags    = xor(
  583.                                     buffer_flags,
  584.                                     BUFFER_READ_ONLY )
  585.             current_buffer  = bl_buffer
  586.          }
  587.  
  588.          # update the disk file's read-only bit
  589.          fattr = filemode( fn )
  590.  
  591.          if ( fattr >= 0 )
  592.          {
  593.             # would the disk read-only mode match the buffer's?
  594.             fmode = !!and( fattr, _READ_ONLY )
  595.  
  596.             bmode = !!and( bufferListFlag[ fn ], BUFFER_READ_ONLY )
  597.  
  598.             # if so, make the change to the disk file
  599.             if ( fmode == bmode )
  600.             {
  601.                modeMsg = bmode ? "writable" : "read-only"
  602.          
  603.                optional_function( "disable_status_bar_prompts" )
  604.  
  605.                resp = toupper( confirm( \
  606.                      "make file " fn " " modeMsg   \
  607.                      " also? [yn]", "yYnN" ))
  608.  
  609.                optional_function( "restore_status_bar" )
  610.  
  611.                if ( resp == "Y" ) {
  612.                   message( "making file " fn " " modeMsg )
  613.                   fattr = xor( fattr, _READ_ONLY )
  614.                   filemode( fn, fattr )
  615.                } else
  616.                   # replace misleading "canceled" message
  617.                   message( "" )
  618.             }
  619.          }
  620.       }
  621.    }
  622.  
  623.    if ( buffer_closed )
  624.    {
  625.       start_win = current_window
  626.    
  627.       do {
  628.          update_current_caption();
  629.          if(and(buffer_flags, BUFFER_SYSTEM))
  630.             next_buffer();
  631.       }
  632.       while( next_window("", 0, 1) != start_win )
  633.    
  634.       current_window = start_win
  635.    }
  636.  
  637.    update_current_view()
  638.  
  639.    if ( errors )
  640.       return FALSE
  641.    else
  642.       return TRUE
  643. }
  644.  
  645.  
  646. ############################################################################
  647.  
  648. ## prompt user for filename for edit_file() and read_file() operations
  649. #
  650. global function edit_file_key( fn )
  651. {
  652.    if ( !fn )
  653.       fn = prompt_history( "EDITFILE", "File: ", "", 1, 1, "edit_file_dialog" )
  654.  
  655.    if( fn )
  656.    {
  657.       # add prompt history here so even invalid fn arguments get added.
  658.       add_prompt_history( "EDITFILE", fn )
  659.  
  660.       if( edit_file( fn ))
  661.       {
  662.          display_filename()
  663.          return TRUE
  664.       }
  665.       else
  666.       {
  667.          message( "Cannot open file '%s'", fn )
  668.          return FALSE
  669.       }
  670.    }
  671.    else
  672.       return FALSE
  673. }
  674.  
  675. global function read_file_key( fn )
  676. {
  677.    local   result = FALSE
  678.  
  679.    if ( !fn )
  680.       fn = prompt_history( "EDITFILE", "File to read: ", "", 1, 1, "insert_prompt" )
  681.  
  682.    if ( fn )
  683.    {
  684.       result = read_file( fn )
  685.       if ( !result )
  686.          message( "Cannot read '%s'", fn )
  687.       else
  688.          add_prompt_history( "EDITFILE", fn )
  689.    }
  690.    return result
  691. }
  692.  
  693.  
  694. ############################################################################
  695.  
  696. global function delete_buffer_without_asking( bufferToDelete )
  697. {
  698.    local   priorBuffer = current_buffer
  699.  
  700.    if (argcount())
  701.       current_buffer = bufferToDelete
  702.  
  703.    if ( current_buffer == bufferToDelete )
  704.    {
  705.       #
  706.       # if the buffer exists, clear the buffer modified bit first
  707.       # in order to avoid the "buffer had been modified" prompt
  708.       #
  709.       buffer_flags = and( buffer_flags, not( BUFFER_MODIFIED ) )
  710.       delete_buffer()
  711.       current_buffer = priorBuffer 
  712.    }
  713. }
  714.  
  715. global function display_filename( useNotify )
  716. {
  717.    local   fn = buffer_filename
  718.  
  719.    # get the status of the modified bit in the current buffer
  720.    if ( and( buffer_flags, BUFFER_MODIFIED ))
  721.       fn = fn "*"
  722.  
  723.    if ( useNotify )
  724.       notify( "File: " fn )
  725.    else
  726.       message( "File: " fn )
  727. }
  728.  
  729. global function change_output_name( fn )
  730. {
  731.    local   newOutputName
  732.  
  733.    if ( fn )
  734.       newOutputName = fn
  735.    else
  736.       newOutputName = prompt("Enter new output file name: ", "", "change_output_name_dialog" )
  737.  
  738.    if (newOutputName)
  739.    {
  740.       buffer_filename = newOutputName
  741.       buffer_name     = path_fname(buffer_filename) path_ext(buffer_filename)
  742.       buffer_flags    = or( buffer_flags, BUFFER_MODIFIED )
  743.       buffer_flags    = and( buffer_flags, not(BUFFER_SCRATCH) )
  744.  
  745.    #   display_redraw()
  746.       update_current_caption()
  747.       update_current_view()
  748.    
  749.       new_edit_file();
  750.       syntax_new_buffer();
  751.    }
  752.  
  753.    return newOutputName
  754. }
  755.  
  756.  
  757. #
  758. # paste()
  759. #
  760. # a synonym for insert_scrap()
  761. #
  762. #global function paste()                                         #PUBLIC #INT
  763. #{
  764. #   return insert_scrap()
  765. #}
  766.  
  767. # cut()
  768. #
  769. # a synonym for delete_to_scrap()
  770. #
  771. #global function cut()                                           #PUBLIC #INT
  772. #{
  773. #   return delete_to_scrap()
  774. #}
  775.  
  776. #
  777. # print_buffer() is now a primitive
  778. #
  779. #global function print_buffer()                                  #PUBLIC #VOID
  780. #{
  781. #  local expand_tabs = and( buffer_flags, BUFFER_EXPAND_TABS );
  782. #
  783. #  message( "Printing..." );
  784. #
  785. #  buffer_flags = or( buffer_flags, BUFFER_EXPAND_TABS );
  786. #
  787. #  if ( selection_type() != 0 )
  788. #     write_marked_block( print_device )
  789. #  else
  790. #     write_buffer( print_device )
  791. #
  792. #
  793. #
  794. #  if (!expand_tabs)
  795. #     buffer_flags = and( buffer_flags, not(BUFFER_EXPAND_TABS) );
  796. #
  797. #  message( "Printing complete." )
  798. #}
  799.  
  800.  
  801. #
  802. #  Backups will be made in the appropriate directory according to the 
  803. #  following rules:
  804. #
  805. #
  806. #
  807. #    Using c:\foo\test.c  as the same test file
  808. #                         and editor_path() is c:\cpe
  809. #
  810. #
  811. #                       \    backup_file_ext ==
  812. #                        \
  813. #                         \                          │
  814. #                          \         ".bak"          │     "" | "*"
  815. #  backup_directory      == ┌────────────────────────┼──────────────────────┐
  816. #                           │                        │                      │
  817. #                        "" │ c:\cpe\backup\test.bak │ c:\cpe\backup\test.c │
  818. #                           │                        │                      │
  819. #                        ───┼────────────────────────┼──────────────────────┤
  820. #                           │                        │                      │
  821. #                 "." | "*" │ c:\foo\test.bak        │ c:\foo\test.bak      │
  822. #                           │                        │                      │
  823. #                        ───┼────────────────────────┼──────────────────────┤
  824. #                           │                        │                      │
  825. #         (any dir)  "~~~\" │ ~~~\test.bak           │ ~~~\test.c           │
  826. #                           │                        │                      │
  827. #                           └────────────────────────┴──────────────────────┘
  828. #
  829. #    Values for backup_directory:
  830. #      "*"|"."  - means the directory where the file was originally loaded from
  831. #      "~~~\"   - any valid drive and path name.
  832. #      ""       - use the editor_path()\backup directory
  833. #
  834. #    Values for backup_file_ext:
  835. #      ".bak"   - and valid extension to be used as the new backup file extension
  836. #                 ( if !("*" or "") always uses ".bak" for now
  837. #      ""       - use the current file's extension as the backup file extension
  838. #      "*"      - use the current file's extension as the backup file extension
  839. #
  840.  
  841. global backup_directory
  842. global backup_file_ext
  843.  
  844. global function backup_file( fname )
  845. {
  846.    local   pos, bakFilename
  847.    local   pname
  848.    local   new_ext  = ".bak";
  849.    local   new_path;
  850.    local   dest_drive;
  851.    local   fmode;
  852.    local   cwd
  853.  
  854.    if (length(fname) == 0)
  855.       fname = buffer_filename
  856.  
  857.    fname = buildpath( fname );
  858.    if (!cindex(fname, "."))
  859.       fname = fname "."
  860.  
  861.    if (!backup_files_enabled || (filemode( fname ) == -1))
  862. #      return TRUE;
  863. #     tell caller that the backup file was not created but
  864. #     that's ok....maybe?
  865.       return 2;
  866.    else
  867.    {
  868.       # change to fname's directory.
  869.       cwd = getcwd()
  870.       chdir(path_path(fname))
  871.  
  872.       if (backup_file_ext == "*" || !backup_file_ext)
  873.          new_ext = path_ext( fname );
  874.       else
  875.          new_ext = backup_file_ext;
  876.  
  877.       if (substr( new_ext, 1, 1) != ".")
  878.          new_ext = "." new_ext;
  879.  
  880.       new_ext = substr( new_ext, 1, 4);
  881.  
  882.       if (backup_directory && backup_directory != "*" && \
  883.                               backup_directory != "." )
  884.       {
  885.          backup_directory = buildpath( backup_directory );
  886.          new_path = trim( backup_directory, "\\" );
  887.          if (filemode( new_path ) != _SUBDIR )
  888.          {
  889.             warning( "Invalid backup directory path: " backup_directory "." );
  890.             backup_directory = "";
  891.          }
  892.       }
  893.  
  894.       if (!backup_directory)
  895.       {
  896.          new_path = editor_path()
  897.          if (length(new_path))
  898.             new_path = new_path "backup"
  899.          else
  900.             new_path = buildpath( "\\cpe\\backup" );
  901.  
  902.          if (!mkpath(new_path))
  903.          {
  904.             warning( "Cannot create backup directory '%s'.", new_path );
  905.             backup_directory = "*";
  906.          }
  907.       }
  908.  
  909.       if (backup_directory == "*" || backup_directory == ".")
  910.          new_path = path_path( fname )
  911.  
  912.       pname = "\\" path_fname( fname );
  913.       new_path = trim( new_path, "\\" );
  914.  
  915.       bakFilename =  new_path pname new_ext;
  916.       if (bakFilename == fname )
  917.          bakFilename = new_path pname ".bak"
  918.  
  919.       # change back to the original working directory.
  920.       chdir(cwd)
  921.  
  922.       message( "Writing backup file '%s'.", bakFilename );
  923.  
  924.       dest_drive = substr( bakFilename, 1, 1 );
  925.       if (bakFilename != fname )
  926.       {
  927.          fmode = filemode( bakFilename );
  928.          if ( (fmode != -1) && and( fmode, _READ_ONLY ))
  929.          {
  930.             if (tolower(confirm( "`" bakFilename "' is read-only, overwrite?",
  931.                                  "yYnN")) != "y")
  932.             {
  933.                return FALSE;
  934.             }
  935.  
  936.             filemode( bakFilename, _NORMAL );
  937.          }
  938.  
  939. # DWM 5/23/94  ** Do not move for backup **
  940. # Always copy file, otherwise extended attributes are lost
  941. #         if ( dest_drive == substr( fname, 1, 1 ) )
  942. #         {
  943. #            # backing up on same disk, just rename it.
  944. #            #
  945. #            if (buffer_size * 2 >= disk_free( dest_drive ))
  946. #            {
  947. #               warning( "Not enough disk space on drive " dest_drive \
  948. #                        ": no backup created!");
  949. #
  950. #               return FALSE;
  951. #            }
  952. #
  953. #            unlink( bakFilename );
  954. #            return rename( fname, bakFilename )
  955. #         }
  956. #         else
  957.             return filecopy( fname, bakFilename )
  958.       }
  959.    }
  960.    return FALSE
  961. }
  962.  
  963.  
  964. ## write_all_buffers()
  965. #
  966. # write all of the modified buffers to their output file but first
  967. # check the read/write permission of the output file.  Two passes are
  968. # made at writing the files: first, save all of the r/w files, second,
  969. # try to save the read-only files which will generate an error.
  970. #
  971. global function write_all_buffers()                             #PUBLIC #INT
  972. {
  973.    local   old_curnt_buffer = current_buffer
  974.    local   fmask = BUFFER_MODIFIED + BUFFER_READ_ONLY
  975.    local   buf = current_buffer;
  976.    local   exitBuffers = 0;
  977.  
  978.    if  ( and(buffer_flags,BUFFER_SYSTEM) )
  979.       old_curnt_buffer = next_buffer();
  980.  
  981.    #
  982.    # loop through the list of buffers and write all of the
  983.    # read/write files first
  984.    #
  985.    do {
  986.       #
  987.       # see if the buffer has changed
  988.       #
  989.       if ( and( buffer_flags, fmask ) == BUFFER_MODIFIED)
  990.       {
  991.          #
  992.          # don't save read-only files this pass
  993.          #
  994.          message( buffer_filename )
  995.          if (!backup_file( buffer_filename ))
  996.          {
  997.             warning( "Unable to create backup file for `" buffer_filename "'" );
  998.             exitBuffers = 1;
  999.          }
  1000.          if (write_buffer_key() < 0 )
  1001.          {
  1002.             exitBuffers = 1;
  1003.          }
  1004.       }
  1005.       next_buffer()
  1006.    } while (old_curnt_buffer != current_buffer)
  1007.  
  1008.    current_buffer = buf;
  1009.  
  1010.    message( "" )
  1011.    return !exitBuffers;
  1012. }
  1013.  
  1014.  
  1015. ## alter current_buffer's tab settings
  1016. #
  1017. global function tabs( stops1, stops2 )                  #PUBLIC #VOID
  1018. {
  1019.    if( !stops1 )
  1020.       stops1 = prompt( "Enter tab stop settings: ", buffer_tabs, "tabs_dialog" )
  1021.    else if (argcount() > 1)
  1022.       stops1 = stops1 " " stops2;
  1023.  
  1024.    if( stops1 )
  1025.    {
  1026.       buffer_tabs = stops1
  1027.       message( "" )
  1028.    }
  1029. }
  1030.  
  1031.  
  1032. ## delete_buffer_key()
  1033. #
  1034. # A replacement for the delete_buffer() function to be bound to keys.
  1035. # It protects the user from accidently having a system buffer (such as the
  1036. # DOS buffer or help buffer) become current.
  1037. #
  1038. # The bufid argument is an optional buffer id of the buffer to delete.
  1039. # If not specified, the current buffer will be deleted.
  1040. #
  1041. # The nextFlag argument is a flag which should be set to 1 (TRUE) to have the
  1042. # deleted buffer be replaced with next_buffer() rather than prev_buffer().
  1043. # The default is prev_buffer().
  1044. #
  1045. # The return value is FALSE if the specified buffer is the only buffer or if
  1046. # they answer no to the "really delete?" prompt.
  1047. #
  1048. global function delete_buffer_key( bufid, nextFlag )
  1049. {
  1050.    local   priorWindow = current_window
  1051.    local   priorBuffer = current_buffer
  1052.    local   replacementBuffer
  1053.    local   sentinel
  1054.    local   resp
  1055.    local   eid
  1056.  
  1057.    if(!create_new_bufwin)
  1058.    {
  1059.       # make the specified buffer current
  1060.       if ( bufid )
  1061.          current_buffer = bufid
  1062.       else
  1063.          bufid = current_buffer
  1064.    
  1065.       # find the buffer to use to replace the deleted buffer.  Stop
  1066.       # now if the buffer to delete is the last regular buffer.
  1067.    
  1068.       if ( nextFlag )
  1069.          next_buffer()
  1070.       else
  1071.          prev_buffer()
  1072.    
  1073.    #   if ( current_buffer == bufid )
  1074.    #   {
  1075.    #      current_buffer = priorBuffer
  1076.    #      warning( "Cannot delete last buffer" )
  1077.    #      return FALSE
  1078.    #   }
  1079.    #   else
  1080.    #   {
  1081.          replacementBuffer = current_buffer
  1082.          current_buffer = bufid
  1083.    #   }
  1084.    
  1085.       # ask for confirmation if the buffer has been modified
  1086.       if ( and( buffer_flags, BUFFER_MODIFIED ))
  1087.       {
  1088.          current_buffer = priorBuffer
  1089.          resp = toupper( confirm(
  1090.                          buffer_filename " has been modified. Delete [ynw]? ",
  1091.                          "YyNnWw" ))
  1092.    
  1093.          if ( resp == "Y" )
  1094.          {
  1095.             # yes, turn off the modified bit and proceed
  1096.             current_buffer = bufid
  1097.             buffer_flags = xor( buffer_flags, BUFFER_MODIFIED )
  1098.          }
  1099.          else if ( resp == "W" )
  1100.          {
  1101.             # write first, clearing the modified bit as we do
  1102.             current_buffer = bufid
  1103.             write_buffer()
  1104.          }
  1105.          else
  1106.          {
  1107.             return FALSE
  1108.          }
  1109.       }
  1110.    
  1111.       # fix all non_system windows attached to this buffer
  1112.    
  1113.       if ( and( window_flags, WINDOW_SYSTEM ) )
  1114.          sentinel = next_window()
  1115.    
  1116.       else
  1117.          sentinel = current_window
  1118.    
  1119.       do
  1120.       {
  1121.          if ( bufid == current_buffer )
  1122.             attach_window_buffer( current_window, replacementBuffer )
  1123.    
  1124.          next_window("", 0, 1)
  1125.       } while ( current_window != sentinel )
  1126.    
  1127.       # restore the original window
  1128.       if ( priorWindow )
  1129.          current_window = priorWindow
  1130.    
  1131.       # delete the buffer, now that no windows (other than the current
  1132.       # one) are attached
  1133.    
  1134.       current_buffer = bufid
  1135.       eid = "unlock_file"             # avoids compiler warning
  1136.    
  1137.       if (function_id( eid ))
  1138.          execute_function( "unlock_file " buffer_filename );
  1139.    
  1140.       delete_buffer()
  1141.       if (bufid == current_buffer)
  1142.       {
  1143.          # failed to delete buffer
  1144.          current_buffer = priorBuffer
  1145.          return FALSE
  1146.       }
  1147.    
  1148.       # restore the original buffer if it was not the one deleted
  1149.       if ( priorBuffer != bufid )
  1150.          current_buffer = priorBuffer
  1151.    
  1152.       else
  1153.       {
  1154.          # otherwise make the new current buffer the appropriate
  1155.          # non-system buffer; and display the new filename
  1156.          current_buffer = replacementBuffer
  1157.          display_filename()
  1158.       }
  1159.    }
  1160.    else
  1161.    {
  1162.       eid = "unlock_file"             # avoids compiler warning
  1163.    
  1164.       if (function_id( eid ))
  1165.          execute_function( "unlock_file " buffer_filename );
  1166.    
  1167.       delete_buffer();
  1168.    }
  1169.  
  1170.    return TRUE
  1171. }
  1172.  
  1173.  
  1174. #global function toggle_real_space( on )
  1175. #{
  1176. #   if( argcount() < 1 )
  1177. #      on = !and( buffer_flags, BUFFER_REAL_SPACE_ONLY );
  1178. #   else
  1179. #      on = 0+on
  1180. #
  1181. #   #
  1182. #   # toggle the setting
  1183. #   #
  1184. #   if (on)
  1185. #      buffer_flags = or( buffer_flags, BUFFER_REAL_SPACE_ONLY )
  1186. #   else
  1187. #      buffer_flags = and( buffer_flags, not(BUFFER_REAL_SPACE_ONLY) )
  1188. #
  1189. #
  1190. #   message( and( buffer_flags, BUFFER_REAL_SPACE_ONLY )    \
  1191. #            ? "Real space only."            \
  1192. #            : "Virtual space allowed." )
  1193. #   return on;
  1194. #}
  1195.  
  1196. ## toggle insert mode
  1197. #
  1198. #global function toggle_insert_mode( on )
  1199. #{
  1200. #   if( argcount() < 1 )
  1201. #      on = and( buffer_flags, BUFFER_OVERTYPE_MODE )
  1202. #   else
  1203. #      on = 0+on
  1204. #
  1205. #   if ( on )
  1206. #   {
  1207. #      buffer_flags = and(buffer_flags, not(BUFFER_OVERTYPE_MODE ))
  1208. #      message( "Insert Mode" )
  1209. #   }
  1210. #   else
  1211. #   {
  1212. #      buffer_flags = or(buffer_flags, BUFFER_OVERTYPE_MODE )
  1213. #      message( "Overtype Mode" )
  1214. #   }
  1215. #}
  1216.  
  1217. ## toggle tabs to spaces
  1218. #
  1219. #global function toggle_tabs_to_spaces( on )
  1220. #{
  1221. #   if( argcount() < 1 )
  1222. #      on = !and( buffer_flags, BUFFER_TABS_TO_SPACES )
  1223. #   else
  1224. #      on = 0+on
  1225. #
  1226. #   toggle_buffer_flags( BUFFER_TABS_TO_SPACES, on )
  1227. #   message( "Tabs to spaces " ( on ? "enabled." : "disabled." ))
  1228. #}
  1229.  
  1230. #global function toggle_buffer_expand_tabs( on )
  1231. #{
  1232. #   if( argcount() < 1 )
  1233. #      on = !and( buffer_flags, BUFFER_EXPAND_TABS );
  1234. #   else
  1235. #      on = 0+on
  1236. #
  1237. #   #
  1238. #   # toggle the setting
  1239. #   #
  1240. #   if (on)
  1241. #      buffer_flags = or( buffer_flags, BUFFER_EXPAND_TABS )
  1242. #   else
  1243. #      buffer_flags = and( buffer_flags, not(BUFFER_EXPAND_TABS) )
  1244. #
  1245. #
  1246. #   message( "Buffer expand tabs %s." , on ? "on" : "off");
  1247. #   return on;
  1248. #}
  1249.  
  1250. #global function toggle_buffer_snap2eol( on )
  1251. #{
  1252. #   if( argcount() < 1 )
  1253. #      on = !and( buffer_flags, BUFFER_SNAP_TO_EOL );
  1254. #   else
  1255. #      on = 0+on
  1256. #
  1257. #   #
  1258. #   # toggle the setting
  1259. #   #
  1260. #   if (on)
  1261. #      buffer_flags = or( buffer_flags, BUFFER_SNAP_TO_EOL )
  1262. #   else
  1263. #      buffer_flags = and( buffer_flags, not(BUFFER_SNAP_TO_EOL) )
  1264. #
  1265. #
  1266. #   message( "Buffer snap to end of line %s." , on ? "on" : "off");
  1267. #   return on;
  1268. #}
  1269.  
  1270. global function write_buffer_key()
  1271. {
  1272.    local status
  1273.  
  1274.       if (buffer_filename)
  1275.       {
  1276.          if ( !and( buffer_flags, BUFFER_MODIFIED ) )
  1277.             message( buffer_name ": has not been modified. " );
  1278.          else if ( is_scratch_file(current_buffer)  )
  1279.             status = write_buffer_as()
  1280.          else
  1281.          {
  1282.             
  1283.             message( buffer_name ":" );
  1284.             backup_file( buffer_filename );
  1285.             status = write_buffer();
  1286.             if ((status >= 0) && (!errno))
  1287.                message( buffer_name ":" buffer_last_line " lines written" );
  1288.  
  1289.             ## The system will display the unable to write message
  1290.             ##
  1291.             #else
  1292.             #  message( "Unable to write " buffer_name );
  1293.          }
  1294.       }
  1295.       else
  1296.          warning( "buffer_filename is not defined. " );
  1297.    return status
  1298. }
  1299.  
  1300. function write_block_or_buffer_key()
  1301. {
  1302.    local   fileToWrite
  1303.    local   status = -1
  1304.  
  1305.    if( selection_type() == NO_SELECTION )
  1306.       write_buffer_key()
  1307.    else
  1308.       gui_write_block()
  1309.  
  1310.    return status;
  1311. }
  1312.  
  1313. global function write_buffer_as( fileToWrite )
  1314. {
  1315.    local flags
  1316.    local status
  1317.    local old_filename = ""
  1318.  
  1319.    if ( !fileToWrite )
  1320.       fileToWrite = prompt( "Write File as: ", "", "write_buffer_as_dialog" )
  1321.  
  1322.    if (fileToWrite)
  1323.    {
  1324.       backup_file( fileToWrite );
  1325.  
  1326.       # temporarily change filename so write_buffer will update
  1327.       # buffer_original_filename if write is successful
  1328.       old_filename = buffer_filename
  1329.       buffer_filename = fileToWrite
  1330.  
  1331.       status = write_buffer()
  1332.  
  1333.       if ( status != -1 )
  1334.          {
  1335.          buffer_name = path_fname(buffer_filename) path_ext(buffer_filename)
  1336.          flags = buffer_flags
  1337.          flags = and(flags, not(BUFFER_SCRATCH) )
  1338.          buffer_flags = flags
  1339.          buffer_flags = and(buffer_flags, not(BUFFER_MODIFIED) )
  1340.          update_current_caption()
  1341.    
  1342.          message( "Write successful." )
  1343.          }
  1344.       else
  1345.          buffer_filename = old_filename   # restore old filename
  1346.  
  1347.       update_current_view()
  1348.       
  1349.       new_edit_file()
  1350.       syntax_new_buffer()
  1351.    }
  1352.  
  1353.    return status;
  1354. }
  1355.  
  1356. global function next_buffer_key()
  1357. {
  1358.    next_buffer()
  1359.    display_filename()
  1360. }
  1361.  
  1362. global function prev_buffer_key()
  1363. {
  1364.    prev_buffer()
  1365.    display_filename() 
  1366. }
  1367.  
  1368. global function next_buffer_mask()
  1369. {
  1370.    if(create_new_bufwin)
  1371.       next_window();
  1372.    else
  1373.       next_buffer(window_name)
  1374.  
  1375.    display_filename()
  1376. }
  1377.  
  1378. global function prev_buffer_mask()
  1379. {
  1380.    if(create_new_bufwin)
  1381.       prev_window();
  1382.    else
  1383.       prev_buffer(window_name)
  1384.  
  1385.    display_filename()
  1386. }
  1387.  
  1388. global function set_buffer_mask(mask)
  1389. {
  1390.    # if all files are included then reset buffer mask so title
  1391.    # bar doesn't display buffer mask 
  1392.    if ( mask ==  "*.*" || mask == "*" || mask == "*." )
  1393.       reset_buffer_mask()
  1394.    else
  1395.       window_name = mask;
  1396.  
  1397.    # now go to the next buffer that matches the mask
  1398.    next_buffer( mask )
  1399.  
  1400.    # get the title to update
  1401.    display_redraw()
  1402. }
  1403.  
  1404. global function reset_buffer_mask()
  1405. {
  1406.    window_name = ""
  1407.  
  1408.    # get the title to update
  1409.    display_redraw()
  1410. }
  1411.  
  1412. global function set_buffer_mask_key()
  1413. {
  1414.    local fn
  1415.  
  1416.    if ( !fn )
  1417.       fn = prompt_history( "BUFFERMASK", "Buffer filter: ", "", 1, 1, "buffer_filter_prompt"  )
  1418.  
  1419.    if( fn )
  1420.       set_buffer_mask(fn)
  1421. }
  1422.  
  1423. global function is_scratch_file( bufferId )
  1424. {
  1425.    local retval         = FALSE
  1426.    local old_cur_buffer = current_buffer
  1427.  
  1428.    current_buffer = bufferId   
  1429.  
  1430.    retval = and(buffer_flags, BUFFER_SCRATCH)
  1431.  
  1432.    current_buffer = old_cur_buffer
  1433.    return retval
  1434. }
  1435.  
  1436. #function toggle_read_only( on )
  1437. #{
  1438. #   local resp, bmode, fmode, modeMsg, old_sb, fn, fattr
  1439. #
  1440. #   if( argcount() < 1 )
  1441. #      on = !and( buffer_flags, BUFFER_READ_ONLY );
  1442. #   else
  1443. #      on = 0+on
  1444. #
  1445. #   #
  1446. #   # toggle the setting
  1447. #   #
  1448. #   if (on)
  1449. #      buffer_flags = or( buffer_flags, BUFFER_READ_ONLY )
  1450. #   else
  1451. #      buffer_flags = and( buffer_flags, not(BUFFER_READ_ONLY) )
  1452. #
  1453. #
  1454. #   message( on ? "Buffer is read-only." : "Buffer is not read-only." )
  1455. #
  1456. #   # update the disk file's read-only bit
  1457. #   fn    = buffer_filename
  1458. #   fattr = filemode( fn )
  1459. #   
  1460. #   if ( fattr >= 0 )
  1461. #   {
  1462. #      # would the disk read-only mode match the buffer's?
  1463. #      fmode = !!and( fattr, _READ_ONLY )
  1464. #   
  1465. #      bmode = !!and( buffer_flags, BUFFER_READ_ONLY )
  1466. #   
  1467. #      # if so, make the change to the disk file
  1468. #      if ( fmode != bmode )
  1469. #      {
  1470. #         modeMsg = bmode ? "read-only" : "writable"
  1471. #   
  1472. #         optional_function( "disable_status_bar_prompts" )
  1473. #
  1474. #         resp = toupper( confirm( \
  1475. #               "make file " fn " " modeMsg   \
  1476. #               " also? [yn]", "yYnN" ))
  1477. #   
  1478. #         optional_function( "restore_status_bar" )
  1479. #   
  1480. #         if ( resp == "Y" ) {
  1481. #            message( "making file " fn " " modeMsg )
  1482. #            fattr = xor( fattr, _READ_ONLY )
  1483. #            filemode( fn, fattr )
  1484. #         } else
  1485. #            # replace misleading "canceled" message
  1486. #            message( "" )
  1487. #      }
  1488. #   }
  1489. #   return on;
  1490. #}
  1491.  
  1492. global function create_buf_and_win_key(fn)
  1493. {
  1494.    local rc
  1495.  
  1496.    if ( argcount() < 1 || fn == "" || fn == 0 )
  1497.       fn = prompt_history( "EDITFILE", "File: ", "", 1, 1, "create_buf_and_win_prompt" )
  1498.  
  1499.    rc = create_buf_and_win(fn)
  1500.  
  1501.    if ( message_level == 0 )
  1502.       if ( rc )
  1503.          message( buffer_filename )
  1504.       else if ( fn )
  1505.          warning("Unable to edit " fn)
  1506.  
  1507. }
  1508.  
  1509. global function toggle_create_new_win(on)
  1510. {
  1511.    local first = current_window;
  1512.    local old_create_new_bufwin = create_new_bufwin;
  1513.    local id;
  1514.  
  1515.    if( argcount() < 1 )
  1516.       on = !create_new_bufwin
  1517.  
  1518.    if(!mdi_mode && on)
  1519.    {
  1520.       warning("One buffer per window mode is not valid in SDI mode");
  1521.       return;
  1522.    }
  1523.  
  1524.  
  1525.    #  If the user turns this feature on, we need to set up the windows
  1526.    #  as if he/she opened all their buffers in this mode;
  1527.    #  temp_create_new_bufwin == 2 if files specified on the command line at startup;
  1528.    if ( on && !old_create_new_bufwin )
  1529.    {
  1530.       cascade_buffers();
  1531.       #  assigning current_window to first sometimes doesn't work if cascade_buffers() deletes windows;
  1532. #        current_window = first;
  1533.    }
  1534.  
  1535.    create_new_bufwin = on;
  1536.  
  1537.    if (and(status_bar_flags, STB_MESSAGES))
  1538.       message("One buffer per window is %s", create_new_bufwin ? "on" : "off")
  1539.  
  1540.    return on;
  1541. }
  1542.  
  1543. function read_line( line )
  1544. {
  1545.    local text
  1546.    line = ( line ) ? line : current_line
  1547.  
  1548.    save_position();
  1549.    goto_line(line);
  1550.    text = read_buffer();
  1551.    restore_position(TRUE);
  1552.  
  1553.    return text
  1554. }
  1555.  
  1556. #
  1557. # This file contains functions to keep the editor's current directory
  1558. # synchronized with the current buffer.  To use, compile this file and
  1559. # execute the function toggle_dir_sync() with a 0 to disable, a 1 to enable
  1560. # or no argument to toggle the state.
  1561. #
  1562.  
  1563. function toggle_dir_sync(on)
  1564. {
  1565.    local handler_id = function_id("buffer_chdir")
  1566.    local dir_sync_active = handler_active(EVENT.NEW_CURNT_BUFFER, handler_id)
  1567.  
  1568.    if( argcount() < 1 )
  1569.    {
  1570.       on = !dir_sync_active
  1571.    }
  1572.  
  1573.    if (on)
  1574.    {
  1575.       if (!dir_sync_active)
  1576.       {
  1577.          attach_event_handler(EVENT.NEW_CURNT_BUFFER, handler_id)
  1578.          if (!editor_running)
  1579.             attach_event_handler(EVENT.EDITOR_RUNNING, handler_id)
  1580.          buffer_chdir()
  1581.       }
  1582.       notify("New buffer handler activated")
  1583.    }
  1584.    else
  1585.    {
  1586.       delete_event(EVENT.NEW_CURNT_BUFFER, handler_id)
  1587.       delete_event(EVENT.EDITOR_RUNNING, handler_id)
  1588.       notify("New buffer handler removed")
  1589.    }
  1590.  
  1591. }
  1592.  
  1593. global function handler_active(event, handler)
  1594. {
  1595.    local event_list = query_event( event )
  1596.    local i
  1597.    
  1598.    for (i in event_list)
  1599.       if (event_list[i] == handler)
  1600.          return TRUE
  1601.  
  1602.    return FALSE
  1603. }
  1604.  
  1605. function buffer_chdir()
  1606. {
  1607.    local path = path_path(buffer_filename)
  1608.    
  1609.    if (and(buffer_flags, BUFFER_SYSTEM))
  1610.       return
  1611.       
  1612.    if (editor_running && path)
  1613.       chdir(path)
  1614. }
  1615.  
  1616. #function remove_old()                           
  1617. #{
  1618. #   local handler_id = function_id("delete_buffer")
  1619. #   local event = EVENT.DELETING_WINDOW
  1620. #   delete_event(event, handler_id)
  1621. #}
  1622.  
  1623. #function query_bind_buff_to_win()
  1624. #{
  1625. #   return handler_active(EVENT.DELETING_WINDOW, function_id("delete_buffer_in_win"))
  1626. #}
  1627.  
  1628. global function toggle_buffer_flags(mask, value)
  1629. {
  1630.    local fn, fattr, fmode, bmode, modeMsg, resp;
  1631.    local msg, suffix = "disabled.";
  1632.    
  1633.    #  If no value supplied, toggle the old value;
  1634.    if(argcount() < 2)
  1635.       value = xor(mask, and(buffer_flags, mask));
  1636.  
  1637.    if ( value )
  1638.    {
  1639.       buffer_flags = or( buffer_flags, mask )
  1640.       suffix = "enabled.";
  1641.    }
  1642.    else
  1643.       buffer_flags = and( buffer_flags, not(mask) )
  1644.  
  1645.    if(mask == BUFFER_EXPAND_TABS)
  1646.       msg = "Buffer expand tabs " suffix;
  1647.    else if(mask == BUFFER_MODIFIED)
  1648.       msg = "Buffer modified " suffix;
  1649.    else if(mask == BUFFER_OVERTYPE_MODE)
  1650.       msg = value ? "Buffer overtype mode enabled." : "Buffer insert mode enabled";
  1651.    else if(mask == BUFFER_REAL_SPACE_ONLY)
  1652.       msg = "Buffer real space only " suffix;
  1653.    else if(mask == BUFFER_SNAP_TO_EOL)
  1654.       msg = "Buffer snap to end of line " suffix;
  1655.    else if(mask == BUFFER_TABS_TO_SPACES)
  1656.       msg = "Buffer tabs to spaces " suffix;
  1657.    else if(mask == BUFFER_WP_ENABLED)
  1658.       msg = "Buffer word processing " suffix;
  1659.    else if(mask == BUFFER_READ_ONLY)
  1660.    {
  1661.       #  Special processing for read only toggle;
  1662.       # update the disk file's read-only bit
  1663.       fn    = buffer_filename
  1664.       fattr = filemode( fn )
  1665.    
  1666.       if ( fattr >= 0 )
  1667.       {
  1668.          # would the disk read-only mode match the buffer's?
  1669.          fmode = !!and( fattr, _READ_ONLY )
  1670.    
  1671.          bmode = !!and( buffer_flags, BUFFER_READ_ONLY )
  1672.    
  1673.          # if so, make the change to the disk file
  1674.          if ( fmode != bmode )
  1675.          {
  1676.             modeMsg = bmode ? "read-only" : "writable"
  1677.    
  1678.             optional_function( "disable_status_bar_prompts" )
  1679.  
  1680.             resp = toupper( confirm( \
  1681.                   "make file " fn " " modeMsg   \
  1682.                   " also? [yn]", "yYnN" ))
  1683.    
  1684.             optional_function( "restore_status_bar" )
  1685.    
  1686.             if ( resp == "Y" )
  1687.             {
  1688.                message( "making file " fn " " modeMsg )
  1689.                fattr = xor( fattr, _READ_ONLY )
  1690.                filemode( fn, fattr )
  1691.             }
  1692.             else
  1693.                message( "" )
  1694.          }
  1695.       }
  1696.    }
  1697.    else
  1698.       msg = "Buffer mode " suffix;
  1699.  
  1700.    message(msg);
  1701.  
  1702.    return value;
  1703. }
  1704.