Ilya Zakharevich on Sun, 23 Feb 2003 00:25:29 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PATCH CVS] log files, TeX, colors


This patch:

a) Emits correct colors for prompt and user input to the logfile;

b) makes emiting colors to logfile optional (use default(log,2) to emit colors);

c) allows writing a logfile as a TeX file (use default(log,3));

d) Allows changing the style of TeX output (\frac vs \over [to simplify
   parsing]), \left/\right pairs, insertion of \PARIbreak after "monomials"
   by setting bits in default(TeXstyle);

e) Corrects an omission in ?\ ;

f) splits TeX output for polynomials into several lines;

Limitations:

 1) TeX output to logfiles is enabled only with \o3 (should be easy to fix);

 2) My TeX was never too stellar; please check whether you can improve the
    default macros;

      Try

	default(log,3)
	default(seriesprecision,9)
	sin(x+y)

      or

	default(log,3)
	default(TeXstyle,2)
	sin(x+y)

 3) log file is always written as if \PARIbreak were user-enabled;

 4) tex does not like it very much when TeX files have an extension .log.
    I changed my default so that it is .plog:

	logfile = "~/pari-logs/day-%Y-%m-%d=time-%H-%M-%S.plog"

Enjoy,
Ilya

diff -pru pari/src/gp/gp.c pari-my-200302/src/gp/gp.c
--- pari/src/gp/gp.c	Sat Feb 22 18:58:44 2003
+++ pari-my-200302/src/gp/gp.c	Sat Feb 22 23:49:34 2003
@@ -720,11 +720,18 @@ sd_histsize(const char *v, int flag)
 static GEN
 sd_log(const char *v, int flag)
 {
-  ulong old = GP_DATA->flags;
-  GEN r = sd_gptoggle(v,flag,"log",LOG);
-  if (GP_DATA->flags != old)
+  static const char * const msg[] = {
+      "(off)",
+      "(on)",
+      "(on with colors)",
+      "(TeX output)", NULL
+  };
+  ulong oldstyle = logstyle;
+  GEN res = sd_ulong(v,flag,"log", &logstyle, 0, 3, (char**)msg);
+
+  if (!oldstyle != !logstyle)		/* Compare converts to boolean */
   { /* toggled LOG */
-    if (old & LOG)
+    if (oldstyle)
     { /* close log */
       if (flag == d_ACKNOWLEDGE)
         pariputsf("   [logfile was \"%s\"]\n", current_logfile);
@@ -737,9 +744,30 @@ sd_log(const char *v, int flag)
 #ifndef WINCE
       setbuf(logfile,(char *)NULL);
 #endif
+      if (logfile && logstyle == logstyle_TeX) { /* Default values */
+	  fprintf(logfile, "%s%s%s%s%s%s%s%s%s%s\n",
+		  "\\ifx \\PARIbreak\\undefined\n",
+		  "  \\def\\PARIbreak{\\hskip 0pt plus \\hsize\\relax\\discretionary{}{}{}}\\fi\n",
+		  "\\ifx \\PARIpromptSTART\\undefined\n",
+		  "  \\def\\PARIpromptSTART|{\\vskip\\medskipamount\\bgroup\\bf}\\fi\n",
+		  "\\ifx \\PARIpromptEND\\undefined\n",
+		  "  \\def\\PARIpromptEND|{\\egroup\\bgroup\\tt}\\fi\n",
+		  "\\ifx \\PARIinputEND\\undefined\n",
+		  "  \\def\\PARIinputEND|{\\egroup}\\fi\n",
+		  "\\ifx \\PARIout\\undefined\n",
+		  "  \\def\\PARIout#1#2{\\vskip\\smallskipamount$\\displaystyle{\\tt\\%#1} = #2$}\\fi\n");
+      }
     }
   }
-  return r;
+  return res;
+}
+
+static GEN
+sd_TeXstyle(const char *v, int flag)
+{
+  static const char * const msg[] = { NULL,
+	"(bits 0x1/0x2/0x4 control output of \\frac/\\left/\\PARIbreak)"};
+  return sd_ulong(v,flag,"TeXstyle", &TeXstyle, 0, 7, (char**)msg);
 }
 
 static GEN
@@ -980,6 +1008,7 @@ default_type gp_default_list[] =
   {"seriesprecision",(void*)sd_seriesprecision},
   {"simplify",(void*)sd_simplify},
   {"strictmatch",(void*)sd_strictmatch},
+  {"TeXstyle",(void *)sd_TeXstyle},
   {"timer",(void *)sd_timer},
   {NULL,NULL} /* sentinel */
 };
@@ -1301,7 +1330,7 @@ slash_commands(void)
 \\h {m-n}: hashtable information\n\
 \\l {f}  : enable/disable logfile (set logfile=f)\n\
 \\m {n}  : print result in prettymatrix format\n\
-\\o {n}  : change output method (0=raw, 1=prettymatrix, 2=prettyprint)\n\
+\\o {n}  : change output method (0=raw, 1=prettymatrix, 2=prettyprint, 3=2-dim)\n\
 \\p {n}  : change real precision\n\
 \\ps{n}  : change series precision\n\
 \\q      : quit completely this GP session\n\
@@ -2439,7 +2468,15 @@ get_line_from_file(char *prompt, filtre_
     if (GP_DATA->flags & ECHO)
       { pariputs(prompt); pariputs(s); pariputc('\n'); }
     else
-      if (logfile) fprintf(logfile, "%s%s\n",prompt,s);
+      if (logfile) {
+	if (logstyle == logstyle_TeX)
+	  fprintf(logfile,
+		  "\\PARIpromptSTART|%s\\PARIpromptEND|%s\\PARIinputEND|%%\n",
+		  prompt,s);
+	else
+	  fprintf(logfile, "%s%s\n",prompt,s);
+      }
+    
     pariflush();
   }
   if (GP_DATA->flags & TEXMACS) 
diff -pru pari/src/gp/gp_rl.c pari-my-200302/src/gp/gp_rl.c
--- pari/src/gp/gp_rl.c	Sun Jan 12 16:32:50 2003
+++ pari-my-200302/src/gp/gp_rl.c	Sat Feb 22 23:41:28 2003
@@ -912,7 +912,22 @@ get_line_from_readline(char *prompt, cha
     }
 
     /* update logfile */
-    if (logfile) fprintf(logfile, "%s%s\n",bare_prompt,s);
+    switch (logstyle) {
+    case logstyle_TeX:
+	fprintf(logfile,
+		"\\PARIpromptSTART|%s\\PARIpromptEND|%s\\PARIinputEND|%%\n",
+		bare_prompt,s);
+	break;
+    case logstyle_plain:
+	fprintf(logfile, "%s%s\n",bare_prompt,s);
+	break;
+    case logstyle_color:
+	/* Can't do in one pass, since term_get_color() returns a static */
+	fprintf(logfile, "%s%s", term_get_color(c_PROMPT), bare_prompt);
+	fprintf(logfile, "%s%s", term_get_color(c_INPUT), s);
+	fprintf(logfile, "%s\n", term_get_color(c_NONE));
+	break;
+    }
   }
   unblock_SIGINT(); /* bug in readline 2.0: need to unblock ^C */
   return 1;
diff -pru pari/src/headers/paristio.h pari-my-200302/src/headers/paristio.h
--- pari/src/headers/paristio.h	Sun Jan 12 16:32:52 2003
+++ pari-my-200302/src/headers/paristio.h	Sat Feb 22 23:13:30 2003
@@ -58,6 +58,18 @@ typedef struct pariFILE {
 
 extern PariOUT *pariOut, *pariErr;
 extern FILE    *pari_outfile, *logfile, *infile, *errfile;
+extern ulong    logstyle, TeXstyle;
+
+enum logstyles {
+    logstyle_none,			/* 0 */
+    logstyle_plain,			/* 1 */
+    logstyle_color,			/* 2 */
+    logstyle_TeX,			/* 3 */
+};
+
+#define TEXSTYLE_FRAC	1
+#define TEXSTYLE_PAREN	2
+#define TEXSTYLE_BREAK	4
 
 extern pari_sp avma,bot,top;
 extern size_t memused;
diff -pru pari/src/language/anal.h pari-my-200302/src/language/anal.h
--- pari/src/language/anal.h	Sun Jan 12 16:32:56 2003
+++ pari-my-200302/src/language/anal.h	Sat Feb 22 23:58:44 2003
@@ -279,11 +279,12 @@ extern void bruti(GEN g, pariout_t *T, i
 extern void matbruti(GEN g, pariout_t *T);
 extern void sori(GEN g, pariout_t *T);
 extern void texi(GEN g, pariout_t *T, int nosign);
+extern void texi_nobrace(GEN g, pariout_t *T, int nosign);
 extern pariout_t DFLT_OUTPUT;
 
 /* GP_DATA->flags */
 enum { QUIET=1, TEST=2, SIMPLIFY=4, CHRONO=8, ECHO=16, STRICTMATCH=32,
-       USE_READLINE=64, SECURE=128, EMACS=256, TEXMACS=512, LOG=1024};
+       USE_READLINE=64, SECURE=128, EMACS=256, TEXMACS=512};
 /* GP */
 #define pariputs_opt(s) if (!(GP_DATA->flags & QUIET)) pariputs(s)
 
diff -pru pari/src/language/es.c pari-my-200302/src/language/es.c
--- pari/src/language/es.c	Sat Feb 22 18:58:50 2003
+++ pari-my-200302/src/language/es.c	Sun Feb 23 00:06:12 2003
@@ -317,7 +317,12 @@ pariputsf(const char *format, ...)
 void
 term_color(int c)
 {
+  FILE *o_logfile = logfile;
+
+  if (logstyle != logstyle_color)
+      logfile = 0;				/* Ugly hack... */
   pariputs(term_get_color(c));
+  logfile = o_logfile;
 }
 
 void
@@ -1457,13 +1462,25 @@ wr_texnome(pariout_t *T, GEN a, const ch
     if (sig) { putsigne(sig); texi(a,T,sig); }
     else
     {
-      pariputs(" + \\left("); texi(a,T,sig); pariputs("\\right) ");
+      if (TeXstyle & TEXSTYLE_PAREN)
+        pariputs(" + (");
+      else
+	pariputs(" + \\left(");
+      texi_nobrace(a,T,sig);		/* With braces no auto-linebreaks */
+      if (TeXstyle & TEXSTYLE_PAREN)
+        pariputs(") ");
+      else
+	pariputs("\\right) ");
     }
     if (d)
     {
       if (GP_DATA && (GP_DATA->flags & TEXMACS)) pariputs("\\*");
       texnome(v,d);
     }
+    if (TeXstyle & TEXSTYLE_BREAK)
+      pariputs("\\PARIbreak ");
+    if (!sig)
+      pariputc('\n');			/* Avoid TeX buffer overflow */
   }
 }
 
@@ -1501,13 +1518,23 @@ wr_lead_texnome(pariout_t *T, GEN a,cons
     if (isfactor(a)) texi(a,T,nosign);
     else
     {
-      pariputs(" \\left("); texi(a,T,0); pariputs("\\right) ");
+      if (TeXstyle & TEXSTYLE_PAREN)
+        pariputs(" (");
+      else
+        pariputs(" \\left(");
+      texi_nobrace(a,T,0);		/* With braces no auto-linebreaks */
+      if (TeXstyle & TEXSTYLE_PAREN)
+        pariputs(") ");
+      else
+	pariputs("\\right) ");
     }
     if (d)
     {
       if (GP_DATA && (GP_DATA->flags & TEXMACS)) pariputs("\\*");
       texnome(v,d);
     }
+    if (TeXstyle & TEXSTYLE_BREAK)
+      pariputs("\\PARIbreak ");
   }
 }
 
@@ -1909,17 +1936,25 @@ sori(GEN g, pariout_t *T)
 void
 texi(GEN g, pariout_t *T, int nosign)
 {
+    pariputc('{');
+    texi_nobrace(g, T, nosign);
+    pariputc('}');
+}
+
+void
+texi_nobrace(GEN g, pariout_t *T, int nosign)
+{
   long tg,i,j,l,r;
   GEN a,b;
   const char *v;
   char buf[67];
 
-  if (isnull(g)) { pariputs("{0}"); return; }
-  r = isone(g); pariputc('{');
+  if (isnull(g)) { pariputc('0'); return; }
+  r = isone(g);
   if (r)
   {
     if (!nosign && r<0) pariputc('-');
-    pariputs("1}"); return;
+    pariputs("1"); return;
   }
 
   tg = typ(g);
@@ -1934,7 +1969,11 @@ texi(GEN g, pariout_t *T, int nosign)
       texi((GEN)g[1],T,0); break;
 
     case t_FRAC: case t_FRACN: case t_RFRAC: case t_RFRACN:
-      texi((GEN)g[1],T,nosign); pariputs("\\over");
+      if (TeXstyle & TEXSTYLE_FRAC)
+	pariputs("\\frac");		/* Assume that texi() puts braces */
+      texi((GEN)g[1],T,nosign); 
+      if (!(TeXstyle & TEXSTYLE_FRAC))
+	  pariputs("\\over");
       texi((GEN)g[2],T,0); break;
 
     case t_COMPLEX: case t_QUAD: r = (tg==t_QUAD);
@@ -2059,7 +2098,6 @@ texi(GEN g, pariout_t *T, int nosign)
       }
       pariputc('}'); break;
   }
-  pariputc('}');
 }
 
 /*******************************************************************/
@@ -2131,8 +2169,25 @@ tex2mail_output(GEN z, long n)
     else
       sprintf(s, "\\%%%ld = ", n);
     pariputs_opt(s);
-    if (o_logfile)
-	fprintf(o_logfile, "%%%ld = ", n);
+    if (o_logfile) {
+	switch (logstyle) {
+	case logstyle_plain:
+	  plain_out:
+	    fprintf(o_logfile, "%%%ld = ", n);
+	    break;
+	case logstyle_color:
+	    if (!n)
+		goto plain_out;
+	    fprintf(o_logfile, "%s%%%ld = ", term_get_color(c_HIST), n);
+	    /* Can't merge, term_get_color() uses statics...: */
+	    fprintf(o_logfile, "%s", term_get_color(c_OUTPUT));
+	    break;
+	case logstyle_TeX:
+	    fprintf(o_logfile, "\\PARIout{%ld}", n);
+	    break;
+	    
+	}
+    }    
   }
   /* output */
   gen_output(z, &T);
@@ -2143,7 +2198,27 @@ tex2mail_output(GEN z, long n)
   if (o_logfile) {
     pari_outfile = o_logfile;
     /* XXXX Maybe it is better to output in another format? */
-    outbrute(z); pariputc('\n'); pariflush();
+    if (logstyle == logstyle_TeX) {
+	int extrabraces = 0;
+	ulong o_style = TeXstyle;
+
+	switch (typ(z)) {
+	case t_FRAC: case t_FRACN: case t_RFRAC: case t_RFRACN:
+	    if (!(TeXstyle & TEXSTYLE_FRAC))
+		/* Extra braces disable line breaks, avoid them if possible */
+		extrabraces = 1;
+	}
+	if (extrabraces)
+	  pariputc('{');
+	TeXstyle |= TEXSTYLE_BREAK;
+	outtex(z);
+	if (extrabraces)
+	  pariputc('}');
+	pariputc('%');
+	TeXstyle = o_style;
+    } else
+	outbrute(z);
+    pariputc('\n'); pariflush();
   }
   logfile = o_logfile;
   pari_outfile = o_out;
diff -pru pari/src/language/init.c pari-my-200302/src/language/init.c
--- pari/src/language/init.c	Sat Feb 22 18:58:50 2003
+++ pari-my-200302/src/language/init.c	Sat Feb 22 19:58:36 2003
@@ -29,6 +29,7 @@ Foundation, Inc., 59 Temple Place - Suit
 
 /*      Variables statiques communes :         */
 FILE    *pari_outfile, *errfile, *logfile, *infile;
+ulong   logstyle, TeXstyle;
 GEN     *polun, *polx;
 GEN     gnil, gzero, gun, gdeux, ghalf, polvar, gi;
 GEN     gpi=NULL, geuler=NULL, bernzone=NULL;