Back to index

texmacs  1.0.7.15
evaluate_misc.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : evaluate_misc.cpp
00004 * DESCRIPTION: various other primitives for evaluation
00005 * COPYRIGHT  : (C) 2006  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "evaluate_main.hpp"
00013 #include "std_environment.hpp"
00014 #include "vars.hpp"
00015 #include "analyze.hpp"
00016 #include "url.hpp"
00017 #include "../../Typeset/Graphics/frame.hpp"
00018 #include "image_files.hpp"
00019 #include "renderer.hpp"
00020 
00021 static hashmap<string,tree> local_ref ("?");
00022 static hashmap<string,tree> global_ref ("?");
00023 
00024 tree
00025 evaluate_formatting (tree t, string v) {
00026   int i, n= N(t);
00027   tree r (t, n-1);
00028   for (i=0; i<n-1; i++) r[i]= evaluate (t[i]);
00029   tree oldv= std_env [v];
00030   tree newv= oldv * r;
00031   assoc_environment local (1);
00032   local->raw_write (0, v, newv);
00033   begin_with (std_env, local);
00034   tree b= evaluate (t[n-1]);
00035   end_with (std_env);
00036   return r * tree (TFORMAT, b);
00037 }
00038 
00039 tree
00040 evaluate_table (tree t) {
00041   // FIXME: we should execute values in old cell format
00042   assoc_environment local (1);
00043   local->raw_write (0, CELL_FORMAT, tree (TFORMAT));
00044   begin_with (std_env, local);
00045   int i, n= N(t);
00046   tree r (t, n);
00047   for (i=0; i<n; i++) r[i]= evaluate (t[i]);
00048   end_with (std_env);
00049   return r;
00050 }
00051 
00052 tree
00053 evaluate_hard_id (tree t) {
00054   if (N(t) == 0) {
00055     pointer ptr= (pointer) std_env.operator -> ();
00056     return "%" * as_hexadecimal (ptr);
00057   }
00058   else {
00059     t= expand (t[0], true);
00060     pointer ptr1= (pointer) std_env.operator -> ();
00061     pointer ptr2= (pointer) t.operator -> ();
00062     return "%" * as_hexadecimal (ptr1) * "-" * as_hexadecimal (ptr2);
00063   }
00064 }
00065 
00066 tree
00067 evaluate_script (tree t) {
00068   if (N(t) != 1 && N(t) != 2) return tree (ERROR, "bad script");
00069   if (N(t) == 1) return tree (SCRIPT, evaluate (t[0]));
00070   else return tree (SCRIPT, evaluate (t[0]), expand (t[1], true));
00071 }
00072 
00073 tree
00074 evaluate_set_binding (tree t) {
00075   tree keys, value;
00076   if (N(t) == 1) {
00077     keys= std_env ["the-tags"];
00078     if (!is_tuple (keys))
00079       return tree (ERROR, "bad set binding");
00080     for (int i=0; i<N(keys); i++)
00081       if (!is_atomic (keys[i]))
00082        return tree (ERROR, "bad set binding");
00083     value= evaluate (t[0]);
00084     assoc_environment local (2);
00085     local->raw_write (0, string ("the-tags"), tree (TUPLE));
00086     local->raw_write (1, string ("the-label"), copy (value));
00087     assign (std_env, local);
00088   }
00089   else if (N(t) >= 2) {
00090     tree key= evaluate (t[0]);
00091     if (!is_atomic (key)) 
00092       return tree (ERROR, "bad set binding");
00093     keys= tuple (key);
00094     value= evaluate (t[1]);
00095   }
00096   else return tree (ERROR, "bad set binding");
00097 
00098   for (int i=0; i<N(keys); i++) {
00099     string key= keys[i]->label;
00100     tree old_value= local_ref[key];
00101     string part= as_string (std_env ["current-part"]);
00102     url base_file_name (as_string (std_env ["base-file-name"]));
00103     url cur_file_name (as_string (std_env ["cur-file-name"]));
00104     if (is_func (old_value, TUPLE) && (N(old_value) >= 2))
00105       local_ref (key)= tuple (copy (value), old_value[1]);
00106     else local_ref (key)= tuple (copy (value), "?");
00107     if (cur_file_name != base_file_name || N(part) != 0) {
00108       string extra;
00109       if (cur_file_name != base_file_name)
00110        extra << as_string (delta (base_file_name, cur_file_name));
00111       if (N(part) != 0)
00112        extra << "#" << part (1, N(part));
00113       local_ref (key) << extra;
00114     }
00115     /* FIXME:
00116     if (complete && is_tuple (old_value) && N(old_value) >= 1) {
00117       string old_s= tree_as_string (old_value[0]);
00118       string new_s= tree_as_string (value);
00119       if (new_s != old_s && !starts (key, "auto-")) {
00120        if (new_s == "") system_warning ("Redefined", key);
00121        else system_warning ("Redefined " * key * " as", new_s);
00122       }
00123     }
00124     */
00125   }
00126 
00127   return ""; // FIXME: do stuff from concater_rep::typeset_set_binding instead
00128 }
00129 
00130 tree
00131 evaluate_get_binding (tree t) {
00132   if (N(t) != 1 && N(t) != 2) return tree (ERROR, "bad get binding");
00133   string key= evaluate_string (t[0]);
00134   tree value= local_ref->contains (key)? local_ref [key]: global_ref [key];
00135   int type= (N(t) == 1? 0: as_int (evaluate_string (t[1])));
00136   if (type != 0 && type != 1) type= 0;
00137   if (is_func (value, TUPLE) && (N(value) >= 2)) value= value[type];
00138   else if (type == 1) value= tree (UNINIT);
00139   /* FIXME:
00140   if (complete && value == tree (UNINIT))
00141     system_warning ("Undefined reference", key);
00142   */
00143   return value;
00144 }
00145 
00146 tree
00147 evaluate_pattern (tree t) {
00148   url base_file_name (as_string (std_env ["base-file-name"]));
00149   url im= evaluate_string (t[0]);
00150   url image= resolve (relative (base_file_name, im));
00151   if (is_none (image))
00152     image= resolve (url ("$TEXMACS_PATTERN_PATH") * im);
00153   if (is_none (image)) return "white";
00154   int imw_pt, imh_pt;
00155   int dpi= as_int (as_string (std_env ["dpi"]));
00156   image_size (image, imw_pt, imh_pt);
00157   double pt= ((double) dpi*PIXEL) / 72.0;
00158   SI imw= (SI) (((double) imw_pt) * pt);
00159   SI imh= (SI) (((double) imh_pt) * pt);
00160   if (imw <= 0 || imh <= 0) return "white";
00161   string w= evaluate_string (t[1]);
00162   string h= evaluate_string (t[2]);
00163   if (is_length (w))
00164     w= as_string (as_length (w));
00165   else if (is_magnification (w))
00166     w= as_string ((SI) (get_magnification (w) * ((double) imw)));
00167   if (is_length (h))
00168     h= as_string (as_length (h));
00169   else if (is_magnification (h))
00170     h= as_string ((SI) (get_magnification (h) * ((double) imh)));
00171   if (w == "" && h != "") {
00172     if (is_int (h)) w= as_string ((SI) ((as_double (h) * imw) / imh));
00173     else if (is_percentage (h))
00174       w= as_string (100.0 * (as_percentage (h) * imw) / imh) * "@";
00175     else return "white";
00176   }
00177   else if (h == "" && w != "") {
00178     if (is_int (w)) h= as_string ((SI) ((as_double (w) * imh) / imw));
00179     else if (is_percentage (w))
00180       h= as_string (100.0 * (as_percentage (w) * imh) / imw) * "@";
00181     else return "white";
00182   }
00183   else if (w == "" && h == "") {
00184     w= as_string (imw);
00185     h= as_string (imh);
00186   }
00187   else if ((!is_int (w) && !is_percentage (w)) ||
00188           (!is_int (h) && !is_percentage (h)))
00189     return "white";
00190   tree r (PATTERN, as_string (image), w, h);
00191   if (N(t) == 4) r << evaluate (t[3]);
00192   return r;
00193 }
00194 
00195 tree
00196 evaluate_point (tree t) {
00197   int i, n= N(t);
00198   tree u (_POINT, n);
00199   for (i=0; i<n; i++)
00200     u[i]= evaluate (t[i]);
00201   if (n==0 || is_double (u[0])) return u;
00202   return as_tree (as_point (u));
00203 }
00204 
00205 /*
00206 tree
00207 evaluate_box_info (tree t) {
00208   tree t1= t[0];
00209   tree t2= t[1];
00210   if (!is_string (t2))
00211     return tree (ERROR, "bad box info");
00212   return box_info (edit_env (this), t1, as_string (t2));
00213 }
00214 
00215 tree
00216 evaluate_frame_direct (tree t) {
00217   tree t1= evaluate (t[0]);
00218   return as_tree (!nil (fr) ? fr (::as_point (t1)) : point ());
00219 }
00220 
00221 tree
00222 evaluate_frame_inverse (tree t) {
00223   tree t1= evaluate (t[0]);
00224   return as_tree (!nil (fr) ? fr [::as_point (t1)] : point ());
00225 }
00226 */