Source

xemacs-beta / src / print.c

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
/* Lisp object printing and output streams.
   Copyright (C) 1985, 1986, 1988, 1992-1995 Free Software Foundation, Inc.
   Copyright (C) 1995, 1996 Ben Wing.

This file is part of XEmacs.

XEmacs is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

XEmacs is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with XEmacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* Synched up with: Not synched with FSF. */

/* This file has been Mule-ized. */

/* Seriously hacked on by Ben Wing for Mule. */

#include <config.h>
#include "lisp.h"

#ifndef standalone
#include "backtrace.h"
#include "buffer.h"
#include "bytecode.h"
#include "console-tty.h"
#include "console-stream.h"
#include "extents.h"
#include "frame.h"
#include "emacsfns.h"
#include "insdel.h"
#include "lstream.h"

#endif /* not standalone */

Lisp_Object Vstandard_output, Qstandard_output;

/* The subroutine object for external-debugging-output is kept here
   for the convenience of the debugger.  */
Lisp_Object Qexternal_debugging_output;
Lisp_Object Qalternate_debugging_output;
 
/* Avoid actual stack overflow in print.  */
static int print_depth;

/* Maximum length of list or vector to print in full; noninteger means
   effectively infinity */

Lisp_Object Vprint_length;
Lisp_Object Qprint_length;

/* Maximum length of string to print in full; noninteger means
   effectively infinity */

Lisp_Object Vprint_string_length;
Lisp_Object Qprint_string_length;

/* Maximum depth of list to print in full; noninteger means
   effectively infinity.  */

Lisp_Object Vprint_level;

/* Label to use when making echo-area messages. */

Lisp_Object Vprint_message_label;

/* Nonzero means print newlines in strings as \n.  */

int print_escape_newlines;
int print_readably;
int print_gensym;

Lisp_Object Qprint_escape_newlines;
Lisp_Object Qprint_readably;

/* Force immediate output of all printed data.  Used for debugging. */
int print_unbuffered;

FILE *termscript;	/* Stdio stream being used for copy of all output.  */



int stdout_needs_newline;

/* Write a string (in internal format) to stdio stream STREAM. */
   
void
write_string_to_stdio_stream (FILE *stream, struct console *con,
			      CONST Bufbyte *str,
			      Bytecount offset, Bytecount len,
			      enum external_data_format fmt)
{
  int extlen;
  CONST Extbyte *extptr;

  GET_CHARPTR_EXT_DATA_ALLOCA (str + offset, len, fmt, extptr, extlen);
  if (stream)
    fwrite (extptr, 1, extlen, stream);
  else
    {
      assert (CONSOLE_TTY_P (con));
      Lstream_write (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream),
		     extptr, extlen);
    }
  if (stream == stdout || stream == stderr ||
      (!stream && CONSOLE_TTY_DATA (con)->is_stdio))
    {
      if (termscript)
	{
	  fwrite (extptr, 1, extlen, termscript);
	  fflush (termscript);
	}
      stdout_needs_newline = (extptr[extlen - 1] != '\n');
    }
}

/* Write a string to the output location specified in FUNCTION.
   Arguments NONRELOC, RELOC, OFFSET, and LEN are as in
   buffer_insert_string_1() in insdel.c. */

static void
output_string (Lisp_Object function, CONST Bufbyte *nonreloc,
	       Lisp_Object reloc, Bytecount offset, Bytecount len)
{
  /* This function can GC */
  Charcount ccoff, cclen;
  /* We change the value of nonreloc (fetching it from reloc as
     necessary), but we don't want to pass this changed value on to
     other functions that take both a nonreloc and a reloc, or things
     may get confused and an assertion failure in
     fixup_internal_substring() may get triggered. */
  CONST Bufbyte *newnonreloc = nonreloc;
  struct gcpro gcpro1, gcpro2;

  /* Emacs won't print whilst GCing, but an external debugger might */
  if (gc_in_progress) return;

  /* Perhaps not necessary but probably safer. */
  GCPRO2 (function, reloc);

  fixup_internal_substring (newnonreloc, reloc, offset, &len);

  if (STRINGP (reloc))
    newnonreloc = XSTRING_DATA (reloc);

  ccoff = bytecount_to_charcount (newnonreloc, offset);
  cclen = bytecount_to_charcount (newnonreloc + offset, len);

  if (LSTREAMP (function))
    {
      /* Lstream_write() could easily cause GC inside of it, if the
	 stream is a print-stream. (It will call output_string()
	 recursively.) This is probably the fastest way to fix this
	 problem. (alloca() is very fast on machines that have it
	 built-in, and you avoid some nasty problems with recursion
	 that could result from using a static buffer somewhere.)

	 The other possibility is to inhibit GC, but that of course
	 would require an unwind-protect, which is usually a lot
	 slower than the small amount of memcpy()ing that happens
	 here. */
      if (STRINGP (reloc))
	{
	  Bufbyte *copied = (Bufbyte *) alloca (len);
	  memcpy (copied, newnonreloc + offset, len);
	  Lstream_write (XLSTREAM (function), copied, len);
	}
      else
	Lstream_write (XLSTREAM (function), newnonreloc + offset, len);

      if (print_unbuffered)
	Lstream_flush (XLSTREAM (function));
    }

#ifndef standalone
  else if (BUFFERP (function))
    {
      CHECK_LIVE_BUFFER (function);
      buffer_insert_string (XBUFFER (function), nonreloc, reloc, offset, len);
    }
  else if (MARKERP (function))
    {
      /* marker_position will err if marker doesn't point anywhere */
      Bufpos spoint = marker_position (function);

      buffer_insert_string_1 (XBUFFER (Fmarker_buffer (function)),
			      spoint, nonreloc, reloc, offset, len,
			      0);
      Fset_marker (function, make_int (spoint + cclen),
		   Fmarker_buffer (function));
    }
  else if (FRAMEP (function))
    {
      struct frame *f = XFRAME (function);
      if (!EQ (Vprint_message_label, echo_area_status (f)))
	clear_echo_area_from_print (f, Qnil, 1);
      echo_area_append (f, nonreloc, reloc, offset, len, Vprint_message_label);
    }
#endif /* not standalone */
  else if (EQ (function, Qt) || EQ (function, Qnil))
    {
      write_string_to_stdio_stream (stdout, 0, newnonreloc, offset, len,
				    FORMAT_TERMINAL);
    }
  else
    {
      Charcount iii;

      for (iii = ccoff; iii < cclen + ccoff; iii++)
	{
	  call1 (function,
		 make_char (charptr_emchar_n (newnonreloc, iii)));
	  if (STRINGP (reloc))
	    newnonreloc = XSTRING_DATA (reloc);
	}
    }

  UNGCPRO;
}

struct print_stream
{
  FILE *file;
  Lisp_Object fun;
};

#define get_print_stream(stream) \
  ((struct print_stream *) Lstream_data (stream))

DEFINE_LSTREAM_IMPLEMENTATION ("print", lstream_print,
			       sizeof (struct print_stream));

static Lisp_Object
make_print_output_stream (FILE *file, Lisp_Object fun)
{
  Lstream *str = Lstream_new (lstream_print, "w");
  struct print_stream *ps = get_print_stream (str);
  Lisp_Object val = Qnil;

  Lstream_set_character_mode (str);
  ps->file = file;
  ps->fun = fun;
  XSETLSTREAM (val, str);
  return val;
}

/* #### This isn't being used anywhere at the moment.  Is it supposed
   to be? */
#if 0
static void
reset_print_stream (Lstream *str, FILE *file, Lisp_Object fun)
{
  struct print_stream *ps = get_print_stream (str);

  Lstream_reopen (str);
  ps->file = file;
  ps->fun = fun;
}
#endif

static Lisp_Object
print_marker (Lisp_Object obj, void (*markobj) (Lisp_Object))
{
  return get_print_stream (XLSTREAM (obj))->fun;
}

static int
print_writer (Lstream *stream, CONST unsigned char *data, int size)
{
  struct print_stream *ps = get_print_stream (stream);

  if (ps->file)
    {
      write_string_to_stdio_stream (ps->file, 0, data, 0, size,
				    FORMAT_TERMINAL);
      /* Make sure it really gets written now. */
      if (print_unbuffered)
	fflush (ps->file);
    }
  else
    output_string (ps->fun, data, Qnil, 0, size);
  return size;
}

static Lisp_Object
canonicalize_printcharfun (Lisp_Object printcharfun)
{
  if (NILP (printcharfun))
    printcharfun = Vstandard_output;

  if (EQ (printcharfun, Qt) || NILP (printcharfun))
    {
#ifndef standalone
      printcharfun = Fselected_frame (Qnil); /* print to minibuffer */
#endif
    }
  return (printcharfun);
}


static Lisp_Object
print_prepare (Lisp_Object printcharfun)
{
  FILE *stdio_stream = 0;

  /* Emacs won't print whilst GCing, but an external debugger might */
  if (gc_in_progress)
    return (Qnil);

  printcharfun = canonicalize_printcharfun (printcharfun);
  if (EQ (printcharfun, Qnil))
    {
      stdio_stream = stdout;
    }
#if 0 /* Don't bother */
  else if (SUBRP (indirect_function (printcharfun, 0))
           && (XSUBR (indirect_function (printcharfun, 0)) 
               == Sexternal_debugging_output))
    {
      stdio_stream = stderr;
    }
#endif
  
  return make_print_output_stream (stdio_stream, printcharfun);
}

static void
print_finish (Lisp_Object stream)
{
  /* Emacs won't print whilst GCing, but an external debugger might */
  if (gc_in_progress)
    return;

  Lstream_delete (XLSTREAM (stream));
}

#if 1 /* Prefer space over "speed" */
#define write_char_internal(string_of_length_1, stream) \
  write_string_1 ((CONST Bufbyte *) (string_of_length_1), 1, (stream))
#else
#define write_char_internal(string_of_length_1, stream) \
  output_string ((stream), (CONST Bufbyte *) (string_of_length_1), Qnil, 0, 1)
#endif

/* NOTE:  Do not call this with the data of a Lisp_String,
 *  as printcharfun might cause a GC, which might cause
 *  the string's data to be relocated.
 *  Use print_object_internal (string, printcharfun, 0)
 *  to princ a Lisp_String
 * Note: "stream" should be the result of "canonicalize_printcharfun"
 *  (ie Qnil means stdout, not Vstandard_output, etc)
 */
void
write_string_1 (CONST Bufbyte *str, Bytecount size, Lisp_Object stream)
{
  /* This function can GC */
  assert (size >= 0);
  output_string (stream, str, Qnil, 0, size);
}

void
write_c_string (CONST char *str, Lisp_Object stream)
{
  /* This function can GC */
  write_string_1 ((CONST Bufbyte *) str, strlen (str), stream);
}


DEFUN ("write-char", Fwrite_char, 1, 2, 0, /*
Output character CH to stream STREAM.
STREAM defaults to the value of `standard-output' (which see).
*/
       (ch, stream))
{
  /* This function can GC */
  Bufbyte str[MAX_EMCHAR_LEN];
  Bytecount len;

  CHECK_CHAR_COERCE_INT (ch);
  len = set_charptr_emchar (str, XCHAR (ch));
  output_string (canonicalize_printcharfun (stream), str, Qnil, 0, len);
  return ch;
}

#ifndef standalone

void
temp_output_buffer_setup (CONST char *bufname)
{
  /* This function can GC */
  struct buffer *old = current_buffer;
  Lisp_Object buf;

#ifdef I18N3
  /* #### This function should accept a Lisp_Object instead of a char *,
     so that proper translation on the buffer name can occur. */
#endif

  Fset_buffer (Fget_buffer_create (build_string (bufname)));

  current_buffer->read_only = Qnil;
  Ferase_buffer (Fcurrent_buffer ());

  XSETBUFFER (buf, current_buffer);
  specbind (Qstandard_output, buf);

  set_buffer_internal (old);
}

Lisp_Object
internal_with_output_to_temp_buffer (CONST char *bufname, 
                                     Lisp_Object (*function) (Lisp_Object arg),
                                     Lisp_Object arg, 
                                     Lisp_Object same_frame)
{
  int speccount = specpdl_depth ();
  struct gcpro gcpro1, gcpro2, gcpro3;
  Lisp_Object buf = Qnil;

  GCPRO3 (buf, arg, same_frame);

  temp_output_buffer_setup (GETTEXT (bufname));
  buf = Vstandard_output;

  arg = (*function) (arg);

  temp_output_buffer_show (buf, same_frame);
  UNGCPRO;

  return unbind_to (speccount, arg);
}

DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, 1, UNEVALLED, 0, /*
Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.
The buffer is cleared out initially, and marked as unmodified when done.
All output done by BODY is inserted in that buffer by default.
The buffer is displayed in another window, but not selected.
The value of the last form in BODY is returned.
If BODY does not finish normally, the buffer BUFNAME is not displayed.

If variable `temp-buffer-show-function' is non-nil, call it at the end
to get the buffer displayed.  It gets one argument, the buffer to display.
*/
       (args))
{
  /* This function can GC */
  struct gcpro gcpro1;
  Lisp_Object name;
  int speccount = specpdl_depth ();
  Lisp_Object buf, val;

#ifdef I18N3
  /* #### should set the buffer to be translating.  See print_internal(). */
#endif

  GCPRO1 (args);
  name = Feval (Fcar (args));
  UNGCPRO;

  CHECK_STRING (name);
  temp_output_buffer_setup ((char *) XSTRING_DATA (name));
  buf = Vstandard_output;

  val = Fprogn (Fcdr (args));

  temp_output_buffer_show (buf, Qnil);

  return unbind_to (speccount, val);
}
#endif /* not standalone */

DEFUN ("terpri", Fterpri, 0, 1, 0, /*
Output a newline to STREAM.
If STREAM is omitted or nil, the value of `standard-output' is used.
*/
       (stream))
{
  /* This function can GC */
  Bufbyte str[1];
  str[0] = '\n';
  output_string (canonicalize_printcharfun (stream), str, Qnil, 0, 1);
  return Qt;
}

DEFUN ("prin1", Fprin1, 1, 2, 0, /*
Output the printed representation of OBJECT, any Lisp object.
Quoting characters are printed when needed to make output that `read'
can handle, whenever this is possible.
Output stream is STREAM, or value of `standard-output' (which see).
*/
       (object, stream))
{
  /* This function can GC */
  Lisp_Object the_stream = Qnil;
  struct gcpro gcpro1, gcpro2, gcpro3;

  GCPRO3 (object, stream, the_stream);
  print_depth = 0;
  the_stream = print_prepare (stream);
  print_internal (object, the_stream, 1);
  print_finish (the_stream);
  UNGCPRO;
  return object;
}

/* a buffer which is used to hold output being built by prin1-to-string */
Lisp_Object Vprin1_to_string_buffer;

DEFUN ("prin1-to-string", Fprin1_to_string, 1, 2, 0, /*
Return a string containing the printed representation of OBJECT,
any Lisp object.  Quoting characters are used when needed to make output
that `read' can handle, whenever this is possible, unless the optional
second argument NOESCAPE is non-nil.
*/
       (object, noescape))
{
  /* This function can GC */
  Lisp_Object old = Fcurrent_buffer ();
  struct buffer *out = XBUFFER (Vprin1_to_string_buffer);
  Lisp_Object stream = Qnil;
  struct gcpro gcpro1, gcpro2, gcpro3;

  GCPRO3 (object, old, stream);
  stream = print_prepare (Vprin1_to_string_buffer);
  set_buffer_internal (out);
  Ferase_buffer (Fcurrent_buffer ());
  print_depth = 0;
  print_internal (object, stream, NILP (noescape));
  print_finish (stream);
  stream = Qnil;                /* No GC surprises! */
  object = make_string_from_buffer (out, 
				    BUF_BEG (out),
				    BUF_Z (out) - 1);
  Ferase_buffer (Fcurrent_buffer ());
  Fset_buffer (old);
  UNGCPRO;
  return (object);
}

DEFUN ("princ", Fprinc, 1, 2, 0, /*
Output the printed representation of OBJECT, any Lisp object.
No quoting characters are used; no delimiters are printed around
the contents of strings.
Output stream is STREAM, or value of standard-output (which see).
*/
       (obj, stream))
{
  /* This function can GC */
  Lisp_Object the_stream = Qnil;
  struct gcpro gcpro1, gcpro2, gcpro3;

  GCPRO3 (obj, stream, the_stream);
  the_stream = print_prepare (stream);
  print_depth = 0;
  print_internal (obj, the_stream, 0);
  print_finish (the_stream);
  UNGCPRO;
  return (obj);
}

DEFUN ("print", Fprint, 1, 2, 0, /*
Output the printed representation of OBJECT, with newlines around it.
Quoting characters are printed when needed to make output that `read'
can handle, whenever this is possible.
Output stream is STREAM, or value of `standard-output' (which see).
*/
       (obj, stream))
{
  /* This function can GC */
  Lisp_Object the_stream = Qnil;
  struct gcpro gcpro1, gcpro2, gcpro3;

  GCPRO3 (obj, stream, the_stream);
  the_stream = print_prepare (stream);
  print_depth = 0;
  write_char_internal ("\n", the_stream);
  print_internal (obj, the_stream, 1);
  write_char_internal ("\n", the_stream);
  print_finish (the_stream);
  UNGCPRO;
  return obj;
}

#include "emacsfns.h"
/* Synched with Emacs 19.34 */
DEFUN ("error-message-string", Ferror_message_string, 1, 1, 0, /*
Convert an error value (ERROR-SYMBOL . DATA) to an error message.
*/
  (obj))
{
  struct buffer *old = XBUFFER(Fcurrent_buffer());
  Lisp_Object original, printcharfun, value;
  struct gcpro gcpro1;

  print_error_message (obj, Vprin1_to_string_buffer, NULL);

  set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
  value = Fbuffer_substring (Fpoint_min(Fcurrent_buffer()),
			     Fpoint_max(Fcurrent_buffer()),
			     Fcurrent_buffer());

  GCPRO1 (value);
  Ferase_buffer (Fcurrent_buffer());
  set_buffer_internal (old);
  UNGCPRO;

  return value;
}

/* Print an error message for the error DATA
   onto Lisp output stream STREAM (suitable for the print functions).  */

print_error_message (data, stream)
     Lisp_Object data, stream;
{
  Lisp_Object errname, errmsg, file_error, tail;
  struct gcpro gcpro1;
  int i;

  errname = Fcar (data);

  if (EQ (errname, Qerror))
    {
      data = Fcdr (data);
      if (!CONSP (data)) data = Qnil;
      errmsg = Fcar (data);
      file_error = Qnil;
    }
  else
    {
      errmsg = Fget (errname, Qerror_message, Qnil);
      file_error = Fmemq (Qfile_error,
			  Fget (errname, Qerror_conditions, Qnil));
    }

  /* Print an error message including the data items.  */

  tail = Fcdr_safe (data);
  GCPRO1 (tail);

  /* For file-error, make error message by concatenating
     all the data items.  They are all strings.  */
  if (!NILP (file_error) && !NILP (tail))
    errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;

  if (STRINGP (errmsg))
    Fprinc (errmsg, stream);
  else
    write_string_1 ((CONST Bufbyte *)"Peculiar error", 14, stream);

  for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
    {
      write_string_1 ((CONST Bufbyte *)(i ? ", " : ": "), 2, stream);
      if (!NILP (file_error))
	Fprinc (Fcar (tail), stream);
      else
	Fprin1 (Fcar (tail), stream);
    }
  UNGCPRO;
}

#ifdef LISP_FLOAT_TYPE

Lisp_Object Vfloat_output_format;
Lisp_Object Qfloat_output_format;

void
float_to_string (char *buf, double data)
/*
 * This buffer should be at least as large as the max string size of the
 * largest float, printed in the biggest notation.  This is undoubtably
 * 20d float_output_format, with the negative of the C-constant "HUGE"
 * from <math.h>.
 * 
 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
 * 
 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
 * case of -1e307 in 20d float_output_format. What is one to do (short of
 * re-writing _doprnt to be more sane)?
 * 			-wsr
 */
{
  Bufbyte *cp, c;
  int width;
      
  if (NILP (Vfloat_output_format)
      || !STRINGP (Vfloat_output_format))
  lose:
    sprintf (buf, "%.16g", data);
  else			/* oink oink */
    {
      /* Check that the spec we have is fully valid.
	 This means not only valid for printf,
	 but meant for floats, and reasonable.  */
      cp = XSTRING_DATA (Vfloat_output_format);

      if (cp[0] != '%')
	goto lose;
      if (cp[1] != '.')
	goto lose;

      cp += 2;
      for (width = 0; (c = *cp, isdigit (c)); cp++)
	{
	  width *= 10;
	  width += c - '0';
	}

      if (*cp != 'e' && *cp != 'f' && *cp != 'g' && *cp != 'E' && *cp != 'G') 
	goto lose;

      if (width < (int) (*cp != 'e' && *cp != 'E') || width > DBL_DIG)
	goto lose;

      if (cp[1] != 0)
	goto lose;

      sprintf (buf, (char *) XSTRING_DATA (Vfloat_output_format),
	       data);
    }

  /* added by jwz: don't allow "1.0" to print as "1"; that destroys
     the read-equivalence of lisp objects.  (* x 1) and (* x 1.0) do
     not do the same thing, so it's important that the printed
     representation of that form not be corrupted by the printer.
   */
  {
    Bufbyte *s = (Bufbyte *) buf; /* don't use signed chars here!
				     isdigit() can't hack them! */
    if (*s == '-') s++;
    for (; *s; s++)
      /* if there's a non-digit, then there is a decimal point, or
	 it's in exponential notation, both of which are ok. */
      if (!isdigit (*s))
	goto DONE_LABEL;
    /* otherwise, we need to hack it. */
    *s++ = '.';
    *s++ = '0';
    *s = 0;
  }
 DONE_LABEL:

  /* Some machines print "0.4" as ".4".  I don't like that. */
  if (buf [0] == '.' || (buf [0] == '-' && buf [1] == '.'))
    {
      int i;
      for (i = strlen (buf) + 1; i >= 0; i--)
	buf [i+1] = buf [i];
      buf [(buf [0] == '-' ? 1 : 0)] = '0';
    }
}
#endif /* LISP_FLOAT_TYPE */

static void
print_vector_internal (CONST char *start, CONST char *end,
                       Lisp_Object obj, 
                       Lisp_Object printcharfun, int escapeflag)
{
  /* This function can GC */
  int i;
  int len = vector_length (XVECTOR (obj));
  int last = len;
  struct gcpro gcpro1, gcpro2;
  GCPRO2 (obj, printcharfun);

  if (INTP (Vprint_length))
    {
      int max = XINT (Vprint_length);
      if (max < len) last = max;
    }

  write_c_string (start, printcharfun);
  for (i = 0; i < last; i++)
    {
      Lisp_Object elt = vector_data (XVECTOR (obj))[i];
      if (i != 0) write_char_internal (" ", printcharfun);
      print_internal (elt, printcharfun, escapeflag);
    }
  UNGCPRO;
  if (last != len)
    write_c_string (" ...", printcharfun);
  write_c_string (end, printcharfun);
}

static void
default_object_printer (Lisp_Object obj, Lisp_Object printcharfun,
			int escapeflag)
{
  struct lcrecord_header *header =
    (struct lcrecord_header *) XPNTR (obj);
  char buf[200];

  if (print_readably)
    error ("printing unreadable object #<%s 0x%x>",
	   header->lheader.implementation->name, header->uid);

  sprintf (buf, "#<%s 0x%x>", header->lheader.implementation->name,
	   header->uid);
  write_c_string (buf, printcharfun);
}

void
internal_object_printer (Lisp_Object obj, Lisp_Object printcharfun,
			 int escapeflag)
{
  char buf[200];
  sprintf (buf, "#<INTERNAL OBJECT (XEmacs bug?) (%s) 0x%x>",
	   XRECORD_LHEADER (obj)->implementation->name,
	   (EMACS_INT) XPNTR (obj));
  write_c_string (buf, printcharfun);
}

void
print_internal (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
{
  /* This function can GC */
  char buf[256];

  QUIT;

  /* Emacs won't print whilst GCing, but an external debugger might */
  if (gc_in_progress) return;

#ifdef I18N3
  /* #### Both input and output streams should have a flag associated
     with them indicating whether output to that stream, or strings
     read from the stream, get translated using Fgettext().  Such a
     stream is called a "translating stream".  For the minibuffer and
     external-debugging-output this is always true on output, and
     with-output-to-temp-buffer sets the flag to true for the buffer
     it creates.  This flag should also be user-settable.  Perhaps it
     should be split up into two flags, one for input and one for
     output. */
#endif

  print_depth++;

  if (print_depth > 200)
    error ("Apparently circular structure being printed");

  switch (XTYPE (obj))
    {
    case Lisp_Int:
      {
	sprintf (buf, "%d", XINT (obj));
	write_c_string (buf, printcharfun);
	break;
      }

    case Lisp_Char:
      {
	/* God intended that this be #\..., you know. */
	Emchar ch = XCHAR (obj);
	write_c_string ("?", printcharfun);
	if (ch == '\n')
	  strcpy (buf, "\\n");
	else if (ch == '\r')
	  strcpy (buf, "\\r");
	else if (ch == '\t')
	  strcpy (buf, "\\t");
	else if (ch < 32)
	  sprintf (buf, "\\^%c", ch + 64);
	else if (ch == 127)
	  strcpy (buf, "\\^?");
	else if (ch >= 128 && ch < 160)
	  {
	    Bytecount i;
	    strcpy (buf, "\\^");
	    i = set_charptr_emchar ((unsigned char *) (buf + 2), ch + 64);
	    buf[2+i] = '\0';
	  }
	else if (ch < 127
		 && !isdigit (ch)
		 && !isalpha (ch)
		 && ch != '^') /* must not backslash this or it will
				  be interpreted as the start of a
				  control char */
	  sprintf (buf, "\\%c", ch);
	else
	  {
	    Bytecount i;
	    i = set_charptr_emchar ((unsigned char *) buf, ch);
	    buf[i] = '\0';
	  }
	write_c_string (buf, printcharfun);
	break;
      }

    case Lisp_String:
      {
	Bytecount size = XSTRING_LENGTH (obj);
	struct gcpro gcpro1, gcpro2;
	int max = size;
	GCPRO2 (obj, printcharfun);

	if (INTP (Vprint_string_length) &&
	    XINT (Vprint_string_length) < max)
	  max = XINT (Vprint_string_length);
	if (max < 0)
	  max = 0;

	/* !!#### This handles MAX incorrectly for Mule. */
	if (!escapeflag)
	  {
	    /* This deals with GC-relocation */
	    output_string (printcharfun, 0, obj, 0, max);
	    if (max < size)
	      write_c_string (" ...", printcharfun);
	  }
	else
	  {
	    Bytecount i;
	    struct Lisp_String *s = XSTRING (obj);
	    Bytecount last = 0;

	    write_char_internal ("\"", printcharfun);
	    for (i = 0; i < max; i++)
	      {
		Bufbyte ch = string_byte (s, i);
		if (ch == '\"' || ch == '\\' 
		    || (ch == '\n' && print_escape_newlines))
		  {
		    if (i > last)
		      {
			output_string (printcharfun, 0, obj, last,
				       i - last);
		      }
		    if (ch == '\n')
		      {
			write_c_string ("\\n", printcharfun);
		      }
		    else
		      {
			write_char_internal ("\\", printcharfun);
			/* This is correct for Mule because the
			   character is either \ or " */
			write_char_internal ((char *) (string_data (s) + i),
					     printcharfun);
		      }
		    last = i + 1;
		  }
	      }
	    if (max > last)
	      {
		output_string (printcharfun, 0, obj, last,
			       max - last);
	      }
	    if (max < size)
	      write_c_string (" ...", printcharfun);
	    write_char_internal ("\"", printcharfun);
	  }
	UNGCPRO;
	break;
      }

    case Lisp_Cons:
      {
	struct gcpro gcpro1, gcpro2;

	/* If deeper than spec'd depth, print placeholder.  */
	if (INTP (Vprint_level)
	    && print_depth > XINT (Vprint_level))
	  {
	    write_c_string ("...", printcharfun);
	    break;
	  }

	/* If print_readably is on, print (quote -foo-) as '-foo-
	   (Yeah, this should really be what print-pretty does, but we
	   don't have the rest of a pretty printer, and this actually
	   has non-negligible impact on size/speed of .elc files.)
	 */
	if (print_readably &&
	    EQ (XCAR (obj), Qquote) &&
	    CONSP (XCDR (obj)) &&
	    NILP (XCDR (XCDR (obj))))
	  {
	    obj = XCAR (XCDR (obj));
	    GCPRO2 (obj, printcharfun);
	    write_char_internal ("'", printcharfun);
	    UNGCPRO;
	    print_internal (obj, printcharfun, escapeflag);
	    break;
	  }

	GCPRO2 (obj, printcharfun);
	write_char_internal ("(", printcharfun);
	{
	  int i = 0;
	  int max = 0;

	  if (INTP (Vprint_length))
	    max = XINT (Vprint_length);
	  while (CONSP (obj))
	    {
	      if (i++)
		write_char_internal (" ", printcharfun);
	      if (max && i > max)
		{
		  write_c_string ("...", printcharfun);
		  break;
		}
	      print_internal (Fcar (obj), printcharfun,
			      escapeflag);
	      obj = Fcdr (obj);
	    }
	}
	if (!NILP (obj) && !CONSP (obj))
	  {
	    write_c_string (" . ", printcharfun);
	    print_internal (obj, printcharfun, escapeflag);
	  }
	UNGCPRO;
	write_char_internal (")", printcharfun);
	break;
      }

#ifndef LRECORD_VECTOR
    case Lisp_Vector:
      {
	/* If deeper than spec'd depth, print placeholder.  */
	if (INTP (Vprint_level)
	    && print_depth > XINT (Vprint_level))
	  {
	    write_c_string ("...", printcharfun);
	    break;
	  }

	/* God intended that this be #(...), you know. */
	print_vector_internal ("[", "]", obj, printcharfun, escapeflag);
	break;
      }
#endif /* !LRECORD_VECTOR */

#ifndef LRECORD_SYMBOL
    case Lisp_Symbol:
      {
        print_symbol (obj, printcharfun, escapeflag);
        break;
      }
#endif /* !LRECORD_SYMBOL */

    case Lisp_Record:
      {
	struct lrecord_header *lheader = XRECORD_LHEADER (obj);
	struct gcpro gcpro1, gcpro2;

	GCPRO2 (obj, printcharfun);
	if (lheader->implementation->printer)
	  ((lheader->implementation->printer)
	   (obj, printcharfun, escapeflag));
	else
	  default_object_printer (obj, printcharfun, escapeflag);
	UNGCPRO;
	break;
      }

    default:
      {
	/* We're in trouble if this happens!
	   Probably should just abort () */
	if (print_readably)
	  error ("printing illegal data type #o%03o",
		 (int) XTYPE (obj));
	write_c_string ("#<EMACS BUG: ILLEGAL DATATYPE ",
			printcharfun);
	sprintf (buf, "(#o%3o)", (int) XTYPE (obj));
	write_c_string (buf, printcharfun);
	write_c_string
	  (" Save your buffers immediately and please report this bug>",
	   printcharfun);
	break;
      }
    }

  print_depth--;
}

static void
print_compiled_function_internal (CONST char *start, CONST char *end,
				  Lisp_Object obj, 
				  Lisp_Object printcharfun, int escapeflag)
{
  /* This function can GC */
  struct Lisp_Compiled_Function *b =
    XCOMPILED_FUNCTION (obj); /* GC doesn't relocate */
  int docp = b->flags.documentationp;
  int intp = b->flags.interactivep;
  struct gcpro gcpro1, gcpro2;
  char buf[100];
  GCPRO2 (obj, printcharfun);

  write_c_string (start, printcharfun);
#ifdef COMPILED_FUNCTION_ANNOTATION_HACK
  if (!print_readably)
    {
      Lisp_Object ann = compiled_function_annotation (b);
      if (!NILP (ann))
	{
	  write_c_string ("(from ", printcharfun);
	  print_internal (ann, printcharfun, 1);
	  write_c_string (") ", printcharfun);
	}
    }
#endif /* COMPILED_FUNCTION_ANNOTATION_HACK */
  /* COMPILED_ARGLIST = 0 */
  print_internal (b->arglist, printcharfun, escapeflag);
  /* COMPILED_BYTECODE = 1 */
  write_char_internal (" ", printcharfun);
  /* we don't really want to see that junk in the bytecode instructions. */
  if (STRINGP (b->bytecodes) && !print_readably)
    {
      sprintf (buf, "\"...(%ld)\"", (long) XSTRING_LENGTH (b->bytecodes));
      write_c_string (buf, printcharfun);
    }
  else
    print_internal (b->bytecodes, printcharfun, escapeflag);
  /* COMPILED_CONSTANTS = 2 */
  write_char_internal (" ", printcharfun);
  print_internal (b->constants, printcharfun, escapeflag);
  /* COMPILED_STACK_DEPTH = 3 */
  sprintf (buf, " %d", b->maxdepth);
  write_c_string (buf, printcharfun);
  /* COMPILED_DOC_STRING = 4 */
  if (docp || intp)
    {
      write_char_internal (" ", printcharfun);
      print_internal (compiled_function_documentation (b), printcharfun,
		      escapeflag);
    }
  /* COMPILED_INTERACTIVE = 5 */
  if (intp)
    {
      write_char_internal (" ", printcharfun);
      print_internal (compiled_function_interactive (b), printcharfun,
		      escapeflag);
    }
  UNGCPRO;
  write_c_string (end, printcharfun);
}

void
print_compiled_function (Lisp_Object obj, Lisp_Object printcharfun,
			 int escapeflag)
{
  /* This function can GC */
  print_compiled_function_internal (((print_readably) ? "#[" :
				     "#<compiled-function "),
				    ((print_readably) ? "]" : ">"),
				    obj, printcharfun, escapeflag);
}

#ifdef LISP_FLOAT_TYPE
void
print_float (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
{
  char pigbuf[350];	/* see comments in float_to_string */

  float_to_string (pigbuf, float_data (XFLOAT (obj)));
  write_c_string (pigbuf, printcharfun);
}
#endif /* LISP_FLOAT_TYPE */

void
print_symbol (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
{
  /* This function can GC */
  /* #### Bug!! (intern "") isn't printed in some distinguished way */
  /* ####  (the reader also loses on it) */
  struct Lisp_String *name = XSYMBOL (obj)->name;
  Bytecount size = string_length (name);
  struct gcpro gcpro1, gcpro2;

  if (!escapeflag)
    {
      /* This deals with GC-relocation */
      Lisp_Object nameobj;
      XSETSTRING (nameobj, name);
      output_string (printcharfun, 0, nameobj, 0, size);
      return;
    }
  GCPRO2 (obj, printcharfun);

  if (print_gensym) 
    {
      Lisp_Object tem = oblookup (Vobarray, string_data (name), size);
      if (!EQ (tem, obj))
	/* (read) would return a new symbol with the same name.
	   This isn't quite correct, because that symbol might not
	   really be uninterned (it might be interned in some other
	   obarray) but there's no way to win in that case without
	   implementing a real package system.
	   */
	write_c_string ("#:", printcharfun);
    }

  /* Does it look like an integer or a float? */
  {
    Bufbyte *data = string_data (name);
    Bytecount confusing = 0;

    if (size == 0)
      goto not_yet_confused;    /* Really confusing */
    else if (isdigit (data[0]))
      confusing = 0;
    else if (size == 1)
      goto not_yet_confused;
    else if (data[0] == '-' || data[0] == '+')
      confusing = 1;
    else
      goto not_yet_confused;

    for (; confusing < size; confusing++)
      { 
        if (!isdigit (data[confusing]))
          {
            confusing = 0;
            break;
          }
      }
  not_yet_confused:

#ifdef LISP_FLOAT_TYPE
    if (!confusing)
      confusing = isfloat_string ((char *) data);
#endif
    if (confusing)
      write_char_internal ("\\", printcharfun);
  }

  {
    Lisp_Object nameobj;
    Bytecount i;
    Bytecount last = 0;
        
    XSETSTRING (nameobj, name);
    for (i = 0; i < size; i++)
      {
	Bufbyte c = string_byte (name, i);

	if (c == '\"' || c == '\\' || c == '\'' || c == ';' || c == '#' ||
	    c == '(' || c == ')' || c == ',' || c =='.' || c == '`' ||
	    c == '[' || c == ']' || c == '?' || c <= 040)
	  {
	    if (i > last)
	      {
		output_string (printcharfun, 0, nameobj, last,
			       i - last);
	      }
	    write_char_internal ("\\", printcharfun);
	    last = i;
	  }
      }
    output_string (printcharfun, 0, nameobj, last, size - last);
  }
  UNGCPRO;
}


int alternate_do_pointer;
char alternate_do_string[5000];

DEFUN ("alternate-debugging-output", Falternate_debugging_output, 1, 1, 0, /*
Append CHARACTER to the array `alternate_do_string'.
This can be used in place of `external-debugging-output' as a function
to be passed to `print'.  Before calling `print', set `alternate_do_pointer'
to 0.

*/
       (character))
{
  Bufbyte str[MAX_EMCHAR_LEN];
  Bytecount len;
  int extlen;
  CONST Extbyte *extptr;

  CHECK_CHAR_COERCE_INT (character);
  len = set_charptr_emchar (str, XCHAR (character));
  GET_CHARPTR_EXT_DATA_ALLOCA (str, len, FORMAT_TERMINAL, extptr, extlen);
  memcpy (alternate_do_string + alternate_do_pointer, extptr, extlen);
  alternate_do_pointer += extlen;
  alternate_do_string[alternate_do_pointer] = 0;
  return character;
}

DEFUN ("external-debugging-output", Fexternal_debugging_output, 1, 3, 0, /*
Write CHAR-OR-STRING to stderr or stdout.
If optional arg STDOUT-P is non-nil, write to stdout; otherwise, write
to stderr.  You can use this function to write directly to the terminal.
This function can be used as the STREAM argument of Fprint() or the like.

If you have opened a termscript file (using `open-termscript'), then
the output also will be logged to this file.
*/
       (char_or_string, stdout_p, device))
{
  FILE *file = 0;
  struct console *con = 0;

  if (NILP (device))
    {
      if (!NILP (stdout_p))
	file = stdout;
      else
	file = stderr;
    }
  else
    {
      CHECK_LIVE_DEVICE (device);
      if (!DEVICE_TTY_P (XDEVICE (device)) &&
	  !DEVICE_STREAM_P (XDEVICE (device)))
	signal_simple_error ("Must be tty or stream device", device);
      con = XCONSOLE (DEVICE_CONSOLE (XDEVICE (device)));
      if (DEVICE_TTY_P (XDEVICE (device)))
	file = 0;
      else if (!NILP (stdout_p))
	file = CONSOLE_STREAM_DATA (con)->outfd;
      else
	file = CONSOLE_STREAM_DATA (con)->errfd;
    }

  if (STRINGP (char_or_string))
    write_string_to_stdio_stream (file, con,
				  XSTRING_DATA (char_or_string),
				  0, XSTRING_LENGTH (char_or_string),
				  FORMAT_TERMINAL);
  else
    {
      Bufbyte str[MAX_EMCHAR_LEN];
      Bytecount len;

      CHECK_CHAR_COERCE_INT (char_or_string);
      len = set_charptr_emchar (str, XCHAR (char_or_string));
      write_string_to_stdio_stream (file, con, str, 0, len, FORMAT_TERMINAL);
    }

  return char_or_string;
}

DEFUN ("open-termscript", Fopen_termscript, 1, 1, "FOpen termscript file: ", /*
Start writing all terminal output to FILE as well as the terminal.
FILE = nil means just close any termscript file currently open.
*/
       (file))
{
  /* This function can GC */
  if (termscript != 0)
    fclose (termscript);
  termscript = 0;

  if (! NILP (file))
    {
      file = Fexpand_file_name (file, Qnil);
      termscript = fopen ((char *) XSTRING_DATA (file), "w");
      if (termscript == 0)
	report_file_error ("Opening termscript", Fcons (file, Qnil));
    }
  return Qnil;
}

#if 1
/* Debugging kludge -- unbuffered */
static int debug_print_length = 50;
static int debug_print_level = 15;
Lisp_Object debug_temp;
void debug_print_no_newline (Lisp_Object debug_print_obj);
void
debug_print_no_newline (Lisp_Object debug_print_obj)
{
  /* This function can GC */
  int old_print_readably = print_readably;
  int old_print_depth = print_depth;
  Lisp_Object old_print_length = Vprint_length;
  Lisp_Object old_print_level = Vprint_level;
  Lisp_Object old_inhibit_quit = Vinhibit_quit;
  struct gcpro gcpro1, gcpro2, gcpro3;
  GCPRO3 (old_print_level, old_print_length, old_inhibit_quit);

  if (gc_in_progress)
    stderr_out ("** gc-in-progress!  Bad idea to print anything! **\n");

  print_depth = 0;
  print_readably = 0;
  print_unbuffered++;
  /* Could use unwind-protect, but why bother? */
  if (debug_print_length > 0)
    Vprint_length = make_int (debug_print_length);
  if (debug_print_level > 0)
    Vprint_level = make_int (debug_print_level);
  print_internal (debug_print_obj, Qexternal_debugging_output, 1);
  Vinhibit_quit = old_inhibit_quit;
  Vprint_level = old_print_level;
  Vprint_length = old_print_length;
  print_depth = old_print_depth;
  print_readably = old_print_readably;
  print_unbuffered--;
  UNGCPRO;
}

void debug_print (Lisp_Object debug_print_obj);
void
debug_print (Lisp_Object debug_print_obj)
{
  debug_print_no_newline (debug_print_obj);
  stderr_out ("\n");
  fflush (stderr);
}

/* Debugging kludge -- unbuffered */
void
debug_backtrace (void)
{
  /* This function can GC */
  int old_print_readably = print_readably;
  int old_print_depth = print_depth;
  Lisp_Object old_print_length = Vprint_length;
  Lisp_Object old_print_level = Vprint_level;
  Lisp_Object old_inhibit_quit = Vinhibit_quit;
  struct gcpro gcpro1, gcpro2, gcpro3;
  GCPRO3 (old_print_level, old_print_length, old_inhibit_quit);

  if (gc_in_progress)
    stderr_out ("** gc-in-progress!  Bad idea to print anything! **\n");

  print_depth = 0;
  print_readably = 0;
  print_unbuffered++;
  /* Could use unwind-protect, but why bother? */
  if (debug_print_length > 0)
    Vprint_length = make_int (debug_print_length);
  if (debug_print_level > 0)
    Vprint_level = make_int (debug_print_level);
  Fbacktrace (Qexternal_debugging_output, Qt);
  stderr_out ("\n");
  fflush (stderr);
  Vinhibit_quit = old_inhibit_quit;
  Vprint_level = old_print_level;
  Vprint_length = old_print_length;
  print_depth = old_print_depth;
  print_readably = old_print_readably;
  print_unbuffered--;
  UNGCPRO;
}

void
debug_short_backtrace (int length)
{
  int first = 1;
  struct backtrace *bt = backtrace_list;
  stderr_out ("   [");
  fflush (stderr);
  while (length > 0 && bt)
    {
      if (!first)
	{
	  stderr_out (", ");
	  fflush (stderr);
	}
      if (COMPILED_FUNCTIONP (*bt->function))
	{
	  Lisp_Object ann = Fcompiled_function_annotation (*bt->function);
	  if (!NILP (ann))
	    {
	      stderr_out ("<compiled-function from ");
	      fflush (stderr);
	      debug_print_no_newline (ann);
	      stderr_out (">");
	      fflush (stderr);
	    }
	  else
	    {
	      stderr_out ("<compiled-function of unknown origin>");
	      fflush (stderr);
	    }
	}
      else
	debug_print_no_newline (*bt->function);
      first = 0;
      length--;
      bt = bt->next;
    }
  stderr_out ("]\n");
  fflush (stderr);
}

#endif /* debugging kludge */


void
syms_of_print (void)
{
  defsymbol (&Qprint_escape_newlines, "print-escape-newlines");
  defsymbol (&Qprint_readably, "print-readably");

  defsymbol (&Qstandard_output, "standard-output");

#ifdef LISP_FLOAT_TYPE
  defsymbol (&Qfloat_output_format, "float-output-format");
#endif

  defsymbol (&Qprint_length, "print-length");

  defsymbol (&Qprint_string_length, "print-string-length");
  DEFSUBR (Fprin1);
  DEFSUBR (Fprin1_to_string);
  DEFSUBR (Fprinc);
  DEFSUBR (Fprint);
  DEFSUBR (Ferror_message_string);
  DEFSUBR (Fterpri);
  DEFSUBR (Fwrite_char);
  DEFSUBR (Falternate_debugging_output);
  defsymbol (&Qalternate_debugging_output, "alternate-debugging-output");
  DEFSUBR (Fexternal_debugging_output);
  DEFSUBR (Fopen_termscript);
  defsymbol (&Qexternal_debugging_output, "external-debugging-output");
#ifndef standalone
  DEFSUBR (Fwith_output_to_temp_buffer);
#endif /* not standalone */
}

void
lstream_type_create_print (void)
{
  LSTREAM_HAS_METHOD (print, writer);
  LSTREAM_HAS_METHOD (print, marker);
}

void
vars_of_print (void)
{
  alternate_do_pointer = 0;

  DEFVAR_LISP ("standard-output", &Vstandard_output /*
Output stream `print' uses by default for outputting a character.
This may be any function of one argument.
It may also be a buffer (output is inserted before point)
or a marker (output is inserted and the marker is advanced)
or the symbol t (output appears in the minibuffer line).
*/ );
  Vstandard_output = Qt;

#ifdef LISP_FLOAT_TYPE
  DEFVAR_LISP ("float-output-format", &Vfloat_output_format /*
The format descriptor string that lisp uses to print floats.
This is a %-spec like those accepted by `printf' in C,
but with some restrictions.  It must start with the two characters `%.'.
After that comes an integer precision specification,
and then a letter which controls the format.
The letters allowed are `e', `f' and `g'.
Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
Use `f' for decimal point notation \"DIGITS.DIGITS\".
Use `g' to choose the shorter of those two formats for the number at hand.
The precision in any of these cases is the number of digits following
the decimal point.  With `f', a precision of 0 means to omit the
decimal point.  0 is not allowed with `f' or `g'.

A value of nil means to use `%.16g'.

Regardless of the value of `float-output-format', a floating point number
will never be printed in such a way that it is ambiguous with an integer;
that is, a floating-point number will always be printed with a decimal
point and/or an exponent, even if the digits following the decimal point
are all zero.  This is to preserve read-equivalence.
*/ );
  Vfloat_output_format = Qnil;
#endif /* LISP_FLOAT_TYPE */

  DEFVAR_LISP ("print-length", &Vprint_length /*
Maximum length of list or vector to print before abbreviating.
A value of nil means no limit.
*/ );
  Vprint_length = Qnil;

  DEFVAR_LISP ("print-string-length", &Vprint_string_length /*
Maximum length of string to print before abbreviating.
A value of nil means no limit.
*/ );
  Vprint_string_length = Qnil;

  DEFVAR_LISP ("print-level", &Vprint_level /*
Maximum depth of list nesting to print before abbreviating.
A value of nil means no limit.
*/ );
  Vprint_level = Qnil;

  DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines /*
Non-nil means print newlines in strings as backslash-n.
*/ );
  print_escape_newlines = 0;

  DEFVAR_BOOL ("print-readably", &print_readably /*
If non-nil, then all objects will be printed in a readable form.
If an object has no readable representation, then an error is signalled.
When print-readably is true, compiled-function objects will be written in
 #[...] form instead of in #<compiled-function [...]> form, and two-element
 lists of the form (quote object) will be written as the equivalent 'object.
Do not SET this variable; bind it instead.
*/ );
  print_readably = 0;

  DEFVAR_BOOL ("print-gensym", &print_gensym /*
If non-nil, then uninterned symbols will be printed specially.
Uninterned symbols are those which are not present in `obarray', that is,
those which were made with `make-symbol' or by calling `intern' with a
second argument.

When print-gensym is true, such symbols will be preceded by \"#:\", which
causes the reader to create a new symbol instead of interning and returning
an existing one.  Beware: the #: syntax creates a new symbol each time it is
seen, so if you print an object which contains two pointers to the same
uninterned symbol, `read' will not duplicate that structure.

Also, since XEmacs has no real notion of packages, there is no way for the
printer to distinguish between symbols interned in no obarray, and symbols
interned in an alternate obarray.
*/ );
  print_gensym = 0;

  DEFVAR_LISP ("print-message-label", &Vprint_message_label /*
Label for minibuffer messages created with `print'.  This should
generally be bound with `let' rather than set.  (See `display-message'.)
*/ );
  Vprint_message_label = Qprint;

  /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
  staticpro (&Vprin1_to_string_buffer);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.