home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / pr2grp.zip / group.pel < prev    next >
Text File  |  1995-08-06  |  13KB  |  381 lines

  1. ##############################################################################
  2. ##
  3. ##         2Rud Software
  4. ##
  5. ##     This source code listing contains information that is
  6. ##     has been written by 2Rud Software and may be copied
  7. ##     duplicated, translated, transmitted, stored, retrieved
  8. ##     or in any manner or by any method conveyed or disclosed
  9. ##     to a third party or parties without express written
  10. ##     permission from 2Rud Software
  11. ##
  12. #      Usage:  
  13. #            1) Add this file to the wpel.rsp
  14. #            2) Run the PEL compiler
  15. #            3) *I* suggest put putting a key-binding on ALT-G to open_group
  16. #               and another on ALT-A for add_file_to_group
  17. #            4) Add Groups and add files to groups
  18. #
  19. ##############################################################################
  20.  
  21. # dialog IDs
  22. local GroupDlg            = 0
  23. local IDD_GROUP_DLG       = 100
  24. local IDLB_GROUP          = 101
  25. local IDLB_FILENAME       = 102
  26. local IDPB_ADD            = 103
  27. local IDPB_DELETE         = 104
  28. local IDRB_GROUP          = 105
  29. local IDRB_FILE           = 106
  30. local IDPB_SET            = 107
  31. local IDST_CURRENT        = 108
  32.   
  33. #variables
  34. local user_dll       = "USER"
  35. local GroupBufferID;
  36. local current_group="";
  37. local selected_group="";
  38. local File_Checked=FALSE;
  39. local Group_Checked=TRUE;
  40. local filename_array[]
  41. local num_files_in_group
  42. local lastPath="*.*"
  43.    
  44. # create a buffer for the group information
  45. function init_group()
  46. {
  47.    GroupBufferID=create_buffer("group_buffer","group.dat",BUFFER_SYSTEM+BUFFER_NO_UNDO+BUFFER_REAL_SPACE_ONLY);
  48. }
  49.    
  50. # shows the Group dialog box
  51. function open_group()
  52. {
  53.  
  54.    local orig_buffer = current_buffer;
  55.  
  56.    if (GroupBufferID==0)
  57.    {
  58.       init_group();
  59.    }
  60.    current_buffer = GroupBufferID;
  61.    GroupDlg = create_dialog(function_id("group_dlg_callback"), 0,
  62.                         IDD_GROUP_DLG, user_dll );
  63.  
  64.    begin_dialog(GroupDlg);
  65.    delete_dialog(GroupDlg);
  66.    current_buffer = orig_buffer;
  67. }
  68.  
  69. # callback function for the Group dialog box
  70. # handles all the events that are generated by it
  71. function group_dlg_callback()
  72. {
  73.    local new_group
  74.    local group
  75.    local filename
  76.    local old_winid
  77.    local next_winid
  78.    local winid_array[]
  79.    local i
  80.    local resp
  81.    local id
  82.    local search_str
  83.    local selected_filename
  84.    local listOfFiles
  85.    local arrayOfFiles
  86.    local fileToAdd
  87.    # initialization
  88.    if ( callback_msg == DM_INIT )
  89.    {
  90.       # check the group radio button
  91.       set_dialog_item(GroupDlg, IDRB_GROUP, DAC_CHECK);
  92.       # get all the group names and put them in the Group listbox
  93.       goto_buffer_top();
  94.       while (search("$$$",SEARCH_FORWARD+SEARCH_ADVANCE))
  95.       {
  96.          group = ltrim( trim(read_buffer()),"$$$" )
  97.          set_dialog_item(GroupDlg, IDLB_GROUP, DAC_ADD_ITEM, group);
  98.          Group_Checked=TRUE;
  99.       }
  100.       # select the first item in the group
  101.       set_dialog_item(GroupDlg, IDLB_GROUP, DAC_SELECT_INDEX,0);
  102.       # set the current group static text
  103.       set_dialog_item(GroupDlg, IDST_CURRENT, DAC_TEXT,current_group);
  104.       
  105.       return DRC_MSG_PROCESSED
  106.    }
  107.    # if OPEN pushbutton pressed
  108.    if ( callback_msg == DM_OK )
  109.    {
  110.       current_group=selected_group
  111.       # make sure any changes are reflected
  112.       write_buffer()
  113.       #get an array of window ids for the current opened buffers
  114.       old_winid=current_window
  115.       i=1
  116.       winid_array[i]=current_window
  117.       next_winid=next_window("",1);
  118.       i++
  119.       while (old_winid != next_winid)
  120.       {
  121.          winid_array[i]=next_winid
  122.          next_winid=next_window("",1);
  123.          i++
  124.       }
  125.       #delete the current window/buffers
  126.       for (id in winid_array)
  127.       {
  128.          assign_current_window(winid_array[id])
  129.          delete_window_and_buffer()
  130.       }
  131.       # now open the files in the selected group
  132.       for (i in filename_array)
  133.       {
  134.          add_prompt_history("EDITFILE", filename_array[i])
  135.          create_buf_and_win(filename_array[i])
  136.       }
  137.       return DRC_EXIT
  138.    }
  139.    # a group in the listbox is selected
  140.    if ( callback_msg == DM_SELECT && callback_index == IDLB_GROUP )
  141.    {
  142.       goto_buffer_top();
  143.       # clear out file listbox
  144.       set_dialog_item(GroupDlg, IDLB_FILENAME, DAC_DELETE_INDEX);
  145.       # clear out filename array 
  146.       for (i in filename_array)
  147.       {
  148.          filename_array[i]=""
  149.       }
  150.       # search for the selected group in the Group buffer
  151.       selected_group=query_dialog_item(GroupDlg, IDLB_GROUP, DAC_SELECT_ITEM);
  152.       num_files_in_group=0
  153.       search_str="\\$\\$\\$<"selected_group">";
  154.       search(search_str,SEARCH_FWD_REGEX_MAX);
  155.       # for all the files in the group put them in the filename array
  156.       # and in the filename listbox
  157.       if(down(1))
  158.       {
  159.          num_files_in_group=1
  160.          filename= ltrim( trim(read_buffer()) )
  161.          while (substr(filename,1,3) != "$$$")
  162.          {
  163.             # make sure there is a filename there
  164.             if (filename!="" && (length(filename) > 1) )
  165.             {
  166.                # add the filename to the filename array so we don't
  167.                # have to scan the buffer if OPEN is pressed
  168.                filename_array[num_files_in_group]=filename
  169.                num_files_in_group++
  170.                set_dialog_item(GroupDlg, IDLB_FILENAME, DAC_ADD_ITEM, filename);
  171.             }
  172.             if ( !down(1) )
  173.                break
  174.  
  175.             filename = ltrim( trim(read_buffer()) )
  176.          }
  177.       }
  178.       return DRC_MSG_PROCESSED
  179.    }
  180.    # DELETE pushbutton pressed
  181.    if ( callback_msg == DM_CLICK && callback_index == IDPB_DELETE )
  182.    {
  183.       
  184.       # if they want to delete a file
  185.       if (File_Checked==TRUE)
  186.       {
  187.          # find the group
  188.          goto_buffer_top()
  189.          selected_group=query_dialog_item(GroupDlg, IDLB_GROUP, DAC_SELECT_ITEM);
  190.          selected_filename=query_dialog_item(GroupDlg, IDLB_FILENAME, DAC_SELECT_ITEM);
  191.          search_str="\\$\\$\\$<"selected_group">";
  192.          set_status_bar_flags( and(status_bar_flags, not(STB_PROMPTS)));
  193.          # make sure they really want to do this
  194.          resp = toupper( confirm(
  195.                        "Are you sure you want to delete " selected_filename"? [yn]? ",
  196.                        "YyNn" ))
  197.          restore_status_bar();
  198.          if ( resp == "Y" )
  199.          {
  200.             # make sure the group exists
  201.             if(!search(search_str,SEARCH_FWD_REGEX_MAX))
  202.             {
  203.                error("Group not found!")
  204.             }
  205.             else
  206.             {
  207.                # find the filename
  208.                search_str="<"selected_filename">";
  209.                # do magical regex things with the backslash!  
  210.                gsub("\\\\", "\\\\", search_str)
  211.                if(search(search_str,SEARCH_FWD_REGEX_MAX))
  212.                {
  213.                   # delete the line containing the filename (buffer and listbox)
  214.                   delete_line()
  215.                   set_dialog_item(GroupDlg, IDLB_FILENAME, DAC_DELETE_ITEM,selected_filename);
  216.                }
  217.                else
  218.                {
  219.                   error("Filename not found!")
  220.                }
  221.             }
  222.          }
  223.       }
  224.       # Group is  checked; delete the group and associated filenames
  225.       else
  226.       {
  227.          selected_group=query_dialog_item(GroupDlg, IDLB_GROUP, DAC_SELECT_ITEM);
  228.          set_status_bar_flags( and(status_bar_flags, not(STB_PROMPTS)));
  229.          # make sure they really want to do this
  230.          resp = toupper( confirm(
  231.                        "Are you sure you want to delete the Group - " selected_group"? [yn]? ",
  232.                        "YyNn" ))
  233.          restore_status_bar();
  234.          if ( resp == "Y" )
  235.          {
  236.             set_dialog_item(GroupDlg, IDLB_GROUP, DAC_DELETE_ITEM,selected_group);
  237.             goto_buffer_top()
  238.             search_str="\\$\\$\\$<"selected_group">";
  239.             # find the group in the group buffer
  240.             if(search(search_str,SEARCH_FWD_REGEX_MAX))
  241.             {
  242.                # group is found - delete the line
  243.                delete_line()
  244.                # now delete all the filenames in the group
  245.                filename= ltrim( trim(read_buffer()) )
  246.                # stop when you get to the next group of a blank line (or eof)
  247.                while (substr(filename,1,3) != "$$$" && (length(filename)) > 1)
  248.                {
  249.                   # make sure there is a filename there
  250.                   if (filename!="" && (length(filename) > 1) )
  251.                   {
  252.                      set_dialog_item(GroupDlg, IDLB_FILENAME, DAC_DELETE_ITEM,filename);
  253.                      delete_line()
  254.                   }
  255.                   #now read the next line
  256.                   filename = ltrim( trim(read_buffer()) )
  257.                }
  258.             }
  259.             else
  260.             {
  261.                error("Group not found!")
  262.             }
  263.          }
  264.       }
  265.  
  266.       return DRC_MSG_PROCESSED
  267.    }
  268.    # if SET pushbutton pressed
  269.    if ( callback_msg == DM_CLICK && callback_index == IDPB_SET )
  270.    {
  271.       current_group=selected_group
  272.       write_buffer()
  273.       return DRC_EXIT
  274.    }
  275.    # if CANCEL pushbutton pressed
  276.    if ( callback_msg == DM_CANCEL )
  277.    {
  278.       delete_buffer();
  279.       GroupBufferID=0;
  280.       return DRC_EXIT
  281.    }
  282.    # if they select the FILE radio button
  283.    if ( callback_msg == DM_CLICK && callback_index == IDRB_FILE )
  284.    {
  285.       File_Checked=TRUE;
  286.       Group_Checked=FALSE;
  287.       return DRC_MSG_PROCESSED
  288.    }
  289.    # selected the GROUP radio button
  290.    if ( callback_msg == DM_CLICK && callback_index == IDRB_GROUP )
  291.    {
  292.       File_Checked=FALSE;
  293.       Group_Checked=TRUE;
  294.       return DRC_MSG_PROCESSED
  295.    }
  296.    # ADD pushbutton pressed with GROUP radio button selected
  297.    if ( callback_msg == DM_CLICK && callback_index == IDPB_ADD && Group_Checked==TRUE )
  298.    {
  299.       # get the new group name from the user
  300.       set_status_bar_flags( and(status_bar_flags, not(STB_PROMPTS)));
  301.       new_group = prompt_history("GROUP", "Enter new Group :");
  302.       restore_status_bar();
  303.       # add the new group name to the listbox and buffer
  304.       set_dialog_item(GroupDlg, IDLB_GROUP, DAC_ADD_ITEM, new_group);
  305.       goto_buffer_bottom();
  306.       goto_eol();
  307.       insert_string("\n%s%s\n","$$$",new_group)
  308.       return DRC_MSG_PROCESSED
  309.    }
  310.    # ADD pushbutton pressed with FILENAME radio button selected
  311.    if ( callback_msg == DM_CLICK && callback_index == IDPB_ADD && File_Checked==TRUE )
  312.    {
  313.       # show the open dialog
  314.       listOfFiles = open_dialog( lastPath, "Add a file to a Group", 0, "" )
  315.  
  316.       if ( listOfFiles )
  317.       {
  318.          # parse out the files and flags.  Flags are always the first
  319.          # element in the list.
  320.          split(listOfFiles, arrayOfFiles, ";")
  321.          for(i in arrayOfFiles)
  322.          {
  323.    
  324.             if ( i != 1 )
  325.             {
  326.                fileToAdd = arrayOfFiles[i]
  327.                # add it to the buffer 
  328.                goto_buffer_top();
  329.                search("\\$\\$\\$<"selected_group">",SEARCH_FWD_REGEX_MAX);
  330.                goto_eol()
  331.                insert_string("\n%s",fileToAdd);
  332.                # add it to the listbox
  333.                set_dialog_item(GroupDlg, IDLB_FILENAME, DAC_ADD_ITEM, fileToAdd);
  334.                filename_array[num_files_in_group]=fileToAdd
  335.                num_files_in_group++
  336.             }
  337.          }
  338.             lastPath = path_path(fileToAdd) "*.*"
  339.       }
  340.       return DRC_MSG_PROCESSED
  341.    }
  342. return DRC_MSG_PROCESSED
  343.  
  344. }
  345.  
  346. # adds the current buffer filename to the current group
  347. function add_file_to_group()
  348. {
  349.    local orig_buffer = current_buffer;
  350.    local orig_buffer_name =buffer_filename;
  351.    local resp;
  352.  
  353.    if (GroupBufferID==0)
  354.    {                                                                        
  355.       init_group();
  356.    }
  357.    if (current_group =="")
  358.    {
  359.       error("Current group not defined!")
  360.    }
  361.    else
  362.    {
  363.       current_buffer = GroupBufferID;
  364.       set_status_bar_flags( and(status_bar_flags, not(STB_PROMPTS)));
  365.       resp = toupper( confirm(
  366.                        "Add " orig_buffer_name " to group " current_group " [yn]? ",
  367.                        "YyNn" ))
  368.       restore_status_bar();
  369.  
  370.       if ( resp == "Y" )
  371.       {
  372.          goto_buffer_top();
  373.          search("\\$\\$\\$<"current_group">",SEARCH_FWD_REGEX_MAX);
  374.          goto_eol()
  375.          insert_string("\n%s",orig_buffer_name);
  376.          write_buffer();
  377.       }
  378.       current_buffer = orig_buffer;
  379.    }
  380. }
  381.