home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ME22-OS2.ZIP / MACROS.ZIP / DIRED2.M < prev    next >
Text File  |  1988-01-01  |  3KB  |  129 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.   kid = keyboard_push();
  62.  
  63.   assign_key("up_entry",   UPKEY);
  64.   assign_key("down_entry", DOWNKEY);
  65.   assign_key("del_entry",  'd');
  66.   assign_key("del_entry",  'D');
  67.   assign_key("del_entry",  DEL);
  68.  
  69.   while ((c = get_tty_char()) != ESC && c != '\n')
  70.     command(c);
  71.  
  72.   keyboard_pop(1);
  73.  
  74.   /* Get rid of all highlighting and delete the directory listing */
  75.   clear_mark();
  76.   unexplode_window();
  77.   if (c == '\n')
  78.     fname = get_filename();
  79.   delete_buffer(dirbuf);
  80.  
  81.   if (c == ESC)                 /* we didn't choose a file */
  82.     show_buffer(orig_buf);
  83.   else                          /* we chose a file to edit */
  84.     show_buffer(create_buffer(fname));
  85. }
  86.  
  87. up_entry()
  88. {
  89.   if (currlinenum() > 1)
  90.   {
  91.     clear_mark();
  92.     up();
  93.     markline();
  94.   }
  95. }
  96.  
  97. down_entry()
  98. {
  99.   if (currlinenum() < lastlinenum())
  100.   {
  101.     clear_mark();
  102.     down();
  103.     markline();
  104.   }
  105. }
  106.  
  107. del_entry()
  108. {
  109.   choice = get_tty_str("Are you sure? (Y/N)");
  110.   if (choice == "Y" || choice == "y")
  111.   {
  112.     os_command(strcat("del ", get_filename()));
  113.     delline();
  114.     markline();
  115.   }
  116. }
  117.  
  118. /* get_filename - extracts the file name from the current line */
  119. get_filename()
  120. {
  121.   string root, extension;
  122.   
  123.   root = rtrim(substr(currline(), 1, 8));
  124.   extension = substr(currline(), 10, 3);
  125.   if (extension != "   ")
  126.     root = sprintf("%s.%s", root, rtrim(extension));
  127.   return root;
  128. }
  129.