~ubuntu-branches/ubuntu/hardy/texmacs/hardy

« back to all changes in this revision

Viewing changes to plugins/maple/src/maple_filter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Treinen
  • Date: 2004-04-19 20:34:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040419203400-g4e34ih0315wcn8v
Tags: upstream-1.0.3-R2
ImportĀ upstreamĀ versionĀ 1.0.3-R2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <string.h>
 
5
#include <iostream>
 
6
#include <string>
 
7
using namespace std;
 
8
 
 
9
/*#define DATA_BEGIN   ((string) "debut" )
 
10
#define DATA_END     ((string) "fin" )
 
11
#define DATA_ESCAPE  ((string) "escape" )*/
 
12
 
 
13
#define DATA_BEGIN   ((char) 2)
 
14
#define DATA_END     ((char) 5)
 
15
#define DATA_ESCAPE  ((char) 27)
 
16
 
 
17
static int counter= 0;
 
18
 
 
19
void
 
20
next_input () {
 
21
  counter++;
 
22
  cout << DATA_BEGIN << "channel:prompt" << DATA_END;
 
23
  cout << DATA_BEGIN << "latex:\\brown Maple " << counter
 
24
       << "]\\ " << DATA_END;
 
25
}
 
26
 
 
27
string
 
28
remplacer(string in, string what, string by) {
 
29
  string result= in;
 
30
  while (result.find(what)!=string::npos) {
 
31
    result= result.replace (result.find(what), what.length(), by);
 
32
  }
 
33
  return result;
 
34
}
 
35
 
 
36
void
 
37
passer (string &s, int indentation) {
 
38
  s= remplacer (s, "\\textbf{proc}", "\\proc");  
 
39
  s= remplacer (s, "\\textbf{end proc}", "\\endproc \n");
 
40
  s= remplacer (s, "\\textbf{if}", "\\if");  
 
41
  s= remplacer (s, "\\textbf{then}", "\\then"); 
 
42
  s= remplacer (s, "\\textbf{else}", "\\else");         
 
43
  s= remplacer (s, "\\textbf{end if}", "\\endif");
 
44
  
 
45
  s= remplacer(s, "\\,", "\\mult ");
 
46
  // l'espace correspond souvent a une multiplication,
 
47
  // pas toujours malheureusement!
 
48
  s= remplacer(s, "\\ast ", "\\mult ");//       * =\\mult
 
49
  // s= remplacer(s,"\\mathrm","\\mathrom");
 
50
  // Joris devrait arranger ce probleme de conversion LaTeX
 
51
  // a moins que je ne trouve qqc
 
52
  // Maple est un peu incoherent a ce sujet
 
53
  s= remplacer (s, "{\\displaystyle ", "\\dsp{ ");
 
54
  //on peut faire mieux?
 
55
 
 
56
  s= remplacer (s, "(", " \\bigpl ");
 
57
  //les parentheses deviennent modulables cf session_maple.ts
 
58
  s= remplacer (s, ")", " \\bigpr ");
 
59
  //modulables cf session_maple.ts
 
60
  
 
61
  if (indentation == 0) {
 
62
    s= remplacer (s, "\\maplemultiline{","\\hfill $");
 
63
    //hfill donne un prob avec les procedures qui sont indentees
 
64
    s= remplacer (s, "\\\\","$\\hfill\\  \\cr \\  \\hfill $ ");
 
65
    //idem: prob avec les procedures
 
66
  }
 
67
  else s= remplacer (s, "\\maplemultiline{","$");
 
68
  cout <<s;
 
69
}
 
70
 
 
71
int
 
72
search_matching (char entree[], int &niveau) {
 
73
  int pos=0;
 
74
  while (pos<=strlen(entree)) {
 
75
    switch(entree[pos]) {
 
76
    case '{': niveau++;break; 
 
77
    case '}': niveau--;break;
 
78
    }
 
79
    if (niveau==0) break;pos++;
 
80
  }
 
81
  return pos;
 
82
}
 
83
 
 
84
int
 
85
main() {
 
86
  int c,d;
 
87
  const int max_length=1000;
 
88
  char buffer[max_length];
 
89
 
 
90
  /* passer l'annonce et afficher le premier prompt */
 
91
  /*lire le premier*/
 
92
  c=0;
 
93
  cout << DATA_BEGIN << "verbatim:";
 
94
  cout << "    |\\^/|     Maple \n";
 
95
  cout << "._|\\|   |/|_. Copyright by Waterloo Maple Inc\n";
 
96
  cout << " \\  MAPLE  /  All rights reserved. Maple is a registered trademark of\n";
 
97
  cout << " <____ ____>  Waterloo Maple Inc.\n";
 
98
  cout << "      |       Type ? for help.\n";
 
99
  cout << "\n";
 
100
  cout << "Interface with TeXmacs by Christian Even (c) 2002.\n";
 
101
  next_input ();
 
102
  cout << DATA_END;
 
103
  fflush(stdout);
 
104
 
 
105
  /* lecture de l'annonce et premiere entree */
 
106
  while(1) {
 
107
    cin.getline(buffer,max_length);
 
108
    if (strcmp (buffer,"\\begin{mapleinput}" ) == 0) break;
 
109
  } /*while*/
 
110
 
 
111
  /*boucle principale*/
 
112
  while (true) { 
 
113
    c=0;
 
114
    while(true) { /*boucle entree/sortie*/
 
115
      int i,j,multi;    
 
116
      cin.getline(buffer,max_length);
 
117
      string debut(buffer,0,11);
 
118
        
 
119
      // comment lire l'aide en ligne?
 
120
      // if (buffer[0]='?') {
 
121
      //   lire tout sans convertir en string jusqu'a \begin{mapleinput}  ???} 
 
122
      // else
 
123
 
 
124
      if (debut=="\\mapleplot{" ) {
 
125
        c=0;
 
126
        i=11;
 
127
        while (buffer[i] != '}') i++;
 
128
        string nomduplot(buffer,11,i-11);
 
129
        string message="cat $HOME/" + nomduplot;
 
130
        cout << DATA_BEGIN << "latex:\\hfill" << DATA_END;
 
131
        cout << DATA_BEGIN << "ps:";
 
132
        fflush(stdout);
 
133
        
 
134
        i=0;
 
135
        while (i<=100000000) i++;
 
136
        // boucle de temporisation: a ameliorer!!!!!
 
137
        // il faudrait tester l'arrivee du plot.eps
 
138
 
 
139
        system(message.c_str());
 
140
        cout << DATA_END;
 
141
        cout << DATA_BEGIN << "latex:\\hfill" << DATA_END;               
 
142
        cout <<"\n";
 
143
      }
 
144
      else if (strcmp (buffer, "quit;" ) == 0) break;
 
145
      else if (strcmp (buffer, "quit" ) == 0) break;
 
146
      else if (strcmp (buffer,"\\begin{mapleinput}" ) == 0) { c=0; break; }
 
147
      else if (strcmp (buffer,"\\end{mapleinput}" ) == 0) {
 
148
        cout << DATA_BEGIN << "verbatim:"; c=1; }
 
149
 
 
150
      else if (strcmp (buffer,"\\begin{maplelatex}" ) == 0) {
 
151
        c=1;
 
152
        cout << DATA_BEGIN << "latex:";
 
153
        cin.getline(buffer,max_length);
 
154
        while (strcmp(buffer,"\\end{maplelatex}" ) != 0) {
 
155
          if (strcmp (buffer,"\\maplemultiline{" ) == 0) {
 
156
            int niveau =1;
 
157
            int pos;
 
158
            int indentation=0;
 
159
            string sortielatex="\\maplemultiline{";
 
160
            niveau=1;
 
161
            cin.getline (buffer, max_length);
 
162
            pos=search_matching (buffer, niveau);
 
163
            while (true) {
 
164
              pos= search_matching (buffer, niveau);
 
165
              if (niveau==0) break;
 
166
              sortielatex= sortielatex+buffer;
 
167
              cin.getline (buffer, max_length);
 
168
            } // fin du while
 
169
            string debut (buffer, 0, pos);
 
170
            sortielatex=sortielatex+ debut;
 
171
            if (sortielatex.find ("\\textbf{proc}") != string::npos)
 
172
              indentation =1;
 
173
            // cout <<"l'indentation vaut "<< indentation <<"\n";
 
174
            passer (sortielatex, indentation);
 
175
            // string fin(buffer,pos+1,max_length-pos);
 
176
            cout << "$\\hfill \\cr";
 
177
            cin.getline (buffer, max_length);
 
178
          }     
 
179
          /* else {string sortielatex(buffer,0,strlen(buffer));
 
180
             passer(sortielatex,0);
 
181
             cin.getline(buffer,max_length);}; */
 
182
          else {
 
183
            string sortielatex(buffer,0,strlen(buffer));
 
184
            while (strcmp (buffer,"\\]" ) != 0) {
 
185
              cin.getline(buffer,max_length);
 
186
              string suite(buffer,0,strlen(buffer));    
 
187
              sortielatex=sortielatex+suite;
 
188
            }
 
189
            passer(sortielatex,0);
 
190
            cin.getline(buffer,max_length);
 
191
          }
 
192
        } // fin du while latex
 
193
        cout <<DATA_END;
 
194
      }
 
195
 
 
196
      // else if (strcmp (buffer,"\\end{maplelatex}" ) == 0) cout << DATA_END;
 
197
      else if (strcmp (buffer," \\[ " ) == 0) cout << buffer;
 
198
      else if (strcmp (buffer," \\] " ) == 0) cout << buffer;
 
199
      else if (strcmp (buffer,"\\begin{maplettyout}" ) == 0) c= 1;
 
200
      else if (strcmp (buffer,"\\end{maplettyout}" ) == 0) c= 1;
 
201
      else if (c==1)  fputs(buffer,stdout); fputs("\n",stdout);
 
202
      // cout << buffer;
 
203
    } /* fin de la boucle entree/sortie* */
 
204
 
 
205
    if (strcmp (buffer, "quit" ) == 0) {
 
206
      cout << DATA_END << DATA_BEGIN
 
207
           << "latex: \\red $$\\mathrm{La session Maple est termin\\'ee}$$";
 
208
      cout << DATA_END;
 
209
      cout << DATA_ESCAPE;fflush(stdout);break;
 
210
    }
 
211
 
 
212
    if (!cin) {
 
213
      cout<< "La session Maple est arretee.3";
 
214
      cout << DATA_ESCAPE;
 
215
      cout << DATA_END;    
 
216
      fflush(stdout);
 
217
      break;
 
218
    }
 
219
 
 
220
    if (strcmp (buffer, "quit;" ) == 0) {
 
221
      cout << "La session est arretee.1";
 
222
      cout << DATA_ESCAPE;
 
223
      cout << DATA_END;
 
224
      fflush(stdout);
 
225
      break;
 
226
    }
 
227
    
 
228
    next_input ();
 
229
    cout << DATA_END;
 
230
    fflush(stdout);
 
231
  } // fin de la boucle  principale
 
232
 
 
233
  return 0;
 
234
} // main