/***********************************************************************\
|*                                                                     *|
|* DIRED - a very simple directory editor                              *|
|*                                                                     *|
|* You can invoke this from the editor by loading in the DIRED macro   *|
|* and pressing <ALT> D. A directory listing will appear in another    *|
|* window. Move the highlight to a file and press ENTER. The selected  *|
|* file will replace the directory listing in the new window. You may  *|
|* also delete the selected file by pressing 'D' or the DEL key.       *|
|*                                                                     *|
|* Feel free to add your own enhancements.                             *|
|*                                                                     *|
|* (C) Copyright 1987  Marc Adler   Magma Software Systems             *|
\***********************************************************************/

#define ALT_D   160
#define UPKEY   200
#define DOWNKEY 208
#define DEL     211
#define ESC     27

init()
{
  assign_key("dired", ALT_D);
}

dired()
{
  string path;
  string dirtmpfile;
  int    dirbuf, orig_buf;
  int    c;
  
  dirtmpfile = "$$$DIR$$.$$";
  
  /* Prompt the user for the file spec */
  path = get_tty_str("Path : (default *.*)");
  os_command(sprintf("DIR %s > %s", path, dirtmpfile));
  
  orig_buf = currbuf();
  dirbuf = setcurrbuf(create_buffer(dirtmpfile));

  /* Get rid of the first 4 lines of the DIR listing - they contain nada */
  for (i = 1;  i <= 4;  i++)
    delline();

  /* We want to get rid of all the subdirectory listings */
  while (fsearch("<DIR>"))
    delline();

  /* Get rid of the file total & disk free info */
  goeof();
  delline();
  gobof();

  /* Display the directory listing in a full-screen window */
  show_buffer(dirbuf);
  explode_window();
  markline();   /* highlite the first entry */

  while ((c = get_tty_char()) != ESC && c != '\r')
  {
    if (c == UPKEY)
      up_entry();
    else if (c == DOWNKEY)
      down_entry();
    else if (c == 'd' || c == 'D' || c == DEL)
      del_entry();
  }

  /* Get rid of all highlighting and delete the directory listing */
  clear_mark();
  unexplode_window();
  if (c == '\r')
    fname = get_filename();
  delete_buffer(dirbuf);

  if (c == ESC)                 /* we didn't choose a file */
    show_buffer(orig_buf);
  else                          /* we chose a file to edit */
    show_buffer(create_buffer(fname));
}

up_entry()
{
  if (currlinenum() > 1)
  {
    clear_mark();
    up();
    markline();
  }
}

down_entry()
{
  if (currlinenum() < lastlinenum())
  {
    clear_mark();
    down();
    markline();
  }
}

del_entry()
{
  choice = get_tty_str("Are you sure? (Y/N)");
  if (choice == "Y" || choice == "y")
  {
    os_command(strcat("del ", get_filename()));
    delline();
    markline();
  }
}

/* get_filename - extracts the file name from the current line */
get_filename()
{
  string root, extension;
  
  root = rtrim(substr(currline(), 1, 8));
  extension = substr(currline(), 10, 3);
  if (extension != "   ")
    root = sprintf("%s.%s", root, rtrim(extension));
  return root;
}
