My various dotfiles

chap-22.texi 190KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286
  1. @node Printer, Reader, Streams, Top
  2. @chapter Printer
  3. @menu
  4. * The Lisp Printer::
  5. * The Lisp Pretty Printer::
  6. * Formatted Output::
  7. * Printer Dictionary::
  8. @end menu
  9. @node The Lisp Printer, The Lisp Pretty Printer, Printer, Printer
  10. @section The Lisp Printer
  11. @c including concept-print
  12. @menu
  13. * Overview of The Lisp Printer::
  14. * Printer Dispatching::
  15. * Default Print-Object Methods::
  16. * Examples of Printer Behavior::
  17. @end menu
  18. @node Overview of The Lisp Printer, Printer Dispatching, The Lisp Printer, The Lisp Printer
  19. @subsection Overview of The Lisp Printer
  20. @r{Common Lisp} provides a representation of most @i{objects} in the form
  21. of printed text called the printed representation.
  22. Functions such as @b{print} take an @i{object}
  23. and send the characters of its printed representation to a @i{stream}.
  24. The collection of routines that does this is known as the (@r{Common Lisp}) printer.
  25. Reading a printed representation
  26. typically
  27. produces an @i{object} that is @b{equal} to the
  28. originally printed @i{object}.
  29. @menu
  30. * Multiple Possible Textual Representations::
  31. * Printer Escaping::
  32. @end menu
  33. @node Multiple Possible Textual Representations, Printer Escaping, Overview of The Lisp Printer, Overview of The Lisp Printer
  34. @subsubsection Multiple Possible Textual Representations
  35. Most @i{objects} have more than one possible textual representation.
  36. For example, the positive @i{integer} with a magnitude of twenty-seven
  37. can be textually expressed in any of these ways:
  38. @example
  39. 27 27. #o33 #x1B #b11011 #.(* 3 3 3) 81/3
  40. @end example
  41. A list containing the two symbols @t{A} and @t{B} can also be textually
  42. expressed in a variety of ways:
  43. @example
  44. (A B) (a b) ( a b ) (\A |B|)
  45. (|\A|
  46. B
  47. )
  48. @end example
  49. In general,
  50. from the point of view of the @i{Lisp reader},
  51. wherever @i{whitespace} is permissible in a textual representation,
  52. any number of @i{spaces} and @i{newlines} can appear in @i{standard syntax}.
  53. When a function such as @b{print} produces a printed representation,
  54. it must choose
  55. from among many possible textual representations.
  56. In most cases, it chooses a
  57. program readable representation,
  58. but in certain cases it might use a more compact notation that is not
  59. program-readable.
  60. A number of option variables, called
  61. @i{printer control variables}
  62. @IGindex{printer control variable}
  63. ,
  64. are provided to permit control of individual aspects of the
  65. printed representation of @i{objects}.
  66. Figure 22--1 shows the @i{standardized} @i{printer control variables};
  67. there might also be @i{implementation-defined} @i{printer control variables}.
  68. @group
  69. @noindent
  70. @w{ *print-array* *print-gensym* *print-pprint-dispatch* }
  71. @w{ *print-base* *print-length* *print-pretty* }
  72. @w{ *print-case* *print-level* *print-radix* }
  73. @w{ *print-circle* *print-lines* *print-readably* }
  74. @w{ *print-escape* *print-miser-width* *print-right-margin* }
  75. @noindent
  76. @w{ Figure 22--1: Standardized Printer Control Variables }
  77. @end group
  78. In addition to the @i{printer control variables},
  79. the following additional @i{defined names}
  80. relate to or affect the behavior of the @i{Lisp printer}:
  81. @group
  82. @noindent
  83. @w{ *package* *read-eval* readtable-case }
  84. @w{ *read-default-float-format* *readtable* }
  85. @noindent
  86. @w{ Figure 22--2: Additional Influences on the Lisp printer. }
  87. @end group
  88. @node Printer Escaping, , Multiple Possible Textual Representations, Overview of The Lisp Printer
  89. @subsubsection Printer Escaping
  90. The @i{variable} @b{*print-escape*} controls whether the @i{Lisp printer}
  91. tries to produce notations such as escape characters and package prefixes.
  92. The @i{variable} @b{*print-readably*} can be used to override
  93. many of the individual aspects controlled by the other
  94. @i{printer control variables} when program-readable output
  95. is especially important.
  96. One of the many effects of making the @i{value} of @b{*print-readably*} be @i{true}
  97. is that the @i{Lisp printer} behaves as if @b{*print-escape*} were also @i{true}.
  98. For notational convenience, we say that
  99. if the value of either @b{*print-readably*} or @b{*print-escape*} is @i{true},
  100. then @i{printer escaping}
  101. @IGindex{printer escaping}
  102. is ``enabled'';
  103. and we say that
  104. if the values of both @b{*print-readably*} and @b{*print-escape*} are @i{false},
  105. then @i{printer escaping} is ``disabled''.
  106. @node Printer Dispatching, Default Print-Object Methods, Overview of The Lisp Printer, The Lisp Printer
  107. @subsection Printer Dispatching
  108. The @i{Lisp printer} makes its determination of how to print an
  109. @i{object} as follows:
  110. If the @i{value} of @b{*print-pretty*} is @i{true},
  111. printing is controlled by the @i{current pprint dispatch table};
  112. see @ref{Pretty Print Dispatch Tables}.
  113. Otherwise (if the @i{value} of @b{*print-pretty*} is @i{false}),
  114. the object's @b{print-object} method is used;
  115. see @ref{Default Print-Object Methods}.
  116. @node Default Print-Object Methods, Examples of Printer Behavior, Printer Dispatching, The Lisp Printer
  117. @subsection Default Print-Object Methods
  118. This section describes the default behavior of
  119. @b{print-object} methods for the @i{standardized} @i{types}.
  120. @menu
  121. * Printing Numbers::
  122. * Printing Integers::
  123. * Printing Ratios::
  124. * Printing Floats::
  125. * Printing Complexes::
  126. * Note about Printing Numbers::
  127. * Printing Characters::
  128. * Printing Symbols::
  129. * Package Prefixes for Symbols::
  130. * Effect of Readtable Case on the Lisp Printer::
  131. * Examples of Effect of Readtable Case on the Lisp Printer::
  132. * Printing Strings::
  133. * Printing Lists and Conses::
  134. * Printing Bit Vectors::
  135. * Printing Other Vectors::
  136. * Printing Other Arrays::
  137. * Examples of Printing Arrays::
  138. * Printing Random States::
  139. * Printing Pathnames::
  140. * Printing Structures::
  141. * Printing Other Objects::
  142. @end menu
  143. @node Printing Numbers, Printing Integers, Default Print-Object Methods, Default Print-Object Methods
  144. @subsubsection Printing Numbers
  145. @node Printing Integers, Printing Ratios, Printing Numbers, Default Print-Object Methods
  146. @subsubsection Printing Integers
  147. @i{Integers} are printed in the radix specified by the @i{current output base}
  148. in positional notation, most significant digit first.
  149. If appropriate, a radix specifier can be printed; see @b{*print-radix*}.
  150. If an @i{integer} is negative, a minus sign is printed and then the
  151. absolute value of the @i{integer} is printed.
  152. The @i{integer} zero is represented
  153. by the single digit @t{0} and never has a sign.
  154. A decimal point might be printed,
  155. depending on the @i{value} of @b{*print-radix*}.
  156. For related information about the syntax of an @i{integer},
  157. see @ref{Syntax of an Integer}.
  158. @node Printing Ratios, Printing Floats, Printing Integers, Default Print-Object Methods
  159. @subsubsection Printing Ratios
  160. @IRindex{ratio}
  161. @i{Ratios} are printed as follows:
  162. the absolute value of the numerator is printed, as for an @i{integer};
  163. then a @t{/}; then the denominator. The numerator and denominator are
  164. both printed in the radix specified by the @i{current output base};
  165. they are obtained as if by
  166. @b{numerator} and @b{denominator}, and so @i{ratios}
  167. are printed in reduced form (lowest terms).
  168. If appropriate, a radix specifier can be printed; see
  169. @b{*print-radix*}.
  170. If the ratio is negative, a minus sign is printed before the numerator.
  171. For related information about the syntax of a @i{ratio},
  172. see @ref{Syntax of a Ratio}.
  173. @node Printing Floats, Printing Complexes, Printing Ratios, Default Print-Object Methods
  174. @subsubsection Printing Floats
  175. @IRindex{float}
  176. If the magnitude of the @i{float} is either zero or between 10^{-3} (inclusive)
  177. and 10^7 (exclusive), it is printed as the integer part of the number,
  178. then a decimal point,
  179. followed by the fractional part of the number;
  180. there is always at least one
  181. digit on each side of the decimal point.
  182. If the sign of the number
  183. (as determined by @b{float-sign})
  184. is negative, then a minus sign is printed before the number.
  185. If the format of the number
  186. does not match that specified by
  187. @b{*read-default-float-format*}, then the @i{exponent marker} for
  188. that format and the digit @t{0} are also printed.
  189. For example, the base of the natural logarithms as a @i{short float}
  190. might be printed as @t{2.71828S0}.
  191. For non-zero magnitudes outside of the range 10^{-3} to 10^7,
  192. a @i{float} is printed in computerized scientific notation.
  193. The representation of the number is scaled to be between
  194. 1 (inclusive) and 10 (exclusive) and then printed, with one digit
  195. before the decimal point and at least one digit after the decimal point.
  196. Next the @i{exponent marker} for the format is printed,
  197. except that
  198. if the format of the number matches that specified by
  199. @b{*read-default-float-format*}, then the @i{exponent marker} @t{E}
  200. is used.
  201. Finally, the power of ten by which the fraction must be multiplied
  202. to equal the original number is printed as a decimal integer.
  203. For example, Avogadro's number as a @i{short float}
  204. is printed as @t{6.02S23}.
  205. For related information about the syntax of a @i{float},
  206. see @ref{Syntax of a Float}.
  207. @node Printing Complexes, Note about Printing Numbers, Printing Floats, Default Print-Object Methods
  208. @subsubsection Printing Complexes
  209. @IRindex{complex}
  210. A @i{complex} is printed as @t{#C}, an open parenthesis,
  211. the printed representation of its real part, a space,
  212. the printed representation of its imaginary part, and finally
  213. a close parenthesis.
  214. For related information about the syntax of a @i{complex},
  215. see @ref{Syntax of a Complex} and @ref{Sharpsign C}.
  216. @node Note about Printing Numbers, Printing Characters, Printing Complexes, Default Print-Object Methods
  217. @subsubsection Note about Printing Numbers
  218. The printed representation of a number must not contain @i{escape} @i{characters};
  219. see @ref{Escape Characters and Potential Numbers}.
  220. @node Printing Characters, Printing Symbols, Note about Printing Numbers, Default Print-Object Methods
  221. @subsubsection Printing Characters
  222. When @i{printer escaping} is disabled,
  223. a @i{character} prints as itself;
  224. it is sent directly to the output @i{stream}.
  225. When @i{printer escaping} is enabled,
  226. then @t{#\} syntax is used.
  227. When the printer types out the name of a @i{character},
  228. it uses the same table as the @t{#\} @i{reader macro} would use;
  229. therefore any @i{character} name that is typed out
  230. is acceptable as input (in that @i{implementation}).
  231. If a @i{non-graphic} @i{character} has a @i{standardized} @i{name}_5,
  232. that @i{name} is preferred over non-standard @i{names}
  233. for printing in @t{#\} notation.
  234. For the @i{graphic} @i{standard characters},
  235. the @i{character} itself is always used
  236. for printing in @t{#\} notation---even if
  237. the @i{character} also has a @i{name}_5.
  238. For details about the @t{#\} @i{reader macro}, see @ref{Sharpsign Backslash}.
  239. @node Printing Symbols, Package Prefixes for Symbols, Printing Characters, Default Print-Object Methods
  240. @subsubsection Printing Symbols
  241. When @i{printer escaping} is disabled,
  242. only the characters of the @i{symbol}'s @i{name} are output
  243. (but the case in which to print characters in the @i{name} is
  244. controlled by @b{*print-case*};
  245. see @ref{Effect of Readtable Case on the Lisp Printer}).
  246. The remainder of this section applies only
  247. when @i{printer escaping} is enabled.
  248. When printing a @i{symbol}, the printer inserts enough
  249. @i{single escape} and/or @i{multiple escape}
  250. characters (@i{backslashes} and/or @i{vertical-bars}) so that if
  251. @b{read} were called with the same @b{*readtable*} and
  252. with @b{*read-base*} bound to the @i{current output base}, it
  253. would return the same @i{symbol} (if it is not
  254. @i{apparently uninterned}) or an @i{uninterned} @i{symbol}
  255. with the same @i{print name} (otherwise).
  256. For example, if the @i{value} of @b{*print-base*} were @t{16}
  257. when printing the symbol @t{face}, it would have to be printed as
  258. @t{\FACE} or @t{\Face} or @t{|FACE|},
  259. because the token @t{face} would be read as a hexadecimal
  260. number (decimal value 64206) if the @i{value} of @b{*read-base*} were @t{16}.
  261. For additional restrictions concerning characters with nonstandard
  262. @i{syntax types} in the @i{current readtable}, see the @i{variable} @b{*print-readably*}
  263. For information about how the @i{Lisp reader} parses @i{symbols},
  264. see @ref{Symbols as Tokens} and @ref{Sharpsign Colon}.
  265. @b{nil} might be printed as @t{()}
  266. when @b{*print-pretty*} is @i{true}
  267. and @i{printer escaping} is enabled.
  268. @node Package Prefixes for Symbols, Effect of Readtable Case on the Lisp Printer, Printing Symbols, Default Print-Object Methods
  269. @subsubsection Package Prefixes for Symbols
  270. @i{Package prefixes} are printed if necessary.
  271. The rules for @i{package prefixes} are as follows.
  272. When the @i{symbol} is printed, if it is in the @t{KEYWORD} @i{package},
  273. then it is printed with a preceding @i{colon}; otherwise, if
  274. it is @i{accessible} in the @i{current package}, it is printed without any
  275. @i{package prefix}; otherwise, it is printed with a @i{package prefix}.
  276. A @i{symbol} that is @i{apparently uninterned} is printed
  277. preceded by ``@t{#:}''
  278. if @b{*print-gensym*} is @i{true} and @i{printer escaping} is enabled;
  279. if @b{*print-gensym*} is @i{false} or @i{printer escaping} is disabled,
  280. then the @i{symbol} is printed without a prefix,
  281. as if it were in the @i{current package}.
  282. Because the @t{#:} syntax does not intern the
  283. following symbol, it is necessary to use circular-list syntax
  284. if @b{*print-circle*} is @i{true} and
  285. the same uninterned symbol appears several times in an expression
  286. to be printed. For example, the result of
  287. @example
  288. (let ((x (make-symbol "FOO"))) (list x x))
  289. @end example
  290. would be printed as @t{(#:foo #:foo)} if @b{*print-circle*}
  291. were @i{false}, but as @t{(#1=#:foo #1#)} if @b{*print-circle*}
  292. were @i{true}.
  293. A summary of the preceding package prefix rules follows:
  294. @table @asis
  295. @item @t{foo:bar}
  296. @t{foo:bar} is printed when @i{symbol} @t{bar}
  297. is external in its @i{home package} @t{foo}
  298. and is not @i{accessible} in the @i{current package}.
  299. @item @t{foo::bar}
  300. @t{foo::bar} is printed when @t{bar} is internal in its @i{home package}
  301. @t{foo} and is not @i{accessible} in the @i{current package}.
  302. @item @t{:bar}
  303. @t{:bar} is printed when the home package of @t{bar} is the @t{KEYWORD} @i{package}.
  304. @item #:bar
  305. @t{#:bar} is printed when @t{bar} is @i{apparently uninterned},
  306. even in the pathological case that @t{bar}
  307. has no @i{home package} but is nevertheless somehow @i{accessible}
  308. in the @i{current package}.
  309. @end table
  310. @node Effect of Readtable Case on the Lisp Printer, Examples of Effect of Readtable Case on the Lisp Printer, Package Prefixes for Symbols, Default Print-Object Methods
  311. @subsubsection Effect of Readtable Case on the Lisp Printer
  312. When
  313. @i{printer escaping} is disabled,
  314. or the characters under consideration are not already
  315. quoted specifically by @i{single escape} or @i{multiple escape}
  316. syntax,
  317. the @i{readtable case} of the @i{current readtable}
  318. affects the way the @i{Lisp printer} writes @i{symbols}
  319. in the following ways:
  320. @table @asis
  321. @item @t{:upcase}
  322. When the @i{readtable case} is @t{:upcase},
  323. @i{uppercase} @i{characters}
  324. are printed in the case specified by @b{*print-case*}, and
  325. @i{lowercase} @i{characters} are printed in their own case.
  326. @item @t{:downcase}
  327. When the @i{readtable case} is @t{:downcase},
  328. @i{uppercase} @i{characters} are printed in their own case, and
  329. @i{lowercase} @i{characters}
  330. are printed in the case specified by @b{*print-case*}.
  331. @item @t{:preserve}
  332. When the @i{readtable case} is @t{:preserve},
  333. all @i{alphabetic} @i{characters} are printed in their own case.
  334. @item @t{:invert}
  335. When the @i{readtable case} is @t{:invert},
  336. the case of all @i{alphabetic} @i{characters}
  337. in single case symbol names is inverted.
  338. Mixed-case symbol names are printed as is.
  339. @end table
  340. The rules for escaping @i{alphabetic} @i{characters} in symbol names are affected by
  341. the @b{readtable-case}
  342. if @i{printer escaping} is enabled.
  343. @i{Alphabetic} @i{characters} are escaped as follows:
  344. @table @asis
  345. @item @t{:upcase}
  346. When the @i{readtable case} is @t{:upcase},
  347. all @i{lowercase} @i{characters} must be escaped.
  348. @item @t{:downcase}
  349. When the @i{readtable case} is @t{:downcase},
  350. all @i{uppercase} @i{characters} must be escaped.
  351. @item @t{:preserve}
  352. When the @i{readtable case} is @t{:preserve},
  353. no @i{alphabetic} @i{characters} need be escaped.
  354. @item @t{:invert}
  355. When the @i{readtable case} is @t{:invert},
  356. no @i{alphabetic} @i{characters} need be escaped.
  357. @end table
  358. @node Examples of Effect of Readtable Case on the Lisp Printer, Printing Strings, Effect of Readtable Case on the Lisp Printer, Default Print-Object Methods
  359. @subsubsection Examples of Effect of Readtable Case on the Lisp Printer
  360. @example
  361. (defun test-readtable-case-printing ()
  362. (let ((*readtable* (copy-readtable nil))
  363. (*print-case* *print-case*))
  364. (format t "READTABLE-CASE *PRINT-CASE* Symbol-name Output~
  365. ~
  366. ~
  367. (dolist (readtable-case '(:upcase :downcase :preserve :invert))
  368. (setf (readtable-case *readtable*) readtable-case)
  369. (dolist (print-case '(:upcase :downcase :capitalize))
  370. (dolist (symbol '(|ZEBRA| |Zebra| |zebra|))
  371. (setq *print-case* print-case)
  372. (format t "~&:~A~15T:~A~29T~A~42T~A"
  373. (string-upcase readtable-case)
  374. (string-upcase print-case)
  375. (symbol-name symbol)
  376. (prin1-to-string symbol)))))))
  377. @end example
  378. The output from @t{(test-readtable-case-printing)} should be as follows:
  379. @example
  380. READTABLE-CASE *PRINT-CASE* Symbol-name Output
  381. --------------------------------------------------
  382. :UPCASE :UPCASE ZEBRA ZEBRA
  383. :UPCASE :UPCASE Zebra |Zebra|
  384. :UPCASE :UPCASE zebra |zebra|
  385. :UPCASE :DOWNCASE ZEBRA zebra
  386. :UPCASE :DOWNCASE Zebra |Zebra|
  387. :UPCASE :DOWNCASE zebra |zebra|
  388. :UPCASE :CAPITALIZE ZEBRA Zebra
  389. :UPCASE :CAPITALIZE Zebra |Zebra|
  390. :UPCASE :CAPITALIZE zebra |zebra|
  391. :DOWNCASE :UPCASE ZEBRA |ZEBRA|
  392. :DOWNCASE :UPCASE Zebra |Zebra|
  393. :DOWNCASE :UPCASE zebra ZEBRA
  394. :DOWNCASE :DOWNCASE ZEBRA |ZEBRA|
  395. :DOWNCASE :DOWNCASE Zebra |Zebra|
  396. :DOWNCASE :DOWNCASE zebra zebra
  397. :DOWNCASE :CAPITALIZE ZEBRA |ZEBRA|
  398. :DOWNCASE :CAPITALIZE Zebra |Zebra|
  399. :DOWNCASE :CAPITALIZE zebra Zebra
  400. :PRESERVE :UPCASE ZEBRA ZEBRA
  401. :PRESERVE :UPCASE Zebra Zebra
  402. :PRESERVE :UPCASE zebra zebra
  403. :PRESERVE :DOWNCASE ZEBRA ZEBRA
  404. :PRESERVE :DOWNCASE Zebra Zebra
  405. :PRESERVE :DOWNCASE zebra zebra
  406. :PRESERVE :CAPITALIZE ZEBRA ZEBRA
  407. :PRESERVE :CAPITALIZE Zebra Zebra
  408. :PRESERVE :CAPITALIZE zebra zebra
  409. :INVERT :UPCASE ZEBRA zebra
  410. :INVERT :UPCASE Zebra Zebra
  411. :INVERT :UPCASE zebra ZEBRA
  412. :INVERT :DOWNCASE ZEBRA zebra
  413. :INVERT :DOWNCASE Zebra Zebra
  414. :INVERT :DOWNCASE zebra ZEBRA
  415. :INVERT :CAPITALIZE ZEBRA zebra
  416. :INVERT :CAPITALIZE Zebra Zebra
  417. :INVERT :CAPITALIZE zebra ZEBRA
  418. @end example
  419. @node Printing Strings, Printing Lists and Conses, Examples of Effect of Readtable Case on the Lisp Printer, Default Print-Object Methods
  420. @subsubsection Printing Strings
  421. The characters of the @i{string} are output in order.
  422. If @i{printer escaping} is enabled,
  423. a @i{double-quote} is output before and after, and all
  424. @i{double-quotes} and @i{single escapes} are preceded by @i{backslash}.
  425. The printing of @i{strings} is not affected by @b{*print-array*}.
  426. Only the @i{active} @i{elements} of the @i{string} are printed.
  427. For information on how the @i{Lisp reader} parses @i{strings},
  428. see @ref{Double-Quote}.
  429. @node Printing Lists and Conses, Printing Bit Vectors, Printing Strings, Default Print-Object Methods
  430. @subsubsection Printing Lists and Conses
  431. Wherever possible, list notation is preferred over dot notation.
  432. Therefore the following algorithm is used to print a @i{cons} x:
  433. @table @asis
  434. @item 1.
  435. A @i{left-parenthesis} is printed.
  436. @item 2.
  437. The @i{car} of x is printed.
  438. @item 3.
  439. If the @i{cdr} of x is itself a @i{cons},
  440. it is made to be the current @i{cons}
  441. (@i{i.e.}, x becomes that @i{cons}),
  442. a @i{space}
  443. is printed,
  444. and step 2 is re-entered.
  445. @item 4.
  446. If the @i{cdr} of x is not @i{null},
  447. a @i{space},
  448. a @i{dot},
  449. a @i{space},
  450. and the @i{cdr} of x are printed.
  451. @item 5.
  452. A @i{right-parenthesis} is printed.
  453. @end table
  454. Actually, the above algorithm is only used when @b{*print-pretty*}
  455. is @i{false}. When @b{*print-pretty*} is @i{true} (or
  456. when @b{pprint} is used),
  457. additional @i{whitespace}_1
  458. may replace the use of a single @i{space},
  459. and a more elaborate algorithm with similar goals but more presentational
  460. flexibility is used; see @ref{Printer Dispatching}.
  461. Although the two expressions below are equivalent,
  462. and the reader accepts
  463. either one and
  464. produces
  465. the same @i{cons}, the printer
  466. always prints such a @i{cons} in the second form.
  467. @example
  468. (a . (b . ((c . (d . nil)) . (e . nil))))
  469. (a b (c d) e)
  470. @end example
  471. The printing of @i{conses} is affected by @b{*print-level*},
  472. @b{*print-length*}, and @b{*print-circle*}.
  473. Following are examples of printed representations of @i{lists}:
  474. @example
  475. (a . b) ;A dotted pair of a and b
  476. (a.b) ;A list of one element, the symbol named a.b
  477. (a. b) ;A list of two elements a. and b
  478. (a .b) ;A list of two elements a and .b
  479. (a b . c) ;A dotted list of a and b with c at the end; two conses
  480. .iot ;The symbol whose name is .iot
  481. (. b) ;Invalid -- an error is signaled if an attempt is made to read
  482. ;this syntax.
  483. (a .) ;Invalid -- an error is signaled.
  484. (a .. b) ;Invalid -- an error is signaled.
  485. (a . . b) ;Invalid -- an error is signaled.
  486. (a b c ...) ;Invalid -- an error is signaled.
  487. (a \. b) ;A list of three elements a, ., and b
  488. (a |.| b) ;A list of three elements a, ., and b
  489. (a \... b) ;A list of three elements a, ..., and b
  490. (a |...| b) ;A list of three elements a, ..., and b
  491. @end example
  492. For information on how the @i{Lisp reader} parses @i{lists} and @i{conses},
  493. see @ref{Left-Parenthesis}.
  494. @node Printing Bit Vectors, Printing Other Vectors, Printing Lists and Conses, Default Print-Object Methods
  495. @subsubsection Printing Bit Vectors
  496. A @i{bit vector} is printed as @t{#*} followed by the bits of the @i{bit vector}
  497. in order. If @b{*print-array*} is @i{false}, then the @i{bit vector} is
  498. printed in a format (using @t{#<}) that is concise but not readable.
  499. Only the @i{active} @i{elements} of the @i{bit vector} are printed.
  500. [Reviewer Note by Barrett: Need to provide for @t{#5*0} as an alternate
  501. notation for @t{#*00000}.]
  502. For information on @i{Lisp reader} parsing of @i{bit vectors},
  503. see @ref{Sharpsign Asterisk}.
  504. @node Printing Other Vectors, Printing Other Arrays, Printing Bit Vectors, Default Print-Object Methods
  505. @subsubsection Printing Other Vectors
  506. If @b{*print-array*} is @i{true}
  507. and @b{*print-readably*} is @i{false},
  508. any
  509. @i{vector}
  510. other than a @i{string} or @i{bit vector} is printed using
  511. general-vector syntax; this means that information
  512. about specialized vector representations does not appear.
  513. The printed representation of a zero-length @i{vector} is @t{#()}.
  514. The printed representation of a non-zero-length @i{vector} begins with @t{#(}.
  515. Following that, the first element of the @i{vector} is printed.
  516. If there are any other elements, they are printed in turn, with
  517. each such additional element preceded by
  518. a @i{space} if @b{*print-pretty*} is @i{false},
  519. or @i{whitespace}_1 if @b{*print-pretty*} is @i{true}.
  520. A @i{right-parenthesis} after the last element
  521. terminates the printed representation of the @i{vector}.
  522. The printing of @i{vectors}
  523. is affected by @b{*print-level*} and @b{*print-length*}.
  524. If the @i{vector} has a @i{fill pointer},
  525. then only those elements below
  526. the @i{fill pointer} are printed.
  527. If both @b{*print-array*} and @b{*print-readably*} are @i{false},
  528. the @i{vector} is not printed as described above,
  529. but in a format (using @t{#<}) that is concise but not readable.
  530. If @b{*print-readably*} is @i{true},
  531. the @i{vector} prints in an @i{implementation-defined} manner;
  532. see the @i{variable} @b{*print-readably*}.
  533. For information on how the @i{Lisp reader} parses these ``other @i{vectors},''
  534. see @ref{Sharpsign Left-Parenthesis}.
  535. @node Printing Other Arrays, Examples of Printing Arrays, Printing Other Vectors, Default Print-Object Methods
  536. @subsubsection Printing Other Arrays
  537. If @b{*print-array*} is @i{true}
  538. and @b{*print-readably*} is @i{false},
  539. any
  540. @i{array} other than a @i{vector} is printed
  541. using @t{#}@t{n}@t{A} format.
  542. Let @t{n} be the @i{rank} of the @i{array}.
  543. Then @t{#} is printed, then @t{n} as a decimal integer,
  544. then @t{A}, then @t{n} open parentheses.
  545. Next the @i{elements} are scanned in row-major order,
  546. using @b{write} on each @i{element},
  547. and separating @i{elements} from each other with @i{whitespace}_1.
  548. The array's dimensions are numbered 0 to @t{n}-1 from left to right,
  549. and are enumerated with the rightmost index changing fastest.
  550. Every time the index for dimension @t{j} is incremented,
  551. the following actions are taken:
  552. @table @asis
  553. @item @t{*}
  554. If @t{j} < @t{n}-1, then a close parenthesis is printed.
  555. @item @t{*}
  556. If incrementing the index for dimension @t{j} caused it to equal
  557. dimension @t{j}, that index is reset to zero and the
  558. index for dimension @t{j}-1 is incremented (thereby performing these three steps recursively),
  559. unless @t{j}=0, in which case the entire algorithm is terminated.
  560. If incrementing the index for dimension @t{j} did not cause it to
  561. equal dimension @t{j}, then a space is printed.
  562. @item @t{*}
  563. If @t{j} < @t{n}-1, then an open parenthesis is printed.
  564. @end table
  565. This causes the contents to be printed in a format suitable for
  566. @t{:initial-contents} to @b{make-array}.
  567. The lists effectively printed by this procedure are subject to
  568. truncation by @b{*print-level*} and @b{*print-length*}.
  569. If the @i{array}
  570. is of a specialized @i{type}, containing bits or characters,
  571. then the innermost lists generated by the algorithm given above can instead
  572. be printed using bit-vector or string syntax, provided that these innermost
  573. lists would not be subject to truncation by @b{*print-length*}.
  574. If both @b{*print-array*} and @b{*print-readably*} are @i{false},
  575. then the @i{array} is printed
  576. in a format (using @t{#<}) that is concise but not readable.
  577. If @b{*print-readably*} is @i{true},
  578. the @i{array} prints in an @i{implementation-defined} manner;
  579. see the @i{variable} @b{*print-readably*}.
  580. In particular,
  581. this may be important for arrays having some dimension @t{0}.
  582. For information on how the @i{Lisp reader} parses these ``other @i{arrays},''
  583. see @ref{Sharpsign A}.
  584. @node Examples of Printing Arrays, Printing Random States, Printing Other Arrays, Default Print-Object Methods
  585. @subsubsection Examples of Printing Arrays
  586. @example
  587. (let ((a (make-array '(3 3)))
  588. (*print-pretty* t)
  589. (*print-array* t))
  590. (dotimes (i 3) (dotimes (j 3) (setf (aref a i j) (format nil "<~D,~D>" i j))))
  591. (print a)
  592. (print (make-array 9 :displaced-to a)))
  593. @t{ |> } #2A(("<0,0>" "<0,1>" "<0,2>")
  594. @t{ |> } ("<1,0>" "<1,1>" "<1,2>")
  595. @t{ |> } ("<2,0>" "<2,1>" "<2,2>"))
  596. @t{ |> } #("<0,0>" "<0,1>" "<0,2>" "<1,0>" "<1,1>" "<1,2>" "<2,0>" "<2,1>" "<2,2>")
  597. @result{} #<ARRAY 9 indirect 36363476>
  598. @end example
  599. @node Printing Random States, Printing Pathnames, Examples of Printing Arrays, Default Print-Object Methods
  600. @subsubsection Printing Random States
  601. A specific syntax for printing @i{objects} of @i{type} @b{random-state} is
  602. not specified. However, every @i{implementation}
  603. must arrange to print a @i{random state} @i{object} in such a way that,
  604. within the same implementation, @b{read}
  605. can construct from the printed representation a copy of the
  606. @i{random state}
  607. object as if the copy had been made by @b{make-random-state}.
  608. If the type @i{random state} is effectively implemented
  609. by using the machinery for @b{defstruct},
  610. the usual structure syntax can then be used for printing
  611. @i{random state}
  612. objects; one might look something like
  613. @example
  614. #S(RANDOM-STATE :DATA #(14 49 98436589 786345 8734658324 ... ))
  615. @end example
  616. where the components are @i{implementation-dependent}.
  617. @node Printing Pathnames, Printing Structures, Printing Random States, Default Print-Object Methods
  618. @subsubsection Printing Pathnames
  619. When @i{printer escaping} is enabled,
  620. the syntax @t{#P"..."} is how a
  621. @i{pathname} is printed by @b{write} and the other functions herein described.
  622. The @t{"..."} is the namestring representation of the pathname.
  623. When @i{printer escaping} is disabled,
  624. @b{write} writes a @i{pathname} @i{P}
  625. by writing @t{(namestring @i{P})} instead.
  626. For information on how the @i{Lisp reader} parses @i{pathnames},
  627. see @ref{Sharpsign P}.
  628. @node Printing Structures, Printing Other Objects, Printing Pathnames, Default Print-Object Methods
  629. @subsubsection Printing Structures
  630. By default, a @i{structure} of type S is printed using @t{#S} syntax.
  631. This behavior can be customized by specifying a @t{:print-function}
  632. or @t{:print-object} option to the @b{defstruct} @i{form} that defines S,
  633. or by writing a @b{print-object} @i{method}
  634. that is @i{specialized} for @i{objects} of type S.
  635. Different structures might print out in different ways;
  636. the default notation for structures is:
  637. @example
  638. #S(@i{structure-name} @{@i{slot-key} @i{slot-value}@}{*})
  639. @end example
  640. where @t{#S} indicates structure syntax,
  641. @i{structure-name} is a @i{structure name},
  642. each @i{slot-key} is an initialization argument @i{name}
  643. for a @i{slot} in the @i{structure},
  644. and each corresponding @i{slot-value} is a representation
  645. of the @i{object} in that @i{slot}.
  646. For information on how the @i{Lisp reader} parses @i{structures},
  647. see @ref{Sharpsign S}.
  648. @node Printing Other Objects, , Printing Structures, Default Print-Object Methods
  649. @subsubsection Printing Other Objects
  650. Other @i{objects} are printed in an @i{implementation-dependent} manner.
  651. It is not required that an @i{implementation} print those @i{objects}
  652. @i{readably}.
  653. For example, @i{hash tables},
  654. @i{readtables},
  655. @i{packages},
  656. @i{streams},
  657. and @i{functions}
  658. might not print @i{readably}.
  659. A common notation to use in this circumstance is @t{#<...>}.
  660. Since @t{#<} is not readable by the @i{Lisp reader},
  661. the precise format of the text which follows is not important,
  662. but a common format to use is that provided by the @b{print-unreadable-object} @i{macro}.
  663. For information on how the @i{Lisp reader} treats this notation,
  664. see @ref{Sharpsign Less-Than-Sign}.
  665. For information on how to notate @i{objects} that cannot be printed @i{readably},
  666. see @ref{Sharpsign Dot}.
  667. @node Examples of Printer Behavior, , Default Print-Object Methods, The Lisp Printer
  668. @subsection Examples of Printer Behavior
  669. @example
  670. (let ((*print-escape* t)) (fresh-line) (write #\a))
  671. @t{ |> } #\a
  672. @result{} #\a
  673. (let ((*print-escape* nil) (*print-readably* nil))
  674. (fresh-line)
  675. (write #\a))
  676. @t{ |> } a
  677. @result{} #\a
  678. (progn (fresh-line) (prin1 #\a))
  679. @t{ |> } #\a
  680. @result{} #\a
  681. (progn (fresh-line) (print #\a))
  682. @t{ |> }
  683. @t{ |> } #\a
  684. @result{} #\a
  685. (progn (fresh-line) (princ #\a))
  686. @t{ |> } a
  687. @result{} #\a
  688. (dolist (val '(t nil))
  689. (let ((*print-escape* val) (*print-readably* val))
  690. (print '#\a)
  691. (prin1 #\a) (write-char #\Space)
  692. (princ #\a) (write-char #\Space)
  693. (write #\a)))
  694. @t{ |> } #\a #\a a #\a
  695. @t{ |> } #\a #\a a a
  696. @result{} NIL
  697. (progn (fresh-line) (write '(let ((a 1) (b 2)) (+ a b))))
  698. @t{ |> } (LET ((A 1) (B 2)) (+ A B))
  699. @result{} (LET ((A 1) (B 2)) (+ A B))
  700. (progn (fresh-line) (pprint '(let ((a 1) (b 2)) (+ a b))))
  701. @t{ |> } (LET ((A 1)
  702. @t{ |> } (B 2))
  703. @t{ |> } (+ A B))
  704. @result{} (LET ((A 1) (B 2)) (+ A B))
  705. (progn (fresh-line)
  706. (write '(let ((a 1) (b 2)) (+ a b)) :pretty t))
  707. @t{ |> } (LET ((A 1)
  708. @t{ |> } (B 2))
  709. @t{ |> } (+ A B))
  710. @result{} (LET ((A 1) (B 2)) (+ A B))
  711. (with-output-to-string (s)
  712. (write 'write :stream s)
  713. (prin1 'prin1 s))
  714. @result{} "WRITEPRIN1"
  715. @end example
  716. @c end of including concept-print
  717. @node The Lisp Pretty Printer, Formatted Output, The Lisp Printer, Printer
  718. @section The Lisp Pretty Printer
  719. @c including concept-pprint
  720. @menu
  721. * Pretty Printer Concepts::
  722. * Examples of using the Pretty Printer::
  723. * Notes about the Pretty Printer's Background::
  724. @end menu
  725. @node Pretty Printer Concepts, Examples of using the Pretty Printer, The Lisp Pretty Printer, The Lisp Pretty Printer
  726. @subsection Pretty Printer Concepts
  727. The facilities provided by the @i{pretty printer}
  728. @IGindex{pretty printer}
  729. permit
  730. @i{programs} to redefine the way in which @i{code} is displayed,
  731. and allow the full power of @i{pretty printing} to be applied
  732. to complex combinations of data structures.
  733. Whether any given style of output is in fact ``pretty'' is inherently a
  734. somewhat subjective issue. However, since the effect of the
  735. @i{pretty printer} can be customized by @i{conforming programs},
  736. the necessary flexibility is provided for individual @i{programs}
  737. to achieve an arbitrary degree of aesthetic control.
  738. By providing direct access to the mechanisms within the pretty printer
  739. that make dynamic decisions about layout, the macros and functions
  740. @b{pprint-logical-block}, @b{pprint-newline}, and
  741. @b{pprint-indent} make it possible to specify pretty printing
  742. layout rules as a part of any function that produces output. They also
  743. make it very easy for the detection of circularity and sharing, and
  744. abbreviation based on length and nesting depth to be supported by the
  745. function.
  746. The @i{pretty printer} is driven entirely by dispatch based on
  747. the @i{value} of @b{*print-pprint-dispatch*}.
  748. The @i{function} @b{set-pprint-dispatch} makes it possible
  749. for @i{conforming programs} to associate new pretty printing
  750. functions with a @i{type}.
  751. @menu
  752. * Dynamic Control of the Arrangement of Output::
  753. * Format Directive Interface::
  754. * Compiling Format Strings::
  755. * Pretty Print Dispatch Tables::
  756. * Pretty Printer Margins::
  757. @end menu
  758. @node Dynamic Control of the Arrangement of Output, Format Directive Interface, Pretty Printer Concepts, Pretty Printer Concepts
  759. @subsubsection Dynamic Control of the Arrangement of Output
  760. The actions of the @i{pretty printer} when a piece of output is too
  761. large to fit in the space available can be precisely controlled.
  762. Three concepts underlie
  763. the way these operations work---@i{logical blocks}
  764. @IGindex{logical blocks}
  765. ,
  766. @i{conditional newlines}
  767. @IGindex{conditional newlines}
  768. ,
  769. and @i{sections}
  770. @IGindex{sections}
  771. .
  772. Before proceeding further, it is important to define these terms.
  773. The first line of Figure 22--3 shows a schematic piece of output. Each of
  774. the characters in the output is represented by ``@t{-}''. The positions of
  775. conditional newlines are indicated by digits. The beginnings and ends of
  776. logical blocks are indicated by ``@t{<}'' and ``@t{>}'' respectively.
  777. The output as a whole is a logical block and the outermost section. This
  778. section is indicated by the @t{0}'s on the second line of Figure 1. Logical
  779. blocks nested within the output are specified by the macro
  780. @b{pprint-logical-block}. Conditional newline positions are specified
  781. by calls to @b{pprint-newline}. Each conditional newline defines
  782. two sections (one before it and one after it) and is associated with a
  783. third (the section immediately containing it).
  784. The section after a conditional newline consists of: all the output up to,
  785. but not including, (a) the next conditional newline immediately contained
  786. in the same logical block; or if (a) is not applicable, (b) the next
  787. newline that is at a lesser level of nesting in logical blocks; or if (b)
  788. is not applicable, (c) the end of the output.
  789. The section before a conditional newline consists of: all the output back
  790. to, but not including, (a) the previous conditional newline that is
  791. immediately contained in the same logical block; or if (a) is not
  792. applicable, (b) the beginning of the immediately containing logical block.
  793. The last four lines in Figure 1 indicate the sections before and after the
  794. four conditional newlines.
  795. The section immediately containing a conditional newline is the shortest
  796. section that contains the conditional newline in question. In Figure 22--3,
  797. the first conditional newline is immediately contained in the section
  798. marked with @t{0}'s, the second and third conditional newlines are immediately
  799. contained in the section before the fourth conditional newline, and the
  800. fourth conditional newline is immediately contained in the section after
  801. the first conditional newline.
  802. @example
  803. <-1---<--<--2---3->--4-->->
  804. 000000000000000000000000000
  805. 11 111111111111111111111111
  806. 22 222
  807. 333 3333
  808. 44444444444444 44444
  809. @end example
  810. @w{ Figure 22--2: Example of Logical Blocks, Conditional Newlines, and Sections}
  811. Whenever possible, the pretty printer displays the entire contents of a
  812. section on a single line. However, if the section is too long to fit in
  813. the space available, line breaks are inserted at conditional newline
  814. positions within the section.
  815. @node Format Directive Interface, Compiling Format Strings, Dynamic Control of the Arrangement of Output, Pretty Printer Concepts
  816. @subsubsection Format Directive Interface
  817. The primary interface to operations for dynamically determining the
  818. arrangement of output is provided through the functions and macros of the
  819. pretty printer. Figure 22--3 shows the defined names related to @i{pretty printing}.
  820. @group
  821. @noindent
  822. @w{ *print-lines* pprint-dispatch pprint-pop }
  823. @w{ *print-miser-width* pprint-exit-if-list-exhausted pprint-tab }
  824. @w{ *print-pprint-dispatch* pprint-fill pprint-tabular }
  825. @w{ *print-right-margin* pprint-indent set-pprint-dispatch }
  826. @w{ copy-pprint-dispatch pprint-linear write }
  827. @w{ format pprint-logical-block }
  828. @w{ formatter pprint-newline }
  829. @noindent
  830. @w{ Figure 22--3: Defined names related to pretty printing. }
  831. @end group
  832. Figure 22--4 identifies a set of @i{format directives} which serve
  833. as an alternate interface to the same pretty printing operations in a
  834. more textually compact form.
  835. @group
  836. @noindent
  837. @w{ @t{~I} @t{~W} @t{~<...~:>} }
  838. @w{ @t{~:T} @t{~/.../} @t{~_} }
  839. @noindent
  840. @w{ Figure 22--4: Format directives related to Pretty Printing}
  841. @end group
  842. @node Compiling Format Strings, Pretty Print Dispatch Tables, Format Directive Interface, Pretty Printer Concepts
  843. @subsubsection Compiling Format Strings
  844. A @i{format string} is essentially a program in a special-purpose language
  845. that performs printing, and that is interpreted by the @i{function} @b{format}.
  846. The @b{formatter} @i{macro} provides the efficiency of using a @i{compiled function}
  847. to do that same printing but without losing the textual compactness of @i{format strings}.
  848. A @i{format control}
  849. @IGindex{format control}
  850. is either a @i{format string} or a @i{function}
  851. that was returned by the the @b{formatter} @i{macro}.
  852. @node Pretty Print Dispatch Tables, Pretty Printer Margins, Compiling Format Strings, Pretty Printer Concepts
  853. @subsubsection Pretty Print Dispatch Tables
  854. A @i{pprint dispatch table}
  855. @IGindex{pprint dispatch table}
  856. is a mapping from keys to pairs of values.
  857. Each key is a @i{type specifier}.
  858. The values associated with a key are
  859. a ``function'' (specifically, a @i{function designator} or @b{nil})
  860. and a ``numerical priority'' (specifically, a @i{real}).
  861. Basic insertion and retrieval is done based on the keys with the equality
  862. of keys being tested by @b{equal}.
  863. When @b{*print-pretty*} is @i{true},
  864. the @i{current pprint dispatch table}
  865. @IGindex{current pprint dispatch table}
  866. (in @b{*print-pprint-dispatch*})
  867. controls how @i{objects} are printed.
  868. The information in this table takes precedence over
  869. all other mechanisms for specifying how to print @i{objects}.
  870. In particular, it
  871. has priority over
  872. user-defined @b{print-object} @i{methods}
  873. because the @i{current pprint dispatch table} is consulted first.
  874. The function is chosen from the @i{current pprint dispatch table}
  875. by finding the highest priority function
  876. that is associated with a @i{type specifier} that matches the @i{object};
  877. if there is more than one such function,
  878. it is @i{implementation-dependent} which is used.
  879. However, if there is no
  880. information in the table
  881. about how to @i{pretty print} a particular kind of @i{object},
  882. a @i{function} is invoked which uses @b{print-object} to print the @i{object}.
  883. The value of @b{*print-pretty*} is still @i{true}
  884. when this function is @i{called},
  885. and individual methods for @b{print-object} might still elect to
  886. produce output in a special format conditional on the @i{value} of @b{*print-pretty*}.
  887. @node Pretty Printer Margins, , Pretty Print Dispatch Tables, Pretty Printer Concepts
  888. @subsubsection Pretty Printer Margins
  889. A primary goal of pretty printing is to keep the output between a pair of
  890. margins.
  891. The column where the output begins is taken as the left margin.
  892. If the current column cannot be determined at the time output begins,
  893. the left margin is assumed to be zero.
  894. The right margin is controlled by @b{*print-right-margin*}.
  895. @node Examples of using the Pretty Printer, Notes about the Pretty Printer's Background, Pretty Printer Concepts, The Lisp Pretty Printer
  896. @subsection Examples of using the Pretty Printer
  897. As an example of the interaction of logical blocks, conditional newlines,
  898. and indentation, consider the function @t{simple-pprint-defun} below. This
  899. function prints out lists whose @i{cars} are @b{defun} in the
  900. standard way assuming that the list has exactly length @t{4}.
  901. @example
  902. (defun simple-pprint-defun (*standard-output* list)
  903. (pprint-logical-block (*standard-output* list :prefix "(" :suffix ")")
  904. (write (first list))
  905. (write-char #\Space)
  906. (pprint-newline :miser)
  907. (pprint-indent :current 0)
  908. (write (second list))
  909. (write-char #\Space)
  910. (pprint-newline :fill)
  911. (write (third list))
  912. (pprint-indent :block 1)
  913. (write-char #\Space)
  914. (pprint-newline :linear)
  915. (write (fourth list))))
  916. @end example
  917. Suppose that one evaluates the following:
  918. @example
  919. (simple-pprint-defun *standard-output* '(defun prod (x y) (* x y)))
  920. @end example
  921. If the line width available is greater than or equal to @t{26}, then all of the
  922. output appears on one line. If the line width available is reduced to @t{25},
  923. a line break is inserted at the
  924. linear-style conditional newline
  925. @ITindex{linear-style conditional newline}
  926. before the
  927. @i{expression} @t{(* x y)}, producing the output shown. The
  928. @t{(pprint-indent :block 1)} causes @t{(* x y)} to be printed at a relative
  929. indentation of @t{1} in the logical block.
  930. @example
  931. (DEFUN PROD (X Y)
  932. (* X Y))
  933. @end example
  934. If the line width available is @t{15}, a line break is also inserted at the
  935. fill style conditional newline before the argument list. The call on
  936. @t{(pprint-indent :current 0)} causes the argument list to line up under the
  937. function name.
  938. @example
  939. (DEFUN PROD
  940. (X Y)
  941. (* X Y))
  942. @end example
  943. If @b{*print-miser-width*} were greater than or equal to 14, the example
  944. output above would have been as follows, because all indentation changes
  945. are ignored in miser mode and line breaks are inserted at
  946. miser-style conditional newlines.
  947. @ITindex{miser-style conditional newline}
  948. @example
  949. (DEFUN
  950. PROD
  951. (X Y)
  952. (* X Y))
  953. @end example
  954. As an example of a per-line prefix, consider that evaluating the following
  955. produces the output shown with a line width of @t{20} and
  956. @b{*print-miser-width*} of @b{nil}.
  957. @example
  958. (pprint-logical-block (*standard-output* nil :per-line-prefix ";;; ")
  959. (simple-pprint-defun *standard-output* '(defun prod (x y) (* x y))))
  960. ;;; (DEFUN PROD
  961. ;;; (X Y)
  962. ;;; (* X Y))
  963. @end example
  964. As a more complex (and realistic) example, consider the function @t{pprint-let}
  965. below. This specifies how to print a @b{let} @i{form} in the traditional
  966. style. It is more complex than the example above, because it has to deal with
  967. nested structure. Also, unlike the example above it contains complete code to
  968. readably print any possible list that begins with the @i{symbol} @b{let}.
  969. The outermost @b{pprint-logical-block} @i{form} handles the printing of
  970. the input list as a whole and specifies that parentheses should be printed in the
  971. output. The second @b{pprint-logical-block} @i{form} handles the list
  972. of binding pairs. Each pair in the list is itself printed by the innermost
  973. @b{pprint-logical-block}. (A @b{loop} @i{form} is used instead of
  974. merely decomposing the pair into two @i{objects} so that readable output will
  975. be produced no matter whether the list corresponding to the pair has one element,
  976. two elements, or (being malformed) has more than two elements.)
  977. A space and a
  978. fill-style conditional newline
  979. @ITindex{fill-style conditional newline}
  980. are placed after
  981. each pair except the last. The loop at the end of the topmost
  982. @b{pprint-logical-block} @i{form} prints out the forms in the body
  983. of the @b{let} @i{form} separated by spaces and
  984. linear-style conditional newlines.
  985. @example
  986. (defun pprint-let (*standard-output* list)
  987. (pprint-logical-block (nil list :prefix "(" :suffix ")")
  988. (write (pprint-pop))
  989. (pprint-exit-if-list-exhausted)
  990. (write-char #\Space)
  991. (pprint-logical-block (nil (pprint-pop) :prefix "(" :suffix ")")
  992. (pprint-exit-if-list-exhausted)
  993. (loop (pprint-logical-block (nil (pprint-pop) :prefix "(" :suffix ")")
  994. (pprint-exit-if-list-exhausted)
  995. (loop (write (pprint-pop))
  996. (pprint-exit-if-list-exhausted)
  997. (write-char #\Space)
  998. (pprint-newline :linear)))
  999. (pprint-exit-if-list-exhausted)
  1000. (write-char #\Space)
  1001. (pprint-newline :fill)))
  1002. (pprint-indent :block 1)
  1003. (loop (pprint-exit-if-list-exhausted)
  1004. (write-char #\Space)
  1005. (pprint-newline :linear)
  1006. (write (pprint-pop)))))
  1007. @end example
  1008. Suppose that one evaluates the following with @b{*print-level*} being 4,
  1009. and @b{*print-circle*} being @i{true}.
  1010. @example
  1011. (pprint-let *standard-output*
  1012. '#1=(let (x (*print-length* (f (g 3)))
  1013. (z . 2) (k (car y)))
  1014. (setq x (sqrt z)) #1#))
  1015. @end example
  1016. If the line length is greater than or equal to @t{77}, the output produced
  1017. appears on one line. However, if the line length is @t{76}, line breaks are
  1018. inserted at the linear-style conditional newlines separating the forms in
  1019. the body and the output below is produced. Note that, the degenerate
  1020. binding pair @t{x} is printed readably even though it fails to be a list; a
  1021. depth abbreviation marker is printed in place of @t{(g 3)}; the binding pair
  1022. @t{(z . 2)} is printed readably even though it is not a proper list; and
  1023. appropriate circularity markers are printed.
  1024. @example
  1025. #1=(LET (X (*PRINT-LENGTH* (F #)) (Z . 2) (K (CAR Y)))
  1026. (SETQ X (SQRT Z))
  1027. #1#)
  1028. @end example
  1029. If the line length is reduced to @t{35}, a line break is inserted at one of the
  1030. fill-style conditional newlines separating the binding pairs.
  1031. @example
  1032. #1=(LET (X (*PRINT-PRETTY* (F #))
  1033. (Z . 2) (K (CAR Y)))
  1034. (SETQ X (SQRT Z))
  1035. #1#)
  1036. @end example
  1037. Suppose that the line length is further reduced to @t{22} and @b{*print-length*} is
  1038. set to @t{3}. In this situation, line breaks are inserted after both the first
  1039. and second binding pairs. In addition, the second binding pair is itself
  1040. broken across two lines. Clause (b) of the description of fill-style
  1041. conditional newlines (see the @i{function} @b{pprint-newline})
  1042. prevents the binding pair @t{(z . 2)} from being printed
  1043. at the end of the third line. Note that the length abbreviation hides the
  1044. circularity from view and therefore the printing of circularity markers
  1045. disappears.
  1046. @example
  1047. (LET (X
  1048. (*PRINT-LENGTH*
  1049. (F #))
  1050. (Z . 2) ...)
  1051. (SETQ X (SQRT Z))
  1052. ...)
  1053. @end example
  1054. The next function prints a vector using ``@t{#(...)}'' notation.
  1055. @example
  1056. (defun pprint-vector (*standard-output* v)
  1057. (pprint-logical-block (nil nil :prefix "#(" :suffix ")")
  1058. (let ((end (length v)) (i 0))
  1059. (when (plusp end)
  1060. (loop (pprint-pop)
  1061. (write (aref v i))
  1062. (if (= (incf i) end) (return nil))
  1063. (write-char #\Space)
  1064. (pprint-newline :fill))))))
  1065. @end example
  1066. Evaluating the following with a line length of 15 produces the output shown.
  1067. @example
  1068. (pprint-vector *standard-output* '#(12 34 567 8 9012 34 567 89 0 1 23))
  1069. #(12 34 567 8
  1070. 9012 34 567
  1071. 89 0 1 23)
  1072. @end example
  1073. As examples of the convenience of specifying pretty printing with
  1074. @i{format strings}, consider that the functions @t{simple-pprint-defun}
  1075. and @t{pprint-let} used as examples above can be compactly defined as follows.
  1076. (The function @t{pprint-vector} cannot be defined using @b{format}
  1077. because the data structure it traverses is not a list.)
  1078. @example
  1079. (defun simple-pprint-defun (*standard-output* list)
  1080. (format T "~:<~W ~@@_~:I~W ~:_~W~1I ~_~W~:>" list))
  1081. (defun pprint-let (*standard-output* list)
  1082. (format T "~:<~W~{@t{^}}~:<~@@@{~:<~@@@{~W~{@t{^}}~_~@}~:>~{@t{^}}~:_~@}~:>~1I~@@@{~{@t{^}}~_~W~@}~:>" list))
  1083. @end example
  1084. In the following example, the first @i{form} restores
  1085. @b{*print-pprint-dispatch*} to the equivalent of its initial value.
  1086. The next two forms then set up a special way to pretty print ratios.
  1087. Note that the more specific @i{type specifier} has to be associated
  1088. with a higher priority.
  1089. @example
  1090. (setq *print-pprint-dispatch* (copy-pprint-dispatch nil))
  1091. (set-pprint-dispatch 'ratio
  1092. #'(lambda (s obj)
  1093. (format s "#.(/ ~W ~W)"
  1094. (numerator obj) (denominator obj))))
  1095. (set-pprint-dispatch '(and ratio (satisfies minusp))
  1096. #'(lambda (s obj)
  1097. (format s "#.(- (/ ~W ~W))"
  1098. (- (numerator obj)) (denominator obj)))
  1099. 5)
  1100. (pprint '(1/3 -2/3))
  1101. (#.(/ 1 3) #.(- (/ 2 3)))
  1102. @end example
  1103. The following two @i{forms} illustrate the definition of
  1104. pretty printing functions for types of @i{code}. The first
  1105. @i{form} illustrates how to specify the traditional method
  1106. for printing quoted objects using @i{single-quote}. Note
  1107. the care taken to ensure that data lists that happen to begin
  1108. with @b{quote} will be printed readably. The second form
  1109. specifies that lists beginning with the symbol @t{my-let}
  1110. should print the same way that lists beginning with @b{let}
  1111. print when the initial @i{pprint dispatch table} is in effect.
  1112. @example
  1113. (set-pprint-dispatch '(cons (member quote)) ()
  1114. #'(lambda (s list)
  1115. (if (and (consp (cdr list)) (null (cddr list)))
  1116. (funcall (formatter "'~W") s (cadr list))
  1117. (pprint-fill s list))))
  1118. (set-pprint-dispatch '(cons (member my-let))
  1119. (pprint-dispatch '(let) nil))
  1120. @end example
  1121. The next example specifies a default method for printing lists that do not
  1122. correspond to function calls. Note that the functions @b{pprint-linear},
  1123. @b{pprint-fill}, and @b{pprint-tabular} are all defined with
  1124. optional @i{colon-p} and @i{at-sign-p} arguments so that they can
  1125. be used as @b{pprint dispatch functions} as well as @t{~/.../}
  1126. functions.
  1127. @example
  1128. (set-pprint-dispatch '(cons (not (and symbol (satisfies fboundp))))
  1129. #'pprint-fill -5)
  1130. ;; Assume a line length of 9
  1131. (pprint '(0 b c d e f g h i j k))
  1132. (0 b c d
  1133. e f g h
  1134. i j k)
  1135. @end example
  1136. This final example shows how to define a pretty printing function for a
  1137. user defined data structure.
  1138. @example
  1139. (defstruct family mom kids)
  1140. (set-pprint-dispatch 'family
  1141. #'(lambda (s f)
  1142. (funcall (formatter "~@@<#<~;~W and ~2I~_~/pprint-fill/~;>~:>")
  1143. s (family-mom f) (family-kids f))))
  1144. @end example
  1145. The pretty printing function for the structure @t{family} specifies how to
  1146. adjust the layout of the output so that it can fit aesthetically into
  1147. a variety of line widths. In addition, it obeys
  1148. the printer control variables @b{*print-level*},
  1149. @b{*print-length*}, @b{*print-lines*},
  1150. @b{*print-circle*}
  1151. and @b{*print-escape*},
  1152. and can tolerate several different kinds of malformity in the data structure.
  1153. The output below shows what is printed out with a right margin of @t{25},
  1154. @b{*print-pretty*} being @i{true}, @b{*print-escape*} being @i{false},
  1155. and a malformed @t{kids} list.
  1156. @example
  1157. (write (list 'principal-family
  1158. (make-family :mom "Lucy"
  1159. :kids '("Mark" "Bob" . "Dan")))
  1160. :right-margin 25 :pretty T :escape nil :miser-width nil)
  1161. (PRINCIPAL-FAMILY
  1162. #<Lucy and
  1163. Mark Bob . Dan>)
  1164. @end example
  1165. Note that a pretty printing function for a structure is different from
  1166. the structure's @b{print-object} @i{method}.
  1167. While
  1168. @b{print-object} @i{methods}
  1169. are permanently associated with a structure,
  1170. pretty printing functions are stored in
  1171. @i{pprint dispatch tables} and can be rapidly changed to reflect
  1172. different printing needs. If there is no pretty printing function for
  1173. a structure in the current @i{pprint dispatch table},
  1174. its @b{print-object} @i{method}
  1175. is used instead.
  1176. @node Notes about the Pretty Printer's Background, , Examples of using the Pretty Printer, The Lisp Pretty Printer
  1177. @subsection Notes about the Pretty Printer's Background
  1178. For a background reference to the abstract concepts detailed in this
  1179. section, see @i{XP: A Common Lisp Pretty Printing System}. The details of that paper are not binding on
  1180. this document, but may be helpful in establishing a conceptual basis for
  1181. understanding this material.
  1182. @c end of including concept-pprint
  1183. @node Formatted Output, Printer Dictionary, The Lisp Pretty Printer, Printer
  1184. @section Formatted Output
  1185. @c including concept-format
  1186. [Editorial Note by KMP: This is transplanted from FORMAT and will need a bit of work before
  1187. it looks good standing alone. Bear with me.]
  1188. @b{format} is useful for producing nicely formatted text, producing
  1189. good-looking messages, and so on. @b{format} can generate and return
  1190. a @i{string} or output to @i{destination}.
  1191. The @i{control-string} argument to @b{format} is actually a @i{format control}.
  1192. That is, it can be either a @i{format string} or a @i{function},
  1193. for example a @i{function} returned
  1194. by the @b{formatter} @i{macro}.
  1195. If it is a @i{function}, the @i{function} is called with the appropriate
  1196. output stream as its first argument and the data arguments to @b{format}
  1197. as its remaining arguments. The function should perform whatever output is
  1198. necessary and return the unused tail of the arguments (if any).
  1199. The compilation process performed by @b{formatter} produces a @i{function}
  1200. that would do with its @i{arguments} as the @b{format} interpreter
  1201. would do with those @i{arguments}.
  1202. The remainder of this section describes what happens if the @i{control-string}
  1203. is a @i{format string}.
  1204. @i{Control-string} is composed of simple text (@i{characters})
  1205. and embedded directives.
  1206. @b{format} writes the simple text as is;
  1207. each embedded directive specifies further text output
  1208. that is to appear at the corresponding point within the simple text.
  1209. Most directives use one or more elements of @i{args} to
  1210. create their output.
  1211. A directive consists of a @i{tilde},
  1212. optional prefix parameters
  1213. separated by commas, optional @i{colon} and @i{at-sign} modifiers,
  1214. and a single character indicating what kind of directive this is.
  1215. There is no required ordering between the @i{at-sign} and @i{colon} modifier.
  1216. The @i{case} of the directive character is ignored.
  1217. Prefix parameters are notated as signed (sign is optional) decimal numbers,
  1218. or as a @i{single-quote} followed by a character.
  1219. For example, @t{~5,'0d} can be used
  1220. to print an @i{integer}
  1221. in decimal radix in five columns with leading zeros,
  1222. or @t{~5,'*d} to get leading asterisks.
  1223. In place of a prefix parameter to a directive, @t{V} (or @t{v}) can be used.
  1224. In this case, @b{format} takes an argument from @i{args} as a parameter to
  1225. the directive. The argument should be an @i{integer} or @i{character}.
  1226. If the @i{arg} used by a @t{V} parameter is @b{nil},
  1227. the effect is as if the parameter had been omitted.
  1228. @t{#} can be used in place of a prefix parameter; it
  1229. represents the number of @i{args} remaining to be processed.
  1230. When used within a recursive format, in the context of @t{~?} or @t{~@{},
  1231. the @t{#} prefix parameter represents the number of @i{format arguments}
  1232. remaining within the recursive call.
  1233. Examples of @i{format strings}:
  1234. @group
  1235. @noindent
  1236. @w{ @t{"~S"} ;This is an S directive with no parameters or modifiers. }
  1237. @w{ @t{"~3,-4:@@s"} ;This is an S directive with two parameters, @t{3} and @t{-4}, }
  1238. @w{ ; and both the @i{colon} and @i{at-sign} flags. }
  1239. @w{ @t{"~,+4S"} ;Here the first prefix parameter is omitted and takes }
  1240. @w{ ; on its default value, while the second parameter is @t{4}. }
  1241. @noindent
  1242. @w{ Figure 22--5: Examples of format control strings }
  1243. @end group
  1244. @b{format} sends the output to @i{destination}.
  1245. If @i{destination} is @b{nil},
  1246. @b{format} creates and returns a @i{string}
  1247. containing the output from @i{control-string}.
  1248. If @i{destination} is @i{non-nil},
  1249. it must be a @i{string} with a @i{fill pointer},
  1250. a @i{stream}, or the symbol @b{t}.
  1251. If @i{destination} is a @i{string} with a @i{fill pointer},
  1252. the output is added to the end of the @i{string}.
  1253. If @i{destination} is a @i{stream},
  1254. the output is sent to that @i{stream}.
  1255. If @i{destination} is @b{t},
  1256. the output is sent to @i{standard output}.
  1257. In the description of the directives that follows,
  1258. the term @i{arg} in general
  1259. refers to the next item of the set of @i{args} to be processed.
  1260. The word or phrase at the beginning of each description is a mnemonic
  1261. for the directive.
  1262. @b{format} directives do not bind any of the printer control
  1263. variables (@b{*print-...*}) except as specified in the following
  1264. descriptions.
  1265. Implementations may specify the binding of new, implementation-specific
  1266. printer control variables for each @b{format} directive, but they
  1267. may neither bind any standard printer control variables not
  1268. specified in description of a @b{format}
  1269. directive nor fail to bind
  1270. any standard printer control variables as specified in the
  1271. description.
  1272. @menu
  1273. * FORMAT Basic Output::
  1274. * FORMAT Radix Control::
  1275. * FORMAT Floating-Point Printers::
  1276. * FORMAT Printer Operations::
  1277. * FORMAT Pretty Printer Operations::
  1278. * FORMAT Layout Control::
  1279. * FORMAT Control-Flow Operations::
  1280. * FORMAT Miscellaneous Operations::
  1281. * FORMAT Miscellaneous Pseudo-Operations::
  1282. * Additional Information about FORMAT Operations::
  1283. * Examples of FORMAT::
  1284. * Notes about FORMAT::
  1285. @end menu
  1286. @node FORMAT Basic Output, FORMAT Radix Control, Formatted Output, Formatted Output
  1287. @subsection FORMAT Basic Output
  1288. @menu
  1289. * Tilde C-> Character::
  1290. * Tilde Percent-> Newline::
  1291. * Tilde Ampersand-> Fresh-Line::
  1292. * Tilde Vertical-Bar-> Page::
  1293. * Tilde Tilde-> Tilde::
  1294. @end menu
  1295. @node Tilde C-> Character, Tilde Percent-> Newline, FORMAT Basic Output, FORMAT Basic Output
  1296. @subsubsection Tilde C: Character
  1297. The next @i{arg} should be a @i{character};
  1298. it is printed
  1299. according to the modifier flags.
  1300. @t{~C} prints the @i{character}
  1301. as if by using @b{write-char} if it is a @i{simple character}.
  1302. @i{Characters} that are not @i{simple}
  1303. are not necessarily printed as if by @b{write-char},
  1304. but are displayed in an @i{implementation-defined}, abbreviated format.
  1305. For example,
  1306. @example
  1307. (format nil "~C" #\A) @result{} "A"
  1308. (format nil "~C" #\Space) @result{} " "
  1309. @end example
  1310. @t{~:C} is the same as @t{~C} for @i{printing} @i{characters},
  1311. but other @i{characters} are ``spelled out.'' The intent is that this
  1312. is a ``pretty'' format for printing characters.
  1313. For @i{simple} @i{characters} that are not @i{printing},
  1314. what is spelled out is the @i{name} of the @i{character} (see @b{char-name}).
  1315. For @i{characters} that are not @i{simple} and not @i{printing},
  1316. what is spelled out is @i{implementation-defined}.
  1317. For example,
  1318. @example
  1319. (format nil "~:C" #\A) @result{} "A"
  1320. (format nil "~:C" #\Space) @result{} "Space"
  1321. ;; This next example assumes an implementation-defined "Control" attribute.
  1322. (format nil "~:C" #\Control-Space)
  1323. @result{} "Control-Space"
  1324. @i{OR}@result{} "c-Space"
  1325. @end example
  1326. @t{~:@@C} prints what @t{~:C} would, and then
  1327. if the @i{character} requires unusual shift keys on the keyboard to type it,
  1328. this fact is mentioned. For example,
  1329. @example
  1330. (format nil "~:@@C" #\Control-Partial) @result{} "Control-{\partial} (Top-F)"
  1331. @end example
  1332. This is the format used for telling the user about a key he is expected to type,
  1333. in prompts, for instance. The precise output may depend not only
  1334. on the implementation, but on the particular I/O devices in use.
  1335. @t{~@@C}
  1336. prints the @i{character} in a way that the @i{Lisp reader} can understand,
  1337. using @t{#\} syntax.
  1338. @t{~@@C} binds @b{*print-escape*} to @b{t}.
  1339. @node Tilde Percent-> Newline, Tilde Ampersand-> Fresh-Line, Tilde C-> Character, FORMAT Basic Output
  1340. @subsubsection Tilde Percent: Newline
  1341. This outputs a @t{#\Newline} character, thereby terminating the current
  1342. output line and beginning a new one.
  1343. @t{~@i{n}%} outputs @i{n} newlines.
  1344. No @i{arg} is used.
  1345. @node Tilde Ampersand-> Fresh-Line, Tilde Vertical-Bar-> Page, Tilde Percent-> Newline, FORMAT Basic Output
  1346. @subsubsection Tilde Ampersand: Fresh-Line
  1347. Unless it can be determined that the output stream
  1348. is already at the beginning of a line,
  1349. this outputs a newline.
  1350. @t{~@i{n}&} calls @b{fresh-line}
  1351. and then outputs @i{n}- 1 newlines.
  1352. @t{~0&} does nothing.
  1353. @node Tilde Vertical-Bar-> Page, Tilde Tilde-> Tilde, Tilde Ampersand-> Fresh-Line, FORMAT Basic Output
  1354. @subsubsection Tilde Vertical-Bar: Page
  1355. This outputs a page separator character, if possible.
  1356. @t{~@i{n}|} does this @i{n} times.
  1357. @node Tilde Tilde-> Tilde, , Tilde Vertical-Bar-> Page, FORMAT Basic Output
  1358. @subsubsection Tilde Tilde: Tilde
  1359. This outputs a @i{tilde}. @t{~@i{n}~} outputs @i{n} tildes.
  1360. @node FORMAT Radix Control, FORMAT Floating-Point Printers, FORMAT Basic Output, Formatted Output
  1361. @subsection FORMAT Radix Control
  1362. @menu
  1363. * Tilde R-> Radix::
  1364. * Tilde D-> Decimal::
  1365. * Tilde B-> Binary::
  1366. * Tilde O-> Octal::
  1367. * Tilde X-> Hexadecimal::
  1368. @end menu
  1369. @node Tilde R-> Radix, Tilde D-> Decimal, FORMAT Radix Control, FORMAT Radix Control
  1370. @subsubsection Tilde R: Radix
  1371. @t{~@i{n}R} prints @i{arg} in radix @i{n}.
  1372. The modifier flags and any remaining parameters are used as for
  1373. the @t{~D} directive.
  1374. @t{~D} is the same as @t{~10R}.
  1375. The full form is
  1376. @t{~@i{radix},@i{mincol},@i{padchar},@i{commachar},@i{comma-interval}R}.
  1377. If no prefix parameters are given to @t{~R}, then a different
  1378. interpretation is given. The argument should be an @i{integer}.
  1379. For example, if @i{arg} is 4:
  1380. @table @asis
  1381. @item @t{*}
  1382. @t{~R} prints @i{arg} as a cardinal English number: @t{four}.
  1383. @item @t{*}
  1384. @t{~:R} prints @i{arg} as an ordinal English number: @t{fourth}.
  1385. @item @t{*}
  1386. @t{~@@R} prints @i{arg} as a Roman numeral: @t{IV}.
  1387. @item @t{*}
  1388. @t{~:@@R} prints @i{arg} as an old Roman numeral: @t{IIII}.
  1389. @end table
  1390. For example:
  1391. @example
  1392. (format nil "~,,' ,4:B" 13) @result{} "1101"
  1393. (format nil "~,,' ,4:B" 17) @result{} "1 0001"
  1394. (format nil "~19,0,' ,4:B" 3333) @result{} "0000 1101 0000 0101"
  1395. (format nil "~3,,,' ,2:R" 17) @result{} "1 22"
  1396. (format nil "~,,'|,2:D" #xFFFF) @result{} "6|55|35"
  1397. @end example
  1398. If and only if the first parameter, @i{n}, is supplied,
  1399. @t{~R} binds
  1400. @b{*print-escape*} to @i{false},
  1401. @b{*print-radix*} to @i{false},
  1402. @b{*print-base*} to @i{n},
  1403. and @b{*print-readably*} to @i{false}.
  1404. If and only if no parameters are supplied,
  1405. @t{~R} binds @b{*print-base*} to @t{10}.
  1406. @node Tilde D-> Decimal, Tilde B-> Binary, Tilde R-> Radix, FORMAT Radix Control
  1407. @subsubsection Tilde D: Decimal
  1408. An @i{arg}, which should be an @i{integer},
  1409. is printed in decimal radix.
  1410. @t{~D} will never put a decimal point after the number.
  1411. @t{~@i{mincol}D} uses
  1412. a column width of @i{mincol}; spaces are inserted on
  1413. the left if the number requires fewer than @i{mincol} columns for its digits
  1414. and sign. If the number doesn't fit in @i{mincol} columns, additional columns
  1415. are used as needed.
  1416. @t{~@i{mincol},@i{padchar}D} uses @i{padchar} as the pad character
  1417. instead of space.
  1418. If @i{arg} is not an @i{integer}, it is printed in @t{~A} format and decimal base.
  1419. The @t{@@} modifier causes the number's sign to be printed always; the default
  1420. is to print it only if the number is negative.
  1421. The @t{:} modifier causes commas to be printed between groups of digits;
  1422. @i{commachar} may be used to change the character used as the comma.
  1423. @i{comma-interval}
  1424. must be an @i{integer} and defaults to 3. When the @t{:}
  1425. modifier is given to any of
  1426. these directives, the @i{commachar}
  1427. is printed between groups of @i{comma-interval}
  1428. digits.
  1429. Thus the most general form of @t{~D} is
  1430. @t{~@i{mincol},@i{padchar},@i{commachar},@i{comma-interval}D}.
  1431. @t{~D} binds
  1432. @b{*print-escape*} to @i{false},
  1433. @b{*print-radix*} to @i{false},
  1434. @b{*print-base*} to @t{10},
  1435. and @b{*print-readably*} to @i{false}.
  1436. @node Tilde B-> Binary, Tilde O-> Octal, Tilde D-> Decimal, FORMAT Radix Control
  1437. @subsubsection Tilde B: Binary
  1438. This is just like @t{~D} but prints in binary radix (radix 2)
  1439. instead of decimal. The full form is therefore
  1440. @t{~@i{mincol},@i{padchar},@i{commachar},@i{comma-interval}B}.
  1441. @t{~B} binds
  1442. @b{*print-escape*} to @i{false},
  1443. @b{*print-radix*} to @i{false},
  1444. @b{*print-base*} to @t{2},
  1445. and @b{*print-readably*} to @i{false}.
  1446. @node Tilde O-> Octal, Tilde X-> Hexadecimal, Tilde B-> Binary, FORMAT Radix Control
  1447. @subsubsection Tilde O: Octal
  1448. This is just like @t{~D} but prints in octal radix (radix 8)
  1449. instead of decimal. The full form is therefore
  1450. @t{~@i{mincol},@i{padchar},@i{commachar},@i{comma-interval}O}.
  1451. @t{~O} binds
  1452. @b{*print-escape*} to @i{false},
  1453. @b{*print-radix*} to @i{false},
  1454. @b{*print-base*} to @t{8},
  1455. and @b{*print-readably*} to @i{false}.
  1456. @node Tilde X-> Hexadecimal, , Tilde O-> Octal, FORMAT Radix Control
  1457. @subsubsection Tilde X: Hexadecimal
  1458. This is just like @t{~D} but prints in hexadecimal radix
  1459. (radix 16) instead of decimal. The full form is therefore
  1460. @t{~@i{mincol},@i{padchar},@i{commachar},@i{comma-interval}X}.
  1461. @t{~X} binds
  1462. @b{*print-escape*} to @i{false},
  1463. @b{*print-radix*} to @i{false},
  1464. @b{*print-base*} to @t{16},
  1465. and @b{*print-readably*} to @i{false}.
  1466. @node FORMAT Floating-Point Printers, FORMAT Printer Operations, FORMAT Radix Control, Formatted Output
  1467. @subsection FORMAT Floating-Point Printers
  1468. @menu
  1469. * Tilde F-> Fixed-Format Floating-Point::
  1470. * Tilde E-> Exponential Floating-Point::
  1471. * Tilde G-> General Floating-Point::
  1472. * Tilde Dollarsign-> Monetary Floating-Point::
  1473. @end menu
  1474. @node Tilde F-> Fixed-Format Floating-Point, Tilde E-> Exponential Floating-Point, FORMAT Floating-Point Printers, FORMAT Floating-Point Printers
  1475. @subsubsection Tilde F: Fixed-Format Floating-Point
  1476. The next @i{arg} is printed as a @i{float}.
  1477. The full form is @t{~@i{w},@i{d},@i{k},@i{overflowchar},@i{padchar}F}.
  1478. The parameter @i{w}
  1479. is the width of the field to be printed; @i{d} is the number
  1480. of digits to print after the decimal point; @i{k} is a scale factor
  1481. that defaults to zero.
  1482. Exactly @i{w} characters will
  1483. be output. First, leading copies of the character @i{padchar}
  1484. (which defaults to a space) are printed, if necessary, to pad the
  1485. field on the left.
  1486. If the @i{arg} is negative, then a minus sign is printed;
  1487. if the @i{arg} is not negative, then a plus sign is printed
  1488. if and only if the @t{@@}
  1489. modifier was supplied. Then a sequence
  1490. of digits, containing a single embedded decimal point, is printed;
  1491. this represents the magnitude of the value of @i{arg} times 10^@i{k},
  1492. rounded to @i{d} fractional digits.
  1493. When rounding up and rounding down would produce printed values
  1494. equidistant from the scaled value of @i{arg}, then the implementation
  1495. is free to use either one. For example, printing the argument
  1496. @t{6.375} using the format @t{~4,2F} may correctly produce
  1497. either @t{6.37} or @t{6.38}.
  1498. Leading zeros are not permitted, except that a single
  1499. zero digit is output before the decimal point if the printed value
  1500. is less than one, and this single zero digit is not output
  1501. at all if @i{w}=@i{d}+1.
  1502. If it is impossible to print the value in the required format in a field
  1503. of width @i{w}, then one of two actions is taken. If the
  1504. parameter @i{overflowchar} is supplied, then @i{w} copies of that
  1505. parameter are printed instead of the scaled value of @i{arg}.
  1506. If the @i{overflowchar} parameter is omitted, then the scaled value
  1507. is printed using more than @i{w} characters, as many more as may be
  1508. needed.
  1509. If the @i{w} parameter is omitted, then the field is of variable width.
  1510. In effect, a value is chosen
  1511. for @i{w} in such a way that no leading pad characters need to be printed
  1512. and exactly @i{d} characters will follow the decimal point.
  1513. For example, the directive @t{~,2F} will print exactly
  1514. two digits after the decimal point and as many as necessary before the
  1515. decimal point.
  1516. If the parameter @i{d} is omitted, then there is no constraint
  1517. on the number of digits to appear after the decimal point.
  1518. A value is chosen for @i{d} in such a way that as many digits
  1519. as possible may be printed subject to the width constraint
  1520. imposed by the parameter @i{w} and the constraint that no trailing
  1521. zero digits may appear in the fraction, except that if the
  1522. fraction to be printed is zero, then a single zero digit should
  1523. appear after the decimal point if permitted by the width constraint.
  1524. If both @i{w} and @i{d} are omitted, then the effect is to print
  1525. the value using ordinary free-format output; @b{prin1} uses this format
  1526. for any number whose magnitude is either zero or between
  1527. 10^{-3} (inclusive) and 10^7 (exclusive).
  1528. If @i{w} is omitted, then if the magnitude of @i{arg} is so large (or, if
  1529. @i{d} is also omitted, so small) that more than 100 digits would have to
  1530. be printed, then an implementation is free, at its discretion, to print
  1531. the number using exponential notation instead, as if by the directive
  1532. @t{~E} (with all parameters to @t{~E} defaulted, not
  1533. taking their values from the @t{~F} directive).
  1534. If @i{arg} is a @i{rational}
  1535. number, then it is coerced to be a @i{single float}
  1536. and then printed. Alternatively, an implementation is permitted to
  1537. process a @i{rational}
  1538. number by any other method that has essentially the
  1539. same behavior but avoids loss of precision or overflow
  1540. because of the coercion. If @i{w} and @i{d} are
  1541. not supplied and the number has no exact decimal representation,
  1542. for example @t{1/3}, some precision cutoff must be chosen
  1543. by the implementation since only a finite number of digits may be printed.
  1544. If @i{arg} is a @i{complex} number or some non-numeric
  1545. @i{object},
  1546. then it is printed using the format directive @t{~@i{w}D},
  1547. thereby printing it in decimal radix and a minimum field width of @i{w}.
  1548. @t{~F} binds
  1549. @b{*print-escape*} to @i{false}
  1550. and @b{*print-readably*} to @i{false}.
  1551. @node Tilde E-> Exponential Floating-Point, Tilde G-> General Floating-Point, Tilde F-> Fixed-Format Floating-Point, FORMAT Floating-Point Printers
  1552. @subsubsection Tilde E: Exponential Floating-Point
  1553. The next @i{arg} is printed as a @i{float} in exponential notation.
  1554. The full form is
  1555. @t{~@i{w},@i{d},@i{e},@i{k},@i{overflowchar},@i{padchar},@i{exponentchar}E}.
  1556. The parameter @i{w}
  1557. is the width of the field to be printed; @i{d} is the number
  1558. of digits to print after the decimal point; @i{e} is the number
  1559. of digits to use when printing the exponent;
  1560. @i{k} is a scale factor that defaults to one (not zero).
  1561. Exactly @i{w} characters will
  1562. be output. First, leading copies of the character @i{padchar}
  1563. (which defaults to a space) are printed, if necessary, to pad the
  1564. field on the left.
  1565. If the @i{arg} is negative, then a minus sign is printed;
  1566. if the @i{arg} is not negative, then a plus sign is printed
  1567. if and only if the @t{@@}
  1568. modifier was supplied. Then a sequence
  1569. of digits containing a single embedded decimal point is printed.
  1570. The form of this sequence of digits depends on the scale factor @i{k}.
  1571. If @i{k} is zero, then @i{d} digits are printed after the decimal
  1572. point, and a single zero digit appears before the decimal point if
  1573. the total field width will permit it. If @i{k} is positive,
  1574. then it must be strictly less than @i{d}+2; @i{k} significant digits
  1575. are printed before the decimal point, and @i{d}- @i{k}+1
  1576. digits are printed after the decimal point. If @i{k} is negative,
  1577. then it must be strictly greater than - @i{d};
  1578. a single zero digit appears before the decimal point if
  1579. the total field width will permit it, and after the decimal point
  1580. are printed first
  1581. - @i{k} zeros and then @i{d}+@i{k} significant digits.
  1582. The printed fraction must be properly rounded.
  1583. When rounding up and rounding down would produce printed values
  1584. equidistant from the scaled value of @i{arg}, then the implementation
  1585. is free to use either one. For example, printing the argument
  1586. @t{637.5} using the format @t{~8,2E} may correctly produce
  1587. either @t{6.37E+2} or @t{6.38E+2}.
  1588. Following the digit sequence, the exponent is printed.
  1589. First the character parameter @i{exponentchar} is printed; if this
  1590. parameter is omitted, then the @i{exponent marker} that
  1591. @b{prin1} would use is printed, as determined from the
  1592. type of the @i{float} and the current value of
  1593. @b{*read-default-float-format*}.
  1594. Next, either a plus sign or a minus sign
  1595. is printed, followed by @i{e} digits representing the power of
  1596. ten by which the printed fraction must be multiplied
  1597. to properly represent the rounded value of @i{arg}.
  1598. If it is impossible to print the value in the required format in a field
  1599. of width @i{w}, possibly because @i{k} is too large or too small
  1600. or because the exponent cannot be printed in @i{e} character positions,
  1601. then one of two actions is taken. If the
  1602. parameter @i{overflowchar} is supplied, then @i{w} copies of that
  1603. parameter are printed instead of the scaled value of @i{arg}.
  1604. If the @i{overflowchar} parameter is omitted, then the scaled value
  1605. is printed using more than @i{w} characters, as many more as may be
  1606. needed; if the problem is that @i{d} is too small for the supplied @i{k}
  1607. or that @i{e} is too small, then a larger value is used for @i{d} or @i{e}
  1608. as may be needed.
  1609. If the @i{w} parameter is omitted, then the field is of variable width.
  1610. In effect a value is chosen
  1611. for @i{w} in such a way that no leading pad characters need to be printed.
  1612. If the parameter @i{d} is omitted, then there is no constraint
  1613. on the number of digits to appear.
  1614. A value is chosen for @i{d} in such a way that as many digits
  1615. as possible may be printed subject to the width constraint
  1616. imposed by the parameter @i{w}, the constraint of the scale factor @i{k},
  1617. and the constraint that no trailing
  1618. zero digits may appear in the fraction, except that if the
  1619. fraction to be printed is zero then a single zero digit should
  1620. appear after the decimal point.
  1621. If the parameter @i{e} is omitted, then the exponent is printed
  1622. using the smallest number of digits necessary to represent its value.
  1623. If all of @i{w}, @i{d}, and @i{e} are omitted, then the effect is to print
  1624. the value using ordinary free-format exponential-notation output;
  1625. @b{prin1} uses
  1626. a similar
  1627. format for any non-zero number whose magnitude
  1628. is less than 10^{-3} or greater than or equal to 10^7.
  1629. The only difference is that the @t{~E}
  1630. directive always prints a plus or minus sign in front of the
  1631. exponent, while @b{prin1} omits the plus sign if the exponent is
  1632. non-negative.
  1633. If @i{arg} is a @i{rational}
  1634. number, then it is coerced to be a @i{single float}
  1635. and then printed. Alternatively, an implementation is permitted to
  1636. process a @i{rational}
  1637. number by any other method that has essentially the
  1638. same behavior but avoids loss of precision or overflow
  1639. because of the coercion. If @i{w} and @i{d} are
  1640. unsupplied and the number has no exact decimal representation,
  1641. for example @t{1/3}, some precision cutoff must be chosen
  1642. by the implementation since only a finite number of digits may be printed.
  1643. If @i{arg} is a @i{complex} number or some non-numeric
  1644. @i{object},
  1645. then it is printed using the format directive @t{~@i{w}D},
  1646. thereby printing it in decimal radix and a minimum field width of @i{w}.
  1647. @t{~E} binds
  1648. @b{*print-escape*} to @i{false}
  1649. and @b{*print-readably*} to @i{false}.
  1650. @node Tilde G-> General Floating-Point, Tilde Dollarsign-> Monetary Floating-Point, Tilde E-> Exponential Floating-Point, FORMAT Floating-Point Printers
  1651. @subsubsection Tilde G: General Floating-Point
  1652. The next @i{arg} is printed as a @i{float}
  1653. in either fixed-format or exponential notation as appropriate.
  1654. The full form is @t{~@i{w},@i{d},@i{e},@i{k},@i{overflowchar},@i{padchar},@i{exponentchar}G}.
  1655. The format in which to print @i{arg} depends on the magnitude (absolute
  1656. value) of the @i{arg}. Let @i{n} be an integer such that
  1657. 10^{{n}-1} \le |@i{arg}| < 10^@i{n}.
  1658. Let @i{ee} equal @i{e}+2, or 4 if @i{e} is omitted.
  1659. Let @i{ww} equal @i{w}- @i{ee},
  1660. or @b{nil} if @i{w} is omitted. If @i{d} is omitted, first let @i{q}
  1661. be the number of digits needed to print @i{arg} with no loss
  1662. of information and without leading or trailing zeros;
  1663. then let @i{d} equal @t{(max @i{q} (min @i{n} 7))}.
  1664. Let @i{dd} equal @i{d}- @i{n}.
  1665. If 0 \le @i{dd} \le @i{d}, then @i{arg} is printed
  1666. as if by the format directives
  1667. @t{~@i{ww},@i{dd},,@i{overflowchar},@i{padchar}F~@i{ee}@@T}
  1668. Note that the scale factor @i{k} is not passed to the @t{~F}
  1669. directive. For all other values of @i{dd}, @i{arg} is printed as if
  1670. by the format directive
  1671. @t{~@i{w},@i{d},@i{e},@i{k},@i{overflowchar},@i{padchar},@i{exponentchar}E}
  1672. In either case, an @t{@@}
  1673. modifier is supplied to the @t{~F}
  1674. or @t{~E} directive if and only if one was supplied to the
  1675. @t{~G} directive.
  1676. @t{~G} binds
  1677. @b{*print-escape*} to @i{false}
  1678. and @b{*print-readably*} to @i{false}.
  1679. @node Tilde Dollarsign-> Monetary Floating-Point, , Tilde G-> General Floating-Point, FORMAT Floating-Point Printers
  1680. @subsubsection Tilde Dollarsign: Monetary Floating-Point
  1681. The next @i{arg} is printed as a @i{float} in fixed-format notation.
  1682. The full form is @t{~@i{d},@i{n},@i{w},@i{padchar}$}.
  1683. The parameter @i{d} is the number
  1684. of digits to print after the decimal point (default value 2);
  1685. @i{n} is the minimum number of digits to print before the decimal
  1686. point (default value 1);
  1687. @i{w} is the minimum total width of the field to be printed (default
  1688. value 0).
  1689. First padding and the sign are output.
  1690. If the @i{arg} is negative, then a minus sign is printed;
  1691. if the @i{arg} is not negative, then a plus sign is printed
  1692. if and only if the @t{@@} modifier was supplied.
  1693. If the @t{:} modifier is used, the sign appears before any padding,
  1694. and otherwise after the padding.
  1695. If @i{w} is supplied and the number of other characters to be output
  1696. is less than @i{w}, then copies of @i{padchar} (which defaults
  1697. to a space) are output to
  1698. make the total field width equal @i{w}.
  1699. Then @i{n} digits are printed for the integer part of @i{arg},
  1700. with leading zeros if necessary; then a decimal point;
  1701. then @i{d} digits of fraction, properly rounded.
  1702. If the magnitude of @i{arg} is so large that more than @i{m} digits would
  1703. have to be printed, where @i{m} is the larger of @i{w} and 100, then an
  1704. implementation is free, at its discretion, to print the number using
  1705. exponential notation instead, as if by the directive
  1706. @t{~@i{w},@i{q},,,,@i{padchar}E}, where @i{w} and @i{padchar} are
  1707. present or omitted according to whether they were present or omitted in
  1708. the @t{~$} directive, and where @i{q}=@i{d}+@i{n}- 1,
  1709. where @i{d} and @i{n} are the (possibly default) values given to the
  1710. @t{~$} directive.
  1711. If @i{arg} is a @i{rational}
  1712. number, then it is coerced to be a @i{single float}
  1713. and then printed. Alternatively, an implementation is permitted to
  1714. process a @i{rational} number by any
  1715. other method that has essentially the
  1716. same behavior but avoids loss of precision or overflow
  1717. because of the coercion.
  1718. If @i{arg} is a @i{complex} number or some non-numeric
  1719. @i{object},
  1720. then it is printed using the format directive @t{~@i{w}D},
  1721. thereby printing it in decimal radix and a minimum field width of @i{w}.
  1722. @t{~$} binds @b{*print-escape*} to @i{false}
  1723. and @b{*print-readably*} to @i{false}.
  1724. @node FORMAT Printer Operations, FORMAT Pretty Printer Operations, FORMAT Floating-Point Printers, Formatted Output
  1725. @subsection FORMAT Printer Operations
  1726. @menu
  1727. * Tilde A-> Aesthetic::
  1728. * Tilde S-> Standard::
  1729. * Tilde W-> Write::
  1730. @end menu
  1731. @node Tilde A-> Aesthetic, Tilde S-> Standard, FORMAT Printer Operations, FORMAT Printer Operations
  1732. @subsubsection Tilde A: Aesthetic
  1733. An @i{arg}, any @i{object},
  1734. is printed without escape characters
  1735. (as by @b{princ}). If @i{arg} is a @i{string},
  1736. its @i{characters}
  1737. will be output verbatim.
  1738. If @i{arg} is @b{nil} it will be printed as @b{nil};
  1739. the @i{colon} modifier (@t{~:A}) will cause an @i{arg} of @b{nil} to be printed as @t{()},
  1740. but if @i{arg} is a composite structure, such as a @i{list} or @i{vector},
  1741. any contained occurrences of @b{nil} will still be printed as @b{nil}.
  1742. @t{~@i{mincol}A} inserts spaces on the right, if necessary, to make the
  1743. width at least @i{mincol} columns. The @t{@@}
  1744. modifier causes the spaces
  1745. to be inserted on the left rather than the right.
  1746. @t{~@i{mincol},@i{colinc},@i{minpad},@i{padchar}A}
  1747. is the full form of @t{~A},
  1748. which allows control of the padding.
  1749. The @i{string} is padded on the right (or on the left if the
  1750. @t{@@} modifier is used) with at least @i{minpad} copies
  1751. of @i{padchar}; padding characters are then inserted @i{colinc} characters
  1752. at a time until the total width is at least @i{mincol}.
  1753. The defaults are @t{0} for @i{mincol} and @i{minpad}, @t{1} for @i{colinc},
  1754. and the space character for @i{padchar}.
  1755. @t{~A} binds @b{*print-escape*} to @i{false},
  1756. and @b{*print-readably*} to @i{false}.
  1757. @node Tilde S-> Standard, Tilde W-> Write, Tilde A-> Aesthetic, FORMAT Printer Operations
  1758. @subsubsection Tilde S: Standard
  1759. This is just like @t{~A}, but @i{arg} is printed with escape
  1760. characters (as by @b{prin1} rather than @t{princ}). The output is
  1761. therefore suitable for input to @b{read}. @t{~S} accepts
  1762. all the arguments and modifiers that @t{~A} does.
  1763. @t{~S} binds @b{*print-escape*} to @b{t}.
  1764. @node Tilde W-> Write, , Tilde S-> Standard, FORMAT Printer Operations
  1765. @subsubsection Tilde W: Write
  1766. An argument, any @i{object}, is printed obeying every printer control
  1767. variable (as by @b{write}). In addition, @t{~W} interacts correctly with depth
  1768. abbreviation, by not resetting the depth counter to zero. @t{~W} does not
  1769. accept parameters. If given the @i{colon} modifier, @t{~W} binds @b{*print-pretty*}
  1770. to @i{true}. If given the @i{at-sign} modifier, @t{~W} binds @b{*print-level*}
  1771. and @b{*print-length*} to @b{nil}.
  1772. @t{~W} provides automatic support for the detection of circularity and
  1773. sharing. If the @i{value} of @b{*print-circle*} is not @b{nil} and @t{~W} is applied
  1774. to an argument that is a circular (or shared) reference, an appropriate
  1775. @t{#@i{n}#} marker is inserted in the output instead of printing the argument.
  1776. @node FORMAT Pretty Printer Operations, FORMAT Layout Control, FORMAT Printer Operations, Formatted Output
  1777. @subsection FORMAT Pretty Printer Operations
  1778. The following constructs provide access to the @i{pretty printer}:
  1779. @menu
  1780. * Tilde Underscore-> Conditional Newline::
  1781. * Tilde Less-Than-Sign-> Logical Block::
  1782. * Tilde I-> Indent::
  1783. * Tilde Slash-> Call Function::
  1784. @end menu
  1785. @node Tilde Underscore-> Conditional Newline, Tilde Less-Than-Sign-> Logical Block, FORMAT Pretty Printer Operations, FORMAT Pretty Printer Operations
  1786. @subsubsection Tilde Underscore: Conditional Newline
  1787. Without any modifiers, @t{~_} is the same as @t{(pprint-newline :linear)}.
  1788. @t{~@@_} is the same as @t{(pprint-newline :miser)}.
  1789. @t{~:_} is the same as @t{(pprint-newline :fill)}.
  1790. @t{~:@@_} is the same as @t{(pprint-newline :mandatory)}.
  1791. @node Tilde Less-Than-Sign-> Logical Block, Tilde I-> Indent, Tilde Underscore-> Conditional Newline, FORMAT Pretty Printer Operations
  1792. @subsubsection Tilde Less-Than-Sign: Logical Block
  1793. @t{~<...~:>}
  1794. If @t{~:>} is used to terminate a @t{~<...~>},
  1795. the directive is equivalent to a call to @b{pprint-logical-block}.
  1796. The argument corresponding to the @t{~<...~:>} directive is treated in
  1797. the same way as the @i{list} argument to @b{pprint-logical-block},
  1798. thereby providing automatic support for non-@i{list} arguments and
  1799. the detection of circularity, sharing, and depth abbreviation.
  1800. The portion of the @i{control-string} nested within the @t{~<...~:>}
  1801. specifies the @t{:prefix} (or @t{:per-line-prefix}), @t{:suffix},
  1802. and body of the @b{pprint-logical-block}.
  1803. The @i{control-string} portion enclosed by @t{~<...~:>} can be divided
  1804. into segments @t{~<@i{prefix}~;@i{body}~;@i{suffix}~:>}
  1805. by @t{~;} directives. If the first section is terminated by @t{~@@;},
  1806. it specifies a per-line prefix rather than a simple prefix.
  1807. The @i{prefix} and @i{suffix} cannot contain format directives.
  1808. An error is signaled if either the prefix or suffix fails to be a
  1809. constant string or if the enclosed portion is divided into more than three segments.
  1810. If the enclosed portion is divided into only two segments, the @i{suffix}
  1811. defaults to the null string. If the enclosed portion consists of only
  1812. a single segment, both the @i{prefix} and the @i{suffix} default to
  1813. the null string. If the @i{colon} modifier is used (@i{i.e.}, @t{~:<...~:>}),
  1814. the @i{prefix} and @i{suffix} default to @t{"("} and @t{")"}
  1815. (respectively) instead of the null string.
  1816. The body segment can be any arbitrary @i{format string}.
  1817. This @i{format string} is applied to the elements of the list
  1818. corresponding to the @t{~<...~:>} directive as a whole.
  1819. Elements are extracted from this list using @b{pprint-pop},
  1820. thereby providing automatic support for malformed lists, and the detection
  1821. of circularity, sharing, and length abbreviation.
  1822. Within the body segment, @t{~{@t{^}}} acts like @b{pprint-exit-if-list-exhausted}.
  1823. @t{~<...~:>} supports a feature not supported by @b{pprint-logical-block}.
  1824. If @t{~:@@>} is used to terminate the directive (@i{i.e.}, @t{~<...~:@@>}),
  1825. then a fill-style conditional newline is automatically inserted after each
  1826. group of blanks immediately contained in the body (except for blanks
  1827. after a ~<@i{Newline}> directive). This makes it easy to achieve the
  1828. equivalent of paragraph filling.
  1829. If the @i{at-sign} modifier is used with @t{~<...~:>}, the entire remaining argument
  1830. list is passed to the directive as its argument. All of the remaining
  1831. arguments are always consumed by @t{~@@<...~:>}, even if they are not all used
  1832. by the @i{format string} nested in the directive. Other than the difference in
  1833. its argument, @t{~@@<...~:>} is exactly the same as @t{~<...~:>} except that
  1834. circularity detection is not applied if @t{~@@<...~:>} is encountered at top
  1835. level in a @i{format string}. This ensures that circularity detection is
  1836. applied only to data lists, not to @i{format argument} @i{lists}.
  1837. @t{" . #@i{n}#"} is printed if circularity or sharing has to be indicated
  1838. for its argument as a whole.
  1839. To a considerable extent, the basic form of the directive @t{~<...~>} is
  1840. incompatible with the dynamic control of the arrangement of output by
  1841. @t{~W}, @t{~_}, @t{~<...~:>}, @t{~I}, and @t{~:T}. As a result, an error
  1842. is signaled if any of these directives is nested within @t{~<...~>}.
  1843. Beyond this, an error is also signaled if the @t{~<...~:;...~>} form of
  1844. @t{~<...~>} is used in the same @i{format string} with
  1845. @t{~W}, @t{~_}, @t{~<...~:>}, @t{~I}, or @t{~:T}.
  1846. See also @ref{Tilde Less-Than-Sign-> Justification}.
  1847. @node Tilde I-> Indent, Tilde Slash-> Call Function, Tilde Less-Than-Sign-> Logical Block, FORMAT Pretty Printer Operations
  1848. @subsubsection Tilde I: Indent
  1849. @t{~@i{n}I} is the same as @t{(pprint-indent :block n)}.
  1850. @t{~@i{n}:I} is the same as @t{(pprint-indent :current n)}.
  1851. In both cases, @i{n} defaults to zero, if it is omitted.
  1852. @node Tilde Slash-> Call Function, , Tilde I-> Indent, FORMAT Pretty Printer Operations
  1853. @subsubsection Tilde Slash: Call Function
  1854. @t{~/@i{name}/}
  1855. User defined functions can be called from within a format
  1856. string by using the directive @t{~/@i{name}/}.
  1857. The @i{colon} modifier, the @i{at-sign} modifier, and arbitrarily many parameters
  1858. can be specified with the @t{~/@i{name}/} directive.
  1859. @i{name} can be any arbitrary string that does not contain a "/".
  1860. All of the characters in @i{name} are treated as if they were upper case.
  1861. If @i{name} contains a single @i{colon} (@t{:}) or double @i{colon} (@t{::}),
  1862. then everything up to but not including the first @t{":"} or @t{"::"}
  1863. is taken to be a @i{string} that names a @i{package}.
  1864. Everything after the first @t{":"} or @t{"::"} (if any) is taken to be a
  1865. @i{string} that names a @t{symbol}. The function corresponding to a
  1866. @t{~/name/} directive is obtained by looking up the @i{symbol}
  1867. that has the indicated name in the indicated @i{package}.
  1868. If @i{name} does not contain a @t{":"} or @t{"::"},
  1869. then the whole @i{name} string is looked up in the @t{COMMON-LISP-USER} @i{package}.
  1870. When a @t{~/name/} directive is encountered,
  1871. the indicated function is called with four or more arguments.
  1872. The first four arguments are:
  1873. the output stream,
  1874. the @i{format argument} corresponding to the directive,
  1875. a @i{generalized boolean} that is @i{true} if the @i{colon} modifier was used,
  1876. and a @i{generalized boolean} that is @i{true} if the @i{at-sign} modifier was used.
  1877. The remaining arguments consist of any parameters specified with the directive.
  1878. The function should print the argument appropriately.
  1879. Any values returned by the function are ignored.
  1880. The three @i{functions}
  1881. @b{pprint-linear},
  1882. @b{pprint-fill},
  1883. and @b{pprint-tabular}
  1884. are specifically designed so that they can be called by @t{~/.../}
  1885. (@i{i.e.}, @t{~/pprint-linear/}, @t{~/pprint-fill/}, and @t{~/pprint-tabular/}).
  1886. In particular they take @i{colon} and @i{at-sign} arguments.
  1887. @node FORMAT Layout Control, FORMAT Control-Flow Operations, FORMAT Pretty Printer Operations, Formatted Output
  1888. @subsection FORMAT Layout Control
  1889. @menu
  1890. * Tilde T-> Tabulate::
  1891. * Tilde Less-Than-Sign-> Justification::
  1892. * Tilde Greater-Than-Sign-> End of Justification::
  1893. @end menu
  1894. @node Tilde T-> Tabulate, Tilde Less-Than-Sign-> Justification, FORMAT Layout Control, FORMAT Layout Control
  1895. @subsubsection Tilde T: Tabulate
  1896. This spaces over to a given column.
  1897. @t{~@i{colnum},@i{colinc}T} will output
  1898. sufficient spaces to move the cursor to column @i{colnum}. If the cursor
  1899. is already at or beyond column @i{colnum}, it will output spaces to move it to
  1900. column @i{colnum}+@i{k}*@i{colinc} for the smallest positive integer
  1901. @i{k} possible, unless @i{colinc} is zero, in which case no spaces
  1902. are output if the cursor is already at or beyond column @i{colnum}.
  1903. @i{colnum} and @i{colinc} default to @t{1}.
  1904. If for some reason the current absolute column position cannot be determined
  1905. by direct inquiry,
  1906. @b{format}
  1907. may be able to deduce the current column position by noting
  1908. that certain directives (such as @t{~%}, or @t{~&},
  1909. or @t{~A}
  1910. with the argument being a string containing a newline) cause
  1911. the column position to be reset to zero, and counting the number of characters
  1912. emitted since that point. If that fails, @b{format}
  1913. may attempt a
  1914. similar deduction on the riskier assumption that the destination was
  1915. at column zero when @b{format}
  1916. was invoked. If even this heuristic fails
  1917. or is implementationally inconvenient, at worst
  1918. the @t{~T} operation will simply output two spaces.
  1919. @t{~@@T} performs relative tabulation.
  1920. @t{~@i{colrel},@i{colinc}@@T} outputs @i{colrel} spaces
  1921. and then outputs the smallest non-negative
  1922. number of additional spaces necessary to move the cursor
  1923. to a column that is a multiple
  1924. of @i{colinc}. For example, the directive
  1925. @t{~3,8@@T} outputs
  1926. three spaces and then moves the cursor to a ``standard multiple-of-eight
  1927. tab stop'' if not at one already.
  1928. If the current output column cannot be determined, however,
  1929. then @i{colinc} is ignored, and exactly @i{colrel} spaces are output.
  1930. If the @i{colon} modifier is used with the @t{~T} directive,
  1931. the tabbing computation is done relative to the horizontal position where the
  1932. section immediately containing the directive begins, rather than with
  1933. respect to a horizontal position of zero. The numerical parameters are
  1934. both interpreted as being in units of @i{ems} and both default to @t{1}.
  1935. @t{~@i{n},@i{m}:T} is the same as
  1936. @t{(pprint-tab :section @i{n} @i{m})}.
  1937. @t{~@i{n},@i{m}:@@T} is the same as
  1938. @t{(pprint-tab :section-relative @i{n} @i{m})}.
  1939. @node Tilde Less-Than-Sign-> Justification, Tilde Greater-Than-Sign-> End of Justification, Tilde T-> Tabulate, FORMAT Layout Control
  1940. @subsubsection Tilde Less-Than-Sign: Justification
  1941. @t{~@i{mincol},@i{colinc},@i{minpad},@i{padchar}<@i{str}~>}
  1942. This justifies the text produced by processing @i{str}
  1943. within a field at least @i{mincol} columns wide. @i{str}
  1944. may be divided up into segments with @t{~;}, in which case the
  1945. spacing is evenly divided between the text segments.
  1946. With no modifiers, the leftmost text segment is left justified in the
  1947. field, and the rightmost text segment is right justified. If there is
  1948. only one text element, as a special case, it is right justified.
  1949. The @t{:} modifier causes
  1950. spacing to be introduced before the first text segment; the
  1951. @t{@@} modifier causes spacing to be added after the last.
  1952. The @i{minpad} parameter (default @t{0}) is the minimum number of
  1953. padding characters to be output between each segment.
  1954. The padding character is supplied by @i{padchar},
  1955. which defaults to the space character.
  1956. If the total width needed to satisfy these constraints is greater
  1957. than @i{mincol}, then the width used is @i{mincol}+@i{k}*@i{colinc}
  1958. for the smallest possible non-negative integer value @i{k}.
  1959. @i{colinc} defaults to @t{1}, and @i{mincol} defaults to @t{0}.
  1960. Note that @i{str} may include @b{format} directives.
  1961. All the clauses in @i{str} are processed in order;
  1962. it is the resulting pieces of text that are justified.
  1963. The @t{~@t{^} } directive may be used to terminate processing of the
  1964. clauses prematurely, in which case only the completely processed clauses
  1965. are justified.
  1966. If the first clause of a @t{~<}
  1967. is terminated with @t{~:;} instead of
  1968. @t{~;}, then it is used in a special way. All of the clauses are
  1969. processed (subject to @t{~@t{^} }, of course), but the
  1970. first one is not used
  1971. in performing the spacing and padding. When the padded result has been
  1972. determined, then if it will fit on the current line of output, it is
  1973. output, and the text for the first clause is discarded. If, however, the
  1974. padded text will not fit on the current line, then the text segment for
  1975. the first clause is output before the padded text. The first clause
  1976. ought to contain a newline (such as a @t{~%} directive). The first
  1977. clause is always processed, and so any arguments it refers to will be
  1978. used; the decision is whether to use the resulting segment of text, not
  1979. whether to process the first clause. If the @t{~:;} has a prefix
  1980. parameter @i{n}, then the padded text must fit on the current line with
  1981. @i{n} character positions to spare to avoid outputting the first clause's
  1982. text. For example, the control string
  1983. @example
  1984. "~
  1985. @end example
  1986. can be used to print a list of items separated by commas without
  1987. breaking items over line boundaries, beginning each line with
  1988. @t{;; }. The prefix parameter
  1989. @t{1} in @t{~1:;} accounts for the width of the
  1990. comma that will follow the justified item if it is not the last
  1991. element in the list, or the period
  1992. if it is. If @t{~:;} has a second
  1993. prefix parameter, then it is used as the width of the line,
  1994. thus overriding the natural line width of the output stream. To make
  1995. the preceding example use a line width of 50, one would write
  1996. @example
  1997. "~
  1998. @end example
  1999. If the second argument is not supplied, then @b{format} uses the
  2000. line width of the @i{destination} output stream.
  2001. If this cannot be determined (for example, when producing a
  2002. @i{string} result), then @b{format} uses @t{72} as the line length.
  2003. See also @ref{Tilde Less-Than-Sign-> Logical Block}.
  2004. @node Tilde Greater-Than-Sign-> End of Justification, , Tilde Less-Than-Sign-> Justification, FORMAT Layout Control
  2005. @subsubsection Tilde Greater-Than-Sign: End of Justification
  2006. @t{~>} terminates a @t{~<}.
  2007. The consequences of using it elsewhere are undefined.
  2008. @node FORMAT Control-Flow Operations, FORMAT Miscellaneous Operations, FORMAT Layout Control, Formatted Output
  2009. @subsection FORMAT Control-Flow Operations
  2010. @menu
  2011. * Tilde Asterisk-> Go-To::
  2012. * Tilde Left-Bracket-> Conditional Expression::
  2013. * Tilde Right-Bracket-> End of Conditional Expression::
  2014. * Tilde Left-Brace-> Iteration::
  2015. * Tilde Right-Brace-> End of Iteration::
  2016. * Tilde Question-Mark-> Recursive Processing::
  2017. @end menu
  2018. @node Tilde Asterisk-> Go-To, Tilde Left-Bracket-> Conditional Expression, FORMAT Control-Flow Operations, FORMAT Control-Flow Operations
  2019. @subsubsection Tilde Asterisk: Go-To
  2020. The next @i{arg} is ignored.
  2021. @t{~@i{n}*} ignores the next @i{n} arguments.
  2022. @t{~:*} backs up in the list of
  2023. arguments so that the argument last processed will be processed again.
  2024. @t{~@i{n}:*} backs up @i{n} arguments.
  2025. When within a @t{~@{} construct
  2026. (see below), the ignoring (in either direction) is relative to the list
  2027. of arguments being processed by the iteration.
  2028. @t{~@i{n}@@*}
  2029. goes to the @i{n}th @i{arg}, where 0 means the first one;
  2030. @i{n} defaults to 0, so @t{~@@*} goes back to the first @i{arg}.
  2031. Directives after a @t{~@i{n}@@*}
  2032. will take arguments in sequence beginning with the one gone to.
  2033. When within a @t{~@{} construct, the ``goto''
  2034. is relative to the list of arguments being processed by the iteration.
  2035. @node Tilde Left-Bracket-> Conditional Expression, Tilde Right-Bracket-> End of Conditional Expression, Tilde Asterisk-> Go-To, FORMAT Control-Flow Operations
  2036. @subsubsection Tilde Left-Bracket: Conditional Expression
  2037. @t{~[@i{str0}~;@i{str1}~;@i{...}~;@i{strn}~]}
  2038. This is a set of control strings, called @i{clauses}, one of which is
  2039. chosen and used. The clauses are separated by @t{~;}
  2040. and the construct is terminated by @t{~]}. For example,
  2041. @t{"~[Siamese~;Manx~;Persian~] Cat"}
  2042. The @i{arg}th
  2043. clause is selected, where the first clause is number 0.
  2044. If a prefix parameter is given (as @t{~@i{n}[}),
  2045. then the parameter is used instead of an argument.
  2046. If @i{arg} is out of range then no clause is selected
  2047. and no error is signaled.
  2048. After the selected alternative has been processed, the control string
  2049. continues after the @t{~]}.
  2050. @t{~[@i{str0}~;@i{str1}~;@i{...}~;@i{strn}~:;@i{default}~]}
  2051. has a default case.
  2052. If the @i{last} @t{~;} used to separate clauses
  2053. is @t{~:;} instead, then the last clause is an else clause
  2054. that is performed if no other clause is selected.
  2055. For example:
  2056. @t{"~[Siamese~;Manx~;Persian~:;Alley~] Cat"}
  2057. @t{~:[@i{alternative}~;@i{consequent}~]}
  2058. selects the @i{alternative} control string if @i{arg} is @i{false},
  2059. and selects the @i{consequent} control string otherwise.
  2060. @t{~@@[@i{consequent}~]}
  2061. tests the argument. If it is @i{true},
  2062. then the argument is not used up by the @t{~[} command
  2063. but remains as the next one to be processed,
  2064. and the one clause @i{consequent} is processed.
  2065. If the @i{arg} is @i{false}, then the argument is used up,
  2066. and the clause is not processed.
  2067. The clause therefore should normally use exactly one argument,
  2068. and may expect it to be @i{non-nil}.
  2069. For example:
  2070. @example
  2071. (setq *print-level* nil *print-length* 5)
  2072. (format nil
  2073. "~@@[ print level = ~D~]~@@[ print length = ~D~]"
  2074. *print-level* *print-length*)
  2075. @result{} " print length = 5"
  2076. @end example
  2077. Note also that
  2078. @example
  2079. (format @i{stream} "...~@@[@i{str}~]..." ...)
  2080. @equiv{} (format @i{stream} "...~:[~;~:*@i{str}~]..." ...)
  2081. @end example
  2082. The combination of @t{~[} and @t{#} is useful, for
  2083. example, for dealing with English conventions for printing lists:
  2084. @example
  2085. (setq foo "Items:~#[ none~; ~S~; ~S and ~S~
  2086. ~:;~@@@{~#[~; and~] ~S~@t{^} ,~@}~].")
  2087. (format nil foo) @result{} "Items: none."
  2088. (format nil foo 'foo) @result{} "Items: FOO."
  2089. (format nil foo 'foo 'bar) @result{} "Items: FOO and BAR."
  2090. (format nil foo 'foo 'bar 'baz) @result{} "Items: FOO, BAR, and BAZ."
  2091. (format nil foo 'foo 'bar 'baz 'quux) @result{} "Items: FOO, BAR, BAZ, and QUUX."
  2092. @end example
  2093. @node Tilde Right-Bracket-> End of Conditional Expression, Tilde Left-Brace-> Iteration, Tilde Left-Bracket-> Conditional Expression, FORMAT Control-Flow Operations
  2094. @subsubsection Tilde Right-Bracket: End of Conditional Expression
  2095. @t{~]} terminates a @t{~[}.
  2096. The consequences of using it elsewhere are undefined.
  2097. @node Tilde Left-Brace-> Iteration, Tilde Right-Brace-> End of Iteration, Tilde Right-Bracket-> End of Conditional Expression, FORMAT Control-Flow Operations
  2098. @subsubsection Tilde Left-Brace: Iteration
  2099. @t{~@{{@i{str}}~@}}
  2100. This is an iteration construct. The argument should be a @i{list},
  2101. which is used as a set of arguments
  2102. as if for a recursive call to @b{format}.
  2103. The @i{string} @i{str} is used repeatedly as the control string.
  2104. Each iteration can absorb as many elements of the @i{list} as it likes
  2105. as arguments;
  2106. if @i{str} uses up two arguments by itself, then two elements of the
  2107. @i{list} will get used up each time around the loop.
  2108. If before any iteration step the @i{list}
  2109. is empty, then the iteration is terminated.
  2110. Also, if a prefix parameter @i{n} is given, then there will be at most @i{n}
  2111. repetitions of processing of @i{str}.
  2112. Finally, the @t{~@t{^} } directive can be
  2113. used to terminate the iteration prematurely.
  2114. For example:
  2115. @example
  2116. (format nil "The winners are:~@{ ~S~@}."
  2117. '(fred harry jill))
  2118. @result{} "The winners are: FRED HARRY JILL."
  2119. (format nil "Pairs:~@{ <~S,~S>~@}."
  2120. '(a 1 b 2 c 3))
  2121. @result{} "Pairs: <A,1> <B,2> <C,3>."
  2122. @end example
  2123. @t{~:@{ @i{str}~@} } is similar,
  2124. but the argument should be a @i{list} of sublists.
  2125. At each repetition step, one sublist
  2126. is used as the set of arguments for
  2127. processing @i{str}; on the next repetition, a new sublist
  2128. is used, whether
  2129. or not all of the last sublist had been processed.
  2130. For example:
  2131. @example
  2132. (format nil "Pairs:~:@{ <~S,~S>~@} ."
  2133. '((a 1) (b 2) (c 3)))
  2134. @result{} "Pairs: <A,1> <B,2> <C,3>."
  2135. @end example
  2136. @t{~@@@{ @i{str}~@} }
  2137. is similar to @t{~@{ @i{str}~@} }, but instead of
  2138. using one argument that is a list, all the remaining arguments
  2139. are used as the list of arguments for the iteration.
  2140. Example:
  2141. @example
  2142. (format nil "Pairs:~@@@{ <~S,~S>~@} ." 'a 1 'b 2 'c 3)
  2143. @result{} "Pairs: <A,1> <B,2> <C,3>."
  2144. @end example
  2145. If the iteration is terminated before all the remaining arguments are
  2146. consumed, then any arguments not processed by the iteration remain to be
  2147. processed by any directives following the iteration construct.
  2148. @t{~:@@@{ @i{str}~@} }
  2149. combines the features
  2150. of @t{~:@{ @i{str}~@} }
  2151. and @t{~@@@{ @i{str}~@} }.
  2152. All the remaining arguments
  2153. are used, and each one must be a @i{list}.
  2154. On each iteration, the next argument is
  2155. used as a @i{list} of arguments to @i{str}.
  2156. Example:
  2157. @example
  2158. (format nil "Pairs:~:@@@{ <~S,~S>~@} ."
  2159. '(a 1) '(b 2) '(c 3))
  2160. @result{} "Pairs: <A,1> <B,2> <C,3>."
  2161. @end example
  2162. Terminating the repetition construct with @t{~:@} }
  2163. instead of @t{~@} }
  2164. forces @i{str} to be processed at least once, even if the initial
  2165. list of arguments is null. However, this will not override an explicit
  2166. prefix parameter of zero.
  2167. If @i{str} is empty, then an argument is used as @i{str}.
  2168. It must be a @i{format control}
  2169. and precede any arguments processed by the iteration. As an example,
  2170. the following are equivalent:
  2171. @example
  2172. (apply #'format stream string arguments)
  2173. @equiv{} (format stream "~1@{~:@}" string arguments)
  2174. @end example
  2175. This will use @t{string} as a formatting string.
  2176. The @t{~1@{ } says it will
  2177. be processed at most once, and the @t{~:@} }
  2178. says it will be processed at least once.
  2179. Therefore it is processed exactly once, using @t{arguments} as the arguments.
  2180. This case may be handled more clearly by the @t{~?} directive,
  2181. but this general feature of @t{~@{ }
  2182. is more powerful than @t{~?}.
  2183. @node Tilde Right-Brace-> End of Iteration, Tilde Question-Mark-> Recursive Processing, Tilde Left-Brace-> Iteration, FORMAT Control-Flow Operations
  2184. @subsubsection Tilde Right-Brace: End of Iteration
  2185. @t{~@}} terminates a @t{~@{}.
  2186. The consequences of using it elsewhere are undefined.
  2187. @node Tilde Question-Mark-> Recursive Processing, , Tilde Right-Brace-> End of Iteration, FORMAT Control-Flow Operations
  2188. @subsubsection Tilde Question-Mark: Recursive Processing
  2189. The next @i{arg} must be a @i{format control}, and the one after it a @i{list};
  2190. both are consumed by the @t{~?} directive.
  2191. The two are processed as a @i{control-string}, with the elements of the @i{list}
  2192. as the arguments. Once the recursive processing
  2193. has been finished, the processing of the control
  2194. string containing the @t{~?} directive is resumed.
  2195. Example:
  2196. @example
  2197. (format nil "~? ~D" "<~A ~D>" '("Foo" 5) 7) @result{} "<Foo 5> 7"
  2198. (format nil "~? ~D" "<~A ~D>" '("Foo" 5 14) 7) @result{} "<Foo 5> 7"
  2199. @end example
  2200. Note that in the second example three arguments are supplied
  2201. to the @i{format string} @t{"<~A ~D>"}, but only two are processed
  2202. and the third is therefore ignored.
  2203. With the @t{@@}
  2204. modifier, only one @i{arg} is directly consumed.
  2205. The @i{arg} must be a @i{string};
  2206. it is processed as part of the control
  2207. string as if it had appeared in place of the @t{~@@?} construct,
  2208. and any directives in the recursively processed control string may
  2209. consume arguments of the control string containing the @t{~@@?}
  2210. directive.
  2211. Example:
  2212. @example
  2213. (format nil "~@@? ~D" "<~A ~D>" "Foo" 5 7) @result{} "<Foo 5> 7"
  2214. (format nil "~@@? ~D" "<~A ~D>" "Foo" 5 14 7) @result{} "<Foo 5> 14"
  2215. @end example
  2216. @node FORMAT Miscellaneous Operations, FORMAT Miscellaneous Pseudo-Operations, FORMAT Control-Flow Operations, Formatted Output
  2217. @subsection FORMAT Miscellaneous Operations
  2218. @menu
  2219. * Tilde Left-Paren-> Case Conversion::
  2220. * Tilde Right-Paren-> End of Case Conversion::
  2221. * Tilde P-> Plural::
  2222. @end menu
  2223. @node Tilde Left-Paren-> Case Conversion, Tilde Right-Paren-> End of Case Conversion, FORMAT Miscellaneous Operations, FORMAT Miscellaneous Operations
  2224. @subsubsection Tilde Left-Paren: Case Conversion
  2225. @t{~(@i{str}~)}
  2226. The contained control string @i{str} is processed, and what it produces
  2227. is subject to case conversion.
  2228. With no flags, every @i{uppercase} @i{character}
  2229. is converted to the corresponding @i{lowercase} @i{character}.
  2230. @t{~:(} capitalizes all words, as if by @b{string-capitalize}.
  2231. @t{~@@(}
  2232. capitalizes just the first word and forces the rest to lower
  2233. case.
  2234. @t{~:@@(} converts every lowercase character
  2235. to the corresponding uppercase character.
  2236. In this example @t{~@@(} is used to cause the first word
  2237. produced by @t{~@@R} to be capitalized:
  2238. @example
  2239. (format nil "~@@R ~(~@@R~)" 14 14)
  2240. @result{} "XIV xiv"
  2241. (defun f (n) (format nil "~@@(~R~) error~:P detected." n)) @result{} F
  2242. (f 0) @result{} "Zero errors detected."
  2243. (f 1) @result{} "One error detected."
  2244. (f 23) @result{} "Twenty-three errors detected."
  2245. @end example
  2246. When case conversions appear nested, the outer conversion dominates,
  2247. as illustrated in the following example:
  2248. @example
  2249. (format nil "~@@(how is ~:(BOB SMITH~)?~)")
  2250. @result{} "How is bob smith?"
  2251. @i{NOT}@result{} "How is Bob Smith?"
  2252. @end example
  2253. @node Tilde Right-Paren-> End of Case Conversion, Tilde P-> Plural, Tilde Left-Paren-> Case Conversion, FORMAT Miscellaneous Operations
  2254. @subsubsection Tilde Right-Paren: End of Case Conversion
  2255. @t{~)} terminates a @t{~(}.
  2256. The consequences of using it elsewhere are undefined.
  2257. @node Tilde P-> Plural, , Tilde Right-Paren-> End of Case Conversion, FORMAT Miscellaneous Operations
  2258. @subsubsection Tilde P: Plural
  2259. If @i{arg} is not @b{eql}
  2260. to the integer @t{1}, a lowercase @t{s} is
  2261. printed; if @i{arg} is @b{eql} to @t{1}, nothing is printed.
  2262. If @i{arg} is a floating-point @t{1.0}, the @t{s} is
  2263. printed.
  2264. @t{~:P} does the same thing,
  2265. after doing a @t{~:*} to back up one argument;
  2266. that is, it prints a lowercase @t{s} if the previous argument was not
  2267. @t{1}.
  2268. @t{~@@P}
  2269. prints @t{y} if the argument is @t{1}, or @t{ies} if it is
  2270. not. @t{~:@@P} does the same thing, but backs up first.
  2271. @example
  2272. (format nil "~D tr~:@@P/~D win~:P" 7 1) @result{} "7 tries/1 win"
  2273. (format nil "~D tr~:@@P/~D win~:P" 1 0) @result{} "1 try/0 wins"
  2274. (format nil "~D tr~:@@P/~D win~:P" 1 3) @result{} "1 try/3 wins"
  2275. @end example
  2276. @node FORMAT Miscellaneous Pseudo-Operations, Additional Information about FORMAT Operations, FORMAT Miscellaneous Operations, Formatted Output
  2277. @subsection FORMAT Miscellaneous Pseudo-Operations
  2278. @menu
  2279. * Tilde Semicolon-> Clause Separator::
  2280. * Tilde Circumflex-> Escape Upward::
  2281. * Tilde Newline-> Ignored Newline::
  2282. @end menu
  2283. @node Tilde Semicolon-> Clause Separator, Tilde Circumflex-> Escape Upward, FORMAT Miscellaneous Pseudo-Operations, FORMAT Miscellaneous Pseudo-Operations
  2284. @subsubsection Tilde Semicolon: Clause Separator
  2285. This separates clauses in @t{~[} and @t{~<} constructs.
  2286. The consequences of using it elsewhere are undefined.
  2287. @node Tilde Circumflex-> Escape Upward, Tilde Newline-> Ignored Newline, Tilde Semicolon-> Clause Separator, FORMAT Miscellaneous Pseudo-Operations
  2288. @subsubsection Tilde Circumflex: Escape Upward
  2289. {@t{~@t{^} }}
  2290. This is an escape construct. If there are no more arguments remaining to
  2291. be processed, then the immediately
  2292. enclosing @t{~@{ } or @t{~<} construct
  2293. is terminated. If there is no such enclosing construct, then the entire
  2294. formatting operation is terminated.
  2295. In the @t{~<} case, the formatting
  2296. is performed, but no more segments are processed before doing the
  2297. justification.
  2298. @t{~@t{^} } may appear anywhere in a @t{~@{ }
  2299. construct.
  2300. @example
  2301. (setq donestr "Done.~{@t{^}} ~D warning~:P.~{@t{^}} ~D error~:P.")
  2302. @result{} "Done.~{@t{^}} ~D warning~:P.~{@t{^}} ~D error~:P."
  2303. (format nil donestr) @result{} "Done."
  2304. (format nil donestr 3) @result{} "Done. 3 warnings."
  2305. (format nil donestr 1 5) @result{} "Done. 1 warning. 5 errors."
  2306. @end example
  2307. If a prefix parameter is given, then termination occurs if the parameter
  2308. is zero. (Hence @t{~{@t{^}}} is equivalent to
  2309. @t{~#{@t{^}}}.) If two
  2310. parameters are given, termination occurs if they are equal.
  2311. [Reviewer Note by Barmar: Which equality predicate?] If three
  2312. parameters are given, termination occurs if the first is less than or
  2313. equal to the second and the second is less than or equal to the third.
  2314. Of course, this is useless if all the prefix parameters are constants; at
  2315. least one of them should be a @t{#} or a @t{V} parameter.
  2316. If @t{~{@t{^}}} is used within a @t{~:@{ }
  2317. construct, then it terminates
  2318. the current iteration step because in the standard case it tests for
  2319. remaining arguments of the current step only; the next iteration step
  2320. commences immediately. @t{~:{@t{^}}} is used to terminate
  2321. the iteration process.
  2322. @t{~:{@t{^}}}
  2323. may be used only if the command it would terminate is
  2324. @t{~:@{ } or @t{~:@@@{ }.
  2325. The entire iteration process is terminated if and only if the sublist that is
  2326. supplying the arguments for the current iteration step is the last sublist in
  2327. the case of @t{~:@{ },
  2328. or the last @b{format}
  2329. argument in the case of @t{~:@@@{ }.
  2330. @t{~:{@t{^}}} is not
  2331. equivalent to @t{~#:{@t{^}}};
  2332. the latter terminates the entire iteration if and only if no
  2333. arguments remain for the current iteration step.
  2334. For example:
  2335. @example
  2336. (format nil "~:@{ ~@@?~:@t{^} ...~@} " '(("a") ("b"))) @result{} "a...b"
  2337. @end example
  2338. If @t{~{@t{^}}} appears within a control string being processed
  2339. under the control of a @t{~?} directive, but not within
  2340. any @t{~@{ } or @t{~<} construct within that string,
  2341. then the string being
  2342. processed will be terminated, thereby ending processing
  2343. of the @t{~?} directive. Processing then
  2344. continues within the string
  2345. containing the @t{~?} directive at the point following that directive.
  2346. If @t{~{@t{^}}}
  2347. appears within a @t{~[} or @t{~(} construct,
  2348. then all the commands up to the @t{~{@t{^}}} are properly selected
  2349. or case-converted,
  2350. the @t{~[} or @t{~(} processing is terminated,
  2351. and the outward search continues
  2352. for a @t{~@{ } or @t{~<} construct
  2353. to be terminated. For example:
  2354. @example
  2355. (setq tellstr "~@@(~@@[~R~]~{@t{^}} ~A!~)")
  2356. @result{} "~@@(~@@[~R~]~{@t{^}} ~A!~)"
  2357. (format nil tellstr 23) @result{} "Twenty-three!"
  2358. (format nil tellstr nil "losers") @result{} " Losers!"
  2359. (format nil tellstr 23 "losers") @result{} "Twenty-three losers!"
  2360. @end example
  2361. Following are examples of the use of @t{~{@t{^}}}
  2362. within a @t{~<} construct.
  2363. @example
  2364. (format nil "~15<~S~;~{@t{^}}~S~;~{@t{^}}~S~>" 'foo)
  2365. @result{} " FOO"
  2366. (format nil "~15<~S~;~{@t{^}}~S~;~{@t{^}}~S~>" 'foo 'bar)
  2367. @result{} "FOO BAR"
  2368. (format nil "~15<~S~;~{@t{^}}~S~;~{@t{^}}~S~>" 'foo 'bar 'baz)
  2369. @result{} "FOO BAR BAZ"
  2370. @end example
  2371. @node Tilde Newline-> Ignored Newline, , Tilde Circumflex-> Escape Upward, FORMAT Miscellaneous Pseudo-Operations
  2372. @subsubsection Tilde Newline: Ignored Newline
  2373. @i{Tilde} immediately followed by a @i{newline} ignores the @i{newline}
  2374. and any following non-newline @i{whitespace}_1 characters.
  2375. With a @t{:},
  2376. the @i{newline} is ignored,
  2377. but any following @i{whitespace}_1 is left in place.
  2378. With an @t{@@},
  2379. the @i{newline} is left in place,
  2380. but any following @i{whitespace}_1 is ignored.
  2381. For example:
  2382. @example
  2383. (defun type-clash-error (fn nargs argnum right-type wrong-type)
  2384. (format *error-output*
  2385. "~&~S requires its ~:[~:R~;~*~]~
  2386. argument to be of type ~S,~
  2387. with an argument of type ~S.~
  2388. fn (eql nargs 1) argnum right-type wrong-type))
  2389. (type-clash-error 'aref nil 2 'integer 'vector) prints:
  2390. AREF requires its second argument to be of type INTEGER,
  2391. but it was called with an argument of type VECTOR.
  2392. NIL
  2393. (type-clash-error 'car 1 1 'list 'short-float) prints:
  2394. CAR requires its argument to be of type LIST,
  2395. but it was called with an argument of type SHORT-FLOAT.
  2396. NIL
  2397. @end example
  2398. Note that in this example newlines appear in the output only as specified
  2399. by the @t{~&} and @t{~%} directives; the
  2400. actual newline characters
  2401. in the control string are suppressed because each is preceded by a tilde.
  2402. @node Additional Information about FORMAT Operations, Examples of FORMAT, FORMAT Miscellaneous Pseudo-Operations, Formatted Output
  2403. @subsection Additional Information about FORMAT Operations
  2404. @menu
  2405. * Nesting of FORMAT Operations::
  2406. * Missing and Additional FORMAT Arguments::
  2407. * Additional FORMAT Parameters::
  2408. * Undefined FORMAT Modifier Combinations::
  2409. @end menu
  2410. @node Nesting of FORMAT Operations, Missing and Additional FORMAT Arguments, Additional Information about FORMAT Operations, Additional Information about FORMAT Operations
  2411. @subsubsection Nesting of FORMAT Operations
  2412. The case-conversion, conditional, iteration, and justification
  2413. constructs can contain other formatting constructs by bracketing them.
  2414. These constructs must nest properly with respect to each other.
  2415. For example, it is not legitimate to put the start of a case-conversion
  2416. construct in each arm of a conditional and the
  2417. end of the case-conversion construct outside the conditional:
  2418. @example
  2419. (format nil "~:[abc~:@@(def~;ghi~
  2420. :@@(jkl~]mno~)" x) ;Invalid!
  2421. @end example
  2422. This notation is invalid because the @t{~[...~;...~]}
  2423. and @t{~(...~)} constructs are not properly nested.
  2424. The processing indirection caused by the @t{~?} directive
  2425. is also a kind of nesting for the purposes of this rule of proper nesting.
  2426. It is not permitted to
  2427. start a bracketing construct within a string processed
  2428. under control of a @t{~?}
  2429. directive and end the construct at some point after the @t{~?} construct
  2430. in the string containing that construct, or vice versa.
  2431. For example, this situation is invalid:
  2432. @example
  2433. (format nil "~@@?ghi~)" "abc~@@(def") ;Invalid!
  2434. @end example
  2435. This notation
  2436. is invalid because the @t{~?}
  2437. and @t{~(...~)} constructs are not properly nested.
  2438. @node Missing and Additional FORMAT Arguments, Additional FORMAT Parameters, Nesting of FORMAT Operations, Additional Information about FORMAT Operations
  2439. @subsubsection Missing and Additional FORMAT Arguments
  2440. The consequences are undefined if no @i{arg} remains for a directive
  2441. requiring an argument. However, it is permissible for one or more @i{args}
  2442. to remain unprocessed by a directive; such @i{args} are ignored.
  2443. @node Additional FORMAT Parameters, Undefined FORMAT Modifier Combinations, Missing and Additional FORMAT Arguments, Additional Information about FORMAT Operations
  2444. @subsubsection Additional FORMAT Parameters
  2445. The consequences are undefined if a format directive is given more parameters
  2446. than it is described here as accepting.
  2447. @node Undefined FORMAT Modifier Combinations, , Additional FORMAT Parameters, Additional Information about FORMAT Operations
  2448. @subsubsection Undefined FORMAT Modifier Combinations
  2449. The consequences are undefined if @i{colon} or @i{at-sign} modifiers
  2450. are given to a directive in a combination not specifically described
  2451. here as being meaningful.
  2452. @node Examples of FORMAT, Notes about FORMAT, Additional Information about FORMAT Operations, Formatted Output
  2453. @subsection Examples of FORMAT
  2454. @example
  2455. (format nil "foo") @result{} "foo"
  2456. (setq x 5) @result{} 5
  2457. (format nil "The answer is ~D." x) @result{} "The answer is 5."
  2458. (format nil "The answer is ~3D." x) @result{} "The answer is 5."
  2459. (format nil "The answer is ~3,'0D." x) @result{} "The answer is 005."
  2460. (format nil "The answer is ~:D." (expt 47 x))
  2461. @result{} "The answer is 229,345,007."
  2462. (setq y "elephant") @result{} "elephant"
  2463. (format nil "Look at the ~A!" y) @result{} "Look at the elephant!"
  2464. (setq n 3) @result{} 3
  2465. (format nil "~D item~:P found." n) @result{} "3 items found."
  2466. (format nil "~R dog~:[s are~; is~] here." n (= n 1))
  2467. @result{} "three dogs are here."
  2468. (format nil "~R dog~:*~[s are~; is~:;s are~] here." n)
  2469. @result{} "three dogs are here."
  2470. (format nil "Here ~[are~;is~:;are~] ~:*~R pupp~:@@P." n)
  2471. @result{} "Here are three puppies."
  2472. @end example
  2473. @example
  2474. (defun foo (x)
  2475. (format nil "~6,2F|~6,2,1,'*F|~6,2,,'?F|~6F|~,2F|~F"
  2476. x x x x x x)) @result{} FOO
  2477. (foo 3.14159) @result{} " 3.14| 31.42| 3.14|3.1416|3.14|3.14159"
  2478. (foo -3.14159) @result{} " -3.14|-31.42| -3.14|-3.142|-3.14|-3.14159"
  2479. (foo 100.0) @result{} "100.00|******|100.00| 100.0|100.00|100.0"
  2480. (foo 1234.0) @result{} "1234.00|******|??????|1234.0|1234.00|1234.0"
  2481. (foo 0.006) @result{} " 0.01| 0.06| 0.01| 0.006|0.01|0.006"
  2482. @end example
  2483. @example
  2484. (defun foo (x)
  2485. (format nil
  2486. "~9,2,1,,'*E|~10,3,2,2,'?,,'$E|~
  2487. ~9,3,2,-2,'
  2488. x x x x))
  2489. (foo 3.14159) @result{} " 3.14E+0| 31.42$-01|+.003E+03| 3.14E+0"
  2490. (foo -3.14159) @result{} " -3.14E+0|-31.42$-01|-.003E+03| -3.14E+0"
  2491. (foo 1100.0) @result{} " 1.10E+3| 11.00$+02|+.001E+06| 1.10E+3"
  2492. (foo 1100.0L0) @result{} " 1.10L+3| 11.00$+02|+.001L+06| 1.10L+3"
  2493. (foo 1.1E13) @result{} "*********| 11.00$+12|+.001E+16| 1.10E+13"
  2494. (foo 1.1L120) @result{} "*********|??????????|
  2495. (foo 1.1L1200) @result{} "*********|??????????|
  2496. @end example
  2497. As an example of the effects of varying the scale factor, the code
  2498. @example
  2499. (dotimes (k 13)
  2500. (format t "~
  2501. (- k 5) (- k 5) 3.14159))
  2502. @end example
  2503. produces the following output:
  2504. @example
  2505. Scale factor -5: | 0.000003E+06|
  2506. Scale factor -4: | 0.000031E+05|
  2507. Scale factor -3: | 0.000314E+04|
  2508. Scale factor -2: | 0.003142E+03|
  2509. Scale factor -1: | 0.031416E+02|
  2510. Scale factor 0: | 0.314159E+01|
  2511. Scale factor 1: | 3.141590E+00|
  2512. Scale factor 2: | 31.41590E-01|
  2513. Scale factor 3: | 314.1590E-02|
  2514. Scale factor 4: | 3141.590E-03|
  2515. Scale factor 5: | 31415.90E-04|
  2516. Scale factor 6: | 314159.0E-05|
  2517. Scale factor 7: | 3141590.E-06|
  2518. @end example
  2519. @example
  2520. (defun foo (x)
  2521. (format nil "~9,2,1,,'*G|~9,3,2,3,'?,,'$G|~9,3,2,0,'
  2522. x x x x))
  2523. (foo 0.0314159) @result{} " 3.14E-2|314.2$-04|0.314E-01| 3.14E-2"
  2524. (foo 0.314159) @result{} " 0.31 |0.314 |0.314 | 0.31 "
  2525. (foo 3.14159) @result{} " 3.1 | 3.14 | 3.14 | 3.1 "
  2526. (foo 31.4159) @result{} " 31. | 31.4 | 31.4 | 31. "
  2527. (foo 314.159) @result{} " 3.14E+2| 314. | 314. | 3.14E+2"
  2528. (foo 3141.59) @result{} " 3.14E+3|314.2$+01|0.314E+04| 3.14E+3"
  2529. (foo 3141.59L0) @result{} " 3.14L+3|314.2$+01|0.314L+04| 3.14L+3"
  2530. (foo 3.14E12) @result{} "*********|314.0$+10|0.314E+13| 3.14E+12"
  2531. (foo 3.14L120) @result{} "*********|?????????|
  2532. (foo 3.14L1200) @result{} "*********|?????????|
  2533. @end example
  2534. @example
  2535. (format nil "~10<foo~;bar~>") @result{} "foo bar"
  2536. (format nil "~10:<foo~;bar~>") @result{} " foo bar"
  2537. (format nil "~10<foobar~>") @result{} " foobar"
  2538. (format nil "~10:<foobar~>") @result{} " foobar"
  2539. (format nil "~10:@@<foo~;bar~>") @result{} " foo bar "
  2540. (format nil "~10@@<foobar~>") @result{} "foobar "
  2541. (format nil "~10:@@<foobar~>") @result{} " foobar "
  2542. @end example
  2543. @example
  2544. (FORMAT NIL "Written to ~A." #P"foo.bin")
  2545. @result{} "Written to foo.bin."
  2546. @end example
  2547. @node Notes about FORMAT, , Examples of FORMAT, Formatted Output
  2548. @subsection Notes about FORMAT
  2549. Formatted output is performed not only by @b{format},
  2550. but by certain other functions that accept a @i{format control}
  2551. the way @b{format} does. For example, error-signaling functions
  2552. such as @b{cerror} accept @i{format controls}.
  2553. Note that the meaning of @b{nil} and @b{t} as destinations to @b{format}
  2554. are different than those of @b{nil} and @b{t} as @i{stream designators}.
  2555. The @t{~{@t{^}}} should appear only at the beginning of a @t{~<} clause,
  2556. because it aborts the entire clause in which it appears (as well as
  2557. all following clauses).
  2558. @c end of including concept-format
  2559. @node Printer Dictionary, , Formatted Output, Printer
  2560. @section Printer Dictionary
  2561. @c including dict-printer
  2562. @menu
  2563. * copy-pprint-dispatch::
  2564. * formatter::
  2565. * pprint-dispatch::
  2566. * pprint-exit-if-list-exhausted::
  2567. * pprint-fill::
  2568. * pprint-indent::
  2569. * pprint-logical-block::
  2570. * pprint-newline::
  2571. * pprint-pop::
  2572. * pprint-tab::
  2573. * print-object::
  2574. * print-unreadable-object::
  2575. * set-pprint-dispatch::
  2576. * write::
  2577. * write-to-string::
  2578. * *print-array*::
  2579. * *print-base*::
  2580. * *print-case*::
  2581. * *print-circle*::
  2582. * *print-escape*::
  2583. * *print-gensym*::
  2584. * *print-level*::
  2585. * *print-lines*::
  2586. * *print-miser-width*::
  2587. * *print-pprint-dispatch*::
  2588. * *print-pretty*::
  2589. * *print-readably*::
  2590. * *print-right-margin*::
  2591. * print-not-readable::
  2592. * print-not-readable-object::
  2593. * format::
  2594. @end menu
  2595. @node copy-pprint-dispatch, formatter, Printer Dictionary, Printer Dictionary
  2596. @subsection copy-pprint-dispatch [Function]
  2597. @code{copy-pprint-dispatch} @i{{&optional} table} @result{} @i{new-table}
  2598. @subsubheading Arguments and Values::
  2599. @i{table}---a @i{pprint dispatch table}, or @b{nil}.
  2600. @i{new-table}---a @i{fresh} @i{pprint dispatch table}.
  2601. @subsubheading Description::
  2602. Creates and returns a copy of the specified @i{table},
  2603. or of the @i{value} of @b{*print-pprint-dispatch*} if no @i{table} is specified,
  2604. or of the initial @i{value} of @b{*print-pprint-dispatch*} if @b{nil} is specified.
  2605. @subsubheading Exceptional Situations::
  2606. Should signal an error of @i{type} @b{type-error} if @i{table}
  2607. is not a @i{pprint dispatch table}.
  2608. @node formatter, pprint-dispatch, copy-pprint-dispatch, Printer Dictionary
  2609. @subsection formatter [Macro]
  2610. @code{formatter} @i{control-string} @result{} @i{function}
  2611. @subsubheading Arguments and Values::
  2612. @i{control-string}---a @i{format string}; not evaluated.
  2613. @i{function}---a @i{function}.
  2614. @subsubheading Description::
  2615. Returns a @i{function} which has behavior equivalent to:
  2616. @example
  2617. #'(lambda (*standard-output* &rest arguments)
  2618. (apply #'format t @i{control-string} arguments)
  2619. @i{arguments-tail})
  2620. @end example
  2621. where @i{arguments-tail} is either the tail of @i{arguments}
  2622. which has as its @i{car} the argument that would be processed next
  2623. if there were more format directives in the @i{control-string},
  2624. or else @b{nil} if no more @i{arguments} follow the most recently
  2625. processed argument.
  2626. @subsubheading Examples::
  2627. @example
  2628. (funcall (formatter "~&~A~A") *standard-output* 'a 'b 'c)
  2629. @t{ |> } AB
  2630. @result{} (C)
  2631. (format t (formatter "~&~A~A") 'a 'b 'c)
  2632. @t{ |> } AB
  2633. @result{} NIL
  2634. @end example
  2635. @subsubheading Exceptional Situations::
  2636. Might signal an error (at macro expansion time or at run time) if the argument
  2637. is not a valid @i{format string}.
  2638. @subsubheading See Also::
  2639. @ref{format}
  2640. @node pprint-dispatch, pprint-exit-if-list-exhausted, formatter, Printer Dictionary
  2641. @subsection pprint-dispatch [Function]
  2642. @code{pprint-dispatch} @i{object {&optional} table} @result{} @i{function, found-p}
  2643. @subsubheading Arguments and Values::
  2644. @i{object}---an @i{object}.
  2645. @i{table}---a @i{pprint dispatch table}, or @b{nil}.
  2646. The default is the @i{value} of @b{*print-pprint-dispatch*}.
  2647. @i{function}---a @i{function designator}.
  2648. @i{found-p}---a @i{generalized boolean}.
  2649. @subsubheading Description::
  2650. Retrieves the highest priority function in @i{table} that is
  2651. associated with a @i{type specifier} that matches @i{object}.
  2652. The function is chosen by finding all of the @i{type specifiers} in @i{table}
  2653. that match the @i{object} and
  2654. selecting the highest priority function associated with any of these
  2655. @i{type specifiers}. If there is more than one highest priority function,
  2656. an arbitrary choice is made. If no @i{type specifiers} match the
  2657. @i{object}, a function is returned that prints @i{object}
  2658. using @b{print-object}.
  2659. The @i{secondary value}, @i{found-p}, is @i{true} if a matching
  2660. @i{type specifier} was found in @i{table}, or @i{false} otherwise.
  2661. If @i{table} is @b{nil},
  2662. retrieval is done in the
  2663. @i{initial pprint dispatch table}.
  2664. @subsubheading Affected By::
  2665. The state of the @i{table}.
  2666. @subsubheading Exceptional Situations::
  2667. Should signal an error of @i{type} @b{type-error} if @i{table} is neither a
  2668. @i{pprint-dispatch-table} nor @b{nil}.
  2669. @subsubheading Notes::
  2670. @example
  2671. (let ((*print-pretty* t))
  2672. (write object :stream s))
  2673. @equiv{} (funcall (pprint-dispatch object) s object)
  2674. @end example
  2675. @node pprint-exit-if-list-exhausted, pprint-fill, pprint-dispatch, Printer Dictionary
  2676. @subsection pprint-exit-if-list-exhausted [Local Macro]
  2677. @subsubheading Syntax::
  2678. @code{pprint-exit-if-list-exhausted} @i{<@i{no @i{arguments}}>} @result{} @i{@b{nil}}
  2679. @subsubheading Description::
  2680. Tests whether or not the @i{list} passed to
  2681. the @i{lexically current logical block}
  2682. has been exhausted; see @ref{Dynamic Control of the Arrangement of Output}.
  2683. If this @i{list} has been
  2684. reduced to @b{nil}, @b{pprint-exit-if-list-exhausted} terminates the execution
  2685. of the @i{lexically current logical block} except for the printing
  2686. of the suffix. Otherwise @b{pprint-exit-if-list-exhausted} returns @b{nil}.
  2687. Whether or not @b{pprint-exit-if-list-exhausted} is @i{fbound} in the
  2688. @i{global environment} is @i{implementation-dependent};
  2689. however, the restrictions on redefinition and @i{shadowing} of
  2690. @b{pprint-exit-if-list-exhausted} are the same as for @i{symbols} in the @t{COMMON-LISP} @i{package}
  2691. which are @i{fbound} in the @i{global environment}.
  2692. The consequences of attempting to use @b{pprint-exit-if-list-exhausted} outside
  2693. of @b{pprint-logical-block} are undefined.
  2694. @subsubheading Exceptional Situations::
  2695. An error is signaled (at macro expansion time or at run time) if
  2696. @b{pprint-exit-if-list-exhausted} is used anywhere other than
  2697. lexically within a call on @b{pprint-logical-block}.
  2698. Also, the consequences of executing @b{pprint-if-list-exhausted} outside
  2699. of the dynamic extent of the @b{pprint-logical-block} which lexically
  2700. contains it are undefined.
  2701. @subsubheading See Also::
  2702. @ref{pprint-logical-block}
  2703. ,
  2704. @ref{pprint-pop}
  2705. .
  2706. @node pprint-fill, pprint-indent, pprint-exit-if-list-exhausted, Printer Dictionary
  2707. @subsection pprint-fill, pprint-linear, pprint-tabular [Function]
  2708. @code{pprint-fill} @i{stream object {&optional} colon-p at-sign-p} @result{} @i{@b{nil}}
  2709. @code{pprint-linear} @i{stream object {&optional} colon-p at-sign-p} @result{} @i{@b{nil}}
  2710. @code{pprint-tabular} @i{stream object {&optional} colon-p at-sign-p tabsize} @result{} @i{@b{nil}}
  2711. @subsubheading Arguments and Values::
  2712. @i{stream}---an @i{output} @i{stream designator}.
  2713. @i{object}---an @i{object}.
  2714. @i{colon-p}---a @i{generalized boolean}.
  2715. The default is @i{true}.
  2716. @i{at-sign-p}---a @i{generalized boolean}.
  2717. The default is @i{implementation-dependent}.
  2718. @i{tabsize}---a non-negative @i{integer}.
  2719. The default is @t{16}.
  2720. @subsubheading Description::
  2721. The functions @b{pprint-fill}, @b{pprint-linear}, and
  2722. @b{pprint-tabular} specify particular ways of @i{pretty printing}
  2723. a @i{list} to @i{stream}.
  2724. Each function prints parentheses around the output if and only
  2725. if @i{colon-p} is @i{true}.
  2726. Each function ignores its @i{at-sign-p} argument.
  2727. (Both arguments are included even though only one is needed
  2728. so that these functions can be used via @t{~/.../}
  2729. and as @b{set-pprint-dispatch} functions, as well as directly.)
  2730. Each function handles abbreviation and the detection of circularity
  2731. and sharing correctly, and uses @b{write} to print @i{object}
  2732. when it is a @i{non-list}.
  2733. If @i{object} is a @i{list} and
  2734. if the @i{value} of @b{*print-pretty*} is @i{false},
  2735. each of these functions prints @i{object}
  2736. using a minimum of @i{whitespace},
  2737. as described in @ref{Printing Lists and Conses}.
  2738. Otherwise (if @i{object} is a @i{list} and
  2739. if the @i{value} of @b{*print-pretty*} is @i{true}):
  2740. @table @asis
  2741. @item @t{*}
  2742. The @i{function} @b{pprint-linear} prints a @i{list} either all on one line,
  2743. or with each @i{element} on a separate line.
  2744. @item @t{*}
  2745. The @i{function} @b{pprint-fill} prints a @i{list} with as many @i{elements}
  2746. as possible on each line.
  2747. @item @t{*}
  2748. The @i{function} @b{pprint-tabular} is the same as @b{pprint-fill}
  2749. except that it prints the @i{elements} so that they line up in columns.
  2750. The @i{tabsize} specifies the column spacing in @i{ems},
  2751. which is the total spacing from the leading edge of one column to
  2752. the leading edge of the next.
  2753. @end table
  2754. @subsubheading Examples::
  2755. Evaluating the following with a line length of @t{25} produces the output shown.
  2756. @example
  2757. (progn (princ "Roads ")
  2758. (pprint-tabular *standard-output* '(elm main maple center) nil nil 8))
  2759. Roads ELM MAIN
  2760. MAPLE CENTER
  2761. @end example
  2762. @subsubheading Side Effects::
  2763. Performs output to the indicated @i{stream}.
  2764. @subsubheading Affected By::
  2765. The cursor position on the indicated @i{stream}, if it can be determined.
  2766. @subsubheading Notes::
  2767. The @i{function} @b{pprint-tabular} could be defined as follows:
  2768. @example
  2769. (defun pprint-tabular (s list &optional (colon-p t) at-sign-p (tabsize nil))
  2770. (declare (ignore at-sign-p))
  2771. (when (null tabsize) (setq tabsize 16))
  2772. (pprint-logical-block (s list :prefix (if colon-p "(" "")
  2773. :suffix (if colon-p ")" ""))
  2774. (pprint-exit-if-list-exhausted)
  2775. (loop (write (pprint-pop) :stream s)
  2776. (pprint-exit-if-list-exhausted)
  2777. (write-char #\Space s)
  2778. (pprint-tab :section-relative 0 tabsize s)
  2779. (pprint-newline :fill s))))
  2780. @end example
  2781. Note that it would have been inconvenient to specify this function
  2782. using @b{format}, because of the need to pass its @i{tabsize} argument
  2783. through to a @t{~:T} format directive nested within an iteration over a list.
  2784. @node pprint-indent, pprint-logical-block, pprint-fill, Printer Dictionary
  2785. @subsection pprint-indent [Function]
  2786. @code{pprint-indent} @i{relative-to n {&optional} stream} @result{} @i{@b{nil}}
  2787. @subsubheading Arguments and Values::
  2788. @i{relative-to}---either @t{:block} or @t{:current}.
  2789. @i{n}---a @i{real}.
  2790. @i{stream}---an @i{output} @i{stream designator}.
  2791. The default is @i{standard output}.
  2792. @subsubheading Description::
  2793. @b{pprint-indent} specifies the indentation to use in a logical block on @i{stream}.
  2794. If @i{stream} is a @i{pretty printing stream}
  2795. and the @i{value} of @b{*print-pretty*} is @i{true},
  2796. @b{pprint-indent} sets the indentation in the innermost
  2797. dynamically enclosing logical block;
  2798. otherwise, @b{pprint-indent} has no effect.
  2799. @i{N} specifies the indentation in
  2800. @i{ems}. If @i{relative-to} is @t{:block}, the indentation is set
  2801. to the horizontal position of the first character in the @i{dynamically current logical block} plus @i{n}
  2802. @i{ems}. If @i{relative-to} is @t{:current}, the indentation is set
  2803. to the current output position plus @i{n} @i{ems}. (For robustness
  2804. in the face of variable-width fonts, it is advisable to use @t{:current}
  2805. with an @i{n} of zero whenever possible.)
  2806. @i{N} can be negative;
  2807. however, the total indentation cannot be moved
  2808. left of the beginning of the line
  2809. or left of the end of the rightmost per-line prefix---an attempt to move beyond
  2810. one of these limits is treated
  2811. the same as an attempt to move to that limit.
  2812. Changes in indentation caused by @i{pprint-indent}
  2813. do not take effect until after the next line break. In addition, in
  2814. miser mode all calls to @b{pprint-indent} are ignored, forcing the lines
  2815. corresponding to the logical block to line up under the first character in
  2816. the block.
  2817. @subsubheading Exceptional Situations::
  2818. An error is signaled if @i{relative-to} is any @i{object} other
  2819. than @t{:block} or @t{:current}.
  2820. @subsubheading See Also::
  2821. @ref{Tilde I-> Indent}
  2822. @node pprint-logical-block, pprint-newline, pprint-indent, Printer Dictionary
  2823. @subsection pprint-logical-block [Macro]
  2824. @code{pprint-logical-block} @i{@r{(}stream-symbol object
  2825. {&key} prefix per-line-prefix suffix@r{)}
  2826. @{@i{declaration}@}{*} @{@i{form}@}{*}}@*
  2827. @result{} @i{@b{nil}}
  2828. @subsubheading Arguments and Values::
  2829. @i{stream-symbol}---a @i{stream variable designator}.
  2830. @i{object}---an @i{object}; evaluated.
  2831. @t{:prefix}---a @i{string}; evaluated.
  2832. Complicated defaulting behavior; see below.
  2833. @t{:per-line-prefix}---a @i{string}; evaluated.
  2834. Complicated defaulting behavior; see below.
  2835. @t{:suffix}---a @i{string}; evaluated.
  2836. The default is the @i{null} @i{string}.
  2837. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  2838. @i{forms}---an @i{implicit progn}.
  2839. @subsubheading Description::
  2840. Causes printing to be grouped into a logical block.
  2841. The logical block is printed to the @i{stream} that is the @i{value}
  2842. of the @i{variable} denoted by @i{stream-symbol}.
  2843. During the execution of the @i{forms},
  2844. that @i{variable} is @i{bound} to a @i{pretty printing stream}
  2845. that supports decisions about the arrangement of output
  2846. and then forwards the output to the destination stream.
  2847. All the standard printing functions
  2848. (@i{e.g.}, @b{write},
  2849. @b{princ},
  2850. and @b{terpri})
  2851. can be used to print output to the @i{pretty printing stream}.
  2852. All and only the output sent to this @i{pretty printing stream}
  2853. is treated as being in the logical block.
  2854. The @i{prefix} specifies a prefix to be printed before the beginning of
  2855. the logical block.
  2856. The @i{per-line-prefix} specifies a prefix that is printed before the block
  2857. and at the beginning of each new line in the block.
  2858. The @t{:prefix} and @t{:pre-line-prefix} @i{arguments} are mutually exclusive.
  2859. If neither @t{:prefix} nor @t{:per-line-prefix} is specified,
  2860. a @i{prefix} of the @i{null} @i{string} is assumed.
  2861. The @i{suffix} specifies a suffix that is printed just after the logical block.
  2862. The @i{object} is
  2863. normally
  2864. a @i{list} that the body @i{forms} are responsible for printing.
  2865. If @i{object} is not a @i{list},
  2866. it is printed using @b{write}.
  2867. (This makes it easier to write printing functions that are robust
  2868. in the face of malformed arguments.)
  2869. If @b{*print-circle*}
  2870. is @i{non-nil} and @i{object} is a circular (or shared) reference to a @i{cons},
  2871. then an appropriate ``@t{#@i{n}#}'' marker is printed. (This
  2872. makes it easy to write printing functions that provide full support
  2873. for circularity and sharing abbreviation.) If @b{*print-level*} is not
  2874. @b{nil} and the logical block is at a dynamic nesting depth of greater
  2875. than @b{*print-level*} in logical blocks, ``@t{#}'' is printed.
  2876. (This makes easy to write printing functions that provide full support for depth
  2877. abbreviation.)
  2878. If either of the three conditions above occurs, the indicated output is
  2879. printed on @i{stream-symbol} and the body @i{forms} are skipped
  2880. along with the printing of the @t{:prefix} and @t{:suffix}.
  2881. (If the body @i{forms} are not to be responsible for printing a list,
  2882. then the first two tests above can be turned off by supplying @b{nil} for
  2883. the @i{object} argument.)
  2884. In addition to the @i{object} argument of @b{pprint-logical-block},
  2885. the arguments of the standard printing functions (such as @b{write},
  2886. @b{print}, @b{prin1}, and @b{pprint}, as well as the arguments
  2887. of the standard @i{format directives} such as @t{~A}, @t{~S},
  2888. (and @t{~W}) are all checked (when necessary) for circularity and sharing.
  2889. However, such checking is not applied to the arguments of the
  2890. functions @b{write-line}, @b{write-string}, and @b{write-char}
  2891. or to the literal text output by @b{format}. A consequence of this is
  2892. that you must use one of the latter functions if you want to print some
  2893. literal text in the output that is not supposed to be checked for circularity
  2894. or sharing.
  2895. The body @i{forms} of a @b{pprint-logical-block} @i{form}
  2896. must not perform any side-effects on the surrounding environment; for
  2897. example, no @i{variables} must be assigned which have not been
  2898. @i{bound} within its scope.
  2899. The @b{pprint-logical-block} @i{macro} may be used regardless of the @i{value} of @b{*print-pretty*}.
  2900. @subsubheading Affected By::
  2901. @b{*print-circle*}, @b{*print-level*}.
  2902. @subsubheading Exceptional Situations::
  2903. An error of @i{type} @b{type-error} is signaled if any of the @t{:suffix},
  2904. @t{:prefix}, or @t{:per-line-prefix} is supplied but does not evaluate
  2905. to a @i{string}.
  2906. An error is signaled if @t{:prefix} and @t{:pre-line-prefix} are both used.
  2907. @b{pprint-logical-block} and the @i{pretty printing stream} it creates
  2908. have @i{dynamic extent}. The consequences are undefined if, outside
  2909. of this extent, output is attempted to the @i{pretty printing stream} it creates.
  2910. It is also unspecified what happens if, within this extent, any output is
  2911. sent directly to the underlying destination stream.
  2912. @subsubheading See Also::
  2913. @ref{pprint-pop}
  2914. ,
  2915. @ref{pprint-exit-if-list-exhausted}
  2916. ,
  2917. @ref{Tilde Less-Than-Sign-> Logical Block}
  2918. @subsubheading Notes::
  2919. One reason for using the @b{pprint-logical-block} @i{macro} when the @i{value} of @b{*print-pretty*}
  2920. is @b{nil} would be to allow it to perform checking for @i{dotted lists},
  2921. as well as (in conjunction with @b{pprint-pop})
  2922. checking for @b{*print-level*} or @b{*print-length*} being exceeded.
  2923. Detection of circularity and sharing is supported by the @i{pretty printer}
  2924. by in essence performing requested output twice. On the first pass,
  2925. circularities and sharing are detected and the actual outputting of characters
  2926. is suppressed. On the second pass, the appropriate ``@t{#@i{n}=}''
  2927. and ``@t{#@i{n}#}'' markers are inserted and characters are output.
  2928. This is why the restriction on side-effects is necessary.
  2929. Obeying this restriction is facilitated by using @b{pprint-pop},
  2930. instead of an ordinary @b{pop} when traversing a list being printed by
  2931. the body @i{forms} of the @b{pprint-logical-block} @i{form}.)
  2932. @node pprint-newline, pprint-pop, pprint-logical-block, Printer Dictionary
  2933. @subsection pprint-newline [Function]
  2934. @code{pprint-newline} @i{kind {&optional} stream} @result{} @i{@b{nil}}
  2935. @subsubheading Arguments and Values::
  2936. @i{kind}---one of @t{:linear}, @t{:fill}, @t{:miser}, or @t{:mandatory}.
  2937. @i{stream}---a @i{stream designator}.
  2938. The default is @i{standard output}.
  2939. @subsubheading Description::
  2940. If @i{stream} is a @i{pretty printing stream}
  2941. and the @i{value} of @b{*print-pretty*} is @i{true},
  2942. a line break is inserted in the output
  2943. when the appropriate condition below is satisfied;
  2944. otherwise, @b{pprint-newline} has no effect.
  2945. @i{Kind} specifies the style of conditional newline.
  2946. This @i{parameter} is treated as follows:
  2947. @table @asis
  2948. @item @t{:linear}
  2949. This specifies a
  2950. ``linear-style'' @i{conditional newline}.
  2951. @ITindex{linear-style conditional newline}
  2952. A line break is inserted
  2953. if and only if the immediately containing @i{section}
  2954. cannot be printed on one line.
  2955. The effect of this is that line breaks are
  2956. either inserted at every linear-style conditional newline in a logical block
  2957. or at none of them.
  2958. @item @t{:miser}
  2959. This specifies a
  2960. ``miser-style'' @i{conditional newline}.
  2961. @ITindex{miser-style conditional newline}
  2962. A line break is inserted
  2963. if and only if the immediately containing @i{section}
  2964. cannot be printed on one line
  2965. and miser style is in effect in the immediately containing logical block.
  2966. The effect of this is that miser-style conditional newlines
  2967. act like linear-style conditional newlines,
  2968. but only when miser style is in effect.
  2969. Miser style is in effect for a logical block if and only if
  2970. the starting position of the logical block
  2971. is less than or equal to
  2972. @b{*print-miser-width*} @i{ems} from the right margin.
  2973. @item @t{:fill}
  2974. This specifies a
  2975. ``fill-style'' @i{conditional newline}.
  2976. @ITindex{fill-style conditional newline}
  2977. A line break is inserted if and only if
  2978. either (a) the following @i{section} cannot be printed
  2979. on the end of the current line,
  2980. (b) the preceding @i{section} was not printed on a single line,
  2981. or (c) the immediately containing @i{section} cannot
  2982. be printed on one line and miser style is in effect
  2983. in the immediately containing logical block.
  2984. If a logical block is broken up into a number of subsections
  2985. by fill-style conditional newlines,
  2986. the basic effect is that the logical block
  2987. is printed with as many subsections as possible on each line.
  2988. However, if miser style is in effect,
  2989. fill-style conditional newlines act like linear-style conditional newlines.
  2990. @item @t{:mandatory}
  2991. This specifies a
  2992. ``mandatory-style'' @i{conditional newline}.
  2993. @ITindex{mandatory-style conditional newline}
  2994. A line break is always inserted.
  2995. This implies that none of the containing @i{sections}
  2996. can be printed on a single line and
  2997. will therefore trigger the insertion of line breaks
  2998. at linear-style conditional newlines in these @i{sections}.
  2999. @end table
  3000. When a line break is inserted by any type of conditional newline,
  3001. any blanks that immediately precede the conditional newline are omitted
  3002. from the output and indentation is introduced at the beginning of the next line.
  3003. By default, the indentation causes the following line to begin
  3004. in the same horizontal position
  3005. as the first character in the immediately containing logical block.
  3006. (The indentation can be changed via @b{pprint-indent}.)
  3007. There are a variety of ways unconditional newlines can be introduced into
  3008. the output (@i{i.e.}, via @b{terpri} or by printing a string containing a newline
  3009. character). As with mandatory conditional newlines, this prevents any of
  3010. the containing @i{sections} from being printed on one line. In general, when
  3011. an unconditional newline is encountered, it is printed out without
  3012. suppression of the preceding blanks and without any indentation following
  3013. it. However, if a per-line prefix has been specified (see
  3014. @b{pprint-logical-block}), this prefix will always be printed no matter
  3015. how a newline originates.
  3016. @subsubheading Examples::
  3017. See @ref{Examples of using the Pretty Printer}.
  3018. @subsubheading Side Effects::
  3019. Output to @i{stream}.
  3020. @subsubheading Affected By::
  3021. @b{*print-pretty*}, @b{*print-miser*}.
  3022. The presence of containing logical blocks.
  3023. The placement of newlines and conditional newlines.
  3024. @subsubheading Exceptional Situations::
  3025. An error of @i{type} @b{type-error} is signaled if @i{kind}
  3026. is not one of @t{:linear}, @t{:fill}, @t{:miser}, or @t{:mandatory}.
  3027. @subsubheading See Also::
  3028. @ref{Tilde Underscore-> Conditional Newline},
  3029. @ref{Examples of using the Pretty Printer}
  3030. @node pprint-pop, pprint-tab, pprint-newline, Printer Dictionary
  3031. @subsection pprint-pop [Local Macro]
  3032. @subsubheading Syntax::
  3033. @code{pprint-pop} @i{<@i{no @i{arguments}}>} @result{} @i{object}
  3034. @subsubheading Arguments and Values::
  3035. @i{object}---an @i{element} of the @i{list}
  3036. being printed in the @i{lexically current logical block},
  3037. or @b{nil}.
  3038. @subsubheading Description::
  3039. Pops one @i{element} from the @i{list} being printed
  3040. in the @i{lexically current logical block}, obeying @b{*print-length*}
  3041. and @b{*print-circle*} as described below.
  3042. Each time @b{pprint-pop} is called, it pops the next value off the
  3043. @i{list} passed to the @i{lexically current logical block} and returns it.
  3044. However, before doing this, it performs three tests:
  3045. @table @asis
  3046. @item @t{*}
  3047. If the remaining `list' is not a @i{list},
  3048. ``@t{. }'' is printed followed by the remaining `list.'
  3049. (This makes it easier to write printing functions that
  3050. are robust in the face of malformed arguments.)
  3051. @item @t{*}
  3052. If @b{*print-length*} is @i{non-nil},
  3053. and @b{pprint-pop} has already been called @b{*print-length*} times
  3054. within the immediately containing logical block,
  3055. ``@t{...}'' is printed.
  3056. (This makes it easy to write printing functions that properly handle
  3057. @b{*print-length*}.)
  3058. @item @t{*}
  3059. If @b{*print-circle*} is
  3060. @i{non-nil}, and the remaining list is a circular (or shared) reference,
  3061. then ``@t{. }'' is printed followed by an appropriate
  3062. ``@t{#@i{n}#}'' marker.
  3063. (This catches instances of @i{cdr} circularity and sharing in lists.)
  3064. @end table
  3065. If either of the three conditions above occurs, the indicated output is
  3066. printed on the @i{pretty printing stream} created by the immediately containing
  3067. @b{pprint-logical-block} and the execution of the immediately containing
  3068. @b{pprint-logical-block} is terminated except for the printing of the suffix.
  3069. If @b{pprint-logical-block} is given a `list' argument of @b{nil}---because
  3070. it is not processing a list---@b{pprint-pop} can still be used to obtain
  3071. support for @b{*print-length*}.
  3072. In this situation, the first and third tests above are disabled and
  3073. @b{pprint-pop} always returns @b{nil}.
  3074. See @ref{Examples of using the Pretty Printer}---specifically, the @b{pprint-vector} example.
  3075. Whether or not @b{pprint-pop} is @i{fbound} in the
  3076. @i{global environment} is @i{implementation-dependent};
  3077. however, the restrictions on redefinition and @i{shadowing} of
  3078. @b{pprint-pop} are the same as for @i{symbols} in the @t{COMMON-LISP} @i{package}
  3079. which are @i{fbound} in the @i{global environment}.
  3080. The consequences of attempting to use @b{pprint-pop} outside
  3081. of @b{pprint-logical-block} are undefined.
  3082. @subsubheading Side Effects::
  3083. Might cause output
  3084. to the @i{pretty printing stream} associated with the lexically current logical block.
  3085. @subsubheading Affected By::
  3086. @b{*print-length*}, @b{*print-circle*}.
  3087. @subsubheading Exceptional Situations::
  3088. An error is signaled (either at macro expansion time or at run time)
  3089. if a usage of @b{pprint-pop} occurs where there is no lexically
  3090. containing @b{pprint-logical-block} @i{form}.
  3091. The consequences are undefined if @b{pprint-pop} is executed outside
  3092. of the @i{dynamic extent} of this @b{pprint-logical-block}.
  3093. @subsubheading See Also::
  3094. @ref{pprint-exit-if-list-exhausted}
  3095. ,
  3096. @ref{pprint-logical-block}
  3097. .
  3098. @subsubheading Notes::
  3099. It is frequently a good idea to call @b{pprint-exit-if-list-exhausted}
  3100. before calling @b{pprint-pop}.
  3101. @node pprint-tab, print-object, pprint-pop, Printer Dictionary
  3102. @subsection pprint-tab [Function]
  3103. @code{pprint-tab} @i{kind colnum colinc {&optional} stream} @result{} @i{@b{nil}}
  3104. @subsubheading Arguments and Values::
  3105. @i{kind}---one of @t{:line}, @t{:section}, @t{:line-relative},
  3106. or @t{:section-relative}.
  3107. @i{colnum}---a non-negative @i{integer}.
  3108. @i{colinc}---a non-negative @i{integer}.
  3109. @i{stream}---an @i{output} @i{stream designator}.
  3110. @subsubheading Description::
  3111. Specifies tabbing to @i{stream} as performed by the standard @t{~T} format directive.
  3112. If @i{stream} is a @i{pretty printing stream} and
  3113. the @i{value} of @b{*print-pretty*} is @i{true},
  3114. tabbing is performed;
  3115. otherwise, @b{pprint-tab} has no effect.
  3116. The arguments @i{colnum} and @i{colinc} correspond to the two
  3117. @i{parameters} to @t{~T} and are in terms of @i{ems}.
  3118. The @i{kind} argument specifies the style of tabbing. It must be one of
  3119. @t{:line} (tab as by @t{~T}),
  3120. @t{:section} (tab as by @t{~:T},
  3121. but measuring horizontal positions relative to
  3122. the start of the dynamically enclosing section),
  3123. @t{:line-relative} (tab as by @t{~@@T}), or
  3124. @t{:section-relative} (tab as by @t{~:@@T},
  3125. but measuring horizontal positions relative to
  3126. the start of the dynamically enclosing section).
  3127. @subsubheading Exceptional Situations::
  3128. An error is signaled if @i{kind} is not one of @t{:line},
  3129. @t{:section}, @t{:line-relative}, or @t{:section-relative}.
  3130. @subsubheading See Also::
  3131. @ref{pprint-logical-block}
  3132. @node print-object, print-unreadable-object, pprint-tab, Printer Dictionary
  3133. @subsection print-object [Standard Generic Function]
  3134. @subsubheading Syntax::
  3135. @code{print-object} @i{object stream} @result{} @i{object}
  3136. @subsubheading Method Signatures::
  3137. @code{print-object} @i{@r{(}@i{object} standard-object@r{)} @i{stream}}
  3138. @code{print-object} @i{@r{(}@i{object} structure-object@r{)} @i{stream}}
  3139. @subsubheading Arguments and Values::
  3140. @i{object}---an @i{object}.
  3141. @i{stream}---a @i{stream}.
  3142. @subsubheading Description::
  3143. The @i{generic function} @b{print-object} writes the printed representation of @i{object}
  3144. to @i{stream}.
  3145. The @i{function} @b{print-object} is called by the @i{Lisp printer};
  3146. it should not be called by the user.
  3147. Each implementation is required to provide a @i{method} on
  3148. the @i{class} @b{standard-object} and on the @i{class} @b{structure-object}.
  3149. In addition, each @i{implementation} must provide
  3150. @i{methods} on enough other @i{classes}
  3151. so as to ensure that there is always an applicable @i{method}.
  3152. Implementations are free to add @i{methods} for other @i{classes}.
  3153. Users may write @i{methods} for @b{print-object} for their own
  3154. @i{classes} if they do not wish to inherit an
  3155. @i{implementation-dependent} @i{method}.
  3156. The @i{method} on the @i{class} @b{structure-object} prints the object in the
  3157. default @t{#S} notation; see @ref{Printing Structures}.
  3158. @i{Methods} on @b{print-object} are responsible for implementing
  3159. their part of the semantics of the @i{printer control variables}, as follows:
  3160. @table @asis
  3161. @item @b{*print-readably*}
  3162. All methods for @b{print-object} must obey @b{*print-readably*}.
  3163. This includes both user-defined methods and @i{implementation-defined} methods.
  3164. Readable printing of @i{structures} and @i{standard objects}
  3165. is controlled by their @b{print-object} method,
  3166. not by their @b{make-load-form} @i{method}.
  3167. @i{Similarity} for these @i{objects} is application dependent
  3168. and hence is defined to be whatever these @i{methods} do;
  3169. see @ref{Similarity of Literal Objects}.
  3170. @item @b{*print-escape*}
  3171. Each @i{method} must implement @b{*print-escape*}.
  3172. @item @b{*print-pretty*}
  3173. The @i{method} may wish to perform specialized line breaking
  3174. or other output conditional on the @i{value} of @b{*print-pretty*}.
  3175. For further information,
  3176. see (for example) the @i{macro} @b{pprint-fill}.
  3177. See also @ref{Pretty Print Dispatch Tables} and @ref{Examples of using the Pretty Printer}.
  3178. @item @b{*print-length*}
  3179. @i{Methods} that produce output of indefinite length must obey
  3180. @b{*print-length*}.
  3181. For further information,
  3182. see (for example) the @i{macros} @b{pprint-logical-block}
  3183. and @b{pprint-pop}.
  3184. See also @ref{Pretty Print Dispatch Tables} and @ref{Examples of using the Pretty Printer}.
  3185. @item @b{*print-level*}
  3186. The printer takes care of @b{*print-level*} automatically,
  3187. provided that each @i{method} handles exactly one level of structure and
  3188. calls @b{write} (or an equivalent @i{function}) recursively if
  3189. there are more structural levels. The printer's decision of whether an
  3190. @i{object} has components (and therefore should not be printed when the
  3191. printing depth is not less than @b{*print-level*}) is
  3192. @i{implementation-dependent}. In some implementations its
  3193. @b{print-object} @i{method} is not called;
  3194. in others the @i{method} is called,
  3195. and the determination that the @i{object} has components is based on what
  3196. it tries to write to the @i{stream}.
  3197. @item @b{*print-circle*}
  3198. When the @i{value} of @b{*print-circle*} is @i{true},
  3199. a user-defined
  3200. @b{print-object} @i{method}
  3201. can print @i{objects} to the supplied @i{stream}
  3202. using @b{write},
  3203. @b{prin1},
  3204. @b{princ},
  3205. or @b{format}
  3206. and expect circularities to be detected
  3207. and printed using the @t{#@i{n}#} syntax.
  3208. If a user-defined
  3209. @b{print-object} @i{method}
  3210. prints to a @i{stream} other than the one
  3211. that was supplied, then circularity detection starts over for that
  3212. @i{stream}. See @b{*print-circle*}.
  3213. @item @b{*print-base*},
  3214. @b{*print-radix*},
  3215. @b{*print-case*},
  3216. @b{*print-gensym*},
  3217. and @b{*print-array*}
  3218. These @i{printer control variables} apply to specific types of @i{objects}
  3219. and are handled by the @i{methods} for those @i{objects}.
  3220. @end table
  3221. If these rules are not obeyed, the results are undefined.
  3222. In general, the printer and the @b{print-object} methods should not
  3223. rebind the print control variables as they operate recursively through the
  3224. structure, but this is @i{implementation-dependent}.
  3225. In some implementations the @i{stream} argument passed to a
  3226. @b{print-object} @i{method} is not the original @i{stream},
  3227. but is an intermediate @i{stream} that implements part of the printer.
  3228. @i{methods} should therefore not depend on the identity of this @i{stream}.
  3229. @subsubheading See Also::
  3230. @ref{pprint-fill; pprint-linear; pprint-tabular}
  3231. ,
  3232. @ref{pprint-logical-block}
  3233. ,
  3234. @ref{pprint-pop}
  3235. ,
  3236. @ref{write; prin1; print; pprint; princ}
  3237. ,
  3238. @b{*print-readably*},
  3239. @b{*print-escape*},
  3240. @b{*print-pretty*},
  3241. @b{*print-length*},
  3242. @ref{Default Print-Object Methods},
  3243. @ref{Printing Structures},
  3244. @ref{Pretty Print Dispatch Tables},
  3245. @ref{Examples of using the Pretty Printer}
  3246. @node print-unreadable-object, set-pprint-dispatch, print-object, Printer Dictionary
  3247. @subsection print-unreadable-object [Macro]
  3248. @code{print-unreadable-object} @i{@r{(}object stream {&key} type identity@r{)} @{@i{form}@}{*}} @result{} @i{@b{nil}}
  3249. @subsubheading Arguments and Values::
  3250. @i{object}---an @i{object}; evaluated.
  3251. @i{stream}---
  3252. a @i{stream designator}; evaluated.
  3253. @i{type}---a @i{generalized boolean}; evaluated.
  3254. @i{identity}---a @i{generalized boolean}; evaluated.
  3255. @i{forms}---an @i{implicit progn}.
  3256. @subsubheading Description::
  3257. Outputs a printed representation of @i{object} on @i{stream},
  3258. beginning with ``@t{#<}'' and ending with ``@t{>}''.
  3259. Everything output to @i{stream} by the body @i{forms}
  3260. is enclosed in the the angle brackets.
  3261. If @i{type} is @i{true}, the output from @i{forms}
  3262. is preceded by a brief description of the @i{object}'s
  3263. @i{type} and a space character.
  3264. If @i{identity} is @i{true},
  3265. the output from @i{forms} is followed by a space character
  3266. and a representation of the @i{object}'s identity,
  3267. typically a storage address.
  3268. If either @i{type} or @i{identity} is not supplied,
  3269. its value is @i{false}. It is valid to omit the body @i{forms}.
  3270. If @i{type} and @i{identity} are both true and there are no
  3271. body @i{forms}, only one space character separates the type
  3272. and the identity.
  3273. @subsubheading Examples::
  3274. ;; Note that in this example, the precise form of the output
  3275. ;; is @i{implementation-dependent}.
  3276. @example
  3277. (defmethod print-object ((obj airplane) stream)
  3278. (print-unreadable-object (obj stream :type t :identity t)
  3279. (princ (tail-number obj) stream)))
  3280. (prin1-to-string my-airplane)
  3281. @result{} "#<Airplane NW0773 36000123135>"
  3282. @i{OR}@result{} "#<FAA:AIRPLANE NW0773 17>"
  3283. @end example
  3284. @subsubheading Exceptional Situations::
  3285. If @b{*print-readably*} is @i{true}, @b{print-unreadable-object}
  3286. signals an error of @i{type} @b{print-not-readable} without printing anything.
  3287. @node set-pprint-dispatch, write, print-unreadable-object, Printer Dictionary
  3288. @subsection set-pprint-dispatch [Function]
  3289. @code{set-pprint-dispatch} @i{type-specifier function {&optional} priority table} @result{} @i{@b{nil}}
  3290. @subsubheading Arguments and Values::
  3291. @i{type-specifier}---a @i{type specifier}.
  3292. @i{function}---a @i{function}, a @i{function name}, or @b{nil}.
  3293. @i{priority}---a @i{real}.
  3294. The default is @t{0}.
  3295. @i{table}---a @i{pprint dispatch table}.
  3296. The default is the @i{value} of @b{*print-pprint-dispatch*}.
  3297. @subsubheading Description::
  3298. Installs an entry into the @i{pprint dispatch table} which is @i{table}.
  3299. @i{Type-specifier}
  3300. is the @i{key}
  3301. of the entry. The first action of @b{set-pprint-dispatch} is to remove any
  3302. pre-existing entry associated with @i{type-specifier}. This guarantees that
  3303. there will never be two entries associated with the same @i{type specifier}
  3304. in a given @i{pprint dispatch table}. Equality of @i{type specifiers} is
  3305. tested by @b{equal}.
  3306. Two values are associated with each @i{type specifier} in a
  3307. @i{pprint dispatch table}: a @i{function} and a @i{priority}.
  3308. The @i{function} must accept two arguments: the @i{stream} to which output
  3309. is sent and the @i{object} to be printed. The @i{function} should
  3310. @i{pretty print} the @i{object} to the @i{stream}. The @i{function}
  3311. can assume that object satisfies the @i{type} given by @i{type-specifier}.
  3312. The @i{function} must obey @b{*print-readably*}.
  3313. Any values returned by the @i{function} are ignored.
  3314. @i{Priority} is a priority to resolve conflicts
  3315. when an object matches more than one entry.
  3316. It is permissible for @i{function} to be @b{nil}. In this situation,
  3317. there will be no @i{type-specifier} entry in @i{table} after
  3318. @b{set-pprint-dispatch} returns.
  3319. @subsubheading Exceptional Situations::
  3320. An error is signaled if @i{priority} is not a @i{real}.
  3321. @subsubheading Notes::
  3322. Since @i{pprint dispatch tables} are often used to control the pretty
  3323. printing of Lisp code, it is common for the @i{type-specifier} to be
  3324. an @i{expression} of the form
  3325. @example
  3326. (cons @i{car-type} @i{cdr-type})
  3327. @end example
  3328. This signifies that the corresponding object must be a cons cell
  3329. whose @i{car} matches the @i{type specifier} @i{car-type}
  3330. and whose @i{cdr} matches the @i{type specifier} @i{cdr-type}.
  3331. The @i{cdr-type} can be omitted in which case it defaults to @b{t}.
  3332. @node write, write-to-string, set-pprint-dispatch, Printer Dictionary
  3333. @subsection write, prin1, print, pprint, princ [Function]
  3334. @code{write} @i{@i{object} {&key} \writekeys{stream}}@*
  3335. @result{} @i{object}
  3336. @code{prin} @i{1} @result{} @i{object {&optional} output-stream}
  3337. {object}
  3338. @code{princ} @i{object {&optional} output-stream} @result{} @i{object}
  3339. @code{print} @i{object {&optional} output-stream} @result{} @i{object}
  3340. @code{pprint} @i{object {&optional} output-stream} @result{} @i{<@i{no @i{values}}>}
  3341. @subsubheading Arguments and Values::
  3342. @i{object}---an @i{object}.
  3343. @i{output-stream}---an @i{output} @i{stream designator}.
  3344. The default is @i{standard output}.
  3345. \writekeydescriptions{@i{stream}---an @i{output} @i{stream designator}.
  3346. The default is @i{standard output}.}
  3347. @subsubheading Description::
  3348. @b{write}, @b{prin1}, @b{princ}, @b{print}, and @b{pprint}
  3349. write the printed representation of @i{object} to @i{output-stream}.
  3350. @b{write} is the general entry point to the @i{Lisp printer}.
  3351. For each explicitly supplied @i{keyword parameter} named in Figure 22--6,
  3352. the corresponding @i{printer control variable} is dynamically bound to its @i{value}
  3353. while printing goes on;
  3354. for each @i{keyword parameter} in Figure 22--6 that is not explicitly supplied,
  3355. the value of the corresponding @i{printer control variable} is the same as it was
  3356. at the time @b{write} was invoked.
  3357. Once the appropriate @i{bindings} are @i{established},
  3358. the @i{object} is output by the @i{Lisp printer}.
  3359. @group
  3360. @noindent
  3361. @w{ Parameter Corresponding Dynamic Variable }
  3362. @w{ @i{array} @b{*print-array*} }
  3363. @w{ @i{base} @b{*print-base*} }
  3364. @w{ @i{case} @b{*print-case*} }
  3365. @w{ @i{circle} @b{*print-circle*} }
  3366. @w{ @i{escape} @b{*print-escape*} }
  3367. @w{ @i{gensym} @b{*print-gensym*} }
  3368. @w{ @i{length} @b{*print-length*} }
  3369. @w{ @i{level} @b{*print-level*} }
  3370. @w{ @i{lines} @b{*print-lines*} }
  3371. @w{ @i{miser-width} @b{*print-miser-width*} }
  3372. @w{ @i{pprint-dispatch} @b{*print-pprint-dispatch*} }
  3373. @w{ @i{pretty} @b{*print-pretty*} }
  3374. @w{ @i{radix} @b{*print-radix*} }
  3375. @w{ @i{readably} @b{*print-readably*} }
  3376. @w{ @i{right-margin} @b{*print-right-margin*} }
  3377. @noindent
  3378. @w{ Figure 22--6: Argument correspondences for the WRITE function.}
  3379. @end group
  3380. @b{prin1}, @b{princ}, @b{print}, and @b{pprint} implicitly
  3381. @i{bind} certain print parameters to particular values. The remaining parameter
  3382. values are taken from
  3383. @b{*print-array*},
  3384. @b{*print-base*},
  3385. @b{*print-case*},
  3386. @b{*print-circle*},
  3387. @b{*print-escape*},
  3388. @b{*print-gensym*},
  3389. @b{*print-length*},
  3390. @b{*print-level*},
  3391. @b{*print-lines*},
  3392. @b{*print-miser-width*},
  3393. @b{*print-pprint-dispatch*},
  3394. @b{*print-pretty*},
  3395. @b{*print-radix*},
  3396. and @b{*print-right-margin*}.
  3397. @b{prin1} produces output suitable for input to @b{read}.
  3398. It binds @b{*print-escape*} to @i{true}.
  3399. @b{princ} is just like @b{prin1} except that the
  3400. output has no @i{escape} @i{characters}.
  3401. It binds @b{*print-escape*} to @i{false}
  3402. and @b{*print-readably*} to @i{false}.
  3403. The general rule is that output from @b{princ} is intended to look
  3404. good to people, while output from @b{prin1} is intended to
  3405. be acceptable to @b{read}.
  3406. @b{print} is just like @b{prin1}
  3407. except that the printed representation
  3408. of @i{object} is preceded by a newline
  3409. and followed by a space.
  3410. @b{pprint} is just like @b{print} except that the trailing
  3411. space is omitted and
  3412. @i{object} is printed with the @b{*print-pretty*} flag @i{non-nil}
  3413. to produce pretty output.
  3414. @i{Output-stream} specifies the @i{stream} to which
  3415. output is to be sent.
  3416. @subsubheading Affected By::
  3417. @b{*standard-output*},
  3418. @b{*terminal-io*},
  3419. @b{*print-escape*},
  3420. @b{*print-radix*},
  3421. @b{*print-base*},
  3422. @b{*print-circle*},
  3423. @b{*print-pretty*},
  3424. @b{*print-level*},
  3425. @b{*print-length*},
  3426. @b{*print-case*},
  3427. @b{*print-gensym*},
  3428. @b{*print-array*},
  3429. @b{*read-default-float-format*}.
  3430. @subsubheading See Also::
  3431. @ref{readtable-case}
  3432. ,
  3433. @ref{FORMAT Printer Operations}
  3434. @subsubheading Notes::
  3435. The @i{functions} @b{prin1} and @b{print} do not bind @b{*print-readably*}.
  3436. @example
  3437. (prin1 object output-stream)
  3438. @equiv{} (write object :stream output-stream :escape t)
  3439. @end example
  3440. @example
  3441. (princ object output-stream)
  3442. @equiv{} (write object stream output-stream :escape nil :readably nil)
  3443. @end example
  3444. @example
  3445. (print object output-stream)
  3446. @equiv{} (progn (terpri output-stream)
  3447. (write object :stream output-stream
  3448. :escape t)
  3449. (write-char #\space output-stream))
  3450. @end example
  3451. @example
  3452. (pprint object output-stream)
  3453. @equiv{} (write object :stream output-stream :escape t :pretty t)
  3454. @end example
  3455. @node write-to-string, *print-array*, write, Printer Dictionary
  3456. @subsection write-to-string, prin1-to-string, princ-to-string [Function]
  3457. @code{write-to-string} @i{object {&key} \writekeys{}}@*
  3458. @result{} @i{string}
  3459. @code{prin} @i{1} @result{} @i{-to-string}
  3460. {object} {string}
  3461. @code{princ-to-string} @i{object} @result{} @i{string}
  3462. @subsubheading Arguments and Values::
  3463. @i{object}---an @i{object}.
  3464. \writekeydescriptions{}
  3465. @i{string}---a @i{string}.
  3466. @subsubheading Description::
  3467. @b{write-to-string}, @b{prin1-to-string}, and @b{princ-to-string}
  3468. are used to create a @i{string} consisting of the printed representation
  3469. of @i{object}.
  3470. @i{Object} is effectively printed as if by @b{write},
  3471. @b{prin1}, or @b{princ}, respectively,
  3472. and the @i{characters} that would be output are made
  3473. into a @i{string}.
  3474. @b{write-to-string} is the general output function.
  3475. It has the ability to specify all the parameters applicable
  3476. to the printing of @i{object}.
  3477. @b{prin1-to-string} acts like @b{write-to-string} with
  3478. @t{:escape t}, that is, escape characters are written where appropriate.
  3479. @b{princ-to-string} acts like @b{write-to-string} with
  3480. @t{:escape nil :readably nil}.
  3481. Thus no @i{escape} @i{characters} are written.
  3482. All other keywords that would be specified to @b{write-to-string}
  3483. are default values when @b{prin1-to-string}
  3484. or @b{princ-to-string} is invoked.
  3485. The meanings and defaults for the keyword arguments to @b{write-to-string}
  3486. are the same as those for @b{write}.
  3487. @subsubheading Examples::
  3488. @example
  3489. (prin1-to-string "abc") @result{} "\"abc\""
  3490. (princ-to-string "abc") @result{} "abc"
  3491. @end example
  3492. @subsubheading Affected By::
  3493. @b{*print-escape*},
  3494. @b{*print-radix*},
  3495. @b{*print-base*},
  3496. @b{*print-circle*},
  3497. @b{*print-pretty*},
  3498. @b{*print-level*},
  3499. @b{*print-length*},
  3500. @b{*print-case*},
  3501. @b{*print-gensym*},
  3502. @b{*print-array*},
  3503. @b{*read-default-float-format*}.
  3504. @subsubheading See Also::
  3505. @ref{write; prin1; print; pprint; princ}
  3506. @subsubheading Notes::
  3507. @example
  3508. (write-to-string @i{object} @{@i{key} @i{argument}@}{*})
  3509. @equiv{} (with-output-to-string (#1=#:string-stream)
  3510. (write object :stream #1# @{@i{key} @i{argument}@}{*}))
  3511. (princ-to-string @i{object})
  3512. @equiv{} (with-output-to-string (string-stream)
  3513. (princ @i{object} string-stream))
  3514. (prin1-to-string @i{object})
  3515. @equiv{} (with-output-to-string (string-stream)
  3516. (prin1 @i{object} string-stream))
  3517. @end example
  3518. @node *print-array*, *print-base*, write-to-string, Printer Dictionary
  3519. @subsection *print-array* [Variable]
  3520. @subsubheading Value Type::
  3521. a @i{generalized boolean}.
  3522. @subsubheading Initial Value::
  3523. @i{implementation-dependent}.
  3524. @subsubheading Description::
  3525. Controls the format in which @i{arrays} are printed.
  3526. If it is @i{false}, the contents of @i{arrays} other than @i{strings}
  3527. are never printed. Instead, @i{arrays} are printed in a concise form using
  3528. @t{#<} that gives enough information for the user to be able to identify the
  3529. @i{array}, but does not include the entire @i{array} contents.
  3530. If it is @i{true}, non-@i{string} @i{arrays} are printed using
  3531. @t{#(...)}, @t{#*}, or @t{#nA} syntax.
  3532. @subsubheading Affected By::
  3533. The @i{implementation}.
  3534. @subsubheading See Also::
  3535. @ref{Sharpsign Left-Parenthesis},
  3536. @ref{Sharpsign Less-Than-Sign}
  3537. @node *print-base*, *print-case*, *print-array*, Printer Dictionary
  3538. @subsection *print-base*, *print-radix* [Variable]
  3539. @subsubheading Value Type::
  3540. @b{*print-base*}---a @i{radix}.
  3541. @b{*print-radix*}---a @i{generalized boolean}.
  3542. @subsubheading Initial Value::
  3543. The initial @i{value} of @b{*print-base*} is @t{10}.
  3544. The initial @i{value} of @b{*print-radix*} is @i{false}.
  3545. @subsubheading Description::
  3546. @b{*print-base*} and @b{*print-radix*} control the printing
  3547. of @i{rationals}.
  3548. The @i{value} of @b{*print-base*} is called the @i{current output base}
  3549. @IGindex{current output base}
  3550. .
  3551. The @i{value} of @b{*print-base*} is the @i{radix} in which the printer
  3552. will print @i{rationals}. For radices above @t{10}, letters of
  3553. the alphabet are used to represent digits above @t{9}.
  3554. If the @i{value} of @b{*print-radix*} is @i{true},
  3555. the printer will print a radix specifier to indicate the @i{radix}
  3556. in which it is printing a @i{rational} number. The radix specifier
  3557. is always printed using lowercase letters. If @b{*print-base*}
  3558. is @t{2}, @t{8}, or @t{16}, then the radix specifier used is @t{#b},
  3559. @t{#o}, or @t{#x}, respectively. For @i{integers}, base ten is
  3560. indicated by a trailing decimal point instead of a leading radix
  3561. specifier; for @i{ratios}, @t{#10r} is used.
  3562. @subsubheading Examples::
  3563. @example
  3564. (let ((*print-base* 24.) (*print-radix* t))
  3565. (print 23.))
  3566. @t{ |> } #24rN
  3567. @result{} 23
  3568. (setq *print-base* 10) @result{} 10
  3569. (setq *print-radix* nil) @result{} NIL
  3570. (dotimes (i 35)
  3571. (let ((*print-base* (+ i 2))) ;print the decimal number 40
  3572. (write 40) ;in each base from 2 to 36
  3573. (if (zerop (mod i 10)) (terpri) (format t " "))))
  3574. @t{ |> } 101000
  3575. @t{ |> } 1111 220 130 104 55 50 44 40 37 34
  3576. @t{ |> } 31 2C 2A 28 26 24 22 20 1J 1I
  3577. @t{ |> } 1H 1G 1F 1E 1D 1C 1B 1A 19 18
  3578. @t{ |> } 17 16 15 14
  3579. @result{} NIL
  3580. (dolist (pb '(2 3 8 10 16))
  3581. (let ((*print-radix* t) ;print the integer 10 and
  3582. (*print-base* pb)) ;the ratio 1/10 in bases 2,
  3583. (format t "~&~S ~S~
  3584. @t{ |> } #b1010 #b1/1010
  3585. @t{ |> } #3r101 #3r1/101
  3586. @t{ |> } #o12 #o1/12
  3587. @t{ |> } 10. #10r1/10
  3588. @t{ |> } #xA #x1/A
  3589. @result{} NIL
  3590. @end example
  3591. @subsubheading Affected By::
  3592. Might be @i{bound} by @b{format}, and @b{write}, @b{write-to-string}.
  3593. @subsubheading See Also::
  3594. @ref{format}
  3595. ,
  3596. @ref{write; prin1; print; pprint; princ}
  3597. ,
  3598. @ref{write-to-string; prin1-to-string; princ-to-string}
  3599. @node *print-case*, *print-circle*, *print-base*, Printer Dictionary
  3600. @subsection *print-case* [Variable]
  3601. @subsubheading Value Type::
  3602. One of the @i{symbols} @t{:upcase}, @t{:downcase}, or @t{:capitalize}.
  3603. @subsubheading Initial Value::
  3604. The @i{symbol} @t{:upcase}.
  3605. @subsubheading Description::
  3606. The @i{value} of @b{*print-case*} controls the case (upper, lower, or mixed) in
  3607. which to print any uppercase characters in the names of @i{symbols}
  3608. when vertical-bar syntax is not used.
  3609. @b{*print-case*} has an effect at all times when the @i{value} of @b{*print-escape*}
  3610. is @i{false}. @b{*print-case*} also has an effect when
  3611. the @i{value} of @b{*print-escape*} is @i{true} unless inside an escape context
  3612. (@i{i.e.}, unless between @i{vertical-bars} or after a @i{slash}).
  3613. @subsubheading Examples::
  3614. @example
  3615. (defun test-print-case ()
  3616. (dolist (*print-case* '(:upcase :downcase :capitalize))
  3617. (format t "~&~S ~S~
  3618. @result{} TEST-PC
  3619. ;; Although the choice of which characters to escape is specified by
  3620. ;; *PRINT-CASE*, the choice of how to escape those characters
  3621. ;; (i.e., whether single escapes or multiple escapes are used)
  3622. ;; is implementation-dependent. The examples here show two of the
  3623. ;; many valid ways in which escaping might appear.
  3624. (test-print-case) ;Implementation A
  3625. @t{ |> } THIS-AND-THAT |And-something-elSE|
  3626. @t{ |> } this-and-that a\n\d-\s\o\m\e\t\h\i\n\g-\e\lse
  3627. @t{ |> } This-And-That A\n\d-\s\o\m\e\t\h\i\n\g-\e\lse
  3628. @result{} NIL
  3629. (test-print-case) ;Implementation B
  3630. @t{ |> } THIS-AND-THAT |And-something-elSE|
  3631. @t{ |> } this-and-that a|nd-something-el|se
  3632. @t{ |> } This-And-That A|nd-something-el|se
  3633. @result{} NIL
  3634. @end example
  3635. @subsubheading See Also::
  3636. @ref{write; prin1; print; pprint; princ}
  3637. @subsubheading Notes::
  3638. @b{read} normally converts lowercase characters appearing
  3639. in @i{symbols} to corresponding uppercase characters,
  3640. so that internally print names normally contain only uppercase characters.
  3641. If @b{*print-escape*} is @i{true},
  3642. lowercase characters in the @i{name} of a @i{symbol}
  3643. are always printed in lowercase, and
  3644. are preceded by a single escape character
  3645. or enclosed by multiple escape characters;
  3646. uppercase characters in the @i{name} of a @i{symbol}
  3647. are printed in upper case, in lower case, or in mixed case
  3648. so as to capitalize words, according to the value of
  3649. @b{*print-case*}. The convention for what constitutes
  3650. a ``word'' is the same as for @b{string-capitalize}.
  3651. @node *print-circle*, *print-escape*, *print-case*, Printer Dictionary
  3652. @subsection *print-circle* [Variable]
  3653. @subsubheading Value Type::
  3654. a @i{generalized boolean}.
  3655. @subsubheading Initial Value::
  3656. @i{false}.
  3657. @subsubheading Description::
  3658. Controls the attempt to detect circularity and sharing in an @i{object}
  3659. being printed.
  3660. If @i{false},
  3661. the printing process merely proceeds by recursive descent without attempting
  3662. to detect circularity and sharing.
  3663. If @i{true},
  3664. the printer will endeavor to detect cycles and sharing
  3665. in the structure to be printed,
  3666. and to use @t{#@i{n}=} and @t{#@i{n}#}
  3667. syntax to indicate the circularities or shared components.
  3668. If @i{true}, a user-defined
  3669. @b{print-object} @i{method}
  3670. can print
  3671. @i{objects} to the supplied @i{stream} using @b{write}, @b{prin1},
  3672. @b{princ}, or @b{format} and expect circularities and sharing
  3673. to be detected and printed using the @t{#@i{n}#} syntax.
  3674. If a user-defined
  3675. @b{print-object} @i{method}
  3676. prints to a @i{stream} other than the one
  3677. that was supplied, then circularity detection starts over for that @i{stream}.
  3678. Note that implementations should not use @t{#@i{n}#} notation
  3679. when the @i{Lisp reader} would automatically assure sharing without it
  3680. (@i{e.g.}, as happens with @i{interned} @i{symbols}).
  3681. @subsubheading Examples::
  3682. @example
  3683. (let ((a (list 1 2 3)))
  3684. (setf (cdddr a) a)
  3685. (let ((*print-circle* t))
  3686. (write a)
  3687. :done))
  3688. @t{ |> } #1=(1 2 3 . #1#)
  3689. @result{} :DONE
  3690. @end example
  3691. @subsubheading See Also::
  3692. @ref{write; prin1; print; pprint; princ}
  3693. @subsubheading Notes::
  3694. An attempt to print a circular structure with @b{*print-circle*}
  3695. set to @b{nil} may lead to looping behavior and failure to terminate.
  3696. @node *print-escape*, *print-gensym*, *print-circle*, Printer Dictionary
  3697. @subsection *print-escape* [Variable]
  3698. @subsubheading Value Type::
  3699. a @i{generalized boolean}.
  3700. @subsubheading Initial Value::
  3701. @i{true}.
  3702. @subsubheading Description::
  3703. If @i{false},
  3704. escape characters and @i{package prefixes} are not output
  3705. when an expression is printed.
  3706. If @i{true}, an attempt is made to print an @i{expression}
  3707. in such a way that it can be read again to produce an @b{equal} @i{expression}.
  3708. (This is only a guideline; not a requirement. See @b{*print-readably*}.)
  3709. For more specific details of how the @i{value} of @b{*print-escape*}
  3710. affects the printing of certain @i{types},
  3711. see @ref{Default Print-Object Methods}.
  3712. @subsubheading Examples::
  3713. @example
  3714. (let ((*print-escape* t)) (write #\a))
  3715. @t{ |> } #\a
  3716. @result{} #\a
  3717. (let ((*print-escape* nil)) (write #\a))
  3718. @t{ |> } a
  3719. @result{} #\a
  3720. @end example
  3721. @subsubheading Affected By::
  3722. @b{princ}, @b{prin1}, @b{format}
  3723. @subsubheading See Also::
  3724. @ref{write; prin1; print; pprint; princ}
  3725. ,
  3726. @ref{readtable-case}
  3727. @subsubheading Notes::
  3728. @b{princ} effectively binds @b{*print-escape*} to @i{false}.
  3729. @b{prin1} effectively binds @b{*print-escape*} to @i{true}.
  3730. @node *print-gensym*, *print-level*, *print-escape*, Printer Dictionary
  3731. @subsection *print-gensym* [Variable]
  3732. @subsubheading Value Type::
  3733. a @i{generalized boolean}.
  3734. @subsubheading Initial Value::
  3735. @i{true}.
  3736. @subsubheading Description::
  3737. Controls whether the prefix ``@t{#:}'' is printed before
  3738. @i{apparently uninterned} @i{symbols}.
  3739. The prefix is printed before such @i{symbols}
  3740. if and only if the @i{value} of @b{*print-gensym*} is @i{true}.
  3741. @subsubheading Examples::
  3742. @example
  3743. (let ((*print-gensym* nil))
  3744. (print (gensym)))
  3745. @t{ |> } G6040
  3746. @result{} #:G6040
  3747. @end example
  3748. @subsubheading See Also::
  3749. @ref{write; prin1; print; pprint; princ}
  3750. , @b{*print-escape*}
  3751. @node *print-level*, *print-lines*, *print-gensym*, Printer Dictionary
  3752. @subsection *print-level*, *print-length* [Variable]
  3753. @subsubheading Value Type::
  3754. a non-negative @i{integer}, or @b{nil}.
  3755. @subsubheading Initial Value::
  3756. @b{nil}.
  3757. @subsubheading Description::
  3758. @b{*print-level*} controls how many levels deep a nested @i{object} will print.
  3759. If it is @i{false}, then no control is exercised.
  3760. Otherwise, it is an @i{integer} indicating the maximum level to be printed.
  3761. An @i{object} to be printed is at level @t{0};
  3762. its components (as of a @i{list} or @i{vector}) are at level @t{1};
  3763. and so on.
  3764. If an @i{object} to be recursively printed has components
  3765. and is at a level equal to or greater than the @i{value} of @b{*print-level*},
  3766. then the @i{object} is printed as ``@t{#}''.
  3767. @b{*print-length*} controls how many elements at a given level are printed.
  3768. If it is @i{false}, there is no limit to the number of components printed.
  3769. Otherwise, it is an @i{integer} indicating the maximum number of @i{elements}
  3770. of an @i{object} to be printed. If exceeded, the printer will print
  3771. ``@t{...}'' in place of the other @i{elements}. In the case of a @i{dotted list},
  3772. if the @i{list} contains exactly as many @i{elements} as the @i{value} of @b{*print-length*},
  3773. the terminating @i{atom} is printed rather than printing ``@t{...}''
  3774. @b{*print-level*} and @b{*print-length*} affect the printing
  3775. of an any @i{object} printed with a list-like syntax. They do not affect
  3776. the printing of @i{symbols}, @i{strings}, and @i{bit vectors}.
  3777. @subsubheading Examples::
  3778. @example
  3779. (setq a '(1 (2 (3 (4 (5 (6))))))) @result{} (1 (2 (3 (4 (5 (6))))))
  3780. (dotimes (i 8)
  3781. (let ((*print-level* i))
  3782. (format t "~&~D -- ~S~
  3783. @t{ |> } 0 -- #
  3784. @t{ |> } 1 -- (1 #)
  3785. @t{ |> } 2 -- (1 (2 #))
  3786. @t{ |> } 3 -- (1 (2 (3 #)))
  3787. @t{ |> } 4 -- (1 (2 (3 (4 #))))
  3788. @t{ |> } 5 -- (1 (2 (3 (4 (5 #)))))
  3789. @t{ |> } 6 -- (1 (2 (3 (4 (5 (6))))))
  3790. @t{ |> } 7 -- (1 (2 (3 (4 (5 (6))))))
  3791. @result{} NIL
  3792. (setq a '(1 2 3 4 5 6)) @result{} (1 2 3 4 5 6)
  3793. (dotimes (i 7)
  3794. (let ((*print-length* i))
  3795. (format t "~&~D -- ~S~
  3796. @t{ |> } 0 -- (...)
  3797. @t{ |> } 1 -- (1 ...)
  3798. @t{ |> } 2 -- (1 2 ...)
  3799. @t{ |> } 3 -- (1 2 3 ...)
  3800. @t{ |> } 4 -- (1 2 3 4 ...)
  3801. @t{ |> } 5 -- (1 2 3 4 5 6)
  3802. @t{ |> } 6 -- (1 2 3 4 5 6)
  3803. @result{} NIL
  3804. (dolist (level-length '((0 1) (1 1) (1 2) (1 3) (1 4)
  3805. (2 1) (2 2) (2 3) (3 2) (3 3) (3 4)))
  3806. (let ((*print-level* (first level-length))
  3807. (*print-length* (second level-length)))
  3808. (format t "~&~D ~D -- ~S~
  3809. *print-level* *print-length*
  3810. '(if (member x y) (+ (car x) 3) '(foo . #(a b c d "Baz"))))))
  3811. @t{ |> } 0 1 -- #
  3812. @t{ |> } 1 1 -- (IF ...)
  3813. @t{ |> } 1 2 -- (IF # ...)
  3814. @t{ |> } 1 3 -- (IF # # ...)
  3815. @t{ |> } 1 4 -- (IF # # #)
  3816. @t{ |> } 2 1 -- (IF ...)
  3817. @t{ |> } 2 2 -- (IF (MEMBER X ...) ...)
  3818. @t{ |> } 2 3 -- (IF (MEMBER X Y) (+ # 3) ...)
  3819. @t{ |> } 3 2 -- (IF (MEMBER X ...) ...)
  3820. @t{ |> } 3 3 -- (IF (MEMBER X Y) (+ (CAR X) 3) ...)
  3821. @t{ |> } 3 4 -- (IF (MEMBER X Y) (+ (CAR X) 3) '(FOO . #(A B C D ...)))
  3822. @result{} NIL
  3823. @end example
  3824. @subsubheading See Also::
  3825. @ref{write; prin1; print; pprint; princ}
  3826. @node *print-lines*, *print-miser-width*, *print-level*, Printer Dictionary
  3827. @subsection *print-lines* [Variable]
  3828. @subsubheading Value Type::
  3829. a non-negative @i{integer}, or @b{nil}.
  3830. @subsubheading Initial Value::
  3831. @b{nil}.
  3832. @subsubheading Description::
  3833. When the @i{value} of @b{*print-lines*} is other than @b{nil},
  3834. it is a limit on the number of output lines produced when something is pretty
  3835. printed. If an attempt is made to go beyond that many lines,
  3836. ``@t{..}'' is printed at the end of the last line followed by all of the
  3837. suffixes (closing delimiters) that are pending to be printed.
  3838. @subsubheading Examples::
  3839. @example
  3840. (let ((*print-right-margin* 25) (*print-lines* 3))
  3841. (pprint '(progn (setq a 1 b 2 c 3 d 4))))
  3842. @t{ |> } (PROGN (SETQ A 1
  3843. @t{ |> } B 2
  3844. @t{ |> } C 3 ..))
  3845. @result{} <@i{no @i{values}}>
  3846. @end example
  3847. @subsubheading Notes::
  3848. The ``@t{..}'' notation is intentionally different than
  3849. the ``@t{...}'' notation used for level abbreviation, so that the two
  3850. different situations can be visually distinguished.
  3851. This notation is used to increase the likelihood that the @i{Lisp reader}
  3852. will signal an error if an attempt is later made to read the abbreviated output.
  3853. Note however that if the truncation occurs in a @i{string},
  3854. as in @t{"This string has been trunc.."}, the problem situation cannot be
  3855. detected later and no such error will be signaled.
  3856. @node *print-miser-width*, *print-pprint-dispatch*, *print-lines*, Printer Dictionary
  3857. @subsection *print-miser-width* [Variable]
  3858. @subsubheading Value Type::
  3859. a non-negative @i{integer}, or @b{nil}.
  3860. @subsubheading Initial Value::
  3861. @i{implementation-dependent}
  3862. @subsubheading Description::
  3863. If it is not @b{nil}, the @i{pretty printer} switches to a compact
  3864. style of output (called miser style) whenever the width available for
  3865. printing a substructure is less than or equal to this many @i{ems}.
  3866. @node *print-pprint-dispatch*, *print-pretty*, *print-miser-width*, Printer Dictionary
  3867. @subsection *print-pprint-dispatch* [Variable]
  3868. @subsubheading Value Type::
  3869. a @i{pprint dispatch table}.
  3870. @subsubheading Initial Value::
  3871. @i{implementation-dependent}, but the initial entries all use a
  3872. special class of priorities that have the property that they are less
  3873. than every priority that can be specified using @b{set-pprint-dispatch},
  3874. so that the initial contents of any entry can be overridden.
  3875. @subsubheading Description::
  3876. The @i{pprint dispatch table} which currently controls the @i{pretty printer}.
  3877. @subsubheading See Also::
  3878. @b{*print-pretty*},
  3879. @ref{Pretty Print Dispatch Tables}
  3880. @subsubheading Notes::
  3881. The intent is that the initial @i{value} of this @i{variable} should
  3882. cause `traditional' @i{pretty printing} of @i{code}.
  3883. In general, however, you can put a value in @b{*print-pprint-dispatch*}
  3884. that makes pretty-printed output look exactly like non-pretty-printed output.
  3885. Setting @b{*print-pretty*} to @i{true}
  3886. just causes the functions contained in the @i{current pprint dispatch table}
  3887. to have priority over normal @b{print-object} methods;
  3888. it has no magic way of enforcing that those functions actually produce pretty
  3889. output. For details, see @ref{Pretty Print Dispatch Tables}.
  3890. @node *print-pretty*, *print-readably*, *print-pprint-dispatch*, Printer Dictionary
  3891. @subsection *print-pretty* [Variable]
  3892. @subsubheading Value Type::
  3893. a @i{generalized boolean}.
  3894. @subsubheading Initial Value::
  3895. @i{implementation-dependent}.
  3896. @subsubheading Description::
  3897. Controls whether the @i{Lisp printer} calls the @i{pretty printer}.
  3898. If it is @i{false},
  3899. the @i{pretty printer} is not used and
  3900. a minimum
  3901. of @i{whitespace}_1
  3902. is output when printing an expression.
  3903. If it is @i{true},
  3904. the @i{pretty printer} is used, and the @i{Lisp printer} will endeavor
  3905. to insert extra @i{whitespace}_1 where appropriate to make @i{expressions}
  3906. more readable.
  3907. @b{*print-pretty*} has an effect even when the @i{value} of @b{*print-escape*}
  3908. is @i{false}.
  3909. @subsubheading Examples::
  3910. @example
  3911. (setq *print-pretty* 'nil) @result{} NIL
  3912. (progn (write '(let ((a 1) (b 2) (c 3)) (+ a b c))) nil)
  3913. @t{ |> } (LET ((A 1) (B 2) (C 3)) (+ A B C))
  3914. @result{} NIL
  3915. (let ((*print-pretty* t))
  3916. (progn (write '(let ((a 1) (b 2) (c 3)) (+ a b c))) nil))
  3917. @t{ |> } (LET ((A 1)
  3918. @t{ |> } (B 2)
  3919. @t{ |> } (C 3))
  3920. @t{ |> } (+ A B C))
  3921. @result{} NIL
  3922. ;; Note that the first two expressions printed by this next form
  3923. ;; differ from the second two only in whether escape characters are printed.
  3924. ;; In all four cases, extra whitespace is inserted by the pretty printer.
  3925. (flet ((test (x)
  3926. (let ((*print-pretty* t))
  3927. (print x)
  3928. (format t "~
  3929. (terpri) (princ x) (princ " ")
  3930. (format t "~
  3931. (test '#'(lambda () (list "a" #@b{'c} #'d))))
  3932. @t{ |> } #'(LAMBDA ()
  3933. @t{ |> } (LIST "a" #@b{'C} #'D))
  3934. @t{ |> } #'(LAMBDA ()
  3935. @t{ |> } (LIST "a" #@b{'C} #'D))
  3936. @t{ |> } #'(LAMBDA ()
  3937. @t{ |> } (LIST a b 'C #'D))
  3938. @t{ |> } #'(LAMBDA ()
  3939. @t{ |> } (LIST a b 'C #'D))
  3940. @result{} NIL
  3941. @end example
  3942. @subsubheading See Also::
  3943. @ref{write; prin1; print; pprint; princ}
  3944. @node *print-readably*, *print-right-margin*, *print-pretty*, Printer Dictionary
  3945. @subsection *print-readably* [Variable]
  3946. @subsubheading Value Type::
  3947. a @i{generalized boolean}.
  3948. @subsubheading Initial Value::
  3949. @i{false}.
  3950. @subsubheading Description::
  3951. If @b{*print-readably*} is @i{true},
  3952. some special rules for printing @i{objects} go into effect.
  3953. Specifically, printing any @i{object} O_1 produces a printed
  3954. representation that, when seen by the @i{Lisp reader}
  3955. while the @i{standard readtable} is in effect,
  3956. will produce
  3957. an @i{object} O_2 that is @i{similar} to O_1.
  3958. The printed representation produced might or might not be the same as
  3959. the printed representation produced when @b{*print-readably*} is @i{false}.
  3960. If printing an @i{object} @i{readably} is not possible,
  3961. an error of @i{type} @b{print-not-readable} is signaled rather than
  3962. using a syntax (@i{e.g.}, the ``@t{#<}'' syntax) that would not be readable by
  3963. the same @i{implementation}.
  3964. If the @i{value} of some other @i{printer control variable} is such
  3965. that these requirements would be violated, the @i{value} of that other
  3966. @i{variable} is ignored.
  3967. Specifically, if @b{*print-readably*} is @i{true},
  3968. printing proceeds as if
  3969. @b{*print-escape*},
  3970. @b{*print-array*},
  3971. and @b{*print-gensym*} were also @i{true},
  3972. and as if
  3973. @b{*print-length*},
  3974. @b{*print-level*},
  3975. and @b{*print-lines*} were @i{false}.
  3976. If @b{*print-readably*} is @i{false},
  3977. the normal rules for printing and the normal interpretations
  3978. of other @i{printer control variables} are in effect.
  3979. Individual @i{methods} for @b{print-object}, including user-defined
  3980. @i{methods}, are responsible for implementing these requirements.
  3981. If @b{*read-eval*} is @i{false} and @b{*print-readably*} is @i{true},
  3982. any such method that would output a reference to the ``@t{#.}'' @i{reader macro}
  3983. will either output something else or will signal an error (as described above).
  3984. @subsubheading Examples::
  3985. @example
  3986. (let ((x (list "a" '\a (gensym) '((a (b (c))) d e f g)))
  3987. (*print-escape* nil)
  3988. (*print-gensym* nil)
  3989. (*print-level* 3)
  3990. (*print-length* 3))
  3991. (write x)
  3992. (let ((*print-readably* t))
  3993. (terpri)
  3994. (write x)
  3995. :done))
  3996. @t{ |> } (a a G4581 ((A #) D E ...))
  3997. @t{ |> } ("a" |a| #:G4581 ((A (B (C))) D E F G))
  3998. @result{} :DONE
  3999. ;; This is setup code is shared between the examples
  4000. ;; of three hypothetical implementations which follow.
  4001. (setq table (make-hash-table)) @result{} #<HASH-TABLE EQL 0/120 32005763>
  4002. (setf (gethash table 1) 'one) @result{} ONE
  4003. (setf (gethash table 2) 'two) @result{} TWO
  4004. ;; Implementation A
  4005. (let ((*print-readably* t)) (print table))
  4006. Error: Can't print #<HASH-TABLE EQL 0/120 32005763> readably.
  4007. ;; Implementation B
  4008. ;; No standardized #S notation for hash tables is defined,
  4009. ;; but there might be an implementation-defined notation.
  4010. (let ((*print-readably* t)) (print table))
  4011. @t{ |> } #S(HASH-TABLE :TEST EQL :SIZE 120 :CONTENTS (1 ONE 2 TWO))
  4012. @result{} #<HASH-TABLE EQL 0/120 32005763>
  4013. ;; Implementation C
  4014. ;; Note that #. notation can only be used if *READ-EVAL* is true.
  4015. ;; If *READ-EVAL* were false, this same implementation might have to
  4016. ;; signal an error unless it had yet another printing strategy to fall
  4017. ;; back on.
  4018. (let ((*print-readably* t)) (print table))
  4019. @t{ |> } #.(LET ((HASH-TABLE (MAKE-HASH-TABLE)))
  4020. @t{ |> } (SETF (GETHASH 1 HASH-TABLE) ONE)
  4021. @t{ |> } (SETF (GETHASH 2 HASH-TABLE) TWO)
  4022. @t{ |> } HASH-TABLE)
  4023. @result{} #<HASH-TABLE EQL 0/120 32005763>
  4024. @end example
  4025. @subsubheading See Also::
  4026. @ref{write; prin1; print; pprint; princ}
  4027. ,
  4028. @ref{print-unreadable-object}
  4029. @subsubheading Notes::
  4030. The rules for ``@i{similarity}'' imply that
  4031. @t{#A} or @t{#(}
  4032. syntax cannot be used for @i{arrays} of @i{element type}
  4033. other than @b{t}.
  4034. An implementation will have to use another syntax
  4035. or signal an error of @i{type} @b{print-not-readable}.
  4036. @node *print-right-margin*, print-not-readable, *print-readably*, Printer Dictionary
  4037. @subsection *print-right-margin* [Variable]
  4038. @subsubheading Value Type::
  4039. a non-negative @i{integer}, or @b{nil}.
  4040. @subsubheading Initial Value::
  4041. @b{nil}.
  4042. @subsubheading Description::
  4043. If it is @i{non-nil}, it specifies the right margin (as @i{integer}
  4044. number of @i{ems}) to use when the @i{pretty printer} is making
  4045. layout decisions.
  4046. If it is @b{nil}, the right margin is taken to be the maximum line length
  4047. such that output can be displayed without wraparound or truncation.
  4048. If this cannot be determined, an @i{implementation-dependent} value is used.
  4049. @subsubheading Notes::
  4050. This measure is in units of @i{ems} in order to be compatible with
  4051. @i{implementation-defined} variable-width fonts while still not
  4052. requiring the language to provide support for fonts.
  4053. @node print-not-readable, print-not-readable-object, *print-right-margin*, Printer Dictionary
  4054. @subsection print-not-readable [Condition Type]
  4055. @subsubheading Class Precedence List::
  4056. @b{print-not-readable},
  4057. @b{error},
  4058. @b{serious-condition},
  4059. @b{condition},
  4060. @b{t}
  4061. @subsubheading Description::
  4062. The @i{type} @b{print-not-readable} consists of error conditions that occur during
  4063. output while @b{*print-readably*} is @i{true}, as a result of attempting
  4064. to write a printed representation with the @i{Lisp printer}
  4065. that would not be correctly read back with the @i{Lisp reader}.
  4066. The object which could not be printed is initialized by
  4067. the @t{:object} initialization argument to @b{make-condition}, and is @i{accessed} by
  4068. the @i{function} @b{print-not-readable-object}.
  4069. @subsubheading See Also::
  4070. @ref{print-not-readable-object}
  4071. @node print-not-readable-object, format, print-not-readable, Printer Dictionary
  4072. @subsection print-not-readable-object [Function]
  4073. @code{print-not-readable-object} @i{condition} @result{} @i{object}
  4074. @subsubheading Arguments and Values::
  4075. @i{condition}---a @i{condition} of @i{type} @b{print-not-readable}.
  4076. @i{object}---an @i{object}.
  4077. @subsubheading Description::
  4078. Returns the @i{object} that could not be printed readably
  4079. in the situation represented by @i{condition}.
  4080. @subsubheading See Also::
  4081. @b{print-not-readable},
  4082. @ref{Conditions}
  4083. @node format, , print-not-readable-object, Printer Dictionary
  4084. @subsection format [Function]
  4085. @code{format} @i{destination control-string {&rest} args} @result{} @i{result}
  4086. @subsubheading Arguments and Values::
  4087. @i{destination}---@b{nil},
  4088. @b{t},
  4089. a @i{stream},
  4090. or a @i{string} with a @i{fill pointer}.
  4091. @i{control-string}---a @i{format control}.
  4092. @i{args}---@i{format arguments} for @i{control-string}.
  4093. @i{result}---if @i{destination} is @i{non-nil}, then @b{nil};
  4094. otherwise, a @i{string}.
  4095. @subsubheading Description::
  4096. @b{format} produces formatted output by outputting the characters
  4097. of @i{control-string} and observing that a @i{tilde}
  4098. introduces a directive. The character after the tilde, possibly preceded
  4099. by prefix parameters and modifiers, specifies what kind of formatting
  4100. is desired. Most directives use one or more elements of @i{args} to
  4101. create their output.
  4102. If @i{destination} is a @i{string}, a @i{stream}, or @b{t},
  4103. then the @i{result} is @b{nil}. Otherwise, the @i{result} is
  4104. a @i{string} containing the `output.'
  4105. @b{format} is useful for producing nicely formatted text, producing
  4106. good-looking messages, and so on. @b{format} can generate and return
  4107. a @i{string} or output to @i{destination}.
  4108. For details on how the @i{control-string} is interpreted,
  4109. see @ref{Formatted Output}.
  4110. @subsubheading Affected By::
  4111. @b{*standard-output*},
  4112. @b{*print-escape*},
  4113. @b{*print-radix*},
  4114. @b{*print-base*},
  4115. @b{*print-circle*},
  4116. @b{*print-pretty*},
  4117. @b{*print-level*},
  4118. @b{*print-length*},
  4119. @b{*print-case*},
  4120. @b{*print-gensym*},
  4121. @b{*print-array*}.
  4122. @subsubheading Exceptional Situations::
  4123. If @i{destination} is a @i{string} with a @i{fill pointer},
  4124. the consequences are undefined if destructive modifications are performed
  4125. directly on the @i{string} during the @i{dynamic extent} of the call.
  4126. @subsubheading See Also::
  4127. @ref{write; prin1; print; pprint; princ}
  4128. ,
  4129. @ref{Documentation of Implementation-Defined Scripts}
  4130. @c end of including dict-printer
  4131. @c %**end of chapter