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

  1. /***********************************************************************\
  2. |*                                                                     *|
  3. |* DIRED - a very simple directory editor                              *|
  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. |* file will replace the directory listing in the new window. You may  *|
  9. |* also delete the selected file 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. \***********************************************************************/
  15.  
  16. #define ALT_D   160
  17. #define UPKEY   200
  18. #define DOWNKEY 208
  19. #define DEL     211
  20. #define ESC     27
  21.  
  22. init()
  23. {
  24.   assign_key("dired", ALT_D);
  25. }
  26.  
  27. dired()
  28. {
  29.   string path;
  30.   string dirtmpfile;
  31.   int    dirbuf, orig_buf;
  32.   int    c;
  33.   
  34.   dirtmpfile = "$$$DIR$$.$$";
  35.   
  36.   /* Prompt the user for the file spec */
  37.   path = get_tty_str("Path : (default *.*)");
  38.   os_command(sprintf("DIR %s > %s", path, dirtmpfile));
  39.   
  40.   orig_buf = currbuf();
  41.   dirbuf = setcurrbuf(create_buffer(dirtmpfile));
  42.  
  43.   /* Get rid of the first 4 lines of the DIR listing - they contain nada */
  44.   for (i = 1;  i <= 4;  i++)
  45.     delline();
  46.  
  47.   /* We want to get rid of all the subdirectory listings */
  48.   while (fsearch("<DIR>"))
  49.     delline();
  50.  
  51.   /* Get rid of the file total & disk free info */
  52.   goeof();
  53.   delline();
  54.   gobof();
  55.  
  56.   /* Display the directory listing in a full-screen window */
  57.   show_buffer(dirbuf);
  58.   explode_window();
  59.   markline();   /* highlite the first entry */
  60.  
  61.   while ((c = get_tty_char()) != ESC && c != '\r')
  62.   {
  63.     if (c == UPKEY)
  64.       up_entry();
  65.     else if (c == DOWNKEY)
  66.       down_entry();
  67.     else if (c == 'd' || c == 'D' || c == DEL)
  68.       del_entry();
  69.   }
  70.  
  71.   /* Get rid of all highlighting and delete the directory listing */
  72.   clear_mark();
  73.   unexplode_window();
  74.   if (c == '\r')
  75.     fname = get_filename();
  76.   delete_buffer(dirbuf);
  77.  
  78.   if (c == ESC)                 /* we didn't choose a file */
  79.     show_buffer(orig_buf);
  80.   else                          /* we chose a file to edit */
  81.     show_buffer(create_buffer(fname));
  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.     os_command(strcat("del ", 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 root, extension;
  119.   
  120.   root = rtrim(substr(currline(), 1, 8));
  121.   extension = substr(currline(), 10, 3);
  122.   if (extension != "   ")
  123.     root = sprintf("%s.%s", root, rtrim(extension));
  124.   return root;
  125. }
  126.