Back to index

texmacs  1.0.7.15
modification.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : modification.hpp
00004 * DESCRIPTION: elementary tree modifications
00005 * COPYRIGHT  : (C) 2008  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 #ifndef MODIFICATION_H
00013 #define MODIFICATION_H
00014 #include "tree.hpp"
00015 #include "path.hpp"
00016 
00017 /******************************************************************************
00018 * Elementary tree modifications
00019 ******************************************************************************/
00020 
00021 #define MOD_ASSIGN       1
00022 #define MOD_INSERT       2
00023 #define MOD_REMOVE       3
00024 #define MOD_SPLIT        4
00025 #define MOD_JOIN         5
00026 #define MOD_ASSIGN_NODE  6
00027 #define MOD_INSERT_NODE  7
00028 #define MOD_REMOVE_NODE  8
00029 #define MOD_SET_CURSOR   9
00030 
00031 /******************************************************************************
00032 * The modification class
00033 ******************************************************************************/
00034 
00035 typedef int modification_type;
00036 class modification;
00037 
00038 class modification_rep: concrete_struct {
00039 public:
00040   modification_type k;
00041   path p;
00042   tree t;
00043 
00044 public:
00045   inline modification_rep (modification_type k2, path p2):
00046     k (k2), p (p2) {}
00047   inline modification_rep (modification_type k2, path p2, tree t2):
00048     k (k2), p (p2), t (t2) {}
00049   inline ~modification_rep () {}
00050   friend class modification;
00051 };
00052 
00053 class modification {
00054 CONCRETE(modification);
00055   inline modification (modification_type k, path p):
00056     rep (tm_new<modification_rep> (k, p)) {}
00057   inline modification (modification_type k, path p, tree t):
00058     rep (tm_new<modification_rep> (k, p, t)) {}
00059 };
00060 CONCRETE_CODE(modification);
00061 
00062 bool operator == (modification m1, modification m2);
00063 bool operator != (modification m1, modification m2);
00064 tm_ostream& operator << (tm_ostream& out, modification mod);
00065 
00066 /******************************************************************************
00067 * Constructors and accessors
00068 ******************************************************************************/
00069 
00070 inline modification mod_assign (path p, tree t) {
00071   return modification (MOD_ASSIGN, p, t); }
00072 inline modification mod_insert (path p, int pos, tree t) {
00073   return modification (MOD_INSERT, p * pos, t); }
00074 inline modification mod_remove (path p, int pos, int nr) {
00075   return modification (MOD_REMOVE, p * path (pos, nr)); }
00076 inline modification mod_split (path p, int pos, int at) {
00077   return modification (MOD_SPLIT, p * path (pos, at)); }
00078 inline modification mod_join (path p, int pos) {
00079   return modification (MOD_JOIN, p * pos); }
00080 inline modification mod_assign_node (path p, tree_label lab) {
00081   return modification (MOD_ASSIGN_NODE, p, tree (lab)); }
00082 inline modification mod_insert_node (path p, int pos, tree t) {
00083   return modification (MOD_INSERT_NODE, p * pos, t); }
00084 inline modification mod_remove_node (path p, int pos) {
00085   return modification (MOD_REMOVE_NODE, p * pos); }
00086 inline modification mod_set_cursor (path p, int pos, tree data) {
00087   return modification (MOD_SET_CURSOR, p * pos, data); }
00088 inline modification operator * (int i, modification mod) {
00089   return modification (mod->k, path (i, mod->p), mod->t); }
00090 inline modification operator * (path p, modification mod) {
00091   return modification (mod->k, p * mod->p, mod->t); }
00092 inline modification operator * (modification mod, int i) {
00093   return modification (mod->k, mod->p * i, mod->t); }
00094 inline modification operator / (modification mod, path p) {
00095   return modification (mod->k, mod->p / p, mod->t); }
00096 inline modification copy (modification mod) {
00097   return modification (mod->k, copy (mod->p), copy (mod->t)); }
00098 
00099 path root (modification mod);
00100 int index (modification mod);
00101 int argument (modification mod);
00102 tree_label L (modification mod);
00103 
00104 /******************************************************************************
00105 * Further routines on modifications
00106 ******************************************************************************/
00107 
00108 bool is_applicable (tree t, modification mod);
00109 tree clean_apply (tree t, modification mod);
00110 void raw_apply (tree& t, modification mod);      // in observer.cpp
00111 void apply (tree& t, modification mod);          // in observer.cpp
00112 
00113 /******************************************************************************
00114 * Hooks
00115 ******************************************************************************/
00116 
00117 void edit_announce (editor_rep* ed, modification mod);
00118 void edit_done (editor_rep* ed, modification mod);
00119 void edit_touch (editor_rep* ed, path p);
00120 void archive_announce (archiver_rep* buf, modification mod);
00121 void link_announce (observer obs, modification mod);
00122 
00123 #endif // defined MODIFICATION_H