home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ME22-OS2.ZIP / MACROS.ZIP / SELECTB.M < prev    next >
Text File  |  1988-09-09  |  3KB  |  124 lines

  1. /***********************************************************************\
  2. |*                                                                     *|
  3. |* SELECTB - a simple buffer selector patterned after DIRED            *|
  4. |*                                                                     *|
  5. |* You can invoke this from the editor by loading in the DIRED macro   *|
  6. |* and pressing <ALT> D. A directory listing will appear in another    *|
  7. |* window. Move the highlight to a file and press ENTER. The selected  *|
  8. |* bufr will replace the directory listing in the new window. You may  *|
  9. |* also delete the selected bufr by pressing 'D' or the DEL key.       *|
  10. |*                                                                     *|
  11. |* Feel free to add your own enhancements.                             *|
  12. |*                                                                     *|
  13. |* (C) Copyright 1987  Marc Adler   Magma Software Systems             *|
  14. |*     SELECTB was modified from DIRED by Blake McBride                *|
  15. \***********************************************************************/
  16.  
  17. #define ALT_D   160
  18. #define UPKEY   200
  19. #define DOWNKEY 208
  20. #define DEL     211
  21. #define ESC     27
  22.  
  23. init()
  24. {
  25.   assign_key("dired", ALT_D);
  26. }
  27.  
  28. dired()
  29. {
  30.   string path;
  31.   string dirtmpfile, fname;
  32.   int    dirbuf, orig_buf, cb, mod;
  33.   int    c;
  34.  
  35.   orig_buf = currbuf();
  36.   dirtmpfile = "buffer_list";
  37.   dirbuf = create_buffer(dirtmpfile);
  38.  
  39.   cb = next_buffer(dirbuf);
  40.   while (cb != dirbuf)
  41.   {
  42.     setcurrbuf(cb);
  43.     fname = filename();          /* extract the filename and modified flag */
  44.     mod = buffer_modified();
  45.     setcurrbuf(dirbuf);
  46.     if (mod)                     /* insert the entry into the listing buffer */
  47.       insert(sprintf("* %s\n", fname));
  48.     else
  49.       insert(sprintf("  %s\n", fname));
  50.     cb = next_buffer(cb);
  51.   }
  52.  
  53.   /* Display the directory listing in a full-screen window */
  54.   delline();
  55.   show_buffer(dirbuf);
  56.   gobof();
  57.   explode_window();
  58.   markline();   /* highlite the first entry */
  59.  
  60.   while ((c = get_tty_char()) != ESC && c != '\r')
  61.   {
  62.     if (c == UPKEY)
  63.       up_entry();
  64.     else if (c == DOWNKEY)
  65.       down_entry();
  66.     else if (c == 'd' || c == 'D' || c == DEL)
  67.       del_entry();
  68.   }
  69.  
  70.   /* Get rid of all highlighting and delete the directory listing */
  71.   clear_mark();
  72.   unexplode_window();
  73.   if (c == '\r')
  74.     fname = get_filename();
  75.   delete_buffer(dirbuf);
  76.  
  77.   if (c == ESC)                 /* we didn't choose a file */
  78.     show_buffer(orig_buf);
  79.   else                          /* we chose a file to edit */
  80.     show_buffer(find_buffer(fname));
  81.   explode_window();
  82. }
  83.  
  84. up_entry()
  85. {
  86.   if (currlinenum() > 1)
  87.   {
  88.     clear_mark();
  89.     up();
  90.     markline();
  91.   }
  92. }
  93.  
  94. down_entry()
  95. {
  96.   if (currlinenum() < lastlinenum())
  97.   {
  98.     clear_mark();
  99.     down();
  100.     markline();
  101.   }
  102. }
  103.  
  104. del_entry()
  105. {
  106.   choice = get_tty_str("Are you sure? (Y/N)");
  107.   if (choice == "Y" || choice == "y")
  108.   {
  109.     delete_buffer(find_buffer(get_filename()));
  110.     delline();
  111.     markline();
  112.   }
  113. }
  114.  
  115. /* get_filename - extracts the file name from the current line */
  116. get_filename()
  117. {
  118.   string name;
  119.  
  120.   name = rtrim(currline());
  121.   name = substr(name, 3, strlen(name)-2);
  122.   return name;
  123. }
  124.