tex.web 1008 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010
  1. % This program is copyright (C) 1982 by D. E. Knuth; all rights are reserved.
  2. % Unlimited copying and redistribution of this file are permitted as long
  3. % as this file is not modified. Modifications are permitted, but only if
  4. % the resulting file is not named tex.web. (The WEB system provides
  5. % for alterations via an auxiliary file; the master file should stay intact.)
  6. % See Appendix H of the WEB manual for hints on how to install this program.
  7. % And see Appendix A of the TRIP manual for details about how to validate it.
  8. % TeX is a trademark of the American Mathematical Society.
  9. % METAFONT is a trademark of Addison-Wesley Publishing Company.
  10. % Version 0 was released in September 1982 after it passed a variety of tests.
  11. % Version 1 was released in November 1983 after thorough testing.
  12. % Version 1.1 fixed ``disappearing font identifiers'' et alia (July 1984).
  13. % Version 1.2 allowed `0' in response to an error, et alia (October 1984).
  14. % Version 1.3 made memory allocation more flexible and local (November 1984).
  15. % Version 1.4 fixed accents right after line breaks, et alia (April 1985).
  16. % Version 1.5 fixed \the\toks after other expansion in \edefs (August 1985).
  17. % Version 2.0 (almost identical to 1.5) corresponds to "Volume B" (April 1986).
  18. % Version 2.1 corrected anomalies in discretionary breaks (January 1987).
  19. % Version 2.2 corrected "(Please type...)" with null \endlinechar (April 1987).
  20. % Version 2.3 avoided incomplete page in premature termination (August 1987).
  21. % Version 2.4 fixed \noaligned rules in indented displays (August 1987).
  22. % Version 2.5 saved cur_order when expanding tokens (September 1987).
  23. % Version 2.6 added 10sp slop when shipping leaders (November 1987).
  24. % Version 2.7 improved rounding of negative-width characters (November 1987).
  25. % Version 2.8 fixed weird bug if no \patterns are used (December 1987).
  26. % Version 2.9 made \csname\endcsname's "relax" local (December 1987).
  27. % Version 2.91 fixed \outer\def\a0{}\a\a bug (April 1988).
  28. % Version 2.92 fixed \patterns, also file names with complex macros (May 1988).
  29. % Version 2.93 fixed negative halving in allocator when mem_min<0 (June 1988).
  30. % Version 2.94 kept open_log_file from calling fatal_error (November 1988).
  31. % Version 2.95 solved that problem a better way (December 1988).
  32. % Version 2.96 corrected bug in "Infinite shrinkage" recovery (January 1989).
  33. % Version 2.97 corrected blunder in creating 2.95 (February 1989).
  34. % Version 2.98 omitted save_for_after at outer level (March 1989).
  35. % Version 2.99 caught $$\begingroup\halign..$$ (June 1989).
  36. % Version 2.991 caught .5\ifdim.6... (June 1989).
  37. % Version 2.992 introduced major changes for 8-bit extensions (September 1989).
  38. % Version 2.993 fixed a save_stack synchronization bug et alia (December 1989).
  39. % Version 3.0 fixed unusual displays; was more \output robust (March 1990).
  40. % Version 3.1 fixed nullfont, disabled \write{\the\prevgraf} (September 1990).
  41. % Version 3.14 fixed unprintable font names and corrected typos (March 1991).
  42. % Version 3.141 more of same; reconstituted ligatures better (March 1992).
  43. % Version 3.1415 preserved nonexplicit kerns, tidied up (February 1993).
  44. % Version 3.14159 allowed fontmemsize to change; bulletproofing (March 1995).
  45. % Version 3.141592 fixed \xleaders, glueset, weird alignments (December 2002).
  46. % Version 3.1415926 was a general cleanup with minor fixes (February 2008).
  47. % Version 3.14159265 was similar (January 2014).
  48. % Version 3.141592653 was similar but more extensive (January 2021).
  49. % A reward of $327.68 will be paid to the first finder of any remaining bug.
  50. % Although considerable effort has been expended to make the TeX program
  51. % correct and reliable, no warranty is implied; the author disclaims any
  52. % obligation or liability for damages, including but not limited to
  53. % special, indirect, or consequential damages arising out of or in
  54. % connection with the use or performance of this software. This work has
  55. % been a ``labor of love'' and the author hopes that users enjoy it.
  56. % Here is TeX material that gets inserted after \input webmac
  57. \def\hang{\hangindent 3em\noindent\ignorespaces}
  58. \def\hangg#1 {\hang\hbox{#1 }}
  59. \def\textindent#1{\hangindent2.5em\noindent\hbox to2.5em{\hss#1 }\ignorespaces}
  60. \font\ninerm=cmr9
  61. \let\mc=\ninerm % medium caps for names like SAIL
  62. \def\PASCAL{Pascal}
  63. \def\ph{\hbox{Pascal-H}}
  64. \def\pct!{{\char`\%}} % percent sign in ordinary text
  65. \font\logo=logo10 % font used for the METAFONT logo
  66. \def\MF{{\logo META}\-{\logo FONT}}
  67. \def\<#1>{$\langle#1\rangle$}
  68. \def\section{\mathhexbox278}
  69. \def\(#1){} % this is used to make section names sort themselves better
  70. \def\9#1{} % this is used for sort keys in the index via @@:sort key}{entry@@>
  71. \outer\def\N#1. \[#2]#3.{\MN#1.\vfil\eject % begin starred section
  72. \def\rhead{PART #2:\uppercase{#3}} % define running headline
  73. \message{*\modno} % progress report
  74. \edef\next{\write\cont{\Z{\?#2]#3}{\modno}{\the\pageno}}}\next
  75. \ifon\startsection{\bf\ignorespaces#3.\quad}\ignorespaces}
  76. \let\?=\relax % we want to be able to \write a \?
  77. \def\title{\TeX82}
  78. \def\topofcontents{\hsize 5.5in
  79. \vglue 0pt plus 1fil minus 1.5in
  80. \def\?##1]{\hbox to 1in{\hfil##1.\ }}
  81. }
  82. \def\botofcontents{\vskip 0pt plus 1fil minus 1.5in}
  83. \pageno=3
  84. \def\glob{13} % this should be the section number of "<Global...>"
  85. \def\gglob{20, 26} % this should be the next two sections of "<Global...>"
  86. @* \[1] Introduction.
  87. This is \TeX, a document compiler intended to produce typesetting of high
  88. quality.
  89. The \PASCAL\ program that follows is the definition of \TeX82, a standard
  90. @:PASCAL}{\PASCAL@>
  91. @!@:TeX82}{\TeX82@>
  92. version of \TeX\ that is designed to be highly portable so that identical output
  93. will be obtainable on a great variety of computers.
  94. The main purpose of the following program is to explain the algorithms of \TeX\
  95. as clearly as possible. As a result, the program will not necessarily be very
  96. efficient when a particular \PASCAL\ compiler has translated it into a
  97. particular machine language. However, the program has been written so that it
  98. can be tuned to run efficiently in a wide variety of operating environments
  99. by making comparatively few changes. Such flexibility is possible because
  100. the documentation that follows is written in the \.{WEB} language, which is
  101. at a higher level than \PASCAL; the preprocessing step that converts \.{WEB}
  102. to \PASCAL\ is able to introduce most of the necessary refinements.
  103. Semi-automatic translation to other languages is also feasible, because the
  104. program below does not make extensive use of features that are peculiar to
  105. \PASCAL.
  106. A large piece of software like \TeX\ has inherent complexity that cannot
  107. be reduced below a certain level of difficulty, although each individual
  108. part is fairly simple by itself. The \.{WEB} language is intended to make
  109. the algorithms as readable as possible, by reflecting the way the
  110. individual program pieces fit together and by providing the
  111. cross-references that connect different parts. Detailed comments about
  112. what is going on, and about why things were done in certain ways, have
  113. been liberally sprinkled throughout the program. These comments explain
  114. features of the implementation, but they rarely attempt to explain the
  115. \TeX\ language itself, since the reader is supposed to be familiar with
  116. {\sl The \TeX book}.
  117. @.WEB@>
  118. @:TeXbook}{\sl The \TeX book@>
  119. @ The present implementation has a long ancestry, beginning in the summer
  120. of~1977, when Michael~F. Plass and Frank~M. Liang designed and coded
  121. a prototype
  122. @^Plass, Michael Frederick@>
  123. @^Liang, Franklin Mark@>
  124. @^Knuth, Donald Ervin@>
  125. based on some specifications that the author had made in May of that year.
  126. This original proto\TeX\ included macro definitions and elementary
  127. manipulations on boxes and glue, but it did not have line-breaking,
  128. page-breaking, mathematical formulas, alignment routines, error recovery,
  129. or the present semantic nest; furthermore,
  130. it used character lists instead of token lists, so that a control sequence
  131. like \.{\\halign} was represented by a list of seven characters. A
  132. complete version of \TeX\ was designed and coded by the author in late
  133. 1977 and early 1978; that program, like its prototype, was written in the
  134. {\mc SAIL} language, for which an excellent debugging system was
  135. available. Preliminary plans to convert the {\mc SAIL} code into a form
  136. somewhat like the present ``web'' were developed by Luis Trabb~Pardo and
  137. @^Trabb Pardo, Luis Isidoro@>
  138. the author at the beginning of 1979, and a complete implementation was
  139. created by Ignacio~A. Zabala in 1979 and 1980. The \TeX82 program, which
  140. @^Zabala Salelles, Ignacio Andr\'es@>
  141. was written by the author during the latter part of 1981 and the early
  142. part of 1982, also incorporates ideas from the 1979 implementation of
  143. @^Guibas, Leonidas Ioannis@>
  144. @^Sedgewick, Robert@>
  145. @^Wyatt, Douglas Kirk@>
  146. \TeX\ in {\mc MESA} that was written by Leonidas Guibas, Robert Sedgewick,
  147. and Douglas Wyatt at the Xerox Palo Alto Research Center. Several hundred
  148. refinements were introduced into \TeX82 based on the experiences gained with
  149. the original implementations, so that essentially every part of the system
  150. has been substantially improved. After the appearance of ``Version 0'' in
  151. September 1982, this program benefited greatly from the comments of
  152. many other people, notably David~R. Fuchs and Howard~W. Trickey.
  153. A final revision in September 1989 extended the input character set to
  154. eight-bit codes and introduced the ability to hyphenate words from
  155. different languages, based on some ideas of Michael~J. Ferguson.
  156. @^Fuchs, David Raymond@>
  157. @^Trickey, Howard Wellington@>
  158. @^Ferguson, Michael John@>
  159. No doubt there still is plenty of room for improvement, but the author
  160. is firmly committed to keeping \TeX82 ``frozen'' from now on; stability
  161. and reliability are to be its main virtues.
  162. On the other hand, the \.{WEB} description can be extended without changing
  163. the core of \TeX82 itself, and the program has been designed so that such
  164. extensions are not extremely difficult to make.
  165. The |banner| string defined here should be changed whenever \TeX\
  166. undergoes any modifications, so that it will be clear which version of
  167. \TeX\ might be the guilty party when a problem arises.
  168. @^extensions to \TeX@>
  169. @^system dependencies@>
  170. If this program is changed, the resulting system should not be called
  171. `\TeX'; the official name `\TeX' by itself is reserved
  172. for software systems that are fully compatible with each other.
  173. A special test suite called the ``\.{TRIP} test'' is available for
  174. helping to determine whether a particular implementation deserves to be
  175. known as `\TeX' [cf.~Stanford Computer Science report CS1027,
  176. November 1984].
  177. @d banner=='This is TeX, Version 3.141592653' {printed when \TeX\ starts}
  178. @ Different \PASCAL s have slightly different conventions, and the present
  179. @!@:PASCAL H}{\ph@>
  180. program expresses \TeX\ in terms of the \PASCAL\ that was
  181. available to the author in 1982. Constructions that apply to
  182. this particular compiler, which we shall call \ph, should help the
  183. reader see how to make an appropriate interface for other systems
  184. if necessary. (\ph\ is Charles Hedrick's modification of a compiler
  185. @^Hedrick, Charles Locke@>
  186. for the DECsystem-10 that was originally developed at the University of
  187. Hamburg; cf.\ {\sl Software---Practice and Experience \bf6} (1976),
  188. 29--42. The \TeX\ program below is intended to be adaptable, without
  189. extensive changes, to most other versions of \PASCAL, so it does not fully
  190. use the admirable features of \ph. Indeed, a conscious effort has been
  191. made here to avoid using several idiosyncratic features of standard
  192. \PASCAL\ itself, so that most of the code can be translated mechanically
  193. into other high-level languages. For example, the `\&{with}' and `\\{new}'
  194. features are not used, nor are pointer types, set types, or enumerated
  195. scalar types; there are no `\&{var}' parameters, except in the case of files;
  196. there are no tag fields on variant records; there are no assignments
  197. |real:=integer|; no procedures are declared local to other procedures.)
  198. The portions of this program that involve system-dependent code, where
  199. changes might be necessary because of differences between \PASCAL\ compilers
  200. and/or differences between
  201. operating systems, can be identified by looking at the sections whose
  202. numbers are listed under `system dependencies' in the index. Furthermore,
  203. the index entries for `dirty \PASCAL' list all places where the restrictions
  204. of \PASCAL\ have not been followed perfectly, for one reason or another.
  205. @!@^system dependencies@>
  206. @!@^dirty \PASCAL@>
  207. Incidentally, \PASCAL's standard |round| function can be problematical,
  208. because it disagrees with the IEEE floating-point standard.
  209. Many implementors have
  210. therefore chosen to substitute their own home-grown rounding procedure.
  211. @ The program begins with a normal \PASCAL\ program heading, whose
  212. components will be filled in later, using the conventions of \.{WEB}.
  213. @.WEB@>
  214. For example, the portion of the program called `\X\glob:Global
  215. variables\X' below will be replaced by a sequence of variable declarations
  216. that starts in $\section\glob$ of this documentation. In this way, we are able
  217. to define each individual global variable when we are prepared to
  218. understand what it means; we do not have to define all of the globals at
  219. once. Cross references in $\section\glob$, where it says ``See also
  220. sections \gglob, \dots,'' also make it possible to look at the set of
  221. all global variables, if desired. Similar remarks apply to the other
  222. portions of the program heading.
  223. Actually the heading shown here is not quite normal: The |program| line
  224. does not mention any |output| file, because \ph\ would ask the \TeX\ user
  225. to specify a file name if |output| were specified here.
  226. @:PASCAL H}{\ph@>
  227. @^system dependencies@>
  228. @d mtype==t@&y@&p@&e {this is a \.{WEB} coding trick:}
  229. @f mtype==type {`\&{mtype}' will be equivalent to `\&{type}'}
  230. @f type==true {but `|type|' will not be treated as a reserved word}
  231. @p @t\4@>@<Compiler directives@>@/
  232. program TEX; {all file names are defined dynamically}
  233. label @<Labels in the outer block@>@/
  234. const @<Constants in the outer block@>@/
  235. mtype @<Types in the outer block@>@/
  236. var @<Global variables@>@/
  237. @#
  238. procedure initialize; {this procedure gets things started properly}
  239. var @<Local variables for initialization@>@/
  240. begin @<Initialize whatever \TeX\ might access@>@;
  241. end;@#
  242. @t\4@>@<Basic printing procedures@>@/
  243. @t\4@>@<Error handling procedures@>@/
  244. @ The overall \TeX\ program begins with the heading just shown, after which
  245. comes a bunch of procedure declarations and function declarations.
  246. Finally we will get to the main program, which begins with the
  247. comment `|start_here|'. If you want to skip down to the
  248. main program now, you can look up `|start_here|' in the index.
  249. But the author suggests that the best way to understand this program
  250. is to follow pretty much the order of \TeX's components as they appear in the
  251. \.{WEB} description you are now reading, since the present ordering is
  252. intended to combine the advantages of the ``bottom up'' and ``top down''
  253. approaches to the problem of understanding a somewhat complicated system.
  254. @ Three labels must be declared in the main program, so we give them
  255. symbolic names.
  256. @d start_of_TEX=1 {go here when \TeX's variables are initialized}
  257. @d end_of_TEX=9998 {go here to close files and terminate gracefully}
  258. @d final_end=9999 {this label marks the ending of the program}
  259. @<Labels in the out...@>=
  260. start_of_TEX@t\hskip-2pt@>, end_of_TEX@t\hskip-2pt@>,@,final_end;
  261. {key control points}
  262. @ Some of the code below is intended to be used only when diagnosing the
  263. strange behavior that sometimes occurs when \TeX\ is being installed or
  264. when system wizards are fooling around with \TeX\ without quite knowing
  265. what they are doing. Such code will not normally be compiled; it is
  266. delimited by the codewords `$|debug|\ldots|gubed|$', with apologies
  267. to people who wish to preserve the purity of English.
  268. Similarly, there is some conditional code delimited by
  269. `$|stat|\ldots|tats|$' that is intended for use when statistics are to be
  270. kept about \TeX's memory usage. The |stat| $\ldots$ |tats| code also
  271. implements diagnostic information for \.{\\tracingparagraphs},
  272. \.{\\tracingpages}, and \.{\\tracingrestores}.
  273. @^debugging@>
  274. @d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging}
  275. @d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging}
  276. @f debug==begin
  277. @f gubed==end
  278. @#
  279. @d stat==@{ {change this to `$\\{stat}\equiv\null$' when gathering
  280. usage statistics}
  281. @d tats==@t@>@} {change this to `$\\{tats}\equiv\null$' when gathering
  282. usage statistics}
  283. @f stat==begin
  284. @f tats==end
  285. @ This program has two important variations: (1) There is a long and slow
  286. version called \.{INITEX}, which does the extra calculations needed to
  287. @.INITEX@>
  288. initialize \TeX's internal tables; and (2)~there is a shorter and faster
  289. production version, which cuts the initialization to a bare minimum.
  290. Parts of the program that are needed in (1) but not in (2) are delimited by
  291. the codewords `$|init|\ldots|tini|$'.
  292. @d init== {change this to `$\\{init}\equiv\.{@@\{}$' in the production version}
  293. @d tini== {change this to `$\\{tini}\equiv\.{@@\}}$' in the production version}
  294. @f init==begin
  295. @f tini==end
  296. @<Initialize whatever...@>=
  297. @<Set initial values of key variables@>@/
  298. @!init @<Initialize table entries (done by \.{INITEX} only)@>@;@+tini
  299. @ If the first character of a \PASCAL\ comment is a dollar sign,
  300. \ph\ treats the comment as a list of ``compiler directives'' that will
  301. affect the translation of this program into machine language. The
  302. directives shown below specify full checking and inclusion of the \PASCAL\
  303. debugger when \TeX\ is being debugged, but they cause range checking and other
  304. redundant code to be eliminated when the production system is being generated.
  305. Arithmetic overflow will be detected in all cases.
  306. @:PASCAL H}{\ph@>
  307. @^system dependencies@>
  308. @^overflow in arithmetic@>
  309. @<Compiler directives@>=
  310. @{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
  311. @!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}
  312. @ This \TeX\ implementation conforms to the rules of the {\sl Pascal User
  313. @:PASCAL}{\PASCAL@>
  314. @^system dependencies@>
  315. Manual} published by Jensen and Wirth in 1975, except where system-dependent
  316. @^Wirth, Niklaus@>
  317. @^Jensen, Kathleen@>
  318. code is necessary to make a useful system program, and except in another
  319. respect where such conformity would unnecessarily obscure the meaning
  320. and clutter up the code: We assume that |case| statements may include a
  321. default case that applies if no matching label is found. Thus, we shall use
  322. constructions like
  323. $$\vbox{\halign{\ignorespaces#\hfil\cr
  324. |case x of|\cr
  325. 1: $\langle\,$code for $x=1\,\rangle$;\cr
  326. 3: $\langle\,$code for $x=3\,\rangle$;\cr
  327. |othercases| $\langle\,$code for |x<>1| and |x<>3|$\,\rangle$\cr
  328. |endcases|\cr}}$$
  329. since most \PASCAL\ compilers have plugged this hole in the language by
  330. incorporating some sort of default mechanism. For example, the \ph\
  331. compiler allows `|others|:' as a default label, and other \PASCAL s allow
  332. syntaxes like `\&{else}' or `\&{otherwise}' or `\\{otherwise}:', etc. The
  333. definitions of |othercases| and |endcases| should be changed to agree with
  334. local conventions. Note that no semicolon appears before |endcases| in
  335. this program, so the definition of |endcases| should include a semicolon
  336. if the compiler wants one. (Of course, if no default mechanism is
  337. available, the |case| statements of \TeX\ will have to be laboriously
  338. extended by listing all remaining cases. People who are stuck with such
  339. \PASCAL s have, in fact, done this, successfully but not happily!)
  340. @:PASCAL H}{\ph@>
  341. @d othercases == others: {default for cases not listed explicitly}
  342. @d endcases == @+end {follows the default case in an extended |case| statement}
  343. @f othercases == else
  344. @f endcases == end
  345. @ The following parameters can be changed at compile time to extend or
  346. reduce \TeX's capacity. They may have different values in \.{INITEX} and
  347. in production versions of \TeX.
  348. @.INITEX@>
  349. @^system dependencies@>
  350. @<Constants...@>=
  351. @!mem_max=30000; {greatest index in \TeX's internal |mem| array;
  352. must be strictly less than |max_halfword|;
  353. must be equal to |mem_top| in \.{INITEX}, otherwise |>=mem_top|}
  354. @!mem_min=0; {smallest index in \TeX's internal |mem| array;
  355. must be |min_halfword| or more;
  356. must be equal to |mem_bot| in \.{INITEX}, otherwise |<=mem_bot|}
  357. @!buf_size=500; {maximum number of characters simultaneously present in
  358. current lines of open files and in control sequences between
  359. \.{\\csname} and \.{\\endcsname}; must not exceed |max_halfword|}
  360. @!error_line=72; {width of context lines on terminal error messages}
  361. @!half_error_line=42; {width of first lines of contexts in terminal
  362. error messages; should be between 30 and |error_line-15|}
  363. @!max_print_line=79; {width of longest text lines output; should be at least 60}
  364. @!stack_size=200; {maximum number of simultaneous input sources}
  365. @!max_in_open=6; {maximum number of input files and error insertions that
  366. can be going on simultaneously}
  367. @!font_max=75; {maximum internal font number; must not exceed |max_quarterword|
  368. and must be at most |font_base+256|}
  369. @!font_mem_size=20000; {number of words of |font_info| for all fonts}
  370. @!param_size=60; {maximum number of simultaneous macro parameters}
  371. @!nest_size=40; {maximum number of semantic levels simultaneously active}
  372. @!max_strings=3000; {maximum number of strings; must not exceed |max_halfword|}
  373. @!string_vacancies=8000; {the minimum number of characters that should be
  374. available for the user's control sequences and font names,
  375. after \TeX's own error messages are stored}
  376. @!pool_size=32000; {maximum number of characters in strings, including all
  377. error messages and help texts, and the names of all fonts and
  378. control sequences; must exceed |string_vacancies| by the total
  379. length of \TeX's own strings, which is currently about 23000}
  380. @!save_size=600; {space for saving values outside of current group; must be
  381. at most |max_halfword|}
  382. @!trie_size=8000; {space for hyphenation patterns; should be larger for
  383. \.{INITEX} than it is in production versions of \TeX}
  384. @!trie_op_size=500; {space for ``opcodes'' in the hyphenation patterns}
  385. @!dvi_buf_size=800; {size of the output buffer; must be a multiple of 8}
  386. @!file_name_size=40; {file names shouldn't be longer than this}
  387. @!pool_name='TeXformats:TEX.POOL ';
  388. {string of length |file_name_size|; tells where the string pool appears}
  389. @.TeXformats@>
  390. @ Like the preceding parameters, the following quantities can be changed
  391. at compile time to extend or reduce \TeX's capacity. But if they are changed,
  392. it is necessary to rerun the initialization program \.{INITEX}
  393. @.INITEX@>
  394. to generate new tables for the production \TeX\ program.
  395. One can't simply make helter-skelter changes to the following constants,
  396. since certain rather complex initialization
  397. numbers are computed from them. They are defined here using
  398. \.{WEB} macros, instead of being put into \PASCAL's |const| list, in order to
  399. emphasize this distinction.
  400. @d mem_bot=0 {smallest index in the |mem| array dumped by \.{INITEX};
  401. must not be less than |mem_min|}
  402. @d mem_top==30000 {largest index in the |mem| array dumped by \.{INITEX};
  403. must be substantially larger than |mem_bot|
  404. and not greater than |mem_max|}
  405. @d font_base=0 {smallest internal font number; must not be less
  406. than |min_quarterword|}
  407. @d hash_size=2100 {maximum number of control sequences; it should be at most
  408. about |(mem_max-mem_min)/10|}
  409. @d hash_prime=1777 {a prime number equal to about 85\pct! of |hash_size|}
  410. @d hyph_size=307 {another prime; the number of \.{\\hyphenation} exceptions}
  411. @^system dependencies@>
  412. @ In case somebody has inadvertently made bad settings of the ``constants,''
  413. \TeX\ checks them using a global variable called |bad|.
  414. This is the first of many sections of \TeX\ where global variables are
  415. defined.
  416. @<Glob...@>=
  417. @!bad:integer; {is some ``constant'' wrong?}
  418. @ Later on we will say `\ignorespaces|if mem_max>=max_halfword then bad:=14|',
  419. or something similar. (We can't do that until |max_halfword| has been defined.)
  420. @<Check the ``constant'' values for consistency@>=
  421. bad:=0;
  422. if (half_error_line<30)or(half_error_line>error_line-15) then bad:=1;
  423. if max_print_line<60 then bad:=2;
  424. if dvi_buf_size mod 8<>0 then bad:=3;
  425. if mem_bot+1100>mem_top then bad:=4;
  426. if hash_prime>hash_size then bad:=5;
  427. if max_in_open>=128 then bad:=6;
  428. if mem_top<256+11 then bad:=7; {we will want |null_list>255|}
  429. @ Labels are given symbolic names by the following definitions, so that
  430. occasional |goto| statements will be meaningful. We insert the label
  431. `|exit|' just before the `\ignorespaces|end|\unskip' of a procedure in
  432. which we have used the `|return|' statement defined below; the label
  433. `|restart|' is occasionally used at the very beginning of a procedure; and
  434. the label `|reswitch|' is occasionally used just prior to a |case|
  435. statement in which some cases change the conditions and we wish to branch
  436. to the newly applicable case. Loops that are set up with the |loop|
  437. construction defined below are commonly exited by going to `|done|' or to
  438. `|found|' or to `|not_found|', and they are sometimes repeated by going to
  439. `|continue|'. If two or more parts of a subroutine start differently but
  440. end up the same, the shared code may be gathered together at
  441. `|common_ending|'.
  442. Incidentally, this program never declares a label that isn't actually used,
  443. because some fussy \PASCAL\ compilers will complain about redundant labels.
  444. @d exit=10 {go here to leave a procedure}
  445. @d restart=20 {go here to start a procedure again}
  446. @d reswitch=21 {go here to start a case statement again}
  447. @d continue=22 {go here to resume a loop}
  448. @d done=30 {go here to exit a loop}
  449. @d done1=31 {like |done|, when there is more than one loop}
  450. @d done2=32 {for exiting the second loop in a long block}
  451. @d done3=33 {for exiting the third loop in a very long block}
  452. @d done4=34 {for exiting the fourth loop in an extremely long block}
  453. @d done5=35 {for exiting the fifth loop in an immense block}
  454. @d done6=36 {for exiting the sixth loop in a block}
  455. @d found=40 {go here when you've found it}
  456. @d found1=41 {like |found|, when there's more than one per routine}
  457. @d found2=42 {like |found|, when there's more than two per routine}
  458. @d not_found=45 {go here when you've found nothing}
  459. @d common_ending=50 {go here when you want to merge with another branch}
  460. @ Here are some macros for common programming idioms.
  461. @d incr(#) == #:=#+1 {increase a variable by unity}
  462. @d decr(#) == #:=#-1 {decrease a variable by unity}
  463. @d negate(#) == #:=-# {change the sign of a variable}
  464. @d loop == @+ while true do@+ {repeat over and over until a |goto| happens}
  465. @f loop == xclause
  466. {\.{WEB}'s |xclause| acts like `\ignorespaces|while true do|\unskip'}
  467. @d do_nothing == {empty statement}
  468. @d return == goto exit {terminate a procedure call}
  469. @f return == nil
  470. @d empty=0 {symbolic name for a null constant}
  471. @* \[2] The character set.
  472. In order to make \TeX\ readily portable to a wide variety of
  473. computers, all of its input text is converted to an internal eight-bit
  474. code that includes standard ASCII, the ``American Standard Code for
  475. Information Interchange.'' This conversion is done immediately when each
  476. character is read in. Conversely, characters are converted from ASCII to
  477. the user's external representation just before they are output to a
  478. text file.
  479. Such an internal code is relevant to users of \TeX\ primarily because it
  480. governs the positions of characters in the fonts. For example, the
  481. character `\.A' has ASCII code $65=@'101$, and when \TeX\ typesets
  482. this letter it specifies character number 65 in the current font.
  483. If that font actually has `\.A' in a different position, \TeX\ doesn't
  484. know what the real position is; the program that does the actual printing from
  485. \TeX's device-independent files is responsible for converting from ASCII to
  486. a particular font encoding.
  487. @^ASCII code@>
  488. \TeX's internal code also defines the value of constants
  489. that begin with a reverse apostrophe; and it provides an index to the
  490. \.{\\catcode}, \.{\\mathcode}, \.{\\uccode}, \.{\\lccode}, and \.{\\delcode}
  491. tables.
  492. @ Characters of text that have been converted to \TeX's internal form
  493. are said to be of type |ASCII_code|, which is a subrange of the integers.
  494. @<Types...@>=
  495. @!ASCII_code=0..255; {eight-bit numbers}
  496. @ The original \PASCAL\ compiler was designed in the late 60s, when six-bit
  497. character sets were common, so it did not make provision for lowercase
  498. letters. Nowadays, of course, we need to deal with both capital and small
  499. letters in a convenient way, especially in a program for typesetting;
  500. so the present specification of \TeX\ has been written under the assumption
  501. that the \PASCAL\ compiler and run-time system permit the use of text files
  502. with more than 64 distinguishable characters. More precisely, we assume that
  503. the character set contains at least the letters and symbols associated
  504. with ASCII codes @'40 through @'176; all of these characters are now
  505. available on most computer terminals.
  506. Since we are dealing with more characters than were present in the first
  507. \PASCAL\ compilers, we have to decide what to call the associated data
  508. type. Some \PASCAL s use the original name |char| for the
  509. characters in text files, even though there now are more than 64 such
  510. characters, while other \PASCAL s consider |char| to be a 64-element
  511. subrange of a larger data type that has some other name.
  512. In order to accommodate this difference, we shall use the name |text_char|
  513. to stand for the data type of the characters that are converted to and
  514. from |ASCII_code| when they are input and output. We shall also assume
  515. that |text_char| consists of the elements |chr(first_text_char)| through
  516. |chr(last_text_char)|, inclusive. The following definitions should be
  517. adjusted if necessary.
  518. @^system dependencies@>
  519. @d text_char == char {the data type of characters in text files}
  520. @d first_text_char=0 {ordinal number of the smallest element of |text_char|}
  521. @d last_text_char=255 {ordinal number of the largest element of |text_char|}
  522. @<Local variables for init...@>=
  523. @!i:integer;
  524. @ The \TeX\ processor converts between ASCII code and
  525. the user's external character set by means of arrays |xord| and |xchr|
  526. that are analogous to \PASCAL's |ord| and |chr| functions.
  527. @<Glob...@>=
  528. @!xord: array [text_char] of ASCII_code;
  529. {specifies conversion of input characters}
  530. @!xchr: array [ASCII_code] of text_char;
  531. {specifies conversion of output characters}
  532. @ Since we are assuming that our \PASCAL\ system is able to read and
  533. write the visible characters of standard ASCII (although not
  534. necessarily using the ASCII codes to represent them), the following
  535. assignment statements initialize the standard part of the |xchr| array
  536. properly, without needing any system-dependent changes. On the other
  537. hand, it is possible to implement \TeX\ with less complete character
  538. sets, and in such cases it will be necessary to change something here.
  539. @^system dependencies@>
  540. @<Set init...@>=
  541. xchr[@'40]:=' ';
  542. xchr[@'41]:='!';
  543. xchr[@'42]:='"';
  544. xchr[@'43]:='#';
  545. xchr[@'44]:='$';
  546. xchr[@'45]:='%';
  547. xchr[@'46]:='&';
  548. xchr[@'47]:='''';@/
  549. xchr[@'50]:='(';
  550. xchr[@'51]:=')';
  551. xchr[@'52]:='*';
  552. xchr[@'53]:='+';
  553. xchr[@'54]:=',';
  554. xchr[@'55]:='-';
  555. xchr[@'56]:='.';
  556. xchr[@'57]:='/';@/
  557. xchr[@'60]:='0';
  558. xchr[@'61]:='1';
  559. xchr[@'62]:='2';
  560. xchr[@'63]:='3';
  561. xchr[@'64]:='4';
  562. xchr[@'65]:='5';
  563. xchr[@'66]:='6';
  564. xchr[@'67]:='7';@/
  565. xchr[@'70]:='8';
  566. xchr[@'71]:='9';
  567. xchr[@'72]:=':';
  568. xchr[@'73]:=';';
  569. xchr[@'74]:='<';
  570. xchr[@'75]:='=';
  571. xchr[@'76]:='>';
  572. xchr[@'77]:='?';@/
  573. xchr[@'100]:='@@';
  574. xchr[@'101]:='A';
  575. xchr[@'102]:='B';
  576. xchr[@'103]:='C';
  577. xchr[@'104]:='D';
  578. xchr[@'105]:='E';
  579. xchr[@'106]:='F';
  580. xchr[@'107]:='G';@/
  581. xchr[@'110]:='H';
  582. xchr[@'111]:='I';
  583. xchr[@'112]:='J';
  584. xchr[@'113]:='K';
  585. xchr[@'114]:='L';
  586. xchr[@'115]:='M';
  587. xchr[@'116]:='N';
  588. xchr[@'117]:='O';@/
  589. xchr[@'120]:='P';
  590. xchr[@'121]:='Q';
  591. xchr[@'122]:='R';
  592. xchr[@'123]:='S';
  593. xchr[@'124]:='T';
  594. xchr[@'125]:='U';
  595. xchr[@'126]:='V';
  596. xchr[@'127]:='W';@/
  597. xchr[@'130]:='X';
  598. xchr[@'131]:='Y';
  599. xchr[@'132]:='Z';
  600. xchr[@'133]:='[';
  601. xchr[@'134]:='\';
  602. xchr[@'135]:=']';
  603. xchr[@'136]:='^';
  604. xchr[@'137]:='_';@/
  605. xchr[@'140]:='`';
  606. xchr[@'141]:='a';
  607. xchr[@'142]:='b';
  608. xchr[@'143]:='c';
  609. xchr[@'144]:='d';
  610. xchr[@'145]:='e';
  611. xchr[@'146]:='f';
  612. xchr[@'147]:='g';@/
  613. xchr[@'150]:='h';
  614. xchr[@'151]:='i';
  615. xchr[@'152]:='j';
  616. xchr[@'153]:='k';
  617. xchr[@'154]:='l';
  618. xchr[@'155]:='m';
  619. xchr[@'156]:='n';
  620. xchr[@'157]:='o';@/
  621. xchr[@'160]:='p';
  622. xchr[@'161]:='q';
  623. xchr[@'162]:='r';
  624. xchr[@'163]:='s';
  625. xchr[@'164]:='t';
  626. xchr[@'165]:='u';
  627. xchr[@'166]:='v';
  628. xchr[@'167]:='w';@/
  629. xchr[@'170]:='x';
  630. xchr[@'171]:='y';
  631. xchr[@'172]:='z';
  632. xchr[@'173]:='{';
  633. xchr[@'174]:='|';
  634. xchr[@'175]:='}';
  635. xchr[@'176]:='~';@/
  636. @ Some of the ASCII codes without visible characters have been given symbolic
  637. names in this program because they are used with a special meaning.
  638. @d null_code=@'0 {ASCII code that might disappear}
  639. @d carriage_return=@'15 {ASCII code used at end of line}
  640. @d invalid_code=@'177 {ASCII code that many systems prohibit in text files}
  641. @ The ASCII code is ``standard'' only to a certain extent, since many
  642. computer installations have found it advantageous to have ready access
  643. to more than 94 printing characters. Appendix~C of {\sl The \TeX book\/}
  644. gives a complete specification of the intended correspondence between
  645. characters and \TeX's internal representation.
  646. @:TeXbook}{\sl The \TeX book@>
  647. If \TeX\ is being used
  648. on a garden-variety \PASCAL\ for which only standard ASCII
  649. codes will appear in the input and output files, it doesn't really matter
  650. what codes are specified in |xchr[0..@'37]|, but the safest policy is to
  651. blank everything out by using the code shown below.
  652. However, other settings of |xchr| will make \TeX\ more friendly on
  653. computers that have an extended character set, so that users can type things
  654. like `\.^^Z' instead of `\.{\\ne}'. People with extended character sets can
  655. assign codes arbitrarily, giving an |xchr| equivalent to whatever
  656. characters the users of \TeX\ are allowed to have in their input files.
  657. It is best to make the codes correspond to the intended interpretations as
  658. shown in Appendix~C whenever possible; but this is not necessary. For
  659. example, in countries with an alphabet of more than 26 letters, it is
  660. usually best to map the additional letters into codes less than~@'40.
  661. To get the most ``permissive'' character set, change |' '| on the
  662. right of these assignment statements to |chr(i)|.
  663. @^character set dependencies@>
  664. @^system dependencies@>
  665. @<Set init...@>=
  666. for i:=0 to @'37 do xchr[i]:=' ';
  667. for i:=@'177 to @'377 do xchr[i]:=' ';
  668. @ The following system-independent code makes the |xord| array contain a
  669. suitable inverse to the information in |xchr|. Note that if |xchr[i]=xchr[j]|
  670. where |i<j<@'177|, the value of |xord[xchr[i]]| will turn out to be
  671. |j| or more; hence, standard ASCII code numbers will be used instead of
  672. codes below @'40 in case there is a coincidence.
  673. @<Set init...@>=
  674. for i:=first_text_char to last_text_char do xord[chr(i)]:=invalid_code;
  675. for i:=@'200 to @'377 do xord[xchr[i]]:=i;
  676. for i:=0 to @'176 do xord[xchr[i]]:=i;
  677. @* \[3] Input and output.
  678. The bane of portability is the fact that different operating systems treat
  679. input and output quite differently, perhaps because computer scientists
  680. have not given sufficient attention to this problem. People have felt somehow
  681. that input and output are not part of ``real'' programming. Well, it is true
  682. that some kinds of programming are more fun than others. With existing
  683. input/output conventions being so diverse and so messy, the only sources of
  684. joy in such parts of the code are the rare occasions when one can find a
  685. way to make the program a little less bad than it might have been. We have
  686. two choices, either to attack I/O now and get it over with, or to postpone
  687. I/O until near the end. Neither prospect is very attractive, so let's
  688. get it over with.
  689. The basic operations we need to do are (1)~inputting and outputting of
  690. text, to or from a file or the user's terminal; (2)~inputting and
  691. outputting of eight-bit bytes, to or from a file; (3)~instructing the
  692. operating system to initiate (``open'') or to terminate (``close'') input or
  693. output from a specified file; (4)~testing whether the end of an input
  694. file has been reached.
  695. \TeX\ needs to deal with two kinds of files.
  696. We shall use the term |alpha_file| for a file that contains textual data,
  697. and the term |byte_file| for a file that contains eight-bit binary information.
  698. These two types turn out to be the same on many computers, but
  699. sometimes there is a significant distinction, so we shall be careful to
  700. distinguish between them. Standard protocols for transferring
  701. such files from computer to computer, via high-speed networks, are
  702. now becoming available to more and more communities of users.
  703. The program actually makes use also of a third kind of file, called a
  704. |word_file|, when dumping and reloading base information for its own
  705. initialization. We shall define a word file later; but it will be possible
  706. for us to specify simple operations on word files before they are defined.
  707. @<Types...@>=
  708. @!eight_bits=0..255; {unsigned one-byte quantity}
  709. @!alpha_file=packed file of text_char; {files that contain textual data}
  710. @!byte_file=packed file of eight_bits; {files that contain binary data}
  711. @ Most of what we need to do with respect to input and output can be handled
  712. by the I/O facilities that are standard in \PASCAL, i.e., the routines
  713. called |get|, |put|, |eof|, and so on. But
  714. standard \PASCAL\ does not allow file variables to be associated with file
  715. names that are determined at run time, so it cannot be used to implement
  716. \TeX; some sort of extension to \PASCAL's ordinary |reset| and |rewrite|
  717. is crucial for our purposes. We shall assume that |name_of_file| is a variable
  718. of an appropriate type such that the \PASCAL\ run-time system being used to
  719. implement \TeX\ can open a file whose external name is specified by
  720. |name_of_file|.
  721. @^system dependencies@>
  722. @<Glob...@>=
  723. @!name_of_file:packed array[1..file_name_size] of char;@;@/
  724. {on some systems this may be a \&{record} variable}
  725. @!name_length:0..file_name_size;@/{this many characters are actually
  726. relevant in |name_of_file| (the rest are blank)}
  727. @ The \ph\ compiler with which the present version of \TeX\ was prepared has
  728. extended the rules of \PASCAL\ in a very convenient way. To open file~|f|,
  729. we can write
  730. $$\vbox{\halign{#\hfil\qquad&#\hfil\cr
  731. |reset(f,@t\\{name}@>,'/O')|&for input;\cr
  732. |rewrite(f,@t\\{name}@>,'/O')|&for output.\cr}}$$
  733. The `\\{name}' parameter, which is of type `{\bf packed array
  734. $[\langle\\{any}\rangle]$ of \\{char}}', stands for the name of
  735. the external file that is being opened for input or output.
  736. Blank spaces that might appear in \\{name} are ignored.
  737. The `\.{/O}' parameter tells the operating system not to issue its own
  738. error messages if something goes wrong. If a file of the specified name
  739. cannot be found, or if such a file cannot be opened for some other reason
  740. (e.g., someone may already be trying to write the same file), we will have
  741. |@!erstat(f)<>0| after an unsuccessful |reset| or |rewrite|. This allows
  742. \TeX\ to undertake appropriate corrective action.
  743. @:PASCAL H}{\ph@>
  744. @^system dependencies@>
  745. \TeX's file-opening procedures return |false| if no file identified by
  746. |name_of_file| could be opened.
  747. @d reset_OK(#)==erstat(#)=0
  748. @d rewrite_OK(#)==erstat(#)=0
  749. @p function a_open_in(var f:alpha_file):boolean;
  750. {open a text file for input}
  751. begin reset(f,name_of_file,'/O'); a_open_in:=reset_OK(f);
  752. end;
  753. @#
  754. function a_open_out(var f:alpha_file):boolean;
  755. {open a text file for output}
  756. begin rewrite(f,name_of_file,'/O'); a_open_out:=rewrite_OK(f);
  757. end;
  758. @#
  759. function b_open_in(var f:byte_file):boolean;
  760. {open a binary file for input}
  761. begin reset(f,name_of_file,'/O'); b_open_in:=reset_OK(f);
  762. end;
  763. @#
  764. function b_open_out(var f:byte_file):boolean;
  765. {open a binary file for output}
  766. begin rewrite(f,name_of_file,'/O'); b_open_out:=rewrite_OK(f);
  767. end;
  768. @#
  769. function w_open_in(var f:word_file):boolean;
  770. {open a word file for input}
  771. begin reset(f,name_of_file,'/O'); w_open_in:=reset_OK(f);
  772. end;
  773. @#
  774. function w_open_out(var f:word_file):boolean;
  775. {open a word file for output}
  776. begin rewrite(f,name_of_file,'/O'); w_open_out:=rewrite_OK(f);
  777. end;
  778. @ Files can be closed with the \ph\ routine `|close(f)|', which
  779. @:PASCAL H}{\ph@>
  780. @^system dependencies@>
  781. should be used when all input or output with respect to |f| has been completed.
  782. This makes |f| available to be opened again, if desired; and if |f| was used for
  783. output, the |close| operation makes the corresponding external file appear
  784. on the user's area, ready to be read.
  785. These procedures should not generate error messages if a file is
  786. being closed before it has been successfully opened.
  787. @p procedure a_close(var f:alpha_file); {close a text file}
  788. begin close(f);
  789. end;
  790. @#
  791. procedure b_close(var f:byte_file); {close a binary file}
  792. begin close(f);
  793. end;
  794. @#
  795. procedure w_close(var f:word_file); {close a word file}
  796. begin close(f);
  797. end;
  798. @ Binary input and output are done with \PASCAL's ordinary |get| and |put|
  799. procedures, so we don't have to make any other special arrangements for
  800. binary~I/O. Text output is also easy to do with standard \PASCAL\ routines.
  801. The treatment of text input is more difficult, however, because
  802. of the necessary translation to |ASCII_code| values.
  803. \TeX's conventions should be efficient, and they should
  804. blend nicely with the user's operating environment.
  805. @ Input from text files is read one line at a time, using a routine called
  806. |input_ln|. This function is defined in terms of global variables called
  807. |buffer|, |first|, and |last| that will be described in detail later; for
  808. now, it suffices for us to know that |buffer| is an array of |ASCII_code|
  809. values, and that |first| and |last| are indices into this array
  810. representing the beginning and ending of a line of text.
  811. @<Glob...@>=
  812. @!buffer:array[0..buf_size] of ASCII_code; {lines of characters being read}
  813. @!first:0..buf_size; {the first unused position in |buffer|}
  814. @!last:0..buf_size; {end of the line just input to |buffer|}
  815. @!max_buf_stack:0..buf_size; {largest index used in |buffer|}
  816. @ The |input_ln| function brings the next line of input from the specified
  817. file into available positions of the buffer array and returns the value
  818. |true|, unless the file has already been entirely read, in which case it
  819. returns |false| and sets |last:=first|. In general, the |ASCII_code|
  820. numbers that represent the next line of the file are input into
  821. |buffer[first]|, |buffer[first+1]|, \dots, |buffer[last-1]|; and the
  822. global variable |last| is set equal to |first| plus the length of the
  823. line. Trailing blanks are removed from the line; thus, either |last=first|
  824. (in which case the line was entirely blank) or |buffer[last-1]<>" "|.
  825. An overflow error is given, however, if the normal actions of |input_ln|
  826. would make |last>=buf_size|; this is done so that other parts of \TeX\
  827. can safely look at the contents of |buffer[last+1]| without overstepping
  828. the bounds of the |buffer| array. Upon entry to |input_ln|, the condition
  829. |first<buf_size| will always hold, so that there is always room for an
  830. ``empty'' line.
  831. The variable |max_buf_stack|, which is used to keep track of how large
  832. the |buf_size| parameter must be to accommodate the present job, is
  833. also kept up to date by |input_ln|.
  834. If the |bypass_eoln| parameter is |true|, |input_ln| will do a |get|
  835. before looking at the first character of the line; this skips over
  836. an |eoln| that was in |f^|. The procedure does not do a |get| when it
  837. reaches the end of the line; therefore it can be used to acquire input
  838. from the user's terminal as well as from ordinary text files.
  839. Standard \PASCAL\ says that a file should have |eoln| immediately
  840. before |eof|, but \TeX\ needs only a weaker restriction: If |eof|
  841. occurs in the middle of a line, the system function |eoln| should return
  842. a |true| result (even though |f^| will be undefined).
  843. Since the inner loop of |input_ln| is part of \TeX's ``inner loop''---each
  844. character of input comes in at this place---it is wise to reduce system
  845. overhead by making use of special routines that read in an entire array
  846. of characters at once, if such routines are available. The following
  847. code uses standard \PASCAL\ to illustrate what needs to be done, but
  848. finer tuning is often possible at well-developed \PASCAL\ sites.
  849. @^inner loop@>
  850. @p function input_ln(var f:alpha_file;@!bypass_eoln:boolean):boolean;
  851. {inputs the next line or returns |false|}
  852. var last_nonblank:0..buf_size; {|last| with trailing blanks removed}
  853. begin if bypass_eoln then if not eof(f) then get(f);
  854. {input the first character of the line into |f^|}
  855. last:=first; {cf.\ Matthew 19\thinspace:\thinspace30}
  856. if eof(f) then input_ln:=false
  857. else begin last_nonblank:=first;
  858. while not eoln(f) do
  859. begin if last>=max_buf_stack then
  860. begin max_buf_stack:=last+1;
  861. if max_buf_stack=buf_size then
  862. @<Report overflow of the input buffer, and abort@>;
  863. end;
  864. buffer[last]:=xord[f^]; get(f); incr(last);
  865. if buffer[last-1]<>" " then last_nonblank:=last;
  866. end;
  867. last:=last_nonblank; input_ln:=true;
  868. end;
  869. end;
  870. @ The user's terminal acts essentially like other files of text, except
  871. that it is used both for input and for output. When the terminal is
  872. considered an input file, the file variable is called |term_in|, and when it
  873. is considered an output file the file variable is |term_out|.
  874. @^system dependencies@>
  875. @<Glob...@>=
  876. @!term_in:alpha_file; {the terminal as an input file}
  877. @!term_out:alpha_file; {the terminal as an output file}
  878. @ Here is how to open the terminal files
  879. in \ph. The `\.{/I}' switch suppresses the first |get|.
  880. @:PASCAL H}{\ph@>
  881. @^system dependencies@>
  882. @d t_open_in==reset(term_in,'TTY:','/O/I') {open the terminal for text input}
  883. @d t_open_out==rewrite(term_out,'TTY:','/O') {open the terminal for text output}
  884. @ Sometimes it is necessary to synchronize the input/output mixture that
  885. happens on the user's terminal, and three system-dependent
  886. procedures are used for this
  887. purpose. The first of these, |update_terminal|, is called when we want
  888. to make sure that everything we have output to the terminal so far has
  889. actually left the computer's internal buffers and been sent.
  890. The second, |clear_terminal|, is called when we wish to cancel any
  891. input that the user may have typed ahead (since we are about to
  892. issue an unexpected error message). The third, |wake_up_terminal|,
  893. is supposed to revive the terminal if the user has disabled it by
  894. some instruction to the operating system. The following macros show how
  895. these operations can be specified in \ph:
  896. @:PASCAL H}{\ph@>
  897. @^system dependencies@>
  898. @d update_terminal == break(term_out) {empty the terminal output buffer}
  899. @d clear_terminal == break_in(term_in,true) {clear the terminal input buffer}
  900. @d wake_up_terminal == do_nothing {cancel the user's cancellation of output}
  901. @ We need a special routine to read the first line of \TeX\ input from
  902. the user's terminal. This line is different because it is read before we
  903. have opened the transcript file; there is sort of a ``chicken and
  904. egg'' problem here. If the user types `\.{\\input paper}' on the first
  905. line, or if some macro invoked by that line does such an \.{\\input},
  906. the transcript file will be named `\.{paper.log}'; but if no \.{\\input}
  907. commands are performed during the first line of terminal input, the transcript
  908. file will acquire its default name `\.{texput.log}'. (The transcript file
  909. will not contain error messages generated by the first line before the
  910. first \.{\\input} command.)
  911. @.texput@>
  912. The first line is even more special if we are lucky enough to have an operating
  913. system that treats \TeX\ differently from a run-of-the-mill \PASCAL\ object
  914. program. It's nice to let the user start running a \TeX\ job by typing
  915. a command line like `\.{tex paper}'; in such a case, \TeX\ will operate
  916. as if the first line of input were `\.{paper}', i.e., the first line will
  917. consist of the remainder of the command line, after the part that invoked
  918. \TeX.
  919. The first line is special also because it may be read before \TeX\ has
  920. input a format file. In such cases, normal error messages cannot yet
  921. be given. The following code uses concepts that will be explained later.
  922. (If the \PASCAL\ compiler does not support non-local |@!goto|\unskip, the
  923. @^system dependencies@>
  924. statement `|goto final_end|' should be replaced by something that
  925. quietly terminates the program.)
  926. @<Report overflow of the input buffer, and abort@>=
  927. if format_ident=0 then
  928. begin write_ln(term_out,'Buffer size exceeded!'); goto final_end;
  929. @.Buffer size exceeded@>
  930. end
  931. else begin cur_input.loc_field:=first; cur_input.limit_field:=last-1;
  932. overflow("buffer size",buf_size);
  933. @:TeX capacity exceeded buffer size}{\quad buffer size@>
  934. end
  935. @ Different systems have different ways to get started. But regardless of
  936. what conventions are adopted, the routine that initializes the terminal
  937. should satisfy the following specifications:
  938. \yskip\textindent{1)}It should open file |term_in| for input from the
  939. terminal. (The file |term_out| will already be open for output to the
  940. terminal.)
  941. \textindent{2)}If the user has given a command line, this line should be
  942. considered the first line of terminal input. Otherwise the
  943. user should be prompted with `\.{**}', and the first line of input
  944. should be whatever is typed in response.
  945. \textindent{3)}The first line of input, which might or might not be a
  946. command line, should appear in locations |first| to |last-1| of the
  947. |buffer| array.
  948. \textindent{4)}The global variable |loc| should be set so that the
  949. character to be read next by \TeX\ is in |buffer[loc]|. This
  950. character should not be blank, and we should have |loc<last|.
  951. \yskip\noindent(It may be necessary to prompt the user several times
  952. before a non-blank line comes in. The prompt is `\.{**}' instead of the
  953. later `\.*' because the meaning is slightly different: `\.{\\input}' need
  954. not be typed immediately after~`\.{**}'.)
  955. @d loc==cur_input.loc_field {location of first unread character in |buffer|}
  956. @ The following program does the required initialization
  957. without retrieving a possible command line.
  958. It should be clear how to modify this routine to deal with command lines,
  959. if the system permits them.
  960. @^system dependencies@>
  961. @p function init_terminal:boolean; {gets the terminal input started}
  962. label exit;
  963. begin t_open_in;
  964. loop@+begin wake_up_terminal; write(term_out,'**'); update_terminal;
  965. @.**@>
  966. if not input_ln(term_in,true) then {this shouldn't happen}
  967. begin write_ln(term_out);
  968. write(term_out,'! End of file on the terminal... why?');
  969. @.End of file on the terminal@>
  970. init_terminal:=false; return;
  971. end;
  972. loc:=first;
  973. while (loc<last)and(buffer[loc]=" ") do incr(loc);
  974. if loc<last then
  975. begin init_terminal:=true;
  976. return; {return unless the line was all blank}
  977. end;
  978. write_ln(term_out,'Please type the name of your input file.');
  979. end;
  980. exit:end;
  981. @* \[4] String handling.
  982. Control sequence names and diagnostic messages are variable-length strings
  983. of eight-bit characters. Since \PASCAL\ does not have a well-developed string
  984. mechanism, \TeX\ does all of its string processing by homegrown methods.
  985. Elaborate facilities for dynamic strings are not needed, so all of the
  986. necessary operations can be handled with a simple data structure.
  987. The array |str_pool| contains all of the (eight-bit) ASCII codes in all
  988. of the strings, and the array |str_start| contains indices of the starting
  989. points of each string. Strings are referred to by integer numbers, so that
  990. string number |s| comprises the characters |str_pool[j]| for
  991. |str_start[s]<=j<str_start[s+1]|. Additional integer variables
  992. |pool_ptr| and |str_ptr| indicate the number of entries used so far
  993. in |str_pool| and |str_start|, respectively; locations
  994. |str_pool[pool_ptr]| and |str_start[str_ptr]| are
  995. ready for the next string to be allocated.
  996. String numbers 0 to 255 are reserved for strings that correspond to single
  997. ASCII characters. This is in accordance with the conventions of \.{WEB},
  998. @.WEB@>
  999. which converts single-character strings into the ASCII code number of the
  1000. single character involved, while it converts other strings into integers
  1001. and builds a string pool file. Thus, when the string constant \.{"."} appears
  1002. in the program below, \.{WEB} converts it into the integer 46, which is the
  1003. ASCII code for a period, while \.{WEB} will convert a string like \.{"hello"}
  1004. into some integer greater than~255. String number 46 will presumably be the
  1005. single character `\..'; but some ASCII codes have no standard visible
  1006. representation, and \TeX\ sometimes needs to be able to print an arbitrary
  1007. ASCII character, so the first 256 strings are used to specify exactly what
  1008. should be printed for each of the 256 possibilities.
  1009. Elements of the |str_pool| array must be ASCII codes that can actually
  1010. be printed; i.e., they must have an |xchr| equivalent in the local
  1011. character set. (This restriction applies only to preloaded strings,
  1012. not to those generated dynamically by the user.)
  1013. Some \PASCAL\ compilers won't pack integers into a single byte unless the
  1014. integers lie in the range |-128..127|. To accommodate such systems
  1015. we access the string pool only via macros that can easily be redefined.
  1016. @^system dependencies@>
  1017. @d si(#) == # {convert from |ASCII_code| to |packed_ASCII_code|}
  1018. @d so(#) == # {convert from |packed_ASCII_code| to |ASCII_code|}
  1019. @<Types...@>=
  1020. @!pool_pointer = 0..pool_size; {for variables that point into |str_pool|}
  1021. @!str_number = 0..max_strings; {for variables that point into |str_start|}
  1022. @!packed_ASCII_code = 0..255; {elements of |str_pool| array}
  1023. @ @<Glob...@>=
  1024. @!str_pool:packed array[pool_pointer] of packed_ASCII_code; {the characters}
  1025. @!str_start : array[str_number] of pool_pointer; {the starting pointers}
  1026. @!pool_ptr : pool_pointer; {first unused position in |str_pool|}
  1027. @!str_ptr : str_number; {number of the current string being created}
  1028. @!init_pool_ptr : pool_pointer; {the starting value of |pool_ptr|}
  1029. @!init_str_ptr : str_number; {the starting value of |str_ptr|}
  1030. @ Several of the elementary string operations are performed using \.{WEB}
  1031. macros instead of \PASCAL\ procedures, because many of the
  1032. operations are done quite frequently and we want to avoid the
  1033. overhead of procedure calls. For example, here is
  1034. a simple macro that computes the length of a string.
  1035. @.WEB@>
  1036. @d length(#)==(str_start[#+1]-str_start[#]) {the number of characters
  1037. in string number \#}
  1038. @ The length of the current string is called |cur_length|:
  1039. @d cur_length == (pool_ptr - str_start[str_ptr])
  1040. @ Strings are created by appending character codes to |str_pool|.
  1041. The |append_char| macro, defined here, does not check to see if the
  1042. value of |pool_ptr| has gotten too high; this test is supposed to be
  1043. made before |append_char| is used. There is also a |flush_char|
  1044. macro, which erases the last character appended.
  1045. To test if there is room to append |l| more characters to |str_pool|,
  1046. we shall write |str_room(l)|, which aborts \TeX\ and gives an
  1047. apologetic error message if there isn't enough room.
  1048. @d append_char(#) == {put |ASCII_code| \# at the end of |str_pool|}
  1049. begin str_pool[pool_ptr]:=si(#); incr(pool_ptr);
  1050. end
  1051. @d flush_char == decr(pool_ptr) {forget the last character in the pool}
  1052. @d str_room(#) == {make sure that the pool hasn't overflowed}
  1053. begin if pool_ptr+# > pool_size then
  1054. overflow("pool size",pool_size-init_pool_ptr);
  1055. @:TeX capacity exceeded pool size}{\quad pool size@>
  1056. end
  1057. @ Once a sequence of characters has been appended to |str_pool|, it
  1058. officially becomes a string when the function |make_string| is called.
  1059. This function returns the identification number of the new string as its
  1060. value.
  1061. @p function make_string : str_number; {current string enters the pool}
  1062. begin if str_ptr=max_strings then
  1063. overflow("number of strings",max_strings-init_str_ptr);
  1064. @:TeX capacity exceeded number of strings}{\quad number of strings@>
  1065. incr(str_ptr); str_start[str_ptr]:=pool_ptr;
  1066. make_string:=str_ptr-1;
  1067. end;
  1068. @ To destroy the most recently made string, we say |flush_string|.
  1069. @d flush_string==begin decr(str_ptr); pool_ptr:=str_start[str_ptr];
  1070. end
  1071. @ The following subroutine compares string |s| with another string of the
  1072. same length that appears in |buffer| starting at position |k|;
  1073. the result is |true| if and only if the strings are equal.
  1074. Empirical tests indicate that |str_eq_buf| is used in such a way that
  1075. it tends to return |true| about 80 percent of the time.
  1076. @p function str_eq_buf(@!s:str_number;@!k:integer):boolean;
  1077. {test equality of strings}
  1078. label not_found; {loop exit}
  1079. var j: pool_pointer; {running index}
  1080. @!result: boolean; {result of comparison}
  1081. begin j:=str_start[s];
  1082. while j<str_start[s+1] do
  1083. begin if so(str_pool[j])<>buffer[k] then
  1084. begin result:=false; goto not_found;
  1085. end;
  1086. incr(j); incr(k);
  1087. end;
  1088. result:=true;
  1089. not_found: str_eq_buf:=result;
  1090. end;
  1091. @ Here is a similar routine, but it compares two strings in the string pool,
  1092. and it does not assume that they have the same length.
  1093. @p function str_eq_str(@!s,@!t:str_number):boolean;
  1094. {test equality of strings}
  1095. label not_found; {loop exit}
  1096. var j,@!k: pool_pointer; {running indices}
  1097. @!result: boolean; {result of comparison}
  1098. begin result:=false;
  1099. if length(s)<>length(t) then goto not_found;
  1100. j:=str_start[s]; k:=str_start[t];
  1101. while j<str_start[s+1] do
  1102. begin if str_pool[j]<>str_pool[k] then goto not_found;
  1103. incr(j); incr(k);
  1104. end;
  1105. result:=true;
  1106. not_found: str_eq_str:=result;
  1107. end;
  1108. @ The initial values of |str_pool|, |str_start|, |pool_ptr|,
  1109. and |str_ptr| are computed by the \.{INITEX} program, based in part
  1110. on the information that \.{WEB} has output while processing \TeX.
  1111. @.INITEX@>
  1112. @^string pool@>
  1113. @p @!init function get_strings_started:boolean; {initializes the string pool,
  1114. but returns |false| if something goes wrong}
  1115. label done,exit;
  1116. var k,@!l:0..255; {small indices or counters}
  1117. @!m,@!n:text_char; {characters input from |pool_file|}
  1118. @!g:str_number; {garbage}
  1119. @!a:integer; {accumulator for check sum}
  1120. @!c:boolean; {check sum has been checked}
  1121. begin pool_ptr:=0; str_ptr:=0; str_start[0]:=0;
  1122. @<Make the first 256 strings@>;
  1123. @<Read the other strings from the \.{TEX.POOL} file and return |true|,
  1124. or give an error message and return |false|@>;
  1125. exit:end;
  1126. tini
  1127. @ @d app_lc_hex(#)==l:=#;
  1128. if l<10 then append_char(l+"0")@+else append_char(l-10+"a")
  1129. @<Make the first 256...@>=
  1130. for k:=0 to 255 do
  1131. begin if (@<Character |k| cannot be printed@>) then
  1132. begin append_char("^"); append_char("^");
  1133. if k<@'100 then append_char(k+@'100)
  1134. else if k<@'200 then append_char(k-@'100)
  1135. else begin app_lc_hex(k div 16); app_lc_hex(k mod 16);
  1136. end;
  1137. end
  1138. else append_char(k);
  1139. g:=make_string;
  1140. end
  1141. @ The first 128 strings will contain 95 standard ASCII characters, and the
  1142. other 33 characters will be printed in three-symbol form like `\.{\^\^A}'
  1143. unless a system-dependent change is made here. Installations that have
  1144. an extended character set, where for example |xchr[@'32]=@t\.{\'^^Z\'}@>|,
  1145. would like string @'32 to be the single character @'32 instead of the
  1146. three characters @'136, @'136, @'132 (\.{\^\^Z}). On the other hand,
  1147. even people with an extended character set will want to represent string
  1148. @'15 by \.{\^\^M}, since @'15 is |carriage_return|; the idea is to
  1149. produce visible strings instead of tabs or line-feeds or carriage-returns
  1150. or bell-rings or characters that are treated anomalously in text files.
  1151. Unprintable characters of codes 128--255 are, similarly, rendered
  1152. \.{\^\^80}--\.{\^\^ff}.
  1153. The boolean expression defined here should be |true| unless \TeX\
  1154. internal code number~|k| corresponds to a non-troublesome visible
  1155. symbol in the local character set. An appropriate formula for the
  1156. extended character set recommended in {\sl The \TeX book\/} would, for
  1157. example, be `|k in [0,@'10..@'12,@'14,@'15,@'33,@'177..@'377]|'.
  1158. If character |k| cannot be printed, and |k<@'200|, then character |k+@'100| or
  1159. |k-@'100| must be printable; moreover, ASCII codes |[@'41..@'46,
  1160. @'60..@'71, @'136, @'141..@'146, @'160..@'171]| must be printable.
  1161. Thus, at least 80 printable characters are needed.
  1162. @:TeXbook}{\sl The \TeX book@>
  1163. @^character set dependencies@>
  1164. @^system dependencies@>
  1165. @<Character |k| cannot be printed@>=
  1166. (k<" ")or(k>"~")
  1167. @ When the \.{WEB} system program called \.{TANGLE} processes the \.{TEX.WEB}
  1168. description that you are now reading, it outputs the \PASCAL\ program
  1169. \.{TEX.PAS} and also a string pool file called \.{TEX.POOL}. The \.{INITEX}
  1170. @.WEB@>@.INITEX@>
  1171. program reads the latter file, where each string appears as a two-digit decimal
  1172. length followed by the string itself, and the information is recorded in
  1173. \TeX's string memory.
  1174. @<Glob...@>=
  1175. @!init @!pool_file:alpha_file; {the string-pool file output by \.{TANGLE}}
  1176. tini
  1177. @ @d bad_pool(#)==begin wake_up_terminal; write_ln(term_out,#);
  1178. a_close(pool_file); get_strings_started:=false; return;
  1179. end
  1180. @<Read the other strings...@>=
  1181. name_of_file:=pool_name; {we needn't set |name_length|}
  1182. if a_open_in(pool_file) then
  1183. begin c:=false;
  1184. repeat @<Read one string, but return |false| if the
  1185. string memory space is getting too tight for comfort@>;
  1186. until c;
  1187. a_close(pool_file); get_strings_started:=true;
  1188. end
  1189. else bad_pool('! I can''t read TEX.POOL.')
  1190. @.I can't read TEX.POOL@>
  1191. @ @<Read one string...@>=
  1192. begin if eof(pool_file) then bad_pool('! TEX.POOL has no check sum.');
  1193. @.TEX.POOL has no check sum@>
  1194. read(pool_file,m,n); {read two digits of string length}
  1195. if m='*' then @<Check the pool check sum@>
  1196. else begin if (xord[m]<"0")or(xord[m]>"9")or@|
  1197. (xord[n]<"0")or(xord[n]>"9") then
  1198. bad_pool('! TEX.POOL line doesn''t begin with two digits.');
  1199. @.TEX.POOL line doesn't...@>
  1200. l:=xord[m]*10+xord[n]-"0"*11; {compute the length}
  1201. if pool_ptr+l+string_vacancies>pool_size then
  1202. bad_pool('! You have to increase POOLSIZE.');
  1203. @.You have to increase POOLSIZE@>
  1204. for k:=1 to l do
  1205. begin if eoln(pool_file) then m:=' '@+else read(pool_file,m);
  1206. append_char(xord[m]);
  1207. end;
  1208. read_ln(pool_file); g:=make_string;
  1209. end;
  1210. end
  1211. @ The \.{WEB} operation \.{@@\$} denotes the value that should be at the
  1212. end of this \.{TEX.POOL} file; any other value means that the wrong pool
  1213. file has been loaded.
  1214. @^check sum@>
  1215. @<Check the pool check sum@>=
  1216. begin a:=0; k:=1;
  1217. loop@+ begin if (xord[n]<"0")or(xord[n]>"9") then
  1218. bad_pool('! TEX.POOL check sum doesn''t have nine digits.');
  1219. @.TEX.POOL check sum...@>
  1220. a:=10*a+xord[n]-"0";
  1221. if k=9 then goto done;
  1222. incr(k); read(pool_file,n);
  1223. end;
  1224. done: if a<>@$ then bad_pool('! TEX.POOL doesn''t match; TANGLE me again.');
  1225. @.TEX.POOL doesn't match@>
  1226. c:=true;
  1227. end
  1228. @* \[5] On-line and off-line printing.
  1229. Messages that are sent to a user's terminal and to the transcript-log file
  1230. are produced by several `|print|' procedures. These procedures will
  1231. direct their output to a variety of places, based on the setting of
  1232. the global variable |selector|, which has the following possible
  1233. values:
  1234. \yskip
  1235. \hang |term_and_log|, the normal setting, prints on the terminal and on the
  1236. transcript file.
  1237. \hang |log_only|, prints only on the transcript file.
  1238. \hang |term_only|, prints only on the terminal.
  1239. \hang |no_print|, doesn't print at all. This is used only in rare cases
  1240. before the transcript file is open.
  1241. \hang |pseudo|, puts output into a cyclic buffer that is used
  1242. by the |show_context| routine; when we get to that routine we shall discuss
  1243. the reasoning behind this curious mode.
  1244. \hang |new_string|, appends the output to the current string in the
  1245. string pool.
  1246. \hang 0 to 15, prints on one of the sixteen files for \.{\\write} output.
  1247. \yskip
  1248. \noindent The symbolic names `|term_and_log|', etc., have been assigned
  1249. numeric codes that satisfy the convenient relations |no_print+1=term_only|,
  1250. |no_print+2=log_only|, |term_only+2=log_only+1=term_and_log|.
  1251. Three additional global variables, |tally| and |term_offset| and
  1252. |file_offset|, record the number of characters that have been printed
  1253. since they were most recently cleared to zero. We use |tally| to record
  1254. the length of (possibly very long) stretches of printing; |term_offset|
  1255. and |file_offset|, on the other hand, keep track of how many characters
  1256. have appeared so far on the current line that has been output to the
  1257. terminal or to the transcript file, respectively.
  1258. @d no_print=16 {|selector| setting that makes data disappear}
  1259. @d term_only=17 {printing is destined for the terminal only}
  1260. @d log_only=18 {printing is destined for the transcript file only}
  1261. @d term_and_log=19 {normal |selector| setting}
  1262. @d pseudo=20 {special |selector| setting for |show_context|}
  1263. @d new_string=21 {printing is deflected to the string pool}
  1264. @d max_selector=21 {highest selector setting}
  1265. @<Glob...@>=
  1266. @!log_file : alpha_file; {transcript of \TeX\ session}
  1267. @!selector : 0..max_selector; {where to print a message}
  1268. @!dig : array[0..22] of 0..15; {digits in a number being output}
  1269. @!tally : integer; {the number of characters recently printed}
  1270. @!term_offset : 0..max_print_line;
  1271. {the number of characters on the current terminal line}
  1272. @!file_offset : 0..max_print_line;
  1273. {the number of characters on the current file line}
  1274. @!trick_buf:array[0..error_line] of ASCII_code; {circular buffer for
  1275. pseudoprinting}
  1276. @!trick_count: integer; {threshold for pseudoprinting, explained later}
  1277. @!first_count: integer; {another variable for pseudoprinting}
  1278. @ @<Initialize the output routines@>=
  1279. selector:=term_only; tally:=0; term_offset:=0; file_offset:=0;
  1280. @ Macro abbreviations for output to the terminal and to the log file are
  1281. defined here for convenience. Some systems need special conventions
  1282. for terminal output, and it is possible to adhere to those conventions
  1283. by changing |wterm|, |wterm_ln|, and |wterm_cr| in this section.
  1284. @^system dependencies@>
  1285. @d wterm(#)==write(term_out,#)
  1286. @d wterm_ln(#)==write_ln(term_out,#)
  1287. @d wterm_cr==write_ln(term_out)
  1288. @d wlog(#)==write(log_file,#)
  1289. @d wlog_ln(#)==write_ln(log_file,#)
  1290. @d wlog_cr==write_ln(log_file)
  1291. @ To end a line of text output, we call |print_ln|.
  1292. @<Basic print...@>=
  1293. procedure print_ln; {prints an end-of-line}
  1294. begin case selector of
  1295. term_and_log: begin wterm_cr; wlog_cr;
  1296. term_offset:=0; file_offset:=0;
  1297. end;
  1298. log_only: begin wlog_cr; file_offset:=0;
  1299. end;
  1300. term_only: begin wterm_cr; term_offset:=0;
  1301. end;
  1302. no_print,pseudo,new_string: do_nothing;
  1303. othercases write_ln(write_file[selector])
  1304. endcases;@/
  1305. end; {|tally| is not affected}
  1306. @ The |print_char| procedure sends one character to the desired destination,
  1307. using the |xchr| array to map it into an external character compatible with
  1308. |input_ln|. All printing comes through |print_ln| or |print_char|.
  1309. @<Basic printing...@>=
  1310. procedure print_char(@!s:ASCII_code); {prints a single character}
  1311. label exit;
  1312. begin if @<Character |s| is the current new-line character@> then
  1313. if selector<pseudo then
  1314. begin print_ln; return;
  1315. end;
  1316. case selector of
  1317. term_and_log: begin wterm(xchr[s]); wlog(xchr[s]);
  1318. incr(term_offset); incr(file_offset);
  1319. if term_offset=max_print_line then
  1320. begin wterm_cr; term_offset:=0;
  1321. end;
  1322. if file_offset=max_print_line then
  1323. begin wlog_cr; file_offset:=0;
  1324. end;
  1325. end;
  1326. log_only: begin wlog(xchr[s]); incr(file_offset);
  1327. if file_offset=max_print_line then print_ln;
  1328. end;
  1329. term_only: begin wterm(xchr[s]); incr(term_offset);
  1330. if term_offset=max_print_line then print_ln;
  1331. end;
  1332. no_print: do_nothing;
  1333. pseudo: if tally<trick_count then trick_buf[tally mod error_line]:=s;
  1334. new_string: begin if pool_ptr<pool_size then append_char(s);
  1335. end; {we drop characters if the string space is full}
  1336. othercases write(write_file[selector],xchr[s])
  1337. endcases;@/
  1338. incr(tally);
  1339. exit:end;
  1340. @ An entire string is output by calling |print|. Note that if we are outputting
  1341. the single standard ASCII character \.c, we could call |print("c")|, since
  1342. |"c"=99| is the number of a single-character string, as explained above. But
  1343. |print_char("c")| is quicker, so \TeX\ goes directly to the |print_char|
  1344. routine when it knows that this is safe. (The present implementation
  1345. assumes that it is always safe to print a visible ASCII character.)
  1346. @^system dependencies@>
  1347. @<Basic print...@>=
  1348. procedure print(@!s:integer); {prints string |s|}
  1349. label exit;
  1350. var j:pool_pointer; {current character code position}
  1351. @!nl:integer; {new-line character to restore}
  1352. begin if s>=str_ptr then s:="???" {this can't happen}
  1353. @.???@>
  1354. else if s<256 then
  1355. if s<0 then s:="???" {can't happen}
  1356. else begin if selector>pseudo then
  1357. begin print_char(s); return; {internal strings are not expanded}
  1358. end;
  1359. if (@<Character |s| is the current new-line character@>) then
  1360. if selector<pseudo then
  1361. begin print_ln; return;
  1362. end;
  1363. nl:=new_line_char; new_line_char:=-1;
  1364. {temporarily disable new-line character}
  1365. j:=str_start[s];
  1366. while j<str_start[s+1] do
  1367. begin print_char(so(str_pool[j])); incr(j);
  1368. end;
  1369. new_line_char:=nl; return;
  1370. end;
  1371. j:=str_start[s];
  1372. while j<str_start[s+1] do
  1373. begin print_char(so(str_pool[j])); incr(j);
  1374. end;
  1375. exit:end;
  1376. @ Control sequence names, file names, and strings constructed with
  1377. \.{\\string} might contain |ASCII_code| values that can't
  1378. be printed using |print_char|. Therefore we use |slow_print| for them:
  1379. @<Basic print...@>=
  1380. procedure slow_print(@!s:integer); {prints string |s|}
  1381. var j:pool_pointer; {current character code position}
  1382. begin if (s>=str_ptr) or (s<256) then print(s)
  1383. else begin j:=str_start[s];
  1384. while j<str_start[s+1] do
  1385. begin print(so(str_pool[j])); incr(j);
  1386. end;
  1387. end;
  1388. end;
  1389. @ Here is the very first thing that \TeX\ prints: a headline that identifies
  1390. the version number and format package. The |term_offset| variable is temporarily
  1391. incorrect, but the discrepancy is not serious since we assume that this
  1392. part of the program is system dependent.
  1393. @^system dependencies@>
  1394. @<Initialize the output...@>=
  1395. wterm(banner);
  1396. if format_ident=0 then wterm_ln(' (no format preloaded)')
  1397. else begin slow_print(format_ident); print_ln;
  1398. end;
  1399. update_terminal;
  1400. @ The procedure |print_nl| is like |print|, but it makes sure that the
  1401. string appears at the beginning of a new line.
  1402. @<Basic print...@>=
  1403. procedure print_nl(@!s:str_number); {prints string |s| at beginning of line}
  1404. begin if ((term_offset>0)and(odd(selector)))or@|
  1405. ((file_offset>0)and(selector>=log_only)) then print_ln;
  1406. print(s);
  1407. end;
  1408. @ The procedure |print_esc| prints a string that is preceded by
  1409. the user's escape character (which is usually a backslash).
  1410. @<Basic print...@>=
  1411. procedure print_esc(@!s:str_number); {prints escape character, then |s|}
  1412. var c:integer; {the escape character code}
  1413. begin @<Set variable |c| to the current escape character@>;
  1414. if c>=0 then if c<256 then print(c);
  1415. slow_print(s);
  1416. end;
  1417. @ An array of digits in the range |0..15| is printed by |print_the_digs|.
  1418. @<Basic print...@>=
  1419. procedure print_the_digs(@!k:eight_bits);
  1420. {prints |dig[k-1]|$\,\ldots\,$|dig[0]|}
  1421. begin while k>0 do
  1422. begin decr(k);
  1423. if dig[k]<10 then print_char("0"+dig[k])
  1424. else print_char("A"-10+dig[k]);
  1425. end;
  1426. end;
  1427. @ The following procedure, which prints out the decimal representation of a
  1428. given integer |n|, has been written carefully so that it works properly
  1429. if |n=0| or if |(-n)| would cause overflow. It does not apply |mod| or |div|
  1430. to negative arguments, since such operations are not implemented consistently
  1431. by all \PASCAL\ compilers.
  1432. @<Basic print...@>=
  1433. procedure print_int(@!n:integer); {prints an integer in decimal form}
  1434. var k:0..23; {index to current digit; we assume that $\vert n\vert<10^{23}$}
  1435. @!m:integer; {used to negate |n| in possibly dangerous cases}
  1436. begin k:=0;
  1437. if n<0 then
  1438. begin print_char("-");
  1439. if n>-100000000 then negate(n)
  1440. else begin m:=-1-n; n:=m div 10; m:=(m mod 10)+1; k:=1;
  1441. if m<10 then dig[0]:=m
  1442. else begin dig[0]:=0; incr(n);
  1443. end;
  1444. end;
  1445. end;
  1446. repeat dig[k]:=n mod 10; n:=n div 10; incr(k);
  1447. until n=0;
  1448. print_the_digs(k);
  1449. end;
  1450. @ Here is a trivial procedure to print two digits; it is usually called with
  1451. a parameter in the range |0<=n<=99|.
  1452. @p procedure print_two(@!n:integer); {prints two least significant digits}
  1453. begin n:=abs(n) mod 100; print_char("0"+(n div 10));
  1454. print_char("0"+(n mod 10));
  1455. end;
  1456. @ Hexadecimal printing of nonnegative integers is accomplished by |print_hex|.
  1457. @p procedure print_hex(@!n:integer);
  1458. {prints a positive integer in hexadecimal form}
  1459. var k:0..22; {index to current digit; we assume that $0\L n<16^{22}$}
  1460. begin k:=0; print_char("""");
  1461. repeat dig[k]:=n mod 16; n:=n div 16; incr(k);
  1462. until n=0;
  1463. print_the_digs(k);
  1464. end;
  1465. @ Old versions of \TeX\ needed a procedure called |print_ASCII| whose function
  1466. is now subsumed by |print|. We retain the old name here as a possible aid to
  1467. future software arch\ae ologists.
  1468. @d print_ASCII == print
  1469. @ Roman numerals are produced by the |print_roman_int| routine. Readers
  1470. who like puzzles might enjoy trying to figure out how this tricky code
  1471. works; therefore no explanation will be given. Notice that 1990 yields
  1472. \.{mcmxc}, not \.{mxm}.
  1473. @p procedure print_roman_int(@!n:integer);
  1474. label exit;
  1475. var j,@!k: pool_pointer; {mysterious indices into |str_pool|}
  1476. @!u,@!v: nonnegative_integer; {mysterious numbers}
  1477. begin j:=str_start["m2d5c2l5x2v5i"]; v:=1000;
  1478. loop@+ begin while n>=v do
  1479. begin print_char(so(str_pool[j])); n:=n-v;
  1480. end;
  1481. if n<=0 then return; {nonpositive input produces no output}
  1482. k:=j+2; u:=v div (so(str_pool[k-1])-"0");
  1483. if str_pool[k-1]=si("2") then
  1484. begin k:=k+2; u:=u div (so(str_pool[k-1])-"0");
  1485. end;
  1486. if n+u>=v then
  1487. begin print_char(so(str_pool[k])); n:=n+u;
  1488. end
  1489. else begin j:=j+2; v:=v div (so(str_pool[j-1])-"0");
  1490. end;
  1491. end;
  1492. exit:end;
  1493. @ The |print| subroutine will not print a string that is still being
  1494. created. The following procedure will.
  1495. @p procedure print_current_string; {prints a yet-unmade string}
  1496. var j:pool_pointer; {points to current character code}
  1497. begin j:=str_start[str_ptr];
  1498. while j<pool_ptr do
  1499. begin print_char(so(str_pool[j])); incr(j);
  1500. end;
  1501. end;
  1502. @ Here is a procedure that asks the user to type a line of input,
  1503. assuming that the |selector| setting is either |term_only| or |term_and_log|.
  1504. The input is placed into locations |first| through |last-1| of the
  1505. |buffer| array, and echoed on the transcript file if appropriate.
  1506. This procedure is never called when |interaction<scroll_mode|.
  1507. @d prompt_input(#)==begin wake_up_terminal; print(#); term_input;
  1508. end {prints a string and gets a line of input}
  1509. @p procedure term_input; {gets a line from the terminal}
  1510. var k:0..buf_size; {index into |buffer|}
  1511. begin update_terminal; {now the user sees the prompt for sure}
  1512. if not input_ln(term_in,true) then fatal_error("End of file on the terminal!");
  1513. @.End of file on the terminal@>
  1514. term_offset:=0; {the user's line ended with \<\rm return>}
  1515. decr(selector); {prepare to echo the input}
  1516. if last<>first then for k:=first to last-1 do print(buffer[k]);
  1517. print_ln; incr(selector); {restore previous status}
  1518. end;
  1519. @* \[6] Reporting errors.
  1520. When something anomalous is detected, \TeX\ typically does something like this:
  1521. $$\vbox{\halign{#\hfil\cr
  1522. |print_err("Something anomalous has been detected");|\cr
  1523. |help3("This is the first line of my offer to help.")|\cr
  1524. |("This is the second line. I'm trying to")|\cr
  1525. |("explain the best way for you to proceed.");|\cr
  1526. |error;|\cr}}$$
  1527. A two-line help message would be given using |help2|, etc.; these informal
  1528. helps should use simple vocabulary that complements the words used in the
  1529. official error message that was printed. (Outside the U.S.A., the help
  1530. messages should preferably be translated into the local vernacular. Each
  1531. line of help is at most 60 characters long, in the present implementation,
  1532. so that |max_print_line| will not be exceeded.)
  1533. The |print_err| procedure supplies a `\.!' before the official message,
  1534. and makes sure that the terminal is awake if a stop is going to occur.
  1535. The |error| procedure supplies a `\..' after the official message, then it
  1536. shows the location of the error; and if |interaction=error_stop_mode|,
  1537. it also enters into a dialog with the user, during which time the help
  1538. message may be printed.
  1539. @^system dependencies@>
  1540. @ The global variable |interaction| has four settings, representing increasing
  1541. amounts of user interaction:
  1542. @d batch_mode=0 {omits all stops and omits terminal output}
  1543. @d nonstop_mode=1 {omits all stops}
  1544. @d scroll_mode=2 {omits error stops}
  1545. @d error_stop_mode=3 {stops at every opportunity to interact}
  1546. @d print_err(#)==begin if interaction=error_stop_mode then wake_up_terminal;
  1547. print_nl("! "); print(#);
  1548. end
  1549. @<Glob...@>=
  1550. @!interaction:batch_mode..error_stop_mode; {current level of interaction}
  1551. @ @<Set init...@>=interaction:=error_stop_mode;
  1552. @ \TeX\ is careful not to call |error| when the print |selector| setting
  1553. might be unusual. The only possible values of |selector| at the time of
  1554. error messages are
  1555. \yskip\hang|no_print| (when |interaction=batch_mode|
  1556. and |log_file| not yet open);
  1557. \hang|term_only| (when |interaction>batch_mode| and |log_file| not yet open);
  1558. \hang|log_only| (when |interaction=batch_mode| and |log_file| is open);
  1559. \hang|term_and_log| (when |interaction>batch_mode| and |log_file| is open).
  1560. @<Initialize the print |selector| based on |interaction|@>=
  1561. if interaction=batch_mode then selector:=no_print@+else selector:=term_only
  1562. @ A global variable |deletions_allowed| is set |false| if the |get_next|
  1563. routine is active when |error| is called; this ensures that |get_next|
  1564. and related routines like |get_token| will never be called recursively.
  1565. A similar interlock is provided by |set_box_allowed|.
  1566. @^recursion@>
  1567. The global variable |history| records the worst level of error that
  1568. has been detected. It has four possible values: |spotless|, |warning_issued|,
  1569. |error_message_issued|, and |fatal_error_stop|.
  1570. Another global variable, |error_count|, is increased by one when an
  1571. |error| occurs without an interactive dialog, and it is reset to zero at
  1572. the end of every paragraph. If |error_count| reaches 100, \TeX\ decides
  1573. that there is no point in continuing further.
  1574. @d spotless=0 {|history| value when nothing has been amiss yet}
  1575. @d warning_issued=1 {|history| value when |begin_diagnostic| has been called}
  1576. @d error_message_issued=2 {|history| value when |error| has been called}
  1577. @d fatal_error_stop=3 {|history| value when termination was premature}
  1578. @<Glob...@>=
  1579. @!deletions_allowed:boolean; {is it safe for |error| to call |get_token|?}
  1580. @!set_box_allowed:boolean; {is it safe to do a \.{\\setbox} assignment?}
  1581. @!history:spotless..fatal_error_stop; {has the source input been clean so far?}
  1582. @!error_count:-1..100; {the number of scrolled errors since the
  1583. last paragraph ended}
  1584. @ The value of |history| is initially |fatal_error_stop|, but it will
  1585. be changed to |spotless| if \TeX\ survives the initialization process.
  1586. @<Set init...@>=
  1587. deletions_allowed:=true; set_box_allowed:=true;
  1588. error_count:=0; {|history| is initialized elsewhere}
  1589. @ Since errors can be detected almost anywhere in \TeX, we want to declare the
  1590. error procedures near the beginning of the program. But the error procedures
  1591. in turn use some other procedures, which need to be declared |forward|
  1592. before we get to |error| itself.
  1593. It is possible for |error| to be called recursively if some error arises
  1594. when |get_token| is being used to delete a token, and/or if some fatal error
  1595. occurs while \TeX\ is trying to fix a non-fatal one. But such recursion
  1596. @^recursion@>
  1597. is never more than two levels deep.
  1598. @<Error handling...@>=
  1599. procedure@?normalize_selector; forward;@t\2@>@/
  1600. procedure@?get_token; forward;@t\2@>@/
  1601. procedure@?term_input; forward;@t\2@>@/
  1602. procedure@?show_context; forward;@t\2@>@/
  1603. procedure@?begin_file_reading; forward;@t\2@>@/
  1604. procedure@?open_log_file; forward;@t\2@>@/
  1605. procedure@?close_files_and_terminate; forward;@t\2@>@/
  1606. procedure@?clear_for_error_prompt; forward;@t\2@>@/
  1607. procedure@?give_err_help; forward;@t\2@>@/
  1608. @t\4\hskip-\fontdimen2\font@>@;@+@!debug@+procedure@?debug_help;
  1609. forward;@;@+gubed
  1610. @ Individual lines of help are recorded in the array |help_line|, which
  1611. contains entries in positions |0..(help_ptr-1)|. They should be printed
  1612. in reverse order, i.e., with |help_line[0]| appearing last.
  1613. @d hlp1(#)==help_line[0]:=#;@+end
  1614. @d hlp2(#)==help_line[1]:=#; hlp1
  1615. @d hlp3(#)==help_line[2]:=#; hlp2
  1616. @d hlp4(#)==help_line[3]:=#; hlp3
  1617. @d hlp5(#)==help_line[4]:=#; hlp4
  1618. @d hlp6(#)==help_line[5]:=#; hlp5
  1619. @d help0==help_ptr:=0 {sometimes there might be no help}
  1620. @d help1==@+begin help_ptr:=1; hlp1 {use this with one help line}
  1621. @d help2==@+begin help_ptr:=2; hlp2 {use this with two help lines}
  1622. @d help3==@+begin help_ptr:=3; hlp3 {use this with three help lines}
  1623. @d help4==@+begin help_ptr:=4; hlp4 {use this with four help lines}
  1624. @d help5==@+begin help_ptr:=5; hlp5 {use this with five help lines}
  1625. @d help6==@+begin help_ptr:=6; hlp6 {use this with six help lines}
  1626. @<Glob...@>=
  1627. @!help_line:array[0..5] of str_number; {helps for the next |error|}
  1628. @!help_ptr:0..6; {the number of help lines present}
  1629. @!use_err_help:boolean; {should the |err_help| list be shown?}
  1630. @ @<Set init...@>=
  1631. help_ptr:=0; use_err_help:=false;
  1632. @ The |jump_out| procedure just cuts across all active procedure levels and
  1633. goes to |end_of_TEX|. This is the only nontrivial |@!goto| statement in the
  1634. whole program. It is used when there is no recovery from a particular error.
  1635. Some \PASCAL\ compilers do not implement non-local |goto| statements.
  1636. @^system dependencies@>
  1637. In such cases the body of |jump_out| should simply be
  1638. `|close_files_and_terminate|;\thinspace' followed by a call on some system
  1639. procedure that quietly terminates the program.
  1640. @<Error hand...@>=
  1641. procedure jump_out;
  1642. begin goto end_of_TEX;
  1643. end;
  1644. @ Here now is the general |error| routine.
  1645. @<Error hand...@>=
  1646. procedure error; {completes the job of error reporting}
  1647. label continue,exit;
  1648. var c:ASCII_code; {what the user types}
  1649. @!s1,@!s2,@!s3,@!s4:integer;
  1650. {used to save global variables when deleting tokens}
  1651. begin if history<error_message_issued then history:=error_message_issued;
  1652. print_char("."); show_context;
  1653. if interaction=error_stop_mode then
  1654. @<Get user's advice and |return|@>;
  1655. incr(error_count);
  1656. if error_count=100 then
  1657. begin print_nl("(That makes 100 errors; please try again.)");
  1658. @.That makes 100 errors...@>
  1659. history:=fatal_error_stop; jump_out;
  1660. end;
  1661. @<Put help message on the transcript file@>;
  1662. exit:end;
  1663. @ @<Get user's advice...@>=
  1664. loop@+begin continue: if interaction<>error_stop_mode then return;
  1665. clear_for_error_prompt; prompt_input("? ");
  1666. @.?\relax@>
  1667. if last=first then return;
  1668. c:=buffer[first];
  1669. if c>="a" then c:=c+"A"-"a"; {convert to uppercase}
  1670. @<Interpret code |c| and |return| if done@>;
  1671. end
  1672. @ It is desirable to provide an `\.E' option here that gives the user
  1673. an easy way to return from \TeX\ to the system editor, with the offending
  1674. line ready to be edited. But such an extension requires some system
  1675. wizardry, so the present implementation simply types out the name of the
  1676. file that should be
  1677. edited and the relevant line number.
  1678. @^system dependencies@>
  1679. There is a secret `\.D' option available when the debugging routines haven't
  1680. been commented~out.
  1681. @^debugging@>
  1682. @<Interpret code |c| and |return| if done@>=
  1683. case c of
  1684. "0","1","2","3","4","5","6","7","8","9": if deletions_allowed then
  1685. @<Delete \(c)|c-"0"| tokens and |goto continue|@>;
  1686. @t\4\4@>@;@+@!debug "D": begin debug_help; goto continue;@+end;@+gubed@/
  1687. "E": if base_ptr>0 then if input_stack[base_ptr].name_field>=256 then
  1688. begin print_nl("You want to edit file ");
  1689. @.You want to edit file x@>
  1690. slow_print(input_stack[base_ptr].name_field);
  1691. print(" at line "); print_int(line);
  1692. interaction:=scroll_mode; jump_out;
  1693. end;
  1694. "H": @<Print the help information and |goto continue|@>;
  1695. "I":@<Introduce new material from the terminal and |return|@>;
  1696. "Q","R","S":@<Change the interaction level and |return|@>;
  1697. "X":begin interaction:=scroll_mode; jump_out;
  1698. end;
  1699. othercases do_nothing
  1700. endcases;@/
  1701. @<Print the menu of available options@>
  1702. @ @<Print the menu...@>=
  1703. begin print("Type <return> to proceed, S to scroll future error messages,");@/
  1704. @.Type <return> to proceed...@>
  1705. print_nl("R to run without stopping, Q to run quietly,");@/
  1706. print_nl("I to insert something, ");
  1707. if base_ptr>0 then if input_stack[base_ptr].name_field>=256 then
  1708. print("E to edit your file,");
  1709. if deletions_allowed then
  1710. print_nl("1 or ... or 9 to ignore the next 1 to 9 tokens of input,");
  1711. print_nl("H for help, X to quit.");
  1712. end
  1713. @ Here the author of \TeX\ apologizes for making use of the numerical
  1714. relation between |"Q"|, |"R"|, |"S"|, and the desired interaction settings
  1715. |batch_mode|, |nonstop_mode|, |scroll_mode|.
  1716. @^Knuth, Donald Ervin@>
  1717. @<Change the interaction...@>=
  1718. begin error_count:=0; interaction:=batch_mode+c-"Q";
  1719. print("OK, entering ");
  1720. case c of
  1721. "Q":begin print_esc("batchmode"); decr(selector);
  1722. end;
  1723. "R":print_esc("nonstopmode");
  1724. "S":print_esc("scrollmode");
  1725. end; {there are no other cases}
  1726. print("..."); print_ln; update_terminal; return;
  1727. end
  1728. @ When the following code is executed, |buffer[(first+1)..(last-1)]| may
  1729. contain the material inserted by the user; otherwise another prompt will
  1730. be given. In order to understand this part of the program fully, you need
  1731. to be familiar with \TeX's input stacks.
  1732. @<Introduce new material...@>=
  1733. begin begin_file_reading; {enter a new syntactic level for terminal input}
  1734. {now |state=mid_line|, so an initial blank space will count as a blank}
  1735. if last>first+1 then
  1736. begin loc:=first+1; buffer[first]:=" ";
  1737. end
  1738. else begin prompt_input("insert>"); loc:=first;
  1739. @.insert>@>
  1740. end;
  1741. first:=last;
  1742. cur_input.limit_field:=last-1; {no |end_line_char| ends this line}
  1743. return;
  1744. end
  1745. @ We allow deletion of up to 99 tokens at a time.
  1746. @<Delete \(c)|c-"0"| tokens...@>=
  1747. begin s1:=cur_tok; s2:=cur_cmd; s3:=cur_chr; s4:=align_state;
  1748. align_state:=1000000; OK_to_interrupt:=false;
  1749. if (last>first+1) and (buffer[first+1]>="0")and(buffer[first+1]<="9") then
  1750. c:=c*10+buffer[first+1]-"0"*11
  1751. else c:=c-"0";
  1752. while c>0 do
  1753. begin get_token; {one-level recursive call of |error| is possible}
  1754. decr(c);
  1755. end;
  1756. cur_tok:=s1; cur_cmd:=s2; cur_chr:=s3; align_state:=s4; OK_to_interrupt:=true;
  1757. help2("I have just deleted some text, as you asked.")@/
  1758. ("You can now delete more, or insert, or whatever.");
  1759. show_context; goto continue;
  1760. end
  1761. @ @<Print the help info...@>=
  1762. begin if use_err_help then
  1763. begin give_err_help; use_err_help:=false;
  1764. end
  1765. else begin if help_ptr=0 then
  1766. help2("Sorry, I don't know how to help in this situation.")@/
  1767. @t\kern1em@>("Maybe you should try asking a human?");
  1768. repeat decr(help_ptr); print(help_line[help_ptr]); print_ln;
  1769. until help_ptr=0;
  1770. end;
  1771. help4("Sorry, I already gave what help I could...")@/
  1772. ("Maybe you should try asking a human?")@/
  1773. ("An error might have occurred before I noticed any problems.")@/
  1774. ("``If all else fails, read the instructions.''");@/
  1775. goto continue;
  1776. end
  1777. @ @<Put help message on the transcript file@>=
  1778. if interaction>batch_mode then decr(selector); {avoid terminal output}
  1779. if use_err_help then
  1780. begin print_ln; give_err_help;
  1781. end
  1782. else while help_ptr>0 do
  1783. begin decr(help_ptr); print_nl(help_line[help_ptr]);
  1784. end;
  1785. print_ln;
  1786. if interaction>batch_mode then incr(selector); {re-enable terminal output}
  1787. print_ln
  1788. @ A dozen or so error messages end with a parenthesized integer, so we
  1789. save a teeny bit of program space by declaring the following procedure:
  1790. @p procedure int_error(@!n:integer);
  1791. begin print(" ("); print_int(n); print_char(")"); error;
  1792. end;
  1793. @ In anomalous cases, the print selector might be in an unknown state;
  1794. the following subroutine is called to fix things just enough to keep
  1795. running a bit longer.
  1796. @p procedure normalize_selector;
  1797. begin if log_opened then selector:=term_and_log
  1798. else selector:=term_only;
  1799. if job_name=0 then open_log_file;
  1800. if interaction=batch_mode then decr(selector);
  1801. end;
  1802. @ The following procedure prints \TeX's last words before dying.
  1803. @d succumb==begin if interaction=error_stop_mode then
  1804. interaction:=scroll_mode; {no more interaction}
  1805. if log_opened then error;
  1806. @!debug if interaction>batch_mode then debug_help;@+gubed@;@/
  1807. history:=fatal_error_stop; jump_out; {irrecoverable error}
  1808. end
  1809. @<Error hand...@>=
  1810. procedure fatal_error(@!s:str_number); {prints |s|, and that's it}
  1811. begin normalize_selector;@/
  1812. print_err("Emergency stop"); help1(s); succumb;
  1813. @.Emergency stop@>
  1814. end;
  1815. @ Here is the most dreaded error message.
  1816. @<Error hand...@>=
  1817. procedure overflow(@!s:str_number;@!n:integer); {stop due to finiteness}
  1818. begin normalize_selector;
  1819. print_err("TeX capacity exceeded, sorry [");
  1820. @.TeX capacity exceeded ...@>
  1821. print(s); print_char("="); print_int(n); print_char("]");
  1822. help2("If you really absolutely need more capacity,")@/
  1823. ("you can ask a wizard to enlarge me.");
  1824. succumb;
  1825. end;
  1826. @ The program might sometime run completely amok, at which point there is
  1827. no choice but to stop. If no previous error has been detected, that's bad
  1828. news; a message is printed that is really intended for the \TeX\
  1829. maintenance person instead of the user (unless the user has been
  1830. particularly diabolical). The index entries for `this can't happen' may
  1831. help to pinpoint the problem.
  1832. @^dry rot@>
  1833. @<Error hand...@>=
  1834. procedure confusion(@!s:str_number);
  1835. {consistency check violated; |s| tells where}
  1836. begin normalize_selector;
  1837. if history<error_message_issued then
  1838. begin print_err("This can't happen ("); print(s); print_char(")");
  1839. @.This can't happen@>
  1840. help1("I'm broken. Please show this to someone who can fix can fix");
  1841. end
  1842. else begin print_err("I can't go on meeting you like this");
  1843. @.I can't go on...@>
  1844. help2("One of your faux pas seems to have wounded me deeply...")@/
  1845. ("in fact, I'm barely conscious. Please fix it and try again.");
  1846. end;
  1847. succumb;
  1848. end;
  1849. @ Users occasionally want to interrupt \TeX\ while it's running.
  1850. If the \PASCAL\ runtime system allows this, one can implement
  1851. a routine that sets the global variable |interrupt| to some nonzero value
  1852. when such an interrupt is signalled. Otherwise there is probably at least
  1853. a way to make |interrupt| nonzero using the \PASCAL\ debugger.
  1854. @^system dependencies@>
  1855. @^debugging@>
  1856. @d check_interrupt==begin if interrupt<>0 then pause_for_instructions;
  1857. end
  1858. @<Global...@>=
  1859. @!interrupt:integer; {should \TeX\ pause for instructions?}
  1860. @!OK_to_interrupt:boolean; {should interrupts be observed?}
  1861. @ @<Set init...@>=
  1862. interrupt:=0; OK_to_interrupt:=true;
  1863. @ When an interrupt has been detected, the program goes into its
  1864. highest interaction level and lets the user have nearly the full flexibility of
  1865. the |error| routine. \TeX\ checks for interrupts only at times when it is
  1866. safe to do this.
  1867. @p procedure pause_for_instructions;
  1868. begin if OK_to_interrupt then
  1869. begin interaction:=error_stop_mode;
  1870. if (selector=log_only)or(selector=no_print) then
  1871. incr(selector);
  1872. print_err("Interruption");
  1873. @.Interruption@>
  1874. help3("You rang?")@/
  1875. ("Try to insert an instruction for me (e.g., `I\showlists'),")@/
  1876. ("unless you just want to quit by typing `X'.");
  1877. deletions_allowed:=false; error; deletions_allowed:=true;
  1878. interrupt:=0;
  1879. end;
  1880. end;
  1881. @* \[7] Arithmetic with scaled dimensions.
  1882. The principal computations performed by \TeX\ are done entirely in terms of
  1883. integers less than $2^{31}$ in magnitude; and divisions are done only when both
  1884. dividend and divisor are nonnegative. Thus, the arithmetic specified in this
  1885. program can be carried out in exactly the same way on a wide variety of
  1886. computers, including some small ones. Why? Because the arithmetic
  1887. calculations need to be spelled out precisely in order to guarantee that
  1888. \TeX\ will produce identical output on different machines. If some
  1889. quantities were rounded differently in different implementations, we would
  1890. find that line breaks and even page breaks might occur in different places.
  1891. Hence the arithmetic of \TeX\ has been designed with care, and systems that
  1892. claim to be implementations of \TeX82 should follow precisely the
  1893. @:TeX82}{\TeX82@>
  1894. calculations as they appear in the present program.
  1895. (Actually there are three places where \TeX\ uses |div| with a possibly negative
  1896. numerator. These are harmless; see |div| in the index. Also if the user
  1897. sets the \.{\\time} or the \.{\\year} to a negative value, some diagnostic
  1898. information will involve negative-numerator division. The same remarks
  1899. apply for |mod| as well as for |div|.)
  1900. @ Here is a routine that calculates half of an integer, using an
  1901. unambiguous convention with respect to signed odd numbers.
  1902. @p function half(@!x:integer):integer;
  1903. begin if odd(x) then half:=(x+1) div 2
  1904. else half:=x @!div 2;
  1905. end;
  1906. @ Fixed-point arithmetic is done on {\sl scaled integers\/} that are multiples
  1907. of $2^{-16}$. In other words, a binary point is assumed to be sixteen bit
  1908. positions from the right end of a binary computer word.
  1909. @d unity == @'200000 {$2^{16}$, represents 1.00000}
  1910. @d two == @'400000 {$2^{17}$, represents 2.00000}
  1911. @<Types...@>=
  1912. @!scaled = integer; {this type is used for scaled integers}
  1913. @!nonnegative_integer=0..@'17777777777; {$0\L x<2^{31}$}
  1914. @!small_number=0..63; {this type is self-explanatory}
  1915. @ The following function is used to create a scaled integer from a given decimal
  1916. fraction $(.d_0d_1\ldots d_{k-1})$, where |0<=k<=17|. The digit $d_i$ is
  1917. given in |dig[i]|, and the calculation produces a correctly rounded result.
  1918. @p function round_decimals(@!k:small_number) : scaled;
  1919. {converts a decimal fraction}
  1920. var a:integer; {the accumulator}
  1921. begin a:=0;
  1922. while k>0 do
  1923. begin decr(k); a:=(a+dig[k]*two) div 10;
  1924. end;
  1925. round_decimals:=(a+1) div 2;
  1926. end;
  1927. @ Conversely, here is a procedure analogous to |print_int|. If the output
  1928. of this procedure is subsequently read by \TeX\ and converted by the
  1929. |round_decimals| routine above, it turns out that the original value will
  1930. be reproduced exactly; the ``simplest'' such decimal number is output,
  1931. but there is always at least one digit following the decimal point.
  1932. The invariant relation in the \&{repeat} loop is that a sequence of
  1933. decimal digits yet to be printed will yield the original number if and only if
  1934. they form a fraction~$f$ in the range $s-\delta\L10\cdot2^{16}f<s$.
  1935. We can stop if and only if $f=0$ satisfies this condition; the loop will
  1936. terminate before $s$ can possibly become zero.
  1937. @p procedure print_scaled(@!s:scaled); {prints scaled real, rounded to five
  1938. digits}
  1939. var delta:scaled; {amount of allowable inaccuracy}
  1940. begin if s<0 then
  1941. begin print_char("-"); negate(s); {print the sign, if negative}
  1942. end;
  1943. print_int(s div unity); {print the integer part}
  1944. print_char(".");
  1945. s:=10*(s mod unity)+5; delta:=10;
  1946. repeat if delta>unity then s:=s+@'100000-50000; {round the last digit}
  1947. print_char("0"+(s div unity)); s:=10*(s mod unity); delta:=delta*10;
  1948. until s<=delta;
  1949. end;
  1950. @ Physical sizes that a \TeX\ user specifies for portions of documents are
  1951. represented internally as scaled points. Thus, if we define an `sp' (scaled
  1952. @^sp@>
  1953. point) as a unit equal to $2^{-16}$ printer's points, every dimension
  1954. inside of \TeX\ is an integer number of sp. There are exactly
  1955. 4,736,286.72 sp per inch. Users are not allowed to specify dimensions
  1956. larger than $2^{30}-1$ sp, which is a distance of about 18.892 feet (5.7583
  1957. meters); two such quantities can be added without overflow on a 32-bit
  1958. computer.
  1959. The present implementation of \TeX\ does not check for overflow when
  1960. @^overflow in arithmetic@>
  1961. dimensions are added or subtracted. This could be done by inserting a
  1962. few dozen tests of the form `\ignorespaces|if x>=@'10000000000 then
  1963. @t\\{report\_overflow}@>|', but the chance of overflow is so remote that
  1964. such tests do not seem worthwhile.
  1965. \TeX\ needs to do only a few arithmetic operations on scaled quantities,
  1966. other than addition and subtraction, and the following subroutines do most of
  1967. the work. A single computation might use several subroutine calls, and it is
  1968. desirable to avoid producing multiple error messages in case of arithmetic
  1969. overflow; so the routines set the global variable |arith_error| to |true|
  1970. instead of reporting errors directly to the user. Another global variable,
  1971. |remainder|, holds the remainder after a division.
  1972. @<Glob...@>=
  1973. @!arith_error:boolean; {has arithmetic overflow occurred recently?}
  1974. @!remainder:scaled; {amount subtracted to get an exact division}
  1975. @ The first arithmetical subroutine we need computes $nx+y$, where |x|
  1976. and~|y| are |scaled| and |n| is an integer. We will also use it to
  1977. multiply integers.
  1978. @d nx_plus_y(#)==mult_and_add(#,@'7777777777)
  1979. @d mult_integers(#)==mult_and_add(#,0,@'17777777777)
  1980. @p function mult_and_add(@!n:integer;@!x,@!y,@!max_answer:scaled):scaled;
  1981. begin if n<0 then
  1982. begin negate(x); negate(n);
  1983. end;
  1984. if n=0 then mult_and_add:=y
  1985. else if ((x<=(max_answer-y) div n)and(-x<=(max_answer+y) div n)) then
  1986. mult_and_add:=n*x+y
  1987. else begin arith_error:=true; mult_and_add:=0;
  1988. end;
  1989. end;
  1990. @ We also need to divide scaled dimensions by integers.
  1991. @p function x_over_n(@!x:scaled;@!n:integer):scaled;
  1992. var negative:boolean; {should |remainder| be negated?}
  1993. begin negative:=false;
  1994. if n=0 then
  1995. begin arith_error:=true; x_over_n:=0; remainder:=x;
  1996. end
  1997. else begin if n<0 then
  1998. begin negate(x); negate(n); negative:=true;
  1999. end;
  2000. if x>=0 then
  2001. begin x_over_n:=x div n; remainder:=x mod n;
  2002. end
  2003. else begin x_over_n:=-((-x) div n); remainder:=-((-x) mod n);
  2004. end;
  2005. end;
  2006. if negative then negate(remainder);
  2007. end;
  2008. @ Then comes the multiplication of a scaled number by a fraction |n/d|,
  2009. where |n| and |d| are nonnegative integers |<=@t$2^{16}$@>| and |d| is
  2010. positive. It would be too dangerous to multiply by~|n| and then divide
  2011. by~|d|, in separate operations, since overflow might well occur; and it
  2012. would be too inaccurate to divide by |d| and then multiply by |n|. Hence
  2013. this subroutine simulates 1.5-precision arithmetic.
  2014. @p function xn_over_d(@!x:scaled; @!n,@!d:integer):scaled;
  2015. var positive:boolean; {was |x>=0|?}
  2016. @!t,@!u,@!v:nonnegative_integer; {intermediate quantities}
  2017. begin if x>=0 then positive:=true
  2018. else begin negate(x); positive:=false;
  2019. end;
  2020. t:=(x mod @'100000)*n;
  2021. u:=(x div @'100000)*n+(t div @'100000);
  2022. v:=(u mod d)*@'100000 + (t mod @'100000);
  2023. if u div d>=@'100000 then arith_error:=true
  2024. else u:=@'100000*(u div d) + (v div d);
  2025. if positive then
  2026. begin xn_over_d:=u; remainder:=v mod d;
  2027. end
  2028. else begin xn_over_d:=-u; remainder:=-(v mod d);
  2029. end;
  2030. end;
  2031. @ The next subroutine is used to compute the ``badness'' of glue, when a
  2032. total~|t| is supposed to be made from amounts that sum to~|s|. According
  2033. to {\sl The \TeX book}, the badness of this situation is $100(t/s)^3$;
  2034. however, badness is simply a heuristic, so we need not squeeze out the
  2035. last drop of accuracy when computing it. All we really want is an
  2036. approximation that has similar properties.
  2037. @:TeXbook}{\sl The \TeX book@>
  2038. The actual method used to compute the badness is easier to read from the
  2039. program than to describe in words. It produces an integer value that is a
  2040. reasonably close approximation to $100(t/s)^3$, and all implementations
  2041. of \TeX\ should use precisely this method. Any badness of $2^{13}$ or more is
  2042. treated as infinitely bad, and represented by 10000.
  2043. It is not difficult to prove that $$\hbox{|badness(t+1,s)>=badness(t,s)
  2044. >=badness(t,s+1)|}.$$ The badness function defined here is capable of
  2045. computing at most 1095 distinct values, but that is plenty.
  2046. @d inf_bad = 10000 {infinitely bad value}
  2047. @p function badness(@!t,@!s:scaled):halfword; {compute badness, given |t>=0|}
  2048. var r:integer; {approximation to $\alpha t/s$, where $\alpha^3\approx
  2049. 100\cdot2^{18}$}
  2050. begin if t=0 then badness:=0
  2051. else if s<=0 then badness:=inf_bad
  2052. else begin if t<=7230584 then r:=(t*297) div s {$297^3=99.94\times2^{18}$}
  2053. else if s>=1663497 then r:=t div (s div 297)
  2054. else r:=t;
  2055. if r>1290 then badness:=inf_bad {$1290^3<2^{31}<1291^3$}
  2056. else badness:=(r*r*r+@'400000) div @'1000000;
  2057. end; {that was $r^3/2^{18}$, rounded to the nearest integer}
  2058. end;
  2059. @ When \TeX\ ``packages'' a list into a box, it needs to calculate the
  2060. proportionality ratio by which the glue inside the box should stretch
  2061. or shrink. This calculation does not affect \TeX's decision making,
  2062. so the precise details of rounding, etc., in the glue calculation are not
  2063. of critical importance for the consistency of results on different computers.
  2064. We shall use the type |glue_ratio| for such proportionality ratios.
  2065. A glue ratio should take the same amount of memory as an
  2066. |integer| (usually 32 bits) if it is to blend smoothly with \TeX's
  2067. other data structures. Thus |glue_ratio| should be equivalent to
  2068. |short_real| in some implementations of \PASCAL. Alternatively,
  2069. it is possible to deal with glue ratios using nothing but fixed-point
  2070. arithmetic; see {\sl TUGboat \bf3},1 (March 1982), 10--27. (But the
  2071. routines cited there must be modified to allow negative glue ratios.)
  2072. @^system dependencies@>
  2073. @d set_glue_ratio_zero(#) == #:=0.0 {store the representation of zero ratio}
  2074. @d set_glue_ratio_one(#) == #:=1.0 {store the representation of unit ratio}
  2075. @d float(#) == # {convert from |glue_ratio| to type |real|}
  2076. @d unfloat(#) == # {convert from |real| to type |glue_ratio|}
  2077. @d float_constant(#) == #.0 {convert |integer| constant to |real|}
  2078. @<Types...@>=
  2079. @!glue_ratio=real; {one-word representation of a glue expansion factor}
  2080. @* \[8] Packed data.
  2081. In order to make efficient use of storage space, \TeX\ bases its major data
  2082. structures on a |memory_word|, which contains either a (signed) integer,
  2083. possibly scaled, or a (signed) |glue_ratio|, or a small number of
  2084. fields that are one half or one quarter of the size used for storing
  2085. integers.
  2086. If |x| is a variable of type |memory_word|, it contains up to four
  2087. fields that can be referred to as follows:
  2088. $$\vbox{\halign{\hfil#&#\hfil&#\hfil\cr
  2089. |x|&.|int|&(an |integer|)\cr
  2090. |x|&.|sc|\qquad&(a |scaled| integer)\cr
  2091. |x|&.|gr|&(a |glue_ratio|)\cr
  2092. |x.hh.lh|, |x.hh|&.|rh|&(two halfword fields)\cr
  2093. |x.hh.b0|, |x.hh.b1|, |x.hh|&.|rh|&(two quarterword fields, one halfword
  2094. field)\cr
  2095. |x.qqqq.b0|, |x.qqqq.b1|, |x.qqqq|&.|b2|, |x.qqqq.b3|\hskip-100pt
  2096. &\qquad\qquad\qquad(four quarterword fields)\cr}}$$
  2097. This is somewhat cumbersome to write, and not very readable either, but
  2098. macros will be used to make the notation shorter and more transparent.
  2099. The \PASCAL\ code below gives a formal definition of |memory_word| and
  2100. its subsidiary types, using packed variant records. \TeX\ makes no
  2101. assumptions about the relative positions of the fields within a word.
  2102. Since we are assuming 32-bit integers, a halfword must contain at least
  2103. 16 bits, and a quarterword must contain at least 8 bits.
  2104. @^system dependencies@>
  2105. But it doesn't hurt to have more bits; for example, with enough 36-bit
  2106. words you might be able to have |mem_max| as large as 262142, which is
  2107. eight times as much memory as anybody had during the first four years of
  2108. \TeX's existence.
  2109. N.B.: Valuable memory space will be dreadfully wasted unless \TeX\ is compiled
  2110. by a \PASCAL\ that packs all of the |memory_word| variants into
  2111. the space of a single integer. This means, for example, that |glue_ratio|
  2112. words should be |short_real| instead of |real| on some computers. Some
  2113. \PASCAL\ compilers will pack an integer whose subrange is `|0..255|' into
  2114. an eight-bit field, but others insist on allocating space for an additional
  2115. sign bit; on such systems you can get 256 values into a quarterword only
  2116. if the subrange is `|-128..127|'.
  2117. The present implementation tries to accommodate as many variations as possible,
  2118. so it makes few assumptions. If integers having the subrange
  2119. `|min_quarterword..max_quarterword|' can be packed into a quarterword,
  2120. and if integers having the subrange `|min_halfword..max_halfword|'
  2121. can be packed into a halfword, everything should work satisfactorily.
  2122. It is usually most efficient to have |min_quarterword=min_halfword=0|,
  2123. so one should try to achieve this unless it causes a severe problem.
  2124. The values defined here are recommended for most 32-bit computers.
  2125. @d min_quarterword=0 {smallest allowable value in a |quarterword|}
  2126. @d max_quarterword=255 {largest allowable value in a |quarterword|}
  2127. @d min_halfword==0 {smallest allowable value in a |halfword|}
  2128. @d max_halfword==65535 {largest allowable value in a |halfword|}
  2129. @ Here are the inequalities that the quarterword and halfword values
  2130. must satisfy (or rather, the inequalities that they mustn't satisfy):
  2131. @<Check the ``constant''...@>=
  2132. init if (mem_min<>mem_bot)or(mem_max<>mem_top) then bad:=10;@+tini@;@/
  2133. if (mem_min>mem_bot)or(mem_max<mem_top) then bad:=10;
  2134. if (min_quarterword>0)or(max_quarterword<127) then bad:=11;
  2135. if (min_halfword>0)or(max_halfword<32767) then bad:=12;
  2136. if (min_quarterword<min_halfword)or@|
  2137. (max_quarterword>max_halfword) then bad:=13;
  2138. if (mem_min<min_halfword)or(mem_max>=max_halfword)or@|
  2139. (mem_bot-mem_min>max_halfword+1) then bad:=14;
  2140. if (font_base<min_quarterword)or(font_max>max_quarterword) then bad:=15;
  2141. if font_max>font_base+256 then bad:=16;
  2142. if (save_size>max_halfword)or(max_strings>max_halfword) then bad:=17;
  2143. if buf_size>max_halfword then bad:=18;
  2144. if max_quarterword-min_quarterword<255 then bad:=19;
  2145. @ The operation of adding or subtracting |min_quarterword| occurs quite
  2146. frequently in \TeX, so it is convenient to abbreviate this operation
  2147. by using the macros |qi| and |qo| for input and output to and from
  2148. quarterword format.
  2149. The inner loop of \TeX\ will run faster with respect to compilers
  2150. that don't optimize expressions like `|x+0|' and `|x-0|', if these
  2151. macros are simplified in the obvious way when |min_quarterword=0|.
  2152. @^inner loop@>@^system dependencies@>
  2153. @d qi(#)==#+min_quarterword
  2154. {to put an |eight_bits| item into a quarterword}
  2155. @d qo(#)==#-min_quarterword
  2156. {to take an |eight_bits| item out of a quarterword}
  2157. @d hi(#)==#+min_halfword
  2158. {to put a sixteen-bit item into a halfword}
  2159. @d ho(#)==#-min_halfword
  2160. {to take a sixteen-bit item from a halfword}
  2161. @ The reader should study the following definitions closely:
  2162. @^system dependencies@>
  2163. @d sc==int {|scaled| data is equivalent to |integer|}
  2164. @<Types...@>=
  2165. @!quarterword = min_quarterword..max_quarterword; {1/4 of a word}
  2166. @!halfword=min_halfword..max_halfword; {1/2 of a word}
  2167. @!two_choices = 1..2; {used when there are two variants in a record}
  2168. @!four_choices = 1..4; {used when there are four variants in a record}
  2169. @!two_halves = packed record@;@/
  2170. @!rh:halfword;
  2171. case two_choices of
  2172. 1: (@!lh:halfword);
  2173. 2: (@!b0:quarterword; @!b1:quarterword);
  2174. end;
  2175. @!four_quarters = packed record@;@/
  2176. @!b0:quarterword;
  2177. @!b1:quarterword;
  2178. @!b2:quarterword;
  2179. @!b3:quarterword;
  2180. end;
  2181. @!memory_word = record@;@/
  2182. case four_choices of
  2183. 1: (@!int:integer);
  2184. 2: (@!gr:glue_ratio);
  2185. 3: (@!hh:two_halves);
  2186. 4: (@!qqqq:four_quarters);
  2187. end;
  2188. @!word_file = file of memory_word;
  2189. @ When debugging, we may want to print a |memory_word| without knowing
  2190. what type it is; so we print it in all modes.
  2191. @^dirty \PASCAL@>@^debugging@>
  2192. @p @!debug procedure print_word(@!w:memory_word);
  2193. {prints |w| in all ways}
  2194. begin print_int(w.int); print_char(" ");@/
  2195. print_scaled(w.sc); print_char(" ");@/
  2196. print_scaled(round(unity*float(w.gr))); print_ln;@/
  2197. @^real multiplication@>
  2198. print_int(w.hh.lh); print_char("="); print_int(w.hh.b0); print_char(":");
  2199. print_int(w.hh.b1); print_char(";"); print_int(w.hh.rh); print_char(" ");@/
  2200. print_int(w.qqqq.b0); print_char(":"); print_int(w.qqqq.b1); print_char(":");
  2201. print_int(w.qqqq.b2); print_char(":"); print_int(w.qqqq.b3);
  2202. end;
  2203. gubed
  2204. @* \[9] Dynamic memory allocation.
  2205. The \TeX\ system does nearly all of its own memory allocation, so that it
  2206. can readily be transported into environments that do not have automatic
  2207. facilities for strings, garbage collection, etc., and so that it can be in
  2208. control of what error messages the user receives. The dynamic storage
  2209. requirements of \TeX\ are handled by providing a large array |mem| in
  2210. which consecutive blocks of words are used as nodes by the \TeX\ routines.
  2211. Pointer variables are indices into this array, or into another array
  2212. called |eqtb| that will be explained later. A pointer variable might
  2213. also be a special flag that lies outside the bounds of |mem|, so we
  2214. allow pointers to assume any |halfword| value. The minimum halfword
  2215. value represents a null pointer. \TeX\ does not assume that |mem[null]| exists.
  2216. @d pointer==halfword {a flag or a location in |mem| or |eqtb|}
  2217. @d null==min_halfword {the null pointer}
  2218. @<Glob...@>=
  2219. @!temp_ptr:pointer; {a pointer variable for occasional emergency use}
  2220. @ The |mem| array is divided into two regions that are allocated separately,
  2221. but the dividing line between these two regions is not fixed; they grow
  2222. together until finding their ``natural'' size in a particular job.
  2223. Locations less than or equal to |lo_mem_max| are used for storing
  2224. variable-length records consisting of two or more words each. This region
  2225. is maintained using an algorithm similar to the one described in exercise
  2226. 2.5--19 of {\sl The Art of Computer Programming}. However, no size field
  2227. appears in the allocated nodes; the program is responsible for knowing the
  2228. relevant size when a node is freed. Locations greater than or equal to
  2229. |hi_mem_min| are used for storing one-word records; a conventional
  2230. \.{AVAIL} stack is used for allocation in this region.
  2231. Locations of |mem| between |mem_bot| and |mem_top| may be dumped as part
  2232. of preloaded format files, by the \.{INITEX} preprocessor.
  2233. @.INITEX@>
  2234. Production versions of \TeX\ may extend the memory at both ends in order to
  2235. provide more space; locations between |mem_min| and |mem_bot| are always
  2236. used for variable-size nodes, and locations between |mem_top| and |mem_max|
  2237. are always used for single-word nodes.
  2238. The key pointers that govern |mem| allocation have a prescribed order:
  2239. $$\advance\thickmuskip-2mu
  2240. \hbox{|null<=mem_min<=mem_bot<lo_mem_max<
  2241. hi_mem_min<mem_top<=mem_end<=mem_max|.}$$
  2242. Empirical tests show that the present implementation of \TeX\ tends to
  2243. spend about 9\pct! of its running time allocating nodes, and about 6\pct!
  2244. deallocating them after their use.
  2245. @<Glob...@>=
  2246. @!mem : array[mem_min..mem_max] of memory_word; {the big dynamic storage area}
  2247. @!lo_mem_max : pointer; {the largest location of variable-size memory in use}
  2248. @!hi_mem_min : pointer; {the smallest location of one-word memory in use}
  2249. @ In order to study the memory requirements of particular applications, it
  2250. is possible to prepare a version of \TeX\ that keeps track of current and
  2251. maximum memory usage. When code between the delimiters |@!stat| $\ldots$
  2252. |tats| is not ``commented out,'' \TeX\ will run a bit slower but it will
  2253. report these statistics when |tracing_stats| is sufficiently large.
  2254. @<Glob...@>=
  2255. @!var_used, @!dyn_used : integer; {how much memory is in use}
  2256. @ Let's consider the one-word memory region first, since it's the
  2257. simplest. The pointer variable |mem_end| holds the highest-numbered location
  2258. of |mem| that has ever been used. The free locations of |mem| that
  2259. occur between |hi_mem_min| and |mem_end|, inclusive, are of type
  2260. |two_halves|, and we write |info(p)| and |link(p)| for the |lh|
  2261. and |rh| fields of |mem[p]| when it is of this type. The single-word
  2262. free locations form a linked list
  2263. $$|avail|,\;\hbox{|link(avail)|},\;\hbox{|link(link(avail))|},\;\ldots$$
  2264. terminated by |null|.
  2265. @d link(#) == mem[#].hh.rh {the |link| field of a memory word}
  2266. @d info(#) == mem[#].hh.lh {the |info| field of a memory word}
  2267. @<Glob...@>=
  2268. @!avail : pointer; {head of the list of available one-word nodes}
  2269. @!mem_end : pointer; {the last one-word node used in |mem|}
  2270. @ If memory is exhausted, it might mean that the user has forgotten
  2271. a right brace. We will define some procedures later that try to help
  2272. pinpoint the trouble.
  2273. @p @<Declare the procedure called |show_token_list|@>@/
  2274. @<Declare the procedure called |runaway|@>
  2275. @ The function |get_avail| returns a pointer to a new one-word node whose
  2276. |link| field is null. However, \TeX\ will halt if there is no more room left.
  2277. @^inner loop@>
  2278. If the available-space list is empty, i.e., if |avail=null|,
  2279. we try first to increase |mem_end|. If that cannot be done, i.e., if
  2280. |mem_end=mem_max|, we try to decrease |hi_mem_min|. If that cannot be
  2281. done, i.e., if |hi_mem_min=lo_mem_max+1|, we have to quit.
  2282. @p function get_avail : pointer; {single-word node allocation}
  2283. var p:pointer; {the new node being got}
  2284. begin p:=avail; {get top location in the |avail| stack}
  2285. if p<>null then avail:=link(avail) {and pop it off}
  2286. else if mem_end<mem_max then {or go into virgin territory}
  2287. begin incr(mem_end); p:=mem_end;
  2288. end
  2289. else begin decr(hi_mem_min); p:=hi_mem_min;
  2290. if hi_mem_min<=lo_mem_max then
  2291. begin runaway; {if memory is exhausted, display possible runaway text}
  2292. overflow("main memory size",mem_max+1-mem_min);
  2293. {quit; all one-word nodes are busy}
  2294. @:TeX capacity exceeded main memory size}{\quad main memory size@>
  2295. end;
  2296. end;
  2297. link(p):=null; {provide an oft-desired initialization of the new node}
  2298. @!stat incr(dyn_used);@+tats@;{maintain statistics}
  2299. get_avail:=p;
  2300. end;
  2301. @ Conversely, a one-word node is recycled by calling |free_avail|.
  2302. This routine is part of \TeX's ``inner loop,'' so we want it to be fast.
  2303. @^inner loop@>
  2304. @d free_avail(#)== {single-word node liberation}
  2305. begin link(#):=avail; avail:=#;
  2306. @!stat decr(dyn_used);@+tats@/
  2307. end
  2308. @ There's also a |fast_get_avail| routine, which saves the procedure-call
  2309. overhead at the expense of extra programming. This routine is used in
  2310. the places that would otherwise account for the most calls of |get_avail|.
  2311. @^inner loop@>
  2312. @d fast_get_avail(#)==@t@>@;@/
  2313. begin #:=avail; {avoid |get_avail| if possible, to save time}
  2314. if #=null then #:=get_avail
  2315. else begin avail:=link(#); link(#):=null;
  2316. @!stat incr(dyn_used);@+tats@/
  2317. end;
  2318. end
  2319. @ The procedure |flush_list(p)| frees an entire linked list of
  2320. one-word nodes that starts at position |p|.
  2321. @^inner loop@>
  2322. @p procedure flush_list(@!p:pointer); {makes list of single-word nodes
  2323. available}
  2324. var @!q,@!r:pointer; {list traversers}
  2325. begin if p<>null then
  2326. begin r:=p;
  2327. repeat q:=r; r:=link(r); @!stat decr(dyn_used);@+tats@/
  2328. until r=null; {now |q| is the last node on the list}
  2329. link(q):=avail; avail:=p;
  2330. end;
  2331. end;
  2332. @ The available-space list that keeps track of the variable-size portion
  2333. of |mem| is a nonempty, doubly-linked circular list of empty nodes,
  2334. pointed to by the roving pointer |rover|.
  2335. Each empty node has size 2 or more; the first word contains the special
  2336. value |max_halfword| in its |link| field and the size in its |info| field;
  2337. the second word contains the two pointers for double linking.
  2338. Each nonempty node also has size 2 or more. Its first word is of type
  2339. |two_halves|\kern-1pt, and its |link| field is never equal to |max_halfword|.
  2340. Otherwise there is complete flexibility with respect to the contents
  2341. of its other fields and its other words.
  2342. (We require |mem_max<max_halfword| because terrible things can happen
  2343. when |max_halfword| appears in the |link| field of a nonempty node.)
  2344. @d empty_flag == max_halfword {the |link| of an empty variable-size node}
  2345. @d is_empty(#) == (link(#)=empty_flag) {tests for empty node}
  2346. @d node_size == info {the size field in empty variable-size nodes}
  2347. @d llink(#) == info(#+1) {left link in doubly-linked list of empty nodes}
  2348. @d rlink(#) == link(#+1) {right link in doubly-linked list of empty nodes}
  2349. @<Glob...@>=
  2350. @!rover : pointer; {points to some node in the list of empties}
  2351. @ A call to |get_node| with argument |s| returns a pointer to a new node
  2352. of size~|s|, which must be 2~or more. The |link| field of the first word
  2353. of this new node is set to null. An overflow stop occurs if no suitable
  2354. space exists.
  2355. If |get_node| is called with $s=2^{30}$, it simply merges adjacent free
  2356. areas and returns the value |max_halfword|.
  2357. @p function get_node(@!s:integer):pointer; {variable-size node allocation}
  2358. label found,exit,restart;
  2359. var p:pointer; {the node currently under inspection}
  2360. @!q:pointer; {the node physically after node |p|}
  2361. @!r:integer; {the newly allocated node, or a candidate for this honor}
  2362. @!t:integer; {temporary register}
  2363. begin restart: p:=rover; {start at some free node in the ring}
  2364. repeat @<Try to allocate within node |p| and its physical successors,
  2365. and |goto found| if allocation was possible@>;
  2366. @^inner loop@>
  2367. p:=rlink(p); {move to the next node in the ring}
  2368. until p=rover; {repeat until the whole list has been traversed}
  2369. if s=@'10000000000 then
  2370. begin get_node:=max_halfword; return;
  2371. end;
  2372. if lo_mem_max+2<hi_mem_min then if lo_mem_max+2<=mem_bot+max_halfword then
  2373. @<Grow more variable-size memory and |goto restart|@>;
  2374. overflow("main memory size",mem_max+1-mem_min);
  2375. {sorry, nothing satisfactory is left}
  2376. @:TeX capacity exceeded main memory size}{\quad main memory size@>
  2377. found: link(r):=null; {this node is now nonempty}
  2378. @!stat var_used:=var_used+s; {maintain usage statistics}
  2379. tats@;@/
  2380. get_node:=r;
  2381. exit:end;
  2382. @ The lower part of |mem| grows by 1000 words at a time, unless
  2383. we are very close to going under. When it grows, we simply link
  2384. a new node into the available-space list. This method of controlled
  2385. growth helps to keep the |mem| usage consecutive when \TeX\ is
  2386. implemented on ``virtual memory'' systems.
  2387. @^virtual memory@>
  2388. @<Grow more variable-size memory and |goto restart|@>=
  2389. begin if hi_mem_min-lo_mem_max>=1998 then t:=lo_mem_max+1000
  2390. else t:=lo_mem_max+1+(hi_mem_min-lo_mem_max) div 2;
  2391. {|lo_mem_max+2<=t<hi_mem_min|}
  2392. p:=llink(rover); q:=lo_mem_max; rlink(p):=q; llink(rover):=q;@/
  2393. if t>mem_bot+max_halfword then t:=mem_bot+max_halfword;
  2394. rlink(q):=rover; llink(q):=p; link(q):=empty_flag; node_size(q):=t-lo_mem_max;@/
  2395. lo_mem_max:=t; link(lo_mem_max):=null; info(lo_mem_max):=null;
  2396. rover:=q; goto restart;
  2397. end
  2398. @ Empirical tests show that the routine in this section performs a
  2399. node-merging operation about 0.75 times per allocation, on the average,
  2400. after which it finds that |r>p+1| about 95\pct! of the time.
  2401. @<Try to allocate...@>=
  2402. q:=p+node_size(p); {find the physical successor}
  2403. @^inner loop@>
  2404. while is_empty(q) do {merge node |p| with node |q|}
  2405. begin t:=rlink(q);
  2406. if q=rover then rover:=t;
  2407. llink(t):=llink(q); rlink(llink(q)):=t;@/
  2408. q:=q+node_size(q);
  2409. end;
  2410. r:=q-s;
  2411. if r>p+1 then @<Allocate from the top of node |p| and |goto found|@>;
  2412. if r=p then if rlink(p)<>p then
  2413. @<Allocate entire node |p| and |goto found|@>;
  2414. node_size(p):=q-p {reset the size in case it grew}
  2415. @ @<Allocate from the top...@>=
  2416. begin node_size(p):=r-p; {store the remaining size}
  2417. @^inner loop@>
  2418. rover:=p; {start searching here next time}
  2419. goto found;
  2420. end
  2421. @ Here we delete node |p| from the ring, and let |rover| rove around.
  2422. @<Allocate entire...@>=
  2423. begin rover:=rlink(p); t:=llink(p);
  2424. llink(rover):=t; rlink(t):=rover;
  2425. goto found;
  2426. end
  2427. @ Conversely, when some variable-size node |p| of size |s| is no longer needed,
  2428. the operation |free_node(p,s)| will make its words available, by inserting
  2429. |p| as a new empty node just before where |rover| now points.
  2430. @^inner loop@>
  2431. @p procedure free_node(@!p:pointer; @!s:halfword); {variable-size node
  2432. liberation}
  2433. var q:pointer; {|llink(rover)|}
  2434. begin node_size(p):=s; link(p):=empty_flag;
  2435. q:=llink(rover); llink(p):=q; rlink(p):=rover; {set both links}
  2436. llink(rover):=p; rlink(q):=p; {insert |p| into the ring}
  2437. @!stat var_used:=var_used-s;@+tats@;{maintain statistics}
  2438. end;
  2439. @ Just before \.{INITEX} writes out the memory, it sorts the doubly linked
  2440. available space list. The list is probably very short at such times, so a
  2441. simple insertion sort is used. The smallest available location will be
  2442. pointed to by |rover|, the next-smallest by |rlink(rover)|, etc.
  2443. @p @!init procedure sort_avail; {sorts the available variable-size nodes
  2444. by location}
  2445. var p,@!q,@!r: pointer; {indices into |mem|}
  2446. @!old_rover:pointer; {initial |rover| setting}
  2447. begin p:=get_node(@'10000000000); {merge adjacent free areas}
  2448. p:=rlink(rover); rlink(rover):=max_halfword; old_rover:=rover;
  2449. while p<>old_rover do @<Sort \(p)|p| into the list starting at |rover|
  2450. and advance |p| to |rlink(p)|@>;
  2451. p:=rover;
  2452. while rlink(p)<>max_halfword do
  2453. begin llink(rlink(p)):=p; p:=rlink(p);
  2454. end;
  2455. rlink(p):=rover; llink(rover):=p;
  2456. end;
  2457. tini
  2458. @ The following |while| loop is guaranteed to
  2459. terminate, since the list that starts at
  2460. |rover| ends with |max_halfword| during the sorting procedure.
  2461. @<Sort \(p)|p|...@>=
  2462. if p<rover then
  2463. begin q:=p; p:=rlink(q); rlink(q):=rover; rover:=q;
  2464. end
  2465. else begin q:=rover;
  2466. while rlink(q)<p do q:=rlink(q);
  2467. r:=rlink(p); rlink(p):=rlink(q); rlink(q):=p; p:=r;
  2468. end
  2469. @* \[10] Data structures for boxes and their friends.
  2470. From the computer's standpoint, \TeX's chief mission is to create
  2471. horizontal and vertical lists. We shall now investigate how the elements
  2472. of these lists are represented internally as nodes in the dynamic memory.
  2473. A horizontal or vertical list is linked together by |link| fields in
  2474. the first word of each node. Individual nodes represent boxes, glue,
  2475. penalties, or special things like discretionary hyphens; because of this
  2476. variety, some nodes are longer than others, and we must distinguish different
  2477. kinds of nodes. We do this by putting a `|type|' field in the first word,
  2478. together with the link and an optional `|subtype|'.
  2479. @d type(#) == mem[#].hh.b0 {identifies what kind of node this is}
  2480. @d subtype(#) == mem[#].hh.b1 {secondary identification in some cases}
  2481. @ A |@!char_node|, which represents a single character, is the most important
  2482. kind of node because it accounts for the vast majority of all boxes.
  2483. Special precautions are therefore taken to ensure that a |char_node| does
  2484. not take up much memory space. Every such node is one word long, and in fact
  2485. it is identifiable by this property, since other kinds of nodes have at least
  2486. two words, and they appear in |mem| locations less than |hi_mem_min|.
  2487. This makes it possible to omit the |type| field in a |char_node|, leaving
  2488. us room for two bytes that identify a |font| and a |character| within
  2489. that font.
  2490. Note that the format of a |char_node| allows for up to 256 different
  2491. fonts and up to 256 characters per font; but most implementations will
  2492. probably limit the total number of fonts to fewer than 75 per job,
  2493. and most fonts will stick to characters whose codes are
  2494. less than 128 (since higher codes
  2495. are more difficult to access on most keyboards).
  2496. Extensions of \TeX\ intended for oriental languages will need even more
  2497. than $256\times256$ possible characters, when we consider different sizes
  2498. @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
  2499. and styles of type. It is suggested that Chinese and Japanese fonts be
  2500. handled by representing such characters in two consecutive |char_node|
  2501. entries: The first of these has |font=font_base|, and its |link| points
  2502. to the second;
  2503. the second identifies the font and the character dimensions.
  2504. The saving feature about oriental characters is that most of them have
  2505. the same box dimensions. The |character| field of the first |char_node|
  2506. is a ``\\{charext}'' that distinguishes between graphic symbols whose
  2507. dimensions are identical for typesetting purposes. (See the \MF\ manual.)
  2508. Such an extension of \TeX\ would not be difficult; further details are
  2509. left to the reader.
  2510. In order to make sure that the |character| code fits in a quarterword,
  2511. \TeX\ adds the quantity |min_quarterword| to the actual code.
  2512. Character nodes appear only in horizontal lists, never in vertical lists.
  2513. @d is_char_node(#) == (#>=hi_mem_min)
  2514. {does the argument point to a |char_node|?}
  2515. @d font == type {the font code in a |char_node|}
  2516. @d character == subtype {the character code in a |char_node|}
  2517. @ An |hlist_node| stands for a box that was made from a horizontal list.
  2518. Each |hlist_node| is seven words long, and contains the following fields
  2519. (in addition to the mandatory |type| and |link|, which we shall not
  2520. mention explicitly when discussing the other node types): The |height| and
  2521. |width| and |depth| are scaled integers denoting the dimensions of the
  2522. box. There is also a |shift_amount| field, a scaled integer indicating
  2523. how much this box should be lowered (if it appears in a horizontal list),
  2524. or how much it should be moved to the right (if it appears in a vertical
  2525. list). There is a |list_ptr| field, which points to the beginning of the
  2526. list from which this box was fabricated; if |list_ptr| is |null|, the box
  2527. is empty. Finally, there are three fields that represent the setting of
  2528. the glue: |glue_set(p)| is a word of type |glue_ratio| that represents
  2529. the proportionality constant for glue setting; |glue_sign(p)| is
  2530. |stretching| or |shrinking| or |normal| depending on whether or not the
  2531. glue should stretch or shrink or remain rigid; and |glue_order(p)|
  2532. specifies the order of infinity to which glue setting applies (|normal|,
  2533. |fil|, |fill|, or |filll|). The |subtype| field is not used.
  2534. @d hlist_node=0 {|type| of hlist nodes}
  2535. @d box_node_size=7 {number of words to allocate for a box node}
  2536. @d width_offset=1 {position of |width| field in a box node}
  2537. @d depth_offset=2 {position of |depth| field in a box node}
  2538. @d height_offset=3 {position of |height| field in a box node}
  2539. @d width(#) == mem[#+width_offset].sc {width of the box, in sp}
  2540. @d depth(#) == mem[#+depth_offset].sc {depth of the box, in sp}
  2541. @d height(#) == mem[#+height_offset].sc {height of the box, in sp}
  2542. @d shift_amount(#) == mem[#+4].sc {repositioning distance, in sp}
  2543. @d list_offset=5 {position of |list_ptr| field in a box node}
  2544. @d list_ptr(#) == link(#+list_offset) {beginning of the list inside the box}
  2545. @d glue_order(#) == subtype(#+list_offset) {applicable order of infinity}
  2546. @d glue_sign(#) == type(#+list_offset) {stretching or shrinking}
  2547. @d normal=0 {the most common case when several cases are named}
  2548. @d stretching = 1 {glue setting applies to the stretch components}
  2549. @d shrinking = 2 {glue setting applies to the shrink components}
  2550. @d glue_offset = 6 {position of |glue_set| in a box node}
  2551. @d glue_set(#) == mem[#+glue_offset].gr
  2552. {a word of type |glue_ratio| for glue setting}
  2553. @ The |new_null_box| function returns a pointer to an |hlist_node| in
  2554. which all subfields have the values corresponding to `\.{\\hbox\{\}}'.
  2555. (The |subtype| field is set to |min_quarterword|, for historic reasons
  2556. that are no longer relevant.)
  2557. @p function new_null_box:pointer; {creates a new box node}
  2558. var p:pointer; {the new node}
  2559. begin p:=get_node(box_node_size); type(p):=hlist_node;
  2560. subtype(p):=min_quarterword;
  2561. width(p):=0; depth(p):=0; height(p):=0; shift_amount(p):=0; list_ptr(p):=null;
  2562. glue_sign(p):=normal; glue_order(p):=normal; set_glue_ratio_zero(glue_set(p));
  2563. new_null_box:=p;
  2564. end;
  2565. @ A |vlist_node| is like an |hlist_node| in all respects except that it
  2566. contains a vertical list.
  2567. @d vlist_node=1 {|type| of vlist nodes}
  2568. @ A |rule_node| stands for a solid black rectangle; it has |width|,
  2569. |depth|, and |height| fields just as in an |hlist_node|. However, if
  2570. any of these dimensions is $-2^{30}$, the actual value will be determined
  2571. by running the rule up to the boundary of the innermost enclosing box.
  2572. This is called a ``running dimension.'' The |width| is never running in
  2573. an hlist; the |height| and |depth| are never running in a~vlist.
  2574. @d rule_node=2 {|type| of rule nodes}
  2575. @d rule_node_size=4 {number of words to allocate for a rule node}
  2576. @d null_flag==-@'10000000000 {$-2^{30}$, signifies a missing item}
  2577. @d is_running(#) == (#=null_flag) {tests for a running dimension}
  2578. @ A new rule node is delivered by the |new_rule| function. It
  2579. makes all the dimensions ``running,'' so you have to change the
  2580. ones that are not allowed to run.
  2581. @p function new_rule:pointer;
  2582. var p:pointer; {the new node}
  2583. begin p:=get_node(rule_node_size); type(p):=rule_node;
  2584. subtype(p):=0; {the |subtype| is not used}
  2585. width(p):=null_flag; depth(p):=null_flag; height(p):=null_flag;
  2586. new_rule:=p;
  2587. end;
  2588. @ Insertions are represented by |ins_node| records, where the |subtype|
  2589. indicates the corresponding box number. For example, `\.{\\insert 250}'
  2590. leads to an |ins_node| whose |subtype| is |250+min_quarterword|.
  2591. The |height| field of an |ins_node| is slightly misnamed; it actually holds
  2592. the natural height plus depth of the vertical list being inserted.
  2593. The |depth| field holds the |split_max_depth| to be used in case this
  2594. insertion is split, and the |split_top_ptr| points to the corresponding
  2595. |split_top_skip|. The |float_cost| field holds the |floating_penalty| that
  2596. will be used if this insertion floats to a subsequent page after a
  2597. split insertion of the same class. There is one more field, the
  2598. |ins_ptr|, which points to the beginning of the vlist for the insertion.
  2599. @d ins_node=3 {|type| of insertion nodes}
  2600. @d ins_node_size=5 {number of words to allocate for an insertion}
  2601. @d float_cost(#)==mem[#+1].int {the |floating_penalty| to be used}
  2602. @d ins_ptr(#)==info(#+4) {the vertical list to be inserted}
  2603. @d split_top_ptr(#)==link(#+4) {the |split_top_skip| to be used}
  2604. @ A |mark_node| has a |mark_ptr| field that points to the reference count
  2605. of a token list that contains the user's \.{\\mark} text.
  2606. This field occupies a full word instead of a halfword, because
  2607. there's nothing to put in the other halfword; it is easier in \PASCAL\ to
  2608. use the full word than to risk leaving garbage in the unused half.
  2609. @d mark_node=4 {|type| of a mark node}
  2610. @d small_node_size=2 {number of words to allocate for most node types}
  2611. @d mark_ptr(#)==mem[#+1].int {head of the token list for a mark}
  2612. @ An |adjust_node|, which occurs only in horizontal lists,
  2613. specifies material that will be moved out into the surrounding
  2614. vertical list; i.e., it is used to implement \TeX's `\.{\\vadjust}'
  2615. operation. The |adjust_ptr| field points to the vlist containing this
  2616. material.
  2617. @d adjust_node=5 {|type| of an adjust node}
  2618. @d adjust_ptr==mark_ptr {vertical list to be moved out of horizontal list}
  2619. @ A |ligature_node|, which occurs only in horizontal lists, specifies
  2620. a character that was fabricated from the interaction of two or more
  2621. actual characters. The second word of the node, which is called the
  2622. |lig_char| word, contains |font| and |character| fields just as in a
  2623. |char_node|. The characters that generated the ligature have not been
  2624. forgotten, since they are needed for diagnostic messages and for
  2625. hyphenation; the |lig_ptr| field points to a linked list of character
  2626. nodes for all original characters that have been deleted. (This list
  2627. might be empty if the characters that generated the ligature were
  2628. retained in other nodes.)
  2629. The |subtype| field is 0, plus 2 and/or 1 if the original source of the
  2630. ligature included implicit left and/or right boundaries.
  2631. @d ligature_node=6 {|type| of a ligature node}
  2632. @d lig_char(#)==#+1 {the word where the ligature is to be found}
  2633. @d lig_ptr(#)==link(lig_char(#)) {the list of characters}
  2634. @ The |new_ligature| function creates a ligature node having given
  2635. contents of the |font|, |character|, and |lig_ptr| fields. We also have
  2636. a |new_lig_item| function, which returns a two-word node having a given
  2637. |character| field. Such nodes are used for temporary processing as ligatures
  2638. are being created.
  2639. @p function new_ligature(@!f,@!c:quarterword; @!q:pointer):pointer;
  2640. var p:pointer; {the new node}
  2641. begin p:=get_node(small_node_size); type(p):=ligature_node;
  2642. font(lig_char(p)):=f; character(lig_char(p)):=c; lig_ptr(p):=q;
  2643. subtype(p):=0; new_ligature:=p;
  2644. end;
  2645. @#
  2646. function new_lig_item(@!c:quarterword):pointer;
  2647. var p:pointer; {the new node}
  2648. begin p:=get_node(small_node_size); character(p):=c; lig_ptr(p):=null;
  2649. new_lig_item:=p;
  2650. end;
  2651. @ A |disc_node|, which occurs only in horizontal lists, specifies a
  2652. ``dis\-cretion\-ary'' line break. If such a break occurs at node |p|, the text
  2653. that starts at |pre_break(p)| will precede the break, the text that starts at
  2654. |post_break(p)| will follow the break, and text that appears in the next
  2655. |replace_count(p)| nodes will be ignored. For example, an ordinary
  2656. discretionary hyphen, indicated by `\.{\\-}', yields a |disc_node| with
  2657. |pre_break| pointing to a |char_node| containing a hyphen, |post_break=null|,
  2658. and |replace_count=0|. All three of the discretionary texts must be
  2659. lists that consist entirely of character, kern, box, rule, and ligature nodes.
  2660. If |pre_break(p)=null|, the |ex_hyphen_penalty| will be charged for this
  2661. break. Otherwise the |hyphen_penalty| will be charged. The texts will
  2662. actually be substituted into the list by the line-breaking algorithm if it
  2663. decides to make the break, and the discretionary node will disappear at
  2664. that time; thus, the output routine sees only discretionaries that were
  2665. not chosen.
  2666. @d disc_node=7 {|type| of a discretionary node}
  2667. @d replace_count==subtype {how many subsequent nodes to replace}
  2668. @d pre_break==llink {text that precedes a discretionary break}
  2669. @d post_break==rlink {text that follows a discretionary break}
  2670. @p function new_disc:pointer; {creates an empty |disc_node|}
  2671. var p:pointer; {the new node}
  2672. begin p:=get_node(small_node_size); type(p):=disc_node;
  2673. replace_count(p):=0; pre_break(p):=null; post_break(p):=null;
  2674. new_disc:=p;
  2675. end;
  2676. @ A |whatsit_node| is a wild card reserved for extensions to \TeX. The
  2677. |subtype| field in its first word says what `\\{whatsit}' it is, and
  2678. implicitly determines the node size (which must be 2 or more) and the
  2679. format of the remaining words. When a |whatsit_node| is encountered
  2680. in a list, special actions are invoked; knowledgeable people who are
  2681. careful not to mess up the rest of \TeX\ are able to make \TeX\ do new
  2682. things by adding code at the end of the program. For example, there
  2683. might be a `\TeX nicolor' extension to specify different colors of ink,
  2684. @^extensions to \TeX@>
  2685. and the whatsit node might contain the desired parameters.
  2686. The present implementation of \TeX\ treats the features associated with
  2687. `\.{\\write}' and `\.{\\special}' as if they were extensions, in order to
  2688. illustrate how such routines might be coded. We shall defer further
  2689. discussion of extensions until the end of this program.
  2690. @d whatsit_node=8 {|type| of special extension nodes}
  2691. @ A |math_node|, which occurs only in horizontal lists, appears before and
  2692. after mathematical formulas. The |subtype| field is |before| before the
  2693. formula and |after| after it. There is a |width| field, which represents
  2694. the amount of surrounding space inserted by \.{\\mathsurround}.
  2695. @d math_node=9 {|type| of a math node}
  2696. @d before=0 {|subtype| for math node that introduces a formula}
  2697. @d after=1 {|subtype| for math node that winds up a formula}
  2698. @p function new_math(@!w:scaled;@!s:small_number):pointer;
  2699. var p:pointer; {the new node}
  2700. begin p:=get_node(small_node_size); type(p):=math_node;
  2701. subtype(p):=s; width(p):=w; new_math:=p;
  2702. end;
  2703. @ \TeX\ makes use of the fact that |hlist_node|, |vlist_node|,
  2704. |rule_node|, |ins_node|, |mark_node|, |adjust_node|, |ligature_node|,
  2705. |disc_node|, |whatsit_node|, and |math_node| are at the low end of the
  2706. type codes, by permitting a break at glue in a list if and only if the
  2707. |type| of the previous node is less than |math_node|. Furthermore, a
  2708. node is discarded after a break if its type is |math_node| or~more.
  2709. @d precedes_break(#)==(type(#)<math_node)
  2710. @d non_discardable(#)==(type(#)<math_node)
  2711. @ A |glue_node| represents glue in a list. However, it is really only
  2712. a pointer to a separate glue specification, since \TeX\ makes use of the
  2713. fact that many essentially identical nodes of glue are usually present.
  2714. If |p| points to a |glue_node|, |glue_ptr(p)| points to
  2715. another packet of words that specify the stretch and shrink components, etc.
  2716. Glue nodes also serve to represent leaders; the |subtype| is used to
  2717. distinguish between ordinary glue (which is called |normal|) and the three
  2718. kinds of leaders (which are called |a_leaders|, |c_leaders|, and |x_leaders|).
  2719. The |leader_ptr| field points to a rule node or to a box node containing the
  2720. leaders; it is set to |null| in ordinary glue nodes.
  2721. Many kinds of glue are computed from \TeX's ``skip'' parameters, and
  2722. it is helpful to know which parameter has led to a particular glue node.
  2723. Therefore the |subtype| is set to indicate the source of glue, whenever
  2724. it originated as a parameter. We will be defining symbolic names for the
  2725. parameter numbers later (e.g., |line_skip_code=0|, |baseline_skip_code=1|,
  2726. etc.); it suffices for now to say that the |subtype| of parametric glue
  2727. will be the same as the parameter number, plus~one.
  2728. In math formulas there are two more possibilities for the |subtype| in a
  2729. glue node: |mu_glue| denotes an \.{\\mskip} (where the units are scaled \.{mu}
  2730. instead of scaled \.{pt}); and |cond_math_glue| denotes the `\.{\\nonscript}'
  2731. feature that cancels the glue node immediately following if it appears
  2732. in a subscript.
  2733. @d glue_node=10 {|type| of node that points to a glue specification}
  2734. @d cond_math_glue=98 {special |subtype| to suppress glue in the next node}
  2735. @d mu_glue=99 {|subtype| for math glue}
  2736. @d a_leaders=100 {|subtype| for aligned leaders}
  2737. @d c_leaders=101 {|subtype| for centered leaders}
  2738. @d x_leaders=102 {|subtype| for expanded leaders}
  2739. @d glue_ptr==llink {pointer to a glue specification}
  2740. @d leader_ptr==rlink {pointer to box or rule node for leaders}
  2741. @ A glue specification has a halfword reference count in its first word,
  2742. @^reference counts@>
  2743. representing |null| plus the number of glue nodes that point to it (less one).
  2744. Note that the reference count appears in the same position as
  2745. the |link| field in list nodes; this is the field that is initialized
  2746. to |null| when a node is allocated, and it is also the field that is flagged
  2747. by |empty_flag| in empty nodes.
  2748. Glue specifications also contain three |scaled| fields, for the |width|,
  2749. |stretch|, and |shrink| dimensions. Finally, there are two one-byte
  2750. fields called |stretch_order| and |shrink_order|; these contain the
  2751. orders of infinity (|normal|, |fil|, |fill|, or |filll|)
  2752. corresponding to the stretch and shrink values.
  2753. @d glue_spec_size=4 {number of words to allocate for a glue specification}
  2754. @d glue_ref_count(#) == link(#) {reference count of a glue specification}
  2755. @d stretch(#) == mem[#+2].sc {the stretchability of this glob of glue}
  2756. @d shrink(#) == mem[#+3].sc {the shrinkability of this glob of glue}
  2757. @d stretch_order == type {order of infinity for stretching}
  2758. @d shrink_order == subtype {order of infinity for shrinking}
  2759. @d fil=1 {first-order infinity}
  2760. @d fill=2 {second-order infinity}
  2761. @d filll=3 {third-order infinity}
  2762. @<Types...@>=
  2763. @!glue_ord=normal..filll; {infinity to the 0, 1, 2, or 3 power}
  2764. @ Here is a function that returns a pointer to a copy of a glue spec.
  2765. The reference count in the copy is |null|, because there is assumed
  2766. to be exactly one reference to the new specification.
  2767. @p function new_spec(@!p:pointer):pointer; {duplicates a glue specification}
  2768. var q:pointer; {the new spec}
  2769. begin q:=get_node(glue_spec_size);@/
  2770. mem[q]:=mem[p]; glue_ref_count(q):=null;@/
  2771. width(q):=width(p); stretch(q):=stretch(p); shrink(q):=shrink(p);
  2772. new_spec:=q;
  2773. end;
  2774. @ And here's a function that creates a glue node for a given parameter
  2775. identified by its code number; for example,
  2776. |new_param_glue(line_skip_code)| returns a pointer to a glue node for the
  2777. current \.{\\lineskip}.
  2778. @p function new_param_glue(@!n:small_number):pointer;
  2779. var p:pointer; {the new node}
  2780. @!q:pointer; {the glue specification}
  2781. begin p:=get_node(small_node_size); type(p):=glue_node; subtype(p):=n+1;
  2782. leader_ptr(p):=null;@/
  2783. q:=@<Current |mem| equivalent of glue parameter number |n|@>@t@>;
  2784. glue_ptr(p):=q; incr(glue_ref_count(q));
  2785. new_param_glue:=p;
  2786. end;
  2787. @ Glue nodes that are more or less anonymous are created by |new_glue|,
  2788. whose argument points to a glue specification.
  2789. @p function new_glue(@!q:pointer):pointer;
  2790. var p:pointer; {the new node}
  2791. begin p:=get_node(small_node_size); type(p):=glue_node; subtype(p):=normal;
  2792. leader_ptr(p):=null; glue_ptr(p):=q; incr(glue_ref_count(q));
  2793. new_glue:=p;
  2794. end;
  2795. @ Still another subroutine is needed: This one is sort of a combination
  2796. of |new_param_glue| and |new_glue|. It creates a glue node for one of
  2797. the current glue parameters, but it makes a fresh copy of the glue
  2798. specification, since that specification will probably be subject to change,
  2799. while the parameter will stay put. The global variable |temp_ptr| is
  2800. set to the address of the new spec.
  2801. @p function new_skip_param(@!n:small_number):pointer;
  2802. var p:pointer; {the new node}
  2803. begin temp_ptr:=new_spec(@<Current |mem| equivalent of glue parameter...@>);
  2804. p:=new_glue(temp_ptr); glue_ref_count(temp_ptr):=null; subtype(p):=n+1;
  2805. new_skip_param:=p;
  2806. end;
  2807. @ A |kern_node| has a |width| field to specify a (normally negative)
  2808. amount of spacing. This spacing correction appears in horizontal lists
  2809. between letters like A and V when the font designer said that it looks
  2810. better to move them closer together or further apart. A kern node can
  2811. also appear in a vertical list, when its `|width|' denotes additional
  2812. spacing in the vertical direction. The |subtype| is either |normal| (for
  2813. kerns inserted from font information or math mode calculations) or |explicit|
  2814. (for kerns inserted from \.{\\kern} and \.{\\/} commands) or |acc_kern|
  2815. (for kerns inserted from non-math accents) or |mu_glue| (for kerns
  2816. inserted from \.{\\mkern} specifications in math formulas).
  2817. @d kern_node=11 {|type| of a kern node}
  2818. @d explicit=1 {|subtype| of kern nodes from \.{\\kern} and \.{\\/}}
  2819. @d acc_kern=2 {|subtype| of kern nodes from accents}
  2820. @ The |new_kern| function creates a kern node having a given width.
  2821. @p function new_kern(@!w:scaled):pointer;
  2822. var p:pointer; {the new node}
  2823. begin p:=get_node(small_node_size); type(p):=kern_node;
  2824. subtype(p):=normal;
  2825. width(p):=w;
  2826. new_kern:=p;
  2827. end;
  2828. @ A |penalty_node| specifies the penalty associated with line or page
  2829. breaking, in its |penalty| field. This field is a fullword integer, but
  2830. the full range of integer values is not used: Any penalty |>=10000| is
  2831. treated as infinity, and no break will be allowed for such high values.
  2832. Similarly, any penalty |<=-10000| is treated as negative infinity, and a
  2833. break will be forced.
  2834. @d penalty_node=12 {|type| of a penalty node}
  2835. @d inf_penalty=inf_bad {``infinite'' penalty value}
  2836. @d eject_penalty=-inf_penalty {``negatively infinite'' penalty value}
  2837. @d penalty(#) == mem[#+1].int {the added cost of breaking a list here}
  2838. @ Anyone who has been reading the last few sections of the program will
  2839. be able to guess what comes next.
  2840. @p function new_penalty(@!m:integer):pointer;
  2841. var p:pointer; {the new node}
  2842. begin p:=get_node(small_node_size); type(p):=penalty_node;
  2843. subtype(p):=0; {the |subtype| is not used}
  2844. penalty(p):=m; new_penalty:=p;
  2845. end;
  2846. @ You might think that we have introduced enough node types by now. Well,
  2847. almost, but there is one more: An |unset_node| has nearly the same format
  2848. as an |hlist_node| or |vlist_node|; it is used for entries in \.{\\halign}
  2849. or \.{\\valign} that are not yet in their final form, since the box
  2850. dimensions are their ``natural'' sizes before any glue adjustment has been
  2851. made. The |glue_set| word is not present; instead, we have a |glue_stretch|
  2852. field, which contains the total stretch of order |glue_order| that is
  2853. present in the hlist or vlist being boxed.
  2854. Similarly, the |shift_amount| field is replaced by a |glue_shrink| field,
  2855. containing the total shrink of order |glue_sign| that is present.
  2856. The |subtype| field is called |span_count|; an unset box typically
  2857. contains the data for |qo(span_count)+1| columns.
  2858. Unset nodes will be changed to box nodes when alignment is completed.
  2859. @d unset_node=13 {|type| for an unset node}
  2860. @d glue_stretch(#)==mem[#+glue_offset].sc {total stretch in an unset node}
  2861. @d glue_shrink==shift_amount {total shrink in an unset node}
  2862. @d span_count==subtype {indicates the number of spanned columns}
  2863. @ In fact, there are still more types coming. When we get to math formula
  2864. processing we will see that a |style_node| has |type=14|; and a number
  2865. of larger type codes will also be defined, for use in math mode only.
  2866. @ Warning: If any changes are made to these data structure layouts, such as
  2867. changing any of the node sizes or even reordering the words of nodes,
  2868. the |copy_node_list| procedure and the memory initialization code
  2869. below may have to be changed. Such potentially dangerous parts of the
  2870. program are listed in the index under `data structure assumptions'.
  2871. @!@^data structure assumptions@>
  2872. However, other references to the nodes are made symbolically in terms of
  2873. the \.{WEB} macro definitions above, so that format changes will leave
  2874. \TeX's other algorithms intact.
  2875. @^system dependencies@>
  2876. @* \[11] Memory layout.
  2877. Some areas of |mem| are dedicated to fixed usage, since static allocation is
  2878. more efficient than dynamic allocation when we can get away with it. For
  2879. example, locations |mem_bot| to |mem_bot+3| are always used to store the
  2880. specification for glue that is `\.{0pt plus 0pt minus 0pt}'. The
  2881. following macro definitions accomplish the static allocation by giving
  2882. symbolic names to the fixed positions. Static variable-size nodes appear
  2883. in locations |mem_bot| through |lo_mem_stat_max|, and static single-word nodes
  2884. appear in locations |hi_mem_stat_min| through |mem_top|, inclusive. It is
  2885. harmless to let |lig_trick| and |garbage| share the same location of |mem|.
  2886. @d zero_glue==mem_bot {specification for \.{0pt plus 0pt minus 0pt}}
  2887. @d fil_glue==zero_glue+glue_spec_size {\.{0pt plus 1fil minus 0pt}}
  2888. @d fill_glue==fil_glue+glue_spec_size {\.{0pt plus 1fill minus 0pt}}
  2889. @d ss_glue==fill_glue+glue_spec_size {\.{0pt plus 1fil minus 1fil}}
  2890. @d fil_neg_glue==ss_glue+glue_spec_size {\.{0pt plus -1fil minus 0pt}}
  2891. @d lo_mem_stat_max==fil_neg_glue+glue_spec_size-1 {largest statically
  2892. allocated word in the variable-size |mem|}
  2893. @#
  2894. @d page_ins_head==mem_top {list of insertion data for current page}
  2895. @d contrib_head==mem_top-1 {vlist of items not yet on current page}
  2896. @d page_head==mem_top-2 {vlist for current page}
  2897. @d temp_head==mem_top-3 {head of a temporary list of some kind}
  2898. @d hold_head==mem_top-4 {head of a temporary list of another kind}
  2899. @d adjust_head==mem_top-5 {head of adjustment list returned by |hpack|}
  2900. @d active==mem_top-7 {head of active list in |line_break|, needs two words}
  2901. @d align_head==mem_top-8 {head of preamble list for alignments}
  2902. @d end_span==mem_top-9 {tail of spanned-width lists}
  2903. @d omit_template==mem_top-10 {a constant token list}
  2904. @d null_list==mem_top-11 {permanently empty list}
  2905. @d lig_trick==mem_top-12 {a ligature masquerading as a |char_node|}
  2906. @d garbage==mem_top-12 {used for scrap information}
  2907. @d backup_head==mem_top-13 {head of token list built by |scan_keyword|}
  2908. @d hi_mem_stat_min==mem_top-13 {smallest statically allocated word in
  2909. the one-word |mem|}
  2910. @d hi_mem_stat_usage=14 {the number of one-word nodes always present}
  2911. @ The following code gets |mem| off to a good start, when \TeX\ is
  2912. initializing itself the slow~way.
  2913. @<Local variables for init...@>=
  2914. @!k:integer; {index into |mem|, |eqtb|, etc.}
  2915. @ @<Initialize table entries...@>=
  2916. for k:=mem_bot+1 to lo_mem_stat_max do mem[k].sc:=0;
  2917. {all glue dimensions are zeroed}
  2918. @^data structure assumptions@>
  2919. k:=mem_bot;@+while k<=lo_mem_stat_max do
  2920. {set first words of glue specifications}
  2921. begin glue_ref_count(k):=null+1;
  2922. stretch_order(k):=normal; shrink_order(k):=normal;
  2923. k:=k+glue_spec_size;
  2924. end;
  2925. stretch(fil_glue):=unity; stretch_order(fil_glue):=fil;@/
  2926. stretch(fill_glue):=unity; stretch_order(fill_glue):=fill;@/
  2927. stretch(ss_glue):=unity; stretch_order(ss_glue):=fil;@/
  2928. shrink(ss_glue):=unity; shrink_order(ss_glue):=fil;@/
  2929. stretch(fil_neg_glue):=-unity; stretch_order(fil_neg_glue):=fil;@/
  2930. rover:=lo_mem_stat_max+1;
  2931. link(rover):=empty_flag; {now initialize the dynamic memory}
  2932. node_size(rover):=1000; {which is a 1000-word available node}
  2933. llink(rover):=rover; rlink(rover):=rover;@/
  2934. lo_mem_max:=rover+1000; link(lo_mem_max):=null; info(lo_mem_max):=null;@/
  2935. for k:=hi_mem_stat_min to mem_top do
  2936. mem[k]:=mem[lo_mem_max]; {clear list heads}
  2937. @<Initialize the special list heads and constant nodes@>;
  2938. avail:=null; mem_end:=mem_top;
  2939. hi_mem_min:=hi_mem_stat_min; {initialize the one-word memory}
  2940. var_used:=lo_mem_stat_max+1-mem_bot; dyn_used:=hi_mem_stat_usage;
  2941. {initialize statistics}
  2942. @ If \TeX\ is extended improperly, the |mem| array might get screwed up.
  2943. For example, some pointers might be wrong, or some ``dead'' nodes might not
  2944. have been freed when the last reference to them disappeared. Procedures
  2945. |check_mem| and |search_mem| are available to help diagnose such
  2946. problems. These procedures make use of two arrays called |free| and
  2947. |was_free| that are present only if \TeX's debugging routines have
  2948. been included. (You may want to decrease the size of |mem| while you
  2949. @^debugging@>
  2950. are debugging.)
  2951. @<Glob...@>=
  2952. @!debug @!free: packed array [mem_min..mem_max] of boolean; {free cells}
  2953. @t\hskip10pt@>@!was_free: packed array [mem_min..mem_max] of boolean;
  2954. {previously free cells}
  2955. @t\hskip10pt@>@!was_mem_end,@!was_lo_max,@!was_hi_min: pointer;
  2956. {previous |mem_end|, |lo_mem_max|, and |hi_mem_min|}
  2957. @t\hskip10pt@>@!panicking:boolean; {do we want to check memory constantly?}
  2958. gubed
  2959. @ @<Set initial...@>=
  2960. @!debug was_mem_end:=mem_min; {indicate that everything was previously free}
  2961. was_lo_max:=mem_min; was_hi_min:=mem_max;
  2962. panicking:=false;
  2963. gubed
  2964. @ Procedure |check_mem| makes sure that the available space lists of
  2965. |mem| are well formed, and it optionally prints out all locations
  2966. that are reserved now but were free the last time this procedure was called.
  2967. @p @!debug procedure check_mem(@!print_locs : boolean);
  2968. label done1,done2; {loop exits}
  2969. var p,@!q:pointer; {current locations of interest in |mem|}
  2970. @!clobbered:boolean; {is something amiss?}
  2971. begin for p:=mem_min to lo_mem_max do free[p]:=false; {you can probably
  2972. do this faster}
  2973. for p:=hi_mem_min to mem_end do free[p]:=false; {ditto}
  2974. @<Check single-word |avail| list@>;
  2975. @<Check variable-size |avail| list@>;
  2976. @<Check flags of unavailable nodes@>;
  2977. if print_locs then @<Print newly busy locations@>;
  2978. for p:=mem_min to lo_mem_max do was_free[p]:=free[p];
  2979. for p:=hi_mem_min to mem_end do was_free[p]:=free[p];
  2980. {|was_free:=free| might be faster}
  2981. was_mem_end:=mem_end; was_lo_max:=lo_mem_max; was_hi_min:=hi_mem_min;
  2982. end;
  2983. gubed
  2984. @ @<Check single-word...@>=
  2985. p:=avail; q:=null; clobbered:=false;
  2986. while p<>null do
  2987. begin if (p>mem_end)or(p<hi_mem_min) then clobbered:=true
  2988. else if free[p] then clobbered:=true;
  2989. if clobbered then
  2990. begin print_nl("AVAIL list clobbered at ");
  2991. @.AVAIL list clobbered...@>
  2992. print_int(q); goto done1;
  2993. end;
  2994. free[p]:=true; q:=p; p:=link(q);
  2995. end;
  2996. done1:
  2997. @ @<Check variable-size...@>=
  2998. p:=rover; q:=null; clobbered:=false;
  2999. repeat if (p>=lo_mem_max)or(p<mem_min) then clobbered:=true
  3000. else if (rlink(p)>=lo_mem_max)or(rlink(p)<mem_min) then clobbered:=true
  3001. else if not(is_empty(p))or(node_size(p)<2)or@|
  3002. (p+node_size(p)>lo_mem_max)or@| (llink(rlink(p))<>p) then clobbered:=true;
  3003. if clobbered then
  3004. begin print_nl("Double-AVAIL list clobbered at ");
  3005. print_int(q); goto done2;
  3006. end;
  3007. for q:=p to p+node_size(p)-1 do {mark all locations free}
  3008. begin if free[q] then
  3009. begin print_nl("Doubly free location at ");
  3010. @.Doubly free location...@>
  3011. print_int(q); goto done2;
  3012. end;
  3013. free[q]:=true;
  3014. end;
  3015. q:=p; p:=rlink(p);
  3016. until p=rover;
  3017. done2:
  3018. @ @<Check flags...@>=
  3019. p:=mem_min;
  3020. while p<=lo_mem_max do {node |p| should not be empty}
  3021. begin if is_empty(p) then
  3022. begin print_nl("Bad flag at "); print_int(p);
  3023. @.Bad flag...@>
  3024. end;
  3025. while (p<=lo_mem_max) and not free[p] do incr(p);
  3026. while (p<=lo_mem_max) and free[p] do incr(p);
  3027. end
  3028. @ @<Print newly busy...@>=
  3029. begin print_nl("New busy locs:");
  3030. for p:=mem_min to lo_mem_max do
  3031. if not free[p] and ((p>was_lo_max) or was_free[p]) then
  3032. begin print_char(" "); print_int(p);
  3033. end;
  3034. for p:=hi_mem_min to mem_end do
  3035. if not free[p] and
  3036. ((p<was_hi_min) or (p>was_mem_end) or was_free[p]) then
  3037. begin print_char(" "); print_int(p);
  3038. end;
  3039. end
  3040. @ The |search_mem| procedure attempts to answer the question ``Who points
  3041. to node~|p|?'' In doing so, it fetches |link| and |info| fields of |mem|
  3042. that might not be of type |two_halves|. Strictly speaking, this is
  3043. @^dirty \PASCAL@>
  3044. undefined in \PASCAL, and it can lead to ``false drops'' (words that seem to
  3045. point to |p| purely by coincidence). But for debugging purposes, we want
  3046. to rule out the places that do {\sl not\/} point to |p|, so a few false
  3047. drops are tolerable.
  3048. @p @!debug procedure search_mem(@!p:pointer); {look for pointers to |p|}
  3049. var q:integer; {current position being searched}
  3050. begin for q:=mem_min to lo_mem_max do
  3051. begin if link(q)=p then
  3052. begin print_nl("LINK("); print_int(q); print_char(")");
  3053. end;
  3054. if info(q)=p then
  3055. begin print_nl("INFO("); print_int(q); print_char(")");
  3056. end;
  3057. end;
  3058. for q:=hi_mem_min to mem_end do
  3059. begin if link(q)=p then
  3060. begin print_nl("LINK("); print_int(q); print_char(")");
  3061. end;
  3062. if info(q)=p then
  3063. begin print_nl("INFO("); print_int(q); print_char(")");
  3064. end;
  3065. end;
  3066. @<Search |eqtb| for equivalents equal to |p|@>;
  3067. @<Search |save_stack| for equivalents that point to |p|@>;
  3068. @<Search |hyph_list| for pointers to |p|@>;
  3069. end;
  3070. gubed
  3071. @* \[12] Displaying boxes.
  3072. We can reinforce our knowledge of the data structures just introduced
  3073. by considering two procedures that display a list in symbolic form.
  3074. The first of these, called |short_display|, is used in ``overfull box''
  3075. messages to give the top-level description of a list. The other one,
  3076. called |show_node_list|, prints a detailed description of exactly what
  3077. is in the data structure.
  3078. The philosophy of |short_display| is to ignore the fine points about exactly
  3079. what is inside boxes, except that ligatures and discretionary breaks are
  3080. expanded. As a result, |short_display| is a recursive procedure, but the
  3081. recursion is never more than one level deep.
  3082. @^recursion@>
  3083. A global variable |font_in_short_display| keeps track of the font code that
  3084. is assumed to be present when |short_display| begins; deviations from this
  3085. font will be printed.
  3086. @<Glob...@>=
  3087. @!font_in_short_display:integer; {an internal font number}
  3088. @ Boxes, rules, inserts, whatsits, marks, and things in general that are
  3089. sort of ``complicated'' are indicated only by printing `\.{[]}'.
  3090. @p procedure short_display(@!p:integer); {prints highlights of list |p|}
  3091. var n:integer; {for replacement counts}
  3092. begin while p>mem_min do
  3093. begin if is_char_node(p) then
  3094. begin if p<=mem_end then
  3095. begin if font(p)<>font_in_short_display then
  3096. begin if (font(p)<font_base)or(font(p)>font_max) then
  3097. print_char("*")
  3098. @.*\relax@>
  3099. else @<Print the font identifier for |font(p)|@>;
  3100. print_char(" "); font_in_short_display:=font(p);
  3101. end;
  3102. print_ASCII(qo(character(p)));
  3103. end;
  3104. end
  3105. else @<Print a short indication of the contents of node |p|@>;
  3106. p:=link(p);
  3107. end;
  3108. end;
  3109. @ @<Print a short indication of the contents of node |p|@>=
  3110. case type(p) of
  3111. hlist_node,vlist_node,ins_node,whatsit_node,mark_node,adjust_node,
  3112. unset_node: print("[]");
  3113. rule_node: print_char("|");
  3114. glue_node: if glue_ptr(p)<>zero_glue then print_char(" ");
  3115. math_node: print_char("$");
  3116. ligature_node: short_display(lig_ptr(p));
  3117. disc_node: begin short_display(pre_break(p));
  3118. short_display(post_break(p));@/
  3119. n:=replace_count(p);
  3120. while n>0 do
  3121. begin if link(p)<>null then p:=link(p);
  3122. decr(n);
  3123. end;
  3124. end;
  3125. othercases do_nothing
  3126. endcases
  3127. @ The |show_node_list| routine requires some auxiliary subroutines: one to
  3128. print a font-and-character combination, one to print a token list without
  3129. its reference count, and one to print a rule dimension.
  3130. @p procedure print_font_and_char(@!p:integer); {prints |char_node| data}
  3131. begin if p>mem_end then print_esc("CLOBBERED.")
  3132. else begin if (font(p)<font_base)or(font(p)>font_max) then print_char("*")
  3133. @.*\relax@>
  3134. else @<Print the font identifier for |font(p)|@>;
  3135. print_char(" "); print_ASCII(qo(character(p)));
  3136. end;
  3137. end;
  3138. @#
  3139. procedure print_mark(@!p:integer); {prints token list data in braces}
  3140. begin print_char("{");
  3141. if (p<hi_mem_min)or(p>mem_end) then print_esc("CLOBBERED.")
  3142. else show_token_list(link(p),null,max_print_line-10);
  3143. print_char("}");
  3144. end;
  3145. @#
  3146. procedure print_rule_dimen(@!d:scaled); {prints dimension in rule node}
  3147. begin if is_running(d) then print_char("*") else print_scaled(d);
  3148. @.*\relax@>
  3149. end;
  3150. @ Then there is a subroutine that prints glue stretch and shrink, possibly
  3151. followed by the name of finite units:
  3152. @p procedure print_glue(@!d:scaled;@!order:integer;@!s:str_number);
  3153. {prints a glue component}
  3154. begin print_scaled(d);
  3155. if (order<normal)or(order>filll) then print("foul")
  3156. else if order>normal then
  3157. begin print("fil");
  3158. while order>fil do
  3159. begin print_char("l"); decr(order);
  3160. end;
  3161. end
  3162. else if s<>0 then print(s);
  3163. end;
  3164. @ The next subroutine prints a whole glue specification.
  3165. @p procedure print_spec(@!p:integer;@!s:str_number);
  3166. {prints a glue specification}
  3167. begin if (p<mem_min)or(p>=lo_mem_max) then print_char("*")
  3168. @.*\relax@>
  3169. else begin print_scaled(width(p));
  3170. if s<>0 then print(s);
  3171. if stretch(p)<>0 then
  3172. begin print(" plus "); print_glue(stretch(p),stretch_order(p),s);
  3173. end;
  3174. if shrink(p)<>0 then
  3175. begin print(" minus "); print_glue(shrink(p),shrink_order(p),s);
  3176. end;
  3177. end;
  3178. end;
  3179. @ We also need to declare some procedures that appear later in this
  3180. documentation.
  3181. @p @<Declare procedures needed for displaying the elements of mlists@>@;
  3182. @<Declare the procedure called |print_skip_param|@>
  3183. @ Since boxes can be inside of boxes, |show_node_list| is inherently recursive,
  3184. @^recursion@>
  3185. up to a given maximum number of levels. The history of nesting is indicated
  3186. by the current string, which will be printed at the beginning of each line;
  3187. the length of this string, namely |cur_length|, is the depth of nesting.
  3188. Recursive calls on |show_node_list| therefore use the following pattern:
  3189. @d node_list_display(#)==
  3190. begin append_char("."); show_node_list(#); flush_char;
  3191. end {|str_room| need not be checked; see |show_box| below}
  3192. @ A global variable called |depth_threshold| is used to record the maximum
  3193. depth of nesting for which |show_node_list| will show information. If we
  3194. have |depth_threshold=0|, for example, only the top level information will
  3195. be given and no sublists will be traversed. Another global variable, called
  3196. |breadth_max|, tells the maximum number of items to show at each level;
  3197. |breadth_max| had better be positive, or you won't see anything.
  3198. @<Glob...@>=
  3199. @!depth_threshold : integer; {maximum nesting depth in box displays}
  3200. @!breadth_max : integer; {maximum number of items shown at the same list level}
  3201. @ Now we are ready for |show_node_list| itself. This procedure has been
  3202. written to be ``extra robust'' in the sense that it should not crash or get
  3203. into a loop even if the data structures have been messed up by bugs in
  3204. the rest of the program. You can safely call its parent routine
  3205. |show_box(p)| for arbitrary values of |p| when you are debugging \TeX.
  3206. However, in the presence of bad data, the procedure may
  3207. @^dirty \PASCAL@>@^debugging@>
  3208. fetch a |memory_word| whose variant is different from the way it was stored;
  3209. for example, it might try to read |mem[p].hh| when |mem[p]|
  3210. contains a scaled integer, if |p| is a pointer that has been
  3211. clobbered or chosen at random.
  3212. @p procedure show_node_list(@!p:integer); {prints a node list symbolically}
  3213. label exit;
  3214. var n:integer; {the number of items already printed at this level}
  3215. @!g:real; {a glue ratio, as a floating point number}
  3216. begin if cur_length>depth_threshold then
  3217. begin if p>null then print(" []");
  3218. {indicate that there's been some truncation}
  3219. return;
  3220. end;
  3221. n:=0;
  3222. while p>mem_min do
  3223. begin print_ln; print_current_string; {display the nesting history}
  3224. if p>mem_end then {pointer out of range}
  3225. begin print("Bad link, display aborted."); return;
  3226. @.Bad link...@>
  3227. end;
  3228. incr(n); if n>breadth_max then {time to stop}
  3229. begin print("etc."); return;
  3230. @.etc@>
  3231. end;
  3232. @<Display node |p|@>;
  3233. p:=link(p);
  3234. end;
  3235. exit:
  3236. end;
  3237. @ @<Display node |p|@>=
  3238. if is_char_node(p) then print_font_and_char(p)
  3239. else case type(p) of
  3240. hlist_node,vlist_node,unset_node: @<Display box |p|@>;
  3241. rule_node: @<Display rule |p|@>;
  3242. ins_node: @<Display insertion |p|@>;
  3243. whatsit_node: @<Display the whatsit node |p|@>;
  3244. glue_node: @<Display glue |p|@>;
  3245. kern_node: @<Display kern |p|@>;
  3246. math_node: @<Display math node |p|@>;
  3247. ligature_node: @<Display ligature |p|@>;
  3248. penalty_node: @<Display penalty |p|@>;
  3249. disc_node: @<Display discretionary |p|@>;
  3250. mark_node: @<Display mark |p|@>;
  3251. adjust_node: @<Display adjustment |p|@>;
  3252. @t\4@>@<Cases of |show_node_list| that arise in mlists only@>@;
  3253. othercases print("Unknown node type!")
  3254. endcases
  3255. @ @<Display box |p|@>=
  3256. begin if type(p)=hlist_node then print_esc("h")
  3257. else if type(p)=vlist_node then print_esc("v")
  3258. else print_esc("unset");
  3259. print("box("); print_scaled(height(p)); print_char("+");
  3260. print_scaled(depth(p)); print(")x"); print_scaled(width(p));
  3261. if type(p)=unset_node then
  3262. @<Display special fields of the unset node |p|@>
  3263. else begin @<Display the value of |glue_set(p)|@>;
  3264. if shift_amount(p)<>0 then
  3265. begin print(", shifted "); print_scaled(shift_amount(p));
  3266. end;
  3267. end;
  3268. node_list_display(list_ptr(p)); {recursive call}
  3269. end
  3270. @ @<Display special fields of the unset node |p|@>=
  3271. begin if span_count(p)<>min_quarterword then
  3272. begin print(" ("); print_int(qo(span_count(p))+1);
  3273. print(" columns)");
  3274. end;
  3275. if glue_stretch(p)<>0 then
  3276. begin print(", stretch "); print_glue(glue_stretch(p),glue_order(p),0);
  3277. end;
  3278. if glue_shrink(p)<>0 then
  3279. begin print(", shrink "); print_glue(glue_shrink(p),glue_sign(p),0);
  3280. end;
  3281. end
  3282. @ The code will have to change in this place if |glue_ratio| is
  3283. a structured type instead of an ordinary |real|. Note that this routine
  3284. should avoid arithmetic errors even if the |glue_set| field holds an
  3285. arbitrary random value. The following code assumes that a properly
  3286. formed nonzero |real| number has absolute value $2^{20}$ or more when
  3287. it is regarded as an integer; this precaution was adequate to prevent
  3288. floating point underflow on the author's computer.
  3289. @^system dependencies@>
  3290. @^dirty \PASCAL@>
  3291. @<Display the value of |glue_set(p)|@>=
  3292. g:=float(glue_set(p));
  3293. if (g<>float_constant(0))and(glue_sign(p)<>normal) then
  3294. begin print(", glue set ");
  3295. if glue_sign(p)=shrinking then print("- ");
  3296. if abs(mem[p+glue_offset].int)<@'4000000 then print("?.?")
  3297. else if abs(g)>float_constant(20000) then
  3298. begin if g>float_constant(0) then print_char(">")
  3299. else print("< -");
  3300. print_glue(20000*unity,glue_order(p),0);
  3301. end
  3302. else print_glue(round(unity*g),glue_order(p),0);
  3303. @^real multiplication@>
  3304. end
  3305. @ @<Display rule |p|@>=
  3306. begin print_esc("rule("); print_rule_dimen(height(p)); print_char("+");
  3307. print_rule_dimen(depth(p)); print(")x"); print_rule_dimen(width(p));
  3308. end
  3309. @ @<Display insertion |p|@>=
  3310. begin print_esc("insert"); print_int(qo(subtype(p)));
  3311. print(", natural size "); print_scaled(height(p));
  3312. print("; split("); print_spec(split_top_ptr(p),0);
  3313. print_char(","); print_scaled(depth(p));
  3314. print("); float cost "); print_int(float_cost(p));
  3315. node_list_display(ins_ptr(p)); {recursive call}
  3316. end
  3317. @ @<Display glue |p|@>=
  3318. if subtype(p)>=a_leaders then @<Display leaders |p|@>
  3319. else begin print_esc("glue");
  3320. if subtype(p)<>normal then
  3321. begin print_char("(");
  3322. if subtype(p)<cond_math_glue then
  3323. print_skip_param(subtype(p)-1)
  3324. else if subtype(p)=cond_math_glue then print_esc("nonscript")
  3325. else print_esc("mskip");
  3326. print_char(")");
  3327. end;
  3328. if subtype(p)<>cond_math_glue then
  3329. begin print_char(" ");
  3330. if subtype(p)<cond_math_glue then print_spec(glue_ptr(p),0)
  3331. else print_spec(glue_ptr(p),"mu");
  3332. end;
  3333. end
  3334. @ @<Display leaders |p|@>=
  3335. begin print_esc("");
  3336. if subtype(p)=c_leaders then print_char("c")
  3337. else if subtype(p)=x_leaders then print_char("x");
  3338. print("leaders "); print_spec(glue_ptr(p),0);
  3339. node_list_display(leader_ptr(p)); {recursive call}
  3340. end
  3341. @ An ``explicit'' kern value is indicated implicitly by an explicit space.
  3342. @<Display kern |p|@>=
  3343. if subtype(p)<>mu_glue then
  3344. begin print_esc("kern");
  3345. if subtype(p)<>normal then print_char(" ");
  3346. print_scaled(width(p));
  3347. if subtype(p)=acc_kern then print(" (for accent)");
  3348. @.for accent@>
  3349. end
  3350. else begin print_esc("mkern"); print_scaled(width(p)); print("mu");
  3351. end
  3352. @ @<Display math node |p|@>=
  3353. begin print_esc("math");
  3354. if subtype(p)=before then print("on")
  3355. else print("off");
  3356. if width(p)<>0 then
  3357. begin print(", surrounded "); print_scaled(width(p));
  3358. end;
  3359. end
  3360. @ @<Display ligature |p|@>=
  3361. begin print_font_and_char(lig_char(p)); print(" (ligature ");
  3362. if subtype(p)>1 then print_char("|");
  3363. font_in_short_display:=font(lig_char(p)); short_display(lig_ptr(p));
  3364. if odd(subtype(p)) then print_char("|");
  3365. print_char(")");
  3366. end
  3367. @ @<Display penalty |p|@>=
  3368. begin print_esc("penalty "); print_int(penalty(p));
  3369. end
  3370. @ The |post_break| list of a discretionary node is indicated by a prefixed
  3371. `\.{\char'174}' instead of the `\..' before the |pre_break| list.
  3372. @<Display discretionary |p|@>=
  3373. begin print_esc("discretionary");
  3374. if replace_count(p)>0 then
  3375. begin print(" replacing "); print_int(replace_count(p));
  3376. end;
  3377. node_list_display(pre_break(p)); {recursive call}
  3378. append_char("|"); show_node_list(post_break(p)); flush_char; {recursive call}
  3379. end
  3380. @ @<Display mark |p|@>=
  3381. begin print_esc("mark"); print_mark(mark_ptr(p));
  3382. end
  3383. @ @<Display adjustment |p|@>=
  3384. begin print_esc("vadjust"); node_list_display(adjust_ptr(p)); {recursive call}
  3385. end
  3386. @ The recursive machinery is started by calling |show_box|.
  3387. @^recursion@>
  3388. @p procedure show_box(@!p:pointer);
  3389. begin @<Assign the values |depth_threshold:=show_box_depth| and
  3390. |breadth_max:=show_box_breadth|@>;
  3391. if breadth_max<=0 then breadth_max:=5;
  3392. if pool_ptr+depth_threshold>=pool_size then
  3393. depth_threshold:=pool_size-pool_ptr-1;
  3394. {now there's enough room for prefix string}
  3395. show_node_list(p); {the show starts at |p|}
  3396. print_ln;
  3397. end;
  3398. @* \[13] Destroying boxes.
  3399. When we are done with a node list, we are obliged to return it to free
  3400. storage, including all of its sublists. The recursive procedure
  3401. |flush_node_list| does this for us.
  3402. @ First, however, we shall consider two non-recursive procedures that do
  3403. simpler tasks. The first of these, |delete_token_ref|, is called when
  3404. a pointer to a token list's reference count is being removed. This means
  3405. that the token list should disappear if the reference count was |null|,
  3406. otherwise the count should be decreased by one.
  3407. @^reference counts@>
  3408. @d token_ref_count(#) == info(#) {reference count preceding a token list}
  3409. @p procedure delete_token_ref(@!p:pointer); {|p| points to the reference count
  3410. of a token list that is losing one reference}
  3411. begin if token_ref_count(p)=null then flush_list(p)
  3412. else decr(token_ref_count(p));
  3413. end;
  3414. @ Similarly, |delete_glue_ref| is called when a pointer to a glue
  3415. specification is being withdrawn.
  3416. @^reference counts@>
  3417. @d fast_delete_glue_ref(#)==@t@>@;@/
  3418. begin if glue_ref_count(#)=null then free_node(#,glue_spec_size)
  3419. else decr(glue_ref_count(#));
  3420. end
  3421. @p procedure delete_glue_ref(@!p:pointer); {|p| points to a glue specification}
  3422. fast_delete_glue_ref(p);
  3423. @ Now we are ready to delete any node list, recursively.
  3424. In practice, the nodes deleted are usually charnodes (about 2/3 of the time),
  3425. and they are glue nodes in about half of the remaining cases.
  3426. @^recursion@>
  3427. @p procedure flush_node_list(@!p:pointer); {erase list of nodes starting at |p|}
  3428. label done; {go here when node |p| has been freed}
  3429. var q:pointer; {successor to node |p|}
  3430. begin while p<>null do
  3431. @^inner loop@>
  3432. begin q:=link(p);
  3433. if is_char_node(p) then free_avail(p)
  3434. else begin case type(p) of
  3435. hlist_node,vlist_node,unset_node: begin flush_node_list(list_ptr(p));
  3436. free_node(p,box_node_size); goto done;
  3437. end;
  3438. rule_node: begin free_node(p,rule_node_size); goto done;
  3439. end;
  3440. ins_node: begin flush_node_list(ins_ptr(p));
  3441. delete_glue_ref(split_top_ptr(p));
  3442. free_node(p,ins_node_size); goto done;
  3443. end;
  3444. whatsit_node: @<Wipe out the whatsit node |p| and |goto done|@>;
  3445. glue_node: begin fast_delete_glue_ref(glue_ptr(p));
  3446. if leader_ptr(p)<>null then flush_node_list(leader_ptr(p));
  3447. end;
  3448. kern_node,math_node,penalty_node: do_nothing;
  3449. ligature_node: flush_node_list(lig_ptr(p));
  3450. mark_node: delete_token_ref(mark_ptr(p));
  3451. disc_node: begin flush_node_list(pre_break(p));
  3452. flush_node_list(post_break(p));
  3453. end;
  3454. adjust_node: flush_node_list(adjust_ptr(p));
  3455. @t\4@>@<Cases of |flush_node_list| that arise in mlists only@>@;
  3456. othercases confusion("flushing")
  3457. @:this can't happen flushing}{\quad flushing@>
  3458. endcases;@/
  3459. free_node(p,small_node_size);
  3460. done:end;
  3461. p:=q;
  3462. end;
  3463. end;
  3464. @* \[14] Copying boxes.
  3465. Another recursive operation that acts on boxes is sometimes needed: The
  3466. procedure |copy_node_list| returns a pointer to another node list that has
  3467. the same structure and meaning as the original. Note that since glue
  3468. specifications and token lists have reference counts, we need not make
  3469. copies of them. Reference counts can never get too large to fit in a
  3470. halfword, since each pointer to a node is in a different memory address,
  3471. and the total number of memory addresses fits in a halfword.
  3472. @^recursion@>
  3473. @^reference counts@>
  3474. (Well, there actually are also references from outside |mem|; if the
  3475. |save_stack| is made arbitrarily large, it would theoretically be possible
  3476. to break \TeX\ by overflowing a reference count. But who would want to do that?)
  3477. @d add_token_ref(#)==incr(token_ref_count(#)) {new reference to a token list}
  3478. @d add_glue_ref(#)==incr(glue_ref_count(#)) {new reference to a glue spec}
  3479. @ The copying procedure copies words en masse without bothering
  3480. to look at their individual fields. If the node format changes---for
  3481. example, if the size is altered, or if some link field is moved to another
  3482. relative position---then this code may need to be changed too.
  3483. @^data structure assumptions@>
  3484. @p function copy_node_list(@!p:pointer):pointer; {makes a duplicate of the
  3485. node list that starts at |p| and returns a pointer to the new list}
  3486. var h:pointer; {temporary head of copied list}
  3487. @!q:pointer; {previous position in new list}
  3488. @!r:pointer; {current node being fabricated for new list}
  3489. @!words:0..5; {number of words remaining to be copied}
  3490. begin h:=get_avail; q:=h;
  3491. while p<>null do
  3492. begin @<Make a copy of node |p| in node |r|@>;
  3493. link(q):=r; q:=r; p:=link(p);
  3494. end;
  3495. link(q):=null; q:=link(h); free_avail(h);
  3496. copy_node_list:=q;
  3497. end;
  3498. @ @<Make a copy of node |p|...@>=
  3499. words:=1; {this setting occurs in more branches than any other}
  3500. if is_char_node(p) then r:=get_avail
  3501. else @<Case statement to copy different types and set |words| to the number
  3502. of initial words not yet copied@>;
  3503. while words>0 do
  3504. begin decr(words); mem[r+words]:=mem[p+words];
  3505. end
  3506. @ @<Case statement to copy...@>=
  3507. case type(p) of
  3508. hlist_node,vlist_node,unset_node: begin r:=get_node(box_node_size);
  3509. mem[r+6]:=mem[p+6]; mem[r+5]:=mem[p+5]; {copy the last two words}
  3510. list_ptr(r):=copy_node_list(list_ptr(p)); {this affects |mem[r+5]|}
  3511. words:=5;
  3512. end;
  3513. rule_node: begin r:=get_node(rule_node_size); words:=rule_node_size;
  3514. end;
  3515. ins_node: begin r:=get_node(ins_node_size); mem[r+4]:=mem[p+4];
  3516. add_glue_ref(split_top_ptr(p));
  3517. ins_ptr(r):=copy_node_list(ins_ptr(p)); {this affects |mem[r+4]|}
  3518. words:=ins_node_size-1;
  3519. end;
  3520. whatsit_node:@<Make a partial copy of the whatsit node |p| and make |r|
  3521. point to it; set |words| to the number of initial words not yet copied@>;
  3522. glue_node: begin r:=get_node(small_node_size); add_glue_ref(glue_ptr(p));
  3523. glue_ptr(r):=glue_ptr(p); leader_ptr(r):=copy_node_list(leader_ptr(p));
  3524. end;
  3525. kern_node,math_node,penalty_node: begin r:=get_node(small_node_size);
  3526. words:=small_node_size;
  3527. end;
  3528. ligature_node: begin r:=get_node(small_node_size);
  3529. mem[lig_char(r)]:=mem[lig_char(p)]; {copy |font| and |character|}
  3530. lig_ptr(r):=copy_node_list(lig_ptr(p));
  3531. end;
  3532. disc_node: begin r:=get_node(small_node_size);
  3533. pre_break(r):=copy_node_list(pre_break(p));
  3534. post_break(r):=copy_node_list(post_break(p));
  3535. end;
  3536. mark_node: begin r:=get_node(small_node_size); add_token_ref(mark_ptr(p));
  3537. words:=small_node_size;
  3538. end;
  3539. adjust_node: begin r:=get_node(small_node_size);
  3540. adjust_ptr(r):=copy_node_list(adjust_ptr(p));
  3541. end; {|words=1=small_node_size-1|}
  3542. othercases confusion("copying")
  3543. @:this can't happen copying}{\quad copying@>
  3544. endcases
  3545. @* \[15] The command codes.
  3546. Before we can go any further, we need to define symbolic names for the internal
  3547. code numbers that represent the various commands obeyed by \TeX. These codes
  3548. are somewhat arbitrary, but not completely so. For example, the command
  3549. codes for character types are fixed by the language, since a user says,
  3550. e.g., `\.{\\catcode \`\\\${} = 3}' to make \.{\char'44} a math delimiter,
  3551. and the command code |math_shift| is equal to~3. Some other codes have
  3552. been made adjacent so that |case| statements in the program need not consider
  3553. cases that are widely spaced, or so that |case| statements can be replaced
  3554. by |if| statements.
  3555. At any rate, here is the list, for future reference. First come the
  3556. ``catcode'' commands, several of which share their numeric codes with
  3557. ordinary commands when the catcode cannot emerge from \TeX's scanning routine.
  3558. @d escape=0 {escape delimiter (called \.\\ in {\sl The \TeX book\/})}
  3559. @:TeXbook}{\sl The \TeX book@>
  3560. @d relax=0 {do nothing ( \.{\\relax} )}
  3561. @d left_brace=1 {beginning of a group ( \.\{ )}
  3562. @d right_brace=2 {ending of a group ( \.\} )}
  3563. @d math_shift=3 {mathematics shift character ( \.\$ )}
  3564. @d tab_mark=4 {alignment delimiter ( \.\&, \.{\\span} )}
  3565. @d car_ret=5 {end of line ( |carriage_return|, \.{\\cr}, \.{\\crcr} )}
  3566. @d out_param=5 {output a macro parameter}
  3567. @d mac_param=6 {macro parameter symbol ( \.\# )}
  3568. @d sup_mark=7 {superscript ( \.{\char'136} )}
  3569. @d sub_mark=8 {subscript ( \.{\char'137} )}
  3570. @d ignore=9 {characters to ignore ( \.{\^\^@@} )}
  3571. @d endv=9 {end of \<v_j> list in alignment template}
  3572. @d spacer=10 {characters equivalent to blank space ( \.{\ } )}
  3573. @d letter=11 {characters regarded as letters ( \.{A..Z}, \.{a..z} )}
  3574. @d other_char=12 {none of the special character types}
  3575. @d active_char=13 {characters that invoke macros ( \.{\char`\~} )}
  3576. @d par_end=13 {end of paragraph ( \.{\\par} )}
  3577. @d match=13 {match a macro parameter}
  3578. @d comment=14 {characters that introduce comments ( \.\% )}
  3579. @d end_match=14 {end of parameters to macro}
  3580. @d stop=14 {end of job ( \.{\\end}, \.{\\dump} )}
  3581. @d invalid_char=15 {characters that shouldn't appear ( \.{\^\^?} )}
  3582. @d delim_num=15 {specify delimiter numerically ( \.{\\delimiter} )}
  3583. @d max_char_code=15 {largest catcode for individual characters}
  3584. @ Next are the ordinary run-of-the-mill command codes. Codes that are
  3585. |min_internal| or more represent internal quantities that might be
  3586. expanded by `\.{\\the}'.
  3587. @d char_num=16 {character specified numerically ( \.{\\char} )}
  3588. @d math_char_num=17 {explicit math code ( \.{\\mathchar} )}
  3589. @d mark=18 {mark definition ( \.{\\mark} )}
  3590. @d xray=19 {peek inside of \TeX\ ( \.{\\show}, \.{\\showbox}, etc.~)}
  3591. @d make_box=20 {make a box ( \.{\\box}, \.{\\copy}, \.{\\hbox}, etc.~)}
  3592. @d hmove=21 {horizontal motion ( \.{\\moveleft}, \.{\\moveright} )}
  3593. @d vmove=22 {vertical motion ( \.{\\raise}, \.{\\lower} )}
  3594. @d un_hbox=23 {unglue a box ( \.{\\unhbox}, \.{\\unhcopy} )}
  3595. @d un_vbox=24 {unglue a box ( \.{\\unvbox}, \.{\\unvcopy} )}
  3596. @d remove_item=25 {nullify last item ( \.{\\unpenalty},
  3597. \.{\\unkern}, \.{\\unskip} )}
  3598. @d hskip=26 {horizontal glue ( \.{\\hskip}, \.{\\hfil}, etc.~)}
  3599. @d vskip=27 {vertical glue ( \.{\\vskip}, \.{\\vfil}, etc.~)}
  3600. @d mskip=28 {math glue ( \.{\\mskip} )}
  3601. @d kern=29 {fixed space ( \.{\\kern} )}
  3602. @d mkern=30 {math kern ( \.{\\mkern} )}
  3603. @d leader_ship=31 {use a box ( \.{\\shipout}, \.{\\leaders}, etc.~)}
  3604. @d halign=32 {horizontal table alignment ( \.{\\halign} )}
  3605. @d valign=33 {vertical table alignment ( \.{\\valign} )}
  3606. @d no_align=34 {temporary escape from alignment ( \.{\\noalign} )}
  3607. @d vrule=35 {vertical rule ( \.{\\vrule} )}
  3608. @d hrule=36 {horizontal rule ( \.{\\hrule} )}
  3609. @d insert=37 {vlist inserted in box ( \.{\\insert} )}
  3610. @d vadjust=38 {vlist inserted in enclosing paragraph ( \.{\\vadjust} )}
  3611. @d ignore_spaces=39 {gobble |spacer| tokens ( \.{\\ignorespaces} )}
  3612. @d after_assignment=40 {save till assignment is done ( \.{\\afterassignment} )}
  3613. @d after_group=41 {save till group is done ( \.{\\aftergroup} )}
  3614. @d break_penalty=42 {additional badness ( \.{\\penalty} )}
  3615. @d start_par=43 {begin paragraph ( \.{\\indent}, \.{\\noindent} )}
  3616. @d ital_corr=44 {italic correction ( \.{\\/} )}
  3617. @d accent=45 {attach accent in text ( \.{\\accent} )}
  3618. @d math_accent=46 {attach accent in math ( \.{\\mathaccent} )}
  3619. @d discretionary=47 {discretionary texts ( \.{\\-}, \.{\\discretionary} )}
  3620. @d eq_no=48 {equation number ( \.{\\eqno}, \.{\\leqno} )}
  3621. @d left_right=49 {variable delimiter ( \.{\\left}, \.{\\right} )}
  3622. @d math_comp=50 {component of formula ( \.{\\mathbin}, etc.~)}
  3623. @d limit_switch=51 {diddle limit conventions ( \.{\\displaylimits}, etc.~)}
  3624. @d above=52 {generalized fraction ( \.{\\above}, \.{\\atop}, etc.~)}
  3625. @d math_style=53 {style specification ( \.{\\displaystyle}, etc.~)}
  3626. @d math_choice=54 {choice specification ( \.{\\mathchoice} )}
  3627. @d non_script=55 {conditional math glue ( \.{\\nonscript} )}
  3628. @d vcenter=56 {vertically center a vbox ( \.{\\vcenter} )}
  3629. @d case_shift=57 {force specific case ( \.{\\lowercase}, \.{\\uppercase}~)}
  3630. @d message=58 {send to user ( \.{\\message}, \.{\\errmessage} )}
  3631. @d extension=59 {extensions to \TeX\ ( \.{\\write}, \.{\\special}, etc.~)}
  3632. @d in_stream=60 {files for reading ( \.{\\openin}, \.{\\closein} )}
  3633. @d begin_group=61 {begin local grouping ( \.{\\begingroup} )}
  3634. @d end_group=62 {end local grouping ( \.{\\endgroup} )}
  3635. @d omit=63 {omit alignment template ( \.{\\omit} )}
  3636. @d ex_space=64 {explicit space ( \.{\\\ } )}
  3637. @d no_boundary=65 {suppress boundary ligatures ( \.{\\noboundary} )}
  3638. @d radical=66 {square root and similar signs ( \.{\\radical} )}
  3639. @d end_cs_name=67 {end control sequence ( \.{\\endcsname} )}
  3640. @d min_internal=68 {the smallest code that can follow \.{\\the}}
  3641. @d char_given=68 {character code defined by \.{\\chardef}}
  3642. @d math_given=69 {math code defined by \.{\\mathchardef}}
  3643. @d last_item=70 {most recent item ( \.{\\lastpenalty},
  3644. \.{\\lastkern}, \.{\\lastskip} )}
  3645. @d max_non_prefixed_command=70 {largest command code that can't be \.{\\global}}
  3646. @ The next codes are special; they all relate to mode-independent
  3647. assignment of values to \TeX's internal registers or tables.
  3648. Codes that are |max_internal| or less represent internal quantities
  3649. that might be expanded by `\.{\\the}'.
  3650. @d toks_register=71 {token list register ( \.{\\toks} )}
  3651. @d assign_toks=72 {special token list ( \.{\\output}, \.{\\everypar}, etc.~)}
  3652. @d assign_int=73 {user-defined integer ( \.{\\tolerance}, \.{\\day}, etc.~)}
  3653. @d assign_dimen=74 {user-defined length ( \.{\\hsize}, etc.~)}
  3654. @d assign_glue=75 {user-defined glue ( \.{\\baselineskip}, etc.~)}
  3655. @d assign_mu_glue=76 {user-defined muglue ( \.{\\thinmuskip}, etc.~)}
  3656. @d assign_font_dimen=77 {user-defined font dimension ( \.{\\fontdimen} )}
  3657. @d assign_font_int=78 {user-defined font integer ( \.{\\hyphenchar},
  3658. \.{\\skewchar} )}
  3659. @d set_aux=79 {specify state info ( \.{\\spacefactor}, \.{\\prevdepth} )}
  3660. @d set_prev_graf=80 {specify state info ( \.{\\prevgraf} )}
  3661. @d set_page_dimen=81 {specify state info ( \.{\\pagegoal}, etc.~)}
  3662. @d set_page_int=82 {specify state info ( \.{\\deadcycles},
  3663. \.{\\insertpenalties} )}
  3664. @d set_box_dimen=83 {change dimension of box ( \.{\\wd}, \.{\\ht}, \.{\\dp} )}
  3665. @d set_shape=84 {specify fancy paragraph shape ( \.{\\parshape} )}
  3666. @d def_code=85 {define a character code ( \.{\\catcode}, etc.~)}
  3667. @d def_family=86 {declare math fonts ( \.{\\textfont}, etc.~)}
  3668. @d set_font=87 {set current font ( font identifiers )}
  3669. @d def_font=88 {define a font file ( \.{\\font} )}
  3670. @d register=89 {internal register ( \.{\\count}, \.{\\dimen}, etc.~)}
  3671. @d max_internal=89 {the largest code that can follow \.{\\the}}
  3672. @d advance=90 {advance a register or parameter ( \.{\\advance} )}
  3673. @d multiply=91 {multiply a register or parameter ( \.{\\multiply} )}
  3674. @d divide=92 {divide a register or parameter ( \.{\\divide} )}
  3675. @d prefix=93 {qualify a definition ( \.{\\global}, \.{\\long}, \.{\\outer} )}
  3676. @d let=94 {assign a command code ( \.{\\let}, \.{\\futurelet} )}
  3677. @d shorthand_def=95 {code definition ( \.{\\chardef}, \.{\\countdef}, etc.~)}
  3678. @d read_to_cs=96 {read into a control sequence ( \.{\\read} )}
  3679. @d def=97 {macro definition ( \.{\\def}, \.{\\gdef}, \.{\\xdef}, \.{\\edef} )}
  3680. @d set_box=98 {set a box ( \.{\\setbox} )}
  3681. @d hyph_data=99 {hyphenation data ( \.{\\hyphenation}, \.{\\patterns} )}
  3682. @d set_interaction=100 {define level of interaction ( \.{\\batchmode}, etc.~)}
  3683. @d max_command=100 {the largest command code seen at |big_switch|}
  3684. @ The remaining command codes are extra special, since they cannot get through
  3685. \TeX's scanner to the main control routine. They have been given values higher
  3686. than |max_command| so that their special nature is easily discernible.
  3687. The ``expandable'' commands come first.
  3688. @d undefined_cs=max_command+1 {initial state of most |eq_type| fields}
  3689. @d expand_after=max_command+2 {special expansion ( \.{\\expandafter} )}
  3690. @d no_expand=max_command+3 {special nonexpansion ( \.{\\noexpand} )}
  3691. @d input=max_command+4 {input a source file ( \.{\\input}, \.{\\endinput} )}
  3692. @d if_test=max_command+5 {conditional text ( \.{\\if}, \.{\\ifcase}, etc.~)}
  3693. @d fi_or_else=max_command+6 {delimiters for conditionals ( \.{\\else}, etc.~)}
  3694. @d cs_name=max_command+7 {make a control sequence from tokens ( \.{\\csname} )}
  3695. @d convert=max_command+8 {convert to text ( \.{\\number}, \.{\\string}, etc.~)}
  3696. @d the=max_command+9 {expand an internal quantity ( \.{\\the} )}
  3697. @d top_bot_mark=max_command+10 {inserted mark ( \.{\\topmark}, etc.~)}
  3698. @d call=max_command+11 {non-long, non-outer control sequence}
  3699. @d long_call=max_command+12 {long, non-outer control sequence}
  3700. @d outer_call=max_command+13 {non-long, outer control sequence}
  3701. @d long_outer_call=max_command+14 {long, outer control sequence}
  3702. @d end_template=max_command+15 {end of an alignment template}
  3703. @d dont_expand=max_command+16 {the following token was marked by \.{\\noexpand}}
  3704. @d glue_ref=max_command+17 {the equivalent points to a glue specification}
  3705. @d shape_ref=max_command+18 {the equivalent points to a parshape specification}
  3706. @d box_ref=max_command+19 {the equivalent points to a box node, or is |null|}
  3707. @d data=max_command+20 {the equivalent is simply a halfword number}
  3708. @* \[16] The semantic nest.
  3709. \TeX\ is typically in the midst of building many lists at once. For example,
  3710. when a math formula is being processed, \TeX\ is in math mode and
  3711. working on an mlist; this formula has temporarily interrupted \TeX\ from
  3712. being in horizontal mode and building the hlist of a paragraph; and this
  3713. paragraph has temporarily interrupted \TeX\ from being in vertical mode
  3714. and building the vlist for the next page of a document. Similarly, when a
  3715. \.{\\vbox} occurs inside of an \.{\\hbox}, \TeX\ is temporarily
  3716. interrupted from working in restricted horizontal mode, and it enters
  3717. internal vertical mode. The ``semantic nest'' is a stack that
  3718. keeps track of what lists and modes are currently suspended.
  3719. At each level of processing we are in one of six modes:
  3720. \yskip\hang|vmode| stands for vertical mode (the page builder);
  3721. \hang|hmode| stands for horizontal mode (the paragraph builder);
  3722. \hang|mmode| stands for displayed formula mode;
  3723. \hang|-vmode| stands for internal vertical mode (e.g., in a \.{\\vbox});
  3724. \hang|-hmode| stands for restricted horizontal mode (e.g., in an \.{\\hbox});
  3725. \hang|-mmode| stands for math formula mode (not displayed).
  3726. \yskip\noindent The mode is temporarily set to zero while processing \.{\\write}
  3727. texts.
  3728. Numeric values are assigned to |vmode|, |hmode|, and |mmode| so that
  3729. \TeX's ``big semantic switch'' can select the appropriate thing to
  3730. do by computing the value |abs(mode)+cur_cmd|, where |mode| is the current
  3731. mode and |cur_cmd| is the current command code.
  3732. @d vmode=1 {vertical mode}
  3733. @d hmode=vmode+max_command+1 {horizontal mode}
  3734. @d mmode=hmode+max_command+1 {math mode}
  3735. @p procedure print_mode(@!m:integer); {prints the mode represented by |m|}
  3736. begin if m>0 then
  3737. case m div (max_command+1) of
  3738. 0:print("vertical");
  3739. 1:print("horizontal");
  3740. 2:print("display math");
  3741. end
  3742. else if m=0 then print("no")
  3743. else case (-m) div (max_command+1) of
  3744. 0:print("internal vertical");
  3745. 1:print("restricted horizontal");
  3746. 2:print("math");
  3747. end;
  3748. print(" mode");
  3749. end;
  3750. @ The state of affairs at any semantic level can be represented by
  3751. five values:
  3752. \yskip\hang|mode| is the number representing the semantic mode, as
  3753. just explained.
  3754. \yskip\hang|head| is a |pointer| to a list head for the list being built;
  3755. |link(head)| therefore points to the first element of the list, or
  3756. to |null| if the list is empty.
  3757. \yskip\hang|tail| is a |pointer| to the final node of the list being
  3758. built; thus, |tail=head| if and only if the list is empty.
  3759. \yskip\hang|prev_graf| is the number of lines of the current paragraph that
  3760. have already been put into the present vertical list.
  3761. \yskip\hang|aux| is an auxiliary |memory_word| that gives further information
  3762. that is needed to characterize the situation.
  3763. \yskip\noindent
  3764. In vertical mode, |aux| is also known as |prev_depth|; it is the scaled
  3765. value representing the depth of the previous box, for use in baseline
  3766. calculations, or it is |<=-1000|pt if the next box on the vertical list is to
  3767. be exempt from baseline calculations. In horizontal mode, |aux| is also
  3768. known as |space_factor| and |clang|; it holds the current space factor used in
  3769. spacing calculations, and the current language used for hyphenation.
  3770. (The value of |clang| is undefined in restricted horizontal mode.)
  3771. In math mode, |aux| is also known as |incompleat_noad|; if
  3772. not |null|, it points to a record that represents the numerator of a
  3773. generalized fraction for which the denominator is currently being formed
  3774. in the current list.
  3775. There is also a sixth quantity, |mode_line|, which correlates
  3776. the semantic nest with the user's input; |mode_line| contains the source
  3777. line number at which the current level of nesting was entered. The negative
  3778. of this line number is the |mode_line| at the level of the
  3779. user's output routine.
  3780. In horizontal mode, the |prev_graf| field is used for initial language data.
  3781. The semantic nest is an array called |nest| that holds the |mode|, |head|,
  3782. |tail|, |prev_graf|, |aux|, and |mode_line| values for all semantic levels
  3783. below the currently active one. Information about the currently active
  3784. level is kept in the global quantities |mode|, |head|, |tail|, |prev_graf|,
  3785. |aux|, and |mode_line|, which live in a \PASCAL\ record that is ready to
  3786. be pushed onto |nest| if necessary.
  3787. @d ignore_depth==-65536000 {|prev_depth| value that is ignored}
  3788. @<Types...@>=
  3789. @!list_state_record=record@!mode_field:-mmode..mmode;@+
  3790. @!head_field,@!tail_field: pointer;
  3791. @!pg_field,@!ml_field: integer;@+
  3792. @!aux_field: memory_word;
  3793. end;
  3794. @ @d mode==cur_list.mode_field {current mode}
  3795. @d head==cur_list.head_field {header node of current list}
  3796. @d tail==cur_list.tail_field {final node on current list}
  3797. @d prev_graf==cur_list.pg_field {number of paragraph lines accumulated}
  3798. @d aux==cur_list.aux_field {auxiliary data about the current list}
  3799. @d prev_depth==aux.sc {the name of |aux| in vertical mode}
  3800. @d space_factor==aux.hh.lh {part of |aux| in horizontal mode}
  3801. @d clang==aux.hh.rh {the other part of |aux| in horizontal mode}
  3802. @d incompleat_noad==aux.int {the name of |aux| in math mode}
  3803. @d mode_line==cur_list.ml_field {source file line number at beginning of list}
  3804. @<Glob...@>=
  3805. @!nest:array[0..nest_size] of list_state_record;
  3806. @!nest_ptr:0..nest_size; {first unused location of |nest|}
  3807. @!max_nest_stack:0..nest_size; {maximum of |nest_ptr| when pushing}
  3808. @!cur_list:list_state_record; {the ``top'' semantic state}
  3809. @!shown_mode:-mmode..mmode; {most recent mode shown by \.{\\tracingcommands}}
  3810. @ Here is a common way to make the current list grow:
  3811. @d tail_append(#)==begin link(tail):=#; tail:=link(tail);
  3812. end
  3813. @ We will see later that the vertical list at the bottom semantic level is split
  3814. into two parts; the ``current page'' runs from |page_head| to |page_tail|,
  3815. and the ``contribution list'' runs from |contrib_head| to |tail| of
  3816. semantic level zero. The idea is that contributions are first formed in
  3817. vertical mode, then ``contributed'' to the current page (during which time
  3818. the page-breaking decisions are made). For now, we don't need to know
  3819. any more details about the page-building process.
  3820. @<Set init...@>=
  3821. nest_ptr:=0; max_nest_stack:=0;
  3822. mode:=vmode; head:=contrib_head; tail:=contrib_head;
  3823. prev_depth:=ignore_depth; mode_line:=0;
  3824. prev_graf:=0; shown_mode:=0;
  3825. @<Start a new current page@>;
  3826. @ When \TeX's work on one level is interrupted, the state is saved by
  3827. calling |push_nest|. This routine changes |head| and |tail| so that
  3828. a new (empty) list is begun; it does not change |mode| or |aux|.
  3829. @p procedure push_nest; {enter a new semantic level, save the old}
  3830. begin if nest_ptr>max_nest_stack then
  3831. begin max_nest_stack:=nest_ptr;
  3832. if nest_ptr=nest_size then overflow("semantic nest size",nest_size);
  3833. @:TeX capacity exceeded semantic nest size}{\quad semantic nest size@>
  3834. end;
  3835. nest[nest_ptr]:=cur_list; {stack the record}
  3836. incr(nest_ptr); head:=get_avail; tail:=head; prev_graf:=0; mode_line:=line;
  3837. end;
  3838. @ Conversely, when \TeX\ is finished on the current level, the former
  3839. state is restored by calling |pop_nest|. This routine will never be
  3840. called at the lowest semantic level, nor will it be called unless |head|
  3841. is a node that should be returned to free memory.
  3842. @p procedure pop_nest; {leave a semantic level, re-enter the old}
  3843. begin free_avail(head); decr(nest_ptr); cur_list:=nest[nest_ptr];
  3844. end;
  3845. @ Here is a procedure that displays what \TeX\ is working on, at all levels.
  3846. @p procedure@?print_totals; forward;@t\2@>
  3847. procedure show_activities;
  3848. var p:0..nest_size; {index into |nest|}
  3849. @!m:-mmode..mmode; {mode}
  3850. @!a:memory_word; {auxiliary}
  3851. @!q,@!r:pointer; {for showing the current page}
  3852. @!t:integer; {ditto}
  3853. begin nest[nest_ptr]:=cur_list; {put the top level into the array}
  3854. print_nl(""); print_ln;
  3855. for p:=nest_ptr downto 0 do
  3856. begin m:=nest[p].mode_field; a:=nest[p].aux_field;
  3857. print_nl("### "); print_mode(m);
  3858. print(" entered at line "); print_int(abs(nest[p].ml_field));
  3859. if m=hmode then if nest[p].pg_field <> @'40600000 then
  3860. begin print(" (language"); print_int(nest[p].pg_field mod @'200000);
  3861. print(":hyphenmin"); print_int(nest[p].pg_field div @'20000000);
  3862. print_char(","); print_int((nest[p].pg_field div @'200000) mod @'100);
  3863. print_char(")");
  3864. end;
  3865. if nest[p].ml_field<0 then print(" (\output routine)");
  3866. if p=0 then
  3867. begin @<Show the status of the current page@>;
  3868. if link(contrib_head)<>null then
  3869. print_nl("### recent contributions:");
  3870. end;
  3871. show_box(link(nest[p].head_field));
  3872. @<Show the auxiliary field, |a|@>;
  3873. end;
  3874. end;
  3875. @ @<Show the auxiliary...@>=
  3876. case abs(m) div (max_command+1) of
  3877. 0: begin print_nl("prevdepth ");
  3878. if a.sc<=ignore_depth then print("ignored")
  3879. else print_scaled(a.sc);
  3880. if nest[p].pg_field<>0 then
  3881. begin print(", prevgraf ");
  3882. print_int(nest[p].pg_field); print(" line");
  3883. if nest[p].pg_field<>1 then print_char("s");
  3884. end;
  3885. end;
  3886. 1: begin print_nl("spacefactor "); print_int(a.hh.lh);
  3887. if m>0 then@+ if a.hh.rh>0 then
  3888. begin print(", current language "); print_int(a.hh.rh);@+
  3889. end;
  3890. end;
  3891. 2: if a.int<>null then
  3892. begin print("this will begin denominator of:"); show_box(a.int);@+
  3893. end;
  3894. end {there are no other cases}
  3895. @* \[17] The table of equivalents.
  3896. Now that we have studied the data structures for \TeX's semantic routines,
  3897. we ought to consider the data structures used by its syntactic routines. In
  3898. other words, our next concern will be
  3899. the tables that \TeX\ looks at when it is scanning
  3900. what the user has written.
  3901. The biggest and most important such table is called |eqtb|. It holds the
  3902. current ``equivalents'' of things; i.e., it explains what things mean
  3903. or what their current values are, for all quantities that are subject to
  3904. the nesting structure provided by \TeX's grouping mechanism. There are six
  3905. parts to |eqtb|:
  3906. \yskip\hangg 1) |eqtb[active_base..(hash_base-1)]| holds the current
  3907. equivalents of single-character control sequences.
  3908. \yskip\hangg 2) |eqtb[hash_base..(glue_base-1)]| holds the current
  3909. equivalents of multiletter control sequences.
  3910. \yskip\hangg 3) |eqtb[glue_base..(local_base-1)]| holds the current
  3911. equivalents of glue parameters like the current baselineskip.
  3912. \yskip\hangg 4) |eqtb[local_base..(int_base-1)]| holds the current
  3913. equivalents of local halfword quantities like the current box registers,
  3914. the current ``catcodes,'' the current font, and a pointer to the current
  3915. paragraph shape.
  3916. \yskip\hangg 5) |eqtb[int_base..(dimen_base-1)]| holds the current
  3917. equivalents of fullword integer parameters like the current hyphenation
  3918. penalty.
  3919. \yskip\hangg 6) |eqtb[dimen_base..eqtb_size]| holds the current equivalents
  3920. of fullword dimension parameters like the current hsize or amount of
  3921. hanging indentation.
  3922. \yskip\noindent Note that, for example, the current amount of
  3923. baselineskip glue is determined by the setting of a particular location
  3924. in region~3 of |eqtb|, while the current meaning of the control sequence
  3925. `\.{\\baselineskip}' (which might have been changed by \.{\\def} or
  3926. \.{\\let}) appears in region~2.
  3927. @ Each entry in |eqtb| is a |memory_word|. Most of these words are of type
  3928. |two_halves|, and subdivided into three fields:
  3929. \yskip\hangg 1) The |eq_level| (a quarterword) is the level of grouping at
  3930. which this equivalent was defined. If the level is |level_zero|, the
  3931. equivalent has never been defined; |level_one| refers to the outer level
  3932. (outside of all groups), and this level is also used for global
  3933. definitions that never go away. Higher levels are for equivalents that
  3934. will disappear at the end of their group. @^global definitions@>
  3935. \yskip\hangg 2) The |eq_type| (another quarterword) specifies what kind of
  3936. entry this is. There are many types, since each \TeX\ primitive like
  3937. \.{\\hbox}, \.{\\def}, etc., has its own special code. The list of
  3938. command codes above includes all possible settings of the |eq_type| field.
  3939. \yskip\hangg 3) The |equiv| (a halfword) is the current equivalent value.
  3940. This may be a font number, a pointer into |mem|, or a variety of other
  3941. things.
  3942. @d eq_level_field(#)==#.hh.b1
  3943. @d eq_type_field(#)==#.hh.b0
  3944. @d equiv_field(#)==#.hh.rh
  3945. @d eq_level(#)==eq_level_field(eqtb[#]) {level of definition}
  3946. @d eq_type(#)==eq_type_field(eqtb[#]) {command code for equivalent}
  3947. @d equiv(#)==equiv_field(eqtb[#]) {equivalent value}
  3948. @d level_zero=min_quarterword {level for undefined quantities}
  3949. @d level_one=level_zero+1 {outermost level for defined quantities}
  3950. @ Many locations in |eqtb| have symbolic names. The purpose of the next
  3951. paragraphs is to define these names, and to set up the initial values of the
  3952. equivalents.
  3953. In the first region we have 256 equivalents for ``active characters'' that
  3954. act as control sequences, followed by 256 equivalents for single-character
  3955. control sequences.
  3956. Then comes region~2, which corresponds to the hash table that we will
  3957. define later. The maximum address in this region is used for a dummy
  3958. control sequence that is perpetually undefined. There also are several
  3959. locations for control sequences that are perpetually defined
  3960. (since they are used in error recovery).
  3961. @d active_base=1 {beginning of region 1, for active character equivalents}
  3962. @d single_base=active_base+256 {equivalents of one-character control sequences}
  3963. @d null_cs=single_base+256 {equivalent of \.{\\csname\\endcsname}}
  3964. @d hash_base=null_cs+1 {beginning of region 2, for the hash table}
  3965. @d frozen_control_sequence=hash_base+hash_size {for error recovery}
  3966. @d frozen_protection=frozen_control_sequence {inaccessible but definable}
  3967. @d frozen_cr=frozen_control_sequence+1 {permanent `\.{\\cr}'}
  3968. @d frozen_end_group=frozen_control_sequence+2 {permanent `\.{\\endgroup}'}
  3969. @d frozen_right=frozen_control_sequence+3 {permanent `\.{\\right}'}
  3970. @d frozen_fi=frozen_control_sequence+4 {permanent `\.{\\fi}'}
  3971. @d frozen_end_template=frozen_control_sequence+5 {permanent `\.{\\endtemplate}'}
  3972. @d frozen_endv=frozen_control_sequence+6 {second permanent `\.{\\endtemplate}'}
  3973. @d frozen_relax=frozen_control_sequence+7 {permanent `\.{\\relax}'}
  3974. @d end_write=frozen_control_sequence+8 {permanent `\.{\\endwrite}'}
  3975. @d frozen_dont_expand=frozen_control_sequence+9
  3976. {permanent `\.{\\notexpanded:}'}
  3977. @d frozen_null_font=frozen_control_sequence+10
  3978. {permanent `\.{\\nullfont}'}
  3979. @d font_id_base=frozen_null_font-font_base
  3980. {begins table of 257 permanent font identifiers}
  3981. @d undefined_control_sequence=frozen_null_font+257 {dummy location}
  3982. @d glue_base=undefined_control_sequence+1 {beginning of region 3}
  3983. @<Initialize table entries...@>=
  3984. eq_type(undefined_control_sequence):=undefined_cs;
  3985. equiv(undefined_control_sequence):=null;
  3986. eq_level(undefined_control_sequence):=level_zero;
  3987. for k:=active_base to undefined_control_sequence-1 do
  3988. eqtb[k]:=eqtb[undefined_control_sequence];
  3989. @ Here is a routine that displays the current meaning of an |eqtb| entry
  3990. in region 1 or~2. (Similar routines for the other regions will appear
  3991. below.)
  3992. @<Show equivalent |n|, in region 1 or 2@>=
  3993. begin sprint_cs(n); print_char("="); print_cmd_chr(eq_type(n),equiv(n));
  3994. if eq_type(n)>=call then
  3995. begin print_char(":"); show_token_list(link(equiv(n)),null,32);
  3996. end;
  3997. end
  3998. @ Region 3 of |eqtb| contains the 256 \.{\\skip} registers, as well as the
  3999. glue parameters defined here. It is important that the ``muskip''
  4000. parameters have larger numbers than the others.
  4001. @d line_skip_code=0 {interline glue if |baseline_skip| is infeasible}
  4002. @d baseline_skip_code=1 {desired glue between baselines}
  4003. @d par_skip_code=2 {extra glue just above a paragraph}
  4004. @d above_display_skip_code=3 {extra glue just above displayed math}
  4005. @d below_display_skip_code=4 {extra glue just below displayed math}
  4006. @d above_display_short_skip_code=5
  4007. {glue above displayed math following short lines}
  4008. @d below_display_short_skip_code=6
  4009. {glue below displayed math following short lines}
  4010. @d left_skip_code=7 {glue at left of justified lines}
  4011. @d right_skip_code=8 {glue at right of justified lines}
  4012. @d top_skip_code=9 {glue at top of main pages}
  4013. @d split_top_skip_code=10 {glue at top of split pages}
  4014. @d tab_skip_code=11 {glue between aligned entries}
  4015. @d space_skip_code=12 {glue between words (if not |zero_glue|)}
  4016. @d xspace_skip_code=13 {glue after sentences (if not |zero_glue|)}
  4017. @d par_fill_skip_code=14 {glue on last line of paragraph}
  4018. @d thin_mu_skip_code=15 {thin space in math formula}
  4019. @d med_mu_skip_code=16 {medium space in math formula}
  4020. @d thick_mu_skip_code=17 {thick space in math formula}
  4021. @d glue_pars=18 {total number of glue parameters}
  4022. @d skip_base=glue_base+glue_pars {table of 256 ``skip'' registers}
  4023. @d mu_skip_base=skip_base+256 {table of 256 ``muskip'' registers}
  4024. @d local_base=mu_skip_base+256 {beginning of region 4}
  4025. @#
  4026. @d skip(#)==equiv(skip_base+#) {|mem| location of glue specification}
  4027. @d mu_skip(#)==equiv(mu_skip_base+#) {|mem| location of math glue spec}
  4028. @d glue_par(#)==equiv(glue_base+#) {|mem| location of glue specification}
  4029. @d line_skip==glue_par(line_skip_code)
  4030. @d baseline_skip==glue_par(baseline_skip_code)
  4031. @d par_skip==glue_par(par_skip_code)
  4032. @d above_display_skip==glue_par(above_display_skip_code)
  4033. @d below_display_skip==glue_par(below_display_skip_code)
  4034. @d above_display_short_skip==glue_par(above_display_short_skip_code)
  4035. @d below_display_short_skip==glue_par(below_display_short_skip_code)
  4036. @d left_skip==glue_par(left_skip_code)
  4037. @d right_skip==glue_par(right_skip_code)
  4038. @d top_skip==glue_par(top_skip_code)
  4039. @d split_top_skip==glue_par(split_top_skip_code)
  4040. @d tab_skip==glue_par(tab_skip_code)
  4041. @d space_skip==glue_par(space_skip_code)
  4042. @d xspace_skip==glue_par(xspace_skip_code)
  4043. @d par_fill_skip==glue_par(par_fill_skip_code)
  4044. @d thin_mu_skip==glue_par(thin_mu_skip_code)
  4045. @d med_mu_skip==glue_par(med_mu_skip_code)
  4046. @d thick_mu_skip==glue_par(thick_mu_skip_code)
  4047. @<Current |mem| equivalent of glue parameter number |n|@>=glue_par(n)
  4048. @ Sometimes we need to convert \TeX's internal code numbers into symbolic
  4049. form. The |print_skip_param| routine gives the symbolic name of a glue
  4050. parameter.
  4051. @<Declare the procedure called |print_skip_param|@>=
  4052. procedure print_skip_param(@!n:integer);
  4053. begin case n of
  4054. line_skip_code: print_esc("lineskip");
  4055. baseline_skip_code: print_esc("baselineskip");
  4056. par_skip_code: print_esc("parskip");
  4057. above_display_skip_code: print_esc("abovedisplayskip");
  4058. below_display_skip_code: print_esc("belowdisplayskip");
  4059. above_display_short_skip_code: print_esc("abovedisplayshortskip");
  4060. below_display_short_skip_code: print_esc("belowdisplayshortskip");
  4061. left_skip_code: print_esc("leftskip");
  4062. right_skip_code: print_esc("rightskip");
  4063. top_skip_code: print_esc("topskip");
  4064. split_top_skip_code: print_esc("splittopskip");
  4065. tab_skip_code: print_esc("tabskip");
  4066. space_skip_code: print_esc("spaceskip");
  4067. xspace_skip_code: print_esc("xspaceskip");
  4068. par_fill_skip_code: print_esc("parfillskip");
  4069. thin_mu_skip_code: print_esc("thinmuskip");
  4070. med_mu_skip_code: print_esc("medmuskip");
  4071. thick_mu_skip_code: print_esc("thickmuskip");
  4072. othercases print("[unknown glue parameter!]")
  4073. endcases;
  4074. end;
  4075. @ The symbolic names for glue parameters are put into \TeX's hash table
  4076. by using the routine called |primitive|, defined below. Let us enter them
  4077. now, so that we don't have to list all those parameter names anywhere else.
  4078. @<Put each of \TeX's primitives into the hash table@>=
  4079. primitive("lineskip",assign_glue,glue_base+line_skip_code);@/
  4080. @!@:line_skip_}{\.{\\lineskip} primitive@>
  4081. primitive("baselineskip",assign_glue,glue_base+baseline_skip_code);@/
  4082. @!@:baseline_skip_}{\.{\\baselineskip} primitive@>
  4083. primitive("parskip",assign_glue,glue_base+par_skip_code);@/
  4084. @!@:par_skip_}{\.{\\parskip} primitive@>
  4085. primitive("abovedisplayskip",assign_glue,glue_base+above_display_skip_code);@/
  4086. @!@:above_display_skip_}{\.{\\abovedisplayskip} primitive@>
  4087. primitive("belowdisplayskip",assign_glue,glue_base+below_display_skip_code);@/
  4088. @!@:below_display_skip_}{\.{\\belowdisplayskip} primitive@>
  4089. primitive("abovedisplayshortskip",
  4090. assign_glue,glue_base+above_display_short_skip_code);@/
  4091. @!@:above_display_short_skip_}{\.{\\abovedisplayshortskip} primitive@>
  4092. primitive("belowdisplayshortskip",
  4093. assign_glue,glue_base+below_display_short_skip_code);@/
  4094. @!@:below_display_short_skip_}{\.{\\belowdisplayshortskip} primitive@>
  4095. primitive("leftskip",assign_glue,glue_base+left_skip_code);@/
  4096. @!@:left_skip_}{\.{\\leftskip} primitive@>
  4097. primitive("rightskip",assign_glue,glue_base+right_skip_code);@/
  4098. @!@:right_skip_}{\.{\\rightskip} primitive@>
  4099. primitive("topskip",assign_glue,glue_base+top_skip_code);@/
  4100. @!@:top_skip_}{\.{\\topskip} primitive@>
  4101. primitive("splittopskip",assign_glue,glue_base+split_top_skip_code);@/
  4102. @!@:split_top_skip_}{\.{\\splittopskip} primitive@>
  4103. primitive("tabskip",assign_glue,glue_base+tab_skip_code);@/
  4104. @!@:tab_skip_}{\.{\\tabskip} primitive@>
  4105. primitive("spaceskip",assign_glue,glue_base+space_skip_code);@/
  4106. @!@:space_skip_}{\.{\\spaceskip} primitive@>
  4107. primitive("xspaceskip",assign_glue,glue_base+xspace_skip_code);@/
  4108. @!@:xspace_skip_}{\.{\\xspaceskip} primitive@>
  4109. primitive("parfillskip",assign_glue,glue_base+par_fill_skip_code);@/
  4110. @!@:par_fill_skip_}{\.{\\parfillskip} primitive@>
  4111. primitive("thinmuskip",assign_mu_glue,glue_base+thin_mu_skip_code);@/
  4112. @!@:thin_mu_skip_}{\.{\\thinmuskip} primitive@>
  4113. primitive("medmuskip",assign_mu_glue,glue_base+med_mu_skip_code);@/
  4114. @!@:med_mu_skip_}{\.{\\medmuskip} primitive@>
  4115. primitive("thickmuskip",assign_mu_glue,glue_base+thick_mu_skip_code);@/
  4116. @!@:thick_mu_skip_}{\.{\\thickmuskip} primitive@>
  4117. @ @<Cases of |print_cmd_chr| for symbolic printing of primitives@>=
  4118. assign_glue,assign_mu_glue: if chr_code<skip_base then
  4119. print_skip_param(chr_code-glue_base)
  4120. else if chr_code<mu_skip_base then
  4121. begin print_esc("skip"); print_int(chr_code-skip_base);
  4122. end
  4123. else begin print_esc("muskip"); print_int(chr_code-mu_skip_base);
  4124. end;
  4125. @ All glue parameters and registers are initially `\.{0pt plus0pt minus0pt}'.
  4126. @<Initialize table entries...@>=
  4127. equiv(glue_base):=zero_glue; eq_level(glue_base):=level_one;
  4128. eq_type(glue_base):=glue_ref;
  4129. for k:=glue_base+1 to local_base-1 do eqtb[k]:=eqtb[glue_base];
  4130. glue_ref_count(zero_glue):=glue_ref_count(zero_glue)+local_base-glue_base;
  4131. @ @<Show equivalent |n|, in region 3@>=
  4132. if n<skip_base then
  4133. begin print_skip_param(n-glue_base); print_char("=");
  4134. if n<glue_base+thin_mu_skip_code then print_spec(equiv(n),"pt")
  4135. else print_spec(equiv(n),"mu");
  4136. end
  4137. else if n<mu_skip_base then
  4138. begin print_esc("skip"); print_int(n-skip_base); print_char("=");
  4139. print_spec(equiv(n),"pt");
  4140. end
  4141. else begin print_esc("muskip"); print_int(n-mu_skip_base); print_char("=");
  4142. print_spec(equiv(n),"mu");
  4143. end
  4144. @ Region 4 of |eqtb| contains the local quantities defined here. The
  4145. bulk of this region is taken up by five tables that are indexed by eight-bit
  4146. characters; these tables are important to both the syntactic and semantic
  4147. portions of \TeX. There are also a bunch of special things like font and
  4148. token parameters, as well as the tables of \.{\\toks} and \.{\\box}
  4149. registers.
  4150. @d par_shape_loc=local_base {specifies paragraph shape}
  4151. @d output_routine_loc=local_base+1 {points to token list for \.{\\output}}
  4152. @d every_par_loc=local_base+2 {points to token list for \.{\\everypar}}
  4153. @d every_math_loc=local_base+3 {points to token list for \.{\\everymath}}
  4154. @d every_display_loc=local_base+4 {points to token list for \.{\\everydisplay}}
  4155. @d every_hbox_loc=local_base+5 {points to token list for \.{\\everyhbox}}
  4156. @d every_vbox_loc=local_base+6 {points to token list for \.{\\everyvbox}}
  4157. @d every_job_loc=local_base+7 {points to token list for \.{\\everyjob}}
  4158. @d every_cr_loc=local_base+8 {points to token list for \.{\\everycr}}
  4159. @d err_help_loc=local_base+9 {points to token list for \.{\\errhelp}}
  4160. @d toks_base=local_base+10 {table of 256 token list registers}
  4161. @d box_base=toks_base+256 {table of 256 box registers}
  4162. @d cur_font_loc=box_base+256 {internal font number outside math mode}
  4163. @d math_font_base=cur_font_loc+1 {table of 48 math font numbers}
  4164. @d cat_code_base=math_font_base+48
  4165. {table of 256 command codes (the ``catcodes'')}
  4166. @d lc_code_base=cat_code_base+256 {table of 256 lowercase mappings}
  4167. @d uc_code_base=lc_code_base+256 {table of 256 uppercase mappings}
  4168. @d sf_code_base=uc_code_base+256 {table of 256 spacefactor mappings}
  4169. @d math_code_base=sf_code_base+256 {table of 256 math mode mappings}
  4170. @d int_base=math_code_base+256 {beginning of region 5}
  4171. @#
  4172. @d par_shape_ptr==equiv(par_shape_loc)
  4173. @d output_routine==equiv(output_routine_loc)
  4174. @d every_par==equiv(every_par_loc)
  4175. @d every_math==equiv(every_math_loc)
  4176. @d every_display==equiv(every_display_loc)
  4177. @d every_hbox==equiv(every_hbox_loc)
  4178. @d every_vbox==equiv(every_vbox_loc)
  4179. @d every_job==equiv(every_job_loc)
  4180. @d every_cr==equiv(every_cr_loc)
  4181. @d err_help==equiv(err_help_loc)
  4182. @d toks(#)==equiv(toks_base+#)
  4183. @d box(#)==equiv(box_base+#)
  4184. @d cur_font==equiv(cur_font_loc)
  4185. @d fam_fnt(#)==equiv(math_font_base+#)
  4186. @d cat_code(#)==equiv(cat_code_base+#)
  4187. @d lc_code(#)==equiv(lc_code_base+#)
  4188. @d uc_code(#)==equiv(uc_code_base+#)
  4189. @d sf_code(#)==equiv(sf_code_base+#)
  4190. @d math_code(#)==equiv(math_code_base+#)
  4191. {Note: |math_code(c)| is the true math code plus |min_halfword|}
  4192. @<Put each...@>=
  4193. primitive("output",assign_toks,output_routine_loc);
  4194. @!@:output_}{\.{\\output} primitive@>
  4195. primitive("everypar",assign_toks,every_par_loc);
  4196. @!@:every_par_}{\.{\\everypar} primitive@>
  4197. primitive("everymath",assign_toks,every_math_loc);
  4198. @!@:every_math_}{\.{\\everymath} primitive@>
  4199. primitive("everydisplay",assign_toks,every_display_loc);
  4200. @!@:every_display_}{\.{\\everydisplay} primitive@>
  4201. primitive("everyhbox",assign_toks,every_hbox_loc);
  4202. @!@:every_hbox_}{\.{\\everyhbox} primitive@>
  4203. primitive("everyvbox",assign_toks,every_vbox_loc);
  4204. @!@:every_vbox_}{\.{\\everyvbox} primitive@>
  4205. primitive("everyjob",assign_toks,every_job_loc);
  4206. @!@:every_job_}{\.{\\everyjob} primitive@>
  4207. primitive("everycr",assign_toks,every_cr_loc);
  4208. @!@:every_cr_}{\.{\\everycr} primitive@>
  4209. primitive("errhelp",assign_toks,err_help_loc);
  4210. @!@:err_help_}{\.{\\errhelp} primitive@>
  4211. @ @<Cases of |print_cmd_chr|...@>=
  4212. assign_toks: if chr_code>=toks_base then
  4213. begin print_esc("toks"); print_int(chr_code-toks_base);
  4214. end
  4215. else case chr_code of
  4216. output_routine_loc: print_esc("output");
  4217. every_par_loc: print_esc("everypar");
  4218. every_math_loc: print_esc("everymath");
  4219. every_display_loc: print_esc("everydisplay");
  4220. every_hbox_loc: print_esc("everyhbox");
  4221. every_vbox_loc: print_esc("everyvbox");
  4222. every_job_loc: print_esc("everyjob");
  4223. every_cr_loc: print_esc("everycr");
  4224. othercases print_esc("errhelp")
  4225. endcases;
  4226. @ We initialize most things to null or undefined values. An undefined font
  4227. is represented by the internal code |font_base|.
  4228. However, the character code tables are given initial values based on the
  4229. conventional interpretation of ASCII code. These initial values should
  4230. not be changed when \TeX\ is adapted for use with non-English languages;
  4231. all changes to the initialization conventions should be made in format
  4232. packages, not in \TeX\ itself, so that global interchange of formats is
  4233. possible.
  4234. @d null_font==font_base
  4235. @d var_code==@'70000 {math code meaning ``use the current family''}
  4236. @<Initialize table entries...@>=
  4237. par_shape_ptr:=null; eq_type(par_shape_loc):=shape_ref;
  4238. eq_level(par_shape_loc):=level_one;@/
  4239. for k:=output_routine_loc to toks_base+255 do
  4240. eqtb[k]:=eqtb[undefined_control_sequence];
  4241. box(0):=null; eq_type(box_base):=box_ref; eq_level(box_base):=level_one;
  4242. for k:=box_base+1 to box_base+255 do eqtb[k]:=eqtb[box_base];
  4243. cur_font:=null_font; eq_type(cur_font_loc):=data;
  4244. eq_level(cur_font_loc):=level_one;@/
  4245. for k:=math_font_base to math_font_base+47 do eqtb[k]:=eqtb[cur_font_loc];
  4246. equiv(cat_code_base):=0; eq_type(cat_code_base):=data;
  4247. eq_level(cat_code_base):=level_one;@/
  4248. for k:=cat_code_base+1 to int_base-1 do eqtb[k]:=eqtb[cat_code_base];
  4249. for k:=0 to 255 do
  4250. begin cat_code(k):=other_char; math_code(k):=hi(k); sf_code(k):=1000;
  4251. end;
  4252. cat_code(carriage_return):=car_ret; cat_code(" "):=spacer;
  4253. cat_code("\"):=escape; cat_code("%"):=comment;
  4254. cat_code(invalid_code):=invalid_char; cat_code(null_code):=ignore;
  4255. for k:="0" to "9" do math_code(k):=hi(k+var_code);
  4256. for k:="A" to "Z" do
  4257. begin cat_code(k):=letter; cat_code(k+"a"-"A"):=letter;@/
  4258. math_code(k):=hi(k+var_code+@"100);
  4259. math_code(k+"a"-"A"):=hi(k+"a"-"A"+var_code+@"100);@/
  4260. lc_code(k):=k+"a"-"A"; lc_code(k+"a"-"A"):=k+"a"-"A";@/
  4261. uc_code(k):=k; uc_code(k+"a"-"A"):=k;@/
  4262. sf_code(k):=999;
  4263. end;
  4264. @ @<Show equivalent |n|, in region 4@>=
  4265. if n=par_shape_loc then
  4266. begin print_esc("parshape"); print_char("=");
  4267. if par_shape_ptr=null then print_char("0")
  4268. else print_int(info(par_shape_ptr));
  4269. end
  4270. else if n<toks_base then
  4271. begin print_cmd_chr(assign_toks,n); print_char("=");
  4272. if equiv(n)<>null then show_token_list(link(equiv(n)),null,32);
  4273. end
  4274. else if n<box_base then
  4275. begin print_esc("toks"); print_int(n-toks_base); print_char("=");
  4276. if equiv(n)<>null then show_token_list(link(equiv(n)),null,32);
  4277. end
  4278. else if n<cur_font_loc then
  4279. begin print_esc("box"); print_int(n-box_base); print_char("=");
  4280. if equiv(n)=null then print("void")
  4281. else begin depth_threshold:=0; breadth_max:=1; show_node_list(equiv(n));
  4282. end;
  4283. end
  4284. else if n<cat_code_base then @<Show the font identifier in |eqtb[n]|@>
  4285. else @<Show the halfword code in |eqtb[n]|@>
  4286. @ @<Show the font identifier in |eqtb[n]|@>=
  4287. begin if n=cur_font_loc then print("current font")
  4288. else if n<math_font_base+16 then
  4289. begin print_esc("textfont"); print_int(n-math_font_base);
  4290. end
  4291. else if n<math_font_base+32 then
  4292. begin print_esc("scriptfont"); print_int(n-math_font_base-16);
  4293. end
  4294. else begin print_esc("scriptscriptfont"); print_int(n-math_font_base-32);
  4295. end;
  4296. print_char("=");@/
  4297. print_esc(hash[font_id_base+equiv(n)].rh);
  4298. {that's |font_id_text(equiv(n))|}
  4299. end
  4300. @ @<Show the halfword code in |eqtb[n]|@>=
  4301. if n<math_code_base then
  4302. begin if n<lc_code_base then
  4303. begin print_esc("catcode"); print_int(n-cat_code_base);
  4304. end
  4305. else if n<uc_code_base then
  4306. begin print_esc("lccode"); print_int(n-lc_code_base);
  4307. end
  4308. else if n<sf_code_base then
  4309. begin print_esc("uccode"); print_int(n-uc_code_base);
  4310. end
  4311. else begin print_esc("sfcode"); print_int(n-sf_code_base);
  4312. end;
  4313. print_char("="); print_int(equiv(n));
  4314. end
  4315. else begin print_esc("mathcode"); print_int(n-math_code_base);
  4316. print_char("="); print_int(ho(equiv(n)));
  4317. end
  4318. @ Region 5 of |eqtb| contains the integer parameters and registers defined
  4319. here, as well as the |del_code| table. The latter table differs from the
  4320. |cat_code..math_code| tables that precede it, since delimiter codes are
  4321. fullword integers while the other kinds of codes occupy at most a
  4322. halfword. This is what makes region~5 different from region~4. We will
  4323. store the |eq_level| information in an auxiliary array of quarterwords
  4324. that will be defined later.
  4325. @d pretolerance_code=0 {badness tolerance before hyphenation}
  4326. @d tolerance_code=1 {badness tolerance after hyphenation}
  4327. @d line_penalty_code=2 {added to the badness of every line}
  4328. @d hyphen_penalty_code=3 {penalty for break after discretionary hyphen}
  4329. @d ex_hyphen_penalty_code=4 {penalty for break after explicit hyphen}
  4330. @d club_penalty_code=5 {penalty for creating a club line}
  4331. @d widow_penalty_code=6 {penalty for creating a widow line}
  4332. @d display_widow_penalty_code=7 {ditto, just before a display}
  4333. @d broken_penalty_code=8 {penalty for breaking a page at a broken line}
  4334. @d bin_op_penalty_code=9 {penalty for breaking after a binary operation}
  4335. @d rel_penalty_code=10 {penalty for breaking after a relation}
  4336. @d pre_display_penalty_code=11
  4337. {penalty for breaking just before a displayed formula}
  4338. @d post_display_penalty_code=12
  4339. {penalty for breaking just after a displayed formula}
  4340. @d inter_line_penalty_code=13 {additional penalty between lines}
  4341. @d double_hyphen_demerits_code=14 {demerits for double hyphen break}
  4342. @d final_hyphen_demerits_code=15 {demerits for final hyphen break}
  4343. @d adj_demerits_code=16 {demerits for adjacent incompatible lines}
  4344. @d mag_code=17 {magnification ratio}
  4345. @d delimiter_factor_code=18 {ratio for variable-size delimiters}
  4346. @d looseness_code=19 {change in number of lines for a paragraph}
  4347. @d time_code=20 {current time of day}
  4348. @d day_code=21 {current day of the month}
  4349. @d month_code=22 {current month of the year}
  4350. @d year_code=23 {current year of our Lord}
  4351. @d show_box_breadth_code=24 {nodes per level in |show_box|}
  4352. @d show_box_depth_code=25 {maximum level in |show_box|}
  4353. @d hbadness_code=26 {hboxes exceeding this badness will be shown by |hpack|}
  4354. @d vbadness_code=27 {vboxes exceeding this badness will be shown by |vpack|}
  4355. @d pausing_code=28 {pause after each line is read from a file}
  4356. @d tracing_online_code=29 {show diagnostic output on terminal}
  4357. @d tracing_macros_code=30 {show macros as they are being expanded}
  4358. @d tracing_stats_code=31 {show memory usage if \TeX\ knows it}
  4359. @d tracing_paragraphs_code=32 {show line-break calculations}
  4360. @d tracing_pages_code=33 {show page-break calculations}
  4361. @d tracing_output_code=34 {show boxes when they are shipped out}
  4362. @d tracing_lost_chars_code=35 {show characters that aren't in the font}
  4363. @d tracing_commands_code=36 {show command codes at |big_switch|}
  4364. @d tracing_restores_code=37 {show equivalents when they are restored}
  4365. @d uc_hyph_code=38 {hyphenate words beginning with a capital letter}
  4366. @d output_penalty_code=39 {penalty found at current page break}
  4367. @d max_dead_cycles_code=40 {bound on consecutive dead cycles of output}
  4368. @d hang_after_code=41 {hanging indentation changes after this many lines}
  4369. @d floating_penalty_code=42 {penalty for insertions held over after a split}
  4370. @d global_defs_code=43 {override \.{\\global} specifications}
  4371. @d cur_fam_code=44 {current family}
  4372. @d escape_char_code=45 {escape character for token output}
  4373. @d default_hyphen_char_code=46 {value of \.{\\hyphenchar} when a font is loaded}
  4374. @d default_skew_char_code=47 {value of \.{\\skewchar} when a font is loaded}
  4375. @d end_line_char_code=48 {character placed at the right end of the buffer}
  4376. @d new_line_char_code=49 {character that prints as |print_ln|}
  4377. @d language_code=50 {current hyphenation table}
  4378. @d left_hyphen_min_code=51 {minimum left hyphenation fragment size}
  4379. @d right_hyphen_min_code=52 {minimum right hyphenation fragment size}
  4380. @d holding_inserts_code=53 {do not remove insertion nodes from \.{\\box255}}
  4381. @d error_context_lines_code=54 {maximum intermediate line pairs shown}
  4382. @d int_pars=55 {total number of integer parameters}
  4383. @d count_base=int_base+int_pars {256 user \.{\\count} registers}
  4384. @d del_code_base=count_base+256 {256 delimiter code mappings}
  4385. @d dimen_base=del_code_base+256 {beginning of region 6}
  4386. @#
  4387. @d del_code(#)==eqtb[del_code_base+#].int
  4388. @d count(#)==eqtb[count_base+#].int
  4389. @d int_par(#)==eqtb[int_base+#].int {an integer parameter}
  4390. @d pretolerance==int_par(pretolerance_code)
  4391. @d tolerance==int_par(tolerance_code)
  4392. @d line_penalty==int_par(line_penalty_code)
  4393. @d hyphen_penalty==int_par(hyphen_penalty_code)
  4394. @d ex_hyphen_penalty==int_par(ex_hyphen_penalty_code)
  4395. @d club_penalty==int_par(club_penalty_code)
  4396. @d widow_penalty==int_par(widow_penalty_code)
  4397. @d display_widow_penalty==int_par(display_widow_penalty_code)
  4398. @d broken_penalty==int_par(broken_penalty_code)
  4399. @d bin_op_penalty==int_par(bin_op_penalty_code)
  4400. @d rel_penalty==int_par(rel_penalty_code)
  4401. @d pre_display_penalty==int_par(pre_display_penalty_code)
  4402. @d post_display_penalty==int_par(post_display_penalty_code)
  4403. @d inter_line_penalty==int_par(inter_line_penalty_code)
  4404. @d double_hyphen_demerits==int_par(double_hyphen_demerits_code)
  4405. @d final_hyphen_demerits==int_par(final_hyphen_demerits_code)
  4406. @d adj_demerits==int_par(adj_demerits_code)
  4407. @d mag==int_par(mag_code)
  4408. @d delimiter_factor==int_par(delimiter_factor_code)
  4409. @d looseness==int_par(looseness_code)
  4410. @d time==int_par(time_code)
  4411. @d day==int_par(day_code)
  4412. @d month==int_par(month_code)
  4413. @d year==int_par(year_code)
  4414. @d show_box_breadth==int_par(show_box_breadth_code)
  4415. @d show_box_depth==int_par(show_box_depth_code)
  4416. @d hbadness==int_par(hbadness_code)
  4417. @d vbadness==int_par(vbadness_code)
  4418. @d pausing==int_par(pausing_code)
  4419. @d tracing_online==int_par(tracing_online_code)
  4420. @d tracing_macros==int_par(tracing_macros_code)
  4421. @d tracing_stats==int_par(tracing_stats_code)
  4422. @d tracing_paragraphs==int_par(tracing_paragraphs_code)
  4423. @d tracing_pages==int_par(tracing_pages_code)
  4424. @d tracing_output==int_par(tracing_output_code)
  4425. @d tracing_lost_chars==int_par(tracing_lost_chars_code)
  4426. @d tracing_commands==int_par(tracing_commands_code)
  4427. @d tracing_restores==int_par(tracing_restores_code)
  4428. @d uc_hyph==int_par(uc_hyph_code)
  4429. @d output_penalty==int_par(output_penalty_code)
  4430. @d max_dead_cycles==int_par(max_dead_cycles_code)
  4431. @d hang_after==int_par(hang_after_code)
  4432. @d floating_penalty==int_par(floating_penalty_code)
  4433. @d global_defs==int_par(global_defs_code)
  4434. @d cur_fam==int_par(cur_fam_code)
  4435. @d escape_char==int_par(escape_char_code)
  4436. @d default_hyphen_char==int_par(default_hyphen_char_code)
  4437. @d default_skew_char==int_par(default_skew_char_code)
  4438. @d end_line_char==int_par(end_line_char_code)
  4439. @d new_line_char==int_par(new_line_char_code)
  4440. @d language==int_par(language_code)
  4441. @d left_hyphen_min==int_par(left_hyphen_min_code)
  4442. @d right_hyphen_min==int_par(right_hyphen_min_code)
  4443. @d holding_inserts==int_par(holding_inserts_code)
  4444. @d error_context_lines==int_par(error_context_lines_code)
  4445. @<Assign the values |depth_threshold:=show_box_depth|...@>=
  4446. depth_threshold:=show_box_depth;
  4447. breadth_max:=show_box_breadth
  4448. @ We can print the symbolic name of an integer parameter as follows.
  4449. @p procedure print_param(@!n:integer);
  4450. begin case n of
  4451. pretolerance_code:print_esc("pretolerance");
  4452. tolerance_code:print_esc("tolerance");
  4453. line_penalty_code:print_esc("linepenalty");
  4454. hyphen_penalty_code:print_esc("hyphenpenalty");
  4455. ex_hyphen_penalty_code:print_esc("exhyphenpenalty");
  4456. club_penalty_code:print_esc("clubpenalty");
  4457. widow_penalty_code:print_esc("widowpenalty");
  4458. display_widow_penalty_code:print_esc("displaywidowpenalty");
  4459. broken_penalty_code:print_esc("brokenpenalty");
  4460. bin_op_penalty_code:print_esc("binoppenalty");
  4461. rel_penalty_code:print_esc("relpenalty");
  4462. pre_display_penalty_code:print_esc("predisplaypenalty");
  4463. post_display_penalty_code:print_esc("postdisplaypenalty");
  4464. inter_line_penalty_code:print_esc("interlinepenalty");
  4465. double_hyphen_demerits_code:print_esc("doublehyphendemerits");
  4466. final_hyphen_demerits_code:print_esc("finalhyphendemerits");
  4467. adj_demerits_code:print_esc("adjdemerits");
  4468. mag_code:print_esc("mag");
  4469. delimiter_factor_code:print_esc("delimiterfactor");
  4470. looseness_code:print_esc("looseness");
  4471. time_code:print_esc("time");
  4472. day_code:print_esc("day");
  4473. month_code:print_esc("month");
  4474. year_code:print_esc("year");
  4475. show_box_breadth_code:print_esc("showboxbreadth");
  4476. show_box_depth_code:print_esc("showboxdepth");
  4477. hbadness_code:print_esc("hbadness");
  4478. vbadness_code:print_esc("vbadness");
  4479. pausing_code:print_esc("pausing");
  4480. tracing_online_code:print_esc("tracingonline");
  4481. tracing_macros_code:print_esc("tracingmacros");
  4482. tracing_stats_code:print_esc("tracingstats");
  4483. tracing_paragraphs_code:print_esc("tracingparagraphs");
  4484. tracing_pages_code:print_esc("tracingpages");
  4485. tracing_output_code:print_esc("tracingoutput");
  4486. tracing_lost_chars_code:print_esc("tracinglostchars");
  4487. tracing_commands_code:print_esc("tracingcommands");
  4488. tracing_restores_code:print_esc("tracingrestores");
  4489. uc_hyph_code:print_esc("uchyph");
  4490. output_penalty_code:print_esc("outputpenalty");
  4491. max_dead_cycles_code:print_esc("maxdeadcycles");
  4492. hang_after_code:print_esc("hangafter");
  4493. floating_penalty_code:print_esc("floatingpenalty");
  4494. global_defs_code:print_esc("globaldefs");
  4495. cur_fam_code:print_esc("fam");
  4496. escape_char_code:print_esc("escapechar");
  4497. default_hyphen_char_code:print_esc("defaulthyphenchar");
  4498. default_skew_char_code:print_esc("defaultskewchar");
  4499. end_line_char_code:print_esc("endlinechar");
  4500. new_line_char_code:print_esc("newlinechar");
  4501. language_code:print_esc("language");
  4502. left_hyphen_min_code:print_esc("lefthyphenmin");
  4503. right_hyphen_min_code:print_esc("righthyphenmin");
  4504. holding_inserts_code:print_esc("holdinginserts");
  4505. error_context_lines_code:print_esc("errorcontextlines");
  4506. othercases print("[unknown integer parameter!]")
  4507. endcases;
  4508. end;
  4509. @ The integer parameter names must be entered into the hash table.
  4510. @<Put each...@>=
  4511. primitive("pretolerance",assign_int,int_base+pretolerance_code);@/
  4512. @!@:pretolerance_}{\.{\\pretolerance} primitive@>
  4513. primitive("tolerance",assign_int,int_base+tolerance_code);@/
  4514. @!@:tolerance_}{\.{\\tolerance} primitive@>
  4515. primitive("linepenalty",assign_int,int_base+line_penalty_code);@/
  4516. @!@:line_penalty_}{\.{\\linepenalty} primitive@>
  4517. primitive("hyphenpenalty",assign_int,int_base+hyphen_penalty_code);@/
  4518. @!@:hyphen_penalty_}{\.{\\hyphenpenalty} primitive@>
  4519. primitive("exhyphenpenalty",assign_int,int_base+ex_hyphen_penalty_code);@/
  4520. @!@:ex_hyphen_penalty_}{\.{\\exhyphenpenalty} primitive@>
  4521. primitive("clubpenalty",assign_int,int_base+club_penalty_code);@/
  4522. @!@:club_penalty_}{\.{\\clubpenalty} primitive@>
  4523. primitive("widowpenalty",assign_int,int_base+widow_penalty_code);@/
  4524. @!@:widow_penalty_}{\.{\\widowpenalty} primitive@>
  4525. primitive("displaywidowpenalty",
  4526. assign_int,int_base+display_widow_penalty_code);@/
  4527. @!@:display_widow_penalty_}{\.{\\displaywidowpenalty} primitive@>
  4528. primitive("brokenpenalty",assign_int,int_base+broken_penalty_code);@/
  4529. @!@:broken_penalty_}{\.{\\brokenpenalty} primitive@>
  4530. primitive("binoppenalty",assign_int,int_base+bin_op_penalty_code);@/
  4531. @!@:bin_op_penalty_}{\.{\\binoppenalty} primitive@>
  4532. primitive("relpenalty",assign_int,int_base+rel_penalty_code);@/
  4533. @!@:rel_penalty_}{\.{\\relpenalty} primitive@>
  4534. primitive("predisplaypenalty",assign_int,int_base+pre_display_penalty_code);@/
  4535. @!@:pre_display_penalty_}{\.{\\predisplaypenalty} primitive@>
  4536. primitive("postdisplaypenalty",assign_int,int_base+post_display_penalty_code);@/
  4537. @!@:post_display_penalty_}{\.{\\postdisplaypenalty} primitive@>
  4538. primitive("interlinepenalty",assign_int,int_base+inter_line_penalty_code);@/
  4539. @!@:inter_line_penalty_}{\.{\\interlinepenalty} primitive@>
  4540. primitive("doublehyphendemerits",
  4541. assign_int,int_base+double_hyphen_demerits_code);@/
  4542. @!@:double_hyphen_demerits_}{\.{\\doublehyphendemerits} primitive@>
  4543. primitive("finalhyphendemerits",
  4544. assign_int,int_base+final_hyphen_demerits_code);@/
  4545. @!@:final_hyphen_demerits_}{\.{\\finalhyphendemerits} primitive@>
  4546. primitive("adjdemerits",assign_int,int_base+adj_demerits_code);@/
  4547. @!@:adj_demerits_}{\.{\\adjdemerits} primitive@>
  4548. primitive("mag",assign_int,int_base+mag_code);@/
  4549. @!@:mag_}{\.{\\mag} primitive@>
  4550. primitive("delimiterfactor",assign_int,int_base+delimiter_factor_code);@/
  4551. @!@:delimiter_factor_}{\.{\\delimiterfactor} primitive@>
  4552. primitive("looseness",assign_int,int_base+looseness_code);@/
  4553. @!@:looseness_}{\.{\\looseness} primitive@>
  4554. primitive("time",assign_int,int_base+time_code);@/
  4555. @!@:time_}{\.{\\time} primitive@>
  4556. primitive("day",assign_int,int_base+day_code);@/
  4557. @!@:day_}{\.{\\day} primitive@>
  4558. primitive("month",assign_int,int_base+month_code);@/
  4559. @!@:month_}{\.{\\month} primitive@>
  4560. primitive("year",assign_int,int_base+year_code);@/
  4561. @!@:year_}{\.{\\year} primitive@>
  4562. primitive("showboxbreadth",assign_int,int_base+show_box_breadth_code);@/
  4563. @!@:show_box_breadth_}{\.{\\showboxbreadth} primitive@>
  4564. primitive("showboxdepth",assign_int,int_base+show_box_depth_code);@/
  4565. @!@:show_box_depth_}{\.{\\showboxdepth} primitive@>
  4566. primitive("hbadness",assign_int,int_base+hbadness_code);@/
  4567. @!@:hbadness_}{\.{\\hbadness} primitive@>
  4568. primitive("vbadness",assign_int,int_base+vbadness_code);@/
  4569. @!@:vbadness_}{\.{\\vbadness} primitive@>
  4570. primitive("pausing",assign_int,int_base+pausing_code);@/
  4571. @!@:pausing_}{\.{\\pausing} primitive@>
  4572. primitive("tracingonline",assign_int,int_base+tracing_online_code);@/
  4573. @!@:tracing_online_}{\.{\\tracingonline} primitive@>
  4574. primitive("tracingmacros",assign_int,int_base+tracing_macros_code);@/
  4575. @!@:tracing_macros_}{\.{\\tracingmacros} primitive@>
  4576. primitive("tracingstats",assign_int,int_base+tracing_stats_code);@/
  4577. @!@:tracing_stats_}{\.{\\tracingstats} primitive@>
  4578. primitive("tracingparagraphs",assign_int,int_base+tracing_paragraphs_code);@/
  4579. @!@:tracing_paragraphs_}{\.{\\tracingparagraphs} primitive@>
  4580. primitive("tracingpages",assign_int,int_base+tracing_pages_code);@/
  4581. @!@:tracing_pages_}{\.{\\tracingpages} primitive@>
  4582. primitive("tracingoutput",assign_int,int_base+tracing_output_code);@/
  4583. @!@:tracing_output_}{\.{\\tracingoutput} primitive@>
  4584. primitive("tracinglostchars",assign_int,int_base+tracing_lost_chars_code);@/
  4585. @!@:tracing_lost_chars_}{\.{\\tracinglostchars} primitive@>
  4586. primitive("tracingcommands",assign_int,int_base+tracing_commands_code);@/
  4587. @!@:tracing_commands_}{\.{\\tracingcommands} primitive@>
  4588. primitive("tracingrestores",assign_int,int_base+tracing_restores_code);@/
  4589. @!@:tracing_restores_}{\.{\\tracingrestores} primitive@>
  4590. primitive("uchyph",assign_int,int_base+uc_hyph_code);@/
  4591. @!@:uc_hyph_}{\.{\\uchyph} primitive@>
  4592. primitive("outputpenalty",assign_int,int_base+output_penalty_code);@/
  4593. @!@:output_penalty_}{\.{\\outputpenalty} primitive@>
  4594. primitive("maxdeadcycles",assign_int,int_base+max_dead_cycles_code);@/
  4595. @!@:max_dead_cycles_}{\.{\\maxdeadcycles} primitive@>
  4596. primitive("hangafter",assign_int,int_base+hang_after_code);@/
  4597. @!@:hang_after_}{\.{\\hangafter} primitive@>
  4598. primitive("floatingpenalty",assign_int,int_base+floating_penalty_code);@/
  4599. @!@:floating_penalty_}{\.{\\floatingpenalty} primitive@>
  4600. primitive("globaldefs",assign_int,int_base+global_defs_code);@/
  4601. @!@:global_defs_}{\.{\\globaldefs} primitive@>
  4602. primitive("fam",assign_int,int_base+cur_fam_code);@/
  4603. @!@:fam_}{\.{\\fam} primitive@>
  4604. primitive("escapechar",assign_int,int_base+escape_char_code);@/
  4605. @!@:escape_char_}{\.{\\escapechar} primitive@>
  4606. primitive("defaulthyphenchar",assign_int,int_base+default_hyphen_char_code);@/
  4607. @!@:default_hyphen_char_}{\.{\\defaulthyphenchar} primitive@>
  4608. primitive("defaultskewchar",assign_int,int_base+default_skew_char_code);@/
  4609. @!@:default_skew_char_}{\.{\\defaultskewchar} primitive@>
  4610. primitive("endlinechar",assign_int,int_base+end_line_char_code);@/
  4611. @!@:end_line_char_}{\.{\\endlinechar} primitive@>
  4612. primitive("newlinechar",assign_int,int_base+new_line_char_code);@/
  4613. @!@:new_line_char_}{\.{\\newlinechar} primitive@>
  4614. primitive("language",assign_int,int_base+language_code);@/
  4615. @!@:language_}{\.{\\language} primitive@>
  4616. primitive("lefthyphenmin",assign_int,int_base+left_hyphen_min_code);@/
  4617. @!@:left_hyphen_min_}{\.{\\lefthyphenmin} primitive@>
  4618. primitive("righthyphenmin",assign_int,int_base+right_hyphen_min_code);@/
  4619. @!@:right_hyphen_min_}{\.{\\righthyphenmin} primitive@>
  4620. primitive("holdinginserts",assign_int,int_base+holding_inserts_code);@/
  4621. @!@:holding_inserts_}{\.{\\holdinginserts} primitive@>
  4622. primitive("errorcontextlines",assign_int,int_base+error_context_lines_code);@/
  4623. @!@:error_context_lines_}{\.{\\errorcontextlines} primitive@>
  4624. @ @<Cases of |print_cmd_chr|...@>=
  4625. assign_int: if chr_code<count_base then print_param(chr_code-int_base)
  4626. else begin print_esc("count"); print_int(chr_code-count_base);
  4627. end;
  4628. @ The integer parameters should really be initialized by a macro package;
  4629. the following initialization does the minimum to keep \TeX\ from
  4630. complete failure.
  4631. @^null delimiter@>
  4632. @<Initialize table entries...@>=
  4633. for k:=int_base to del_code_base-1 do eqtb[k].int:=0;
  4634. mag:=1000; tolerance:=10000; hang_after:=1; max_dead_cycles:=25;
  4635. escape_char:="\"; end_line_char:=carriage_return;
  4636. for k:=0 to 255 do del_code(k):=-1;
  4637. del_code("."):=0; {this null delimiter is used in error recovery}
  4638. @ The following procedure, which is called just before \TeX\ initializes its
  4639. input and output, establishes the initial values of the date and time.
  4640. @^system dependencies@>
  4641. Since standard \PASCAL\ cannot provide such information, something special
  4642. is needed. The program here simply assumes that suitable values appear in
  4643. the global variables \\{sys\_time}, \\{sys\_day}, \\{sys\_month}, and
  4644. \\{sys\_year} (which are initialized to noon on 4 July 1776,
  4645. in case the implementor is careless).
  4646. @p procedure fix_date_and_time;
  4647. begin sys_time:=12*60;
  4648. sys_day:=4; sys_month:=7; sys_year:=1776; {self-evident truths}
  4649. time:=sys_time; {minutes since midnight}
  4650. day:=sys_day; {day of the month}
  4651. month:=sys_month; {month of the year}
  4652. year:=sys_year; {Anno Domini}
  4653. end;
  4654. @ @<Show equivalent |n|, in region 5@>=
  4655. begin if n<count_base then print_param(n-int_base)
  4656. else if n<del_code_base then
  4657. begin print_esc("count"); print_int(n-count_base);
  4658. end
  4659. else begin print_esc("delcode"); print_int(n-del_code_base);
  4660. end;
  4661. print_char("="); print_int(eqtb[n].int);
  4662. end
  4663. @ @<Set variable |c| to the current escape character@>=c:=escape_char
  4664. @ @<Character |s| is the current new-line character@>=s=new_line_char
  4665. @ \TeX\ is occasionally supposed to print diagnostic information that
  4666. goes only into the transcript file, unless |tracing_online| is positive.
  4667. Here are two routines that adjust the destination of print commands:
  4668. @p procedure begin_diagnostic; {prepare to do some tracing}
  4669. begin old_setting:=selector;
  4670. if (tracing_online<=0)and(selector=term_and_log) then
  4671. begin decr(selector);
  4672. if history=spotless then history:=warning_issued;
  4673. end;
  4674. end;
  4675. @#
  4676. procedure end_diagnostic(@!blank_line:boolean);
  4677. {restore proper conditions after tracing}
  4678. begin print_nl("");
  4679. if blank_line then print_ln;
  4680. selector:=old_setting;
  4681. end;
  4682. @ Of course we had better declare a few more global variables, if the previous
  4683. routines are going to work.
  4684. @<Glob...@>=
  4685. @!old_setting:0..max_selector;
  4686. @!sys_time,@!sys_day,@!sys_month,@!sys_year:integer;
  4687. {date and time supplied by external system}
  4688. @ The final region of |eqtb| contains the dimension parameters defined
  4689. here, and the 256 \.{\\dimen} registers.
  4690. @d par_indent_code=0 {indentation of paragraphs}
  4691. @d math_surround_code=1 {space around math in text}
  4692. @d line_skip_limit_code=2 {threshold for |line_skip| instead of |baseline_skip|}
  4693. @d hsize_code=3 {line width in horizontal mode}
  4694. @d vsize_code=4 {page height in vertical mode}
  4695. @d max_depth_code=5 {maximum depth of boxes on main pages}
  4696. @d split_max_depth_code=6 {maximum depth of boxes on split pages}
  4697. @d box_max_depth_code=7 {maximum depth of explicit vboxes}
  4698. @d hfuzz_code=8 {tolerance for overfull hbox messages}
  4699. @d vfuzz_code=9 {tolerance for overfull vbox messages}
  4700. @d delimiter_shortfall_code=10 {maximum amount uncovered by variable delimiters}
  4701. @d null_delimiter_space_code=11 {blank space in null delimiters}
  4702. @d script_space_code=12 {extra space after subscript or superscript}
  4703. @d pre_display_size_code=13 {length of text preceding a display}
  4704. @d display_width_code=14 {length of line for displayed equation}
  4705. @d display_indent_code=15 {indentation of line for displayed equation}
  4706. @d overfull_rule_code=16 {width of rule that identifies overfull hboxes}
  4707. @d hang_indent_code=17 {amount of hanging indentation}
  4708. @d h_offset_code=18 {amount of horizontal offset when shipping pages out}
  4709. @d v_offset_code=19 {amount of vertical offset when shipping pages out}
  4710. @d emergency_stretch_code=20 {reduces badnesses on final pass of line-breaking}
  4711. @d dimen_pars=21 {total number of dimension parameters}
  4712. @d scaled_base=dimen_base+dimen_pars
  4713. {table of 256 user-defined \.{\\dimen} registers}
  4714. @d eqtb_size=scaled_base+255 {largest subscript of |eqtb|}
  4715. @#
  4716. @d dimen(#)==eqtb[scaled_base+#].sc
  4717. @d dimen_par(#)==eqtb[dimen_base+#].sc {a scaled quantity}
  4718. @d par_indent==dimen_par(par_indent_code)
  4719. @d math_surround==dimen_par(math_surround_code)
  4720. @d line_skip_limit==dimen_par(line_skip_limit_code)
  4721. @d hsize==dimen_par(hsize_code)
  4722. @d vsize==dimen_par(vsize_code)
  4723. @d max_depth==dimen_par(max_depth_code)
  4724. @d split_max_depth==dimen_par(split_max_depth_code)
  4725. @d box_max_depth==dimen_par(box_max_depth_code)
  4726. @d hfuzz==dimen_par(hfuzz_code)
  4727. @d vfuzz==dimen_par(vfuzz_code)
  4728. @d delimiter_shortfall==dimen_par(delimiter_shortfall_code)
  4729. @d null_delimiter_space==dimen_par(null_delimiter_space_code)
  4730. @d script_space==dimen_par(script_space_code)
  4731. @d pre_display_size==dimen_par(pre_display_size_code)
  4732. @d display_width==dimen_par(display_width_code)
  4733. @d display_indent==dimen_par(display_indent_code)
  4734. @d overfull_rule==dimen_par(overfull_rule_code)
  4735. @d hang_indent==dimen_par(hang_indent_code)
  4736. @d h_offset==dimen_par(h_offset_code)
  4737. @d v_offset==dimen_par(v_offset_code)
  4738. @d emergency_stretch==dimen_par(emergency_stretch_code)
  4739. @p procedure print_length_param(@!n:integer);
  4740. begin case n of
  4741. par_indent_code:print_esc("parindent");
  4742. math_surround_code:print_esc("mathsurround");
  4743. line_skip_limit_code:print_esc("lineskiplimit");
  4744. hsize_code:print_esc("hsize");
  4745. vsize_code:print_esc("vsize");
  4746. max_depth_code:print_esc("maxdepth");
  4747. split_max_depth_code:print_esc("splitmaxdepth");
  4748. box_max_depth_code:print_esc("boxmaxdepth");
  4749. hfuzz_code:print_esc("hfuzz");
  4750. vfuzz_code:print_esc("vfuzz");
  4751. delimiter_shortfall_code:print_esc("delimitershortfall");
  4752. null_delimiter_space_code:print_esc("nulldelimiterspace");
  4753. script_space_code:print_esc("scriptspace");
  4754. pre_display_size_code:print_esc("predisplaysize");
  4755. display_width_code:print_esc("displaywidth");
  4756. display_indent_code:print_esc("displayindent");
  4757. overfull_rule_code:print_esc("overfullrule");
  4758. hang_indent_code:print_esc("hangindent");
  4759. h_offset_code:print_esc("hoffset");
  4760. v_offset_code:print_esc("voffset");
  4761. emergency_stretch_code:print_esc("emergencystretch");
  4762. othercases print("[unknown dimen parameter!]")
  4763. endcases;
  4764. end;
  4765. @ @<Put each...@>=
  4766. primitive("parindent",assign_dimen,dimen_base+par_indent_code);@/
  4767. @!@:par_indent_}{\.{\\parindent} primitive@>
  4768. primitive("mathsurround",assign_dimen,dimen_base+math_surround_code);@/
  4769. @!@:math_surround_}{\.{\\mathsurround} primitive@>
  4770. primitive("lineskiplimit",assign_dimen,dimen_base+line_skip_limit_code);@/
  4771. @!@:line_skip_limit_}{\.{\\lineskiplimit} primitive@>
  4772. primitive("hsize",assign_dimen,dimen_base+hsize_code);@/
  4773. @!@:hsize_}{\.{\\hsize} primitive@>
  4774. primitive("vsize",assign_dimen,dimen_base+vsize_code);@/
  4775. @!@:vsize_}{\.{\\vsize} primitive@>
  4776. primitive("maxdepth",assign_dimen,dimen_base+max_depth_code);@/
  4777. @!@:max_depth_}{\.{\\maxdepth} primitive@>
  4778. primitive("splitmaxdepth",assign_dimen,dimen_base+split_max_depth_code);@/
  4779. @!@:split_max_depth_}{\.{\\splitmaxdepth} primitive@>
  4780. primitive("boxmaxdepth",assign_dimen,dimen_base+box_max_depth_code);@/
  4781. @!@:box_max_depth_}{\.{\\boxmaxdepth} primitive@>
  4782. primitive("hfuzz",assign_dimen,dimen_base+hfuzz_code);@/
  4783. @!@:hfuzz_}{\.{\\hfuzz} primitive@>
  4784. primitive("vfuzz",assign_dimen,dimen_base+vfuzz_code);@/
  4785. @!@:vfuzz_}{\.{\\vfuzz} primitive@>
  4786. primitive("delimitershortfall",
  4787. assign_dimen,dimen_base+delimiter_shortfall_code);@/
  4788. @!@:delimiter_shortfall_}{\.{\\delimitershortfall} primitive@>
  4789. primitive("nulldelimiterspace",
  4790. assign_dimen,dimen_base+null_delimiter_space_code);@/
  4791. @!@:null_delimiter_space_}{\.{\\nulldelimiterspace} primitive@>
  4792. primitive("scriptspace",assign_dimen,dimen_base+script_space_code);@/
  4793. @!@:script_space_}{\.{\\scriptspace} primitive@>
  4794. primitive("predisplaysize",assign_dimen,dimen_base+pre_display_size_code);@/
  4795. @!@:pre_display_size_}{\.{\\predisplaysize} primitive@>
  4796. primitive("displaywidth",assign_dimen,dimen_base+display_width_code);@/
  4797. @!@:display_width_}{\.{\\displaywidth} primitive@>
  4798. primitive("displayindent",assign_dimen,dimen_base+display_indent_code);@/
  4799. @!@:display_indent_}{\.{\\displayindent} primitive@>
  4800. primitive("overfullrule",assign_dimen,dimen_base+overfull_rule_code);@/
  4801. @!@:overfull_rule_}{\.{\\overfullrule} primitive@>
  4802. primitive("hangindent",assign_dimen,dimen_base+hang_indent_code);@/
  4803. @!@:hang_indent_}{\.{\\hangindent} primitive@>
  4804. primitive("hoffset",assign_dimen,dimen_base+h_offset_code);@/
  4805. @!@:h_offset_}{\.{\\hoffset} primitive@>
  4806. primitive("voffset",assign_dimen,dimen_base+v_offset_code);@/
  4807. @!@:v_offset_}{\.{\\voffset} primitive@>
  4808. primitive("emergencystretch",assign_dimen,dimen_base+emergency_stretch_code);@/
  4809. @!@:emergency_stretch_}{\.{\\emergencystretch} primitive@>
  4810. @ @<Cases of |print_cmd_chr|...@>=
  4811. assign_dimen: if chr_code<scaled_base then
  4812. print_length_param(chr_code-dimen_base)
  4813. else begin print_esc("dimen"); print_int(chr_code-scaled_base);
  4814. end;
  4815. @ @<Initialize table entries...@>=
  4816. for k:=dimen_base to eqtb_size do eqtb[k].sc:=0;
  4817. @ @<Show equivalent |n|, in region 6@>=
  4818. begin if n<scaled_base then print_length_param(n-dimen_base)
  4819. else begin print_esc("dimen"); print_int(n-scaled_base);
  4820. end;
  4821. print_char("="); print_scaled(eqtb[n].sc); print("pt");
  4822. end
  4823. @ Here is a procedure that displays the contents of |eqtb[n]|
  4824. symbolically.
  4825. @p@t\4@>@<Declare the procedure called |print_cmd_chr|@>@;@/
  4826. @!stat procedure show_eqtb(@!n:pointer);
  4827. begin if n<active_base then print_char("?") {this can't happen}
  4828. else if n<glue_base then @<Show equivalent |n|, in region 1 or 2@>
  4829. else if n<local_base then @<Show equivalent |n|, in region 3@>
  4830. else if n<int_base then @<Show equivalent |n|, in region 4@>
  4831. else if n<dimen_base then @<Show equivalent |n|, in region 5@>
  4832. else if n<=eqtb_size then @<Show equivalent |n|, in region 6@>
  4833. else print_char("?"); {this can't happen either}
  4834. end;
  4835. tats
  4836. @ The last two regions of |eqtb| have fullword values instead of the
  4837. three fields |eq_level|, |eq_type|, and |equiv|. An |eq_type| is unnecessary,
  4838. but \TeX\ needs to store the |eq_level| information in another array
  4839. called |xeq_level|.
  4840. @<Glob...@>=
  4841. @!eqtb:array[active_base..eqtb_size] of memory_word;
  4842. @!xeq_level:array[int_base..eqtb_size] of quarterword;
  4843. @ @<Set init...@>=
  4844. for k:=int_base to eqtb_size do xeq_level[k]:=level_one;
  4845. @ When the debugging routine |search_mem| is looking for pointers having a
  4846. given value, it is interested only in regions 1 to~3 of~|eqtb|, and in the
  4847. first part of region~4.
  4848. @<Search |eqtb| for equivalents equal to |p|@>=
  4849. for q:=active_base to box_base+255 do
  4850. begin if equiv(q)=p then
  4851. begin print_nl("EQUIV("); print_int(q); print_char(")");
  4852. end;
  4853. end
  4854. @* \[18] The hash table.
  4855. Control sequences are stored and retrieved by means of a fairly standard hash
  4856. table algorithm called the method of ``coalescing lists'' (cf.\ Algorithm 6.4C
  4857. in {\sl The Art of Computer Programming\/}). Once a control sequence enters the
  4858. table, it is never removed, because there are complicated situations
  4859. involving \.{\\gdef} where the removal of a control sequence at the end of
  4860. a group would be a mistake preventable only by the introduction of a
  4861. complicated reference-count mechanism.
  4862. The actual sequence of letters forming a control sequence identifier is
  4863. stored in the |str_pool| array together with all the other strings. An
  4864. auxiliary array |hash| consists of items with two halfword fields per
  4865. word. The first of these, called |next(p)|, points to the next identifier
  4866. belonging to the same coalesced list as the identifier corresponding to~|p|;
  4867. and the other, called |text(p)|, points to the |str_start| entry for
  4868. |p|'s identifier. If position~|p| of the hash table is empty, we have
  4869. |text(p)=0|; if position |p| is either empty or the end of a coalesced
  4870. hash list, we have |next(p)=0|. An auxiliary pointer variable called
  4871. |hash_used| is maintained in such a way that all locations |p>=hash_used|
  4872. are nonempty. The global variable |cs_count| tells how many multiletter
  4873. control sequences have been defined, if statistics are being kept.
  4874. A global boolean variable called |no_new_control_sequence| is set to
  4875. |true| during the time that new hash table entries are forbidden.
  4876. @d next(#) == hash[#].lh {link for coalesced lists}
  4877. @d text(#) == hash[#].rh {string number for control sequence name}
  4878. @d hash_is_full == (hash_used=hash_base) {test if all positions are occupied}
  4879. @d font_id_text(#) == text(font_id_base+#) {a frozen font identifier's name}
  4880. @<Glob...@>=
  4881. @!hash: array[hash_base..undefined_control_sequence-1] of two_halves;
  4882. {the hash table}
  4883. @!hash_used:pointer; {allocation pointer for |hash|}
  4884. @!no_new_control_sequence:boolean; {are new identifiers legal?}
  4885. @!cs_count:integer; {total number of known identifiers}
  4886. @ @<Set init...@>=
  4887. no_new_control_sequence:=true; {new identifiers are usually forbidden}
  4888. next(hash_base):=0; text(hash_base):=0;
  4889. for k:=hash_base+1 to undefined_control_sequence-1 do hash[k]:=hash[hash_base];
  4890. @ @<Initialize table entries...@>=
  4891. hash_used:=frozen_control_sequence; {nothing is used}
  4892. cs_count:=0;
  4893. eq_type(frozen_dont_expand):=dont_expand;
  4894. text(frozen_dont_expand):="notexpanded:";
  4895. @.notexpanded:@>
  4896. @ Here is the subroutine that searches the hash table for an identifier
  4897. that matches a given string of length |l>1| appearing in |buffer[j..
  4898. (j+l-1)]|. If the identifier is found, the corresponding hash table address
  4899. is returned. Otherwise, if the global variable |no_new_control_sequence|
  4900. is |true|, the dummy address |undefined_control_sequence| is returned.
  4901. Otherwise the identifier is inserted into the hash table and its location
  4902. is returned.
  4903. @p function id_lookup(@!j,@!l:integer):pointer; {search the hash table}
  4904. label found; {go here if you found it}
  4905. var h:integer; {hash code}
  4906. @!d:integer; {number of characters in incomplete current string}
  4907. @!p:pointer; {index in |hash| array}
  4908. @!k:pointer; {index in |buffer| array}
  4909. begin @<Compute the hash code |h|@>;
  4910. p:=h+hash_base; {we start searching here; note that |0<=h<hash_prime|}
  4911. loop@+begin if text(p)>0 then if length(text(p))=l then
  4912. if str_eq_buf(text(p),j) then goto found;
  4913. if next(p)=0 then
  4914. begin if no_new_control_sequence then
  4915. p:=undefined_control_sequence
  4916. else @<Insert a new control sequence after |p|, then make
  4917. |p| point to it@>;
  4918. goto found;
  4919. end;
  4920. p:=next(p);
  4921. end;
  4922. found: id_lookup:=p;
  4923. end;
  4924. @ @<Insert a new control...@>=
  4925. begin if text(p)>0 then
  4926. begin repeat if hash_is_full then overflow("hash size",hash_size);
  4927. @:TeX capacity exceeded hash size}{\quad hash size@>
  4928. decr(hash_used);
  4929. until text(hash_used)=0; {search for an empty location in |hash|}
  4930. next(p):=hash_used; p:=hash_used;
  4931. end;
  4932. str_room(l); d:=cur_length;
  4933. while pool_ptr>str_start[str_ptr] do
  4934. begin decr(pool_ptr); str_pool[pool_ptr+l]:=str_pool[pool_ptr];
  4935. end; {move current string up to make room for another}
  4936. for k:=j to j+l-1 do append_char(buffer[k]);
  4937. text(p):=make_string; pool_ptr:=pool_ptr+d;
  4938. @!stat incr(cs_count);@+tats@;@/
  4939. end
  4940. @ The value of |hash_prime| should be roughly 85\pct! of |hash_size|, and it
  4941. should be a prime number. The theory of hashing tells us to expect fewer
  4942. than two table probes, on the average, when the search is successful.
  4943. [See J.~S. Vitter, {\sl Journal of the ACM\/ \bf30} (1983), 231--258.]
  4944. @^Vitter, Jeffrey Scott@>
  4945. @<Compute the hash code |h|@>=
  4946. h:=buffer[j];
  4947. for k:=j+1 to j+l-1 do
  4948. begin h:=h+h+buffer[k];
  4949. while h>=hash_prime do h:=h-hash_prime;
  4950. end
  4951. @ Single-character control sequences do not need to be looked up in a hash
  4952. table, since we can use the character code itself as a direct address.
  4953. The procedure |print_cs| prints the name of a control sequence, given
  4954. a pointer to its address in |eqtb|. A space is printed after the name
  4955. unless it is a single nonletter or an active character. This procedure
  4956. might be invoked with invalid data, so it is ``extra robust.'' The
  4957. individual characters must be printed one at a time using |print|, since
  4958. they may be unprintable.
  4959. @<Basic printing...@>=
  4960. procedure print_cs(@!p:integer); {prints a purported control sequence}
  4961. begin if p<hash_base then {single character}
  4962. if p>=single_base then
  4963. if p=null_cs then
  4964. begin print_esc("csname"); print_esc("endcsname"); print_char(" ");
  4965. end
  4966. else begin print_esc(p-single_base);
  4967. if cat_code(p-single_base)=letter then print_char(" ");
  4968. end
  4969. else if p<active_base then print_esc("IMPOSSIBLE.")
  4970. @.IMPOSSIBLE@>
  4971. else print(p-active_base)
  4972. else if p>=undefined_control_sequence then print_esc("IMPOSSIBLE.")
  4973. else if (text(p)<0)or(text(p)>=str_ptr) then print_esc("NONEXISTENT.")
  4974. @.NONEXISTENT@>
  4975. else begin print_esc(text(p));
  4976. print_char(" ");
  4977. end;
  4978. end;
  4979. @ Here is a similar procedure; it avoids the error checks, and it never
  4980. prints a space after the control sequence.
  4981. @<Basic printing procedures@>=
  4982. procedure sprint_cs(@!p:pointer); {prints a control sequence}
  4983. begin if p<hash_base then
  4984. if p<single_base then print(p-active_base)
  4985. else if p<null_cs then print_esc(p-single_base)
  4986. else begin print_esc("csname"); print_esc("endcsname");
  4987. end
  4988. else print_esc(text(p));
  4989. end;
  4990. @ We need to put \TeX's ``primitive'' control sequences into the hash
  4991. table, together with their command code (which will be the |eq_type|)
  4992. and an operand (which will be the |equiv|). The |primitive| procedure
  4993. does this, in a way that no \TeX\ user can. The global value |cur_val|
  4994. contains the new |eqtb| pointer after |primitive| has acted.
  4995. @p @!init procedure primitive(@!s:str_number;@!c:quarterword;@!o:halfword);
  4996. var k:pool_pointer; {index into |str_pool|}
  4997. @!j:small_number; {index into |buffer|}
  4998. @!l:small_number; {length of the string}
  4999. begin if s<256 then cur_val:=s+single_base
  5000. else begin k:=str_start[s]; l:=str_start[s+1]-k;
  5001. {we will move |s| into the (empty) |buffer|}
  5002. for j:=0 to l-1 do buffer[j]:=so(str_pool[k+j]);
  5003. cur_val:=id_lookup(0,l); {|no_new_control_sequence| is |false|}
  5004. flush_string; text(cur_val):=s; {we don't want to have the string twice}
  5005. end;
  5006. eq_level(cur_val):=level_one; eq_type(cur_val):=c; equiv(cur_val):=o;
  5007. end;
  5008. tini
  5009. @ Many of \TeX's primitives need no |equiv|, since they are identifiable
  5010. by their |eq_type| alone. These primitives are loaded into the hash table
  5011. as follows:
  5012. @<Put each of \TeX's primitives into the hash table@>=
  5013. primitive(" ",ex_space,0);@/
  5014. @!@:Single-character primitives /}{\quad\.{\\\ }@>
  5015. primitive("/",ital_corr,0);@/
  5016. @!@:Single-character primitives /}{\quad\.{\\/}@>
  5017. primitive("accent",accent,0);@/
  5018. @!@:accent_}{\.{\\accent} primitive@>
  5019. primitive("advance",advance,0);@/
  5020. @!@:advance_}{\.{\\advance} primitive@>
  5021. primitive("afterassignment",after_assignment,0);@/
  5022. @!@:after_assignment_}{\.{\\afterassignment} primitive@>
  5023. primitive("aftergroup",after_group,0);@/
  5024. @!@:after_group_}{\.{\\aftergroup} primitive@>
  5025. primitive("begingroup",begin_group,0);@/
  5026. @!@:begin_group_}{\.{\\begingroup} primitive@>
  5027. primitive("char",char_num,0);@/
  5028. @!@:char_}{\.{\\char} primitive@>
  5029. primitive("csname",cs_name,0);@/
  5030. @!@:cs_name_}{\.{\\csname} primitive@>
  5031. primitive("delimiter",delim_num,0);@/
  5032. @!@:delimiter_}{\.{\\delimiter} primitive@>
  5033. primitive("divide",divide,0);@/
  5034. @!@:divide_}{\.{\\divide} primitive@>
  5035. primitive("endcsname",end_cs_name,0);@/
  5036. @!@:end_cs_name_}{\.{\\endcsname} primitive@>
  5037. primitive("endgroup",end_group,0);
  5038. @!@:end_group_}{\.{\\endgroup} primitive@>
  5039. text(frozen_end_group):="endgroup"; eqtb[frozen_end_group]:=eqtb[cur_val];@/
  5040. primitive("expandafter",expand_after,0);@/
  5041. @!@:expand_after_}{\.{\\expandafter} primitive@>
  5042. primitive("font",def_font,0);@/
  5043. @!@:font_}{\.{\\font} primitive@>
  5044. primitive("fontdimen",assign_font_dimen,0);@/
  5045. @!@:font_dimen_}{\.{\\fontdimen} primitive@>
  5046. primitive("halign",halign,0);@/
  5047. @!@:halign_}{\.{\\halign} primitive@>
  5048. primitive("hrule",hrule,0);@/
  5049. @!@:hrule_}{\.{\\hrule} primitive@>
  5050. primitive("ignorespaces",ignore_spaces,0);@/
  5051. @!@:ignore_spaces_}{\.{\\ignorespaces} primitive@>
  5052. primitive("insert",insert,0);@/
  5053. @!@:insert_}{\.{\\insert} primitive@>
  5054. primitive("mark",mark,0);@/
  5055. @!@:mark_}{\.{\\mark} primitive@>
  5056. primitive("mathaccent",math_accent,0);@/
  5057. @!@:math_accent_}{\.{\\mathaccent} primitive@>
  5058. primitive("mathchar",math_char_num,0);@/
  5059. @!@:math_char_}{\.{\\mathchar} primitive@>
  5060. primitive("mathchoice",math_choice,0);@/
  5061. @!@:math_choice_}{\.{\\mathchoice} primitive@>
  5062. primitive("multiply",multiply,0);@/
  5063. @!@:multiply_}{\.{\\multiply} primitive@>
  5064. primitive("noalign",no_align,0);@/
  5065. @!@:no_align_}{\.{\\noalign} primitive@>
  5066. primitive("noboundary",no_boundary,0);@/
  5067. @!@:no_boundary_}{\.{\\noboundary} primitive@>
  5068. primitive("noexpand",no_expand,0);@/
  5069. @!@:no_expand_}{\.{\\noexpand} primitive@>
  5070. primitive("nonscript",non_script,0);@/
  5071. @!@:non_script_}{\.{\\nonscript} primitive@>
  5072. primitive("omit",omit,0);@/
  5073. @!@:omit_}{\.{\\omit} primitive@>
  5074. primitive("parshape",set_shape,0);@/
  5075. @!@:par_shape_}{\.{\\parshape} primitive@>
  5076. primitive("penalty",break_penalty,0);@/
  5077. @!@:penalty_}{\.{\\penalty} primitive@>
  5078. primitive("prevgraf",set_prev_graf,0);@/
  5079. @!@:prev_graf_}{\.{\\prevgraf} primitive@>
  5080. primitive("radical",radical,0);@/
  5081. @!@:radical_}{\.{\\radical} primitive@>
  5082. primitive("read",read_to_cs,0);@/
  5083. @!@:read_}{\.{\\read} primitive@>
  5084. primitive("relax",relax,256); {cf.\ |scan_file_name|}
  5085. @!@:relax_}{\.{\\relax} primitive@>
  5086. text(frozen_relax):="relax"; eqtb[frozen_relax]:=eqtb[cur_val];@/
  5087. primitive("setbox",set_box,0);@/
  5088. @!@:set_box_}{\.{\\setbox} primitive@>
  5089. primitive("the",the,0);@/
  5090. @!@:the_}{\.{\\the} primitive@>
  5091. primitive("toks",toks_register,0);@/
  5092. @!@:toks_}{\.{\\toks} primitive@>
  5093. primitive("vadjust",vadjust,0);@/
  5094. @!@:vadjust_}{\.{\\vadjust} primitive@>
  5095. primitive("valign",valign,0);@/
  5096. @!@:valign_}{\.{\\valign} primitive@>
  5097. primitive("vcenter",vcenter,0);@/
  5098. @!@:vcenter_}{\.{\\vcenter} primitive@>
  5099. primitive("vrule",vrule,0);@/
  5100. @!@:vrule_}{\.{\\vrule} primitive@>
  5101. @ Each primitive has a corresponding inverse, so that it is possible to
  5102. display the cryptic numeric contents of |eqtb| in symbolic form.
  5103. Every call of |primitive| in this program is therefore accompanied by some
  5104. straightforward code that forms part of the |print_cmd_chr| routine
  5105. below.
  5106. @<Cases of |print_cmd_chr|...@>=
  5107. accent: print_esc("accent");
  5108. advance: print_esc("advance");
  5109. after_assignment: print_esc("afterassignment");
  5110. after_group: print_esc("aftergroup");
  5111. assign_font_dimen: print_esc("fontdimen");
  5112. begin_group: print_esc("begingroup");
  5113. break_penalty: print_esc("penalty");
  5114. char_num: print_esc("char");
  5115. cs_name: print_esc("csname");
  5116. def_font: print_esc("font");
  5117. delim_num: print_esc("delimiter");
  5118. divide: print_esc("divide");
  5119. end_cs_name: print_esc("endcsname");
  5120. end_group: print_esc("endgroup");
  5121. ex_space: print_esc(" ");
  5122. expand_after: print_esc("expandafter");
  5123. halign: print_esc("halign");
  5124. hrule: print_esc("hrule");
  5125. ignore_spaces: print_esc("ignorespaces");
  5126. insert: print_esc("insert");
  5127. ital_corr: print_esc("/");
  5128. mark: print_esc("mark");
  5129. math_accent: print_esc("mathaccent");
  5130. math_char_num: print_esc("mathchar");
  5131. math_choice: print_esc("mathchoice");
  5132. multiply: print_esc("multiply");
  5133. no_align: print_esc("noalign");
  5134. no_boundary:print_esc("noboundary");
  5135. no_expand: print_esc("noexpand");
  5136. non_script: print_esc("nonscript");
  5137. omit: print_esc("omit");
  5138. radical: print_esc("radical");
  5139. read_to_cs: print_esc("read");
  5140. relax: print_esc("relax");
  5141. set_box: print_esc("setbox");
  5142. set_prev_graf: print_esc("prevgraf");
  5143. set_shape: print_esc("parshape");
  5144. the: print_esc("the");
  5145. toks_register: print_esc("toks");
  5146. vadjust: print_esc("vadjust");
  5147. valign: print_esc("valign");
  5148. vcenter: print_esc("vcenter");
  5149. vrule: print_esc("vrule");
  5150. @ We will deal with the other primitives later, at some point in the program
  5151. where their |eq_type| and |equiv| values are more meaningful. For example,
  5152. the primitives for math mode will be loaded when we consider the routines
  5153. that deal with formulas. It is easy to find where each particular
  5154. primitive was treated by looking in the index at the end; for example, the
  5155. section where |"radical"| entered |eqtb| is listed under `\.{\\radical}
  5156. primitive'. (Primitives consisting of a single nonalphabetic character,
  5157. @!like `\.{\\/}', are listed under `Single-character primitives'.)
  5158. @!@^Single-character primitives@>
  5159. Meanwhile, this is a convenient place to catch up on something we were unable
  5160. to do before the hash table was defined:
  5161. @<Print the font identifier for |font(p)|@>=
  5162. print_esc(font_id_text(font(p)))
  5163. @* \[19] Saving and restoring equivalents.
  5164. The nested structure provided by `$\.{\char'173}\ldots\.{\char'175}$' groups
  5165. in \TeX\ means that |eqtb| entries valid in outer groups should be saved
  5166. and restored later if they are overridden inside the braces. When a new |eqtb|
  5167. value is being assigned, the program therefore checks to see if the previous
  5168. entry belongs to an outer level. In such a case, the old value is placed
  5169. on the |save_stack| just before the new value enters |eqtb|. At the
  5170. end of a grouping level, i.e., when the right brace is sensed, the
  5171. |save_stack| is used to restore the outer values, and the inner ones are
  5172. destroyed.
  5173. Entries on the |save_stack| are of type |memory_word|. The top item on
  5174. this stack is |save_stack[p]|, where |p=save_ptr-1|; it contains three
  5175. fields called |save_type|, |save_level|, and |save_index|, and it is
  5176. interpreted in one of four ways:
  5177. \yskip\hangg 1) If |save_type(p)=restore_old_value|, then
  5178. |save_index(p)| is a location in |eqtb| whose current value should
  5179. be destroyed at the end of the current group and replaced by |save_stack[p-1]|.
  5180. Furthermore if |save_index(p)>=int_base|, then |save_level(p)|
  5181. should replace the corresponding entry in |xeq_level|.
  5182. \yskip\hangg 2) If |save_type(p)=restore_zero|, then |save_index(p)|
  5183. is a location in |eqtb| whose current value should be destroyed at the end
  5184. of the current group, when it should be
  5185. replaced by the value of |eqtb[undefined_control_sequence]|.
  5186. \yskip\hangg 3) If |save_type(p)=insert_token|, then |save_index(p)|
  5187. is a token that should be inserted into \TeX's input when the current
  5188. group ends.
  5189. \yskip\hangg 4) If |save_type(p)=level_boundary|, then |save_level(p)|
  5190. is a code explaining what kind of group we were previously in, and
  5191. |save_index(p)| points to the level boundary word at the bottom of
  5192. the entries for that group.
  5193. @d save_type(#)==save_stack[#].hh.b0 {classifies a |save_stack| entry}
  5194. @d save_level(#)==save_stack[#].hh.b1
  5195. {saved level for regions 5 and 6, or group code}
  5196. @d save_index(#)==save_stack[#].hh.rh
  5197. {|eqtb| location or token or |save_stack| location}
  5198. @d restore_old_value=0 {|save_type| when a value should be restored later}
  5199. @d restore_zero=1 {|save_type| when an undefined entry should be restored}
  5200. @d insert_token=2 {|save_type| when a token is being saved for later use}
  5201. @d level_boundary=3 {|save_type| corresponding to beginning of group}
  5202. @ Here are the group codes that are used to discriminate between different
  5203. kinds of groups. They allow \TeX\ to decide what special actions, if any,
  5204. should be performed when a group ends.
  5205. \def\grp{\.{\char'173...\char'175}}
  5206. Some groups are not supposed to be ended by right braces. For example,
  5207. the `\.\$' that begins a math formula causes a |math_shift_group| to
  5208. be started, and this should be terminated by a matching `\.\$'. Similarly,
  5209. a group that starts with \.{\\left} should end with \.{\\right}, and
  5210. one that starts with \.{\\begingroup} should end with \.{\\endgroup}.
  5211. @d bottom_level=0 {group code for the outside world}
  5212. @d simple_group=1 {group code for local structure only}
  5213. @d hbox_group=2 {code for `\.{\\hbox}\grp'}
  5214. @d adjusted_hbox_group=3 {code for `\.{\\hbox}\grp' in vertical mode}
  5215. @d vbox_group=4 {code for `\.{\\vbox}\grp'}
  5216. @d vtop_group=5 {code for `\.{\\vtop}\grp'}
  5217. @d align_group=6 {code for `\.{\\halign}\grp', `\.{\\valign}\grp'}
  5218. @d no_align_group=7 {code for `\.{\\noalign}\grp'}
  5219. @d output_group=8 {code for output routine}
  5220. @d math_group=9 {code for, e.g., `\.{\char'136}\grp'}
  5221. @d disc_group=10 {code for `\.{\\discretionary}\grp\grp\grp'}
  5222. @d insert_group=11 {code for `\.{\\insert}\grp', `\.{\\vadjust}\grp'}
  5223. @d vcenter_group=12 {code for `\.{\\vcenter}\grp'}
  5224. @d math_choice_group=13 {code for `\.{\\mathchoice}\grp\grp\grp\grp'}
  5225. @d semi_simple_group=14 {code for `\.{\\begingroup...\\endgroup}'}
  5226. @d math_shift_group=15 {code for `\.{\$...\$}'}
  5227. @d math_left_group=16 {code for `\.{\\left...\\right}'}
  5228. @d max_group_code=16
  5229. @<Types...@>=
  5230. @!group_code=0..max_group_code; {|save_level| for a level boundary}
  5231. @ The global variable |cur_group| keeps track of what sort of group we are
  5232. currently in. Another global variable, |cur_boundary|, points to the
  5233. topmost |level_boundary| word. And |cur_level| is the current depth of
  5234. nesting. The routines are designed to preserve the condition that no entry
  5235. in the |save_stack| or in |eqtb| ever has a level greater than |cur_level|.
  5236. @ @<Glob...@>=
  5237. @!save_stack : array[0..save_size] of memory_word;
  5238. @!save_ptr : 0..save_size; {first unused entry on |save_stack|}
  5239. @!max_save_stack:0..save_size; {maximum usage of save stack}
  5240. @!cur_level: quarterword; {current nesting level for groups}
  5241. @!cur_group: group_code; {current group type}
  5242. @!cur_boundary: 0..save_size; {where the current level begins}
  5243. @ At this time it might be a good idea for the reader to review the introduction
  5244. to |eqtb| that was given above just before the long lists of parameter names.
  5245. Recall that the ``outer level'' of the program is |level_one|, since
  5246. undefined control sequences are assumed to be ``defined'' at |level_zero|.
  5247. @<Set init...@>=
  5248. save_ptr:=0; cur_level:=level_one; cur_group:=bottom_level; cur_boundary:=0;
  5249. max_save_stack:=0;
  5250. @ The following macro is used to test if there is room for up to six more
  5251. entries on |save_stack|. By making a conservative test like this, we can
  5252. get by with testing for overflow in only a few places.
  5253. @d check_full_save_stack==if save_ptr>max_save_stack then
  5254. begin max_save_stack:=save_ptr;
  5255. if max_save_stack>save_size-6 then overflow("save size",save_size);
  5256. @:TeX capacity exceeded save size}{\quad save size@>
  5257. end
  5258. @ Procedure |new_save_level| is called when a group begins. The
  5259. argument is a group identification code like `|hbox_group|'. After
  5260. calling this routine, it is safe to put five more entries on |save_stack|.
  5261. In some cases integer-valued items are placed onto the
  5262. |save_stack| just below a |level_boundary| word, because this is a
  5263. convenient place to keep information that is supposed to ``pop up'' just
  5264. when the group has finished.
  5265. For example, when `\.{\\hbox to 100pt}\grp' is being treated, the 100pt
  5266. dimension is stored on |save_stack| just before |new_save_level| is
  5267. called.
  5268. We use the notation |saved(k)| to stand for an integer item that
  5269. appears in location |save_ptr+k| of the save stack.
  5270. @d saved(#)==save_stack[save_ptr+#].int
  5271. @p procedure new_save_level(@!c:group_code); {begin a new level of grouping}
  5272. begin check_full_save_stack;
  5273. save_type(save_ptr):=level_boundary; save_level(save_ptr):=cur_group;
  5274. save_index(save_ptr):=cur_boundary;
  5275. if cur_level=max_quarterword then overflow("grouping levels",
  5276. @:TeX capacity exceeded grouping levels}{\quad grouping levels@>
  5277. max_quarterword-min_quarterword);
  5278. {quit if |(cur_level+1)| is too big to be stored in |eqtb|}
  5279. cur_boundary:=save_ptr; incr(cur_level); incr(save_ptr); cur_group:=c;
  5280. end;
  5281. @ Just before an entry of |eqtb| is changed, the following procedure should
  5282. be called to update the other data structures properly. It is important
  5283. to keep in mind that reference counts in |mem| include references from
  5284. within |save_stack|, so these counts must be handled carefully.
  5285. @^reference counts@>
  5286. @p procedure eq_destroy(@!w:memory_word); {gets ready to forget |w|}
  5287. var q:pointer; {|equiv| field of |w|}
  5288. begin case eq_type_field(w) of
  5289. call,long_call,outer_call,long_outer_call: delete_token_ref(equiv_field(w));
  5290. glue_ref: delete_glue_ref(equiv_field(w));
  5291. shape_ref: begin q:=equiv_field(w); {we need to free a \.{\\parshape} block}
  5292. if q<>null then free_node(q,info(q)+info(q)+1);
  5293. end; {such a block is |2n+1| words long, where |n=info(q)|}
  5294. box_ref: flush_node_list(equiv_field(w));
  5295. othercases do_nothing
  5296. endcases;
  5297. end;
  5298. @ To save a value of |eqtb[p]| that was established at level |l|, we
  5299. can use the following subroutine.
  5300. @p procedure eq_save(@!p:pointer;@!l:quarterword); {saves |eqtb[p]|}
  5301. begin check_full_save_stack;
  5302. if l=level_zero then save_type(save_ptr):=restore_zero
  5303. else begin save_stack[save_ptr]:=eqtb[p]; incr(save_ptr);
  5304. save_type(save_ptr):=restore_old_value;
  5305. end;
  5306. save_level(save_ptr):=l; save_index(save_ptr):=p; incr(save_ptr);
  5307. end;
  5308. @ The procedure |eq_define| defines an |eqtb| entry having specified
  5309. |eq_type| and |equiv| fields, and saves the former value if appropriate.
  5310. This procedure is used only for entries in the first four regions of |eqtb|,
  5311. i.e., only for entries that have |eq_type| and |equiv| fields.
  5312. After calling this routine, it is safe to put four more entries on
  5313. |save_stack|, provided that there was room for four more entries before
  5314. the call, since |eq_save| makes the necessary test.
  5315. @p procedure eq_define(@!p:pointer;@!t:quarterword;@!e:halfword);
  5316. {new data for |eqtb|}
  5317. begin if eq_level(p)=cur_level then eq_destroy(eqtb[p])
  5318. else if cur_level>level_one then eq_save(p,eq_level(p));
  5319. eq_level(p):=cur_level; eq_type(p):=t; equiv(p):=e;
  5320. end;
  5321. @ The counterpart of |eq_define| for the remaining (fullword) positions in
  5322. |eqtb| is called |eq_word_define|. Since |xeq_level[p]>=level_one| for all
  5323. |p|, a `|restore_zero|' will never be used in this case.
  5324. @p procedure eq_word_define(@!p:pointer;@!w:integer);
  5325. begin if xeq_level[p]<>cur_level then
  5326. begin eq_save(p,xeq_level[p]); xeq_level[p]:=cur_level;
  5327. end;
  5328. eqtb[p].int:=w;
  5329. end;
  5330. @ The |eq_define| and |eq_word_define| routines take care of local definitions.
  5331. @^global definitions@>
  5332. Global definitions are done in almost the same way, but there is no need
  5333. to save old values, and the new value is associated with |level_one|.
  5334. @p procedure geq_define(@!p:pointer;@!t:quarterword;@!e:halfword);
  5335. {global |eq_define|}
  5336. begin eq_destroy(eqtb[p]);
  5337. eq_level(p):=level_one; eq_type(p):=t; equiv(p):=e;
  5338. end;
  5339. @#
  5340. procedure geq_word_define(@!p:pointer;@!w:integer); {global |eq_word_define|}
  5341. begin eqtb[p].int:=w; xeq_level[p]:=level_one;
  5342. end;
  5343. @ Subroutine |save_for_after| puts a token on the stack for save-keeping.
  5344. @p procedure save_for_after(@!t:halfword);
  5345. begin if cur_level>level_one then
  5346. begin check_full_save_stack;
  5347. save_type(save_ptr):=insert_token; save_level(save_ptr):=level_zero;
  5348. save_index(save_ptr):=t; incr(save_ptr);
  5349. end;
  5350. end;
  5351. @ The |unsave| routine goes the other way, taking items off of |save_stack|.
  5352. This routine takes care of restoration when a level ends; everything
  5353. belonging to the topmost group is cleared off of the save stack.
  5354. @p@t\4@>@<Declare the procedure called |restore_trace|@>@;@/
  5355. procedure@?back_input; forward; @t\2@>
  5356. procedure unsave; {pops the top level off the save stack}
  5357. label done;
  5358. var p:pointer; {position to be restored}
  5359. @!l:quarterword; {saved level, if in fullword regions of |eqtb|}
  5360. @!t:halfword; {saved value of |cur_tok|}
  5361. begin if cur_level>level_one then
  5362. begin decr(cur_level);
  5363. @<Clear off top level from |save_stack|@>;
  5364. end
  5365. else confusion("curlevel"); {|unsave| is not used when |cur_group=bottom_level|}
  5366. @:this can't happen curlevel}{\quad curlevel@>
  5367. end;
  5368. @ @<Clear off...@>=
  5369. loop@+begin decr(save_ptr);
  5370. if save_type(save_ptr)=level_boundary then goto done;
  5371. p:=save_index(save_ptr);
  5372. if save_type(save_ptr)=insert_token then
  5373. @<Insert token |p| into \TeX's input@>
  5374. else begin if save_type(save_ptr)=restore_old_value then
  5375. begin l:=save_level(save_ptr); decr(save_ptr);
  5376. end
  5377. else save_stack[save_ptr]:=eqtb[undefined_control_sequence];
  5378. @<Store \(s)|save_stack[save_ptr]| in |eqtb[p]|, unless
  5379. |eqtb[p]| holds a global value@>;
  5380. end;
  5381. end;
  5382. done: cur_group:=save_level(save_ptr); cur_boundary:=save_index(save_ptr)
  5383. @ A global definition, which sets the level to |level_one|,
  5384. @^global definitions@>
  5385. will not be undone by |unsave|. If at least one global definition of
  5386. |eqtb[p]| has been carried out within the group that just ended, the
  5387. last such definition will therefore survive.
  5388. @<Store \(s)|save...@>=
  5389. if p<int_base then
  5390. if eq_level(p)=level_one then
  5391. begin eq_destroy(save_stack[save_ptr]); {destroy the saved value}
  5392. @!stat if tracing_restores>0 then restore_trace(p,"retaining");@+tats@;@/
  5393. end
  5394. else begin eq_destroy(eqtb[p]); {destroy the current value}
  5395. eqtb[p]:=save_stack[save_ptr]; {restore the saved value}
  5396. @!stat if tracing_restores>0 then restore_trace(p,"restoring");@+tats@;@/
  5397. end
  5398. else if xeq_level[p]<>level_one then
  5399. begin eqtb[p]:=save_stack[save_ptr]; xeq_level[p]:=l;
  5400. @!stat if tracing_restores>0 then restore_trace(p,"restoring");@+tats@;@/
  5401. end
  5402. else begin
  5403. @!stat if tracing_restores>0 then restore_trace(p,"retaining");@+tats@;@/
  5404. end
  5405. @ @<Declare the procedure called |restore_trace|@>=
  5406. @!stat procedure restore_trace(@!p:pointer;@!s:str_number);
  5407. {|eqtb[p]| has just been restored or retained}
  5408. begin begin_diagnostic; print_char("{"); print(s); print_char(" ");
  5409. show_eqtb(p); print_char("}");
  5410. end_diagnostic(false);
  5411. end;
  5412. tats
  5413. @ When looking for possible pointers to a memory location, it is helpful
  5414. to look for references from |eqtb| that might be waiting on the
  5415. save stack. Of course, we might find spurious pointers too; but this
  5416. routine is merely an aid when debugging, and at such times we are
  5417. grateful for any scraps of information, even if they prove to be irrelevant.
  5418. @^dirty \PASCAL@>
  5419. @<Search |save_stack| for equivalents that point to |p|@>=
  5420. if save_ptr>0 then for q:=0 to save_ptr-1 do
  5421. begin if equiv_field(save_stack[q])=p then
  5422. begin print_nl("SAVE("); print_int(q); print_char(")");
  5423. end;
  5424. end
  5425. @ Most of the parameters kept in |eqtb| can be changed freely, but there's
  5426. an exception: The magnification should not be used with two different
  5427. values during any \TeX\ job, since a single magnification is applied to an
  5428. entire run. The global variable |mag_set| is set to the current magnification
  5429. whenever it becomes necessary to ``freeze'' it at a particular value.
  5430. @<Glob...@>=
  5431. @!mag_set:integer; {if nonzero, this magnification should be used henceforth}
  5432. @ @<Set init...@>=
  5433. mag_set:=0;
  5434. @ The |prepare_mag| subroutine is called whenever \TeX\ wants to use |mag|
  5435. for magnification.
  5436. @p procedure prepare_mag;
  5437. begin if (mag_set>0)and(mag<>mag_set) then
  5438. begin print_err("Incompatible magnification ("); print_int(mag);
  5439. @.Incompatible magnification@>
  5440. print(");"); print_nl(" the previous value will be retained");
  5441. help2("I can handle only one magnification ratio per job. So I've")@/
  5442. ("reverted to the magnification you used earlier on this run.");@/
  5443. int_error(mag_set);
  5444. geq_word_define(int_base+mag_code,mag_set); {|mag:=mag_set|}
  5445. end;
  5446. if (mag<=0)or(mag>32768) then
  5447. begin print_err("Illegal magnification has been changed to 1000");@/
  5448. @.Illegal magnification...@>
  5449. help1("The magnification ratio must be between 1 and 32768.");
  5450. int_error(mag); geq_word_define(int_base+mag_code,1000);
  5451. end;
  5452. mag_set:=mag;
  5453. end;
  5454. @* \[20] Token lists.
  5455. A \TeX\ token is either a character or a control sequence, and it is
  5456. @^token@>
  5457. represented internally in one of two ways: (1)~A character whose ASCII
  5458. code number is |c| and whose command code is |m| is represented as the
  5459. number $2^8m+c$; the command code is in the range |1<=m<=14|. (2)~A control
  5460. sequence whose |eqtb| address is |p| is represented as the number
  5461. |cs_token_flag+p|. Here |cs_token_flag=@t$2^{12}-1$@>| is larger than
  5462. $2^8m+c$, yet it is small enough that |cs_token_flag+p< max_halfword|;
  5463. thus, a token fits comfortably in a halfword.
  5464. A token |t| represents a |left_brace| command if and only if
  5465. |t<left_brace_limit|; it represents a |right_brace| command if and only if
  5466. we have |left_brace_limit<=t<right_brace_limit|; and it represents a |match| or
  5467. |end_match| command if and only if |match_token<=t<=end_match_token|.
  5468. The following definitions take care of these token-oriented constants
  5469. and a few others.
  5470. @d cs_token_flag==@'7777 {amount added to the |eqtb| location in a
  5471. token that stands for a control sequence; is a multiple of~256, less~1}
  5472. @d left_brace_token=@'0400 {$2^8\cdot|left_brace|$}
  5473. @d left_brace_limit=@'1000 {$2^8\cdot(|left_brace|+1)$}
  5474. @d right_brace_token=@'1000 {$2^8\cdot|right_brace|$}
  5475. @d right_brace_limit=@'1400 {$2^8\cdot(|right_brace|+1)$}
  5476. @d math_shift_token=@'1400 {$2^8\cdot|math_shift|$}
  5477. @d tab_token=@'2000 {$2^8\cdot|tab_mark|$}
  5478. @d out_param_token=@'2400 {$2^8\cdot|out_param|$}
  5479. @d space_token=@'5040 {$2^8\cdot|spacer|+|" "|$}
  5480. @d letter_token=@'5400 {$2^8\cdot|letter|$}
  5481. @d other_token=@'6000 {$2^8\cdot|other_char|$}
  5482. @d match_token=@'6400 {$2^8\cdot|match|$}
  5483. @d end_match_token=@'7000 {$2^8\cdot|end_match|$}
  5484. @ @<Check the ``constant''...@>=
  5485. if cs_token_flag+undefined_control_sequence>max_halfword then bad:=21;
  5486. @ A token list is a singly linked list of one-word nodes in |mem|, where
  5487. each word contains a token and a link. Macro definitions, output-routine
  5488. definitions, marks, \.{\\write} texts, and a few other things
  5489. are remembered by \TeX\ in the form
  5490. of token lists, usually preceded by a node with a reference count in its
  5491. |token_ref_count| field. The token stored in location |p| is called
  5492. |info(p)|.
  5493. Three special commands appear in the token lists of macro definitions.
  5494. When |m=match|, it means that \TeX\ should scan a parameter
  5495. for the current macro; when |m=end_match|, it means that parameter
  5496. matching should end and \TeX\ should start reading the macro text; and
  5497. when |m=out_param|, it means that \TeX\ should insert parameter
  5498. number |c| into the text at this point.
  5499. The enclosing \.{\char'173} and \.{\char'175} characters of a macro
  5500. definition are omitted, but an output routine
  5501. will be enclosed in braces.
  5502. Here is an example macro definition that illustrates these conventions.
  5503. After \TeX\ processes the text
  5504. $$\.{\\def\\mac a\#1\#2 \\b \{\#1\\-a \#\#1\#2 \#2\}}$$
  5505. the definition of \.{\\mac} is represented as a token list containing
  5506. $$\def\,{\hskip2pt}
  5507. \vbox{\halign{\hfil#\hfil\cr
  5508. (reference count), |letter|\,\.a, |match|\,\#, |match|\,\#, |spacer|\,\.\ ,
  5509. \.{\\b}, |end_match|,\cr
  5510. |out_param|\,1, \.{\\-}, |letter|\,\.a, |spacer|\,\.\ , |mac_param|\,\#,
  5511. |other_char|\,\.1,\cr
  5512. |out_param|\,2, |spacer|\,\.\ , |out_param|\,2.\cr}}$$
  5513. The procedure |scan_toks| builds such token lists, and |macro_call|
  5514. does the parameter matching.
  5515. @^reference counts@>
  5516. Examples such as
  5517. $$\.{\\def\\m\{\\def\\m\{a\}\ b\}}$$
  5518. explain why reference counts would be needed even if \TeX\ had no \.{\\let}
  5519. operation: When the token list for \.{\\m} is being read, the redefinition of
  5520. \.{\\m} changes the |eqtb| entry before the token list has been fully
  5521. consumed, so we dare not simply destroy a token list when its
  5522. control sequence is being redefined.
  5523. If the parameter-matching part of a definition ends with `\.{\#\{}',
  5524. the corresponding token list will have `\.\{' just before the `|end_match|'
  5525. and also at the very end. The first `\.\{' is used to delimit the parameter; the
  5526. second one keeps the first from disappearing.
  5527. @ The procedure |show_token_list|, which prints a symbolic form of
  5528. the token list that starts at a given node |p|, illustrates these
  5529. conventions. The token list being displayed should not begin with a reference
  5530. count. However, the procedure is intended to be robust, so that if the
  5531. memory links are awry or if |p| is not really a pointer to a token list,
  5532. nothing catastrophic will happen.
  5533. An additional parameter |q| is also given; this parameter is either null
  5534. or it points to a node in the token list where a certain magic computation
  5535. takes place that will be explained later. (Basically, |q| is non-null when
  5536. we are printing the two-line context information at the time of an error
  5537. message; |q| marks the place corresponding to where the second line
  5538. should begin.)
  5539. For example, if |p| points to the node containing the first \.a in the
  5540. token list above, then |show_token_list| will print the string
  5541. $$\hbox{`\.{a\#1\#2\ \\b\ ->\#1\\-a\ \#\#1\#2\ \#2}';}$$
  5542. and if |q| points to the node containing the second \.a,
  5543. the magic computation will be performed just before the second \.a is printed.
  5544. The generation will stop, and `\.{\\ETC.}' will be printed, if the length
  5545. of printing exceeds a given limit~|l|. Anomalous entries are printed in the
  5546. form of control sequences that are not followed by a blank space, e.g.,
  5547. `\.{\\BAD.}'; this cannot be confused with actual control sequences because
  5548. a real control sequence named \.{BAD} would come out `\.{\\BAD\ }'.
  5549. @<Declare the procedure called |show_token_list|@>=
  5550. procedure show_token_list(@!p,@!q:integer;@!l:integer);
  5551. label exit;
  5552. var m,@!c:integer; {pieces of a token}
  5553. @!match_chr:ASCII_code; {character used in a `|match|'}
  5554. @!n:ASCII_code; {the highest parameter number, as an ASCII digit}
  5555. begin match_chr:="#"; n:="0"; tally:=0;
  5556. while (p<>null) and (tally<l) do
  5557. begin if p=q then @<Do magic computation@>;
  5558. @<Display token |p|, and |return| if there are problems@>;
  5559. p:=link(p);
  5560. end;
  5561. if p<>null then print_esc("ETC.");
  5562. @.ETC@>
  5563. exit:
  5564. end;
  5565. @ @<Display token |p|...@>=
  5566. if (p<hi_mem_min) or (p>mem_end) then
  5567. begin print_esc("CLOBBERED."); return;
  5568. @.CLOBBERED@>
  5569. end;
  5570. if info(p)>=cs_token_flag then print_cs(info(p)-cs_token_flag)
  5571. else begin m:=info(p) div @'400; c:=info(p) mod @'400;
  5572. if info(p)<0 then print_esc("BAD.")
  5573. @.BAD@>
  5574. else @<Display the token $(|m|,|c|)$@>;
  5575. end
  5576. @ The procedure usually ``learns'' the character code used for macro
  5577. parameters by seeing one in a |match| command before it runs into any
  5578. |out_param| commands.
  5579. @<Display the token ...@>=
  5580. case m of
  5581. left_brace,right_brace,math_shift,tab_mark,sup_mark,sub_mark,spacer,
  5582. letter,other_char: print(c);
  5583. mac_param: begin print(c); print(c);
  5584. end;
  5585. out_param: begin print(match_chr);
  5586. if c<=9 then print_char(c+"0")
  5587. else begin print_char("!"); return;
  5588. end;
  5589. end;
  5590. match: begin match_chr:=c; print(c); incr(n); print_char(n);
  5591. if n>"9" then return;
  5592. end;
  5593. end_match: print("->");
  5594. @.->@>
  5595. othercases print_esc("BAD.")
  5596. @.BAD@>
  5597. endcases
  5598. @ Here's the way we sometimes want to display a token list, given a pointer
  5599. to its reference count; the pointer may be null.
  5600. @p procedure token_show(@!p:pointer);
  5601. begin if p<>null then show_token_list(link(p),null,10000000);
  5602. end;
  5603. @ The |print_meaning| subroutine displays |cur_cmd| and |cur_chr| in
  5604. symbolic form, including the expansion of a macro or mark.
  5605. @p procedure print_meaning;
  5606. begin print_cmd_chr(cur_cmd,cur_chr);
  5607. if cur_cmd>=call then
  5608. begin print_char(":"); print_ln; token_show(cur_chr);
  5609. end
  5610. else if cur_cmd=top_bot_mark then
  5611. begin print_char(":"); print_ln;
  5612. token_show(cur_mark[cur_chr]);
  5613. end;
  5614. end;
  5615. @* \[21] Introduction to the syntactic routines.
  5616. Let's pause a moment now and try to look at the Big Picture.
  5617. The \TeX\ program consists of three main parts: syntactic routines,
  5618. semantic routines, and output routines. The chief purpose of the
  5619. syntactic routines is to deliver the user's input to the semantic routines,
  5620. one token at a time. The semantic routines act as an interpreter
  5621. responding to these tokens, which may be regarded as commands. And the
  5622. output routines are periodically called on to convert box-and-glue
  5623. lists into a compact set of instructions that will be sent
  5624. to a typesetter. We have discussed the basic data structures and utility
  5625. routines of \TeX, so we are good and ready to plunge into the real activity by
  5626. considering the syntactic routines.
  5627. Our current goal is to come to grips with the |get_next| procedure,
  5628. which is the keystone of \TeX's input mechanism. Each call of |get_next|
  5629. sets the value of three variables |cur_cmd|, |cur_chr|, and |cur_cs|,
  5630. representing the next input token.
  5631. $$\vbox{\halign{#\hfil\cr
  5632. \hbox{|cur_cmd| denotes a command code from the long list of codes
  5633. given above;}\cr
  5634. \hbox{|cur_chr| denotes a character code or other modifier of the command
  5635. code;}\cr
  5636. \hbox{|cur_cs| is the |eqtb| location of the current control sequence,}\cr
  5637. \hbox{\qquad if the current token was a control sequence,
  5638. otherwise it's zero.}\cr}}$$
  5639. Underlying this external behavior of |get_next| is all the machinery
  5640. necessary to convert from character files to tokens. At a given time we
  5641. may be only partially finished with the reading of several files (for
  5642. which \.{\\input} was specified), and partially finished with the expansion
  5643. of some user-defined macros and/or some macro parameters, and partially
  5644. finished with the generation of some text in a template for \.{\\halign},
  5645. and so on. When reading a character file, special characters must be
  5646. classified as math delimiters, etc.; comments and extra blank spaces must
  5647. be removed, paragraphs must be recognized, and control sequences must be
  5648. found in the hash table. Furthermore there are occasions in which the
  5649. scanning routines have looked ahead for a word like `\.{plus}' but only
  5650. part of that word was found, hence a few characters must be put back
  5651. into the input and scanned again.
  5652. To handle these situations, which might all be present simultaneously,
  5653. \TeX\ uses various stacks that hold information about the incomplete
  5654. activities, and there is a finite state control for each level of the
  5655. input mechanism. These stacks record the current state of an implicitly
  5656. recursive process, but the |get_next| procedure is not recursive.
  5657. Therefore it will not be difficult to translate these algorithms into
  5658. low-level languages that do not support recursion.
  5659. @<Glob...@>=
  5660. @!cur_cmd: eight_bits; {current command set by |get_next|}
  5661. @!cur_chr: halfword; {operand of current command}
  5662. @!cur_cs: pointer; {control sequence found here, zero if none found}
  5663. @!cur_tok: halfword; {packed representative of |cur_cmd| and |cur_chr|}
  5664. @ The |print_cmd_chr| routine prints a symbolic interpretation of a
  5665. command code and its modifier. This is used in certain `\.{You can\'t}'
  5666. error messages, and in the implementation of diagnostic routines like
  5667. \.{\\show}.
  5668. The body of |print_cmd_chr| is a rather tedious listing of print
  5669. commands, and most of it is essentially an inverse to the |primitive|
  5670. routine that enters a \TeX\ primitive into |eqtb|. Therefore much of
  5671. this procedure appears elsewhere in the program,
  5672. together with the corresponding |primitive| calls.
  5673. @d chr_cmd(#)==begin print(#); print_ASCII(chr_code);
  5674. end
  5675. @<Declare the procedure called |print_cmd_chr|@>=
  5676. procedure print_cmd_chr(@!cmd:quarterword;@!chr_code:halfword);
  5677. begin case cmd of
  5678. left_brace: chr_cmd("begin-group character ");
  5679. right_brace: chr_cmd("end-group character ");
  5680. math_shift: chr_cmd("math shift character ");
  5681. mac_param: chr_cmd("macro parameter character ");
  5682. sup_mark: chr_cmd("superscript character ");
  5683. sub_mark: chr_cmd("subscript character ");
  5684. endv: print("end of alignment template");
  5685. spacer: chr_cmd("blank space ");
  5686. letter: chr_cmd("the letter ");
  5687. other_char: chr_cmd("the character ");
  5688. @t\4@>@<Cases of |print_cmd_chr| for symbolic printing of primitives@>@/
  5689. othercases print("[unknown command code!]")
  5690. endcases;
  5691. end;
  5692. @ Here is a procedure that displays the current command.
  5693. @p procedure show_cur_cmd_chr;
  5694. begin begin_diagnostic; print_nl("{");
  5695. if mode<>shown_mode then
  5696. begin print_mode(mode); print(": "); shown_mode:=mode;
  5697. end;
  5698. print_cmd_chr(cur_cmd,cur_chr); print_char("}");
  5699. end_diagnostic(false);
  5700. end;
  5701. @* \[22] Input stacks and states.
  5702. This implementation of
  5703. \TeX\ uses two different conventions for representing sequential stacks.
  5704. @^stack conventions@>@^conventions for representing stacks@>
  5705. \yskip\hangg 1) If there is frequent access to the top entry, and if the
  5706. stack is essentially never empty, then the top entry is kept in a global
  5707. variable (even better would be a machine register), and the other entries
  5708. appear in the array $\\{stack}[0\to(\\{ptr}-1)]$. For example, the
  5709. semantic stack described above is handled this way, and so is the input
  5710. stack that we are about to study.
  5711. \yskip\hangg 2) If there is infrequent top access, the entire stack contents
  5712. are in the array $\\{stack}[0\to(\\{ptr}-1)]$. For example, the |save_stack|
  5713. is treated this way, as we have seen.
  5714. \yskip\noindent
  5715. The state of \TeX's input mechanism appears in the input stack, whose
  5716. entries are records with six fields, called |state|, |index|, |start|, |loc|,
  5717. |limit|, and |name|. This stack is maintained with
  5718. convention~(1), so it is declared in the following way:
  5719. @<Types...@>=
  5720. @!in_state_record = record
  5721. @!state_field, @!index_field: quarterword;
  5722. @!start_field,@!loc_field, @!limit_field, @!name_field: halfword;
  5723. end;
  5724. @ @<Glob...@>=
  5725. @!input_stack : array[0..stack_size] of in_state_record;
  5726. @!input_ptr : 0..stack_size; {first unused location of |input_stack|}
  5727. @!max_in_stack: 0..stack_size; {largest value of |input_ptr| when pushing}
  5728. @!cur_input : in_state_record;
  5729. {the ``top'' input state, according to convention (1)}
  5730. @ We've already defined the special variable |loc==cur_input.loc_field|
  5731. in our discussion of basic input-output routines. The other components of
  5732. |cur_input| are defined in the same way:
  5733. @d state==cur_input.state_field {current scanner state}
  5734. @d index==cur_input.index_field {reference for buffer information}
  5735. @d start==cur_input.start_field {starting position in |buffer|}
  5736. @d limit==cur_input.limit_field {end of current line in |buffer|}
  5737. @d name==cur_input.name_field {name of the current file}
  5738. @ Let's look more closely now at the control variables
  5739. (|state|,~|index|,~|start|,~|loc|,~|limit|,~|name|),
  5740. assuming that \TeX\ is reading a line of characters that have been input
  5741. from some file or from the user's terminal. There is an array called
  5742. |buffer| that acts as a stack of all lines of characters that are
  5743. currently being read from files, including all lines on subsidiary
  5744. levels of the input stack that are not yet completed. \TeX\ will return to
  5745. the other lines when it is finished with the present input file.
  5746. (Incidentally, on a machine with byte-oriented addressing, it might be
  5747. appropriate to combine |buffer| with the |str_pool| array,
  5748. letting the buffer entries grow downward from the top of the string pool
  5749. and checking that these two tables don't bump into each other.)
  5750. The line we are currently working on begins in position |start| of the
  5751. buffer; the next character we are about to read is |buffer[loc]|; and
  5752. |limit| is the location of the last character present. If |loc>limit|,
  5753. the line has been completely read. Usually |buffer[limit]| is the
  5754. |end_line_char|, denoting the end of a line, but this is not
  5755. true if the current line is an insertion that was entered on the user's
  5756. terminal in response to an error message.
  5757. The |name| variable is a string number that designates the name of
  5758. the current file, if we are reading a text file. It is zero if we
  5759. are reading from the terminal; it is |n+1| if we are reading from
  5760. input stream |n|, where |0<=n<=16|. (Input stream 16 stands for
  5761. an invalid stream number; in such cases the input is actually from
  5762. the terminal, under control of the procedure |read_toks|.)
  5763. The |state| variable has one of three values, when we are scanning such
  5764. files:
  5765. $$\baselineskip 15pt\vbox{\halign{#\hfil\cr
  5766. 1) |state=mid_line| is the normal state.\cr
  5767. 2) |state=skip_blanks| is like |mid_line|, but blanks are ignored.\cr
  5768. 3) |state=new_line| is the state at the beginning of a line.\cr}}$$
  5769. These state values are assigned numeric codes so that if we add the state
  5770. code to the next character's command code, we get distinct values. For
  5771. example, `|mid_line+spacer|' stands for the case that a blank
  5772. space character occurs in the middle of a line when it is not being
  5773. ignored; after this case is processed, the next value of |state| will
  5774. be |skip_blanks|.
  5775. @d mid_line=1 {|state| code when scanning a line of characters}
  5776. @d skip_blanks=2+max_char_code {|state| code when ignoring blanks}
  5777. @d new_line=3+max_char_code+max_char_code {|state| code at start of line}
  5778. @ Additional information about the current line is available via the
  5779. |index| variable, which counts how many lines of characters are present
  5780. in the buffer below the current level. We have |index=0| when reading
  5781. from the terminal and prompting the user for each line; then if the user types,
  5782. e.g., `\.{\\input paper}', we will have |index=1| while reading
  5783. the file \.{paper.tex}. However, it does not follow that |index| is the
  5784. same as the input stack pointer, since many of the levels on the input
  5785. stack may come from token lists. For example, the instruction `\.{\\input
  5786. paper}' might occur in a token list.
  5787. The global variable |in_open| is equal to the |index|
  5788. value of the highest non-token-list level. Thus, the number of partially read
  5789. lines in the buffer is |in_open+1|, and we have |in_open=index|
  5790. when we are not reading a token list.
  5791. If we are not currently reading from the terminal, or from an input
  5792. stream, we are reading from the file variable |input_file[index]|. We use
  5793. the notation |terminal_input| as a convenient abbreviation for |name=0|,
  5794. and |cur_file| as an abbreviation for |input_file[index]|.
  5795. The global variable |line| contains the line number in the topmost
  5796. open file, for use in error messages. If we are not reading from
  5797. the terminal, |line_stack[index]| holds the line number for the
  5798. enclosing level, so that |line| can be restored when the current
  5799. file has been read. Line numbers should never be negative, since the
  5800. negative of the current line number is used to identify the user's output
  5801. routine in the |mode_line| field of the semantic nest entries.
  5802. If more information about the input state is needed, it can be
  5803. included in small arrays like those shown here. For example,
  5804. the current page or segment number in the input file might be
  5805. put into a variable |@!page|, maintained for enclosing levels in
  5806. `\ignorespaces|@!page_stack:array[1..max_in_open] of integer|\unskip'
  5807. by analogy with |line_stack|.
  5808. @^system dependencies@>
  5809. @d terminal_input==(name=0) {are we reading from the terminal?}
  5810. @d cur_file==input_file[index] {the current |alpha_file| variable}
  5811. @<Glob...@>=
  5812. @!in_open : 0..max_in_open; {the number of lines in the buffer, less one}
  5813. @!open_parens : 0..max_in_open; {the number of open text files}
  5814. @!input_file : array[1..max_in_open] of alpha_file;
  5815. @!line : integer; {current line number in the current source file}
  5816. @!line_stack : array[1..max_in_open] of integer;
  5817. @ Users of \TeX\ sometimes forget to balance left and right braces properly,
  5818. and one of the ways \TeX\ tries to spot such errors is by considering an
  5819. input file as broken into subfiles by control sequences that
  5820. are declared to be \.{\\outer}.
  5821. A variable called |scanner_status| tells \TeX\ whether or not to complain
  5822. when a subfile ends. This variable has six possible values:
  5823. \yskip\hang|normal|, means that a subfile can safely end here without incident.
  5824. \yskip\hang|skipping|, means that a subfile can safely end here, but not a file,
  5825. because we're reading past some conditional text that was not selected.
  5826. \yskip\hang|defining|, means that a subfile shouldn't end now because a
  5827. macro is being defined.
  5828. \yskip\hang|matching|, means that a subfile shouldn't end now because a
  5829. macro is being used and we are searching for the end of its arguments.
  5830. \yskip\hang|aligning|, means that a subfile shouldn't end now because we are
  5831. not finished with the preamble of an \.{\\halign} or \.{\\valign}.
  5832. \yskip\hang|absorbing|, means that a subfile shouldn't end now because we are
  5833. reading a balanced token list for \.{\\message}, \.{\\write}, etc.
  5834. \yskip\noindent
  5835. If the |scanner_status| is not |normal|, the variable |warning_index| points
  5836. to the |eqtb| location for the relevant control sequence name to print
  5837. in an error message.
  5838. @d skipping=1 {|scanner_status| when passing conditional text}
  5839. @d defining=2 {|scanner_status| when reading a macro definition}
  5840. @d matching=3 {|scanner_status| when reading macro arguments}
  5841. @d aligning=4 {|scanner_status| when reading an alignment preamble}
  5842. @d absorbing=5 {|scanner_status| when reading a balanced text}
  5843. @<Glob...@>=
  5844. @!scanner_status : normal..absorbing; {can a subfile end now?}
  5845. @!warning_index : pointer; {identifier relevant to non-|normal| scanner status}
  5846. @!def_ref : pointer; {reference count of token list being defined}
  5847. @ Here is a procedure that uses |scanner_status| to print a warning message
  5848. when a subfile has ended, and at certain other crucial times:
  5849. @<Declare the procedure called |runaway|@>=
  5850. procedure runaway;
  5851. var p:pointer; {head of runaway list}
  5852. begin if scanner_status>skipping then
  5853. begin print_nl("Runaway ");
  5854. @.Runaway...@>
  5855. case scanner_status of
  5856. defining: begin print("definition"); p:=def_ref;
  5857. end;
  5858. matching: begin print("argument"); p:=temp_head;
  5859. end;
  5860. aligning: begin print("preamble"); p:=hold_head;
  5861. end;
  5862. absorbing: begin print("text"); p:=def_ref;
  5863. end;
  5864. end; {there are no other cases}
  5865. print_char("?");print_ln; show_token_list(link(p),null,error_line-10);
  5866. end;
  5867. end;
  5868. @ However, all this discussion about input state really applies only to the
  5869. case that we are inputting from a file. There is another important case,
  5870. namely when we are currently getting input from a token list. In this case
  5871. |state=token_list|, and the conventions about the other state variables
  5872. are different:
  5873. \yskip\hang|loc| is a pointer to the current node in the token list, i.e.,
  5874. the node that will be read next. If |loc=null|, the token list has been
  5875. fully read.
  5876. \yskip\hang|start| points to the first node of the token list; this node
  5877. may or may not contain a reference count, depending on the type of token
  5878. list involved.
  5879. \yskip\hang|token_type|, which takes the place of |index| in the
  5880. discussion above, is a code number that explains what kind of token list
  5881. is being scanned.
  5882. \yskip\hang|name| points to the |eqtb| address of the control sequence
  5883. being expanded, if the current token list is a macro.
  5884. \yskip\hang|param_start|, which takes the place of |limit|, tells where
  5885. the parameters of the current macro begin in the |param_stack|, if the
  5886. current token list is a macro.
  5887. \yskip\noindent The |token_type| can take several values, depending on
  5888. where the current token list came from:
  5889. \yskip\hang|parameter|, if a parameter is being scanned;
  5890. \hang|u_template|, if the \<u_j> part of an alignment
  5891. template is being scanned;
  5892. \hang|v_template|, if the \<v_j> part of an alignment
  5893. template is being scanned;
  5894. \hang|backed_up|, if the token list being scanned has been inserted as
  5895. `to be read again';
  5896. \hang|inserted|, if the token list being scanned has been inserted as
  5897. the text expansion of a \.{\\count} or similar variable;
  5898. \hang|macro|, if a user-defined control sequence is being scanned;
  5899. \hang|output_text|, if an \.{\\output} routine is being scanned;
  5900. \hang|every_par_text|, if the text of \.{\\everypar} is being scanned;
  5901. \hang|every_math_text|, if the text of \.{\\everymath} is being scanned;
  5902. \hang|every_display_text|, if the text of \.{\\everydisplay} is being scanned;
  5903. \hang|every_hbox_text|, if the text of \.{\\everyhbox} is being scanned;
  5904. \hang|every_vbox_text|, if the text of \.{\\everyvbox} is being scanned;
  5905. \hang|every_job_text|, if the text of \.{\\everyjob} is being scanned;
  5906. \hang|every_cr_text|, if the text of \.{\\everycr} is being scanned;
  5907. \hang|mark_text|, if the text of a \.{\\mark} is being scanned;
  5908. \hang|write_text|, if the text of a \.{\\write} is being scanned.
  5909. \yskip\noindent
  5910. The codes for |output_text|, |every_par_text|, etc., are equal to a constant
  5911. plus the corresponding codes for token list parameters |output_routine_loc|,
  5912. |every_par_loc|, etc. The token list begins with a reference count if and
  5913. only if |token_type>=macro|.
  5914. @^reference counts@>
  5915. @d token_list=0 {|state| code when scanning a token list}
  5916. @d token_type==index {type of current token list}
  5917. @d param_start==limit {base of macro parameters in |param_stack|}
  5918. @d parameter=0 {|token_type| code for parameter}
  5919. @d u_template=1 {|token_type| code for \<u_j> template}
  5920. @d v_template=2 {|token_type| code for \<v_j> template}
  5921. @d backed_up=3 {|token_type| code for text to be reread}
  5922. @d inserted=4 {|token_type| code for inserted texts}
  5923. @d macro=5 {|token_type| code for defined control sequences}
  5924. @d output_text=6 {|token_type| code for output routines}
  5925. @d every_par_text=7 {|token_type| code for \.{\\everypar}}
  5926. @d every_math_text=8 {|token_type| code for \.{\\everymath}}
  5927. @d every_display_text=9 {|token_type| code for \.{\\everydisplay}}
  5928. @d every_hbox_text=10 {|token_type| code for \.{\\everyhbox}}
  5929. @d every_vbox_text=11 {|token_type| code for \.{\\everyvbox}}
  5930. @d every_job_text=12 {|token_type| code for \.{\\everyjob}}
  5931. @d every_cr_text=13 {|token_type| code for \.{\\everycr}}
  5932. @d mark_text=14 {|token_type| code for \.{\\topmark}, etc.}
  5933. @d write_text=15 {|token_type| code for \.{\\write}}
  5934. @ The |param_stack| is an auxiliary array used to hold pointers to the token
  5935. lists for parameters at the current level and subsidiary levels of input.
  5936. This stack is maintained with convention (2), and it grows at a different
  5937. rate from the others.
  5938. @<Glob...@>=
  5939. @!param_stack:array [0..param_size] of pointer;
  5940. {token list pointers for parameters}
  5941. @!param_ptr:0..param_size; {first unused entry in |param_stack|}
  5942. @!max_param_stack:integer;
  5943. {largest value of |param_ptr|, will be |<=param_size+9|}
  5944. @ The input routines must also interact with the processing of
  5945. \.{\\halign} and \.{\\valign}, since the appearance of tab marks and
  5946. \.{\\cr} in certain places is supposed to trigger the beginning of special
  5947. \<v_j> template text in the scanner. This magic is accomplished by an
  5948. |align_state| variable that is increased by~1 when a `\.{\char'173}' is
  5949. scanned and decreased by~1 when a `\.{\char'175}' is scanned. The |align_state|
  5950. is nonzero during the \<u_j> template, after which it is set to zero; the
  5951. \<v_j> template begins when a tab mark or \.{\\cr} occurs at a time that
  5952. |align_state=0|.
  5953. @<Glob...@>=
  5954. @!align_state:integer; {group level with respect to current alignment}
  5955. @ Thus, the ``current input state'' can be very complicated indeed; there
  5956. can be many levels and each level can arise in a variety of ways. The
  5957. |show_context| procedure, which is used by \TeX's error-reporting routine to
  5958. print out the current input state on all levels down to the most recent
  5959. line of characters from an input file, illustrates most of these conventions.
  5960. The global variable |base_ptr| contains the lowest level that was
  5961. displayed by this procedure.
  5962. @<Glob...@>=
  5963. @!base_ptr:0..stack_size; {shallowest level shown by |show_context|}
  5964. @ The status at each level is indicated by printing two lines, where the first
  5965. line indicates what was read so far and the second line shows what remains
  5966. to be read. The context is cropped, if necessary, so that the first line
  5967. contains at most |half_error_line| characters, and the second contains
  5968. at most |error_line|. Non-current input levels whose |token_type| is
  5969. `|backed_up|' are shown only if they have not been fully read.
  5970. @p procedure show_context; {prints where the scanner is}
  5971. label done;
  5972. var old_setting:0..max_selector; {saved |selector| setting}
  5973. @!nn:integer; {number of contexts shown so far, less one}
  5974. @!bottom_line:boolean; {have we reached the final context to be shown?}
  5975. @<Local variables for formatting calculations@>@/
  5976. begin base_ptr:=input_ptr; input_stack[base_ptr]:=cur_input;
  5977. {store current state}
  5978. nn:=-1; bottom_line:=false;
  5979. loop@+begin cur_input:=input_stack[base_ptr]; {enter into the context}
  5980. if (state<>token_list) then
  5981. if (name>17) or (base_ptr=0) then bottom_line:=true;
  5982. if (base_ptr=input_ptr)or bottom_line or(nn<error_context_lines) then
  5983. @<Display the current context@>
  5984. else if nn=error_context_lines then
  5985. begin print_nl("..."); incr(nn); {omitted if |error_context_lines<0|}
  5986. end;
  5987. if bottom_line then goto done;
  5988. decr(base_ptr);
  5989. end;
  5990. done: cur_input:=input_stack[input_ptr]; {restore original state}
  5991. end;
  5992. @ @<Display the current context@>=
  5993. begin if (base_ptr=input_ptr) or (state<>token_list) or
  5994. (token_type<>backed_up) or (loc<>null) then
  5995. {we omit backed-up token lists that have already been read}
  5996. begin tally:=0; {get ready to count characters}
  5997. old_setting:=selector;
  5998. if state<>token_list then
  5999. begin @<Print location of current line@>;
  6000. @<Pseudoprint the line@>;
  6001. end
  6002. else begin @<Print type of token list@>;
  6003. @<Pseudoprint the token list@>;
  6004. end;
  6005. selector:=old_setting; {stop pseudoprinting}
  6006. @<Print two lines using the tricky pseudoprinted information@>;
  6007. incr(nn);
  6008. end;
  6009. end
  6010. @ This routine should be changed, if necessary, to give the best possible
  6011. indication of where the current line resides in the input file.
  6012. For example, on some systems it is best to print both a page and line number.
  6013. @^system dependencies@>
  6014. @<Print location of current line@>=
  6015. if name<=17 then
  6016. if terminal_input then
  6017. if base_ptr=0 then print_nl("<*>") else print_nl("<insert> ")
  6018. else begin print_nl("<read ");
  6019. if name=17 then print_char("*")@+else print_int(name-1);
  6020. @.*\relax@>
  6021. print_char(">");
  6022. end
  6023. else begin print_nl("l."); print_int(line);
  6024. end;
  6025. print_char(" ")
  6026. @ @<Print type of token list@>=
  6027. case token_type of
  6028. parameter: print_nl("<argument> ");
  6029. u_template,v_template: print_nl("<template> ");
  6030. backed_up: if loc=null then print_nl("<recently read> ")
  6031. else print_nl("<to be read again> ");
  6032. inserted: print_nl("<inserted text> ");
  6033. macro: begin print_ln; print_cs(name);
  6034. end;
  6035. output_text: print_nl("<output> ");
  6036. every_par_text: print_nl("<everypar> ");
  6037. every_math_text: print_nl("<everymath> ");
  6038. every_display_text: print_nl("<everydisplay> ");
  6039. every_hbox_text: print_nl("<everyhbox> ");
  6040. every_vbox_text: print_nl("<everyvbox> ");
  6041. every_job_text: print_nl("<everyjob> ");
  6042. every_cr_text: print_nl("<everycr> ");
  6043. mark_text: print_nl("<mark> ");
  6044. write_text: print_nl("<write> ");
  6045. othercases print_nl("?") {this should never happen}
  6046. endcases
  6047. @ Here it is necessary to explain a little trick. We don't want to store a long
  6048. string that corresponds to a token list, because that string might take up
  6049. lots of memory; and we are printing during a time when an error message is
  6050. being given, so we dare not do anything that might overflow one of \TeX's
  6051. tables. So `pseudoprinting' is the answer: We enter a mode of printing
  6052. that stores characters into a buffer of length |error_line|, where character
  6053. $k+1$ is placed into \hbox{|trick_buf[k mod error_line]|} if
  6054. |k<trick_count|, otherwise character |k| is dropped. Initially we set
  6055. |tally:=0| and |trick_count:=1000000|; then when we reach the
  6056. point where transition from line 1 to line 2 should occur, we
  6057. set |first_count:=tally| and |trick_count:=@tmax@>(error_line,
  6058. tally+1+error_line-half_error_line)|. At the end of the
  6059. pseudoprinting, the values of |first_count|, |tally|, and
  6060. |trick_count| give us all the information we need to print the two lines,
  6061. and all of the necessary text is in |trick_buf|.
  6062. Namely, let |l| be the length of the descriptive information that appears
  6063. on the first line. The length of the context information gathered for that
  6064. line is |k=first_count|, and the length of the context information
  6065. gathered for line~2 is $m=\min(|tally|, |trick_count|)-k$. If |l+k<=h|,
  6066. where |h=half_error_line|, we print |trick_buf[0..k-1]| after the
  6067. descriptive information on line~1, and set |n:=l+k|; here |n| is the
  6068. length of line~1. If $l+k>h$, some cropping is necessary, so we set |n:=h|
  6069. and print `\.{...}' followed by
  6070. $$\hbox{|trick_buf[(l+k-h+3)..k-1]|,}$$
  6071. where subscripts of |trick_buf| are circular modulo |error_line|. The
  6072. second line consists of |n|~spaces followed by |trick_buf[k..(k+m-1)]|,
  6073. unless |n+m>error_line|; in the latter case, further cropping is done.
  6074. This is easier to program than to explain.
  6075. @<Local variables for formatting...@>=
  6076. @!i:0..buf_size; {index into |buffer|}
  6077. @!j:0..buf_size; {end of current line in |buffer|}
  6078. @!l:0..half_error_line; {length of descriptive information on line 1}
  6079. @!m:integer; {context information gathered for line 2}
  6080. @!n:0..error_line; {length of line 1}
  6081. @!p: integer; {starting or ending place in |trick_buf|}
  6082. @!q: integer; {temporary index}
  6083. @ The following code sets up the print routines so that they will gather
  6084. the desired information.
  6085. @d begin_pseudoprint==
  6086. begin l:=tally; tally:=0; selector:=pseudo;
  6087. trick_count:=1000000;
  6088. end
  6089. @d set_trick_count==
  6090. begin first_count:=tally;
  6091. trick_count:=tally+1+error_line-half_error_line;
  6092. if trick_count<error_line then trick_count:=error_line;
  6093. end
  6094. @ And the following code uses the information after it has been gathered.
  6095. @<Print two lines using the tricky pseudoprinted information@>=
  6096. if trick_count=1000000 then set_trick_count;
  6097. {|set_trick_count| must be performed}
  6098. if tally<trick_count then m:=tally-first_count
  6099. else m:=trick_count-first_count; {context on line 2}
  6100. if l+first_count<=half_error_line then
  6101. begin p:=0; n:=l+first_count;
  6102. end
  6103. else begin print("..."); p:=l+first_count-half_error_line+3;
  6104. n:=half_error_line;
  6105. end;
  6106. for q:=p to first_count-1 do print_char(trick_buf[q mod error_line]);
  6107. print_ln;
  6108. for q:=1 to n do print_char(" "); {print |n| spaces to begin line~2}
  6109. if m+n<=error_line then p:=first_count+m else p:=first_count+(error_line-n-3);
  6110. for q:=first_count to p-1 do print_char(trick_buf[q mod error_line]);
  6111. if m+n>error_line then print("...")
  6112. @ But the trick is distracting us from our current goal, which is to
  6113. understand the input state. So let's concentrate on the data structures that
  6114. are being pseudoprinted as we finish up the |show_context| procedure.
  6115. @<Pseudoprint the line@>=
  6116. begin_pseudoprint;
  6117. if buffer[limit]=end_line_char then j:=limit
  6118. else j:=limit+1; {determine the effective end of the line}
  6119. if j>0 then for i:=start to j-1 do
  6120. begin if i=loc then set_trick_count;
  6121. print(buffer[i]);
  6122. end
  6123. @ @<Pseudoprint the token list@>=
  6124. begin_pseudoprint;
  6125. if token_type<macro then show_token_list(start,loc,100000)
  6126. else show_token_list(link(start),loc,100000) {avoid reference count}
  6127. @ Here is the missing piece of |show_token_list| that is activated when the
  6128. token beginning line~2 is about to be shown:
  6129. @<Do magic computation@>=set_trick_count
  6130. @* \[23] Maintaining the input stacks.
  6131. The following subroutines change the input status in commonly needed ways.
  6132. First comes |push_input|, which stores the current state and creates a
  6133. new level (having, initially, the same properties as the old).
  6134. @d push_input==@t@> {enter a new input level, save the old}
  6135. begin if input_ptr>max_in_stack then
  6136. begin max_in_stack:=input_ptr;
  6137. if input_ptr=stack_size then overflow("input stack size",stack_size);
  6138. @:TeX capacity exceeded input stack size}{\quad input stack size@>
  6139. end;
  6140. input_stack[input_ptr]:=cur_input; {stack the record}
  6141. incr(input_ptr);
  6142. end
  6143. @ And of course what goes up must come down.
  6144. @d pop_input==@t@> {leave an input level, re-enter the old}
  6145. begin decr(input_ptr); cur_input:=input_stack[input_ptr];
  6146. end
  6147. @ Here is a procedure that starts a new level of token-list input, given
  6148. a token list |p| and its type |t|. If |t=macro|, the calling routine should
  6149. set |name| and |loc|.
  6150. @d back_list(#)==begin_token_list(#,backed_up) {backs up a simple token list}
  6151. @d ins_list(#)==begin_token_list(#,inserted) {inserts a simple token list}
  6152. @p procedure begin_token_list(@!p:pointer;@!t:quarterword);
  6153. begin push_input; state:=token_list; start:=p; token_type:=t;
  6154. if t>=macro then {the token list starts with a reference count}
  6155. begin add_token_ref(p);
  6156. if t=macro then param_start:=param_ptr
  6157. else begin loc:=link(p);
  6158. if tracing_macros>1 then
  6159. begin begin_diagnostic; print_nl("");
  6160. case t of
  6161. mark_text:print_esc("mark");
  6162. write_text:print_esc("write");
  6163. othercases print_cmd_chr(assign_toks,t-output_text+output_routine_loc)
  6164. endcases;@/
  6165. print("->"); token_show(p); end_diagnostic(false);
  6166. end;
  6167. end;
  6168. end
  6169. else loc:=p;
  6170. end;
  6171. @ When a token list has been fully scanned, the following computations
  6172. should be done as we leave that level of input. The |token_type| tends
  6173. to be equal to either |backed_up| or |inserted| about 2/3 of the time.
  6174. @^inner loop@>
  6175. @p procedure end_token_list; {leave a token-list input level}
  6176. begin if token_type>=backed_up then {token list to be deleted}
  6177. begin if token_type<=inserted then flush_list(start)
  6178. else begin delete_token_ref(start); {update reference count}
  6179. if token_type=macro then {parameters must be flushed}
  6180. while param_ptr>param_start do
  6181. begin decr(param_ptr);
  6182. flush_list(param_stack[param_ptr]);
  6183. end;
  6184. end;
  6185. end
  6186. else if token_type=u_template then
  6187. if align_state>500000 then align_state:=0
  6188. else fatal_error("(interwoven alignment preambles are not allowed)");
  6189. @.interwoven alignment preambles...@>
  6190. pop_input;
  6191. check_interrupt;
  6192. end;
  6193. @ Sometimes \TeX\ has read too far and wants to ``unscan'' what it has
  6194. seen. The |back_input| procedure takes care of this by putting the token
  6195. just scanned back into the input stream, ready to be read again. This
  6196. procedure can be used only if |cur_tok| represents the token to be
  6197. replaced. Some applications of \TeX\ use this procedure a lot,
  6198. so it has been slightly optimized for speed.
  6199. @^inner loop@>
  6200. @p procedure back_input; {undoes one token of input}
  6201. var p:pointer; {a token list of length one}
  6202. begin while (state=token_list)and(loc=null)and(token_type<>v_template) do
  6203. end_token_list; {conserve stack space}
  6204. p:=get_avail; info(p):=cur_tok;
  6205. if cur_tok<right_brace_limit then
  6206. if cur_tok<left_brace_limit then decr(align_state)
  6207. else incr(align_state);
  6208. push_input; state:=token_list; start:=p; token_type:=backed_up;
  6209. loc:=p; {that was |back_list(p)|, without procedure overhead}
  6210. end;
  6211. @ @<Insert token |p| into \TeX's input@>=
  6212. begin t:=cur_tok; cur_tok:=p; back_input; cur_tok:=t;
  6213. end
  6214. @ The |back_error| routine is used when we want to replace an offending token
  6215. just before issuing an error message. This routine, like |back_input|,
  6216. requires that |cur_tok| has been set. We disable interrupts during the
  6217. call of |back_input| so that the help message won't be lost.
  6218. @p procedure back_error; {back up one token and call |error|}
  6219. begin OK_to_interrupt:=false; back_input; OK_to_interrupt:=true; error;
  6220. end;
  6221. @#
  6222. procedure ins_error; {back up one inserted token and call |error|}
  6223. begin OK_to_interrupt:=false; back_input; token_type:=inserted;
  6224. OK_to_interrupt:=true; error;
  6225. end;
  6226. @ The |begin_file_reading| procedure starts a new level of input for lines
  6227. of characters to be read from a file, or as an insertion from the
  6228. terminal. It does not take care of opening the file, nor does it set |loc|
  6229. or |limit| or |line|.
  6230. @^system dependencies@>
  6231. @p procedure begin_file_reading;
  6232. begin if in_open=max_in_open then overflow("text input levels",max_in_open);
  6233. @:TeX capacity exceeded text input levels}{\quad text input levels@>
  6234. if first=buf_size then overflow("buffer size",buf_size);
  6235. @:TeX capacity exceeded buffer size}{\quad buffer size@>
  6236. incr(in_open); push_input; index:=in_open;
  6237. line_stack[index]:=line; start:=first; state:=mid_line;
  6238. name:=0; {|terminal_input| is now |true|}
  6239. end;
  6240. @ Conversely, the variables must be downdated when such a level of input
  6241. is finished:
  6242. @p procedure end_file_reading;
  6243. begin first:=start; line:=line_stack[index];
  6244. if name>17 then a_close(cur_file); {forget it}
  6245. pop_input; decr(in_open);
  6246. end;
  6247. @ In order to keep the stack from overflowing during a long sequence of
  6248. inserted `\.{\\show}' commands, the following routine removes completed
  6249. error-inserted lines from memory.
  6250. @p procedure clear_for_error_prompt;
  6251. begin while (state<>token_list)and terminal_input and@|
  6252. (input_ptr>0)and(loc>limit) do end_file_reading;
  6253. print_ln; clear_terminal;
  6254. end;
  6255. @ To get \TeX's whole input mechanism going, we perform the following
  6256. actions.
  6257. @<Initialize the input routines@>=
  6258. begin input_ptr:=0; max_in_stack:=0;
  6259. in_open:=0; open_parens:=0; max_buf_stack:=0;
  6260. param_ptr:=0; max_param_stack:=0;
  6261. first:=buf_size; repeat buffer[first]:=0; decr(first); until first=0;
  6262. scanner_status:=normal; warning_index:=null; first:=1;
  6263. state:=new_line; start:=1; index:=0; line:=0; name:=0;
  6264. force_eof:=false;
  6265. align_state:=1000000;@/
  6266. if not init_terminal then goto final_end;
  6267. limit:=last; first:=last+1; {|init_terminal| has set |loc| and |last|}
  6268. end
  6269. @* \[24] Getting the next token.
  6270. The heart of \TeX's input mechanism is the |get_next| procedure, which
  6271. we shall develop in the next few sections of the program. Perhaps we
  6272. shouldn't actually call it the ``heart,'' however, because it really acts
  6273. as \TeX's eyes and mouth, reading the source files and gobbling them up.
  6274. And it also helps \TeX\ to regurgitate stored token lists that are to be
  6275. processed again.
  6276. @^eyes and mouth@>
  6277. The main duty of |get_next| is to input one token and to set |cur_cmd|
  6278. and |cur_chr| to that token's command code and modifier. Furthermore, if
  6279. the input token is a control sequence, the |eqtb| location of that control
  6280. sequence is stored in |cur_cs|; otherwise |cur_cs| is set to zero.
  6281. Underlying this simple description is a certain amount of complexity
  6282. because of all the cases that need to be handled.
  6283. However, the inner loop of |get_next| is reasonably short and fast.
  6284. When |get_next| is asked to get the next token of a \.{\\read} line,
  6285. it sets |cur_cmd=cur_chr=cur_cs=0| in the case that no more tokens
  6286. appear on that line. (There might not be any tokens at all, if the
  6287. |end_line_char| has |ignore| as its catcode.)
  6288. @ The value of |par_loc| is the |eqtb| address of `\.{\\par}'. This quantity
  6289. is needed because a blank line of input is supposed to be exactly equivalent
  6290. to the appearance of \.{\\par}; we must set |cur_cs:=par_loc|
  6291. when detecting a blank line.
  6292. @<Glob...@>=
  6293. @!par_loc:pointer; {location of `\.{\\par}' in |eqtb|}
  6294. @!par_token:halfword; {token representing `\.{\\par}'}
  6295. @ @<Put each...@>=
  6296. primitive("par",par_end,256); {cf.\ |scan_file_name|}
  6297. @!@:par_}{\.{\\par} primitive@>
  6298. par_loc:=cur_val; par_token:=cs_token_flag+par_loc;
  6299. @ @<Cases of |print_cmd_chr|...@>=
  6300. par_end:print_esc("par");
  6301. @ Before getting into |get_next|, let's consider the subroutine that
  6302. is called when an `\.{\\outer}' control sequence has been scanned or
  6303. when the end of a file has been reached. These two cases are distinguished
  6304. by |cur_cs|, which is zero at the end of a file.
  6305. @p procedure check_outer_validity;
  6306. var p:pointer; {points to inserted token list}
  6307. @!q:pointer; {auxiliary pointer}
  6308. begin if scanner_status<>normal then
  6309. begin deletions_allowed:=false;
  6310. @<Back up an outer control sequence so that it can be reread@>;
  6311. if scanner_status>skipping then
  6312. @<Tell the user what has run away and try to recover@>
  6313. else begin print_err("Incomplete "); print_cmd_chr(if_test,cur_if);
  6314. @.Incomplete \\if...@>
  6315. print("; all text was ignored after line "); print_int(skip_line);
  6316. help3("A forbidden control sequence occurred in skipped text.")@/
  6317. ("This kind of error happens when you say `\if...' and forget")@/
  6318. ("the matching `\fi'. I've inserted a `\fi'; this might work.");
  6319. if cur_cs<>0 then cur_cs:=0
  6320. else help_line[2]:=@|
  6321. "The file ended while I was skipping conditional text.";
  6322. cur_tok:=cs_token_flag+frozen_fi; ins_error;
  6323. end;
  6324. deletions_allowed:=true;
  6325. end;
  6326. end;
  6327. @ An outer control sequence that occurs in a \.{\\read} will not be reread,
  6328. since the error recovery for \.{\\read} is not very powerful.
  6329. @<Back up an outer control sequence so that it can be reread@>=
  6330. if cur_cs<>0 then
  6331. begin if (state=token_list)or(name<1)or(name>17) then
  6332. begin p:=get_avail; info(p):=cs_token_flag+cur_cs;
  6333. back_list(p); {prepare to read the control sequence again}
  6334. end;
  6335. cur_cmd:=spacer; cur_chr:=" "; {replace it by a space}
  6336. end
  6337. @ @<Tell the user what has run away...@>=
  6338. begin runaway; {print a definition, argument, or preamble}
  6339. if cur_cs=0 then print_err("File ended")
  6340. @.File ended while scanning...@>
  6341. else begin cur_cs:=0; print_err("Forbidden control sequence found");
  6342. @.Forbidden control sequence...@>
  6343. end;
  6344. print(" while scanning ");
  6345. @<Print either `\.{definition}' or `\.{use}' or `\.{preamble}' or `\.{text}',
  6346. and insert tokens that should lead to recovery@>;
  6347. print(" of "); sprint_cs(warning_index);
  6348. help4("I suspect you have forgotten a `}', causing me")@/
  6349. ("to read past where you wanted me to stop.")@/
  6350. ("I'll try to recover; but if the error is serious,")@/
  6351. ("you'd better type `E' or `X' now and fix your file.");@/
  6352. error;
  6353. end
  6354. @ The recovery procedure can't be fully understood without knowing more
  6355. about the \TeX\ routines that should be aborted, but we can sketch the
  6356. ideas here: For a runaway definition or a runaway balanced text
  6357. we will insert a right brace; for a
  6358. runaway preamble, we will insert a special \.{\\cr} token and a right
  6359. brace; and for a runaway argument, we will set |long_state| to
  6360. |outer_call| and insert \.{\\par}.
  6361. @<Print either `\.{definition}' or ...@>=
  6362. p:=get_avail;
  6363. case scanner_status of
  6364. defining:begin print("definition"); info(p):=right_brace_token+"}";
  6365. end;
  6366. matching:begin print("use"); info(p):=par_token; long_state:=outer_call;
  6367. end;
  6368. aligning:begin print("preamble"); info(p):=right_brace_token+"}"; q:=p;
  6369. p:=get_avail; link(p):=q; info(p):=cs_token_flag+frozen_cr;
  6370. align_state:=-1000000;
  6371. end;
  6372. absorbing:begin print("text"); info(p):=right_brace_token+"}";
  6373. end;
  6374. end; {there are no other cases}
  6375. ins_list(p)
  6376. @ We need to mention a procedure here that may be called by |get_next|.
  6377. @p procedure@?firm_up_the_line; forward;
  6378. @ Now we're ready to take the plunge into |get_next| itself. Parts of
  6379. this routine are executed more often than any other instructions of \TeX.
  6380. @^mastication@>@^inner loop@>
  6381. @d switch=25 {a label in |get_next|}
  6382. @d start_cs=26 {another}
  6383. @p procedure get_next; {sets |cur_cmd|, |cur_chr|, |cur_cs| to next token}
  6384. label restart, {go here to get the next input token}
  6385. switch, {go here to eat the next character from a file}
  6386. reswitch, {go here to digest it again}
  6387. start_cs, {go here to start looking for a control sequence}
  6388. found, {go here when a control sequence has been found}
  6389. exit; {go here when the next input token has been got}
  6390. var k:0..buf_size; {an index into |buffer|}
  6391. @!t:halfword; {a token}
  6392. @!cat:0..max_char_code; {|cat_code(cur_chr)|, usually}
  6393. @!c,@!cc:ASCII_code; {constituents of a possible expanded code}
  6394. @!d:2..3; {number of excess characters in an expanded code}
  6395. begin restart: cur_cs:=0;
  6396. if state<>token_list then
  6397. @<Input from external file, |goto restart| if no input found@>
  6398. else @<Input from token list, |goto restart| if end of list or
  6399. if a parameter needs to be expanded@>;
  6400. @<If an alignment entry has just ended, take appropriate action@>;
  6401. exit:end;
  6402. @ An alignment entry ends when a tab or \.{\\cr} occurs, provided that the
  6403. current level of braces is the same as the level that was present at the
  6404. beginning of that alignment entry; i.e., provided that |align_state| has
  6405. returned to the value it had after the \<u_j> template for that entry.
  6406. @^inner loop@>
  6407. @<If an alignment entry has just ended, take appropriate action@>=
  6408. if cur_cmd<=car_ret then if cur_cmd>=tab_mark then if align_state=0 then
  6409. @<Insert the \(v)\<v_j> template and |goto restart|@>
  6410. @ @<Input from external file, |goto restart| if no input found@>=
  6411. @^inner loop@>
  6412. begin switch: if loc<=limit then {current line not yet finished}
  6413. begin cur_chr:=buffer[loc]; incr(loc);
  6414. reswitch: cur_cmd:=cat_code(cur_chr);
  6415. @<Change state if necessary, and |goto switch| if the
  6416. current character should be ignored,
  6417. or |goto reswitch| if the current character
  6418. changes to another@>;
  6419. end
  6420. else begin state:=new_line;@/
  6421. @<Move to next line of file,
  6422. or |goto restart| if there is no next line,
  6423. or |return| if a \.{\\read} line has finished@>;
  6424. check_interrupt;
  6425. goto switch;
  6426. end;
  6427. end
  6428. @ The following 48-way switch accomplishes the scanning quickly, assuming
  6429. that a decent \PASCAL\ compiler has translated the code. Note that the numeric
  6430. values for |mid_line|, |skip_blanks|, and |new_line| are spaced
  6431. apart from each other by |max_char_code+1|, so we can add a character's
  6432. command code to the state to get a single number that characterizes both.
  6433. @d any_state_plus(#) == mid_line+#,skip_blanks+#,new_line+#
  6434. @<Change state if necessary...@>=
  6435. case state+cur_cmd of
  6436. @<Cases where character is ignored@>: goto switch;
  6437. any_state_plus(escape): @<Scan a control sequence
  6438. and set |state:=skip_blanks| or |mid_line|@>;
  6439. any_state_plus(active_char): @<Process an active-character control sequence
  6440. and set |state:=mid_line|@>;
  6441. any_state_plus(sup_mark): @<If this |sup_mark| starts an expanded character
  6442. like~\.{\^\^A} or~\.{\^\^df}, then |goto reswitch|,
  6443. otherwise set |state:=mid_line|@>;
  6444. any_state_plus(invalid_char): @<Decry the invalid character and
  6445. |goto restart|@>;
  6446. @t\4@>@<Handle situations involving spaces, braces, changes of state@>@;
  6447. othercases do_nothing
  6448. endcases
  6449. @ @<Cases where character is ignored@>=
  6450. any_state_plus(ignore),skip_blanks+spacer,new_line+spacer
  6451. @ We go to |restart| instead of to |switch|, because |state| might equal
  6452. |token_list| after the error has been dealt with
  6453. (cf.\ |clear_for_error_prompt|).
  6454. @<Decry the invalid...@>=
  6455. begin print_err("Text line contains an invalid character");
  6456. @.Text line contains...@>
  6457. help2("A funny symbol that I can't read has just been input.")@/
  6458. ("Continue, and I'll forget that it ever happened.");@/
  6459. deletions_allowed:=false; error; deletions_allowed:=true;
  6460. goto restart;
  6461. end
  6462. @ @d add_delims_to(#)==#+math_shift,#+tab_mark,#+mac_param,
  6463. #+sub_mark,#+letter,#+other_char
  6464. @<Handle situations involving spaces, braces, changes of state@>=
  6465. mid_line+spacer:@<Enter |skip_blanks| state, emit a space@>;
  6466. mid_line+car_ret:@<Finish line, emit a space@>;
  6467. skip_blanks+car_ret,any_state_plus(comment):
  6468. @<Finish line, |goto switch|@>;
  6469. new_line+car_ret:@<Finish line, emit a \.{\\par}@>;
  6470. mid_line+left_brace: incr(align_state);
  6471. skip_blanks+left_brace,new_line+left_brace: begin
  6472. state:=mid_line; incr(align_state);
  6473. end;
  6474. mid_line+right_brace: decr(align_state);
  6475. skip_blanks+right_brace,new_line+right_brace: begin
  6476. state:=mid_line; decr(align_state);
  6477. end;
  6478. add_delims_to(skip_blanks),add_delims_to(new_line): state:=mid_line;
  6479. @ When a character of type |spacer| gets through, its character code is
  6480. changed to $\.{"\ "}=@'40$. This means that the ASCII codes for tab and space,
  6481. and for the space inserted at the end of a line, will
  6482. be treated alike when macro parameters are being matched. We do this
  6483. since such characters are indistinguishable on most computer terminal displays.
  6484. @<Finish line, emit a space@>=
  6485. begin loc:=limit+1; cur_cmd:=spacer; cur_chr:=" ";
  6486. end
  6487. @ The following code is performed only when |cur_cmd=spacer|.
  6488. @<Enter |skip_blanks| state, emit a space@>=
  6489. begin state:=skip_blanks; cur_chr:=" ";
  6490. end
  6491. @ @<Finish line, |goto switch|@>=
  6492. begin loc:=limit+1; goto switch;
  6493. end
  6494. @ @<Finish line, emit a \.{\\par}@>=
  6495. begin loc:=limit+1; cur_cs:=par_loc; cur_cmd:=eq_type(cur_cs);
  6496. cur_chr:=equiv(cur_cs);
  6497. if cur_cmd>=outer_call then check_outer_validity;
  6498. end
  6499. @ Notice that a code like \.{\^\^8} becomes \.x if not followed by a hex digit.
  6500. @d is_hex(#)==(((#>="0")and(#<="9"))or((#>="a")and(#<="f")))
  6501. @d hex_to_cur_chr==
  6502. if c<="9" then cur_chr:=c-"0" @+else cur_chr:=c-"a"+10;
  6503. if cc<="9" then cur_chr:=16*cur_chr+cc-"0"
  6504. else cur_chr:=16*cur_chr+cc-"a"+10
  6505. @<If this |sup_mark| starts an expanded character...@>=
  6506. begin if cur_chr=buffer[loc] then if loc<limit then
  6507. begin c:=buffer[loc+1]; @+if c<@'200 then {yes we have an expanded char}
  6508. begin loc:=loc+2;
  6509. if is_hex(c) then if loc<=limit then
  6510. begin cc:=buffer[loc]; @+if is_hex(cc) then
  6511. begin incr(loc); hex_to_cur_chr; goto reswitch;
  6512. end;
  6513. end;
  6514. if c<@'100 then cur_chr:=c+@'100 @+else cur_chr:=c-@'100;
  6515. goto reswitch;
  6516. end;
  6517. end;
  6518. state:=mid_line;
  6519. end
  6520. @ @<Process an active-character...@>=
  6521. begin cur_cs:=cur_chr+active_base;
  6522. cur_cmd:=eq_type(cur_cs); cur_chr:=equiv(cur_cs); state:=mid_line;
  6523. if cur_cmd>=outer_call then check_outer_validity;
  6524. end
  6525. @ Control sequence names are scanned only when they appear in some line of
  6526. a file; once they have been scanned the first time, their |eqtb| location
  6527. serves as a unique identification, so \TeX\ doesn't need to refer to the
  6528. original name any more except when it prints the equivalent in symbolic form.
  6529. The program that scans a control sequence has been written carefully
  6530. in order to avoid the blowups that might otherwise occur if a malicious
  6531. user tried something like `\.{\\catcode\'15=0}'. The algorithm might
  6532. look at |buffer[limit+1]|, but it never looks at |buffer[limit+2]|.
  6533. If expanded characters like `\.{\^\^A}' or `\.{\^\^df}'
  6534. appear in or just following
  6535. a control sequence name, they are converted to single characters in the
  6536. buffer and the process is repeated, slowly but surely.
  6537. @<Scan a control...@>=
  6538. begin if loc>limit then cur_cs:=null_cs {|state| is irrelevant in this case}
  6539. else begin start_cs: k:=loc; cur_chr:=buffer[k]; cat:=cat_code(cur_chr);
  6540. incr(k);
  6541. if cat=letter then state:=skip_blanks
  6542. else if cat=spacer then state:=skip_blanks
  6543. else state:=mid_line;
  6544. if (cat=letter)and(k<=limit) then
  6545. @<Scan ahead in the buffer until finding a nonletter;
  6546. if an expanded code is encountered, reduce it
  6547. and |goto start_cs|; otherwise if a multiletter control
  6548. sequence is found, adjust |cur_cs| and |loc|, and
  6549. |goto found|@>
  6550. else @<If an expanded code is present, reduce it and |goto start_cs|@>;
  6551. cur_cs:=single_base+buffer[loc]; incr(loc);
  6552. end;
  6553. found: cur_cmd:=eq_type(cur_cs); cur_chr:=equiv(cur_cs);
  6554. if cur_cmd>=outer_call then check_outer_validity;
  6555. end
  6556. @ Whenever we reach the following piece of code, we will have
  6557. |cur_chr=buffer[k-1]| and |k<=limit+1| and |cat=cat_code(cur_chr)|. If an
  6558. expanded code like \.{\^\^A} or \.{\^\^df} appears in |buffer[(k-1)..(k+1)]|
  6559. or |buffer[(k-1)..(k+2)]|, we
  6560. will store the corresponding code in |buffer[k-1]| and shift the rest of
  6561. the buffer left two or three places.
  6562. @<If an expanded...@>=
  6563. begin if buffer[k]=cur_chr then @+if cat=sup_mark then @+if k<limit then
  6564. begin c:=buffer[k+1]; @+if c<@'200 then {yes, one is indeed present}
  6565. begin d:=2;
  6566. if is_hex(c) then @+if k+2<=limit then
  6567. begin cc:=buffer[k+2]; @+if is_hex(cc) then incr(d);
  6568. end;
  6569. if d>2 then
  6570. begin hex_to_cur_chr; buffer[k-1]:=cur_chr;
  6571. end
  6572. else if c<@'100 then buffer[k-1]:=c+@'100
  6573. else buffer[k-1]:=c-@'100;
  6574. limit:=limit-d; first:=first-d;
  6575. while k<=limit do
  6576. begin buffer[k]:=buffer[k+d]; incr(k);
  6577. end;
  6578. goto start_cs;
  6579. end;
  6580. end;
  6581. end
  6582. @ @<Scan ahead in the buffer...@>=
  6583. begin repeat cur_chr:=buffer[k]; cat:=cat_code(cur_chr); incr(k);
  6584. until (cat<>letter)or(k>limit);
  6585. @<If an expanded...@>;
  6586. if cat<>letter then decr(k);
  6587. {now |k| points to first nonletter}
  6588. if k>loc+1 then {multiletter control sequence has been scanned}
  6589. begin cur_cs:=id_lookup(loc,k-loc); loc:=k; goto found;
  6590. end;
  6591. end
  6592. @ Let's consider now what happens when |get_next| is looking at a token list.
  6593. @<Input from token list, |goto restart| if end of list or
  6594. if a parameter needs to be expanded@>=
  6595. if loc<>null then {list not exhausted}
  6596. @^inner loop@>
  6597. begin t:=info(loc); loc:=link(loc); {move to next}
  6598. if t>=cs_token_flag then {a control sequence token}
  6599. begin cur_cs:=t-cs_token_flag;
  6600. cur_cmd:=eq_type(cur_cs); cur_chr:=equiv(cur_cs);
  6601. if cur_cmd>=outer_call then
  6602. if cur_cmd=dont_expand then
  6603. @<Get the next token, suppressing expansion@>
  6604. else check_outer_validity;
  6605. end
  6606. else begin cur_cmd:=t div @'400; cur_chr:=t mod @'400;
  6607. case cur_cmd of
  6608. left_brace: incr(align_state);
  6609. right_brace: decr(align_state);
  6610. out_param: @<Insert macro parameter and |goto restart|@>;
  6611. othercases do_nothing
  6612. endcases;
  6613. end;
  6614. end
  6615. else begin {we are done with this token list}
  6616. end_token_list; goto restart; {resume previous level}
  6617. end
  6618. @ The present point in the program is reached only when the |expand|
  6619. routine has inserted a special marker into the input. In this special
  6620. case, |info(loc)| is known to be a control sequence token, and |link(loc)=null|.
  6621. @d no_expand_flag=257 {this characterizes a special variant of |relax|}
  6622. @<Get the next token, suppressing expansion@>=
  6623. begin cur_cs:=info(loc)-cs_token_flag; loc:=null;@/
  6624. cur_cmd:=eq_type(cur_cs); cur_chr:=equiv(cur_cs);
  6625. if cur_cmd>max_command then
  6626. begin cur_cmd:=relax; cur_chr:=no_expand_flag;
  6627. end;
  6628. end
  6629. @ @<Insert macro parameter...@>=
  6630. begin begin_token_list(param_stack[param_start+cur_chr-1],parameter);
  6631. goto restart;
  6632. end
  6633. @ All of the easy branches of |get_next| have now been taken care of.
  6634. There is one more branch.
  6635. @d end_line_char_inactive == (end_line_char<0)or(end_line_char>255)
  6636. @<Move to next line of file, or |goto restart|...@>=
  6637. if name>17 then @<Read next line of file into |buffer|, or
  6638. |goto restart| if the file has ended@>
  6639. else begin if not terminal_input then {\.{\\read} line has ended}
  6640. begin cur_cmd:=0; cur_chr:=0; return;
  6641. end;
  6642. if input_ptr>0 then {text was inserted during error recovery}
  6643. begin end_file_reading; goto restart; {resume previous level}
  6644. end;
  6645. if selector<log_only then open_log_file;
  6646. if interaction>nonstop_mode then
  6647. begin if end_line_char_inactive then incr(limit);
  6648. if limit=start then {previous line was empty}
  6649. print_nl("(Please type a command or say `\end')");
  6650. @.Please type...@>
  6651. print_ln; first:=start;
  6652. prompt_input("*"); {input on-line into |buffer|}
  6653. @.*\relax@>
  6654. limit:=last;
  6655. if end_line_char_inactive then decr(limit)
  6656. else buffer[limit]:=end_line_char;
  6657. first:=limit+1;
  6658. loc:=start;
  6659. end
  6660. else fatal_error("*** (job aborted, no legal \end found)");
  6661. @.job aborted@>
  6662. {nonstop mode, which is intended for overnight batch processing,
  6663. never waits for on-line input}
  6664. end
  6665. @ The global variable |force_eof| is normally |false|; it is set |true|
  6666. by an \.{\\endinput} command.
  6667. @<Glob...@>=
  6668. @!force_eof:boolean; {should the next \.{\\input} be aborted early?}
  6669. @ @<Read next line of file into |buffer|, or
  6670. |goto restart| if the file has ended@>=
  6671. begin incr(line); first:=start;
  6672. if not force_eof then
  6673. begin if input_ln(cur_file,true) then {not end of file}
  6674. firm_up_the_line {this sets |limit|}
  6675. else force_eof:=true;
  6676. end;
  6677. if force_eof then
  6678. begin print_char(")"); decr(open_parens);
  6679. update_terminal; {show user that file has been read}
  6680. force_eof:=false;
  6681. end_file_reading; {resume previous level}
  6682. check_outer_validity; goto restart;
  6683. end;
  6684. if end_line_char_inactive then decr(limit)
  6685. else buffer[limit]:=end_line_char;
  6686. first:=limit+1; loc:=start; {ready to read}
  6687. end
  6688. @ If the user has set the |pausing| parameter to some positive value,
  6689. and if nonstop mode has not been selected, each line of input is displayed
  6690. on the terminal and the transcript file, followed by `\.{=>}'.
  6691. \TeX\ waits for a response. If the response is simply |carriage_return|, the
  6692. line is accepted as it stands, otherwise the line typed is
  6693. used instead of the line in the file.
  6694. @p procedure firm_up_the_line;
  6695. var k:0..buf_size; {an index into |buffer|}
  6696. begin limit:=last;
  6697. if pausing>0 then if interaction>nonstop_mode then
  6698. begin wake_up_terminal; print_ln;
  6699. if start<limit then for k:=start to limit-1 do print(buffer[k]);
  6700. first:=limit; prompt_input("=>"); {wait for user response}
  6701. @.=>@>
  6702. if last>first then
  6703. begin for k:=first to last-1 do {move line down in buffer}
  6704. buffer[k+start-first]:=buffer[k];
  6705. limit:=start+last-first;
  6706. end;
  6707. end;
  6708. end;
  6709. @ Since |get_next| is used so frequently in \TeX, it is convenient
  6710. to define three related procedures that do a little more:
  6711. \yskip\hang|get_token| not only sets |cur_cmd| and |cur_chr|, it
  6712. also sets |cur_tok|, a packed halfword version of the current token.
  6713. \yskip\hang|get_x_token|, meaning ``get an expanded token,'' is like
  6714. |get_token|, but if the current token turns out to be a user-defined
  6715. control sequence (i.e., a macro call), or a conditional,
  6716. or something like \.{\\topmark} or \.{\\expandafter} or \.{\\csname},
  6717. it is eliminated from the input by beginning the expansion of the macro
  6718. or the evaluation of the conditional.
  6719. \yskip\hang|x_token| is like |get_x_token| except that it assumes that
  6720. |get_next| has already been called.
  6721. \yskip\noindent
  6722. In fact, these three procedures account for almost every use of |get_next|.
  6723. @ No new control sequences will be defined except during a call of
  6724. |get_token|, or when \.{\\csname} compresses a token list, because
  6725. |no_new_control_sequence| is always |true| at other times.
  6726. @p procedure get_token; {sets |cur_cmd|, |cur_chr|, |cur_tok|}
  6727. begin no_new_control_sequence:=false; get_next; no_new_control_sequence:=true;
  6728. @^inner loop@>
  6729. if cur_cs=0 then cur_tok:=(cur_cmd*@'400)+cur_chr
  6730. else cur_tok:=cs_token_flag+cur_cs;
  6731. end;
  6732. @* \[25] Expanding the next token.
  6733. Only a dozen or so command codes |>max_command| can possibly be returned by
  6734. |get_next|; in increasing order, they are |undefined_cs|, |expand_after|,
  6735. |no_expand|, |input|, |if_test|, |fi_or_else|, |cs_name|, |convert|, |the|,
  6736. |top_bot_mark|, |call|, |long_call|, |outer_call|, |long_outer_call|, and
  6737. |end_template|.{\emergencystretch=40pt\par}
  6738. The |expand| subroutine is used when |cur_cmd>max_command|. It removes a
  6739. ``call'' or a conditional or one of the other special operations just
  6740. listed. It follows that |expand| might invoke itself recursively. In all
  6741. cases, |expand| destroys the current token, but it sets things up so that
  6742. the next |get_next| will deliver the appropriate next token. The value of
  6743. |cur_tok| need not be known when |expand| is called.
  6744. Since several of the basic scanning routines communicate via global variables,
  6745. their values are saved as local variables of |expand| so that
  6746. recursive calls don't invalidate them.
  6747. @^recursion@>
  6748. @p@t\4@>@<Declare the procedure called |macro_call|@>@;@/
  6749. @t\4@>@<Declare the procedure called |insert_relax|@>@;@/
  6750. procedure@?pass_text; forward;@t\2@>
  6751. procedure@?start_input; forward;@t\2@>
  6752. procedure@?conditional; forward;@t\2@>
  6753. procedure@?get_x_token; forward;@t\2@>
  6754. procedure@?conv_toks; forward;@t\2@>
  6755. procedure@?ins_the_toks; forward;@t\2@>
  6756. procedure expand;
  6757. var t:halfword; {token that is being ``expanded after''}
  6758. @!p,@!q,@!r:pointer; {for list manipulation}
  6759. @!j:0..buf_size; {index into |buffer|}
  6760. @!cv_backup:integer; {to save the global quantity |cur_val|}
  6761. @!cvl_backup,@!radix_backup,@!co_backup:small_number;
  6762. {to save |cur_val_level|, etc.}
  6763. @!backup_backup:pointer; {to save |link(backup_head)|}
  6764. @!save_scanner_status:small_number; {temporary storage of |scanner_status|}
  6765. begin cv_backup:=cur_val; cvl_backup:=cur_val_level; radix_backup:=radix;
  6766. co_backup:=cur_order; backup_backup:=link(backup_head);
  6767. if cur_cmd<call then @<Expand a nonmacro@>
  6768. else if cur_cmd<end_template then macro_call
  6769. else @<Insert a token containing |frozen_endv|@>;
  6770. cur_val:=cv_backup; cur_val_level:=cvl_backup; radix:=radix_backup;
  6771. cur_order:=co_backup; link(backup_head):=backup_backup;
  6772. end;
  6773. @ @<Expand a nonmacro@>=
  6774. begin if tracing_commands>1 then show_cur_cmd_chr;
  6775. case cur_cmd of
  6776. top_bot_mark:@<Insert the \(a)appropriate mark text into the scanner@>;
  6777. expand_after:@<Expand the token after the next token@>;
  6778. no_expand:@<Suppress expansion of the next token@>;
  6779. cs_name:@<Manufacture a control sequence name@>;
  6780. convert:conv_toks; {this procedure is discussed in Part 27 below}
  6781. the:ins_the_toks; {this procedure is discussed in Part 27 below}
  6782. if_test:conditional; {this procedure is discussed in Part 28 below}
  6783. fi_or_else:@<Terminate the current conditional and skip to \.{\\fi}@>;
  6784. input:@<Initiate or terminate input from a file@>;
  6785. othercases @<Complain about an undefined macro@>
  6786. endcases;
  6787. end
  6788. @ It takes only a little shuffling to do what \TeX\ calls \.{\\expandafter}.
  6789. @<Expand the token after...@>=
  6790. begin get_token; t:=cur_tok; get_token;
  6791. if cur_cmd>max_command then expand@+else back_input;
  6792. cur_tok:=t; back_input;
  6793. end
  6794. @ The implementation of \.{\\noexpand} is a bit trickier, because it is
  6795. necessary to insert a special `|dont_expand|' marker into \TeX's reading
  6796. mechanism. This special marker is processed by |get_next|, but it does
  6797. not slow down the inner loop.
  6798. Since \.{\\outer} macros might arise here, we must also
  6799. clear the |scanner_status| temporarily.
  6800. @<Suppress expansion...@>=
  6801. begin save_scanner_status:=scanner_status; scanner_status:=normal;
  6802. get_token; scanner_status:=save_scanner_status; t:=cur_tok;
  6803. back_input; {now |start| and |loc| point to the backed-up token |t|}
  6804. if t>=cs_token_flag then
  6805. begin p:=get_avail; info(p):=cs_token_flag+frozen_dont_expand;
  6806. link(p):=loc; start:=p; loc:=p;
  6807. end;
  6808. end
  6809. @ @<Complain about an undefined macro@>=
  6810. begin print_err("Undefined control sequence");
  6811. @.Undefined control sequence@>
  6812. help5("The control sequence at the end of the top line")@/
  6813. ("of your error message was never \def'ed. If you have")@/
  6814. ("misspelled it (e.g., `\hobx'), type `I' and the correct")@/
  6815. ("spelling (e.g., `I\hbox'). Otherwise just continue,")@/
  6816. ("and I'll forget about whatever was undefined.");
  6817. error;
  6818. end
  6819. @ The |expand| procedure and some other routines that construct token
  6820. lists find it convenient to use the following macros, which are valid only if
  6821. the variables |p| and |q| are reserved for token-list building.
  6822. @d store_new_token(#)==begin q:=get_avail; link(p):=q; info(q):=#;
  6823. p:=q; {|link(p)| is |null|}
  6824. end
  6825. @d fast_store_new_token(#)==begin fast_get_avail(q); link(p):=q; info(q):=#;
  6826. p:=q; {|link(p)| is |null|}
  6827. end
  6828. @ @<Manufacture a control...@>=
  6829. begin r:=get_avail; p:=r; {head of the list of characters}
  6830. repeat get_x_token;
  6831. if cur_cs=0 then store_new_token(cur_tok);
  6832. until cur_cs<>0;
  6833. if cur_cmd<>end_cs_name then @<Complain about missing \.{\\endcsname}@>;
  6834. @<Look up the characters of list |r| in the hash table, and set |cur_cs|@>;
  6835. flush_list(r);
  6836. if eq_type(cur_cs)=undefined_cs then
  6837. begin eq_define(cur_cs,relax,256); {N.B.: The |save_stack| might change}
  6838. end; {the control sequence will now match `\.{\\relax}'}
  6839. cur_tok:=cur_cs+cs_token_flag; back_input;
  6840. end
  6841. @ @<Complain about missing \.{\\endcsname}@>=
  6842. begin print_err("Missing "); print_esc("endcsname"); print(" inserted");
  6843. @.Missing \\endcsname...@>
  6844. help2("The control sequence marked <to be read again> should")@/
  6845. ("not appear between \csname and \endcsname.");
  6846. back_error;
  6847. end
  6848. @ @<Look up the characters of list |r| in the hash table...@>=
  6849. j:=first; p:=link(r);
  6850. while p<>null do
  6851. begin if j>=max_buf_stack then
  6852. begin max_buf_stack:=j+1;
  6853. if max_buf_stack=buf_size then
  6854. overflow("buffer size",buf_size);
  6855. @:TeX capacity exceeded buffer size}{\quad buffer size@>
  6856. end;
  6857. buffer[j]:=info(p) mod @'400; incr(j); p:=link(p);
  6858. end;
  6859. if j>first+1 then
  6860. begin no_new_control_sequence:=false; cur_cs:=id_lookup(first,j-first);
  6861. no_new_control_sequence:=true;
  6862. end
  6863. else if j=first then cur_cs:=null_cs {the list is empty}
  6864. else cur_cs:=single_base+buffer[first] {the list has length one}
  6865. @ An |end_template| command is effectively changed to an |endv| command
  6866. by the following code. (The reason for this is discussed below; the
  6867. |frozen_end_template| at the end of the template has passed the
  6868. |check_outer_validity| test, so its mission of error detection has been
  6869. accomplished.)
  6870. @<Insert a token containing |frozen_endv|@>=
  6871. begin cur_tok:=cs_token_flag+frozen_endv; back_input;
  6872. end
  6873. @ The processing of \.{\\input} involves the |start_input| subroutine,
  6874. which will be declared later; the processing of \.{\\endinput} is trivial.
  6875. @<Put each...@>=
  6876. primitive("input",input,0);@/
  6877. @!@:input_}{\.{\\input} primitive@>
  6878. primitive("endinput",input,1);@/
  6879. @!@:end_input_}{\.{\\endinput} primitive@>
  6880. @ @<Cases of |print_cmd_chr|...@>=
  6881. input: if chr_code=0 then print_esc("input")@+else print_esc("endinput");
  6882. @ @<Initiate or terminate input...@>=
  6883. if cur_chr>0 then force_eof:=true
  6884. else if name_in_progress then insert_relax
  6885. else start_input
  6886. @ Sometimes the expansion looks too far ahead, so we want to insert
  6887. a harmless \.{\\relax} into the user's input.
  6888. @<Declare the procedure called |insert_relax|@>=
  6889. procedure insert_relax;
  6890. begin cur_tok:=cs_token_flag+cur_cs; back_input;
  6891. cur_tok:=cs_token_flag+frozen_relax; back_input; token_type:=inserted;
  6892. end;
  6893. @ Here is a recursive procedure that is \TeX's usual way to get the
  6894. next token of input. It has been slightly optimized to take account of
  6895. common cases.
  6896. @p procedure get_x_token; {sets |cur_cmd|, |cur_chr|, |cur_tok|,
  6897. and expands macros}
  6898. label restart,done;
  6899. begin restart: get_next;
  6900. @^inner loop@>
  6901. if cur_cmd<=max_command then goto done;
  6902. if cur_cmd>=call then
  6903. if cur_cmd<end_template then macro_call
  6904. else begin cur_cs:=frozen_endv; cur_cmd:=endv;
  6905. goto done; {|cur_chr=null_list|}
  6906. end
  6907. else expand;
  6908. goto restart;
  6909. done: if cur_cs=0 then cur_tok:=(cur_cmd*@'400)+cur_chr
  6910. else cur_tok:=cs_token_flag+cur_cs;
  6911. end;
  6912. @ The |get_x_token| procedure is essentially equivalent to two consecutive
  6913. procedure calls: |get_next; x_token|.
  6914. @p procedure x_token; {|get_x_token| without the initial |get_next|}
  6915. begin while cur_cmd>max_command do
  6916. begin expand;
  6917. get_next;
  6918. end;
  6919. if cur_cs=0 then cur_tok:=(cur_cmd*@'400)+cur_chr
  6920. else cur_tok:=cs_token_flag+cur_cs;
  6921. end;
  6922. @ A control sequence that has been \.{\\def}'ed by the user is expanded by
  6923. \TeX's |macro_call| procedure.
  6924. Before we get into the details of |macro_call|, however, let's consider the
  6925. treatment of primitives like \.{\\topmark}, since they are essentially
  6926. macros without parameters. The token lists for such marks are kept in a
  6927. global array of five pointers; we refer to the individual entries of this
  6928. array by symbolic names |top_mark|, etc. The value of |top_mark| is either
  6929. |null| or a pointer to the reference count of a token list.
  6930. @d top_mark_code=0 {the mark in effect at the previous page break}
  6931. @d first_mark_code=1 {the first mark between |top_mark| and |bot_mark|}
  6932. @d bot_mark_code=2 {the mark in effect at the current page break}
  6933. @d split_first_mark_code=3 {the first mark found by \.{\\vsplit}}
  6934. @d split_bot_mark_code=4 {the last mark found by \.{\\vsplit}}
  6935. @d top_mark==cur_mark[top_mark_code]
  6936. @d first_mark==cur_mark[first_mark_code]
  6937. @d bot_mark==cur_mark[bot_mark_code]
  6938. @d split_first_mark==cur_mark[split_first_mark_code]
  6939. @d split_bot_mark==cur_mark[split_bot_mark_code]
  6940. @<Glob...@>=
  6941. @!cur_mark:array[top_mark_code..split_bot_mark_code] of pointer;
  6942. {token lists for marks}
  6943. @ @<Set init...@>=
  6944. top_mark:=null; first_mark:=null; bot_mark:=null;
  6945. split_first_mark:=null; split_bot_mark:=null;
  6946. @ @<Put each...@>=
  6947. primitive("topmark",top_bot_mark,top_mark_code);
  6948. @!@:top_mark_}{\.{\\topmark} primitive@>
  6949. primitive("firstmark",top_bot_mark,first_mark_code);
  6950. @!@:first_mark_}{\.{\\firstmark} primitive@>
  6951. primitive("botmark",top_bot_mark,bot_mark_code);
  6952. @!@:bot_mark_}{\.{\\botmark} primitive@>
  6953. primitive("splitfirstmark",top_bot_mark,split_first_mark_code);
  6954. @!@:split_first_mark_}{\.{\\splitfirstmark} primitive@>
  6955. primitive("splitbotmark",top_bot_mark,split_bot_mark_code);
  6956. @!@:split_bot_mark_}{\.{\\splitbotmark} primitive@>
  6957. @ @<Cases of |print_cmd_chr|...@>=
  6958. top_bot_mark: case chr_code of
  6959. first_mark_code: print_esc("firstmark");
  6960. bot_mark_code: print_esc("botmark");
  6961. split_first_mark_code: print_esc("splitfirstmark");
  6962. split_bot_mark_code: print_esc("splitbotmark");
  6963. othercases print_esc("topmark")
  6964. endcases;
  6965. @ The following code is activated when |cur_cmd=top_bot_mark| and
  6966. when |cur_chr| is a code like |top_mark_code|.
  6967. @<Insert the \(a)appropriate mark text into the scanner@>=
  6968. begin if cur_mark[cur_chr]<>null then
  6969. begin_token_list(cur_mark[cur_chr],mark_text);
  6970. end
  6971. @ Now let's consider |macro_call| itself, which is invoked when \TeX\ is
  6972. scanning a control sequence whose |cur_cmd| is either |call|, |long_call|,
  6973. |outer_call|, or |long_outer_call|. The control sequence definition
  6974. appears in the token list whose reference count is in location |cur_chr|
  6975. of |mem|.
  6976. The global variable |long_state| will be set to |call| or to |long_call|,
  6977. depending on whether or not the control sequence disallows \.{\\par}
  6978. in its parameters. The |get_next| routine will set |long_state| to
  6979. |outer_call| and emit \.{\\par}, if a file ends or if an \.{\\outer}
  6980. control sequence occurs in the midst of an argument.
  6981. @<Glob...@>=
  6982. @!long_state:call..long_outer_call; {governs the acceptance of \.{\\par}}
  6983. @ The parameters, if any, must be scanned before the macro is expanded.
  6984. Parameters are token lists without reference counts. They are placed on
  6985. an auxiliary stack called |pstack| while they are being scanned, since
  6986. the |param_stack| may be losing entries during the matching process.
  6987. (Note that |param_stack| can't be gaining entries, since |macro_call| is
  6988. the only routine that puts anything onto |param_stack|, and it
  6989. is not recursive.)
  6990. @<Glob...@>=
  6991. @!pstack:array[0..8] of pointer; {arguments supplied to a macro}
  6992. @ After parameter scanning is complete, the parameters are moved to the
  6993. |param_stack|. Then the macro body is fed to the scanner; in other words,
  6994. |macro_call| places the defined text of the control sequence at the
  6995. top of\/ \TeX's input stack, so that |get_next| will proceed to read it
  6996. next.
  6997. The global variable |cur_cs| contains the |eqtb| address of the control sequence
  6998. being expanded, when |macro_call| begins. If this control sequence has not been
  6999. declared \.{\\long}, i.e., if its command code in the |eq_type| field is
  7000. not |long_call| or |long_outer_call|, its parameters are not allowed to contain
  7001. the control sequence \.{\\par}. If an illegal \.{\\par} appears, the macro
  7002. call is aborted, and the \.{\\par} will be rescanned.
  7003. @<Declare the procedure called |macro_call|@>=
  7004. procedure macro_call; {invokes a user-defined control sequence}
  7005. label exit, continue, done, done1, found;
  7006. var r:pointer; {current node in the macro's token list}
  7007. @!p:pointer; {current node in parameter token list being built}
  7008. @!q:pointer; {new node being put into the token list}
  7009. @!s:pointer; {backup pointer for parameter matching}
  7010. @!t:pointer; {cycle pointer for backup recovery}
  7011. @!u,@!v:pointer; {auxiliary pointers for backup recovery}
  7012. @!rbrace_ptr:pointer; {one step before the last |right_brace| token}
  7013. @!n:small_number; {the number of parameters scanned}
  7014. @!unbalance:halfword; {unmatched left braces in current parameter}
  7015. @!m:halfword; {the number of tokens or groups (usually)}
  7016. @!ref_count:pointer; {start of the token list}
  7017. @!save_scanner_status:small_number; {|scanner_status| upon entry}
  7018. @!save_warning_index:pointer; {|warning_index| upon entry}
  7019. @!match_chr:ASCII_code; {character used in parameter}
  7020. begin save_scanner_status:=scanner_status; save_warning_index:=warning_index;
  7021. warning_index:=cur_cs; ref_count:=cur_chr; r:=link(ref_count); n:=0;
  7022. if tracing_macros>0 then @<Show the text of the macro being expanded@>;
  7023. if info(r)<>end_match_token then
  7024. @<Scan the parameters and make |link(r)| point to the macro body; but
  7025. |return| if an illegal \.{\\par} is detected@>;
  7026. @<Feed the macro body and its parameters to the scanner@>;
  7027. exit:scanner_status:=save_scanner_status; warning_index:=save_warning_index;
  7028. end;
  7029. @ Before we put a new token list on the input stack, it is wise to clean off
  7030. all token lists that have recently been depleted. Then a user macro that ends
  7031. with a call to itself will not require unbounded stack space.
  7032. @<Feed the macro body and its parameters to the scanner@>=
  7033. while (state=token_list)and(loc=null)and(token_type<>v_template) do
  7034. end_token_list; {conserve stack space}
  7035. begin_token_list(ref_count,macro); name:=warning_index; loc:=link(r);
  7036. if n>0 then
  7037. begin if param_ptr+n>max_param_stack then
  7038. begin max_param_stack:=param_ptr+n;
  7039. if max_param_stack>param_size then
  7040. overflow("parameter stack size",param_size);
  7041. @:TeX capacity exceeded parameter stack size}{\quad parameter stack size@>
  7042. end;
  7043. for m:=0 to n-1 do param_stack[param_ptr+m]:=pstack[m];
  7044. param_ptr:=param_ptr+n;
  7045. end
  7046. @ At this point, the reader will find it advisable to review the explanation
  7047. of token list format that was presented earlier, since many aspects of that
  7048. format are of importance chiefly in the |macro_call| routine.
  7049. The token list might begin with a string of compulsory tokens before the
  7050. first |match| or |end_match|. In that case the macro name is supposed to be
  7051. followed by those tokens; the following program will set |s=null| to
  7052. represent this restriction. Otherwise |s| will be set to the first token of
  7053. a string that will delimit the next parameter.
  7054. @<Scan the parameters and make |link(r)| point to the macro body...@>=
  7055. begin scanner_status:=matching; unbalance:=0;
  7056. long_state:=eq_type(cur_cs);
  7057. if long_state>=outer_call then long_state:=long_state-2;
  7058. repeat link(temp_head):=null;
  7059. if (info(r)>match_token+255)or(info(r)<match_token) then s:=null
  7060. else begin match_chr:=info(r)-match_token; s:=link(r); r:=s;
  7061. p:=temp_head; m:=0;
  7062. end;
  7063. @<Scan a parameter until its delimiter string has been found; or, if |s=null|,
  7064. simply scan the delimiter string@>;@/
  7065. {now |info(r)| is a token whose command code is either |match| or |end_match|}
  7066. until info(r)=end_match_token;
  7067. end
  7068. @ If |info(r)| is a |match| or |end_match| command, it cannot be equal to
  7069. any token found by |get_token|. Therefore an undelimited parameter---i.e.,
  7070. a |match| that is immediately followed by |match| or |end_match|---will
  7071. always fail the test `|cur_tok=info(r)|' in the following algorithm.
  7072. @<Scan a parameter until its delimiter string has been found; or, ...@>=
  7073. continue: get_token; {set |cur_tok| to the next token of input}
  7074. if cur_tok=info(r) then
  7075. @<Advance \(r)|r|; |goto found| if the parameter delimiter has been
  7076. fully matched, otherwise |goto continue|@>;
  7077. @<Contribute the recently matched tokens to the current parameter, and
  7078. |goto continue| if a partial match is still in effect;
  7079. but abort if |s=null|@>;
  7080. if cur_tok=par_token then if long_state<>long_call then
  7081. @<Report a runaway argument and abort@>;
  7082. if cur_tok<right_brace_limit then
  7083. if cur_tok<left_brace_limit then
  7084. @<Contribute an entire group to the current parameter@>
  7085. else @<Report an extra right brace and |goto continue|@>
  7086. else @<Store the current token, but |goto continue| if it is
  7087. a blank space that would become an undelimited parameter@>;
  7088. incr(m);
  7089. if info(r)>end_match_token then goto continue;
  7090. if info(r)<match_token then goto continue;
  7091. found: if s<>null then @<Tidy up the parameter just scanned, and tuck it away@>
  7092. @ @<Store the current token, but |goto continue| if it is...@>=
  7093. begin if cur_tok=space_token then
  7094. if info(r)<=end_match_token then
  7095. if info(r)>=match_token then goto continue;
  7096. store_new_token(cur_tok);
  7097. end
  7098. @ A slightly subtle point arises here: When the parameter delimiter ends
  7099. with `\.{\#\{}', the token list will have a left brace both before and
  7100. after the |end_match|\kern-.4pt. Only one of these should affect the
  7101. |align_state|, but both will be scanned, so we must make a correction.
  7102. @<Advance \(r)|r|; |goto found| if the parameter delimiter has been fully...@>=
  7103. begin r:=link(r);
  7104. if (info(r)>=match_token)and(info(r)<=end_match_token) then
  7105. begin if cur_tok<left_brace_limit then decr(align_state);
  7106. goto found;
  7107. end
  7108. else goto continue;
  7109. end
  7110. @ @<Report an extra right brace and |goto continue|@>=
  7111. begin back_input; print_err("Argument of "); sprint_cs(warning_index);
  7112. @.Argument of \\x has...@>
  7113. print(" has an extra }");
  7114. help6("I've run across a `}' that doesn't seem to match anything.")@/
  7115. ("For example, `\def\a#1{...}' and `\a}' would produce")@/
  7116. ("this error. If you simply proceed now, the `\par' that")@/
  7117. ("I've just inserted will cause me to report a runaway")@/
  7118. ("argument that might be the root of the problem. But if")@/
  7119. ("your `}' was spurious, just type `2' and it will go away.");
  7120. incr(align_state); long_state:=call; cur_tok:=par_token; ins_error;
  7121. goto continue;
  7122. end {a white lie; the \.{\\par} won't always trigger a runaway}
  7123. @ If |long_state=outer_call|, a runaway argument has already been reported.
  7124. @<Report a runaway argument and abort@>=
  7125. begin if long_state=call then
  7126. begin runaway; print_err("Paragraph ended before ");
  7127. @.Paragraph ended before...@>
  7128. sprint_cs(warning_index); print(" was complete");
  7129. help3("I suspect you've forgotten a `}', causing me to apply this")@/
  7130. ("control sequence to too much text. How can we recover?")@/
  7131. ("My plan is to forget the whole thing and hope for the best.");
  7132. back_error;
  7133. end;
  7134. pstack[n]:=link(temp_head); align_state:=align_state-unbalance;
  7135. for m:=0 to n do flush_list(pstack[m]);
  7136. return;
  7137. end
  7138. @ When the following code becomes active, we have matched tokens from |s| to
  7139. the predecessor of |r|, and we have found that |cur_tok<>info(r)|. An
  7140. interesting situation now presents itself: If the parameter is to be
  7141. delimited by a string such as `\.{ab}', and if we have scanned `\.{aa}',
  7142. we want to contribute one `\.a' to the current parameter and resume
  7143. looking for a `\.b'. The program must account for such partial matches and
  7144. for others that can be quite complex. But most of the time we have |s=r|
  7145. and nothing needs to be done.
  7146. Incidentally, it is possible for \.{\\par} tokens to sneak in to certain
  7147. parameters of non-\.{\\long} macros. For example, consider a case like
  7148. `\.{\\def\\a\#1\\par!\{...\}}' where the first \.{\\par} is not followed
  7149. by an exclamation point. In such situations it does not seem appropriate
  7150. to prohibit the \.{\\par}, so \TeX\ keeps quiet about this bending of
  7151. the rules.
  7152. @<Contribute the recently matched tokens to the current parameter...@>=
  7153. if s<>r then
  7154. if s=null then @<Report an improper use of the macro and abort@>
  7155. else begin t:=s;
  7156. repeat store_new_token(info(t)); incr(m); u:=link(t); v:=s;
  7157. loop@+ begin if u=r then
  7158. if cur_tok<>info(v) then goto done
  7159. else begin r:=link(v); goto continue;
  7160. end;
  7161. if info(u)<>info(v) then goto done;
  7162. u:=link(u); v:=link(v);
  7163. end;
  7164. done: t:=link(t);
  7165. until t=r;
  7166. r:=s; {at this point, no tokens are recently matched}
  7167. end
  7168. @ @<Report an improper use...@>=
  7169. begin print_err("Use of "); sprint_cs(warning_index);
  7170. @.Use of x doesn't match...@>
  7171. print(" doesn't match its definition");
  7172. help4("If you say, e.g., `\def\a1{...}', then you must always")@/
  7173. ("put `1' after `\a', since control sequence names are")@/
  7174. ("made up of letters only. The macro here has not been")@/
  7175. ("followed by the required stuff, so I'm ignoring it.");
  7176. error; return;
  7177. end
  7178. @ @<Contribute an entire group to the current parameter@>=
  7179. begin unbalance:=1;
  7180. @^inner loop@>
  7181. loop@+ begin fast_store_new_token(cur_tok); get_token;
  7182. if cur_tok=par_token then if long_state<>long_call then
  7183. @<Report a runaway argument and abort@>;
  7184. if cur_tok<right_brace_limit then
  7185. if cur_tok<left_brace_limit then incr(unbalance)
  7186. else begin decr(unbalance);
  7187. if unbalance=0 then goto done1;
  7188. end;
  7189. end;
  7190. done1: rbrace_ptr:=p; store_new_token(cur_tok);
  7191. end
  7192. @ If the parameter consists of a single group enclosed in braces, we must
  7193. strip off the enclosing braces. That's why |rbrace_ptr| was introduced.
  7194. @<Tidy up the parameter just scanned, and tuck it away@>=
  7195. begin if (m=1)and(info(p)<right_brace_limit) then
  7196. begin link(rbrace_ptr):=null; free_avail(p);
  7197. p:=link(temp_head); pstack[n]:=link(p); free_avail(p);
  7198. end
  7199. else pstack[n]:=link(temp_head);
  7200. incr(n);
  7201. if tracing_macros>0 then
  7202. begin begin_diagnostic; print_nl(match_chr); print_int(n);
  7203. print("<-"); show_token_list(pstack[n-1],null,1000);
  7204. end_diagnostic(false);
  7205. end;
  7206. end
  7207. @ @<Show the text of the macro being expanded@>=
  7208. begin begin_diagnostic; print_ln; print_cs(warning_index);
  7209. token_show(ref_count); end_diagnostic(false);
  7210. end
  7211. @* \[26] Basic scanning subroutines.
  7212. Let's turn now to some procedures that \TeX\ calls upon frequently to digest
  7213. certain kinds of patterns in the input. Most of these are quite simple;
  7214. some are quite elaborate. Almost all of the routines call |get_x_token|,
  7215. which can cause them to be invoked recursively.
  7216. @^stomach@>
  7217. @^recursion@>
  7218. @ The |scan_left_brace| routine is called when a left brace is supposed to be
  7219. the next non-blank token. (The term ``left brace'' means, more precisely,
  7220. a character whose catcode is |left_brace|.) \TeX\ allows \.{\\relax} to
  7221. appear before the |left_brace|.
  7222. @p procedure scan_left_brace; {reads a mandatory |left_brace|}
  7223. begin @<Get the next non-blank non-relax non-call token@>;
  7224. if cur_cmd<>left_brace then
  7225. begin print_err("Missing { inserted");
  7226. @.Missing \{ inserted@>
  7227. help4("A left brace was mandatory here, so I've put one in.")@/
  7228. ("You might want to delete and/or insert some corrections")@/
  7229. ("so that I will find a matching right brace soon.")@/
  7230. ("(If you're confused by all this, try typing `I}' now.)");
  7231. back_error; cur_tok:=left_brace_token+"{"; cur_cmd:=left_brace;
  7232. cur_chr:="{"; incr(align_state);
  7233. end;
  7234. end;
  7235. @ @<Get the next non-blank non-relax non-call token@>=
  7236. repeat get_x_token;
  7237. until (cur_cmd<>spacer)and(cur_cmd<>relax)
  7238. @ The |scan_optional_equals| routine looks for an optional `\.=' sign preceded
  7239. by optional spaces; `\.{\\relax}' is not ignored here.
  7240. @p procedure scan_optional_equals;
  7241. begin @<Get the next non-blank non-call token@>;
  7242. if cur_tok<>other_token+"=" then back_input;
  7243. end;
  7244. @ @<Get the next non-blank non-call token@>=
  7245. repeat get_x_token;
  7246. until cur_cmd<>spacer
  7247. @ In case you are getting bored, here is a slightly less trivial routine:
  7248. Given a string of lowercase letters, like `\.{pt}' or `\.{plus}' or
  7249. `\.{width}', the |scan_keyword| routine checks to see whether the next
  7250. tokens of input match this string. The match must be exact, except that
  7251. uppercase letters will match their lowercase counterparts; uppercase
  7252. equivalents are determined by subtracting |"a"-"A"|, rather than using the
  7253. |uc_code| table, since \TeX\ uses this routine only for its own limited
  7254. set of keywords.
  7255. If a match is found, the characters are effectively removed from the input
  7256. and |true| is returned. Otherwise |false| is returned, and the input
  7257. is left essentially unchanged (except for the fact that some macros
  7258. may have been expanded, etc.).
  7259. @^inner loop@>
  7260. @p function scan_keyword(@!s:str_number):boolean; {look for a given string}
  7261. label exit;
  7262. var p:pointer; {tail of the backup list}
  7263. @!q:pointer; {new node being added to the token list via |store_new_token|}
  7264. @!k:pool_pointer; {index into |str_pool|}
  7265. begin p:=backup_head; link(p):=null; k:=str_start[s];
  7266. while k<str_start[s+1] do
  7267. begin get_x_token; {recursion is possible here}
  7268. @^recursion@>
  7269. if (cur_cs=0)and@|
  7270. ((cur_chr=so(str_pool[k]))or(cur_chr=so(str_pool[k])-"a"+"A")) then
  7271. begin store_new_token(cur_tok); incr(k);
  7272. end
  7273. else if (cur_cmd<>spacer)or(p<>backup_head) then
  7274. begin back_input;
  7275. if p<>backup_head then back_list(link(backup_head));
  7276. scan_keyword:=false; return;
  7277. end;
  7278. end;
  7279. flush_list(link(backup_head)); scan_keyword:=true;
  7280. exit:end;
  7281. @ Here is a procedure that sounds an alarm when mu and non-mu units
  7282. are being switched.
  7283. @p procedure mu_error;
  7284. begin print_err("Incompatible glue units");
  7285. @.Incompatible glue units@>
  7286. help1("I'm going to assume that 1mu=1pt when they're mixed.");
  7287. error;
  7288. end;
  7289. @ The next routine `|scan_something_internal|' is used to fetch internal
  7290. numeric quantities like `\.{\\hsize}', and also to handle the `\.{\\the}'
  7291. when expanding constructions like `\.{\\the\\toks0}' and
  7292. `\.{\\the\\baselineskip}'. Soon we will be considering the |scan_int|
  7293. procedure, which calls |scan_something_internal|; on the other hand,
  7294. |scan_something_internal| also calls |scan_int|, for constructions like
  7295. `\.{\\catcode\`\\\$}' or `\.{\\fontdimen} \.3 \.{\\ff}'. So we
  7296. have to declare |scan_int| as a |forward| procedure. A few other
  7297. procedures are also declared at this point.
  7298. @p procedure@?scan_int; forward; {scans an integer value}
  7299. @t\4\4@>@<Declare procedures that scan restricted classes of integers@>@;
  7300. @t\4\4@>@<Declare procedures that scan font-related stuff@>
  7301. @ \TeX\ doesn't know exactly what to expect when |scan_something_internal|
  7302. begins. For example, an integer or dimension or glue value could occur
  7303. immediately after `\.{\\hskip}'; and one can even say \.{\\the} with
  7304. respect to token lists in constructions like
  7305. `\.{\\xdef\\o\{\\the\\output\}}'. On the other hand, only integers are
  7306. allowed after a construction like `\.{\\count}'. To handle the various
  7307. possibilities, |scan_something_internal| has a |level| parameter, which
  7308. tells the ``highest'' kind of quantity that |scan_something_internal| is
  7309. allowed to produce. Six levels are distinguished, namely |int_val|,
  7310. |dimen_val|, |glue_val|, |mu_val|, |ident_val|, and |tok_val|.
  7311. The output of |scan_something_internal| (and of the other routines
  7312. |scan_int|, |scan_dimen|, and |scan_glue| below) is put into the global
  7313. variable |cur_val|, and its level is put into |cur_val_level|. The highest
  7314. values of |cur_val_level| are special: |mu_val| is used only when
  7315. |cur_val| points to something in a ``muskip'' register, or to one of the
  7316. three parameters \.{\\thinmuskip}, \.{\\medmuskip}, \.{\\thickmuskip};
  7317. |ident_val| is used only when |cur_val| points to a font identifier;
  7318. |tok_val| is used only when |cur_val| points to |null| or to the reference
  7319. count of a token list. The last two cases are allowed only when
  7320. |scan_something_internal| is called with |level=tok_val|.
  7321. If the output is glue, |cur_val| will point to a glue specification, and
  7322. the reference count of that glue will have been updated to reflect this
  7323. reference; if the output is a nonempty token list, |cur_val| will point to
  7324. its reference count, but in this case the count will not have been updated.
  7325. Otherwise |cur_val| will contain the integer or scaled value in question.
  7326. @d int_val=0 {integer values}
  7327. @d dimen_val=1 {dimension values}
  7328. @d glue_val=2 {glue specifications}
  7329. @d mu_val=3 {math glue specifications}
  7330. @d ident_val=4 {font identifier}
  7331. @d tok_val=5 {token lists}
  7332. @<Glob...@>=
  7333. @!cur_val:integer; {value returned by numeric scanners}
  7334. @!cur_val_level:int_val..tok_val; {the ``level'' of this value}
  7335. @ The hash table is initialized with `\.{\\count}', `\.{\\dimen}', `\.{\\skip}',
  7336. and `\.{\\muskip}' all having |register| as their command code; they are
  7337. distinguished by the |chr_code|, which is either |int_val|, |dimen_val|,
  7338. |glue_val|, or |mu_val|.
  7339. @<Put each...@>=
  7340. primitive("count",register,int_val);
  7341. @!@:count_}{\.{\\count} primitive@>
  7342. primitive("dimen",register,dimen_val);
  7343. @!@:dimen_}{\.{\\dimen} primitive@>
  7344. primitive("skip",register,glue_val);
  7345. @!@:skip_}{\.{\\skip} primitive@>
  7346. primitive("muskip",register,mu_val);
  7347. @!@:mu_skip_}{\.{\\muskip} primitive@>
  7348. @ @<Cases of |print_cmd_chr|...@>=
  7349. register: if chr_code=int_val then print_esc("count")
  7350. else if chr_code=dimen_val then print_esc("dimen")
  7351. else if chr_code=glue_val then print_esc("skip")
  7352. else print_esc("muskip");
  7353. @ OK, we're ready for |scan_something_internal| itself. A second parameter,
  7354. |negative|, is set |true| if the value that is found should be negated.
  7355. It is assumed that |cur_cmd| and |cur_chr| represent the first token of
  7356. the internal quantity to be scanned; an error will be signalled if
  7357. |cur_cmd<min_internal| or |cur_cmd>max_internal|.
  7358. @d scanned_result_end(#)==cur_val_level:=#;@+end
  7359. @d scanned_result(#)==@+begin cur_val:=#;scanned_result_end
  7360. @p procedure scan_something_internal(@!level:small_number;@!negative:boolean);
  7361. {fetch an internal parameter}
  7362. var m:halfword; {|chr_code| part of the operand token}
  7363. @!p:0..nest_size; {index into |nest|}
  7364. begin m:=cur_chr;
  7365. case cur_cmd of
  7366. def_code: @<Fetch a character code from some table@>;
  7367. toks_register,assign_toks,def_family,set_font,def_font: @<Fetch a token list or
  7368. font identifier, provided that |level=tok_val|@>;
  7369. assign_int: scanned_result(eqtb[m].int)(int_val);
  7370. assign_dimen: scanned_result(eqtb[m].sc)(dimen_val);
  7371. assign_glue: scanned_result(equiv(m))(glue_val);
  7372. assign_mu_glue: scanned_result(equiv(m))(mu_val);
  7373. set_aux: @<Fetch the |space_factor| or the |prev_depth|@>;
  7374. set_prev_graf: @<Fetch the |prev_graf|@>;
  7375. set_page_int:@<Fetch the |dead_cycles| or the |insert_penalties|@>;
  7376. set_page_dimen: @<Fetch something on the |page_so_far|@>;
  7377. set_shape: @<Fetch the |par_shape| size@>;
  7378. set_box_dimen: @<Fetch a box dimension@>;
  7379. char_given,math_given: scanned_result(cur_chr)(int_val);
  7380. assign_font_dimen: @<Fetch a font dimension@>;
  7381. assign_font_int: @<Fetch a font integer@>;
  7382. register: @<Fetch a register@>;
  7383. last_item: @<Fetch an item in the current node, if appropriate@>;
  7384. othercases @<Complain that \.{\\the} can't do this; give zero result@>
  7385. endcases;@/
  7386. while cur_val_level>level do @<Convert \(c)|cur_val| to a lower level@>;
  7387. @<Fix the reference count, if any, and negate |cur_val| if |negative|@>;
  7388. end;
  7389. @ @<Fetch a character code from some table@>=
  7390. begin scan_char_num;
  7391. if m=math_code_base then scanned_result(ho(math_code(cur_val)))(int_val)
  7392. else if m<math_code_base then scanned_result(equiv(m+cur_val))(int_val)
  7393. else scanned_result(eqtb[m+cur_val].int)(int_val);
  7394. end
  7395. @ @<Fetch a token list...@>=
  7396. if level<>tok_val then
  7397. begin print_err("Missing number, treated as zero");
  7398. @.Missing number...@>
  7399. help3("A number should have been here; I inserted `0'.")@/
  7400. ("(If you can't figure out why I needed to see a number,")@/
  7401. ("look up `weird error' in the index to The TeXbook.)");
  7402. @:TeXbook}{\sl The \TeX book@>
  7403. back_error; scanned_result(0)(dimen_val);
  7404. end
  7405. else if cur_cmd<=assign_toks then
  7406. begin if cur_cmd<assign_toks then {|cur_cmd=toks_register|}
  7407. begin scan_eight_bit_int; m:=toks_base+cur_val;
  7408. end;
  7409. scanned_result(equiv(m))(tok_val);
  7410. end
  7411. else begin back_input; scan_font_ident;
  7412. scanned_result(font_id_base+cur_val)(ident_val);
  7413. end
  7414. @ Users refer to `\.{\\the\\spacefactor}' only in horizontal
  7415. mode, and to `\.{\\the\\prevdepth}' only in vertical mode; so we put the
  7416. associated mode in the modifier part of the |set_aux| command.
  7417. The |set_page_int| command has modifier 0 or 1, for `\.{\\deadcycles}' and
  7418. `\.{\\insertpenalties}', respectively. The |set_box_dimen| command is
  7419. modified by either |width_offset|, |height_offset|, or |depth_offset|.
  7420. And the |last_item| command is modified by either |int_val|, |dimen_val|,
  7421. |glue_val|, |input_line_no_code|, or |badness_code|.
  7422. @d input_line_no_code=glue_val+1 {code for \.{\\inputlineno}}
  7423. @d badness_code=glue_val+2 {code for \.{\\badness}}
  7424. @<Put each...@>=
  7425. primitive("spacefactor",set_aux,hmode);
  7426. @!@:space_factor_}{\.{\\spacefactor} primitive@>
  7427. primitive("prevdepth",set_aux,vmode);@/
  7428. @!@:prev_depth_}{\.{\\prevdepth} primitive@>
  7429. primitive("deadcycles",set_page_int,0);
  7430. @!@:dead_cycles_}{\.{\\deadcycles} primitive@>
  7431. primitive("insertpenalties",set_page_int,1);
  7432. @!@:insert_penalties_}{\.{\\insertpenalties} primitive@>
  7433. primitive("wd",set_box_dimen,width_offset);
  7434. @!@:wd_}{\.{\\wd} primitive@>
  7435. primitive("ht",set_box_dimen,height_offset);
  7436. @!@:ht_}{\.{\\ht} primitive@>
  7437. primitive("dp",set_box_dimen,depth_offset);
  7438. @!@:dp_}{\.{\\dp} primitive@>
  7439. primitive("lastpenalty",last_item,int_val);
  7440. @!@:last_penalty_}{\.{\\lastpenalty} primitive@>
  7441. primitive("lastkern",last_item,dimen_val);
  7442. @!@:last_kern_}{\.{\\lastkern} primitive@>
  7443. primitive("lastskip",last_item,glue_val);
  7444. @!@:last_skip_}{\.{\\lastskip} primitive@>
  7445. primitive("inputlineno",last_item,input_line_no_code);
  7446. @!@:input_line_no_}{\.{\\inputlineno} primitive@>
  7447. primitive("badness",last_item,badness_code);
  7448. @!@:badness_}{\.{\\badness} primitive@>
  7449. @ @<Cases of |print_cmd_chr|...@>=
  7450. set_aux: if chr_code=vmode then print_esc("prevdepth")
  7451. @+else print_esc("spacefactor");
  7452. set_page_int: if chr_code=0 then print_esc("deadcycles")
  7453. @+else print_esc("insertpenalties");
  7454. set_box_dimen: if chr_code=width_offset then print_esc("wd")
  7455. else if chr_code=height_offset then print_esc("ht")
  7456. else print_esc("dp");
  7457. last_item: case chr_code of
  7458. int_val: print_esc("lastpenalty");
  7459. dimen_val: print_esc("lastkern");
  7460. glue_val: print_esc("lastskip");
  7461. input_line_no_code: print_esc("inputlineno");
  7462. othercases print_esc("badness")
  7463. endcases;
  7464. @ @<Fetch the |space_factor| or the |prev_depth|@>=
  7465. if abs(mode)<>m then
  7466. begin print_err("Improper "); print_cmd_chr(set_aux,m);
  7467. @.Improper \\spacefactor@>
  7468. @.Improper \\prevdepth@>
  7469. help4("You can refer to \spacefactor only in horizontal mode;")@/
  7470. ("you can refer to \prevdepth only in vertical mode; and")@/
  7471. ("neither of these is meaningful inside \write. So")@/
  7472. ("I'm forgetting what you said and using zero instead.");
  7473. error;
  7474. if level<>tok_val then scanned_result(0)(dimen_val)
  7475. else scanned_result(0)(int_val);
  7476. end
  7477. else if m=vmode then scanned_result(prev_depth)(dimen_val)
  7478. else scanned_result(space_factor)(int_val)
  7479. @ @<Fetch the |dead_cycles| or the |insert_penalties|@>=
  7480. begin if m=0 then cur_val:=dead_cycles@+else cur_val:=insert_penalties;
  7481. cur_val_level:=int_val;
  7482. end
  7483. @ @<Fetch a box dimension@>=
  7484. begin scan_eight_bit_int;
  7485. if box(cur_val)=null then cur_val:=0 @+else cur_val:=mem[box(cur_val)+m].sc;
  7486. cur_val_level:=dimen_val;
  7487. end
  7488. @ Inside an \.{\\output} routine, a user may wish to look at the page totals
  7489. that were present at the moment when output was triggered.
  7490. @d max_dimen==@'7777777777 {$2^{30}-1$}
  7491. @<Fetch something on the |page_so_far|@>=
  7492. begin if (page_contents=empty) and (not output_active) then
  7493. if m=0 then cur_val:=max_dimen@+else cur_val:=0
  7494. else cur_val:=page_so_far[m];
  7495. cur_val_level:=dimen_val;
  7496. end
  7497. @ @<Fetch the |prev_graf|@>=
  7498. if mode=0 then scanned_result(0)(int_val) {|prev_graf=0| within \.{\\write}}
  7499. else begin nest[nest_ptr]:=cur_list; p:=nest_ptr;
  7500. while abs(nest[p].mode_field)<>vmode do decr(p);
  7501. scanned_result(nest[p].pg_field)(int_val);
  7502. end
  7503. @ @<Fetch the |par_shape| size@>=
  7504. begin if par_shape_ptr=null then cur_val:=0
  7505. else cur_val:=info(par_shape_ptr);
  7506. cur_val_level:=int_val;
  7507. end
  7508. @ Here is where \.{\\lastpenalty}, \.{\\lastkern}, and \.{\\lastskip} are
  7509. implemented. The reference count for \.{\\lastskip} will be updated later.
  7510. We also handle \.{\\inputlineno} and \.{\\badness} here, because they are
  7511. legal in similar contexts.
  7512. @<Fetch an item in the current node...@>=
  7513. if cur_chr>glue_val then
  7514. begin if cur_chr=input_line_no_code then cur_val:=line
  7515. else cur_val:=last_badness; {|cur_chr=badness_code|}
  7516. cur_val_level:=int_val;
  7517. end
  7518. else begin if cur_chr=glue_val then cur_val:=zero_glue@+else cur_val:=0;
  7519. cur_val_level:=cur_chr;
  7520. if not is_char_node(tail)and(mode<>0) then
  7521. case cur_chr of
  7522. int_val: if type(tail)=penalty_node then cur_val:=penalty(tail);
  7523. dimen_val: if type(tail)=kern_node then cur_val:=width(tail);
  7524. glue_val: if type(tail)=glue_node then
  7525. begin cur_val:=glue_ptr(tail);
  7526. if subtype(tail)=mu_glue then cur_val_level:=mu_val;
  7527. end;
  7528. end {there are no other cases}
  7529. else if (mode=vmode)and(tail=head) then
  7530. case cur_chr of
  7531. int_val: cur_val:=last_penalty;
  7532. dimen_val: cur_val:=last_kern;
  7533. glue_val: if last_glue<>max_halfword then cur_val:=last_glue;
  7534. end; {there are no other cases}
  7535. end
  7536. @ @<Fetch a font dimension@>=
  7537. begin find_font_dimen(false); font_info[fmem_ptr].sc:=0;
  7538. scanned_result(font_info[cur_val].sc)(dimen_val);
  7539. end
  7540. @ @<Fetch a font integer@>=
  7541. begin scan_font_ident;
  7542. if m=0 then scanned_result(hyphen_char[cur_val])(int_val)
  7543. else scanned_result(skew_char[cur_val])(int_val);
  7544. end
  7545. @ @<Fetch a register@>=
  7546. begin scan_eight_bit_int;
  7547. case m of
  7548. int_val:cur_val:=count(cur_val);
  7549. dimen_val:cur_val:=dimen(cur_val);
  7550. glue_val: cur_val:=skip(cur_val);
  7551. mu_val: cur_val:=mu_skip(cur_val);
  7552. end; {there are no other cases}
  7553. cur_val_level:=m;
  7554. end
  7555. @ @<Complain that \.{\\the} can't do this; give zero result@>=
  7556. begin print_err("You can't use `"); print_cmd_chr(cur_cmd,cur_chr);
  7557. @.You can't use x after ...@>
  7558. print("' after "); print_esc("the");
  7559. help1("I'm forgetting what you said and using zero instead.");
  7560. error;
  7561. if level<>tok_val then scanned_result(0)(dimen_val)
  7562. else scanned_result(0)(int_val);
  7563. end
  7564. @ When a |glue_val| changes to a |dimen_val|, we use the width component
  7565. of the glue; there is no need to decrease the reference count, since it
  7566. has not yet been increased. When a |dimen_val| changes to an |int_val|,
  7567. we use scaled points so that the value doesn't actually change. And when a
  7568. |mu_val| changes to a |glue_val|, the value doesn't change either.
  7569. @<Convert \(c)|cur_val| to a lower level@>=
  7570. begin if cur_val_level=glue_val then cur_val:=width(cur_val)
  7571. else if cur_val_level=mu_val then mu_error;
  7572. decr(cur_val_level);
  7573. end
  7574. @ If |cur_val| points to a glue specification at this point, the reference
  7575. count for the glue does not yet include the reference by |cur_val|.
  7576. If |negative| is |true|, |cur_val_level| is known to be |<=mu_val|.
  7577. @<Fix the reference count, if any, ...@>=
  7578. if negative then
  7579. if cur_val_level>=glue_val then
  7580. begin cur_val:=new_spec(cur_val);
  7581. @<Negate all three glue components of |cur_val|@>;
  7582. end
  7583. else negate(cur_val)
  7584. else if (cur_val_level>=glue_val)and(cur_val_level<=mu_val) then
  7585. add_glue_ref(cur_val)
  7586. @ @<Negate all three...@>=
  7587. begin negate(width(cur_val));
  7588. negate(stretch(cur_val));
  7589. negate(shrink(cur_val));
  7590. end
  7591. @ Our next goal is to write the |scan_int| procedure, which scans anything that
  7592. \TeX\ treats as an integer. But first we might as well look at some simple
  7593. applications of |scan_int| that have already been made inside of
  7594. |scan_something_internal|.
  7595. @ @<Declare procedures that scan restricted classes of integers@>=
  7596. procedure scan_eight_bit_int;
  7597. begin scan_int;
  7598. if (cur_val<0)or(cur_val>255) then
  7599. begin print_err("Bad register code");
  7600. @.Bad register code@>
  7601. help2("A register number must be between 0 and 255.")@/
  7602. ("I changed this one to zero."); int_error(cur_val); cur_val:=0;
  7603. end;
  7604. end;
  7605. @ @<Declare procedures that scan restricted classes of integers@>=
  7606. procedure scan_char_num;
  7607. begin scan_int;
  7608. if (cur_val<0)or(cur_val>255) then
  7609. begin print_err("Bad character code");
  7610. @.Bad character code@>
  7611. help2("A character number must be between 0 and 255.")@/
  7612. ("I changed this one to zero."); int_error(cur_val); cur_val:=0;
  7613. end;
  7614. end;
  7615. @ While we're at it, we might as well deal with similar routines that
  7616. will be needed later.
  7617. @<Declare procedures that scan restricted classes of integers@>=
  7618. procedure scan_four_bit_int;
  7619. begin scan_int;
  7620. if (cur_val<0)or(cur_val>15) then
  7621. begin print_err("Bad number");
  7622. @.Bad number@>
  7623. help2("Since I expected to read a number between 0 and 15,")@/
  7624. ("I changed this one to zero."); int_error(cur_val); cur_val:=0;
  7625. end;
  7626. end;
  7627. @ @<Declare procedures that scan restricted classes of integers@>=
  7628. procedure scan_fifteen_bit_int;
  7629. begin scan_int;
  7630. if (cur_val<0)or(cur_val>@'77777) then
  7631. begin print_err("Bad mathchar");
  7632. @.Bad mathchar@>
  7633. help2("A mathchar number must be between 0 and 32767.")@/
  7634. ("I changed this one to zero."); int_error(cur_val); cur_val:=0;
  7635. end;
  7636. end;
  7637. @ @<Declare procedures that scan restricted classes of integers@>=
  7638. procedure scan_twenty_seven_bit_int;
  7639. begin scan_int;
  7640. if (cur_val<0)or(cur_val>@'777777777) then
  7641. begin print_err("Bad delimiter code");
  7642. @.Bad delimiter code@>
  7643. help2("A numeric delimiter code must be between 0 and 2^{27}-1.")@/
  7644. ("I changed this one to zero."); int_error(cur_val); cur_val:=0;
  7645. end;
  7646. end;
  7647. @ An integer number can be preceded by any number of spaces and `\.+' or
  7648. `\.-' signs. Then comes either a decimal constant (i.e., radix 10), an
  7649. octal constant (i.e., radix 8, preceded by~\.\'), a hexadecimal constant
  7650. (radix 16, preceded by~\."), an alphabetic constant (preceded by~\.\`), or
  7651. an internal variable. After scanning is complete,
  7652. |cur_val| will contain the answer, which must be at most
  7653. $2^{31}-1=2147483647$ in absolute value. The value of |radix| is set to
  7654. 10, 8, or 16 in the cases of decimal, octal, or hexadecimal constants,
  7655. otherwise |radix| is set to zero. An optional space follows a constant.
  7656. @d octal_token=other_token+"'" {apostrophe, indicates an octal constant}
  7657. @d hex_token=other_token+"""" {double quote, indicates a hex constant}
  7658. @d alpha_token=other_token+"`" {reverse apostrophe, precedes alpha constants}
  7659. @d point_token=other_token+"." {decimal point}
  7660. @d continental_point_token=other_token+"," {decimal point, Eurostyle}
  7661. @<Glob...@>=
  7662. @!radix:small_number; {|scan_int| sets this to 8, 10, 16, or zero}
  7663. @ We initialize the following global variables just in case |expand|
  7664. comes into action before any of the basic scanning routines has assigned
  7665. them a value.
  7666. @<Set init...@>=
  7667. cur_val:=0; cur_val_level:=int_val; radix:=0; cur_order:=normal;
  7668. @ The |scan_int| routine is used also to scan the integer part of a
  7669. fraction; for example, the `\.3' in `\.{3.14159}' will be found by
  7670. |scan_int|. The |scan_dimen| routine assumes that |cur_tok=point_token|
  7671. after the integer part of such a fraction has been scanned by |scan_int|,
  7672. and that the decimal point has been backed up to be scanned again.
  7673. @p procedure scan_int; {sets |cur_val| to an integer}
  7674. label done;
  7675. var negative:boolean; {should the answer be negated?}
  7676. @!m:integer; {|@t$2^{31}$@> div radix|, the threshold of danger}
  7677. @!d:small_number; {the digit just scanned}
  7678. @!vacuous:boolean; {have no digits appeared?}
  7679. @!OK_so_far:boolean; {has an error message been issued?}
  7680. begin radix:=0; OK_so_far:=true;@/
  7681. @<Get the next non-blank non-sign token; set |negative| appropriately@>;
  7682. if cur_tok=alpha_token then @<Scan an alphabetic character code into |cur_val|@>
  7683. else if (cur_cmd>=min_internal)and(cur_cmd<=max_internal) then
  7684. scan_something_internal(int_val,false)
  7685. else @<Scan a numeric constant@>;
  7686. if negative then negate(cur_val);
  7687. end;
  7688. @ @<Get the next non-blank non-sign token...@>=
  7689. negative:=false;
  7690. repeat @<Get the next non-blank non-call token@>;
  7691. if cur_tok=other_token+"-" then
  7692. begin negative := not negative; cur_tok:=other_token+"+";
  7693. end;
  7694. until cur_tok<>other_token+"+"
  7695. @ A space is ignored after an alphabetic character constant, so that
  7696. such constants behave like numeric ones.
  7697. @<Scan an alphabetic character code into |cur_val|@>=
  7698. begin get_token; {suppress macro expansion}
  7699. if cur_tok<cs_token_flag then
  7700. begin cur_val:=cur_chr;
  7701. if cur_cmd<=right_brace then
  7702. if cur_cmd=right_brace then incr(align_state)
  7703. else decr(align_state);
  7704. end
  7705. else if cur_tok<cs_token_flag+single_base then
  7706. cur_val:=cur_tok-cs_token_flag-active_base
  7707. else cur_val:=cur_tok-cs_token_flag-single_base;
  7708. if cur_val>255 then
  7709. begin print_err("Improper alphabetic constant");
  7710. @.Improper alphabetic constant@>
  7711. help2("A one-character control sequence belongs after a ` mark.")@/
  7712. ("So I'm essentially inserting \0 here.");
  7713. cur_val:="0"; back_error;
  7714. end
  7715. else @<Scan an optional space@>;
  7716. end
  7717. @ @<Scan an optional space@>=
  7718. begin get_x_token; if cur_cmd<>spacer then back_input;
  7719. end
  7720. @ @<Scan a numeric constant@>=
  7721. begin radix:=10; m:=214748364;
  7722. if cur_tok=octal_token then
  7723. begin radix:=8; m:=@'2000000000; get_x_token;
  7724. end
  7725. else if cur_tok=hex_token then
  7726. begin radix:=16; m:=@'1000000000; get_x_token;
  7727. end;
  7728. vacuous:=true; cur_val:=0;@/
  7729. @<Accumulate the constant until |cur_tok| is not a suitable digit@>;
  7730. if vacuous then @<Express astonishment that no number was here@>
  7731. else if cur_cmd<>spacer then back_input;
  7732. end
  7733. @ @d infinity==@'17777777777 {the largest positive value that \TeX\ knows}
  7734. @d zero_token=other_token+"0" {zero, the smallest digit}
  7735. @d A_token=letter_token+"A" {the smallest special hex digit}
  7736. @d other_A_token=other_token+"A" {special hex digit of type |other_char|}
  7737. @<Accumulate the constant...@>=
  7738. loop@+ begin if (cur_tok<zero_token+radix)and(cur_tok>=zero_token)and
  7739. (cur_tok<=zero_token+9) then d:=cur_tok-zero_token
  7740. else if radix=16 then
  7741. if (cur_tok<=A_token+5)and(cur_tok>=A_token) then d:=cur_tok-A_token+10
  7742. else if (cur_tok<=other_A_token+5)and(cur_tok>=other_A_token) then
  7743. d:=cur_tok-other_A_token+10
  7744. else goto done
  7745. else goto done;
  7746. vacuous:=false;
  7747. if (cur_val>=m)and((cur_val>m)or(d>7)or(radix<>10)) then
  7748. begin if OK_so_far then
  7749. begin print_err("Number too big");
  7750. @.Number too big@>
  7751. help2("I can only go up to 2147483647='17777777777=""7FFFFFFF,")@/
  7752. ("so I'm using that number instead of yours.");
  7753. error; cur_val:=infinity; OK_so_far:=false;
  7754. end;
  7755. end
  7756. else cur_val:=cur_val*radix+d;
  7757. get_x_token;
  7758. end;
  7759. done:
  7760. @ @<Express astonishment...@>=
  7761. begin print_err("Missing number, treated as zero");
  7762. @.Missing number...@>
  7763. help3("A number should have been here; I inserted `0'.")@/
  7764. ("(If you can't figure out why I needed to see a number,")@/
  7765. ("look up `weird error' in the index to The TeXbook.)");
  7766. @:TeXbook}{\sl The \TeX book@>
  7767. back_error;
  7768. end
  7769. @ The |scan_dimen| routine is similar to |scan_int|, but it sets |cur_val| to
  7770. a |scaled| value, i.e., an integral number of sp. One of its main tasks
  7771. is therefore to interpret the abbreviations for various kinds of units and
  7772. to convert measurements to scaled points.
  7773. There are three parameters: |mu| is |true| if the finite units must be
  7774. `\.{mu}', while |mu| is |false| if `\.{mu}' units are disallowed;
  7775. |inf| is |true| if the infinite units `\.{fil}', `\.{fill}', `\.{filll}'
  7776. are permitted; and |shortcut| is |true| if |cur_val| already contains
  7777. an integer and only the units need to be considered.
  7778. The order of infinity that was found in the case of infinite glue is returned
  7779. in the global variable |cur_order|.
  7780. @<Glob...@>=
  7781. @!cur_order:glue_ord; {order of infinity found by |scan_dimen|}
  7782. @ Constructions like `\.{-\'77 pt}' are legal dimensions, so |scan_dimen|
  7783. may begin with |scan_int|. This explains why it is convenient to use
  7784. |scan_int| also for the integer part of a decimal fraction.
  7785. Several branches of |scan_dimen| work with |cur_val| as an integer and
  7786. with an auxiliary fraction |f|, so that the actual quantity of interest is
  7787. $|cur_val|+|f|/2^{16}$. At the end of the routine, this ``unpacked''
  7788. representation is put into the single word |cur_val|, which suddenly
  7789. switches significance from |integer| to |scaled|.
  7790. @d attach_fraction=88 {go here to pack |cur_val| and |f| into |cur_val|}
  7791. @d attach_sign=89 {go here when |cur_val| is correct except perhaps for sign}
  7792. @d scan_normal_dimen==scan_dimen(false,false,false)
  7793. @p procedure scan_dimen(@!mu,@!inf,@!shortcut:boolean);
  7794. {sets |cur_val| to a dimension}
  7795. label done, done1, done2, found, not_found, attach_fraction, attach_sign;
  7796. var negative:boolean; {should the answer be negated?}
  7797. @!f:integer; {numerator of a fraction whose denominator is $2^{16}$}
  7798. @<Local variables for dimension calculations@>@;
  7799. begin f:=0; arith_error:=false; cur_order:=normal; negative:=false;
  7800. if not shortcut then
  7801. begin @<Get the next non-blank non-sign...@>;
  7802. if (cur_cmd>=min_internal)and(cur_cmd<=max_internal) then
  7803. @<Fetch an internal dimension and |goto attach_sign|,
  7804. or fetch an internal integer@>
  7805. else begin back_input;
  7806. if cur_tok=continental_point_token then cur_tok:=point_token;
  7807. if cur_tok<>point_token then scan_int
  7808. else begin radix:=10; cur_val:=0;
  7809. end;
  7810. if cur_tok=continental_point_token then cur_tok:=point_token;
  7811. if (radix=10)and(cur_tok=point_token) then @<Scan decimal fraction@>;
  7812. end;
  7813. end;
  7814. if cur_val<0 then {in this case |f=0|}
  7815. begin negative := not negative; negate(cur_val);
  7816. end;
  7817. @<Scan units and set |cur_val| to $x\cdot(|cur_val|+f/2^{16})$, where there
  7818. are |x| sp per unit; |goto attach_sign| if the units are internal@>;
  7819. @<Scan an optional space@>;
  7820. attach_sign: if arith_error or(abs(cur_val)>=@'10000000000) then
  7821. @<Report that this dimension is out of range@>;
  7822. if negative then negate(cur_val);
  7823. end;
  7824. @ @<Fetch an internal dimension and |goto attach_sign|...@>=
  7825. if mu then
  7826. begin scan_something_internal(mu_val,false);
  7827. @<Coerce glue to a dimension@>;
  7828. if cur_val_level=mu_val then goto attach_sign;
  7829. if cur_val_level<>int_val then mu_error;
  7830. end
  7831. else begin scan_something_internal(dimen_val,false);
  7832. if cur_val_level=dimen_val then goto attach_sign;
  7833. end
  7834. @ @<Local variables for dimension calculations@>=
  7835. @!num,@!denom:1..65536; {conversion ratio for the scanned units}
  7836. @!k,@!kk:small_number; {number of digits in a decimal fraction}
  7837. @!p,@!q:pointer; {top of decimal digit stack}
  7838. @!v:scaled; {an internal dimension}
  7839. @!save_cur_val:integer; {temporary storage of |cur_val|}
  7840. @ The following code is executed when |scan_something_internal| was
  7841. called asking for |mu_val|, when we really wanted a ``mudimen'' instead
  7842. of ``muglue.''
  7843. @<Coerce glue to a dimension@>=
  7844. if cur_val_level>=glue_val then
  7845. begin v:=width(cur_val); delete_glue_ref(cur_val); cur_val:=v;
  7846. end
  7847. @ When the following code is executed, we have |cur_tok=point_token|, but this
  7848. token has been backed up using |back_input|; we must first discard it.
  7849. It turns out that a decimal point all by itself is equivalent to `\.{0.0}'.
  7850. Let's hope people don't use that fact.
  7851. @<Scan decimal fraction@>=
  7852. begin k:=0; p:=null; get_token; {|point_token| is being re-scanned}
  7853. loop@+ begin get_x_token;
  7854. if (cur_tok>zero_token+9)or(cur_tok<zero_token) then goto done1;
  7855. if k<17 then {digits for |k>=17| cannot affect the result}
  7856. begin q:=get_avail; link(q):=p; info(q):=cur_tok-zero_token;
  7857. p:=q; incr(k);
  7858. end;
  7859. end;
  7860. done1: for kk:=k downto 1 do
  7861. begin dig[kk-1]:=info(p); q:=p; p:=link(p); free_avail(q);
  7862. end;
  7863. f:=round_decimals(k);
  7864. if cur_cmd<>spacer then back_input;
  7865. end
  7866. @ Now comes the harder part: At this point in the program, |cur_val| is a
  7867. nonnegative integer and $f/2^{16}$ is a nonnegative fraction less than 1;
  7868. we want to multiply the sum of these two quantities by the appropriate
  7869. factor, based on the specified units, in order to produce a |scaled|
  7870. result, and we want to do the calculation with fixed point arithmetic that
  7871. does not overflow.
  7872. @<Scan units and set |cur_val| to $x\cdot(|cur_val|+f/2^{16})$...@>=
  7873. if inf then @<Scan for \(f)\.{fil} units; |goto attach_fraction| if found@>;
  7874. @<Scan for \(u)units that are internal dimensions;
  7875. |goto attach_sign| with |cur_val| set if found@>;
  7876. if mu then @<Scan for \(m)\.{mu} units and |goto attach_fraction|@>;
  7877. if scan_keyword("true") then @<Adjust \(f)for the magnification ratio@>;
  7878. @.true@>
  7879. if scan_keyword("pt") then goto attach_fraction; {the easy case}
  7880. @.pt@>
  7881. @<Scan for \(a)all other units and adjust |cur_val| and |f| accordingly;
  7882. |goto done| in the case of scaled points@>;
  7883. attach_fraction: if cur_val>=@'40000 then arith_error:=true
  7884. else cur_val:=cur_val*unity+f;
  7885. done:
  7886. @ A specification like `\.{filllll}' or `\.{fill L L L}' will lead to two
  7887. error messages (one for each additional keyword \.{"l"}).
  7888. @<Scan for \(f)\.{fil} units...@>=
  7889. if scan_keyword("fil") then
  7890. @.fil@>
  7891. begin cur_order:=fil;
  7892. while scan_keyword("l") do
  7893. begin if cur_order=filll then
  7894. begin print_err("Illegal unit of measure (");
  7895. @.Illegal unit of measure@>
  7896. print("replaced by filll)");
  7897. help1("I dddon't go any higher than filll."); error;
  7898. end
  7899. else incr(cur_order);
  7900. end;
  7901. goto attach_fraction;
  7902. end
  7903. @ @<Scan for \(u)units that are internal dimensions...@>=
  7904. save_cur_val:=cur_val;
  7905. @<Get the next non-blank non-call...@>;
  7906. if (cur_cmd<min_internal)or(cur_cmd>max_internal) then back_input
  7907. else begin if mu then
  7908. begin scan_something_internal(mu_val,false); @<Coerce glue...@>;
  7909. if cur_val_level<>mu_val then mu_error;
  7910. end
  7911. else scan_something_internal(dimen_val,false);
  7912. v:=cur_val; goto found;
  7913. end;
  7914. if mu then goto not_found;
  7915. if scan_keyword("em") then v:=(@<The em width for |cur_font|@>)
  7916. @.em@>
  7917. else if scan_keyword("ex") then v:=(@<The x-height for |cur_font|@>)
  7918. @.ex@>
  7919. else goto not_found;
  7920. @<Scan an optional space@>;
  7921. found:cur_val:=nx_plus_y(save_cur_val,v,xn_over_d(v,f,@'200000));
  7922. goto attach_sign;
  7923. not_found:
  7924. @ @<Scan for \(m)\.{mu} units and |goto attach_fraction|@>=
  7925. if scan_keyword("mu") then goto attach_fraction
  7926. @.mu@>
  7927. else begin print_err("Illegal unit of measure ("); print("mu inserted)");
  7928. @.Illegal unit of measure@>
  7929. help4("The unit of measurement in math glue must be mu.")@/
  7930. ("To recover gracefully from this error, it's best to")@/
  7931. ("delete the erroneous units; e.g., type `2' to delete")@/
  7932. ("two letters. (See Chapter 27 of The TeXbook.)");
  7933. @:TeXbook}{\sl The \TeX book@>
  7934. error; goto attach_fraction;
  7935. end
  7936. @ @<Adjust \(f)for the magnification ratio@>=
  7937. begin prepare_mag;
  7938. if mag<>1000 then
  7939. begin cur_val:=xn_over_d(cur_val,1000,mag);
  7940. f:=(1000*f+@'200000*remainder) div mag;
  7941. cur_val:=cur_val+(f div @'200000); f:=f mod @'200000;
  7942. end;
  7943. end
  7944. @ The necessary conversion factors can all be specified exactly as
  7945. fractions whose numerator and denominator sum to 32768 or less.
  7946. According to the definitions here, $\rm2660\,dd\approx1000.33297\,mm$;
  7947. this agrees well with the value $\rm1000.333\,mm$ cited by Bosshard
  7948. @^Bosshard, Hans Rudolf@>
  7949. in {\sl Technische Grundlagen zur Satzherstellung\/} (Bern, 1980).
  7950. @d set_conversion_end(#)== denom:=#; end
  7951. @d set_conversion(#)==@+begin num:=#; set_conversion_end
  7952. @<Scan for \(a)all other units and adjust |cur_val| and |f|...@>=
  7953. if scan_keyword("in") then set_conversion(7227)(100)
  7954. @.in@>
  7955. else if scan_keyword("pc") then set_conversion(12)(1)
  7956. @.pc@>
  7957. else if scan_keyword("cm") then set_conversion(7227)(254)
  7958. @.cm@>
  7959. else if scan_keyword("mm") then set_conversion(7227)(2540)
  7960. @.mm@>
  7961. else if scan_keyword("bp") then set_conversion(7227)(7200)
  7962. @.bp@>
  7963. else if scan_keyword("dd") then set_conversion(1238)(1157)
  7964. @.dd@>
  7965. else if scan_keyword("cc") then set_conversion(14856)(1157)
  7966. @.cc@>
  7967. else if scan_keyword("sp") then goto done
  7968. @.sp@>
  7969. else @<Complain about unknown unit and |goto done2|@>;
  7970. cur_val:=xn_over_d(cur_val,num,denom);
  7971. f:=(num*f+@'200000*remainder) div denom;@/
  7972. cur_val:=cur_val+(f div @'200000); f:=f mod @'200000;
  7973. done2:
  7974. @ @<Complain about unknown unit...@>=
  7975. begin print_err("Illegal unit of measure ("); print("pt inserted)");
  7976. @.Illegal unit of measure@>
  7977. help6("Dimensions can be in units of em, ex, in, pt, pc,")@/
  7978. ("cm, mm, dd, cc, bp, or sp; but yours is a new one!")@/
  7979. ("I'll assume that you meant to say pt, for printer's points.")@/
  7980. ("To recover gracefully from this error, it's best to")@/
  7981. ("delete the erroneous units; e.g., type `2' to delete")@/
  7982. ("two letters. (See Chapter 27 of The TeXbook.)");
  7983. @:TeXbook}{\sl The \TeX book@>
  7984. error; goto done2;
  7985. end
  7986. @ @<Report that this dimension is out of range@>=
  7987. begin print_err("Dimension too large");
  7988. @.Dimension too large@>
  7989. help2("I can't work with sizes bigger than about 19 feet.")@/
  7990. ("Continue and I'll use the largest value I can.");@/
  7991. error; cur_val:=max_dimen; arith_error:=false;
  7992. end
  7993. @ The final member of \TeX's value-scanning trio is |scan_glue|, which
  7994. makes |cur_val| point to a glue specification. The reference count of that
  7995. glue spec will take account of the fact that |cur_val| is pointing to~it.
  7996. The |level| parameter should be either |glue_val| or |mu_val|.
  7997. Since |scan_dimen| was so much more complex than |scan_int|, we might expect
  7998. |scan_glue| to be even worse. But fortunately, it is very simple, since
  7999. most of the work has already been done.
  8000. @p procedure scan_glue(@!level:small_number);
  8001. {sets |cur_val| to a glue spec pointer}
  8002. label exit;
  8003. var negative:boolean; {should the answer be negated?}
  8004. @!q:pointer; {new glue specification}
  8005. @!mu:boolean; {does |level=mu_val|?}
  8006. begin mu:=(level=mu_val); @<Get the next non-blank non-sign...@>;
  8007. if (cur_cmd>=min_internal)and(cur_cmd<=max_internal) then
  8008. begin scan_something_internal(level,negative);
  8009. if cur_val_level>=glue_val then
  8010. begin if cur_val_level<>level then mu_error;
  8011. return;
  8012. end;
  8013. if cur_val_level=int_val then scan_dimen(mu,false,true)
  8014. else if level=mu_val then mu_error;
  8015. end
  8016. else begin back_input; scan_dimen(mu,false,false);
  8017. if negative then negate(cur_val);
  8018. end;
  8019. @<Create a new glue specification whose width is |cur_val|; scan for its
  8020. stretch and shrink components@>;
  8021. exit:end;
  8022. @ @<Create a new glue specification whose width is |cur_val|...@>=
  8023. q:=new_spec(zero_glue); width(q):=cur_val;
  8024. if scan_keyword("plus") then
  8025. @.plus@>
  8026. begin scan_dimen(mu,true,false);
  8027. stretch(q):=cur_val; stretch_order(q):=cur_order;
  8028. end;
  8029. if scan_keyword("minus") then
  8030. @.minus@>
  8031. begin scan_dimen(mu,true,false);
  8032. shrink(q):=cur_val; shrink_order(q):=cur_order;
  8033. end;
  8034. cur_val:=q
  8035. @ Here's a similar procedure that returns a pointer to a rule node. This
  8036. routine is called just after \TeX\ has seen \.{\\hrule} or \.{\\vrule};
  8037. therefore |cur_cmd| will be either |hrule| or |vrule|. The idea is to store
  8038. the default rule dimensions in the node, then to override them if
  8039. `\.{height}' or `\.{width}' or `\.{depth}' specifications are
  8040. found (in any order).
  8041. @d default_rule=26214 {0.4\thinspace pt}
  8042. @p function scan_rule_spec:pointer;
  8043. label reswitch;
  8044. var q:pointer; {the rule node being created}
  8045. begin q:=new_rule; {|width|, |depth|, and |height| all equal |null_flag| now}
  8046. if cur_cmd=vrule then width(q):=default_rule
  8047. else begin height(q):=default_rule; depth(q):=0;
  8048. end;
  8049. reswitch: if scan_keyword("width") then
  8050. @.width@>
  8051. begin scan_normal_dimen; width(q):=cur_val; goto reswitch;
  8052. end;
  8053. if scan_keyword("height") then
  8054. @.height@>
  8055. begin scan_normal_dimen; height(q):=cur_val; goto reswitch;
  8056. end;
  8057. if scan_keyword("depth") then
  8058. @.depth@>
  8059. begin scan_normal_dimen; depth(q):=cur_val; goto reswitch;
  8060. end;
  8061. scan_rule_spec:=q;
  8062. end;
  8063. @* \[27] Building token lists.
  8064. The token lists for macros and for other things like \.{\\mark} and \.{\\output}
  8065. and \.{\\write} are produced by a procedure called |scan_toks|.
  8066. Before we get into the details of |scan_toks|, let's consider a much
  8067. simpler task, that of converting the current string into a token list.
  8068. The |str_toks| function does this; it classifies spaces as type |spacer|
  8069. and everything else as type |other_char|.
  8070. The token list created by |str_toks| begins at |link(temp_head)| and ends
  8071. at the value |p| that is returned. (If |p=temp_head|, the list is empty.)
  8072. @p function str_toks(@!b:pool_pointer):pointer;
  8073. {converts |str_pool[b..pool_ptr-1]| to a token list}
  8074. var p:pointer; {tail of the token list}
  8075. @!q:pointer; {new node being added to the token list via |store_new_token|}
  8076. @!t:halfword; {token being appended}
  8077. @!k:pool_pointer; {index into |str_pool|}
  8078. begin str_room(1);
  8079. p:=temp_head; link(p):=null; k:=b;
  8080. while k<pool_ptr do
  8081. begin t:=so(str_pool[k]);
  8082. if t=" " then t:=space_token
  8083. else t:=other_token+t;
  8084. fast_store_new_token(t);
  8085. incr(k);
  8086. end;
  8087. pool_ptr:=b; str_toks:=p;
  8088. end;
  8089. @ The main reason for wanting |str_toks| is the next function,
  8090. |the_toks|, which has similar input/output characteristics.
  8091. This procedure is supposed to scan something like `\.{\\skip\\count12}',
  8092. i.e., whatever can follow `\.{\\the}', and it constructs a token list
  8093. containing something like `\.{-3.0pt minus 0.5fill}'.
  8094. @p function the_toks:pointer;
  8095. var old_setting:0..max_selector; {holds |selector| setting}
  8096. @!p,@!q,@!r:pointer; {used for copying a token list}
  8097. @!b:pool_pointer; {base of temporary string}
  8098. begin get_x_token; scan_something_internal(tok_val,false);
  8099. if cur_val_level>=ident_val then @<Copy the token list@>
  8100. else begin old_setting:=selector; selector:=new_string; b:=pool_ptr;
  8101. case cur_val_level of
  8102. int_val:print_int(cur_val);
  8103. dimen_val:begin print_scaled(cur_val); print("pt");
  8104. end;
  8105. glue_val: begin print_spec(cur_val,"pt"); delete_glue_ref(cur_val);
  8106. end;
  8107. mu_val: begin print_spec(cur_val,"mu"); delete_glue_ref(cur_val);
  8108. end;
  8109. end; {there are no other cases}
  8110. selector:=old_setting; the_toks:=str_toks(b);
  8111. end;
  8112. end;
  8113. @ @<Copy the token list@>=
  8114. begin p:=temp_head; link(p):=null;
  8115. if cur_val_level=ident_val then store_new_token(cs_token_flag+cur_val)
  8116. else if cur_val<>null then
  8117. begin r:=link(cur_val); {do not copy the reference count}
  8118. while r<>null do
  8119. begin fast_store_new_token(info(r)); r:=link(r);
  8120. end;
  8121. end;
  8122. the_toks:=p;
  8123. end
  8124. @ Here's part of the |expand| subroutine that we are now ready to complete:
  8125. @p procedure ins_the_toks;
  8126. begin link(garbage):=the_toks; ins_list(link(temp_head));
  8127. end;
  8128. @ The primitives \.{\\number}, \.{\\romannumeral}, \.{\\string}, \.{\\meaning},
  8129. \.{\\fontname}, and \.{\\jobname} are defined as follows.
  8130. @d number_code=0 {command code for \.{\\number}}
  8131. @d roman_numeral_code=1 {command code for \.{\\romannumeral}}
  8132. @d string_code=2 {command code for \.{\\string}}
  8133. @d meaning_code=3 {command code for \.{\\meaning}}
  8134. @d font_name_code=4 {command code for \.{\\fontname}}
  8135. @d job_name_code=5 {command code for \.{\\jobname}}
  8136. @<Put each...@>=
  8137. primitive("number",convert,number_code);@/
  8138. @!@:number_}{\.{\\number} primitive@>
  8139. primitive("romannumeral",convert,roman_numeral_code);@/
  8140. @!@:roman_numeral_}{\.{\\romannumeral} primitive@>
  8141. primitive("string",convert,string_code);@/
  8142. @!@:string_}{\.{\\string} primitive@>
  8143. primitive("meaning",convert,meaning_code);@/
  8144. @!@:meaning_}{\.{\\meaning} primitive@>
  8145. primitive("fontname",convert,font_name_code);@/
  8146. @!@:font_name_}{\.{\\fontname} primitive@>
  8147. primitive("jobname",convert,job_name_code);@/
  8148. @!@:job_name_}{\.{\\jobname} primitive@>
  8149. @ @<Cases of |print_cmd_chr|...@>=
  8150. convert: case chr_code of
  8151. number_code: print_esc("number");
  8152. roman_numeral_code: print_esc("romannumeral");
  8153. string_code: print_esc("string");
  8154. meaning_code: print_esc("meaning");
  8155. font_name_code: print_esc("fontname");
  8156. othercases print_esc("jobname")
  8157. endcases;
  8158. @ The procedure |conv_toks| uses |str_toks| to insert the token list
  8159. for |convert| functions into the scanner; `\.{\\outer}' control sequences
  8160. are allowed to follow `\.{\\string}' and `\.{\\meaning}'.
  8161. @p procedure conv_toks;
  8162. var old_setting:0..max_selector; {holds |selector| setting}
  8163. @!c:number_code..job_name_code; {desired type of conversion}
  8164. @!save_scanner_status:small_number; {|scanner_status| upon entry}
  8165. @!b:pool_pointer; {base of temporary string}
  8166. begin c:=cur_chr; @<Scan the argument for command |c|@>;
  8167. old_setting:=selector; selector:=new_string; b:=pool_ptr;
  8168. @<Print the result of command |c|@>;
  8169. selector:=old_setting; link(garbage):=str_toks(b); ins_list(link(temp_head));
  8170. end;
  8171. @ @<Scan the argument for command |c|@>=
  8172. case c of
  8173. number_code,roman_numeral_code: scan_int;
  8174. string_code, meaning_code: begin save_scanner_status:=scanner_status;
  8175. scanner_status:=normal; get_token; scanner_status:=save_scanner_status;
  8176. end;
  8177. font_name_code: scan_font_ident;
  8178. job_name_code: if job_name=0 then open_log_file;
  8179. end {there are no other cases}
  8180. @ @<Print the result of command |c|@>=
  8181. case c of
  8182. number_code: print_int(cur_val);
  8183. roman_numeral_code: print_roman_int(cur_val);
  8184. string_code:if cur_cs<>0 then sprint_cs(cur_cs)
  8185. else print_char(cur_chr);
  8186. meaning_code: print_meaning;
  8187. font_name_code: begin print(font_name[cur_val]);
  8188. if font_size[cur_val]<>font_dsize[cur_val] then
  8189. begin print(" at "); print_scaled(font_size[cur_val]);
  8190. print("pt");
  8191. end;
  8192. end;
  8193. job_name_code: print(job_name);
  8194. end {there are no other cases}
  8195. @ Now we can't postpone the difficulties any longer; we must bravely tackle
  8196. |scan_toks|. This function returns a pointer to the tail of a new token
  8197. list, and it also makes |def_ref| point to the reference count at the
  8198. head of that list.
  8199. There are two boolean parameters, |macro_def| and |xpand|. If |macro_def|
  8200. is true, the goal is to create the token list for a macro definition;
  8201. otherwise the goal is to create the token list for some other \TeX\
  8202. primitive: \.{\\mark}, \.{\\output}, \.{\\everypar}, \.{\\lowercase},
  8203. \.{\\uppercase}, \.{\\message}, \.{\\errmessage}, \.{\\write}, or
  8204. \.{\\special}. In the latter cases a left brace must be scanned next; this
  8205. left brace will not be part of the token list, nor will the matching right
  8206. brace that comes at the end. If |xpand| is false, the token list will
  8207. simply be copied from the input using |get_token|. Otherwise all expandable
  8208. tokens will be expanded until unexpandable tokens are left, except that
  8209. the results of expanding `\.{\\the}' are not expanded further.
  8210. If both |macro_def| and |xpand| are true, the expansion applies
  8211. only to the macro body (i.e., to the material following the first
  8212. |left_brace| character).
  8213. The value of |cur_cs| when |scan_toks| begins should be the |eqtb|
  8214. address of the control sequence to display in ``runaway'' error
  8215. messages.
  8216. @p function scan_toks(@!macro_def,@!xpand:boolean):pointer;
  8217. label found,continue,done,done1,done2;
  8218. var t:halfword; {token representing the highest parameter number}
  8219. @!s:halfword; {saved token}
  8220. @!p:pointer; {tail of the token list being built}
  8221. @!q:pointer; {new node being added to the token list via |store_new_token|}
  8222. @!unbalance:halfword; {number of unmatched left braces}
  8223. @!hash_brace:halfword; {possible `\.{\#\{}' token}
  8224. begin if macro_def then scanner_status:=defining
  8225. @+else scanner_status:=absorbing;
  8226. warning_index:=cur_cs; def_ref:=get_avail; token_ref_count(def_ref):=null;
  8227. p:=def_ref; hash_brace:=0; t:=zero_token;
  8228. if macro_def then @<Scan and build the parameter part of the macro definition@>
  8229. else scan_left_brace; {remove the compulsory left brace}
  8230. @<Scan and build the body of the token list; |goto found| when finished@>;
  8231. found: scanner_status:=normal;
  8232. if hash_brace<>0 then store_new_token(hash_brace);
  8233. scan_toks:=p;
  8234. end;
  8235. @ @<Scan and build the parameter part...@>=
  8236. begin loop begin continue: get_token; {set |cur_cmd|, |cur_chr|, |cur_tok|}
  8237. if cur_tok<right_brace_limit then goto done1;
  8238. if cur_cmd=mac_param then
  8239. @<If the next character is a parameter number, make |cur_tok|
  8240. a |match| token; but if it is a left brace, store
  8241. `|left_brace|, |end_match|', set |hash_brace|, and |goto done|@>;
  8242. store_new_token(cur_tok);
  8243. end;
  8244. done1: store_new_token(end_match_token);
  8245. if cur_cmd=right_brace then
  8246. @<Express shock at the missing left brace; |goto found|@>;
  8247. done: end
  8248. @ @<Express shock...@>=
  8249. begin print_err("Missing { inserted"); incr(align_state);
  8250. @.Missing \{ inserted@>
  8251. help2("Where was the left brace? You said something like `\def\a}',")@/
  8252. ("which I'm going to interpret as `\def\a{}'."); error; goto found;
  8253. end
  8254. @ @<If the next character is a parameter number...@>=
  8255. begin s:=match_token+cur_chr; get_token;
  8256. if cur_tok<left_brace_limit then
  8257. begin hash_brace:=cur_tok;
  8258. store_new_token(cur_tok); store_new_token(end_match_token);
  8259. goto done;
  8260. end;
  8261. if t=zero_token+9 then
  8262. begin print_err("You already have nine parameters");
  8263. @.You already have nine...@>
  8264. help2("I'm going to ignore the # sign you just used,")@/
  8265. ("as well as the token that followed it."); error; goto continue;
  8266. end
  8267. else begin incr(t);
  8268. if cur_tok<>t then
  8269. begin print_err("Parameters must be numbered consecutively");
  8270. @.Parameters...consecutively@>
  8271. help2("I've inserted the digit you should have used after the #.")@/
  8272. ("Type `1' to delete what you did use."); back_error;
  8273. end;
  8274. cur_tok:=s;
  8275. end;
  8276. end
  8277. @ @<Scan and build the body of the token list; |goto found| when finished@>=
  8278. unbalance:=1;
  8279. loop@+ begin if xpand then @<Expand the next part of the input@>
  8280. else get_token;
  8281. if cur_tok<right_brace_limit then
  8282. if cur_cmd<right_brace then incr(unbalance)
  8283. else begin decr(unbalance);
  8284. if unbalance=0 then goto found;
  8285. end
  8286. else if cur_cmd=mac_param then
  8287. if macro_def then @<Look for parameter number or \.{\#\#}@>;
  8288. store_new_token(cur_tok);
  8289. end
  8290. @ Here we insert an entire token list created by |the_toks| without
  8291. expanding it further.
  8292. @<Expand the next part of the input@>=
  8293. begin loop begin get_next;
  8294. if cur_cmd<=max_command then goto done2;
  8295. if cur_cmd<>the then expand
  8296. else begin q:=the_toks;
  8297. if link(temp_head)<>null then
  8298. begin link(p):=link(temp_head); p:=q;
  8299. end;
  8300. end;
  8301. end;
  8302. done2: x_token
  8303. end
  8304. @ @<Look for parameter number...@>=
  8305. begin s:=cur_tok;
  8306. if xpand then get_x_token else get_token;
  8307. if cur_cmd<>mac_param then
  8308. if (cur_tok<=zero_token)or(cur_tok>t) then
  8309. begin print_err("Illegal parameter number in definition of ");
  8310. @.Illegal parameter number...@>
  8311. sprint_cs(warning_index);
  8312. help3("You meant to type ## instead of #, right?")@/
  8313. ("Or maybe a } was forgotten somewhere earlier, and things")@/
  8314. ("are all screwed up? I'm going to assume that you meant ##.");
  8315. back_error; cur_tok:=s;
  8316. end
  8317. else cur_tok:=out_param_token-"0"+cur_chr;
  8318. end
  8319. @ Another way to create a token list is via the \.{\\read} command. The
  8320. sixteen files potentially usable for reading appear in the following
  8321. global variables. The value of |read_open[n]| will be |closed| if
  8322. stream number |n| has not been opened or if it has been fully read;
  8323. |just_open| if an \.{\\openin} but not a \.{\\read} has been done;
  8324. and |normal| if it is open and ready to read the next line.
  8325. @d closed=2 {not open, or at end of file}
  8326. @d just_open=1 {newly opened, first line not yet read}
  8327. @<Glob...@>=
  8328. @!read_file:array[0..15] of alpha_file; {used for \.{\\read}}
  8329. @!read_open:array[0..16] of normal..closed; {state of |read_file[n]|}
  8330. @ @<Set init...@>=
  8331. for k:=0 to 16 do read_open[k]:=closed;
  8332. @ The |read_toks| procedure constructs a token list like that for any
  8333. macro definition, and makes |cur_val| point to it. Parameter |r| points
  8334. to the control sequence that will receive this token list.
  8335. @p procedure read_toks(@!n:integer;@!r:pointer);
  8336. label done;
  8337. var p:pointer; {tail of the token list}
  8338. @!q:pointer; {new node being added to the token list via |store_new_token|}
  8339. @!s:integer; {saved value of |align_state|}
  8340. @!m:small_number; {stream number}
  8341. begin scanner_status:=defining; warning_index:=r;
  8342. def_ref:=get_avail; token_ref_count(def_ref):=null;
  8343. p:=def_ref; {the reference count}
  8344. store_new_token(end_match_token);
  8345. if (n<0)or(n>15) then m:=16@+else m:=n;
  8346. s:=align_state; align_state:=1000000; {disable tab marks, etc.}
  8347. repeat @<Input and store tokens from the next line of the file@>;
  8348. until align_state=1000000;
  8349. cur_val:=def_ref; scanner_status:=normal; align_state:=s;
  8350. end;
  8351. @ @<Input and store tokens from the next line of the file@>=
  8352. begin_file_reading; name:=m+1;
  8353. if read_open[m]=closed then @<Input for \.{\\read} from the terminal@>
  8354. else if read_open[m]=just_open then @<Input the first line of |read_file[m]|@>
  8355. else @<Input the next line of |read_file[m]|@>;
  8356. limit:=last;
  8357. if end_line_char_inactive then decr(limit)
  8358. else buffer[limit]:=end_line_char;
  8359. first:=limit+1; loc:=start; state:=new_line;@/
  8360. loop@+ begin get_token;
  8361. if cur_tok=0 then goto done;
  8362. {|cur_cmd=cur_chr=0| will occur at the end of the line}
  8363. if align_state<1000000 then {unmatched `\.\}' aborts the line}
  8364. begin repeat get_token; until cur_tok=0;
  8365. align_state:=1000000; goto done;
  8366. end;
  8367. store_new_token(cur_tok);
  8368. end;
  8369. done: end_file_reading
  8370. @ Here we input on-line into the |buffer| array, prompting the user explicitly
  8371. if |n>=0|. The value of |n| is set negative so that additional prompts
  8372. will not be given in the case of multi-line input.
  8373. @<Input for \.{\\read} from the terminal@>=
  8374. if interaction>nonstop_mode then
  8375. if n<0 then prompt_input("")
  8376. else begin wake_up_terminal;
  8377. print_ln; sprint_cs(r); prompt_input("="); n:=-1;
  8378. end
  8379. else fatal_error("*** (cannot \read from terminal in nonstop modes)")
  8380. @.cannot \\read@>
  8381. @ The first line of a file must be treated specially, since |input_ln|
  8382. must be told not to start with |get|.
  8383. @^system dependencies@>
  8384. @<Input the first line of |read_file[m]|@>=
  8385. if input_ln(read_file[m],false) then read_open[m]:=normal
  8386. else begin a_close(read_file[m]); read_open[m]:=closed;
  8387. end
  8388. @ An empty line is appended at the end of a |read_file|.
  8389. @^empty line at end of file@>
  8390. @<Input the next line of |read_file[m]|@>=
  8391. begin if not input_ln(read_file[m],true) then
  8392. begin a_close(read_file[m]); read_open[m]:=closed;
  8393. if align_state<>1000000 then
  8394. begin runaway;
  8395. print_err("File ended within "); print_esc("read");
  8396. @.File ended within \\read@>
  8397. help1("This \read has unbalanced braces.");
  8398. align_state:=1000000; limit:=0; error;
  8399. end;
  8400. end;
  8401. end
  8402. @* \[28] Conditional processing.
  8403. We consider now the way \TeX\ handles various kinds of \.{\\if} commands.
  8404. @d if_char_code=0 { `\.{\\if}' }
  8405. @d if_cat_code=1 { `\.{\\ifcat}' }
  8406. @d if_int_code=2 { `\.{\\ifnum}' }
  8407. @d if_dim_code=3 { `\.{\\ifdim}' }
  8408. @d if_odd_code=4 { `\.{\\ifodd}' }
  8409. @d if_vmode_code=5 { `\.{\\ifvmode}' }
  8410. @d if_hmode_code=6 { `\.{\\ifhmode}' }
  8411. @d if_mmode_code=7 { `\.{\\ifmmode}' }
  8412. @d if_inner_code=8 { `\.{\\ifinner}' }
  8413. @d if_void_code=9 { `\.{\\ifvoid}' }
  8414. @d if_hbox_code=10 { `\.{\\ifhbox}' }
  8415. @d if_vbox_code=11 { `\.{\\ifvbox}' }
  8416. @d ifx_code=12 { `\.{\\ifx}' }
  8417. @d if_eof_code=13 { `\.{\\ifeof}' }
  8418. @d if_true_code=14 { `\.{\\iftrue}' }
  8419. @d if_false_code=15 { `\.{\\iffalse}' }
  8420. @d if_case_code=16 { `\.{\\ifcase}' }
  8421. @<Put each...@>=
  8422. primitive("if",if_test,if_char_code);
  8423. @!@:if_char_}{\.{\\if} primitive@>
  8424. primitive("ifcat",if_test,if_cat_code);
  8425. @!@:if_cat_code_}{\.{\\ifcat} primitive@>
  8426. primitive("ifnum",if_test,if_int_code);
  8427. @!@:if_int_}{\.{\\ifnum} primitive@>
  8428. primitive("ifdim",if_test,if_dim_code);
  8429. @!@:if_dim_}{\.{\\ifdim} primitive@>
  8430. primitive("ifodd",if_test,if_odd_code);
  8431. @!@:if_odd_}{\.{\\ifodd} primitive@>
  8432. primitive("ifvmode",if_test,if_vmode_code);
  8433. @!@:if_vmode_}{\.{\\ifvmode} primitive@>
  8434. primitive("ifhmode",if_test,if_hmode_code);
  8435. @!@:if_hmode_}{\.{\\ifhmode} primitive@>
  8436. primitive("ifmmode",if_test,if_mmode_code);
  8437. @!@:if_mmode_}{\.{\\ifmmode} primitive@>
  8438. primitive("ifinner",if_test,if_inner_code);
  8439. @!@:if_inner_}{\.{\\ifinner} primitive@>
  8440. primitive("ifvoid",if_test,if_void_code);
  8441. @!@:if_void_}{\.{\\ifvoid} primitive@>
  8442. primitive("ifhbox",if_test,if_hbox_code);
  8443. @!@:if_hbox_}{\.{\\ifhbox} primitive@>
  8444. primitive("ifvbox",if_test,if_vbox_code);
  8445. @!@:if_vbox_}{\.{\\ifvbox} primitive@>
  8446. primitive("ifx",if_test,ifx_code);
  8447. @!@:ifx_}{\.{\\ifx} primitive@>
  8448. primitive("ifeof",if_test,if_eof_code);
  8449. @!@:if_eof_}{\.{\\ifeof} primitive@>
  8450. primitive("iftrue",if_test,if_true_code);
  8451. @!@:if_true_}{\.{\\iftrue} primitive@>
  8452. primitive("iffalse",if_test,if_false_code);
  8453. @!@:if_false_}{\.{\\iffalse} primitive@>
  8454. primitive("ifcase",if_test,if_case_code);
  8455. @!@:if_case_}{\.{\\ifcase} primitive@>
  8456. @ @<Cases of |print_cmd_chr|...@>=
  8457. if_test: case chr_code of
  8458. if_cat_code:print_esc("ifcat");
  8459. if_int_code:print_esc("ifnum");
  8460. if_dim_code:print_esc("ifdim");
  8461. if_odd_code:print_esc("ifodd");
  8462. if_vmode_code:print_esc("ifvmode");
  8463. if_hmode_code:print_esc("ifhmode");
  8464. if_mmode_code:print_esc("ifmmode");
  8465. if_inner_code:print_esc("ifinner");
  8466. if_void_code:print_esc("ifvoid");
  8467. if_hbox_code:print_esc("ifhbox");
  8468. if_vbox_code:print_esc("ifvbox");
  8469. ifx_code:print_esc("ifx");
  8470. if_eof_code:print_esc("ifeof");
  8471. if_true_code:print_esc("iftrue");
  8472. if_false_code:print_esc("iffalse");
  8473. if_case_code:print_esc("ifcase");
  8474. othercases print_esc("if")
  8475. endcases;
  8476. @ Conditions can be inside conditions, and this nesting has a stack
  8477. that is independent of the |save_stack|.
  8478. Four global variables represent the top of the condition stack:
  8479. |cond_ptr| points to pushed-down entries, if any; |if_limit| specifies
  8480. the largest code of a |fi_or_else| command that is syntactically legal;
  8481. |cur_if| is the name of the current type of conditional; and |if_line|
  8482. is the line number at which it began.
  8483. If no conditions are currently in progress, the condition stack has the
  8484. special state |cond_ptr=null|, |if_limit=normal|, |cur_if=0|, |if_line=0|.
  8485. Otherwise |cond_ptr| points to a two-word node; the |type|, |subtype|, and
  8486. |link| fields of the first word contain |if_limit|, |cur_if|, and
  8487. |cond_ptr| at the next level, and the second word contains the
  8488. corresponding |if_line|.
  8489. @d if_node_size=2 {number of words in stack entry for conditionals}
  8490. @d if_line_field(#)==mem[#+1].int
  8491. @d if_code=1 {code for \.{\\if...} being evaluated}
  8492. @d fi_code=2 {code for \.{\\fi}}
  8493. @d else_code=3 {code for \.{\\else}}
  8494. @d or_code=4 {code for \.{\\or}}
  8495. @<Glob...@>=
  8496. @!cond_ptr:pointer; {top of the condition stack}
  8497. @!if_limit:normal..or_code; {upper bound on |fi_or_else| codes}
  8498. @!cur_if:small_number; {type of conditional being worked on}
  8499. @!if_line:integer; {line where that conditional began}
  8500. @ @<Set init...@>=
  8501. cond_ptr:=null; if_limit:=normal; cur_if:=0; if_line:=0;
  8502. @ @<Put each...@>=
  8503. primitive("fi",fi_or_else,fi_code);
  8504. @!@:fi_}{\.{\\fi} primitive@>
  8505. text(frozen_fi):="fi"; eqtb[frozen_fi]:=eqtb[cur_val];
  8506. primitive("or",fi_or_else,or_code);
  8507. @!@:or_}{\.{\\or} primitive@>
  8508. primitive("else",fi_or_else,else_code);
  8509. @!@:else_}{\.{\\else} primitive@>
  8510. @ @<Cases of |print_cmd_chr|...@>=
  8511. fi_or_else: if chr_code=fi_code then print_esc("fi")
  8512. else if chr_code=or_code then print_esc("or")
  8513. else print_esc("else");
  8514. @ When we skip conditional text, we keep track of the line number
  8515. where skipping began, for use in error messages.
  8516. @<Glob...@>=
  8517. @!skip_line:integer; {skipping began here}
  8518. @ Here is a procedure that ignores text until coming to an \.{\\or},
  8519. \.{\\else}, or \.{\\fi} at the current level of $\.{\\if}\ldots\.{\\fi}$
  8520. nesting. After it has acted, |cur_chr| will indicate the token that
  8521. was found, but |cur_tok| will not be set (because this makes the
  8522. procedure run faster).
  8523. @p procedure pass_text;
  8524. label done;
  8525. var l:integer; {level of $\.{\\if}\ldots\.{\\fi}$ nesting}
  8526. @!save_scanner_status:small_number; {|scanner_status| upon entry}
  8527. begin save_scanner_status:=scanner_status; scanner_status:=skipping; l:=0;
  8528. skip_line:=line;
  8529. loop@+ begin get_next;
  8530. if cur_cmd=fi_or_else then
  8531. begin if l=0 then goto done;
  8532. if cur_chr=fi_code then decr(l);
  8533. end
  8534. else if cur_cmd=if_test then incr(l);
  8535. end;
  8536. done: scanner_status:=save_scanner_status;
  8537. end;
  8538. @ When we begin to process a new \.{\\if}, we set |if_limit:=if_code|; then
  8539. if\/ \.{\\or} or \.{\\else} or \.{\\fi} occurs before the current \.{\\if}
  8540. condition has been evaluated, \.{\\relax} will be inserted.
  8541. For example, a sequence of commands like `\.{\\ifvoid1\\else...\\fi}'
  8542. would otherwise require something after the `\.1'.
  8543. @<Push the condition stack@>=
  8544. begin p:=get_node(if_node_size); link(p):=cond_ptr; type(p):=if_limit;
  8545. subtype(p):=cur_if; if_line_field(p):=if_line;
  8546. cond_ptr:=p; cur_if:=cur_chr; if_limit:=if_code; if_line:=line;
  8547. end
  8548. @ @<Pop the condition stack@>=
  8549. begin p:=cond_ptr; if_line:=if_line_field(p);
  8550. cur_if:=subtype(p); if_limit:=type(p); cond_ptr:=link(p);
  8551. free_node(p,if_node_size);
  8552. end
  8553. @ Here's a procedure that changes the |if_limit| code corresponding to
  8554. a given value of |cond_ptr|.
  8555. @p procedure change_if_limit(@!l:small_number;@!p:pointer);
  8556. label exit;
  8557. var q:pointer;
  8558. begin if p=cond_ptr then if_limit:=l {that's the easy case}
  8559. else begin q:=cond_ptr;
  8560. loop@+ begin if q=null then confusion("if");
  8561. @:this can't happen if}{\quad if@>
  8562. if link(q)=p then
  8563. begin type(q):=l; return;
  8564. end;
  8565. q:=link(q);
  8566. end;
  8567. end;
  8568. exit:end;
  8569. @ A condition is started when the |expand| procedure encounters
  8570. an |if_test| command; in that case |expand| reduces to |conditional|,
  8571. which is a recursive procedure.
  8572. @^recursion@>
  8573. @p procedure conditional;
  8574. label exit,common_ending;
  8575. var b:boolean; {is the condition true?}
  8576. @!r:"<"..">"; {relation to be evaluated}
  8577. @!m,@!n:integer; {to be tested against the second operand}
  8578. @!p,@!q:pointer; {for traversing token lists in \.{\\ifx} tests}
  8579. @!save_scanner_status:small_number; {|scanner_status| upon entry}
  8580. @!save_cond_ptr:pointer; {|cond_ptr| corresponding to this conditional}
  8581. @!this_if:small_number; {type of this conditional}
  8582. begin @<Push the condition stack@>;@+save_cond_ptr:=cond_ptr;this_if:=cur_chr;@/
  8583. @<Either process \.{\\ifcase} or set |b| to the value of a boolean condition@>;
  8584. if tracing_commands>1 then @<Display the value of |b|@>;
  8585. if b then
  8586. begin change_if_limit(else_code,save_cond_ptr);
  8587. return; {wait for \.{\\else} or \.{\\fi}}
  8588. end;
  8589. @<Skip to \.{\\else} or \.{\\fi}, then |goto common_ending|@>;
  8590. common_ending: if cur_chr=fi_code then @<Pop the condition stack@>
  8591. else if_limit:=fi_code; {wait for \.{\\fi}}
  8592. exit:end;
  8593. @ In a construction like `\.{\\if\\iftrue abc\\else d\\fi}', the first
  8594. \.{\\else} that we come to after learning that the \.{\\if} is false is
  8595. not the \.{\\else} we're looking for. Hence the following curious
  8596. logic is needed.
  8597. @ @<Skip to \.{\\else} or \.{\\fi}...@>=
  8598. loop@+ begin pass_text;
  8599. if cond_ptr=save_cond_ptr then
  8600. begin if cur_chr<>or_code then goto common_ending;
  8601. print_err("Extra "); print_esc("or");
  8602. @.Extra \\or@>
  8603. help1("I'm ignoring this; it doesn't match any \if.");
  8604. error;
  8605. end
  8606. else if cur_chr=fi_code then @<Pop the condition stack@>;
  8607. end
  8608. @ @<Either process \.{\\ifcase} or set |b|...@>=
  8609. case this_if of
  8610. if_char_code, if_cat_code: @<Test if two characters match@>;
  8611. if_int_code, if_dim_code: @<Test relation between integers or dimensions@>;
  8612. if_odd_code: @<Test if an integer is odd@>;
  8613. if_vmode_code: b:=(abs(mode)=vmode);
  8614. if_hmode_code: b:=(abs(mode)=hmode);
  8615. if_mmode_code: b:=(abs(mode)=mmode);
  8616. if_inner_code: b:=(mode<0);
  8617. if_void_code, if_hbox_code, if_vbox_code: @<Test box register status@>;
  8618. ifx_code: @<Test if two tokens match@>;
  8619. if_eof_code: begin scan_four_bit_int; b:=(read_open[cur_val]=closed);
  8620. end;
  8621. if_true_code: b:=true;
  8622. if_false_code: b:=false;
  8623. if_case_code: @<Select the appropriate case
  8624. and |return| or |goto common_ending|@>;
  8625. end {there are no other cases}
  8626. @ @<Display the value of |b|@>=
  8627. begin begin_diagnostic;
  8628. if b then print("{true}")@+else print("{false}");
  8629. end_diagnostic(false);
  8630. end
  8631. @ Here we use the fact that |"<"|, |"="|, and |">"| are consecutive ASCII
  8632. codes.
  8633. @^ASCII code@>
  8634. @<Test relation between integers or dimensions@>=
  8635. begin if this_if=if_int_code then scan_int@+else scan_normal_dimen;
  8636. n:=cur_val; @<Get the next non-blank non-call...@>;
  8637. if (cur_tok>=other_token+"<")and(cur_tok<=other_token+">") then
  8638. r:=cur_tok-other_token
  8639. else begin print_err("Missing = inserted for ");
  8640. @.Missing = inserted@>
  8641. print_cmd_chr(if_test,this_if);
  8642. help1("I was expecting to see `<', `=', or `>'. Didn't.");
  8643. back_error; r:="=";
  8644. end;
  8645. if this_if=if_int_code then scan_int@+else scan_normal_dimen;
  8646. case r of
  8647. "<": b:=(n<cur_val);
  8648. "=": b:=(n=cur_val);
  8649. ">": b:=(n>cur_val);
  8650. end;
  8651. end
  8652. @ @<Test if an integer is odd@>=
  8653. begin scan_int; b:=odd(cur_val);
  8654. end
  8655. @ @<Test box register status@>=
  8656. begin scan_eight_bit_int; p:=box(cur_val);
  8657. if this_if=if_void_code then b:=(p=null)
  8658. else if p=null then b:=false
  8659. else if this_if=if_hbox_code then b:=(type(p)=hlist_node)
  8660. else b:=(type(p)=vlist_node);
  8661. end
  8662. @ An active character will be treated as category 13 following
  8663. \.{\\if\\noexpand} or following \.{\\ifcat\\noexpand}. We use the fact that
  8664. active characters have the smallest tokens, among all control sequences.
  8665. @d get_x_token_or_active_char==@t@>@;
  8666. begin get_x_token;
  8667. if cur_cmd=relax then if cur_chr=no_expand_flag then
  8668. begin cur_cmd:=active_char;
  8669. cur_chr:=cur_tok-cs_token_flag-active_base;
  8670. end;
  8671. end
  8672. @<Test if two characters match@>=
  8673. begin get_x_token_or_active_char;
  8674. if (cur_cmd>active_char)or(cur_chr>255) then {not a character}
  8675. begin m:=relax; n:=256;
  8676. end
  8677. else begin m:=cur_cmd; n:=cur_chr;
  8678. end;
  8679. get_x_token_or_active_char;
  8680. if (cur_cmd>active_char)or(cur_chr>255) then
  8681. begin cur_cmd:=relax; cur_chr:=256;
  8682. end;
  8683. if this_if=if_char_code then b:=(n=cur_chr)@+else b:=(m=cur_cmd);
  8684. end
  8685. @ Note that `\.{\\ifx}' will declare two macros different if one is \\{long}
  8686. or \\{outer} and the other isn't, even though the texts of the macros are
  8687. the same.
  8688. We need to reset |scanner_status|, since \.{\\outer} control sequences
  8689. are allowed, but we might be scanning a macro definition or preamble.
  8690. @<Test if two tokens match@>=
  8691. begin save_scanner_status:=scanner_status; scanner_status:=normal;
  8692. get_next; n:=cur_cs; p:=cur_cmd; q:=cur_chr;
  8693. get_next; if cur_cmd<>p then b:=false
  8694. else if cur_cmd<call then b:=(cur_chr=q)
  8695. else @<Test if two macro texts match@>;
  8696. scanner_status:=save_scanner_status;
  8697. end
  8698. @ Note also that `\.{\\ifx}' decides that macros \.{\\a} and \.{\\b} are
  8699. different in examples like this:
  8700. $$\vbox{\halign{\.{#}\hfil&\qquad\.{#}\hfil\cr
  8701. {}\\def\\a\{\\c\}&
  8702. {}\\def\\c\{\}\cr
  8703. {}\\def\\b\{\\d\}&
  8704. {}\\def\\d\{\}\cr}}$$
  8705. @<Test if two macro texts match@>=
  8706. begin p:=link(cur_chr); q:=link(equiv(n)); {omit reference counts}
  8707. if p=q then b:=true
  8708. else begin while (p<>null)and(q<>null) do
  8709. if info(p)<>info(q) then p:=null
  8710. else begin p:=link(p); q:=link(q);
  8711. end;
  8712. b:=((p=null)and(q=null));
  8713. end;
  8714. end
  8715. @ @<Select the appropriate case and |return| or |goto common_ending|@>=
  8716. begin scan_int; n:=cur_val; {|n| is the number of cases to pass}
  8717. if tracing_commands>1 then
  8718. begin begin_diagnostic; print("{case "); print_int(n); print_char("}");
  8719. end_diagnostic(false);
  8720. end;
  8721. while n<>0 do
  8722. begin pass_text;
  8723. if cond_ptr=save_cond_ptr then
  8724. if cur_chr=or_code then decr(n)
  8725. else goto common_ending
  8726. else if cur_chr=fi_code then @<Pop the condition stack@>;
  8727. end;
  8728. change_if_limit(or_code,save_cond_ptr);
  8729. return; {wait for \.{\\or}, \.{\\else}, or \.{\\fi}}
  8730. end
  8731. @ The processing of conditionals is complete except for the following
  8732. code, which is actually part of |expand|. It comes into play when
  8733. \.{\\or}, \.{\\else}, or \.{\\fi} is scanned.
  8734. @<Terminate the current conditional and skip to \.{\\fi}@>=
  8735. if cur_chr>if_limit then
  8736. if if_limit=if_code then insert_relax {condition not yet evaluated}
  8737. else begin print_err("Extra "); print_cmd_chr(fi_or_else,cur_chr);
  8738. @.Extra \\or@>
  8739. @.Extra \\else@>
  8740. @.Extra \\fi@>
  8741. help1("I'm ignoring this; it doesn't match any \if.");
  8742. error;
  8743. end
  8744. else begin while cur_chr<>fi_code do pass_text; {skip to \.{\\fi}}
  8745. @<Pop the condition stack@>;
  8746. end
  8747. @* \[29] File names.
  8748. It's time now to fret about file names. Besides the fact that different
  8749. operating systems treat files in different ways, we must cope with the
  8750. fact that completely different naming conventions are used by different
  8751. groups of people. The following programs show what is required for one
  8752. particular operating system; similar routines for other systems are not
  8753. difficult to devise.
  8754. @^fingers@>
  8755. @^system dependencies@>
  8756. \TeX\ assumes that a file name has three parts: the name proper; its
  8757. ``extension''; and a ``file area'' where it is found in an external file
  8758. system. The extension of an input file or a write file is assumed to be
  8759. `\.{.tex}' unless otherwise specified; it is `\.{.log}' on the
  8760. transcript file that records each run of \TeX; it is `\.{.tfm}' on the font
  8761. metric files that describe characters in the fonts \TeX\ uses; it is
  8762. `\.{.dvi}' on the output files that specify typesetting information; and it
  8763. is `\.{.fmt}' on the format files written by \.{INITEX} to initialize \TeX.
  8764. The file area can be arbitrary on input files, but files are usually
  8765. output to the user's current area. If an input file cannot be
  8766. found on the specified area, \TeX\ will look for it on a special system
  8767. area; this special area is intended for commonly used input files like
  8768. \.{webmac.tex}.
  8769. Simple uses of \TeX\ refer only to file names that have no explicit
  8770. extension or area. For example, a person usually says `\.{\\input} \.{paper}'
  8771. or `\.{\\font\\tenrm} \.= \.{helvetica}' instead of `\.{\\input}
  8772. \.{paper.new}' or `\.{\\font\\tenrm} \.= \.{<csd.knuth>test}'. Simple file
  8773. names are best, because they make the \TeX\ source files portable;
  8774. whenever a file name consists entirely of letters and digits, it should be
  8775. treated in the same way by all implementations of \TeX. However, users
  8776. need the ability to refer to other files in their environment, especially
  8777. when responding to error messages concerning unopenable files; therefore
  8778. we want to let them use the syntax that appears in their favorite
  8779. operating system.
  8780. The following procedures don't allow spaces to be part of
  8781. file names; but some users seem to like names that are spaced-out.
  8782. System-dependent changes to allow such things should probably
  8783. be made with reluctance, and only when an entire file name that
  8784. includes spaces is ``quoted'' somehow.
  8785. @ In order to isolate the system-dependent aspects of file names, the
  8786. @^system dependencies@>
  8787. system-independent parts of \TeX\ are expressed in terms
  8788. of three system-dependent
  8789. procedures called |begin_name|, |more_name|, and |end_name|. In
  8790. essence, if the user-specified characters of the file name are $c_1\ldots c_n$,
  8791. the system-independent driver program does the operations
  8792. $$|begin_name|;\,|more_name|(c_1);\,\ldots\,;\,|more_name|(c_n);
  8793. \,|end_name|.$$
  8794. These three procedures communicate with each other via global variables.
  8795. Afterwards the file name will appear in the string pool as three strings
  8796. called |cur_name|\penalty10000\hskip-.05em,
  8797. |cur_area|, and |cur_ext|; the latter two are null (i.e.,
  8798. |""|), unless they were explicitly specified by the user.
  8799. Actually the situation is slightly more complicated, because \TeX\ needs
  8800. to know when the file name ends. The |more_name| routine is a function
  8801. (with side effects) that returns |true| on the calls |more_name|$(c_1)$,
  8802. \dots, |more_name|$(c_{n-1})$. The final call |more_name|$(c_n)$
  8803. returns |false|; or, it returns |true| and the token following $c_n$ is
  8804. something like `\.{\\hbox}' (i.e., not a character). In other words,
  8805. |more_name| is supposed to return |true| unless it is sure that the
  8806. file name has been completely scanned; and |end_name| is supposed to be able
  8807. to finish the assembly of |cur_name|, |cur_area|, and |cur_ext| regardless of
  8808. whether $|more_name|(c_n)$ returned |true| or |false|.
  8809. @<Glob...@>=
  8810. @!cur_name:str_number; {name of file just scanned}
  8811. @!cur_area:str_number; {file area just scanned, or \.{""}}
  8812. @!cur_ext:str_number; {file extension just scanned, or \.{""}}
  8813. @ The file names we shall deal with for illustrative purposes have the
  8814. following structure: If the name contains `\.>' or `\.:', the file area
  8815. consists of all characters up to and including the final such character;
  8816. otherwise the file area is null. If the remaining file name contains
  8817. `\..', the file extension consists of all such characters from the first
  8818. remaining `\..' to the end, otherwise the file extension is null.
  8819. @^system dependencies@>
  8820. We can scan such file names easily by using two global variables that keep track
  8821. of the occurrences of area and extension delimiters:
  8822. @<Glob...@>=
  8823. @!area_delimiter:pool_pointer; {the most recent `\.>' or `\.:', if any}
  8824. @!ext_delimiter:pool_pointer; {the relevant `\..', if any}
  8825. @ Input files that can't be found in the user's area may appear in a standard
  8826. system area called |TEX_area|. Font metric files whose areas are not given
  8827. explicitly are assumed to appear in a standard system area called
  8828. |TEX_font_area|. These system area names will, of course, vary from place
  8829. to place.
  8830. @^system dependencies@>
  8831. @d TEX_area=="TeXinputs:"
  8832. @.TeXinputs@>
  8833. @d TEX_font_area=="TeXfonts:"
  8834. @.TeXfonts@>
  8835. @ Here now is the first of the system-dependent routines for file name scanning.
  8836. @^system dependencies@>
  8837. @p procedure begin_name;
  8838. begin area_delimiter:=0; ext_delimiter:=0;
  8839. end;
  8840. @ And here's the second. The string pool might change as the file name is
  8841. being scanned, since a new \.{\\csname} might be entered; therefore we keep
  8842. |area_delimiter| and |ext_delimiter| relative to the beginning of the current
  8843. string, instead of assigning an absolute address like |pool_ptr| to them.
  8844. @^system dependencies@>
  8845. @p function more_name(@!c:ASCII_code):boolean;
  8846. begin if c=" " then more_name:=false
  8847. else begin str_room(1); append_char(c); {contribute |c| to the current string}
  8848. if (c=">")or(c=":") then
  8849. begin area_delimiter:=cur_length; ext_delimiter:=0;
  8850. end
  8851. else if (c=".")and(ext_delimiter=0) then ext_delimiter:=cur_length;
  8852. more_name:=true;
  8853. end;
  8854. end;
  8855. @ The third.
  8856. @^system dependencies@>
  8857. @p procedure end_name;
  8858. begin if str_ptr+3>max_strings then
  8859. overflow("number of strings",max_strings-init_str_ptr);
  8860. @:TeX capacity exceeded number of strings}{\quad number of strings@>
  8861. if area_delimiter=0 then cur_area:=""
  8862. else begin cur_area:=str_ptr;
  8863. str_start[str_ptr+1]:=str_start[str_ptr]+area_delimiter; incr(str_ptr);
  8864. end;
  8865. if ext_delimiter=0 then
  8866. begin cur_ext:=""; cur_name:=make_string;
  8867. end
  8868. else begin cur_name:=str_ptr;
  8869. str_start[str_ptr+1]:=str_start[str_ptr]+ext_delimiter-area_delimiter-1;
  8870. incr(str_ptr); cur_ext:=make_string;
  8871. end;
  8872. end;
  8873. @ Conversely, here is a routine that takes three strings and prints a file
  8874. name that might have produced them. (The routine is system dependent, because
  8875. some operating systems put the file area last instead of first.)
  8876. @^system dependencies@>
  8877. @<Basic printing...@>=
  8878. procedure print_file_name(@!n,@!a,@!e:integer);
  8879. begin slow_print(a); slow_print(n); slow_print(e);
  8880. end;
  8881. @ Another system-dependent routine is needed to convert three internal
  8882. \TeX\ strings
  8883. into the |name_of_file| value that is used to open files. The present code
  8884. allows both lowercase and uppercase letters in the file name.
  8885. @^system dependencies@>
  8886. @d append_to_name(#)==begin c:=#; incr(k);
  8887. if k<=file_name_size then name_of_file[k]:=xchr[c];
  8888. end
  8889. @p procedure pack_file_name(@!n,@!a,@!e:str_number);
  8890. var k:integer; {number of positions filled in |name_of_file|}
  8891. @!c: ASCII_code; {character being packed}
  8892. @!j:pool_pointer; {index into |str_pool|}
  8893. begin k:=0;
  8894. for j:=str_start[a] to str_start[a+1]-1 do append_to_name(so(str_pool[j]));
  8895. for j:=str_start[n] to str_start[n+1]-1 do append_to_name(so(str_pool[j]));
  8896. for j:=str_start[e] to str_start[e+1]-1 do append_to_name(so(str_pool[j]));
  8897. if k<=file_name_size then name_length:=k@+else name_length:=file_name_size;
  8898. for k:=name_length+1 to file_name_size do name_of_file[k]:=' ';
  8899. end;
  8900. @ A messier routine is also needed, since format file names must be scanned
  8901. before \TeX's string mechanism has been initialized. We shall use the
  8902. global variable |TEX_format_default| to supply the text for default system areas
  8903. and extensions related to format files.
  8904. @^system dependencies@>
  8905. @d format_default_length=20 {length of the |TEX_format_default| string}
  8906. @d format_area_length=11 {length of its area part}
  8907. @d format_ext_length=4 {length of its `\.{.fmt}' part}
  8908. @d format_extension=".fmt" {the extension, as a \.{WEB} constant}
  8909. @<Glob...@>=
  8910. @!TEX_format_default:packed array[1..format_default_length] of char;
  8911. @ @<Set init...@>=
  8912. TEX_format_default:='TeXformats:plain.fmt';
  8913. @.TeXformats@>
  8914. @.plain@>
  8915. @^system dependencies@>
  8916. @ @<Check the ``constant'' values for consistency@>=
  8917. if format_default_length>file_name_size then bad:=31;
  8918. @ Here is the messy routine that was just mentioned. It sets |name_of_file|
  8919. from the first |n| characters of |TEX_format_default|, followed by
  8920. |buffer[a..b]|, followed by the last |format_ext_length| characters of
  8921. |TEX_format_default|.
  8922. We dare not give error messages here, since \TeX\ calls this routine before
  8923. the |error| routine is ready to roll. Instead, we simply drop excess characters,
  8924. since the error will be detected in another way when a strange file name
  8925. isn't found.
  8926. @^system dependencies@>
  8927. @p procedure pack_buffered_name(@!n:small_number;@!a,@!b:integer);
  8928. var k:integer; {number of positions filled in |name_of_file|}
  8929. @!c: ASCII_code; {character being packed}
  8930. @!j:integer; {index into |buffer| or |TEX_format_default|}
  8931. begin if n+b-a+1+format_ext_length>file_name_size then
  8932. b:=a+file_name_size-n-1-format_ext_length;
  8933. k:=0;
  8934. for j:=1 to n do append_to_name(xord[TEX_format_default[j]]);
  8935. for j:=a to b do append_to_name(buffer[j]);
  8936. for j:=format_default_length-format_ext_length+1 to format_default_length do
  8937. append_to_name(xord[TEX_format_default[j]]);
  8938. if k<=file_name_size then name_length:=k@+else name_length:=file_name_size;
  8939. for k:=name_length+1 to file_name_size do name_of_file[k]:=' ';
  8940. end;
  8941. @ Here is the only place we use |pack_buffered_name|. This part of the program
  8942. becomes active when a ``virgin'' \TeX\ is trying to get going, just after
  8943. the preliminary initialization, or when the user is substituting another
  8944. format file by typing `\.\&' after the initial `\.{**}' prompt. The buffer
  8945. contains the first line of input in |buffer[loc..(last-1)]|, where
  8946. |loc<last| and |buffer[loc]<>" "|.
  8947. @<Declare the function called |open_fmt_file|@>=
  8948. function open_fmt_file:boolean;
  8949. label found,exit;
  8950. var j:0..buf_size; {the first space after the format file name}
  8951. begin j:=loc;
  8952. if buffer[loc]="&" then
  8953. begin incr(loc); j:=loc; buffer[last]:=" ";
  8954. while buffer[j]<>" " do incr(j);
  8955. pack_buffered_name(0,loc,j-1); {try first without the system file area}
  8956. if w_open_in(fmt_file) then goto found;
  8957. pack_buffered_name(format_area_length,loc,j-1);
  8958. {now try the system format file area}
  8959. if w_open_in(fmt_file) then goto found;
  8960. wake_up_terminal;
  8961. wterm_ln('Sorry, I can''t find that format;',' will try PLAIN.');
  8962. @.Sorry, I can't find...@>
  8963. update_terminal;
  8964. end;
  8965. {now pull out all the stops: try for the system \.{plain} file}
  8966. pack_buffered_name(format_default_length-format_ext_length,1,0);
  8967. if not w_open_in(fmt_file) then
  8968. begin wake_up_terminal;
  8969. wterm_ln('I can''t find the PLAIN format file!');
  8970. @.I can't find PLAIN...@>
  8971. @.plain@>
  8972. open_fmt_file:=false; return;
  8973. end;
  8974. found:loc:=j; open_fmt_file:=true;
  8975. exit:end;
  8976. @ Operating systems often make it possible to determine the exact name (and
  8977. possible version number) of a file that has been opened. The following routine,
  8978. which simply makes a \TeX\ string from the value of |name_of_file|, should
  8979. ideally be changed to deduce the full name of file~|f|, which is the file
  8980. most recently opened, if it is possible to do this in a \PASCAL\ program.
  8981. @^system dependencies@>
  8982. This routine might be called after string memory has overflowed, hence
  8983. we dare not use `|str_room|'.
  8984. @p function make_name_string:str_number;
  8985. var k:1..file_name_size; {index into |name_of_file|}
  8986. begin if (pool_ptr+name_length>pool_size)or(str_ptr=max_strings)or
  8987. (cur_length>0) then
  8988. make_name_string:="?"
  8989. else begin for k:=1 to name_length do append_char(xord[name_of_file[k]]);
  8990. make_name_string:=make_string;
  8991. end;
  8992. end;
  8993. function a_make_name_string(var f:alpha_file):str_number;
  8994. begin a_make_name_string:=make_name_string;
  8995. end;
  8996. function b_make_name_string(var f:byte_file):str_number;
  8997. begin b_make_name_string:=make_name_string;
  8998. end;
  8999. function w_make_name_string(var f:word_file):str_number;
  9000. begin w_make_name_string:=make_name_string;
  9001. end;
  9002. @ Now let's consider the ``driver''
  9003. routines by which \TeX\ deals with file names
  9004. in a system-independent manner. First comes a procedure that looks for a
  9005. file name in the input by calling |get_x_token| for the information.
  9006. @p procedure scan_file_name;
  9007. label done;
  9008. begin name_in_progress:=true; begin_name;
  9009. @<Get the next non-blank non-call...@>;
  9010. loop@+begin if (cur_cmd>other_char)or(cur_chr>255) then {not a character}
  9011. begin back_input; goto done;
  9012. end;
  9013. if not more_name(cur_chr) then goto done;
  9014. get_x_token;
  9015. end;
  9016. done: end_name; name_in_progress:=false;
  9017. end;
  9018. @ The global variable |name_in_progress| is used to prevent recursive
  9019. use of |scan_file_name|, since the |begin_name| and other procedures
  9020. communicate via global variables. Recursion would arise only by
  9021. devious tricks like `\.{\\input\\input f}'; such attempts at sabotage
  9022. must be thwarted. Furthermore, |name_in_progress| prevents \.{\\input}
  9023. @^recursion@>
  9024. from being initiated when a font size specification is being scanned.
  9025. Another global variable, |job_name|, contains the file name that was first
  9026. \.{\\input} by the user. This name is extended by `\.{.log}' and `\.{.dvi}'
  9027. and `\.{.fmt}' in the names of \TeX's output files.
  9028. @<Glob...@>=
  9029. @!name_in_progress:boolean; {is a file name being scanned?}
  9030. @!job_name:str_number; {principal file name}
  9031. @!log_opened:boolean; {has the transcript file been opened?}
  9032. @ Initially |job_name=0|; it becomes nonzero as soon as the true name is known.
  9033. We have |job_name=0| if and only if the `\.{log}' file has not been opened,
  9034. except of course for a short time just after |job_name| has become nonzero.
  9035. @<Initialize the output...@>=
  9036. job_name:=0; name_in_progress:=false; log_opened:=false;
  9037. @ Here is a routine that manufactures the output file names, assuming that
  9038. |job_name<>0|. It ignores and changes the current settings of |cur_area|
  9039. and |cur_ext|.
  9040. @d pack_cur_name==pack_file_name(cur_name,cur_area,cur_ext)
  9041. @p procedure pack_job_name(@!s:str_number); {|s = ".log"|, |".dvi"|, or
  9042. |format_extension|}
  9043. begin cur_area:=""; cur_ext:=s;
  9044. cur_name:=job_name; pack_cur_name;
  9045. end;
  9046. @ If some trouble arises when \TeX\ tries to open a file, the following
  9047. routine calls upon the user to supply another file name. Parameter~|s|
  9048. is used in the error message to identify the type of file; parameter~|e|
  9049. is the default extension if none is given. Upon exit from the routine,
  9050. variables |cur_name|, |cur_area|, |cur_ext|, and |name_of_file| are
  9051. ready for another attempt at file opening.
  9052. @p procedure prompt_file_name(@!s,@!e:str_number);
  9053. label done;
  9054. var k:0..buf_size; {index into |buffer|}
  9055. begin if interaction=scroll_mode then wake_up_terminal;
  9056. if s="input file name" then print_err("I can't find file `")
  9057. @.I can't find file x@>
  9058. else print_err("I can't write on file `");
  9059. @.I can't write on file x@>
  9060. print_file_name(cur_name,cur_area,cur_ext); print("'.");
  9061. if e=".tex" then show_context;
  9062. print_nl("Please type another "); print(s);
  9063. @.Please type...@>
  9064. if interaction<scroll_mode then
  9065. fatal_error("*** (job aborted, file error in nonstop mode)");
  9066. @.job aborted, file error...@>
  9067. clear_terminal; prompt_input(": "); @<Scan file name in the buffer@>;
  9068. if cur_ext="" then cur_ext:=e;
  9069. pack_cur_name;
  9070. end;
  9071. @ @<Scan file name in the buffer@>=
  9072. begin begin_name; k:=first;
  9073. while (buffer[k]=" ")and(k<last) do incr(k);
  9074. loop@+ begin if k=last then goto done;
  9075. if not more_name(buffer[k]) then goto done;
  9076. incr(k);
  9077. end;
  9078. done:end_name;
  9079. end
  9080. @ Here's an example of how these conventions are used. Whenever it is time to
  9081. ship out a box of stuff, we shall use the macro |ensure_dvi_open|.
  9082. @d ensure_dvi_open==if output_file_name=0 then
  9083. begin if job_name=0 then open_log_file;
  9084. pack_job_name(".dvi");
  9085. while not b_open_out(dvi_file) do
  9086. prompt_file_name("file name for output",".dvi");
  9087. output_file_name:=b_make_name_string(dvi_file);
  9088. end
  9089. @<Glob...@>=
  9090. @!dvi_file: byte_file; {the device-independent output goes here}
  9091. @!output_file_name: str_number; {full name of the output file}
  9092. @!log_name:str_number; {full name of the log file}
  9093. @ @<Initialize the output...@>=output_file_name:=0;
  9094. @ The |open_log_file| routine is used to open the transcript file and to help
  9095. it catch up to what has previously been printed on the terminal.
  9096. @p procedure open_log_file;
  9097. var old_setting:0..max_selector; {previous |selector| setting}
  9098. @!k:0..buf_size; {index into |months| and |buffer|}
  9099. @!l:0..buf_size; {end of first input line}
  9100. @!months:packed array [1..36] of char; {abbreviations of month names}
  9101. begin old_setting:=selector;
  9102. if job_name=0 then job_name:="texput";
  9103. @.texput@>
  9104. pack_job_name(".log");
  9105. while not a_open_out(log_file) do @<Try to get a different log file name@>;
  9106. log_name:=a_make_name_string(log_file);
  9107. selector:=log_only; log_opened:=true;
  9108. @<Print the banner line, including the date and time@>;
  9109. input_stack[input_ptr]:=cur_input; {make sure bottom level is in memory}
  9110. print_nl("**");
  9111. @.**@>
  9112. l:=input_stack[0].limit_field; {last position of first line}
  9113. if buffer[l]=end_line_char then decr(l);
  9114. for k:=1 to l do print(buffer[k]);
  9115. print_ln; {now the transcript file contains the first line of input}
  9116. selector:=old_setting+2; {|log_only| or |term_and_log|}
  9117. end;
  9118. @ Sometimes |open_log_file| is called at awkward moments when \TeX\ is
  9119. unable to print error messages or even to |show_context|.
  9120. The |prompt_file_name| routine can result in a |fatal_error|, but the |error|
  9121. routine will not be invoked because |log_opened| will be false.
  9122. The normal idea of |batch_mode| is that nothing at all should be written
  9123. on the terminal. However, in the unusual case that
  9124. no log file could be opened, we make an exception and allow
  9125. an explanatory message to be seen.
  9126. Incidentally, the program always refers to the log file as a `\.{transcript
  9127. file}', because some systems cannot use the extension `\.{.log}' for
  9128. this file.
  9129. @<Try to get a different log file name@>=
  9130. begin selector:=term_only;
  9131. prompt_file_name("transcript file name",".log");
  9132. end
  9133. @ @<Print the banner...@>=
  9134. begin wlog(banner);
  9135. slow_print(format_ident); print(" ");
  9136. print_int(sys_day); print_char(" ");
  9137. months:='JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC';
  9138. for k:=3*sys_month-2 to 3*sys_month do wlog(months[k]);
  9139. print_char(" "); print_int(sys_year); print_char(" ");
  9140. print_two(sys_time div 60); print_char(":"); print_two(sys_time mod 60);
  9141. end
  9142. @ Let's turn now to the procedure that is used to initiate file reading
  9143. when an `\.{\\input}' command is being processed.
  9144. Beware: For historic reasons, this code foolishly conserves a tiny bit
  9145. of string pool space; but that can confuse the interactive `\.E' option.
  9146. @^system dependencies@>
  9147. @p procedure start_input; {\TeX\ will \.{\\input} something}
  9148. label done;
  9149. begin scan_file_name; {set |cur_name| to desired file name}
  9150. if cur_ext="" then cur_ext:=".tex";
  9151. pack_cur_name;
  9152. loop@+ begin begin_file_reading; {set up |cur_file| and new level of input}
  9153. if a_open_in(cur_file) then goto done;
  9154. if cur_area="" then
  9155. begin pack_file_name(cur_name,TEX_area,cur_ext);
  9156. if a_open_in(cur_file) then goto done;
  9157. end;
  9158. end_file_reading; {remove the level that didn't work}
  9159. prompt_file_name("input file name",".tex");
  9160. end;
  9161. done: name:=a_make_name_string(cur_file);
  9162. if job_name=0 then
  9163. begin job_name:=cur_name; open_log_file;
  9164. end; {|open_log_file| doesn't |show_context|, so |limit|
  9165. and |loc| needn't be set to meaningful values yet}
  9166. if term_offset+length(name)>max_print_line-2 then print_ln
  9167. else if (term_offset>0)or(file_offset>0) then print_char(" ");
  9168. print_char("("); incr(open_parens); slow_print(name); update_terminal;
  9169. state:=new_line;
  9170. if name=str_ptr-1 then {conserve string pool space (but see note above)}
  9171. begin flush_string; name:=cur_name;
  9172. end;
  9173. @<Read the first line of the new file@>;
  9174. end;
  9175. @ Here we have to remember to tell the |input_ln| routine not to
  9176. start with a |get|. If the file is empty, it is considered to
  9177. contain a single blank line.
  9178. @^system dependencies@>
  9179. @^empty line at end of file@>
  9180. @<Read the first line...@>=
  9181. begin line:=1;
  9182. if input_ln(cur_file,false) then do_nothing;
  9183. firm_up_the_line;
  9184. if end_line_char_inactive then decr(limit)
  9185. else buffer[limit]:=end_line_char;
  9186. first:=limit+1; loc:=start;
  9187. end
  9188. @* \[30] Font metric data.
  9189. \TeX\ gets its knowledge about fonts from font metric files, also called
  9190. \.{TFM} files; the `\.T' in `\.{TFM}' stands for \TeX,
  9191. but other programs know about them too.
  9192. @:TFM files}{\.{TFM} files@>
  9193. @^font metric files@>
  9194. The information in a \.{TFM} file appears in a sequence of 8-bit bytes.
  9195. Since the number of bytes is always a multiple of 4, we could
  9196. also regard the file as a sequence of 32-bit words, but \TeX\ uses the
  9197. byte interpretation. The format of \.{TFM} files was designed by
  9198. Lyle Ramshaw in 1980. The intent is to convey a lot of different kinds
  9199. @^Ramshaw, Lyle Harold@>
  9200. of information in a compact but useful form.
  9201. @<Glob...@>=
  9202. @!tfm_file:byte_file;
  9203. @ The first 24 bytes (6 words) of a \.{TFM} file contain twelve 16-bit
  9204. integers that give the lengths of the various subsequent portions
  9205. of the file. These twelve integers are, in order:
  9206. $$\vbox{\halign{\hfil#&$\null=\null$#\hfil\cr
  9207. |lf|&length of the entire file, in words;\cr
  9208. |lh|&length of the header data, in words;\cr
  9209. |bc|&smallest character code in the font;\cr
  9210. |ec|&largest character code in the font;\cr
  9211. |nw|&number of words in the width table;\cr
  9212. |nh|&number of words in the height table;\cr
  9213. |nd|&number of words in the depth table;\cr
  9214. |ni|&number of words in the italic correction table;\cr
  9215. |nl|&number of words in the lig/kern table;\cr
  9216. |nk|&number of words in the kern table;\cr
  9217. |ne|&number of words in the extensible character table;\cr
  9218. |np|&number of font parameter words.\cr}}$$
  9219. They are all nonnegative and less than $2^{15}$. We must have |bc-1<=ec<=255|,
  9220. and
  9221. $$\hbox{|lf=6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np|.}$$
  9222. Note that a font may contain as many as 256 characters (if |bc=0| and |ec=255|),
  9223. and as few as 0 characters (if |bc=ec+1|).
  9224. Incidentally, when two or more 8-bit bytes are combined to form an integer of
  9225. 16 or more bits, the most significant bytes appear first in the file.
  9226. This is called BigEndian order.
  9227. @!@^BigEndian order@>
  9228. @ The rest of the \.{TFM} file may be regarded as a sequence of ten data
  9229. arrays having the informal specification
  9230. $$\def\arr$[#1]#2${\&{array} $[#1]$ \&{of} #2}
  9231. \vbox{\halign{\hfil\\{#}&$\,:\,$\arr#\hfil\cr
  9232. header&|[0..lh-1]@t\\{stuff}@>|\cr
  9233. char\_info&|[bc..ec]char_info_word|\cr
  9234. width&|[0..nw-1]fix_word|\cr
  9235. height&|[0..nh-1]fix_word|\cr
  9236. depth&|[0..nd-1]fix_word|\cr
  9237. italic&|[0..ni-1]fix_word|\cr
  9238. lig\_kern&|[0..nl-1]lig_kern_command|\cr
  9239. kern&|[0..nk-1]fix_word|\cr
  9240. exten&|[0..ne-1]extensible_recipe|\cr
  9241. param&|[1..np]fix_word|\cr}}$$
  9242. The most important data type used here is a |@!fix_word|, which is
  9243. a 32-bit representation of a binary fraction. A |fix_word| is a signed
  9244. quantity, with the two's complement of the entire word used to represent
  9245. negation. Of the 32 bits in a |fix_word|, exactly 12 are to the left of the
  9246. binary point; thus, the largest |fix_word| value is $2048-2^{-20}$, and
  9247. the smallest is $-2048$. We will see below, however, that all but two of
  9248. the |fix_word| values must lie between $-16$ and $+16$.
  9249. @ The first data array is a block of header information, which contains
  9250. general facts about the font. The header must contain at least two words,
  9251. |header[0]| and |header[1]|, whose meaning is explained below.
  9252. Additional header information of use to other software routines might
  9253. also be included, but \TeX82 does not need to know about such details.
  9254. For example, 16 more words of header information are in use at the Xerox
  9255. Palo Alto Research Center; the first ten specify the character coding
  9256. scheme used (e.g., `\.{XEROX text}' or `\.{TeX math symbols}'), the next five
  9257. give the font identifier (e.g., `\.{HELVETICA}' or `\.{CMSY}'), and the
  9258. last gives the ``face byte.'' The program that converts \.{DVI} files
  9259. to Xerox printing format gets this information by looking at the \.{TFM}
  9260. file, which it needs to read anyway because of other information that
  9261. is not explicitly repeated in \.{DVI}~format.
  9262. \yskip\hang|header[0]| is a 32-bit check sum that \TeX\ will copy into
  9263. the \.{DVI} output file. Later on when the \.{DVI} file is printed,
  9264. possibly on another computer, the actual font that gets used is supposed
  9265. to have a check sum that agrees with the one in the \.{TFM} file used by
  9266. \TeX. In this way, users will be warned about potential incompatibilities.
  9267. (However, if the check sum is zero in either the font file or the \.{TFM}
  9268. file, no check is made.) The actual relation between this check sum and
  9269. the rest of the \.{TFM} file is not important; the check sum is simply an
  9270. identification number with the property that incompatible fonts almost
  9271. always have distinct check sums.
  9272. @^check sum@>
  9273. \yskip\hang|header[1]| is a |fix_word| containing the design size of
  9274. the font, in units of \TeX\ points. This number must be at least 1.0; it is
  9275. fairly arbitrary, but usually the design size is 10.0 for a ``10 point''
  9276. font, i.e., a font that was designed to look best at a 10-point size,
  9277. whatever that really means. When a \TeX\ user asks for a font
  9278. `\.{at} $\delta$ \.{pt}', the effect is to override the design size
  9279. and replace it by $\delta$, and to multiply the $x$ and~$y$ coordinates
  9280. of the points in the font image by a factor of $\delta$ divided by the
  9281. design size. {\sl All other dimensions in the\/ \.{TFM} file are
  9282. |fix_word|\kern-1pt\ numbers in design-size units}, with the exception of
  9283. |param[1]| (which denotes the slant ratio). Thus, for example, the value
  9284. of |param[6]|, which defines the \.{em} unit, is often the |fix_word| value
  9285. $2^{20}=1.0$, since many fonts have a design size equal to one em.
  9286. The other dimensions must be less than 16 design-size units in absolute
  9287. value; thus, |header[1]| and |param[1]| are the only |fix_word|
  9288. entries in the whole \.{TFM} file whose first byte might be something
  9289. besides 0 or 255.
  9290. @ Next comes the |char_info| array, which contains one |@!char_info_word|
  9291. per character. Each word in this part of the file contains six fields
  9292. packed into four bytes as follows.
  9293. \yskip\hang first byte: |@!width_index| (8 bits)\par
  9294. \hang second byte: |@!height_index| (4 bits) times 16, plus |@!depth_index|
  9295. (4~bits)\par
  9296. \hang third byte: |@!italic_index| (6 bits) times 4, plus |@!tag|
  9297. (2~bits)\par
  9298. \hang fourth byte: |@!remainder| (8 bits)\par
  9299. \yskip\noindent
  9300. The actual width of a character is \\{width}|[width_index]|, in design-size
  9301. units; this is a device for compressing information, since many characters
  9302. have the same width. Since it is quite common for many characters
  9303. to have the same height, depth, or italic correction, the \.{TFM} format
  9304. imposes a limit of 16 different heights, 16 different depths, and
  9305. 64 different italic corrections.
  9306. @!@^italic correction@>
  9307. The italic correction of a character has two different uses.
  9308. (a)~In ordinary text, the italic correction is added to the width only if
  9309. the \TeX\ user specifies `\.{\\/}' after the character.
  9310. (b)~In math formulas, the italic correction is always added to the width,
  9311. except with respect to the positioning of subscripts.
  9312. Incidentally, the relation $\\{width}[0]=\\{height}[0]=\\{depth}[0]=
  9313. \\{italic}[0]=0$ should always hold, so that an index of zero implies a
  9314. value of zero. The |width_index| should never be zero unless the
  9315. character does not exist in the font, since a character is valid if and
  9316. only if it lies between |bc| and |ec| and has a nonzero |width_index|.
  9317. @ The |tag| field in a |char_info_word| has four values that explain how to
  9318. interpret the |remainder| field.
  9319. \yskip\hangg|tag=0| (|no_tag|) means that |remainder| is unused.\par
  9320. \hangg|tag=1| (|lig_tag|) means that this character has a ligature/kerning
  9321. program starting at position |remainder| in the |lig_kern| array.\par
  9322. \hangg|tag=2| (|list_tag|) means that this character is part of a chain of
  9323. characters of ascending sizes, and not the largest in the chain. The
  9324. |remainder| field gives the character code of the next larger character.\par
  9325. \hangg|tag=3| (|ext_tag|) means that this character code represents an
  9326. extensible character, i.e., a character that is built up of smaller pieces
  9327. so that it can be made arbitrarily large. The pieces are specified in
  9328. |@!exten[remainder]|.\par
  9329. \yskip\noindent
  9330. Characters with |tag=2| and |tag=3| are treated as characters with |tag=0|
  9331. unless they are used in special circumstances in math formulas. For example,
  9332. the \.{\\sum} operation looks for a |list_tag|, and the \.{\\left}
  9333. operation looks for both |list_tag| and |ext_tag|.
  9334. @d no_tag=0 {vanilla character}
  9335. @d lig_tag=1 {character has a ligature/kerning program}
  9336. @d list_tag=2 {character has a successor in a charlist}
  9337. @d ext_tag=3 {character is extensible}
  9338. @ The |lig_kern| array contains instructions in a simple programming language
  9339. that explains what to do for special letter pairs. Each word in this array is a
  9340. |@!lig_kern_command| of four bytes.
  9341. \yskip\hang first byte: |skip_byte|, indicates that this is the final program
  9342. step if the byte is 128 or more, otherwise the next step is obtained by
  9343. skipping this number of intervening steps.\par
  9344. \hang second byte: |next_char|, ``if |next_char| follows the current character,
  9345. then perform the operation and stop, otherwise continue.''\par
  9346. \hang third byte: |op_byte|, indicates a ligature step if less than~128,
  9347. a kern step otherwise.\par
  9348. \hang fourth byte: |remainder|.\par
  9349. \yskip\noindent
  9350. In a kern step, an
  9351. additional space equal to |kern[256*(op_byte-128)+remainder]| is inserted
  9352. between the current character and |next_char|. This amount is
  9353. often negative, so that the characters are brought closer together
  9354. by kerning; but it might be positive.
  9355. There are eight kinds of ligature steps, having |op_byte| codes $4a+2b+c$ where
  9356. $0\le a\le b+c$ and $0\le b,c\le1$. The character whose code is
  9357. |remainder| is inserted between the current character and |next_char|;
  9358. then the current character is deleted if $b=0$, and |next_char| is
  9359. deleted if $c=0$; then we pass over $a$~characters to reach the next
  9360. current character (which may have a ligature/kerning program of its own).
  9361. If the very first instruction of the |lig_kern| array has |skip_byte=255|,
  9362. the |next_char| byte is the so-called boundary character of this font;
  9363. the value of |next_char| need not lie between |bc| and~|ec|.
  9364. If the very last instruction of the |lig_kern| array has |skip_byte=255|,
  9365. there is a special ligature/kerning program for a boundary character at the
  9366. left, beginning at location |256*op_byte+remainder|.
  9367. The interpretation is that \TeX\ puts implicit boundary characters
  9368. before and after each consecutive string of characters from the same font.
  9369. These implicit characters do not appear in the output, but they can affect
  9370. ligatures and kerning.
  9371. If the very first instruction of a character's |lig_kern| program has
  9372. |skip_byte>128|, the program actually begins in location
  9373. |256*op_byte+remainder|. This feature allows access to large |lig_kern|
  9374. arrays, because the first instruction must otherwise
  9375. appear in a location |<=255|.
  9376. Any instruction with |skip_byte>128| in the |lig_kern| array must satisfy
  9377. the condition
  9378. $$\hbox{|256*op_byte+remainder<nl|.}$$
  9379. If such an instruction is encountered during
  9380. normal program execution, it denotes an unconditional halt; no ligature
  9381. or kerning command is performed.
  9382. @d stop_flag==qi(128) {value indicating `\.{STOP}' in a lig/kern program}
  9383. @d kern_flag==qi(128) {op code for a kern step}
  9384. @d skip_byte(#)==#.b0
  9385. @d next_char(#)==#.b1
  9386. @d op_byte(#)==#.b2
  9387. @d rem_byte(#)==#.b3
  9388. @ Extensible characters are specified by an |@!extensible_recipe|, which
  9389. consists of four bytes called |@!top|, |@!mid|, |@!bot|, and |@!rep| (in this
  9390. order). These bytes are the character codes of individual pieces used to
  9391. build up a large symbol. If |top|, |mid|, or |bot| are zero, they are not
  9392. present in the built-up result. For example, an extensible vertical line is
  9393. like an extensible bracket, except that the top and bottom pieces are missing.
  9394. Let $T$, $M$, $B$, and $R$ denote the respective pieces, or an empty box
  9395. if the piece isn't present. Then the extensible characters have the form
  9396. $TR^kMR^kB$ from top to bottom, for some |k>=0|, unless $M$ is absent;
  9397. in the latter case we can have $TR^kB$ for both even and odd values of~|k|.
  9398. The width of the extensible character is the width of $R$; and the
  9399. height-plus-depth is the sum of the individual height-plus-depths of the
  9400. components used, since the pieces are butted together in a vertical list.
  9401. @d ext_top(#)==#.b0 {|top| piece in a recipe}
  9402. @d ext_mid(#)==#.b1 {|mid| piece in a recipe}
  9403. @d ext_bot(#)==#.b2 {|bot| piece in a recipe}
  9404. @d ext_rep(#)==#.b3 {|rep| piece in a recipe}
  9405. @ The final portion of a \.{TFM} file is the |param| array, which is another
  9406. sequence of |fix_word| values.
  9407. \yskip\hang|param[1]=slant| is the amount of italic slant, which is used
  9408. to help position accents. For example, |slant=.25| means that when you go
  9409. up one unit, you also go .25 units to the right. The |slant| is a pure
  9410. number; it's the only |fix_word| other than the design size itself that is
  9411. not scaled by the design size.
  9412. \hang|param[2]=space| is the normal spacing between words in text.
  9413. Note that character |" "| in the font need not have anything to do with
  9414. blank spaces.
  9415. \hang|param[3]=space_stretch| is the amount of glue stretching between words.
  9416. \hang|param[4]=space_shrink| is the amount of glue shrinking between words.
  9417. \hang|param[5]=x_height| is the size of one ex in the font; it is also
  9418. the height of letters for which accents don't have to be raised or lowered.
  9419. \hang|param[6]=quad| is the size of one em in the font.
  9420. \hang|param[7]=extra_space| is the amount added to |param[2]| at the
  9421. ends of sentences.
  9422. \yskip\noindent
  9423. If fewer than seven parameters are present, \TeX\ sets the missing parameters
  9424. to zero. Fonts used for math symbols are required to have
  9425. additional parameter information, which is explained later.
  9426. @d slant_code=1
  9427. @d space_code=2
  9428. @d space_stretch_code=3
  9429. @d space_shrink_code=4
  9430. @d x_height_code=5
  9431. @d quad_code=6
  9432. @d extra_space_code=7
  9433. @ So that is what \.{TFM} files hold. Since \TeX\ has to absorb such information
  9434. about lots of fonts, it stores most of the data in a large array called
  9435. |font_info|. Each item of |font_info| is a |memory_word|; the |fix_word|
  9436. data gets converted into |scaled| entries, while everything else goes into
  9437. words of type |four_quarters|.
  9438. When the user defines \.{\\font\\f}, say, \TeX\ assigns an internal number
  9439. to the user's font~\.{\\f}. Adding this number to |font_id_base| gives the
  9440. |eqtb| location of a ``frozen'' control sequence that will always select
  9441. the font.
  9442. @<Types...@>=
  9443. @!internal_font_number=font_base..font_max; {|font| in a |char_node|}
  9444. @!font_index=0..font_mem_size; {index into |font_info|}
  9445. @ Here now is the (rather formidable) array of font arrays.
  9446. @d non_char==qi(256) {a |halfword| code that can't match a real character}
  9447. @d non_address=0 {a spurious |bchar_label|}
  9448. @<Glob...@>=
  9449. @!font_info:array[font_index] of memory_word;
  9450. {the big collection of font data}
  9451. @!fmem_ptr:font_index; {first unused word of |font_info|}
  9452. @!font_ptr:internal_font_number; {largest internal font number in use}
  9453. @!font_check:array[internal_font_number] of four_quarters; {check sum}
  9454. @!font_size:array[internal_font_number] of scaled; {``at'' size}
  9455. @!font_dsize:array[internal_font_number] of scaled; {``design'' size}
  9456. @!font_params:array[internal_font_number] of font_index; {how many font
  9457. parameters are present}
  9458. @!font_name:array[internal_font_number] of str_number; {name of the font}
  9459. @!font_area:array[internal_font_number] of str_number; {area of the font}
  9460. @!font_bc:array[internal_font_number] of eight_bits;
  9461. {beginning (smallest) character code}
  9462. @!font_ec:array[internal_font_number] of eight_bits;
  9463. {ending (largest) character code}
  9464. @!font_glue:array[internal_font_number] of pointer;
  9465. {glue specification for interword space, |null| if not allocated}
  9466. @!font_used:array[internal_font_number] of boolean;
  9467. {has a character from this font actually appeared in the output?}
  9468. @!hyphen_char:array[internal_font_number] of integer;
  9469. {current \.{\\hyphenchar} values}
  9470. @!skew_char:array[internal_font_number] of integer;
  9471. {current \.{\\skewchar} values}
  9472. @!bchar_label:array[internal_font_number] of font_index;
  9473. {start of |lig_kern| program for left boundary character,
  9474. |non_address| if there is none}
  9475. @!font_bchar:array[internal_font_number] of min_quarterword..non_char;
  9476. {boundary character, |non_char| if there is none}
  9477. @!font_false_bchar:array[internal_font_number] of min_quarterword..non_char;
  9478. {|font_bchar| if it doesn't exist in the font, otherwise |non_char|}
  9479. @ Besides the arrays just enumerated, we have directory arrays that make it
  9480. easy to get at the individual entries in |font_info|. For example, the
  9481. |char_info| data for character |c| in font |f| will be in
  9482. |font_info[char_base[f]+c].qqqq|; and if |w| is the |width_index|
  9483. part of this word (the |b0| field), the width of the character is
  9484. |font_info[width_base[f]+w].sc|. (These formulas assume that
  9485. |min_quarterword| has already been added to |c| and to |w|, since \TeX\
  9486. stores its quarterwords that way.)
  9487. @<Glob...@>=
  9488. @!char_base:array[internal_font_number] of integer;
  9489. {base addresses for |char_info|}
  9490. @!width_base:array[internal_font_number] of integer;
  9491. {base addresses for widths}
  9492. @!height_base:array[internal_font_number] of integer;
  9493. {base addresses for heights}
  9494. @!depth_base:array[internal_font_number] of integer;
  9495. {base addresses for depths}
  9496. @!italic_base:array[internal_font_number] of integer;
  9497. {base addresses for italic corrections}
  9498. @!lig_kern_base:array[internal_font_number] of integer;
  9499. {base addresses for ligature/kerning programs}
  9500. @!kern_base:array[internal_font_number] of integer;
  9501. {base addresses for kerns}
  9502. @!exten_base:array[internal_font_number] of integer;
  9503. {base addresses for extensible recipes}
  9504. @!param_base:array[internal_font_number] of integer;
  9505. {base addresses for font parameters}
  9506. @ @<Set init...@>=
  9507. for k:=font_base to font_max do font_used[k]:=false;
  9508. @ \TeX\ always knows at least one font, namely the null font. It has no
  9509. characters, and its seven parameters are all equal to zero.
  9510. @<Initialize table...@>=
  9511. font_ptr:=null_font; fmem_ptr:=7;
  9512. font_name[null_font]:="nullfont"; font_area[null_font]:="";
  9513. hyphen_char[null_font]:="-"; skew_char[null_font]:=-1;
  9514. bchar_label[null_font]:=non_address;
  9515. font_bchar[null_font]:=non_char; font_false_bchar[null_font]:=non_char;
  9516. font_bc[null_font]:=1; font_ec[null_font]:=0;
  9517. font_size[null_font]:=0; font_dsize[null_font]:=0;
  9518. char_base[null_font]:=0; width_base[null_font]:=0;
  9519. height_base[null_font]:=0; depth_base[null_font]:=0;
  9520. italic_base[null_font]:=0; lig_kern_base[null_font]:=0;
  9521. kern_base[null_font]:=0; exten_base[null_font]:=0;
  9522. font_glue[null_font]:=null; font_params[null_font]:=7;
  9523. param_base[null_font]:=-1;
  9524. for k:=0 to 6 do font_info[k].sc:=0;
  9525. @ @<Put each...@>=
  9526. primitive("nullfont",set_font,null_font);
  9527. @!@:null_font_}{\.{\\nullfont} primitive@>
  9528. text(frozen_null_font):="nullfont"; eqtb[frozen_null_font]:=eqtb[cur_val];
  9529. @ Of course we want to define macros that suppress the detail of how font
  9530. information is actually packed, so that we don't have to write things like
  9531. $$\hbox{|font_info[width_base[f]+font_info[char_base[f]+c].qqqq.b0].sc|}$$
  9532. too often. The \.{WEB} definitions here make |char_info(f)(c)| the
  9533. |four_quarters| word of font information corresponding to character
  9534. |c| of font |f|. If |q| is such a word, |char_width(f)(q)| will be
  9535. the character's width; hence the long formula above is at least
  9536. abbreviated to
  9537. $$\hbox{|char_width(f)(char_info(f)(c))|.}$$
  9538. Usually, of course, we will fetch |q| first and look at several of its
  9539. fields at the same time.
  9540. The italic correction of a character will be denoted by
  9541. |char_italic(f)(q)|, so it is analogous to |char_width|. But we will get
  9542. at the height and depth in a slightly different way, since we usually want
  9543. to compute both height and depth if we want either one. The value of
  9544. |height_depth(q)| will be the 8-bit quantity
  9545. $$b=|height_index|\times16+|depth_index|,$$ and if |b| is such a byte we
  9546. will write |char_height(f)(b)| and |char_depth(f)(b)| for the height and
  9547. depth of the character |c| for which |q=char_info(f)(c)|. Got that?
  9548. The tag field will be called |char_tag(q)|; the remainder byte will be
  9549. called |rem_byte(q)|, using a macro that we have already defined above.
  9550. Access to a character's |width|, |height|, |depth|, and |tag| fields is
  9551. part of \TeX's inner loop, so we want these macros to produce code that is
  9552. as fast as possible under the circumstances.
  9553. @^inner loop@>
  9554. @d char_info_end(#)==#].qqqq
  9555. @d char_info(#)==font_info[char_base[#]+char_info_end
  9556. @d char_width_end(#)==#.b0].sc
  9557. @d char_width(#)==font_info[width_base[#]+char_width_end
  9558. @d char_exists(#)==(#.b0>min_quarterword)
  9559. @d char_italic_end(#)==(qo(#.b2)) div 4].sc
  9560. @d char_italic(#)==font_info[italic_base[#]+char_italic_end
  9561. @d height_depth(#)==qo(#.b1)
  9562. @d char_height_end(#)==(#) div 16].sc
  9563. @d char_height(#)==font_info[height_base[#]+char_height_end
  9564. @d char_depth_end(#)==(#) mod 16].sc
  9565. @d char_depth(#)==font_info[depth_base[#]+char_depth_end
  9566. @d char_tag(#)==((qo(#.b2)) mod 4)
  9567. @ The global variable |null_character| is set up to be a word of
  9568. |char_info| for a character that doesn't exist. Such a word provides a
  9569. convenient way to deal with erroneous situations.
  9570. @<Glob...@>=
  9571. @!null_character:four_quarters; {nonexistent character information}
  9572. @ @<Set init...@>=
  9573. null_character.b0:=min_quarterword; null_character.b1:=min_quarterword;
  9574. null_character.b2:=min_quarterword; null_character.b3:=min_quarterword;
  9575. @ Here are some macros that help process ligatures and kerns.
  9576. We write |char_kern(f)(j)| to find the amount of kerning specified by
  9577. kerning command~|j| in font~|f|. If |j| is the |char_info| for a character
  9578. with a ligature/kern program, the first instruction of that program is either
  9579. |i=font_info[lig_kern_start(f)(j)]| or |font_info[lig_kern_restart(f)(i)]|,
  9580. depending on whether or not |skip_byte(i)<=stop_flag|.
  9581. The constant |kern_base_offset| should be simplified, for \PASCAL\ compilers
  9582. that do not do local optimization.
  9583. @^system dependencies@>
  9584. @d char_kern_end(#)==256*op_byte(#)+rem_byte(#)].sc
  9585. @d char_kern(#)==font_info[kern_base[#]+char_kern_end
  9586. @d kern_base_offset==256*(128+min_quarterword)
  9587. @d lig_kern_start(#)==lig_kern_base[#]+rem_byte {beginning of lig/kern program}
  9588. @d lig_kern_restart_end(#)==256*op_byte(#)+rem_byte(#)+32768-kern_base_offset
  9589. @d lig_kern_restart(#)==lig_kern_base[#]+lig_kern_restart_end
  9590. @ Font parameters are referred to as |slant(f)|, |space(f)|, etc.
  9591. @d param_end(#)==param_base[#]].sc
  9592. @d param(#)==font_info[#+param_end
  9593. @d slant==param(slant_code) {slant to the right, per unit distance upward}
  9594. @d space==param(space_code) {normal space between words}
  9595. @d space_stretch==param(space_stretch_code) {stretch between words}
  9596. @d space_shrink==param(space_shrink_code) {shrink between words}
  9597. @d x_height==param(x_height_code) {one ex}
  9598. @d quad==param(quad_code) {one em}
  9599. @d extra_space==param(extra_space_code) {additional space at end of sentence}
  9600. @<The em width for |cur_font|@>=quad(cur_font)
  9601. @ @<The x-height for |cur_font|@>=x_height(cur_font)
  9602. @ \TeX\ checks the information of a \.{TFM} file for validity as the
  9603. file is being read in, so that no further checks will be needed when
  9604. typesetting is going on. The somewhat tedious subroutine that does this
  9605. is called |read_font_info|. It has four parameters: the user font
  9606. identifier~|u|, the file name and area strings |nom| and |aire|, and the
  9607. ``at'' size~|s|. If |s|~is negative, it's the negative of a scale factor
  9608. to be applied to the design size; |s=-1000| is the normal case.
  9609. Otherwise |s| will be substituted for the design size; in this
  9610. case, |s| must be positive and less than $2048\rm\,pt$
  9611. (i.e., it must be less than $2^{27}$ when considered as an integer).
  9612. The subroutine opens and closes a global file variable called |tfm_file|.
  9613. It returns the value of the internal font number that was just loaded.
  9614. If an error is detected, an error message is issued and no font
  9615. information is stored; |null_font| is returned in this case.
  9616. @d bad_tfm=11 {label for |read_font_info|}
  9617. @d abort==goto bad_tfm {do this when the \.{TFM} data is wrong}
  9618. @p function read_font_info(@!u:pointer;@!nom,@!aire:str_number;
  9619. @!s:scaled):internal_font_number; {input a \.{TFM} file}
  9620. label done,bad_tfm,not_found;
  9621. var k:font_index; {index into |font_info|}
  9622. @!file_opened:boolean; {was |tfm_file| successfully opened?}
  9623. @!lf,@!lh,@!bc,@!ec,@!nw,@!nh,@!nd,@!ni,@!nl,@!nk,@!ne,@!np:halfword;
  9624. {sizes of subfiles}
  9625. @!f:internal_font_number; {the new font's number}
  9626. @!g:internal_font_number; {the number to return}
  9627. @!a,@!b,@!c,@!d:eight_bits; {byte variables}
  9628. @!qw:four_quarters;@!sw:scaled; {accumulators}
  9629. @!bch_label:integer; {left boundary start location, or infinity}
  9630. @!bchar:0..256; {boundary character, or 256}
  9631. @!z:scaled; {the design size or the ``at'' size}
  9632. @!alpha:integer;@!beta:1..16;
  9633. {auxiliary quantities used in fixed-point multiplication}
  9634. begin g:=null_font;@/
  9635. @<Read and check the font data; |abort| if the \.{TFM} file is
  9636. malformed; if there's no room for this font, say so and |goto
  9637. done|; otherwise |incr(font_ptr)| and |goto done|@>;
  9638. bad_tfm: @<Report that the font won't be loaded@>;
  9639. done: if file_opened then b_close(tfm_file);
  9640. read_font_info:=g;
  9641. end;
  9642. @ There are programs called \.{TFtoPL} and \.{PLtoTF} that convert
  9643. between the \.{TFM} format and a symbolic property-list format
  9644. that can be easily edited. These programs contain extensive
  9645. diagnostic information, so \TeX\ does not have to bother giving
  9646. precise details about why it rejects a particular \.{TFM} file.
  9647. @.TFtoPL@> @.PLtoTF@>
  9648. @d start_font_error_message==print_err("Font "); sprint_cs(u);
  9649. print_char("="); print_file_name(nom,aire,"");
  9650. if s>=0 then
  9651. begin print(" at "); print_scaled(s); print("pt");
  9652. end
  9653. else if s<>-1000 then
  9654. begin print(" scaled "); print_int(-s);
  9655. end
  9656. @<Report that the font won't be loaded@>=
  9657. start_font_error_message;
  9658. @.Font x=xx not loadable...@>
  9659. if file_opened then print(" not loadable: Bad metric (TFM) file")
  9660. else print(" not loadable: Metric (TFM) file not found");
  9661. help5("I wasn't able to read the size data for this font,")@/
  9662. ("so I will ignore the font specification.")@/
  9663. ("[Wizards can fix TFM files using TFtoPL/PLtoTF.]")@/
  9664. ("You might try inserting a different font spec;")@/
  9665. ("e.g., type `I\font<same font id>=<substitute font name>'.");
  9666. error
  9667. @ @<Read and check...@>=
  9668. @<Open |tfm_file| for input@>;
  9669. @<Read the {\.{TFM}} size fields@>;
  9670. @<Use size fields to allocate font information@>;
  9671. @<Read the {\.{TFM}} header@>;
  9672. @<Read character data@>;
  9673. @<Read box dimensions@>;
  9674. @<Read ligature/kern program@>;
  9675. @<Read extensible character recipes@>;
  9676. @<Read font parameters@>;
  9677. @<Make final adjustments and |goto done|@>
  9678. @ @<Open |tfm_file| for input@>=
  9679. file_opened:=false;
  9680. if aire="" then pack_file_name(nom,TEX_font_area,".tfm")
  9681. else pack_file_name(nom,aire,".tfm");
  9682. if not b_open_in(tfm_file) then abort;
  9683. file_opened:=true
  9684. @ Note: A malformed \.{TFM} file might be shorter than it claims to be;
  9685. thus |eof(tfm_file)| might be true when |read_font_info| refers to
  9686. |tfm_file^| or when it says |get(tfm_file)|. If such circumstances
  9687. cause system error messages, you will have to defeat them somehow,
  9688. for example by defining |fget| to be `\ignorespaces|begin get(tfm_file);|
  9689. |if eof(tfm_file) then abort; end|\unskip'.
  9690. @^system dependencies@>
  9691. @d fget==get(tfm_file)
  9692. @d fbyte==tfm_file^
  9693. @d read_sixteen(#)==begin #:=fbyte;
  9694. if #>127 then abort;
  9695. fget; #:=#*@'400+fbyte;
  9696. end
  9697. @d store_four_quarters(#)==begin fget; a:=fbyte; qw.b0:=qi(a);
  9698. fget; b:=fbyte; qw.b1:=qi(b);
  9699. fget; c:=fbyte; qw.b2:=qi(c);
  9700. fget; d:=fbyte; qw.b3:=qi(d);
  9701. #:=qw;
  9702. end
  9703. @ @<Read the {\.{TFM}} size fields@>=
  9704. begin read_sixteen(lf);
  9705. fget; read_sixteen(lh);
  9706. fget; read_sixteen(bc);
  9707. fget; read_sixteen(ec);
  9708. if (bc>ec+1)or(ec>255) then abort;
  9709. if bc>255 then {|bc=256| and |ec=255|}
  9710. begin bc:=1; ec:=0;
  9711. end;
  9712. fget; read_sixteen(nw);
  9713. fget; read_sixteen(nh);
  9714. fget; read_sixteen(nd);
  9715. fget; read_sixteen(ni);
  9716. fget; read_sixteen(nl);
  9717. fget; read_sixteen(nk);
  9718. fget; read_sixteen(ne);
  9719. fget; read_sixteen(np);
  9720. if lf<>6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np then abort;
  9721. if (nw=0)or(nh=0)or(nd=0)or(ni=0) then abort;
  9722. end
  9723. @ The preliminary settings of the index-offset variables |char_base|,
  9724. |width_base|, |lig_kern_base|, |kern_base|, and |exten_base| will be
  9725. corrected later by subtracting |min_quarterword| from them; and we will
  9726. subtract 1 from |param_base| too. It's best to forget about such anomalies
  9727. until later.
  9728. @<Use size fields to allocate font information@>=
  9729. lf:=lf-6-lh; {|lf| words should be loaded into |font_info|}
  9730. if np<7 then lf:=lf+7-np; {at least seven parameters will appear}
  9731. if (font_ptr=font_max)or(fmem_ptr+lf>font_mem_size) then
  9732. @<Apologize for not loading the font, |goto done|@>;
  9733. f:=font_ptr+1;
  9734. char_base[f]:=fmem_ptr-bc;
  9735. width_base[f]:=char_base[f]+ec+1;
  9736. height_base[f]:=width_base[f]+nw;
  9737. depth_base[f]:=height_base[f]+nh;
  9738. italic_base[f]:=depth_base[f]+nd;
  9739. lig_kern_base[f]:=italic_base[f]+ni;
  9740. kern_base[f]:=lig_kern_base[f]+nl-kern_base_offset;
  9741. exten_base[f]:=kern_base[f]+kern_base_offset+nk;
  9742. param_base[f]:=exten_base[f]+ne
  9743. @ @<Apologize for not loading...@>=
  9744. begin start_font_error_message;
  9745. print(" not loaded: Not enough room left");
  9746. @.Font x=xx not loaded...@>
  9747. help4("I'm afraid I won't be able to make use of this font,")@/
  9748. ("because my memory for character-size data is too small.")@/
  9749. ("If you're really stuck, ask a wizard to enlarge me.")@/
  9750. ("Or maybe try `I\font<same font id>=<name of loaded font>'.");
  9751. error; goto done;
  9752. end
  9753. @ Only the first two words of the header are needed by \TeX82.
  9754. @<Read the {\.{TFM}} header@>=
  9755. begin if lh<2 then abort;
  9756. store_four_quarters(font_check[f]);
  9757. fget; read_sixteen(z); {this rejects a negative design size}
  9758. fget; z:=z*@'400+fbyte; fget; z:=(z*@'20)+(fbyte div@'20);
  9759. if z<unity then abort;
  9760. while lh>2 do
  9761. begin fget;fget;fget;fget;decr(lh); {ignore the rest of the header}
  9762. end;
  9763. font_dsize[f]:=z;
  9764. if s<>-1000 then
  9765. if s>=0 then z:=s
  9766. else z:=xn_over_d(z,-s,1000);
  9767. font_size[f]:=z;
  9768. end
  9769. @ @<Read character data@>=
  9770. for k:=fmem_ptr to width_base[f]-1 do
  9771. begin store_four_quarters(font_info[k].qqqq);
  9772. if (a>=nw)or(b div @'20>=nh)or(b mod @'20>=nd)or
  9773. (c div 4>=ni) then abort;
  9774. case c mod 4 of
  9775. lig_tag: if d>=nl then abort;
  9776. ext_tag: if d>=ne then abort;
  9777. list_tag: @<Check for charlist cycle@>;
  9778. othercases do_nothing {|no_tag|}
  9779. endcases;
  9780. end
  9781. @ We want to make sure that there is no cycle of characters linked together
  9782. by |list_tag| entries, since such a cycle would get \TeX\ into an endless
  9783. loop. If such a cycle exists, the routine here detects it when processing
  9784. the largest character code in the cycle.
  9785. @d check_byte_range(#)==begin if (#<bc)or(#>ec) then abort@+end
  9786. @d current_character_being_worked_on==k+bc-fmem_ptr
  9787. @<Check for charlist cycle@>=
  9788. begin check_byte_range(d);
  9789. while d<current_character_being_worked_on do
  9790. begin qw:=char_info(f)(d);
  9791. {N.B.: not |qi(d)|, since |char_base[f]| hasn't been adjusted yet}
  9792. if char_tag(qw)<>list_tag then goto not_found;
  9793. d:=qo(rem_byte(qw)); {next character on the list}
  9794. end;
  9795. if d=current_character_being_worked_on then abort; {yes, there's a cycle}
  9796. not_found:end
  9797. @ A |fix_word| whose four bytes are $(a,b,c,d)$ from left to right represents
  9798. the number
  9799. $$x=\left\{\vcenter{\halign{$#$,\hfil\qquad&if $#$\hfil\cr
  9800. b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=0;\cr
  9801. -16+b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=255.\cr}}\right.$$
  9802. (No other choices of |a| are allowed, since the magnitude of a number in
  9803. design-size units must be less than 16.) We want to multiply this
  9804. quantity by the integer~|z|, which is known to be less than $2^{27}$.
  9805. If $|z|<2^{23}$, the individual multiplications $b\cdot z$,
  9806. $c\cdot z$, $d\cdot z$ cannot overflow; otherwise we will divide |z| by 2,
  9807. 4, 8, or 16, to obtain a multiplier less than $2^{23}$, and we can
  9808. compensate for this later. If |z| has thereby been replaced by
  9809. $|z|^\prime=|z|/2^e$, let $\beta=2^{4-e}$; we shall compute
  9810. $$\lfloor(b+c\cdot2^{-8}+d\cdot2^{-16})\,z^\prime/\beta\rfloor$$
  9811. if $a=0$, or the same quantity minus $\alpha=2^{4+e}z^\prime$ if $a=255$.
  9812. This calculation must be done exactly, in order to guarantee portability
  9813. of \TeX\ between computers.
  9814. @d store_scaled(#)==begin fget; a:=fbyte; fget; b:=fbyte;
  9815. fget; c:=fbyte; fget; d:=fbyte;@/
  9816. sw:=(((((d*z)div@'400)+(c*z))div@'400)+(b*z))div beta;
  9817. if a=0 then #:=sw@+else if a=255 then #:=sw-alpha@+else abort;
  9818. end
  9819. @<Read box dimensions@>=
  9820. begin @<Replace |z| by $|z|^\prime$ and compute $\alpha,\beta$@>;
  9821. for k:=width_base[f] to lig_kern_base[f]-1 do
  9822. store_scaled(font_info[k].sc);
  9823. if font_info[width_base[f]].sc<>0 then abort; {\\{width}[0] must be zero}
  9824. if font_info[height_base[f]].sc<>0 then abort; {\\{height}[0] must be zero}
  9825. if font_info[depth_base[f]].sc<>0 then abort; {\\{depth}[0] must be zero}
  9826. if font_info[italic_base[f]].sc<>0 then abort; {\\{italic}[0] must be zero}
  9827. end
  9828. @ @<Replace |z|...@>=
  9829. begin alpha:=16;
  9830. while z>=@'40000000 do
  9831. begin z:=z div 2; alpha:=alpha+alpha;
  9832. end;
  9833. beta:=256 div alpha; alpha:=alpha*z;
  9834. end
  9835. @ @d check_existence(#)==@t@>@;@/
  9836. begin check_byte_range(#);
  9837. qw:=char_info(f)(#); {N.B.: not |qi(#)|}
  9838. if not char_exists(qw) then abort;
  9839. end
  9840. @<Read ligature/kern program@>=
  9841. bch_label:=@'77777; bchar:=256;
  9842. if nl>0 then
  9843. begin for k:=lig_kern_base[f] to kern_base[f]+kern_base_offset-1 do
  9844. begin store_four_quarters(font_info[k].qqqq);
  9845. if a>128 then
  9846. begin if 256*c+d>=nl then abort;
  9847. if a=255 then if k=lig_kern_base[f] then bchar:=b;
  9848. end
  9849. else begin if b<>bchar then check_existence(b);
  9850. if c<128 then check_existence(d) {check ligature}
  9851. else if 256*(c-128)+d>=nk then abort; {check kern}
  9852. if a<128 then if k-lig_kern_base[f]+a+1>=nl then abort;
  9853. end;
  9854. end;
  9855. if a=255 then bch_label:=256*c+d;
  9856. end;
  9857. for k:=kern_base[f]+kern_base_offset to exten_base[f]-1 do
  9858. store_scaled(font_info[k].sc);
  9859. @ @<Read extensible character recipes@>=
  9860. for k:=exten_base[f] to param_base[f]-1 do
  9861. begin store_four_quarters(font_info[k].qqqq);
  9862. if a<>0 then check_existence(a);
  9863. if b<>0 then check_existence(b);
  9864. if c<>0 then check_existence(c);
  9865. check_existence(d);
  9866. end
  9867. @ We check to see that the \.{TFM} file doesn't end prematurely; but
  9868. no error message is given for files having more than |lf| words.
  9869. @<Read font parameters@>=
  9870. begin for k:=1 to np do
  9871. if k=1 then {the |slant| parameter is a pure number}
  9872. begin fget; sw:=fbyte; if sw>127 then sw:=sw-256;
  9873. fget; sw:=sw*@'400+fbyte; fget; sw:=sw*@'400+fbyte;
  9874. fget; font_info[param_base[f]].sc:=
  9875. (sw*@'20)+(fbyte div@'20);
  9876. end
  9877. else store_scaled(font_info[param_base[f]+k-1].sc);
  9878. if eof(tfm_file) then abort;
  9879. for k:=np+1 to 7 do font_info[param_base[f]+k-1].sc:=0;
  9880. end
  9881. @ Now to wrap it up, we have checked all the necessary things about the \.{TFM}
  9882. file, and all we need to do is put the finishing touches on the data for
  9883. the new font.
  9884. @d adjust(#)==#[f]:=qo(#[f])
  9885. {correct for the excess |min_quarterword| that was added}
  9886. @<Make final adjustments...@>=
  9887. if np>=7 then font_params[f]:=np@+else font_params[f]:=7;
  9888. hyphen_char[f]:=default_hyphen_char; skew_char[f]:=default_skew_char;
  9889. if bch_label<nl then bchar_label[f]:=bch_label+lig_kern_base[f]
  9890. else bchar_label[f]:=non_address;
  9891. font_bchar[f]:=qi(bchar);
  9892. font_false_bchar[f]:=qi(bchar);
  9893. if bchar<=ec then if bchar>=bc then
  9894. begin qw:=char_info(f)(bchar); {N.B.: not |qi(bchar)|}
  9895. if char_exists(qw) then font_false_bchar[f]:=non_char;
  9896. end;
  9897. font_name[f]:=nom;
  9898. font_area[f]:=aire;
  9899. font_bc[f]:=bc; font_ec[f]:=ec; font_glue[f]:=null;
  9900. adjust(char_base); adjust(width_base); adjust(lig_kern_base);
  9901. adjust(kern_base); adjust(exten_base);
  9902. decr(param_base[f]);
  9903. fmem_ptr:=fmem_ptr+lf; font_ptr:=f; g:=f; goto done
  9904. @ Before we forget about the format of these tables, let's deal with two
  9905. of \TeX's basic scanning routines related to font information.
  9906. @<Declare procedures that scan font-related stuff@>=
  9907. procedure scan_font_ident;
  9908. var f:internal_font_number;
  9909. @!m:halfword;
  9910. begin @<Get the next non-blank non-call...@>;
  9911. if cur_cmd=def_font then f:=cur_font
  9912. else if cur_cmd=set_font then f:=cur_chr
  9913. else if cur_cmd=def_family then
  9914. begin m:=cur_chr; scan_four_bit_int; f:=equiv(m+cur_val);
  9915. end
  9916. else begin print_err("Missing font identifier");
  9917. @.Missing font identifier@>
  9918. help2("I was looking for a control sequence whose")@/
  9919. ("current meaning has been defined by \font.");
  9920. back_error; f:=null_font;
  9921. end;
  9922. cur_val:=f;
  9923. end;
  9924. @ The following routine is used to implement `\.{\\fontdimen} |n| |f|'.
  9925. The boolean parameter |writing| is set |true| if the calling program
  9926. intends to change the parameter value.
  9927. @<Declare procedures that scan font-related stuff@>=
  9928. procedure find_font_dimen(@!writing:boolean);
  9929. {sets |cur_val| to |font_info| location}
  9930. var f:internal_font_number;
  9931. @!n:integer; {the parameter number}
  9932. begin scan_int; n:=cur_val; scan_font_ident; f:=cur_val;
  9933. if n<=0 then cur_val:=fmem_ptr
  9934. else begin if writing and(n<=space_shrink_code)and@|
  9935. (n>=space_code)and(font_glue[f]<>null) then
  9936. begin delete_glue_ref(font_glue[f]);
  9937. font_glue[f]:=null;
  9938. end;
  9939. if n>font_params[f] then
  9940. if f<font_ptr then cur_val:=fmem_ptr
  9941. else @<Increase the number of parameters in the last font@>
  9942. else cur_val:=n+param_base[f];
  9943. end;
  9944. @<Issue an error message if |cur_val=fmem_ptr|@>;
  9945. end;
  9946. @ @<Issue an error message if |cur_val=fmem_ptr|@>=
  9947. if cur_val=fmem_ptr then
  9948. begin print_err("Font "); print_esc(font_id_text(f));
  9949. print(" has only "); print_int(font_params[f]);
  9950. print(" fontdimen parameters");
  9951. @.Font x has only...@>
  9952. help2("To increase the number of font parameters, you must")@/
  9953. ("use \fontdimen immediately after the \font is loaded.");
  9954. error;
  9955. end
  9956. @ @<Increase the number of parameters...@>=
  9957. begin repeat if fmem_ptr=font_mem_size then
  9958. overflow("font memory",font_mem_size);
  9959. @:TeX capacity exceeded font memory}{\quad font memory@>
  9960. font_info[fmem_ptr].sc:=0; incr(fmem_ptr); incr(font_params[f]);
  9961. until n=font_params[f];
  9962. cur_val:=fmem_ptr-1; {this equals |param_base[f]+font_params[f]|}
  9963. end
  9964. @ When \TeX\ wants to typeset a character that doesn't exist, the
  9965. character node is not created; thus the output routine can assume
  9966. that characters exist when it sees them. The following procedure
  9967. prints a warning message unless the user has suppressed it.
  9968. @p procedure char_warning(@!f:internal_font_number;@!c:eight_bits);
  9969. begin if tracing_lost_chars>0 then
  9970. begin begin_diagnostic;
  9971. print_nl("Missing character: There is no ");
  9972. @.Missing character@>
  9973. print_ASCII(c); print(" in font ");
  9974. slow_print(font_name[f]); print_char("!"); end_diagnostic(false);
  9975. end;
  9976. end;
  9977. @ Here is a function that returns a pointer to a character node for a
  9978. given character in a given font. If that character doesn't exist,
  9979. |null| is returned instead.
  9980. @p function new_character(@!f:internal_font_number;@!c:eight_bits):pointer;
  9981. label exit;
  9982. var p:pointer; {newly allocated node}
  9983. begin if font_bc[f]<=c then if font_ec[f]>=c then
  9984. if char_exists(char_info(f)(qi(c))) then
  9985. begin p:=get_avail; font(p):=f; character(p):=qi(c);
  9986. new_character:=p; return;
  9987. end;
  9988. char_warning(f,c);
  9989. new_character:=null;
  9990. exit:end;
  9991. @* \[31] Device-independent file format.
  9992. The most important output produced by a run of \TeX\ is the ``device
  9993. independent'' (\.{DVI}) file that specifies where characters and rules
  9994. are to appear on printed pages. The form of these files was designed by
  9995. David R. Fuchs in 1979. Almost any reasonable typesetting device can be
  9996. @^Fuchs, David Raymond@>
  9997. @:DVI_files}{\.{DVI} files@>
  9998. driven by a program that takes \.{DVI} files as input, and dozens of such
  9999. \.{DVI}-to-whatever programs have been written. Thus, it is possible to
  10000. print the output of \TeX\ on many different kinds of equipment, using \TeX\
  10001. as a device-independent ``front end.''
  10002. A \.{DVI} file is a stream of 8-bit bytes, which may be regarded as a
  10003. series of commands in a machine-like language. The first byte of each command
  10004. is the operation code, and this code is followed by zero or more bytes
  10005. that provide parameters to the command. The parameters themselves may consist
  10006. of several consecutive bytes; for example, the `|set_rule|' command has two
  10007. parameters, each of which is four bytes long. Parameters are usually
  10008. regarded as nonnegative integers; but four-byte-long parameters,
  10009. and shorter parameters that denote distances, can be
  10010. either positive or negative. Such parameters are given in two's complement
  10011. notation. For example, a two-byte-long distance parameter has a value between
  10012. $-2^{15}$ and $2^{15}-1$. As in \.{TFM} files, numbers that occupy
  10013. more than one byte position appear in BigEndian order.
  10014. A \.{DVI} file consists of a ``preamble,'' followed by a sequence of one
  10015. or more ``pages,'' followed by a ``postamble.'' The preamble is simply a
  10016. |pre| command, with its parameters that define the dimensions used in the
  10017. file; this must come first. Each ``page'' consists of a |bop| command,
  10018. followed by any number of other commands that tell where characters are to
  10019. be placed on a physical page, followed by an |eop| command. The pages
  10020. appear in the order that \TeX\ generated them. If we ignore |nop| commands
  10021. and \\{fnt\_def} commands (which are allowed between any two commands in
  10022. the file), each |eop| command is immediately followed by a |bop| command,
  10023. or by a |post| command; in the latter case, there are no more pages in the
  10024. file, and the remaining bytes form the postamble. Further details about
  10025. the postamble will be explained later.
  10026. Some parameters in \.{DVI} commands are ``pointers.'' These are four-byte
  10027. quantities that give the location number of some other byte in the file;
  10028. the first byte is number~0, then comes number~1, and so on. For example,
  10029. one of the parameters of a |bop| command points to the previous |bop|;
  10030. this makes it feasible to read the pages in backwards order, in case the
  10031. results are being directed to a device that stacks its output face up.
  10032. Suppose the preamble of a \.{DVI} file occupies bytes 0 to 99. Now if the
  10033. first page occupies bytes 100 to 999, say, and if the second
  10034. page occupies bytes 1000 to 1999, then the |bop| that starts in byte 1000
  10035. points to 100 and the |bop| that starts in byte 2000 points to 1000. (The
  10036. very first |bop|, i.e., the one starting in byte 100, has a pointer of~$-1$.)
  10037. @ The \.{DVI} format is intended to be both compact and easily interpreted
  10038. by a machine. Compactness is achieved by making most of the information
  10039. implicit instead of explicit. When a \.{DVI}-reading program reads the
  10040. commands for a page, it keeps track of several quantities: (a)~The current
  10041. font |f| is an integer; this value is changed only
  10042. by \\{fnt} and \\{fnt\_num} commands. (b)~The current position on the page
  10043. is given by two numbers called the horizontal and vertical coordinates,
  10044. |h| and |v|. Both coordinates are zero at the upper left corner of the page;
  10045. moving to the right corresponds to increasing the horizontal coordinate, and
  10046. moving down corresponds to increasing the vertical coordinate. Thus, the
  10047. coordinates are essentially Cartesian, except that vertical directions are
  10048. flipped; the Cartesian version of |(h,v)| would be |(h,-v)|. (c)~The
  10049. current spacing amounts are given by four numbers |w|, |x|, |y|, and |z|,
  10050. where |w| and~|x| are used for horizontal spacing and where |y| and~|z|
  10051. are used for vertical spacing. (d)~There is a stack containing
  10052. |(h,v,w,x,y,z)| values; the \.{DVI} commands |push| and |pop| are used to
  10053. change the current level of operation. Note that the current font~|f| is
  10054. not pushed and popped; the stack contains only information about
  10055. positioning.
  10056. The values of |h|, |v|, |w|, |x|, |y|, and |z| are signed integers having up
  10057. to 32 bits, including the sign. Since they represent physical distances,
  10058. there is a small unit of measurement such that increasing |h| by~1 means
  10059. moving a certain tiny distance to the right. The actual unit of
  10060. measurement is variable, as explained below; \TeX\ sets things up so that
  10061. its \.{DVI} output is in sp units, i.e., scaled points, in agreement with
  10062. all the |scaled| dimensions in \TeX's data structures.
  10063. @ Here is a list of all the commands that may appear in a \.{DVI} file. Each
  10064. command is specified by its symbolic name (e.g., |bop|), its opcode byte
  10065. (e.g., 139), and its parameters (if any). The parameters are followed
  10066. by a bracketed number telling how many bytes they occupy; for example,
  10067. `|p[4]|' means that parameter |p| is four bytes long.
  10068. \yskip\hang|set_char_0| 0. Typeset character number~0 from font~|f|
  10069. such that the reference point of the character is at |(h,v)|. Then
  10070. increase |h| by the width of that character. Note that a character may
  10071. have zero or negative width, so one cannot be sure that |h| will advance
  10072. after this command; but |h| usually does increase.
  10073. \yskip\hang\\{set\_char\_1} through \\{set\_char\_127} (opcodes 1 to 127).
  10074. Do the operations of |set_char_0|; but use the character whose number
  10075. matches the opcode, instead of character~0.
  10076. \yskip\hang|set1| 128 |c[1]|. Same as |set_char_0|, except that character
  10077. number~|c| is typeset. \TeX82 uses this command for characters in the
  10078. range |128<=c<256|.
  10079. \yskip\hang|@!set2| 129 |c[2]|. Same as |set1|, except that |c|~is two
  10080. bytes long, so it is in the range |0<=c<65536|. \TeX82 never uses this
  10081. command, but it should come in handy for extensions of \TeX\ that deal
  10082. with oriental languages.
  10083. @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
  10084. \yskip\hang|@!set3| 130 |c[3]|. Same as |set1|, except that |c|~is three
  10085. bytes long, so it can be as large as $2^{24}-1$. Not even the Chinese
  10086. language has this many characters, but this command might prove useful
  10087. in some yet unforeseen extension.
  10088. \yskip\hang|@!set4| 131 |c[4]|. Same as |set1|, except that |c|~is four
  10089. bytes long. Imagine that.
  10090. \yskip\hang|set_rule| 132 |a[4]| |b[4]|. Typeset a solid black rectangle
  10091. of height~|a| and width~|b|, with its bottom left corner at |(h,v)|. Then
  10092. set |h:=h+b|. If either |a<=0| or |b<=0|, nothing should be typeset. Note
  10093. that if |b<0|, the value of |h| will decrease even though nothing else happens.
  10094. See below for details about how to typeset rules so that consistency with
  10095. \MF\ is guaranteed.
  10096. \yskip\hang|@!put1| 133 |c[1]|. Typeset character number~|c| from font~|f|
  10097. such that the reference point of the character is at |(h,v)|. (The `put'
  10098. commands are exactly like the `set' commands, except that they simply put out a
  10099. character or a rule without moving the reference point afterwards.)
  10100. \yskip\hang|@!put2| 134 |c[2]|. Same as |set2|, except that |h| is not changed.
  10101. \yskip\hang|@!put3| 135 |c[3]|. Same as |set3|, except that |h| is not changed.
  10102. \yskip\hang|@!put4| 136 |c[4]|. Same as |set4|, except that |h| is not changed.
  10103. \yskip\hang|put_rule| 137 |a[4]| |b[4]|. Same as |set_rule|, except that
  10104. |h| is not changed.
  10105. \yskip\hang|nop| 138. No operation, do nothing. Any number of |nop|'s
  10106. may occur between \.{DVI} commands, but a |nop| cannot be inserted between
  10107. a command and its parameters or between two parameters.
  10108. \yskip\hang|bop| 139 $c_0[4]$ $c_1[4]$ $\ldots$ $c_9[4]$ $p[4]$. Beginning
  10109. of a page: Set |(h,v,w,x,y,z):=(0,0,0,0,0,0)| and set the stack empty. Set
  10110. the current font |f| to an undefined value. The ten $c_i$ parameters hold
  10111. the values of \.{\\count0} $\ldots$ \.{\\count9} in \TeX\ at the time
  10112. \.{\\shipout} was invoked for this page; they can be used to identify
  10113. pages, if a user wants to print only part of a \.{DVI} file. The parameter
  10114. |p| points to the previous |bop| in the file; the first
  10115. |bop| has $p=-1$.
  10116. \yskip\hang|eop| 140. End of page: Print what you have read since the
  10117. previous |bop|. At this point the stack should be empty. (The \.{DVI}-reading
  10118. programs that drive most output devices will have kept a buffer of the
  10119. material that appears on the page that has just ended. This material is
  10120. largely, but not entirely, in order by |v| coordinate and (for fixed |v|) by
  10121. |h|~coordinate; so it usually needs to be sorted into some order that is
  10122. appropriate for the device in question.)
  10123. \yskip\hang|push| 141. Push the current values of |(h,v,w,x,y,z)| onto the
  10124. top of the stack; do not change any of these values. Note that |f| is
  10125. not pushed.
  10126. \yskip\hang|pop| 142. Pop the top six values off of the stack and assign
  10127. them respectively to |(h,v,w,x,y,z)|. The number of pops should never
  10128. exceed the number of pushes, since it would be highly embarrassing if the
  10129. stack were empty at the time of a |pop| command.
  10130. \yskip\hang|right1| 143 |b[1]|. Set |h:=h+b|, i.e., move right |b| units.
  10131. The parameter is a signed number in two's complement notation, |-128<=b<128|;
  10132. if |b<0|, the reference point moves left.
  10133. \yskip\hang|@!right2| 144 |b[2]|. Same as |right1|, except that |b| is a
  10134. two-byte quantity in the range |-32768<=b<32768|.
  10135. \yskip\hang|@!right3| 145 |b[3]|. Same as |right1|, except that |b| is a
  10136. three-byte quantity in the range |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  10137. \yskip\hang|@!right4| 146 |b[4]|. Same as |right1|, except that |b| is a
  10138. four-byte quantity in the range |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  10139. \yskip\hang|w0| 147. Set |h:=h+w|; i.e., move right |w| units. With luck,
  10140. this parameterless command will usually suffice, because the same kind of motion
  10141. will occur several times in succession; the following commands explain how
  10142. |w| gets particular values.
  10143. \yskip\hang|w1| 148 |b[1]|. Set |w:=b| and |h:=h+b|. The value of |b| is a
  10144. signed quantity in two's complement notation, |-128<=b<128|. This command
  10145. changes the current |w|~spacing and moves right by |b|.
  10146. \yskip\hang|@!w2| 149 |b[2]|. Same as |w1|, but |b| is two bytes long,
  10147. |-32768<=b<32768|.
  10148. \yskip\hang|@!w3| 150 |b[3]|. Same as |w1|, but |b| is three bytes long,
  10149. |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  10150. \yskip\hang|@!w4| 151 |b[4]|. Same as |w1|, but |b| is four bytes long,
  10151. |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  10152. \yskip\hang|x0| 152. Set |h:=h+x|; i.e., move right |x| units. The `|x|'
  10153. commands are like the `|w|' commands except that they involve |x| instead
  10154. of |w|.
  10155. \yskip\hang|x1| 153 |b[1]|. Set |x:=b| and |h:=h+b|. The value of |b| is a
  10156. signed quantity in two's complement notation, |-128<=b<128|. This command
  10157. changes the current |x|~spacing and moves right by |b|.
  10158. \yskip\hang|@!x2| 154 |b[2]|. Same as |x1|, but |b| is two bytes long,
  10159. |-32768<=b<32768|.
  10160. \yskip\hang|@!x3| 155 |b[3]|. Same as |x1|, but |b| is three bytes long,
  10161. |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  10162. \yskip\hang|@!x4| 156 |b[4]|. Same as |x1|, but |b| is four bytes long,
  10163. |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  10164. \yskip\hang|down1| 157 |a[1]|. Set |v:=v+a|, i.e., move down |a| units.
  10165. The parameter is a signed number in two's complement notation, |-128<=a<128|;
  10166. if |a<0|, the reference point moves up.
  10167. \yskip\hang|@!down2| 158 |a[2]|. Same as |down1|, except that |a| is a
  10168. two-byte quantity in the range |-32768<=a<32768|.
  10169. \yskip\hang|@!down3| 159 |a[3]|. Same as |down1|, except that |a| is a
  10170. three-byte quantity in the range |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  10171. \yskip\hang|@!down4| 160 |a[4]|. Same as |down1|, except that |a| is a
  10172. four-byte quantity in the range |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  10173. \yskip\hang|y0| 161. Set |v:=v+y|; i.e., move down |y| units. With luck,
  10174. this parameterless command will usually suffice, because the same kind of motion
  10175. will occur several times in succession; the following commands explain how
  10176. |y| gets particular values.
  10177. \yskip\hang|y1| 162 |a[1]|. Set |y:=a| and |v:=v+a|. The value of |a| is a
  10178. signed quantity in two's complement notation, |-128<=a<128|. This command
  10179. changes the current |y|~spacing and moves down by |a|.
  10180. \yskip\hang|@!y2| 163 |a[2]|. Same as |y1|, but |a| is two bytes long,
  10181. |-32768<=a<32768|.
  10182. \yskip\hang|@!y3| 164 |a[3]|. Same as |y1|, but |a| is three bytes long,
  10183. |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  10184. \yskip\hang|@!y4| 165 |a[4]|. Same as |y1|, but |a| is four bytes long,
  10185. |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  10186. \yskip\hang|z0| 166. Set |v:=v+z|; i.e., move down |z| units. The `|z|' commands
  10187. are like the `|y|' commands except that they involve |z| instead of |y|.
  10188. \yskip\hang|z1| 167 |a[1]|. Set |z:=a| and |v:=v+a|. The value of |a| is a
  10189. signed quantity in two's complement notation, |-128<=a<128|. This command
  10190. changes the current |z|~spacing and moves down by |a|.
  10191. \yskip\hang|@!z2| 168 |a[2]|. Same as |z1|, but |a| is two bytes long,
  10192. |-32768<=a<32768|.
  10193. \yskip\hang|@!z3| 169 |a[3]|. Same as |z1|, but |a| is three bytes long,
  10194. |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  10195. \yskip\hang|@!z4| 170 |a[4]|. Same as |z1|, but |a| is four bytes long,
  10196. |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  10197. \yskip\hang|fnt_num_0| 171. Set |f:=0|. Font 0 must previously have been
  10198. defined by a \\{fnt\_def} instruction, as explained below.
  10199. \yskip\hang\\{fnt\_num\_1} through \\{fnt\_num\_63} (opcodes 172 to 234). Set
  10200. |f:=1|, \dots, \hbox{|f:=63|}, respectively.
  10201. \yskip\hang|fnt1| 235 |k[1]|. Set |f:=k|. \TeX82 uses this command for font
  10202. numbers in the range |64<=k<256|.
  10203. \yskip\hang|@!fnt2| 236 |k[2]|. Same as |fnt1|, except that |k|~is two
  10204. bytes long, so it is in the range |0<=k<65536|. \TeX82 never generates this
  10205. command, but large font numbers may prove useful for specifications of
  10206. color or texture, or they may be used for special fonts that have fixed
  10207. numbers in some external coding scheme.
  10208. \yskip\hang|@!fnt3| 237 |k[3]|. Same as |fnt1|, except that |k|~is three
  10209. bytes long, so it can be as large as $2^{24}-1$.
  10210. \yskip\hang|@!fnt4| 238 |k[4]|. Same as |fnt1|, except that |k|~is four
  10211. bytes long; this is for the really big font numbers (and for the negative ones).
  10212. \yskip\hang|xxx1| 239 |k[1]| |x[k]|. This command is undefined in
  10213. general; it functions as a $(k+2)$-byte |nop| unless special \.{DVI}-reading
  10214. programs are being used. \TeX82 generates |xxx1| when a short enough
  10215. \.{\\special} appears, setting |k| to the number of bytes being sent. It
  10216. is recommended that |x| be a string having the form of a keyword followed
  10217. by possible parameters relevant to that keyword.
  10218. \yskip\hang|@!xxx2| 240 |k[2]| |x[k]|. Like |xxx1|, but |0<=k<65536|.
  10219. \yskip\hang|@!xxx3| 241 |k[3]| |x[k]|. Like |xxx1|, but |0<=k<@t$2^{24}$@>|.
  10220. \yskip\hang|xxx4| 242 |k[4]| |x[k]|. Like |xxx1|, but |k| can be ridiculously
  10221. large. \TeX82 uses |xxx4| when sending a string of length 256 or more.
  10222. \yskip\hang|fnt_def1| 243 |k[1]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  10223. Define font |k|, where |0<=k<256|; font definitions will be explained shortly.
  10224. \yskip\hang|@!fnt_def2| 244 |k[2]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  10225. Define font |k|, where |0<=k<65536|.
  10226. \yskip\hang|@!fnt_def3| 245 |k[3]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  10227. Define font |k|, where |0<=k<@t$2^{24}$@>|.
  10228. \yskip\hang|@!fnt_def4| 246 |k[4]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  10229. Define font |k|, where |@t$-2^{31}$@><=k<@t$2^{31}$@>|.
  10230. \yskip\hang|pre| 247 |i[1]| |num[4]| |den[4]| |mag[4]| |k[1]| |x[k]|.
  10231. Beginning of the preamble; this must come at the very beginning of the
  10232. file. Parameters |i|, |num|, |den|, |mag|, |k|, and |x| are explained below.
  10233. \yskip\hang|post| 248. Beginning of the postamble, see below.
  10234. \yskip\hang|post_post| 249. Ending of the postamble, see below.
  10235. \yskip\noindent Commands 250--255 are undefined at the present time.
  10236. @ @d set_char_0=0 {typeset character 0 and move right}
  10237. @d set1=128 {typeset a character and move right}
  10238. @d set_rule=132 {typeset a rule and move right}
  10239. @d put_rule=137 {typeset a rule}
  10240. @d nop=138 {no operation}
  10241. @d bop=139 {beginning of page}
  10242. @d eop=140 {ending of page}
  10243. @d push=141 {save the current positions}
  10244. @d pop=142 {restore previous positions}
  10245. @d right1=143 {move right}
  10246. @d w0=147 {move right by |w|}
  10247. @d w1=148 {move right and set |w|}
  10248. @d x0=152 {move right by |x|}
  10249. @d x1=153 {move right and set |x|}
  10250. @d down1=157 {move down}
  10251. @d y0=161 {move down by |y|}
  10252. @d y1=162 {move down and set |y|}
  10253. @d z0=166 {move down by |z|}
  10254. @d z1=167 {move down and set |z|}
  10255. @d fnt_num_0=171 {set current font to 0}
  10256. @d fnt1=235 {set current font}
  10257. @d xxx1=239 {extension to \.{DVI} primitives}
  10258. @d xxx4=242 {potentially long extension to \.{DVI} primitives}
  10259. @d fnt_def1=243 {define the meaning of a font number}
  10260. @d pre=247 {preamble}
  10261. @d post=248 {postamble beginning}
  10262. @d post_post=249 {postamble ending}
  10263. @ The preamble contains basic information about the file as a whole. As
  10264. stated above, there are six parameters:
  10265. $$\hbox{|@!i[1]| |@!num[4]| |@!den[4]| |@!mag[4]| |@!k[1]| |@!x[k]|.}$$
  10266. The |i| byte identifies \.{DVI} format; currently this byte is always set
  10267. to~2. (The value |i=3| is currently used for an extended format that
  10268. allows a mixture of right-to-left and left-to-right typesetting.
  10269. Some day we will set |i=4|, when \.{DVI} format makes another
  10270. incompatible change---perhaps in the year 2048.)
  10271. The next two parameters, |num| and |den|, are positive integers that define
  10272. the units of measurement; they are the numerator and denominator of a
  10273. fraction by which all dimensions in the \.{DVI} file could be multiplied
  10274. in order to get lengths in units of $10^{-7}$ meters. Since $\rm 7227{pt} =
  10275. 254{cm}$, and since \TeX\ works with scaled points where there are $2^{16}$
  10276. sp in a point, \TeX\ sets
  10277. $|num|/|den|=(254\cdot10^5)/(7227\cdot2^{16})=25400000/473628672$.
  10278. @^sp@>
  10279. The |mag| parameter is what \TeX\ calls \.{\\mag}, i.e., 1000 times the
  10280. desired magnification. The actual fraction by which dimensions are
  10281. multiplied is therefore $|mag|\cdot|num|/1000|den|$. Note that if a \TeX\
  10282. source document does not call for any `\.{true}' dimensions, and if you
  10283. change it only by specifying a different \.{\\mag} setting, the \.{DVI}
  10284. file that \TeX\ creates will be completely unchanged except for the value
  10285. of |mag| in the preamble and postamble. (Fancy \.{DVI}-reading programs allow
  10286. users to override the |mag|~setting when a \.{DVI} file is being printed.)
  10287. Finally, |k| and |x| allow the \.{DVI} writer to include a comment, which is not
  10288. interpreted further. The length of comment |x| is |k|, where |0<=k<256|.
  10289. @d id_byte=2 {identifies the kind of \.{DVI} files described here}
  10290. @ Font definitions for a given font number |k| contain further parameters
  10291. $$\hbox{|c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.}$$
  10292. The four-byte value |c| is the check sum that \TeX\ found in the \.{TFM}
  10293. file for this font; |c| should match the check sum of the font found by
  10294. programs that read this \.{DVI} file.
  10295. @^check sum@>
  10296. Parameter |s| contains a fixed-point scale factor that is applied to
  10297. the character widths in font |k|; font dimensions in \.{TFM} files and
  10298. other font files are relative to this quantity, which is called the
  10299. ``at size'' elsewhere in this documentation. The value of |s| is
  10300. always positive and less than $2^{27}$. It is given in the same units
  10301. as the other \.{DVI} dimensions, i.e., in sp when \TeX82 has made the
  10302. file. Parameter |d| is similar to |s|; it is the ``design size,'' and
  10303. (like~|s|) it is given in \.{DVI} units. Thus, font |k| is to be used
  10304. at $|mag|\cdot s/1000d$ times its normal size.
  10305. The remaining part of a font definition gives the external name of the font,
  10306. which is an ASCII string of length |a+l|. The number |a| is the length
  10307. of the ``area'' or directory, and |l| is the length of the font name itself;
  10308. the standard local system font area is supposed to be used when |a=0|.
  10309. The |n| field contains the area in its first |a| bytes.
  10310. Font definitions must appear before the first use of a particular font number.
  10311. Once font |k| is defined, it must not be defined again; however, we
  10312. shall see below that font definitions appear in the postamble as well as
  10313. in the pages, so in this sense each font number is defined exactly twice,
  10314. if at all. Like |nop| commands, font definitions can
  10315. appear before the first |bop|, or between an |eop| and a |bop|.
  10316. @ Sometimes it is desirable to make horizontal or vertical rules line up
  10317. precisely with certain features in characters of a font. It is possible to
  10318. guarantee the correct matching between \.{DVI} output and the characters
  10319. generated by \MF\ by adhering to the following principles: (1)~The \MF\
  10320. characters should be positioned so that a bottom edge or left edge that is
  10321. supposed to line up with the bottom or left edge of a rule appears at the
  10322. reference point, i.e., in row~0 and column~0 of the \MF\ raster. This
  10323. ensures that the position of the rule will not be rounded differently when
  10324. the pixel size is not a perfect multiple of the units of measurement in
  10325. the \.{DVI} file. (2)~A typeset rule of height $a>0$ and width $b>0$
  10326. should be equivalent to a \MF-generated character having black pixels in
  10327. precisely those raster positions whose \MF\ coordinates satisfy
  10328. |0<=x<@t$\alpha$@>b| and |0<=y<@t$\alpha$@>a|, where $\alpha$ is the number
  10329. of pixels per \.{DVI} unit.
  10330. @:METAFONT}{\MF@>
  10331. @^alignment of rules with characters@>
  10332. @^rules aligning with characters@>
  10333. @ The last page in a \.{DVI} file is followed by `|post|'; this command
  10334. introduces the postamble, which summarizes important facts that \TeX\ has
  10335. accumulated about the file, making it possible to print subsets of the data
  10336. with reasonable efficiency. The postamble has the form
  10337. $$\vbox{\halign{\hbox{#\hfil}\cr
  10338. |post| |p[4]| |num[4]| |den[4]| |mag[4]| |l[4]| |u[4]| |s[2]| |t[2]|\cr
  10339. $\langle\,$font definitions$\,\rangle$\cr
  10340. |post_post| |q[4]| |i[1]| 223's$[{\G}4]$\cr}}$$
  10341. Here |p| is a pointer to the final |bop| in the file. The next three
  10342. parameters, |num|, |den|, and |mag|, are duplicates of the quantities that
  10343. appeared in the preamble.
  10344. Parameters |l| and |u| give respectively the height-plus-depth of the tallest
  10345. page and the width of the widest page, in the same units as other dimensions
  10346. of the file. These numbers might be used by a \.{DVI}-reading program to
  10347. position individual ``pages'' on large sheets of film or paper; however,
  10348. the standard convention for output on normal size paper is to position each
  10349. page so that the upper left-hand corner is exactly one inch from the left
  10350. and the top. Experience has shown that it is unwise to design \.{DVI}-to-printer
  10351. software that attempts cleverly to center the output; a fixed position of
  10352. the upper left corner is easiest for users to understand and to work with.
  10353. Therefore |l| and~|u| are often ignored.
  10354. Parameter |s| is the maximum stack depth (i.e., the largest excess of
  10355. |push| commands over |pop| commands) needed to process this file. Then
  10356. comes |t|, the total number of pages (|bop| commands) present.
  10357. The postamble continues with font definitions, which are any number of
  10358. \\{fnt\_def} commands as described above, possibly interspersed with |nop|
  10359. commands. Each font number that is used in the \.{DVI} file must be defined
  10360. exactly twice: Once before it is first selected by a \\{fnt} command, and once
  10361. in the postamble.
  10362. @ The last part of the postamble, following the |post_post| byte that
  10363. signifies the end of the font definitions, contains |q|, a pointer to the
  10364. |post| command that started the postamble. An identification byte, |i|,
  10365. comes next; this currently equals~2, as in the preamble.
  10366. The |i| byte is followed by four or more bytes that are all equal to
  10367. the decimal number 223 (i.e., @'337 in octal). \TeX\ puts out four to seven of
  10368. these trailing bytes, until the total length of the file is a multiple of
  10369. four bytes, since this works out best on machines that pack four bytes per
  10370. word; but any number of 223's is allowed, as long as there are at least four
  10371. of them. In effect, 223 is a sort of signature that is added at the very end.
  10372. @^Fuchs, David Raymond@>
  10373. This curious way to finish off a \.{DVI} file makes it feasible for
  10374. \.{DVI}-reading programs to find the postamble first, on most computers,
  10375. even though \TeX\ wants to write the postamble last. Most operating
  10376. systems permit random access to individual words or bytes of a file, so
  10377. the \.{DVI} reader can start at the end and skip backwards over the 223's
  10378. until finding the identification byte. Then it can back up four bytes, read
  10379. |q|, and move to byte |q| of the file. This byte should, of course,
  10380. contain the value 248 (|post|); now the postamble can be read, so the
  10381. \.{DVI} reader can discover all the information needed for typesetting the
  10382. pages. Note that it is also possible to skip through the \.{DVI} file at
  10383. reasonably high speed to locate a particular page, if that proves
  10384. desirable. This saves a lot of time, since \.{DVI} files used in production
  10385. jobs tend to be large.
  10386. Unfortunately, however, standard \PASCAL\ does not include the ability to
  10387. @^system dependencies@>
  10388. access a random position in a file, or even to determine the length of a file.
  10389. Almost all systems nowadays provide the necessary capabilities, so \.{DVI}
  10390. format has been designed to work most efficiently with modern operating systems.
  10391. But if \.{DVI} files have to be processed under the restrictions of standard
  10392. \PASCAL, one can simply read them from front to back, since the necessary
  10393. header information is present in the preamble and in the font definitions.
  10394. (The |l| and |u| and |s| and |t| parameters, which appear only in the
  10395. postamble, are ``frills'' that are handy but not absolutely necessary.)
  10396. @* \[32] Shipping pages out.
  10397. After considering \TeX's eyes and stomach, we come now to the bowels.
  10398. @^bowels@>
  10399. The |ship_out| procedure is given a pointer to a box; its mission is
  10400. to describe that box in \.{DVI} form, outputting a ``page'' to |dvi_file|.
  10401. The \.{DVI} coordinates $(h,v)=(0,0)$ should correspond to the upper left
  10402. corner of the box being shipped.
  10403. Since boxes can be inside of boxes inside of boxes, the main work of
  10404. |ship_out| is done by two mutually recursive routines, |hlist_out|
  10405. and |vlist_out|, which traverse the hlists and vlists inside of horizontal
  10406. and vertical boxes.
  10407. As individual pages are being processed, we need to accumulate
  10408. information about the entire set of pages, since such statistics must be
  10409. reported in the postamble. The global variables |total_pages|, |max_v|,
  10410. |max_h|, |max_push|, and |last_bop| are used to record this information.
  10411. The variable |doing_leaders| is |true| while leaders are being output.
  10412. The variable |dead_cycles| contains the number of times an output routine
  10413. has been initiated since the last |ship_out|.
  10414. A few additional global variables are also defined here for use in
  10415. |vlist_out| and |hlist_out|. They could have been local variables, but
  10416. that would waste stack space when boxes are deeply nested, since the
  10417. values of these variables are not needed during recursive calls.
  10418. @^recursion@>
  10419. @<Glob...@>=
  10420. @!total_pages:integer; {the number of pages that have been shipped out}
  10421. @!max_v:scaled; {maximum height-plus-depth of pages shipped so far}
  10422. @!max_h:scaled; {maximum width of pages shipped so far}
  10423. @!max_push:integer; {deepest nesting of |push| commands encountered so far}
  10424. @!last_bop:integer; {location of previous |bop| in the \.{DVI} output}
  10425. @!dead_cycles:integer; {recent outputs that didn't ship anything out}
  10426. @!doing_leaders:boolean; {are we inside a leader box?}
  10427. @#
  10428. @!c,@!f:quarterword; {character and font in current |char_node|}
  10429. @!rule_ht,@!rule_dp,@!rule_wd:scaled; {size of current rule being output}
  10430. @!g:pointer; {current glue specification}
  10431. @!lq,@!lr:integer; {quantities used in calculations for leaders}
  10432. @ @<Set init...@>=
  10433. total_pages:=0; max_v:=0; max_h:=0; max_push:=0; last_bop:=-1;
  10434. doing_leaders:=false; dead_cycles:=0; cur_s:=-1;
  10435. @ The \.{DVI} bytes are output to a buffer instead of being written directly
  10436. to the output file. This makes it possible to reduce the overhead of
  10437. subroutine calls, thereby measurably speeding up the computation, since
  10438. output of \.{DVI} bytes is part of \TeX's inner loop. And it has another
  10439. advantage as well, since we can change instructions in the buffer in order to
  10440. make the output more compact. For example, a `|down2|' command can be
  10441. changed to a `|y2|', thereby making a subsequent `|y0|' command possible,
  10442. saving two bytes.
  10443. The output buffer is divided into two parts of equal size; the bytes found
  10444. in |dvi_buf[0..half_buf-1]| constitute the first half, and those in
  10445. |dvi_buf[half_buf..dvi_buf_size-1]| constitute the second. The global
  10446. variable |dvi_ptr| points to the position that will receive the next
  10447. output byte. When |dvi_ptr| reaches |dvi_limit|, which is always equal
  10448. to one of the two values |half_buf| or |dvi_buf_size|, the half buffer that
  10449. is about to be invaded next is sent to the output and |dvi_limit| is
  10450. changed to its other value. Thus, there is always at least a half buffer's
  10451. worth of information present, except at the very beginning of the job.
  10452. Bytes of the \.{DVI} file are numbered sequentially starting with 0;
  10453. the next byte to be generated will be number |dvi_offset+dvi_ptr|.
  10454. A byte is present in the buffer only if its number is |>=dvi_gone|.
  10455. @<Types...@>=
  10456. @!dvi_index=0..dvi_buf_size; {an index into the output buffer}
  10457. @ Some systems may find it more efficient to make |dvi_buf| a |packed|
  10458. array, since output of four bytes at once may be facilitated.
  10459. @^system dependencies@>
  10460. @<Glob...@>=
  10461. @!dvi_buf:array[dvi_index] of eight_bits; {buffer for \.{DVI} output}
  10462. @!half_buf:dvi_index; {half of |dvi_buf_size|}
  10463. @!dvi_limit:dvi_index; {end of the current half buffer}
  10464. @!dvi_ptr:dvi_index; {the next available buffer address}
  10465. @!dvi_offset:integer; {|dvi_buf_size| times the number of times the
  10466. output buffer has been fully emptied}
  10467. @!dvi_gone:integer; {the number of bytes already output to |dvi_file|}
  10468. @ Initially the buffer is all in one piece; we will output half of it only
  10469. after it first fills up.
  10470. @<Set init...@>=
  10471. half_buf:=dvi_buf_size div 2; dvi_limit:=dvi_buf_size; dvi_ptr:=0;
  10472. dvi_offset:=0; dvi_gone:=0;
  10473. @ The actual output of |dvi_buf[a..b]| to |dvi_file| is performed by calling
  10474. |write_dvi(a,b)|. For best results, this procedure should be optimized to
  10475. run as fast as possible on each particular system, since it is part of
  10476. \TeX's inner loop. It is safe to assume that |a| and |b+1| will both be
  10477. multiples of 4 when |write_dvi(a,b)| is called; therefore it is possible on
  10478. many machines to use efficient methods to pack four bytes per word and to
  10479. output an array of words with one system call.
  10480. @^system dependencies@>
  10481. @^inner loop@>
  10482. @^defecation@>
  10483. @p procedure write_dvi(@!a,@!b:dvi_index);
  10484. var k:dvi_index;
  10485. begin for k:=a to b do write(dvi_file,dvi_buf[k]);
  10486. end;
  10487. @ To put a byte in the buffer without paying the cost of invoking a procedure
  10488. each time, we use the macro |dvi_out|.
  10489. @d dvi_out(#)==@+begin dvi_buf[dvi_ptr]:=#; incr(dvi_ptr);
  10490. if dvi_ptr=dvi_limit then dvi_swap;
  10491. end
  10492. @p procedure dvi_swap; {outputs half of the buffer}
  10493. begin if dvi_limit=dvi_buf_size then
  10494. begin write_dvi(0,half_buf-1); dvi_limit:=half_buf;
  10495. dvi_offset:=dvi_offset+dvi_buf_size; dvi_ptr:=0;
  10496. end
  10497. else begin write_dvi(half_buf,dvi_buf_size-1); dvi_limit:=dvi_buf_size;
  10498. end;
  10499. dvi_gone:=dvi_gone+half_buf;
  10500. end;
  10501. @ Here is how we clean out the buffer when \TeX\ is all through; |dvi_ptr|
  10502. will be a multiple of~4.
  10503. @<Empty the last bytes out of |dvi_buf|@>=
  10504. if dvi_limit=half_buf then write_dvi(half_buf,dvi_buf_size-1);
  10505. if dvi_ptr>0 then write_dvi(0,dvi_ptr-1)
  10506. @ The |dvi_four| procedure outputs four bytes in two's complement notation,
  10507. without risking arithmetic overflow.
  10508. @p procedure dvi_four(@!x:integer);
  10509. begin if x>=0 then dvi_out(x div @'100000000)
  10510. else begin x:=x+@'10000000000;
  10511. x:=x+@'10000000000;
  10512. dvi_out((x div @'100000000) + 128);
  10513. end;
  10514. x:=x mod @'100000000; dvi_out(x div @'200000);
  10515. x:=x mod @'200000; dvi_out(x div @'400);
  10516. dvi_out(x mod @'400);
  10517. end;
  10518. @ A mild optimization of the output is performed by the |dvi_pop|
  10519. routine, which issues a |pop| unless it is possible to cancel a
  10520. `|push| |pop|' pair. The parameter to |dvi_pop| is the byte address
  10521. following the old |push| that matches the new |pop|.
  10522. @p procedure dvi_pop(@!l:integer);
  10523. begin if (l=dvi_offset+dvi_ptr)and(dvi_ptr>0) then decr(dvi_ptr)
  10524. else dvi_out(pop);
  10525. end;
  10526. @ Here's a procedure that outputs a font definition. Since \TeX82 uses at
  10527. most 256 different fonts per job, |fnt_def1| is always used as the command code.
  10528. @p procedure dvi_font_def(@!f:internal_font_number);
  10529. var k:pool_pointer; {index into |str_pool|}
  10530. begin dvi_out(fnt_def1);
  10531. dvi_out(f-font_base-1);@/
  10532. dvi_out(qo(font_check[f].b0));
  10533. dvi_out(qo(font_check[f].b1));
  10534. dvi_out(qo(font_check[f].b2));
  10535. dvi_out(qo(font_check[f].b3));@/
  10536. dvi_four(font_size[f]);
  10537. dvi_four(font_dsize[f]);@/
  10538. dvi_out(length(font_area[f]));
  10539. dvi_out(length(font_name[f]));
  10540. @<Output the font name whose internal number is |f|@>;
  10541. end;
  10542. @ @<Output the font name whose internal number is |f|@>=
  10543. for k:=str_start[font_area[f]] to str_start[font_area[f]+1]-1 do
  10544. dvi_out(so(str_pool[k]));
  10545. for k:=str_start[font_name[f]] to str_start[font_name[f]+1]-1 do
  10546. dvi_out(so(str_pool[k]))
  10547. @ Versions of \TeX\ intended for small computers might well choose to omit
  10548. the ideas in the next few parts of this program, since it is not really
  10549. necessary to optimize the \.{DVI} code by making use of the |w0|, |x0|,
  10550. |y0|, and |z0| commands. Furthermore, the algorithm that we are about to
  10551. describe does not pretend to give an optimum reduction in the length
  10552. of the \.{DVI} code; after all, speed is more important than compactness.
  10553. But the method is surprisingly effective, and it takes comparatively little
  10554. time.
  10555. We can best understand the basic idea by first considering a simpler problem
  10556. that has the same essential characteristics. Given a sequence of digits,
  10557. say $3\,1\,4\,1\,5\,9\,2\,6\,5\,3\,5\,8\,9$, we want to assign subscripts
  10558. $d$, $y$, or $z$ to each digit so as to maximize the number of ``$y$-hits''
  10559. and ``$z$-hits''; a $y$-hit is an instance of two appearances of the same
  10560. digit with the subscript $y$, where no $y$'s intervene between the two
  10561. appearances, and a $z$-hit is defined similarly. For example, the sequence
  10562. above could be decorated with subscripts as follows:
  10563. $$3_z\,1_y\,4_d\,1_y\,5_y\,9_d\,2_d\,6_d\,5_y\,3_z\,5_y\,8_d\,9_d.$$
  10564. There are three $y$-hits ($1_y\ldots1_y$ and $5_y\ldots5_y\ldots5_y$) and
  10565. one $z$-hit ($3_z\ldots3_z$); there are no $d$-hits, since the two appearances
  10566. of $9_d$ have $d$'s between them, but we don't count $d$-hits so it doesn't
  10567. matter how many there are. These subscripts are analogous to the \.{DVI}
  10568. commands called \\{down}, $y$, and $z$, and the digits are analogous to
  10569. different amounts of vertical motion; a $y$-hit or $z$-hit corresponds to
  10570. the opportunity to use the one-byte commands |y0| or |z0| in a \.{DVI} file.
  10571. \TeX's method of assigning subscripts works like this: Append a new digit,
  10572. say $\delta$, to the right of the sequence. Now look back through the
  10573. sequence until one of the following things happens: (a)~You see
  10574. $\delta_y$ or $\delta_z$, and this was the first time you encountered a
  10575. $y$ or $z$ subscript, respectively. Then assign $y$ or $z$ to the new
  10576. $\delta$; you have scored a hit. (b)~You see $\delta_d$, and no $y$
  10577. subscripts have been encountered so far during this search. Then change
  10578. the previous $\delta_d$ to $\delta_y$ (this corresponds to changing a
  10579. command in the output buffer), and assign $y$ to the new $\delta$; it's
  10580. another hit. (c)~You see $\delta_d$, and a $y$ subscript has been seen
  10581. but not a $z$. Change the previous $\delta_d$ to $\delta_z$ and assign
  10582. $z$ to the new $\delta$. (d)~You encounter both $y$ and $z$ subscripts
  10583. before encountering a suitable $\delta$, or you scan all the way to the
  10584. front of the sequence. Assign $d$ to the new $\delta$; this assignment may
  10585. be changed later.
  10586. The subscripts $3_z\,1_y\,4_d\ldots\,$ in the example above were, in fact,
  10587. produced by this procedure, as the reader can verify. (Go ahead and try it.)
  10588. @ In order to implement such an idea, \TeX\ maintains a stack of pointers
  10589. to the \\{down}, $y$, and $z$ commands that have been generated for the
  10590. current page. And there is a similar stack for \\{right}, |w|, and |x|
  10591. commands. These stacks are called the down stack and right stack, and their
  10592. top elements are maintained in the variables |down_ptr| and |right_ptr|.
  10593. Each entry in these stacks contains four fields: The |width| field is
  10594. the amount of motion down or to the right; the |location| field is the
  10595. byte number of the \.{DVI} command in question (including the appropriate
  10596. |dvi_offset|); the |link| field points to the next item below this one
  10597. on the stack; and the |info| field encodes the options for possible change
  10598. in the \.{DVI} command.
  10599. @d movement_node_size=3 {number of words per entry in the down and right stacks}
  10600. @d location(#)==mem[#+2].int {\.{DVI} byte number for a movement command}
  10601. @<Glob...@>=
  10602. @!down_ptr,@!right_ptr:pointer; {heads of the down and right stacks}
  10603. @ @<Set init...@>=
  10604. down_ptr:=null; right_ptr:=null;
  10605. @ Here is a subroutine that produces a \.{DVI} command for some specified
  10606. downward or rightward motion. It has two parameters: |w| is the amount
  10607. of motion, and |o| is either |down1| or |right1|. We use the fact that
  10608. the command codes have convenient arithmetic properties: |y1-down1=w1-right1|
  10609. and |z1-down1=x1-right1|.
  10610. @p procedure movement(@!w:scaled;@!o:eight_bits);
  10611. label exit,found,not_found,2,1;
  10612. var mstate:small_number; {have we seen a |y| or |z|?}
  10613. @!p,@!q:pointer; {current and top nodes on the stack}
  10614. @!k:integer; {index into |dvi_buf|, modulo |dvi_buf_size|}
  10615. begin q:=get_node(movement_node_size); {new node for the top of the stack}
  10616. width(q):=w; location(q):=dvi_offset+dvi_ptr;
  10617. if o=down1 then
  10618. begin link(q):=down_ptr; down_ptr:=q;
  10619. end
  10620. else begin link(q):=right_ptr; right_ptr:=q;
  10621. end;
  10622. @<Look at the other stack entries until deciding what sort of \.{DVI} command
  10623. to generate; |goto found| if node |p| is a ``hit''@>;
  10624. @<Generate a |down| or |right| command for |w| and |return|@>;
  10625. found: @<Generate a |y0| or |z0| command in order to reuse a previous
  10626. appearance of~|w|@>;
  10627. exit:end;
  10628. @ The |info| fields in the entries of the down stack or the right stack
  10629. have six possible settings: |y_here| or |z_here| mean that the \.{DVI}
  10630. command refers to |y| or |z|, respectively (or to |w| or |x|, in the
  10631. case of horizontal motion); |yz_OK| means that the \.{DVI} command is
  10632. \\{down} (or \\{right}) but can be changed to either |y| or |z| (or
  10633. to either |w| or |x|); |y_OK| means that it is \\{down} and can be changed
  10634. to |y| but not |z|; |z_OK| is similar; and |d_fixed| means it must stay
  10635. \\{down}.
  10636. The four settings |yz_OK|, |y_OK|, |z_OK|, |d_fixed| would not need to
  10637. be distinguished from each other if we were simply solving the
  10638. digit-subscripting problem mentioned above. But in \TeX's case there is
  10639. a complication because of the nested structure of |push| and |pop|
  10640. commands. Suppose we add parentheses to the digit-subscripting problem,
  10641. redefining hits so that $\delta_y\ldots \delta_y$ is a hit if all $y$'s between
  10642. the $\delta$'s are enclosed in properly nested parentheses, and if the
  10643. parenthesis level of the right-hand $\delta_y$ is deeper than or equal to
  10644. that of the left-hand one. Thus, `(' and `)' correspond to `|push|'
  10645. and `|pop|'. Now if we want to assign a subscript to the final 1 in the
  10646. sequence
  10647. $$2_y\,7_d\,1_d\,(\,8_z\,2_y\,8_z\,)\,1$$
  10648. we cannot change the previous $1_d$ to $1_y$, since that would invalidate
  10649. the $2_y\ldots2_y$ hit. But we can change it to $1_z$, scoring a hit
  10650. since the intervening $8_z$'s are enclosed in parentheses.
  10651. The program below removes movement nodes that are introduced after a |push|,
  10652. before it outputs the corresponding |pop|.
  10653. @d y_here=1 {|info| when the movement entry points to a |y| command}
  10654. @d z_here=2 {|info| when the movement entry points to a |z| command}
  10655. @d yz_OK=3 {|info| corresponding to an unconstrained \\{down} command}
  10656. @d y_OK=4 {|info| corresponding to a \\{down} that can't become a |z|}
  10657. @d z_OK=5 {|info| corresponding to a \\{down} that can't become a |y|}
  10658. @d d_fixed=6 {|info| corresponding to a \\{down} that can't change}
  10659. @ When the |movement| procedure gets to the label |found|, the value of
  10660. |info(p)| will be either |y_here| or |z_here|. If it is, say, |y_here|,
  10661. the procedure generates a |y0| command (or a |w0| command), and marks
  10662. all |info| fields between |q| and |p| so that |y| is not OK in that range.
  10663. @<Generate a |y0| or |z0| command...@>=
  10664. info(q):=info(p);
  10665. if info(q)=y_here then
  10666. begin dvi_out(o+y0-down1); {|y0| or |w0|}
  10667. while link(q)<>p do
  10668. begin q:=link(q);
  10669. case info(q) of
  10670. yz_OK: info(q):=z_OK;
  10671. y_OK: info(q):=d_fixed;
  10672. othercases do_nothing
  10673. endcases;
  10674. end;
  10675. end
  10676. else begin dvi_out(o+z0-down1); {|z0| or |x0|}
  10677. while link(q)<>p do
  10678. begin q:=link(q);
  10679. case info(q) of
  10680. yz_OK: info(q):=y_OK;
  10681. z_OK: info(q):=d_fixed;
  10682. othercases do_nothing
  10683. endcases;
  10684. end;
  10685. end
  10686. @ @<Generate a |down| or |right|...@>=
  10687. info(q):=yz_OK;
  10688. if abs(w)>=@'40000000 then
  10689. begin dvi_out(o+3); {|down4| or |right4|}
  10690. dvi_four(w); return;
  10691. end;
  10692. if abs(w)>=@'100000 then
  10693. begin dvi_out(o+2); {|down3| or |right3|}
  10694. if w<0 then w:=w+@'100000000;
  10695. dvi_out(w div @'200000); w:=w mod @'200000; goto 2;
  10696. end;
  10697. if abs(w)>=@'200 then
  10698. begin dvi_out(o+1); {|down2| or |right2|}
  10699. if w<0 then w:=w+@'200000;
  10700. goto 2;
  10701. end;
  10702. dvi_out(o); {|down1| or |right1|}
  10703. if w<0 then w:=w+@'400;
  10704. goto 1;
  10705. 2: dvi_out(w div @'400);
  10706. 1: dvi_out(w mod @'400); return
  10707. @ As we search through the stack, we are in one of three states,
  10708. |y_seen|, |z_seen|, or |none_seen|, depending on whether we have
  10709. encountered |y_here| or |z_here| nodes. These states are encoded as
  10710. multiples of 6, so that they can be added to the |info| fields for quick
  10711. decision-making.
  10712. @^inner loop@>
  10713. @d none_seen=0 {no |y_here| or |z_here| nodes have been encountered yet}
  10714. @d y_seen=6 {we have seen |y_here| but not |z_here|}
  10715. @d z_seen=12 {we have seen |z_here| but not |y_here|}
  10716. @<Look at the other stack entries until deciding...@>=
  10717. p:=link(q); mstate:=none_seen;
  10718. while p<>null do
  10719. begin if width(p)=w then @<Consider a node with matching width;
  10720. |goto found| if it's a hit@>
  10721. else case mstate+info(p) of
  10722. none_seen+y_here: mstate:=y_seen;
  10723. none_seen+z_here: mstate:=z_seen;
  10724. y_seen+z_here,z_seen+y_here: goto not_found;
  10725. othercases do_nothing
  10726. endcases;
  10727. p:=link(p);
  10728. end;
  10729. not_found:
  10730. @ We might find a valid hit in a |y| or |z| byte that is already gone
  10731. from the buffer. But we can't change bytes that are gone forever; ``the
  10732. moving finger writes, $\ldots\,\,$.''
  10733. @<Consider a node with matching width...@>=
  10734. case mstate+info(p) of
  10735. none_seen+yz_OK,none_seen+y_OK,z_seen+yz_OK,z_seen+y_OK:@t@>@;@/
  10736. if location(p)<dvi_gone then goto not_found
  10737. else @<Change buffered instruction to |y| or |w| and |goto found|@>;
  10738. none_seen+z_OK,y_seen+yz_OK,y_seen+z_OK:@t@>@;@/
  10739. if location(p)<dvi_gone then goto not_found
  10740. else @<Change buffered instruction to |z| or |x| and |goto found|@>;
  10741. none_seen+y_here,none_seen+z_here,y_seen+z_here,z_seen+y_here: goto found;
  10742. othercases do_nothing
  10743. endcases
  10744. @ @<Change buffered instruction to |y| or |w| and |goto found|@>=
  10745. begin k:=location(p)-dvi_offset;
  10746. if k<0 then k:=k+dvi_buf_size;
  10747. dvi_buf[k]:=dvi_buf[k]+y1-down1;
  10748. info(p):=y_here; goto found;
  10749. end
  10750. @ @<Change buffered instruction to |z| or |x| and |goto found|@>=
  10751. begin k:=location(p)-dvi_offset;
  10752. if k<0 then k:=k+dvi_buf_size;
  10753. dvi_buf[k]:=dvi_buf[k]+z1-down1;
  10754. info(p):=z_here; goto found;
  10755. end
  10756. @ In case you are wondering when all the movement nodes are removed from
  10757. \TeX's memory, the answer is that they are recycled just before
  10758. |hlist_out| and |vlist_out| finish outputting a box. This restores the
  10759. down and right stacks to the state they were in before the box was output,
  10760. except that some |info|'s may have become more restrictive.
  10761. @p procedure prune_movements(@!l:integer);
  10762. {delete movement nodes with |location>=l|}
  10763. label done,exit;
  10764. var p:pointer; {node being deleted}
  10765. begin while down_ptr<>null do
  10766. begin if location(down_ptr)<l then goto done;
  10767. p:=down_ptr; down_ptr:=link(p); free_node(p,movement_node_size);
  10768. end;
  10769. done: while right_ptr<>null do
  10770. begin if location(right_ptr)<l then return;
  10771. p:=right_ptr; right_ptr:=link(p); free_node(p,movement_node_size);
  10772. end;
  10773. exit:end;
  10774. @ The actual distances by which we want to move might be computed as the
  10775. sum of several separate movements. For example, there might be several
  10776. glue nodes in succession, or we might want to move right by the width of
  10777. some box plus some amount of glue. More importantly, the baselineskip
  10778. distances are computed in terms of glue together with the depth and
  10779. height of adjacent boxes, and we want the \.{DVI} file to lump these
  10780. three quantities together into a single motion.
  10781. Therefore, \TeX\ maintains two pairs of global variables: |dvi_h| and |dvi_v|
  10782. are the |h| and |v| coordinates corresponding to the commands actually
  10783. output to the \.{DVI} file, while |cur_h| and |cur_v| are the coordinates
  10784. corresponding to the current state of the output routines. Coordinate
  10785. changes will accumulate in |cur_h| and |cur_v| without being reflected
  10786. in the output, until such a change becomes necessary or desirable; we
  10787. can call the |movement| procedure whenever we want to make |dvi_h=cur_h|
  10788. or |dvi_v=cur_v|.
  10789. The current font reflected in the \.{DVI} output is called |dvi_f|;
  10790. there is no need for a `\\{cur\_f}' variable.
  10791. The depth of nesting of |hlist_out| and |vlist_out| is called |cur_s|;
  10792. this is essentially the depth of |push| commands in the \.{DVI} output.
  10793. @d synch_h==if cur_h<>dvi_h then
  10794. begin movement(cur_h-dvi_h,right1); dvi_h:=cur_h;
  10795. end
  10796. @d synch_v==if cur_v<>dvi_v then
  10797. begin movement(cur_v-dvi_v,down1); dvi_v:=cur_v;
  10798. end
  10799. @<Glob...@>=
  10800. @!dvi_h,@!dvi_v:scaled; {a \.{DVI} reader program thinks we are here}
  10801. @!cur_h,@!cur_v:scaled; {\TeX\ thinks we are here}
  10802. @!dvi_f:internal_font_number; {the current font}
  10803. @!cur_s:integer; {current depth of output box nesting, initially $-1$}
  10804. @ @<Initialize variables as |ship_out| begins@>=
  10805. dvi_h:=0; dvi_v:=0; cur_h:=h_offset; dvi_f:=null_font;
  10806. ensure_dvi_open;
  10807. if total_pages=0 then
  10808. begin dvi_out(pre); dvi_out(id_byte); {output the preamble}
  10809. @^preamble of \.{DVI} file@>
  10810. dvi_four(25400000); dvi_four(473628672); {conversion ratio for sp}
  10811. prepare_mag; dvi_four(mag); {magnification factor is frozen}
  10812. old_setting:=selector; selector:=new_string;
  10813. print(" TeX output "); print_int(year); print_char(".");
  10814. print_two(month); print_char("."); print_two(day);
  10815. print_char(":"); print_two(time div 60);
  10816. print_two(time mod 60);
  10817. selector:=old_setting; dvi_out(cur_length);
  10818. for s:=str_start[str_ptr] to pool_ptr-1 do dvi_out(so(str_pool[s]));
  10819. pool_ptr:=str_start[str_ptr]; {flush the current string}
  10820. end
  10821. @ When |hlist_out| is called, its duty is to output the box represented
  10822. by the |hlist_node| pointed to by |temp_ptr|. The reference point of that
  10823. box has coordinates |(cur_h,cur_v)|.
  10824. Similarly, when |vlist_out| is called, its duty is to output the box represented
  10825. by the |vlist_node| pointed to by |temp_ptr|. The reference point of that
  10826. box has coordinates |(cur_h,cur_v)|.
  10827. @^recursion@>
  10828. @p procedure@?vlist_out; forward; {|hlist_out| and |vlist_out| are mutually
  10829. recursive}
  10830. @ The recursive procedures |hlist_out| and |vlist_out| each have local variables
  10831. |save_h| and |save_v| to hold the values of |dvi_h| and |dvi_v| just before
  10832. entering a new level of recursion. In effect, the values of |save_h| and
  10833. |save_v| on \TeX's run-time stack correspond to the values of |h| and |v|
  10834. that a \.{DVI}-reading program will push onto its coordinate stack.
  10835. @d move_past=13 {go to this label when advancing past glue or a rule}
  10836. @d fin_rule=14 {go to this label to finish processing a rule}
  10837. @d next_p=15 {go to this label when finished with node |p|}
  10838. @p @t\4@>@<Declare procedures needed in |hlist_out|, |vlist_out|@>@t@>@/
  10839. procedure hlist_out; {output an |hlist_node| box}
  10840. label reswitch, move_past, fin_rule, next_p;
  10841. var base_line: scaled; {the baseline coordinate for this box}
  10842. @!left_edge: scaled; {the left coordinate for this box}
  10843. @!save_h,@!save_v: scaled; {what |dvi_h| and |dvi_v| should pop to}
  10844. @!this_box: pointer; {pointer to containing box}
  10845. @!g_order: glue_ord; {applicable order of infinity for glue}
  10846. @!g_sign: normal..shrinking; {selects type of glue}
  10847. @!p:pointer; {current position in the hlist}
  10848. @!save_loc:integer; {\.{DVI} byte location upon entry}
  10849. @!leader_box:pointer; {the leader box being replicated}
  10850. @!leader_wd:scaled; {width of leader box being replicated}
  10851. @!lx:scaled; {extra space between leader boxes}
  10852. @!outer_doing_leaders:boolean; {were we doing leaders?}
  10853. @!edge:scaled; {left edge of sub-box, or right edge of leader space}
  10854. @!glue_temp:real; {glue value before rounding}
  10855. @!cur_glue:real; {glue seen so far}
  10856. @!cur_g:scaled; {rounded equivalent of |cur_glue| times the glue ratio}
  10857. begin cur_g:=0; cur_glue:=float_constant(0);
  10858. this_box:=temp_ptr; g_order:=glue_order(this_box);
  10859. g_sign:=glue_sign(this_box); p:=list_ptr(this_box);
  10860. incr(cur_s);
  10861. if cur_s>0 then dvi_out(push);
  10862. if cur_s>max_push then max_push:=cur_s;
  10863. save_loc:=dvi_offset+dvi_ptr; base_line:=cur_v; left_edge:=cur_h;
  10864. while p<>null do @<Output node |p| for |hlist_out| and move to the next node,
  10865. maintaining the condition |cur_v=base_line|@>;
  10866. prune_movements(save_loc);
  10867. if cur_s>0 then dvi_pop(save_loc);
  10868. decr(cur_s);
  10869. end;
  10870. @ We ought to give special care to the efficiency of one part of |hlist_out|,
  10871. since it belongs to \TeX's inner loop. When a |char_node| is encountered,
  10872. we save a little time by processing several nodes in succession until
  10873. reaching a non-|char_node|. The program uses the fact that |set_char_0=0|.
  10874. @^inner loop@>
  10875. @<Output node |p| for |hlist_out|...@>=
  10876. reswitch: if is_char_node(p) then
  10877. begin synch_h; synch_v;
  10878. repeat f:=font(p); c:=character(p);
  10879. if f<>dvi_f then @<Change font |dvi_f| to |f|@>;
  10880. if c>=qi(128) then dvi_out(set1);
  10881. dvi_out(qo(c));@/
  10882. cur_h:=cur_h+char_width(f)(char_info(f)(c));
  10883. p:=link(p);
  10884. until not is_char_node(p);
  10885. dvi_h:=cur_h;
  10886. end
  10887. else @<Output the non-|char_node| |p| for |hlist_out|
  10888. and move to the next node@>
  10889. @ @<Change font |dvi_f| to |f|@>=
  10890. begin if not font_used[f] then
  10891. begin dvi_font_def(f); font_used[f]:=true;
  10892. end;
  10893. if f<=64+font_base then dvi_out(f-font_base-1+fnt_num_0)
  10894. else begin dvi_out(fnt1); dvi_out(f-font_base-1);
  10895. end;
  10896. dvi_f:=f;
  10897. end
  10898. @ @<Output the non-|char_node| |p| for |hlist_out|...@>=
  10899. begin case type(p) of
  10900. hlist_node,vlist_node:@<Output a box in an hlist@>;
  10901. rule_node: begin rule_ht:=height(p); rule_dp:=depth(p); rule_wd:=width(p);
  10902. goto fin_rule;
  10903. end;
  10904. whatsit_node: @<Output the whatsit node |p| in an hlist@>;
  10905. glue_node: @<Move right or output leaders@>;
  10906. kern_node,math_node:cur_h:=cur_h+width(p);
  10907. ligature_node: @<Make node |p| look like a |char_node| and |goto reswitch|@>;
  10908. othercases do_nothing
  10909. endcases;@/
  10910. goto next_p;
  10911. fin_rule: @<Output a rule in an hlist@>;
  10912. move_past: cur_h:=cur_h+rule_wd;
  10913. next_p:p:=link(p);
  10914. end
  10915. @ @<Output a box in an hlist@>=
  10916. if list_ptr(p)=null then cur_h:=cur_h+width(p)
  10917. else begin save_h:=dvi_h; save_v:=dvi_v;
  10918. cur_v:=base_line+shift_amount(p); {shift the box down}
  10919. temp_ptr:=p; edge:=cur_h;
  10920. if type(p)=vlist_node then vlist_out@+else hlist_out;
  10921. dvi_h:=save_h; dvi_v:=save_v;
  10922. cur_h:=edge+width(p); cur_v:=base_line;
  10923. end
  10924. @ @<Output a rule in an hlist@>=
  10925. if is_running(rule_ht) then rule_ht:=height(this_box);
  10926. if is_running(rule_dp) then rule_dp:=depth(this_box);
  10927. rule_ht:=rule_ht+rule_dp; {this is the rule thickness}
  10928. if (rule_ht>0)and(rule_wd>0) then {we don't output empty rules}
  10929. begin synch_h; cur_v:=base_line+rule_dp; synch_v;
  10930. dvi_out(set_rule); dvi_four(rule_ht); dvi_four(rule_wd);
  10931. cur_v:=base_line; dvi_h:=dvi_h+rule_wd;
  10932. end
  10933. @ @d billion==float_constant(1000000000)
  10934. @d vet_glue(#)== glue_temp:=#;
  10935. if glue_temp>billion then
  10936. glue_temp:=billion
  10937. else if glue_temp<-billion then
  10938. glue_temp:=-billion
  10939. @<Move right or output leaders@>=
  10940. begin g:=glue_ptr(p); rule_wd:=width(g)-cur_g;
  10941. if g_sign<>normal then
  10942. begin if g_sign=stretching then
  10943. begin if stretch_order(g)=g_order then
  10944. begin cur_glue:=cur_glue+stretch(g);
  10945. vet_glue(float(glue_set(this_box))*cur_glue);
  10946. @^real multiplication@>
  10947. cur_g:=round(glue_temp);
  10948. end;
  10949. end
  10950. else if shrink_order(g)=g_order then
  10951. begin cur_glue:=cur_glue-shrink(g);
  10952. vet_glue(float(glue_set(this_box))*cur_glue);
  10953. cur_g:=round(glue_temp);
  10954. end;
  10955. end;
  10956. rule_wd:=rule_wd+cur_g;
  10957. if subtype(p)>=a_leaders then
  10958. @<Output leaders in an hlist, |goto fin_rule| if a rule
  10959. or to |next_p| if done@>;
  10960. goto move_past;
  10961. end
  10962. @ @<Output leaders in an hlist...@>=
  10963. begin leader_box:=leader_ptr(p);
  10964. if type(leader_box)=rule_node then
  10965. begin rule_ht:=height(leader_box); rule_dp:=depth(leader_box);
  10966. goto fin_rule;
  10967. end;
  10968. leader_wd:=width(leader_box);
  10969. if (leader_wd>0)and(rule_wd>0) then
  10970. begin rule_wd:=rule_wd+10; {compensate for floating-point rounding}
  10971. edge:=cur_h+rule_wd; lx:=0;
  10972. @<Let |cur_h| be the position of the first box, and set |leader_wd+lx|
  10973. to the spacing between corresponding parts of boxes@>;
  10974. while cur_h+leader_wd<=edge do
  10975. @<Output a leader box at |cur_h|,
  10976. then advance |cur_h| by |leader_wd+lx|@>;
  10977. cur_h:=edge-10; goto next_p;
  10978. end;
  10979. end
  10980. @ The calculations related to leaders require a bit of care. First, in the
  10981. case of |a_leaders| (aligned leaders), we want to move |cur_h| to
  10982. |left_edge| plus the smallest multiple of |leader_wd| for which the result
  10983. is not less than the current value of |cur_h|; i.e., |cur_h| should become
  10984. $|left_edge|+|leader_wd|\times\lceil
  10985. (|cur_h|-|left_edge|)/|leader_wd|\rceil$. The program here should work in
  10986. all cases even though some implementations of \PASCAL\ give nonstandard
  10987. results for the |div| operation when |cur_h| is less than |left_edge|.
  10988. In the case of |c_leaders| (centered leaders), we want to increase |cur_h|
  10989. by half of the excess space not occupied by the leaders; and in the
  10990. case of |x_leaders| (expanded leaders) we increase |cur_h|
  10991. by $1/(q+1)$ of this excess space, where $q$ is the number of times the
  10992. leader box will be replicated. Slight inaccuracies in the division might
  10993. accumulate; half of this rounding error is placed at each end of the leaders.
  10994. @<Let |cur_h| be the position of the first box, ...@>=
  10995. if subtype(p)=a_leaders then
  10996. begin save_h:=cur_h;
  10997. cur_h:=left_edge+leader_wd*((cur_h-left_edge)@!div leader_wd);
  10998. if cur_h<save_h then cur_h:=cur_h+leader_wd;
  10999. end
  11000. else begin lq:=rule_wd div leader_wd; {the number of box copies}
  11001. lr:=rule_wd mod leader_wd; {the remaining space}
  11002. if subtype(p)=c_leaders then cur_h:=cur_h+(lr div 2)
  11003. else begin lx:=lr div (lq+1);
  11004. cur_h:=cur_h+((lr-(lq-1)*lx) div 2);
  11005. end;
  11006. end
  11007. @ The `\\{synch}' operations here are intended to decrease the number of
  11008. bytes needed to specify horizontal and vertical motion in the \.{DVI} output.
  11009. @<Output a leader box at |cur_h|, ...@>=
  11010. begin cur_v:=base_line+shift_amount(leader_box); synch_v; save_v:=dvi_v;@/
  11011. synch_h; save_h:=dvi_h; temp_ptr:=leader_box;
  11012. outer_doing_leaders:=doing_leaders; doing_leaders:=true;
  11013. if type(leader_box)=vlist_node then vlist_out@+else hlist_out;
  11014. doing_leaders:=outer_doing_leaders;
  11015. dvi_v:=save_v; dvi_h:=save_h; cur_v:=base_line;
  11016. cur_h:=save_h+leader_wd+lx;
  11017. end
  11018. @ The |vlist_out| routine is similar to |hlist_out|, but a bit simpler.
  11019. @p procedure vlist_out; {output a |vlist_node| box}
  11020. label move_past, fin_rule, next_p;
  11021. var left_edge: scaled; {the left coordinate for this box}
  11022. @!top_edge: scaled; {the top coordinate for this box}
  11023. @!save_h,@!save_v: scaled; {what |dvi_h| and |dvi_v| should pop to}
  11024. @!this_box: pointer; {pointer to containing box}
  11025. @!g_order: glue_ord; {applicable order of infinity for glue}
  11026. @!g_sign: normal..shrinking; {selects type of glue}
  11027. @!p:pointer; {current position in the vlist}
  11028. @!save_loc:integer; {\.{DVI} byte location upon entry}
  11029. @!leader_box:pointer; {the leader box being replicated}
  11030. @!leader_ht:scaled; {height of leader box being replicated}
  11031. @!lx:scaled; {extra space between leader boxes}
  11032. @!outer_doing_leaders:boolean; {were we doing leaders?}
  11033. @!edge:scaled; {bottom boundary of leader space}
  11034. @!glue_temp:real; {glue value before rounding}
  11035. @!cur_glue:real; {glue seen so far}
  11036. @!cur_g:scaled; {rounded equivalent of |cur_glue| times the glue ratio}
  11037. begin cur_g:=0; cur_glue:=float_constant(0);
  11038. this_box:=temp_ptr; g_order:=glue_order(this_box);
  11039. g_sign:=glue_sign(this_box); p:=list_ptr(this_box);
  11040. incr(cur_s);
  11041. if cur_s>0 then dvi_out(push);
  11042. if cur_s>max_push then max_push:=cur_s;
  11043. save_loc:=dvi_offset+dvi_ptr; left_edge:=cur_h; cur_v:=cur_v-height(this_box);
  11044. top_edge:=cur_v;
  11045. while p<>null do @<Output node |p| for |vlist_out| and move to the next node,
  11046. maintaining the condition |cur_h=left_edge|@>;
  11047. prune_movements(save_loc);
  11048. if cur_s>0 then dvi_pop(save_loc);
  11049. decr(cur_s);
  11050. end;
  11051. @ @<Output node |p| for |vlist_out|...@>=
  11052. begin if is_char_node(p) then confusion("vlistout")
  11053. @:this can't happen vlistout}{\quad vlistout@>
  11054. else @<Output the non-|char_node| |p| for |vlist_out|@>;
  11055. next_p:p:=link(p);
  11056. end
  11057. @ @<Output the non-|char_node| |p| for |vlist_out|@>=
  11058. begin case type(p) of
  11059. hlist_node,vlist_node:@<Output a box in a vlist@>;
  11060. rule_node: begin rule_ht:=height(p); rule_dp:=depth(p); rule_wd:=width(p);
  11061. goto fin_rule;
  11062. end;
  11063. whatsit_node: @<Output the whatsit node |p| in a vlist@>;
  11064. glue_node: @<Move down or output leaders@>;
  11065. kern_node:cur_v:=cur_v+width(p);
  11066. othercases do_nothing
  11067. endcases;@/
  11068. goto next_p;
  11069. fin_rule: @<Output a rule in a vlist, |goto next_p|@>;
  11070. move_past: cur_v:=cur_v+rule_ht;
  11071. end
  11072. @ The |synch_v| here allows the \.{DVI} output to use one-byte commands
  11073. for adjusting |v| in most cases, since the baselineskip distance will
  11074. usually be constant.
  11075. @<Output a box in a vlist@>=
  11076. if list_ptr(p)=null then cur_v:=cur_v+height(p)+depth(p)
  11077. else begin cur_v:=cur_v+height(p); synch_v;
  11078. save_h:=dvi_h; save_v:=dvi_v;
  11079. cur_h:=left_edge+shift_amount(p); {shift the box right}
  11080. temp_ptr:=p;
  11081. if type(p)=vlist_node then vlist_out@+else hlist_out;
  11082. dvi_h:=save_h; dvi_v:=save_v;
  11083. cur_v:=save_v+depth(p); cur_h:=left_edge;
  11084. end
  11085. @ @<Output a rule in a vlist...@>=
  11086. if is_running(rule_wd) then rule_wd:=width(this_box);
  11087. rule_ht:=rule_ht+rule_dp; {this is the rule thickness}
  11088. cur_v:=cur_v+rule_ht;
  11089. if (rule_ht>0)and(rule_wd>0) then {we don't output empty rules}
  11090. begin synch_h; synch_v;
  11091. dvi_out(put_rule); dvi_four(rule_ht); dvi_four(rule_wd);
  11092. end;
  11093. goto next_p
  11094. @ @<Move down or output leaders@>=
  11095. begin g:=glue_ptr(p); rule_ht:=width(g)-cur_g;
  11096. if g_sign<>normal then
  11097. begin if g_sign=stretching then
  11098. begin if stretch_order(g)=g_order then
  11099. begin cur_glue:=cur_glue+stretch(g);
  11100. vet_glue(float(glue_set(this_box))*cur_glue);
  11101. @^real multiplication@>
  11102. cur_g:=round(glue_temp);
  11103. end;
  11104. end
  11105. else if shrink_order(g)=g_order then
  11106. begin cur_glue:=cur_glue-shrink(g);
  11107. vet_glue(float(glue_set(this_box))*cur_glue);
  11108. cur_g:=round(glue_temp);
  11109. end;
  11110. end;
  11111. rule_ht:=rule_ht+cur_g;
  11112. if subtype(p)>=a_leaders then
  11113. @<Output leaders in a vlist, |goto fin_rule| if a rule
  11114. or to |next_p| if done@>;
  11115. goto move_past;
  11116. end
  11117. @ @<Output leaders in a vlist...@>=
  11118. begin leader_box:=leader_ptr(p);
  11119. if type(leader_box)=rule_node then
  11120. begin rule_wd:=width(leader_box); rule_dp:=0;
  11121. goto fin_rule;
  11122. end;
  11123. leader_ht:=height(leader_box)+depth(leader_box);
  11124. if (leader_ht>0)and(rule_ht>0) then
  11125. begin rule_ht:=rule_ht+10; {compensate for floating-point rounding}
  11126. edge:=cur_v+rule_ht; lx:=0;
  11127. @<Let |cur_v| be the position of the first box, and set |leader_ht+lx|
  11128. to the spacing between corresponding parts of boxes@>;
  11129. while cur_v+leader_ht<=edge do
  11130. @<Output a leader box at |cur_v|,
  11131. then advance |cur_v| by |leader_ht+lx|@>;
  11132. cur_v:=edge-10; goto next_p;
  11133. end;
  11134. end
  11135. @ @<Let |cur_v| be the position of the first box, ...@>=
  11136. if subtype(p)=a_leaders then
  11137. begin save_v:=cur_v;
  11138. cur_v:=top_edge+leader_ht*((cur_v-top_edge)@!div leader_ht);
  11139. if cur_v<save_v then cur_v:=cur_v+leader_ht;
  11140. end
  11141. else begin lq:=rule_ht div leader_ht; {the number of box copies}
  11142. lr:=rule_ht mod leader_ht; {the remaining space}
  11143. if subtype(p)=c_leaders then cur_v:=cur_v+(lr div 2)
  11144. else begin lx:=lr div (lq+1);
  11145. cur_v:=cur_v+((lr-(lq-1)*lx) div 2);
  11146. end;
  11147. end
  11148. @ When we reach this part of the program, |cur_v| indicates the top of a
  11149. leader box, not its baseline.
  11150. @<Output a leader box at |cur_v|, ...@>=
  11151. begin cur_h:=left_edge+shift_amount(leader_box); synch_h; save_h:=dvi_h;@/
  11152. cur_v:=cur_v+height(leader_box); synch_v; save_v:=dvi_v;
  11153. temp_ptr:=leader_box;
  11154. outer_doing_leaders:=doing_leaders; doing_leaders:=true;
  11155. if type(leader_box)=vlist_node then vlist_out@+else hlist_out;
  11156. doing_leaders:=outer_doing_leaders;
  11157. dvi_v:=save_v; dvi_h:=save_h; cur_h:=left_edge;
  11158. cur_v:=save_v-height(leader_box)+leader_ht+lx;
  11159. end
  11160. @ The |hlist_out| and |vlist_out| procedures are now complete, so we are
  11161. ready for the |ship_out| routine that gets them started in the first place.
  11162. @p procedure ship_out(@!p:pointer); {output the box |p|}
  11163. label done;
  11164. var page_loc:integer; {location of the current |bop|}
  11165. @!j,@!k:0..9; {indices to first ten count registers}
  11166. @!s:pool_pointer; {index into |str_pool|}
  11167. @!old_setting:0..max_selector; {saved |selector| setting}
  11168. begin if tracing_output>0 then
  11169. begin print_nl(""); print_ln;
  11170. print("Completed box being shipped out");
  11171. @.Completed box...@>
  11172. end;
  11173. if term_offset>max_print_line-9 then print_ln
  11174. else if (term_offset>0)or(file_offset>0) then print_char(" ");
  11175. print_char("["); j:=9;
  11176. while (count(j)=0)and(j>0) do decr(j);
  11177. for k:=0 to j do
  11178. begin print_int(count(k));
  11179. if k<j then print_char(".");
  11180. end;
  11181. update_terminal;
  11182. if tracing_output>0 then
  11183. begin print_char("]");
  11184. begin_diagnostic; show_box(p); end_diagnostic(true);
  11185. end;
  11186. @<Ship box |p| out@>;
  11187. if tracing_output<=0 then print_char("]");
  11188. dead_cycles:=0;
  11189. update_terminal; {progress report}
  11190. @<Flush the box from memory, showing statistics if requested@>;
  11191. end;
  11192. @ @<Flush the box from memory, showing statistics if requested@>=
  11193. @!stat if tracing_stats>1 then
  11194. begin print_nl("Memory usage before: ");
  11195. @.Memory usage...@>
  11196. print_int(var_used); print_char("&");
  11197. print_int(dyn_used); print_char(";");
  11198. end;
  11199. tats@/
  11200. flush_node_list(p);
  11201. @!stat if tracing_stats>1 then
  11202. begin print(" after: ");
  11203. print_int(var_used); print_char("&");
  11204. print_int(dyn_used); print("; still untouched: ");
  11205. print_int(hi_mem_min-lo_mem_max-1); print_ln;
  11206. end;
  11207. tats
  11208. @ @<Ship box |p| out@>=
  11209. @<Update the values of |max_h| and |max_v|; but if the page is too large,
  11210. |goto done|@>;
  11211. @<Initialize variables as |ship_out| begins@>;
  11212. page_loc:=dvi_offset+dvi_ptr;
  11213. dvi_out(bop);
  11214. for k:=0 to 9 do dvi_four(count(k));
  11215. dvi_four(last_bop); last_bop:=page_loc;
  11216. cur_v:=height(p)+v_offset; temp_ptr:=p;
  11217. if type(p)=vlist_node then vlist_out@+else hlist_out;
  11218. dvi_out(eop); incr(total_pages); cur_s:=-1;
  11219. done:
  11220. @ Sometimes the user will generate a huge page because other error messages
  11221. are being ignored. Such pages are not output to the \.{dvi} file, since they
  11222. may confuse the printing software.
  11223. @<Update the values of |max_h| and |max_v|; but if the page is too large...@>=
  11224. if (height(p)>max_dimen)or@|(depth(p)>max_dimen)or@|
  11225. (height(p)+depth(p)+v_offset>max_dimen)or@|
  11226. (width(p)+h_offset>max_dimen) then
  11227. begin print_err("Huge page cannot be shipped out");
  11228. @.Huge page...@>
  11229. help2("The page just created is more than 18 feet tall or")@/
  11230. ("more than 18 feet wide, so I suspect something went wrong.");
  11231. error;
  11232. if tracing_output<=0 then
  11233. begin begin_diagnostic;
  11234. print_nl("The following box has been deleted:");
  11235. @.The following...deleted@>
  11236. show_box(p);
  11237. end_diagnostic(true);
  11238. end;
  11239. goto done;
  11240. end;
  11241. if height(p)+depth(p)+v_offset>max_v then max_v:=height(p)+depth(p)+v_offset;
  11242. if width(p)+h_offset>max_h then max_h:=width(p)+h_offset
  11243. @ At the end of the program, we must finish things off by writing the
  11244. post\-amble. If |total_pages=0|, the \.{DVI} file was never opened.
  11245. If |total_pages>=65536|, the \.{DVI} file will lie. And if
  11246. |max_push>=65536|, the user deserves whatever chaos might ensue.
  11247. An integer variable |k| will be declared for use by this routine.
  11248. @<Finish the \.{DVI} file@>=
  11249. while cur_s>-1 do
  11250. begin if cur_s>0 then dvi_out(pop)
  11251. else begin dvi_out(eop); incr(total_pages);
  11252. end;
  11253. decr(cur_s);
  11254. end;
  11255. if total_pages=0 then print_nl("No pages of output.")
  11256. @.No pages of output@>
  11257. else begin dvi_out(post); {beginning of the postamble}
  11258. dvi_four(last_bop); last_bop:=dvi_offset+dvi_ptr-5; {|post| location}
  11259. dvi_four(25400000); dvi_four(473628672); {conversion ratio for sp}
  11260. prepare_mag; dvi_four(mag); {magnification factor}
  11261. dvi_four(max_v); dvi_four(max_h);@/
  11262. dvi_out(max_push div 256); dvi_out(max_push mod 256);@/
  11263. dvi_out((total_pages div 256) mod 256); dvi_out(total_pages mod 256);@/
  11264. @<Output the font definitions for all fonts that were used@>;
  11265. dvi_out(post_post); dvi_four(last_bop); dvi_out(id_byte);@/
  11266. k:=4+((dvi_buf_size-dvi_ptr) mod 4); {the number of 223's}
  11267. while k>0 do
  11268. begin dvi_out(223); decr(k);
  11269. end;
  11270. @<Empty the last bytes out of |dvi_buf|@>;
  11271. print_nl("Output written on "); slow_print(output_file_name);
  11272. @.Output written on x@>
  11273. print(" ("); print_int(total_pages); print(" page");
  11274. if total_pages<>1 then print_char("s");
  11275. print(", "); print_int(dvi_offset+dvi_ptr); print(" bytes).");
  11276. b_close(dvi_file);
  11277. end
  11278. @ @<Output the font definitions...@>=
  11279. while font_ptr>font_base do
  11280. begin if font_used[font_ptr] then dvi_font_def(font_ptr);
  11281. decr(font_ptr);
  11282. end
  11283. @* \[33] Packaging.
  11284. We're essentially done with the parts of \TeX\ that are concerned with
  11285. the input (|get_next|) and the output (|ship_out|). So it's time to
  11286. get heavily into the remaining part, which does the real work of typesetting.
  11287. After lists are constructed, \TeX\ wraps them up and puts them into boxes.
  11288. Two major subroutines are given the responsibility for this task: |hpack|
  11289. applies to horizontal lists (hlists) and |vpack| applies to vertical lists
  11290. (vlists). The main duty of |hpack| and |vpack| is to compute the dimensions
  11291. of the resulting boxes, and to adjust the glue if one of those dimensions
  11292. is pre-specified. The computed sizes normally enclose all of the material
  11293. inside the new box; but some items may stick out if negative glue is used,
  11294. if the box is overfull, or if a \.{\\vbox} includes other boxes that have
  11295. been shifted left.
  11296. The subroutine call |hpack(p,w,m)| returns a pointer to an |hlist_node|
  11297. for a box containing the hlist that starts at |p|. Parameter |w| specifies
  11298. a width; and parameter |m| is either `|exactly|' or `|additional|'. Thus,
  11299. |hpack(p,w,exactly)| produces a box whose width is exactly |w|, while
  11300. |hpack(p,w,additional)| yields a box whose width is the natural width plus
  11301. |w|. It is convenient to define a macro called `|natural|' to cover the
  11302. most common case, so that we can say |hpack(p,natural)| to get a box that
  11303. has the natural width of list |p|.
  11304. Similarly, |vpack(p,w,m)| returns a pointer to a |vlist_node| for a
  11305. box containing the vlist that starts at |p|. In this case |w| represents
  11306. a height instead of a width; the parameter |m| is interpreted as in |hpack|.
  11307. @d exactly=0 {a box dimension is pre-specified}
  11308. @d additional=1 {a box dimension is increased from the natural one}
  11309. @d natural==0,additional {shorthand for parameters to |hpack| and |vpack|}
  11310. @ The parameters to |hpack| and |vpack| correspond to \TeX's primitives
  11311. like `\.{\\hbox} \.{to} \.{300pt}', `\.{\\hbox} \.{spread} \.{10pt}'; note
  11312. that `\.{\\hbox}' with no dimension following it is equivalent to
  11313. `\.{\\hbox} \.{spread} \.{0pt}'. The |scan_spec| subroutine scans such
  11314. constructions in the user's input, including the mandatory left brace that
  11315. follows them, and it puts the specification onto |save_stack| so that the
  11316. desired box can later be obtained by executing the following code:
  11317. $$\vbox{\halign{#\hfil\cr
  11318. |save_ptr:=save_ptr-2;|\cr
  11319. |hpack(p,saved(1),saved(0)).|\cr}}$$
  11320. Special care is necessary to ensure that the special |save_stack| codes
  11321. are placed just below the new group code, because scanning can change
  11322. |save_stack| when \.{\\csname} appears.
  11323. @p procedure scan_spec(@!c:group_code;@!three_codes:boolean);
  11324. {scans a box specification and left brace}
  11325. label found;
  11326. var @!s:integer; {temporarily saved value}
  11327. @!spec_code:exactly..additional;
  11328. begin if three_codes then s:=saved(0);
  11329. if scan_keyword("to") then spec_code:=exactly
  11330. @.to@>
  11331. else if scan_keyword("spread") then spec_code:=additional
  11332. @.spread@>
  11333. else begin spec_code:=additional; cur_val:=0;
  11334. goto found;
  11335. end;
  11336. scan_normal_dimen;
  11337. found: if three_codes then
  11338. begin saved(0):=s; incr(save_ptr);
  11339. end;
  11340. saved(0):=spec_code; saved(1):=cur_val; save_ptr:=save_ptr+2;
  11341. new_save_level(c); scan_left_brace;
  11342. end;
  11343. @ To figure out the glue setting, |hpack| and |vpack| determine how much
  11344. stretchability and shrinkability are present, considering all four orders
  11345. of infinity. The highest order of infinity that has a nonzero coefficient
  11346. is then used as if no other orders were present.
  11347. For example, suppose that the given list contains six glue nodes with
  11348. the respective stretchabilities 3pt, 8fill, 5fil, 6pt, $-3$fil, $-8$fill.
  11349. Then the total is essentially 2fil; and if a total additional space of 6pt
  11350. is to be achieved by stretching, the actual amounts of stretch will be
  11351. 0pt, 0pt, 15pt, 0pt, $-9$pt, and 0pt, since only `fil' glue will be
  11352. considered. (The `fill' glue is therefore not really stretching infinitely
  11353. with respect to `fil'; nobody would actually want that to happen.)
  11354. The arrays |total_stretch| and |total_shrink| are used to determine how much
  11355. glue of each kind is present. A global variable |last_badness| is used
  11356. to implement \.{\\badness}.
  11357. @<Glob...@>=
  11358. @!total_stretch, @!total_shrink: array[glue_ord] of scaled;
  11359. {glue found by |hpack| or |vpack|}
  11360. @!last_badness:integer; {badness of the most recently packaged box}
  11361. @ If the global variable |adjust_tail| is non-null, the |hpack| routine
  11362. also removes all occurrences of |ins_node|, |mark_node|, and |adjust_node|
  11363. items and appends the resulting material onto the list that ends at
  11364. location |adjust_tail|.
  11365. @<Glob...@>=
  11366. @!adjust_tail:pointer; {tail of adjustment list}
  11367. @ @<Set init...@>=adjust_tail:=null; last_badness:=0;
  11368. @ Here now is |hpack|, which contains few if any surprises.
  11369. @p function hpack(@!p:pointer;@!w:scaled;@!m:small_number):pointer;
  11370. label reswitch, common_ending, exit;
  11371. var r:pointer; {the box node that will be returned}
  11372. @!q:pointer; {trails behind |p|}
  11373. @!h,@!d,@!x:scaled; {height, depth, and natural width}
  11374. @!s:scaled; {shift amount}
  11375. @!g:pointer; {points to a glue specification}
  11376. @!o:glue_ord; {order of infinity}
  11377. @!f:internal_font_number; {the font in a |char_node|}
  11378. @!i:four_quarters; {font information about a |char_node|}
  11379. @!hd:eight_bits; {height and depth indices for a character}
  11380. begin last_badness:=0; r:=get_node(box_node_size); type(r):=hlist_node;
  11381. subtype(r):=min_quarterword; shift_amount(r):=0;
  11382. q:=r+list_offset; link(q):=p;@/
  11383. h:=0; @<Clear dimensions to zero@>;
  11384. while p<>null do @<Examine node |p| in the hlist, taking account of its effect
  11385. on the dimensions of the new box, or moving it to the adjustment list;
  11386. then advance |p| to the next node@>;
  11387. if adjust_tail<>null then link(adjust_tail):=null;
  11388. height(r):=h; depth(r):=d;@/
  11389. @<Determine the value of |width(r)| and the appropriate glue setting;
  11390. then |return| or |goto common_ending|@>;
  11391. common_ending: @<Finish issuing a diagnostic message
  11392. for an overfull or underfull hbox@>;
  11393. exit: hpack:=r;
  11394. end;
  11395. @ @<Clear dimensions to zero@>=
  11396. d:=0; x:=0;
  11397. total_stretch[normal]:=0; total_shrink[normal]:=0;
  11398. total_stretch[fil]:=0; total_shrink[fil]:=0;
  11399. total_stretch[fill]:=0; total_shrink[fill]:=0;
  11400. total_stretch[filll]:=0; total_shrink[filll]:=0
  11401. @ @<Examine node |p| in the hlist, taking account of its effect...@>=
  11402. @^inner loop@>
  11403. begin reswitch: while is_char_node(p) do
  11404. @<Incorporate character dimensions into the dimensions of
  11405. the hbox that will contain~it, then move to the next node@>;
  11406. if p<>null then
  11407. begin case type(p) of
  11408. hlist_node,vlist_node,rule_node,unset_node:
  11409. @<Incorporate box dimensions into the dimensions of
  11410. the hbox that will contain~it@>;
  11411. ins_node,mark_node,adjust_node: if adjust_tail<>null then
  11412. @<Transfer node |p| to the adjustment list@>;
  11413. whatsit_node:@<Incorporate a whatsit node into an hbox@>;
  11414. glue_node:@<Incorporate glue into the horizontal totals@>;
  11415. kern_node,math_node: x:=x+width(p);
  11416. ligature_node: @<Make node |p| look like a |char_node|
  11417. and |goto reswitch|@>;
  11418. othercases do_nothing
  11419. endcases;@/
  11420. p:=link(p);
  11421. end;
  11422. end
  11423. @ @<Make node |p| look like a |char_node| and |goto reswitch|@>=
  11424. begin mem[lig_trick]:=mem[lig_char(p)]; link(lig_trick):=link(p);
  11425. p:=lig_trick; goto reswitch;
  11426. end
  11427. @ The code here implicitly uses the fact that running dimensions are
  11428. indicated by |null_flag|, which will be ignored in the calculations
  11429. because it is a highly negative number.
  11430. @<Incorporate box dimensions into the dimensions of the hbox...@>=
  11431. begin x:=x+width(p);
  11432. if type(p)>=rule_node then s:=0 @+else s:=shift_amount(p);
  11433. if height(p)-s>h then h:=height(p)-s;
  11434. if depth(p)+s>d then d:=depth(p)+s;
  11435. end
  11436. @ The following code is part of \TeX's inner loop; i.e., adding another
  11437. character of text to the user's input will cause each of these instructions
  11438. to be exercised one more time.
  11439. @^inner loop@>
  11440. @<Incorporate character dimensions into the dimensions of the hbox...@>=
  11441. begin f:=font(p); i:=char_info(f)(character(p)); hd:=height_depth(i);
  11442. x:=x+char_width(f)(i);@/
  11443. s:=char_height(f)(hd);@+if s>h then h:=s;
  11444. s:=char_depth(f)(hd);@+if s>d then d:=s;
  11445. p:=link(p);
  11446. end
  11447. @ Although node |q| is not necessarily the immediate predecessor of node |p|,
  11448. it always points to some node in the list preceding |p|. Thus, we can delete
  11449. nodes by moving |q| when necessary. The algorithm takes linear time, and the
  11450. extra computation does not intrude on the inner loop unless it is necessary
  11451. to make a deletion.
  11452. @^inner loop@>
  11453. @<Transfer node |p| to the adjustment list@>=
  11454. begin while link(q)<>p do q:=link(q);
  11455. if type(p)=adjust_node then
  11456. begin link(adjust_tail):=adjust_ptr(p);
  11457. while link(adjust_tail)<>null do adjust_tail:=link(adjust_tail);
  11458. p:=link(p); free_node(link(q),small_node_size);
  11459. end
  11460. else begin link(adjust_tail):=p; adjust_tail:=p; p:=link(p);
  11461. end;
  11462. link(q):=p; p:=q;
  11463. end
  11464. @ @<Incorporate glue into the horizontal totals@>=
  11465. begin g:=glue_ptr(p); x:=x+width(g);@/
  11466. o:=stretch_order(g); total_stretch[o]:=total_stretch[o]+stretch(g);
  11467. o:=shrink_order(g); total_shrink[o]:=total_shrink[o]+shrink(g);
  11468. if subtype(p)>=a_leaders then
  11469. begin g:=leader_ptr(p);
  11470. if height(g)>h then h:=height(g);
  11471. if depth(g)>d then d:=depth(g);
  11472. end;
  11473. end
  11474. @ When we get to the present part of the program, |x| is the natural width
  11475. of the box being packaged.
  11476. @<Determine the value of |width(r)| and the appropriate glue setting...@>=
  11477. if m=additional then w:=x+w;
  11478. width(r):=w; x:=w-x; {now |x| is the excess to be made up}
  11479. if x=0 then
  11480. begin glue_sign(r):=normal; glue_order(r):=normal;
  11481. set_glue_ratio_zero(glue_set(r));
  11482. return;
  11483. end
  11484. else if x>0 then @<Determine horizontal glue stretch setting, then |return|
  11485. or \hbox{|goto common_ending|}@>
  11486. else @<Determine horizontal glue shrink setting, then |return|
  11487. or \hbox{|goto common_ending|}@>
  11488. @ @<Determine horizontal glue stretch setting...@>=
  11489. begin @<Determine the stretch order@>;
  11490. glue_order(r):=o; glue_sign(r):=stretching;
  11491. if total_stretch[o]<>0 then glue_set(r):=unfloat(x/total_stretch[o])
  11492. @^real division@>
  11493. else begin glue_sign(r):=normal;
  11494. set_glue_ratio_zero(glue_set(r)); {there's nothing to stretch}
  11495. end;
  11496. if o=normal then if list_ptr(r)<>null then
  11497. @<Report an underfull hbox and |goto common_ending|, if this box
  11498. is sufficiently bad@>;
  11499. return;
  11500. end
  11501. @ @<Determine the stretch order@>=
  11502. if total_stretch[filll]<>0 then o:=filll
  11503. else if total_stretch[fill]<>0 then o:=fill
  11504. else if total_stretch[fil]<>0 then o:=fil
  11505. else o:=normal
  11506. @ @<Report an underfull hbox and |goto common_ending|, if...@>=
  11507. begin last_badness:=badness(x,total_stretch[normal]);
  11508. if last_badness>hbadness then
  11509. begin print_ln;
  11510. if last_badness>100 then print_nl("Underfull")@+else print_nl("Loose");
  11511. print(" \hbox (badness "); print_int(last_badness);
  11512. @.Underfull \\hbox...@>
  11513. @.Loose \\hbox...@>
  11514. goto common_ending;
  11515. end;
  11516. end
  11517. @ In order to provide a decent indication of where an overfull or underfull
  11518. box originated, we use a global variable |pack_begin_line| that is
  11519. set nonzero only when |hpack| is being called by the paragraph builder
  11520. or the alignment finishing routine.
  11521. @<Glob...@>=
  11522. @!pack_begin_line:integer; {source file line where the current paragraph
  11523. or alignment began; a negative value denotes alignment}
  11524. @ @<Set init...@>=
  11525. pack_begin_line:=0;
  11526. @ @<Finish issuing a diagnostic message for an overfull or underfull hbox@>=
  11527. if output_active then print(") has occurred while \output is active")
  11528. else begin if pack_begin_line<>0 then
  11529. begin if pack_begin_line>0 then print(") in paragraph at lines ")
  11530. else print(") in alignment at lines ");
  11531. print_int(abs(pack_begin_line));
  11532. print("--");
  11533. end
  11534. else print(") detected at line ");
  11535. print_int(line);
  11536. end;
  11537. print_ln;@/
  11538. font_in_short_display:=null_font; short_display(list_ptr(r)); print_ln;@/
  11539. begin_diagnostic; show_box(r); end_diagnostic(true)
  11540. @ @<Determine horizontal glue shrink setting...@>=
  11541. begin @<Determine the shrink order@>;
  11542. glue_order(r):=o; glue_sign(r):=shrinking;
  11543. if total_shrink[o]<>0 then glue_set(r):=unfloat((-x)/total_shrink[o])
  11544. @^real division@>
  11545. else begin glue_sign(r):=normal;
  11546. set_glue_ratio_zero(glue_set(r)); {there's nothing to shrink}
  11547. end;
  11548. if (total_shrink[o]<-x)and(o=normal)and(list_ptr(r)<>null) then
  11549. begin last_badness:=1000000;
  11550. set_glue_ratio_one(glue_set(r)); {use the maximum shrinkage}
  11551. @<Report an overfull hbox and |goto common_ending|, if this box
  11552. is sufficiently bad@>;
  11553. end
  11554. else if o=normal then if list_ptr(r)<>null then
  11555. @<Report a tight hbox and |goto common_ending|, if this box
  11556. is sufficiently bad@>;
  11557. return;
  11558. end
  11559. @ @<Determine the shrink order@>=
  11560. if total_shrink[filll]<>0 then o:=filll
  11561. else if total_shrink[fill]<>0 then o:=fill
  11562. else if total_shrink[fil]<>0 then o:=fil
  11563. else o:=normal
  11564. @ @<Report an overfull hbox and |goto common_ending|, if...@>=
  11565. if (-x-total_shrink[normal]>hfuzz)or(hbadness<100) then
  11566. begin if (overfull_rule>0)and(-x-total_shrink[normal]>hfuzz) then
  11567. begin while link(q)<>null do q:=link(q);
  11568. link(q):=new_rule;
  11569. width(link(q)):=overfull_rule;
  11570. end;
  11571. print_ln; print_nl("Overfull \hbox (");
  11572. @.Overfull \\hbox...@>
  11573. print_scaled(-x-total_shrink[normal]); print("pt too wide");
  11574. goto common_ending;
  11575. end
  11576. @ @<Report a tight hbox and |goto common_ending|, if...@>=
  11577. begin last_badness:=badness(-x,total_shrink[normal]);
  11578. if last_badness>hbadness then
  11579. begin print_ln; print_nl("Tight \hbox (badness "); print_int(last_badness);
  11580. @.Tight \\hbox...@>
  11581. goto common_ending;
  11582. end;
  11583. end
  11584. @ The |vpack| subroutine is actually a special case of a slightly more
  11585. general routine called |vpackage|, which has four parameters. The fourth
  11586. parameter, which is |max_dimen| in the case of |vpack|, specifies the
  11587. maximum depth of the page box that is constructed. The depth is first
  11588. computed by the normal rules; if it exceeds this limit, the reference
  11589. point is simply moved down until the limiting depth is attained.
  11590. @d vpack(#)==vpackage(#,max_dimen) {special case of unconstrained depth}
  11591. @p function vpackage(@!p:pointer;@!h:scaled;@!m:small_number;@!l:scaled):
  11592. pointer;
  11593. label common_ending, exit;
  11594. var r:pointer; {the box node that will be returned}
  11595. @!w,@!d,@!x:scaled; {width, depth, and natural height}
  11596. @!s:scaled; {shift amount}
  11597. @!g:pointer; {points to a glue specification}
  11598. @!o:glue_ord; {order of infinity}
  11599. begin last_badness:=0; r:=get_node(box_node_size); type(r):=vlist_node;
  11600. subtype(r):=min_quarterword; shift_amount(r):=0;
  11601. list_ptr(r):=p;@/
  11602. w:=0; @<Clear dimensions to zero@>;
  11603. while p<>null do @<Examine node |p| in the vlist, taking account of its effect
  11604. on the dimensions of the new box; then advance |p| to the next node@>;
  11605. width(r):=w;
  11606. if d>l then
  11607. begin x:=x+d-l; depth(r):=l;
  11608. end
  11609. else depth(r):=d;
  11610. @<Determine the value of |height(r)| and the appropriate glue setting;
  11611. then |return| or |goto common_ending|@>;
  11612. common_ending: @<Finish issuing a diagnostic message
  11613. for an overfull or underfull vbox@>;
  11614. exit: vpackage:=r;
  11615. end;
  11616. @ @<Examine node |p| in the vlist, taking account of its effect...@>=
  11617. begin if is_char_node(p) then confusion("vpack")
  11618. @:this can't happen vpack}{\quad vpack@>
  11619. else case type(p) of
  11620. hlist_node,vlist_node,rule_node,unset_node:
  11621. @<Incorporate box dimensions into the dimensions of
  11622. the vbox that will contain~it@>;
  11623. whatsit_node:@<Incorporate a whatsit node into a vbox@>;
  11624. glue_node: @<Incorporate glue into the vertical totals@>;
  11625. kern_node: begin x:=x+d+width(p); d:=0;
  11626. end;
  11627. othercases do_nothing
  11628. endcases;
  11629. p:=link(p);
  11630. end
  11631. @ @<Incorporate box dimensions into the dimensions of the vbox...@>=
  11632. begin x:=x+d+height(p); d:=depth(p);
  11633. if type(p)>=rule_node then s:=0 @+else s:=shift_amount(p);
  11634. if width(p)+s>w then w:=width(p)+s;
  11635. end
  11636. @ @<Incorporate glue into the vertical totals@>=
  11637. begin x:=x+d; d:=0;@/
  11638. g:=glue_ptr(p); x:=x+width(g);@/
  11639. o:=stretch_order(g); total_stretch[o]:=total_stretch[o]+stretch(g);
  11640. o:=shrink_order(g); total_shrink[o]:=total_shrink[o]+shrink(g);
  11641. if subtype(p)>=a_leaders then
  11642. begin g:=leader_ptr(p);
  11643. if width(g)>w then w:=width(g);
  11644. end;
  11645. end
  11646. @ When we get to the present part of the program, |x| is the natural height
  11647. of the box being packaged.
  11648. @<Determine the value of |height(r)| and the appropriate glue setting...@>=
  11649. if m=additional then h:=x+h;
  11650. height(r):=h; x:=h-x; {now |x| is the excess to be made up}
  11651. if x=0 then
  11652. begin glue_sign(r):=normal; glue_order(r):=normal;
  11653. set_glue_ratio_zero(glue_set(r));
  11654. return;
  11655. end
  11656. else if x>0 then @<Determine vertical glue stretch setting, then |return|
  11657. or \hbox{|goto common_ending|}@>
  11658. else @<Determine vertical glue shrink setting, then |return|
  11659. or \hbox{|goto common_ending|}@>
  11660. @ @<Determine vertical glue stretch setting...@>=
  11661. begin @<Determine the stretch order@>;
  11662. glue_order(r):=o; glue_sign(r):=stretching;
  11663. if total_stretch[o]<>0 then glue_set(r):=unfloat(x/total_stretch[o])
  11664. @^real division@>
  11665. else begin glue_sign(r):=normal;
  11666. set_glue_ratio_zero(glue_set(r)); {there's nothing to stretch}
  11667. end;
  11668. if o=normal then if list_ptr(r)<>null then
  11669. @<Report an underfull vbox and |goto common_ending|, if this box
  11670. is sufficiently bad@>;
  11671. return;
  11672. end
  11673. @ @<Report an underfull vbox and |goto common_ending|, if...@>=
  11674. begin last_badness:=badness(x,total_stretch[normal]);
  11675. if last_badness>vbadness then
  11676. begin print_ln;
  11677. if last_badness>100 then print_nl("Underfull")@+else print_nl("Loose");
  11678. print(" \vbox (badness "); print_int(last_badness);
  11679. @.Underfull \\vbox...@>
  11680. @.Loose \\vbox...@>
  11681. goto common_ending;
  11682. end;
  11683. end
  11684. @ @<Finish issuing a diagnostic message for an overfull or underfull vbox@>=
  11685. if output_active then print(") has occurred while \output is active")
  11686. else begin if pack_begin_line<>0 then {it's actually negative}
  11687. begin print(") in alignment at lines ");
  11688. print_int(abs(pack_begin_line));
  11689. print("--");
  11690. end
  11691. else print(") detected at line ");
  11692. print_int(line);
  11693. print_ln;@/
  11694. end;
  11695. begin_diagnostic; show_box(r); end_diagnostic(true)
  11696. @ @<Determine vertical glue shrink setting...@>=
  11697. begin @<Determine the shrink order@>;
  11698. glue_order(r):=o; glue_sign(r):=shrinking;
  11699. if total_shrink[o]<>0 then glue_set(r):=unfloat((-x)/total_shrink[o])
  11700. @^real division@>
  11701. else begin glue_sign(r):=normal;
  11702. set_glue_ratio_zero(glue_set(r)); {there's nothing to shrink}
  11703. end;
  11704. if (total_shrink[o]<-x)and(o=normal)and(list_ptr(r)<>null) then
  11705. begin last_badness:=1000000;
  11706. set_glue_ratio_one(glue_set(r)); {use the maximum shrinkage}
  11707. @<Report an overfull vbox and |goto common_ending|, if this box
  11708. is sufficiently bad@>;
  11709. end
  11710. else if o=normal then if list_ptr(r)<>null then
  11711. @<Report a tight vbox and |goto common_ending|, if this box
  11712. is sufficiently bad@>;
  11713. return;
  11714. end
  11715. @ @<Report an overfull vbox and |goto common_ending|, if...@>=
  11716. if (-x-total_shrink[normal]>vfuzz)or(vbadness<100) then
  11717. begin print_ln; print_nl("Overfull \vbox (");
  11718. @.Overfull \\vbox...@>
  11719. print_scaled(-x-total_shrink[normal]); print("pt too high");
  11720. goto common_ending;
  11721. end
  11722. @ @<Report a tight vbox and |goto common_ending|, if...@>=
  11723. begin last_badness:=badness(-x,total_shrink[normal]);
  11724. if last_badness>vbadness then
  11725. begin print_ln; print_nl("Tight \vbox (badness "); print_int(last_badness);
  11726. @.Tight \\vbox...@>
  11727. goto common_ending;
  11728. end;
  11729. end
  11730. @ When a box is being appended to the current vertical list, the
  11731. baselineskip calculation is handled by the |append_to_vlist| routine.
  11732. @p procedure append_to_vlist(@!b:pointer);
  11733. var d:scaled; {deficiency of space between baselines}
  11734. @!p:pointer; {a new glue node}
  11735. begin if prev_depth>ignore_depth then
  11736. begin d:=width(baseline_skip)-prev_depth-height(b);
  11737. if d<line_skip_limit then p:=new_param_glue(line_skip_code)
  11738. else begin p:=new_skip_param(baseline_skip_code);
  11739. width(temp_ptr):=d; {|temp_ptr=glue_ptr(p)|}
  11740. end;
  11741. link(tail):=p; tail:=p;
  11742. end;
  11743. link(tail):=b; tail:=b; prev_depth:=depth(b);
  11744. end;
  11745. @* \[34] Data structures for math mode.
  11746. When \TeX\ reads a formula that is enclosed between \.\$'s, it constructs an
  11747. {\sl mlist}, which is essentially a tree structure representing that
  11748. formula. An mlist is a linear sequence of items, but we can regard it as
  11749. a tree structure because mlists can appear within mlists. For example, many
  11750. of the entries can be subscripted or superscripted, and such ``scripts''
  11751. are mlists in their own right.
  11752. An entire formula is parsed into such a tree before any of the actual
  11753. typesetting is done, because the current style of type is usually not
  11754. known until the formula has been fully scanned. For example, when the
  11755. formula `\.{\$a+b \\over c+d\$}' is being read, there is no way to tell
  11756. that `\.{a+b}' will be in script size until `\.{\\over}' has appeared.
  11757. During the scanning process, each element of the mlist being built is
  11758. classified as a relation, a binary operator, an open parenthesis, etc.,
  11759. or as a construct like `\.{\\sqrt}' that must be built up. This classification
  11760. appears in the mlist data structure.
  11761. After a formula has been fully scanned, the mlist is converted to an hlist
  11762. so that it can be incorporated into the surrounding text. This conversion is
  11763. controlled by a recursive procedure that decides all of the appropriate
  11764. styles by a ``top-down'' process starting at the outermost level and working
  11765. in towards the subformulas. The formula is ultimately pasted together using
  11766. combinations of horizontal and vertical boxes, with glue and penalty nodes
  11767. inserted as necessary.
  11768. An mlist is represented internally as a linked list consisting chiefly
  11769. of ``noads'' (pronounced ``no-adds''), to distinguish them from the somewhat
  11770. similar ``nodes'' in hlists and vlists. Certain kinds of ordinary nodes are
  11771. allowed to appear in mlists together with the noads; \TeX\ tells the difference
  11772. by means of the |type| field, since a noad's |type| is always greater than
  11773. that of a node. An mlist does not contain character nodes, hlist nodes, vlist
  11774. nodes, math nodes, ligature nodes,
  11775. or unset nodes; in particular, each mlist item appears in the
  11776. variable-size part of |mem|, so the |type| field is always present.
  11777. @ Each noad is four or more words long. The first word contains the |type|
  11778. and |subtype| and |link| fields that are already so familiar to us; the
  11779. second, third, and fourth words are called the noad's |nucleus|, |subscr|,
  11780. and |supscr| fields.
  11781. Consider, for example, the simple formula `\.{\$x\^2\$}', which would be
  11782. parsed into an mlist containing a single element called an |ord_noad|.
  11783. The |nucleus| of this noad is a representation of `\.x', the |subscr| is
  11784. empty, and the |supscr| is a representation of `\.2'.
  11785. The |nucleus|, |subscr|, and |supscr| fields are further broken into
  11786. subfields. If |p| points to a noad, and if |q| is one of its principal
  11787. fields (e.g., |q=subscr(p)|), there are several possibilities for the
  11788. subfields, depending on the |math_type| of |q|.
  11789. \yskip\hang|math_type(q)=math_char| means that |fam(q)| refers to one of
  11790. the sixteen font families, and |character(q)| is the number of a character
  11791. within a font of that family, as in a character node.
  11792. \yskip\hang|math_type(q)=math_text_char| is similar, but the character is
  11793. unsubscripted and unsuperscripted and it is followed immediately by another
  11794. character from the same font. (This |math_type| setting appears only
  11795. briefly during the processing; it is used to suppress unwanted italic
  11796. corrections.)
  11797. \yskip\hang|math_type(q)=empty| indicates a field with no value (the
  11798. corresponding attribute of noad |p| is not present).
  11799. \yskip\hang|math_type(q)=sub_box| means that |info(q)| points to a box
  11800. node (either an |hlist_node| or a |vlist_node|) that should be used as the
  11801. value of the field. The |shift_amount| in the subsidiary box node is the
  11802. amount by which that box will be shifted downward.
  11803. \yskip\hang|math_type(q)=sub_mlist| means that |info(q)| points to
  11804. an mlist; the mlist must be converted to an hlist in order to obtain
  11805. the value of this field.
  11806. \yskip\noindent In the latter case, we might have |info(q)=null|. This
  11807. is not the same as |math_type(q)=empty|; for example, `\.{\$P\_\{\}\$}'
  11808. and `\.{\$P\$}' produce different results (the former will not have the
  11809. ``italic correction'' added to the width of |P|, but the ``script skip''
  11810. will be added).
  11811. The definitions of subfields given here are evidently wasteful of space,
  11812. since a halfword is being used for the |math_type| although only three
  11813. bits would be needed. However, there are hardly ever many noads present at
  11814. once, since they are soon converted to nodes that take up even more space,
  11815. so we can afford to represent them in whatever way simplifies the
  11816. programming.
  11817. @d noad_size=4 {number of words in a normal noad}
  11818. @d nucleus(#)==#+1 {the |nucleus| field of a noad}
  11819. @d supscr(#)==#+2 {the |supscr| field of a noad}
  11820. @d subscr(#)==#+3 {the |subscr| field of a noad}
  11821. @d math_type==link {a |halfword| in |mem|}
  11822. @d fam==font {a |quarterword| in |mem|}
  11823. @d math_char=1 {|math_type| when the attribute is simple}
  11824. @d sub_box=2 {|math_type| when the attribute is a box}
  11825. @d sub_mlist=3 {|math_type| when the attribute is a formula}
  11826. @d math_text_char=4 {|math_type| when italic correction is dubious}
  11827. @ Each portion of a formula is classified as Ord, Op, Bin, Rel, Open,
  11828. Close, Punct, or Inner, for purposes of spacing and line breaking. An
  11829. |ord_noad|, |op_noad|, |bin_noad|, |rel_noad|, |open_noad|, |close_noad|,
  11830. |punct_noad|, or |inner_noad| is used to represent portions of the various
  11831. types. For example, an `\.=' sign in a formula leads to the creation of a
  11832. |rel_noad| whose |nucleus| field is a representation of an equals sign
  11833. (usually |fam=0|, |character=@'75|). A formula preceded by \.{\\mathrel}
  11834. also results in a |rel_noad|. When a |rel_noad| is followed by an
  11835. |op_noad|, say, and possibly separated by one or more ordinary nodes (not
  11836. noads), \TeX\ will insert a penalty node (with the current |rel_penalty|)
  11837. just after the formula that corresponds to the |rel_noad|, unless there
  11838. already was a penalty immediately following; and a ``thick space'' will be
  11839. inserted just before the formula that corresponds to the |op_noad|.
  11840. A noad of type |ord_noad|, |op_noad|, \dots, |inner_noad| usually
  11841. has a |subtype=normal|. The only exception is that an |op_noad| might
  11842. have |subtype=limits| or |no_limits|, if the normal positioning of
  11843. limits has been overridden for this operator.
  11844. @d ord_noad=unset_node+3 {|type| of a noad classified Ord}
  11845. @d op_noad=ord_noad+1 {|type| of a noad classified Op}
  11846. @d bin_noad=ord_noad+2 {|type| of a noad classified Bin}
  11847. @d rel_noad=ord_noad+3 {|type| of a noad classified Rel}
  11848. @d open_noad=ord_noad+4 {|type| of a noad classified Open}
  11849. @d close_noad=ord_noad+5 {|type| of a noad classified Close}
  11850. @d punct_noad=ord_noad+6 {|type| of a noad classified Punct}
  11851. @d inner_noad=ord_noad+7 {|type| of a noad classified Inner}
  11852. @d limits=1 {|subtype| of |op_noad| whose scripts are to be above, below}
  11853. @d no_limits=2 {|subtype| of |op_noad| whose scripts are to be normal}
  11854. @ A |radical_noad| is five words long; the fifth word is the |left_delimiter|
  11855. field, which usually represents a square root sign.
  11856. A |fraction_noad| is six words long; it has a |right_delimiter| field
  11857. as well as a |left_delimiter|.
  11858. Delimiter fields are of type |four_quarters|, and they have four subfields
  11859. called |small_fam|, |small_char|, |large_fam|, |large_char|. These subfields
  11860. represent variable-size delimiters by giving the ``small'' and ``large''
  11861. starting characters, as explained in Chapter~17 of {\sl The \TeX book}.
  11862. @:TeXbook}{\sl The \TeX book@>
  11863. A |fraction_noad| is actually quite different from all other noads. Not
  11864. only does it have six words, it has |thickness|, |denominator|, and
  11865. |numerator| fields instead of |nucleus|, |subscr|, and |supscr|. The
  11866. |thickness| is a scaled value that tells how thick to make a fraction
  11867. rule; however, the special value |default_code| is used to stand for the
  11868. |default_rule_thickness| of the current size. The |numerator| and
  11869. |denominator| point to mlists that define a fraction; we always have
  11870. $$\hbox{|math_type(numerator)=math_type(denominator)=sub_mlist|}.$$ The
  11871. |left_delimiter| and |right_delimiter| fields specify delimiters that will
  11872. be placed at the left and right of the fraction. In this way, a
  11873. |fraction_noad| is able to represent all of \TeX's operators \.{\\over},
  11874. \.{\\atop}, \.{\\above}, \.{\\overwithdelims}, \.{\\atopwithdelims}, and
  11875. \.{\\abovewithdelims}.
  11876. @d left_delimiter(#)==#+4 {first delimiter field of a noad}
  11877. @d right_delimiter(#)==#+5 {second delimiter field of a fraction noad}
  11878. @d radical_noad=inner_noad+1 {|type| of a noad for square roots}
  11879. @d radical_noad_size=5 {number of |mem| words in a radical noad}
  11880. @d fraction_noad=radical_noad+1 {|type| of a noad for generalized fractions}
  11881. @d fraction_noad_size=6 {number of |mem| words in a fraction noad}
  11882. @d small_fam(#)==mem[#].qqqq.b0 {|fam| for ``small'' delimiter}
  11883. @d small_char(#)==mem[#].qqqq.b1 {|character| for ``small'' delimiter}
  11884. @d large_fam(#)==mem[#].qqqq.b2 {|fam| for ``large'' delimiter}
  11885. @d large_char(#)==mem[#].qqqq.b3 {|character| for ``large'' delimiter}
  11886. @d thickness==width {|thickness| field in a fraction noad}
  11887. @d default_code==@'10000000000 {denotes |default_rule_thickness|}
  11888. @d numerator==supscr {|numerator| field in a fraction noad}
  11889. @d denominator==subscr {|denominator| field in a fraction noad}
  11890. @ The global variable |empty_field| is set up for initialization of empty
  11891. fields in new noads. Similarly, |null_delimiter| is for the initialization
  11892. of delimiter fields.
  11893. @<Glob...@>=
  11894. @!empty_field:two_halves;
  11895. @!null_delimiter:four_quarters;
  11896. @ @<Set init...@>=
  11897. empty_field.rh:=empty; empty_field.lh:=null;@/
  11898. null_delimiter.b0:=0; null_delimiter.b1:=min_quarterword;@/
  11899. null_delimiter.b2:=0; null_delimiter.b3:=min_quarterword;
  11900. @ The |new_noad| function creates an |ord_noad| that is completely null.
  11901. @p function new_noad:pointer;
  11902. var p:pointer;
  11903. begin p:=get_node(noad_size);
  11904. type(p):=ord_noad; subtype(p):=normal;
  11905. mem[nucleus(p)].hh:=empty_field;
  11906. mem[subscr(p)].hh:=empty_field;
  11907. mem[supscr(p)].hh:=empty_field;
  11908. new_noad:=p;
  11909. end;
  11910. @ A few more kinds of noads will complete the set: An |under_noad| has its
  11911. nucleus underlined; an |over_noad| has it overlined. An |accent_noad| places
  11912. an accent over its nucleus; the accent character appears as
  11913. |fam(accent_chr(p))| and |character(accent_chr(p))|. A |vcenter_noad|
  11914. centers its nucleus vertically with respect to the axis of the formula;
  11915. in such noads we always have |math_type(nucleus(p))=sub_box|.
  11916. And finally, we have |left_noad| and |right_noad| types, to implement
  11917. \TeX's \.{\\left} and \.{\\right}. The |nucleus| of such noads is
  11918. replaced by a |delimiter| field; thus, for example, `\.{\\left(}' produces
  11919. a |left_noad| such that |delimiter(p)| holds the family and character
  11920. codes for all left parentheses. A |left_noad| never appears in an mlist
  11921. except as the first element, and a |right_noad| never appears in an mlist
  11922. except as the last element; furthermore, we either have both a |left_noad|
  11923. and a |right_noad|, or neither one is present. The |subscr| and |supscr|
  11924. fields are always |empty| in a |left_noad| and a |right_noad|.
  11925. @d under_noad=fraction_noad+1 {|type| of a noad for underlining}
  11926. @d over_noad=under_noad+1 {|type| of a noad for overlining}
  11927. @d accent_noad=over_noad+1 {|type| of a noad for accented subformulas}
  11928. @d accent_noad_size=5 {number of |mem| words in an accent noad}
  11929. @d accent_chr(#)==#+4 {the |accent_chr| field of an accent noad}
  11930. @d vcenter_noad=accent_noad+1 {|type| of a noad for \.{\\vcenter}}
  11931. @d left_noad=vcenter_noad+1 {|type| of a noad for \.{\\left}}
  11932. @d right_noad=left_noad+1 {|type| of a noad for \.{\\right}}
  11933. @d delimiter==nucleus {|delimiter| field in left and right noads}
  11934. @d scripts_allowed(#)==(type(#)>=ord_noad)and(type(#)<left_noad)
  11935. @ Math formulas can also contain instructions like \.{\\textstyle} that
  11936. override \TeX's normal style rules. A |style_node| is inserted into the
  11937. data structure to record such instructions; it is three words long, so it
  11938. is considered a node instead of a noad. The |subtype| is either |display_style|
  11939. or |text_style| or |script_style| or |script_script_style|. The
  11940. second and third words of a |style_node| are not used, but they are
  11941. present because a |choice_node| is converted to a |style_node|.
  11942. \TeX\ uses even numbers 0, 2, 4, 6 to encode the basic styles
  11943. |display_style|, \dots, |script_script_style|, and adds~1 to get the
  11944. ``cramped'' versions of these styles. This gives a numerical order that
  11945. is backwards from the convention of Appendix~G in {\sl The \TeX book\/};
  11946. i.e., a smaller style has a larger numerical value.
  11947. @:TeXbook}{\sl The \TeX book@>
  11948. @d style_node=unset_node+1 {|type| of a style node}
  11949. @d style_node_size=3 {number of words in a style node}
  11950. @d display_style=0 {|subtype| for \.{\\displaystyle}}
  11951. @d text_style=2 {|subtype| for \.{\\textstyle}}
  11952. @d script_style=4 {|subtype| for \.{\\scriptstyle}}
  11953. @d script_script_style=6 {|subtype| for \.{\\scriptscriptstyle}}
  11954. @d cramped=1 {add this to an uncramped style if you want to cramp it}
  11955. @p function new_style(@!s:small_number):pointer; {create a style node}
  11956. var p:pointer; {the new node}
  11957. begin p:=get_node(style_node_size); type(p):=style_node;
  11958. subtype(p):=s; width(p):=0; depth(p):=0; {the |width| and |depth| are not used}
  11959. new_style:=p;
  11960. end;
  11961. @ Finally, the \.{\\mathchoice} primitive creates a |choice_node|, which
  11962. has special subfields |display_mlist|, |text_mlist|, |script_mlist|,
  11963. and |script_script_mlist| pointing to the mlists for each style.
  11964. @d choice_node=unset_node+2 {|type| of a choice node}
  11965. @d display_mlist(#)==info(#+1) {mlist to be used in display style}
  11966. @d text_mlist(#)==link(#+1) {mlist to be used in text style}
  11967. @d script_mlist(#)==info(#+2) {mlist to be used in script style}
  11968. @d script_script_mlist(#)==link(#+2) {mlist to be used in scriptscript style}
  11969. @p function new_choice:pointer; {create a choice node}
  11970. var p:pointer; {the new node}
  11971. begin p:=get_node(style_node_size); type(p):=choice_node;
  11972. subtype(p):=0; {the |subtype| is not used}
  11973. display_mlist(p):=null; text_mlist(p):=null; script_mlist(p):=null;
  11974. script_script_mlist(p):=null;
  11975. new_choice:=p;
  11976. end;
  11977. @ Let's consider now the previously unwritten part of |show_node_list|
  11978. that displays the things that can only be present in mlists; this
  11979. program illustrates how to access the data structures just defined.
  11980. In the context of the following program, |p| points to a node or noad that
  11981. should be displayed, and the current string contains the ``recursion history''
  11982. that leads to this point. The recursion history consists of a dot for each
  11983. outer level in which |p| is subsidiary to some node, or in which |p| is
  11984. subsidiary to the |nucleus| field of some noad; the dot is replaced by
  11985. `\.\_' or `\.\^' or `\./' or `\.\\' if |p| is descended from the |subscr|
  11986. or |supscr| or |denominator| or |numerator| fields of noads. For example,
  11987. the current string would be `\.{.\^.\_/}' if |p| points to the |ord_noad| for
  11988. |x| in the (ridiculous) formula
  11989. `\.{\$\\sqrt\{a\^\{\\mathinner\{b\_\{c\\over x+y\}\}\}\}\$}'.
  11990. @<Cases of |show_node_list| that arise...@>=
  11991. style_node:print_style(subtype(p));
  11992. choice_node:@<Display choice node |p|@>;
  11993. ord_noad,op_noad,bin_noad,rel_noad,open_noad,close_noad,punct_noad,inner_noad,
  11994. radical_noad,over_noad,under_noad,vcenter_noad,accent_noad,
  11995. left_noad,right_noad:@<Display normal noad |p|@>;
  11996. fraction_noad:@<Display fraction noad |p|@>;
  11997. @ Here are some simple routines used in the display of noads.
  11998. @<Declare procedures needed for displaying the elements of mlists@>=
  11999. procedure print_fam_and_char(@!p:pointer); {prints family and character}
  12000. begin print_esc("fam"); print_int(fam(p)); print_char(" ");
  12001. print_ASCII(qo(character(p)));
  12002. end;
  12003. @#
  12004. procedure print_delimiter(@!p:pointer); {prints a delimiter as 24-bit hex value}
  12005. var a:integer; {accumulator}
  12006. begin a:=small_fam(p)*256+qo(small_char(p));
  12007. a:=a*@"1000+large_fam(p)*256+qo(large_char(p));
  12008. if a<0 then print_int(a) {this should never happen}
  12009. else print_hex(a);
  12010. end;
  12011. @ The next subroutine will descend to another level of recursion when a
  12012. subsidiary mlist needs to be displayed. The parameter |c| indicates what
  12013. character is to become part of the recursion history. An empty mlist is
  12014. distinguished from a field with |math_type(p)=empty|, because these are
  12015. not equivalent (as explained above).
  12016. @^recursion@>
  12017. @<Declare procedures needed for displaying...@>=
  12018. procedure@?show_info; forward;@t\2@>@?{|show_node_list(info(temp_ptr))|}
  12019. procedure print_subsidiary_data(@!p:pointer;@!c:ASCII_code);
  12020. {display a noad field}
  12021. begin if cur_length>=depth_threshold then
  12022. begin if math_type(p)<>empty then print(" []");
  12023. end
  12024. else begin append_char(c); {include |c| in the recursion history}
  12025. temp_ptr:=p; {prepare for |show_info| if recursion is needed}
  12026. case math_type(p) of
  12027. math_char: begin print_ln; print_current_string; print_fam_and_char(p);
  12028. end;
  12029. sub_box: show_info; {recursive call}
  12030. sub_mlist: if info(p)=null then
  12031. begin print_ln; print_current_string; print("{}");
  12032. end
  12033. else show_info; {recursive call}
  12034. othercases do_nothing {|empty|}
  12035. endcases;@/
  12036. flush_char; {remove |c| from the recursion history}
  12037. end;
  12038. end;
  12039. @ The inelegant introduction of |show_info| in the code above seems better
  12040. than the alternative of using \PASCAL's strange |forward| declaration for a
  12041. procedure with parameters. The \PASCAL\ convention about dropping parameters
  12042. from a post-|forward| procedure is, frankly, so intolerable to the author
  12043. of \TeX\ that he would rather stoop to communication via a global temporary
  12044. variable. (A similar stoopidity occurred with respect to |hlist_out| and
  12045. |vlist_out| above, and it will occur with respect to |mlist_to_hlist| below.)
  12046. @^Knuth, Donald Ervin@>
  12047. @:PASCAL}{\PASCAL@>
  12048. @p procedure show_info; {the reader will kindly forgive this}
  12049. begin show_node_list(info(temp_ptr));
  12050. end;
  12051. @ @<Declare procedures needed for displaying...@>=
  12052. procedure print_style(@!c:integer);
  12053. begin case c div 2 of
  12054. 0: print_esc("displaystyle"); {|display_style=0|}
  12055. 1: print_esc("textstyle"); {|text_style=2|}
  12056. 2: print_esc("scriptstyle"); {|script_style=4|}
  12057. 3: print_esc("scriptscriptstyle"); {|script_script_style=6|}
  12058. othercases print("Unknown style!")
  12059. endcases;
  12060. end;
  12061. @ @<Display choice node |p|@>=
  12062. begin print_esc("mathchoice");
  12063. append_char("D"); show_node_list(display_mlist(p)); flush_char;
  12064. append_char("T"); show_node_list(text_mlist(p)); flush_char;
  12065. append_char("S"); show_node_list(script_mlist(p)); flush_char;
  12066. append_char("s"); show_node_list(script_script_mlist(p)); flush_char;
  12067. end
  12068. @ @<Display normal noad |p|@>=
  12069. begin case type(p) of
  12070. ord_noad: print_esc("mathord");
  12071. op_noad: print_esc("mathop");
  12072. bin_noad: print_esc("mathbin");
  12073. rel_noad: print_esc("mathrel");
  12074. open_noad: print_esc("mathopen");
  12075. close_noad: print_esc("mathclose");
  12076. punct_noad: print_esc("mathpunct");
  12077. inner_noad: print_esc("mathinner");
  12078. over_noad: print_esc("overline");
  12079. under_noad: print_esc("underline");
  12080. vcenter_noad: print_esc("vcenter");
  12081. radical_noad: begin print_esc("radical"); print_delimiter(left_delimiter(p));
  12082. end;
  12083. accent_noad: begin print_esc("accent"); print_fam_and_char(accent_chr(p));
  12084. end;
  12085. left_noad: begin print_esc("left"); print_delimiter(delimiter(p));
  12086. end;
  12087. right_noad: begin print_esc("right"); print_delimiter(delimiter(p));
  12088. end;
  12089. end;
  12090. if subtype(p)<>normal then
  12091. if subtype(p)=limits then print_esc("limits")
  12092. else print_esc("nolimits");
  12093. if type(p)<left_noad then print_subsidiary_data(nucleus(p),".");
  12094. print_subsidiary_data(supscr(p),"^");
  12095. print_subsidiary_data(subscr(p),"_");
  12096. end
  12097. @ @<Display fraction noad |p|@>=
  12098. begin print_esc("fraction, thickness ");
  12099. if thickness(p)=default_code then print("= default")
  12100. else print_scaled(thickness(p));
  12101. if (small_fam(left_delimiter(p))<>0)or@+
  12102. (small_char(left_delimiter(p))<>min_quarterword)or@|
  12103. (large_fam(left_delimiter(p))<>0)or@|
  12104. (large_char(left_delimiter(p))<>min_quarterword) then
  12105. begin print(", left-delimiter "); print_delimiter(left_delimiter(p));
  12106. end;
  12107. if (small_fam(right_delimiter(p))<>0)or@|
  12108. (small_char(right_delimiter(p))<>min_quarterword)or@|
  12109. (large_fam(right_delimiter(p))<>0)or@|
  12110. (large_char(right_delimiter(p))<>min_quarterword) then
  12111. begin print(", right-delimiter "); print_delimiter(right_delimiter(p));
  12112. end;
  12113. print_subsidiary_data(numerator(p),"\");
  12114. print_subsidiary_data(denominator(p),"/");
  12115. end
  12116. @ That which can be displayed can also be destroyed.
  12117. @<Cases of |flush_node_list| that arise...@>=
  12118. style_node: begin free_node(p,style_node_size); goto done;
  12119. end;
  12120. choice_node:begin flush_node_list(display_mlist(p));
  12121. flush_node_list(text_mlist(p));
  12122. flush_node_list(script_mlist(p));
  12123. flush_node_list(script_script_mlist(p));
  12124. free_node(p,style_node_size); goto done;
  12125. end;
  12126. ord_noad,op_noad,bin_noad,rel_noad,open_noad,close_noad,punct_noad,inner_noad,
  12127. radical_noad,over_noad,under_noad,vcenter_noad,accent_noad:@t@>@;@/
  12128. begin if math_type(nucleus(p))>=sub_box then
  12129. flush_node_list(info(nucleus(p)));
  12130. if math_type(supscr(p))>=sub_box then
  12131. flush_node_list(info(supscr(p)));
  12132. if math_type(subscr(p))>=sub_box then
  12133. flush_node_list(info(subscr(p)));
  12134. if type(p)=radical_noad then free_node(p,radical_noad_size)
  12135. else if type(p)=accent_noad then free_node(p,accent_noad_size)
  12136. else free_node(p,noad_size);
  12137. goto done;
  12138. end;
  12139. left_noad,right_noad: begin free_node(p,noad_size); goto done;
  12140. end;
  12141. fraction_noad: begin flush_node_list(info(numerator(p)));
  12142. flush_node_list(info(denominator(p)));
  12143. free_node(p,fraction_noad_size); goto done;
  12144. end;
  12145. @* \[35] Subroutines for math mode.
  12146. In order to convert mlists to hlists, i.e., noads to nodes, we need several
  12147. subroutines that are conveniently dealt with now.
  12148. Let us first introduce the macros that make it easy to get at the parameters and
  12149. other font information. A size code, which is a multiple of 16, is added to a
  12150. family number to get an index into the table of internal font numbers
  12151. for each combination of family and size. (Be alert: Size codes get
  12152. larger as the type gets smaller.)
  12153. @d text_size=0 {size code for the largest size in a family}
  12154. @d script_size=16 {size code for the medium size in a family}
  12155. @d script_script_size=32 {size code for the smallest size in a family}
  12156. @<Basic printing procedures@>=
  12157. procedure print_size(@!s:integer);
  12158. begin if s=text_size then print_esc("textfont")
  12159. else if s=script_size then print_esc("scriptfont")
  12160. else print_esc("scriptscriptfont");
  12161. end;
  12162. @ Before an mlist is converted to an hlist, \TeX\ makes sure that
  12163. the fonts in family~2 have enough parameters to be math-symbol
  12164. fonts, and that the fonts in family~3 have enough parameters to be
  12165. math-extension fonts. The math-symbol parameters are referred to by using the
  12166. following macros, which take a size code as their parameter; for example,
  12167. |num1(cur_size)| gives the value of the |num1| parameter for the current size.
  12168. @^parameters for symbols@>
  12169. @^font parameters@>
  12170. @d mathsy_end(#)==fam_fnt(2+#)]].sc
  12171. @d mathsy(#)==font_info[#+param_base[mathsy_end
  12172. @d math_x_height==mathsy(5) {height of `\.x'}
  12173. @d math_quad==mathsy(6) {\.{18mu}}
  12174. @d num1==mathsy(8) {numerator shift-up in display styles}
  12175. @d num2==mathsy(9) {numerator shift-up in non-display, non-\.{\\atop}}
  12176. @d num3==mathsy(10) {numerator shift-up in non-display \.{\\atop}}
  12177. @d denom1==mathsy(11) {denominator shift-down in display styles}
  12178. @d denom2==mathsy(12) {denominator shift-down in non-display styles}
  12179. @d sup1==mathsy(13) {superscript shift-up in uncramped display style}
  12180. @d sup2==mathsy(14) {superscript shift-up in uncramped non-display}
  12181. @d sup3==mathsy(15) {superscript shift-up in cramped styles}
  12182. @d sub1==mathsy(16) {subscript shift-down if superscript is absent}
  12183. @d sub2==mathsy(17) {subscript shift-down if superscript is present}
  12184. @d sup_drop==mathsy(18) {superscript baseline below top of large box}
  12185. @d sub_drop==mathsy(19) {subscript baseline below bottom of large box}
  12186. @d delim1==mathsy(20) {size of \.{\\atopwithdelims} delimiters
  12187. in display styles}
  12188. @d delim2==mathsy(21) {size of \.{\\atopwithdelims} delimiters in non-displays}
  12189. @d axis_height==mathsy(22) {height of fraction lines above the baseline}
  12190. @d total_mathsy_params=22
  12191. @ The math-extension parameters have similar macros, but the size code is
  12192. omitted (since it is always |cur_size| when we refer to such parameters).
  12193. @^parameters for symbols@>
  12194. @^font parameters@>
  12195. @d mathex(#)==font_info[#+param_base[fam_fnt(3+cur_size)]].sc
  12196. @d default_rule_thickness==mathex(8) {thickness of \.{\\over} bars}
  12197. @d big_op_spacing1==mathex(9) {minimum clearance above a displayed op}
  12198. @d big_op_spacing2==mathex(10) {minimum clearance below a displayed op}
  12199. @d big_op_spacing3==mathex(11) {minimum baselineskip above displayed op}
  12200. @d big_op_spacing4==mathex(12) {minimum baselineskip below displayed op}
  12201. @d big_op_spacing5==mathex(13) {padding above and below displayed limits}
  12202. @d total_mathex_params=13
  12203. @ We also need to compute the change in style between mlists and their
  12204. subsidiaries. The following macros define the subsidiary style for
  12205. an overlined nucleus (|cramped_style|), for a subscript or a superscript
  12206. (|sub_style| or |sup_style|), or for a numerator or denominator (|num_style|
  12207. or |denom_style|).
  12208. @d cramped_style(#)==2*(# div 2)+cramped {cramp the style}
  12209. @d sub_style(#)==2*(# div 4)+script_style+cramped {smaller and cramped}
  12210. @d sup_style(#)==2*(# div 4)+script_style+(# mod 2) {smaller}
  12211. @d num_style(#)==#+2-2*(# div 6) {smaller unless already script-script}
  12212. @d denom_style(#)==2*(# div 2)+cramped+2-2*(# div 6) {smaller, cramped}
  12213. @ When the style changes, the following piece of program computes associated
  12214. information:
  12215. @<Set up the values of |cur_size| and |cur_mu|, based on |cur_style|@>=
  12216. begin if cur_style<script_style then cur_size:=text_size
  12217. else cur_size:=16*((cur_style-text_style) div 2);
  12218. cur_mu:=x_over_n(math_quad(cur_size),18);
  12219. end
  12220. @ Here is a function that returns a pointer to a rule node having a given
  12221. thickness |t|. The rule will extend horizontally to the boundary of the vlist
  12222. that eventually contains it.
  12223. @p function fraction_rule(@!t:scaled):pointer;
  12224. {construct the bar for a fraction}
  12225. var p:pointer; {the new node}
  12226. begin p:=new_rule; height(p):=t; depth(p):=0; fraction_rule:=p;
  12227. end;
  12228. @ The |overbar| function returns a pointer to a vlist box that consists of
  12229. a given box |b|, above which has been placed a kern of height |k| under a
  12230. fraction rule of thickness |t| under additional space of height |t|.
  12231. @p function overbar(@!b:pointer;@!k,@!t:scaled):pointer;
  12232. var p,@!q:pointer; {nodes being constructed}
  12233. begin p:=new_kern(k); link(p):=b; q:=fraction_rule(t); link(q):=p;
  12234. p:=new_kern(t); link(p):=q; overbar:=vpack(p,natural);
  12235. end;
  12236. @ The |var_delimiter| function, which finds or constructs a sufficiently
  12237. large delimiter, is the most interesting of the auxiliary functions that
  12238. currently concern us. Given a pointer |d| to a delimiter field in some noad,
  12239. together with a size code |s| and a vertical distance |v|, this function
  12240. returns a pointer to a box that contains the smallest variant of |d| whose
  12241. height plus depth is |v| or more. (And if no variant is large enough, it
  12242. returns the largest available variant.) In particular, this routine will
  12243. construct arbitrarily large delimiters from extensible components, if
  12244. |d| leads to such characters.
  12245. The value returned is a box whose |shift_amount| has been set so that
  12246. the box is vertically centered with respect to the axis in the given size.
  12247. If a built-up symbol is returned, the height of the box before shifting
  12248. will be the height of its topmost component.
  12249. @p@t\4@>@<Declare subprocedures for |var_delimiter|@>
  12250. function var_delimiter(@!d:pointer;@!s:small_number;@!v:scaled):pointer;
  12251. label found,continue;
  12252. var b:pointer; {the box that will be constructed}
  12253. @!f,@!g: internal_font_number; {best-so-far and tentative font codes}
  12254. @!c,@!x,@!y: quarterword; {best-so-far and tentative character codes}
  12255. @!m,@!n: integer; {the number of extensible pieces}
  12256. @!u: scaled; {height-plus-depth of a tentative character}
  12257. @!w: scaled; {largest height-plus-depth so far}
  12258. @!q: four_quarters; {character info}
  12259. @!hd: eight_bits; {height-depth byte}
  12260. @!r: four_quarters; {extensible pieces}
  12261. @!z: small_number; {runs through font family members}
  12262. @!large_attempt: boolean; {are we trying the ``large'' variant?}
  12263. begin f:=null_font; w:=0; large_attempt:=false;
  12264. z:=small_fam(d); x:=small_char(d);
  12265. loop@+ begin @<Look at the variants of |(z,x)|; set |f| and |c| whenever
  12266. a better character is found; |goto found| as soon as a
  12267. large enough variant is encountered@>;
  12268. if large_attempt then goto found; {there were none large enough}
  12269. large_attempt:=true; z:=large_fam(d); x:=large_char(d);
  12270. end;
  12271. found: if f<>null_font then
  12272. @<Make variable |b| point to a box for |(f,c)|@>
  12273. else begin b:=new_null_box;
  12274. width(b):=null_delimiter_space; {use this width if no delimiter was found}
  12275. end;
  12276. shift_amount(b):=half(height(b)-depth(b)) - axis_height(s);
  12277. var_delimiter:=b;
  12278. end;
  12279. @ The search process is complicated slightly by the facts that some of the
  12280. characters might not be present in some of the fonts, and they might not
  12281. be probed in increasing order of height.
  12282. @<Look at the variants of |(z,x)|; set |f| and |c|...@>=
  12283. if (z<>0)or(x<>min_quarterword) then
  12284. begin z:=z+s+16;
  12285. repeat z:=z-16; g:=fam_fnt(z);
  12286. if g<>null_font then
  12287. @<Look at the list of characters starting with |x| in
  12288. font |g|; set |f| and |c| whenever
  12289. a better character is found; |goto found| as soon as a
  12290. large enough variant is encountered@>;
  12291. until z<16;
  12292. end
  12293. @ @<Look at the list of characters starting with |x|...@>=
  12294. begin y:=x;
  12295. if (qo(y)>=font_bc[g])and(qo(y)<=font_ec[g]) then
  12296. begin continue: q:=char_info(g)(y);
  12297. if char_exists(q) then
  12298. begin if char_tag(q)=ext_tag then
  12299. begin f:=g; c:=y; goto found;
  12300. end;
  12301. hd:=height_depth(q);
  12302. u:=char_height(g)(hd)+char_depth(g)(hd);
  12303. if u>w then
  12304. begin f:=g; c:=y; w:=u;
  12305. if u>=v then goto found;
  12306. end;
  12307. if char_tag(q)=list_tag then
  12308. begin y:=rem_byte(q); goto continue;
  12309. end;
  12310. end;
  12311. end;
  12312. end
  12313. @ Here is a subroutine that creates a new box, whose list contains a
  12314. single character, and whose width includes the italic correction for
  12315. that character. The height or depth of the box will be negative, if
  12316. the height or depth of the character is negative; thus, this routine
  12317. may deliver a slightly different result than |hpack| would produce.
  12318. @<Declare subprocedures for |var_delimiter|@>=
  12319. function char_box(@!f:internal_font_number;@!c:quarterword):pointer;
  12320. var q:four_quarters;
  12321. @!hd:eight_bits; {|height_depth| byte}
  12322. @!b,@!p:pointer; {the new box and its character node}
  12323. begin q:=char_info(f)(c); hd:=height_depth(q);
  12324. b:=new_null_box; width(b):=char_width(f)(q)+char_italic(f)(q);
  12325. height(b):=char_height(f)(hd); depth(b):=char_depth(f)(hd);
  12326. p:=get_avail; character(p):=c; font(p):=f; list_ptr(b):=p; char_box:=b;
  12327. end;
  12328. @ When the following code is executed, |char_tag(q)| will be equal to
  12329. |ext_tag| if and only if a built-up symbol is supposed to be returned.
  12330. @<Make variable |b| point to a box for |(f,c)|@>=
  12331. if char_tag(q)=ext_tag then
  12332. @<Construct an extensible character in a new box |b|,
  12333. using recipe |rem_byte(q)| and font |f|@>
  12334. else b:=char_box(f,c)
  12335. @ When we build an extensible character, it's handy to have the
  12336. following subroutine, which puts a given character on top
  12337. of the characters already in box |b|:
  12338. @<Declare subprocedures for |var_delimiter|@>=
  12339. procedure stack_into_box(@!b:pointer;@!f:internal_font_number;
  12340. @!c:quarterword);
  12341. var p:pointer; {new node placed into |b|}
  12342. begin p:=char_box(f,c); link(p):=list_ptr(b); list_ptr(b):=p;
  12343. height(b):=height(p);
  12344. end;
  12345. @ Another handy subroutine computes the height plus depth of
  12346. a given character:
  12347. @<Declare subprocedures for |var_delimiter|@>=
  12348. function height_plus_depth(@!f:internal_font_number;@!c:quarterword):scaled;
  12349. var q:four_quarters;
  12350. @!hd:eight_bits; {|height_depth| byte}
  12351. begin q:=char_info(f)(c); hd:=height_depth(q);
  12352. height_plus_depth:=char_height(f)(hd)+char_depth(f)(hd);
  12353. end;
  12354. @ @<Construct an extensible...@>=
  12355. begin b:=new_null_box;
  12356. type(b):=vlist_node;
  12357. r:=font_info[exten_base[f]+rem_byte(q)].qqqq;@/
  12358. @<Compute the minimum suitable height, |w|, and the corresponding
  12359. number of extension steps, |n|; also set |width(b)|@>;
  12360. c:=ext_bot(r);
  12361. if c<>min_quarterword then stack_into_box(b,f,c);
  12362. c:=ext_rep(r);
  12363. for m:=1 to n do stack_into_box(b,f,c);
  12364. c:=ext_mid(r);
  12365. if c<>min_quarterword then
  12366. begin stack_into_box(b,f,c); c:=ext_rep(r);
  12367. for m:=1 to n do stack_into_box(b,f,c);
  12368. end;
  12369. c:=ext_top(r);
  12370. if c<>min_quarterword then stack_into_box(b,f,c);
  12371. depth(b):=w-height(b);
  12372. end
  12373. @ The width of an extensible character is the width of the repeatable
  12374. module. If this module does not have positive height plus depth,
  12375. we don't use any copies of it, otherwise we use as few as possible
  12376. (in groups of two if there is a middle part).
  12377. @<Compute the minimum suitable height, |w|, and...@>=
  12378. c:=ext_rep(r); u:=height_plus_depth(f,c);
  12379. w:=0; q:=char_info(f)(c); width(b):=char_width(f)(q)+char_italic(f)(q);@/
  12380. c:=ext_bot(r);@+if c<>min_quarterword then w:=w+height_plus_depth(f,c);
  12381. c:=ext_mid(r);@+if c<>min_quarterword then w:=w+height_plus_depth(f,c);
  12382. c:=ext_top(r);@+if c<>min_quarterword then w:=w+height_plus_depth(f,c);
  12383. n:=0;
  12384. if u>0 then while w<v do
  12385. begin w:=w+u; incr(n);
  12386. if ext_mid(r)<>min_quarterword then w:=w+u;
  12387. end
  12388. @ The next subroutine is much simpler; it is used for numerators and
  12389. denominators of fractions as well as for displayed operators and
  12390. their limits above and below. It takes a given box~|b| and
  12391. changes it so that the new box is centered in a box of width~|w|.
  12392. The centering is done by putting \.{\\hss} glue at the left and right
  12393. of the list inside |b|, then packaging the new box; thus, the
  12394. actual box might not really be centered, if it already contains
  12395. infinite glue.
  12396. The given box might contain a single character whose italic correction
  12397. has been added to the width of the box; in this case a compensating
  12398. kern is inserted.
  12399. @p function rebox(@!b:pointer;@!w:scaled):pointer;
  12400. var p:pointer; {temporary register for list manipulation}
  12401. @!f:internal_font_number; {font in a one-character box}
  12402. @!v:scaled; {width of a character without italic correction}
  12403. begin if (width(b)<>w)and(list_ptr(b)<>null) then
  12404. begin if type(b)=vlist_node then b:=hpack(b,natural);
  12405. p:=list_ptr(b);
  12406. if (is_char_node(p))and(link(p)=null) then
  12407. begin f:=font(p); v:=char_width(f)(char_info(f)(character(p)));
  12408. if v<>width(b) then link(p):=new_kern(width(b)-v);
  12409. end;
  12410. free_node(b,box_node_size);
  12411. b:=new_glue(ss_glue); link(b):=p;
  12412. while link(p)<>null do p:=link(p);
  12413. link(p):=new_glue(ss_glue);
  12414. rebox:=hpack(b,w,exactly);
  12415. end
  12416. else begin width(b):=w; rebox:=b;
  12417. end;
  12418. end;
  12419. @ Here is a subroutine that creates a new glue specification from another
  12420. one that is expressed in `\.{mu}', given the value of the math unit.
  12421. @d mu_mult(#)==nx_plus_y(n,#,xn_over_d(#,f,@'200000))
  12422. @p function math_glue(@!g:pointer;@!m:scaled):pointer;
  12423. var p:pointer; {the new glue specification}
  12424. @!n:integer; {integer part of |m|}
  12425. @!f:scaled; {fraction part of |m|}
  12426. begin n:=x_over_n(m,@'200000); f:=remainder;@/
  12427. if f<0 then
  12428. begin decr(n); f:=f+@'200000;
  12429. end;
  12430. p:=get_node(glue_spec_size);
  12431. width(p):=mu_mult(width(g)); {convert \.{mu} to \.{pt}}
  12432. stretch_order(p):=stretch_order(g);
  12433. if stretch_order(p)=normal then stretch(p):=mu_mult(stretch(g))
  12434. else stretch(p):=stretch(g);
  12435. shrink_order(p):=shrink_order(g);
  12436. if shrink_order(p)=normal then shrink(p):=mu_mult(shrink(g))
  12437. else shrink(p):=shrink(g);
  12438. math_glue:=p;
  12439. end;
  12440. @ The |math_kern| subroutine removes |mu_glue| from a kern node, given
  12441. the value of the math unit.
  12442. @p procedure math_kern(@!p:pointer;@!m:scaled);
  12443. var @!n:integer; {integer part of |m|}
  12444. @!f:scaled; {fraction part of |m|}
  12445. begin if subtype(p)=mu_glue then
  12446. begin n:=x_over_n(m,@'200000); f:=remainder;@/
  12447. if f<0 then
  12448. begin decr(n); f:=f+@'200000;
  12449. end;
  12450. width(p):=mu_mult(width(p)); subtype(p):=explicit;
  12451. end;
  12452. end;
  12453. @ Sometimes it is necessary to destroy an mlist. The following
  12454. subroutine empties the current list, assuming that |abs(mode)=mmode|.
  12455. @p procedure flush_math;
  12456. begin flush_node_list(link(head)); flush_node_list(incompleat_noad);
  12457. link(head):=null; tail:=head; incompleat_noad:=null;
  12458. end;
  12459. @* \[36] Typesetting math formulas.
  12460. \TeX's most important routine for dealing with formulas is called
  12461. |mlist_to_hlist|. After a formula has been scanned and represented as an
  12462. mlist, this routine converts it to an hlist that can be placed into a box
  12463. or incorporated into the text of a paragraph. There are three implicit
  12464. parameters, passed in global variables: |cur_mlist| points to the first
  12465. node or noad in the given mlist (and it might be |null|); |cur_style| is a
  12466. style code; and |mlist_penalties| is |true| if penalty nodes for potential
  12467. line breaks are to be inserted into the resulting hlist. After
  12468. |mlist_to_hlist| has acted, |link(temp_head)| points to the translated hlist.
  12469. Since mlists can be inside mlists, the procedure is recursive. And since this
  12470. is not part of \TeX's inner loop, the program has been written in a manner
  12471. that stresses compactness over efficiency.
  12472. @^recursion@>
  12473. @<Glob...@>=
  12474. @!cur_mlist:pointer; {beginning of mlist to be translated}
  12475. @!cur_style:small_number; {style code at current place in the list}
  12476. @!cur_size:small_number; {size code corresponding to |cur_style|}
  12477. @!cur_mu:scaled; {the math unit width corresponding to |cur_size|}
  12478. @!mlist_penalties:boolean; {should |mlist_to_hlist| insert penalties?}
  12479. @ The recursion in |mlist_to_hlist| is due primarily to a subroutine
  12480. called |clean_box| that puts a given noad field into a box using a given
  12481. math style; |mlist_to_hlist| can call |clean_box|, which can call
  12482. |mlist_to_hlist|.
  12483. @^recursion@>
  12484. The box returned by |clean_box| is ``clean'' in the
  12485. sense that its |shift_amount| is zero.
  12486. @p procedure@?mlist_to_hlist; forward;@t\2@>@/
  12487. function clean_box(@!p:pointer;@!s:small_number):pointer;
  12488. label found;
  12489. var q:pointer; {beginning of a list to be boxed}
  12490. @!save_style:small_number; {|cur_style| to be restored}
  12491. @!x:pointer; {box to be returned}
  12492. @!r:pointer; {temporary pointer}
  12493. begin case math_type(p) of
  12494. math_char: begin cur_mlist:=new_noad; mem[nucleus(cur_mlist)]:=mem[p];
  12495. end;
  12496. sub_box: begin q:=info(p); goto found;
  12497. end;
  12498. sub_mlist: cur_mlist:=info(p);
  12499. othercases begin q:=new_null_box; goto found;
  12500. end
  12501. endcases;@/
  12502. save_style:=cur_style; cur_style:=s; mlist_penalties:=false;@/
  12503. mlist_to_hlist; q:=link(temp_head); {recursive call}
  12504. cur_style:=save_style; {restore the style}
  12505. @<Set up the values of |cur_size| and |cur_mu|, based on |cur_style|@>;
  12506. found: if is_char_node(q)or(q=null) then x:=hpack(q,natural)
  12507. else if (link(q)=null)and(type(q)<=vlist_node)and(shift_amount(q)=0) then
  12508. x:=q {it's already clean}
  12509. else x:=hpack(q,natural);
  12510. @<Simplify a trivial box@>;
  12511. clean_box:=x;
  12512. end;
  12513. @ Here we save memory space in a common case.
  12514. @<Simplify a trivial box@>=
  12515. q:=list_ptr(x);
  12516. if is_char_node(q) then
  12517. begin r:=link(q);
  12518. if r<>null then if link(r)=null then if not is_char_node(r) then
  12519. if type(r)=kern_node then {unneeded italic correction}
  12520. begin free_node(r,small_node_size); link(q):=null;
  12521. end;
  12522. end
  12523. @ It is convenient to have a procedure that converts a |math_char|
  12524. field to an ``unpacked'' form. The |fetch| routine sets |cur_f|, |cur_c|,
  12525. and |cur_i| to the font code, character code, and character information bytes of
  12526. a given noad field. It also takes care of issuing error messages for
  12527. nonexistent characters; in such cases, |char_exists(cur_i)| will be |false|
  12528. after |fetch| has acted, and the field will also have been reset to |empty|.
  12529. @p procedure fetch(@!a:pointer); {unpack the |math_char| field |a|}
  12530. begin cur_c:=character(a); cur_f:=fam_fnt(fam(a)+cur_size);
  12531. if cur_f=null_font then
  12532. @<Complain about an undefined family and set |cur_i| null@>
  12533. else begin if (qo(cur_c)>=font_bc[cur_f])and(qo(cur_c)<=font_ec[cur_f]) then
  12534. cur_i:=char_info(cur_f)(cur_c)
  12535. else cur_i:=null_character;
  12536. if not(char_exists(cur_i)) then
  12537. begin char_warning(cur_f,qo(cur_c));
  12538. math_type(a):=empty; cur_i:=null_character;
  12539. end;
  12540. end;
  12541. end;
  12542. @ @<Complain about an undefined family...@>=
  12543. begin print_err(""); print_size(cur_size); print_char(" ");
  12544. print_int(fam(a)); print(" is undefined (character ");
  12545. print_ASCII(qo(cur_c)); print_char(")");
  12546. help4("Somewhere in the math formula just ended, you used the")@/
  12547. ("stated character from an undefined font family. For example,")@/
  12548. ("plain TeX doesn't allow \it or \sl in subscripts. Proceed,")@/
  12549. ("and I'll try to forget that I needed that character.");
  12550. error; cur_i:=null_character; math_type(a):=empty;
  12551. end
  12552. @ The outputs of |fetch| are placed in global variables.
  12553. @<Glob...@>=
  12554. @!cur_f:internal_font_number; {the |font| field of a |math_char|}
  12555. @!cur_c:quarterword; {the |character| field of a |math_char|}
  12556. @!cur_i:four_quarters; {the |char_info| of a |math_char|,
  12557. or a lig/kern instruction}
  12558. @ We need to do a lot of different things, so |mlist_to_hlist| makes two
  12559. passes over the given mlist.
  12560. The first pass does most of the processing: It removes ``mu'' spacing from
  12561. glue, it recursively evaluates all subsidiary mlists so that only the
  12562. top-level mlist remains to be handled, it puts fractions and square roots
  12563. and such things into boxes, it attaches subscripts and superscripts, and
  12564. it computes the overall height and depth of the top-level mlist so that
  12565. the size of delimiters for a |left_noad| and a |right_noad| will be known.
  12566. The hlist resulting from each noad is recorded in that noad's |new_hlist|
  12567. field, an integer field that replaces the |nucleus| or |thickness|.
  12568. @^recursion@>
  12569. The second pass eliminates all noads and inserts the correct glue and
  12570. penalties between nodes.
  12571. @d new_hlist(#)==mem[nucleus(#)].int {the translation of an mlist}
  12572. @ Here is the overall plan of |mlist_to_hlist|, and the list of its
  12573. local variables.
  12574. @d done_with_noad=80 {go here when a noad has been fully translated}
  12575. @d done_with_node=81 {go here when a node has been fully converted}
  12576. @d check_dimensions=82 {go here to update |max_h| and |max_d|}
  12577. @d delete_q=83 {go here to delete |q| and move to the next node}
  12578. @p@t\4@>@<Declare math construction procedures@>
  12579. procedure mlist_to_hlist;
  12580. label reswitch, check_dimensions, done_with_noad, done_with_node, delete_q,
  12581. done;
  12582. var mlist:pointer; {beginning of the given list}
  12583. @!penalties:boolean; {should penalty nodes be inserted?}
  12584. @!style:small_number; {the given style}
  12585. @!save_style:small_number; {holds |cur_style| during recursion}
  12586. @!q:pointer; {runs through the mlist}
  12587. @!r:pointer; {the most recent noad preceding |q|}
  12588. @!r_type:small_number; {the |type| of noad |r|, or |op_noad| if |r=null|}
  12589. @!t:small_number; {the effective |type| of noad |q| during the second pass}
  12590. @!p,@!x,@!y,@!z: pointer; {temporary registers for list construction}
  12591. @!pen:integer; {a penalty to be inserted}
  12592. @!s:small_number; {the size of a noad to be deleted}
  12593. @!max_h,@!max_d:scaled; {maximum height and depth of the list translated so far}
  12594. @!delta:scaled; {offset between subscript and superscript}
  12595. begin mlist:=cur_mlist; penalties:=mlist_penalties;
  12596. style:=cur_style; {tuck global parameters away as local variables}
  12597. q:=mlist; r:=null; r_type:=op_noad; max_h:=0; max_d:=0;
  12598. @<Set up the values of |cur_size| and |cur_mu|, based on |cur_style|@>;
  12599. while q<>null do @<Process node-or-noad |q| as much as possible in preparation
  12600. for the second pass of |mlist_to_hlist|, then move to the next
  12601. item in the mlist@>;
  12602. @<Convert \(a)a final |bin_noad| to an |ord_noad|@>;
  12603. @<Make a second pass over the mlist, removing all noads and inserting the
  12604. proper spacing and penalties@>;
  12605. end;
  12606. @ We use the fact that no character nodes appear in an mlist, hence
  12607. the field |type(q)| is always present.
  12608. @<Process node-or-noad...@>=
  12609. begin @<Do first-pass processing based on |type(q)|; |goto done_with_noad|
  12610. if a noad has been fully processed, |goto check_dimensions| if it
  12611. has been translated into |new_hlist(q)|, or |goto done_with_node|
  12612. if a node has been fully processed@>;
  12613. check_dimensions: z:=hpack(new_hlist(q),natural);
  12614. if height(z)>max_h then max_h:=height(z);
  12615. if depth(z)>max_d then max_d:=depth(z);
  12616. free_node(z,box_node_size);
  12617. done_with_noad: r:=q; r_type:=type(r);
  12618. done_with_node: q:=link(q);
  12619. end
  12620. @ One of the things we must do on the first pass is change a |bin_noad| to
  12621. an |ord_noad| if the |bin_noad| is not in the context of a binary operator.
  12622. The values of |r| and |r_type| make this fairly easy.
  12623. @<Do first-pass processing...@>=
  12624. reswitch: delta:=0;
  12625. case type(q) of
  12626. bin_noad: case r_type of
  12627. bin_noad,op_noad,rel_noad,open_noad,punct_noad,left_noad:
  12628. begin type(q):=ord_noad; goto reswitch;
  12629. end;
  12630. othercases do_nothing
  12631. endcases;
  12632. rel_noad,close_noad,punct_noad,right_noad: begin@t@>@;@/
  12633. @<Convert \(a)a final |bin_noad| to an |ord_noad|@>;
  12634. if type(q)=right_noad then goto done_with_noad;
  12635. end;
  12636. @t\4@>@<Cases for noads that can follow a |bin_noad|@>@;
  12637. @t\4@>@<Cases for nodes that can appear in an mlist, after which we
  12638. |goto done_with_node|@>@;
  12639. othercases confusion("mlist1")
  12640. @:this can't happen mlist1}{\quad mlist1@>
  12641. endcases;@/
  12642. @<Convert \(n)|nucleus(q)| to an hlist and attach the sub/superscripts@>
  12643. @ @<Convert \(a)a final |bin_noad| to an |ord_noad|@>=
  12644. if r_type=bin_noad then type(r):=ord_noad
  12645. @ @<Cases for nodes that can appear in an mlist...@>=
  12646. style_node: begin cur_style:=subtype(q);
  12647. @<Set up the values of |cur_size| and |cur_mu|, based on |cur_style|@>;
  12648. goto done_with_node;
  12649. end;
  12650. choice_node: @<Change this node to a style node followed by the correct choice,
  12651. then |goto done_with_node|@>;
  12652. ins_node,mark_node,adjust_node,
  12653. whatsit_node,penalty_node,disc_node: goto done_with_node;
  12654. rule_node: begin if height(q)>max_h then max_h:=height(q);
  12655. if depth(q)>max_d then max_d:=depth(q); goto done_with_node;
  12656. end;
  12657. glue_node: begin @<Convert \(m)math glue to ordinary glue@>;
  12658. goto done_with_node;
  12659. end;
  12660. kern_node: begin math_kern(q,cur_mu); goto done_with_node;
  12661. end;
  12662. @ @d choose_mlist(#)==begin p:=#(q); #(q):=null;@+end
  12663. @<Change this node to a style node...@>=
  12664. begin case cur_style div 2 of
  12665. 0: choose_mlist(display_mlist); {|display_style=0|}
  12666. 1: choose_mlist(text_mlist); {|text_style=2|}
  12667. 2: choose_mlist(script_mlist); {|script_style=4|}
  12668. 3: choose_mlist(script_script_mlist); {|script_script_style=6|}
  12669. end; {there are no other cases}
  12670. flush_node_list(display_mlist(q));
  12671. flush_node_list(text_mlist(q));
  12672. flush_node_list(script_mlist(q));
  12673. flush_node_list(script_script_mlist(q));@/
  12674. type(q):=style_node; subtype(q):=cur_style; width(q):=0; depth(q):=0;
  12675. if p<>null then
  12676. begin z:=link(q); link(q):=p;
  12677. while link(p)<>null do p:=link(p);
  12678. link(p):=z;
  12679. end;
  12680. goto done_with_node;
  12681. end
  12682. @ Conditional math glue (`\.{\\nonscript}') results in a |glue_node|
  12683. pointing to |zero_glue|, with |subtype(q)=cond_math_glue|; in such a case
  12684. the node following will be eliminated if it is a glue or kern node and if the
  12685. current size is different from |text_size|. Unconditional math glue
  12686. (`\.{\\muskip}') is converted to normal glue by multiplying the dimensions
  12687. by |cur_mu|.
  12688. @!@:non_script_}{\.{\\nonscript} primitive@>
  12689. @<Convert \(m)math glue to ordinary glue@>=
  12690. if subtype(q)=mu_glue then
  12691. begin x:=glue_ptr(q);
  12692. y:=math_glue(x,cur_mu); delete_glue_ref(x); glue_ptr(q):=y;
  12693. subtype(q):=normal;
  12694. end
  12695. else if (cur_size<>text_size)and(subtype(q)=cond_math_glue) then
  12696. begin p:=link(q);
  12697. if p<>null then if (type(p)=glue_node)or(type(p)=kern_node) then
  12698. begin link(q):=link(p); link(p):=null; flush_node_list(p);
  12699. end;
  12700. end
  12701. @ @<Cases for noads that can follow a |bin_noad|@>=
  12702. left_noad: goto done_with_noad;
  12703. fraction_noad: begin make_fraction(q); goto check_dimensions;
  12704. end;
  12705. op_noad: begin delta:=make_op(q);
  12706. if subtype(q)=limits then goto check_dimensions;
  12707. end;
  12708. ord_noad: make_ord(q);
  12709. open_noad,inner_noad: do_nothing;
  12710. radical_noad: make_radical(q);
  12711. over_noad: make_over(q);
  12712. under_noad: make_under(q);
  12713. accent_noad: make_math_accent(q);
  12714. vcenter_noad: make_vcenter(q);
  12715. @ Most of the actual construction work of |mlist_to_hlist| is done
  12716. by procedures with names
  12717. like |make_fraction|, |make_radical|, etc. To illustrate
  12718. the general setup of such procedures, let's begin with a couple of
  12719. simple ones.
  12720. @<Declare math...@>=
  12721. procedure make_over(@!q:pointer);
  12722. begin info(nucleus(q)):=@|
  12723. overbar(clean_box(nucleus(q),cramped_style(cur_style)),@|
  12724. 3*default_rule_thickness,default_rule_thickness);
  12725. math_type(nucleus(q)):=sub_box;
  12726. end;
  12727. @ @<Declare math...@>=
  12728. procedure make_under(@!q:pointer);
  12729. var p,@!x,@!y: pointer; {temporary registers for box construction}
  12730. @!delta:scaled; {overall height plus depth}
  12731. begin x:=clean_box(nucleus(q),cur_style);
  12732. p:=new_kern(3*default_rule_thickness); link(x):=p;
  12733. link(p):=fraction_rule(default_rule_thickness);
  12734. y:=vpack(x,natural);
  12735. delta:=height(y)+depth(y)+default_rule_thickness;
  12736. height(y):=height(x); depth(y):=delta-height(y);
  12737. info(nucleus(q)):=y; math_type(nucleus(q)):=sub_box;
  12738. end;
  12739. @ @<Declare math...@>=
  12740. procedure make_vcenter(@!q:pointer);
  12741. var v:pointer; {the box that should be centered vertically}
  12742. @!delta:scaled; {its height plus depth}
  12743. begin v:=info(nucleus(q));
  12744. if type(v)<>vlist_node then confusion("vcenter");
  12745. @:this can't happen vcenter}{\quad vcenter@>
  12746. delta:=height(v)+depth(v);
  12747. height(v):=axis_height(cur_size)+half(delta);
  12748. depth(v):=delta-height(v);
  12749. end;
  12750. @ According to the rules in the \.{DVI} file specifications, we ensure alignment
  12751. @^square roots@>
  12752. between a square root sign and the rule above its nucleus by assuming that the
  12753. baseline of the square-root symbol is the same as the bottom of the rule. The
  12754. height of the square-root symbol will be the thickness of the rule, and the
  12755. depth of the square-root symbol should exceed or equal the height-plus-depth
  12756. of the nucleus plus a certain minimum clearance~|clr|. The symbol will be
  12757. placed so that the actual clearance is |clr| plus half the excess.
  12758. @<Declare math...@>=
  12759. procedure make_radical(@!q:pointer);
  12760. var x,@!y:pointer; {temporary registers for box construction}
  12761. @!delta,@!clr:scaled; {dimensions involved in the calculation}
  12762. begin x:=clean_box(nucleus(q),cramped_style(cur_style));
  12763. if cur_style<text_style then {display style}
  12764. clr:=default_rule_thickness+(abs(math_x_height(cur_size)) div 4)
  12765. else begin clr:=default_rule_thickness; clr:=clr + (abs(clr) div 4);
  12766. end;
  12767. y:=var_delimiter(left_delimiter(q),cur_size,height(x)+depth(x)+clr+
  12768. default_rule_thickness);
  12769. delta:=depth(y)-(height(x)+depth(x)+clr);
  12770. if delta>0 then clr:=clr+half(delta); {increase the actual clearance}
  12771. shift_amount(y):=-(height(x)+clr);
  12772. link(y):=overbar(x,clr,height(y));
  12773. info(nucleus(q)):=hpack(y,natural); math_type(nucleus(q)):=sub_box;
  12774. end;
  12775. @ Slants are not considered when placing accents in math mode. The accenter is
  12776. centered over the accentee, and the accent width is treated as zero with
  12777. respect to the size of the final box.
  12778. @<Declare math...@>=
  12779. procedure make_math_accent(@!q:pointer);
  12780. label done,done1;
  12781. var p,@!x,@!y:pointer; {temporary registers for box construction}
  12782. @!a:integer; {address of lig/kern instruction}
  12783. @!c:quarterword; {accent character}
  12784. @!f:internal_font_number; {its font}
  12785. @!i:four_quarters; {its |char_info|}
  12786. @!s:scaled; {amount to skew the accent to the right}
  12787. @!h:scaled; {height of character being accented}
  12788. @!delta:scaled; {space to remove between accent and accentee}
  12789. @!w:scaled; {width of the accentee, not including sub/superscripts}
  12790. begin fetch(accent_chr(q));
  12791. if char_exists(cur_i) then
  12792. begin i:=cur_i; c:=cur_c; f:=cur_f;@/
  12793. @<Compute the amount of skew@>;
  12794. x:=clean_box(nucleus(q),cramped_style(cur_style)); w:=width(x); h:=height(x);
  12795. @<Switch to a larger accent if available and appropriate@>;
  12796. if h<x_height(f) then delta:=h@+else delta:=x_height(f);
  12797. if (math_type(supscr(q))<>empty)or(math_type(subscr(q))<>empty) then
  12798. if math_type(nucleus(q))=math_char then
  12799. @<Swap the subscript and superscript into box |x|@>;
  12800. y:=char_box(f,c);
  12801. shift_amount(y):=s+half(w-width(y));
  12802. width(y):=0; p:=new_kern(-delta); link(p):=x; link(y):=p;
  12803. y:=vpack(y,natural); width(y):=width(x);
  12804. if height(y)<h then @<Make the height of box |y| equal to |h|@>;
  12805. info(nucleus(q)):=y;
  12806. math_type(nucleus(q)):=sub_box;
  12807. end;
  12808. end;
  12809. @ @<Make the height of box |y|...@>=
  12810. begin p:=new_kern(h-height(y)); link(p):=list_ptr(y); list_ptr(y):=p;
  12811. height(y):=h;
  12812. end
  12813. @ @<Switch to a larger accent if available and appropriate@>=
  12814. loop@+ begin if char_tag(i)<>list_tag then goto done;
  12815. y:=rem_byte(i);
  12816. i:=char_info(f)(y);
  12817. if not char_exists(i) then goto done;
  12818. if char_width(f)(i)>w then goto done;
  12819. c:=y;
  12820. end;
  12821. done:
  12822. @ @<Compute the amount of skew@>=
  12823. s:=0;
  12824. if math_type(nucleus(q))=math_char then
  12825. begin fetch(nucleus(q));
  12826. if char_tag(cur_i)=lig_tag then
  12827. begin a:=lig_kern_start(cur_f)(cur_i);
  12828. cur_i:=font_info[a].qqqq;
  12829. if skip_byte(cur_i)>stop_flag then
  12830. begin a:=lig_kern_restart(cur_f)(cur_i);
  12831. cur_i:=font_info[a].qqqq;
  12832. end;
  12833. loop@+ begin if qo(next_char(cur_i))=skew_char[cur_f] then
  12834. begin if op_byte(cur_i)>=kern_flag then
  12835. if skip_byte(cur_i)<=stop_flag then s:=char_kern(cur_f)(cur_i);
  12836. goto done1;
  12837. end;
  12838. if skip_byte(cur_i)>=stop_flag then goto done1;
  12839. a:=a+qo(skip_byte(cur_i))+1;
  12840. cur_i:=font_info[a].qqqq;
  12841. end;
  12842. end;
  12843. end;
  12844. done1:
  12845. @ @<Swap the subscript and superscript into box |x|@>=
  12846. begin flush_node_list(x); x:=new_noad;
  12847. mem[nucleus(x)]:=mem[nucleus(q)];
  12848. mem[supscr(x)]:=mem[supscr(q)];
  12849. mem[subscr(x)]:=mem[subscr(q)];@/
  12850. mem[supscr(q)].hh:=empty_field;
  12851. mem[subscr(q)].hh:=empty_field;@/
  12852. math_type(nucleus(q)):=sub_mlist; info(nucleus(q)):=x;
  12853. x:=clean_box(nucleus(q),cur_style); delta:=delta+height(x)-h; h:=height(x);
  12854. end
  12855. @ The |make_fraction| procedure is a bit different because it sets
  12856. |new_hlist(q)| directly rather than making a sub-box.
  12857. @<Declare math...@>=
  12858. procedure make_fraction(@!q:pointer);
  12859. var p,@!v,@!x,@!y,@!z:pointer; {temporary registers for box construction}
  12860. @!delta,@!delta1,@!delta2,@!shift_up,@!shift_down,@!clr:scaled;
  12861. {dimensions for box calculations}
  12862. begin if thickness(q)=default_code then thickness(q):=default_rule_thickness;
  12863. @<Create equal-width boxes |x| and |z| for the numerator and denominator,
  12864. and compute the default amounts |shift_up| and |shift_down| by which they
  12865. are displaced from the baseline@>;
  12866. if thickness(q)=0 then @<Adjust \(s)|shift_up| and |shift_down| for the case
  12867. of no fraction line@>
  12868. else @<Adjust \(s)|shift_up| and |shift_down| for the case of a fraction line@>;
  12869. @<Construct a vlist box for the fraction, according to |shift_up| and
  12870. |shift_down|@>;
  12871. @<Put the \(f)fraction into a box with its delimiters, and make |new_hlist(q)|
  12872. point to it@>;
  12873. end;
  12874. @ @<Create equal-width boxes |x| and |z| for the numerator and denom...@>=
  12875. x:=clean_box(numerator(q),num_style(cur_style));
  12876. z:=clean_box(denominator(q),denom_style(cur_style));
  12877. if width(x)<width(z) then x:=rebox(x,width(z))
  12878. else z:=rebox(z,width(x));
  12879. if cur_style<text_style then {display style}
  12880. begin shift_up:=num1(cur_size); shift_down:=denom1(cur_size);
  12881. end
  12882. else begin shift_down:=denom2(cur_size);
  12883. if thickness(q)<>0 then shift_up:=num2(cur_size)
  12884. else shift_up:=num3(cur_size);
  12885. end
  12886. @ The numerator and denominator must be separated by a certain minimum
  12887. clearance, called |clr| in the following program. The difference between
  12888. |clr| and the actual clearance is twice |delta|.
  12889. @<Adjust \(s)|shift_up| and |shift_down| for the case of no fraction line@>=
  12890. begin if cur_style<text_style then clr:=7*default_rule_thickness
  12891. else clr:=3*default_rule_thickness;
  12892. delta:=half(clr-((shift_up-depth(x))-(height(z)-shift_down)));
  12893. if delta>0 then
  12894. begin shift_up:=shift_up+delta;
  12895. shift_down:=shift_down+delta;
  12896. end;
  12897. end
  12898. @ In the case of a fraction line, the minimum clearance depends on the actual
  12899. thickness of the line.
  12900. @<Adjust \(s)|shift_up| and |shift_down| for the case of a fraction line@>=
  12901. begin if cur_style<text_style then clr:=3*thickness(q)
  12902. else clr:=thickness(q);
  12903. delta:=half(thickness(q));
  12904. delta1:=clr-((shift_up-depth(x))-(axis_height(cur_size)+delta));
  12905. delta2:=clr-((axis_height(cur_size)-delta)-(height(z)-shift_down));
  12906. if delta1>0 then shift_up:=shift_up+delta1;
  12907. if delta2>0 then shift_down:=shift_down+delta2;
  12908. end
  12909. @ @<Construct a vlist box for the fraction...@>=
  12910. v:=new_null_box; type(v):=vlist_node;
  12911. height(v):=shift_up+height(x); depth(v):=depth(z)+shift_down;
  12912. width(v):=width(x); {this also equals |width(z)|}
  12913. if thickness(q)=0 then
  12914. begin p:=new_kern((shift_up-depth(x))-(height(z)-shift_down));
  12915. link(p):=z;
  12916. end
  12917. else begin y:=fraction_rule(thickness(q));@/
  12918. p:=new_kern((axis_height(cur_size)-delta)-@|(height(z)-shift_down));@/
  12919. link(y):=p; link(p):=z;@/
  12920. p:=new_kern((shift_up-depth(x))-(axis_height(cur_size)+delta));
  12921. link(p):=y;
  12922. end;
  12923. link(x):=p; list_ptr(v):=x
  12924. @ @<Put the \(f)fraction into a box with its delimiters...@>=
  12925. if cur_style<text_style then delta:=delim1(cur_size)
  12926. else delta:=delim2(cur_size);
  12927. x:=var_delimiter(left_delimiter(q), cur_size, delta); link(x):=v;@/
  12928. z:=var_delimiter(right_delimiter(q), cur_size, delta); link(v):=z;@/
  12929. new_hlist(q):=hpack(x,natural)
  12930. @ If the nucleus of an |op_noad| is a single character, it is to be
  12931. centered vertically with respect to the axis, after first being enlarged
  12932. (via a character list in the font) if we are in display style. The normal
  12933. convention for placing displayed limits is to put them above and below the
  12934. operator in display style.
  12935. The italic correction is removed from the character if there is a subscript
  12936. and the limits are not being displayed. The |make_op|
  12937. routine returns the value that should be used as an offset between
  12938. subscript and superscript.
  12939. After |make_op| has acted, |subtype(q)| will be |limits| if and only if
  12940. the limits have been set above and below the operator. In that case,
  12941. |new_hlist(q)| will already contain the desired final box.
  12942. @<Declare math...@>=
  12943. function make_op(@!q:pointer):scaled;
  12944. var delta:scaled; {offset between subscript and superscript}
  12945. @!p,@!v,@!x,@!y,@!z:pointer; {temporary registers for box construction}
  12946. @!c:quarterword;@+@!i:four_quarters; {registers for character examination}
  12947. @!shift_up,@!shift_down:scaled; {dimensions for box calculation}
  12948. begin if (subtype(q)=normal)and(cur_style<text_style) then
  12949. subtype(q):=limits;
  12950. if math_type(nucleus(q))=math_char then
  12951. begin fetch(nucleus(q));
  12952. if (cur_style<text_style)and(char_tag(cur_i)=list_tag) then {make it larger}
  12953. begin c:=rem_byte(cur_i); i:=char_info(cur_f)(c);
  12954. if char_exists(i) then
  12955. begin cur_c:=c; cur_i:=i; character(nucleus(q)):=c;
  12956. end;
  12957. end;
  12958. delta:=char_italic(cur_f)(cur_i); x:=clean_box(nucleus(q),cur_style);
  12959. if (math_type(subscr(q))<>empty)and(subtype(q)<>limits) then
  12960. width(x):=width(x)-delta; {remove italic correction}
  12961. shift_amount(x):=half(height(x)-depth(x)) - axis_height(cur_size);
  12962. {center vertically}
  12963. math_type(nucleus(q)):=sub_box; info(nucleus(q)):=x;
  12964. end
  12965. else delta:=0;
  12966. if subtype(q)=limits then
  12967. @<Construct a box with limits above and below it, skewed by |delta|@>;
  12968. make_op:=delta;
  12969. end;
  12970. @ The following program builds a vlist box |v| for displayed limits. The
  12971. width of the box is not affected by the fact that the limits may be skewed.
  12972. @<Construct a box with limits above and below it...@>=
  12973. begin x:=clean_box(supscr(q),sup_style(cur_style));
  12974. y:=clean_box(nucleus(q),cur_style);
  12975. z:=clean_box(subscr(q),sub_style(cur_style));
  12976. v:=new_null_box; type(v):=vlist_node; width(v):=width(y);
  12977. if width(x)>width(v) then width(v):=width(x);
  12978. if width(z)>width(v) then width(v):=width(z);
  12979. x:=rebox(x,width(v)); y:=rebox(y,width(v)); z:=rebox(z,width(v));@/
  12980. shift_amount(x):=half(delta); shift_amount(z):=-shift_amount(x);
  12981. height(v):=height(y); depth(v):=depth(y);
  12982. @<Attach the limits to |y| and adjust |height(v)|, |depth(v)| to
  12983. account for their presence@>;
  12984. new_hlist(q):=v;
  12985. end
  12986. @ We use |shift_up| and |shift_down| in the following program for the
  12987. amount of glue between the displayed operator |y| and its limits |x| and
  12988. |z|. The vlist inside box |v| will consist of |x| followed by |y| followed
  12989. by |z|, with kern nodes for the spaces between and around them.
  12990. @<Attach the limits to |y| and adjust |height(v)|, |depth(v)|...@>=
  12991. if math_type(supscr(q))=empty then
  12992. begin free_node(x,box_node_size); list_ptr(v):=y;
  12993. end
  12994. else begin shift_up:=big_op_spacing3-depth(x);
  12995. if shift_up<big_op_spacing1 then shift_up:=big_op_spacing1;
  12996. p:=new_kern(shift_up); link(p):=y; link(x):=p;@/
  12997. p:=new_kern(big_op_spacing5); link(p):=x; list_ptr(v):=p;
  12998. height(v):=height(v)+big_op_spacing5+height(x)+depth(x)+shift_up;
  12999. end;
  13000. if math_type(subscr(q))=empty then free_node(z,box_node_size)
  13001. else begin shift_down:=big_op_spacing4-height(z);
  13002. if shift_down<big_op_spacing2 then shift_down:=big_op_spacing2;
  13003. p:=new_kern(shift_down); link(y):=p; link(p):=z;@/
  13004. p:=new_kern(big_op_spacing5); link(z):=p;
  13005. depth(v):=depth(v)+big_op_spacing5+height(z)+depth(z)+shift_down;
  13006. end
  13007. @ A ligature found in a math formula does not create a |ligature_node|, because
  13008. there is no question of hyphenation afterwards; the ligature will simply be
  13009. stored in an ordinary |char_node|, after residing in an |ord_noad|.
  13010. The |math_type| is converted to |math_text_char| here if we would not want to
  13011. apply an italic correction to the current character unless it belongs
  13012. to a math font (i.e., a font with |space=0|).
  13013. No boundary characters enter into these ligatures.
  13014. @<Declare math...@>=
  13015. procedure make_ord(@!q:pointer);
  13016. label restart,exit;
  13017. var a:integer; {address of lig/kern instruction}
  13018. @!p,@!r:pointer; {temporary registers for list manipulation}
  13019. begin restart:@t@>@;@/
  13020. if math_type(subscr(q))=empty then if math_type(supscr(q))=empty then
  13021. if math_type(nucleus(q))=math_char then
  13022. begin p:=link(q);
  13023. if p<>null then if (type(p)>=ord_noad)and(type(p)<=punct_noad) then
  13024. if math_type(nucleus(p))=math_char then
  13025. if fam(nucleus(p))=fam(nucleus(q)) then
  13026. begin math_type(nucleus(q)):=math_text_char;
  13027. fetch(nucleus(q));
  13028. if char_tag(cur_i)=lig_tag then
  13029. begin a:=lig_kern_start(cur_f)(cur_i);
  13030. cur_c:=character(nucleus(p));
  13031. cur_i:=font_info[a].qqqq;
  13032. if skip_byte(cur_i)>stop_flag then
  13033. begin a:=lig_kern_restart(cur_f)(cur_i);
  13034. cur_i:=font_info[a].qqqq;
  13035. end;
  13036. loop@+ begin @<If instruction |cur_i| is a kern with |cur_c|, attach
  13037. the kern after~|q|; or if it is a ligature with |cur_c|, combine
  13038. noads |q| and~|p| appropriately; then |return| if the cursor has
  13039. moved past a noad, or |goto restart|@>;
  13040. if skip_byte(cur_i)>=stop_flag then return;
  13041. a:=a+qo(skip_byte(cur_i))+1;
  13042. cur_i:=font_info[a].qqqq;
  13043. end;
  13044. end;
  13045. end;
  13046. end;
  13047. exit:end;
  13048. @ Note that a ligature between an |ord_noad| and another kind of noad
  13049. is replaced by an |ord_noad|, when the two noads collapse into one.
  13050. But we could make a parenthesis (say) change shape when it follows
  13051. certain letters. Presumably a font designer will define such
  13052. ligatures only when this convention makes sense.
  13053. \chardef\?='174 % vertical line to indicate character retention
  13054. @<If instruction |cur_i| is a kern with |cur_c|, ...@>=
  13055. if next_char(cur_i)=cur_c then if skip_byte(cur_i)<=stop_flag then
  13056. if op_byte(cur_i)>=kern_flag then
  13057. begin p:=new_kern(char_kern(cur_f)(cur_i));
  13058. link(p):=link(q); link(q):=p; return;
  13059. end
  13060. else begin check_interrupt; {allow a way out of infinite ligature loop}
  13061. case op_byte(cur_i) of
  13062. qi(1),qi(5): character(nucleus(q)):=rem_byte(cur_i); {\.{=:\?}, \.{=:\?>}}
  13063. qi(2),qi(6): character(nucleus(p)):=rem_byte(cur_i); {\.{\?=:}, \.{\?=:>}}
  13064. qi(3),qi(7),qi(11):begin r:=new_noad; {\.{\?=:\?}, \.{\?=:\?>}, \.{\?=:\?>>}}
  13065. character(nucleus(r)):=rem_byte(cur_i);
  13066. fam(nucleus(r)):=fam(nucleus(q));@/
  13067. link(q):=r; link(r):=p;
  13068. if op_byte(cur_i)<qi(11) then math_type(nucleus(r)):=math_char
  13069. else math_type(nucleus(r)):=math_text_char; {prevent combination}
  13070. end;
  13071. othercases begin link(q):=link(p);
  13072. character(nucleus(q)):=rem_byte(cur_i); {\.{=:}}
  13073. mem[subscr(q)]:=mem[subscr(p)]; mem[supscr(q)]:=mem[supscr(p)];@/
  13074. free_node(p,noad_size);
  13075. end
  13076. endcases;
  13077. if op_byte(cur_i)>qi(3) then return;
  13078. math_type(nucleus(q)):=math_char; goto restart;
  13079. end
  13080. @ When we get to the following part of the program, we have ``fallen through''
  13081. from cases that did not lead to |check_dimensions| or |done_with_noad| or
  13082. |done_with_node|. Thus, |q|~points to a noad whose nucleus may need to be
  13083. converted to an hlist, and whose subscripts and superscripts need to be
  13084. appended if they are present.
  13085. If |nucleus(q)| is not a |math_char|, the variable |delta| is the amount
  13086. by which a superscript should be moved right with respect to a subscript
  13087. when both are present.
  13088. @^subscripts@>
  13089. @^superscripts@>
  13090. @<Convert \(n)|nucleus(q)| to an hlist and attach the sub/superscripts@>=
  13091. case math_type(nucleus(q)) of
  13092. math_char, math_text_char:
  13093. @<Create a character node |p| for |nucleus(q)|, possibly followed
  13094. by a kern node for the italic correction, and set |delta| to the
  13095. italic correction if a subscript is present@>;
  13096. empty: p:=null;
  13097. sub_box: p:=info(nucleus(q));
  13098. sub_mlist: begin cur_mlist:=info(nucleus(q)); save_style:=cur_style;
  13099. mlist_penalties:=false; mlist_to_hlist; {recursive call}
  13100. @^recursion@>
  13101. cur_style:=save_style; @<Set up the values...@>;
  13102. p:=hpack(link(temp_head),natural);
  13103. end;
  13104. othercases confusion("mlist2")
  13105. @:this can't happen mlist2}{\quad mlist2@>
  13106. endcases;@/
  13107. new_hlist(q):=p;
  13108. if (math_type(subscr(q))=empty)and(math_type(supscr(q))=empty) then
  13109. goto check_dimensions;
  13110. make_scripts(q,delta)
  13111. @ @<Create a character node |p| for |nucleus(q)|...@>=
  13112. begin fetch(nucleus(q));
  13113. if char_exists(cur_i) then
  13114. begin delta:=char_italic(cur_f)(cur_i); p:=new_character(cur_f,qo(cur_c));
  13115. if (math_type(nucleus(q))=math_text_char)and(space(cur_f)<>0) then
  13116. delta:=0; {no italic correction in mid-word of text font}
  13117. if (math_type(subscr(q))=empty)and(delta<>0) then
  13118. begin link(p):=new_kern(delta); delta:=0;
  13119. end;
  13120. end
  13121. else p:=null;
  13122. end
  13123. @ The purpose of |make_scripts(q,delta)| is to attach the subscript and/or
  13124. superscript of noad |q| to the list that starts at |new_hlist(q)|,
  13125. given that the subscript and superscript aren't both empty. The superscript
  13126. will appear to the right of the subscript by a given distance |delta|.
  13127. We set |shift_down| and |shift_up| to the minimum amounts to shift the
  13128. baseline of subscripts and superscripts based on the given nucleus.
  13129. @<Declare math...@>=
  13130. procedure make_scripts(@!q:pointer;@!delta:scaled);
  13131. var p,@!x,@!y,@!z:pointer; {temporary registers for box construction}
  13132. @!shift_up,@!shift_down,@!clr:scaled; {dimensions in the calculation}
  13133. @!t:small_number; {subsidiary size code}
  13134. begin p:=new_hlist(q);
  13135. if is_char_node(p) then
  13136. begin shift_up:=0; shift_down:=0;
  13137. end
  13138. else begin z:=hpack(p,natural);
  13139. if cur_style<script_style then t:=script_size@+else t:=script_script_size;
  13140. shift_up:=height(z)-sup_drop(t);
  13141. shift_down:=depth(z)+sub_drop(t);
  13142. free_node(z,box_node_size);
  13143. end;
  13144. if math_type(supscr(q))=empty then
  13145. @<Construct a subscript box |x| when there is no superscript@>
  13146. else begin @<Construct a superscript box |x|@>;
  13147. if math_type(subscr(q))=empty then shift_amount(x):=-shift_up
  13148. else @<Construct a sub/superscript combination box |x|, with the
  13149. superscript offset by |delta|@>;
  13150. end;
  13151. if new_hlist(q)=null then new_hlist(q):=x
  13152. else begin p:=new_hlist(q);
  13153. while link(p)<>null do p:=link(p);
  13154. link(p):=x;
  13155. end;
  13156. end;
  13157. @ When there is a subscript without a superscript, the top of the subscript
  13158. should not exceed the baseline plus four-fifths of the x-height.
  13159. @<Construct a subscript box |x| when there is no superscript@>=
  13160. begin x:=clean_box(subscr(q),sub_style(cur_style));
  13161. width(x):=width(x)+script_space;
  13162. if shift_down<sub1(cur_size) then shift_down:=sub1(cur_size);
  13163. clr:=height(x)-(abs(math_x_height(cur_size)*4) div 5);
  13164. if shift_down<clr then shift_down:=clr;
  13165. shift_amount(x):=shift_down;
  13166. end
  13167. @ The bottom of a superscript should never descend below the baseline plus
  13168. one-fourth of the x-height.
  13169. @<Construct a superscript box |x|@>=
  13170. begin x:=clean_box(supscr(q),sup_style(cur_style));
  13171. width(x):=width(x)+script_space;
  13172. if odd(cur_style) then clr:=sup3(cur_size)
  13173. else if cur_style<text_style then clr:=sup1(cur_size)
  13174. else clr:=sup2(cur_size);
  13175. if shift_up<clr then shift_up:=clr;
  13176. clr:=depth(x)+(abs(math_x_height(cur_size)) div 4);
  13177. if shift_up<clr then shift_up:=clr;
  13178. end
  13179. @ When both subscript and superscript are present, the subscript must be
  13180. separated from the superscript by at least four times |default_rule_thickness|.
  13181. If this condition would be violated, the subscript moves down, after which
  13182. both subscript and superscript move up so that the bottom of the superscript
  13183. is at least as high as the baseline plus four-fifths of the x-height.
  13184. @<Construct a sub/superscript combination box |x|...@>=
  13185. begin y:=clean_box(subscr(q),sub_style(cur_style));
  13186. width(y):=width(y)+script_space;
  13187. if shift_down<sub2(cur_size) then shift_down:=sub2(cur_size);
  13188. clr:=4*default_rule_thickness-
  13189. ((shift_up-depth(x))-(height(y)-shift_down));
  13190. if clr>0 then
  13191. begin shift_down:=shift_down+clr;
  13192. clr:=(abs(math_x_height(cur_size)*4) div 5)-(shift_up-depth(x));
  13193. if clr>0 then
  13194. begin shift_up:=shift_up+clr;
  13195. shift_down:=shift_down-clr;
  13196. end;
  13197. end;
  13198. shift_amount(x):=delta; {superscript is |delta| to the right of the subscript}
  13199. p:=new_kern((shift_up-depth(x))-(height(y)-shift_down)); link(x):=p; link(p):=y;
  13200. x:=vpack(x,natural); shift_amount(x):=shift_down;
  13201. end
  13202. @ We have now tied up all the loose ends of the first pass of |mlist_to_hlist|.
  13203. The second pass simply goes through and hooks everything together with the
  13204. proper glue and penalties. It also handles the |left_noad| and |right_noad| that
  13205. might be present, since |max_h| and |max_d| are now known. Variable |p| points
  13206. to a node at the current end of the final hlist.
  13207. @<Make a second pass over the mlist, ...@>=
  13208. p:=temp_head; link(p):=null; q:=mlist; r_type:=0; cur_style:=style;
  13209. @<Set up the values of |cur_size| and |cur_mu|, based on |cur_style|@>;
  13210. while q<>null do
  13211. begin @<If node |q| is a style node, change the style and |goto delete_q|;
  13212. otherwise if it is not a noad, put it into the hlist,
  13213. advance |q|, and |goto done|; otherwise set |s| to the size
  13214. of noad |q|, set |t| to the associated type (|ord_noad..
  13215. inner_noad|), and set |pen| to the associated penalty@>;
  13216. @<Append inter-element spacing based on |r_type| and |t|@>;
  13217. @<Append any |new_hlist| entries for |q|, and any appropriate penalties@>;
  13218. r_type:=t;
  13219. delete_q: r:=q; q:=link(q); free_node(r,s);
  13220. done: end
  13221. @ Just before doing the big |case| switch in the second pass, the program
  13222. sets up default values so that most of the branches are short.
  13223. @<If node |q| is a style node, change the style...@>=
  13224. t:=ord_noad; s:=noad_size; pen:=inf_penalty;
  13225. case type(q) of
  13226. op_noad,open_noad,close_noad,punct_noad,inner_noad: t:=type(q);
  13227. bin_noad: begin t:=bin_noad; pen:=bin_op_penalty;
  13228. end;
  13229. rel_noad: begin t:=rel_noad; pen:=rel_penalty;
  13230. end;
  13231. ord_noad,vcenter_noad,over_noad,under_noad: do_nothing;
  13232. radical_noad: s:=radical_noad_size;
  13233. accent_noad: s:=accent_noad_size;
  13234. fraction_noad: s:=fraction_noad_size;
  13235. left_noad,right_noad: t:=make_left_right(q,style,max_d,max_h);
  13236. style_node: @<Change the current style and |goto delete_q|@>;
  13237. whatsit_node,penalty_node,rule_node,disc_node,adjust_node,ins_node,mark_node,
  13238. glue_node,kern_node:@t@>@;@/
  13239. begin link(p):=q; p:=q; q:=link(q); link(p):=null; goto done;
  13240. end;
  13241. othercases confusion("mlist3")
  13242. @:this can't happen mlist3}{\quad mlist3@>
  13243. endcases
  13244. @ The |make_left_right| function constructs a left or right delimiter of
  13245. the required size and returns the value |open_noad| or |close_noad|. The
  13246. |right_noad| and |left_noad| will both be based on the original |style|,
  13247. so they will have consistent sizes.
  13248. We use the fact that |right_noad-left_noad=close_noad-open_noad|.
  13249. @<Declare math...@>=
  13250. function make_left_right(@!q:pointer;@!style:small_number;
  13251. @!max_d,@!max_h:scaled):small_number;
  13252. var delta,@!delta1,@!delta2:scaled; {dimensions used in the calculation}
  13253. begin if style<script_style then cur_size:=text_size
  13254. else cur_size:=16*((style-text_style) div 2);
  13255. delta2:=max_d+axis_height(cur_size);
  13256. delta1:=max_h+max_d-delta2;
  13257. if delta2>delta1 then delta1:=delta2; {|delta1| is max distance from axis}
  13258. delta:=(delta1 div 500)*delimiter_factor;
  13259. delta2:=delta1+delta1-delimiter_shortfall;
  13260. if delta<delta2 then delta:=delta2;
  13261. new_hlist(q):=var_delimiter(delimiter(q),cur_size,delta);
  13262. make_left_right:=type(q)-(left_noad-open_noad); {|open_noad| or |close_noad|}
  13263. end;
  13264. @ @<Change the current style and |goto delete_q|@>=
  13265. begin cur_style:=subtype(q); s:=style_node_size;
  13266. @<Set up the values of |cur_size| and |cur_mu|, based on |cur_style|@>;
  13267. goto delete_q;
  13268. end
  13269. @ The inter-element spacing in math formulas depends on an $8\times8$ table that
  13270. \TeX\ preloads as a 64-digit string. The elements of this string have the
  13271. following significance:
  13272. $$\vbox{\halign{#\hfil\cr
  13273. \.0 means no space;\cr
  13274. \.1 means a conditional thin space (\.{\\nonscript\\mskip\\thinmuskip});\cr
  13275. \.2 means a thin space (\.{\\mskip\\thinmuskip});\cr
  13276. \.3 means a conditional medium space
  13277. (\.{\\nonscript\\mskip\\medmuskip});\cr
  13278. \.4 means a conditional thick space
  13279. (\.{\\nonscript\\mskip\\thickmuskip});\cr
  13280. \.* means an impossible case.\cr}}$$
  13281. This is all pretty cryptic, but {\sl The \TeX book\/} explains what is
  13282. supposed to happen, and the string makes it happen.
  13283. @:TeXbook}{\sl The \TeX book@>
  13284. A global variable |magic_offset| is computed so that if |a| and |b| are
  13285. in the range |ord_noad..inner_noad|, then |str_pool[a*8+b+magic_offset]|
  13286. is the digit for spacing between noad types |a| and |b|.
  13287. If \PASCAL\ had provided a good way to preload constant arrays, this part of
  13288. the program would not have been so strange.
  13289. @:PASCAL}{\PASCAL@>
  13290. @d math_spacing=@;@/
  13291. @t\hskip-35pt@>
  13292. "0234000122*4000133**3**344*0400400*000000234000111*1111112341011"
  13293. @t$ \hskip-35pt$@>
  13294. @<Glob...@>=
  13295. @!magic_offset:integer; {used to find inter-element spacing}
  13296. @ @<Compute the magic offset@>=
  13297. magic_offset:=str_start[math_spacing]-9*ord_noad
  13298. @ @<Append inter-element spacing based on |r_type| and |t|@>=
  13299. if r_type>0 then {not the first noad}
  13300. begin case so(str_pool[r_type*8+t+magic_offset]) of
  13301. "0": x:=0;
  13302. "1": if cur_style<script_style then x:=thin_mu_skip_code@+else x:=0;
  13303. "2": x:=thin_mu_skip_code;
  13304. "3": if cur_style<script_style then x:=med_mu_skip_code@+else x:=0;
  13305. "4": if cur_style<script_style then x:=thick_mu_skip_code@+else x:=0;
  13306. othercases confusion("mlist4")
  13307. @:this can't happen mlist4}{\quad mlist4@>
  13308. endcases;
  13309. if x<>0 then
  13310. begin y:=math_glue(glue_par(x),cur_mu);
  13311. z:=new_glue(y); glue_ref_count(y):=null; link(p):=z; p:=z;@/
  13312. subtype(z):=x+1; {store a symbolic subtype}
  13313. end;
  13314. end
  13315. @ We insert a penalty node after the hlist entries of noad |q| if |pen|
  13316. is not an ``infinite'' penalty, and if the node immediately following |q|
  13317. is not a penalty node or a |rel_noad| or absent entirely.
  13318. @<Append any |new_hlist| entries for |q|, and any appropriate penalties@>=
  13319. if new_hlist(q)<>null then
  13320. begin link(p):=new_hlist(q);
  13321. repeat p:=link(p);
  13322. until link(p)=null;
  13323. end;
  13324. if penalties then if link(q)<>null then if pen<inf_penalty then
  13325. begin r_type:=type(link(q));
  13326. if r_type<>penalty_node then if r_type<>rel_noad then
  13327. begin z:=new_penalty(pen); link(p):=z; p:=z;
  13328. end;
  13329. end
  13330. @* \[37] Alignment.
  13331. It's sort of a miracle whenever \.{\\halign} and \.{\\valign} work, because
  13332. they cut across so many of the control structures of \TeX.
  13333. Therefore the
  13334. present page is probably not the best place for a beginner to start reading
  13335. this program; it is better to master everything else first.
  13336. Let us focus our thoughts on an example of what the input might be, in order
  13337. to get some idea about how the alignment miracle happens. The example doesn't
  13338. do anything useful, but it is sufficiently general to indicate all of the
  13339. special cases that must be dealt with; please do not be disturbed by its
  13340. apparent complexity and meaninglessness.
  13341. $$\vbox{\halign{\.{#}\hfil\cr
  13342. {}\\tabskip 2pt plus 3pt\cr
  13343. {}\\halign to 300pt\{u1\#v1\&\cr
  13344. \hskip 50pt\\tabskip 1pt plus 1fil u2\#v2\&\cr
  13345. \hskip 50pt u3\#v3\\cr\cr
  13346. \hskip 25pt a1\&\\omit a2\&\\vrule\\cr\cr
  13347. \hskip 25pt \\noalign\{\\vskip 3pt\}\cr
  13348. \hskip 25pt b1\\span b2\\cr\cr
  13349. \hskip 25pt \\omit\&c2\\span\\omit\\cr\}\cr}}$$
  13350. Here's what happens:
  13351. \yskip
  13352. (0) When `\.{\\halign to 300pt\{}' is scanned, the |scan_spec| routine
  13353. places the 300pt dimension onto the |save_stack|, and an |align_group|
  13354. code is placed above it. This will make it possible to complete the alignment
  13355. when the matching `\.\}' is found.
  13356. (1) The preamble is scanned next. Macros in the preamble are not expanded,
  13357. @^preamble@>
  13358. except as part of a tabskip specification. For example, if \.{u2} had been
  13359. a macro in the preamble above, it would have been expanded, since \TeX\
  13360. must look for `\.{minus...}' as part of the tabskip glue. A ``preamble list''
  13361. is constructed based on the user's preamble; in our case it contains the
  13362. following seven items:
  13363. $$\vbox{\halign{\.{#}\hfil\qquad&(#)\hfil\cr
  13364. {}\\glue 2pt plus 3pt&the tabskip preceding column 1\cr
  13365. {}\\alignrecord, width $-\infty$&preamble info for column 1\cr
  13366. {}\\glue 2pt plus 3pt&the tabskip between columns 1 and 2\cr
  13367. {}\\alignrecord, width $-\infty$&preamble info for column 2\cr
  13368. {}\\glue 1pt plus 1fil&the tabskip between columns 2 and 3\cr
  13369. {}\\alignrecord, width $-\infty$&preamble info for column 3\cr
  13370. {}\\glue 1pt plus 1fil&the tabskip following column 3\cr}}$$
  13371. These ``alignrecord'' entries have the same size as an |unset_node|,
  13372. since they will later be converted into such nodes. However, at the
  13373. moment they have no |type| or |subtype| fields; they have |info| fields
  13374. instead, and these |info| fields are initially set to the value |end_span|,
  13375. for reasons explained below. Furthermore, the alignrecord nodes have no
  13376. |height| or |depth| fields; these are renamed |u_part| and |v_part|,
  13377. and they point to token lists for the templates of the alignment.
  13378. For example, the |u_part| field in the first alignrecord points to the
  13379. token list `\.{u1}', i.e., the template preceding the `\.\#' for column~1.
  13380. (2) \TeX\ now looks at what follows the \.{\\cr} that ended the preamble.
  13381. It is not `\.{\\noalign}' or `\.{\\omit}', so this input is put back to
  13382. be read again, and the template `\.{u1}' is fed to the scanner. Just
  13383. before reading `\.{u1}', \TeX\ goes into restricted horizontal mode.
  13384. Just after reading `\.{u1}', \TeX\ will see `\.{a1}', and then (when the
  13385. {\.\&} is sensed) \TeX\ will see `\.{v1}'. Then \TeX\ scans an |endv|
  13386. token, indicating the end of a column. At this point an |unset_node| is
  13387. created, containing the contents of the current hlist (i.e., `\.{u1a1v1}').
  13388. The natural width of this unset node replaces the |width| field of the
  13389. alignrecord for column~1; in general, the alignrecords will record the
  13390. maximum natural width that has occurred so far in a given column.
  13391. (3) Since `\.{\\omit}' follows the `\.\&', the templates for column~2
  13392. are now bypassed. Again \TeX\ goes into restricted horizontal mode and
  13393. makes an |unset_node| from the resulting hlist; but this time the
  13394. hlist contains simply `\.{a2}'. The natural width of the new unset box
  13395. is remembered in the |width| field of the alignrecord for column~2.
  13396. (4) A third |unset_node| is created for column 3, using essentially the
  13397. mechanism that worked for column~1; this unset box contains `\.{u3\\vrule
  13398. v3}'. The vertical rule in this case has running dimensions that will later
  13399. extend to the height and depth of the whole first row, since each |unset_node|
  13400. in a row will eventually inherit the height and depth of its enclosing box.
  13401. (5) The first row has now ended; it is made into a single unset box
  13402. comprising the following seven items:
  13403. $$\vbox{\halign{\hbox to 325pt{\qquad\.{#}\hfil}\cr
  13404. {}\\glue 2pt plus 3pt\cr
  13405. {}\\unsetbox for 1 column: u1a1v1\cr
  13406. {}\\glue 2pt plus 3pt\cr
  13407. {}\\unsetbox for 1 column: a2\cr
  13408. {}\\glue 1pt plus 1fil\cr
  13409. {}\\unsetbox for 1 column: u3\\vrule v3\cr
  13410. {}\\glue 1pt plus 1fil\cr}}$$
  13411. The width of this unset row is unimportant, but it has the correct height
  13412. and depth, so the correct baselineskip glue will be computed as the row
  13413. is inserted into a vertical list.
  13414. (6) Since `\.{\\noalign}' follows the current \.{\\cr}, \TeX\ appends
  13415. additional material (in this case \.{\\vskip 3pt}) to the vertical list.
  13416. While processing this material, \TeX\ will be in internal vertical
  13417. mode, and |no_align_group| will be on |save_stack|.
  13418. (7) The next row produces an unset box that looks like this:
  13419. $$\vbox{\halign{\hbox to 325pt{\qquad\.{#}\hfil}\cr
  13420. {}\\glue 2pt plus 3pt\cr
  13421. {}\\unsetbox for 2 columns: u1b1v1u2b2v2\cr
  13422. {}\\glue 1pt plus 1fil\cr
  13423. {}\\unsetbox for 1 column: {\rm(empty)}\cr
  13424. {}\\glue 1pt plus 1fil\cr}}$$
  13425. The natural width of the unset box that spans columns 1~and~2 is stored
  13426. in a ``span node,'' which we will explain later; the |info| field of the
  13427. alignrecord for column~1 now points to the new span node, and the |info|
  13428. of the span node points to |end_span|.
  13429. (8) The final row produces the unset box
  13430. $$\vbox{\halign{\hbox to 325pt{\qquad\.{#}\hfil}\cr
  13431. {}\\glue 2pt plus 3pt\cr
  13432. {}\\unsetbox for 1 column: {\rm(empty)}\cr
  13433. {}\\glue 2pt plus 3pt\cr
  13434. {}\\unsetbox for 2 columns: u2c2v2\cr
  13435. {}\\glue 1pt plus 1fil\cr}}$$
  13436. A new span node is attached to the alignrecord for column 2.
  13437. (9) The last step is to compute the true column widths and to change all the
  13438. unset boxes to hboxes, appending the whole works to the vertical list that
  13439. encloses the \.{\\halign}. The rules for deciding on the final widths of
  13440. each unset column box will be explained below.
  13441. \yskip\noindent
  13442. Note that as \.{\\halign} is being processed, we fearlessly give up control
  13443. to the rest of \TeX. At critical junctures, an alignment routine is
  13444. called upon to step in and do some little action, but most of the time
  13445. these routines just lurk in the background. It's something like
  13446. post-hypnotic suggestion.
  13447. @ We have mentioned that alignrecords contain no |height| or |depth| fields.
  13448. Their |glue_sign| and |glue_order| are pre-empted as well, since it
  13449. is necessary to store information about what to do when a template ends.
  13450. This information is called the |extra_info| field.
  13451. @d u_part(#)==mem[#+height_offset].int {pointer to \<u_j> token list}
  13452. @d v_part(#)==mem[#+depth_offset].int {pointer to \<v_j> token list}
  13453. @d extra_info(#)==info(#+list_offset) {info to remember during template}
  13454. @ Alignments can occur within alignments, so a small stack is used to access
  13455. the alignrecord information. At each level we have a |preamble| pointer,
  13456. indicating the beginning of the preamble list; a |cur_align| pointer,
  13457. indicating the current position in the preamble list; a |cur_span| pointer,
  13458. indicating the value of |cur_align| at the beginning of a sequence of
  13459. spanned columns; a |cur_loop| pointer, indicating the tabskip glue before
  13460. an alignrecord that should be copied next if the current list is extended;
  13461. and the |align_state| variable, which indicates the nesting of braces so
  13462. that \.{\\cr} and \.{\\span} and tab marks are properly intercepted.
  13463. There also are pointers |cur_head| and |cur_tail| to the head and tail
  13464. of a list of adjustments being moved out from horizontal mode to
  13465. vertical~mode.
  13466. The current values of these seven quantities appear in global variables;
  13467. when they have to be pushed down, they are stored in 5-word nodes, and
  13468. |align_ptr| points to the topmost such node.
  13469. @d preamble==link(align_head) {the current preamble list}
  13470. @d align_stack_node_size=5 {number of |mem| words to save alignment states}
  13471. @<Glob...@>=
  13472. @!cur_align:pointer; {current position in preamble list}
  13473. @!cur_span:pointer; {start of currently spanned columns in preamble list}
  13474. @!cur_loop:pointer; {place to copy when extending a periodic preamble}
  13475. @!align_ptr:pointer; {most recently pushed-down alignment stack node}
  13476. @!cur_head,@!cur_tail:pointer; {adjustment list pointers}
  13477. @ The |align_state| and |preamble| variables are initialized elsewhere.
  13478. @<Set init...@>=
  13479. align_ptr:=null; cur_align:=null; cur_span:=null; cur_loop:=null;
  13480. cur_head:=null; cur_tail:=null;
  13481. @ Alignment stack maintenance is handled by a pair of trivial routines
  13482. called |push_alignment| and |pop_alignment|.
  13483. @p procedure push_alignment;
  13484. var p:pointer; {the new alignment stack node}
  13485. begin p:=get_node(align_stack_node_size);
  13486. link(p):=align_ptr; info(p):=cur_align;
  13487. llink(p):=preamble; rlink(p):=cur_span;
  13488. mem[p+2].int:=cur_loop; mem[p+3].int:=align_state;
  13489. info(p+4):=cur_head; link(p+4):=cur_tail;
  13490. align_ptr:=p;
  13491. cur_head:=get_avail;
  13492. end;
  13493. @#
  13494. procedure pop_alignment;
  13495. var p:pointer; {the top alignment stack node}
  13496. begin free_avail(cur_head);
  13497. p:=align_ptr;
  13498. cur_tail:=link(p+4); cur_head:=info(p+4);
  13499. align_state:=mem[p+3].int; cur_loop:=mem[p+2].int;
  13500. cur_span:=rlink(p); preamble:=llink(p);
  13501. cur_align:=info(p); align_ptr:=link(p);
  13502. free_node(p,align_stack_node_size);
  13503. end;
  13504. @ \TeX\ has eight procedures that govern alignments: |init_align| and
  13505. |fin_align| are used at the very beginning and the very end; |init_row| and
  13506. |fin_row| are used at the beginning and end of individual rows; |init_span|
  13507. is used at the beginning of a sequence of spanned columns (possibly involving
  13508. only one column); |init_col| and |fin_col| are used at the beginning and
  13509. end of individual columns; and |align_peek| is used after \.{\\cr} to see
  13510. whether the next item is \.{\\noalign}.
  13511. We shall consider these routines in the order they are first used during
  13512. the course of a complete \.{\\halign}, namely |init_align|, |align_peek|,
  13513. |init_row|, |init_span|, |init_col|, |fin_col|, |fin_row|, |fin_align|.
  13514. @ When \.{\\halign} or \.{\\valign} has been scanned in an appropriate
  13515. mode, \TeX\ calls |init_align|, whose task is to get everything off to a
  13516. good start. This mostly involves scanning the preamble and putting its
  13517. information into the preamble list.
  13518. @^preamble@>
  13519. @p @t\4@>@<Declare the procedure called |get_preamble_token|@>@t@>@/
  13520. procedure@?align_peek; forward;@t\2@>@/
  13521. procedure@?normal_paragraph; forward;@t\2@>@/
  13522. procedure init_align;
  13523. label done, done1, done2, continue;
  13524. var save_cs_ptr:pointer; {|warning_index| value for error messages}
  13525. @!p:pointer; {for short-term temporary use}
  13526. begin save_cs_ptr:=cur_cs; {\.{\\halign} or \.{\\valign}, usually}
  13527. push_alignment; align_state:=-1000000; {enter a new alignment level}
  13528. @<Check for improper alignment in displayed math@>;
  13529. push_nest; {enter a new semantic level}
  13530. @<Change current mode to |-vmode| for \.{\\halign}, |-hmode| for \.{\\valign}@>;
  13531. scan_spec(align_group,false);@/
  13532. @<Scan the preamble and record it in the |preamble| list@>;
  13533. new_save_level(align_group);
  13534. if every_cr<>null then begin_token_list(every_cr,every_cr_text);
  13535. align_peek; {look for \.{\\noalign} or \.{\\omit}}
  13536. end;
  13537. @ In vertical modes, |prev_depth| already has the correct value. But
  13538. if we are in |mmode| (displayed formula mode), we reach out to the
  13539. enclosing vertical mode for the |prev_depth| value that produces the
  13540. correct baseline calculations.
  13541. @<Change current mode...@>=
  13542. if mode=mmode then
  13543. begin mode:=-vmode; prev_depth:=nest[nest_ptr-2].aux_field.sc;
  13544. end
  13545. else if mode>0 then negate(mode)
  13546. @ When \.{\\halign} is used as a displayed formula, there should be
  13547. no other pieces of mlists present.
  13548. @<Check for improper alignment in displayed math@>=
  13549. if (mode=mmode)and((tail<>head)or(incompleat_noad<>null)) then
  13550. begin print_err("Improper "); print_esc("halign"); print(" inside $$'s");
  13551. @.Improper \\halign...@>
  13552. help3("Displays can use special alignments (like \eqalignno)")@/
  13553. ("only if nothing but the alignment itself is between $$'s.")@/
  13554. ("So I've deleted the formulas that preceded this alignment.");
  13555. error; flush_math;
  13556. end
  13557. @ @<Scan the preamble and record it in the |preamble| list@>=
  13558. preamble:=null; cur_align:=align_head; cur_loop:=null; scanner_status:=aligning;
  13559. warning_index:=save_cs_ptr; align_state:=-1000000;
  13560. {at this point, |cur_cmd=left_brace|}
  13561. loop@+ begin @<Append the current tabskip glue to the preamble list@>;
  13562. if cur_cmd=car_ret then goto done; {\.{\\cr} ends the preamble}
  13563. @<Scan preamble text until |cur_cmd| is |tab_mark| or |car_ret|,
  13564. looking for changes in the tabskip glue; append an
  13565. alignrecord to the preamble list@>;
  13566. end;
  13567. done: scanner_status:=normal
  13568. @ @<Append the current tabskip glue to the preamble list@>=
  13569. link(cur_align):=new_param_glue(tab_skip_code);
  13570. cur_align:=link(cur_align)
  13571. @ @<Scan preamble text until |cur_cmd| is |tab_mark| or |car_ret|...@>=
  13572. @<Scan the template \<u_j>, putting the resulting token list in |hold_head|@>;
  13573. link(cur_align):=new_null_box; cur_align:=link(cur_align); {a new alignrecord}
  13574. info(cur_align):=end_span; width(cur_align):=null_flag;
  13575. u_part(cur_align):=link(hold_head);
  13576. @<Scan the template \<v_j>, putting the resulting token list in |hold_head|@>;
  13577. v_part(cur_align):=link(hold_head)
  13578. @ We enter `\.{\\span}' into |eqtb| with |tab_mark| as its command code,
  13579. and with |span_code| as the command modifier. This makes \TeX\ interpret it
  13580. essentially the same as an alignment delimiter like `\.\&', yet it is
  13581. recognizably different when we need to distinguish it from a normal delimiter.
  13582. It also turns out to be useful to give a special |cr_code| to `\.{\\cr}',
  13583. and an even larger |cr_cr_code| to `\.{\\crcr}'.
  13584. The end of a template is represented by two ``frozen'' control sequences
  13585. called \.{\\endtemplate}. The first has the command code |end_template|, which
  13586. is |>outer_call|, so it will not easily disappear in the presence of errors.
  13587. The |get_x_token| routine converts the first into the second, which has |endv|
  13588. as its command code.
  13589. @d span_code=256 {distinct from any character}
  13590. @d cr_code=257 {distinct from |span_code| and from any character}
  13591. @d cr_cr_code=cr_code+1 {this distinguishes \.{\\crcr} from \.{\\cr}}
  13592. @d end_template_token==cs_token_flag+frozen_end_template
  13593. @<Put each of \TeX's primitives into the hash table@>=
  13594. primitive("span",tab_mark,span_code);@/
  13595. @!@:span_}{\.{\\span} primitive@>
  13596. primitive("cr",car_ret,cr_code);
  13597. @!@:cr_}{\.{\\cr} primitive@>
  13598. text(frozen_cr):="cr"; eqtb[frozen_cr]:=eqtb[cur_val];@/
  13599. primitive("crcr",car_ret,cr_cr_code);
  13600. @!@:cr_cr_}{\.{\\crcr} primitive@>
  13601. text(frozen_end_template):="endtemplate"; text(frozen_endv):="endtemplate";
  13602. @.endtemplate@>
  13603. eq_type(frozen_endv):=endv; equiv(frozen_endv):=null_list;
  13604. eq_level(frozen_endv):=level_one;@/
  13605. eqtb[frozen_end_template]:=eqtb[frozen_endv];
  13606. eq_type(frozen_end_template):=end_template;
  13607. @ @<Cases of |print_cmd_chr|...@>=
  13608. tab_mark: if chr_code=span_code then print_esc("span")
  13609. else chr_cmd("alignment tab character ");
  13610. car_ret: if chr_code=cr_code then print_esc("cr")
  13611. else print_esc("crcr");
  13612. @ The preamble is copied directly, except that \.{\\tabskip} causes a change
  13613. to the tabskip glue, thereby possibly expanding macros that immediately
  13614. follow it. An appearance of \.{\\span} also causes such an expansion.
  13615. Note that if the preamble contains `\.{\\global\\tabskip}', the `\.{\\global}'
  13616. token survives in the preamble and the `\.{\\tabskip}' defines new
  13617. tabskip glue (locally).
  13618. @<Declare the procedure called |get_preamble_token|@>=
  13619. procedure get_preamble_token;
  13620. label restart;
  13621. begin restart: get_token;
  13622. while (cur_chr=span_code)and(cur_cmd=tab_mark) do
  13623. begin get_token; {this token will be expanded once}
  13624. if cur_cmd>max_command then
  13625. begin expand; get_token;
  13626. end;
  13627. end;
  13628. if cur_cmd=endv then
  13629. fatal_error("(interwoven alignment preambles are not allowed)");
  13630. @.interwoven alignment preambles...@>
  13631. if (cur_cmd=assign_glue)and(cur_chr=glue_base+tab_skip_code) then
  13632. begin scan_optional_equals; scan_glue(glue_val);
  13633. if global_defs>0 then geq_define(glue_base+tab_skip_code,glue_ref,cur_val)
  13634. else eq_define(glue_base+tab_skip_code,glue_ref,cur_val);
  13635. goto restart;
  13636. end;
  13637. end;
  13638. @ Spaces are eliminated from the beginning of a template.
  13639. @<Scan the template \<u_j>...@>=
  13640. p:=hold_head; link(p):=null;
  13641. loop@+ begin get_preamble_token;
  13642. if cur_cmd=mac_param then goto done1;
  13643. if (cur_cmd<=car_ret)and(cur_cmd>=tab_mark)and(align_state=-1000000) then
  13644. if (p=hold_head)and(cur_loop=null)and(cur_cmd=tab_mark)
  13645. then cur_loop:=cur_align
  13646. else begin print_err("Missing # inserted in alignment preamble");
  13647. @.Missing \# inserted...@>
  13648. help3("There should be exactly one # between &'s, when an")@/
  13649. ("\halign or \valign is being set up. In this case you had")@/
  13650. ("none, so I've put one in; maybe that will work.");
  13651. back_error; goto done1;
  13652. end
  13653. else if (cur_cmd<>spacer)or(p<>hold_head) then
  13654. begin link(p):=get_avail; p:=link(p); info(p):=cur_tok;
  13655. end;
  13656. end;
  13657. done1:
  13658. @ @<Scan the template \<v_j>...@>=
  13659. p:=hold_head; link(p):=null;
  13660. loop@+ begin continue: get_preamble_token;
  13661. if (cur_cmd<=car_ret)and(cur_cmd>=tab_mark)and(align_state=-1000000) then
  13662. goto done2;
  13663. if cur_cmd=mac_param then
  13664. begin print_err("Only one # is allowed per tab");
  13665. @.Only one \# is allowed...@>
  13666. help3("There should be exactly one # between &'s, when an")@/
  13667. ("\halign or \valign is being set up. In this case you had")@/
  13668. ("more than one, so I'm ignoring all but the first.");
  13669. error; goto continue;
  13670. end;
  13671. link(p):=get_avail; p:=link(p); info(p):=cur_tok;
  13672. end;
  13673. done2: link(p):=get_avail; p:=link(p);
  13674. info(p):=end_template_token {put \.{\\endtemplate} at the end}
  13675. @ The tricky part about alignments is getting the templates into the
  13676. scanner at the right time, and recovering control when a row or column
  13677. is finished.
  13678. We usually begin a row after each \.{\\cr} has been sensed, unless that
  13679. \.{\\cr} is followed by \.{\\noalign} or by the right brace that terminates
  13680. the alignment. The |align_peek| routine is used to look ahead and do
  13681. the right thing; it either gets a new row started, or gets a \.{\\noalign}
  13682. started, or finishes off the alignment.
  13683. @<Declare the procedure called |align_peek|@>=
  13684. procedure align_peek;
  13685. label restart;
  13686. begin restart: align_state:=1000000; @<Get the next non-blank non-call token@>;
  13687. if cur_cmd=no_align then
  13688. begin scan_left_brace; new_save_level(no_align_group);
  13689. if mode=-vmode then normal_paragraph;
  13690. end
  13691. else if cur_cmd=right_brace then fin_align
  13692. else if (cur_cmd=car_ret)and(cur_chr=cr_cr_code) then
  13693. goto restart {ignore \.{\\crcr}}
  13694. else begin init_row; {start a new row}
  13695. init_col; {start a new column and replace what we peeked at}
  13696. end;
  13697. end;
  13698. @ To start a row (i.e., a `row' that rhymes with `dough' but not with `bough'),
  13699. we enter a new semantic level, copy the first tabskip glue, and change
  13700. from internal vertical mode to restricted horizontal mode or vice versa.
  13701. The |space_factor| and |prev_depth| are not used on this semantic level,
  13702. but we clear them to zero just to be tidy.
  13703. @p @t\4@>@<Declare the procedure called |init_span|@>@t@>@/
  13704. procedure init_row;
  13705. begin push_nest; mode:=(-hmode-vmode)-mode;
  13706. if mode=-hmode then space_factor:=0 @+else prev_depth:=0;
  13707. tail_append(new_glue(glue_ptr(preamble)));
  13708. subtype(tail):=tab_skip_code+1;@/
  13709. cur_align:=link(preamble); cur_tail:=cur_head; init_span(cur_align);
  13710. end;
  13711. @ The parameter to |init_span| is a pointer to the alignrecord where the
  13712. next column or group of columns will begin. A new semantic level is
  13713. entered, so that the columns will generate a list for subsequent packaging.
  13714. @<Declare the procedure called |init_span|@>=
  13715. procedure init_span(@!p:pointer);
  13716. begin push_nest;
  13717. if mode=-hmode then space_factor:=1000
  13718. else begin prev_depth:=ignore_depth; normal_paragraph;
  13719. end;
  13720. cur_span:=p;
  13721. end;
  13722. @ When a column begins, we assume that |cur_cmd| is either |omit| or else
  13723. the current token should be put back into the input until the \<u_j>
  13724. template has been scanned. (Note that |cur_cmd| might be |tab_mark| or
  13725. |car_ret|.) We also assume that |align_state| is approximately 1000000 at
  13726. this time. We remain in the same mode, and start the template if it is
  13727. called for.
  13728. @p procedure init_col;
  13729. begin extra_info(cur_align):=cur_cmd;
  13730. if cur_cmd=omit then align_state:=0
  13731. else begin back_input; begin_token_list(u_part(cur_align),u_template);
  13732. end; {now |align_state=1000000|}
  13733. end;
  13734. @ The scanner sets |align_state| to zero when the \<u_j> template ends. When
  13735. a subsequent \.{\\cr} or \.{\\span} or tab mark occurs with |align_state=0|,
  13736. the scanner activates the following code, which fires up the \<v_j> template.
  13737. We need to remember the |cur_chr|, which is either |cr_cr_code|, |cr_code|,
  13738. |span_code|, or a character code, depending on how the column text has ended.
  13739. This part of the program had better not be activated when the preamble
  13740. to another alignment is being scanned, or when no alignment preamble is active.
  13741. @<Insert the \(v)\<v_j>...@>=
  13742. begin if (scanner_status=aligning) or (cur_align=null) then
  13743. fatal_error("(interwoven alignment preambles are not allowed)");
  13744. @.interwoven alignment preambles...@>
  13745. cur_cmd:=extra_info(cur_align); extra_info(cur_align):=cur_chr;
  13746. if cur_cmd=omit then begin_token_list(omit_template,v_template)
  13747. else begin_token_list(v_part(cur_align),v_template);
  13748. align_state:=1000000; goto restart;
  13749. end
  13750. @ The token list |omit_template| just referred to is a constant token
  13751. list that contains the special control sequence \.{\\endtemplate} only.
  13752. @<Initialize the special...@>=
  13753. info(omit_template):=end_template_token; {|link(omit_template)=null|}
  13754. @ When the |endv| command at the end of a \<v_j> template comes through the
  13755. scanner, things really start to happen; and it is the |fin_col| routine
  13756. that makes them happen. This routine returns |true| if a row as well as a
  13757. column has been finished.
  13758. @p function fin_col:boolean;
  13759. label exit;
  13760. var p:pointer; {the alignrecord after the current one}
  13761. @!q,@!r:pointer; {temporary pointers for list manipulation}
  13762. @!s:pointer; {a new span node}
  13763. @!u:pointer; {a new unset box}
  13764. @!w:scaled; {natural width}
  13765. @!o:glue_ord; {order of infinity}
  13766. @!n:halfword; {span counter}
  13767. begin if cur_align=null then confusion("endv");
  13768. q:=link(cur_align);@+if q=null then confusion("endv");
  13769. @:this can't happen endv}{\quad endv@>
  13770. if align_state<500000 then
  13771. fatal_error("(interwoven alignment preambles are not allowed)");
  13772. @.interwoven alignment preambles...@>
  13773. p:=link(q);
  13774. @<If the preamble list has been traversed, check that the row has ended@>;
  13775. if extra_info(cur_align)<>span_code then
  13776. begin unsave; new_save_level(align_group);@/
  13777. @<Package an unset box for the current column and record its width@>;
  13778. @<Copy the tabskip glue between columns@>;
  13779. if extra_info(cur_align)>=cr_code then
  13780. begin fin_col:=true; return;
  13781. end;
  13782. init_span(p);
  13783. end;
  13784. align_state:=1000000; @<Get the next non-blank non-call token@>;
  13785. cur_align:=p;
  13786. init_col; fin_col:=false;
  13787. exit: end;
  13788. @ @<If the preamble list has been traversed, check that the row has ended@>=
  13789. if (p=null)and(extra_info(cur_align)<cr_code) then
  13790. if cur_loop<>null then @<Lengthen the preamble periodically@>
  13791. else begin print_err("Extra alignment tab has been changed to ");
  13792. @.Extra alignment tab...@>
  13793. print_esc("cr");
  13794. help3("You have given more \span or & marks than there were")@/
  13795. ("in the preamble to the \halign or \valign now in progress.")@/
  13796. ("So I'll assume that you meant to type \cr instead.");
  13797. extra_info(cur_align):=cr_code; error;
  13798. end
  13799. @ @<Lengthen the preamble...@>=
  13800. begin link(q):=new_null_box; p:=link(q); {a new alignrecord}
  13801. info(p):=end_span; width(p):=null_flag; cur_loop:=link(cur_loop);
  13802. @<Copy the templates from node |cur_loop| into node |p|@>;
  13803. cur_loop:=link(cur_loop);
  13804. link(p):=new_glue(glue_ptr(cur_loop));
  13805. subtype(link(p)):=tab_skip_code+1;
  13806. end
  13807. @ @<Copy the templates from node |cur_loop| into node |p|@>=
  13808. q:=hold_head; r:=u_part(cur_loop);
  13809. while r<>null do
  13810. begin link(q):=get_avail; q:=link(q); info(q):=info(r); r:=link(r);
  13811. end;
  13812. link(q):=null; u_part(p):=link(hold_head);
  13813. q:=hold_head; r:=v_part(cur_loop);
  13814. while r<>null do
  13815. begin link(q):=get_avail; q:=link(q); info(q):=info(r); r:=link(r);
  13816. end;
  13817. link(q):=null; v_part(p):=link(hold_head)
  13818. @ @<Copy the tabskip glue...@>=
  13819. tail_append(new_glue(glue_ptr(link(cur_align))));
  13820. subtype(tail):=tab_skip_code+1
  13821. @ @<Package an unset...@>=
  13822. begin if mode=-hmode then
  13823. begin adjust_tail:=cur_tail; u:=hpack(link(head),natural); w:=width(u);
  13824. cur_tail:=adjust_tail; adjust_tail:=null;
  13825. end
  13826. else begin u:=vpackage(link(head),natural,0); w:=height(u);
  13827. end;
  13828. n:=min_quarterword; {this represents a span count of 1}
  13829. if cur_span<>cur_align then @<Update width entry for spanned columns@>
  13830. else if w>width(cur_align) then width(cur_align):=w;
  13831. type(u):=unset_node; span_count(u):=n;@/
  13832. @<Determine the stretch order@>;
  13833. glue_order(u):=o; glue_stretch(u):=total_stretch[o];@/
  13834. @<Determine the shrink order@>;
  13835. glue_sign(u):=o; glue_shrink(u):=total_shrink[o];@/
  13836. pop_nest; link(tail):=u; tail:=u;
  13837. end
  13838. @ A span node is a 2-word record containing |width|, |info|, and |link|
  13839. fields. The |link| field is not really a link, it indicates the number of
  13840. spanned columns; the |info| field points to a span node for the same
  13841. starting column, having a greater extent of spanning, or to |end_span|,
  13842. which has the largest possible |link| field; the |width| field holds the
  13843. largest natural width corresponding to a particular set of spanned columns.
  13844. A list of the maximum widths so far, for spanned columns starting at a
  13845. given column, begins with the |info| field of the alignrecord for that
  13846. column.
  13847. @d span_node_size=2 {number of |mem| words for a span node}
  13848. @<Initialize the special list heads...@>=
  13849. link(end_span):=max_quarterword+1; info(end_span):=null;
  13850. @ @<Update width entry for spanned columns@>=
  13851. begin q:=cur_span;
  13852. repeat incr(n); q:=link(link(q));
  13853. until q=cur_align;
  13854. if n>max_quarterword then confusion("256 spans"); {this can happen, but won't}
  13855. @^system dependencies@>
  13856. @:this can't happen 256 spans}{\quad 256 spans@>
  13857. q:=cur_span; while link(info(q))<n do q:=info(q);
  13858. if link(info(q))>n then
  13859. begin s:=get_node(span_node_size); info(s):=info(q); link(s):=n;
  13860. info(q):=s; width(s):=w;
  13861. end
  13862. else if width(info(q))<w then width(info(q)):=w;
  13863. end
  13864. @ At the end of a row, we append an unset box to the current vlist (for
  13865. \.{\\halign}) or the current hlist (for \.{\\valign}). This unset box
  13866. contains the unset boxes for the columns, separated by the tabskip glue.
  13867. Everything will be set later.
  13868. @p procedure fin_row;
  13869. var p:pointer; {the new unset box}
  13870. begin if mode=-hmode then
  13871. begin p:=hpack(link(head),natural);
  13872. pop_nest; append_to_vlist(p);
  13873. if cur_head<>cur_tail then
  13874. begin link(tail):=link(cur_head); tail:=cur_tail;
  13875. end;
  13876. end
  13877. else begin p:=vpack(link(head),natural); pop_nest;
  13878. link(tail):=p; tail:=p; space_factor:=1000;
  13879. end;
  13880. type(p):=unset_node; glue_stretch(p):=0;
  13881. if every_cr<>null then begin_token_list(every_cr,every_cr_text);
  13882. align_peek;
  13883. end; {note that |glue_shrink(p)=0| since |glue_shrink==shift_amount|}
  13884. @ Finally, we will reach the end of the alignment, and we can breathe a
  13885. sigh of relief that memory hasn't overflowed. All the unset boxes will now be
  13886. set so that the columns line up, taking due account of spanned columns.
  13887. @p procedure@?do_assignments; forward;@t\2@>@/
  13888. procedure@?resume_after_display; forward;@t\2@>@/
  13889. procedure@?build_page; forward;@t\2@>@/
  13890. procedure fin_align;
  13891. var @!p,@!q,@!r,@!s,@!u,@!v: pointer; {registers for the list operations}
  13892. @!t,@!w:scaled; {width of column}
  13893. @!o:scaled; {shift offset for unset boxes}
  13894. @!n:halfword; {matching span amount}
  13895. @!rule_save:scaled; {temporary storage for |overfull_rule|}
  13896. @!aux_save:memory_word; {temporary storage for |aux|}
  13897. begin if cur_group<>align_group then confusion("align1");
  13898. @:this can't happen align}{\quad align@>
  13899. unsave; {that |align_group| was for individual entries}
  13900. if cur_group<>align_group then confusion("align0");
  13901. unsave; {that |align_group| was for the whole alignment}
  13902. if nest[nest_ptr-1].mode_field=mmode then o:=display_indent
  13903. else o:=0;
  13904. @<Go through the preamble list, determining the column widths and
  13905. changing the alignrecords to dummy unset boxes@>;
  13906. @<Package the preamble list, to determine the actual tabskip glue amounts,
  13907. and let |p| point to this prototype box@>;
  13908. @<Set the glue in all the unset boxes of the current list@>;
  13909. flush_node_list(p); pop_alignment;
  13910. @<Insert the \(c)current list into its environment@>;
  13911. end;@/
  13912. @t\4@>@<Declare the procedure called |align_peek|@>
  13913. @ It's time now to dismantle the preamble list and to compute the column
  13914. widths. Let $w_{ij}$ be the maximum of the natural widths of all entries
  13915. that span columns $i$ through $j$, inclusive. The alignrecord for column~$i$
  13916. contains $w_{ii}$ in its |width| field, and there is also a linked list of
  13917. the nonzero $w_{ij}$ for increasing $j$, accessible via the |info| field;
  13918. these span nodes contain the value $j-i+|min_quarterword|$ in their
  13919. |link| fields. The values of $w_{ii}$ were initialized to |null_flag|, which
  13920. we regard as $-\infty$.
  13921. The final column widths are defined by the formula
  13922. $$w_j=\max_{1\L i\L j}\biggl( w_{ij}-\sum_{i\L k<j}(t_k+w_k)\biggr),$$
  13923. where $t_k$ is the natural width of the tabskip glue between columns
  13924. $k$ and~$k+1$. However, if $w_{ij}=-\infty$ for all |i| in the range
  13925. |1<=i<=j| (i.e., if every entry that involved column~|j| also involved
  13926. column~|j+1|), we let $w_j=0$, and we zero out the tabskip glue after
  13927. column~|j|.
  13928. \TeX\ computes these values by using the following scheme: First $w_1=w_{11}$.
  13929. Then replace $w_{2j}$ by $\max(w_{2j},w_{1j}-t_1-w_1)$, for all $j>1$.
  13930. Then $w_2=w_{22}$. Then replace $w_{3j}$ by $\max(w_{3j},w_{2j}-t_2-w_2)$
  13931. for all $j>2$; and so on. If any $w_j$ turns out to be $-\infty$, its
  13932. value is changed to zero and so is the next tabskip.
  13933. @<Go through the preamble list,...@>=
  13934. q:=link(preamble);
  13935. repeat flush_list(u_part(q)); flush_list(v_part(q));
  13936. p:=link(link(q));
  13937. if width(q)=null_flag then
  13938. @<Nullify |width(q)| and the tabskip glue following this column@>;
  13939. if info(q)<>end_span then
  13940. @<Merge the widths in the span nodes of |q| with those of |p|,
  13941. destroying the span nodes of |q|@>;
  13942. type(q):=unset_node; span_count(q):=min_quarterword; height(q):=0;
  13943. depth(q):=0; glue_order(q):=normal; glue_sign(q):=normal;
  13944. glue_stretch(q):=0; glue_shrink(q):=0; q:=p;
  13945. until q=null
  13946. @ @<Nullify |width(q)| and the tabskip glue following this column@>=
  13947. begin width(q):=0; r:=link(q); s:=glue_ptr(r);
  13948. if s<>zero_glue then
  13949. begin add_glue_ref(zero_glue); delete_glue_ref(s);
  13950. glue_ptr(r):=zero_glue;
  13951. end;
  13952. end
  13953. @ Merging of two span-node lists is a typical exercise in the manipulation of
  13954. linearly linked data structures. The essential invariant in the following
  13955. |repeat| loop is that we want to dispense with node |r|, in |q|'s list,
  13956. and |u| is its successor; all nodes of |p|'s list up to and including |s|
  13957. have been processed, and the successor of |s| matches |r| or precedes |r|
  13958. or follows |r|, according as |link(r)=n| or |link(r)>n| or |link(r)<n|.
  13959. @<Merge the widths...@>=
  13960. begin t:=width(q)+width(glue_ptr(link(q)));
  13961. r:=info(q); s:=end_span; info(s):=p; n:=min_quarterword+1;
  13962. repeat width(r):=width(r)-t; u:=info(r);
  13963. while link(r)>n do
  13964. begin s:=info(s); n:=link(info(s))+1;
  13965. end;
  13966. if link(r)<n then
  13967. begin info(r):=info(s); info(s):=r; decr(link(r)); s:=r;
  13968. end
  13969. else begin if width(r)>width(info(s)) then width(info(s)):=width(r);
  13970. free_node(r,span_node_size);
  13971. end;
  13972. r:=u;
  13973. until r=end_span;
  13974. end
  13975. @ Now the preamble list has been converted to a list of alternating unset
  13976. boxes and tabskip glue, where the box widths are equal to the final
  13977. column sizes. In case of \.{\\valign}, we change the widths to heights,
  13978. so that a correct error message will be produced if the alignment is
  13979. overfull or underfull.
  13980. @<Package the preamble list...@>=
  13981. save_ptr:=save_ptr-2; pack_begin_line:=-mode_line;
  13982. if mode=-vmode then
  13983. begin rule_save:=overfull_rule;
  13984. overfull_rule:=0; {prevent rule from being packaged}
  13985. p:=hpack(preamble,saved(1),saved(0)); overfull_rule:=rule_save;
  13986. end
  13987. else begin q:=link(preamble);
  13988. repeat height(q):=width(q); width(q):=0; q:=link(link(q));
  13989. until q=null;
  13990. p:=vpack(preamble,saved(1),saved(0));
  13991. q:=link(preamble);
  13992. repeat width(q):=height(q); height(q):=0; q:=link(link(q));
  13993. until q=null;
  13994. end;
  13995. pack_begin_line:=0
  13996. @ @<Set the glue in all the unset...@>=
  13997. q:=link(head); s:=head;
  13998. while q<>null do
  13999. begin if not is_char_node(q) then
  14000. if type(q)=unset_node then
  14001. @<Set the unset box |q| and the unset boxes in it@>
  14002. else if type(q)=rule_node then
  14003. @<Make the running dimensions in rule |q| extend to the
  14004. boundaries of the alignment@>;
  14005. s:=q; q:=link(q);
  14006. end
  14007. @ @<Make the running dimensions in rule |q| extend...@>=
  14008. begin if is_running(width(q)) then width(q):=width(p);
  14009. if is_running(height(q)) then height(q):=height(p);
  14010. if is_running(depth(q)) then depth(q):=depth(p);
  14011. if o<>0 then
  14012. begin r:=link(q); link(q):=null; q:=hpack(q,natural);
  14013. shift_amount(q):=o; link(q):=r; link(s):=q;
  14014. end;
  14015. end
  14016. @ The unset box |q| represents a row that contains one or more unset boxes,
  14017. depending on how soon \.{\\cr} occurred in that row.
  14018. @<Set the unset box |q| and the unset boxes in it@>=
  14019. begin if mode=-vmode then
  14020. begin type(q):=hlist_node; width(q):=width(p);
  14021. end
  14022. else begin type(q):=vlist_node; height(q):=height(p);
  14023. end;
  14024. glue_order(q):=glue_order(p); glue_sign(q):=glue_sign(p);
  14025. glue_set(q):=glue_set(p); shift_amount(q):=o;
  14026. r:=link(list_ptr(q)); s:=link(list_ptr(p));
  14027. repeat @<Set the glue in node |r| and change it from an unset node@>;
  14028. r:=link(link(r)); s:=link(link(s));
  14029. until r=null;
  14030. end
  14031. @ A box made from spanned columns will be followed by tabskip glue nodes and
  14032. by empty boxes as if there were no spanning. This permits perfect alignment
  14033. of subsequent entries, and it prevents values that depend on floating point
  14034. arithmetic from entering into the dimensions of any boxes.
  14035. @<Set the glue in node |r|...@>=
  14036. n:=span_count(r); t:=width(s); w:=t; u:=hold_head;
  14037. while n>min_quarterword do
  14038. begin decr(n);
  14039. @<Append tabskip glue and an empty box to list |u|,
  14040. and update |s| and |t| as the prototype nodes are passed@>;
  14041. end;
  14042. if mode=-vmode then
  14043. @<Make the unset node |r| into an |hlist_node| of width |w|,
  14044. setting the glue as if the width were |t|@>
  14045. else @<Make the unset node |r| into a |vlist_node| of height |w|,
  14046. setting the glue as if the height were |t|@>;
  14047. shift_amount(r):=0;
  14048. if u<>hold_head then {append blank boxes to account for spanned nodes}
  14049. begin link(u):=link(r); link(r):=link(hold_head); r:=u;
  14050. end
  14051. @ @<Append tabskip glue and an empty box to list |u|...@>=
  14052. s:=link(s); v:=glue_ptr(s); link(u):=new_glue(v); u:=link(u);
  14053. subtype(u):=tab_skip_code+1; t:=t+width(v);
  14054. if glue_sign(p)=stretching then
  14055. begin if stretch_order(v)=glue_order(p) then
  14056. t:=t+round(float(glue_set(p))*stretch(v));
  14057. @^real multiplication@>
  14058. end
  14059. else if glue_sign(p)=shrinking then
  14060. begin if shrink_order(v)=glue_order(p) then
  14061. t:=t-round(float(glue_set(p))*shrink(v));
  14062. end;
  14063. s:=link(s); link(u):=new_null_box; u:=link(u); t:=t+width(s);
  14064. if mode=-vmode then width(u):=width(s)@+else
  14065. begin type(u):=vlist_node; height(u):=width(s);
  14066. end
  14067. @ @<Make the unset node |r| into an |hlist_node| of width |w|...@>=
  14068. begin height(r):=height(q); depth(r):=depth(q);
  14069. if t=width(r) then
  14070. begin glue_sign(r):=normal; glue_order(r):=normal;
  14071. set_glue_ratio_zero(glue_set(r));
  14072. end
  14073. else if t>width(r) then
  14074. begin glue_sign(r):=stretching;
  14075. if glue_stretch(r)=0 then set_glue_ratio_zero(glue_set(r))
  14076. else glue_set(r):=unfloat((t-width(r))/glue_stretch(r));
  14077. @^real division@>
  14078. end
  14079. else begin glue_order(r):=glue_sign(r); glue_sign(r):=shrinking;
  14080. if glue_shrink(r)=0 then set_glue_ratio_zero(glue_set(r))
  14081. else if (glue_order(r)=normal)and(width(r)-t>glue_shrink(r)) then
  14082. set_glue_ratio_one(glue_set(r))
  14083. else glue_set(r):=unfloat((width(r)-t)/glue_shrink(r));
  14084. end;
  14085. width(r):=w; type(r):=hlist_node;
  14086. end
  14087. @ @<Make the unset node |r| into a |vlist_node| of height |w|...@>=
  14088. begin width(r):=width(q);
  14089. if t=height(r) then
  14090. begin glue_sign(r):=normal; glue_order(r):=normal;
  14091. set_glue_ratio_zero(glue_set(r));
  14092. end
  14093. else if t>height(r) then
  14094. begin glue_sign(r):=stretching;
  14095. if glue_stretch(r)=0 then set_glue_ratio_zero(glue_set(r))
  14096. else glue_set(r):=unfloat((t-height(r))/glue_stretch(r));
  14097. @^real division@>
  14098. end
  14099. else begin glue_order(r):=glue_sign(r); glue_sign(r):=shrinking;
  14100. if glue_shrink(r)=0 then set_glue_ratio_zero(glue_set(r))
  14101. else if (glue_order(r)=normal)and(height(r)-t>glue_shrink(r)) then
  14102. set_glue_ratio_one(glue_set(r))
  14103. else glue_set(r):=unfloat((height(r)-t)/glue_shrink(r));
  14104. end;
  14105. height(r):=w; type(r):=vlist_node;
  14106. end
  14107. @ We now have a completed alignment, in the list that starts at |head|
  14108. and ends at |tail|. This list will be merged with the one that encloses
  14109. it. (In case the enclosing mode is |mmode|, for displayed formulas,
  14110. we will need to insert glue before and after the display; that part of the
  14111. program will be deferred until we're more familiar with such operations.)
  14112. In restricted horizontal mode, the |clang| part of |aux| is undefined;
  14113. an over-cautious \PASCAL\ runtime system may complain about this.
  14114. @^dirty \PASCAL@>
  14115. @<Insert the \(c)current list into its environment@>=
  14116. aux_save:=aux; p:=link(head); q:=tail; pop_nest;
  14117. if mode=mmode then @<Finish an alignment in a display@>
  14118. else begin aux:=aux_save; link(tail):=p;
  14119. if p<>null then tail:=q;
  14120. if mode=vmode then build_page;
  14121. end
  14122. @* \[38] Breaking paragraphs into lines.
  14123. We come now to what is probably the most interesting algorithm of \TeX:
  14124. the mechanism for choosing the ``best possible'' breakpoints that yield
  14125. the individual lines of a paragraph. \TeX's line-breaking algorithm takes
  14126. a given horizontal list and converts it to a sequence of boxes that are
  14127. appended to the current vertical list. In the course of doing this, it
  14128. creates a special data structure containing three kinds of records that are
  14129. not used elsewhere in \TeX. Such nodes are created while a paragraph is
  14130. being processed, and they are destroyed afterwards; thus, the other parts
  14131. of \TeX\ do not need to know anything about how line-breaking is done.
  14132. The method used here is based on an approach devised by Michael F. Plass and
  14133. @^Plass, Michael Frederick@>
  14134. @^Knuth, Donald Ervin@>
  14135. the author in 1977, subsequently generalized and improved by the same two
  14136. people in 1980. A detailed discussion appears in {\sl Software---Practice
  14137. and Experience \bf11} (1981), 1119--1184, where it is shown that the
  14138. line-breaking problem can be regarded as a special case of the problem of
  14139. computing the shortest path in an acyclic network. The cited paper includes
  14140. numerous examples and describes the history of line breaking as it has been
  14141. practiced by printers through the ages. The present implementation adds two
  14142. new ideas to the algorithm of 1980: Memory space requirements are considerably
  14143. reduced by using smaller records for inactive nodes than for active ones,
  14144. and arithmetic overflow is avoided by using ``delta distances'' instead of
  14145. keeping track of the total distance from the beginning of the paragraph to the
  14146. current point.
  14147. @ The |line_break| procedure should be invoked only in horizontal mode; it
  14148. leaves that mode and places its output into the current vlist of the
  14149. enclosing vertical mode (or internal vertical mode).
  14150. There is one explicit parameter: |final_widow_penalty| is the amount of
  14151. additional penalty to be inserted before the final line of the paragraph.
  14152. There are also a number of implicit parameters: The hlist to be broken
  14153. starts at |link(head)|, and it is nonempty. The value of |prev_graf| in the
  14154. enclosing semantic level tells where the paragraph should begin in the
  14155. sequence of line numbers, in case hanging indentation or \.{\\parshape}
  14156. is in use; |prev_graf| is zero unless this paragraph is being continued
  14157. after a displayed formula. Other implicit parameters, such as the
  14158. |par_shape_ptr| and various penalties to use for hyphenation, etc., appear
  14159. in |eqtb|.
  14160. After |line_break| has acted, it will have updated the current vlist and the
  14161. value of |prev_graf|. Furthermore, the global variable |just_box| will
  14162. point to the final box created by |line_break|, so that the width of this
  14163. line can be ascertained when it is necessary to decide whether to use
  14164. |above_display_skip| or |above_display_short_skip| before a displayed formula.
  14165. @<Glob...@>=
  14166. @!just_box:pointer; {the |hlist_node| for the last line of the new paragraph}
  14167. @ Since |line_break| is a rather lengthy procedure---sort of a small world unto
  14168. itself---we must build it up little by little, somewhat more cautiously
  14169. than we have done with the simpler procedures of \TeX. Here is the
  14170. general outline.
  14171. @p@t\4@>@<Declare subprocedures for |line_break|@>
  14172. procedure line_break(@!final_widow_penalty:integer);
  14173. label done,done1,done2,done3,done4,done5,continue;
  14174. var @<Local variables for line breaking@>@;
  14175. begin pack_begin_line:=mode_line; {this is for over/underfull box messages}
  14176. @<Get ready to start line breaking@>;
  14177. @<Find optimal breakpoints@>;
  14178. @<Break the paragraph at the chosen breakpoints, justify the resulting lines
  14179. to the correct widths, and append them to the current vertical list@>;
  14180. @<Clean up the memory by removing the break nodes@>;
  14181. pack_begin_line:=0;
  14182. end;
  14183. @ The first task is to move the list from |head| to |temp_head| and go
  14184. into the enclosing semantic level. We also append the \.{\\parfillskip}
  14185. glue to the end of the paragraph, removing a space (or other glue node) if
  14186. it was there, since spaces usually precede blank lines and instances of
  14187. `\.{\$\$}'. The |par_fill_skip| is preceded by an infinite penalty, so
  14188. it will never be considered as a potential breakpoint.
  14189. This code assumes that a |glue_node| and a |penalty_node| occupy the
  14190. same number of |mem|~words.
  14191. @^data structure assumptions@>
  14192. @<Get ready to start...@>=
  14193. link(temp_head):=link(head);
  14194. if is_char_node(tail) then tail_append(new_penalty(inf_penalty))
  14195. else if type(tail)<>glue_node then tail_append(new_penalty(inf_penalty))
  14196. else begin type(tail):=penalty_node; delete_glue_ref(glue_ptr(tail));
  14197. flush_node_list(leader_ptr(tail)); penalty(tail):=inf_penalty;
  14198. end;
  14199. link(tail):=new_param_glue(par_fill_skip_code);
  14200. init_cur_lang:=prev_graf mod @'200000;
  14201. init_l_hyf:=prev_graf div @'20000000;
  14202. init_r_hyf:=(prev_graf div @'200000) mod @'100;
  14203. pop_nest;
  14204. @ When looking for optimal line breaks, \TeX\ creates a ``break node'' for
  14205. each break that is {\sl feasible}, in the sense that there is a way to end
  14206. a line at the given place without requiring any line to stretch more than
  14207. a given tolerance. A break node is characterized by three things: the position
  14208. of the break (which is a pointer to a |glue_node|, |math_node|, |penalty_node|,
  14209. or |disc_node|); the ordinal number of the line that will follow this
  14210. breakpoint; and the fitness classification of the line that has just
  14211. ended, i.e., |tight_fit|, |decent_fit|, |loose_fit|, or |very_loose_fit|.
  14212. @d tight_fit=3 {fitness classification for lines shrinking 0.5 to 1.0 of their
  14213. shrinkability}
  14214. @d loose_fit=1 {fitness classification for lines stretching 0.5 to 1.0 of their
  14215. stretchability}
  14216. @d very_loose_fit=0 {fitness classification for lines stretching more than
  14217. their stretchability}
  14218. @d decent_fit=2 {fitness classification for all other lines}
  14219. @ The algorithm essentially determines the best possible way to achieve
  14220. each feasible combination of position, line, and fitness. Thus, it answers
  14221. questions like, ``What is the best way to break the opening part of the
  14222. paragraph so that the fourth line is a tight line ending at such-and-such
  14223. a place?'' However, the fact that all lines are to be the same length
  14224. after a certain point makes it possible to regard all sufficiently large
  14225. line numbers as equivalent, when the looseness parameter is zero, and this
  14226. makes it possible for the algorithm to save space and time.
  14227. An ``active node'' and a ``passive node'' are created in |mem| for each
  14228. feasible breakpoint that needs to be considered. Active nodes are three
  14229. words long and passive nodes are two words long. We need active nodes only
  14230. for breakpoints near the place in the paragraph that is currently being
  14231. examined, so they are recycled within a comparatively short time after
  14232. they are created.
  14233. @ An active node for a given breakpoint contains six fields:
  14234. \yskip\hang|link| points to the next node in the list of active nodes; the
  14235. last active node has |link=last_active|.
  14236. \yskip\hang|break_node| points to the passive node associated with this
  14237. breakpoint.
  14238. \yskip\hang|line_number| is the number of the line that follows this
  14239. breakpoint.
  14240. \yskip\hang|fitness| is the fitness classification of the line ending at this
  14241. breakpoint.
  14242. \yskip\hang|type| is either |hyphenated| or |unhyphenated|, depending on
  14243. whether this breakpoint is a |disc_node|.
  14244. \yskip\hang|total_demerits| is the minimum possible sum of demerits over all
  14245. lines leading from the beginning of the paragraph to this breakpoint.
  14246. \yskip\noindent
  14247. The value of |link(active)| points to the first active node on a linked list
  14248. of all currently active nodes. This list is in order by |line_number|,
  14249. except that nodes with |line_number>easy_line| may be in any order relative
  14250. to each other.
  14251. @d active_node_size=3 {number of words in active nodes}
  14252. @d fitness==subtype {|very_loose_fit..tight_fit| on final line for this break}
  14253. @d break_node==rlink {pointer to the corresponding passive node}
  14254. @d line_number==llink {line that begins at this breakpoint}
  14255. @d total_demerits(#)==mem[#+2].int {the quantity that \TeX\ minimizes}
  14256. @d unhyphenated=0 {the |type| of a normal active break node}
  14257. @d hyphenated=1 {the |type| of an active node that breaks at a |disc_node|}
  14258. @d last_active==active {the active list ends where it begins}
  14259. @ @<Initialize the special list heads...@>=
  14260. type(last_active):=hyphenated; line_number(last_active):=max_halfword;
  14261. subtype(last_active):=0; {the |subtype| is never examined by the algorithm}
  14262. @ The passive node for a given breakpoint contains only four fields:
  14263. \yskip\hang|link| points to the passive node created just before this one,
  14264. if any, otherwise it is |null|.
  14265. \yskip\hang|cur_break| points to the position of this breakpoint in the
  14266. horizontal list for the paragraph being broken.
  14267. \yskip\hang|prev_break| points to the passive node that should precede this
  14268. one in an optimal path to this breakpoint.
  14269. \yskip\hang|serial| is equal to |n| if this passive node is the |n|th
  14270. one created during the current pass. (This field is used only when
  14271. printing out detailed statistics about the line-breaking calculations.)
  14272. \yskip\noindent
  14273. There is a global variable called |passive| that points to the most
  14274. recently created passive node. Another global variable, |printed_node|,
  14275. is used to help print out the paragraph when detailed information about
  14276. the line-breaking computation is being displayed.
  14277. @d passive_node_size=2 {number of words in passive nodes}
  14278. @d cur_break==rlink {in passive node, points to position of this breakpoint}
  14279. @d prev_break==llink {points to passive node that should precede this one}
  14280. @d serial==info {serial number for symbolic identification}
  14281. @<Glob...@>=
  14282. @!passive:pointer; {most recent node on passive list}
  14283. @!printed_node:pointer; {most recent node that has been printed}
  14284. @!pass_number:halfword; {the number of passive nodes allocated on this pass}
  14285. @ The active list also contains ``delta'' nodes that help the algorithm
  14286. compute the badness of individual lines. Such nodes appear only between two
  14287. active nodes, and they have |type=delta_node|. If |p| and |r| are active nodes
  14288. and if |q| is a delta node between them, so that |link(p)=q| and |link(q)=r|,
  14289. then |q| tells the space difference between lines in the horizontal list that
  14290. start after breakpoint |p| and lines that start after breakpoint |r|. In
  14291. other words, if we know the length of the line that starts after |p| and
  14292. ends at our current position, then the corresponding length of the line that
  14293. starts after |r| is obtained by adding the amounts in node~|q|. A delta node
  14294. contains six scaled numbers, since it must record the net change in glue
  14295. stretchability with respect to all orders of infinity. The natural width
  14296. difference appears in |mem[q+1].sc|; the stretch differences in units of
  14297. pt, fil, fill, and filll appear in |mem[q+2..q+5].sc|; and the shrink difference
  14298. appears in |mem[q+6].sc|. The |subtype| field of a delta node is not used.
  14299. @d delta_node_size=7 {number of words in a delta node}
  14300. @d delta_node=2 {|type| field in a delta node}
  14301. @ As the algorithm runs, it maintains a set of six delta-like registers
  14302. for the length of the line following the first active breakpoint to the
  14303. current position in the given hlist. When it makes a pass through the
  14304. active list, it also maintains a similar set of six registers for the
  14305. length following the active breakpoint of current interest. A third set
  14306. holds the length of an empty line (namely, the sum of \.{\\leftskip} and
  14307. \.{\\rightskip}); and a fourth set is used to create new delta nodes.
  14308. When we pass a delta node we want to do operations like
  14309. $$\hbox{\ignorespaces|for
  14310. k:=1 to 6 do cur_active_width[k]:=cur_active_width[k]+mem[q+k].sc|};$$ and we
  14311. want to do this without the overhead of |for| loops. The |do_all_six|
  14312. macro makes such six-tuples convenient.
  14313. @d do_all_six(#)==#(1);#(2);#(3);#(4);#(5);#(6)
  14314. @<Glob...@>=
  14315. @!active_width:array[1..6] of scaled;
  14316. {distance from first active node to~|cur_p|}
  14317. @!cur_active_width:array[1..6] of scaled; {distance from current active node}
  14318. @!background:array[1..6] of scaled; {length of an ``empty'' line}
  14319. @!break_width:array[1..6] of scaled; {length being computed after current break}
  14320. @ Let's state the principles of the delta nodes more precisely and concisely,
  14321. so that the following programs will be less obscure. For each legal
  14322. breakpoint~|p| in the paragraph, we define two quantities $\alpha(p)$ and
  14323. $\beta(p)$ such that the length of material in a line from breakpoint~|p|
  14324. to breakpoint~|q| is $\gamma+\beta(q)-\alpha(p)$, for some fixed $\gamma$.
  14325. Intuitively, $\alpha(p)$ and $\beta(q)$ are the total length of material from
  14326. the beginning of the paragraph to a point ``after'' a break at |p| and to a
  14327. point ``before'' a break at |q|; and $\gamma$ is the width of an empty line,
  14328. namely the length contributed by \.{\\leftskip} and \.{\\rightskip}.
  14329. Suppose, for example, that the paragraph consists entirely of alternating
  14330. boxes and glue skips; let the boxes have widths $x_1\ldots x_n$ and
  14331. let the skips have widths $y_1\ldots y_n$, so that the paragraph can be
  14332. represented by $x_1y_1\ldots x_ny_n$. Let $p_i$ be the legal breakpoint
  14333. at $y_i$; then $\alpha(p_i)=x_1+y_1+\cdots+x_i+y_i$, and $\beta(p_i)=
  14334. x_1+y_1+\cdots+x_i$. To check this, note that the length of material from
  14335. $p_2$ to $p_5$, say, is $\gamma+x_3+y_3+x_4+y_4+x_5=\gamma+\beta(p_5)
  14336. -\alpha(p_2)$.
  14337. The quantities $\alpha$, $\beta$, $\gamma$ involve glue stretchability and
  14338. shrinkability as well as a natural width. If we were to compute $\alpha(p)$
  14339. and $\beta(p)$ for each |p|, we would need multiple precision arithmetic, and
  14340. the multiprecise numbers would have to be kept in the active nodes.
  14341. \TeX\ avoids this problem by working entirely with relative differences
  14342. or ``deltas.'' Suppose, for example, that the active list contains
  14343. $a_1\,\delta_1\,a_2\,\delta_2\,a_3$, where the |a|'s are active breakpoints
  14344. and the $\delta$'s are delta nodes. Then $\delta_1=\alpha(a_1)-\alpha(a_2)$
  14345. and $\delta_2=\alpha(a_2)-\alpha(a_3)$. If the line breaking algorithm is
  14346. currently positioned at some other breakpoint |p|, the |active_width| array
  14347. contains the value $\gamma+\beta(p)-\alpha(a_1)$. If we are scanning through
  14348. the list of active nodes and considering a tentative line that runs from
  14349. $a_2$ to~|p|, say, the |cur_active_width| array will contain the value
  14350. $\gamma+\beta(p)-\alpha(a_2)$. Thus, when we move from $a_2$ to $a_3$,
  14351. we want to add $\alpha(a_2)-\alpha(a_3)$ to |cur_active_width|; and this
  14352. is just $\delta_2$, which appears in the active list between $a_2$ and
  14353. $a_3$. The |background| array contains $\gamma$. The |break_width| array
  14354. will be used to calculate values of new delta nodes when the active
  14355. list is being updated.
  14356. @ Glue nodes in a horizontal list that is being paragraphed are not supposed to
  14357. include ``infinite'' shrinkability; that is why the algorithm maintains
  14358. four registers for stretching but only one for shrinking. If the user tries to
  14359. introduce infinite shrinkability, the shrinkability will be reset to finite
  14360. and an error message will be issued. A boolean variable |no_shrink_error_yet|
  14361. prevents this error message from appearing more than once per paragraph.
  14362. @d check_shrinkage(#)==if (shrink_order(#)<>normal)and(shrink(#)<>0) then
  14363. begin #:=finite_shrink(#);
  14364. end
  14365. @<Glob...@>=
  14366. @!no_shrink_error_yet:boolean; {have we complained about infinite shrinkage?}
  14367. @ @<Declare subprocedures for |line_break|@>=
  14368. function finite_shrink(@!p:pointer):pointer; {recovers from infinite shrinkage}
  14369. var q:pointer; {new glue specification}
  14370. begin if no_shrink_error_yet then
  14371. begin no_shrink_error_yet:=false;
  14372. @!stat if tracing_paragraphs>0 then end_diagnostic(true);@+tats@;
  14373. print_err("Infinite glue shrinkage found in a paragraph");
  14374. @.Infinite glue shrinkage...@>
  14375. help5("The paragraph just ended includes some glue that has")@/
  14376. ("infinite shrinkability, e.g., `\hskip 0pt minus 1fil'.")@/
  14377. ("Such glue doesn't belong there---it allows a paragraph")@/
  14378. ("of any length to fit on one line. But it's safe to proceed,")@/
  14379. ("since the offensive shrinkability has been made finite.");
  14380. error;
  14381. @!stat if tracing_paragraphs>0 then begin_diagnostic;@+tats@;
  14382. end;
  14383. q:=new_spec(p); shrink_order(q):=normal;
  14384. delete_glue_ref(p); finite_shrink:=q;
  14385. end;
  14386. @ @<Get ready to start...@>=
  14387. no_shrink_error_yet:=true;@/
  14388. check_shrinkage(left_skip); check_shrinkage(right_skip);@/
  14389. q:=left_skip; r:=right_skip; background[1]:=width(q)+width(r);@/
  14390. background[2]:=0; background[3]:=0; background[4]:=0; background[5]:=0;@/
  14391. background[2+stretch_order(q)]:=stretch(q);@/
  14392. background[2+stretch_order(r)]:=@|background[2+stretch_order(r)]+stretch(r);@/
  14393. background[6]:=shrink(q)+shrink(r);
  14394. @ A pointer variable |cur_p| runs through the given horizontal list as we look
  14395. for breakpoints. This variable is global, since it is used both by |line_break|
  14396. and by its subprocedure |try_break|.
  14397. Another global variable called |threshold| is used to determine the feasibility
  14398. of individual lines: Breakpoints are feasible if there is a way to reach
  14399. them without creating lines whose badness exceeds |threshold|. (The
  14400. badness is compared to |threshold| before penalties are added, so that
  14401. penalty values do not affect the feasibility of breakpoints, except that
  14402. no break is allowed when the penalty is 10000 or more.) If |threshold|
  14403. is 10000 or more, all legal breaks are considered feasible, since the
  14404. |badness| function specified above never returns a value greater than~10000.
  14405. Up to three passes might be made through the paragraph in an attempt to find at
  14406. least one set of feasible breakpoints. On the first pass, we have
  14407. |threshold=pretolerance| and |second_pass=final_pass=false|.
  14408. If this pass fails to find a
  14409. feasible solution, |threshold| is set to |tolerance|, |second_pass| is set
  14410. |true|, and an attempt is made to hyphenate as many words as possible.
  14411. If that fails too, we add |emergency_stretch| to the background
  14412. stretchability and set |final_pass=true|.
  14413. @<Glob...@>=
  14414. @!cur_p:pointer; {the current breakpoint under consideration}
  14415. @!second_pass:boolean; {is this our second attempt to break this paragraph?}
  14416. @!final_pass:boolean; {is this our final attempt to break this paragraph?}
  14417. @!threshold:integer; {maximum badness on feasible lines}
  14418. @ The heart of the line-breaking procedure is `|try_break|', a subroutine
  14419. that tests if the current breakpoint |cur_p| is feasible, by running
  14420. through the active list to see what lines of text can be made from active
  14421. nodes to~|cur_p|. If feasible breaks are possible, new break nodes are
  14422. created. If |cur_p| is too far from an active node, that node is
  14423. deactivated.
  14424. The parameter |pi| to |try_break| is the penalty associated
  14425. with a break at |cur_p|; we have |pi=eject_penalty| if the break is forced,
  14426. and |pi=inf_penalty| if the break is illegal.
  14427. The other parameter, |break_type|, is set to |hyphenated| or |unhyphenated|,
  14428. depending on whether or not the current break is at a |disc_node|. The
  14429. end of a paragraph is also regarded as `|hyphenated|'; this case is
  14430. distinguishable by the condition |cur_p=null|.
  14431. @d copy_to_cur_active(#)==cur_active_width[#]:=active_width[#]
  14432. @d deactivate=60 {go here when node |r| should be deactivated}
  14433. @<Declare subprocedures for |line_break|@>=
  14434. procedure try_break(@!pi:integer;@!break_type:small_number);
  14435. label exit,done,done1,continue,deactivate;
  14436. var r:pointer; {runs through the active list}
  14437. @!prev_r:pointer; {stays a step behind |r|}
  14438. @!old_l:halfword; {maximum line number in current equivalence class of lines}
  14439. @!no_break_yet:boolean; {have we found a feasible break at |cur_p|?}
  14440. @<Other local variables for |try_break|@>@;
  14441. begin @<Make sure that |pi| is in the proper range@>;
  14442. no_break_yet:=true; prev_r:=active; old_l:=0;
  14443. do_all_six(copy_to_cur_active);
  14444. loop@+ begin continue: r:=link(prev_r);
  14445. @<If node |r| is of type |delta_node|, update |cur_active_width|,
  14446. set |prev_r| and |prev_prev_r|, then |goto continue|@>;
  14447. @<If a line number class has ended, create new active nodes for
  14448. the best feasible breaks in that class; then |return|
  14449. if |r=last_active|, otherwise compute the new |line_width|@>;
  14450. @<Consider the demerits for a line from |r| to |cur_p|;
  14451. deactivate node |r| if it should no longer be active;
  14452. then |goto continue| if a line from |r| to |cur_p| is infeasible,
  14453. otherwise record a new feasible break@>;
  14454. end;
  14455. exit: @!stat @<Update the value of |printed_node| for
  14456. symbolic displays@>@+tats@;
  14457. end;
  14458. @ @<Other local variables for |try_break|@>=
  14459. @!prev_prev_r:pointer; {a step behind |prev_r|, if |type(prev_r)=delta_node|}
  14460. @!s:pointer; {runs through nodes ahead of |cur_p|}
  14461. @!q:pointer; {points to a new node being created}
  14462. @!v:pointer; {points to a glue specification or a node ahead of |cur_p|}
  14463. @!t:integer; {node count, if |cur_p| is a discretionary node}
  14464. @!f:internal_font_number; {used in character width calculation}
  14465. @!l:halfword; {line number of current active node}
  14466. @!node_r_stays_active:boolean; {should node |r| remain in the active list?}
  14467. @!line_width:scaled; {the current line will be justified to this width}
  14468. @!fit_class:very_loose_fit..tight_fit; {possible fitness class of test line}
  14469. @!b:halfword; {badness of test line}
  14470. @!d:integer; {demerits of test line}
  14471. @!artificial_demerits:boolean; {has |d| been forced to zero?}
  14472. @!save_link:pointer; {temporarily holds value of |link(cur_p)|}
  14473. @!shortfall:scaled; {used in badness calculations}
  14474. @ @<Make sure that |pi| is in the proper range@>=
  14475. if abs(pi)>=inf_penalty then
  14476. if pi>0 then return {this breakpoint is inhibited by infinite penalty}
  14477. else pi:=eject_penalty {this breakpoint will be forced}
  14478. @ The following code uses the fact that |type(last_active)<>delta_node|.
  14479. @d update_width(#)==@|
  14480. cur_active_width[#]:=cur_active_width[#]+mem[r+#].sc
  14481. @<If node |r|...@>=
  14482. @^inner loop@>
  14483. if type(r)=delta_node then
  14484. begin do_all_six(update_width);
  14485. prev_prev_r:=prev_r; prev_r:=r; goto continue;
  14486. end
  14487. @ As we consider various ways to end a line at |cur_p|, in a given line number
  14488. class, we keep track of the best total demerits known, in an array with
  14489. one entry for each of the fitness classifications. For example,
  14490. |minimal_demerits[tight_fit]| contains the fewest total demerits of feasible
  14491. line breaks ending at |cur_p| with a |tight_fit| line; |best_place[tight_fit]|
  14492. points to the passive node for the break before~|cur_p| that achieves such
  14493. an optimum; and |best_pl_line[tight_fit]| is the |line_number| field in the
  14494. active node corresponding to |best_place[tight_fit]|. When no feasible break
  14495. sequence is known, the |minimal_demerits| entries will be equal to
  14496. |awful_bad|, which is $2^{30}-1$. Another variable, |minimum_demerits|,
  14497. keeps track of the smallest value in the |minimal_demerits| array.
  14498. @d awful_bad==@'7777777777 {more than a billion demerits}
  14499. @<Global...@>=
  14500. @!minimal_demerits:array[very_loose_fit..tight_fit] of integer; {best total
  14501. demerits known for current line class and position, given the fitness}
  14502. @!minimum_demerits:integer; {best total demerits known for current line class
  14503. and position}
  14504. @!best_place:array[very_loose_fit..tight_fit] of pointer; {how to achieve
  14505. |minimal_demerits|}
  14506. @!best_pl_line:array[very_loose_fit..tight_fit] of halfword; {corresponding
  14507. line number}
  14508. @ @<Get ready to start...@>=
  14509. minimum_demerits:=awful_bad;
  14510. minimal_demerits[tight_fit]:=awful_bad;
  14511. minimal_demerits[decent_fit]:=awful_bad;
  14512. minimal_demerits[loose_fit]:=awful_bad;
  14513. minimal_demerits[very_loose_fit]:=awful_bad;
  14514. @ The first part of the following code is part of \TeX's inner loop, so
  14515. we don't want to waste any time. The current active node, namely node |r|,
  14516. contains the line number that will be considered next. At the end of the
  14517. list we have arranged the data structure so that |r=last_active| and
  14518. |line_number(last_active)>old_l|.
  14519. @^inner loop@>
  14520. @<If a line number class...@>=
  14521. begin l:=line_number(r);
  14522. if l>old_l then
  14523. begin {now we are no longer in the inner loop}
  14524. if (minimum_demerits<awful_bad)and@|
  14525. ((old_l<>easy_line)or(r=last_active)) then
  14526. @<Create new active nodes for the best feasible breaks
  14527. just found@>;
  14528. if r=last_active then return;
  14529. @<Compute the new line width@>;
  14530. end;
  14531. end
  14532. @ It is not necessary to create new active nodes having |minimal_demerits|
  14533. greater than
  14534. |minimum_demerits+abs(adj_demerits)|, since such active nodes will never
  14535. be chosen in the final paragraph breaks. This observation allows us to
  14536. omit a substantial number of feasible breakpoints from further consideration.
  14537. @<Create new active nodes...@>=
  14538. begin if no_break_yet then @<Compute the values of |break_width|@>;
  14539. @<Insert a delta node to prepare for breaks at |cur_p|@>;
  14540. if abs(adj_demerits)>=awful_bad-minimum_demerits then
  14541. minimum_demerits:=awful_bad-1
  14542. else minimum_demerits:=minimum_demerits+abs(adj_demerits);
  14543. for fit_class:=very_loose_fit to tight_fit do
  14544. begin if minimal_demerits[fit_class]<=minimum_demerits then
  14545. @<Insert a new active node
  14546. from |best_place[fit_class]| to |cur_p|@>;
  14547. minimal_demerits[fit_class]:=awful_bad;
  14548. end;
  14549. minimum_demerits:=awful_bad;
  14550. @<Insert a delta node to prepare for the next active node@>;
  14551. end
  14552. @ When we insert a new active node for a break at |cur_p|, suppose this
  14553. new node is to be placed just before active node |a|; then we essentially
  14554. want to insert `$\delta\,|cur_p|\,\delta^\prime$' before |a|, where
  14555. $\delta=\alpha(a)-\alpha(|cur_p|)$ and $\delta^\prime=\alpha(|cur_p|)-\alpha(a)$
  14556. in the notation explained above. The |cur_active_width| array now holds
  14557. $\gamma+\beta(|cur_p|)-\alpha(a)$; so $\delta$ can be obtained by
  14558. subtracting |cur_active_width| from the quantity $\gamma+\beta(|cur_p|)-
  14559. \alpha(|cur_p|)$. The latter quantity can be regarded as the length of a
  14560. line ``from |cur_p| to |cur_p|''; we call it the |break_width| at |cur_p|.
  14561. The |break_width| is usually negative, since it consists of the background
  14562. (which is normally zero) minus the width of nodes following~|cur_p| that are
  14563. eliminated after a break. If, for example, node |cur_p| is a glue node, the
  14564. width of this glue is subtracted from the background; and we also look
  14565. ahead to eliminate all subsequent glue and penalty and kern and math
  14566. nodes, subtracting their widths as well.
  14567. Kern nodes do not disappear at a line break unless they are |explicit|.
  14568. @d set_break_width_to_background(#)==break_width[#]:=background[#]
  14569. @<Compute the values of |break...@>=
  14570. begin no_break_yet:=false; do_all_six(set_break_width_to_background);
  14571. s:=cur_p;
  14572. if break_type>unhyphenated then if cur_p<>null then
  14573. @<Compute the discretionary |break_width| values@>;
  14574. while s<>null do
  14575. begin if is_char_node(s) then goto done;
  14576. case type(s) of
  14577. glue_node:@<Subtract glue from |break_width|@>;
  14578. penalty_node: do_nothing;
  14579. math_node: break_width[1]:=break_width[1]-width(s);
  14580. kern_node: if subtype(s)<>explicit then goto done
  14581. else break_width[1]:=break_width[1]-width(s);
  14582. othercases goto done
  14583. endcases;@/
  14584. s:=link(s);
  14585. end;
  14586. done: end
  14587. @ @<Subtract glue from |break...@>=
  14588. begin v:=glue_ptr(s); break_width[1]:=break_width[1]-width(v);
  14589. break_width[2+stretch_order(v)]:=break_width[2+stretch_order(v)]-stretch(v);
  14590. break_width[6]:=break_width[6]-shrink(v);
  14591. end
  14592. @ When |cur_p| is a discretionary break, the length of a line ``from |cur_p| to
  14593. |cur_p|'' has to be defined properly so that the other calculations work out.
  14594. Suppose that the pre-break text at |cur_p| has length $l_0$, the post-break
  14595. text has length $l_1$, and the replacement text has length |l|. Suppose
  14596. also that |q| is the node following the replacement text. Then length of a
  14597. line from |cur_p| to |q| will be computed as $\gamma+\beta(q)-\alpha(|cur_p|)$,
  14598. where $\beta(q)=\beta(|cur_p|)-l_0+l$. The actual length will be the background
  14599. plus $l_1$, so the length from |cur_p| to |cur_p| should be $\gamma+l_0+l_1-l$.
  14600. If the post-break text of the discretionary is empty, a break may also
  14601. discard~|q|; in that unusual case we subtract the length of~|q| and any
  14602. other nodes that will be discarded after the discretionary break.
  14603. The value of $l_0$ need not be computed, since |line_break| will put
  14604. it into the global variable |disc_width| before calling |try_break|.
  14605. @<Glob...@>=
  14606. @!disc_width:scaled; {the length of discretionary material preceding a break}
  14607. @ @<Compute the discretionary |break...@>=
  14608. begin t:=replace_count(cur_p); v:=cur_p; s:=post_break(cur_p);
  14609. while t>0 do
  14610. begin decr(t); v:=link(v);
  14611. @<Subtract the width of node |v| from |break_width|@>;
  14612. end;
  14613. while s<>null do
  14614. begin @<Add the width of node |s| to |break_width|@>;
  14615. s:=link(s);
  14616. end;
  14617. break_width[1]:=break_width[1]+disc_width;
  14618. if post_break(cur_p)=null then s:=link(v);
  14619. {nodes may be discardable after the break}
  14620. end
  14621. @ Replacement texts and discretionary texts are supposed to contain
  14622. only character nodes, kern nodes, ligature nodes, and box or rule nodes.
  14623. @<Subtract the width of node |v|...@>=
  14624. if is_char_node(v) then
  14625. begin f:=font(v);
  14626. break_width[1]:=break_width[1]-char_width(f)(char_info(f)(character(v)));
  14627. end
  14628. else case type(v) of
  14629. ligature_node: begin f:=font(lig_char(v));@/
  14630. break_width[1]:=@|break_width[1]-
  14631. char_width(f)(char_info(f)(character(lig_char(v))));
  14632. end;
  14633. hlist_node,vlist_node,rule_node,kern_node:
  14634. break_width[1]:=break_width[1]-width(v);
  14635. othercases confusion("disc1")
  14636. @:this can't happen disc1}{\quad disc1@>
  14637. endcases
  14638. @ @<Add the width of node |s| to |b...@>=
  14639. if is_char_node(s) then
  14640. begin f:=font(s);
  14641. break_width[1]:=@|break_width[1]+char_width(f)(char_info(f)(character(s)));
  14642. end
  14643. else case type(s) of
  14644. ligature_node: begin f:=font(lig_char(s));
  14645. break_width[1]:=break_width[1]+
  14646. char_width(f)(char_info(f)(character(lig_char(s))));
  14647. end;
  14648. hlist_node,vlist_node,rule_node,kern_node:
  14649. break_width[1]:=break_width[1]+width(s);
  14650. othercases confusion("disc2")
  14651. @:this can't happen disc2}{\quad disc2@>
  14652. endcases
  14653. @ We use the fact that |type(active)<>delta_node|.
  14654. @d convert_to_break_width(#)==@|
  14655. mem[prev_r+#].sc:=@|@t\hskip10pt@>mem[prev_r+#].sc
  14656. -cur_active_width[#]+break_width[#]
  14657. @d store_break_width(#)==active_width[#]:=break_width[#]
  14658. @d new_delta_to_break_width(#)==@|
  14659. mem[q+#].sc:=break_width[#]-cur_active_width[#]
  14660. @<Insert a delta node to prepare for breaks at |cur_p|@>=
  14661. if type(prev_r)=delta_node then {modify an existing delta node}
  14662. begin do_all_six(convert_to_break_width);
  14663. end
  14664. else if prev_r=active then {no delta node needed at the beginning}
  14665. begin do_all_six(store_break_width);
  14666. end
  14667. else begin q:=get_node(delta_node_size); link(q):=r; type(q):=delta_node;@/
  14668. subtype(q):=0; {the |subtype| is not used}
  14669. do_all_six(new_delta_to_break_width);
  14670. link(prev_r):=q; prev_prev_r:=prev_r; prev_r:=q;
  14671. end
  14672. @ When the following code is performed, we will have just inserted at
  14673. least one active node before |r|, so |type(prev_r)<>delta_node|.
  14674. @d new_delta_from_break_width(#)==@|mem[q+#].sc:=
  14675. cur_active_width[#]-break_width[#]
  14676. @<Insert a delta node to prepare for the next active node@>=
  14677. if r<>last_active then
  14678. begin q:=get_node(delta_node_size); link(q):=r; type(q):=delta_node;@/
  14679. subtype(q):=0; {the |subtype| is not used}
  14680. do_all_six(new_delta_from_break_width);
  14681. link(prev_r):=q; prev_prev_r:=prev_r; prev_r:=q;
  14682. end
  14683. @ When we create an active node, we also create the corresponding
  14684. passive node.
  14685. @<Insert a new active node from |best_place[fit_class]| to |cur_p|@>=
  14686. begin q:=get_node(passive_node_size);
  14687. link(q):=passive; passive:=q; cur_break(q):=cur_p;
  14688. @!stat incr(pass_number); serial(q):=pass_number;@+tats@;@/
  14689. prev_break(q):=best_place[fit_class];@/
  14690. q:=get_node(active_node_size); break_node(q):=passive;
  14691. line_number(q):=best_pl_line[fit_class]+1;
  14692. fitness(q):=fit_class; type(q):=break_type;
  14693. total_demerits(q):=minimal_demerits[fit_class];
  14694. link(q):=r; link(prev_r):=q; prev_r:=q;
  14695. @!stat if tracing_paragraphs>0 then
  14696. @<Print a symbolic description of the new break node@>;
  14697. tats@;@/
  14698. end
  14699. @ @<Print a symbolic description of the new break node@>=
  14700. begin print_nl("@@@@"); print_int(serial(passive));
  14701. @.\AT!\AT!@>
  14702. print(": line "); print_int(line_number(q)-1);
  14703. print_char("."); print_int(fit_class);
  14704. if break_type=hyphenated then print_char("-");
  14705. print(" t="); print_int(total_demerits(q));
  14706. print(" -> @@@@");
  14707. if prev_break(passive)=null then print_char("0")
  14708. else print_int(serial(prev_break(passive)));
  14709. end
  14710. @ The length of lines depends on whether the user has specified
  14711. \.{\\parshape} or \.{\\hangindent}. If |par_shape_ptr| is not null, it
  14712. points to a $(2n+1)$-word record in |mem|, where the |info| in the first
  14713. word contains the value of |n|, and the other $2n$ words contain the left
  14714. margins and line lengths for the first |n| lines of the paragraph; the
  14715. specifications for line |n| apply to all subsequent lines. If
  14716. |par_shape_ptr=null|, the shape of the paragraph depends on the value of
  14717. |n=hang_after|; if |n>=0|, hanging indentation takes place on lines |n+1|,
  14718. |n+2|, \dots, otherwise it takes place on lines 1, \dots, $\vert
  14719. n\vert$. When hanging indentation is active, the left margin is
  14720. |hang_indent|, if |hang_indent>=0|, else it is 0; the line length is
  14721. $|hsize|-\vert|hang_indent|\vert$. The normal setting is
  14722. |par_shape_ptr=null|, |hang_after=1|, and |hang_indent=0|.
  14723. Note that if |hang_indent=0|, the value of |hang_after| is irrelevant.
  14724. @^length of lines@> @^hanging indentation@>
  14725. @<Glob...@>=
  14726. @!easy_line:halfword; {line numbers |>easy_line| are equivalent in break nodes}
  14727. @!last_special_line:halfword; {line numbers |>last_special_line| all have
  14728. the same width}
  14729. @!first_width:scaled; {the width of all lines |<=last_special_line|, if
  14730. no \.{\\parshape} has been specified}
  14731. @!second_width:scaled; {the width of all lines |>last_special_line|}
  14732. @!first_indent:scaled; {left margin to go with |first_width|}
  14733. @!second_indent:scaled; {left margin to go with |second_width|}
  14734. @ We compute the values of |easy_line| and the other local variables relating
  14735. to line length when the |line_break| procedure is initializing itself.
  14736. @<Get ready to start...@>=
  14737. if par_shape_ptr=null then
  14738. if hang_indent=0 then
  14739. begin last_special_line:=0; second_width:=hsize;
  14740. second_indent:=0;
  14741. end
  14742. else @<Set line length parameters in preparation for hanging indentation@>
  14743. else begin last_special_line:=info(par_shape_ptr)-1;
  14744. second_width:=mem[par_shape_ptr+2*(last_special_line+1)].sc;
  14745. second_indent:=mem[par_shape_ptr+2*last_special_line+1].sc;
  14746. end;
  14747. if looseness=0 then easy_line:=last_special_line
  14748. else easy_line:=max_halfword
  14749. @ @<Set line length parameters in preparation for hanging indentation@>=
  14750. begin last_special_line:=abs(hang_after);
  14751. if hang_after<0 then
  14752. begin first_width:=hsize-abs(hang_indent);
  14753. if hang_indent>=0 then first_indent:=hang_indent
  14754. else first_indent:=0;
  14755. second_width:=hsize; second_indent:=0;
  14756. end
  14757. else begin first_width:=hsize; first_indent:=0;
  14758. second_width:=hsize-abs(hang_indent);
  14759. if hang_indent>=0 then second_indent:=hang_indent
  14760. else second_indent:=0;
  14761. end;
  14762. end
  14763. @ When we come to the following code, we have just encountered the first
  14764. active node~|r| whose |line_number| field contains |l|. Thus we want to
  14765. compute the length of the $l\mskip1mu$th line of the current paragraph. Furthermore,
  14766. we want to set |old_l| to the last number in the class of line numbers
  14767. equivalent to~|l|.
  14768. @<Compute the new line width@>=
  14769. if l>easy_line then
  14770. begin line_width:=second_width; old_l:=max_halfword-1;
  14771. end
  14772. else begin old_l:=l;
  14773. if l>last_special_line then line_width:=second_width
  14774. else if par_shape_ptr=null then line_width:=first_width
  14775. else line_width:=mem[par_shape_ptr+2*l@,].sc;
  14776. end
  14777. @ The remaining part of |try_break| deals with the calculation of
  14778. demerits for a break from |r| to |cur_p|.
  14779. The first thing to do is calculate the badness, |b|. This value will always
  14780. be between zero and |inf_bad+1|; the latter value occurs only in the
  14781. case of lines from |r| to |cur_p| that cannot shrink enough to fit the necessary
  14782. width. In such cases, node |r| will be deactivated.
  14783. We also deactivate node~|r| when a break at~|cur_p| is forced, since future
  14784. breaks must go through a forced break.
  14785. @<Consider the demerits for a line from |r| to |cur_p|...@>=
  14786. begin artificial_demerits:=false;@/
  14787. @^inner loop@>
  14788. shortfall:=line_width-cur_active_width[1]; {we're this much too short}
  14789. if shortfall>0 then
  14790. @<Set the value of |b| to the badness for stretching the line,
  14791. and compute the corresponding |fit_class|@>
  14792. else @<Set the value of |b| to the badness for shrinking the line,
  14793. and compute the corresponding |fit_class|@>;
  14794. if (b>inf_bad)or(pi=eject_penalty) then
  14795. @<Prepare to deactivate node~|r|, and |goto deactivate| unless
  14796. there is a reason to consider lines of text from |r| to |cur_p|@>
  14797. else begin prev_r:=r;
  14798. if b>threshold then goto continue;
  14799. node_r_stays_active:=true;
  14800. end;
  14801. @<Record a new feasible break@>;
  14802. if node_r_stays_active then goto continue; {|prev_r| has been set to |r|}
  14803. deactivate: @<Deactivate node |r|@>;
  14804. end
  14805. @ When a line must stretch, the available stretchability can be found in the
  14806. subarray |cur_active_width[2..5]|, in units of points, fil, fill, and filll.
  14807. The present section is part of \TeX's inner loop, and it is most often performed
  14808. when the badness is infinite; therefore it is worth while to make a quick
  14809. test for large width excess and small stretchability, before calling the
  14810. |badness| subroutine.
  14811. @^inner loop@>
  14812. @<Set the value of |b| to the badness for stretching...@>=
  14813. if (cur_active_width[3]<>0)or(cur_active_width[4]<>0)or@|
  14814. (cur_active_width[5]<>0) then
  14815. begin b:=0; fit_class:=decent_fit; {infinite stretch}
  14816. end
  14817. else begin if shortfall>7230584 then if cur_active_width[2]<1663497 then
  14818. begin b:=inf_bad; fit_class:=very_loose_fit; goto done1;
  14819. end;
  14820. b:=badness(shortfall,cur_active_width[2]);
  14821. if b>12 then
  14822. if b>99 then fit_class:=very_loose_fit
  14823. else fit_class:=loose_fit
  14824. else fit_class:=decent_fit;
  14825. done1:
  14826. end
  14827. @ Shrinkability is never infinite in a paragraph;
  14828. we can shrink the line from |r| to |cur_p| by at most |cur_active_width[6]|.
  14829. @<Set the value of |b| to the badness for shrinking...@>=
  14830. begin if -shortfall>cur_active_width[6] then b:=inf_bad+1
  14831. else b:=badness(-shortfall,cur_active_width[6]);
  14832. if b>12 then fit_class:=tight_fit@+else fit_class:=decent_fit;
  14833. end
  14834. @ During the final pass, we dare not lose all active nodes, lest we lose
  14835. touch with the line breaks already found. The code shown here makes sure
  14836. that such a catastrophe does not happen, by permitting overfull boxes as
  14837. a last resort. This particular part of \TeX\ was a source of several subtle
  14838. bugs before the correct program logic was finally discovered; readers
  14839. who seek to ``improve'' \TeX\ should therefore think thrice before daring
  14840. to make any changes here.
  14841. @^overfull boxes@>
  14842. @<Prepare to deactivate node~|r|, and |goto deactivate| unless...@>=
  14843. begin if final_pass and (minimum_demerits=awful_bad) and@|
  14844. (link(r)=last_active) and
  14845. (prev_r=active) then
  14846. artificial_demerits:=true {set demerits zero, this break is forced}
  14847. else if b>threshold then goto deactivate;
  14848. node_r_stays_active:=false;
  14849. end
  14850. @ When we get to this part of the code, the line from |r| to |cur_p| is
  14851. feasible, its badness is~|b|, and its fitness classification is |fit_class|.
  14852. We don't want to make an active node for this break yet, but we will
  14853. compute the total demerits and record them in the |minimal_demerits| array,
  14854. if such a break is the current champion among all ways to get to |cur_p|
  14855. in a given line-number class and fitness class.
  14856. @<Record a new feasible break@>=
  14857. if artificial_demerits then d:=0
  14858. else @<Compute the demerits, |d|, from |r| to |cur_p|@>;
  14859. @!stat if tracing_paragraphs>0 then
  14860. @<Print a symbolic description of this feasible break@>;
  14861. tats@;@/
  14862. d:=d+total_demerits(r); {this is the minimum total demerits
  14863. from the beginning to |cur_p| via |r|}
  14864. if d<=minimal_demerits[fit_class] then
  14865. begin minimal_demerits[fit_class]:=d;
  14866. best_place[fit_class]:=break_node(r); best_pl_line[fit_class]:=l;
  14867. if d<minimum_demerits then minimum_demerits:=d;
  14868. end
  14869. @ @<Print a symbolic description of this feasible break@>=
  14870. begin if printed_node<>cur_p then
  14871. @<Print the list between |printed_node| and |cur_p|,
  14872. then set |printed_node:=cur_p|@>;
  14873. print_nl("@@");
  14874. @.\AT!@>
  14875. if cur_p=null then print_esc("par")
  14876. else if type(cur_p)<>glue_node then
  14877. begin if type(cur_p)=penalty_node then print_esc("penalty")
  14878. else if type(cur_p)=disc_node then print_esc("discretionary")
  14879. else if type(cur_p)=kern_node then print_esc("kern")
  14880. else print_esc("math");
  14881. end;
  14882. print(" via @@@@");
  14883. if break_node(r)=null then print_char("0")
  14884. else print_int(serial(break_node(r)));
  14885. print(" b=");
  14886. if b>inf_bad then print_char("*")@+else print_int(b);
  14887. @.*\relax@>
  14888. print(" p="); print_int(pi); print(" d=");
  14889. if artificial_demerits then print_char("*")@+else print_int(d);
  14890. end
  14891. @ @<Print the list between |printed_node| and |cur_p|...@>=
  14892. begin print_nl("");
  14893. if cur_p=null then short_display(link(printed_node))
  14894. else begin save_link:=link(cur_p);
  14895. link(cur_p):=null; print_nl(""); short_display(link(printed_node));
  14896. link(cur_p):=save_link;
  14897. end;
  14898. printed_node:=cur_p;
  14899. end
  14900. @ When the data for a discretionary break is being displayed, we will have
  14901. printed the |pre_break| and |post_break| lists; we want to skip over the
  14902. third list, so that the discretionary data will not appear twice. The
  14903. following code is performed at the very end of |try_break|.
  14904. @<Update the value of |printed_node|...@>=
  14905. if cur_p=printed_node then if cur_p<>null then if type(cur_p)=disc_node then
  14906. begin t:=replace_count(cur_p);
  14907. while t>0 do
  14908. begin decr(t); printed_node:=link(printed_node);
  14909. end;
  14910. end
  14911. @ @<Compute the demerits, |d|, from |r| to |cur_p|@>=
  14912. begin d:=line_penalty+b;
  14913. if abs(d)>=10000 then d:=100000000@+else d:=d*d;
  14914. if pi<>0 then
  14915. if pi>0 then d:=d+pi*pi
  14916. else if pi>eject_penalty then d:=d-pi*pi;
  14917. if (break_type=hyphenated)and(type(r)=hyphenated) then
  14918. if cur_p<>null then d:=d+double_hyphen_demerits
  14919. else d:=d+final_hyphen_demerits;
  14920. if abs(fit_class-fitness(r))>1 then d:=d+adj_demerits;
  14921. end
  14922. @ When an active node disappears, we must delete an adjacent delta node if the
  14923. active node was at the beginning or the end of the active list, or if it
  14924. was surrounded by delta nodes. We also must preserve the property that
  14925. |cur_active_width| represents the length of material from |link(prev_r)|
  14926. to~|cur_p|.
  14927. @d combine_two_deltas(#)==@|mem[prev_r+#].sc:=mem[prev_r+#].sc+mem[r+#].sc
  14928. @d downdate_width(#)==@|cur_active_width[#]:=cur_active_width[#]-
  14929. mem[prev_r+#].sc
  14930. @<Deactivate node |r|@>=
  14931. link(prev_r):=link(r); free_node(r,active_node_size);
  14932. if prev_r=active then @<Update the active widths, since the first active
  14933. node has been deleted@>
  14934. else if type(prev_r)=delta_node then
  14935. begin r:=link(prev_r);
  14936. if r=last_active then
  14937. begin do_all_six(downdate_width);
  14938. link(prev_prev_r):=last_active;
  14939. free_node(prev_r,delta_node_size); prev_r:=prev_prev_r;
  14940. end
  14941. else if type(r)=delta_node then
  14942. begin do_all_six(update_width);
  14943. do_all_six(combine_two_deltas);
  14944. link(prev_r):=link(r); free_node(r,delta_node_size);
  14945. end;
  14946. end
  14947. @ The following code uses the fact that |type(last_active)<>delta_node|. If the
  14948. active list has just become empty, we do not need to update the
  14949. |active_width| array, since it will be initialized when an active
  14950. node is next inserted.
  14951. @d update_active(#)==active_width[#]:=active_width[#]+mem[r+#].sc
  14952. @<Update the active widths,...@>=
  14953. begin r:=link(active);
  14954. if type(r)=delta_node then
  14955. begin do_all_six(update_active);
  14956. do_all_six(copy_to_cur_active);
  14957. link(active):=link(r); free_node(r,delta_node_size);
  14958. end;
  14959. end
  14960. @* \[39] Breaking paragraphs into lines, continued.
  14961. So far we have gotten a little way into the |line_break| routine, having
  14962. covered its important |try_break| subroutine. Now let's consider the
  14963. rest of the process.
  14964. The main loop of |line_break| traverses the given hlist,
  14965. starting at |link(temp_head)|, and calls |try_break| at each legal
  14966. breakpoint. A variable called |auto_breaking| is set to true except
  14967. within math formulas, since glue nodes are not legal breakpoints when
  14968. they appear in formulas.
  14969. The current node of interest in the hlist is pointed to by |cur_p|. Another
  14970. variable, |prev_p|, is usually one step behind |cur_p|, but the real
  14971. meaning of |prev_p| is this: If |type(cur_p)=glue_node| then |cur_p| is a legal
  14972. breakpoint if and only if |auto_breaking| is true and |prev_p| does not
  14973. point to a glue node, penalty node, explicit kern node, or math node.
  14974. The following declarations provide for a few other local variables that are
  14975. used in special calculations.
  14976. @<Local variables for line breaking@>=
  14977. @!auto_breaking:boolean; {is node |cur_p| outside a formula?}
  14978. @!prev_p:pointer; {helps to determine when glue nodes are breakpoints}
  14979. @!q,@!r,@!s,@!prev_s:pointer; {miscellaneous nodes of temporary interest}
  14980. @!f:internal_font_number; {used when calculating character widths}
  14981. @ The `\ignorespaces|loop|\unskip' in the following code is performed at most
  14982. thrice per call of |line_break|, since it is actually a pass over the
  14983. entire paragraph.
  14984. @<Find optimal breakpoints@>=
  14985. threshold:=pretolerance;
  14986. if threshold>=0 then
  14987. begin @!stat if tracing_paragraphs>0 then
  14988. begin begin_diagnostic; print_nl("@@firstpass");@+end;@;@+tats@;@/
  14989. second_pass:=false; final_pass:=false;
  14990. end
  14991. else begin threshold:=tolerance; second_pass:=true;
  14992. final_pass:=(emergency_stretch<=0);
  14993. @!stat if tracing_paragraphs>0 then begin_diagnostic;@+tats@;
  14994. end;
  14995. loop@+ begin if threshold>inf_bad then threshold:=inf_bad;
  14996. if second_pass then @<Initialize for hyphenating a paragraph@>;
  14997. @<Create an active breakpoint representing the beginning of the paragraph@>;
  14998. cur_p:=link(temp_head); auto_breaking:=true;@/
  14999. prev_p:=cur_p; {glue at beginning is not a legal breakpoint}
  15000. while (cur_p<>null)and(link(active)<>last_active) do
  15001. @<Call |try_break| if |cur_p| is a legal breakpoint;
  15002. on the second pass, also try to hyphenate the next
  15003. word, if |cur_p| is a glue node;
  15004. then advance |cur_p| to the next node of the paragraph
  15005. that could possibly be a legal breakpoint@>;
  15006. if cur_p=null then
  15007. @<Try the final line break at the end of the paragraph,
  15008. and |goto done| if the desired breakpoints have been found@>;
  15009. @<Clean up the memory by removing the break nodes@>;
  15010. if not second_pass then
  15011. begin@!stat if tracing_paragraphs>0 then print_nl("@@secondpass");@;@+tats@/
  15012. threshold:=tolerance; second_pass:=true; final_pass:=(emergency_stretch<=0);
  15013. end {if at first you don't succeed, \dots}
  15014. else begin @!stat if tracing_paragraphs>0 then
  15015. print_nl("@@emergencypass");@;@+tats@/
  15016. background[2]:=background[2]+emergency_stretch; final_pass:=true;
  15017. end;
  15018. end;
  15019. done: @!stat if tracing_paragraphs>0 then
  15020. begin end_diagnostic(true); normalize_selector;
  15021. end;@+tats@/
  15022. @ The active node that represents the starting point does not need a
  15023. corresponding passive node.
  15024. @d store_background(#)==active_width[#]:=background[#]
  15025. @<Create an active breakpoint representing the beginning of the paragraph@>=
  15026. q:=get_node(active_node_size);
  15027. type(q):=unhyphenated; fitness(q):=decent_fit;
  15028. link(q):=last_active; break_node(q):=null;
  15029. line_number(q):=prev_graf+1; total_demerits(q):=0; link(active):=q;
  15030. do_all_six(store_background);@/
  15031. passive:=null; printed_node:=temp_head; pass_number:=0;
  15032. font_in_short_display:=null_font
  15033. @ @<Clean...@>=
  15034. q:=link(active);
  15035. while q<>last_active do
  15036. begin cur_p:=link(q);
  15037. if type(q)=delta_node then free_node(q,delta_node_size)
  15038. else free_node(q,active_node_size);
  15039. q:=cur_p;
  15040. end;
  15041. q:=passive;
  15042. while q<>null do
  15043. begin cur_p:=link(q);
  15044. free_node(q,passive_node_size);
  15045. q:=cur_p;
  15046. end
  15047. @ Here is the main switch in the |line_break| routine, where legal breaks
  15048. are determined. As we move through the hlist, we need to keep the |active_width|
  15049. array up to date, so that the badness of individual lines is readily calculated
  15050. by |try_break|. It is convenient to use the short name |act_width| for
  15051. the component of active width that represents real width as opposed to glue.
  15052. @d act_width==active_width[1] {length from first active node to current node}
  15053. @d kern_break==begin if not is_char_node(link(cur_p)) and auto_breaking then
  15054. if type(link(cur_p))=glue_node then try_break(0,unhyphenated);
  15055. act_width:=act_width+width(cur_p);
  15056. end
  15057. @<Call |try_break| if |cur_p| is a legal breakpoint...@>=
  15058. begin if is_char_node(cur_p) then
  15059. @<Advance \(c)|cur_p| to the node following the present
  15060. string of characters@>;
  15061. case type(cur_p) of
  15062. hlist_node,vlist_node,rule_node: act_width:=act_width+width(cur_p);
  15063. whatsit_node: @<Advance \(p)past a whatsit node in the \(l)|line_break| loop@>;
  15064. glue_node: begin @<If node |cur_p| is a legal breakpoint, call |try_break|;
  15065. then update the active widths by including the glue in |glue_ptr(cur_p)|@>;
  15066. if second_pass and auto_breaking then
  15067. @<Try to hyphenate the following word@>;
  15068. end;
  15069. kern_node: if subtype(cur_p)=explicit then kern_break
  15070. else act_width:=act_width+width(cur_p);
  15071. ligature_node: begin f:=font(lig_char(cur_p));
  15072. act_width:=act_width+char_width(f)(char_info(f)(character(lig_char(cur_p))));
  15073. end;
  15074. disc_node: @<Try to break after a discretionary fragment, then |goto done5|@>;
  15075. math_node: begin auto_breaking:=(subtype(cur_p)=after); kern_break;
  15076. end;
  15077. penalty_node: try_break(penalty(cur_p),unhyphenated);
  15078. mark_node,ins_node,adjust_node: do_nothing;
  15079. othercases confusion("paragraph")
  15080. @:this can't happen paragraph}{\quad paragraph@>
  15081. endcases;@/
  15082. prev_p:=cur_p; cur_p:=link(cur_p);
  15083. done5:end
  15084. @ The code that passes over the characters of words in a paragraph is
  15085. part of \TeX's inner loop, so it has been streamlined for speed. We use
  15086. the fact that `\.{\\parfillskip}' glue appears at the end of each paragraph;
  15087. it is therefore unnecessary to check if |link(cur_p)=null| when |cur_p| is a
  15088. character node.
  15089. @^inner loop@>
  15090. @<Advance \(c)|cur_p| to the node following the present string...@>=
  15091. begin prev_p:=cur_p;
  15092. repeat f:=font(cur_p);
  15093. act_width:=act_width+char_width(f)(char_info(f)(character(cur_p)));
  15094. cur_p:=link(cur_p);
  15095. until not is_char_node(cur_p);
  15096. end
  15097. @ When node |cur_p| is a glue node, we look at |prev_p| to see whether or not
  15098. a breakpoint is legal at |cur_p|, as explained above.
  15099. @<If node |cur_p| is a legal breakpoint, call...@>=
  15100. if auto_breaking then
  15101. begin if is_char_node(prev_p) then try_break(0,unhyphenated)
  15102. else if precedes_break(prev_p) then try_break(0,unhyphenated)
  15103. else if (type(prev_p)=kern_node)and(subtype(prev_p)<>explicit) then
  15104. try_break(0,unhyphenated);
  15105. end;
  15106. check_shrinkage(glue_ptr(cur_p)); q:=glue_ptr(cur_p);
  15107. act_width:=act_width+width(q);@|
  15108. active_width[2+stretch_order(q)]:=@|
  15109. active_width[2+stretch_order(q)]+stretch(q);@/
  15110. active_width[6]:=active_width[6]+shrink(q)
  15111. @ The following code knows that discretionary texts contain
  15112. only character nodes, kern nodes, box nodes, rule nodes, and ligature nodes.
  15113. @<Try to break after a discretionary fragment...@>=
  15114. begin s:=pre_break(cur_p); disc_width:=0;
  15115. if s=null then try_break(ex_hyphen_penalty,hyphenated)
  15116. else begin repeat @<Add the width of node |s| to |disc_width|@>;
  15117. s:=link(s);
  15118. until s=null;
  15119. act_width:=act_width+disc_width;
  15120. try_break(hyphen_penalty,hyphenated);
  15121. act_width:=act_width-disc_width;
  15122. end;
  15123. r:=replace_count(cur_p); s:=link(cur_p);
  15124. while r>0 do
  15125. begin @<Add the width of node |s| to |act_width|@>;
  15126. decr(r); s:=link(s);
  15127. end;
  15128. prev_p:=cur_p; cur_p:=s; goto done5;
  15129. end
  15130. @ @<Add the width of node |s| to |disc_width|@>=
  15131. if is_char_node(s) then
  15132. begin f:=font(s);
  15133. disc_width:=disc_width+char_width(f)(char_info(f)(character(s)));
  15134. end
  15135. else case type(s) of
  15136. ligature_node: begin f:=font(lig_char(s));
  15137. disc_width:=disc_width+
  15138. char_width(f)(char_info(f)(character(lig_char(s))));
  15139. end;
  15140. hlist_node,vlist_node,rule_node,kern_node:
  15141. disc_width:=disc_width+width(s);
  15142. othercases confusion("disc3")
  15143. @:this can't happen disc3}{\quad disc3@>
  15144. endcases
  15145. @ @<Add the width of node |s| to |act_width|@>=
  15146. if is_char_node(s) then
  15147. begin f:=font(s);
  15148. act_width:=act_width+char_width(f)(char_info(f)(character(s)));
  15149. end
  15150. else case type(s) of
  15151. ligature_node: begin f:=font(lig_char(s));
  15152. act_width:=act_width+
  15153. char_width(f)(char_info(f)(character(lig_char(s))));
  15154. end;
  15155. hlist_node,vlist_node,rule_node,kern_node:
  15156. act_width:=act_width+width(s);
  15157. othercases confusion("disc4")
  15158. @:this can't happen disc4}{\quad disc4@>
  15159. endcases
  15160. @ The forced line break at the paragraph's end will reduce the list of
  15161. breakpoints so that all active nodes represent breaks at |cur_p=null|.
  15162. On the first pass, we insist on finding an active node that has the
  15163. correct ``looseness.'' On the final pass, there will be at least one active
  15164. node, and we will match the desired looseness as well as we can.
  15165. The global variable |best_bet| will be set to the active node for the best
  15166. way to break the paragraph, and a few other variables are used to
  15167. help determine what is best.
  15168. @<Glob...@>=
  15169. @!best_bet:pointer; {use this passive node and its predecessors}
  15170. @!fewest_demerits:integer; {the demerits associated with |best_bet|}
  15171. @!best_line:halfword; {line number following the last line of the new paragraph}
  15172. @!actual_looseness:integer; {the difference between |line_number(best_bet)|
  15173. and the optimum |best_line|}
  15174. @!line_diff:integer; {the difference between the current line number and
  15175. the optimum |best_line|}
  15176. @ @<Try the final line break at the end of the paragraph...@>=
  15177. begin try_break(eject_penalty,hyphenated);
  15178. if link(active)<>last_active then
  15179. begin @<Find an active node with fewest demerits@>;
  15180. if looseness=0 then goto done;
  15181. @<Find the best active node for the desired looseness@>;
  15182. if (actual_looseness=looseness)or final_pass then goto done;
  15183. end;
  15184. end
  15185. @ @<Find an active node...@>=
  15186. r:=link(active); fewest_demerits:=awful_bad;
  15187. repeat if type(r)<>delta_node then if total_demerits(r)<fewest_demerits then
  15188. begin fewest_demerits:=total_demerits(r); best_bet:=r;
  15189. end;
  15190. r:=link(r);
  15191. until r=last_active;
  15192. best_line:=line_number(best_bet)
  15193. @ The adjustment for a desired looseness is a slightly more complicated
  15194. version of the loop just considered. Note that if a paragraph is broken
  15195. into segments by displayed equations, each segment will be subject to the
  15196. looseness calculation, independently of the other segments.
  15197. @<Find the best active node...@>=
  15198. begin r:=link(active); actual_looseness:=0;
  15199. repeat if type(r)<>delta_node then
  15200. begin line_diff:=line_number(r)-best_line;
  15201. if ((line_diff<actual_looseness)and(looseness<=line_diff))or@|
  15202. ((line_diff>actual_looseness)and(looseness>=line_diff)) then
  15203. begin best_bet:=r; actual_looseness:=line_diff;
  15204. fewest_demerits:=total_demerits(r);
  15205. end
  15206. else if (line_diff=actual_looseness)and@|
  15207. (total_demerits(r)<fewest_demerits) then
  15208. begin best_bet:=r; fewest_demerits:=total_demerits(r);
  15209. end;
  15210. end;
  15211. r:=link(r);
  15212. until r=last_active;
  15213. best_line:=line_number(best_bet);
  15214. end
  15215. @ Once the best sequence of breakpoints has been found (hurray), we call on the
  15216. procedure |post_line_break| to finish the remainder of the work.
  15217. (By introducing this subprocedure, we are able to keep |line_break|
  15218. from getting extremely long.)
  15219. @<Break the paragraph at the chosen...@>=
  15220. post_line_break(final_widow_penalty)
  15221. @ The total number of lines that will be set by |post_line_break|
  15222. is |best_line-prev_graf-1|. The last breakpoint is specified by
  15223. |break_node(best_bet)|, and this passive node points to the other breakpoints
  15224. via the |prev_break| links. The finishing-up phase starts by linking the
  15225. relevant passive nodes in forward order, changing |prev_break| to
  15226. |next_break|. (The |next_break| fields actually reside in the same memory
  15227. space as the |prev_break| fields did, but we give them a new name because
  15228. of their new significance.) Then the lines are justified, one by one.
  15229. @d next_break==prev_break {new name for |prev_break| after links are reversed}
  15230. @<Declare subprocedures for |line_break|@>=
  15231. procedure post_line_break(@!final_widow_penalty:integer);
  15232. label done,done1;
  15233. var q,@!r,@!s:pointer; {temporary registers for list manipulation}
  15234. @!disc_break:boolean; {was the current break at a discretionary node?}
  15235. @!post_disc_break:boolean; {and did it have a nonempty post-break part?}
  15236. @!cur_width:scaled; {width of line number |cur_line|}
  15237. @!cur_indent:scaled; {left margin of line number |cur_line|}
  15238. @!t:quarterword; {used for replacement counts in discretionary nodes}
  15239. @!pen:integer; {use when calculating penalties between lines}
  15240. @!cur_line: halfword; {the current line number being justified}
  15241. begin @<Reverse the links of the relevant passive nodes, setting |cur_p| to the
  15242. first breakpoint@>;
  15243. cur_line:=prev_graf+1;
  15244. repeat @<Justify the line ending at breakpoint |cur_p|, and append it to the
  15245. current vertical list, together with associated penalties and other
  15246. insertions@>;
  15247. incr(cur_line); cur_p:=next_break(cur_p);
  15248. if cur_p<>null then if not post_disc_break then
  15249. @<Prune unwanted nodes at the beginning of the next line@>;
  15250. until cur_p=null;
  15251. if (cur_line<>best_line)or(link(temp_head)<>null) then
  15252. confusion("line breaking");
  15253. @:this can't happen line breaking}{\quad line breaking@>
  15254. prev_graf:=best_line-1;
  15255. end;
  15256. @ The job of reversing links in a list is conveniently regarded as the job
  15257. of taking items off one stack and putting them on another. In this case we
  15258. take them off a stack pointed to by |q| and having |prev_break| fields;
  15259. we put them on a stack pointed to by |cur_p| and having |next_break| fields.
  15260. Node |r| is the passive node being moved from stack to stack.
  15261. @<Reverse the links of the relevant passive nodes...@>=
  15262. q:=break_node(best_bet); cur_p:=null;
  15263. repeat r:=q; q:=prev_break(q); next_break(r):=cur_p; cur_p:=r;
  15264. until q=null
  15265. @ Glue and penalty and kern and math nodes are deleted at the beginning of
  15266. a line, except in the anomalous case that the node to be deleted is actually
  15267. one of the chosen breakpoints. Otherwise
  15268. the pruning done here is designed to match
  15269. the lookahead computation in |try_break|, where the |break_width| values
  15270. are computed for non-discretionary breakpoints.
  15271. @<Prune unwanted nodes at the beginning of the next line@>=
  15272. begin r:=temp_head;
  15273. loop@+ begin q:=link(r);
  15274. if q=cur_break(cur_p) then goto done1;
  15275. {|cur_break(cur_p)| is the next breakpoint}
  15276. {now |q| cannot be |null|}
  15277. if is_char_node(q) then goto done1;
  15278. if non_discardable(q) then goto done1;
  15279. if type(q)=kern_node then if subtype(q)<>explicit then goto done1;
  15280. r:=q; {now |type(q)=glue_node|, |kern_node|, |math_node|, or |penalty_node|}
  15281. end;
  15282. done1: if r<>temp_head then
  15283. begin link(r):=null; flush_node_list(link(temp_head));
  15284. link(temp_head):=q;
  15285. end;
  15286. end
  15287. @ The current line to be justified appears in a horizontal list starting
  15288. at |link(temp_head)| and ending at |cur_break(cur_p)|. If |cur_break(cur_p)| is
  15289. a glue node, we reset the glue to equal the |right_skip| glue; otherwise
  15290. we append the |right_skip| glue at the right. If |cur_break(cur_p)| is a
  15291. discretionary node, we modify the list so that the discretionary break
  15292. is compulsory, and we set |disc_break| to |true|. We also append
  15293. the |left_skip| glue at the left of the line, unless it is zero.
  15294. @<Justify the line ending at breakpoint |cur_p|, and append it...@>=
  15295. @<Modify the end of the line to reflect the nature of the break and to include
  15296. \.{\\rightskip}; also set the proper value of |disc_break|@>;
  15297. @<Put the \(l)\.{\\leftskip} glue at the left and detach this line@>;
  15298. @<Call the packaging subroutine, setting |just_box| to the justified box@>;
  15299. @<Append the new box to the current vertical list, followed by the list of
  15300. special nodes taken out of the box by the packager@>;
  15301. @<Append a penalty node, if a nonzero penalty is appropriate@>
  15302. @ At the end of the following code, |q| will point to the final node on the
  15303. list about to be justified.
  15304. @<Modify the end of the line...@>=
  15305. q:=cur_break(cur_p); disc_break:=false; post_disc_break:=false;
  15306. if q<>null then {|q| cannot be a |char_node|}
  15307. if type(q)=glue_node then
  15308. begin delete_glue_ref(glue_ptr(q));
  15309. glue_ptr(q):=right_skip;
  15310. subtype(q):=right_skip_code+1; add_glue_ref(right_skip);
  15311. goto done;
  15312. end
  15313. else begin if type(q)=disc_node then
  15314. @<Change discretionary to compulsory and set
  15315. |disc_break:=true|@>
  15316. else if (type(q)=math_node)or(type(q)=kern_node) then width(q):=0;
  15317. end
  15318. else begin q:=temp_head;
  15319. while link(q)<>null do q:=link(q);
  15320. end;
  15321. @<Put the \(r)\.{\\rightskip} glue after node |q|@>;
  15322. done:
  15323. @ @<Change discretionary to compulsory...@>=
  15324. begin t:=replace_count(q);
  15325. @<Destroy the |t| nodes following |q|, and
  15326. make |r| point to the following node@>;
  15327. if post_break(q)<>null then @<Transplant the post-break list@>;
  15328. if pre_break(q)<>null then @<Transplant the pre-break list@>;
  15329. link(q):=r; disc_break:=true;
  15330. end
  15331. @ @<Destroy the |t| nodes following |q|...@>=
  15332. if t=0 then r:=link(q)
  15333. else begin r:=q;
  15334. while t>1 do
  15335. begin r:=link(r); decr(t);
  15336. end;
  15337. s:=link(r);
  15338. r:=link(s); link(s):=null;
  15339. flush_node_list(link(q)); replace_count(q):=0;
  15340. end
  15341. @ We move the post-break list from inside node |q| to the main list by
  15342. re\-attaching it just before the present node |r|, then resetting |r|.
  15343. @<Transplant the post-break list@>=
  15344. begin s:=post_break(q);
  15345. while link(s)<>null do s:=link(s);
  15346. link(s):=r; r:=post_break(q); post_break(q):=null; post_disc_break:=true;
  15347. end
  15348. @ We move the pre-break list from inside node |q| to the main list by
  15349. re\-attaching it just after the present node |q|, then resetting |q|.
  15350. @<Transplant the pre-break list@>=
  15351. begin s:=pre_break(q); link(q):=s;
  15352. while link(s)<>null do s:=link(s);
  15353. pre_break(q):=null; q:=s;
  15354. end
  15355. @ @<Put the \(r)\.{\\rightskip} glue after node |q|@>=
  15356. r:=new_param_glue(right_skip_code); link(r):=link(q); link(q):=r; q:=r
  15357. @ The following code begins with |q| at the end of the list to be
  15358. justified. It ends with |q| at the beginning of that list, and with
  15359. |link(temp_head)| pointing to the remainder of the paragraph, if any.
  15360. @<Put the \(l)\.{\\leftskip} glue at the left...@>=
  15361. r:=link(q); link(q):=null; q:=link(temp_head); link(temp_head):=r;
  15362. if left_skip<>zero_glue then
  15363. begin r:=new_param_glue(left_skip_code);
  15364. link(r):=q; q:=r;
  15365. end
  15366. @ @<Append the new box to the current vertical list...@>=
  15367. append_to_vlist(just_box);
  15368. if adjust_head<>adjust_tail then
  15369. begin link(tail):=link(adjust_head); tail:=adjust_tail;
  15370. end;
  15371. adjust_tail:=null
  15372. @ Now |q| points to the hlist that represents the current line of the
  15373. paragraph. We need to compute the appropriate line width, pack the
  15374. line into a box of this size, and shift the box by the appropriate
  15375. amount of indentation.
  15376. @<Call the packaging subroutine...@>=
  15377. if cur_line>last_special_line then
  15378. begin cur_width:=second_width; cur_indent:=second_indent;
  15379. end
  15380. else if par_shape_ptr=null then
  15381. begin cur_width:=first_width; cur_indent:=first_indent;
  15382. end
  15383. else begin cur_width:=mem[par_shape_ptr+2*cur_line].sc;
  15384. cur_indent:=mem[par_shape_ptr+2*cur_line-1].sc;
  15385. end;
  15386. adjust_tail:=adjust_head; just_box:=hpack(q,cur_width,exactly);
  15387. shift_amount(just_box):=cur_indent
  15388. @ Penalties between the lines of a paragraph come from club and widow lines,
  15389. from the |inter_line_penalty| parameter, and from lines that end at
  15390. discretionary breaks. Breaking between lines of a two-line paragraph gets
  15391. both club-line and widow-line penalties. The local variable |pen| will
  15392. be set to the sum of all relevant penalties for the current line, except
  15393. that the final line is never penalized.
  15394. @<Append a penalty node, if a nonzero penalty is appropriate@>=
  15395. if cur_line+1<>best_line then
  15396. begin pen:=inter_line_penalty;
  15397. if cur_line=prev_graf+1 then pen:=pen+club_penalty;
  15398. if cur_line+2=best_line then pen:=pen+final_widow_penalty;
  15399. if disc_break then pen:=pen+broken_penalty;
  15400. if pen<>0 then
  15401. begin r:=new_penalty(pen);
  15402. link(tail):=r; tail:=r;
  15403. end;
  15404. end
  15405. @* \[40] Pre-hyphenation.
  15406. When the line-breaking routine is unable to find a feasible sequence of
  15407. breakpoints, it makes a second pass over the paragraph, attempting to
  15408. hyphenate the hyphenatable words. The goal of hyphenation is to insert
  15409. discretionary material into the paragraph so that there are more
  15410. potential places to break.
  15411. The general rules for hyphenation are somewhat complex and technical,
  15412. because we want to be able to hyphenate words that are preceded or
  15413. followed by punctuation marks, and because we want the rules to work
  15414. for languages other than English. We also must contend with the fact
  15415. that hyphens might radically alter the ligature and kerning structure
  15416. of a word.
  15417. A sequence of characters will be considered for hyphenation only if it
  15418. belongs to a ``potentially hyphenatable part'' of the current paragraph.
  15419. This is a sequence of nodes $p_0p_1\ldots p_m$ where $p_0$ is a glue node,
  15420. $p_1\ldots p_{m-1}$ are either character or ligature or whatsit or
  15421. implicit kern nodes, and $p_m$ is a glue or penalty or insertion or adjust
  15422. or mark or whatsit or explicit kern node. (Therefore hyphenation is
  15423. disabled by boxes, math formulas, and discretionary nodes already inserted
  15424. by the user.) The ligature nodes among $p_1\ldots p_{m-1}$ are effectively
  15425. expanded into the original non-ligature characters; the kern nodes and
  15426. whatsits are ignored. Each character |c| is now classified as either a
  15427. nonletter (if |lc_code(c)=0|), a lowercase letter (if
  15428. |lc_code(c)=c|), or an uppercase letter (otherwise); an uppercase letter
  15429. is treated as if it were |lc_code(c)| for purposes of hyphenation. The
  15430. characters generated by $p_1\ldots p_{m-1}$ may begin with nonletters; let
  15431. $c_1$ be the first letter that is not in the middle of a ligature. Whatsit
  15432. nodes preceding $c_1$ are ignored; a whatsit found after $c_1$ will be the
  15433. terminating node $p_m$. All characters that do not have the same font as
  15434. $c_1$ will be treated as nonletters. The |hyphen_char| for that font
  15435. must be between 0 and 255, otherwise hyphenation will not be attempted.
  15436. \TeX\ looks ahead for as many consecutive letters $c_1\ldots c_n$ as
  15437. possible; however, |n| must be less than 64, so a character that would
  15438. otherwise be $c_{64}$ is effectively not a letter. Furthermore $c_n$ must
  15439. not be in the middle of a ligature. In this way we obtain a string of
  15440. letters $c_1\ldots c_n$ that are generated by nodes $p_a\ldots p_b$, where
  15441. |1<=a<=b+1<=m|. If |n>=l_hyf+r_hyf|, this string qualifies for hyphenation;
  15442. however, |uc_hyph| must be positive, if $c_1$ is uppercase.
  15443. The hyphenation process takes place in three stages. First, the candidate
  15444. sequence $c_1\ldots c_n$ is found; then potential positions for hyphens
  15445. are determined by referring to hyphenation tables; and finally, the nodes
  15446. $p_a\ldots p_b$ are replaced by a new sequence of nodes that includes the
  15447. discretionary breaks found.
  15448. Fortunately, we do not have to do all this calculation very often, because
  15449. of the way it has been taken out of \TeX's inner loop. For example, when
  15450. the second edition of the author's 700-page book {\sl Seminumerical
  15451. Algorithms} was typeset by \TeX, only about 1.2 hyphenations needed to be
  15452. @^Knuth, Donald Ervin@>
  15453. tried per paragraph, since the line breaking algorithm needed to use two
  15454. passes on only about 5 per cent of the paragraphs.
  15455. @<Initialize for hyphenating...@>=
  15456. begin @!init if trie_not_ready then init_trie;@+tini@;@/
  15457. cur_lang:=init_cur_lang; l_hyf:=init_l_hyf; r_hyf:=init_r_hyf;
  15458. end
  15459. @ The letters $c_1\ldots c_n$ that are candidates for hyphenation are placed
  15460. into an array called |hc|; the number |n| is placed into |hn|; pointers to
  15461. nodes $p_{a-1}$ and~$p_b$ in the description above are placed into variables
  15462. |ha| and |hb|; and the font number is placed into |hf|.
  15463. @<Glob...@>=
  15464. @!hc:array[0..65] of 0..256; {word to be hyphenated}
  15465. @!hn:0..64; {the number of positions occupied in |hc|;
  15466. not always a |small_number|}
  15467. @!ha,@!hb:pointer; {nodes |ha..hb| should be replaced by the hyphenated result}
  15468. @!hf:internal_font_number; {font number of the letters in |hc|}
  15469. @!hu:array[0..63] of 0..256; {like |hc|, before conversion to lowercase}
  15470. @!hyf_char:integer; {hyphen character of the relevant font}
  15471. @!cur_lang,@!init_cur_lang:ASCII_code; {current hyphenation table of interest}
  15472. @!l_hyf,@!r_hyf,@!init_l_hyf,@!init_r_hyf:integer; {limits on fragment sizes}
  15473. @!hyf_bchar:halfword; {boundary character after $c_n$}
  15474. @ Hyphenation routines need a few more local variables.
  15475. @<Local variables for line...@>=
  15476. @!j:small_number; {an index into |hc| or |hu|}
  15477. @!c:0..255; {character being considered for hyphenation}
  15478. @ When the following code is activated, the |line_break| procedure is in its
  15479. second pass, and |cur_p| points to a glue node.
  15480. @<Try to hyphenate...@>=
  15481. begin prev_s:=cur_p; s:=link(prev_s);
  15482. if s<>null then
  15483. begin @<Skip to node |ha|, or |goto done1| if no hyphenation
  15484. should be attempted@>;
  15485. if l_hyf+r_hyf>63 then goto done1;
  15486. @<Skip to node |hb|, putting letters into |hu| and |hc|@>;
  15487. @<Check that the nodes following |hb| permit hyphenation and that at least
  15488. |l_hyf+r_hyf| letters have been found, otherwise |goto done1|@>;
  15489. hyphenate;
  15490. end;
  15491. done1: end
  15492. @ @<Declare subprocedures for |line_break|@>=
  15493. @t\4@>@<Declare the function called |reconstitute|@>
  15494. procedure hyphenate;
  15495. label common_ending,done,found,found1,found2,not_found,exit;
  15496. var @<Local variables for hyphenation@>@;
  15497. begin @<Find hyphen locations for the word in |hc|, or |return|@>;
  15498. @<If no hyphens were found, |return|@>;
  15499. @<Replace nodes |ha..hb| by a sequence of nodes that includes
  15500. the discretionary hyphens@>;
  15501. exit:end;
  15502. @ The first thing we need to do is find the node |ha| just before the
  15503. first letter.
  15504. @<Skip to node |ha|, or |goto done1|...@>=
  15505. loop@+ begin if is_char_node(s) then
  15506. begin c:=qo(character(s)); hf:=font(s);
  15507. end
  15508. else if type(s)=ligature_node then
  15509. if lig_ptr(s)=null then goto continue
  15510. else begin q:=lig_ptr(s); c:=qo(character(q)); hf:=font(q);
  15511. end
  15512. else if (type(s)=kern_node)and(subtype(s)=normal) then goto continue
  15513. else if type(s)=whatsit_node then
  15514. begin @<Advance \(p)past a whatsit node in the \(p)pre-hyphenation loop@>;
  15515. goto continue;
  15516. end
  15517. else goto done1;
  15518. if lc_code(c)<>0 then
  15519. if (lc_code(c)=c)or(uc_hyph>0) then goto done2
  15520. else goto done1;
  15521. continue: prev_s:=s; s:=link(prev_s);
  15522. end;
  15523. done2: hyf_char:=hyphen_char[hf];
  15524. if hyf_char<0 then goto done1;
  15525. if hyf_char>255 then goto done1;
  15526. ha:=prev_s
  15527. @ The word to be hyphenated is now moved to the |hu| and |hc| arrays.
  15528. @<Skip to node |hb|, putting letters...@>=
  15529. hn:=0;
  15530. loop@+ begin if is_char_node(s) then
  15531. begin if font(s)<>hf then goto done3;
  15532. hyf_bchar:=character(s); c:=qo(hyf_bchar);
  15533. if lc_code(c)=0 then goto done3;
  15534. if hn=63 then goto done3;
  15535. hb:=s; incr(hn); hu[hn]:=c; hc[hn]:=lc_code(c); hyf_bchar:=non_char;
  15536. end
  15537. else if type(s)=ligature_node then
  15538. @<Move the characters of a ligature node to |hu| and |hc|;
  15539. but |goto done3| if they are not all letters@>
  15540. else if (type(s)=kern_node)and(subtype(s)=normal) then
  15541. begin hb:=s;
  15542. hyf_bchar:=font_bchar[hf];
  15543. end
  15544. else goto done3;
  15545. s:=link(s);
  15546. end;
  15547. done3:
  15548. @ We let |j| be the index of the character being stored when a ligature node
  15549. is being expanded, since we do not want to advance |hn| until we are sure
  15550. that the entire ligature consists of letters. Note that it is possible
  15551. to get to |done3| with |hn=0| and |hb| not set to any value.
  15552. @<Move the characters of a ligature node to |hu| and |hc|...@>=
  15553. begin if font(lig_char(s))<>hf then goto done3;
  15554. j:=hn; q:=lig_ptr(s);@+if q>null then hyf_bchar:=character(q);
  15555. while q>null do
  15556. begin c:=qo(character(q));
  15557. if lc_code(c)=0 then goto done3;
  15558. if j=63 then goto done3;
  15559. incr(j); hu[j]:=c; hc[j]:=lc_code(c);@/
  15560. q:=link(q);
  15561. end;
  15562. hb:=s; hn:=j;
  15563. if odd(subtype(s)) then hyf_bchar:=font_bchar[hf]@+else hyf_bchar:=non_char;
  15564. end
  15565. @ @<Check that the nodes following |hb| permit hyphenation...@>=
  15566. if hn<l_hyf+r_hyf then goto done1; {|l_hyf| and |r_hyf| are |>=1|}
  15567. loop@+ begin if not(is_char_node(s)) then
  15568. case type(s) of
  15569. ligature_node: do_nothing;
  15570. kern_node: if subtype(s)<>normal then goto done4;
  15571. whatsit_node,glue_node,penalty_node,ins_node,adjust_node,mark_node:
  15572. goto done4;
  15573. othercases goto done1
  15574. endcases;
  15575. s:=link(s);
  15576. end;
  15577. done4:
  15578. @* \[41] Post-hyphenation.
  15579. If a hyphen may be inserted between |hc[j]| and |hc[j+1]|, the hyphenation
  15580. procedure will set |hyf[j]| to some small odd number. But before we look
  15581. at \TeX's hyphenation procedure, which is independent of the rest of the
  15582. line-breaking algorithm, let us consider what we will do with the hyphens
  15583. it finds, since it is better to work on this part of the program before
  15584. forgetting what |ha| and |hb|, etc., are all about.
  15585. @<Glob...@>=
  15586. @!hyf:array [0..64] of 0..9; {odd values indicate discretionary hyphens}
  15587. @!init_list:pointer; {list of punctuation characters preceding the word}
  15588. @!init_lig:boolean; {does |init_list| represent a ligature?}
  15589. @!init_lft:boolean; {if so, did the ligature involve a left boundary?}
  15590. @ @<Local variables for hyphenation@>=
  15591. @!i,@!j,@!l:0..65; {indices into |hc| or |hu|}
  15592. @!q,@!r,@!s:pointer; {temporary registers for list manipulation}
  15593. @!bchar:halfword; {boundary character of hyphenated word, or |non_char|}
  15594. @ \TeX\ will never insert a hyphen that has fewer than
  15595. \.{\\lefthyphenmin} letters before it or fewer than
  15596. \.{\\righthyphenmin} after it; hence, a short word has
  15597. comparatively little chance of being hyphenated. If no hyphens have
  15598. been found, we can save time by not having to make any changes to the
  15599. paragraph.
  15600. @<If no hyphens were found, |return|@>=
  15601. for j:=l_hyf to hn-r_hyf do if odd(hyf[j]) then goto found1;
  15602. return;
  15603. found1:
  15604. @ If hyphens are in fact going to be inserted, \TeX\ first deletes the
  15605. subsequence of nodes between |ha| and~|hb|. An attempt is made to
  15606. preserve the effect that implicit boundary characters and punctuation marks
  15607. had on ligatures inside the hyphenated word, by storing a left boundary or
  15608. preceding character in |hu[0]| and by storing a possible right boundary
  15609. in |bchar|. We set |j:=0| if |hu[0]| is to be part of the reconstruction;
  15610. otherwise |j:=1|.
  15611. The variable |s| will point to the tail of the current hlist, and
  15612. |q| will point to the node following |hb|, so that
  15613. things can be hooked up after we reconstitute the hyphenated word.
  15614. @<Replace nodes |ha..hb| by a sequence of nodes...@>=
  15615. q:=link(hb); link(hb):=null; r:=link(ha); link(ha):=null; bchar:=hyf_bchar;
  15616. if is_char_node(ha) then
  15617. if font(ha)<>hf then goto found2
  15618. else begin init_list:=ha; init_lig:=false; hu[0]:=qo(character(ha));
  15619. end
  15620. else if type(ha)=ligature_node then
  15621. if font(lig_char(ha))<>hf then goto found2
  15622. else begin init_list:=lig_ptr(ha); init_lig:=true; init_lft:=(subtype(ha)>1);
  15623. hu[0]:=qo(character(lig_char(ha)));
  15624. if init_list=null then if init_lft then
  15625. begin hu[0]:=256; init_lig:=false;
  15626. end; {in this case a ligature will be reconstructed from scratch}
  15627. free_node(ha,small_node_size);
  15628. end
  15629. else begin {no punctuation found; look for left boundary}
  15630. if not is_char_node(r) then if type(r)=ligature_node then
  15631. if subtype(r)>1 then goto found2;
  15632. j:=1; s:=ha; init_list:=null; goto common_ending;
  15633. end;
  15634. s:=cur_p; {we have |cur_p<>ha| because |type(cur_p)=glue_node|}
  15635. while link(s)<>ha do s:=link(s);
  15636. j:=0; goto common_ending;
  15637. found2: s:=ha; j:=0; hu[0]:=256; init_lig:=false; init_list:=null;
  15638. common_ending: flush_node_list(r);
  15639. @<Reconstitute nodes for the hyphenated word, inserting discretionary hyphens@>;
  15640. flush_list(init_list)
  15641. @ We must now face the fact that the battle is not over, even though the
  15642. {\def\!{\kern-1pt}%
  15643. hyphens have been found: The process of reconstituting a word can be nontrivial
  15644. because ligatures might change when a hyphen is present. {\sl The \TeX book\/}
  15645. discusses the difficulties of the word ``difficult'', and
  15646. the discretionary material surrounding a
  15647. hyphen can be considerably more complex than that. Suppose
  15648. \.{abcdef} is a word in a font for which the only ligatures are \.{b\!c},
  15649. \.{c\!d}, \.{d\!e}, and \.{e\!f}. If this word permits hyphenation
  15650. between \.b and \.c, the two patterns with and without hyphenation are
  15651. $\.a\,\.b\,\.-\,\.{c\!d}\,\.{e\!f}$ and $\.a\,\.{b\!c}\,\.{d\!e}\,\.f$.
  15652. Thus the insertion of a hyphen might cause effects to ripple arbitrarily
  15653. far into the rest of the word. A further complication arises if additional
  15654. hyphens appear together with such rippling, e.g., if the word in the
  15655. example just given could also be hyphenated between \.c and \.d; \TeX\
  15656. avoids this by simply ignoring the additional hyphens in such weird cases.}
  15657. Still further complications arise in the presence of ligatures that do not
  15658. delete the original characters. When punctuation precedes the word being
  15659. hyphenated, \TeX's method is not perfect under all possible scenarios,
  15660. because punctuation marks and letters can propagate information back and forth.
  15661. For example, suppose the original pre-hyphenation pair
  15662. \.{*a} changes to \.{*y} via a \.{\?=:} ligature, which changes to \.{xy}
  15663. via a \.{=:\?} ligature; if $p_{a-1}=\.x$ and $p_a=\.y$, the reconstitution
  15664. procedure isn't smart enough to obtain \.{xy} again. In such cases the
  15665. font designer should include a ligature that goes from \.{xa} to \.{xy}.
  15666. @ The processing is facilitated by a subroutine called |reconstitute|. Given
  15667. a string of characters $x_j\ldots x_n$, there is a smallest index $m\ge j$
  15668. such that the ``translation'' of $x_j\ldots x_n$ by ligatures and kerning
  15669. has the form $y_1\ldots y_t$ followed by the translation of $x_{m+1}\ldots x_n$,
  15670. where $y_1\ldots y_t$ is some nonempty sequence of character, ligature, and
  15671. kern nodes. We call $x_j\ldots x_m$ a ``cut prefix'' of $x_j\ldots x_n$.
  15672. For example, if $x_1x_2x_3=\.{fly}$, and if the font contains `fl' as a
  15673. ligature and a kern between `fl' and `y', then $m=2$, $t=2$, and $y_1$ will
  15674. be a ligature node for `fl' followed by an appropriate kern node~$y_2$.
  15675. In the most common case, $x_j$~forms no ligature with $x_{j+1}$ and we
  15676. simply have $m=j$, $y_1=x_j$. If $m<n$ we can repeat the procedure on
  15677. $x_{m+1}\ldots x_n$ until the entire translation has been found.
  15678. The |reconstitute| function returns the integer $m$ and puts the nodes
  15679. $y_1\ldots y_t$ into a linked list starting at |link(hold_head)|,
  15680. getting the input $x_j\ldots x_n$ from the |hu| array. If $x_j=256$,
  15681. we consider $x_j$ to be an implicit left boundary character; in this
  15682. case |j| must be strictly less than~|n|. There is a
  15683. parameter |bchar|, which is either 256 or an implicit right boundary character
  15684. assumed to be present just following~$x_n$. (The value |hu[n+1]| is never
  15685. explicitly examined, but the algorithm imagines that |bchar| is there.)
  15686. If there exists an index |k| in the range $j\le k\le m$ such that |hyf[k]|
  15687. is odd and such that the result of |reconstitute| would have been different
  15688. if $x_{k+1}$ had been |hchar|, then |reconstitute| sets |hyphen_passed|
  15689. to the smallest such~|k|. Otherwise it sets |hyphen_passed| to zero.
  15690. A special convention is used in the case |j=0|: Then we assume that the
  15691. translation of |hu[0]| appears in a special list of charnodes starting at
  15692. |init_list|; moreover, if |init_lig| is |true|, then |hu[0]| will be
  15693. a ligature character, involving a left boundary if |init_lft| is |true|.
  15694. This facility is provided for cases when a hyphenated
  15695. word is preceded by punctuation (like single or double quotes) that might
  15696. affect the translation of the beginning of the word.
  15697. @<Glob...@>=
  15698. @!hyphen_passed:small_number; {first hyphen in a ligature, if any}
  15699. @ @<Declare the function called |reconstitute|@>=
  15700. function reconstitute(@!j,@!n:small_number;@!bchar,@!hchar:halfword):
  15701. small_number;
  15702. label continue,done;
  15703. var @!p:pointer; {temporary register for list manipulation}
  15704. @!t:pointer; {a node being appended to}
  15705. @!q:four_quarters; {character information or a lig/kern instruction}
  15706. @!cur_rh:halfword; {hyphen character for ligature testing}
  15707. @!test_char:halfword; {hyphen or other character for ligature testing}
  15708. @!w:scaled; {amount of kerning}
  15709. @!k:font_index; {position of current lig/kern instruction}
  15710. begin hyphen_passed:=0; t:=hold_head; w:=0; link(hold_head):=null;
  15711. {at this point |ligature_present=lft_hit=rt_hit=false|}
  15712. @<Set up data structures with the cursor following position |j|@>;
  15713. continue:@<If there's a ligature or kern at the cursor position, update the data
  15714. structures, possibly advancing~|j|; continue until the cursor moves@>;
  15715. @<Append a ligature and/or kern to the translation;
  15716. |goto continue| if the stack of inserted ligatures is nonempty@>;
  15717. reconstitute:=j;
  15718. end;
  15719. @ The reconstitution procedure shares many of the global data structures
  15720. by which \TeX\ has processed the words before they were hyphenated.
  15721. There is an implied ``cursor'' between characters |cur_l| and |cur_r|;
  15722. these characters will be tested for possible ligature activity. If
  15723. |ligature_present| then |cur_l| is a ligature character formed from the
  15724. original characters following |cur_q| in the current translation list.
  15725. There is a ``ligature stack'' between the cursor and character |j+1|,
  15726. consisting of pseudo-ligature nodes linked together by their |link| fields.
  15727. This stack is normally empty unless a ligature command has created a new
  15728. character that will need to be processed later. A pseudo-ligature is
  15729. a special node having a |character| field that represents a potential
  15730. ligature and a |lig_ptr| field that points to a |char_node| or is |null|.
  15731. We have
  15732. $$|cur_r|=\cases{|character(lig_stack)|,&if |lig_stack>null|;\cr
  15733. |qi(hu[j+1])|,&if |lig_stack=null| and |j<n|;\cr
  15734. bchar,&if |lig_stack=null| and |j=n|.\cr}$$
  15735. @<Glob...@>=
  15736. @!cur_l,@!cur_r:halfword; {characters before and after the cursor}
  15737. @!cur_q:pointer; {where a ligature should be detached}
  15738. @!lig_stack:pointer; {unfinished business to the right of the cursor}
  15739. @!ligature_present:boolean; {should a ligature node be made for |cur_l|?}
  15740. @!lft_hit,@!rt_hit:boolean; {did we hit a ligature with a boundary character?}
  15741. @ @d append_charnode_to_t(#)== begin link(t):=get_avail; t:=link(t);
  15742. font(t):=hf; character(t):=#;
  15743. end
  15744. @d set_cur_r==begin if j<n then cur_r:=qi(hu[j+1])@+else cur_r:=bchar;
  15745. if odd(hyf[j]) then cur_rh:=hchar@+else cur_rh:=non_char;
  15746. end
  15747. @<Set up data structures with the cursor following position |j|@>=
  15748. cur_l:=qi(hu[j]); cur_q:=t;
  15749. if j=0 then
  15750. begin ligature_present:=init_lig; p:=init_list;
  15751. if ligature_present then lft_hit:=init_lft;
  15752. while p>null do
  15753. begin append_charnode_to_t(character(p)); p:=link(p);
  15754. end;
  15755. end
  15756. else if cur_l<non_char then append_charnode_to_t(cur_l);
  15757. lig_stack:=null; set_cur_r
  15758. @ We may want to look at the lig/kern program twice, once for a hyphen
  15759. and once for a normal letter. (The hyphen might appear after the letter
  15760. in the program, so we'd better not try to look for both at once.)
  15761. @<If there's a ligature or kern at the cursor position, update...@>=
  15762. if cur_l=non_char then
  15763. begin k:=bchar_label[hf];
  15764. if k=non_address then goto done@+else q:=font_info[k].qqqq;
  15765. end
  15766. else begin q:=char_info(hf)(cur_l);
  15767. if char_tag(q)<>lig_tag then goto done;
  15768. k:=lig_kern_start(hf)(q); q:=font_info[k].qqqq;
  15769. if skip_byte(q)>stop_flag then
  15770. begin k:=lig_kern_restart(hf)(q); q:=font_info[k].qqqq;
  15771. end;
  15772. end; {now |k| is the starting address of the lig/kern program}
  15773. if cur_rh<non_char then test_char:=cur_rh@+else test_char:=cur_r;
  15774. loop@+begin if next_char(q)=test_char then if skip_byte(q)<=stop_flag then
  15775. if cur_rh<non_char then
  15776. begin hyphen_passed:=j; hchar:=non_char; cur_rh:=non_char;
  15777. goto continue;
  15778. end
  15779. else begin if hchar<non_char then if odd(hyf[j]) then
  15780. begin hyphen_passed:=j; hchar:=non_char;
  15781. end;
  15782. if op_byte(q)<kern_flag then
  15783. @<Carry out a ligature replacement, updating the cursor structure
  15784. and possibly advancing~|j|; |goto continue| if the cursor doesn't
  15785. advance, otherwise |goto done|@>;
  15786. w:=char_kern(hf)(q); goto done; {this kern will be inserted below}
  15787. end;
  15788. if skip_byte(q)>=stop_flag then
  15789. if cur_rh=non_char then goto done
  15790. else begin cur_rh:=non_char; goto continue;
  15791. end;
  15792. k:=k+qo(skip_byte(q))+1; q:=font_info[k].qqqq;
  15793. end;
  15794. done:
  15795. @ @d wrap_lig(#)==if ligature_present then
  15796. begin p:=new_ligature(hf,cur_l,link(cur_q));
  15797. if lft_hit then
  15798. begin subtype(p):=2; lft_hit:=false;
  15799. end;
  15800. if # then if lig_stack=null then
  15801. begin incr(subtype(p)); rt_hit:=false;
  15802. end;
  15803. link(cur_q):=p; t:=p; ligature_present:=false;
  15804. end
  15805. @d pop_lig_stack==begin if lig_ptr(lig_stack)>null then
  15806. begin link(t):=lig_ptr(lig_stack); {this is a charnode for |hu[j+1]|}
  15807. t:=link(t); incr(j);
  15808. end;
  15809. p:=lig_stack; lig_stack:=link(p); free_node(p,small_node_size);
  15810. if lig_stack=null then set_cur_r@+else cur_r:=character(lig_stack);
  15811. end {if |lig_stack| isn't |null| we have |cur_rh=non_char|}
  15812. @<Append a ligature and/or kern to the translation...@>=
  15813. wrap_lig(rt_hit);
  15814. if w<>0 then
  15815. begin link(t):=new_kern(w); t:=link(t); w:=0;
  15816. end;
  15817. if lig_stack>null then
  15818. begin cur_q:=t; cur_l:=character(lig_stack); ligature_present:=true;
  15819. pop_lig_stack; goto continue;
  15820. end
  15821. @ @<Carry out a ligature replacement, updating the cursor structure...@>=
  15822. begin if cur_l=non_char then lft_hit:=true;
  15823. if j=n then if lig_stack=null then rt_hit:=true;
  15824. check_interrupt; {allow a way out in case there's an infinite ligature loop}
  15825. case op_byte(q) of
  15826. qi(1),qi(5):begin cur_l:=rem_byte(q); {\.{=:\?}, \.{=:\?>}}
  15827. ligature_present:=true;
  15828. end;
  15829. qi(2),qi(6):begin cur_r:=rem_byte(q); {\.{\?=:}, \.{\?=:>}}
  15830. if lig_stack>null then character(lig_stack):=cur_r
  15831. else begin lig_stack:=new_lig_item(cur_r);
  15832. if j=n then bchar:=non_char
  15833. else begin p:=get_avail; lig_ptr(lig_stack):=p;
  15834. character(p):=qi(hu[j+1]); font(p):=hf;
  15835. end;
  15836. end;
  15837. end;
  15838. qi(3):begin cur_r:=rem_byte(q); {\.{\?=:\?}}
  15839. p:=lig_stack; lig_stack:=new_lig_item(cur_r); link(lig_stack):=p;
  15840. end;
  15841. qi(7),qi(11):begin wrap_lig(false); {\.{\?=:\?>}, \.{\?=:\?>>}}
  15842. cur_q:=t; cur_l:=rem_byte(q); ligature_present:=true;
  15843. end;
  15844. othercases begin cur_l:=rem_byte(q); ligature_present:=true; {\.{=:}}
  15845. if lig_stack>null then pop_lig_stack
  15846. else if j=n then goto done
  15847. else begin append_charnode_to_t(cur_r); incr(j); set_cur_r;
  15848. end;
  15849. end
  15850. endcases;
  15851. if op_byte(q)>qi(4) then if op_byte(q)<>qi(7) then goto done;
  15852. goto continue;
  15853. end
  15854. @ Okay, we're ready to insert the potential hyphenations that were found.
  15855. When the following program is executed, we want to append the word
  15856. |hu[1..hn]| after node |ha|, and node |q| should be appended to the result.
  15857. During this process, the variable |i| will be a temporary
  15858. index into |hu|; the variable |j| will be an index to our current position
  15859. in |hu|; the variable |l| will be the counterpart of |j|, in a discretionary
  15860. branch; the variable |r| will point to new nodes being created; and
  15861. we need a few new local variables:
  15862. @<Local variables for hyph...@>=
  15863. @!major_tail,@!minor_tail:pointer; {the end of lists in the main and
  15864. discretionary branches being reconstructed}
  15865. @!c:ASCII_code; {character temporarily replaced by a hyphen}
  15866. @!c_loc:0..63; {where that character came from}
  15867. @!r_count:integer; {replacement count for discretionary}
  15868. @!hyf_node:pointer; {the hyphen, if it exists}
  15869. @ When the following code is performed, |hyf[0]| and |hyf[hn]| will be zero.
  15870. @<Reconstitute nodes for the hyphenated word...@>=
  15871. repeat l:=j; j:=reconstitute(j,hn,bchar,qi(hyf_char))+1;
  15872. if hyphen_passed=0 then
  15873. begin link(s):=link(hold_head);
  15874. while link(s)>null do s:=link(s);
  15875. if odd(hyf[j-1]) then
  15876. begin l:=j; hyphen_passed:=j-1; link(hold_head):=null;
  15877. end;
  15878. end;
  15879. if hyphen_passed>0 then
  15880. @<Create and append a discretionary node as an alternative to the
  15881. unhyphenated word, and continue to develop both branches until they
  15882. become equivalent@>;
  15883. until j>hn;
  15884. link(s):=q
  15885. @ In this repeat loop we will insert another discretionary if |hyf[j-1]| is
  15886. odd, when both branches of the previous discretionary end at position |j-1|.
  15887. Strictly speaking, we aren't justified in doing this, because we don't know
  15888. that a hyphen after |j-1| is truly independent of those branches. But in almost
  15889. all applications we would rather not lose a potentially valuable hyphenation
  15890. point. (Consider the word `difficult', where the letter `c' is in position |j|.)
  15891. @d advance_major_tail==begin major_tail:=link(major_tail); incr(r_count);
  15892. end
  15893. @<Create and append a discretionary node as an alternative...@>=
  15894. repeat r:=get_node(small_node_size);
  15895. link(r):=link(hold_head); type(r):=disc_node;
  15896. major_tail:=r; r_count:=0;
  15897. while link(major_tail)>null do advance_major_tail;
  15898. i:=hyphen_passed; hyf[i]:=0;
  15899. @<Put the \(c)characters |hu[l..i]| and a hyphen into |pre_break(r)|@>;
  15900. @<Put the \(c)characters |hu[i+1..@,]| into |post_break(r)|, appending to this
  15901. list and to |major_tail| until synchronization has been achieved@>;
  15902. @<Move pointer |s| to the end of the current list, and set |replace_count(r)|
  15903. appropriately@>;
  15904. hyphen_passed:=j-1; link(hold_head):=null;
  15905. until not odd(hyf[j-1])
  15906. @ The new hyphen might combine with the previous character via ligature
  15907. or kern. At this point we have |l-1<=i<j| and |i<hn|.
  15908. @<Put the \(c)characters |hu[l..i]| and a hyphen into |pre_break(r)|@>=
  15909. minor_tail:=null; pre_break(r):=null; hyf_node:=new_character(hf,hyf_char);
  15910. if hyf_node<>null then
  15911. begin incr(i); c:=hu[i]; hu[i]:=hyf_char; free_avail(hyf_node);
  15912. end;
  15913. while l<=i do
  15914. begin l:=reconstitute(l,i,font_bchar[hf],non_char)+1;
  15915. if link(hold_head)>null then
  15916. begin if minor_tail=null then pre_break(r):=link(hold_head)
  15917. else link(minor_tail):=link(hold_head);
  15918. minor_tail:=link(hold_head);
  15919. while link(minor_tail)>null do minor_tail:=link(minor_tail);
  15920. end;
  15921. end;
  15922. if hyf_node<>null then
  15923. begin hu[i]:=c; {restore the character in the hyphen position}
  15924. l:=i; decr(i);
  15925. end
  15926. @ The synchronization algorithm begins with |l=i+1<=j|.
  15927. @<Put the \(c)characters |hu[i+1..@,]| into |post_break(r)|...@>=
  15928. minor_tail:=null; post_break(r):=null; c_loc:=0;
  15929. if bchar_label[hf]<>non_address then {put left boundary at beginning of new line}
  15930. begin decr(l); c:=hu[l]; c_loc:=l; hu[l]:=256;
  15931. end;
  15932. while l<j do
  15933. begin repeat l:=reconstitute(l,hn,bchar,non_char)+1;
  15934. if c_loc>0 then
  15935. begin hu[c_loc]:=c; c_loc:=0;
  15936. end;
  15937. if link(hold_head)>null then
  15938. begin if minor_tail=null then post_break(r):=link(hold_head)
  15939. else link(minor_tail):=link(hold_head);
  15940. minor_tail:=link(hold_head);
  15941. while link(minor_tail)>null do minor_tail:=link(minor_tail);
  15942. end;
  15943. until l>=j;
  15944. while l>j do
  15945. @<Append characters of |hu[j..@,]| to |major_tail|, advancing~|j|@>;
  15946. end
  15947. @ @<Append characters of |hu[j..@,]|...@>=
  15948. begin j:=reconstitute(j,hn,bchar,non_char)+1;
  15949. link(major_tail):=link(hold_head);
  15950. while link(major_tail)>null do advance_major_tail;
  15951. end
  15952. @ Ligature insertion can cause a word to grow exponentially in size. Therefore
  15953. we must test the size of |r_count| here, even though the hyphenated text
  15954. was at most 63 characters long.
  15955. @<Move pointer |s| to the end of the current list...@>=
  15956. if r_count>127 then {we have to forget the discretionary hyphen}
  15957. begin link(s):=link(r); link(r):=null; flush_node_list(r);
  15958. end
  15959. else begin link(s):=r; replace_count(r):=r_count;
  15960. end;
  15961. s:=major_tail
  15962. @* \[42] Hyphenation.
  15963. When a word |hc[1..hn]| has been set up to contain a candidate for hyphenation,
  15964. \TeX\ first looks to see if it is in the user's exception dictionary. If not,
  15965. hyphens are inserted based on patterns that appear within the given word,
  15966. using an algorithm due to Frank~M. Liang.
  15967. @^Liang, Franklin Mark@>
  15968. Let's consider Liang's method first, since it is much more interesting than the
  15969. exception-lookup routine. The algorithm begins by setting |hyf[j]| to zero
  15970. for all |j|, and invalid characters are inserted into |hc[0]|
  15971. and |hc[hn+1]| to serve as delimiters. Then a reasonably fast method is
  15972. used to see which of a given set of patterns occurs in the word
  15973. |hc[0..(hn+1)]|. Each pattern $p_1\ldots p_k$ of length |k| has an associated
  15974. sequence of |k+1| numbers $n_0\ldots n_k$; and if the pattern occurs in
  15975. |hc[(j+1)..(j+k)]|, \TeX\ will set |hyf[j+i]:=@tmax@>(hyf[j+i],@t$n_i$@>)| for
  15976. |0<=i<=k|. After this has been done for each pattern that occurs, a
  15977. discretionary hyphen will be inserted between |hc[j]| and |hc[j+1]| when
  15978. |hyf[j]| is odd, as we have already seen.
  15979. The set of patterns $p_1\ldots p_k$ and associated numbers $n_0\ldots n_k$
  15980. depends, of course, on the language whose words are being hyphenated, and
  15981. on the degree of hyphenation that is desired. A method for finding
  15982. appropriate |p|'s and |n|'s, from a given dictionary of words and acceptable
  15983. hyphenations, is discussed in Liang's Ph.D. thesis (Stanford University,
  15984. 1983); \TeX\ simply starts with the patterns and works from there.
  15985. @ The patterns are stored in a compact table that is also efficient for
  15986. retrieval, using a variant of ``trie memory'' [cf.\ {\sl The Art of
  15987. Computer Programming \bf3} (1973), 481--505]. We can find each pattern
  15988. $p_1\ldots p_k$ by letting $z_0$ be one greater than the relevant language
  15989. index and then, for |1<=i<=k|,
  15990. setting |@t$z_i$@>:=trie_link@t$(z_{i-1})+p_i$@>|; the pattern will be
  15991. identified by the number $z_k$. Since all the pattern information is
  15992. packed together into a single |trie_link| array, it is necessary to
  15993. prevent confusion between the data from inequivalent patterns, so another
  15994. table is provided such that |trie_char@t$(z_i)=p_i$@>| for all |i|. There
  15995. is also a table |trie_op|$(z_k)$ to identify the numbers $n_0\ldots n_k$
  15996. associated with $p_1\ldots p_k$.
  15997. Comparatively few different number sequences $n_0\ldots n_k$ actually occur,
  15998. since most of the |n|'s are generally zero. Therefore the number sequences
  15999. are encoded in such a way that |trie_op|$(z_k)$ is only one byte long.
  16000. If |trie_op(@t$z_k$@>)<>min_quarterword|, when $p_1\ldots p_k$ has matched
  16001. the letters in |hc[(l-k+1)..l@,]| of language |t|,
  16002. we perform all of the required operations
  16003. for this pattern by carrying out the following little program: Set
  16004. |v:=trie_op(@t$z_k$@>)|. Then set |v:=v+op_start[t]|,
  16005. |hyf[l-hyf_distance[v]]:=@tmax@>(hyf[l-hyf_distance[v]], hyf_num[v])|,
  16006. and |v:=hyf_next[v]|; repeat, if necessary, until |v=min_quarterword|.
  16007. @<Types...@>=
  16008. @!trie_pointer=0..trie_size; {an index into |trie|}
  16009. @ @d trie_link(#)==trie[#].rh {``downward'' link in a trie}
  16010. @d trie_char(#)==trie[#].b1 {character matched at this trie location}
  16011. @d trie_op(#)==trie[#].b0 {program for hyphenation at this trie location}
  16012. @<Glob...@>=
  16013. @!trie:array[trie_pointer] of two_halves; {|trie_link|, |trie_char|, |trie_op|}
  16014. @!hyf_distance:array[1..trie_op_size] of small_number; {position |k-j| of $n_j$}
  16015. @!hyf_num:array[1..trie_op_size] of small_number; {value of $n_j$}
  16016. @!hyf_next:array[1..trie_op_size] of quarterword; {continuation code}
  16017. @!op_start:array[ASCII_code] of 0..trie_op_size; {offset for current language}
  16018. @ @<Local variables for hyph...@>=
  16019. @!z:trie_pointer; {an index into |trie|}
  16020. @!v:integer; {an index into |hyf_distance|, etc.}
  16021. @ Assuming that these auxiliary tables have been set up properly, the
  16022. hyphenation algorithm is quite short. In the following code we set |hc[hn+2]|
  16023. to the impossible value 256, in order to guarantee that |hc[hn+3]| will
  16024. never be fetched.
  16025. @<Find hyphen locations for the word in |hc|...@>=
  16026. for j:=0 to hn do hyf[j]:=0;
  16027. @<Look for the word |hc[1..hn]| in the exception table, and |goto found| (with
  16028. |hyf| containing the hyphens) if an entry is found@>;
  16029. if trie_char(cur_lang+1)<>qi(cur_lang) then return; {no patterns for |cur_lang|}
  16030. hc[0]:=0; hc[hn+1]:=0; hc[hn+2]:=256; {insert delimiters}
  16031. for j:=0 to hn-r_hyf+1 do
  16032. begin z:=trie_link(cur_lang+1)+hc[j]; l:=j;
  16033. while hc[l]=qo(trie_char(z)) do
  16034. begin if trie_op(z)<>min_quarterword then
  16035. @<Store \(m)maximum values in the |hyf| table@>;
  16036. incr(l); z:=trie_link(z)+hc[l];
  16037. end;
  16038. end;
  16039. found: for j:=0 to l_hyf-1 do hyf[j]:=0;
  16040. for j:=0 to r_hyf-1 do hyf[hn-j]:=0
  16041. @ @<Store \(m)maximum values in the |hyf| table@>=
  16042. begin v:=trie_op(z);
  16043. repeat v:=v+op_start[cur_lang]; i:=l-hyf_distance[v];
  16044. if hyf_num[v]>hyf[i] then hyf[i]:=hyf_num[v];
  16045. v:=hyf_next[v];
  16046. until v=min_quarterword;
  16047. end
  16048. @ The exception table that is built by \TeX's \.{\\hyphenation} primitive is
  16049. organized as an ordered hash table [cf.\ Amble and Knuth, {\sl The Computer
  16050. @^Amble, Ole@> @^Knuth, Donald Ervin@>
  16051. Journal\/ \bf17} (1974), 135--142] using linear probing. If $\alpha$ and
  16052. $\beta$ are words, we will say that $\alpha<\beta$ if $\vert\alpha\vert<
  16053. \vert\beta\vert$ or if $\vert\alpha\vert=\vert\beta\vert$ and
  16054. $\alpha$ is lexicographically smaller than $\beta$. (The notation $\vert
  16055. \alpha\vert$ stands for the length of $\alpha$.) The idea of ordered hashing
  16056. is to arrange the table so that a given word $\alpha$ can be sought by computing
  16057. a hash address $h=h(\alpha)$ and then looking in table positions |h|, |h-1|,
  16058. \dots, until encountering the first word $\L\alpha$. If this word is
  16059. different from $\alpha$, we can conclude that $\alpha$ is not in the table.
  16060. The words in the table point to lists in |mem| that specify hyphen positions
  16061. in their |info| fields. The list for $c_1\ldots c_n$ contains the number |k| if
  16062. the word $c_1\ldots c_n$ has a discretionary hyphen between $c_k$ and
  16063. $c_{k+1}$.
  16064. @<Types...@>=
  16065. @!hyph_pointer=0..hyph_size; {an index into the ordered hash table}
  16066. @ @<Glob...@>=
  16067. @!hyph_word:array[hyph_pointer] of str_number; {exception words}
  16068. @!hyph_list:array[hyph_pointer] of pointer; {lists of hyphen positions}
  16069. @!hyph_count:hyph_pointer; {the number of words in the exception dictionary}
  16070. @ @<Local variables for init...@>=
  16071. @!z:hyph_pointer; {runs through the exception dictionary}
  16072. @ @<Set init...@>=
  16073. for z:=0 to hyph_size do
  16074. begin hyph_word[z]:=0; hyph_list[z]:=null;
  16075. end;
  16076. hyph_count:=0;
  16077. @ The algorithm for exception lookup is quite simple, as soon as we have
  16078. a few more local variables to work with.
  16079. @<Local variables for hyph...@>=
  16080. @!h:hyph_pointer; {an index into |hyph_word| and |hyph_list|}
  16081. @!k:str_number; {an index into |str_start|}
  16082. @!u:pool_pointer; {an index into |str_pool|}
  16083. @ First we compute the hash code |h|, then we search until we either
  16084. find the word or we don't. Words from different languages are kept
  16085. separate by appending the language code to the string.
  16086. @<Look for the word |hc[1...@>=
  16087. h:=hc[1]; incr(hn); hc[hn]:=cur_lang;
  16088. for j:=2 to hn do h:=(h+h+hc[j]) mod hyph_size;
  16089. loop@+ begin @<If the string |hyph_word[h]| is less than \(hc)|hc[1..hn]|,
  16090. |goto not_found|; but if the two strings are equal,
  16091. set |hyf| to the hyphen positions and |goto found|@>;
  16092. if h>0 then decr(h)@+else h:=hyph_size;
  16093. end;
  16094. not_found: decr(hn)
  16095. @ @<If the string |hyph_word[h]| is less than \(hc)...@>=
  16096. k:=hyph_word[h]; if k=0 then goto not_found;
  16097. if length(k)<hn then goto not_found;
  16098. if length(k)=hn then
  16099. begin j:=1; u:=str_start[k];
  16100. repeat if so(str_pool[u])<hc[j] then goto not_found;
  16101. if so(str_pool[u])>hc[j] then goto done;
  16102. incr(j); incr(u);
  16103. until j>hn;
  16104. @<Insert hyphens as specified in |hyph_list[h]|@>;
  16105. decr(hn); goto found;
  16106. end;
  16107. done:
  16108. @ @<Insert hyphens as specified...@>=
  16109. s:=hyph_list[h];
  16110. while s<>null do
  16111. begin hyf[info(s)]:=1; s:=link(s);
  16112. end
  16113. @ @<Search |hyph_list| for pointers to |p|@>=
  16114. for q:=0 to hyph_size do
  16115. begin if hyph_list[q]=p then
  16116. begin print_nl("HYPH("); print_int(q); print_char(")");
  16117. end;
  16118. end
  16119. @ We have now completed the hyphenation routine, so the |line_break| procedure
  16120. is finished at last. Since the hyphenation exception table is fresh in our
  16121. minds, it's a good time to deal with the routine that adds new entries to it.
  16122. When \TeX\ has scanned `\.{\\hyphenation}', it calls on a procedure named
  16123. |new_hyph_exceptions| to do the right thing.
  16124. @d set_cur_lang==if language<=0 then cur_lang:=0
  16125. else if language>255 then cur_lang:=0
  16126. else cur_lang:=language
  16127. @p procedure new_hyph_exceptions; {enters new exceptions}
  16128. label reswitch, exit, found, not_found;
  16129. var n:0..64; {length of current word; not always a |small_number|}
  16130. @!j:0..64; {an index into |hc|}
  16131. @!h:hyph_pointer; {an index into |hyph_word| and |hyph_list|}
  16132. @!k:str_number; {an index into |str_start|}
  16133. @!p:pointer; {head of a list of hyphen positions}
  16134. @!q:pointer; {used when creating a new node for list |p|}
  16135. @!s,@!t:str_number; {strings being compared or stored}
  16136. @!u,@!v:pool_pointer; {indices into |str_pool|}
  16137. begin scan_left_brace; {a left brace must follow \.{\\hyphenation}}
  16138. set_cur_lang;
  16139. @<Enter as many hyphenation exceptions as are listed,
  16140. until coming to a right brace; then |return|@>;
  16141. exit:end;
  16142. @ @<Enter as many...@>=
  16143. n:=0; p:=null;
  16144. loop@+ begin get_x_token;
  16145. reswitch: case cur_cmd of
  16146. letter,other_char,char_given:@<Append a new letter or hyphen@>;
  16147. char_num: begin scan_char_num; cur_chr:=cur_val; cur_cmd:=char_given;
  16148. goto reswitch;
  16149. end;
  16150. spacer,right_brace: begin if n>1 then @<Enter a hyphenation exception@>;
  16151. if cur_cmd=right_brace then return;
  16152. n:=0; p:=null;
  16153. end;
  16154. othercases @<Give improper \.{\\hyphenation} error@>
  16155. endcases;
  16156. end
  16157. @ @<Give improper \.{\\hyph...@>=
  16158. begin print_err("Improper "); print_esc("hyphenation");
  16159. @.Improper \\hyphenation...@>
  16160. print(" will be flushed");
  16161. help2("Hyphenation exceptions must contain only letters")@/
  16162. ("and hyphens. But continue; I'll forgive and forget.");
  16163. error;
  16164. end
  16165. @ @<Append a new letter or hyphen@>=
  16166. if cur_chr="-" then @<Append the value |n| to list |p|@>
  16167. else begin if lc_code(cur_chr)=0 then
  16168. begin print_err("Not a letter");
  16169. @.Not a letter@>
  16170. help2("Letters in \hyphenation words must have \lccode>0.")@/
  16171. ("Proceed; I'll ignore the character I just read.");
  16172. error;
  16173. end
  16174. else if n<63 then
  16175. begin incr(n); hc[n]:=lc_code(cur_chr);
  16176. end;
  16177. end
  16178. @ @<Append the value |n| to list |p|@>=
  16179. begin if n<63 then
  16180. begin q:=get_avail; link(q):=p; info(q):=n; p:=q;
  16181. end;
  16182. end
  16183. @ @<Enter a hyphenation exception@>=
  16184. begin incr(n); hc[n]:=cur_lang; str_room(n); h:=0;
  16185. for j:=1 to n do
  16186. begin h:=(h+h+hc[j]) mod hyph_size;
  16187. append_char(hc[j]);
  16188. end;
  16189. s:=make_string;
  16190. @<Insert the \(p)pair |(s,p)| into the exception table@>;
  16191. end
  16192. @ @<Insert the \(p)pair |(s,p)|...@>=
  16193. if hyph_count=hyph_size then overflow("exception dictionary",hyph_size);
  16194. @:TeX capacity exceeded exception dictionary}{\quad exception dictionary@>
  16195. incr(hyph_count);
  16196. while hyph_word[h]<>0 do
  16197. begin @<If the string |hyph_word[h]| is less than \(or)or equal to
  16198. |s|, interchange |(hyph_word[h],hyph_list[h])| with |(s,p)|@>;
  16199. if h>0 then decr(h)@+else h:=hyph_size;
  16200. end;
  16201. hyph_word[h]:=s; hyph_list[h]:=p
  16202. @ @<If the string |hyph_word[h]| is less than \(or)...@>=
  16203. k:=hyph_word[h];
  16204. if length(k)<length(s) then goto found;
  16205. if length(k)>length(s) then goto not_found;
  16206. u:=str_start[k]; v:=str_start[s];
  16207. repeat if str_pool[u]<str_pool[v] then goto found;
  16208. if str_pool[u]>str_pool[v] then goto not_found;
  16209. incr(u); incr(v);
  16210. until u=str_start[k+1];
  16211. found:q:=hyph_list[h]; hyph_list[h]:=p; p:=q;@/
  16212. t:=hyph_word[h]; hyph_word[h]:=s; s:=t;
  16213. not_found:
  16214. @* \[43] Initializing the hyphenation tables.
  16215. The trie for \TeX's hyphenation algorithm is built from a sequence of
  16216. patterns following a \.{\\patterns} specification. Such a specification
  16217. is allowed only in \.{INITEX}, since the extra memory for auxiliary tables
  16218. and for the initialization program itself would only clutter up the
  16219. production version of \TeX\ with a lot of deadwood.
  16220. The first step is to build a trie that is linked, instead of packed
  16221. into sequential storage, so that insertions are readily made.
  16222. After all patterns have been processed, \.{INITEX}
  16223. compresses the linked trie by identifying common subtries. Finally the
  16224. trie is packed into the efficient sequential form that the hyphenation
  16225. algorithm actually uses.
  16226. @<Declare subprocedures for |line_break|@>=
  16227. @!init @<Declare procedures for preprocessing hyphenation patterns@>@;
  16228. tini
  16229. @ Before we discuss trie building in detail, let's consider the simpler
  16230. problem of creating the |hyf_distance|, |hyf_num|, and |hyf_next| arrays.
  16231. Suppose, for example, that \TeX\ reads the pattern `\.{ab2cde1}'. This is
  16232. a pattern of length 5, with $n_0\ldots n_5=0\,0\,2\,0\,0\,1$ in the
  16233. notation above. We want the corresponding |trie_op| code |v| to have
  16234. |hyf_distance[v]=3|, |hyf_num[v]=2|, and |hyf_next[v]=@t$v^\prime$@>|,
  16235. where the auxiliary |trie_op| code $v^\prime$ has
  16236. |hyf_distance[@t$v^\prime$@>]=0|, |hyf_num[@t$v^\prime$@>]=1|, and
  16237. |hyf_next[@t$v^\prime$@>]=min_quarterword|.
  16238. \TeX\ computes an appropriate value |v| with the |new_trie_op| subroutine
  16239. below, by setting
  16240. $$\hbox{|@t$v^\prime$@>:=new_trie_op(0,1,min_quarterword)|,\qquad
  16241. |v:=new_trie_op(3,2,@t$v^\prime$@>)|.}$$
  16242. This subroutine looks up its three
  16243. parameters in a special hash table, assigning a new value only if these
  16244. three have not appeared before for the current language.
  16245. The hash table is called |trie_op_hash|, and the number of entries it contains
  16246. is |trie_op_ptr|.
  16247. @<Glob...@>=
  16248. @!init @!trie_op_hash:array[-trie_op_size..trie_op_size] of 0..trie_op_size;
  16249. {trie op codes for quadruples}
  16250. @!trie_used:array[ASCII_code] of quarterword;
  16251. {largest opcode used so far for this language}
  16252. @!trie_op_lang:array[1..trie_op_size] of ASCII_code;
  16253. {language part of a hashed quadruple}
  16254. @!trie_op_val:array[1..trie_op_size] of quarterword;
  16255. {opcode corresponding to a hashed quadruple}
  16256. @!trie_op_ptr:0..trie_op_size; {number of stored ops so far}
  16257. tini
  16258. @ It's tempting to remove the |overflow| stops in the following procedure;
  16259. |new_trie_op| could return |min_quarterword| (thereby simply ignoring
  16260. part of a hyphenation pattern) instead of aborting the job. However, that would
  16261. lead to different hyphenation results on different installations of \TeX\
  16262. using the same patterns. The |overflow| stops are necessary for portability
  16263. of patterns.
  16264. @<Declare procedures for preprocessing hyph...@>=
  16265. function new_trie_op(@!d,@!n:small_number;@!v:quarterword):quarterword;
  16266. label exit;
  16267. var h:-trie_op_size..trie_op_size; {trial hash location}
  16268. @!u:quarterword; {trial op code}
  16269. @!l:0..trie_op_size; {pointer to stored data}
  16270. begin h:=abs(n+313*d+361*v+1009*cur_lang) mod (trie_op_size+trie_op_size)
  16271. - trie_op_size;
  16272. loop@+ begin l:=trie_op_hash[h];
  16273. if l=0 then {empty position found for a new op}
  16274. begin if trie_op_ptr=trie_op_size then
  16275. overflow("pattern memory ops",trie_op_size);
  16276. u:=trie_used[cur_lang];
  16277. if u=max_quarterword then
  16278. overflow("pattern memory ops per language",
  16279. max_quarterword-min_quarterword);
  16280. incr(trie_op_ptr); incr(u); trie_used[cur_lang]:=u;
  16281. hyf_distance[trie_op_ptr]:=d;
  16282. hyf_num[trie_op_ptr]:=n; hyf_next[trie_op_ptr]:=v;
  16283. trie_op_lang[trie_op_ptr]:=cur_lang; trie_op_hash[h]:=trie_op_ptr;
  16284. trie_op_val[trie_op_ptr]:=u; new_trie_op:=u; return;
  16285. end;
  16286. if (hyf_distance[l]=d)and(hyf_num[l]=n)and(hyf_next[l]=v)
  16287. and(trie_op_lang[l]=cur_lang) then
  16288. begin new_trie_op:=trie_op_val[l]; return;
  16289. end;
  16290. if h>-trie_op_size then decr(h)@+else h:=trie_op_size;
  16291. end;
  16292. exit:end;
  16293. @ After |new_trie_op| has compressed the necessary opcode information,
  16294. plenty of information is available to unscramble the data into the
  16295. final form needed by our hyphenation algorithm.
  16296. @<Sort \(t)the hyphenation op tables into proper order@>=
  16297. op_start[0]:=-min_quarterword;
  16298. for j:=1 to 255 do op_start[j]:=op_start[j-1]+qo(trie_used[j-1]);
  16299. for j:=1 to trie_op_ptr do
  16300. trie_op_hash[j]:=op_start[trie_op_lang[j]]+trie_op_val[j]; {destination}
  16301. for j:=1 to trie_op_ptr do while trie_op_hash[j]>j do
  16302. begin k:=trie_op_hash[j];@/
  16303. t:=hyf_distance[k]; hyf_distance[k]:=hyf_distance[j]; hyf_distance[j]:=t;@/
  16304. t:=hyf_num[k]; hyf_num[k]:=hyf_num[j]; hyf_num[j]:=t;@/
  16305. t:=hyf_next[k]; hyf_next[k]:=hyf_next[j]; hyf_next[j]:=t;@/
  16306. trie_op_hash[j]:=trie_op_hash[k]; trie_op_hash[k]:=k;
  16307. end
  16308. @ Before we forget how to initialize the data structures that have been
  16309. mentioned so far, let's write down the code that gets them started.
  16310. @<Initialize table entries...@>=
  16311. for k:=-trie_op_size to trie_op_size do trie_op_hash[k]:=0;
  16312. for k:=0 to 255 do trie_used[k]:=min_quarterword;
  16313. trie_op_ptr:=0;
  16314. @ The linked trie that is used to preprocess hyphenation patterns appears
  16315. in several global arrays. Each node represents an instruction of the form
  16316. ``if you see character |c|, then perform operation |o|, move to the
  16317. next character, and go to node |l|; otherwise go to node |r|.''
  16318. The four quantities |c|, |o|, |l|, and |r| are stored in four arrays
  16319. |trie_c|, |trie_o|, |trie_l|, and |trie_r|. The root of the trie
  16320. is |trie_l[0]|, and the number of nodes is |trie_ptr|. Null trie
  16321. pointers are represented by zero. To initialize the trie, we simply
  16322. set |trie_l[0]| and |trie_ptr| to zero. We also set |trie_c[0]| to some
  16323. arbitrary value, since the algorithm may access it.
  16324. The algorithms maintain the condition
  16325. $$\hbox{|trie_c[trie_r[z]]>trie_c[z]|\qquad
  16326. whenever |z<>0| and |trie_r[z]<>0|};$$ in other words, sibling nodes are
  16327. ordered by their |c| fields.
  16328. @d trie_root==trie_l[0] {root of the linked trie}
  16329. @<Glob...@>=
  16330. @!init @!trie_c:packed array[trie_pointer] of packed_ASCII_code;
  16331. {characters to match}
  16332. @t\hskip10pt@>@!trie_o:packed array[trie_pointer] of quarterword;
  16333. {operations to perform}
  16334. @t\hskip10pt@>@!trie_l:packed array[trie_pointer] of trie_pointer;
  16335. {left subtrie links}
  16336. @t\hskip10pt@>@!trie_r:packed array[trie_pointer] of trie_pointer;
  16337. {right subtrie links}
  16338. @t\hskip10pt@>@!trie_ptr:trie_pointer; {the number of nodes in the trie}
  16339. @t\hskip10pt@>@!trie_hash:packed array[trie_pointer] of trie_pointer;
  16340. {used to identify equivalent subtries}
  16341. tini
  16342. @ Let us suppose that a linked trie has already been constructed.
  16343. Experience shows that we can often reduce its size by recognizing common
  16344. subtries; therefore another hash table is introduced for this purpose,
  16345. somewhat similar to |trie_op_hash|. The new hash table will be
  16346. initialized to zero.
  16347. The function |trie_node(p)| returns |p| if |p| is distinct from other nodes
  16348. that it has seen, otherwise it returns the number of the first equivalent
  16349. node that it has seen.
  16350. Notice that we might make subtries equivalent even if they correspond to
  16351. patterns for different languages, in which the trie ops might mean quite
  16352. different things. That's perfectly all right.
  16353. @<Declare procedures for preprocessing hyph...@>=
  16354. function trie_node(@!p:trie_pointer):trie_pointer; {converts
  16355. to a canonical form}
  16356. label exit;
  16357. var h:trie_pointer; {trial hash location}
  16358. @!q:trie_pointer; {trial trie node}
  16359. begin h:=abs(trie_c[p]+1009*trie_o[p]+@|
  16360. 2718*trie_l[p]+3142*trie_r[p]) mod trie_size;
  16361. loop@+ begin q:=trie_hash[h];
  16362. if q=0 then
  16363. begin trie_hash[h]:=p; trie_node:=p; return;
  16364. end;
  16365. if (trie_c[q]=trie_c[p])and(trie_o[q]=trie_o[p])and@|
  16366. (trie_l[q]=trie_l[p])and(trie_r[q]=trie_r[p]) then
  16367. begin trie_node:=q; return;
  16368. end;
  16369. if h>0 then decr(h)@+else h:=trie_size;
  16370. end;
  16371. exit:end;
  16372. @ A neat recursive procedure is now able to compress a trie by
  16373. traversing it and applying |trie_node| to its nodes in ``bottom up''
  16374. fashion. We will compress the entire trie by clearing |trie_hash| to
  16375. zero and then saying `|trie_root:=compress_trie(trie_root)|'.
  16376. @^recursion@>
  16377. @<Declare procedures for preprocessing hyph...@>=
  16378. function compress_trie(@!p:trie_pointer):trie_pointer;
  16379. begin if p=0 then compress_trie:=0
  16380. else begin trie_l[p]:=compress_trie(trie_l[p]);
  16381. trie_r[p]:=compress_trie(trie_r[p]);
  16382. compress_trie:=trie_node(p);
  16383. end;
  16384. end;
  16385. @ The compressed trie will be packed into the |trie| array using a
  16386. ``top-down first-fit'' procedure. This is a little tricky, so the reader
  16387. should pay close attention: The |trie_hash| array is cleared to zero
  16388. again and renamed |trie_ref| for this phase of the operation; later on,
  16389. |trie_ref[p]| will be nonzero only if the linked trie node |p| is the
  16390. smallest character
  16391. in a family and if the characters |c| of that family have been allocated to
  16392. locations |trie_ref[p]+c| in the |trie| array. Locations of |trie| that
  16393. are in use will have |trie_link=0|, while the unused holes in |trie|
  16394. will be doubly linked with |trie_link| pointing to the next larger vacant
  16395. location and |trie_back| pointing to the next smaller one. This double
  16396. linking will have been carried out only as far as |trie_max|, where
  16397. |trie_max| is the largest index of |trie| that will be needed.
  16398. To save time at the low end of the trie, we maintain array entries
  16399. |trie_min[c]| pointing to the smallest hole that is greater than~|c|.
  16400. Another array |trie_taken| tells whether or not a given location is
  16401. equal to |trie_ref[p]| for some |p|; this array is used to ensure that
  16402. distinct nodes in the compressed trie will have distinct |trie_ref|
  16403. entries.
  16404. @d trie_ref==trie_hash {where linked trie families go into |trie|}
  16405. @d trie_back(#)==trie[#].lh {backward links in |trie| holes}
  16406. @<Glob...@>=
  16407. @!init @!trie_taken:packed array[1..trie_size] of boolean;
  16408. {does a family start here?}
  16409. @t\hskip10pt@>@!trie_min:array[ASCII_code] of trie_pointer;
  16410. {the first possible slot for each character}
  16411. @t\hskip10pt@>@!trie_max:trie_pointer; {largest location used in |trie|}
  16412. @t\hskip10pt@>@!trie_not_ready:boolean; {is the trie still in linked form?}
  16413. tini
  16414. @ Each time \.{\\patterns} appears, it contributes further patterns to
  16415. the future trie, which will be built only when hyphenation is attempted or
  16416. when a format file is dumped. The boolean variable |trie_not_ready|
  16417. will change to |false| when the trie is compressed; this will disable
  16418. further patterns.
  16419. @<Initialize table entries...@>=
  16420. trie_not_ready:=true; trie_root:=0; trie_c[0]:=si(0); trie_ptr:=0;
  16421. @ Here is how the trie-compression data structures are initialized.
  16422. If storage is tight, it would be possible to overlap |trie_op_hash|,
  16423. |trie_op_lang|, and |trie_op_val| with |trie|, |trie_hash|, and |trie_taken|,
  16424. because we finish with the former just before we need the latter.
  16425. @<Get ready to compress the trie@>=
  16426. @<Sort \(t)the hyphenation...@>;
  16427. for p:=0 to trie_size do trie_hash[p]:=0;
  16428. trie_root:=compress_trie(trie_root); {identify equivalent subtries}
  16429. for p:=0 to trie_ptr do trie_ref[p]:=0;
  16430. for p:=0 to 255 do trie_min[p]:=p+1;
  16431. trie_link(0):=1; trie_max:=0
  16432. @ The |first_fit| procedure finds the smallest hole |z| in |trie| such that
  16433. a trie family starting at a given node |p| will fit into vacant positions
  16434. starting at |z|. If |c=trie_c[p]|, this means that location |z-c| must
  16435. not already be taken by some other family, and that |z-c+@t$c^\prime$@>|
  16436. must be vacant for all characters $c^\prime$ in the family. The procedure
  16437. sets |trie_ref[p]| to |z-c| when the first fit has been found.
  16438. @<Declare procedures for preprocessing hyph...@>=
  16439. procedure first_fit(@!p:trie_pointer); {packs a family into |trie|}
  16440. label not_found,found;
  16441. var h:trie_pointer; {candidate for |trie_ref[p]|}
  16442. @!z:trie_pointer; {runs through holes}
  16443. @!q:trie_pointer; {runs through the family starting at |p|}
  16444. @!c:ASCII_code; {smallest character in the family}
  16445. @!l,@!r:trie_pointer; {left and right neighbors}
  16446. @!ll:1..256; {upper limit of |trie_min| updating}
  16447. begin c:=so(trie_c[p]);
  16448. z:=trie_min[c]; {get the first conceivably good hole}
  16449. loop@+ begin h:=z-c;@/
  16450. @<Ensure that |trie_max>=h+256|@>;
  16451. if trie_taken[h] then goto not_found;
  16452. @<If all characters of the family fit relative to |h|, then
  16453. |goto found|,\30\ otherwise |goto not_found|@>;
  16454. not_found: z:=trie_link(z); {move to the next hole}
  16455. end;
  16456. found: @<Pack the family into |trie| relative to |h|@>;
  16457. end;
  16458. @ By making sure that |trie_max| is at least |h+256|, we can be sure that
  16459. |trie_max>z|, since |h=z-c|. It follows that location |trie_max| will
  16460. never be occupied in |trie|, and we will have |trie_max>=trie_link(z)|.
  16461. @<Ensure that |trie_max>=h+256|@>=
  16462. if trie_max<h+256 then
  16463. begin if trie_size<=h+256 then overflow("pattern memory",trie_size);
  16464. @:TeX capacity exceeded pattern memory}{\quad pattern memory@>
  16465. repeat incr(trie_max); trie_taken[trie_max]:=false;
  16466. trie_link(trie_max):=trie_max+1; trie_back(trie_max):=trie_max-1;
  16467. until trie_max=h+256;
  16468. end
  16469. @ @<If all characters of the family fit relative to |h|...@>=
  16470. q:=trie_r[p];
  16471. while q>0 do
  16472. begin if trie_link(h+so(trie_c[q]))=0 then goto not_found;
  16473. q:=trie_r[q];
  16474. end;
  16475. goto found
  16476. @ @<Pack the family into |trie| relative to |h|@>=
  16477. trie_taken[h]:=true; trie_ref[p]:=h; q:=p;
  16478. repeat z:=h+so(trie_c[q]); l:=trie_back(z); r:=trie_link(z);
  16479. trie_back(r):=l; trie_link(l):=r; trie_link(z):=0;
  16480. if l<256 then
  16481. begin if z<256 then ll:=z @+else ll:=256;
  16482. repeat trie_min[l]:=r; incr(l);
  16483. until l=ll;
  16484. end;
  16485. q:=trie_r[q];
  16486. until q=0
  16487. @ To pack the entire linked trie, we use the following recursive procedure.
  16488. @^recursion@>
  16489. @<Declare procedures for preprocessing hyph...@>=
  16490. procedure trie_pack(@!p:trie_pointer); {pack subtries of a family}
  16491. var q:trie_pointer; {a local variable that need not be saved on recursive calls}
  16492. begin repeat q:=trie_l[p];
  16493. if (q>0)and(trie_ref[q]=0) then
  16494. begin first_fit(q); trie_pack(q);
  16495. end;
  16496. p:=trie_r[p];
  16497. until p=0;
  16498. end;
  16499. @ When the whole trie has been allocated into the sequential table, we
  16500. must go through it once again so that |trie| contains the correct
  16501. information. Null pointers in the linked trie will be represented by the
  16502. value~0, which properly implements an ``empty'' family.
  16503. @<Move the data into |trie|@>=
  16504. h.rh:=0; h.b0:=min_quarterword; h.b1:=min_quarterword; {|trie_link:=0|,
  16505. |trie_op:=min_quarterword|, |trie_char:=qi(0)|}
  16506. if trie_root=0 then {no patterns were given}
  16507. begin for r:=0 to 256 do trie[r]:=h;
  16508. trie_max:=256;
  16509. end
  16510. else begin trie_fix(trie_root); {this fixes the non-holes in |trie|}
  16511. r:=0; {now we will zero out all the holes}
  16512. repeat s:=trie_link(r); trie[r]:=h; r:=s;
  16513. until r>trie_max;
  16514. end;
  16515. trie_char(0):=qi("?"); {make |trie_char(c)<>c| for all |c|}
  16516. @ The fixing-up procedure is, of course, recursive. Since the linked trie
  16517. usually has overlapping subtries, the same data may be moved several
  16518. times; but that causes no harm, and at most as much work is done as it
  16519. took to build the uncompressed trie.
  16520. @^recursion@>
  16521. @<Declare procedures for preprocessing hyph...@>=
  16522. procedure trie_fix(@!p:trie_pointer); {moves |p| and its siblings into |trie|}
  16523. var q:trie_pointer; {a local variable that need not be saved on recursive calls}
  16524. @!c:ASCII_code; {another one that need not be saved}
  16525. @!z:trie_pointer; {|trie| reference; this local variable must be saved}
  16526. begin z:=trie_ref[p];
  16527. repeat q:=trie_l[p]; c:=so(trie_c[p]);
  16528. trie_link(z+c):=trie_ref[q]; trie_char(z+c):=qi(c); trie_op(z+c):=trie_o[p];
  16529. if q>0 then trie_fix(q);
  16530. p:=trie_r[p];
  16531. until p=0;
  16532. end;
  16533. @ Now let's go back to the easier problem, of building the linked
  16534. trie. When \.{INITEX} has scanned the `\.{\\patterns}' control
  16535. sequence, it calls on |new_patterns| to do the right thing.
  16536. @<Declare procedures for preprocessing hyph...@>=
  16537. procedure new_patterns; {initializes the hyphenation pattern data}
  16538. label done, done1;
  16539. var k,@!l:0..64; {indices into |hc| and |hyf|;
  16540. not always in |small_number| range}
  16541. @!digit_sensed:boolean; {should the next digit be treated as a letter?}
  16542. @!v:quarterword; {trie op code}
  16543. @!p,@!q:trie_pointer; {nodes of trie traversed during insertion}
  16544. @!first_child:boolean; {is |p=trie_l[q]|?}
  16545. @!c:ASCII_code; {character being inserted}
  16546. begin if trie_not_ready then
  16547. begin set_cur_lang; scan_left_brace; {a left brace must follow \.{\\patterns}}
  16548. @<Enter all of the patterns into a linked trie, until coming to a right
  16549. brace@>;
  16550. end
  16551. else begin print_err("Too late for "); print_esc("patterns");
  16552. help1("All patterns must be given before typesetting begins.");
  16553. error; link(garbage):=scan_toks(false,false); flush_list(def_ref);
  16554. end;
  16555. end;
  16556. @ Novices are not supposed to be using \.{\\patterns}, so the error
  16557. messages are terse. (Note that all error messages appear in \TeX's string
  16558. pool, even if they are used only by \.{INITEX}.)
  16559. @<Enter all of the patterns into a linked trie...@>=
  16560. k:=0; hyf[0]:=0; digit_sensed:=false;
  16561. loop@+ begin get_x_token;
  16562. case cur_cmd of
  16563. letter,other_char:@<Append a new letter or a hyphen level@>;
  16564. spacer,right_brace: begin if k>0 then
  16565. @<Insert a new pattern into the linked trie@>;
  16566. if cur_cmd=right_brace then goto done;
  16567. k:=0; hyf[0]:=0; digit_sensed:=false;
  16568. end;
  16569. othercases begin print_err("Bad "); print_esc("patterns");
  16570. @.Bad \\patterns@>
  16571. help1("(See Appendix H.)"); error;
  16572. end
  16573. endcases;
  16574. end;
  16575. done:
  16576. @ @<Append a new letter or a hyphen level@>=
  16577. if digit_sensed or(cur_chr<"0")or(cur_chr>"9") then
  16578. begin if cur_chr="." then cur_chr:=0 {edge-of-word delimiter}
  16579. else begin cur_chr:=lc_code(cur_chr);
  16580. if cur_chr=0 then
  16581. begin print_err("Nonletter");
  16582. @.Nonletter@>
  16583. help1("(See Appendix H.)"); error;
  16584. end;
  16585. end;
  16586. if k<63 then
  16587. begin incr(k); hc[k]:=cur_chr; hyf[k]:=0; digit_sensed:=false;
  16588. end;
  16589. end
  16590. else if k<63 then
  16591. begin hyf[k]:=cur_chr-"0"; digit_sensed:=true;
  16592. end
  16593. @ When the following code comes into play, the pattern $p_1\ldots p_k$
  16594. appears in |hc[1..k]|, and the corresponding sequence of numbers $n_0\ldots
  16595. n_k$ appears in |hyf[0..k]|.
  16596. @<Insert a new pattern into the linked trie@>=
  16597. begin @<Compute the trie op code, |v|, and set |l:=0|@>;
  16598. q:=0; hc[0]:=cur_lang;
  16599. while l<=k do
  16600. begin c:=hc[l]; incr(l); p:=trie_l[q]; first_child:=true;
  16601. while (p>0)and(c>so(trie_c[p])) do
  16602. begin q:=p; p:=trie_r[q]; first_child:=false;
  16603. end;
  16604. if (p=0)or(c<so(trie_c[p])) then
  16605. @<Insert a new trie node between |q| and |p|, and
  16606. make |p| point to it@>;
  16607. q:=p; {now node |q| represents $p_1\ldots p_{l-1}$}
  16608. end;
  16609. if trie_o[q]<>min_quarterword then
  16610. begin print_err("Duplicate pattern");
  16611. @.Duplicate pattern@>
  16612. help1("(See Appendix H.)"); error;
  16613. end;
  16614. trie_o[q]:=v;
  16615. end
  16616. @ @<Insert a new trie node between |q| and |p|...@>=
  16617. begin if trie_ptr=trie_size then overflow("pattern memory",trie_size);
  16618. @:TeX capacity exceeded pattern memory}{\quad pattern memory@>
  16619. incr(trie_ptr); trie_r[trie_ptr]:=p; p:=trie_ptr; trie_l[p]:=0;
  16620. if first_child then trie_l[q]:=p@+else trie_r[q]:=p;
  16621. trie_c[p]:=si(c); trie_o[p]:=min_quarterword;
  16622. end
  16623. @ @<Compute the trie op code, |v|...@>=
  16624. if hc[1]=0 then hyf[0]:=0;
  16625. if hc[k]=0 then hyf[k]:=0;
  16626. l:=k; v:=min_quarterword;
  16627. loop@+ begin if hyf[l]<>0 then v:=new_trie_op(k-l,hyf[l],v);
  16628. if l>0 then decr(l)@+else goto done1;
  16629. end;
  16630. done1:
  16631. @ Finally we put everything together: Here is how the trie gets to its
  16632. final, efficient form.
  16633. The following packing routine is rigged so that the root of the linked
  16634. tree gets mapped into location 1 of |trie|, as required by the hyphenation
  16635. algorithm. This happens because the first call of |first_fit| will
  16636. ``take'' location~1.
  16637. @<Declare procedures for preprocessing hyphenation patterns@>=
  16638. procedure init_trie;
  16639. var @!p:trie_pointer; {pointer for initialization}
  16640. @!j,@!k,@!t:integer; {all-purpose registers for initialization}
  16641. @!r,@!s:trie_pointer; {used to clean up the packed |trie|}
  16642. @!h:two_halves; {template used to zero out |trie|'s holes}
  16643. begin @<Get ready to compress the trie@>;
  16644. if trie_root<>0 then
  16645. begin first_fit(trie_root); trie_pack(trie_root);
  16646. end;
  16647. @<Move the data into |trie|@>;
  16648. trie_not_ready:=false;
  16649. end;
  16650. @* \[44] Breaking vertical lists into pages.
  16651. The |vsplit| procedure, which implements \TeX's \.{\\vsplit} operation,
  16652. is considerably simpler than |line_break| because it doesn't have to
  16653. worry about hyphenation, and because its mission is to discover a single
  16654. break instead of an optimum sequence of breakpoints. But before we get
  16655. into the details of |vsplit|, we need to consider a few more basic things.
  16656. @ A subroutine called |prune_page_top| takes a pointer to a vlist and
  16657. returns a pointer to a modified vlist in which all glue, kern, and penalty nodes
  16658. have been deleted before the first box or rule node. However, the first
  16659. box or rule is actually preceded by a newly created glue node designed so that
  16660. the topmost baseline will be at distance |split_top_skip| from the top,
  16661. whenever this is possible without backspacing.
  16662. In this routine and those that follow, we make use of the fact that a
  16663. vertical list contains no character nodes, hence the |type| field exists
  16664. for each node in the list.
  16665. @^data structure assumptions@>
  16666. @p function prune_page_top(@!p:pointer):pointer; {adjust top after page break}
  16667. var prev_p:pointer; {lags one step behind |p|}
  16668. @!q:pointer; {temporary variable for list manipulation}
  16669. begin prev_p:=temp_head; link(temp_head):=p;
  16670. while p<>null do
  16671. case type(p) of
  16672. hlist_node,vlist_node,rule_node:@<Insert glue for |split_top_skip|
  16673. and set~|p:=null|@>;
  16674. whatsit_node,mark_node,ins_node: begin prev_p:=p; p:=link(prev_p);
  16675. end;
  16676. glue_node,kern_node,penalty_node: begin q:=p; p:=link(q); link(q):=null;
  16677. link(prev_p):=p; flush_node_list(q);
  16678. end;
  16679. othercases confusion("pruning")
  16680. @:this can't happen pruning}{\quad pruning@>
  16681. endcases;
  16682. prune_page_top:=link(temp_head);
  16683. end;
  16684. @ @<Insert glue for |split_top_skip|...@>=
  16685. begin q:=new_skip_param(split_top_skip_code); link(prev_p):=q; link(q):=p;
  16686. {now |temp_ptr=glue_ptr(q)|}
  16687. if width(temp_ptr)>height(p) then width(temp_ptr):=width(temp_ptr)-height(p)
  16688. else width(temp_ptr):=0;
  16689. p:=null;
  16690. end
  16691. @ The next subroutine finds the best place to break a given vertical list
  16692. so as to obtain a box of height~|h|, with maximum depth~|d|.
  16693. A pointer to the beginning of the vertical list is given,
  16694. and a pointer to the optimum breakpoint is returned. The list is effectively
  16695. followed by a forced break, i.e., a penalty node with the |eject_penalty|;
  16696. if the best break occurs at this artificial node, the value |null| is returned.
  16697. An array of six |scaled| distances is used to keep track of the height
  16698. from the beginning of the list to the current place, just as in |line_break|.
  16699. In fact, we use one of the same arrays, only changing its name to reflect
  16700. its new significance.
  16701. @d active_height==active_width {new name for the six distance variables}
  16702. @d cur_height==active_height[1] {the natural height}
  16703. @d set_height_zero(#)==active_height[#]:=0 {initialize the height to zero}
  16704. @#
  16705. @d update_heights=90 {go here to record glue in the |active_height| table}
  16706. @p function vert_break(@!p:pointer; @!h,@!d:scaled):pointer;
  16707. {finds optimum page break}
  16708. label done,not_found,update_heights;
  16709. var prev_p:pointer; {if |p| is a glue node, |type(prev_p)| determines
  16710. whether |p| is a legal breakpoint}
  16711. @!q,@!r:pointer; {glue specifications}
  16712. @!pi:integer; {penalty value}
  16713. @!b:integer; {badness at a trial breakpoint}
  16714. @!least_cost:integer; {the smallest badness plus penalties found so far}
  16715. @!best_place:pointer; {the most recent break that leads to |least_cost|}
  16716. @!prev_dp:scaled; {depth of previous box in the list}
  16717. @!t:small_number; {|type| of the node following a kern}
  16718. begin prev_p:=p; {an initial glue node is not a legal breakpoint}
  16719. least_cost:=awful_bad; do_all_six(set_height_zero); prev_dp:=0;
  16720. loop@+ begin @<If node |p| is a legal breakpoint, check if this break is
  16721. the best known, and |goto done| if |p| is null or
  16722. if the page-so-far is already too full to accept more stuff@>;
  16723. prev_p:=p; p:=link(prev_p);
  16724. end;
  16725. done: vert_break:=best_place;
  16726. end;
  16727. @ A global variable |best_height_plus_depth| will be set to the natural size
  16728. of the box that corresponds to the optimum breakpoint found by |vert_break|.
  16729. (This value is used by the insertion-splitting algorithm of the page builder.)
  16730. @<Glob...@>=
  16731. @!best_height_plus_depth:scaled; {height of the best box, without stretching or
  16732. shrinking}
  16733. @ A subtle point to be noted here is that the maximum depth~|d| might be
  16734. negative, so |cur_height| and |prev_dp| might need to be corrected even
  16735. after a glue or kern node.
  16736. @<If node |p| is a legal breakpoint, check...@>=
  16737. if p=null then pi:=eject_penalty
  16738. else @<Use node |p| to update the current height and depth measurements;
  16739. if this node is not a legal breakpoint, |goto not_found|
  16740. or |update_heights|,
  16741. otherwise set |pi| to the associated penalty at the break@>;
  16742. @<Check if node |p| is a new champion breakpoint; then \(go)|goto done|
  16743. if |p| is a forced break or if the page-so-far is already too full@>;
  16744. if (type(p)<glue_node)or(type(p)>kern_node) then goto not_found;
  16745. update_heights: @<Update the current height and depth measurements with
  16746. respect to a glue or kern node~|p|@>;
  16747. not_found: if prev_dp>d then
  16748. begin cur_height:=cur_height+prev_dp-d;
  16749. prev_dp:=d;
  16750. end;
  16751. @ @<Use node |p| to update the current height and depth measurements...@>=
  16752. case type(p) of
  16753. hlist_node,vlist_node,rule_node: begin@t@>@;@/
  16754. cur_height:=cur_height+prev_dp+height(p); prev_dp:=depth(p);
  16755. goto not_found;
  16756. end;
  16757. whatsit_node:@<Process whatsit |p| in |vert_break| loop, |goto not_found|@>;
  16758. glue_node: if precedes_break(prev_p) then pi:=0
  16759. else goto update_heights;
  16760. kern_node: begin if link(p)=null then t:=penalty_node
  16761. else t:=type(link(p));
  16762. if t=glue_node then pi:=0@+else goto update_heights;
  16763. end;
  16764. penalty_node: pi:=penalty(p);
  16765. mark_node,ins_node: goto not_found;
  16766. othercases confusion("vertbreak")
  16767. @:this can't happen vertbreak}{\quad vertbreak@>
  16768. endcases
  16769. @ @d deplorable==100000 {more than |inf_bad|, but less than |awful_bad|}
  16770. @<Check if node |p| is a new champion breakpoint; then \(go)...@>=
  16771. if pi<inf_penalty then
  16772. begin @<Compute the badness, |b|, using |awful_bad|
  16773. if the box is too full@>;
  16774. if b<awful_bad then
  16775. if pi<=eject_penalty then b:=pi
  16776. else if b<inf_bad then b:=b+pi
  16777. else b:=deplorable;
  16778. if b<=least_cost then
  16779. begin best_place:=p; least_cost:=b;
  16780. best_height_plus_depth:=cur_height+prev_dp;
  16781. end;
  16782. if (b=awful_bad)or(pi<=eject_penalty) then goto done;
  16783. end
  16784. @ @<Compute the badness, |b|, using |awful_bad| if the box is too full@>=
  16785. if cur_height<h then
  16786. if (active_height[3]<>0) or (active_height[4]<>0) or
  16787. (active_height[5]<>0) then b:=0
  16788. else b:=badness(h-cur_height,active_height[2])
  16789. else if cur_height-h>active_height[6] then b:=awful_bad
  16790. else b:=badness(cur_height-h,active_height[6])
  16791. @ Vertical lists that are subject to the |vert_break| procedure should not
  16792. contain infinite shrinkability, since that would permit any amount of
  16793. information to ``fit'' on one page.
  16794. @<Update the current height and depth measurements with...@>=
  16795. if type(p)=kern_node then q:=p
  16796. else begin q:=glue_ptr(p);
  16797. active_height[2+stretch_order(q)]:=@|
  16798. active_height[2+stretch_order(q)]+stretch(q);@/
  16799. active_height[6]:=active_height[6]+shrink(q);
  16800. if (shrink_order(q)<>normal)and(shrink(q)<>0) then
  16801. begin@t@>@;@/
  16802. print_err("Infinite glue shrinkage found in box being split");@/
  16803. @.Infinite glue shrinkage...@>
  16804. help4("The box you are \vsplitting contains some infinitely")@/
  16805. ("shrinkable glue, e.g., `\vss' or `\vskip 0pt minus 1fil'.")@/
  16806. ("Such glue doesn't belong there; but you can safely proceed,")@/
  16807. ("since the offensive shrinkability has been made finite.");
  16808. error; r:=new_spec(q); shrink_order(r):=normal; delete_glue_ref(q);
  16809. glue_ptr(p):=r; q:=r;
  16810. end;
  16811. end;
  16812. cur_height:=cur_height+prev_dp+width(q); prev_dp:=0
  16813. @ Now we are ready to consider |vsplit| itself. Most of
  16814. its work is accomplished by the two subroutines that we have just considered.
  16815. Given the number of a vlist box |n|, and given a desired page height |h|,
  16816. the |vsplit| function finds the best initial segment of the vlist and
  16817. returns a box for a page of height~|h|. The remainder of the vlist, if
  16818. any, replaces the original box, after removing glue and penalties and
  16819. adjusting for |split_top_skip|. Mark nodes in the split-off box are used to
  16820. set the values of |split_first_mark| and |split_bot_mark|; we use the
  16821. fact that |split_first_mark=null| if and only if |split_bot_mark=null|.
  16822. The original box becomes ``void'' if and only if it has been entirely
  16823. extracted. The extracted box is ``void'' if and only if the original
  16824. box was void (or if it was, erroneously, an hlist box).
  16825. @p function vsplit(@!n:eight_bits; @!h:scaled):pointer;
  16826. {extracts a page of height |h| from box |n|}
  16827. label exit,done;
  16828. var v:pointer; {the box to be split}
  16829. p:pointer; {runs through the vlist}
  16830. q:pointer; {points to where the break occurs}
  16831. begin v:=box(n);
  16832. if split_first_mark<>null then
  16833. begin delete_token_ref(split_first_mark); split_first_mark:=null;
  16834. delete_token_ref(split_bot_mark); split_bot_mark:=null;
  16835. end;
  16836. @<Dispense with trivial cases of void or bad boxes@>;
  16837. q:=vert_break(list_ptr(v),h,split_max_depth);
  16838. @<Look at all the marks in nodes before the break, and set the final
  16839. link to |null| at the break@>;
  16840. q:=prune_page_top(q); p:=list_ptr(v); free_node(v,box_node_size);
  16841. if q=null then box(n):=null {the |eq_level| of the box stays the same}
  16842. else box(n):=vpack(q,natural);
  16843. vsplit:=vpackage(p,h,exactly,split_max_depth);
  16844. exit: end;
  16845. @ @<Dispense with trivial cases of void or bad boxes@>=
  16846. if v=null then
  16847. begin vsplit:=null; return;
  16848. end;
  16849. if type(v)<>vlist_node then
  16850. begin print_err(""); print_esc("vsplit"); print(" needs a ");
  16851. print_esc("vbox");
  16852. @:vsplit_}{\.{\\vsplit needs a \\vbox}@>
  16853. help2("The box you are trying to split is an \hbox.")@/
  16854. ("I can't split such a box, so I'll leave it alone.");
  16855. error; vsplit:=null; return;
  16856. end
  16857. @ It's possible that the box begins with a penalty node that is the
  16858. ``best'' break, so we must be careful to handle this special case correctly.
  16859. @<Look at all the marks...@>=
  16860. p:=list_ptr(v);
  16861. if p=q then list_ptr(v):=null
  16862. else loop@+begin if type(p)=mark_node then
  16863. if split_first_mark=null then
  16864. begin split_first_mark:=mark_ptr(p);
  16865. split_bot_mark:=split_first_mark;
  16866. token_ref_count(split_first_mark):=@|
  16867. token_ref_count(split_first_mark)+2;
  16868. end
  16869. else begin delete_token_ref(split_bot_mark);
  16870. split_bot_mark:=mark_ptr(p);
  16871. add_token_ref(split_bot_mark);
  16872. end;
  16873. if link(p)=q then
  16874. begin link(p):=null; goto done;
  16875. end;
  16876. p:=link(p);
  16877. end;
  16878. done:
  16879. @* \[45] The page builder.
  16880. When \TeX\ appends new material to its main vlist in vertical mode, it uses
  16881. a method something like |vsplit| to decide where a page ends, except that
  16882. the calculations are done ``on line'' as new items come in.
  16883. The main complication in this process is that insertions must be put
  16884. into their boxes and removed from the vlist, in a more-or-less optimum manner.
  16885. We shall use the term ``current page'' for that part of the main vlist that
  16886. is being considered as a candidate for being broken off and sent to the
  16887. user's output routine. The current page starts at |link(page_head)|, and
  16888. it ends at |page_tail|. We have |page_head=page_tail| if this list is empty.
  16889. @^current page@>
  16890. Utter chaos would reign if the user kept changing page specifications
  16891. while a page is being constructed, so the page builder keeps the pertinent
  16892. specifications frozen as soon as the page receives its first box or
  16893. insertion. The global variable |page_contents| is |empty| when the
  16894. current page contains only mark nodes and content-less whatsit nodes; it
  16895. is |inserts_only| if the page contains only insertion nodes in addition to
  16896. marks and whatsits. Glue nodes, kern nodes, and penalty nodes are
  16897. discarded until a box or rule node appears, at which time |page_contents|
  16898. changes to |box_there|. As soon as |page_contents| becomes non-|empty|,
  16899. the current |vsize| and |max_depth| are squirreled away into |page_goal|
  16900. and |page_max_depth|; the latter values will be used until the page has
  16901. been forwarded to the user's output routine. The \.{\\topskip} adjustment
  16902. is made when |page_contents| changes to |box_there|.
  16903. Although |page_goal| starts out equal to |vsize|, it is decreased by the
  16904. scaled natural height-plus-depth of the insertions considered so far, and by
  16905. the \.{\\skip} corrections for those insertions. Therefore it represents
  16906. the size into which the non-inserted material should fit, assuming that
  16907. all insertions in the current page have been made.
  16908. The global variables |best_page_break| and |least_page_cost| correspond
  16909. respectively to the local variables |best_place| and |least_cost| in the
  16910. |vert_break| routine that we have already studied; i.e., they record the
  16911. location and value of the best place currently known for breaking the
  16912. current page. The value of |page_goal| at the time of the best break is
  16913. stored in |best_size|.
  16914. @d inserts_only=1
  16915. {|page_contents| when an insert node has been contributed, but no boxes}
  16916. @d box_there=2 {|page_contents| when a box or rule has been contributed}
  16917. @<Glob...@>=
  16918. @!page_tail:pointer; {the final node on the current page}
  16919. @!page_contents:empty..box_there; {what is on the current page so far?}
  16920. @!page_max_depth:scaled; {maximum box depth on page being built}
  16921. @!best_page_break:pointer; {break here to get the best page known so far}
  16922. @!least_page_cost:integer; {the score for this currently best page}
  16923. @!best_size:scaled; {its |page_goal|}
  16924. @ The page builder has another data structure to keep track of insertions.
  16925. This is a list of four-word nodes, starting and ending at |page_ins_head|.
  16926. That is, the first element of the list is node |r@t$_1$@>=link(page_ins_head)|;
  16927. node $r_j$ is followed by |r@t$_{j+1}$@>=link(r@t$_j$@>)|; and if there are
  16928. |n| items we have |r@t$_{n+1}$@>=page_ins_head|. The |subtype| field of
  16929. each node in this list refers to an insertion number; for example, `\.{\\insert
  16930. 250}' would correspond to a node whose |subtype| is |qi(250)|
  16931. (the same as the |subtype| field of the relevant |ins_node|). These |subtype|
  16932. fields are in increasing order, and |subtype(page_ins_head)=
  16933. qi(255)|, so |page_ins_head| serves as a convenient sentinel
  16934. at the end of the list. A record is present for each insertion number that
  16935. appears in the current page.
  16936. The |type| field in these nodes distinguishes two possibilities that
  16937. might occur as we look ahead before deciding on the optimum page break.
  16938. If |type(r)=inserting|, then |height(r)| contains the total of the
  16939. height-plus-depth dimensions of the box and all its inserts seen so far.
  16940. If |type(r)=split_up|, then no more insertions will be made into this box,
  16941. because at least one previous insertion was too big to fit on the current
  16942. page; |broken_ptr(r)| points to the node where that insertion will be
  16943. split, if \TeX\ decides to split it, |broken_ins(r)| points to the
  16944. insertion node that was tentatively split, and |height(r)| includes also the
  16945. natural height plus depth of the part that would be split off.
  16946. In both cases, |last_ins_ptr(r)| points to the last |ins_node|
  16947. encountered for box |qo(subtype(r))| that would be at least partially
  16948. inserted on the next page; and |best_ins_ptr(r)| points to the last
  16949. such |ins_node| that should actually be inserted, to get the page with
  16950. minimum badness among all page breaks considered so far. We have
  16951. |best_ins_ptr(r)=null| if and only if no insertion for this box should
  16952. be made to produce this optimum page.
  16953. The data structure definitions here use the fact that the |@!height| field
  16954. appears in the fourth word of a box node.
  16955. @^data structure assumptions@>
  16956. @d page_ins_node_size=4 {number of words for a page insertion node}
  16957. @d inserting=0 {an insertion class that has not yet overflowed}
  16958. @d split_up=1 {an overflowed insertion class}
  16959. @d broken_ptr(#)==link(#+1)
  16960. {an insertion for this class will break here if anywhere}
  16961. @d broken_ins(#)==info(#+1) {this insertion might break at |broken_ptr|}
  16962. @d last_ins_ptr(#)==link(#+2) {the most recent insertion for this |subtype|}
  16963. @d best_ins_ptr(#)==info(#+2) {the optimum most recent insertion}
  16964. @<Initialize the special list heads...@>=
  16965. subtype(page_ins_head):=qi(255);
  16966. type(page_ins_head):=split_up; link(page_ins_head):=page_ins_head;
  16967. @ An array |page_so_far| records the heights and depths of everything
  16968. on the current page. This array contains six |scaled| numbers, like the
  16969. similar arrays already considered in |line_break| and |vert_break|; and it
  16970. also contains |page_goal| and |page_depth|, since these values are
  16971. all accessible to the user via |set_page_dimen| commands. The
  16972. value of |page_so_far[1]| is also called |page_total|. The stretch
  16973. and shrink components of the \.{\\skip} corrections for each insertion are
  16974. included in |page_so_far|, but the natural space components of these
  16975. corrections are not, since they have been subtracted from |page_goal|.
  16976. The variable |page_depth| records the depth of the current page; it has been
  16977. adjusted so that it is at most |page_max_depth|. The variable
  16978. |last_glue| points to the glue specification of the most recent node
  16979. contributed from the contribution list, if this was a glue node; otherwise
  16980. |last_glue=max_halfword|. (If the contribution list is nonempty,
  16981. however, the value of |last_glue| is not necessarily accurate.)
  16982. The variables |last_penalty| and |last_kern| are similar. And
  16983. finally, |insert_penalties| holds the sum of the penalties associated with
  16984. all split and floating insertions.
  16985. @d page_goal==page_so_far[0] {desired height of information on page being built}
  16986. @d page_total==page_so_far[1] {height of the current page}
  16987. @d page_shrink==page_so_far[6] {shrinkability of the current page}
  16988. @d page_depth==page_so_far[7] {depth of the current page}
  16989. @<Glob...@>=
  16990. @!page_so_far:array [0..7] of scaled; {height and glue of the current page}
  16991. @!last_glue:pointer; {used to implement \.{\\lastskip}}
  16992. @!last_penalty:integer; {used to implement \.{\\lastpenalty}}
  16993. @!last_kern:scaled; {used to implement \.{\\lastkern}}
  16994. @!insert_penalties:integer; {sum of the penalties for insertions
  16995. that were held over}
  16996. @ @<Put each...@>=
  16997. primitive("pagegoal",set_page_dimen,0);
  16998. @!@:page_goal_}{\.{\\pagegoal} primitive@>
  16999. primitive("pagetotal",set_page_dimen,1);
  17000. @!@:page_total_}{\.{\\pagetotal} primitive@>
  17001. primitive("pagestretch",set_page_dimen,2);
  17002. @!@:page_stretch_}{\.{\\pagestretch} primitive@>
  17003. primitive("pagefilstretch",set_page_dimen,3);
  17004. @!@:page_fil_stretch_}{\.{\\pagefilstretch} primitive@>
  17005. primitive("pagefillstretch",set_page_dimen,4);
  17006. @!@:page_fill_stretch_}{\.{\\pagefillstretch} primitive@>
  17007. primitive("pagefilllstretch",set_page_dimen,5);
  17008. @!@:page_filll_stretch_}{\.{\\pagefilllstretch} primitive@>
  17009. primitive("pageshrink",set_page_dimen,6);
  17010. @!@:page_shrink_}{\.{\\pageshrink} primitive@>
  17011. primitive("pagedepth",set_page_dimen,7);
  17012. @!@:page_depth_}{\.{\\pagedepth} primitive@>
  17013. @ @<Cases of |print_cmd_chr|...@>=
  17014. set_page_dimen: case chr_code of
  17015. 0: print_esc("pagegoal");
  17016. 1: print_esc("pagetotal");
  17017. 2: print_esc("pagestretch");
  17018. 3: print_esc("pagefilstretch");
  17019. 4: print_esc("pagefillstretch");
  17020. 5: print_esc("pagefilllstretch");
  17021. 6: print_esc("pageshrink");
  17022. othercases print_esc("pagedepth")
  17023. endcases;
  17024. @ @d print_plus_end(#)==print(#);@+end
  17025. @d print_plus(#)==if page_so_far[#]<>0 then
  17026. begin print(" plus "); print_scaled(page_so_far[#]); print_plus_end
  17027. @p procedure print_totals;
  17028. begin print_scaled(page_total);
  17029. print_plus(2)("");
  17030. print_plus(3)("fil");
  17031. print_plus(4)("fill");
  17032. print_plus(5)("filll");
  17033. if page_shrink<>0 then
  17034. begin print(" minus "); print_scaled(page_shrink);
  17035. end;
  17036. end;
  17037. @ @<Show the status of the current page@>=
  17038. if page_head<>page_tail then
  17039. begin print_nl("### current page:");
  17040. if output_active then print(" (held over for next output)");
  17041. @.held over for next output@>
  17042. show_box(link(page_head));
  17043. if page_contents>empty then
  17044. begin print_nl("total height "); print_totals;
  17045. @:total_height}{\.{total height}@>
  17046. print_nl(" goal height "); print_scaled(page_goal);
  17047. @.goal height@>
  17048. r:=link(page_ins_head);
  17049. while r<>page_ins_head do
  17050. begin print_ln; print_esc("insert"); t:=qo(subtype(r));
  17051. print_int(t); print(" adds ");
  17052. if count(t)=1000 then t:=height(r)
  17053. else t:=x_over_n(height(r),1000)*count(t);
  17054. print_scaled(t);
  17055. if type(r)=split_up then
  17056. begin q:=page_head; t:=0;
  17057. repeat q:=link(q);
  17058. if (type(q)=ins_node)and(subtype(q)=subtype(r)) then incr(t);
  17059. until q=broken_ins(r);
  17060. print(", #"); print_int(t); print(" might split");
  17061. end;
  17062. r:=link(r);
  17063. end;
  17064. end;
  17065. end
  17066. @ Here is a procedure that is called when the |page_contents| is changing
  17067. from |empty| to |inserts_only| or |box_there|.
  17068. @d set_page_so_far_zero(#)==page_so_far[#]:=0
  17069. @p procedure freeze_page_specs(@!s:small_number);
  17070. begin page_contents:=s;
  17071. page_goal:=vsize; page_max_depth:=max_depth;
  17072. page_depth:=0; do_all_six(set_page_so_far_zero);
  17073. least_page_cost:=awful_bad;
  17074. @!stat if tracing_pages>0 then
  17075. begin begin_diagnostic;
  17076. print_nl("%% goal height="); print_scaled(page_goal);
  17077. @.goal height@>
  17078. print(", max depth="); print_scaled(page_max_depth);
  17079. end_diagnostic(false);
  17080. end;@;@+tats@;@/
  17081. end;
  17082. @ Pages are built by appending nodes to the current list in \TeX's
  17083. vertical mode, which is at the outermost level of the semantic nest. This
  17084. vlist is split into two parts; the ``current page'' that we have been
  17085. talking so much about already, and the ``contribution list'' that receives
  17086. new nodes as they are created. The current page contains everything that
  17087. the page builder has accounted for in its data structures, as described
  17088. above, while the contribution list contains other things that have been
  17089. generated by other parts of \TeX\ but have not yet been
  17090. seen by the page builder.
  17091. The contribution list starts at |link(contrib_head)|, and it ends at the
  17092. current node in \TeX's vertical mode.
  17093. When \TeX\ has appended new material in vertical mode, it calls the procedure
  17094. |build_page|, which tries to catch up by moving nodes from the contribution
  17095. list to the current page. This procedure will succeed in its goal of
  17096. emptying the contribution list, unless a page break is discovered, i.e.,
  17097. unless the current page has grown to the point where the optimum next
  17098. page break has been determined. In the latter case, the nodes after the
  17099. optimum break will go back onto the contribution list, and control will
  17100. effectively pass to the user's output routine.
  17101. We make |type(page_head)=glue_node|, so that an initial glue node on
  17102. the current page will not be considered a valid breakpoint.
  17103. @<Initialize the special list...@>=
  17104. type(page_head):=glue_node; subtype(page_head):=normal;
  17105. @ The global variable |output_active| is true during the time the
  17106. user's output routine is driving \TeX.
  17107. @<Glob...@>=
  17108. @!output_active:boolean; {are we in the midst of an output routine?}
  17109. @ @<Set init...@>=
  17110. output_active:=false; insert_penalties:=0;
  17111. @ The page builder is ready to start a fresh page if we initialize
  17112. the following state variables. (However, the page insertion list is initialized
  17113. elsewhere.)
  17114. @<Start a new current page@>=
  17115. page_contents:=empty; page_tail:=page_head; link(page_head):=null;@/
  17116. last_glue:=max_halfword; last_penalty:=0; last_kern:=0;
  17117. page_depth:=0; page_max_depth:=0
  17118. @ At certain times box 255 is supposed to be void (i.e., |null|),
  17119. or an insertion box is supposed to be ready to accept a vertical list.
  17120. If not, an error message is printed, and the following subroutine
  17121. flushes the unwanted contents, reporting them to the user.
  17122. @p procedure box_error(@!n:eight_bits);
  17123. begin error; begin_diagnostic;
  17124. print_nl("The following box has been deleted:");
  17125. @.The following...deleted@>
  17126. show_box(box(n)); end_diagnostic(true);
  17127. flush_node_list(box(n)); box(n):=null;
  17128. end;
  17129. @ The following procedure guarantees that a given box register
  17130. does not contain an \.{\\hbox}.
  17131. @p procedure ensure_vbox(@!n:eight_bits);
  17132. var p:pointer; {the box register contents}
  17133. begin p:=box(n);
  17134. if p<>null then if type(p)=hlist_node then
  17135. begin print_err("Insertions can only be added to a vbox");
  17136. @.Insertions can only...@>
  17137. help3("Tut tut: You're trying to \insert into a")@/
  17138. ("\box register that now contains an \hbox.")@/
  17139. ("Proceed, and I'll discard its present contents.");
  17140. box_error(n);
  17141. end;
  17142. end;
  17143. @ \TeX\ is not always in vertical mode at the time |build_page|
  17144. is called; the current mode reflects what \TeX\ should return to, after
  17145. the contribution list has been emptied. A call on |build_page| should
  17146. be immediately followed by `|goto big_switch|', which is \TeX's central
  17147. control point.
  17148. @d contribute=80 {go here to link a node into the current page}
  17149. @p @t\4@>@<Declare the procedure called |fire_up|@>@;@/
  17150. procedure build_page; {append contributions to the current page}
  17151. label exit,done,done1,continue,contribute,update_heights;
  17152. var p:pointer; {the node being appended}
  17153. @!q,@!r:pointer; {nodes being examined}
  17154. @!b,@!c:integer; {badness and cost of current page}
  17155. @!pi:integer; {penalty to be added to the badness}
  17156. @!n:min_quarterword..255; {insertion box number}
  17157. @!delta,@!h,@!w:scaled; {sizes used for insertion calculations}
  17158. begin if (link(contrib_head)=null)or output_active then return;
  17159. repeat continue: p:=link(contrib_head);@/
  17160. @<Update the values of |last_glue|, |last_penalty|, and |last_kern|@>;
  17161. @<Move node |p| to the current page; if it is time for a page break,
  17162. put the nodes following the break back onto the contribution list,
  17163. and |return| to the user's output routine if there is one@>;
  17164. until link(contrib_head)=null;
  17165. @<Make the contribution list empty by setting its tail to |contrib_head|@>;
  17166. exit:end;
  17167. @ @d contrib_tail==nest[0].tail_field {tail of the contribution list}
  17168. @<Make the contribution list empty...@>=
  17169. if nest_ptr=0 then tail:=contrib_head {vertical mode}
  17170. else contrib_tail:=contrib_head {other modes}
  17171. @ @<Update the values of |last_glue|...@>=
  17172. if last_glue<>max_halfword then delete_glue_ref(last_glue);
  17173. last_penalty:=0; last_kern:=0;
  17174. if type(p)=glue_node then
  17175. begin last_glue:=glue_ptr(p); add_glue_ref(last_glue);
  17176. end
  17177. else begin last_glue:=max_halfword;
  17178. if type(p)=penalty_node then last_penalty:=penalty(p)
  17179. else if type(p)=kern_node then last_kern:=width(p);
  17180. end
  17181. @ The code here is an example of a many-way switch into routines that
  17182. merge together in different places. Some people call this unstructured
  17183. programming, but the author doesn't see much wrong with it, as long as
  17184. @^Knuth, Donald Ervin@>
  17185. the various labels have a well-understood meaning.
  17186. @<Move node |p| to the current page; ...@>=
  17187. @<If the current page is empty and node |p| is to be deleted, |goto done1|;
  17188. otherwise use node |p| to update the state of the current page;
  17189. if this node is an insertion, |goto contribute|; otherwise if this node
  17190. is not a legal breakpoint, |goto contribute| or |update_heights|;
  17191. otherwise set |pi| to the penalty associated with this breakpoint@>;
  17192. @<Check if node |p| is a new champion breakpoint; then \(if)if it is time for
  17193. a page break, prepare for output, and either fire up the user's
  17194. output routine and |return| or ship out the page and |goto done|@>;
  17195. if (type(p)<glue_node)or(type(p)>kern_node) then goto contribute;
  17196. update_heights:@<Update the current page measurements with respect to the
  17197. glue or kern specified by node~|p|@>;
  17198. contribute: @<Make sure that |page_max_depth| is not exceeded@>;
  17199. @<Link node |p| into the current page and |goto done|@>;
  17200. done1:@<Recycle node |p|@>;
  17201. done:
  17202. @ @<Link node |p| into the current page and |goto done|@>=
  17203. link(page_tail):=p; page_tail:=p;
  17204. link(contrib_head):=link(p); link(p):=null; goto done
  17205. @ @<Recycle node |p|@>=
  17206. link(contrib_head):=link(p); link(p):=null; flush_node_list(p)
  17207. @ The title of this section is already so long, it seems best to avoid
  17208. making it more accurate but still longer, by mentioning the fact that a
  17209. kern node at the end of the contribution list will not be contributed until
  17210. we know its successor.
  17211. @<If the current page is empty...@>=
  17212. case type(p) of
  17213. hlist_node,vlist_node,rule_node: if page_contents<box_there then
  17214. @<Initialize the current page, insert the \.{\\topskip} glue
  17215. ahead of |p|, and |goto continue|@>
  17216. else @<Prepare to move a box or rule node to the current page,
  17217. then |goto contribute|@>;
  17218. whatsit_node: @<Prepare to move whatsit |p| to the current page,
  17219. then |goto contribute|@>;
  17220. glue_node: if page_contents<box_there then goto done1
  17221. else if precedes_break(page_tail) then pi:=0
  17222. else goto update_heights;
  17223. kern_node: if page_contents<box_there then goto done1
  17224. else if link(p)=null then return
  17225. else if type(link(p))=glue_node then pi:=0
  17226. else goto update_heights;
  17227. penalty_node: if page_contents<box_there then goto done1@+else pi:=penalty(p);
  17228. mark_node: goto contribute;
  17229. ins_node: @<Append an insertion to the current page and |goto contribute|@>;
  17230. othercases confusion("page")
  17231. @:this can't happen page}{\quad page@>
  17232. endcases
  17233. @ @<Initialize the current page, insert the \.{\\topskip} glue...@>=
  17234. begin if page_contents=empty then freeze_page_specs(box_there)
  17235. else page_contents:=box_there;
  17236. q:=new_skip_param(top_skip_code); {now |temp_ptr=glue_ptr(q)|}
  17237. if width(temp_ptr)>height(p) then width(temp_ptr):=width(temp_ptr)-height(p)
  17238. else width(temp_ptr):=0;
  17239. link(q):=p; link(contrib_head):=q; goto continue;
  17240. end
  17241. @ @<Prepare to move a box or rule node to the current page...@>=
  17242. begin page_total:=page_total+page_depth+height(p);
  17243. page_depth:=depth(p);
  17244. goto contribute;
  17245. end
  17246. @ @<Make sure that |page_max_depth| is not exceeded@>=
  17247. if page_depth>page_max_depth then
  17248. begin page_total:=@|
  17249. page_total+page_depth-page_max_depth;@/
  17250. page_depth:=page_max_depth;
  17251. end;
  17252. @ @<Update the current page measurements with respect to the glue...@>=
  17253. if type(p)=kern_node then q:=p
  17254. else begin q:=glue_ptr(p);
  17255. page_so_far[2+stretch_order(q)]:=@|
  17256. page_so_far[2+stretch_order(q)]+stretch(q);@/
  17257. page_shrink:=page_shrink+shrink(q);
  17258. if (shrink_order(q)<>normal)and(shrink(q)<>0) then
  17259. begin@t@>@;@/
  17260. print_err("Infinite glue shrinkage found on current page");@/
  17261. @.Infinite glue shrinkage...@>
  17262. help4("The page about to be output contains some infinitely")@/
  17263. ("shrinkable glue, e.g., `\vss' or `\vskip 0pt minus 1fil'.")@/
  17264. ("Such glue doesn't belong there; but you can safely proceed,")@/
  17265. ("since the offensive shrinkability has been made finite.");
  17266. error;
  17267. r:=new_spec(q); shrink_order(r):=normal; delete_glue_ref(q);
  17268. glue_ptr(p):=r; q:=r;
  17269. end;
  17270. end;
  17271. page_total:=page_total+page_depth+width(q); page_depth:=0
  17272. @ @<Check if node |p| is a new champion breakpoint; then \(if)...@>=
  17273. if pi<inf_penalty then
  17274. begin @<Compute the badness, |b|, of the current page,
  17275. using |awful_bad| if the box is too full@>;
  17276. if b<awful_bad then
  17277. if pi<=eject_penalty then c:=pi
  17278. else if b<inf_bad then c:=b+pi+insert_penalties
  17279. else c:=deplorable
  17280. else c:=b;
  17281. if insert_penalties>=10000 then c:=awful_bad;
  17282. @!stat if tracing_pages>0 then @<Display the page break cost@>;@+tats@;@/
  17283. if c<=least_page_cost then
  17284. begin best_page_break:=p; best_size:=page_goal;
  17285. least_page_cost:=c;
  17286. r:=link(page_ins_head);
  17287. while r<>page_ins_head do
  17288. begin best_ins_ptr(r):=last_ins_ptr(r);
  17289. r:=link(r);
  17290. end;
  17291. end;
  17292. if (c=awful_bad)or(pi<=eject_penalty) then
  17293. begin fire_up(p); {output the current page at the best place}
  17294. if output_active then return; {user's output routine will act}
  17295. goto done; {the page has been shipped out by default output routine}
  17296. end;
  17297. end
  17298. @ @<Display the page break cost@>=
  17299. begin begin_diagnostic; print_nl("%");
  17300. print(" t="); print_totals;@/
  17301. print(" g="); print_scaled(page_goal);@/
  17302. print(" b=");
  17303. if b=awful_bad then print_char("*")@+else print_int(b);
  17304. @.*\relax@>
  17305. print(" p="); print_int(pi);
  17306. print(" c=");
  17307. if c=awful_bad then print_char("*")@+else print_int(c);
  17308. if c<=least_page_cost then print_char("#");
  17309. end_diagnostic(false);
  17310. end
  17311. @ @<Compute the badness, |b|, of the current page...@>=
  17312. if page_total<page_goal then
  17313. if (page_so_far[3]<>0) or (page_so_far[4]<>0) or@|
  17314. (page_so_far[5]<>0) then b:=0
  17315. else b:=badness(page_goal-page_total,page_so_far[2])
  17316. else if page_total-page_goal>page_shrink then b:=awful_bad
  17317. else b:=badness(page_total-page_goal,page_shrink)
  17318. @ @<Append an insertion to the current page and |goto contribute|@>=
  17319. begin if page_contents=empty then freeze_page_specs(inserts_only);
  17320. n:=subtype(p); r:=page_ins_head;
  17321. while n>=subtype(link(r)) do r:=link(r);
  17322. n:=qo(n);
  17323. if subtype(r)<>qi(n) then
  17324. @<Create a page insertion node with |subtype(r)=qi(n)|, and
  17325. include the glue correction for box |n| in the
  17326. current page state@>;
  17327. if type(r)=split_up then insert_penalties:=insert_penalties+float_cost(p)
  17328. else begin last_ins_ptr(r):=p;
  17329. delta:=page_goal-page_total-page_depth+page_shrink;
  17330. {this much room is left if we shrink the maximum}
  17331. if count(n)=1000 then h:=height(p)
  17332. else h:=x_over_n(height(p),1000)*count(n); {this much room is needed}
  17333. if ((h<=0)or(h<=delta))and(height(p)+height(r)<=dimen(n)) then
  17334. begin page_goal:=page_goal-h; height(r):=height(r)+height(p);
  17335. end
  17336. else @<Find the best way to split the insertion, and change
  17337. |type(r)| to |split_up|@>;
  17338. end;
  17339. goto contribute;
  17340. end
  17341. @ We take note of the value of \.{\\skip} |n| and the height plus depth
  17342. of \.{\\box}~|n| only when the first \.{\\insert}~|n| node is
  17343. encountered for a new page. A user who changes the contents of \.{\\box}~|n|
  17344. after that first \.{\\insert}~|n| had better be either extremely careful
  17345. or extremely lucky, or both.
  17346. @<Create a page insertion node...@>=
  17347. begin q:=get_node(page_ins_node_size); link(q):=link(r); link(r):=q; r:=q;
  17348. subtype(r):=qi(n); type(r):=inserting; ensure_vbox(n);
  17349. if box(n)=null then height(r):=0
  17350. else height(r):=height(box(n))+depth(box(n));
  17351. best_ins_ptr(r):=null;@/
  17352. q:=skip(n);
  17353. if count(n)=1000 then h:=height(r)
  17354. else h:=x_over_n(height(r),1000)*count(n);
  17355. page_goal:=page_goal-h-width(q);@/
  17356. page_so_far[2+stretch_order(q)]:=@|page_so_far[2+stretch_order(q)]+stretch(q);@/
  17357. page_shrink:=page_shrink+shrink(q);
  17358. if (shrink_order(q)<>normal)and(shrink(q)<>0) then
  17359. begin print_err("Infinite glue shrinkage inserted from "); print_esc("skip");
  17360. @.Infinite glue shrinkage...@>
  17361. print_int(n);
  17362. help3("The correction glue for page breaking with insertions")@/
  17363. ("must have finite shrinkability. But you may proceed,")@/
  17364. ("since the offensive shrinkability has been made finite.");
  17365. error;
  17366. end;
  17367. end
  17368. @ Here is the code that will split a long footnote between pages, in an
  17369. emergency. The current situation deserves to be recapitulated: Node |p|
  17370. is an insertion into box |n|; the insertion will not fit, in its entirety,
  17371. either because it would make the total contents of box |n| greater than
  17372. \.{\\dimen} |n|, or because it would make the incremental amount of growth
  17373. |h| greater than the available space |delta|, or both. (This amount |h| has
  17374. been weighted by the insertion scaling factor, i.e., by \.{\\count} |n|
  17375. over 1000.) Now we will choose the best way to break the vlist of the
  17376. insertion, using the same criteria as in the \.{\\vsplit} operation.
  17377. @<Find the best way to split the insertion...@>=
  17378. begin if count(n)<=0 then w:=max_dimen
  17379. else begin w:=page_goal-page_total-page_depth;
  17380. if count(n)<>1000 then w:=x_over_n(w,count(n))*1000;
  17381. end;
  17382. if w>dimen(n)-height(r) then w:=dimen(n)-height(r);
  17383. q:=vert_break(ins_ptr(p),w,depth(p));
  17384. height(r):=height(r)+best_height_plus_depth;
  17385. @!stat if tracing_pages>0 then @<Display the insertion split cost@>;@+tats@;@/
  17386. if count(n)<>1000 then
  17387. best_height_plus_depth:=x_over_n(best_height_plus_depth,1000)*count(n);
  17388. page_goal:=page_goal-best_height_plus_depth;
  17389. type(r):=split_up; broken_ptr(r):=q; broken_ins(r):=p;
  17390. if q=null then insert_penalties:=insert_penalties+eject_penalty
  17391. else if type(q)=penalty_node then insert_penalties:=insert_penalties+penalty(q);
  17392. end
  17393. @ @<Display the insertion split cost@>=
  17394. begin begin_diagnostic; print_nl("% split"); print_int(n);
  17395. @.split@>
  17396. print(" to "); print_scaled(w);
  17397. print_char(","); print_scaled(best_height_plus_depth);@/
  17398. print(" p=");
  17399. if q=null then print_int(eject_penalty)
  17400. else if type(q)=penalty_node then print_int(penalty(q))
  17401. else print_char("0");
  17402. end_diagnostic(false);
  17403. end
  17404. @ When the page builder has looked at as much material as could appear before
  17405. the next page break, it makes its decision. The break that gave minimum
  17406. badness will be used to put a completed ``page'' into box 255, with insertions
  17407. appended to their other boxes.
  17408. We also set the values of |top_mark|, |first_mark|, and |bot_mark|. The
  17409. program uses the fact that |bot_mark<>null| implies |first_mark<>null|;
  17410. it also knows that |bot_mark=null| implies |top_mark=first_mark=null|.
  17411. The |fire_up| subroutine prepares to output the current page at the best
  17412. place; then it fires up the user's output routine, if there is one,
  17413. or it simply ships out the page. There is one parameter, |c|, which represents
  17414. the node that was being contributed to the page when the decision to
  17415. force an output was made.
  17416. @<Declare the procedure called |fire_up|@>=
  17417. procedure fire_up(@!c:pointer);
  17418. label exit;
  17419. var p,@!q,@!r,@!s:pointer; {nodes being examined and/or changed}
  17420. @!prev_p:pointer; {predecessor of |p|}
  17421. @!n:min_quarterword..255; {insertion box number}
  17422. @!wait:boolean; {should the present insertion be held over?}
  17423. @!save_vbadness:integer; {saved value of |vbadness|}
  17424. @!save_vfuzz: scaled; {saved value of |vfuzz|}
  17425. @!save_split_top_skip: pointer; {saved value of |split_top_skip|}
  17426. begin @<Set the value of |output_penalty|@>;
  17427. if bot_mark<>null then
  17428. begin if top_mark<>null then delete_token_ref(top_mark);
  17429. top_mark:=bot_mark; add_token_ref(top_mark);
  17430. delete_token_ref(first_mark); first_mark:=null;
  17431. end;
  17432. @<Put the \(o)optimal current page into box 255, update |first_mark| and
  17433. |bot_mark|, append insertions to their boxes, and put the
  17434. remaining nodes back on the contribution list@>;
  17435. if (top_mark<>null)and(first_mark=null) then
  17436. begin first_mark:=top_mark; add_token_ref(top_mark);
  17437. end;
  17438. if output_routine<>null then
  17439. if dead_cycles>=max_dead_cycles then
  17440. @<Explain that too many dead cycles have occurred in a row@>
  17441. else @<Fire up the user's output routine and |return|@>;
  17442. @<Perform the default output routine@>;
  17443. exit:end;
  17444. @ @<Set the value of |output_penalty|@>=
  17445. if type(best_page_break)=penalty_node then
  17446. begin geq_word_define(int_base+output_penalty_code,penalty(best_page_break));
  17447. penalty(best_page_break):=inf_penalty;
  17448. end
  17449. else geq_word_define(int_base+output_penalty_code,inf_penalty)
  17450. @ As the page is finally being prepared for output,
  17451. pointer |p| runs through the vlist, with |prev_p| trailing behind;
  17452. pointer |q| is the tail of a list of insertions that
  17453. are being held over for a subsequent page.
  17454. @<Put the \(o)optimal current page into box 255...@>=
  17455. if c=best_page_break then best_page_break:=null; {|c| not yet linked in}
  17456. @<Ensure that box 255 is empty before output@>;
  17457. insert_penalties:=0; {this will count the number of insertions held over}
  17458. save_split_top_skip:=split_top_skip;
  17459. if holding_inserts<=0 then
  17460. @<Prepare all the boxes involved in insertions to act as queues@>;
  17461. q:=hold_head; link(q):=null; prev_p:=page_head; p:=link(prev_p);
  17462. while p<>best_page_break do
  17463. begin if type(p)=ins_node then
  17464. begin if holding_inserts<=0 then
  17465. @<Either insert the material specified by node |p| into the
  17466. appropriate box, or hold it for the next page;
  17467. also delete node |p| from the current page@>;
  17468. end
  17469. else if type(p)=mark_node then @<Update the values of
  17470. |first_mark| and |bot_mark|@>;
  17471. prev_p:=p; p:=link(prev_p);
  17472. end;
  17473. split_top_skip:=save_split_top_skip;
  17474. @<Break the current page at node |p|, put it in box~255,
  17475. and put the remaining nodes on the contribution list@>;
  17476. @<Delete \(t)the page-insertion nodes@>
  17477. @ @<Ensure that box 255 is empty before output@>=
  17478. if box(255)<>null then
  17479. begin print_err(""); print_esc("box"); print("255 is not void");
  17480. @:box255}{\.{\\box255 is not void}@>
  17481. help2("You shouldn't use \box255 except in \output routines.")@/
  17482. ("Proceed, and I'll discard its present contents.");
  17483. box_error(255);
  17484. end
  17485. @ @<Update the values of |first_mark| and |bot_mark|@>=
  17486. begin if first_mark=null then
  17487. begin first_mark:=mark_ptr(p);
  17488. add_token_ref(first_mark);
  17489. end;
  17490. if bot_mark<>null then delete_token_ref(bot_mark);
  17491. bot_mark:=mark_ptr(p); add_token_ref(bot_mark);
  17492. end
  17493. @ When the following code is executed, the current page runs from node
  17494. |link(page_head)| to node |prev_p|, and the nodes from |p| to |page_tail|
  17495. are to be placed back at the front of the contribution list. Furthermore
  17496. the heldover insertions appear in a list from |link(hold_head)| to |q|; we
  17497. will put them into the current page list for safekeeping while the user's
  17498. output routine is active. We might have |q=hold_head|; and |p=null| if
  17499. and only if |prev_p=page_tail|. Error messages are suppressed within
  17500. |vpackage|, since the box might appear to be overfull or underfull simply
  17501. because the stretch and shrink from the \.{\\skip} registers for inserts
  17502. are not actually present in the box.
  17503. @<Break the current page at node |p|, put it...@>=
  17504. if p<>null then
  17505. begin if link(contrib_head)=null then
  17506. if nest_ptr=0 then tail:=page_tail
  17507. else contrib_tail:=page_tail;
  17508. link(page_tail):=link(contrib_head);
  17509. link(contrib_head):=p;
  17510. link(prev_p):=null;
  17511. end;
  17512. save_vbadness:=vbadness; vbadness:=inf_bad;
  17513. save_vfuzz:=vfuzz; vfuzz:=max_dimen; {inhibit error messages}
  17514. box(255):=vpackage(link(page_head),best_size,exactly,page_max_depth);
  17515. vbadness:=save_vbadness; vfuzz:=save_vfuzz;
  17516. if last_glue<>max_halfword then delete_glue_ref(last_glue);
  17517. @<Start a new current page@>; {this sets |last_glue:=max_halfword|}
  17518. if q<>hold_head then
  17519. begin link(page_head):=link(hold_head); page_tail:=q;
  17520. end
  17521. @ If many insertions are supposed to go into the same box, we want to know
  17522. the position of the last node in that box, so that we don't need to waste time
  17523. when linking further information into it. The |last_ins_ptr| fields of the
  17524. page insertion nodes are therefore used for this purpose during the
  17525. packaging phase.
  17526. @<Prepare all the boxes involved in insertions to act as queues@>=
  17527. begin r:=link(page_ins_head);
  17528. while r<>page_ins_head do
  17529. begin if best_ins_ptr(r)<>null then
  17530. begin n:=qo(subtype(r)); ensure_vbox(n);
  17531. if box(n)=null then box(n):=new_null_box;
  17532. p:=box(n)+list_offset;
  17533. while link(p)<>null do p:=link(p);
  17534. last_ins_ptr(r):=p;
  17535. end;
  17536. r:=link(r);
  17537. end;
  17538. end
  17539. @ @<Delete \(t)the page-insertion nodes@>=
  17540. r:=link(page_ins_head);
  17541. while r<>page_ins_head do
  17542. begin q:=link(r); free_node(r,page_ins_node_size); r:=q;
  17543. end;
  17544. link(page_ins_head):=page_ins_head
  17545. @ We will set |best_ins_ptr:=null| and package the box corresponding to
  17546. insertion node~|r|, just after making the final insertion into that box.
  17547. If this final insertion is `|split_up|', the remainder after splitting
  17548. and pruning (if any) will be carried over to the next page.
  17549. @<Either insert the material specified by node |p| into...@>=
  17550. begin r:=link(page_ins_head);
  17551. while subtype(r)<>subtype(p) do r:=link(r);
  17552. if best_ins_ptr(r)=null then wait:=true
  17553. else begin wait:=false; s:=last_ins_ptr(r); link(s):=ins_ptr(p);
  17554. if best_ins_ptr(r)=p then
  17555. @<Wrap up the box specified by node |r|, splitting node |p| if
  17556. called for; set |wait:=true| if node |p| holds a remainder after
  17557. splitting@>
  17558. else begin while link(s)<>null do s:=link(s);
  17559. last_ins_ptr(r):=s;
  17560. end;
  17561. end;
  17562. @<Either append the insertion node |p| after node |q|, and remove it
  17563. from the current page, or delete |node(p)|@>;
  17564. end
  17565. @ @<Wrap up the box specified by node |r|, splitting node |p| if...@>=
  17566. begin if type(r)=split_up then
  17567. if (broken_ins(r)=p)and(broken_ptr(r)<>null) then
  17568. begin while link(s)<>broken_ptr(r) do s:=link(s);
  17569. link(s):=null;
  17570. split_top_skip:=split_top_ptr(p);
  17571. ins_ptr(p):=prune_page_top(broken_ptr(r));
  17572. if ins_ptr(p)<>null then
  17573. begin temp_ptr:=vpack(ins_ptr(p),natural);
  17574. height(p):=height(temp_ptr)+depth(temp_ptr);
  17575. free_node(temp_ptr,box_node_size); wait:=true;
  17576. end;
  17577. end;
  17578. best_ins_ptr(r):=null;
  17579. n:=qo(subtype(r));
  17580. temp_ptr:=list_ptr(box(n));
  17581. free_node(box(n),box_node_size);
  17582. box(n):=vpack(temp_ptr,natural);
  17583. end
  17584. @ @<Either append the insertion node |p|...@>=
  17585. link(prev_p):=link(p); link(p):=null;
  17586. if wait then
  17587. begin link(q):=p; q:=p; incr(insert_penalties);
  17588. end
  17589. else begin delete_glue_ref(split_top_ptr(p));
  17590. free_node(p,ins_node_size);
  17591. end;
  17592. p:=prev_p
  17593. @ The list of heldover insertions, running from |link(page_head)| to
  17594. |page_tail|, must be moved to the contribution list when the user has
  17595. specified no output routine.
  17596. @<Perform the default output routine@>=
  17597. begin if link(page_head)<>null then
  17598. begin if link(contrib_head)=null then
  17599. if nest_ptr=0 then tail:=page_tail@+else contrib_tail:=page_tail
  17600. else link(page_tail):=link(contrib_head);
  17601. link(contrib_head):=link(page_head);
  17602. link(page_head):=null; page_tail:=page_head;
  17603. end;
  17604. ship_out(box(255)); box(255):=null;
  17605. end
  17606. @ @<Explain that too many dead cycles have occurred in a row@>=
  17607. begin print_err("Output loop---"); print_int(dead_cycles);
  17608. @.Output loop...@>
  17609. print(" consecutive dead cycles");
  17610. help3("I've concluded that your \output is awry; it never does a")@/
  17611. ("\shipout, so I'm shipping \box255 out myself. Next time")@/
  17612. ("increase \maxdeadcycles if you want me to be more patient!"); error;
  17613. end
  17614. @ @<Fire up the user's output routine and |return|@>=
  17615. begin output_active:=true;
  17616. incr(dead_cycles);
  17617. push_nest; mode:=-vmode; prev_depth:=ignore_depth; mode_line:=-line;
  17618. begin_token_list(output_routine,output_text);
  17619. new_save_level(output_group); normal_paragraph;
  17620. scan_left_brace;
  17621. return;
  17622. end
  17623. @ When the user's output routine finishes, it has constructed a vlist
  17624. in internal vertical mode, and \TeX\ will do the following:
  17625. @<Resume the page builder after an output routine has come to an end@>=
  17626. begin if (loc<>null) or
  17627. ((token_type<>output_text)and(token_type<>backed_up)) then
  17628. @<Recover from an unbalanced output routine@>;
  17629. end_token_list; {conserve stack space in case more outputs are triggered}
  17630. end_graf; unsave; output_active:=false; insert_penalties:=0;@/
  17631. @<Ensure that box 255 is empty after output@>;
  17632. if tail<>head then {current list goes after heldover insertions}
  17633. begin link(page_tail):=link(head);
  17634. page_tail:=tail;
  17635. end;
  17636. if link(page_head)<>null then {and both go before heldover contributions}
  17637. begin if link(contrib_head)=null then contrib_tail:=page_tail;
  17638. link(page_tail):=link(contrib_head);
  17639. link(contrib_head):=link(page_head);
  17640. link(page_head):=null; page_tail:=page_head;
  17641. end;
  17642. pop_nest; build_page;
  17643. end
  17644. @ @<Recover from an unbalanced output routine@>=
  17645. begin print_err("Unbalanced output routine");
  17646. @.Unbalanced output routine@>
  17647. help2("Your sneaky output routine has problematic {'s and/or }'s.")@/
  17648. ("I can't handle that very well; good luck."); error;
  17649. repeat get_token;
  17650. until loc=null;
  17651. end {loops forever if reading from a file, since |null=min_halfword<=0|}
  17652. @ @<Ensure that box 255 is empty after output@>=
  17653. if box(255)<>null then
  17654. begin print_err("Output routine didn't use all of ");
  17655. print_esc("box"); print_int(255);
  17656. @.Output routine didn't use...@>
  17657. help3("Your \output commands should empty \box255,")@/
  17658. ("e.g., by saying `\shipout\box255'.")@/
  17659. ("Proceed; I'll discard its present contents.");
  17660. box_error(255);
  17661. end
  17662. @* \[46] The chief executive.
  17663. We come now to the |main_control| routine, which contains the master
  17664. switch that causes all the various pieces of \TeX\ to do their things,
  17665. in the right order.
  17666. In a sense, this is the grand climax of the program: It applies all the
  17667. tools that we have worked so hard to construct. In another sense, this is
  17668. the messiest part of the program: It necessarily refers to other pieces
  17669. of code all over the place, so that a person can't fully understand what is
  17670. going on without paging back and forth to be reminded of conventions that
  17671. are defined elsewhere. We are now at the hub of the web, the central nervous
  17672. system that touches most of the other parts and ties them together.
  17673. @^brain@>
  17674. The structure of |main_control| itself is quite simple. There's a label
  17675. called |big_switch|, at which point the next token of input is fetched
  17676. using |get_x_token|. Then the program branches at high speed into one of
  17677. about 100 possible directions, based on the value of the current
  17678. mode and the newly fetched command code; the sum |abs(mode)+cur_cmd|
  17679. indicates what to do next. For example, the case `|vmode+letter|' arises
  17680. when a letter occurs in vertical mode (or internal vertical mode); this
  17681. case leads to instructions that initialize a new paragraph and enter
  17682. horizontal mode.
  17683. The big |case| statement that contains this multiway switch has been labeled
  17684. |reswitch|, so that the program can |goto reswitch| when the next token
  17685. has already been fetched. Most of the cases are quite short; they call
  17686. an ``action procedure'' that does the work for that case, and then they
  17687. either |goto reswitch| or they ``fall through'' to the end of the |case|
  17688. statement, which returns control back to |big_switch|. Thus, |main_control|
  17689. is not an extremely large procedure, in spite of the multiplicity of things
  17690. it must do; it is small enough to be handled by \PASCAL\ compilers that put
  17691. severe restrictions on procedure size.
  17692. @!@^action procedure@>
  17693. One case is singled out for special treatment, because it accounts for most
  17694. of \TeX's activities in typical applications. The process of reading simple
  17695. text and converting it into |char_node| records, while looking for ligatures
  17696. and kerns, is part of \TeX's ``inner loop''; the whole program runs
  17697. efficiently when its inner loop is fast, so this part has been written
  17698. with particular care.
  17699. @ We shall concentrate first on the inner loop of |main_control|, deferring
  17700. consideration of the other cases until later.
  17701. @^inner loop@>
  17702. @d big_switch=60 {go here to branch on the next token of input}
  17703. @d main_loop=70 {go here to typeset a string of consecutive characters}
  17704. @d main_loop_wrapup=80 {go here to finish a character or ligature}
  17705. @d main_loop_move=90 {go here to advance the ligature cursor}
  17706. @d main_loop_move_lig=95 {same, when advancing past a generated ligature}
  17707. @d main_loop_lookahead=100 {go here to bring in another character, if any}
  17708. @d main_lig_loop=110 {go here to check for ligatures or kerning}
  17709. @d append_normal_space=120 {go here to append a normal space between words}
  17710. @p @t\4@>@<Declare action procedures for use by |main_control|@>@;
  17711. @t\4@>@<Declare the procedure called |handle_right_brace|@>@;
  17712. procedure main_control; {governs \TeX's activities}
  17713. label big_switch,reswitch,main_loop,main_loop_wrapup,
  17714. main_loop_move,main_loop_move+1,main_loop_move+2,main_loop_move_lig,
  17715. main_loop_lookahead,main_loop_lookahead+1,
  17716. main_lig_loop,main_lig_loop+1,main_lig_loop+2,
  17717. append_normal_space,exit;
  17718. var@!t:integer; {general-purpose temporary variable}
  17719. begin if every_job<>null then begin_token_list(every_job,every_job_text);
  17720. big_switch: get_x_token;@/
  17721. reswitch: @<Give diagnostic information, if requested@>;
  17722. case abs(mode)+cur_cmd of
  17723. hmode+letter,hmode+other_char,hmode+char_given: goto main_loop;
  17724. hmode+char_num: begin scan_char_num; cur_chr:=cur_val; goto main_loop;@+end;
  17725. hmode+no_boundary: begin get_x_token;
  17726. if (cur_cmd=letter)or(cur_cmd=other_char)or(cur_cmd=char_given)or
  17727. (cur_cmd=char_num) then cancel_boundary:=true;
  17728. goto reswitch;
  17729. end;
  17730. hmode+spacer: if space_factor=1000 then goto append_normal_space
  17731. else app_space;
  17732. hmode+ex_space,mmode+ex_space: goto append_normal_space;
  17733. @t\4@>@<Cases of |main_control| that are not part of the inner loop@>@;
  17734. end; {of the big |case| statement}
  17735. goto big_switch;
  17736. main_loop:@<Append character |cur_chr| and the following characters (if~any)
  17737. to the current hlist in the current font; |goto reswitch| when
  17738. a non-character has been fetched@>;
  17739. append_normal_space:@<Append a normal inter-word space to the current list,
  17740. then |goto big_switch|@>;
  17741. exit:end;
  17742. @ When a new token has just been fetched at |big_switch|, we have an
  17743. ideal place to monitor \TeX's activity.
  17744. @^debugging@>
  17745. @<Give diagnostic information, if requested@>=
  17746. if interrupt<>0 then if OK_to_interrupt then
  17747. begin back_input; check_interrupt; goto big_switch;
  17748. end;
  17749. @!debug if panicking then check_mem(false);@+@;@+gubed
  17750. if tracing_commands>0 then show_cur_cmd_chr
  17751. @ The following part of the program was first written in a structured
  17752. manner, according to the philosophy that ``premature optimization is
  17753. the root of all evil.'' Then it was rearranged into pieces of
  17754. spaghetti so that the most common actions could proceed with little or
  17755. no redundancy.
  17756. The original unoptimized form of this algorithm resembles the
  17757. |reconstitute| procedure, which was described earlier in connection with
  17758. hyphenation. Again we have an implied ``cursor'' between characters
  17759. |cur_l| and |cur_r|. The main difference is that the |lig_stack| can now
  17760. contain a charnode as well as pseudo-ligatures; that stack is now
  17761. usually nonempty, because the next character of input (if any) has been
  17762. appended to it. In |main_control| we have
  17763. $$|cur_r|=\cases{|character(lig_stack)|,&if |lig_stack>null|;\cr
  17764. |font_bchar[cur_font]|,&otherwise;\cr}$$
  17765. except when |character(lig_stack)=font_false_bchar[cur_font]|.
  17766. Several additional global variables are needed.
  17767. @<Glob...@>=
  17768. @!main_f:internal_font_number; {the current font}
  17769. @!main_i:four_quarters; {character information bytes for |cur_l|}
  17770. @!main_j:four_quarters; {ligature/kern command}
  17771. @!main_k:font_index; {index into |font_info|}
  17772. @!main_p:pointer; {temporary register for list manipulation}
  17773. @!main_s:integer; {space factor value}
  17774. @!bchar:halfword; {boundary character of current font, or |non_char|}
  17775. @!false_bchar:halfword; {nonexistent character matching |bchar|, or |non_char|}
  17776. @!cancel_boundary:boolean; {should the left boundary be ignored?}
  17777. @!ins_disc:boolean; {should we insert a discretionary node?}
  17778. @ The boolean variables of the main loop are normally false, and always reset
  17779. to false before the loop is left. That saves us the extra work of initializing
  17780. each time.
  17781. @<Set init...@>=
  17782. ligature_present:=false; cancel_boundary:=false; lft_hit:=false; rt_hit:=false;
  17783. ins_disc:=false;
  17784. @ We leave the |space_factor| unchanged if |sf_code(cur_chr)=0|; otherwise we
  17785. set it equal to |sf_code(cur_chr)|, except that it should never change
  17786. from a value less than 1000 to a value exceeding 1000. The most common
  17787. case is |sf_code(cur_chr)=1000|, so we want that case to be fast.
  17788. The overall structure of the main loop is presented here. Some program labels
  17789. are inside the individual sections.
  17790. @^inner loop@>
  17791. @d adjust_space_factor==@t@>@;@/
  17792. main_s:=sf_code(cur_chr);
  17793. if main_s=1000 then space_factor:=1000
  17794. else if main_s<1000 then
  17795. begin if main_s>0 then space_factor:=main_s;
  17796. end
  17797. else if space_factor<1000 then space_factor:=1000
  17798. else space_factor:=main_s
  17799. @<Append character |cur_chr|...@>=
  17800. adjust_space_factor;@/
  17801. main_f:=cur_font;
  17802. bchar:=font_bchar[main_f]; false_bchar:=font_false_bchar[main_f];
  17803. if mode>0 then if language<>clang then fix_language;
  17804. fast_get_avail(lig_stack); font(lig_stack):=main_f; cur_l:=qi(cur_chr);
  17805. character(lig_stack):=cur_l;@/
  17806. cur_q:=tail;
  17807. if cancel_boundary then
  17808. begin cancel_boundary:=false; main_k:=non_address;
  17809. end
  17810. else main_k:=bchar_label[main_f];
  17811. if main_k=non_address then goto main_loop_move+2; {no left boundary processing}
  17812. cur_r:=cur_l; cur_l:=non_char;
  17813. goto main_lig_loop+1; {begin with cursor after left boundary}
  17814. @#
  17815. main_loop_wrapup:@<Make a ligature node, if |ligature_present|;
  17816. insert a null discretionary, if appropriate@>;
  17817. main_loop_move:@<If the cursor is immediately followed by the right boundary,
  17818. |goto reswitch|; if it's followed by an invalid character, |goto big_switch|;
  17819. otherwise move the cursor one step to the right and |goto main_lig_loop|@>;
  17820. main_loop_lookahead:@<Look ahead for another character, or leave |lig_stack|
  17821. empty if there's none there@>;
  17822. main_lig_loop:@<If there's a ligature/kern command relevant to |cur_l| and
  17823. |cur_r|, adjust the text appropriately; exit to |main_loop_wrapup|@>;
  17824. main_loop_move_lig:@<Move the cursor past a pseudo-ligature, then
  17825. |goto main_loop_lookahead| or |main_lig_loop|@>
  17826. @ If |link(cur_q)| is nonnull when |wrapup| is invoked, |cur_q| points to
  17827. the list of characters that were consumed while building the ligature
  17828. character~|cur_l|.
  17829. A discretionary break is not inserted for an explicit hyphen when we are in
  17830. restricted horizontal mode. In particular, this avoids putting discretionary
  17831. nodes inside of other discretionaries.
  17832. @^inner loop@>
  17833. @d pack_lig(#)== {the parameter is either |rt_hit| or |false|}
  17834. begin main_p:=new_ligature(main_f,cur_l,link(cur_q));
  17835. if lft_hit then
  17836. begin subtype(main_p):=2; lft_hit:=false;
  17837. end;
  17838. if # then if lig_stack=null then
  17839. begin incr(subtype(main_p)); rt_hit:=false;
  17840. end;
  17841. link(cur_q):=main_p; tail:=main_p; ligature_present:=false;
  17842. end
  17843. @d wrapup(#)==if cur_l<non_char then
  17844. begin if link(cur_q)>null then
  17845. if character(tail)=qi(hyphen_char[main_f]) then ins_disc:=true;
  17846. if ligature_present then pack_lig(#);
  17847. if ins_disc then
  17848. begin ins_disc:=false;
  17849. if mode>0 then tail_append(new_disc);
  17850. end;
  17851. end
  17852. @<Make a ligature node, if |ligature_present|;...@>=
  17853. wrapup(rt_hit)
  17854. @ @<If the cursor is immediately followed by the right boundary...@>=
  17855. @^inner loop@>
  17856. if lig_stack=null then goto reswitch;
  17857. cur_q:=tail; cur_l:=character(lig_stack);
  17858. main_loop_move+1:if not is_char_node(lig_stack) then goto main_loop_move_lig;
  17859. main_loop_move+2:if(cur_chr<font_bc[main_f])or(cur_chr>font_ec[main_f]) then
  17860. begin char_warning(main_f,cur_chr); free_avail(lig_stack); goto big_switch;
  17861. end;
  17862. main_i:=char_info(main_f)(cur_l);
  17863. if not char_exists(main_i) then
  17864. begin char_warning(main_f,cur_chr); free_avail(lig_stack); goto big_switch;
  17865. end;
  17866. link(tail):=lig_stack; tail:=lig_stack {|main_loop_lookahead| is next}
  17867. @ Here we are at |main_loop_move_lig|.
  17868. When we begin this code we have |cur_q=tail| and |cur_l=character(lig_stack)|.
  17869. @<Move the cursor past a pseudo-ligature...@>=
  17870. main_p:=lig_ptr(lig_stack);
  17871. if main_p>null then tail_append(main_p); {append a single character}
  17872. temp_ptr:=lig_stack; lig_stack:=link(temp_ptr);
  17873. free_node(temp_ptr,small_node_size);
  17874. main_i:=char_info(main_f)(cur_l); ligature_present:=true;
  17875. if lig_stack=null then
  17876. if main_p>null then goto main_loop_lookahead
  17877. else cur_r:=bchar
  17878. else cur_r:=character(lig_stack);
  17879. goto main_lig_loop
  17880. @ The result of \.{\\char} can participate in a ligature or kern, so we must
  17881. look ahead for it.
  17882. @<Look ahead for another character...@>=
  17883. get_next; {set only |cur_cmd| and |cur_chr|, for speed}
  17884. if cur_cmd=letter then goto main_loop_lookahead+1;
  17885. if cur_cmd=other_char then goto main_loop_lookahead+1;
  17886. if cur_cmd=char_given then goto main_loop_lookahead+1;
  17887. x_token; {now expand and set |cur_cmd|, |cur_chr|, |cur_tok|}
  17888. if cur_cmd=letter then goto main_loop_lookahead+1;
  17889. if cur_cmd=other_char then goto main_loop_lookahead+1;
  17890. if cur_cmd=char_given then goto main_loop_lookahead+1;
  17891. if cur_cmd=char_num then
  17892. begin scan_char_num; cur_chr:=cur_val; goto main_loop_lookahead+1;
  17893. end;
  17894. if cur_cmd=no_boundary then bchar:=non_char;
  17895. cur_r:=bchar; lig_stack:=null; goto main_lig_loop;
  17896. main_loop_lookahead+1: adjust_space_factor;
  17897. fast_get_avail(lig_stack); font(lig_stack):=main_f;
  17898. cur_r:=qi(cur_chr); character(lig_stack):=cur_r;
  17899. if cur_r=false_bchar then cur_r:=non_char {this prevents spurious ligatures}
  17900. @ Even though comparatively few characters have a lig/kern program, several
  17901. of the instructions here count as part of \TeX's inner loop, since a
  17902. @^inner loop@>
  17903. potentially long sequential search must be performed. For example, tests with
  17904. Computer Modern Roman showed that about 40 per cent of all characters
  17905. actually encountered in practice had a lig/kern program, and that about four
  17906. lig/kern commands were investigated for every such character.
  17907. At the beginning of this code we have |main_i=char_info(main_f)(cur_l)|.
  17908. @<If there's a ligature/kern command...@>=
  17909. if char_tag(main_i)<>lig_tag then goto main_loop_wrapup;
  17910. if cur_r=non_char then goto main_loop_wrapup;
  17911. main_k:=lig_kern_start(main_f)(main_i); main_j:=font_info[main_k].qqqq;
  17912. if skip_byte(main_j)<=stop_flag then goto main_lig_loop+2;
  17913. main_k:=lig_kern_restart(main_f)(main_j);
  17914. main_lig_loop+1:main_j:=font_info[main_k].qqqq;
  17915. main_lig_loop+2:if next_char(main_j)=cur_r then
  17916. if skip_byte(main_j)<=stop_flag then
  17917. @<Do ligature or kern command, returning to |main_lig_loop|
  17918. or |main_loop_wrapup| or |main_loop_move|@>;
  17919. if skip_byte(main_j)=qi(0) then incr(main_k)
  17920. else begin if skip_byte(main_j)>=stop_flag then goto main_loop_wrapup;
  17921. main_k:=main_k+qo(skip_byte(main_j))+1;
  17922. end;
  17923. goto main_lig_loop+1
  17924. @ When a ligature or kern instruction matches a character, we know from
  17925. |read_font_info| that the character exists in the font, even though we
  17926. haven't verified its existence in the normal way.
  17927. This section could be made into a subroutine, if the code inside
  17928. |main_control| needs to be shortened.
  17929. \chardef\?='174 % vertical line to indicate character retention
  17930. @<Do ligature or kern command...@>=
  17931. begin if op_byte(main_j)>=kern_flag then
  17932. begin wrapup(rt_hit);
  17933. tail_append(new_kern(char_kern(main_f)(main_j))); goto main_loop_move;
  17934. end;
  17935. if cur_l=non_char then lft_hit:=true
  17936. else if lig_stack=null then rt_hit:=true;
  17937. check_interrupt; {allow a way out in case there's an infinite ligature loop}
  17938. case op_byte(main_j) of
  17939. qi(1),qi(5):begin cur_l:=rem_byte(main_j); {\.{=:\?}, \.{=:\?>}}
  17940. main_i:=char_info(main_f)(cur_l); ligature_present:=true;
  17941. end;
  17942. qi(2),qi(6):begin cur_r:=rem_byte(main_j); {\.{\?=:}, \.{\?=:>}}
  17943. if lig_stack=null then {right boundary character is being consumed}
  17944. begin lig_stack:=new_lig_item(cur_r); bchar:=non_char;
  17945. end
  17946. else if is_char_node(lig_stack) then {|link(lig_stack)=null|}
  17947. begin main_p:=lig_stack; lig_stack:=new_lig_item(cur_r);
  17948. lig_ptr(lig_stack):=main_p;
  17949. end
  17950. else character(lig_stack):=cur_r;
  17951. end;
  17952. qi(3):begin cur_r:=rem_byte(main_j); {\.{\?=:\?}}
  17953. main_p:=lig_stack; lig_stack:=new_lig_item(cur_r);
  17954. link(lig_stack):=main_p;
  17955. end;
  17956. qi(7),qi(11):begin wrapup(false); {\.{\?=:\?>}, \.{\?=:\?>>}}
  17957. cur_q:=tail; cur_l:=rem_byte(main_j);
  17958. main_i:=char_info(main_f)(cur_l); ligature_present:=true;
  17959. end;
  17960. othercases begin cur_l:=rem_byte(main_j); ligature_present:=true; {\.{=:}}
  17961. if lig_stack=null then goto main_loop_wrapup
  17962. else goto main_loop_move+1;
  17963. end
  17964. endcases;
  17965. if op_byte(main_j)>qi(4) then
  17966. if op_byte(main_j)<>qi(7) then goto main_loop_wrapup;
  17967. if cur_l<non_char then goto main_lig_loop;
  17968. main_k:=bchar_label[main_f]; goto main_lig_loop+1;
  17969. end
  17970. @ The occurrence of blank spaces is almost part of \TeX's inner loop,
  17971. @^inner loop@>
  17972. since we usually encounter about one space for every five non-blank characters.
  17973. Therefore |main_control| gives second-highest priority to ordinary spaces.
  17974. When a glue parameter like \.{\\spaceskip} is set to `\.{0pt}', we will
  17975. see to it later that the corresponding glue specification is precisely
  17976. |zero_glue|, not merely a pointer to some specification that happens
  17977. to be full of zeroes. Therefore it is simple to test whether a glue parameter
  17978. is zero or~not.
  17979. @<Append a normal inter-word space...@>=
  17980. if space_skip=zero_glue then
  17981. begin @<Find the glue specification, |main_p|, for
  17982. text spaces in the current font@>;
  17983. temp_ptr:=new_glue(main_p);
  17984. end
  17985. else temp_ptr:=new_param_glue(space_skip_code);
  17986. link(tail):=temp_ptr; tail:=temp_ptr;
  17987. goto big_switch
  17988. @ Having |font_glue| allocated for each text font saves both time and memory.
  17989. If any of the three spacing parameters are subsequently changed by the
  17990. use of \.{\\fontdimen}, the |find_font_dimen| procedure deallocates the
  17991. |font_glue| specification allocated here.
  17992. @<Find the glue specification...@>=
  17993. begin main_p:=font_glue[cur_font];
  17994. if main_p=null then
  17995. begin main_p:=new_spec(zero_glue); main_k:=param_base[cur_font]+space_code;
  17996. width(main_p):=font_info[main_k].sc; {that's |space(cur_font)|}
  17997. stretch(main_p):=font_info[main_k+1].sc; {and |space_stretch(cur_font)|}
  17998. shrink(main_p):=font_info[main_k+2].sc; {and |space_shrink(cur_font)|}
  17999. font_glue[cur_font]:=main_p;
  18000. end;
  18001. end
  18002. @ @<Declare act...@>=
  18003. procedure app_space; {handle spaces when |space_factor<>1000|}
  18004. var@!q:pointer; {glue node}
  18005. begin if (space_factor>=2000)and(xspace_skip<>zero_glue) then
  18006. q:=new_param_glue(xspace_skip_code)
  18007. else begin if space_skip<>zero_glue then main_p:=space_skip
  18008. else @<Find the glue specification...@>;
  18009. main_p:=new_spec(main_p);
  18010. @<Modify the glue specification in |main_p| according to the space factor@>;
  18011. q:=new_glue(main_p); glue_ref_count(main_p):=null;
  18012. end;
  18013. link(tail):=q; tail:=q;
  18014. end;
  18015. @ @<Modify the glue specification in |main_p| according to the space factor@>=
  18016. if space_factor>=2000 then width(main_p):=width(main_p)+extra_space(cur_font);
  18017. stretch(main_p):=xn_over_d(stretch(main_p),space_factor,1000);
  18018. shrink(main_p):=xn_over_d(shrink(main_p),1000,space_factor)
  18019. @ Whew---that covers the main loop. We can now proceed at a leisurely
  18020. pace through the other combinations of possibilities.
  18021. @d any_mode(#)==vmode+#,hmode+#,mmode+# {for mode-independent commands}
  18022. @<Cases of |main_control| that are not part of the inner loop@>=
  18023. any_mode(relax),vmode+spacer,mmode+spacer,mmode+no_boundary:do_nothing;
  18024. any_mode(ignore_spaces): begin @<Get the next non-blank non-call...@>;
  18025. goto reswitch;
  18026. end;
  18027. vmode+stop: if its_all_over then return; {this is the only way out}
  18028. @t\4@>@<Forbidden cases detected in |main_control|@>@+@,any_mode(mac_param):
  18029. report_illegal_case;
  18030. @<Math-only cases in non-math modes, or vice versa@>: insert_dollar_sign;
  18031. @t\4@>@<Cases of |main_control| that build boxes and lists@>@;
  18032. @t\4@>@<Cases of |main_control| that don't depend on |mode|@>@;
  18033. @t\4@>@<Cases of |main_control| that are for extensions to \TeX@>@;
  18034. @ Here is a list of cases where the user has probably gotten into or out of math
  18035. mode by mistake. \TeX\ will insert a dollar sign and rescan the current token.
  18036. @d non_math(#)==vmode+#,hmode+#
  18037. @<Math-only cases in non-math modes...@>=
  18038. non_math(sup_mark), non_math(sub_mark), non_math(math_char_num),
  18039. non_math(math_given), non_math(math_comp), non_math(delim_num),
  18040. non_math(left_right), non_math(above), non_math(radical),
  18041. non_math(math_style), non_math(math_choice), non_math(vcenter),
  18042. non_math(non_script), non_math(mkern), non_math(limit_switch),
  18043. non_math(mskip), non_math(math_accent),
  18044. mmode+endv, mmode+par_end, mmode+stop, mmode+vskip, mmode+un_vbox,
  18045. mmode+valign, mmode+hrule
  18046. @ @<Declare action...@>=
  18047. procedure insert_dollar_sign;
  18048. begin back_input; cur_tok:=math_shift_token+"$";
  18049. print_err("Missing $ inserted");
  18050. @.Missing \$ inserted@>
  18051. help2("I've inserted a begin-math/end-math symbol since I think")@/
  18052. ("you left one out. Proceed, with fingers crossed."); ins_error;
  18053. end;
  18054. @ When erroneous situations arise, \TeX\ usually issues an error message
  18055. specific to the particular error. For example, `\.{\\noalign}' should
  18056. not appear in any mode, since it is recognized by the |align_peek| routine
  18057. in all of its legitimate appearances; a special error message is given
  18058. when `\.{\\noalign}' occurs elsewhere. But sometimes the most appropriate
  18059. error message is simply that the user is not allowed to do what he or she
  18060. has attempted. For example, `\.{\\moveleft}' is allowed only in vertical mode,
  18061. and `\.{\\lower}' only in non-vertical modes. Such cases are enumerated
  18062. here and in the other sections referred to under `See also \dots.'
  18063. @<Forbidden cases...@>=
  18064. vmode+vmove,hmode+hmove,mmode+hmove,any_mode(last_item),
  18065. @ The `|you_cant|' procedure prints a line saying that the current command
  18066. is illegal in the current mode; it identifies these things symbolically.
  18067. @<Declare action...@>=
  18068. procedure you_cant;
  18069. begin print_err("You can't use `");
  18070. @.You can't use x in y mode@>
  18071. print_cmd_chr(cur_cmd,cur_chr);
  18072. print("' in "); print_mode(mode);
  18073. end;
  18074. @ @<Declare act...@>=
  18075. procedure report_illegal_case;
  18076. begin you_cant;
  18077. help4("Sorry, but I'm not programmed to handle this case;")@/
  18078. ("I'll just pretend that you didn't ask for it.")@/
  18079. ("If you're in the wrong mode, you might be able to")@/
  18080. ("return to the right one by typing `I}' or `I$' or `I\par'.");@/
  18081. error;
  18082. end;
  18083. @ Some operations are allowed only in privileged modes, i.e., in cases
  18084. that |mode>0|. The |privileged| function is used to detect violations
  18085. of this rule; it issues an error message and returns |false| if the
  18086. current |mode| is negative.
  18087. @<Declare act...@>=
  18088. function privileged:boolean;
  18089. begin if mode>0 then privileged:=true
  18090. else begin report_illegal_case; privileged:=false;
  18091. end;
  18092. end;
  18093. @ Either \.{\\dump} or \.{\\end} will cause |main_control| to enter the
  18094. endgame, since both of them have `|stop|' as their command code.
  18095. @<Put each...@>=
  18096. primitive("end",stop,0);@/
  18097. @!@:end_}{\.{\\end} primitive@>
  18098. primitive("dump",stop,1);@/
  18099. @!@:dump_}{\.{\\dump} primitive@>
  18100. @ @<Cases of |print_cmd_chr|...@>=
  18101. stop:if chr_code=1 then print_esc("dump")@+else print_esc("end");
  18102. @ We don't want to leave |main_control| immediately when a |stop| command
  18103. is sensed, because it may be necessary to invoke an \.{\\output} routine
  18104. several times before things really grind to a halt. (The output routine
  18105. might even say `\.{\\gdef\\end\{...\}}', to prolong the life of the job.)
  18106. Therefore |its_all_over| is |true| only when the current page
  18107. and contribution list are empty, and when the last output was not a
  18108. ``dead cycle.''
  18109. @<Declare act...@>=
  18110. function its_all_over:boolean; {do this when \.{\\end} or \.{\\dump} occurs}
  18111. label exit;
  18112. begin if privileged then
  18113. begin if (page_head=page_tail)and(head=tail)and(dead_cycles=0) then
  18114. begin its_all_over:=true; return;
  18115. end;
  18116. back_input; {we will try to end again after ejecting residual material}
  18117. tail_append(new_null_box);
  18118. width(tail):=hsize;
  18119. tail_append(new_glue(fill_glue));
  18120. tail_append(new_penalty(-@'10000000000));@/
  18121. build_page; {append \.{\\hbox to \\hsize\{\}\\vfill\\penalty-'10000000000}}
  18122. end;
  18123. its_all_over:=false;
  18124. exit:end;
  18125. @* \[47] Building boxes and lists.
  18126. The most important parts of |main_control| are concerned with \TeX's
  18127. chief mission of box-making. We need to control the activities that put
  18128. entries on vlists and hlists, as well as the activities that convert
  18129. those lists into boxes. All of the necessary machinery has already been
  18130. developed; it remains for us to ``push the buttons'' at the right times.
  18131. @ As an introduction to these routines, let's consider one of the simplest
  18132. cases: What happens when `\.{\\hrule}' occurs in vertical mode, or
  18133. `\.{\\vrule}' in horizontal mode or math mode? The code in |main_control|
  18134. is short, since the |scan_rule_spec| routine already does most of what is
  18135. required; thus, there is no need for a special action procedure.
  18136. Note that baselineskip calculations are disabled after a rule in vertical
  18137. mode, by setting |prev_depth:=ignore_depth|.
  18138. @<Cases of |main_control| that build...@>=
  18139. vmode+hrule,hmode+vrule,mmode+vrule: begin tail_append(scan_rule_spec);
  18140. if abs(mode)=vmode then prev_depth:=ignore_depth
  18141. else if abs(mode)=hmode then space_factor:=1000;
  18142. end;
  18143. @ The processing of things like \.{\\hskip} and \.{\\vskip} is slightly
  18144. more complicated. But the code in |main_control| is very short, since
  18145. it simply calls on the action routine |append_glue|. Similarly, \.{\\kern}
  18146. activates |append_kern|.
  18147. @<Cases of |main_control| that build...@>=
  18148. vmode+vskip,hmode+hskip,mmode+hskip,mmode+mskip: append_glue;
  18149. any_mode(kern),mmode+mkern: append_kern;
  18150. @ The |hskip| and |vskip| command codes are used for control sequences
  18151. like \.{\\hss} and \.{\\vfil} as well as for \.{\\hskip} and \.{\\vskip}.
  18152. The difference is in the value of |cur_chr|.
  18153. @d fil_code=0 {identifies \.{\\hfil} and \.{\\vfil}}
  18154. @d fill_code=1 {identifies \.{\\hfill} and \.{\\vfill}}
  18155. @d ss_code=2 {identifies \.{\\hss} and \.{\\vss}}
  18156. @d fil_neg_code=3 {identifies \.{\\hfilneg} and \.{\\vfilneg}}
  18157. @d skip_code=4 {identifies \.{\\hskip} and \.{\\vskip}}
  18158. @d mskip_code=5 {identifies \.{\\mskip}}
  18159. @<Put each...@>=
  18160. primitive("hskip",hskip,skip_code);@/
  18161. @!@:hskip_}{\.{\\hskip} primitive@>
  18162. primitive("hfil",hskip,fil_code);
  18163. @!@:hfil_}{\.{\\hfil} primitive@>
  18164. primitive("hfill",hskip,fill_code);@/
  18165. @!@:hfill_}{\.{\\hfill} primitive@>
  18166. primitive("hss",hskip,ss_code);
  18167. @!@:hss_}{\.{\\hss} primitive@>
  18168. primitive("hfilneg",hskip,fil_neg_code);@/
  18169. @!@:hfil_neg_}{\.{\\hfilneg} primitive@>
  18170. primitive("vskip",vskip,skip_code);@/
  18171. @!@:vskip_}{\.{\\vskip} primitive@>
  18172. primitive("vfil",vskip,fil_code);
  18173. @!@:vfil_}{\.{\\vfil} primitive@>
  18174. primitive("vfill",vskip,fill_code);@/
  18175. @!@:vfill_}{\.{\\vfill} primitive@>
  18176. primitive("vss",vskip,ss_code);
  18177. @!@:vss_}{\.{\\vss} primitive@>
  18178. primitive("vfilneg",vskip,fil_neg_code);@/
  18179. @!@:vfil_neg_}{\.{\\vfilneg} primitive@>
  18180. primitive("mskip",mskip,mskip_code);@/
  18181. @!@:mskip_}{\.{\\mskip} primitive@>
  18182. primitive("kern",kern,explicit);
  18183. @!@:kern_}{\.{\\kern} primitive@>
  18184. primitive("mkern",mkern,mu_glue);@/
  18185. @!@:mkern_}{\.{\\mkern} primitive@>
  18186. @ @<Cases of |print_cmd_chr|...@>=
  18187. hskip: case chr_code of
  18188. skip_code:print_esc("hskip");
  18189. fil_code:print_esc("hfil");
  18190. fill_code:print_esc("hfill");
  18191. ss_code:print_esc("hss");
  18192. othercases print_esc("hfilneg")
  18193. endcases;
  18194. vskip: case chr_code of
  18195. skip_code:print_esc("vskip");
  18196. fil_code:print_esc("vfil");
  18197. fill_code:print_esc("vfill");
  18198. ss_code:print_esc("vss");
  18199. othercases print_esc("vfilneg")
  18200. endcases;
  18201. mskip: print_esc("mskip");
  18202. kern: print_esc("kern");
  18203. mkern: print_esc("mkern");
  18204. @ All the work relating to glue creation has been relegated to the
  18205. following subroutine. It does not call |build_page|, because it is
  18206. used in at least one place where that would be a mistake.
  18207. @<Declare action...@>=
  18208. procedure append_glue;
  18209. var s:small_number; {modifier of skip command}
  18210. begin s:=cur_chr;
  18211. case s of
  18212. fil_code: cur_val:=fil_glue;
  18213. fill_code: cur_val:=fill_glue;
  18214. ss_code: cur_val:=ss_glue;
  18215. fil_neg_code: cur_val:=fil_neg_glue;
  18216. skip_code: scan_glue(glue_val);
  18217. mskip_code: scan_glue(mu_val);
  18218. end; {now |cur_val| points to the glue specification}
  18219. tail_append(new_glue(cur_val));
  18220. if s>=skip_code then
  18221. begin decr(glue_ref_count(cur_val));
  18222. if s>skip_code then subtype(tail):=mu_glue;
  18223. end;
  18224. end;
  18225. @ @<Declare act...@>=
  18226. procedure append_kern;
  18227. var s:quarterword; {|subtype| of the kern node}
  18228. begin s:=cur_chr; scan_dimen(s=mu_glue,false,false);
  18229. tail_append(new_kern(cur_val)); subtype(tail):=s;
  18230. end;
  18231. @ Many of the actions related to box-making are triggered by the appearance
  18232. of braces in the input. For example, when the user says `\.{\\hbox}
  18233. \.{to} \.{100pt\{$\langle\,\hbox{\rm hlist}\,\rangle$\}}' in vertical mode,
  18234. the information about the box size (100pt, |exactly|) is put onto |save_stack|
  18235. with a level boundary word just above it, and |cur_group:=adjusted_hbox_group|;
  18236. \TeX\ enters restricted horizontal mode to process the hlist. The right
  18237. brace eventually causes |save_stack| to be restored to its former state,
  18238. at which time the information about the box size (100pt, |exactly|) is
  18239. available once again; a box is packaged and we leave restricted horizontal
  18240. mode, appending the new box to the current list of the enclosing mode
  18241. (in this case to the current list of vertical mode), followed by any
  18242. vertical adjustments that were removed from the box by |hpack|.
  18243. The next few sections of the program are therefore concerned with the
  18244. treatment of left and right curly braces.
  18245. @ If a left brace occurs in the middle of a page or paragraph, it simply
  18246. introduces a new level of grouping, and the matching right brace will not have
  18247. such a drastic effect. Such grouping affects neither the mode nor the
  18248. current list.
  18249. @<Cases of |main_control| that build...@>=
  18250. non_math(left_brace): new_save_level(simple_group);
  18251. any_mode(begin_group): new_save_level(semi_simple_group);
  18252. any_mode(end_group): if cur_group=semi_simple_group then unsave
  18253. else off_save;
  18254. @ We have to deal with errors in which braces and such things are not
  18255. properly nested. Sometimes the user makes an error of commission by
  18256. inserting an extra symbol, but sometimes the user makes an error of omission.
  18257. \TeX\ can't always tell one from the other, so it makes a guess and tries
  18258. to avoid getting into a loop.
  18259. The |off_save| routine is called when the current group code is wrong. It tries
  18260. to insert something into the user's input that will help clean off
  18261. the top level.
  18262. @<Declare act...@>=
  18263. procedure off_save;
  18264. var p:pointer; {inserted token}
  18265. begin if cur_group=bottom_level then
  18266. @<Drop current token and complain that it was unmatched@>
  18267. else begin back_input; p:=get_avail; link(temp_head):=p;
  18268. print_err("Missing ");
  18269. @<Prepare to insert a token that matches |cur_group|,
  18270. and print what it is@>;
  18271. print(" inserted"); ins_list(link(temp_head));
  18272. help5("I've inserted something that you may have forgotten.")@/
  18273. ("(See the <inserted text> above.)")@/
  18274. ("With luck, this will get me unwedged. But if you")@/
  18275. ("really didn't forget anything, try typing `2' now; then")@/
  18276. ("my insertion and my current dilemma will both disappear.");
  18277. error;
  18278. end;
  18279. end;
  18280. @ At this point, |link(temp_head)=p|, a pointer to an empty one-word node.
  18281. @<Prepare to insert a token that matches |cur_group|...@>=
  18282. case cur_group of
  18283. semi_simple_group: begin info(p):=cs_token_flag+frozen_end_group;
  18284. print_esc("endgroup");
  18285. @.Missing \\endgroup inserted@>
  18286. end;
  18287. math_shift_group: begin info(p):=math_shift_token+"$"; print_char("$");
  18288. @.Missing \$ inserted@>
  18289. end;
  18290. math_left_group: begin info(p):=cs_token_flag+frozen_right; link(p):=get_avail;
  18291. p:=link(p); info(p):=other_token+"."; print_esc("right.");
  18292. @.Missing \\right\hbox{.} inserted@>
  18293. @^null delimiter@>
  18294. end;
  18295. othercases begin info(p):=right_brace_token+"}"; print_char("}");
  18296. @.Missing \} inserted@>
  18297. end
  18298. endcases
  18299. @ @<Drop current token and complain that it was unmatched@>=
  18300. begin print_err("Extra "); print_cmd_chr(cur_cmd,cur_chr);
  18301. @.Extra x@>
  18302. help1("Things are pretty mixed up, but I think the worst is over.");@/
  18303. error;
  18304. end
  18305. @ The routine for a |right_brace| character branches into many subcases,
  18306. since a variety of things may happen, depending on |cur_group|. Some
  18307. types of groups are not supposed to be ended by a right brace; error
  18308. messages are given in hopes of pinpointing the problem. Most branches
  18309. of this routine will be filled in later, when we are ready to understand
  18310. them; meanwhile, we must prepare ourselves to deal with such errors.
  18311. @<Cases of |main_control| that build...@>=
  18312. any_mode(right_brace): handle_right_brace;
  18313. @ @<Declare the procedure called |handle_right_brace|@>=
  18314. procedure handle_right_brace;
  18315. var p,@!q:pointer; {for short-term use}
  18316. @!d:scaled; {holds |split_max_depth| in |insert_group|}
  18317. @!f:integer; {holds |floating_penalty| in |insert_group|}
  18318. begin case cur_group of
  18319. simple_group: unsave;
  18320. bottom_level: begin print_err("Too many }'s");
  18321. @.Too many \}'s@>
  18322. help2("You've closed more groups than you opened.")@/
  18323. ("Such booboos are generally harmless, so keep going."); error;
  18324. end;
  18325. semi_simple_group,math_shift_group,math_left_group: extra_right_brace;
  18326. @t\4@>@<Cases of |handle_right_brace| where a |right_brace| triggers
  18327. a delayed action@>@;
  18328. othercases confusion("rightbrace")
  18329. @:this can't happen rightbrace}{\quad rightbrace@>
  18330. endcases;
  18331. end;
  18332. @ @<Declare act...@>=
  18333. procedure extra_right_brace;
  18334. begin print_err("Extra }, or forgotten ");
  18335. @.Extra \}, or forgotten x@>
  18336. case cur_group of
  18337. semi_simple_group: print_esc("endgroup");
  18338. math_shift_group: print_char("$");
  18339. math_left_group: print_esc("right");
  18340. end;@/
  18341. help5("I've deleted a group-closing symbol because it seems to be")@/
  18342. ("spurious, as in `$x}$'. But perhaps the } is legitimate and")@/
  18343. ("you forgot something else, as in `\hbox{$x}'. In such cases")@/
  18344. ("the way to recover is to insert both the forgotten and the")@/
  18345. ("deleted material, e.g., by typing `I$}'."); error;
  18346. incr(align_state);
  18347. end;
  18348. @ Here is where we clear the parameters that are supposed to revert to their
  18349. default values after every paragraph and when internal vertical mode is entered.
  18350. @<Declare act...@>=
  18351. procedure normal_paragraph;
  18352. begin if looseness<>0 then eq_word_define(int_base+looseness_code,0);
  18353. if hang_indent<>0 then eq_word_define(dimen_base+hang_indent_code,0);
  18354. if hang_after<>1 then eq_word_define(int_base+hang_after_code,1);
  18355. if par_shape_ptr<>null then eq_define(par_shape_loc,shape_ref,null);
  18356. end;
  18357. @ Now let's turn to the question of how \.{\\hbox} is treated. We actually
  18358. need to consider also a slightly larger context, since constructions like
  18359. `\.{\\setbox3=}\penalty0\.{\\hbox...}' and
  18360. `\.{\\leaders}\penalty0\.{\\hbox...}' and
  18361. `\.{\\lower3.8pt\\hbox...}'
  18362. are supposed to invoke quite
  18363. different actions after the box has been packaged. Conversely,
  18364. constructions like `\.{\\setbox3=}' can be followed by a variety of
  18365. different kinds of boxes, and we would like to encode such things in an
  18366. efficient way.
  18367. In other words, there are two problems: to represent the context of a box,
  18368. and to represent its type.
  18369. The first problem is solved by putting a ``context code'' on the |save_stack|,
  18370. just below the two entries that give the dimensions produced by |scan_spec|.
  18371. The context code is either a (signed) shift amount, or it is a large
  18372. integer |>=box_flag|, where |box_flag=@t$2^{30}$@>|. Codes |box_flag| through
  18373. |box_flag+255| represent `\.{\\setbox0}' through `\.{\\setbox255}';
  18374. codes |box_flag+256| through |box_flag+511| represent `\.{\\global\\setbox0}'
  18375. through `\.{\\global\\setbox255}';
  18376. code |box_flag+512| represents `\.{\\shipout}'; and codes |box_flag+513|
  18377. through |box_flag+515| represent `\.{\\leaders}', `\.{\\cleaders}',
  18378. and `\.{\\xleaders}'.
  18379. The second problem is solved by giving the command code |make_box| to all
  18380. control sequences that produce a box, and by using the following |chr_code|
  18381. values to distinguish between them: |box_code|, |copy_code|, |last_box_code|,
  18382. |vsplit_code|, |vtop_code|, |vtop_code+vmode|, and |vtop_code+hmode|, where
  18383. the latter two are used to denote \.{\\vbox} and \.{\\hbox}, respectively.
  18384. @d box_flag==@'10000000000 {context code for `\.{\\setbox0}'}
  18385. @d ship_out_flag==box_flag+512 {context code for `\.{\\shipout}'}
  18386. @d leader_flag==box_flag+513 {context code for `\.{\\leaders}'}
  18387. @d box_code=0 {|chr_code| for `\.{\\box}'}
  18388. @d copy_code=1 {|chr_code| for `\.{\\copy}'}
  18389. @d last_box_code=2 {|chr_code| for `\.{\\lastbox}'}
  18390. @d vsplit_code=3 {|chr_code| for `\.{\\vsplit}'}
  18391. @d vtop_code=4 {|chr_code| for `\.{\\vtop}'}
  18392. @<Put each...@>=
  18393. primitive("moveleft",hmove,1);
  18394. @!@:move_left_}{\.{\\moveleft} primitive@>
  18395. primitive("moveright",hmove,0);@/
  18396. @!@:move_right_}{\.{\\moveright} primitive@>
  18397. primitive("raise",vmove,1);
  18398. @!@:raise_}{\.{\\raise} primitive@>
  18399. primitive("lower",vmove,0);
  18400. @!@:lower_}{\.{\\lower} primitive@>
  18401. @#
  18402. primitive("box",make_box,box_code);
  18403. @!@:box_}{\.{\\box} primitive@>
  18404. primitive("copy",make_box,copy_code);
  18405. @!@:copy_}{\.{\\copy} primitive@>
  18406. primitive("lastbox",make_box,last_box_code);
  18407. @!@:last_box_}{\.{\\lastbox} primitive@>
  18408. primitive("vsplit",make_box,vsplit_code);
  18409. @!@:vsplit_}{\.{\\vsplit} primitive@>
  18410. primitive("vtop",make_box,vtop_code);@/
  18411. @!@:vtop_}{\.{\\vtop} primitive@>
  18412. primitive("vbox",make_box,vtop_code+vmode);
  18413. @!@:vbox_}{\.{\\vbox} primitive@>
  18414. primitive("hbox",make_box,vtop_code+hmode);@/
  18415. @!@:hbox_}{\.{\\hbox} primitive@>
  18416. primitive("shipout",leader_ship,a_leaders-1); {|ship_out_flag=leader_flag-1|}
  18417. @!@:ship_out_}{\.{\\shipout} primitive@>
  18418. primitive("leaders",leader_ship,a_leaders);
  18419. @!@:leaders_}{\.{\\leaders} primitive@>
  18420. primitive("cleaders",leader_ship,c_leaders);
  18421. @!@:c_leaders_}{\.{\\cleaders} primitive@>
  18422. primitive("xleaders",leader_ship,x_leaders);
  18423. @!@:x_leaders_}{\.{\\xleaders} primitive@>
  18424. @ @<Cases of |print_cmd_chr|...@>=
  18425. hmove: if chr_code=1 then print_esc("moveleft")@+else print_esc("moveright");
  18426. vmove: if chr_code=1 then print_esc("raise")@+else print_esc("lower");
  18427. make_box: case chr_code of
  18428. box_code: print_esc("box");
  18429. copy_code: print_esc("copy");
  18430. last_box_code: print_esc("lastbox");
  18431. vsplit_code: print_esc("vsplit");
  18432. vtop_code: print_esc("vtop");
  18433. vtop_code+vmode: print_esc("vbox");
  18434. othercases print_esc("hbox")
  18435. endcases;
  18436. leader_ship: if chr_code=a_leaders then print_esc("leaders")
  18437. else if chr_code=c_leaders then print_esc("cleaders")
  18438. else if chr_code=x_leaders then print_esc("xleaders")
  18439. else print_esc("shipout");
  18440. @ Constructions that require a box are started by calling |scan_box| with
  18441. a specified context code. The |scan_box| routine verifies
  18442. that a |make_box| command comes next and then it calls |begin_box|.
  18443. @<Cases of |main_control| that build...@>=
  18444. vmode+hmove,hmode+vmove,mmode+vmove: begin t:=cur_chr;
  18445. scan_normal_dimen;
  18446. if t=0 then scan_box(cur_val)@+else scan_box(-cur_val);
  18447. end;
  18448. any_mode(leader_ship): scan_box(leader_flag-a_leaders+cur_chr);
  18449. any_mode(make_box): begin_box(0);
  18450. @ The global variable |cur_box| will point to a newly made box. If the box
  18451. is void, we will have |cur_box=null|. Otherwise we will have
  18452. |type(cur_box)=hlist_node| or |vlist_node| or |rule_node|; the |rule_node|
  18453. case can occur only with leaders.
  18454. @<Glob...@>=
  18455. @!cur_box:pointer; {box to be placed into its context}
  18456. @ The |box_end| procedure does the right thing with |cur_box|, if
  18457. |box_context| represents the context as explained above.
  18458. @<Declare act...@>=
  18459. procedure box_end(@!box_context:integer);
  18460. var p:pointer; {|ord_noad| for new box in math mode}
  18461. begin if box_context<box_flag then @<Append box |cur_box| to the current list,
  18462. shifted by |box_context|@>
  18463. else if box_context<ship_out_flag then @<Store \(c)|cur_box| in a box register@>
  18464. else if cur_box<>null then
  18465. if box_context>ship_out_flag then @<Append a new leader node that
  18466. uses |cur_box|@>
  18467. else ship_out(cur_box);
  18468. end;
  18469. @ The global variable |adjust_tail| will be non-null if and only if the
  18470. current box might include adjustments that should be appended to the
  18471. current vertical list.
  18472. @<Append box |cur_box| to the current...@>=
  18473. begin if cur_box<>null then
  18474. begin shift_amount(cur_box):=box_context;
  18475. if abs(mode)=vmode then
  18476. begin append_to_vlist(cur_box);
  18477. if adjust_tail<>null then
  18478. begin if adjust_head<>adjust_tail then
  18479. begin link(tail):=link(adjust_head); tail:=adjust_tail;
  18480. end;
  18481. adjust_tail:=null;
  18482. end;
  18483. if mode>0 then build_page;
  18484. end
  18485. else begin if abs(mode)=hmode then space_factor:=1000
  18486. else begin p:=new_noad;
  18487. math_type(nucleus(p)):=sub_box;
  18488. info(nucleus(p)):=cur_box; cur_box:=p;
  18489. end;
  18490. link(tail):=cur_box; tail:=cur_box;
  18491. end;
  18492. end;
  18493. end
  18494. @ @<Store \(c)|cur_box| in a box register@>=
  18495. if box_context<box_flag+256 then
  18496. eq_define(box_base-box_flag+box_context,box_ref,cur_box)
  18497. else geq_define(box_base-box_flag-256+box_context,box_ref,cur_box)
  18498. @ @<Append a new leader node ...@>=
  18499. begin @<Get the next non-blank non-relax...@>;
  18500. if ((cur_cmd=hskip)and(abs(mode)<>vmode))or@|
  18501. ((cur_cmd=vskip)and(abs(mode)=vmode)) then
  18502. begin append_glue; subtype(tail):=box_context-(leader_flag-a_leaders);
  18503. leader_ptr(tail):=cur_box;
  18504. end
  18505. else begin print_err("Leaders not followed by proper glue");
  18506. @.Leaders not followed by...@>
  18507. help3("You should say `\leaders <box or rule><hskip or vskip>'.")@/
  18508. ("I found the <box or rule>, but there's no suitable")@/
  18509. ("<hskip or vskip>, so I'm ignoring these leaders."); back_error;
  18510. flush_node_list(cur_box);
  18511. end;
  18512. end
  18513. @ Now that we can see what eventually happens to boxes, we can consider
  18514. the first steps in their creation. The |begin_box| routine is called when
  18515. |box_context| is a context specification, |cur_chr| specifies the type of
  18516. box desired, and |cur_cmd=make_box|.
  18517. @<Declare act...@>=
  18518. procedure begin_box(@!box_context:integer);
  18519. label exit, done;
  18520. var @!p,@!q:pointer; {run through the current list}
  18521. @!m:quarterword; {the length of a replacement list}
  18522. @!k:halfword; {0 or |vmode| or |hmode|}
  18523. @!n:eight_bits; {a box number}
  18524. begin case cur_chr of
  18525. box_code: begin scan_eight_bit_int; cur_box:=box(cur_val);
  18526. box(cur_val):=null; {the box becomes void, at the same level}
  18527. end;
  18528. copy_code: begin scan_eight_bit_int; cur_box:=copy_node_list(box(cur_val));
  18529. end;
  18530. last_box_code: @<If the current list ends with a box node, delete it from
  18531. the list and make |cur_box| point to it; otherwise set |cur_box:=null|@>;
  18532. vsplit_code: @<Split off part of a vertical box, make |cur_box| point to it@>;
  18533. othercases @<Initiate the construction of an hbox or vbox, then |return|@>
  18534. endcases;@/
  18535. box_end(box_context); {in simple cases, we use the box immediately}
  18536. exit:end;
  18537. @ Note that the condition |not is_char_node(tail)| implies that |head<>tail|,
  18538. since |head| is a one-word node.
  18539. @<If the current list ends with a box node, delete it...@>=
  18540. begin cur_box:=null;
  18541. if abs(mode)=mmode then
  18542. begin you_cant; help1("Sorry; this \lastbox will be void."); error;
  18543. end
  18544. else if (mode=vmode)and(head=tail) then
  18545. begin you_cant;
  18546. help2("Sorry...I usually can't take things from the current page.")@/
  18547. ("This \lastbox will therefore be void."); error;
  18548. end
  18549. else begin if not is_char_node(tail) then
  18550. if (type(tail)=hlist_node)or(type(tail)=vlist_node) then
  18551. @<Remove the last box, unless it's part of a discretionary@>;
  18552. end;
  18553. end
  18554. @ @<Remove the last box...@>=
  18555. begin q:=head;
  18556. repeat p:=q;
  18557. if not is_char_node(q) then if type(q)=disc_node then
  18558. begin for m:=1 to replace_count(q) do p:=link(p);
  18559. if p=tail then goto done;
  18560. end;
  18561. q:=link(p);
  18562. until q=tail;
  18563. cur_box:=tail; shift_amount(cur_box):=0;
  18564. tail:=p; link(p):=null;
  18565. done:end
  18566. @ Here we deal with things like `\.{\\vsplit 13 to 100pt}'.
  18567. @<Split off part of a vertical box, make |cur_box| point to it@>=
  18568. begin scan_eight_bit_int; n:=cur_val;
  18569. if not scan_keyword("to") then
  18570. @.to@>
  18571. begin print_err("Missing `to' inserted");
  18572. @.Missing `to' inserted@>
  18573. help2("I'm working on `\vsplit<box number> to <dimen>';")@/
  18574. ("will look for the <dimen> next."); error;
  18575. end;
  18576. scan_normal_dimen;
  18577. cur_box:=vsplit(n,cur_val);
  18578. end
  18579. @ Here is where we enter restricted horizontal mode or internal vertical
  18580. mode, in order to make a box.
  18581. @<Initiate the construction of an hbox or vbox, then |return|@>=
  18582. begin k:=cur_chr-vtop_code; saved(0):=box_context;
  18583. if k=hmode then
  18584. if (box_context<box_flag)and(abs(mode)=vmode) then
  18585. scan_spec(adjusted_hbox_group,true)
  18586. else scan_spec(hbox_group,true)
  18587. else begin if k=vmode then scan_spec(vbox_group,true)
  18588. else begin scan_spec(vtop_group,true); k:=vmode;
  18589. end;
  18590. normal_paragraph;
  18591. end;
  18592. push_nest; mode:=-k;
  18593. if k=vmode then
  18594. begin prev_depth:=ignore_depth;
  18595. if every_vbox<>null then begin_token_list(every_vbox,every_vbox_text);
  18596. end
  18597. else begin space_factor:=1000;
  18598. if every_hbox<>null then begin_token_list(every_hbox,every_hbox_text);
  18599. end;
  18600. return;
  18601. end
  18602. @ @<Declare act...@>=
  18603. procedure scan_box(@!box_context:integer);
  18604. {the next input should specify a box or perhaps a rule}
  18605. begin @<Get the next non-blank non-relax...@>;
  18606. if cur_cmd=make_box then begin_box(box_context)
  18607. else if (box_context>=leader_flag)and((cur_cmd=hrule)or(cur_cmd=vrule)) then
  18608. begin cur_box:=scan_rule_spec; box_end(box_context);
  18609. end
  18610. else begin@t@>@;@/
  18611. print_err("A <box> was supposed to be here");@/
  18612. @.A <box> was supposed to...@>
  18613. help3("I was expecting to see \hbox or \vbox or \copy or \box or")@/
  18614. ("something like that. So you might find something missing in")@/
  18615. ("your output. But keep trying; you can fix this later."); back_error;
  18616. end;
  18617. end;
  18618. @ When the right brace occurs at the end of an \.{\\hbox} or \.{\\vbox} or
  18619. \.{\\vtop} construction, the |package| routine comes into action. We might
  18620. also have to finish a paragraph that hasn't ended.
  18621. @<Cases of |handle...@>=
  18622. hbox_group: package(0);
  18623. adjusted_hbox_group: begin adjust_tail:=adjust_head; package(0);
  18624. end;
  18625. vbox_group: begin end_graf; package(0);
  18626. end;
  18627. vtop_group: begin end_graf; package(vtop_code);
  18628. end;
  18629. @ @<Declare action...@>=
  18630. procedure package(@!c:small_number);
  18631. var h:scaled; {height of box}
  18632. @!p:pointer; {first node in a box}
  18633. @!d:scaled; {max depth}
  18634. begin d:=box_max_depth; unsave; save_ptr:=save_ptr-3;
  18635. if mode=-hmode then cur_box:=hpack(link(head),saved(2),saved(1))
  18636. else begin cur_box:=vpackage(link(head),saved(2),saved(1),d);
  18637. if c=vtop_code then @<Readjust the height and depth of |cur_box|,
  18638. for \.{\\vtop}@>;
  18639. end;
  18640. pop_nest; box_end(saved(0));
  18641. end;
  18642. @ The height of a `\.{\\vtop}' box is inherited from the first item on its list,
  18643. if that item is an |hlist_node|, |vlist_node|, or |rule_node|; otherwise
  18644. the \.{\\vtop} height is zero.
  18645. @<Readjust the height...@>=
  18646. begin h:=0; p:=list_ptr(cur_box);
  18647. if p<>null then if type(p)<=rule_node then h:=height(p);
  18648. depth(cur_box):=depth(cur_box)-h+height(cur_box); height(cur_box):=h;
  18649. end
  18650. @ A paragraph begins when horizontal-mode material occurs in vertical mode,
  18651. or when the paragraph is explicitly started by `\.{\\indent}' or
  18652. `\.{\\noindent}'.
  18653. @<Put each...@>=
  18654. primitive("indent",start_par,1);
  18655. @!@:indent_}{\.{\\indent} primitive@>
  18656. primitive("noindent",start_par,0);
  18657. @!@:no_indent_}{\.{\\noindent} primitive@>
  18658. @ @<Cases of |print_cmd_chr|...@>=
  18659. start_par: if chr_code=0 then print_esc("noindent")@+ else print_esc("indent");
  18660. @ @<Cases of |main_control| that build...@>=
  18661. vmode+start_par: new_graf(cur_chr>0);
  18662. vmode+letter,vmode+other_char,vmode+char_num,vmode+char_given,
  18663. vmode+math_shift,vmode+un_hbox,vmode+vrule,
  18664. vmode+accent,vmode+discretionary,vmode+hskip,vmode+valign,
  18665. vmode+ex_space,vmode+no_boundary:@t@>@;@/
  18666. begin back_input; new_graf(true);
  18667. end;
  18668. @ @<Declare act...@>=
  18669. function norm_min(@!h:integer):small_number;
  18670. begin if h<=0 then norm_min:=1@+else if h>=63 then norm_min:=63@+
  18671. else norm_min:=h;
  18672. end;
  18673. @#
  18674. procedure new_graf(@!indented:boolean);
  18675. begin prev_graf:=0;
  18676. if (mode=vmode)or(head<>tail) then
  18677. tail_append(new_param_glue(par_skip_code));
  18678. push_nest; mode:=hmode; space_factor:=1000; set_cur_lang; clang:=cur_lang;
  18679. prev_graf:=(norm_min(left_hyphen_min)*@'100+norm_min(right_hyphen_min))
  18680. *@'200000+cur_lang;
  18681. if indented then
  18682. begin tail:=new_null_box; link(head):=tail; width(tail):=par_indent;@+
  18683. end;
  18684. if every_par<>null then begin_token_list(every_par,every_par_text);
  18685. if nest_ptr=1 then build_page; {put |par_skip| glue on current page}
  18686. end;
  18687. @ @<Cases of |main_control| that build...@>=
  18688. hmode+start_par,mmode+start_par: indent_in_hmode;
  18689. @ @<Declare act...@>=
  18690. procedure indent_in_hmode;
  18691. var p,@!q:pointer;
  18692. begin if cur_chr>0 then {\.{\\indent}}
  18693. begin p:=new_null_box; width(p):=par_indent;
  18694. if abs(mode)=hmode then space_factor:=1000
  18695. else begin q:=new_noad; math_type(nucleus(q)):=sub_box;
  18696. info(nucleus(q)):=p; p:=q;
  18697. end;
  18698. tail_append(p);
  18699. end;
  18700. end;
  18701. @ A paragraph ends when a |par_end| command is sensed, or when we are in
  18702. horizontal mode when reaching the right brace of vertical-mode routines
  18703. like \.{\\vbox}, \.{\\insert}, or \.{\\output}.
  18704. @<Cases of |main_control| that build...@>=
  18705. vmode+par_end: begin normal_paragraph;
  18706. if mode>0 then build_page;
  18707. end;
  18708. hmode+par_end: begin if align_state<0 then off_save; {this tries to
  18709. recover from an alignment that didn't end properly}
  18710. end_graf; {this takes us to the enclosing mode, if |mode>0|}
  18711. if mode=vmode then build_page;
  18712. end;
  18713. hmode+stop,hmode+vskip,hmode+hrule,hmode+un_vbox,hmode+halign: head_for_vmode;
  18714. @ @<Declare act...@>=
  18715. procedure head_for_vmode;
  18716. begin if mode<0 then
  18717. if cur_cmd<>hrule then off_save
  18718. else begin print_err("You can't use `");
  18719. print_esc("hrule"); print("' here except with leaders");
  18720. @.You can't use \\hrule...@>
  18721. help2("To put a horizontal rule in an hbox or an alignment,")@/
  18722. ("you should use \leaders or \hrulefill (see The TeXbook).");
  18723. error;
  18724. end
  18725. else begin back_input; cur_tok:=par_token; back_input; token_type:=inserted;
  18726. end;
  18727. end;
  18728. @ @<Declare act...@>=
  18729. procedure end_graf;
  18730. begin if mode=hmode then
  18731. begin if head=tail then pop_nest {null paragraphs are ignored}
  18732. else line_break(widow_penalty);
  18733. normal_paragraph;
  18734. error_count:=0;
  18735. end;
  18736. end;
  18737. @ Insertion and adjustment and mark nodes are constructed by the following
  18738. pieces of the program.
  18739. @<Cases of |main_control| that build...@>=
  18740. any_mode(insert),hmode+vadjust,mmode+vadjust: begin_insert_or_adjust;
  18741. any_mode(mark): make_mark;
  18742. @ @<Forbidden...@>=
  18743. vmode+vadjust,
  18744. @ @<Declare act...@>=
  18745. procedure begin_insert_or_adjust;
  18746. begin if cur_cmd=vadjust then cur_val:=255
  18747. else begin scan_eight_bit_int;
  18748. if cur_val=255 then
  18749. begin print_err("You can't "); print_esc("insert"); print_int(255);
  18750. @.You can't \\insert255@>
  18751. help1("I'm changing to \insert0; box 255 is special.");
  18752. error; cur_val:=0;
  18753. end;
  18754. end;
  18755. saved(0):=cur_val; incr(save_ptr);
  18756. new_save_level(insert_group); scan_left_brace; normal_paragraph;
  18757. push_nest; mode:=-vmode; prev_depth:=ignore_depth;
  18758. end;
  18759. @ @<Cases of |handle...@>=
  18760. insert_group: begin end_graf; q:=split_top_skip; add_glue_ref(q);
  18761. d:=split_max_depth; f:=floating_penalty; unsave; decr(save_ptr);
  18762. {now |saved(0)| is the insertion number, or 255 for |vadjust|}
  18763. p:=vpack(link(head),natural); pop_nest;
  18764. if saved(0)<255 then
  18765. begin tail_append(get_node(ins_node_size));
  18766. type(tail):=ins_node; subtype(tail):=qi(saved(0));
  18767. height(tail):=height(p)+depth(p); ins_ptr(tail):=list_ptr(p);
  18768. split_top_ptr(tail):=q; depth(tail):=d; float_cost(tail):=f;
  18769. end
  18770. else begin tail_append(get_node(small_node_size));
  18771. type(tail):=adjust_node;@/
  18772. subtype(tail):=0; {the |subtype| is not used}
  18773. adjust_ptr(tail):=list_ptr(p); delete_glue_ref(q);
  18774. end;
  18775. free_node(p,box_node_size);
  18776. if nest_ptr=0 then build_page;
  18777. end;
  18778. output_group: @<Resume the page builder...@>;
  18779. @ @<Declare act...@>=
  18780. procedure make_mark;
  18781. var p:pointer; {new node}
  18782. begin p:=scan_toks(false,true); p:=get_node(small_node_size);
  18783. type(p):=mark_node; subtype(p):=0; {the |subtype| is not used}
  18784. mark_ptr(p):=def_ref; link(tail):=p; tail:=p;
  18785. end;
  18786. @ Penalty nodes get into a list via the |break_penalty| command.
  18787. @^penalties@>
  18788. @<Cases of |main_control| that build...@>=
  18789. any_mode(break_penalty): append_penalty;
  18790. @ @<Declare action...@>=
  18791. procedure append_penalty;
  18792. begin scan_int; tail_append(new_penalty(cur_val));
  18793. if mode=vmode then build_page;
  18794. end;
  18795. @ The |remove_item| command removes a penalty, kern, or glue node if it
  18796. appears at the tail of the current list, using a brute-force linear scan.
  18797. Like \.{\\lastbox}, this command is not allowed in vertical mode (except
  18798. internal vertical mode), since the current list in vertical mode is sent
  18799. to the page builder. But if we happen to be able to implement it in
  18800. vertical mode, we do.
  18801. @<Cases of |main_control| that build...@>=
  18802. any_mode(remove_item): delete_last;
  18803. @ When |delete_last| is called, |cur_chr| is the |type| of node that
  18804. will be deleted, if present.
  18805. @<Declare action...@>=
  18806. procedure delete_last;
  18807. label exit;
  18808. var @!p,@!q:pointer; {run through the current list}
  18809. @!m:quarterword; {the length of a replacement list}
  18810. begin if (mode=vmode)and(tail=head) then
  18811. @<Apologize for inability to do the operation now,
  18812. unless \.{\\unskip} follows non-glue@>
  18813. else begin if not is_char_node(tail) then if type(tail)=cur_chr then
  18814. begin q:=head;
  18815. repeat p:=q;
  18816. if not is_char_node(q) then if type(q)=disc_node then
  18817. begin for m:=1 to replace_count(q) do p:=link(p);
  18818. if p=tail then return;
  18819. end;
  18820. q:=link(p);
  18821. until q=tail;
  18822. link(p):=null; flush_node_list(tail); tail:=p;
  18823. end;
  18824. end;
  18825. exit:end;
  18826. @ @<Apologize for inability to do the operation...@>=
  18827. begin if (cur_chr<>glue_node)or(last_glue<>max_halfword) then
  18828. begin you_cant;
  18829. help2("Sorry...I usually can't take things from the current page.")@/
  18830. ("Try `I\vskip-\lastskip' instead.");
  18831. if cur_chr=kern_node then help_line[0]:=
  18832. ("Try `I\kern-\lastkern' instead.")
  18833. else if cur_chr<>glue_node then help_line[0]:=@|
  18834. ("Perhaps you can make the output routine do it.");
  18835. error;
  18836. end;
  18837. end
  18838. @ @<Put each...@>=
  18839. primitive("unpenalty",remove_item,penalty_node);@/
  18840. @!@:un_penalty_}{\.{\\unpenalty} primitive@>
  18841. primitive("unkern",remove_item,kern_node);@/
  18842. @!@:un_kern_}{\.{\\unkern} primitive@>
  18843. primitive("unskip",remove_item,glue_node);@/
  18844. @!@:un_skip_}{\.{\\unskip} primitive@>
  18845. primitive("unhbox",un_hbox,box_code);@/
  18846. @!@:un_hbox_}{\.{\\unhbox} primitive@>
  18847. primitive("unhcopy",un_hbox,copy_code);@/
  18848. @!@:un_hcopy_}{\.{\\unhcopy} primitive@>
  18849. primitive("unvbox",un_vbox,box_code);@/
  18850. @!@:un_vbox_}{\.{\\unvbox} primitive@>
  18851. primitive("unvcopy",un_vbox,copy_code);@/
  18852. @!@:un_vcopy_}{\.{\\unvcopy} primitive@>
  18853. @ @<Cases of |print_cmd_chr|...@>=
  18854. remove_item: if chr_code=glue_node then print_esc("unskip")
  18855. else if chr_code=kern_node then print_esc("unkern")
  18856. else print_esc("unpenalty");
  18857. un_hbox: if chr_code=copy_code then print_esc("unhcopy")
  18858. else print_esc("unhbox");
  18859. un_vbox: if chr_code=copy_code then print_esc("unvcopy")
  18860. else print_esc("unvbox");
  18861. @ The |un_hbox| and |un_vbox| commands unwrap one of the 256 current boxes.
  18862. @<Cases of |main_control| that build...@>=
  18863. vmode+un_vbox,hmode+un_hbox,mmode+un_hbox: unpackage;
  18864. @ @<Declare act...@>=
  18865. procedure unpackage;
  18866. label exit;
  18867. var p:pointer; {the box}
  18868. @!c:box_code..copy_code; {should we copy?}
  18869. begin c:=cur_chr; scan_eight_bit_int; p:=box(cur_val);
  18870. if p=null then return;
  18871. if (abs(mode)=mmode)or((abs(mode)=vmode)and(type(p)<>vlist_node))or@|
  18872. ((abs(mode)=hmode)and(type(p)<>hlist_node)) then
  18873. begin print_err("Incompatible list can't be unboxed");
  18874. @.Incompatible list...@>
  18875. help3("Sorry, Pandora. (You sneaky devil.)")@/
  18876. ("I refuse to unbox an \hbox in vertical mode or vice versa.")@/
  18877. ("And I can't open any boxes in math mode.");@/
  18878. error; return;
  18879. end;
  18880. if c=copy_code then link(tail):=copy_node_list(list_ptr(p))
  18881. else begin link(tail):=list_ptr(p); box(cur_val):=null;
  18882. free_node(p,box_node_size);
  18883. end;
  18884. while link(tail)<>null do tail:=link(tail);
  18885. exit:end;
  18886. @ @<Forbidden...@>=vmode+ital_corr,
  18887. @ Italic corrections are converted to kern nodes when the |ital_corr| command
  18888. follows a character. In math mode the same effect is achieved by appending
  18889. a kern of zero here, since italic corrections are supplied later.
  18890. @<Cases of |main_control| that build...@>=
  18891. hmode+ital_corr: append_italic_correction;
  18892. mmode+ital_corr: tail_append(new_kern(0));
  18893. @ @<Declare act...@>=
  18894. procedure append_italic_correction;
  18895. label exit;
  18896. var p:pointer; {|char_node| at the tail of the current list}
  18897. @!f:internal_font_number; {the font in the |char_node|}
  18898. begin if tail<>head then
  18899. begin if is_char_node(tail) then p:=tail
  18900. else if type(tail)=ligature_node then p:=lig_char(tail)
  18901. else return;
  18902. f:=font(p);
  18903. tail_append(new_kern(char_italic(f)(char_info(f)(character(p)))));
  18904. subtype(tail):=explicit;
  18905. end;
  18906. exit:end;
  18907. @ Discretionary nodes are easy in the common case `\.{\\-}', but in the
  18908. general case we must process three braces full of items.
  18909. @<Put each...@>=
  18910. primitive("-",discretionary,1);
  18911. @!@:Single-character primitives -}{\quad\.{\\-}@>
  18912. primitive("discretionary",discretionary,0);
  18913. @!@:discretionary_}{\.{\\discretionary} primitive@>
  18914. @ @<Cases of |print_cmd_chr|...@>=
  18915. discretionary: if chr_code=1 then
  18916. print_esc("-")@+else print_esc("discretionary");
  18917. @ @<Cases of |main_control| that build...@>=
  18918. hmode+discretionary,mmode+discretionary: append_discretionary;
  18919. @ The space factor does not change when we append a discretionary node,
  18920. but it starts out as 1000 in the subsidiary lists.
  18921. @<Declare act...@>=
  18922. procedure append_discretionary;
  18923. var c:integer; {hyphen character}
  18924. begin tail_append(new_disc);
  18925. if cur_chr=1 then
  18926. begin c:=hyphen_char[cur_font];
  18927. if c>=0 then if c<256 then pre_break(tail):=new_character(cur_font,c);
  18928. end
  18929. else begin incr(save_ptr); saved(-1):=0; new_save_level(disc_group);
  18930. scan_left_brace; push_nest; mode:=-hmode; space_factor:=1000;
  18931. end;
  18932. end;
  18933. @ The three discretionary lists are constructed somewhat as if they were
  18934. hboxes. A~subroutine called |build_discretionary| handles the transitions.
  18935. (This is sort of fun.)
  18936. @<Cases of |handle...@>=
  18937. disc_group: build_discretionary;
  18938. @ @<Declare act...@>=
  18939. procedure build_discretionary;
  18940. label done,exit;
  18941. var p,@!q:pointer; {for link manipulation}
  18942. @!n:integer; {length of discretionary list}
  18943. begin unsave;
  18944. @<Prune the current list, if necessary, until it contains only
  18945. |char_node|, |kern_node|, |hlist_node|, |vlist_node|, |rule_node|,
  18946. and |ligature_node| items; set |n| to the length of the list,
  18947. and set |q| to the list's tail@>;
  18948. p:=link(head); pop_nest;
  18949. case saved(-1) of
  18950. 0:pre_break(tail):=p;
  18951. 1:post_break(tail):=p;
  18952. 2:@<Attach list |p| to the current list, and record its length;
  18953. then finish up and |return|@>;
  18954. end; {there are no other cases}
  18955. incr(saved(-1)); new_save_level(disc_group); scan_left_brace;
  18956. push_nest; mode:=-hmode; space_factor:=1000;
  18957. exit:end;
  18958. @ @<Attach list |p| to the current...@>=
  18959. begin if (n>0)and(abs(mode)=mmode) then
  18960. begin print_err("Illegal math "); print_esc("discretionary");
  18961. @.Illegal math \\disc...@>
  18962. help2("Sorry: The third part of a discretionary break must be")@/
  18963. ("empty, in math formulas. I had to delete your third part.");
  18964. flush_node_list(p); n:=0; error;
  18965. end
  18966. else link(tail):=p;
  18967. if n<=max_quarterword then replace_count(tail):=n
  18968. else begin print_err("Discretionary list is too long");
  18969. @.Discretionary list is too long@>
  18970. help2("Wow---I never thought anybody would tweak me here.")@/
  18971. ("You can't seriously need such a huge discretionary list?");
  18972. error;
  18973. end;
  18974. if n>0 then tail:=q;
  18975. decr(save_ptr); return;
  18976. end
  18977. @ During this loop, |p=link(q)| and there are |n| items preceding |p|.
  18978. @<Prune the current list, if necessary...@>=
  18979. q:=head; p:=link(q); n:=0;
  18980. while p<>null do
  18981. begin if not is_char_node(p) then if type(p)>rule_node then
  18982. if type(p)<>kern_node then if type(p)<>ligature_node then
  18983. begin print_err("Improper discretionary list");
  18984. @.Improper discretionary list@>
  18985. help1("Discretionary lists must contain only boxes and kerns.");@/
  18986. error;
  18987. begin_diagnostic;
  18988. print_nl("The following discretionary sublist has been deleted:");
  18989. @.The following...deleted@>
  18990. show_box(p);
  18991. end_diagnostic(true);
  18992. flush_node_list(p); link(q):=null; goto done;
  18993. end;
  18994. q:=p; p:=link(q); incr(n);
  18995. end;
  18996. done:
  18997. @ We need only one more thing to complete the horizontal mode routines, namely
  18998. the \.{\\accent} primitive.
  18999. @<Cases of |main_control| that build...@>=
  19000. hmode+accent: make_accent;
  19001. @ The positioning of accents is straightforward but tedious. Given an accent
  19002. of width |a|, designed for characters of height |x| and slant |s|;
  19003. and given a character of width |w|, height |h|, and slant |t|: We will shift
  19004. the accent down by |x-h|, and we will insert kern nodes that have the effect of
  19005. centering the accent over the character and shifting the accent to the
  19006. right by $\delta={1\over2}(w-a)+h\cdot t-x\cdot s$. If either character is
  19007. absent from the font, we will simply use the other, without shifting.
  19008. @<Declare act...@>=
  19009. procedure make_accent;
  19010. var s,@!t: real; {amount of slant}
  19011. @!p,@!q,@!r:pointer; {character, box, and kern nodes}
  19012. @!f:internal_font_number; {relevant font}
  19013. @!a,@!h,@!x,@!w,@!delta:scaled; {heights and widths, as explained above}
  19014. @!i:four_quarters; {character information}
  19015. begin scan_char_num; f:=cur_font; p:=new_character(f,cur_val);
  19016. if p<>null then
  19017. begin x:=x_height(f); s:=slant(f)/float_constant(65536);
  19018. @^real division@>
  19019. a:=char_width(f)(char_info(f)(character(p)));@/
  19020. do_assignments;@/
  19021. @<Create a character node |q| for the next character,
  19022. but set |q:=null| if problems arise@>;
  19023. if q<>null then @<Append the accent with appropriate kerns,
  19024. then set |p:=q|@>;
  19025. link(tail):=p; tail:=p; space_factor:=1000;
  19026. end;
  19027. end;
  19028. @ @<Create a character node |q| for the next...@>=
  19029. q:=null; f:=cur_font;
  19030. if (cur_cmd=letter)or(cur_cmd=other_char)or(cur_cmd=char_given) then
  19031. q:=new_character(f,cur_chr)
  19032. else if cur_cmd=char_num then
  19033. begin scan_char_num; q:=new_character(f,cur_val);
  19034. end
  19035. else back_input
  19036. @ The kern nodes appended here must be distinguished from other kerns, lest
  19037. they be wiped away by the hyphenation algorithm or by a previous line break.
  19038. The two kerns are computed with (machine-dependent) |real| arithmetic, but
  19039. their sum is machine-independent; the net effect is machine-independent,
  19040. because the user cannot remove these nodes nor access them via \.{\\lastkern}.
  19041. @<Append the accent with appropriate kerns...@>=
  19042. begin t:=slant(f)/float_constant(65536);
  19043. @^real division@>
  19044. i:=char_info(f)(character(q));
  19045. w:=char_width(f)(i); h:=char_height(f)(height_depth(i));
  19046. if h<>x then {the accent must be shifted up or down}
  19047. begin p:=hpack(p,natural); shift_amount(p):=x-h;
  19048. end;
  19049. delta:=round((w-a)/float_constant(2)+h*t-x*s);
  19050. @^real multiplication@>
  19051. @^real addition@>
  19052. r:=new_kern(delta); subtype(r):=acc_kern; link(tail):=r; link(r):=p;
  19053. tail:=new_kern(-a-delta); subtype(tail):=acc_kern; link(p):=tail; p:=q;
  19054. end
  19055. @ When `\.{\\cr}' or `\.{\\span}' or a tab mark comes through the scanner
  19056. into |main_control|, it might be that the user has foolishly inserted
  19057. one of them into something that has nothing to do with alignment. But it is
  19058. far more likely that a left brace or right brace has been omitted, since
  19059. |get_next| takes actions appropriate to alignment only when `\.{\\cr}'
  19060. or `\.{\\span}' or tab marks occur with |align_state=0|. The following
  19061. program attempts to make an appropriate recovery.
  19062. @<Cases of |main_control| that build...@>=
  19063. any_mode(car_ret), any_mode(tab_mark): align_error;
  19064. any_mode(no_align): no_align_error;
  19065. any_mode(omit): omit_error;
  19066. @ @<Declare act...@>=
  19067. procedure align_error;
  19068. begin if abs(align_state)>2 then
  19069. @<Express consternation over the fact that no alignment is in progress@>
  19070. else begin back_input;
  19071. if align_state<0 then
  19072. begin print_err("Missing { inserted");
  19073. @.Missing \{ inserted@>
  19074. incr(align_state); cur_tok:=left_brace_token+"{";
  19075. end
  19076. else begin print_err("Missing } inserted");
  19077. @.Missing \} inserted@>
  19078. decr(align_state); cur_tok:=right_brace_token+"}";
  19079. end;
  19080. help3("I've put in what seems to be necessary to fix")@/
  19081. ("the current column of the current alignment.")@/
  19082. ("Try to go on, since this might almost work."); ins_error;
  19083. end;
  19084. end;
  19085. @ @<Express consternation...@>=
  19086. begin print_err("Misplaced "); print_cmd_chr(cur_cmd,cur_chr);
  19087. @.Misplaced \&@>
  19088. @.Misplaced \\span@>
  19089. @.Misplaced \\cr@>
  19090. if cur_tok=tab_token+"&" then
  19091. begin help6("I can't figure out why you would want to use a tab mark")@/
  19092. ("here. If you just want an ampersand, the remedy is")@/
  19093. ("simple: Just type `I\&' now. But if some right brace")@/
  19094. ("up above has ended a previous alignment prematurely,")@/
  19095. ("you're probably due for more error messages, and you")@/
  19096. ("might try typing `S' now just to see what is salvageable.");
  19097. end
  19098. else begin help5("I can't figure out why you would want to use a tab mark")@/
  19099. ("or \cr or \span just now. If something like a right brace")@/
  19100. ("up above has ended a previous alignment prematurely,")@/
  19101. ("you're probably due for more error messages, and you")@/
  19102. ("might try typing `S' now just to see what is salvageable.");
  19103. end;
  19104. error;
  19105. end
  19106. @ The help messages here contain a little white lie, since \.{\\noalign}
  19107. and \.{\\omit} are allowed also after `\.{\\noalign\{...\}}'.
  19108. @<Declare act...@>=
  19109. procedure no_align_error;
  19110. begin print_err("Misplaced "); print_esc("noalign");
  19111. @.Misplaced \\noalign@>
  19112. help2("I expect to see \noalign only after the \cr of")@/
  19113. ("an alignment. Proceed, and I'll ignore this case."); error;
  19114. end;
  19115. procedure omit_error;
  19116. begin print_err("Misplaced "); print_esc("omit");
  19117. @.Misplaced \\omit@>
  19118. help2("I expect to see \omit only after tab marks or the \cr of")@/
  19119. ("an alignment. Proceed, and I'll ignore this case."); error;
  19120. end;
  19121. @ We've now covered most of the abuses of \.{\\halign} and \.{\\valign}.
  19122. Let's take a look at what happens when they are used correctly.
  19123. @<Cases of |main_control| that build...@>=
  19124. vmode+halign,hmode+valign:init_align;
  19125. mmode+halign: if privileged then
  19126. if cur_group=math_shift_group then init_align
  19127. else off_save;
  19128. vmode+endv,hmode+endv: do_endv;
  19129. @ An |align_group| code is supposed to remain on the |save_stack|
  19130. during an entire alignment, until |fin_align| removes it.
  19131. A devious user might force an |endv| command to occur just about anywhere;
  19132. we must defeat such hacks.
  19133. @<Declare act...@>=
  19134. procedure do_endv;
  19135. begin base_ptr:=input_ptr; input_stack[base_ptr]:=cur_input;
  19136. while (input_stack[base_ptr].index_field<>v_template) and
  19137. (input_stack[base_ptr].loc_field=null) and
  19138. (input_stack[base_ptr].state_field=token_list) do decr(base_ptr);
  19139. if (input_stack[base_ptr].index_field<>v_template) or
  19140. (input_stack[base_ptr].loc_field<>null) or
  19141. (input_stack[base_ptr].state_field<>token_list) then
  19142. fatal_error("(interwoven alignment preambles are not allowed)");
  19143. @.interwoven alignment preambles...@>
  19144. if cur_group=align_group then
  19145. begin end_graf;
  19146. if fin_col then fin_row;
  19147. end
  19148. else off_save;
  19149. end;
  19150. @ @<Cases of |handle_right_brace|...@>=
  19151. align_group: begin back_input; cur_tok:=cs_token_flag+frozen_cr;
  19152. print_err("Missing "); print_esc("cr"); print(" inserted");
  19153. @.Missing \\cr inserted@>
  19154. help1("I'm guessing that you meant to end an alignment here.");
  19155. ins_error;
  19156. end;
  19157. @ @<Cases of |handle_right_brace|...@>=
  19158. no_align_group: begin end_graf; unsave; align_peek;
  19159. end;
  19160. @ Finally, \.{\\endcsname} is not supposed to get through to |main_control|.
  19161. @<Cases of |main_control| that build...@>=
  19162. any_mode(end_cs_name): cs_error;
  19163. @ @<Declare act...@>=
  19164. procedure cs_error;
  19165. begin print_err("Extra "); print_esc("endcsname");
  19166. @.Extra \\endcsname@>
  19167. help1("I'm ignoring this, since I wasn't doing a \csname.");
  19168. error;
  19169. end;
  19170. @* \[48] Building math lists.
  19171. The routines that \TeX\ uses to create mlists are similar to those we have
  19172. just seen for the generation of hlists and vlists. But it is necessary to
  19173. make ``noads'' as well as nodes, so the reader should review the
  19174. discussion of math mode data structures before trying to make sense out of
  19175. the following program.
  19176. Here is a little routine that needs to be done whenever a subformula
  19177. is about to be processed. The parameter is a code like |math_group|.
  19178. @<Declare act...@>=
  19179. procedure push_math(@!c:group_code);
  19180. begin push_nest; mode:=-mmode; incompleat_noad:=null; new_save_level(c);
  19181. end;
  19182. @ We get into math mode from horizontal mode when a `\.\$' (i.e., a
  19183. |math_shift| character) is scanned. We must check to see whether this
  19184. `\.\$' is immediately followed by another, in case display math mode is
  19185. called for.
  19186. @<Cases of |main_control| that build...@>=
  19187. hmode+math_shift:init_math;
  19188. @ @<Declare act...@>=
  19189. procedure init_math;
  19190. label reswitch,found,not_found,done;
  19191. var w:scaled; {new or partial |pre_display_size|}
  19192. @!l:scaled; {new |display_width|}
  19193. @!s:scaled; {new |display_indent|}
  19194. @!p:pointer; {current node when calculating |pre_display_size|}
  19195. @!q:pointer; {glue specification when calculating |pre_display_size|}
  19196. @!f:internal_font_number; {font in current |char_node|}
  19197. @!n:integer; {scope of paragraph shape specification}
  19198. @!v:scaled; {|w| plus possible glue amount}
  19199. @!d:scaled; {increment to |v|}
  19200. begin get_token; {|get_x_token| would fail on \.{\\ifmmode}\thinspace!}
  19201. if (cur_cmd=math_shift)and(mode>0) then @<Go into display math mode@>
  19202. else begin back_input; @<Go into ordinary math mode@>;
  19203. end;
  19204. end;
  19205. @ @<Go into ordinary math mode@>=
  19206. begin push_math(math_shift_group); eq_word_define(int_base+cur_fam_code,-1);
  19207. if every_math<>null then begin_token_list(every_math,every_math_text);
  19208. end
  19209. @ We get into ordinary math mode from display math mode when `\.{\\eqno}' or
  19210. `\.{\\leqno}' appears. In such cases |cur_chr| will be 0 or~1, respectively;
  19211. the value of |cur_chr| is placed onto |save_stack| for safe keeping.
  19212. @<Cases of |main_control| that build...@>=
  19213. mmode+eq_no: if privileged then
  19214. if cur_group=math_shift_group then start_eq_no
  19215. else off_save;
  19216. @ @<Put each...@>=
  19217. primitive("eqno",eq_no,0);
  19218. @!@:eq_no_}{\.{\\eqno} primitive@>
  19219. primitive("leqno",eq_no,1);
  19220. @!@:leq_no_}{\.{\\leqno} primitive@>
  19221. @ When \TeX\ is in display math mode, |cur_group=math_shift_group|,
  19222. so it is not necessary for the |start_eq_no| procedure to test for
  19223. this condition.
  19224. @<Declare act...@>=
  19225. procedure start_eq_no;
  19226. begin saved(0):=cur_chr; incr(save_ptr);
  19227. @<Go into ordinary math mode@>;
  19228. end;
  19229. @ @<Cases of |print_cmd_chr|...@>=
  19230. eq_no:if chr_code=1 then print_esc("leqno")@+else print_esc("eqno");
  19231. @ @<Forbidden...@>=non_math(eq_no),
  19232. @ When we enter display math mode, we need to call |line_break| to
  19233. process the partial paragraph that has just been interrupted by the
  19234. display. Then we can set the proper values of |display_width| and
  19235. |display_indent| and |pre_display_size|.
  19236. @<Go into display math mode@>=
  19237. begin if head=tail then {`\.{\\noindent\$\$}' or `\.{\$\${ }\$\$}'}
  19238. begin pop_nest; w:=-max_dimen;
  19239. end
  19240. else begin line_break(display_widow_penalty);@/
  19241. @<Calculate the natural width, |w|, by which the characters of the
  19242. final line extend to the right of the reference point,
  19243. plus two ems; or set |w:=max_dimen| if the non-blank information
  19244. on that line is affected by stretching or shrinking@>;
  19245. end;
  19246. {now we are in vertical mode, working on the list that will contain the display}
  19247. @<Calculate the length, |l|, and the shift amount, |s|, of the display lines@>;
  19248. push_math(math_shift_group); mode:=mmode;
  19249. eq_word_define(int_base+cur_fam_code,-1);@/
  19250. eq_word_define(dimen_base+pre_display_size_code,w);
  19251. eq_word_define(dimen_base+display_width_code,l);
  19252. eq_word_define(dimen_base+display_indent_code,s);
  19253. if every_display<>null then begin_token_list(every_display,every_display_text);
  19254. if nest_ptr=1 then build_page;
  19255. end
  19256. @ @<Calculate the natural width, |w|, by which...@>=
  19257. v:=shift_amount(just_box)+2*quad(cur_font); w:=-max_dimen;
  19258. p:=list_ptr(just_box);
  19259. while p<>null do
  19260. begin @<Let |d| be the natural width of node |p|;
  19261. if the node is ``visible,'' |goto found|;
  19262. if the node is glue that stretches or shrinks, set |v:=max_dimen|@>;
  19263. if v<max_dimen then v:=v+d;
  19264. goto not_found;
  19265. found: if v<max_dimen then
  19266. begin v:=v+d; w:=v;
  19267. end
  19268. else begin w:=max_dimen; goto done;
  19269. end;
  19270. not_found: p:=link(p);
  19271. end;
  19272. done:
  19273. @ @<Let |d| be the natural width of node |p|...@>=
  19274. reswitch: if is_char_node(p) then
  19275. begin f:=font(p); d:=char_width(f)(char_info(f)(character(p)));
  19276. goto found;
  19277. end;
  19278. case type(p) of
  19279. hlist_node,vlist_node,rule_node: begin d:=width(p); goto found;
  19280. end;
  19281. ligature_node:@<Make node |p| look like a |char_node|...@>;
  19282. kern_node,math_node: d:=width(p);
  19283. glue_node:@<Let |d| be the natural width of this glue; if stretching
  19284. or shrinking, set |v:=max_dimen|; |goto found| in the case of leaders@>;
  19285. whatsit_node: @<Let |d| be the width of the whatsit |p|@>;
  19286. othercases d:=0
  19287. endcases
  19288. @ We need to be careful that |w|, |v|, and |d| do not depend on any |glue_set|
  19289. values, since such values are subject to system-dependent rounding.
  19290. System-dependent numbers are not allowed to infiltrate parameters like
  19291. |pre_display_size|, since \TeX82 is supposed to make the same decisions on all
  19292. machines.
  19293. @<Let |d| be the natural width of this glue...@>=
  19294. begin q:=glue_ptr(p); d:=width(q);
  19295. if glue_sign(just_box)=stretching then
  19296. begin if (glue_order(just_box)=stretch_order(q))and@|
  19297. (stretch(q)<>0) then
  19298. v:=max_dimen;
  19299. end
  19300. else if glue_sign(just_box)=shrinking then
  19301. begin if (glue_order(just_box)=shrink_order(q))and@|
  19302. (shrink(q)<>0) then
  19303. v:=max_dimen;
  19304. end;
  19305. if subtype(p)>=a_leaders then goto found;
  19306. end
  19307. @ A displayed equation is considered to be three lines long, so we
  19308. calculate the length and offset of line number |prev_graf+2|.
  19309. @<Calculate the length, |l|, ...@>=
  19310. if par_shape_ptr=null then
  19311. if (hang_indent<>0)and@|
  19312. (((hang_after>=0)and(prev_graf+2>hang_after))or@|
  19313. (prev_graf+1<-hang_after)) then
  19314. begin l:=hsize-abs(hang_indent);
  19315. if hang_indent>0 then s:=hang_indent@+else s:=0;
  19316. end
  19317. else begin l:=hsize; s:=0;
  19318. end
  19319. else begin n:=info(par_shape_ptr);
  19320. if prev_graf+2>=n then p:=par_shape_ptr+2*n
  19321. else p:=par_shape_ptr+2*(prev_graf+2);
  19322. s:=mem[p-1].sc; l:=mem[p].sc;
  19323. end
  19324. @ Subformulas of math formulas cause a new level of math mode to be entered,
  19325. on the semantic nest as well as the save stack. These subformulas arise in
  19326. several ways: (1)~A left brace by itself indicates the beginning of a
  19327. subformula that will be put into a box, thereby freezing its glue and
  19328. preventing line breaks. (2)~A subscript or superscript is treated as a
  19329. subformula if it is not a single character; the same applies to
  19330. the nucleus of things like \.{\\underline}. (3)~The \.{\\left} primitive
  19331. initiates a subformula that will be terminated by a matching \.{\\right}.
  19332. The group codes placed on |save_stack| in these three cases are
  19333. |math_group|, |math_group|, and |math_left_group|, respectively.
  19334. Here is the code that handles case (1); the other cases are not quite as
  19335. trivial, so we shall consider them later.
  19336. @<Cases of |main_control| that build...@>=
  19337. mmode+left_brace: begin tail_append(new_noad);
  19338. back_input; scan_math(nucleus(tail));
  19339. end;
  19340. @ Recall that the |nucleus|, |subscr|, and |supscr| fields in a noad are
  19341. broken down into subfields called |math_type| and either |info| or
  19342. |(fam,character)|. The job of |scan_math| is to figure out what to place
  19343. in one of these principal fields; it looks at the subformula that
  19344. comes next in the input, and places an encoding of that subformula
  19345. into a given word of |mem|.
  19346. @d fam_in_range==((cur_fam>=0)and(cur_fam<16))
  19347. @<Declare act...@>=
  19348. procedure scan_math(@!p:pointer);
  19349. label restart,reswitch,exit;
  19350. var c:integer; {math character code}
  19351. begin restart:@<Get the next non-blank non-relax...@>;
  19352. reswitch:case cur_cmd of
  19353. letter,other_char,char_given: begin c:=ho(math_code(cur_chr));
  19354. if c=@'100000 then
  19355. begin @<Treat |cur_chr| as an active character@>;
  19356. goto restart;
  19357. end;
  19358. end;
  19359. char_num: begin scan_char_num; cur_chr:=cur_val; cur_cmd:=char_given;
  19360. goto reswitch;
  19361. end;
  19362. math_char_num: begin scan_fifteen_bit_int; c:=cur_val;
  19363. end;
  19364. math_given: c:=cur_chr;
  19365. delim_num: begin scan_twenty_seven_bit_int; c:=cur_val div @'10000;
  19366. end;
  19367. othercases @<Scan a subformula enclosed in braces and |return|@>
  19368. endcases;@/
  19369. math_type(p):=math_char; character(p):=qi(c mod 256);
  19370. if (c>=var_code)and fam_in_range then fam(p):=cur_fam
  19371. else fam(p):=(c div 256) mod 16;
  19372. exit:end;
  19373. @ An active character that is an |outer_call| is allowed here.
  19374. @<Treat |cur_chr|...@>=
  19375. begin cur_cs:=cur_chr+active_base;
  19376. cur_cmd:=eq_type(cur_cs); cur_chr:=equiv(cur_cs);
  19377. x_token; back_input;
  19378. end
  19379. @ The pointer |p| is placed on |save_stack| while a complex subformula
  19380. is being scanned.
  19381. @<Scan a subformula...@>=
  19382. begin back_input; scan_left_brace;@/
  19383. saved(0):=p; incr(save_ptr); push_math(math_group); return;
  19384. end
  19385. @ The simplest math formula is, of course, `\.{\${ }\$}', when no noads are
  19386. generated. The next simplest cases involve a single character, e.g.,
  19387. `\.{\$x\$}'. Even though such cases may not seem to be very interesting,
  19388. the reader can perhaps understand how happy the author was when `\.{\$x\$}'
  19389. was first properly typeset by \TeX. The code in this section was used.
  19390. @^Knuth, Donald Ervin@>
  19391. @<Cases of |main_control| that build...@>=
  19392. mmode+letter,mmode+other_char,mmode+char_given:
  19393. set_math_char(ho(math_code(cur_chr)));
  19394. mmode+char_num: begin scan_char_num; cur_chr:=cur_val;
  19395. set_math_char(ho(math_code(cur_chr)));
  19396. end;
  19397. mmode+math_char_num: begin scan_fifteen_bit_int; set_math_char(cur_val);
  19398. end;
  19399. mmode+math_given: set_math_char(cur_chr);
  19400. mmode+delim_num: begin scan_twenty_seven_bit_int;
  19401. set_math_char(cur_val div @'10000);
  19402. end;
  19403. @ The |set_math_char| procedure creates a new noad appropriate to a given
  19404. math code, and appends it to the current mlist. However, if the math code
  19405. is sufficiently large, the |cur_chr| is treated as an active character and
  19406. nothing is appended.
  19407. @<Declare act...@>=
  19408. procedure set_math_char(@!c:integer);
  19409. var p:pointer; {the new noad}
  19410. begin if c>=@'100000 then
  19411. @<Treat |cur_chr|...@>
  19412. else begin p:=new_noad; math_type(nucleus(p)):=math_char;
  19413. character(nucleus(p)):=qi(c mod 256);
  19414. fam(nucleus(p)):=(c div 256) mod 16;
  19415. if c>=var_code then
  19416. begin if fam_in_range then fam(nucleus(p)):=cur_fam;
  19417. type(p):=ord_noad;
  19418. end
  19419. else type(p):=ord_noad+(c div @'10000);
  19420. link(tail):=p; tail:=p;
  19421. end;
  19422. end;
  19423. @ Primitive math operators like \.{\\mathop} and \.{\\underline} are given
  19424. the command code |math_comp|, supplemented by the noad type that they
  19425. generate.
  19426. @<Put each...@>=
  19427. primitive("mathord",math_comp,ord_noad);
  19428. @!@:math_ord_}{\.{\\mathord} primitive@>
  19429. primitive("mathop",math_comp,op_noad);
  19430. @!@:math_op_}{\.{\\mathop} primitive@>
  19431. primitive("mathbin",math_comp,bin_noad);
  19432. @!@:math_bin_}{\.{\\mathbin} primitive@>
  19433. primitive("mathrel",math_comp,rel_noad);
  19434. @!@:math_rel_}{\.{\\mathrel} primitive@>
  19435. primitive("mathopen",math_comp,open_noad);
  19436. @!@:math_open_}{\.{\\mathopen} primitive@>
  19437. primitive("mathclose",math_comp,close_noad);
  19438. @!@:math_close_}{\.{\\mathclose} primitive@>
  19439. primitive("mathpunct",math_comp,punct_noad);
  19440. @!@:math_punct_}{\.{\\mathpunct} primitive@>
  19441. primitive("mathinner",math_comp,inner_noad);
  19442. @!@:math_inner_}{\.{\\mathinner} primitive@>
  19443. primitive("underline",math_comp,under_noad);
  19444. @!@:underline_}{\.{\\underline} primitive@>
  19445. primitive("overline",math_comp,over_noad);@/
  19446. @!@:overline_}{\.{\\overline} primitive@>
  19447. primitive("displaylimits",limit_switch,normal);
  19448. @!@:display_limits_}{\.{\\displaylimits} primitive@>
  19449. primitive("limits",limit_switch,limits);
  19450. @!@:limits_}{\.{\\limits} primitive@>
  19451. primitive("nolimits",limit_switch,no_limits);
  19452. @!@:no_limits_}{\.{\\nolimits} primitive@>
  19453. @ @<Cases of |print_cmd_chr|...@>=
  19454. math_comp: case chr_code of
  19455. ord_noad: print_esc("mathord");
  19456. op_noad: print_esc("mathop");
  19457. bin_noad: print_esc("mathbin");
  19458. rel_noad: print_esc("mathrel");
  19459. open_noad: print_esc("mathopen");
  19460. close_noad: print_esc("mathclose");
  19461. punct_noad: print_esc("mathpunct");
  19462. inner_noad: print_esc("mathinner");
  19463. under_noad: print_esc("underline");
  19464. othercases print_esc("overline")
  19465. endcases;
  19466. limit_switch: if chr_code=limits then print_esc("limits")
  19467. else if chr_code=no_limits then print_esc("nolimits")
  19468. else print_esc("displaylimits");
  19469. @ @<Cases of |main_control| that build...@>=
  19470. mmode+math_comp: begin tail_append(new_noad);
  19471. type(tail):=cur_chr; scan_math(nucleus(tail));
  19472. end;
  19473. mmode+limit_switch: math_limit_switch;
  19474. @ @<Declare act...@>=
  19475. procedure math_limit_switch;
  19476. label exit;
  19477. begin if head<>tail then if type(tail)=op_noad then
  19478. begin subtype(tail):=cur_chr; return;
  19479. end;
  19480. print_err("Limit controls must follow a math operator");
  19481. @.Limit controls must follow...@>
  19482. help1("I'm ignoring this misplaced \limits or \nolimits command."); error;
  19483. exit:end;
  19484. @ Delimiter fields of noads are filled in by the |scan_delimiter| routine.
  19485. The first parameter of this procedure is the |mem| address where the
  19486. delimiter is to be placed; the second tells if this delimiter follows
  19487. \.{\\radical} or not.
  19488. @<Declare act...@>=
  19489. procedure scan_delimiter(@!p:pointer;@!r:boolean);
  19490. begin if r then scan_twenty_seven_bit_int
  19491. else begin @<Get the next non-blank non-relax...@>;
  19492. case cur_cmd of
  19493. letter,other_char: cur_val:=del_code(cur_chr);
  19494. delim_num: scan_twenty_seven_bit_int;
  19495. othercases cur_val:=-1
  19496. endcases;
  19497. end;
  19498. if cur_val<0 then @<Report that an invalid delimiter code is being changed
  19499. to null; set~|cur_val:=0|@>;
  19500. small_fam(p):=(cur_val div @'4000000) mod 16;
  19501. small_char(p):=qi((cur_val div @'10000) mod 256);
  19502. large_fam(p):=(cur_val div 256) mod 16;
  19503. large_char(p):=qi(cur_val mod 256);
  19504. end;
  19505. @ @<Report that an invalid delimiter...@>=
  19506. begin print_err("Missing delimiter (. inserted)");
  19507. @.Missing delimiter...@>
  19508. help6("I was expecting to see something like `(' or `\{' or")@/
  19509. ("`\}' here. If you typed, e.g., `{' instead of `\{', you")@/
  19510. ("should probably delete the `{' by typing `1' now, so that")@/
  19511. ("braces don't get unbalanced. Otherwise just proceed.")@/
  19512. ("Acceptable delimiters are characters whose \delcode is")@/
  19513. ("nonnegative, or you can use `\delimiter <delimiter code>'.");
  19514. back_error; cur_val:=0;
  19515. end
  19516. @ @<Cases of |main_control| that build...@>=
  19517. mmode+radical:math_radical;
  19518. @ @<Declare act...@>=
  19519. procedure math_radical;
  19520. begin tail_append(get_node(radical_noad_size));
  19521. type(tail):=radical_noad; subtype(tail):=normal;
  19522. mem[nucleus(tail)].hh:=empty_field;
  19523. mem[subscr(tail)].hh:=empty_field;
  19524. mem[supscr(tail)].hh:=empty_field;
  19525. scan_delimiter(left_delimiter(tail),true); scan_math(nucleus(tail));
  19526. end;
  19527. @ @<Cases of |main_control| that build...@>=
  19528. mmode+accent,mmode+math_accent:math_ac;
  19529. @ @<Declare act...@>=
  19530. procedure math_ac;
  19531. begin if cur_cmd=accent then
  19532. @<Complain that the user should have said \.{\\mathaccent}@>;
  19533. tail_append(get_node(accent_noad_size));
  19534. type(tail):=accent_noad; subtype(tail):=normal;
  19535. mem[nucleus(tail)].hh:=empty_field;
  19536. mem[subscr(tail)].hh:=empty_field;
  19537. mem[supscr(tail)].hh:=empty_field;
  19538. math_type(accent_chr(tail)):=math_char;
  19539. scan_fifteen_bit_int;
  19540. character(accent_chr(tail)):=qi(cur_val mod 256);
  19541. if (cur_val>=var_code)and fam_in_range then fam(accent_chr(tail)):=cur_fam
  19542. else fam(accent_chr(tail)):=(cur_val div 256) mod 16;
  19543. scan_math(nucleus(tail));
  19544. end;
  19545. @ @<Complain that the user should have said \.{\\mathaccent}@>=
  19546. begin print_err("Please use "); print_esc("mathaccent");
  19547. print(" for accents in math mode");
  19548. @.Please use \\mathaccent...@>
  19549. help2("I'm changing \accent to \mathaccent here; wish me luck.")@/
  19550. ("(Accents are not the same in formulas as they are in text.)");
  19551. error;
  19552. end
  19553. @ @<Cases of |main_control| that build...@>=
  19554. mmode+vcenter: begin scan_spec(vcenter_group,false); normal_paragraph;
  19555. push_nest; mode:=-vmode; prev_depth:=ignore_depth;
  19556. if every_vbox<>null then begin_token_list(every_vbox,every_vbox_text);
  19557. end;
  19558. @ @<Cases of |handle...@>=
  19559. vcenter_group: begin end_graf; unsave; save_ptr:=save_ptr-2;
  19560. p:=vpack(link(head),saved(1),saved(0)); pop_nest;
  19561. tail_append(new_noad); type(tail):=vcenter_noad;
  19562. math_type(nucleus(tail)):=sub_box; info(nucleus(tail)):=p;
  19563. end;
  19564. @ The routine that inserts a |style_node| holds no surprises.
  19565. @<Put each...@>=
  19566. primitive("displaystyle",math_style,display_style);
  19567. @!@:display_style_}{\.{\\displaystyle} primitive@>
  19568. primitive("textstyle",math_style,text_style);
  19569. @!@:text_style_}{\.{\\textstyle} primitive@>
  19570. primitive("scriptstyle",math_style,script_style);
  19571. @!@:script_style_}{\.{\\scriptstyle} primitive@>
  19572. primitive("scriptscriptstyle",math_style,script_script_style);
  19573. @!@:script_script_style_}{\.{\\scriptscriptstyle} primitive@>
  19574. @ @<Cases of |print_cmd_chr|...@>=
  19575. math_style: print_style(chr_code);
  19576. @ @<Cases of |main_control| that build...@>=
  19577. mmode+math_style: tail_append(new_style(cur_chr));
  19578. mmode+non_script: begin tail_append(new_glue(zero_glue));
  19579. subtype(tail):=cond_math_glue;
  19580. end;
  19581. mmode+math_choice: append_choices;
  19582. @ The routine that scans the four mlists of a \.{\\mathchoice} is very
  19583. much like the routine that builds discretionary nodes.
  19584. @<Declare act...@>=
  19585. procedure append_choices;
  19586. begin tail_append(new_choice); incr(save_ptr); saved(-1):=0;
  19587. push_math(math_choice_group); scan_left_brace;
  19588. end;
  19589. @ @<Cases of |handle_right_brace|...@>=
  19590. math_choice_group: build_choices;
  19591. @ @<Declare act...@>=
  19592. @t\4@>@<Declare the function called |fin_mlist|@>@t@>@;@/
  19593. procedure build_choices;
  19594. label exit;
  19595. var p:pointer; {the current mlist}
  19596. begin unsave; p:=fin_mlist(null);
  19597. case saved(-1) of
  19598. 0:display_mlist(tail):=p;
  19599. 1:text_mlist(tail):=p;
  19600. 2:script_mlist(tail):=p;
  19601. 3:begin script_script_mlist(tail):=p; decr(save_ptr); return;
  19602. end;
  19603. end; {there are no other cases}
  19604. incr(saved(-1)); push_math(math_choice_group); scan_left_brace;
  19605. exit:end;
  19606. @ Subscripts and superscripts are attached to the previous nucleus by the
  19607. @^superscripts@>@^subscripts@>
  19608. action procedure called |sub_sup|. We use the facts that |sub_mark=sup_mark+1|
  19609. and |subscr(p)=supscr(p)+1|.
  19610. @<Cases of |main_control| that build...@>=
  19611. mmode+sub_mark,mmode+sup_mark: sub_sup;
  19612. @ @<Declare act...@>=
  19613. procedure sub_sup;
  19614. var t:small_number; {type of previous sub/superscript}
  19615. @!p:pointer; {field to be filled by |scan_math|}
  19616. begin t:=empty; p:=null;
  19617. if tail<>head then if scripts_allowed(tail) then
  19618. begin p:=supscr(tail)+cur_cmd-sup_mark; {|supscr| or |subscr|}
  19619. t:=math_type(p);
  19620. end;
  19621. if (p=null)or(t<>empty) then @<Insert a dummy noad to be sub/superscripted@>;
  19622. scan_math(p);
  19623. end;
  19624. @ @<Insert a dummy...@>=
  19625. begin tail_append(new_noad);
  19626. p:=supscr(tail)+cur_cmd-sup_mark; {|supscr| or |subscr|}
  19627. if t<>empty then
  19628. begin if cur_cmd=sup_mark then
  19629. begin print_err("Double superscript");
  19630. @.Double superscript@>
  19631. help1("I treat `x^1^2' essentially like `x^1{}^2'.");
  19632. end
  19633. else begin print_err("Double subscript");
  19634. @.Double subscript@>
  19635. help1("I treat `x_1_2' essentially like `x_1{}_2'.");
  19636. end;
  19637. error;
  19638. end;
  19639. end
  19640. @ An operation like `\.{\\over}' causes the current mlist to go into a
  19641. state of suspended animation: |incompleat_noad| points to a |fraction_noad|
  19642. that contains the mlist-so-far as its numerator, while the denominator
  19643. is yet to come. Finally when the mlist is finished, the denominator will
  19644. go into the incompleat fraction noad, and that noad will become the
  19645. whole formula, unless it is surrounded by `\.{\\left}' and `\.{\\right}'
  19646. delimiters.
  19647. @d above_code=0 { `\.{\\above}' }
  19648. @d over_code=1 { `\.{\\over}' }
  19649. @d atop_code=2 { `\.{\\atop}' }
  19650. @d delimited_code=3 { `\.{\\abovewithdelims}', etc.}
  19651. @<Put each...@>=
  19652. primitive("above",above,above_code);@/
  19653. @!@:above_}{\.{\\above} primitive@>
  19654. primitive("over",above,over_code);@/
  19655. @!@:over_}{\.{\\over} primitive@>
  19656. primitive("atop",above,atop_code);@/
  19657. @!@:atop_}{\.{\\atop} primitive@>
  19658. primitive("abovewithdelims",above,delimited_code+above_code);@/
  19659. @!@:above_with_delims_}{\.{\\abovewithdelims} primitive@>
  19660. primitive("overwithdelims",above,delimited_code+over_code);@/
  19661. @!@:over_with_delims_}{\.{\\overwithdelims} primitive@>
  19662. primitive("atopwithdelims",above,delimited_code+atop_code);
  19663. @!@:atop_with_delims_}{\.{\\atopwithdelims} primitive@>
  19664. @ @<Cases of |print_cmd_chr|...@>=
  19665. above: case chr_code of
  19666. over_code:print_esc("over");
  19667. atop_code:print_esc("atop");
  19668. delimited_code+above_code:print_esc("abovewithdelims");
  19669. delimited_code+over_code:print_esc("overwithdelims");
  19670. delimited_code+atop_code:print_esc("atopwithdelims");
  19671. othercases print_esc("above")
  19672. endcases;
  19673. @ @<Cases of |main_control| that build...@>=
  19674. mmode+above: math_fraction;
  19675. @ @<Declare act...@>=
  19676. procedure math_fraction;
  19677. var c:small_number; {the type of generalized fraction we are scanning}
  19678. begin c:=cur_chr;
  19679. if incompleat_noad<>null then
  19680. @<Ignore the fraction operation and complain about this ambiguous case@>
  19681. else begin incompleat_noad:=get_node(fraction_noad_size);
  19682. type(incompleat_noad):=fraction_noad;
  19683. subtype(incompleat_noad):=normal;
  19684. math_type(numerator(incompleat_noad)):=sub_mlist;
  19685. info(numerator(incompleat_noad)):=link(head);
  19686. mem[denominator(incompleat_noad)].hh:=empty_field;
  19687. mem[left_delimiter(incompleat_noad)].qqqq:=null_delimiter;
  19688. mem[right_delimiter(incompleat_noad)].qqqq:=null_delimiter;@/
  19689. link(head):=null; tail:=head;
  19690. @<Use code |c| to distinguish between generalized fractions@>;
  19691. end;
  19692. end;
  19693. @ @<Use code |c|...@>=
  19694. if c>=delimited_code then
  19695. begin scan_delimiter(left_delimiter(incompleat_noad),false);
  19696. scan_delimiter(right_delimiter(incompleat_noad),false);
  19697. end;
  19698. case c mod delimited_code of
  19699. above_code: begin scan_normal_dimen;
  19700. thickness(incompleat_noad):=cur_val;
  19701. end;
  19702. over_code: thickness(incompleat_noad):=default_code;
  19703. atop_code: thickness(incompleat_noad):=0;
  19704. end {there are no other cases}
  19705. @ @<Ignore the fraction...@>=
  19706. begin if c>=delimited_code then
  19707. begin scan_delimiter(garbage,false); scan_delimiter(garbage,false);
  19708. end;
  19709. if c mod delimited_code=above_code then scan_normal_dimen;
  19710. print_err("Ambiguous; you need another { and }");
  19711. @.Ambiguous...@>
  19712. help3("I'm ignoring this fraction specification, since I don't")@/
  19713. ("know whether a construction like `x \over y \over z'")@/
  19714. ("means `{x \over y} \over z' or `x \over {y \over z}'.");
  19715. error;
  19716. end
  19717. @ At the end of a math formula or subformula, the |fin_mlist| routine is
  19718. called upon to return a pointer to the newly completed mlist, and to
  19719. pop the nest back to the enclosing semantic level. The parameter to
  19720. |fin_mlist|, if not null, points to a |right_noad| that ends the
  19721. current mlist; this |right_noad| has not yet been appended.
  19722. @<Declare the function called |fin_mlist|@>=
  19723. function fin_mlist(@!p:pointer):pointer;
  19724. var q:pointer; {the mlist to return}
  19725. begin if incompleat_noad<>null then @<Compleat the incompleat noad@>
  19726. else begin link(tail):=p; q:=link(head);
  19727. end;
  19728. pop_nest; fin_mlist:=q;
  19729. end;
  19730. @ @<Compleat...@>=
  19731. begin math_type(denominator(incompleat_noad)):=sub_mlist;
  19732. info(denominator(incompleat_noad)):=link(head);
  19733. if p=null then q:=incompleat_noad
  19734. else begin q:=info(numerator(incompleat_noad));
  19735. if type(q)<>left_noad then confusion("right");
  19736. @:this can't happen right}{\quad right@>
  19737. info(numerator(incompleat_noad)):=link(q);
  19738. link(q):=incompleat_noad; link(incompleat_noad):=p;
  19739. end;
  19740. end
  19741. @ Now at last we're ready to see what happens when a right brace occurs
  19742. in a math formula. Two special cases are simplified here: Braces are effectively
  19743. removed when they surround a single Ord without sub/superscripts, or when they
  19744. surround an accent that is the nucleus of an Ord atom.
  19745. @<Cases of |handle...@>=
  19746. math_group: begin unsave; decr(save_ptr);@/
  19747. math_type(saved(0)):=sub_mlist; p:=fin_mlist(null); info(saved(0)):=p;
  19748. if p<>null then if link(p)=null then
  19749. if type(p)=ord_noad then
  19750. begin if math_type(subscr(p))=empty then
  19751. if math_type(supscr(p))=empty then
  19752. begin mem[saved(0)].hh:=mem[nucleus(p)].hh;
  19753. free_node(p,noad_size);
  19754. end;
  19755. end
  19756. else if type(p)=accent_noad then if saved(0)=nucleus(tail) then
  19757. if type(tail)=ord_noad then @<Replace the tail of the list by |p|@>;
  19758. end;
  19759. @ @<Replace the tail...@>=
  19760. begin q:=head; while link(q)<>tail do q:=link(q);
  19761. link(q):=p; free_node(tail,noad_size); tail:=p;
  19762. end
  19763. @ We have dealt with all constructions of math mode except `\.{\\left}' and
  19764. `\.{\\right}', so the picture is completed by the following sections of
  19765. the program.
  19766. @<Put each...@>=
  19767. primitive("left",left_right,left_noad);
  19768. @!@:left_}{\.{\\left} primitive@>
  19769. primitive("right",left_right,right_noad);
  19770. @!@:right_}{\.{\\right} primitive@>
  19771. text(frozen_right):="right"; eqtb[frozen_right]:=eqtb[cur_val];
  19772. @ @<Cases of |print_cmd_chr|...@>=
  19773. left_right: if chr_code=left_noad then print_esc("left")
  19774. else print_esc("right");
  19775. @ @<Cases of |main_control| that build...@>=
  19776. mmode+left_right: math_left_right;
  19777. @ @<Declare act...@>=
  19778. procedure math_left_right;
  19779. var t:small_number; {|left_noad| or |right_noad|}
  19780. @!p:pointer; {new noad}
  19781. begin t:=cur_chr;
  19782. if (t=right_noad)and(cur_group<>math_left_group) then
  19783. @<Try to recover from mismatched \.{\\right}@>
  19784. else begin p:=new_noad; type(p):=t;
  19785. scan_delimiter(delimiter(p),false);
  19786. if t=left_noad then
  19787. begin push_math(math_left_group); link(head):=p; tail:=p;
  19788. end
  19789. else begin p:=fin_mlist(p); unsave; {end of |math_left_group|}
  19790. tail_append(new_noad); type(tail):=inner_noad;
  19791. math_type(nucleus(tail)):=sub_mlist;
  19792. info(nucleus(tail)):=p;
  19793. end;
  19794. end;
  19795. end;
  19796. @ @<Try to recover from mismatch...@>=
  19797. begin if cur_group=math_shift_group then
  19798. begin scan_delimiter(garbage,false);
  19799. print_err("Extra "); print_esc("right");
  19800. @.Extra \\right.@>
  19801. help1("I'm ignoring a \right that had no matching \left.");
  19802. error;
  19803. end
  19804. else off_save;
  19805. end
  19806. @ Here is the only way out of math mode.
  19807. @<Cases of |main_control| that build...@>=
  19808. mmode+math_shift: if cur_group=math_shift_group then after_math
  19809. else off_save;
  19810. @ @<Declare act...@>=
  19811. procedure after_math;
  19812. var l:boolean; {`\.{\\leqno}' instead of `\.{\\eqno}'}
  19813. @!danger:boolean; {not enough symbol fonts are present}
  19814. @!m:integer; {|mmode| or |-mmode|}
  19815. @!p:pointer; {the formula}
  19816. @!a:pointer; {box containing equation number}
  19817. @<Local variables for finishing a displayed formula@>@;
  19818. begin danger:=false;
  19819. @<Check that the necessary fonts for math symbols are present;
  19820. if not, flush the current math lists and set |danger:=true|@>;
  19821. m:=mode; l:=false; p:=fin_mlist(null); {this pops the nest}
  19822. if mode=-m then {end of equation number}
  19823. begin @<Check that another \.\$ follows@>;
  19824. cur_mlist:=p; cur_style:=text_style; mlist_penalties:=false;
  19825. mlist_to_hlist; a:=hpack(link(temp_head),natural);
  19826. unsave; decr(save_ptr); {now |cur_group=math_shift_group|}
  19827. if saved(0)=1 then l:=true;
  19828. danger:=false;
  19829. @<Check that the necessary fonts for math symbols are present;
  19830. if not, flush the current math lists and set |danger:=true|@>;
  19831. m:=mode; p:=fin_mlist(null);
  19832. end
  19833. else a:=null;
  19834. if m<0 then @<Finish math in text@>
  19835. else begin if a=null then @<Check that another \.\$ follows@>;
  19836. @<Finish displayed math@>;
  19837. end;
  19838. end;
  19839. @ @<Check that the necessary fonts...@>=
  19840. if (font_params[fam_fnt(2+text_size)]<total_mathsy_params)or@|
  19841. (font_params[fam_fnt(2+script_size)]<total_mathsy_params)or@|
  19842. (font_params[fam_fnt(2+script_script_size)]<total_mathsy_params) then
  19843. begin print_err("Math formula deleted: Insufficient symbol fonts");@/
  19844. @.Math formula deleted...@>
  19845. help3("Sorry, but I can't typeset math unless \textfont 2")@/
  19846. ("and \scriptfont 2 and \scriptscriptfont 2 have all")@/
  19847. ("the \fontdimen values needed in math symbol fonts.");
  19848. error; flush_math; danger:=true;
  19849. end
  19850. else if (font_params[fam_fnt(3+text_size)]<total_mathex_params)or@|
  19851. (font_params[fam_fnt(3+script_size)]<total_mathex_params)or@|
  19852. (font_params[fam_fnt(3+script_script_size)]<total_mathex_params) then
  19853. begin print_err("Math formula deleted: Insufficient extension fonts");@/
  19854. help3("Sorry, but I can't typeset math unless \textfont 3")@/
  19855. ("and \scriptfont 3 and \scriptscriptfont 3 have all")@/
  19856. ("the \fontdimen values needed in math extension fonts.");
  19857. error; flush_math; danger:=true;
  19858. end
  19859. @ The |unsave| is done after everything else here; hence an appearance of
  19860. `\.{\\mathsurround}' inside of `\.{\$...\$}' affects the spacing at these
  19861. particular \.\$'s. This is consistent with the conventions of
  19862. `\.{\$\$...\$\$}', since `\.{\\abovedisplayskip}' inside a display affects the
  19863. space above that display.
  19864. @<Finish math in text@>=
  19865. begin tail_append(new_math(math_surround,before));
  19866. cur_mlist:=p; cur_style:=text_style; mlist_penalties:=(mode>0); mlist_to_hlist;
  19867. link(tail):=link(temp_head);
  19868. while link(tail)<>null do tail:=link(tail);
  19869. tail_append(new_math(math_surround,after));
  19870. space_factor:=1000; unsave;
  19871. end
  19872. @ \TeX\ gets to the following part of the program when the first `\.\$' ending
  19873. a display has been scanned.
  19874. @<Check that another \.\$ follows@>=
  19875. begin get_x_token;
  19876. if cur_cmd<>math_shift then
  19877. begin print_err("Display math should end with $$");
  19878. @.Display math...with \$\$@>
  19879. help2("The `$' that I just saw supposedly matches a previous `$$'.")@/
  19880. ("So I shall assume that you typed `$$' both times.");
  19881. back_error;
  19882. end;
  19883. end
  19884. @ We have saved the worst for last: The fussiest part of math mode processing
  19885. occurs when a displayed formula is being centered and placed with an optional
  19886. equation number.
  19887. @<Local variables for finishing...@>=
  19888. @!b:pointer; {box containing the equation}
  19889. @!w:scaled; {width of the equation}
  19890. @!z:scaled; {width of the line}
  19891. @!e:scaled; {width of equation number}
  19892. @!q:scaled; {width of equation number plus space to separate from equation}
  19893. @!d:scaled; {displacement of equation in the line}
  19894. @!s:scaled; {move the line right this much}
  19895. @!g1,@!g2:small_number; {glue parameter codes for before and after}
  19896. @!r:pointer; {kern node used to position the display}
  19897. @!t:pointer; {tail of adjustment list}
  19898. @ At this time |p| points to the mlist for the formula; |a| is either
  19899. |null| or it points to a box containing the equation number; and we are in
  19900. vertical mode (or internal vertical mode).
  19901. @<Finish displayed math@>=
  19902. cur_mlist:=p; cur_style:=display_style; mlist_penalties:=false;
  19903. mlist_to_hlist; p:=link(temp_head);@/
  19904. adjust_tail:=adjust_head; b:=hpack(p,natural); p:=list_ptr(b);
  19905. t:=adjust_tail; adjust_tail:=null;@/
  19906. w:=width(b); z:=display_width; s:=display_indent;
  19907. if (a=null)or danger then
  19908. begin e:=0; q:=0;
  19909. end
  19910. else begin e:=width(a); q:=e+math_quad(text_size);
  19911. end;
  19912. if w+q>z then
  19913. @<Squeeze the equation as much as possible; if there is an equation
  19914. number that should go on a separate line by itself,
  19915. set~|e:=0|@>;
  19916. @<Determine the displacement, |d|, of the left edge of the equation, with
  19917. respect to the line size |z|, assuming that |l=false|@>;
  19918. @<Append the glue or equation number preceding the display@>;
  19919. @<Append the display and perhaps also the equation number@>;
  19920. @<Append the glue or equation number following the display@>;
  19921. resume_after_display
  19922. @ @<Declare act...@>=
  19923. procedure resume_after_display;
  19924. begin if cur_group<>math_shift_group then confusion("display");
  19925. @:this can't happen display}{\quad display@>
  19926. unsave; prev_graf:=prev_graf+3;
  19927. push_nest; mode:=hmode; space_factor:=1000; set_cur_lang; clang:=cur_lang;
  19928. prev_graf:=(norm_min(left_hyphen_min)*@'100+norm_min(right_hyphen_min))
  19929. *@'200000+cur_lang;
  19930. @<Scan an optional space@>;
  19931. if nest_ptr=1 then build_page;
  19932. end;
  19933. @ The user can force the equation number to go on a separate line
  19934. by causing its width to be zero.
  19935. @<Squeeze the equation as much as possible...@>=
  19936. begin if (e<>0)and((w-total_shrink[normal]+q<=z)or@|
  19937. (total_shrink[fil]<>0)or(total_shrink[fill]<>0)or
  19938. (total_shrink[filll]<>0)) then
  19939. begin free_node(b,box_node_size);
  19940. b:=hpack(p,z-q,exactly);
  19941. end
  19942. else begin e:=0;
  19943. if w>z then
  19944. begin free_node(b,box_node_size);
  19945. b:=hpack(p,z,exactly);
  19946. end;
  19947. end;
  19948. w:=width(b);
  19949. end
  19950. @ We try first to center the display without regard to the existence of
  19951. the equation number. If that would make it too close (where ``too close''
  19952. means that the space between display and equation number is less than the
  19953. width of the equation number), we either center it in the remaining space
  19954. or move it as far from the equation number as possible. The latter alternative
  19955. is taken only if the display begins with glue, since we assume that the
  19956. user put glue there to control the spacing precisely.
  19957. @<Determine the displacement, |d|, of the left edge of the equation...@>=
  19958. d:=half(z-w);
  19959. if (e>0)and(d<2*e) then {too close}
  19960. begin d:=half(z-w-e);
  19961. if p<>null then if not is_char_node(p) then if type(p)=glue_node then d:=0;
  19962. end
  19963. @ If the equation number is set on a line by itself, either before or
  19964. after the formula, we append an infinite penalty so that no page break will
  19965. separate the display from its number; and we use the same size and
  19966. displacement for all three potential lines of the display, even though
  19967. `\.{\\parshape}' may specify them differently.
  19968. @<Append the glue or equation number preceding the display@>=
  19969. tail_append(new_penalty(pre_display_penalty));@/
  19970. if (d+s<=pre_display_size)or l then {not enough clearance}
  19971. begin g1:=above_display_skip_code; g2:=below_display_skip_code;
  19972. end
  19973. else begin g1:=above_display_short_skip_code;
  19974. g2:=below_display_short_skip_code;
  19975. end;
  19976. if l and(e=0) then {it follows that |type(a)=hlist_node|}
  19977. begin shift_amount(a):=s; append_to_vlist(a);
  19978. tail_append(new_penalty(inf_penalty));
  19979. end
  19980. else tail_append(new_param_glue(g1))
  19981. @ @<Append the display and perhaps also the equation number@>=
  19982. if e<>0 then
  19983. begin r:=new_kern(z-w-e-d);
  19984. if l then
  19985. begin link(a):=r; link(r):=b; b:=a; d:=0;
  19986. end
  19987. else begin link(b):=r; link(r):=a;
  19988. end;
  19989. b:=hpack(b,natural);
  19990. end;
  19991. shift_amount(b):=s+d; append_to_vlist(b)
  19992. @ @<Append the glue or equation number following the display@>=
  19993. if (a<>null)and(e=0)and not l then
  19994. begin tail_append(new_penalty(inf_penalty));
  19995. shift_amount(a):=s+z-width(a);
  19996. append_to_vlist(a);
  19997. g2:=0;
  19998. end;
  19999. if t<>adjust_head then {migrating material comes after equation number}
  20000. begin link(tail):=link(adjust_head); tail:=t;
  20001. end;
  20002. tail_append(new_penalty(post_display_penalty));
  20003. if g2>0 then tail_append(new_param_glue(g2))
  20004. @ When \.{\\halign} appears in a display, the alignment routines operate
  20005. essentially as they do in vertical mode. Then the following program is
  20006. activated, with |p| and |q| pointing to the beginning and end of the
  20007. resulting list, and with |aux_save| holding the |prev_depth| value.
  20008. @<Finish an alignment in a display@>=
  20009. begin do_assignments;
  20010. if cur_cmd<>math_shift then @<Pontificate about improper alignment in display@>
  20011. else @<Check that another \.\$ follows@>;
  20012. pop_nest;
  20013. tail_append(new_penalty(pre_display_penalty));
  20014. tail_append(new_param_glue(above_display_skip_code));
  20015. link(tail):=p;
  20016. if p<>null then tail:=q;
  20017. tail_append(new_penalty(post_display_penalty));
  20018. tail_append(new_param_glue(below_display_skip_code));
  20019. prev_depth:=aux_save.sc; resume_after_display;
  20020. end
  20021. @ @<Pontificate...@>=
  20022. begin print_err("Missing $$ inserted");
  20023. @.Missing {\$\$} inserted@>
  20024. help2("Displays can use special alignments (like \eqalignno)")@/
  20025. ("only if nothing but the alignment itself is between $$'s.");
  20026. back_error;
  20027. end
  20028. @* \[49] Mode-independent processing.
  20029. The long |main_control| procedure has now been fully specified, except for
  20030. certain activities that are independent of the current mode. These activities
  20031. do not change the current vlist or hlist or mlist; if they change anything,
  20032. it is the value of a parameter or the meaning of a control sequence.
  20033. Assignments to values in |eqtb| can be global or local. Furthermore, a
  20034. control sequence can be defined to be `\.{\\long}' or `\.{\\outer}', and
  20035. it might or might not be expanded. The prefixes `\.{\\global}', `\.{\\long}',
  20036. and `\.{\\outer}' can occur in any order. Therefore we assign binary numeric
  20037. codes, making it possible to accumulate the union of all specified prefixes
  20038. by adding the corresponding codes. (\PASCAL's |set| operations could also
  20039. have been used.)
  20040. @<Put each...@>=
  20041. primitive("long",prefix,1);
  20042. @!@:long_}{\.{\\long} primitive@>
  20043. primitive("outer",prefix,2);
  20044. @!@:outer_}{\.{\\outer} primitive@>
  20045. primitive("global",prefix,4);
  20046. @!@:global_}{\.{\\global} primitive@>
  20047. primitive("def",def,0);
  20048. @!@:def_}{\.{\\def} primitive@>
  20049. primitive("gdef",def,1);
  20050. @!@:gdef_}{\.{\\gdef} primitive@>
  20051. primitive("edef",def,2);
  20052. @!@:edef_}{\.{\\edef} primitive@>
  20053. primitive("xdef",def,3);
  20054. @!@:xdef_}{\.{\\xdef} primitive@>
  20055. @ @<Cases of |print_cmd_chr|...@>=
  20056. prefix: if chr_code=1 then print_esc("long")
  20057. else if chr_code=2 then print_esc("outer")
  20058. else print_esc("global");
  20059. def: if chr_code=0 then print_esc("def")
  20060. else if chr_code=1 then print_esc("gdef")
  20061. else if chr_code=2 then print_esc("edef")
  20062. else print_esc("xdef");
  20063. @ Every prefix, and every command code that might or might not be prefixed,
  20064. calls the action procedure |prefixed_command|. This routine accumulates
  20065. a sequence of prefixes until coming to a non-prefix, then it carries out
  20066. the command.
  20067. @<Cases of |main_control| that don't...@>=
  20068. any_mode(toks_register),
  20069. any_mode(assign_toks),
  20070. any_mode(assign_int),
  20071. any_mode(assign_dimen),
  20072. any_mode(assign_glue),
  20073. any_mode(assign_mu_glue),
  20074. any_mode(assign_font_dimen),
  20075. any_mode(assign_font_int),
  20076. any_mode(set_aux),
  20077. any_mode(set_prev_graf),
  20078. any_mode(set_page_dimen),
  20079. any_mode(set_page_int),
  20080. any_mode(set_box_dimen),
  20081. any_mode(set_shape),
  20082. any_mode(def_code),
  20083. any_mode(def_family),
  20084. any_mode(set_font),
  20085. any_mode(def_font),
  20086. any_mode(register),
  20087. any_mode(advance),
  20088. any_mode(multiply),
  20089. any_mode(divide),
  20090. any_mode(prefix),
  20091. any_mode(let),
  20092. any_mode(shorthand_def),
  20093. any_mode(read_to_cs),
  20094. any_mode(def),
  20095. any_mode(set_box),
  20096. any_mode(hyph_data),
  20097. any_mode(set_interaction):prefixed_command;
  20098. @ If the user says, e.g., `\.{\\global\\global}', the redundancy is
  20099. silently accepted.
  20100. @<Declare act...@>=
  20101. @t\4@>@<Declare subprocedures for |prefixed_command|@>@t@>@;@/
  20102. procedure prefixed_command;
  20103. label done,exit;
  20104. var a:small_number; {accumulated prefix codes so far}
  20105. @!f:internal_font_number; {identifies a font}
  20106. @!j:halfword; {index into a \.{\\parshape} specification}
  20107. @!k:font_index; {index into |font_info|}
  20108. @!p,@!q:pointer; {for temporary short-term use}
  20109. @!n:integer; {ditto}
  20110. @!e:boolean; {should a definition be expanded? or was \.{\\let} not done?}
  20111. begin a:=0;
  20112. while cur_cmd=prefix do
  20113. begin if not odd(a div cur_chr) then a:=a+cur_chr;
  20114. @<Get the next non-blank non-relax...@>;
  20115. if cur_cmd<=max_non_prefixed_command then
  20116. @<Discard erroneous prefixes and |return|@>;
  20117. end;
  20118. @<Discard the prefixes \.{\\long} and \.{\\outer} if they are irrelevant@>;
  20119. @<Adjust \(f)for the setting of \.{\\globaldefs}@>;
  20120. case cur_cmd of
  20121. @t\4@>@<Assignments@>@;
  20122. othercases confusion("prefix")
  20123. @:this can't happen prefix}{\quad prefix@>
  20124. endcases;
  20125. done: @<Insert a token saved by \.{\\afterassignment}, if any@>;
  20126. exit:end;
  20127. @ @<Discard erroneous...@>=
  20128. begin print_err("You can't use a prefix with `");
  20129. @.You can't use a prefix with x@>
  20130. print_cmd_chr(cur_cmd,cur_chr); print_char("'");
  20131. help1("I'll pretend you didn't say \long or \outer or \global.");
  20132. back_error; return;
  20133. end
  20134. @ @<Discard the prefixes...@>=
  20135. if (cur_cmd<>def)and(a mod 4<>0) then
  20136. begin print_err("You can't use `"); print_esc("long"); print("' or `");
  20137. print_esc("outer"); print("' with `");
  20138. @.You can't use \\long...@>
  20139. print_cmd_chr(cur_cmd,cur_chr); print_char("'");
  20140. help1("I'll pretend you didn't say \long or \outer here.");
  20141. error;
  20142. end
  20143. @ The previous routine does not have to adjust |a| so that |a mod 4=0|,
  20144. since the following routines test for the \.{\\global} prefix as follows.
  20145. @d global==(a>=4)
  20146. @d define(#)==if global then geq_define(#)@+else eq_define(#)
  20147. @d word_define(#)==if global then geq_word_define(#)@+else eq_word_define(#)
  20148. @<Adjust \(f)for the setting of \.{\\globaldefs}@>=
  20149. if global_defs<>0 then
  20150. if global_defs<0 then
  20151. begin if global then a:=a-4;
  20152. end
  20153. else begin if not global then a:=a+4;
  20154. end
  20155. @ When a control sequence is to be defined, by \.{\\def} or \.{\\let} or
  20156. something similar, the |get_r_token| routine will substitute a special
  20157. control sequence for a token that is not redefinable.
  20158. @<Declare subprocedures for |prefixed_command|@>=
  20159. procedure get_r_token;
  20160. label restart;
  20161. begin restart: repeat get_token;
  20162. until cur_tok<>space_token;
  20163. if (cur_cs=0)or(cur_cs>frozen_control_sequence) then
  20164. begin print_err("Missing control sequence inserted");
  20165. @.Missing control...@>
  20166. help5("Please don't say `\def cs{...}', say `\def\cs{...}'.")@/
  20167. ("I've inserted an inaccessible control sequence so that your")@/
  20168. ("definition will be completed without mixing me up too badly.")@/
  20169. ("You can recover graciously from this error, if you're")@/
  20170. ("careful; see exercise 27.2 in The TeXbook.");
  20171. @:TeXbook}{\sl The \TeX book@>
  20172. if cur_cs=0 then back_input;
  20173. cur_tok:=cs_token_flag+frozen_protection; ins_error; goto restart;
  20174. end;
  20175. end;
  20176. @ @<Initialize table entries...@>=
  20177. text(frozen_protection):="inaccessible";
  20178. @.inaccessible@>
  20179. @ Here's an example of the way many of the following routines operate.
  20180. (Unfortunately, they aren't all as simple as this.)
  20181. @<Assignments@>=
  20182. set_font: define(cur_font_loc,data,cur_chr);
  20183. @ When a |def| command has been scanned,
  20184. |cur_chr| is odd if the definition is supposed to be global, and
  20185. |cur_chr>=2| if the definition is supposed to be expanded.
  20186. @<Assignments@>=
  20187. def: begin if odd(cur_chr)and not global and(global_defs>=0) then a:=a+4;
  20188. e:=(cur_chr>=2); get_r_token; p:=cur_cs;
  20189. q:=scan_toks(true,e); define(p,call+(a mod 4),def_ref);
  20190. end;
  20191. @ Both \.{\\let} and \.{\\futurelet} share the command code |let|.
  20192. @<Put each...@>=
  20193. primitive("let",let,normal);@/
  20194. @!@:let_}{\.{\\let} primitive@>
  20195. primitive("futurelet",let,normal+1);@/
  20196. @!@:future_let_}{\.{\\futurelet} primitive@>
  20197. @ @<Cases of |print_cmd_chr|...@>=
  20198. let: if chr_code<>normal then print_esc("futurelet")@+else print_esc("let");
  20199. @ @<Assignments@>=
  20200. let: begin n:=cur_chr;
  20201. get_r_token; p:=cur_cs;
  20202. if n=normal then
  20203. begin repeat get_token;
  20204. until cur_cmd<>spacer;
  20205. if cur_tok=other_token+"=" then
  20206. begin get_token;
  20207. if cur_cmd=spacer then get_token;
  20208. end;
  20209. end
  20210. else begin get_token; q:=cur_tok; get_token; back_input;
  20211. cur_tok:=q; back_input; {look ahead, then back up}
  20212. end; {note that |back_input| doesn't affect |cur_cmd|, |cur_chr|}
  20213. if cur_cmd>=call then add_token_ref(cur_chr);
  20214. define(p,cur_cmd,cur_chr);
  20215. end;
  20216. @ A \.{\\chardef} creates a control sequence whose |cmd| is |char_given|;
  20217. a \.{\\mathchardef} creates a control sequence whose |cmd| is |math_given|;
  20218. and the corresponding |chr| is the character code or math code. A \.{\\countdef}
  20219. or \.{\\dimendef} or \.{\\skipdef} or \.{\\muskipdef} creates a control
  20220. sequence whose |cmd| is |assign_int| or \dots\ or |assign_mu_glue|, and the
  20221. corresponding |chr| is the |eqtb| location of the internal register in question.
  20222. @d char_def_code=0 {|shorthand_def| for \.{\\chardef}}
  20223. @d math_char_def_code=1 {|shorthand_def| for \.{\\mathchardef}}
  20224. @d count_def_code=2 {|shorthand_def| for \.{\\countdef}}
  20225. @d dimen_def_code=3 {|shorthand_def| for \.{\\dimendef}}
  20226. @d skip_def_code=4 {|shorthand_def| for \.{\\skipdef}}
  20227. @d mu_skip_def_code=5 {|shorthand_def| for \.{\\muskipdef}}
  20228. @d toks_def_code=6 {|shorthand_def| for \.{\\toksdef}}
  20229. @<Put each...@>=
  20230. primitive("chardef",shorthand_def,char_def_code);@/
  20231. @!@:char_def_}{\.{\\chardef} primitive@>
  20232. primitive("mathchardef",shorthand_def,math_char_def_code);@/
  20233. @!@:math_char_def_}{\.{\\mathchardef} primitive@>
  20234. primitive("countdef",shorthand_def,count_def_code);@/
  20235. @!@:count_def_}{\.{\\countdef} primitive@>
  20236. primitive("dimendef",shorthand_def,dimen_def_code);@/
  20237. @!@:dimen_def_}{\.{\\dimendef} primitive@>
  20238. primitive("skipdef",shorthand_def,skip_def_code);@/
  20239. @!@:skip_def_}{\.{\\skipdef} primitive@>
  20240. primitive("muskipdef",shorthand_def,mu_skip_def_code);@/
  20241. @!@:mu_skip_def_}{\.{\\muskipdef} primitive@>
  20242. primitive("toksdef",shorthand_def,toks_def_code);@/
  20243. @!@:toks_def_}{\.{\\toksdef} primitive@>
  20244. @ @<Cases of |print_cmd_chr|...@>=
  20245. shorthand_def: case chr_code of
  20246. char_def_code: print_esc("chardef");
  20247. math_char_def_code: print_esc("mathchardef");
  20248. count_def_code: print_esc("countdef");
  20249. dimen_def_code: print_esc("dimendef");
  20250. skip_def_code: print_esc("skipdef");
  20251. mu_skip_def_code: print_esc("muskipdef");
  20252. othercases print_esc("toksdef")
  20253. endcases;
  20254. char_given: begin print_esc("char"); print_hex(chr_code);
  20255. end;
  20256. math_given: begin print_esc("mathchar"); print_hex(chr_code);
  20257. end;
  20258. @ We temporarily define |p| to be |relax|, so that an occurrence of |p|
  20259. while scanning the definition will simply stop the scanning instead of
  20260. producing an ``undefined control sequence'' error or expanding the
  20261. previous meaning. This allows, for instance, `\.{\\chardef\\foo=123\\foo}'.
  20262. @<Assignments@>=
  20263. shorthand_def: begin n:=cur_chr; get_r_token; p:=cur_cs; define(p,relax,256);
  20264. scan_optional_equals;
  20265. case n of
  20266. char_def_code: begin scan_char_num; define(p,char_given,cur_val);
  20267. end;
  20268. math_char_def_code: begin scan_fifteen_bit_int; define(p,math_given,cur_val);
  20269. end;
  20270. othercases begin scan_eight_bit_int;
  20271. case n of
  20272. count_def_code: define(p,assign_int,count_base+cur_val);
  20273. dimen_def_code: define(p,assign_dimen,scaled_base+cur_val);
  20274. skip_def_code: define(p,assign_glue,skip_base+cur_val);
  20275. mu_skip_def_code: define(p,assign_mu_glue,mu_skip_base+cur_val);
  20276. toks_def_code: define(p,assign_toks,toks_base+cur_val);
  20277. end; {there are no other cases}
  20278. end
  20279. endcases;
  20280. end;
  20281. @ @<Assignments@>=
  20282. read_to_cs: begin scan_int; n:=cur_val;
  20283. if not scan_keyword("to") then
  20284. @.to@>
  20285. begin print_err("Missing `to' inserted");
  20286. @.Missing `to'...@>
  20287. help2("You should have said `\read<number> to \cs'.")@/
  20288. ("I'm going to look for the \cs now."); error;
  20289. end;
  20290. get_r_token;
  20291. p:=cur_cs; read_toks(n,p); define(p,call,cur_val);
  20292. end;
  20293. @ The token-list parameters, \.{\\output} and \.{\\everypar}, etc., receive
  20294. their values in the following way. (For safety's sake, we place an
  20295. enclosing pair of braces around an \.{\\output} list.)
  20296. @<Assignments@>=
  20297. toks_register,assign_toks: begin q:=cur_cs;
  20298. if cur_cmd=toks_register then
  20299. begin scan_eight_bit_int; p:=toks_base+cur_val;
  20300. end
  20301. else p:=cur_chr; {|p=every_par_loc| or |output_routine_loc| or \dots}
  20302. scan_optional_equals;
  20303. @<Get the next non-blank non-relax non-call token@>;
  20304. if cur_cmd<>left_brace then @<If the right-hand side is a token parameter
  20305. or token register, finish the assignment and |goto done|@>;
  20306. back_input; cur_cs:=q; q:=scan_toks(false,false);
  20307. if link(def_ref)=null then {empty list: revert to the default}
  20308. begin define(p,undefined_cs,null); free_avail(def_ref);
  20309. end
  20310. else begin if p=output_routine_loc then {enclose in curlies}
  20311. begin link(q):=get_avail; q:=link(q);
  20312. info(q):=right_brace_token+"}";
  20313. q:=get_avail; info(q):=left_brace_token+"{";
  20314. link(q):=link(def_ref); link(def_ref):=q;
  20315. end;
  20316. define(p,call,def_ref);
  20317. end;
  20318. end;
  20319. @ @<If the right-hand side is a token parameter...@>=
  20320. begin if cur_cmd=toks_register then
  20321. begin scan_eight_bit_int; cur_cmd:=assign_toks; cur_chr:=toks_base+cur_val;
  20322. end;
  20323. if cur_cmd=assign_toks then
  20324. begin q:=equiv(cur_chr);
  20325. if q=null then define(p,undefined_cs,null)
  20326. else begin add_token_ref(q); define(p,call,q);
  20327. end;
  20328. goto done;
  20329. end;
  20330. end
  20331. @ Similar routines are used to assign values to the numeric parameters.
  20332. @<Assignments@>=
  20333. assign_int: begin p:=cur_chr; scan_optional_equals; scan_int;
  20334. word_define(p,cur_val);
  20335. end;
  20336. assign_dimen: begin p:=cur_chr; scan_optional_equals;
  20337. scan_normal_dimen; word_define(p,cur_val);
  20338. end;
  20339. assign_glue,assign_mu_glue: begin p:=cur_chr; n:=cur_cmd; scan_optional_equals;
  20340. if n=assign_mu_glue then scan_glue(mu_val)@+else scan_glue(glue_val);
  20341. trap_zero_glue;
  20342. define(p,glue_ref,cur_val);
  20343. end;
  20344. @ When a glue register or parameter becomes zero, it will always point to
  20345. |zero_glue| because of the following procedure. (Exception: The tabskip
  20346. glue isn't trapped while preambles are being scanned.)
  20347. @<Declare subprocedures for |prefixed_command|@>=
  20348. procedure trap_zero_glue;
  20349. begin if (width(cur_val)=0)and(stretch(cur_val)=0)and(shrink(cur_val)=0) then
  20350. begin add_glue_ref(zero_glue);
  20351. delete_glue_ref(cur_val); cur_val:=zero_glue;
  20352. end;
  20353. end;
  20354. @ The various character code tables are changed by the |def_code| commands,
  20355. and the font families are declared by |def_family|.
  20356. @<Put each...@>=
  20357. primitive("catcode",def_code,cat_code_base);
  20358. @!@:cat_code_}{\.{\\catcode} primitive@>
  20359. primitive("mathcode",def_code,math_code_base);
  20360. @!@:math_code_}{\.{\\mathcode} primitive@>
  20361. primitive("lccode",def_code,lc_code_base);
  20362. @!@:lc_code_}{\.{\\lccode} primitive@>
  20363. primitive("uccode",def_code,uc_code_base);
  20364. @!@:uc_code_}{\.{\\uccode} primitive@>
  20365. primitive("sfcode",def_code,sf_code_base);
  20366. @!@:sf_code_}{\.{\\sfcode} primitive@>
  20367. primitive("delcode",def_code,del_code_base);
  20368. @!@:del_code_}{\.{\\delcode} primitive@>
  20369. primitive("textfont",def_family,math_font_base);
  20370. @!@:text_font_}{\.{\\textfont} primitive@>
  20371. primitive("scriptfont",def_family,math_font_base+script_size);
  20372. @!@:script_font_}{\.{\\scriptfont} primitive@>
  20373. primitive("scriptscriptfont",def_family,math_font_base+script_script_size);
  20374. @!@:script_script_font_}{\.{\\scriptscriptfont} primitive@>
  20375. @ @<Cases of |print_cmd_chr|...@>=
  20376. def_code: if chr_code=cat_code_base then print_esc("catcode")
  20377. else if chr_code=math_code_base then print_esc("mathcode")
  20378. else if chr_code=lc_code_base then print_esc("lccode")
  20379. else if chr_code=uc_code_base then print_esc("uccode")
  20380. else if chr_code=sf_code_base then print_esc("sfcode")
  20381. else print_esc("delcode");
  20382. def_family: print_size(chr_code-math_font_base);
  20383. @ The different types of code values have different legal ranges; the
  20384. following program is careful to check each case properly.
  20385. @<Assignments@>=
  20386. def_code: begin @<Let |n| be the largest legal code value, based on |cur_chr|@>;
  20387. p:=cur_chr; scan_char_num; p:=p+cur_val; scan_optional_equals;
  20388. scan_int;
  20389. if ((cur_val<0)and(p<del_code_base))or(cur_val>n) then
  20390. begin print_err("Invalid code ("); print_int(cur_val);
  20391. @.Invalid code@>
  20392. if p<del_code_base then print("), should be in the range 0..")
  20393. else print("), should be at most ");
  20394. print_int(n);
  20395. help1("I'm going to use 0 instead of that illegal code value.");@/
  20396. error; cur_val:=0;
  20397. end;
  20398. if p<math_code_base then define(p,data,cur_val)
  20399. else if p<del_code_base then define(p,data,hi(cur_val))
  20400. else word_define(p,cur_val);
  20401. end;
  20402. @ @<Let |n| be the largest...@>=
  20403. if cur_chr=cat_code_base then n:=max_char_code
  20404. else if cur_chr=math_code_base then n:=@'100000
  20405. else if cur_chr=sf_code_base then n:=@'77777
  20406. else if cur_chr=del_code_base then n:=@'77777777
  20407. else n:=255
  20408. @ @<Assignments@>=
  20409. def_family: begin p:=cur_chr; scan_four_bit_int; p:=p+cur_val;
  20410. scan_optional_equals; scan_font_ident; define(p,data,cur_val);
  20411. end;
  20412. @ Next we consider changes to \TeX's numeric registers.
  20413. @<Assignments@>=
  20414. register,advance,multiply,divide: do_register_command(a);
  20415. @ We use the fact that |register<advance<multiply<divide|.
  20416. @<Declare subprocedures for |prefixed_command|@>=
  20417. procedure do_register_command(@!a:small_number);
  20418. label found,exit;
  20419. var l,@!q,@!r,@!s:pointer; {for list manipulation}
  20420. @!p:int_val..mu_val; {type of register involved}
  20421. begin q:=cur_cmd;
  20422. @<Compute the register location |l| and its type |p|; but |return| if invalid@>;
  20423. if q=register then scan_optional_equals
  20424. else if scan_keyword("by") then do_nothing; {optional `\.{by}'}
  20425. @.by@>
  20426. arith_error:=false;
  20427. if q<multiply then @<Compute result of |register| or
  20428. |advance|, put it in |cur_val|@>
  20429. else @<Compute result of |multiply| or |divide|, put it in |cur_val|@>;
  20430. if arith_error then
  20431. begin print_err("Arithmetic overflow");
  20432. @.Arithmetic overflow@>
  20433. help2("I can't carry out that multiplication or division,")@/
  20434. ("since the result is out of range.");
  20435. if p>=glue_val then delete_glue_ref(cur_val);
  20436. error; return;
  20437. end;
  20438. if p<glue_val then word_define(l,cur_val)
  20439. else begin trap_zero_glue; define(l,glue_ref,cur_val);
  20440. end;
  20441. exit: end;
  20442. @ Here we use the fact that the consecutive codes |int_val..mu_val| and
  20443. |assign_int..assign_mu_glue| correspond to each other nicely.
  20444. @<Compute the register location |l| and its type |p|...@>=
  20445. begin if q<>register then
  20446. begin get_x_token;
  20447. if (cur_cmd>=assign_int)and(cur_cmd<=assign_mu_glue) then
  20448. begin l:=cur_chr; p:=cur_cmd-assign_int; goto found;
  20449. end;
  20450. if cur_cmd<>register then
  20451. begin print_err("You can't use `"); print_cmd_chr(cur_cmd,cur_chr);
  20452. @.You can't use x after ...@>
  20453. print("' after "); print_cmd_chr(q,0);
  20454. help1("I'm forgetting what you said and not changing anything.");
  20455. error; return;
  20456. end;
  20457. end;
  20458. p:=cur_chr; scan_eight_bit_int;
  20459. case p of
  20460. int_val: l:=cur_val+count_base;
  20461. dimen_val: l:=cur_val+scaled_base;
  20462. glue_val: l:=cur_val+skip_base;
  20463. mu_val: l:=cur_val+mu_skip_base;
  20464. end; {there are no other cases}
  20465. end;
  20466. found:
  20467. @ @<Compute result of |register| or |advance|...@>=
  20468. if p<glue_val then
  20469. begin if p=int_val then scan_int@+else scan_normal_dimen;
  20470. if q=advance then cur_val:=cur_val+eqtb[l].int;
  20471. end
  20472. else begin scan_glue(p);
  20473. if q=advance then @<Compute the sum of two glue specs@>;
  20474. end
  20475. @ @<Compute the sum of two glue specs@>=
  20476. begin q:=new_spec(cur_val); r:=equiv(l);
  20477. delete_glue_ref(cur_val);
  20478. width(q):=width(q)+width(r);
  20479. if stretch(q)=0 then stretch_order(q):=normal;
  20480. if stretch_order(q)=stretch_order(r) then stretch(q):=stretch(q)+stretch(r)
  20481. else if (stretch_order(q)<stretch_order(r))and(stretch(r)<>0) then
  20482. begin stretch(q):=stretch(r); stretch_order(q):=stretch_order(r);
  20483. end;
  20484. if shrink(q)=0 then shrink_order(q):=normal;
  20485. if shrink_order(q)=shrink_order(r) then shrink(q):=shrink(q)+shrink(r)
  20486. else if (shrink_order(q)<shrink_order(r))and(shrink(r)<>0) then
  20487. begin shrink(q):=shrink(r); shrink_order(q):=shrink_order(r);
  20488. end;
  20489. cur_val:=q;
  20490. end
  20491. @ @<Compute result of |multiply| or |divide|...@>=
  20492. begin scan_int;
  20493. if p<glue_val then
  20494. if q=multiply then
  20495. if p=int_val then cur_val:=mult_integers(eqtb[l].int,cur_val)
  20496. else cur_val:=nx_plus_y(eqtb[l].int,cur_val,0)
  20497. else cur_val:=x_over_n(eqtb[l].int,cur_val)
  20498. else begin s:=equiv(l); r:=new_spec(s);
  20499. if q=multiply then
  20500. begin width(r):=nx_plus_y(width(s),cur_val,0);
  20501. stretch(r):=nx_plus_y(stretch(s),cur_val,0);
  20502. shrink(r):=nx_plus_y(shrink(s),cur_val,0);
  20503. end
  20504. else begin width(r):=x_over_n(width(s),cur_val);
  20505. stretch(r):=x_over_n(stretch(s),cur_val);
  20506. shrink(r):=x_over_n(shrink(s),cur_val);
  20507. end;
  20508. cur_val:=r;
  20509. end;
  20510. end
  20511. @ The processing of boxes is somewhat different, because we may need
  20512. to scan and create an entire box before we actually change the value of the old
  20513. one.
  20514. @<Assignments@>=
  20515. set_box: begin scan_eight_bit_int;
  20516. if global then n:=256+cur_val@+else n:=cur_val;
  20517. scan_optional_equals;
  20518. if set_box_allowed then scan_box(box_flag+n)
  20519. else begin print_err("Improper "); print_esc("setbox");
  20520. @.Improper \\setbox@>
  20521. help2("Sorry, \setbox is not allowed after \halign in a display,")@/
  20522. ("or between \accent and an accented character."); error;
  20523. end;
  20524. end;
  20525. @ The |space_factor| or |prev_depth| settings are changed when a |set_aux|
  20526. command is sensed. Similarly, |prev_graf| is changed in the presence of
  20527. |set_prev_graf|, and |dead_cycles| or |insert_penalties| in the presence of
  20528. |set_page_int|. These definitions are always global.
  20529. When some dimension of a box register is changed, the change isn't exactly
  20530. global; but \TeX\ does not look at the \.{\\global} switch.
  20531. @<Assignments@>=
  20532. set_aux:alter_aux;
  20533. set_prev_graf:alter_prev_graf;
  20534. set_page_dimen:alter_page_so_far;
  20535. set_page_int:alter_integer;
  20536. set_box_dimen:alter_box_dimen;
  20537. @ @<Declare subprocedures for |prefixed_command|@>=
  20538. procedure alter_aux;
  20539. var c:halfword; {|hmode| or |vmode|}
  20540. begin if cur_chr<>abs(mode) then report_illegal_case
  20541. else begin c:=cur_chr; scan_optional_equals;
  20542. if c=vmode then
  20543. begin scan_normal_dimen; prev_depth:=cur_val;
  20544. end
  20545. else begin scan_int;
  20546. if (cur_val<=0)or(cur_val>32767) then
  20547. begin print_err("Bad space factor");
  20548. @.Bad space factor@>
  20549. help1("I allow only values in the range 1..32767 here.");
  20550. int_error(cur_val);
  20551. end
  20552. else space_factor:=cur_val;
  20553. end;
  20554. end;
  20555. end;
  20556. @ @<Declare subprocedures for |prefixed_command|@>=
  20557. procedure alter_prev_graf;
  20558. var p:0..nest_size; {index into |nest|}
  20559. begin nest[nest_ptr]:=cur_list; p:=nest_ptr;
  20560. while abs(nest[p].mode_field)<>vmode do decr(p);
  20561. scan_optional_equals; scan_int;
  20562. if cur_val<0 then
  20563. begin print_err("Bad "); print_esc("prevgraf");
  20564. @.Bad \\prevgraf@>
  20565. help1("I allow only nonnegative values here.");
  20566. int_error(cur_val);
  20567. end
  20568. else begin nest[p].pg_field:=cur_val; cur_list:=nest[nest_ptr];
  20569. end;
  20570. end;
  20571. @ @<Declare subprocedures for |prefixed_command|@>=
  20572. procedure alter_page_so_far;
  20573. var c:0..7; {index into |page_so_far|}
  20574. begin c:=cur_chr; scan_optional_equals; scan_normal_dimen;
  20575. page_so_far[c]:=cur_val;
  20576. end;
  20577. @ @<Declare subprocedures for |prefixed_command|@>=
  20578. procedure alter_integer;
  20579. var c:0..1; {0 for \.{\\deadcycles}, 1 for \.{\\insertpenalties}}
  20580. begin c:=cur_chr; scan_optional_equals; scan_int;
  20581. if c=0 then dead_cycles:=cur_val
  20582. else insert_penalties:=cur_val;
  20583. end;
  20584. @ @<Declare subprocedures for |prefixed_command|@>=
  20585. procedure alter_box_dimen;
  20586. var c:small_number; {|width_offset| or |height_offset| or |depth_offset|}
  20587. @!b:eight_bits; {box number}
  20588. begin c:=cur_chr; scan_eight_bit_int; b:=cur_val; scan_optional_equals;
  20589. scan_normal_dimen;
  20590. if box(b)<>null then mem[box(b)+c].sc:=cur_val;
  20591. end;
  20592. @ Paragraph shapes are set up in the obvious way.
  20593. @<Assignments@>=
  20594. set_shape: begin scan_optional_equals; scan_int; n:=cur_val;
  20595. if n<=0 then p:=null
  20596. else begin p:=get_node(2*n+1); info(p):=n;
  20597. for j:=1 to n do
  20598. begin scan_normal_dimen;
  20599. mem[p+2*j-1].sc:=cur_val; {indentation}
  20600. scan_normal_dimen;
  20601. mem[p+2*j].sc:=cur_val; {width}
  20602. end;
  20603. end;
  20604. define(par_shape_loc,shape_ref,p);
  20605. end;
  20606. @ Here's something that isn't quite so obvious. It guarantees that
  20607. |info(par_shape_ptr)| can hold any positive~|n| for which |get_node(2*n+1)|
  20608. doesn't overflow the memory capacity.
  20609. @<Check the ``constant''...@>=
  20610. if 2*max_halfword<mem_top-mem_min then bad:=41;
  20611. @ New hyphenation data is loaded by the |hyph_data| command.
  20612. @<Put each...@>=
  20613. primitive("hyphenation",hyph_data,0);
  20614. @!@:hyphenation_}{\.{\\hyphenation} primitive@>
  20615. primitive("patterns",hyph_data,1);
  20616. @!@:patterns_}{\.{\\patterns} primitive@>
  20617. @ @<Cases of |print_cmd_chr|...@>=
  20618. hyph_data: if chr_code=1 then print_esc("patterns")
  20619. else print_esc("hyphenation");
  20620. @ @<Assignments@>=
  20621. hyph_data: if cur_chr=1 then
  20622. begin @!init new_patterns; goto done;@;@+tini@/
  20623. print_err("Patterns can be loaded only by INITEX");
  20624. @.Patterns can be...@>
  20625. help0; error;
  20626. repeat get_token; until cur_cmd=right_brace; {flush the patterns}
  20627. return;
  20628. end
  20629. else begin new_hyph_exceptions; goto done;
  20630. end;
  20631. @ All of \TeX's parameters are kept in |eqtb| except the font information,
  20632. the interaction mode, and the hyphenation tables; these are strictly global.
  20633. @<Assignments@>=
  20634. assign_font_dimen: begin find_font_dimen(true); k:=cur_val;
  20635. scan_optional_equals; scan_normal_dimen; font_info[k].sc:=cur_val;
  20636. end;
  20637. assign_font_int: begin n:=cur_chr; scan_font_ident; f:=cur_val;
  20638. scan_optional_equals; scan_int;
  20639. if n=0 then hyphen_char[f]:=cur_val@+else skew_char[f]:=cur_val;
  20640. end;
  20641. @ @<Put each...@>=
  20642. primitive("hyphenchar",assign_font_int,0);
  20643. @!@:hyphen_char_}{\.{\\hyphenchar} primitive@>
  20644. primitive("skewchar",assign_font_int,1);
  20645. @!@:skew_char_}{\.{\\skewchar} primitive@>
  20646. @ @<Cases of |print_cmd_chr|...@>=
  20647. assign_font_int: if chr_code=0 then print_esc("hyphenchar")
  20648. else print_esc("skewchar");
  20649. @ Here is where the information for a new font gets loaded.
  20650. @<Assignments@>=
  20651. def_font: new_font(a);
  20652. @ @<Declare subprocedures for |prefixed_command|@>=
  20653. procedure new_font(@!a:small_number);
  20654. label common_ending;
  20655. var u:pointer; {user's font identifier}
  20656. @!s:scaled; {stated ``at'' size, or negative of scaled magnification}
  20657. @!f:internal_font_number; {runs through existing fonts}
  20658. @!t:str_number; {name for the frozen font identifier}
  20659. @!old_setting:0..max_selector; {holds |selector| setting}
  20660. @!flushable_string:str_number; {string not yet referenced}
  20661. begin if job_name=0 then open_log_file;
  20662. {avoid confusing \.{texput} with the font name}
  20663. @.texput@>
  20664. get_r_token; u:=cur_cs;
  20665. if u>=hash_base then t:=text(u)
  20666. else if u>=single_base then
  20667. if u=null_cs then t:="FONT"@+else t:=u-single_base
  20668. else begin old_setting:=selector; selector:=new_string;
  20669. print("FONT"); print(u-active_base); selector:=old_setting;
  20670. @.FONTx@>
  20671. str_room(1); t:=make_string;
  20672. end;
  20673. define(u,set_font,null_font); scan_optional_equals; scan_file_name;
  20674. @<Scan the font size specification@>;
  20675. @<If this font has already been loaded, set |f| to the internal
  20676. font number and |goto common_ending|@>;
  20677. f:=read_font_info(u,cur_name,cur_area,s);
  20678. common_ending: equiv(u):=f; eqtb[font_id_base+f]:=eqtb[u]; font_id_text(f):=t;
  20679. end;
  20680. @ @<Scan the font size specification@>=
  20681. name_in_progress:=true; {this keeps |cur_name| from being changed}
  20682. if scan_keyword("at") then @<Put the \(p)(positive) `at' size into |s|@>
  20683. @.at@>
  20684. else if scan_keyword("scaled") then
  20685. @.scaled@>
  20686. begin scan_int; s:=-cur_val;
  20687. if (cur_val<=0)or(cur_val>32768) then
  20688. begin print_err("Illegal magnification has been changed to 1000");@/
  20689. @.Illegal magnification...@>
  20690. help1("The magnification ratio must be between 1 and 32768.");
  20691. int_error(cur_val); s:=-1000;
  20692. end;
  20693. end
  20694. else s:=-1000;
  20695. name_in_progress:=false
  20696. @ @<Put the \(p)(positive) `at' size into |s|@>=
  20697. begin scan_normal_dimen; s:=cur_val;
  20698. if (s<=0)or(s>=@'1000000000) then
  20699. begin print_err("Improper `at' size (");
  20700. print_scaled(s); print("pt), replaced by 10pt");
  20701. @.Improper `at' size...@>
  20702. help2("I can only handle fonts at positive sizes that are")@/
  20703. ("less than 2048pt, so I've changed what you said to 10pt.");
  20704. error; s:=10*unity;
  20705. end;
  20706. end
  20707. @ When the user gives a new identifier to a font that was previously loaded,
  20708. the new name becomes the font identifier of record. Font names `\.{xyz}' and
  20709. `\.{XYZ}' are considered to be different.
  20710. @<If this font has already been loaded...@>=
  20711. flushable_string:=str_ptr-1;
  20712. for f:=font_base+1 to font_ptr do
  20713. if str_eq_str(font_name[f],cur_name)and str_eq_str(font_area[f],cur_area) then
  20714. begin if cur_name=flushable_string then
  20715. begin flush_string; cur_name:=font_name[f];
  20716. end;
  20717. if s>0 then
  20718. begin if s=font_size[f] then goto common_ending;
  20719. end
  20720. else if font_size[f]=xn_over_d(font_dsize[f],-s,1000) then
  20721. goto common_ending;
  20722. end
  20723. @ @<Cases of |print_cmd_chr|...@>=
  20724. set_font:begin print("select font "); slow_print(font_name[chr_code]);
  20725. if font_size[chr_code]<>font_dsize[chr_code] then
  20726. begin print(" at "); print_scaled(font_size[chr_code]);
  20727. print("pt");
  20728. end;
  20729. end;
  20730. @ @<Put each...@>=
  20731. primitive("batchmode",set_interaction,batch_mode);
  20732. @!@:batch_mode_}{\.{\\batchmode} primitive@>
  20733. primitive("nonstopmode",set_interaction,nonstop_mode);
  20734. @!@:nonstop_mode_}{\.{\\nonstopmode} primitive@>
  20735. primitive("scrollmode",set_interaction,scroll_mode);
  20736. @!@:scroll_mode_}{\.{\\scrollmode} primitive@>
  20737. primitive("errorstopmode",set_interaction,error_stop_mode);
  20738. @!@:error_stop_mode_}{\.{\\errorstopmode} primitive@>
  20739. @ @<Cases of |print_cmd_chr|...@>=
  20740. set_interaction: case chr_code of
  20741. batch_mode: print_esc("batchmode");
  20742. nonstop_mode: print_esc("nonstopmode");
  20743. scroll_mode: print_esc("scrollmode");
  20744. othercases print_esc("errorstopmode")
  20745. endcases;
  20746. @ @<Assignments@>=
  20747. set_interaction: new_interaction;
  20748. @ @<Declare subprocedures for |prefixed_command|@>=
  20749. procedure new_interaction;
  20750. begin print_ln;
  20751. interaction:=cur_chr;
  20752. @<Initialize the print |selector| based on |interaction|@>;
  20753. if log_opened then selector:=selector+2;
  20754. end;
  20755. @ The \.{\\afterassignment} command puts a token into the global
  20756. variable |after_token|. This global variable is examined just after
  20757. every assignment has been performed.
  20758. @<Glob...@>=
  20759. @!after_token:halfword; {zero, or a saved token}
  20760. @ @<Set init...@>=
  20761. after_token:=0;
  20762. @ @<Cases of |main_control| that don't...@>=
  20763. any_mode(after_assignment):begin get_token; after_token:=cur_tok;
  20764. end;
  20765. @ @<Insert a token saved by \.{\\afterassignment}, if any@>=
  20766. if after_token<>0 then
  20767. begin cur_tok:=after_token; back_input; after_token:=0;
  20768. end
  20769. @ Here is a procedure that might be called `Get the next non-blank non-relax
  20770. non-call non-assignment token'.
  20771. @<Declare act...@>=
  20772. procedure do_assignments;
  20773. label exit;
  20774. begin loop begin @<Get the next non-blank non-relax...@>;
  20775. if cur_cmd<=max_non_prefixed_command then return;
  20776. set_box_allowed:=false; prefixed_command; set_box_allowed:=true;
  20777. end;
  20778. exit:end;
  20779. @ @<Cases of |main_control| that don't...@>=
  20780. any_mode(after_group):begin get_token; save_for_after(cur_tok);
  20781. end;
  20782. @ Files for \.{\\read} are opened and closed by the |in_stream| command.
  20783. @<Put each...@>=
  20784. primitive("openin",in_stream,1);
  20785. @!@:open_in_}{\.{\\openin} primitive@>
  20786. primitive("closein",in_stream,0);
  20787. @!@:close_in_}{\.{\\closein} primitive@>
  20788. @ @<Cases of |print_cmd_chr|...@>=
  20789. in_stream: if chr_code=0 then print_esc("closein")
  20790. else print_esc("openin");
  20791. @ @<Cases of |main_control| that don't...@>=
  20792. any_mode(in_stream): open_or_close_in;
  20793. @ @<Declare act...@>=
  20794. procedure open_or_close_in;
  20795. var c:0..1; {1 for \.{\\openin}, 0 for \.{\\closein}}
  20796. @!n:0..15; {stream number}
  20797. begin c:=cur_chr; scan_four_bit_int; n:=cur_val;
  20798. if read_open[n]<>closed then
  20799. begin a_close(read_file[n]); read_open[n]:=closed;
  20800. end;
  20801. if c<>0 then
  20802. begin scan_optional_equals; scan_file_name;
  20803. if cur_ext="" then cur_ext:=".tex";
  20804. pack_cur_name;
  20805. if a_open_in(read_file[n]) then read_open[n]:=just_open;
  20806. end;
  20807. end;
  20808. @ The user can issue messages to the terminal, regardless of the
  20809. current mode.
  20810. @<Cases of |main_control| that don't...@>=
  20811. any_mode(message):issue_message;
  20812. @ @<Put each...@>=
  20813. primitive("message",message,0);
  20814. @!@:message_}{\.{\\message} primitive@>
  20815. primitive("errmessage",message,1);
  20816. @!@:err_message_}{\.{\\errmessage} primitive@>
  20817. @ @<Cases of |print_cmd_chr|...@>=
  20818. message: if chr_code=0 then print_esc("message")
  20819. else print_esc("errmessage");
  20820. @ @<Declare act...@>=
  20821. procedure issue_message;
  20822. var old_setting:0..max_selector; {holds |selector| setting}
  20823. @!c:0..1; {identifies \.{\\message} and \.{\\errmessage}}
  20824. @!s:str_number; {the message}
  20825. begin c:=cur_chr; link(garbage):=scan_toks(false,true);
  20826. old_setting:=selector; selector:=new_string;
  20827. token_show(def_ref); selector:=old_setting;
  20828. flush_list(def_ref);
  20829. str_room(1); s:=make_string;
  20830. if c=0 then @<Print string |s| on the terminal@>
  20831. else @<Print string |s| as an error message@>;
  20832. flush_string;
  20833. end;
  20834. @ @<Print string |s| on the terminal@>=
  20835. begin if term_offset+length(s)>max_print_line-2 then print_ln
  20836. else if (term_offset>0)or(file_offset>0) then print_char(" ");
  20837. slow_print(s); update_terminal;
  20838. end
  20839. @ If \.{\\errmessage} occurs often in |scroll_mode|, without user-defined
  20840. \.{\\errhelp}, we don't want to give a long help message each time. So we
  20841. give a verbose explanation only once.
  20842. @<Glob...@>=
  20843. @!long_help_seen:boolean; {has the long \.{\\errmessage} help been used?}
  20844. @ @<Set init...@>=long_help_seen:=false;
  20845. @ @<Print string |s| as an error message@>=
  20846. begin print_err(""); slow_print(s);
  20847. if err_help<>null then use_err_help:=true
  20848. else if long_help_seen then help1("(That was another \errmessage.)")
  20849. else begin if interaction<error_stop_mode then long_help_seen:=true;
  20850. help4("This error message was generated by an \errmessage")@/
  20851. ("command, so I can't give any explicit help.")@/
  20852. ("Pretend that you're Hercule Poirot: Examine all clues,")@/
  20853. @^Poirot, Hercule@>
  20854. ("and deduce the truth by order and method.");
  20855. end;
  20856. error; use_err_help:=false;
  20857. end
  20858. @ The |error| routine calls on |give_err_help| if help is requested from
  20859. the |err_help| parameter.
  20860. @p procedure give_err_help;
  20861. begin token_show(err_help);
  20862. end;
  20863. @ The \.{\\uppercase} and \.{\\lowercase} commands are implemented by
  20864. building a token list and then changing the cases of the letters in it.
  20865. @<Cases of |main_control| that don't...@>=
  20866. any_mode(case_shift):shift_case;
  20867. @ @<Put each...@>=
  20868. primitive("lowercase",case_shift,lc_code_base);
  20869. @!@:lowercase_}{\.{\\lowercase} primitive@>
  20870. primitive("uppercase",case_shift,uc_code_base);
  20871. @!@:uppercase_}{\.{\\uppercase} primitive@>
  20872. @ @<Cases of |print_cmd_chr|...@>=
  20873. case_shift:if chr_code=lc_code_base then print_esc("lowercase")
  20874. else print_esc("uppercase");
  20875. @ @<Declare act...@>=
  20876. procedure shift_case;
  20877. var b:pointer; {|lc_code_base| or |uc_code_base|}
  20878. @!p:pointer; {runs through the token list}
  20879. @!t:halfword; {token}
  20880. @!c:eight_bits; {character code}
  20881. begin b:=cur_chr; p:=scan_toks(false,false); p:=link(def_ref);
  20882. while p<>null do
  20883. begin @<Change the case of the token in |p|, if a change is appropriate@>;
  20884. p:=link(p);
  20885. end;
  20886. back_list(link(def_ref)); free_avail(def_ref); {omit reference count}
  20887. end;
  20888. @ When the case of a |chr_code| changes, we don't change the |cmd|.
  20889. We also change active characters, using the fact that
  20890. |cs_token_flag+active_base| is a multiple of~256.
  20891. @^data structure assumptions@>
  20892. @<Change the case of the token in |p|, if a change is appropriate@>=
  20893. t:=info(p);
  20894. if t<cs_token_flag+single_base then
  20895. begin c:=t mod 256;
  20896. if equiv(b+c)<>0 then info(p):=t-c+equiv(b+c);
  20897. end
  20898. @ We come finally to the last pieces missing from |main_control|, namely the
  20899. `\.{\\show}' commands that are useful when debugging.
  20900. @<Cases of |main_control| that don't...@>=
  20901. any_mode(xray): show_whatever;
  20902. @ @d show_code=0 { \.{\\show} }
  20903. @d show_box_code=1 { \.{\\showbox} }
  20904. @d show_the_code=2 { \.{\\showthe} }
  20905. @d show_lists_code=3 { \.{\\showlists} }
  20906. @<Put each...@>=
  20907. primitive("show",xray,show_code);
  20908. @!@:show_}{\.{\\show} primitive@>
  20909. primitive("showbox",xray,show_box_code);
  20910. @!@:show_box_}{\.{\\showbox} primitive@>
  20911. primitive("showthe",xray,show_the_code);
  20912. @!@:show_the_}{\.{\\showthe} primitive@>
  20913. primitive("showlists",xray,show_lists_code);
  20914. @!@:show_lists_code_}{\.{\\showlists} primitive@>
  20915. @ @<Cases of |print_cmd_chr|...@>=
  20916. xray: case chr_code of
  20917. show_box_code:print_esc("showbox");
  20918. show_the_code:print_esc("showthe");
  20919. show_lists_code:print_esc("showlists");
  20920. othercases print_esc("show")
  20921. endcases;
  20922. @ @<Declare act...@>=
  20923. procedure show_whatever;
  20924. label common_ending;
  20925. var p:pointer; {tail of a token list to show}
  20926. begin case cur_chr of
  20927. show_lists_code: begin begin_diagnostic; show_activities;
  20928. end;
  20929. show_box_code: @<Show the current contents of a box@>;
  20930. show_code: @<Show the current meaning of a token, then |goto common_ending|@>;
  20931. othercases @<Show the current value of some parameter or register,
  20932. then |goto common_ending|@>
  20933. endcases;@/
  20934. @<Complete a potentially long \.{\\show} command@>;
  20935. common_ending: if interaction<error_stop_mode then
  20936. begin help0; decr(error_count);
  20937. end
  20938. else if tracing_online>0 then
  20939. begin@t@>@;@/
  20940. help3("This isn't an error message; I'm just \showing something.")@/
  20941. ("Type `I\show...' to show more (e.g., \show\cs,")@/
  20942. ("\showthe\count10, \showbox255, \showlists).");
  20943. end
  20944. else begin@t@>@;@/
  20945. help5("This isn't an error message; I'm just \showing something.")@/
  20946. ("Type `I\show...' to show more (e.g., \show\cs,")@/
  20947. ("\showthe\count10, \showbox255, \showlists).")@/
  20948. ("And type `I\tracingonline=1\show...' to show boxes and")@/
  20949. ("lists on your terminal as well as in the transcript file.");
  20950. end;
  20951. error;
  20952. end;
  20953. @ @<Show the current meaning of a token...@>=
  20954. begin get_token;
  20955. if interaction=error_stop_mode then wake_up_terminal;
  20956. print_nl("> ");
  20957. if cur_cs<>0 then
  20958. begin sprint_cs(cur_cs); print_char("=");
  20959. end;
  20960. print_meaning; goto common_ending;
  20961. end
  20962. @ @<Cases of |print_cmd_chr|...@>=
  20963. undefined_cs: print("undefined");
  20964. call: print("macro");
  20965. long_call: print_esc("long macro");
  20966. outer_call: print_esc("outer macro");
  20967. long_outer_call: begin print_esc("long"); print_esc("outer macro");
  20968. end;
  20969. end_template: print_esc("outer endtemplate");
  20970. @ @<Show the current contents of a box@>=
  20971. begin scan_eight_bit_int; begin_diagnostic;
  20972. print_nl("> \box"); print_int(cur_val); print_char("=");
  20973. if box(cur_val)=null then print("void")
  20974. else show_box(box(cur_val));
  20975. end
  20976. @ @<Show the current value of some parameter...@>=
  20977. begin p:=the_toks;
  20978. if interaction=error_stop_mode then wake_up_terminal;
  20979. print_nl("> "); token_show(temp_head);
  20980. flush_list(link(temp_head)); goto common_ending;
  20981. end
  20982. @ @<Complete a potentially long \.{\\show} command@>=
  20983. end_diagnostic(true); print_err("OK");
  20984. @.OK@>
  20985. if selector=term_and_log then if tracing_online<=0 then
  20986. begin selector:=term_only; print(" (see the transcript file)");
  20987. selector:=term_and_log;
  20988. end
  20989. @* \[50] Dumping and undumping the tables.
  20990. After \.{INITEX} has seen a collection of fonts and macros, it
  20991. can write all the necessary information on an auxiliary file so
  20992. that production versions of \TeX\ are able to initialize their
  20993. memory at high speed. The present section of the program takes
  20994. care of such output and input. We shall consider simultaneously
  20995. the processes of storing and restoring,
  20996. so that the inverse relation between them is clear.
  20997. @.INITEX@>
  20998. The global variable |format_ident| is a string that is printed right
  20999. after the |banner| line when \TeX\ is ready to start. For \.{INITEX} this
  21000. string says simply `\.{ (INITEX)}'; for other versions of \TeX\ it says,
  21001. for example, `\.{ (preloaded format=plain 1982.11.19)}', showing the year,
  21002. month, and day that the format file was created. We have |format_ident=0|
  21003. before \TeX's tables are loaded.
  21004. @<Glob...@>=
  21005. @!format_ident:str_number;
  21006. @ @<Set init...@>=
  21007. format_ident:=0;
  21008. @ @<Initialize table entries...@>=
  21009. format_ident:=" (INITEX)";
  21010. @ @<Declare act...@>=
  21011. @!init procedure store_fmt_file;
  21012. label found1,found2,done1,done2;
  21013. var j,@!k,@!l:integer; {all-purpose indices}
  21014. @!p,@!q: pointer; {all-purpose pointers}
  21015. @!x: integer; {something to dump}
  21016. @!w: four_quarters; {four ASCII codes}
  21017. begin @<If dumping is not allowed, abort@>;
  21018. @<Create the |format_ident|, open the format file,
  21019. and inform the user that dumping has begun@>;
  21020. @<Dump constants for consistency check@>;
  21021. @<Dump the string pool@>;
  21022. @<Dump the dynamic memory@>;
  21023. @<Dump the table of equivalents@>;
  21024. @<Dump the font information@>;
  21025. @<Dump the hyphenation tables@>;
  21026. @<Dump a couple more things and the closing check word@>;
  21027. @<Close the format file@>;
  21028. end;
  21029. tini
  21030. @ Corresponding to the procedure that dumps a format file, we have a function
  21031. that reads one in. The function returns |false| if the dumped format is
  21032. incompatible with the present \TeX\ table sizes, etc.
  21033. @d bad_fmt=6666 {go here if the format file is unacceptable}
  21034. @d too_small(#)==begin wake_up_terminal;
  21035. wterm_ln('---! Must increase the ',#);
  21036. @.Must increase the x@>
  21037. goto bad_fmt;
  21038. end
  21039. @p @t\4@>@<Declare the function called |open_fmt_file|@>@;
  21040. function load_fmt_file:boolean;
  21041. label bad_fmt,exit;
  21042. var j,@!k:integer; {all-purpose indices}
  21043. @!p,@!q: pointer; {all-purpose pointers}
  21044. @!x: integer; {something undumped}
  21045. @!w: four_quarters; {four ASCII codes}
  21046. begin @<Undump constants for consistency check@>;
  21047. @<Undump the string pool@>;
  21048. @<Undump the dynamic memory@>;
  21049. @<Undump the table of equivalents@>;
  21050. @<Undump the font information@>;
  21051. @<Undump the hyphenation tables@>;
  21052. @<Undump a couple more things and the closing check word@>;
  21053. load_fmt_file:=true; return; {it worked!}
  21054. bad_fmt: wake_up_terminal;
  21055. wterm_ln('(Fatal format file error; I''m stymied)');
  21056. @.Fatal format file error@>
  21057. load_fmt_file:=false;
  21058. exit:end;
  21059. @ The user is not allowed to dump a format file unless |save_ptr=0|.
  21060. This condition implies that |cur_level=level_one|, hence
  21061. the |xeq_level| array is constant and it need not be dumped.
  21062. @<If dumping is not allowed, abort@>=
  21063. if save_ptr<>0 then
  21064. begin print_err("You can't dump inside a group");
  21065. @.You can't dump...@>
  21066. help1("`{...\dump}' is a no-no."); succumb;
  21067. end
  21068. @ Format files consist of |memory_word| items, and we use the following
  21069. macros to dump words of different types:
  21070. @d dump_wd(#)==begin fmt_file^:=#; put(fmt_file);@+end
  21071. @d dump_int(#)==begin fmt_file^.int:=#; put(fmt_file);@+end
  21072. @d dump_hh(#)==begin fmt_file^.hh:=#; put(fmt_file);@+end
  21073. @d dump_qqqq(#)==begin fmt_file^.qqqq:=#; put(fmt_file);@+end
  21074. @<Glob...@>=
  21075. @!fmt_file:word_file; {for input or output of format information}
  21076. @ The inverse macros are slightly more complicated, since we need to check
  21077. the range of the values we are reading in. We say `|undump(a)(b)(x)|' to
  21078. read an integer value |x| that is supposed to be in the range |a<=x<=b|.
  21079. System error messages should be suppressed when undumping.
  21080. @^system dependencies@>
  21081. @d undump_wd(#)==begin get(fmt_file); #:=fmt_file^;@+end
  21082. @d undump_int(#)==begin get(fmt_file); #:=fmt_file^.int;@+end
  21083. @d undump_hh(#)==begin get(fmt_file); #:=fmt_file^.hh;@+end
  21084. @d undump_qqqq(#)==begin get(fmt_file); #:=fmt_file^.qqqq;@+end
  21085. @d undump_end_end(#)==#:=x;@+end
  21086. @d undump_end(#)==(x>#) then goto bad_fmt@+else undump_end_end
  21087. @d undump(#)==begin undump_int(x); if (x<#) or undump_end
  21088. @d undump_size_end_end(#)==too_small(#)@+else undump_end_end
  21089. @d undump_size_end(#)==if x># then undump_size_end_end
  21090. @d undump_size(#)==begin undump_int(x);
  21091. if x<# then goto bad_fmt; undump_size_end
  21092. @ The next few sections of the program should make it clear how we use the
  21093. dump/undump macros.
  21094. @<Dump constants for consistency check@>=
  21095. dump_int(@$);@/
  21096. dump_int(mem_bot);@/
  21097. dump_int(mem_top);@/
  21098. dump_int(eqtb_size);@/
  21099. dump_int(hash_prime);@/
  21100. dump_int(hyph_size)
  21101. @ Sections of a \.{WEB} program that are ``commented out'' still contribute
  21102. strings to the string pool; therefore \.{INITEX} and \TeX\ will have
  21103. the same strings. (And it is, of course, a good thing that they do.)
  21104. @.WEB@>
  21105. @^string pool@>
  21106. @<Undump constants for consistency check@>=
  21107. x:=fmt_file^.int;
  21108. if x<>@$ then goto bad_fmt; {check that strings are the same}
  21109. undump_int(x);
  21110. if x<>mem_bot then goto bad_fmt;
  21111. undump_int(x);
  21112. if x<>mem_top then goto bad_fmt;
  21113. undump_int(x);
  21114. if x<>eqtb_size then goto bad_fmt;
  21115. undump_int(x);
  21116. if x<>hash_prime then goto bad_fmt;
  21117. undump_int(x);
  21118. if x<>hyph_size then goto bad_fmt
  21119. @ @d dump_four_ASCII==
  21120. w.b0:=qi(so(str_pool[k])); w.b1:=qi(so(str_pool[k+1]));
  21121. w.b2:=qi(so(str_pool[k+2])); w.b3:=qi(so(str_pool[k+3]));
  21122. dump_qqqq(w)
  21123. @<Dump the string pool@>=
  21124. dump_int(pool_ptr);
  21125. dump_int(str_ptr);
  21126. for k:=0 to str_ptr do dump_int(str_start[k]);
  21127. k:=0;
  21128. while k+4<pool_ptr do
  21129. begin dump_four_ASCII; k:=k+4;
  21130. end;
  21131. k:=pool_ptr-4; dump_four_ASCII;
  21132. print_ln; print_int(str_ptr); print(" strings of total length ");
  21133. print_int(pool_ptr)
  21134. @ @d undump_four_ASCII==
  21135. undump_qqqq(w);
  21136. str_pool[k]:=si(qo(w.b0)); str_pool[k+1]:=si(qo(w.b1));
  21137. str_pool[k+2]:=si(qo(w.b2)); str_pool[k+3]:=si(qo(w.b3))
  21138. @<Undump the string pool@>=
  21139. undump_size(0)(pool_size)('string pool size')(pool_ptr);
  21140. undump_size(0)(max_strings)('max strings')(str_ptr);
  21141. for k:=0 to str_ptr do undump(0)(pool_ptr)(str_start[k]);
  21142. k:=0;
  21143. while k+4<pool_ptr do
  21144. begin undump_four_ASCII; k:=k+4;
  21145. end;
  21146. k:=pool_ptr-4; undump_four_ASCII;
  21147. init_str_ptr:=str_ptr; init_pool_ptr:=pool_ptr
  21148. @ By sorting the list of available spaces in the variable-size portion of
  21149. |mem|, we are usually able to get by without having to dump very much
  21150. of the dynamic memory.
  21151. We recompute |var_used| and |dyn_used|, so that \.{INITEX} dumps valid
  21152. information even when it has not been gathering statistics.
  21153. @<Dump the dynamic memory@>=
  21154. sort_avail; var_used:=0;
  21155. dump_int(lo_mem_max); dump_int(rover);
  21156. p:=mem_bot; q:=rover; x:=0;
  21157. repeat for k:=p to q+1 do dump_wd(mem[k]);
  21158. x:=x+q+2-p; var_used:=var_used+q-p;
  21159. p:=q+node_size(q); q:=rlink(q);
  21160. until q=rover;
  21161. var_used:=var_used+lo_mem_max-p; dyn_used:=mem_end+1-hi_mem_min;@/
  21162. for k:=p to lo_mem_max do dump_wd(mem[k]);
  21163. x:=x+lo_mem_max+1-p;
  21164. dump_int(hi_mem_min); dump_int(avail);
  21165. for k:=hi_mem_min to mem_end do dump_wd(mem[k]);
  21166. x:=x+mem_end+1-hi_mem_min;
  21167. p:=avail;
  21168. while p<>null do
  21169. begin decr(dyn_used); p:=link(p);
  21170. end;
  21171. dump_int(var_used); dump_int(dyn_used);
  21172. print_ln; print_int(x);
  21173. print(" memory locations dumped; current usage is ");
  21174. print_int(var_used); print_char("&"); print_int(dyn_used)
  21175. @ @<Undump the dynamic memory@>=
  21176. undump(lo_mem_stat_max+1000)(hi_mem_stat_min-1)(lo_mem_max);
  21177. undump(lo_mem_stat_max+1)(lo_mem_max)(rover);
  21178. p:=mem_bot; q:=rover;
  21179. repeat for k:=p to q+1 do undump_wd(mem[k]);
  21180. p:=q+node_size(q);
  21181. if (p>lo_mem_max)or((q>=rlink(q))and(rlink(q)<>rover)) then goto bad_fmt;
  21182. q:=rlink(q);
  21183. until q=rover;
  21184. for k:=p to lo_mem_max do undump_wd(mem[k]);
  21185. if mem_min<mem_bot-2 then {make more low memory available}
  21186. begin p:=llink(rover); q:=mem_min+1;
  21187. link(mem_min):=null; info(mem_min):=null; {we don't use the bottom word}
  21188. rlink(p):=q; llink(rover):=q;@/
  21189. rlink(q):=rover; llink(q):=p; link(q):=empty_flag;
  21190. node_size(q):=mem_bot-q;
  21191. end;
  21192. undump(lo_mem_max+1)(hi_mem_stat_min)(hi_mem_min);
  21193. undump(null)(mem_top)(avail); mem_end:=mem_top;
  21194. for k:=hi_mem_min to mem_end do undump_wd(mem[k]);
  21195. undump_int(var_used); undump_int(dyn_used)
  21196. @ @<Dump the table of equivalents@>=
  21197. @<Dump regions 1 to 4 of |eqtb|@>;
  21198. @<Dump regions 5 and 6 of |eqtb|@>;
  21199. dump_int(par_loc); dump_int(write_loc);@/
  21200. @<Dump the hash table@>
  21201. @ @<Undump the table of equivalents@>=
  21202. @<Undump regions 1 to 6 of |eqtb|@>;
  21203. undump(hash_base)(frozen_control_sequence)(par_loc);
  21204. par_token:=cs_token_flag+par_loc;@/
  21205. undump(hash_base)(frozen_control_sequence)(write_loc);@/
  21206. @<Undump the hash table@>
  21207. @ The table of equivalents usually contains repeated information, so we dump it
  21208. in compressed form: The sequence of $n+2$ values $(n,x_1,\ldots,x_n,m)$ in the
  21209. format file represents $n+m$ consecutive entries of |eqtb|, with |m| extra
  21210. copies of $x_n$, namely $(x_1,\ldots,x_n,x_n,\ldots,x_n)$.
  21211. @<Dump regions 1 to 4 of |eqtb|@>=
  21212. k:=active_base;
  21213. repeat j:=k;
  21214. while j<int_base-1 do
  21215. begin if (equiv(j)=equiv(j+1))and(eq_type(j)=eq_type(j+1))and@|
  21216. (eq_level(j)=eq_level(j+1)) then goto found1;
  21217. incr(j);
  21218. end;
  21219. l:=int_base; goto done1; {|j=int_base-1|}
  21220. found1: incr(j); l:=j;
  21221. while j<int_base-1 do
  21222. begin if (equiv(j)<>equiv(j+1))or(eq_type(j)<>eq_type(j+1))or@|
  21223. (eq_level(j)<>eq_level(j+1)) then goto done1;
  21224. incr(j);
  21225. end;
  21226. done1:dump_int(l-k);
  21227. while k<l do
  21228. begin dump_wd(eqtb[k]); incr(k);
  21229. end;
  21230. k:=j+1; dump_int(k-l);
  21231. until k=int_base
  21232. @ @<Dump regions 5 and 6 of |eqtb|@>=
  21233. repeat j:=k;
  21234. while j<eqtb_size do
  21235. begin if eqtb[j].int=eqtb[j+1].int then goto found2;
  21236. incr(j);
  21237. end;
  21238. l:=eqtb_size+1; goto done2; {|j=eqtb_size|}
  21239. found2: incr(j); l:=j;
  21240. while j<eqtb_size do
  21241. begin if eqtb[j].int<>eqtb[j+1].int then goto done2;
  21242. incr(j);
  21243. end;
  21244. done2:dump_int(l-k);
  21245. while k<l do
  21246. begin dump_wd(eqtb[k]); incr(k);
  21247. end;
  21248. k:=j+1; dump_int(k-l);
  21249. until k>eqtb_size
  21250. @ @<Undump regions 1 to 6 of |eqtb|@>=
  21251. k:=active_base;
  21252. repeat undump_int(x);
  21253. if (x<1)or(k+x>eqtb_size+1) then goto bad_fmt;
  21254. for j:=k to k+x-1 do undump_wd(eqtb[j]);
  21255. k:=k+x;
  21256. undump_int(x);
  21257. if (x<0)or(k+x>eqtb_size+1) then goto bad_fmt;
  21258. for j:=k to k+x-1 do eqtb[j]:=eqtb[k-1];
  21259. k:=k+x;
  21260. until k>eqtb_size
  21261. @ A different scheme is used to compress the hash table, since its lower
  21262. region is usually sparse. When |text(p)<>0| for |p<=hash_used|, we output
  21263. two words, |p| and |hash[p]|. The hash table is, of course, densely packed
  21264. for |p>=hash_used|, so the remaining entries are output in a~block.
  21265. @<Dump the hash table@>=
  21266. dump_int(hash_used); cs_count:=frozen_control_sequence-1-hash_used;
  21267. for p:=hash_base to hash_used do if text(p)<>0 then
  21268. begin dump_int(p); dump_hh(hash[p]); incr(cs_count);
  21269. end;
  21270. for p:=hash_used+1 to undefined_control_sequence-1 do dump_hh(hash[p]);
  21271. dump_int(cs_count);@/
  21272. print_ln; print_int(cs_count); print(" multiletter control sequences")
  21273. @ @<Undump the hash table@>=
  21274. undump(hash_base)(frozen_control_sequence)(hash_used); p:=hash_base-1;
  21275. repeat undump(p+1)(hash_used)(p); undump_hh(hash[p]);
  21276. until p=hash_used;
  21277. for p:=hash_used+1 to undefined_control_sequence-1 do undump_hh(hash[p]);
  21278. undump_int(cs_count)
  21279. @ @<Dump the font information@>=
  21280. dump_int(fmem_ptr);
  21281. for k:=0 to fmem_ptr-1 do dump_wd(font_info[k]);
  21282. dump_int(font_ptr);
  21283. for k:=null_font to font_ptr do
  21284. @<Dump the array info for internal font number |k|@>;
  21285. print_ln; print_int(fmem_ptr-7); print(" words of font info for ");
  21286. print_int(font_ptr-font_base); print(" preloaded font");
  21287. if font_ptr<>font_base+1 then print_char("s")
  21288. @ @<Undump the font information@>=
  21289. undump_size(7)(font_mem_size)('font mem size')(fmem_ptr);
  21290. for k:=0 to fmem_ptr-1 do undump_wd(font_info[k]);
  21291. undump_size(font_base)(font_max)('font max')(font_ptr);
  21292. for k:=null_font to font_ptr do
  21293. @<Undump the array info for internal font number |k|@>
  21294. @ @<Dump the array info for internal font number |k|@>=
  21295. begin dump_qqqq(font_check[k]);
  21296. dump_int(font_size[k]);
  21297. dump_int(font_dsize[k]);
  21298. dump_int(font_params[k]);@/
  21299. dump_int(hyphen_char[k]);
  21300. dump_int(skew_char[k]);@/
  21301. dump_int(font_name[k]);
  21302. dump_int(font_area[k]);@/
  21303. dump_int(font_bc[k]);
  21304. dump_int(font_ec[k]);@/
  21305. dump_int(char_base[k]);
  21306. dump_int(width_base[k]);
  21307. dump_int(height_base[k]);@/
  21308. dump_int(depth_base[k]);
  21309. dump_int(italic_base[k]);
  21310. dump_int(lig_kern_base[k]);@/
  21311. dump_int(kern_base[k]);
  21312. dump_int(exten_base[k]);
  21313. dump_int(param_base[k]);@/
  21314. dump_int(font_glue[k]);@/
  21315. dump_int(bchar_label[k]);
  21316. dump_int(font_bchar[k]);
  21317. dump_int(font_false_bchar[k]);@/
  21318. print_nl("\font"); print_esc(font_id_text(k)); print_char("=");
  21319. print_file_name(font_name[k],font_area[k],"");
  21320. if font_size[k]<>font_dsize[k] then
  21321. begin print(" at "); print_scaled(font_size[k]); print("pt");
  21322. end;
  21323. end
  21324. @ @<Undump the array info for internal font number |k|@>=
  21325. begin undump_qqqq(font_check[k]);@/
  21326. undump_int(font_size[k]);
  21327. undump_int(font_dsize[k]);
  21328. undump(min_halfword)(max_halfword)(font_params[k]);@/
  21329. undump_int(hyphen_char[k]);
  21330. undump_int(skew_char[k]);@/
  21331. undump(0)(str_ptr)(font_name[k]);
  21332. undump(0)(str_ptr)(font_area[k]);@/
  21333. undump(0)(255)(font_bc[k]);
  21334. undump(0)(255)(font_ec[k]);@/
  21335. undump_int(char_base[k]);
  21336. undump_int(width_base[k]);
  21337. undump_int(height_base[k]);@/
  21338. undump_int(depth_base[k]);
  21339. undump_int(italic_base[k]);
  21340. undump_int(lig_kern_base[k]);@/
  21341. undump_int(kern_base[k]);
  21342. undump_int(exten_base[k]);
  21343. undump_int(param_base[k]);@/
  21344. undump(min_halfword)(lo_mem_max)(font_glue[k]);@/
  21345. undump(0)(fmem_ptr-1)(bchar_label[k]);
  21346. undump(min_quarterword)(non_char)(font_bchar[k]);
  21347. undump(min_quarterword)(non_char)(font_false_bchar[k]);
  21348. end
  21349. @ @<Dump the hyphenation tables@>=
  21350. dump_int(hyph_count);
  21351. for k:=0 to hyph_size do if hyph_word[k]<>0 then
  21352. begin dump_int(k); dump_int(hyph_word[k]); dump_int(hyph_list[k]);
  21353. end;
  21354. print_ln; print_int(hyph_count); print(" hyphenation exception");
  21355. if hyph_count<>1 then print_char("s");
  21356. if trie_not_ready then init_trie;
  21357. dump_int(trie_max);
  21358. for k:=0 to trie_max do dump_hh(trie[k]);
  21359. dump_int(trie_op_ptr);
  21360. for k:=1 to trie_op_ptr do
  21361. begin dump_int(hyf_distance[k]);
  21362. dump_int(hyf_num[k]);
  21363. dump_int(hyf_next[k]);
  21364. end;
  21365. print_nl("Hyphenation trie of length "); print_int(trie_max);
  21366. @.Hyphenation trie...@>
  21367. print(" has "); print_int(trie_op_ptr); print(" op");
  21368. if trie_op_ptr<>1 then print_char("s");
  21369. print(" out of "); print_int(trie_op_size);
  21370. for k:=255 downto 0 do if trie_used[k]>min_quarterword then
  21371. begin print_nl(" "); print_int(qo(trie_used[k]));
  21372. print(" for language "); print_int(k);
  21373. dump_int(k); dump_int(qo(trie_used[k]));
  21374. end
  21375. @ Only ``nonempty'' parts of |op_start| need to be restored.
  21376. @<Undump the hyphenation tables@>=
  21377. undump(0)(hyph_size)(hyph_count);
  21378. for k:=1 to hyph_count do
  21379. begin undump(0)(hyph_size)(j);
  21380. undump(0)(str_ptr)(hyph_word[j]);
  21381. undump(min_halfword)(max_halfword)(hyph_list[j]);
  21382. end;
  21383. undump_size(0)(trie_size)('trie size')(j); @+init trie_max:=j;@+tini
  21384. for k:=0 to j do undump_hh(trie[k]);
  21385. undump_size(0)(trie_op_size)('trie op size')(j); @+init trie_op_ptr:=j;@+tini
  21386. for k:=1 to j do
  21387. begin undump(0)(63)(hyf_distance[k]); {a |small_number|}
  21388. undump(0)(63)(hyf_num[k]);
  21389. undump(min_quarterword)(max_quarterword)(hyf_next[k]);
  21390. end;
  21391. init for k:=0 to 255 do trie_used[k]:=min_quarterword;@+tini@;@/
  21392. k:=256;
  21393. while j>0 do
  21394. begin undump(0)(k-1)(k); undump(1)(j)(x);@+init trie_used[k]:=qi(x);@+tini@;@/
  21395. j:=j-x; op_start[k]:=qo(j);
  21396. end;
  21397. @!init trie_not_ready:=false @+tini
  21398. @ We have already printed a lot of statistics, so we set |tracing_stats:=0|
  21399. to prevent them from appearing again.
  21400. @<Dump a couple more things and the closing check word@>=
  21401. dump_int(interaction); dump_int(format_ident); dump_int(69069);
  21402. tracing_stats:=0
  21403. @ @<Undump a couple more things and the closing check word@>=
  21404. undump(batch_mode)(error_stop_mode)(interaction);
  21405. undump(0)(str_ptr)(format_ident);
  21406. undump_int(x);
  21407. if (x<>69069)or eof(fmt_file) then goto bad_fmt
  21408. @ @<Create the |format_ident|...@>=
  21409. selector:=new_string;
  21410. print(" (preloaded format="); print(job_name); print_char(" ");
  21411. print_int(year); print_char(".");
  21412. print_int(month); print_char("."); print_int(day); print_char(")");
  21413. if interaction=batch_mode then selector:=log_only
  21414. else selector:=term_and_log;
  21415. str_room(1);
  21416. format_ident:=make_string;
  21417. pack_job_name(format_extension);
  21418. while not w_open_out(fmt_file) do
  21419. prompt_file_name("format file name",format_extension);
  21420. print_nl("Beginning to dump on file ");
  21421. @.Beginning to dump...@>
  21422. slow_print(w_make_name_string(fmt_file)); flush_string;
  21423. print_nl(""); slow_print(format_ident)
  21424. @ @<Close the format file@>=
  21425. w_close(fmt_file)
  21426. @* \[51] The main program.
  21427. This is it: the part of \TeX\ that executes all those procedures we have
  21428. written.
  21429. Well---almost. Let's leave space for a few more routines that we may
  21430. have forgotten.
  21431. @p @<Last-minute procedures@>
  21432. @ We have noted that there are two versions of \TeX82. One, called \.{INITEX},
  21433. @.INITEX@>
  21434. has to be run first; it initializes everything from scratch, without
  21435. reading a format file, and it has the capability of dumping a format file.
  21436. The other one is called `\.{VIRTEX}'; it is a ``virgin'' program that needs
  21437. @.VIRTEX@>
  21438. to input a format file in order to get started. \.{VIRTEX} typically has
  21439. more memory capacity than \.{INITEX}, because it does not need the space
  21440. consumed by the auxiliary hyphenation tables and the numerous calls on
  21441. |primitive|, etc.
  21442. The \.{VIRTEX} program cannot read a format file instantaneously, of course;
  21443. the best implementations therefore allow for production versions of \TeX\ that
  21444. not only avoid the loading routine for \PASCAL\ object code, they also have
  21445. a format file pre-loaded. This is impossible to do if we stick to standard
  21446. \PASCAL; but there is a simple way to fool many systems into avoiding the
  21447. initialization, as follows:\quad(1)~We declare a global integer variable
  21448. called |ready_already|. The probability is negligible that this
  21449. variable holds any particular value like 314159 when \.{VIRTEX} is first
  21450. loaded.\quad(2)~After we have read in a format file and initialized
  21451. everything, we set |ready_already:=314159|.\quad(3)~Soon \.{VIRTEX}
  21452. will print `\.*', waiting for more input; and at this point we
  21453. interrupt the program and save its core image in some form that the
  21454. operating system can reload speedily.\quad(4)~When that core image is
  21455. activated, the program starts again at the beginning; but now
  21456. |ready_already=314159| and all the other global variables have
  21457. their initial values too. The former chastity has vanished!
  21458. In other words, if we allow ourselves to test the condition
  21459. |ready_already=314159|, before |ready_already| has been
  21460. assigned a value, we can avoid the lengthy initialization. Dirty tricks
  21461. rarely pay off so handsomely.
  21462. @^dirty \PASCAL@>
  21463. @^system dependencies@>
  21464. On systems that allow such preloading, the standard program called \.{TeX}
  21465. should be the one that has \.{plain} format preloaded, since that agrees
  21466. with {\sl The \TeX book}. Other versions, e.g., \.{AmSTeX}, should also
  21467. @:TeXbook}{\sl The \TeX book@>
  21468. @.AmSTeX@>
  21469. @.plain@>
  21470. be provided for commonly used formats.
  21471. @<Glob...@>=
  21472. @!ready_already:integer; {a sacrifice of purity for economy}
  21473. @ Now this is really it: \TeX\ starts and ends here.
  21474. The initial test involving |ready_already| should be deleted if the
  21475. \PASCAL\ runtime system is smart enough to detect such a ``mistake.''
  21476. @^system dependencies@>
  21477. @p begin @!{|start_here|}
  21478. history:=fatal_error_stop; {in case we quit during initialization}
  21479. t_open_out; {open the terminal for output}
  21480. if ready_already=314159 then goto start_of_TEX;
  21481. @<Check the ``constant'' values...@>@;
  21482. if bad>0 then
  21483. begin wterm_ln('Ouch---my internal constants have been clobbered!',
  21484. '---case ',bad:1);
  21485. @.Ouch...clobbered@>
  21486. goto final_end;
  21487. end;
  21488. initialize; {set global variables to their starting values}
  21489. @!init if not get_strings_started then goto final_end;
  21490. init_prim; {call |primitive| for each primitive}
  21491. init_str_ptr:=str_ptr; init_pool_ptr:=pool_ptr; fix_date_and_time;
  21492. tini@/
  21493. ready_already:=314159;
  21494. start_of_TEX: @<Initialize the output routines@>;
  21495. @<Get the first line of input and prepare to start@>;
  21496. history:=spotless; {ready to go!}
  21497. main_control; {come to life}
  21498. final_cleanup; {prepare for death}
  21499. end_of_TEX: close_files_and_terminate;
  21500. final_end: ready_already:=0;
  21501. end.
  21502. @ Here we do whatever is needed to complete \TeX's job gracefully on the
  21503. local operating system. The code here might come into play after a fatal
  21504. error; it must therefore consist entirely of ``safe'' operations that
  21505. cannot produce error messages. For example, it would be a mistake to call
  21506. |str_room| or |make_string| at this time, because a call on |overflow|
  21507. might lead to an infinite loop.
  21508. @^system dependencies@>
  21509. (Actually there's one way to get error messages, via |prepare_mag|;
  21510. but that can't cause infinite recursion.)
  21511. @^recursion@>
  21512. If |final_cleanup| is bypassed, this program doesn't bother to close
  21513. the input files that may still be open.
  21514. @<Last-minute...@>=
  21515. procedure close_files_and_terminate;
  21516. var k:integer; {all-purpose index}
  21517. begin @<Finish the extensions@>; new_line_char:=-1;
  21518. @!stat if tracing_stats>0 then @<Output statistics about this job@>;@;@+tats@/
  21519. wake_up_terminal; @<Finish the \.{DVI} file@>;
  21520. if log_opened then
  21521. begin wlog_cr; a_close(log_file); selector:=selector-2;
  21522. if selector=term_only then
  21523. begin print_nl("Transcript written on ");
  21524. @.Transcript written...@>
  21525. slow_print(log_name); print_char(".");
  21526. end;
  21527. end;
  21528. end;
  21529. @ The present section goes directly to the log file instead of using
  21530. |print| commands, because there's no need for these strings to take
  21531. up |str_pool| memory when a non-{\bf stat} version of \TeX\ is being used.
  21532. @<Output statistics...@>=
  21533. if log_opened then
  21534. begin wlog_ln(' ');
  21535. wlog_ln('Here is how much of TeX''s memory',' you used:');
  21536. @.Here is how much...@>
  21537. wlog(' ',str_ptr-init_str_ptr:1,' string');
  21538. if str_ptr<>init_str_ptr+1 then wlog('s');
  21539. wlog_ln(' out of ', max_strings-init_str_ptr:1);@/
  21540. wlog_ln(' ',pool_ptr-init_pool_ptr:1,' string characters out of ',
  21541. pool_size-init_pool_ptr:1);@/
  21542. wlog_ln(' ',lo_mem_max-mem_min+mem_end-hi_mem_min+2:1,@|
  21543. ' words of memory out of ',mem_end+1-mem_min:1);@/
  21544. wlog_ln(' ',cs_count:1,' multiletter control sequences out of ',
  21545. hash_size:1);@/
  21546. wlog(' ',fmem_ptr:1,' words of font info for ',
  21547. font_ptr-font_base:1,' font');
  21548. if font_ptr<>font_base+1 then wlog('s');
  21549. wlog_ln(', out of ',font_mem_size:1,' for ',font_max-font_base:1);@/
  21550. wlog(' ',hyph_count:1,' hyphenation exception');
  21551. if hyph_count<>1 then wlog('s');
  21552. wlog_ln(' out of ',hyph_size:1);@/
  21553. wlog_ln(' ',max_in_stack:1,'i,',max_nest_stack:1,'n,',@|
  21554. max_param_stack:1,'p,',@|
  21555. max_buf_stack+1:1,'b,',@|
  21556. max_save_stack+6:1,'s stack positions out of ',@|
  21557. stack_size:1,'i,',
  21558. nest_size:1,'n,',
  21559. param_size:1,'p,',
  21560. buf_size:1,'b,',
  21561. save_size:1,'s');
  21562. end
  21563. @ We get to the |final_cleanup| routine when \.{\\end} or \.{\\dump} has
  21564. been scanned and |its_all_over|\kern-2pt.
  21565. @<Last-minute...@>=
  21566. procedure final_cleanup;
  21567. label exit;
  21568. var c:small_number; {0 for \.{\\end}, 1 for \.{\\dump}}
  21569. begin c:=cur_chr; if c<>1 then new_line_char:=-1;
  21570. if job_name=0 then open_log_file;
  21571. while input_ptr>0 do
  21572. if state=token_list then end_token_list@+else end_file_reading;
  21573. while open_parens>0 do
  21574. begin print(" )"); decr(open_parens);
  21575. end;
  21576. if cur_level>level_one then
  21577. begin print_nl("("); print_esc("end occurred ");
  21578. print("inside a group at level ");
  21579. @:end_}{\.{(\\end occurred...)}@>
  21580. print_int(cur_level-level_one); print_char(")");
  21581. end;
  21582. while cond_ptr<>null do
  21583. begin print_nl("("); print_esc("end occurred ");
  21584. print("when "); print_cmd_chr(if_test,cur_if);
  21585. if if_line<>0 then
  21586. begin print(" on line "); print_int(if_line);
  21587. end;
  21588. print(" was incomplete)");
  21589. if_line:=if_line_field(cond_ptr);
  21590. cur_if:=subtype(cond_ptr); temp_ptr:=cond_ptr;
  21591. cond_ptr:=link(cond_ptr); free_node(temp_ptr,if_node_size);
  21592. end;
  21593. if history<>spotless then
  21594. if ((history=warning_issued)or(interaction<error_stop_mode)) then
  21595. if selector=term_and_log then
  21596. begin selector:=term_only;
  21597. print_nl("(see the transcript file for additional information)");
  21598. @.see the transcript file...@>
  21599. selector:=term_and_log;
  21600. end;
  21601. if c=1 then
  21602. begin @!init for c:=top_mark_code to split_bot_mark_code do
  21603. if cur_mark[c]<>null then delete_token_ref(cur_mark[c]);
  21604. if last_glue<>max_halfword then delete_glue_ref(last_glue);
  21605. store_fmt_file; return;@+tini@/
  21606. print_nl("(\dump is performed only by INITEX)"); return;
  21607. @:dump_}{\.{\\dump...only by INITEX}@>
  21608. end;
  21609. exit:end;
  21610. @ @<Last-minute...@>=
  21611. @!init procedure init_prim; {initialize all the primitives}
  21612. begin no_new_control_sequence:=false;
  21613. @<Put each...@>;
  21614. no_new_control_sequence:=true;
  21615. end;
  21616. tini
  21617. @ When we begin the following code, \TeX's tables may still contain garbage;
  21618. the strings might not even be present. Thus we must proceed cautiously to get
  21619. bootstrapped in.
  21620. But when we finish this part of the program, \TeX\ is ready to call on the
  21621. |main_control| routine to do its work.
  21622. @<Get the first line...@>=
  21623. begin @<Initialize the input routines@>;
  21624. if (format_ident=0)or(buffer[loc]="&") then
  21625. begin if format_ident<>0 then initialize; {erase preloaded format}
  21626. if not open_fmt_file then goto final_end;
  21627. if not load_fmt_file then
  21628. begin w_close(fmt_file); goto final_end;
  21629. end;
  21630. w_close(fmt_file);
  21631. while (loc<limit)and(buffer[loc]=" ") do incr(loc);
  21632. end;
  21633. if end_line_char_inactive then decr(limit)
  21634. else buffer[limit]:=end_line_char;
  21635. fix_date_and_time;@/
  21636. @<Compute the magic offset@>;
  21637. @<Initialize the print |selector|...@>;
  21638. if (loc<limit)and(cat_code(buffer[loc])<>escape) then start_input;
  21639. {\.{\\input} assumed}
  21640. end
  21641. @* \[52] Debugging.
  21642. Once \TeX\ is working, you should be able to diagnose most errors with
  21643. the \.{\\show} commands and other diagnostic features. But for the initial
  21644. stages of debugging, and for the revelation of really deep mysteries, you
  21645. can compile \TeX\ with a few more aids, including the \PASCAL\ runtime
  21646. checks and its debugger. An additional routine called |debug_help|
  21647. will also come into play when you type `\.D' after an error message;
  21648. |debug_help| also occurs just before a fatal error causes \TeX\ to succumb.
  21649. @^debugging@>
  21650. @^system dependencies@>
  21651. The interface to |debug_help| is primitive, but it is good enough when used
  21652. with a \PASCAL\ debugger that allows you to set breakpoints and to read
  21653. variables and change their values. After getting the prompt `\.{debug \#}', you
  21654. type either a negative number (this exits |debug_help|), or zero (this
  21655. goes to a location where you can set a breakpoint, thereby entering into
  21656. dialog with the \PASCAL\ debugger), or a positive number |m| followed by
  21657. an argument |n|. The meaning of |m| and |n| will be clear from the
  21658. program below. (If |m=13|, there is an additional argument, |l|.)
  21659. @.debug \#@>
  21660. @d breakpoint=888 {place where a breakpoint is desirable}
  21661. @<Last-minute...@>=
  21662. @!debug procedure debug_help; {routine to display various things}
  21663. label breakpoint,exit;
  21664. var k,@!l,@!m,@!n:integer;
  21665. begin clear_terminal;
  21666. loop begin wake_up_terminal;
  21667. print_nl("debug # (-1 to exit):"); update_terminal;
  21668. @.debug \#@>
  21669. read(term_in,m);
  21670. if m<0 then return
  21671. else if m=0 then
  21672. begin goto breakpoint;@/ {go to every declared label at least once}
  21673. breakpoint: m:=0; @{'BREAKPOINT'@}@/
  21674. end
  21675. else begin read(term_in,n);
  21676. case m of
  21677. @t\4@>@<Numbered cases for |debug_help|@>@;
  21678. othercases print("?")
  21679. endcases;
  21680. end;
  21681. end;
  21682. exit:end;
  21683. gubed
  21684. @ @<Numbered cases...@>=
  21685. 1: print_word(mem[n]); {display |mem[n]| in all forms}
  21686. 2: print_int(info(n));
  21687. 3: print_int(link(n));
  21688. 4: print_word(eqtb[n]);
  21689. 5: print_word(font_info[n]);
  21690. 6: print_word(save_stack[n]);
  21691. 7: show_box(n);
  21692. {show a box, abbreviated by |show_box_depth| and |show_box_breadth|}
  21693. 8: begin breadth_max:=10000; depth_threshold:=pool_size-pool_ptr-10;
  21694. show_node_list(n); {show a box in its entirety}
  21695. end;
  21696. 9: show_token_list(n,null,1000);
  21697. 10: slow_print(n);
  21698. 11: check_mem(n>0); {check wellformedness; print new busy locations if |n>0|}
  21699. 12: search_mem(n); {look for pointers to |n|}
  21700. 13: begin read(term_in,l); print_cmd_chr(n,l);
  21701. end;
  21702. 14: for k:=0 to n do print(buffer[k]);
  21703. 15: begin font_in_short_display:=null_font; short_display(n);
  21704. end;
  21705. 16: panicking:=not panicking;
  21706. @* \[53] Extensions.
  21707. The program above includes a bunch of ``hooks'' that allow further
  21708. capabilities to be added without upsetting \TeX's basic structure.
  21709. Most of these hooks are concerned with ``whatsit'' nodes, which are
  21710. intended to be used for special purposes; whenever a new extension to
  21711. \TeX\ involves a new kind of whatsit node, a corresponding change needs
  21712. to be made to the routines below that deal with such nodes,
  21713. but it will usually be unnecessary to make many changes to the
  21714. other parts of this program.
  21715. In order to demonstrate how extensions can be made, we shall treat
  21716. `\.{\\write}', `\.{\\openout}', `\.{\\closeout}', `\.{\\immediate}',
  21717. `\.{\\special}', and `\.{\\setlanguage}' as if they were extensions.
  21718. These commands are actually primitives of \TeX, and they should
  21719. appear in all implementations of the system; but let's try to imagine
  21720. that they aren't. Then the program below illustrates how a person
  21721. could add them.
  21722. Sometimes, of course, an extension will require changes to \TeX\ itself;
  21723. no system of hooks could be complete enough for all conceivable extensions.
  21724. The features associated with `\.{\\write}' are almost all confined to the
  21725. following paragraphs, but there are small parts of the |print_ln| and
  21726. |print_char| procedures that were introduced specifically to \.{\\write}
  21727. characters. Furthermore one of the token lists recognized by the scanner
  21728. is a |write_text|; and there are a few other miscellaneous places where we
  21729. have already provided for some aspect of \.{\\write}. The goal of a \TeX\
  21730. extender should be to minimize alterations to the standard parts of the
  21731. program, and to avoid them completely if possible. He or she should also
  21732. be quite sure that there's no easy way to accomplish the desired goals
  21733. with the standard features that \TeX\ already has. ``Think thrice before
  21734. extending,'' because that may save a lot of work, and it will also keep
  21735. incompatible extensions of \TeX\ from proliferating.
  21736. @^system dependencies@>
  21737. @^extensions to \TeX@>
  21738. @ First let's consider the format of whatsit nodes that are used to represent
  21739. the data associated with \.{\\write} and its relatives. Recall that a whatsit
  21740. has |type=whatsit_node|, and the |subtype| is supposed to distinguish
  21741. different kinds of whatsits. Each node occupies two or more words; the
  21742. exact number is immaterial, as long as it is readily determined from the
  21743. |subtype| or other data.
  21744. We shall introduce five |subtype| values here, corresponding to the
  21745. control sequences \.{\\openout}, \.{\\write}, \.{\\closeout}, \.{\\special}, and
  21746. \.{\\setlanguage}. The second word of I/O whatsits has a |write_stream| field
  21747. that identifies the write-stream number (0 to 15, or 16 for out-of-range and
  21748. positive, or 17 for out-of-range and negative).
  21749. In the case of \.{\\write} and \.{\\special}, there is also a field that
  21750. points to the reference count of a token list that should be sent. In the
  21751. case of \.{\\openout}, we need three words and three auxiliary subfields
  21752. to hold the string numbers for name, area, and extension.
  21753. @d write_node_size=2 {number of words in a write/whatsit node}
  21754. @d open_node_size=3 {number of words in an open/whatsit node}
  21755. @d open_node=0 {|subtype| in whatsits that represent files to \.{\\openout}}
  21756. @d write_node=1 {|subtype| in whatsits that represent things to \.{\\write}}
  21757. @d close_node=2 {|subtype| in whatsits that represent streams to \.{\\closeout}}
  21758. @d special_node=3 {|subtype| in whatsits that represent \.{\\special} things}
  21759. @d language_node=4 {|subtype| in whatsits that change the current language}
  21760. @d what_lang(#)==link(#+1) {language number, in the range |0..255|}
  21761. @d what_lhm(#)==type(#+1) {minimum left fragment, in the range |1..63|}
  21762. @d what_rhm(#)==subtype(#+1) {minimum right fragment, in the range |1..63|}
  21763. @d write_tokens(#) == link(#+1) {reference count of token list to write}
  21764. @d write_stream(#) == info(#+1) {stream number (0 to 17)}
  21765. @d open_name(#) == link(#+1) {string number of file name to open}
  21766. @d open_area(#) == info(#+2) {string number of file area for |open_name|}
  21767. @d open_ext(#) == link(#+2) {string number of file extension for |open_name|}
  21768. @ The sixteen possible \.{\\write} streams are represented by the |write_file|
  21769. array. The |j|th file is open if and only if |write_open[j]=true|. The last
  21770. two streams are special; |write_open[16]| represents a stream number
  21771. greater than 15, while |write_open[17]| represents a negative stream number,
  21772. and both of these variables are always |false|.
  21773. @<Glob...@>=
  21774. @!write_file:array[0..15] of alpha_file;
  21775. @!write_open:array[0..17] of boolean;
  21776. @ @<Set init...@>=
  21777. for k:=0 to 17 do write_open[k]:=false;
  21778. @ Extensions might introduce new command codes; but it's best to use
  21779. |extension| with a modifier, whenever possible, so that |main_control|
  21780. stays the same.
  21781. @d immediate_code=4 {command modifier for \.{\\immediate}}
  21782. @d set_language_code=5 {command modifier for \.{\\setlanguage}}
  21783. @<Put each...@>=
  21784. primitive("openout",extension,open_node);@/
  21785. @!@:open_out_}{\.{\\openout} primitive@>
  21786. primitive("write",extension,write_node); write_loc:=cur_val;@/
  21787. @!@:write_}{\.{\\write} primitive@>
  21788. primitive("closeout",extension,close_node);@/
  21789. @!@:close_out_}{\.{\\closeout} primitive@>
  21790. primitive("special",extension,special_node);@/
  21791. @!@:special_}{\.{\\special} primitive@>
  21792. primitive("immediate",extension,immediate_code);@/
  21793. @!@:immediate_}{\.{\\immediate} primitive@>
  21794. primitive("setlanguage",extension,set_language_code);@/
  21795. @!@:set_language_}{\.{\\setlanguage} primitive@>
  21796. @ The variable |write_loc| just introduced is used to provide an
  21797. appropriate error message in case of ``runaway'' write texts.
  21798. @<Glob...@>=
  21799. @!write_loc:pointer; {|eqtb| address of \.{\\write}}
  21800. @ @<Cases of |print_cmd_chr|...@>=
  21801. extension: case chr_code of
  21802. open_node:print_esc("openout");
  21803. write_node:print_esc("write");
  21804. close_node:print_esc("closeout");
  21805. special_node:print_esc("special");
  21806. immediate_code:print_esc("immediate");
  21807. set_language_code:print_esc("setlanguage");
  21808. othercases print("[unknown extension!]")
  21809. endcases;
  21810. @ When an |extension| command occurs in |main_control|, in any mode,
  21811. the |do_extension| routine is called.
  21812. @<Cases of |main_control| that are for extensions...@>=
  21813. any_mode(extension):do_extension;
  21814. @ @<Declare act...@>=
  21815. @t\4@>@<Declare procedures needed in |do_extension|@>@;
  21816. procedure do_extension;
  21817. var i,@!j,@!k:integer; {all-purpose integers}
  21818. @!p,@!q,@!r:pointer; {all-purpose pointers}
  21819. begin case cur_chr of
  21820. open_node:@<Implement \.{\\openout}@>;
  21821. write_node:@<Implement \.{\\write}@>;
  21822. close_node:@<Implement \.{\\closeout}@>;
  21823. special_node:@<Implement \.{\\special}@>;
  21824. immediate_code:@<Implement \.{\\immediate}@>;
  21825. set_language_code:@<Implement \.{\\setlanguage}@>;
  21826. othercases confusion("ext1")
  21827. @:this can't happen ext1}{\quad ext1@>
  21828. endcases;
  21829. end;
  21830. @ Here is a subroutine that creates a whatsit node having a given |subtype|
  21831. and a given number of words. It initializes only the first word of the whatsit,
  21832. and appends it to the current list.
  21833. @<Declare procedures needed in |do_extension|@>=
  21834. procedure new_whatsit(@!s:small_number;@!w:small_number);
  21835. var p:pointer; {the new node}
  21836. begin p:=get_node(w); type(p):=whatsit_node; subtype(p):=s;
  21837. link(tail):=p; tail:=p;
  21838. end;
  21839. @ The next subroutine uses |cur_chr| to decide what sort of whatsit is
  21840. involved, and also inserts a |write_stream| number.
  21841. @<Declare procedures needed in |do_ext...@>=
  21842. procedure new_write_whatsit(@!w:small_number);
  21843. begin new_whatsit(cur_chr,w);
  21844. if w<>write_node_size then scan_four_bit_int
  21845. else begin scan_int;
  21846. if cur_val<0 then cur_val:=17
  21847. else if cur_val>15 then cur_val:=16;
  21848. end;
  21849. write_stream(tail):=cur_val;
  21850. end;
  21851. @ @<Implement \.{\\openout}@>=
  21852. begin new_write_whatsit(open_node_size);
  21853. scan_optional_equals; scan_file_name;@/
  21854. open_name(tail):=cur_name; open_area(tail):=cur_area; open_ext(tail):=cur_ext;
  21855. end
  21856. @ When `\.{\\write 12\{...\}}' appears, we scan the token list `\.{\{...\}}'
  21857. without expanding its macros; the macros will be expanded later when this
  21858. token list is rescanned.
  21859. @<Implement \.{\\write}@>=
  21860. begin k:=cur_cs; new_write_whatsit(write_node_size);@/
  21861. cur_cs:=k; p:=scan_toks(false,false); write_tokens(tail):=def_ref;
  21862. end
  21863. @ @<Implement \.{\\closeout}@>=
  21864. begin new_write_whatsit(write_node_size); write_tokens(tail):=null;
  21865. end
  21866. @ When `\.{\\special\{...\}}' appears, we expand the macros in the token
  21867. list as in \.{\\xdef} and \.{\\mark}.
  21868. @<Implement \.{\\special}@>=
  21869. begin new_whatsit(special_node,write_node_size); write_stream(tail):=null;
  21870. p:=scan_toks(false,true); write_tokens(tail):=def_ref;
  21871. end
  21872. @ Each new type of node that appears in our data structure must be capable
  21873. of being displayed, copied, destroyed, and so on. The routines that we
  21874. need for write-oriented whatsits are somewhat like those for mark nodes;
  21875. other extensions might, of course, involve more subtlety here.
  21876. @<Basic printing...@>=
  21877. procedure print_write_whatsit(@!s:str_number;@!p:pointer);
  21878. begin print_esc(s);
  21879. if write_stream(p)<16 then print_int(write_stream(p))
  21880. else if write_stream(p)=16 then print_char("*")
  21881. @.*\relax@>
  21882. else print_char("-");
  21883. end;
  21884. @ @<Display the whatsit...@>=
  21885. case subtype(p) of
  21886. open_node:begin print_write_whatsit("openout",p);
  21887. print_char("="); print_file_name(open_name(p),open_area(p),open_ext(p));
  21888. end;
  21889. write_node:begin print_write_whatsit("write",p);
  21890. print_mark(write_tokens(p));
  21891. end;
  21892. close_node:print_write_whatsit("closeout",p);
  21893. special_node:begin print_esc("special");
  21894. print_mark(write_tokens(p));
  21895. end;
  21896. language_node:begin print_esc("setlanguage");
  21897. print_int(what_lang(p)); print(" (hyphenmin ");
  21898. print_int(what_lhm(p)); print_char(",");
  21899. print_int(what_rhm(p)); print_char(")");
  21900. end;
  21901. othercases print("whatsit?")
  21902. endcases
  21903. @ @<Make a partial copy of the whatsit...@>=
  21904. case subtype(p) of
  21905. open_node: begin r:=get_node(open_node_size); words:=open_node_size;
  21906. end;
  21907. write_node,special_node: begin r:=get_node(write_node_size);
  21908. add_token_ref(write_tokens(p)); words:=write_node_size;
  21909. end;
  21910. close_node,language_node: begin r:=get_node(small_node_size);
  21911. words:=small_node_size;
  21912. end;
  21913. othercases confusion("ext2")
  21914. @:this can't happen ext2}{\quad ext2@>
  21915. endcases
  21916. @ @<Wipe out the whatsit...@>=
  21917. begin case subtype(p) of
  21918. open_node: free_node(p,open_node_size);
  21919. write_node,special_node: begin delete_token_ref(write_tokens(p));
  21920. free_node(p,write_node_size); goto done;
  21921. end;
  21922. close_node,language_node: free_node(p,small_node_size);
  21923. othercases confusion("ext3")
  21924. @:this can't happen ext3}{\quad ext3@>
  21925. endcases;@/
  21926. goto done;
  21927. end
  21928. @ @<Incorporate a whatsit node into a vbox@>=do_nothing
  21929. @ @<Incorporate a whatsit node into an hbox@>=do_nothing
  21930. @ @<Let |d| be the width of the whatsit |p|@>=d:=0
  21931. @ @d adv_past(#)==@+if subtype(#)=language_node then
  21932. begin cur_lang:=what_lang(#); l_hyf:=what_lhm(#); r_hyf:=what_rhm(#);@+end
  21933. @<Advance \(p)past a whatsit node in the \(l)|line_break| loop@>=@+
  21934. adv_past(cur_p)
  21935. @ @<Advance \(p)past a whatsit node in the \(p)pre-hyphenation loop@>=@+
  21936. adv_past(s)
  21937. @ @<Prepare to move whatsit |p| to the current page, then |goto contribute|@>=
  21938. goto contribute
  21939. @ @<Process whatsit |p| in |vert_break| loop, |goto not_found|@>=
  21940. goto not_found
  21941. @ @<Output the whatsit node |p| in a vlist@>=
  21942. out_what(p)
  21943. @ @<Output the whatsit node |p| in an hlist@>=
  21944. out_what(p)
  21945. @ After all this preliminary shuffling, we come finally to the routines
  21946. that actually send out the requested data. Let's do \.{\\special} first
  21947. (it's easier).
  21948. @<Declare procedures needed in |hlist_out|, |vlist_out|@>=
  21949. procedure special_out(@!p:pointer);
  21950. var old_setting:0..max_selector; {holds print |selector|}
  21951. @!k:pool_pointer; {index into |str_pool|}
  21952. begin synch_h; synch_v;@/
  21953. old_setting:=selector; selector:=new_string;
  21954. show_token_list(link(write_tokens(p)),null,pool_size-pool_ptr);
  21955. selector:=old_setting;
  21956. str_room(1);
  21957. if cur_length<256 then
  21958. begin dvi_out(xxx1); dvi_out(cur_length);
  21959. end
  21960. else begin dvi_out(xxx4); dvi_four(cur_length);
  21961. end;
  21962. for k:=str_start[str_ptr] to pool_ptr-1 do dvi_out(so(str_pool[k]));
  21963. pool_ptr:=str_start[str_ptr]; {erase the string}
  21964. end;
  21965. @ To write a token list, we must run it through \TeX's scanner, expanding
  21966. macros and \.{\\the} and \.{\\number}, etc. This might cause runaways,
  21967. if a delimited macro parameter isn't matched, and runaways would be
  21968. extremely confusing since we are calling on \TeX's scanner in the middle
  21969. of a \.{\\shipout} command. Therefore we will put a dummy control sequence as
  21970. a ``stopper,'' right after the token list. This control sequence is
  21971. artificially defined to be \.{\\outer}.
  21972. @:end_write_}{\.{\\endwrite}@>
  21973. @<Initialize table...@>=
  21974. text(end_write):="endwrite"; eq_level(end_write):=level_one;
  21975. eq_type(end_write):=outer_call; equiv(end_write):=null;
  21976. @ @<Declare procedures needed in |hlist_out|, |vlist_out|@>=
  21977. procedure write_out(@!p:pointer);
  21978. var old_setting:0..max_selector; {holds print |selector|}
  21979. @!old_mode:integer; {saved |mode|}
  21980. @!j:small_number; {write stream number}
  21981. @!q,@!r:pointer; {temporary variables for list manipulation}
  21982. begin @<Expand macros in the token list
  21983. and make |link(def_ref)| point to the result@>;
  21984. old_setting:=selector; j:=write_stream(p);
  21985. if write_open[j] then selector:=j
  21986. else begin {write to the terminal if file isn't open}
  21987. if (j=17)and(selector=term_and_log) then selector:=log_only;
  21988. print_nl("");
  21989. end;
  21990. token_show(def_ref); print_ln;
  21991. flush_list(def_ref); selector:=old_setting;
  21992. end;
  21993. @ The final line of this routine is slightly subtle; at least, the author
  21994. didn't think about it until getting burnt! There is a used-up token list
  21995. @^Knuth, Donald Ervin@>
  21996. on the stack, namely the one that contained |end_write_token|. (We
  21997. insert this artificial `\.{\\endwrite}' to prevent runaways, as explained
  21998. above.) If it were not removed, and if there were numerous writes on a
  21999. single page, the stack would overflow.
  22000. @d end_write_token==cs_token_flag+end_write
  22001. @<Expand macros in the token list and...@>=
  22002. q:=get_avail; info(q):=right_brace_token+"}";@/
  22003. r:=get_avail; link(q):=r; info(r):=end_write_token; ins_list(q);@/
  22004. begin_token_list(write_tokens(p),write_text);@/
  22005. q:=get_avail; info(q):=left_brace_token+"{"; ins_list(q);
  22006. {now we're ready to scan
  22007. `\.\{$\langle\,$token list$\,\rangle$\.{\} \\endwrite}'}
  22008. old_mode:=mode; mode:=0;
  22009. {disable \.{\\prevdepth}, \.{\\spacefactor}, \.{\\lastskip}, \.{\\prevgraf}}
  22010. cur_cs:=write_loc; q:=scan_toks(false,true); {expand macros, etc.}
  22011. get_token;@+if cur_tok<>end_write_token then
  22012. @<Recover from an unbalanced write command@>;
  22013. mode:=old_mode;
  22014. end_token_list {conserve stack space}
  22015. @ @<Recover from an unbalanced write command@>=
  22016. begin print_err("Unbalanced write command");
  22017. @.Unbalanced write...@>
  22018. help2("On this page there's a \write with fewer real {'s than }'s.")@/
  22019. ("I can't handle that very well; good luck."); error;
  22020. repeat get_token;
  22021. until cur_tok=end_write_token;
  22022. end
  22023. @ The |out_what| procedure takes care of outputting whatsit nodes for
  22024. |vlist_out| and |hlist_out|\kern-.3pt.
  22025. @<Declare procedures needed in |hlist_out|, |vlist_out|@>=
  22026. procedure out_what(@!p:pointer);
  22027. var j:small_number; {write stream number}
  22028. begin case subtype(p) of
  22029. open_node,write_node,close_node:@<Do some work that has been queued up
  22030. for \.{\\write}@>;
  22031. special_node:special_out(p);
  22032. language_node:do_nothing;
  22033. othercases confusion("ext4")
  22034. @:this can't happen ext4}{\quad ext4@>
  22035. endcases;
  22036. end;
  22037. @ We don't implement \.{\\write} inside of leaders. (The reason is that
  22038. the number of times a leader box appears might be different in different
  22039. implementations, due to machine-dependent rounding in the glue calculations.)
  22040. @^leaders@>
  22041. @<Do some work that has been queued up...@>=
  22042. if not doing_leaders then
  22043. begin j:=write_stream(p);
  22044. if subtype(p)=write_node then write_out(p)
  22045. else begin if write_open[j] then a_close(write_file[j]);
  22046. if subtype(p)=close_node then write_open[j]:=false
  22047. else if j<16 then
  22048. begin cur_name:=open_name(p); cur_area:=open_area(p);
  22049. cur_ext:=open_ext(p);
  22050. if cur_ext="" then cur_ext:=".tex";
  22051. pack_cur_name;
  22052. while not a_open_out(write_file[j]) do
  22053. prompt_file_name("output file name",".tex");
  22054. write_open[j]:=true;
  22055. end;
  22056. end;
  22057. end
  22058. @ The presence of `\.{\\immediate}' causes the |do_extension| procedure
  22059. to descend to one level of recursion. Nothing happens unless \.{\\immediate}
  22060. is followed by `\.{\\openout}', `\.{\\write}', or `\.{\\closeout}'.
  22061. @^recursion@>
  22062. @<Implement \.{\\immediate}@>=
  22063. begin get_x_token;
  22064. if (cur_cmd=extension)and(cur_chr<=close_node) then
  22065. begin p:=tail; do_extension; {append a whatsit node}
  22066. out_what(tail); {do the action immediately}
  22067. flush_node_list(tail); tail:=p; link(p):=null;
  22068. end
  22069. else back_input;
  22070. end
  22071. @ The \.{\\language} extension is somewhat different.
  22072. We need a subroutine that comes into play when a character of
  22073. a non-|clang| language is being appended to the current paragraph.
  22074. @<Declare action...@>=
  22075. procedure fix_language;
  22076. var @!l:ASCII_code; {the new current language}
  22077. begin if language<=0 then l:=0
  22078. else if language>255 then l:=0
  22079. else l:=language;
  22080. if l<>clang then
  22081. begin new_whatsit(language_node,small_node_size);
  22082. what_lang(tail):=l; clang:=l;@/
  22083. what_lhm(tail):=norm_min(left_hyphen_min);
  22084. what_rhm(tail):=norm_min(right_hyphen_min);
  22085. end;
  22086. end;
  22087. @ @<Implement \.{\\setlanguage}@>=
  22088. if abs(mode)<>hmode then report_illegal_case
  22089. else begin new_whatsit(language_node,small_node_size);
  22090. scan_int;
  22091. if cur_val<=0 then clang:=0
  22092. else if cur_val>255 then clang:=0
  22093. else clang:=cur_val;
  22094. what_lang(tail):=clang;
  22095. what_lhm(tail):=norm_min(left_hyphen_min);
  22096. what_rhm(tail):=norm_min(right_hyphen_min);
  22097. end
  22098. @ @<Finish the extensions@>=
  22099. for k:=0 to 15 do if write_open[k] then a_close(write_file[k])
  22100. @* \[54] System-dependent changes.
  22101. This section should be replaced, if necessary, by any special
  22102. modifications of the program
  22103. that are necessary to make \TeX\ work at a particular installation.
  22104. It is usually best to design your change file so that all changes to
  22105. previous sections preserve the section numbering; then everybody's version
  22106. will be consistent with the published program. More extensive changes,
  22107. which introduce new sections, can be inserted here; then only the index
  22108. itself will get a new section number.
  22109. @^system dependencies@>
  22110. @* \[55] Index.
  22111. Here is where you can find all uses of each identifier in the program,
  22112. with underlined entries pointing to where the identifier was defined.
  22113. If the identifier is only one letter long, however, you get to see only
  22114. the underlined entries. {\sl All references are to section numbers instead of
  22115. page numbers.}
  22116. This index also lists error messages and other aspects of the program
  22117. that you might want to look up some day. For example, the entry
  22118. for ``system dependencies'' lists all sections that should receive
  22119. special attention from people who are installing \TeX\ in a new
  22120. operating environment. A list of various things that can't happen appears
  22121. under ``this can't happen''. Approximately 40 sections are listed under
  22122. ``inner loop''; these account for about 60\pct! of \TeX's running time,
  22123. exclusive of input and output.