void edit_select_rep::select (path p1, path p2) { //cout << "Select " << p1 << " -- " << p2 << "\n"; if (start_p == p1 && end_p == p2) return; if (!(rp <= p1 && rp <= p2)) return; if (start_p == end_p && p1 == p2) { start_p= copy (p1); end_p = copy (p2); return; } if (p1 != p2) { path cp= common (p1, p2); tree st= subtree (et, cp); if (!is_func (st, TABLE) && !is_func (st, ROW)) (void) semantic_select (cp, p1, p2, 0); } if (path_less (p1, p2)) { start_p= copy (p1); end_p = copy (p2); } else { start_p= copy (p2); end_p = copy (p1); } notify_change (THE_SELECTION); }
static void adjust_right_script (tree t, path& o1) { while (is_concat (t) && o1->item > 0 && o1->next == path (0)) { tree st= t[o1->item]; if (is_func (st, RSUB) || is_func (st, RSUP) || is_func (st, RPRIME)) { tree pt= t[o1->item-1]; if (!is_atomic (pt)) o1= path (o1->item-1, start (pt)); else { string s= pt->label; int pos= N(s); while (pos > 0) { int prev= pos; tm_char_backwards (s, prev); if (pos == N(s)); else if (is_numeric (s (prev, N(s)))); else if (is_iso_alpha (s (prev, N(s)))); else break; pos= prev; } o1= path (o1->item-1, pos); } } else break; } }
static void adjust_left_script (tree t, path& o2) { while (is_concat (t) && o2->item + 1 < N(t) && o2->next == path (1)) { tree st= t[o2->item]; if (is_func (st, LSUB) || is_func (st, LSUP) || is_func (st, LPRIME)) { tree nt= t[o2->item+1]; if (!is_atomic (nt)) o2= path (o2->item+1, end (nt)); else { string s= nt->label; int pos= 0; while (pos < N(s)) { int next= pos; tm_char_forwards (s, next); if (pos == 0); else if (is_numeric (s (0, next))); else if (is_iso_alpha (s (0, next))); else break; pos= next; } o2= path (o2->item+1, pos); } } else break; } }
path edit_select_rep::selection_get_subtable ( int& row1, int& col1, int& row2, int& col2) { if (selection_active_table ()) { path fp= ::table_search_format (et, common (start_p, end_p)); if (is_nil (fp)) return fp; tree st= subtree (et, fp); table_search_coordinates (st, tail (start_p, N(fp)), row1, col1); table_search_coordinates (st, tail (end_p, N(fp)), row2, col2); if (row1>row2) { int tmp= row1; row1= row2; row2= tmp; } if (col1>col2) { int tmp= col1; col1= col2; col2= tmp; } table_bound (fp, row1, col1, row2, col2); return fp; } else if (selection_active_table (false)) { path fp= ::table_search_format (et, common (start_p, end_p) * 0); if (is_nil (fp)) return fp; path p= fp; tree st; while (true) { st= subtree (et, p); if (is_func (st, TABLE) && N(st) > 0 && is_func (st[0], ROW)) break; if (!is_func (st, TFORMAT)) return path (); p= p * (N(st) - 1); } row1= 0; col1= 0; row2= N(st)-1; col2= N(st[0])-1; return fp; } else return path (); }
path find_subtable_selection (tree et, path p1, path p2, int& row1, int& col1, int& row2, int& col2) { if (is_table_selection (et, p1, p2, true)) { path fp= table_search_format (et, common (p1, p2)); if (is_nil (fp)) return fp; tree st= subtree (et, fp); table_search_coordinates (st, tail (p1, N(fp)), row1, col1); table_search_coordinates (st, tail (p2, N(fp)), row2, col2); if (row1>row2) { int tmp= row1; row1= row2; row2= tmp; } if (col1>col2) { int tmp= col1; col1= col2; col2= tmp; } // table_bound (fp, row1, col1, row2, col2); // FIXME: table editing routines should be moved to src/Data/Tree return fp; } else if (is_table_selection (et, p1, p2, false)) { path fp= table_search_format (et, common (p1, p2) * 0); if (is_nil (fp)) return fp; path p= fp; tree st; while (true) { st= subtree (et, p); if (is_func (st, TABLE) && N(st) > 0 && is_func (st[0], ROW)) break; if (!is_func (st, TFORMAT)) return path (); p= p * (N(st) - 1); } row1= 0; col1= 0; row2= N(st)-1; col2= N(st[0])-1; return fp; } else return path (); }
void drd_info_rep::heuristic_init (hashmap<string,tree> env2) { // time_t tt= texmacs_time (); set_environment (env2); bool flag= true; int round= 0; while (flag) { // cout << HRULE; flag= false; iterator<string> it= iterate (env); while (it->busy()) { string var= it->next(); tree val= env[var]; if (is_func (val, MACRO)) flag= heuristic_init_macro (var, val) | flag; if (is_func (val, XMACRO)) flag= heuristic_init_xmacro (var, val) | flag; } if ((round++) == 10) { cout << "TeXmacs] Warning: bad heuristic drd convergence\n"; flag= false; } } // cout << "--> " << (texmacs_time ()-tt) << "ms\n"; }
tree evaluate_merge (tree t) { int i, n= N(t); if (n == 0) return ""; tree acc= evaluate (t[0]); if (is_concat (acc)) acc= tree_as_string (acc); for (i=1; i<n; i++) { tree add= evaluate (t[i]); if (is_atomic (acc) && (is_atomic (add) || is_concat (add))) acc= acc->label * tree_as_string (add); else if (is_tuple (acc) && is_tuple (add)) acc= acc * add; else if (is_func (acc, MACRO) && is_func (add, MACRO) && (N(acc) == N(add)) && (acc (0, N(acc)-1) == add (0, N(add)-1))) { tree r = copy (acc); tree u1= copy (acc[N(acc)-1]); tree u2= copy (add[N(add)-1]); tree u (CONCAT, u1, u2); if (u1 == "") u= u2; else if (u2 == "") u= u1; else if (is_atomic (u1) && is_atomic (u2)) u= u1->label * u2->label; r[N(r)-1]= u; acc= r; } else return evaluate_error ("bad merge"); } return acc; }
tree drd_info_rep::get_syntax (tree t, path p) { if (is_atomic (t)) { string s= t->label; if (N(s) == 1 || !existing_tree_label (s)) return UNINIT; return get_syntax (as_tree_label (s)); } else if (is_func (t, VALUE, 1) && is_atomic (t[0])) { string s= t[0]->label; if (!existing_tree_label (s)) return UNINIT; return get_syntax (as_tree_label (s)); } else if (L(t) < START_EXTENSIONS) return UNINIT; else { tree fun= the_drd->get_syntax (L(t)); if (fun == UNINIT) return UNINIT; else if (N(t) == 0 && !is_func (fun, MACRO)) return fun; else if (!is_func (fun, MACRO)) return UNINIT; else { int i, n= N(fun)-1; hashmap<tree,tree> tab (UNINIT); for (i=0; i<n; i++) { tree var= tree (ARG, fun[i]); tree val= ""; if (i < N(t)) { if (p == path (-1)) val= t[i]; else val= tree (QUASI, t[i], (tree) (p * i)); } tab (var)= val; } return replace (fun[n], tab); } } }
bool edit_interface_rep::set_hybrid_footer (tree st) { // WARNING: update edit_dynamic_rep::activate_hybrid when updating this if (is_atomic (st)) if (is_func (subtree (et, path_up (tp, 2)), HYBRID, 1)) { string msg; // macro argument string name= st->label; path mp= search_upwards (MACRO); if (!is_nil (mp)) { tree mt= subtree (et, mp); int i, n= N(mt)-1; for (i=0; i<n; i++) if (mt[i] == name) { set_message (concat (kbd ("return"), ": insert argument ", name), "hybrid command"); return true; } } // macro application tree f= get_env_value (name); if (drd->contains (name) && (f == UNINIT)) set_message (concat (kbd ("return"), ": insert primitive ", name), "hybrid command"); else if (is_func (f, MACRO) || is_func (f, XMACRO)) set_message (concat (kbd ("return"), ": insert macro ", name), "hybrid command"); else if (f != UNINIT) set_message (concat (kbd ("return"), ": insert value ", name), "hybrid command"); else return false; return true; } return false; }
tree downgrade_brackets (tree t, bool delete_missing, bool big_dot) { if (is_atomic (t)) return t; int i, n= N(t); tree r (t, n); for (i=0; i<n; i++) r[i]= downgrade_brackets (t[i], delete_missing, big_dot); if (is_func (r, AROUND, 3)) { if (delete_missing && r[0] == "<nobracket>" && r[2] == "<nobracket>") return concat (r[0], r[1], r[2]); tree lb= downgrade_bracket (r[0], false); tree rb= downgrade_bracket (r[2], false); r= concat (lb, r[1], rb); } if (is_func (r, VAR_AROUND, 3)) { tree lb= tree (LEFT, downgrade_bracket (r[0], true)); tree rb= tree (RIGHT, downgrade_bracket (r[2], true)); if (delete_missing) { if (lb == tree (LEFT, ".") && rb == tree (RIGHT, ".")); else if (lb == tree (LEFT, ".")) lb= ""; else if (rb == tree (RIGHT, ".")) rb= ""; } r= concat (lb, r[1], rb); } if (is_func (r, BIG_AROUND, 2)) { tree op= downgrade_bracket (r[0], true); if (big_dot) r= concat (tree (BIG, op), r[1], tree (BIG, ".")); else r= concat (tree (BIG, op), r[1]); } if (is_concat (r)) r= concat_recompose (concat_decompose (r)); return r; }
tree upgrade_above_below (tree t) { if (is_atomic (t)) return t; else if (is_concat (t)) { tree r (CONCAT); for (int i=0; i<N(t); i++) { tree x= upgrade_above_below (t[i]); if (is_concat (x)) r << A(x); else r << x; } return r; } else { int i, n= N(t); tree r (t, n); for (i=0; i<n; i++) r[i]= upgrade_above_below (t[i]); if (is_func (r, ABOVE, 2)) { if (is_func (r[0], BIG)) r= tree (CONCAT, r[0], tree (RSUP, r[1])); else if (is_concat_big (r[0])) r= tree (r[0] * tree (CONCAT, tree (RSUP, r[1]))); } if (is_func (r, BELOW, 2)) { if (is_func (r[0], BIG)) r= tree (CONCAT, r[0], tree (RSUB, r[1])); else if (is_concat_big (r[0])) r= tree (r[0] * tree (CONCAT, tree (RSUB, r[1]))); } return r; } }
void test(char const *str, bool (*is_func)(_iterator &)){ _iterator it = str; printf("[%s] ", it); printf("%d\n", is_func(it)); printf("[%s] ", it); printf("%d\n", is_func(it)); printf("[%s] ", it); printf("%d\n", is_func(it)); printf("\n"); }
bool drd_info_rep::is_dynamic (tree t, bool hack) { if (hack && L(t) >= START_EXTENSIONS) return true; // FIXME: temporary fix if (is_atomic (t)) return false; if (is_func (t, DOCUMENT) || is_func (t, PARA) || is_func (t, CONCAT) || is_func (t, TABLE) || is_func (t, ROW)) return false; return info[L(t)]->pi.arity_mode != ARITY_NORMAL; }
static bool is_concat_big (tree t) { if (!is_concat (t) || N(t) == 0 || !is_func (t[0], BIG)) return false; for (int i=1; i<N(t); i++) if (!is_func (t[i], RSUB, 1) && !is_func (t[i], RSUP, 1)) return false; return true; }
static path table_search_format (tree t, path p) { tree st= subtree (t, p); if (is_func (st, TFORMAT) && is_func (st[N(st)-1], TABLE)) return p; while ((!is_nil (p)) && (!is_func (subtree (t, p), TABLE))) p= path_up (p); if ((!is_nil (p)) && (is_func (subtree (t, path_up (p)), TFORMAT))) p= path_up (p); return p; }
tree edit_typeset_rep::get_init_value (string var) { if (init->contains (var)) { tree t= init [var]; return is_func (t, BACKUP, 2)? t[0]: t; } if (N(pre)==0) typeset_preamble (); tree t= pre [var]; return is_func (t, BACKUP, 2)? t[0]: t; }
bool drd_info_rep::set_locals (tree t) { if (!is_func (t, COLLECTION)) return false; int i, n= N(t); for (i=0; i<n; i++) if (is_func (t[i], ASSOCIATE, 2) && is_atomic (t[i][0])) info (make_tree_label (t[i][0]->label))= tag_info (t[i][1]); return true; }
static void rewrite_symbolic_arguments (tree macro, tree& env) { if (!is_func (env, ATTR)) return; for (int i=1; i<N(env); i+=2) if (is_func (env[i], ARG, 1)) { for (int j=0; j+1<N(macro); j++) if (macro[j] == env[i][0]) env[i]= tree (ARG, as_tree (j)); } }
bool edit_select_rep::selection_active_table (bool strict) { if (!selection_active_any ()) return false; path p= common (start_p, end_p); if ((p == start_p) || (p == end_p)) p= path_up (p); tree t= subtree (et, p); return is_func (t, TFORMAT) || is_func (t, TABLE) || is_func (t, ROW) || is_func (t, CELL) || (!strict && N(t) == 1 && is_func (t[0], TFORMAT)); }
static string index_name (tree t) { if (is_func (t, TUPLE, 2)) t= t[0]; else if (is_func (t, TUPLE, 3)) t= t[0]; else if (is_func (t, TUPLE, 5)) { if (t[0] == "") t= t[3]; else t= t[0]; } if (!is_tuple (t)) t= tuple (t); return locase_all (index_name_sub (t, false)); }
void edit_select_rep::selection_paste (string key) { tree t; string s; (void) ::get_selection (key, t, s, selection_import); if (inside_active_graphics ()) { if (is_tuple (t, "texmacs", 3)) call ("graphics-paste", t[1]); return; } if (is_tuple (t, "extern", 1)) { string mode= get_env_string (MODE); string lan = get_env_string (MODE_LANGUAGE (mode)); if ((selection_import == "latex") && (mode == "prog")) mode= "verbatim"; if ((selection_import == "latex") && (mode == "math")) mode= "latex-math"; if ((selection_import == "html") && (mode == "prog")) mode= "verbatim"; string fm= selection_import * "-snippet"; tree doc= generic_to_tree (selection_decode(lan, as_string(t[1])), fm); if (is_func (doc, DOCUMENT, 1)) doc= doc[0]; // temporary fix insert_tree (doc); } if (is_tuple (t, "texmacs", 3)) { string mode= get_env_string (MODE); string lan = get_env_string (MODE_LANGUAGE (mode)); if (is_compound (t[1], "text", 1) && mode == "text") t= tuple ("texmacs", t[1][0], "text", lan); if (is_compound (t[1], "math", 1) && mode == "math") t= tuple ("texmacs", t[1][0], "math", lan); if (mode == "math" && t[2] == "text") set_message ("Error: invalid paste of text into a formula", "paste"); else if (mode == "prog" && t[2] == "math") { tree in= tuple (lan, t[1]); tree r= stree_to_tree (call ("plugin-math-input", tree_to_stree (in))); insert_tree (r); } else { if ((t[2] != mode) && (t[2] != "src") && (mode != "src") && ((t[2] == "math") || (mode == "math"))) { if (t[2] == "math") insert_tree (compound ("math", ""), path (0, 0)); else if (t[2] == "text") insert_tree (compound ("text", ""), path (0, 0)); else insert_tree (tree (WITH, copy (MODE), copy (t[2]), ""), path (2, 0)); } if (is_func (t[1], TFORMAT) || is_func (t[1], TABLE)) { int row, col; path fp= search_format (row, col); if (is_nil (fp)) insert_tree (compound (copy (TABULAR), t[1])); else table_write_subtable (fp, row, col, t[1]); } else insert_tree (t[1]); } } }
bool is_table_selection (tree et, path p1, path p2, bool strict) { if (p1 == p2) return false; path p= common (p1, p2); if ((p == p1 || p == p2) && !is_nil (p)) p= path_up (p); tree t= subtree (et, p); return is_func (t, TFORMAT) || is_func (t, TABLE) || is_func (t, ROW) || is_func (t, CELL) || (!strict && N(t) == 1 && is_func (t[0], TFORMAT)); }
int exp_is_eqjoin(sql_exp *e) { if (e->flag == cmp_equal) { sql_exp *l = e->l; sql_exp *r = e->r; if (!is_func(l->type) && !is_func(r->type)) return 0; } return -1; }
void edit_select_rep::cut (path p1, path p2) { path p = common (p1, p2); tree st= subtree (et, p); raw_cut (p1, p2); if (!is_func (st, TFORMAT) && !is_func (st, TABLE) && !is_func (st, ROW) && !is_document (subtree (et, p)) && is_concat (subtree (et, path_up (p)))) correct_concat (path_up (p)); }
static tree index_value (tree t) { if (is_func (t, TUPLE, 2)) return t; else if (is_func (t, TUPLE, 3)) return tuple (t[2]); else if (is_func (t, TUPLE, 5)) { tree l= t[3], r= t[4]; if (!is_tuple (l)) l= tuple (l); if (t[1] == "strong") r= compound ("strong", r); if (t[2] != "") r= tuple ("range", t[2], r); return tuple (l, r); } return ""; }
bool edit_graphics_rep::inside_graphics (bool b) { path p = path_up (tp); bool flag= false; tree st = et; while (!is_nil (p)) { if (is_func (st, GRAPHICS)) flag= true; if (b && is_func (st, TEXT_AT)) flag= false; if (is_atomic (st) || p->item < 0 || p->item >= N(st)) break; st= st[p->item]; p = p->next; } return flag || (L(st) == GRAPHICS); }
static int minimal_arity (tree t, tree var) { if (is_atomic (t)) return 0; else if (is_func (t, ARG, 2) && (t[0] == var)) return as_int (t[1]) + 1; else if (is_func (t, MAP_ARGS) && (N(t)>=4) && (t[2] == var)) return as_int (t[3]); else { int i, n= N(t), m= 0; for (i=0; i<n; i++) m= max (m, minimal_arity (t[i], var)); return m; } }
static tree remove_labels (tree t) { if (is_atomic (t)) return t; if (is_func (t, LABEL)) return ""; int i, n= N(t); tree r (L(t)); for (i=0; i<n; i++) { tree u= remove_labels (t[i]); if (!is_concat (t) || u != "") r << u; } if (is_func (r, CONCAT, 0)) return ""; if (is_func (r, CONCAT, 1)) return r[0]; return r; }
bool incomplete_script_selection (tree t, path lp, path rp) { if (!is_func (t, CONCAT)) return false; if (N(lp) < 2 || N(rp) < 2) return false; int l= lp->item, r= rp->item; if (is_func (t[l], RSUB) || is_func (t[l], RSUP)) return true; if (is_func (t[r], LSUB) || is_func (t[r], LSUP)) return true; if (l >0 && (is_func (t[l-1], LSUB) || is_func (t[l-1], LSUP))) return true; if (r+1<N(t) && (is_func (t[r+1], RSUB) || is_func (t[r+1], RSUP))) return true; return false; }
static void table_search_coordinates (tree t, path p, int& row, int& col) { row= col= 0; while (true) { if (is_nil (p)) p= path (1); if (p == path (0)) p= path (0, 0); if (p == path (1)) p= path (N(t)-1, 1); if (is_func (t, TFORMAT)); else if (is_func (t, TABLE)) row= p->item; else if (is_func (t, ROW)) col= p->item; else return; t= t [p->item]; p= p->next; } }