/***********************************************************************\
|*                                                                     *|
|* SELECTB - a simple buffer selector patterned after DIRED            *|
|*                                                                     *|
|* 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  *|
|* bufr will replace the directory listing in the new window. You may  *|
|* also delete the selected bufr by pressing 'D' or the DEL key.       *|
|*                                                                     *|
|* Feel free to add your own enhancements.                             *|
|*                                                                     *|
|* (C) Copyright 1987  Marc Adler   Magma Software Systems             *|
|*     SELECTB was modified from DIRED by Blake McBride                *|
\***********************************************************************/

#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, fname;
  int    dirbuf, orig_buf, cb, mod;
  int    c;

  orig_buf = currbuf();
  dirtmpfile = "buffer_list";
  dirbuf = create_buffer(dirtmpfile);

  cb = next_buffer(dirbuf);
  while (cb != dirbuf)
  {
    setcurrbuf(cb);
    fname = filename();          /* extract the filename and modified flag */
    mod = buffer_modified();
    setcurrbuf(dirbuf);
    if (mod)                     /* insert the entry into the listing buffer */
      insert(sprintf("* %s\n", fname));
    else
      insert(sprintf("  %s\n", fname));
    cb = next_buffer(cb);
  }

  /* Display the directory listing in a full-screen window */
  delline();
  show_buffer(dirbuf);
  gobof();
  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(find_buffer(fname));
  explode_window();
}

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")
  {
    delete_buffer(find_buffer(get_filename()));
    delline();
    markline();
  }
}

/* get_filename - extracts the file name from the current line */
get_filename()
{
  string name;

  name = rtrim(currline());
  name = substr(name, 3, strlen(name)-2);
  return name;
}
