jed-users mailing list

[2002 Date Index] [2002 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

autotext.sl


% File:          autotext.sl      -*- mode: SLang; mode: fold -*-
%
% Author:        Paul Boekholt <p.boekholt@xxxxxxxxx>
% 
% Version:       0.0.1
% 
% This file provides autotext a la MS Word
% I have only tried this on Linux with Jed 0.99.15
% 
% To install this add
% () = evalfile ("autotext.sl");
% to your .jedrc
% 
% $Id: autotext.sl,v 1.8 2002/08/01 15:48:44 paul Exp paul $
%

_traceback=1;
_debug_info=1;


public variable autotext = Assoc_Type [String_Type];
static variable Autotext_File = getenv ("HOME") + "/.autotext";
static variable autotext_has_changed = 0;
  
%{{{ Adding autotext items

public define new_autotext_item ()
{
  variable key, text;
  if ( 0 == markp ())
    {
      flush("no region is defined");
      return;
    } 
  text = bufsubstr ();
  !if (strlen (text)) return;
  key = "&" + strtrim (substr (extract_element (text, 0, '\n'), 1, 40));
  forever
    {
      key = read_mini("Keyword? ", "", key);
      if (0 == assoc_key_exists (autotext, key)) break;
      if (1 == get_y_or_n ("Keyword exists. Overwrite")) break;
    }
  !if (strlen (key)) return;
  autotext[key]=text;
  autotext_has_changed = 1;
}
%}}}

%{{{ Building the menus

%menu for removing autotext items
static define autotext_remove_callback (popup)
{
  variable k;
  foreach (autotext) using ("keys")
    {
      k = ();
      menu_append_item (popup, k, sprintf ("assoc_delete_key(autotext, \"%s\")",k));
    }
  autotext_has_changed = 1;
}

%menu for inserting autotext
static define autotext_menu_callback (popup)
{
  variable key, cmd;
  menu_append_item (popup, "new", "new_autotext_item");
  menu_append_popup (popup, "&Remove");
  menu_set_select_popup_callback (strcat (popup, ".&Remove"),
				  &autotext_remove_callback);
  menu_append_separator (popup);
  foreach (autotext) using ("keys")
    {
      key = ();
      !if (strlen (key)) break;
      cmd = sprintf ("insert(autotext[\"%s\"])", key);
      menu_append_item (popup, key, cmd);
    }
}

%create the menu
static define add_autotext_popup_hook (menubar)
{
  variable menu = "Global.&Edit";
  menu_append_separator (menu);
  menu_append_popup (menu, "&Autotext");
  menu_set_select_popup_callback (strcat (menu, ".&Autotext"),
				  &autotext_menu_callback);
}

%}}}

%{{{ file stuff.

% the autotext file is evaluated as slang code, and since a slang literal 
% string is limited to 256 char, we need to split it up
define slang_string (string)
{
  variable n = strlen (string) / 100 + 1;
  variable strings = String_Type[n];
  variable i = 1, j = 0;
  loop (n)
    {
      strings[j] = make_printable_string (substr (string, i, 100));
      i+=100;
      j++;
    }
  return strjoin (strings, "\n + ");
}


public define save_autotext_file ()
{
  variable key, value, fp;
  if (0 == autotext_has_changed) return 1;
  fp = fopen (Autotext_File, "w");
  if (fp == NULL)
    {
      flush("could not save autotext");
      return 1;
    }
  ERROR_BLOCK
    {
      flush ("I failed to save your autotext");
      () = getkey ();
      _clear_error ();
    }
  foreach (autotext) using ("keys", "values")
    {
      (key, value) = ();
      () = fputs ("autotext[" + make_printable_string (key) + "] = \n" + slang_string (value) + ";\n", fp);
    }
  return 1;
}

public define read_autotext_file ()
{
  !if (1 == file_status (Autotext_File)) return;
  ERROR_BLOCK
    { 
      flush ("error reading the autotext");
      _clear_error (); 
    }
  () = evalfile (Autotext_File);
}
%}}}

append_to_hook ("load_popup_hooks", &add_autotext_popup_hook);
add_to_hook ("_jed_exit_hooks", &save_autotext_file);
read_autotext_file ();



--------------------------
To unsubscribe send email to <jed-users-request@xxxxxxxxxxx> with
the word "unsubscribe" in the message body.
Need help? Email <jed-users-owner@xxxxxxxxxxx>.


[2002 date index] [2002 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]