

int quotes_handle;
int quotes_status;


// if character is a quote and previous character is a Space, Return etc,
// substitute an opening smart quote otherwise substitute a closing smart quote

int quotes_fix(int user, int view, int key)
{
 int p;

 if (key == ''' || key == '\"')
 {
  p = prevchar();
  if (p <= 32 || p == '(' || p == '\"' || p == '' || p == '[' || p == '')
   key = (key == ''') ? '' : '';
  else
   key = (key == ''') ? '' : '';
 }
 return(key);
}


// set up EVENT_KEYPRESS events

void quotes_setevent(int i)
{
 if (i)  
  addeventhandler(0x300, 0, "quotes_fix");
 else
  remeventhandler(0x300, 0, "quotes_fix");

 quotes_status = i;
}


// deal with 'Smart quotes' menu entry

int quotes_entry(int entry, int subcode)
{
 if(subcode == -1)
  quotes_setevent(!quotes_status);
}


// tick or un-tick 'Smart quotes' menu entry

int quotes_flags(int entry, string &text)
{
 return(quotes_status);
}


// replace plain quotes with smart quotes

void quotes_add(void)
{
 int b1, c, p;

 if(activetype(TEXTFRAME) <= 1)
  return;

 b1 = bmcreate("quotes_b1");
 setbmtocaret(b1);
 bmmove(b1, 0, 4);

 while((c = bmchar(b1)))
 {
  if (c == ''' || c == '\"')
  {
   p = bmprevchar(b1);
   setcarettobm(b1);
   if (p <= 32 || p == '(' || p == '\"' || p == '' || p == '[' || p == '')
    type("{Deletef}" + ((c == ''') ? "" : ""));
   else
    type("{Deletef}" + ((c == ''') ? "" : ""));
  }
  else
   bmmove(b1, 1, 0);
 }
 bmdelete(b1);
}


// replace smart quotes with plain quotes

void quotes_remove(void)
{
 int b1, c;

 if(activetype(TEXTFRAME) <= 1)
  return;

 b1 = bmcreate("quotes_b1");
 setbmtocaret(b1);
 bmmove(b1, 0, 4);

 while((c = bmchar(b1)))
 {
  if(c == '' || c == '')
  {
   setcarettobm(b1);
   type("{Deletef}'");
  }
  if(c == '' || c == '')
  {
   setcarettobm(b1);
   type("{Deletef}\"");
  }
  else
   bmmove(b1, 1, 0);
 }
 bmdelete(b1);
}


// deal with  'Smart quotes' menu entry

int quotes_menu_entry(int entry, int subcode)
{
 switch(entry)
 {
  case 0:
         quotes_add();
         break;
  case 1:
         quotes_remove();
         break;
  case 2:
         type("'");
         break;
  case 3:
         type("\"");
         break;
  case 4:
         type("");
         break;
 }
 return(0);
}


// put dotted line on 'Smart quotes' menu

int quotes_menu_flags(int entry, string &text)
{
 return(DOTTED);
}


int quotes_menu(int open)
{
 return(quotes_handle);
}


// create 'Smart quotes' menu and add it to 'Applets' menu
// enable smart quotes

void main(void)
{
 script_menu_initialise();

 quotes_handle = create_menu("{QUOTES_00}");
 addentry_menu(quotes_handle, "quotes_menu_entry","","","","{QUOTES_01}");
 addentry_menu(quotes_handle, "quotes_menu_entry","quotes_menu_flags","","","{QUOTES_02}");
 addentry_menu(quotes_handle, "quotes_menu_entry","","","CS_1","{QUOTES_03}");
 addentry_menu(quotes_handle, "quotes_menu_entry","","","CS_2","{QUOTES_04}");
 addentry_menu(quotes_handle, "quotes_menu_entry","","","CS_3","{QUOTES_05}");

 addentry_menu(script_handle,"quotes_entry","quotes_flags","quotes_menu","","{QUOTES_00}");
 quotes_setevent(1);
}
