pike.git / lib / modules / Stdio.pmod / Readline.pike

version» Context lines:

pike.git/lib/modules/Stdio.pmod/Readline.pike:1: - // $Id: Readline.pike,v 1.59 2006/11/04 19:06:50 nilsson Exp $ + // $Id: Readline.pike,v 1.60 2008/06/28 16:37:00 nilsson Exp $   #pike __REAL_VERSION__      //!   //! @fixme   //! Ought to have support for charset conversion.   class OutputController   { -  static private .File outfd; -  static private .Terminfo.Termcap term; -  static private int xpos = 0, columns = 0; -  static private mapping oldattrs = 0; +  protected private .File outfd; +  protected private .Terminfo.Termcap term; +  protected private int xpos = 0, columns = 0; +  protected private mapping oldattrs = 0;      #define BLINK 1   #define BOLD 2   #define DIM 4   #define REVERSE 8   #define ITALIC 16   #define STANDOUT 32   #define UNDERLINE 64    -  static private int selected_attributes = 0, needed_attributes = 0; -  static private int active_attributes = 0; +  protected private int selected_attributes = 0, needed_attributes = 0; +  protected private int active_attributes = 0;    -  static int low_attribute_mask(array(string) atts) +  protected int low_attribute_mask(array(string) atts)    {    return `|(@rows(([    "blink":BLINK,    "bold":BOLD,    "dim":DIM,    "reverse":REVERSE,    "italic":ITALIC,    "standout":STANDOUT,    "underline":UNDERLINE    ]), atts));    }    -  static void low_set_attributes(int mask, int val, int|void temp) +  protected void low_set_attributes(int mask, int val, int|void temp)    {    int remv = mask & selected_attributes & ~val;    string s = "";       if(remv & 15) {    s += term->put("me")||"";    needed_attributes |= selected_attributes;    active_attributes = 0;    }    if(temp) {
pike.git/lib/modules/Stdio.pmod/Readline.pike:59:    s += cap;    remv &= ~(1<<i);    }    if(sizeof(s))    outfd->write(s);    int add = mask & val & ~selected_attributes;    selected_attributes |= add;    needed_attributes |= add;    }    -  static void low_disable_attributes() +  protected void low_disable_attributes()    {    low_set_attributes(active_attributes, 0, 1);    }    -  static void low_enable_attributes() +  protected void low_enable_attributes()    {    int i, add = needed_attributes;    string s = "";       needed_attributes &= ~add;    for(i=0; add; i++)    if(add & (1<<i)) {    string cap = ({"mb","md","mh","mr","ZH","so","us"})[i];    if(cap && (cap = term->put(cap))) {    s += cap;
pike.git/lib/modules/Stdio.pmod/Readline.pike:118:    //!    void enable()    {    if(term->put("cr") && term->put("do"))    catch { outfd->tcsetattr((["OPOST":0])); };    else    catch { outfd->tcsetattr((["OPOST":1,"ONLCR":1,"OCRNL":0,"OLCUC":0,    "OFILL":1,"OFDEL":0,"ONLRET":0,"ONOCR":0]));};    }    -  static void destroy() +  protected void destroy()    {    disable();    }       //! Check and return the terminal width.    //!    //! @note    //! In Pike 7.4 and earlier this function returned @expr{void@}.    //!    //! @seealso
pike.git/lib/modules/Stdio.pmod/Readline.pike:152:    //! Returns the width of the terminal.    //! @note    //! Does not check the width of the terminal.    //! @seealso    //! @[check_columns]    int get_number_of_columns()    {    return columns;    }    -  static string escapify(string s, void|int hide) +  protected string escapify(string s, void|int hide)    {   #if 1    s=replace(s,    ({    "\000","\001","\002","\003","\004","\005","\006","\007",    "\010","\011","\012","\013","\014","\015","\016","\017",    "\020","\021","\022","\023","\024","\025","\026","\027",    "\030","\031","\032","\033","\034","\035","\036","\037",    "\177",    "\200","\201","\202","\203","\204","\205","\206","\207",
pike.git/lib/modules/Stdio.pmod/Readline.pike:193:    s = s[..i-1]+sprintf("^%c", s[i]+'@')+s[i+1..];    else if(s[i]==127)    s = s[..i-1]+"^?"+s[i+1..];    else if(s[i]>=128 && s[i]<160)    s = s[..i-1]+sprintf("~%c", s[i]-128+'@')+s[i+1..];    return s;   #endif    }       -  static int width(string s) +  protected int width(string s)    {    return sizeof(s);    }    -  static int escapified_width(string s) +  protected int escapified_width(string s)    {    return width(escapify(s));    }       //!    void low_write(string s, void|int word_break)    {    int n = width(s);    if(!n)    return;
pike.git/lib/modules/Stdio.pmod/Readline.pike:424:    catch { oldattrs = outfd->tcgetattr(); };    check_columns();    }   }      //!   //! @fixme   //! Ought to have support for charset conversion.   class InputController   { -  static private object infd, term; -  static private int enabled = -1; -  static private function(:int) close_callback = 0; -  static private string prefix=""; -  static private mapping(int:function|mapping(string:function)) bindings=([]); -  static private function grab_binding = 0; -  static private mapping oldattrs = 0; +  protected private object infd, term; +  protected private int enabled = -1; +  protected private function(:int) close_callback = 0; +  protected private string prefix=""; +  protected private mapping(int:function|mapping(string:function)) bindings=([]); +  protected private function grab_binding = 0; +  protected private mapping oldattrs = 0;       int dumb=0;    -  static void destroy() +  protected void destroy()    {    catch{ infd->set_blocking(); };    if(dumb)    return;    catch{ infd->tcsetattr((["ECHO":1,"ICANON":1])); };    catch{ if(oldattrs) infd->tcsetattr((["ECHO":0,"ICANON":0,"VEOF":0,    "VEOL":0,"VLNEXT":0])&oldattrs); };    }    -  static private string process_input(string s) +  protected private string process_input(string s)    {    int i;       for (i=0; i<sizeof(s); i++)    {    if (!enabled)    return s[i..];    function|mapping(string:function) b = grab_binding || bindings[s[i]];    grab_binding = 0;    if (!b)
pike.git/lib/modules/Stdio.pmod/Readline.pike:481:    {    i += ml-1;    b[m](m);    }    } else    b(s[i..i]);    }    return "";    }    -  static private void read_cb(mixed _, string s) +  protected private void read_cb(mixed _, string s)    {    if (!s || s=="")    return;    if (sizeof(prefix))    {    s = prefix+s;    prefix = "";    }    prefix = process_input(s);    }    -  static private void close_cb() +  protected private void close_cb()    {    if (close_callback && close_callback())    return;    destruct(this);    }    -  static private int set_enabled(int e) +  protected private int set_enabled(int e)    {    if (e != enabled)    {    enabled = e;    if (enabled)    {    string oldprefix = prefix;    prefix = "";    prefix = process_input(oldprefix);    if ((!infd->set_read_callback || !infd->set_close_callback) && infd->set_nonblocking)
pike.git/lib/modules/Stdio.pmod/Readline.pike:789:    }    catch { infd->tcsetattr((["ECHO":0,"ICANON":0,"VMIN":1,"VTIME":0,    "VLNEXT":0])); };    }      }      //!   class DefaultEditKeys   { -  static private multiset word_break_chars = +  protected private multiset word_break_chars =    mkmultiset("\t \n\r/*?_-.[]~&;\!#$%^(){}<>\"'`"/""); -  static object _readline; +  protected object _readline;       //!    void self_insert_command(string str)    {    _readline->insert(str, _readline->getcursorpos());    }       //!    void quoted_insert()    {
pike.git/lib/modules/Stdio.pmod/Readline.pike:883:    void transpose_chars()    {    int p = _readline->getcursorpos();    if (p<0 || p>=sizeof(_readline->gettext()))    return;    string c = _readline->gettext()[p-1..p];    _readline->delete(p-1, p+1);    _readline->insert(reverse(c), p-1);    }    -  static array find_word_to_manipulate() +  protected array find_word_to_manipulate()    {    int p = _readline->getcursorpos();    int ep;    string line = _readline->gettext();    while(word_break_chars[ line[p..p] ] && p < sizeof(line))    p++;    if(p >= sizeof(line)) {    _readline->setcursorpos(p);    return ({ 0, 0 });    }
pike.git/lib/modules/Stdio.pmod/Readline.pike:923:    }       //!    void downcase_word()    {    [string word, string pos]= find_word_to_manipulate();    if(word)    _readline->insert(lower_case(word), pos);    }    -  static int forward_find_word() +  protected int forward_find_word()    {    int p, n;    string line = _readline->gettext();    for(p = _readline->getcursorpos(); p < sizeof(line); p++) {    if(word_break_chars[ line[p..p] ]) {    if(n) break;    } else n = 1;    }    return p;    }    -  static int backward_find_word() +  protected int backward_find_word()    {    int p = _readline->getcursorpos()-1;    string line = _readline->gettext();    if(p >= sizeof(line)) p = sizeof(line) - 1;    while(word_break_chars[ line[p..p] ] && p >= 0)    // find first "non break char"    p--;    for(;p >= 0; p--)    if(word_break_chars[ line[p..p] ]) {    p++; // We want to be one char before the break char.
pike.git/lib/modules/Stdio.pmod/Readline.pike:1036:    {    _readline->redisplay(0);    }       //!    void clear_screen()    {    _readline->redisplay(1);    }    -  static array(array(string|function)) default_bindings = ({ +  protected array(array(string|function)) default_bindings = ({    ({ "^[[A", up_history }),    ({ "^[[B", down_history }),    ({ "^[[C", forward_char }),    ({ "^[[D", backward_char }),    ({ "^[^[[C", forward_word }),    ({ "^[^[[D", backward_word }),    ({ "^[C", capitalize_word }),    ({ "^[c", capitalize_word }),    ({ "^[U", upcase_word }),    ({ "^[u", upcase_word }),
pike.git/lib/modules/Stdio.pmod/Readline.pike:1113:    ({ "\\!kbs", backward_delete_char }),    ({ "^[\\!kdch1", kill_word }),    ({ "^[\\!kbs", backward_kill_word }),    ({ "\\!khome", beginning_of_line }),    ({ "\\!kend", end_of_line }),    ({ "\\!kent", newline }),    ({ "\\!home", beginning_of_line }),    });       //! -  static void set_default_bindings() +  protected void set_default_bindings()    {    object ic = _readline->get_input_controller();    ic->nullbindings();    for(int i=' '; i<'\177'; i++)    ic->bindstr(sprintf("%c", i), self_insert_command);    for(int i='\240'; i<='\377'; i++)    ic->bindstr(sprintf("%c", i), self_insert_command);       if(ic->dumb) {    ic->bind("^J", newline);
pike.git/lib/modules/Stdio.pmod/Readline.pike:1143:    {    _readline = readline;    set_default_bindings();    }      }      //!   class History   { -  static private array(string) historylist; -  static private mapping(int:string) historykeep=([]); -  static private int minhistory, maxhistory, historynum; +  protected private array(string) historylist; +  protected private mapping(int:string) historykeep=([]); +  protected private int minhistory, maxhistory, historynum;       //!    string encode()    {    return historylist*"\n";    }       //!    int get_history_num()    {
pike.git/lib/modules/Stdio.pmod/Readline.pike:1217:    //!    void create(int maxhist, void|array(string) hist)    {    historylist = hist || ({ "" });    minhistory = historynum = 0;    maxhistory = maxhist;    }   }       - static private OutputController output_controller; - static private InputController input_controller; - static private string prompt=""; - static private array(string) prompt_attrs=0; - static private string text="", readtext; - static private function(string:void) newline_func; - static private int cursorpos = 0; - static private int mark = 0; + protected private OutputController output_controller; + protected private InputController input_controller; + protected private string prompt=""; + protected private array(string) prompt_attrs=0; + protected private string text="", readtext; + protected private function(string:void) newline_func; + protected private int cursorpos = 0; + protected private int mark = 0;   /*static private */ History historyobj = 0; - static private int hide = 0; + protected private int hide = 0;    - static private array(string) kill_ring=({}); - static private int kill_ring_size=30; + protected private array(string) kill_ring=({}); + protected private int kill_ring_size=30;      //! get current output control object   //! @returns   //! Terminal output controller object   OutputController get_output_controller()   {    return output_controller;   }      //! get current input control object
pike.git/lib/modules/Stdio.pmod/Readline.pike:1482:    output_controller->write(prompt);    if(prompt_attrs)    output_controller->turn_off(@prompt_attrs);    }    output_controller->write(text,0,hide);    }    cursorpos = sizeof(text);    setcursorpos(p);   }    - static private void initline() + protected private void initline()   {    text = "";    cursorpos = 0;    if (historyobj)    historyobj->initline();   }      //! @fixme   //! Document this function   string newline()
pike.git/lib/modules/Stdio.pmod/Readline.pike:1563:   }      //! @fixme   //! Document this function   void list_completions(array(string) c)   {    message(sprintf("%-*#s",output_controller->get_number_of_columns(),    c*"\n"));   }    - static private void read_newline(string s) + protected private void read_newline(string s)   {    input_controller->disable();    readtext = s;   }      //! @fixme   //! Document this function   void set_nonblocking(function f)   {    int p=cursorpos;
pike.git/lib/modules/Stdio.pmod/Readline.pike:1674:    historyobj = History(hist);   }      //! @fixme   //! Document this function   History get_history()   {    return historyobj;   }    - static void destroy() + protected void destroy()   {    if(input_controller)    destruct(input_controller);    if(output_controller)    destruct(output_controller);   }      //! Creates a Readline object, that takes input from @[infd] and has output   //! on @[outfd].   //!