session.c 161 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701
  1. /* session.c -- user windowing interface to Info.
  2. $Id$
  3. Copyright 1993, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
  4. 2004, 2007, 2008, 2009, 2011, 2012, 2013, 2014, 2015, 2016, 2017
  5. Free Software Foundation, Inc.
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. Originally written by Brian Fox. */
  17. #include "info.h"
  18. #include "display.h"
  19. #include "session.h"
  20. #include "echo-area.h"
  21. #include "search.h"
  22. #include "footnotes.h"
  23. #include "man.h"
  24. #include "variables.h"
  25. #ifdef HAVE_SYS_IOCTL_H
  26. #include <sys/ioctl.h>
  27. #endif
  28. #ifdef __MINGW32__
  29. # define read(f,b,s) w32_read(f,b,s)
  30. # define _read(f,b,s) w32_read(f,b,s)
  31. extern ssize_t w32_read (int, void *, size_t);
  32. #endif
  33. #if defined (HAVE_SYS_TIME_H)
  34. # include <sys/time.h>
  35. # define HAVE_STRUCT_TIMEVAL
  36. #endif /* HAVE_SYS_TIME_H */
  37. /* **************************************************************** */
  38. /* */
  39. /* Running an Info Session */
  40. /* */
  41. /* **************************************************************** */
  42. static void mouse_event_handler (void);
  43. /* The place that we are reading input from. */
  44. static FILE *info_input_stream = NULL;
  45. /* Becomes non-zero when 'q' is typed to an Info window. */
  46. static int quit_info_immediately = 0;
  47. static NODE *allfiles_node = 0;
  48. DECLARE_INFO_COMMAND (info_all_files, _("Show all matching files"))
  49. {
  50. if (!allfiles_node)
  51. {
  52. info_error (_("No file index"));
  53. return;
  54. }
  55. /* FIXME: Copy allfiles_node so it is unique in the window history? */
  56. info_set_node_of_window (window, allfiles_node);
  57. }
  58. static void
  59. allfiles_create_node (char *term, REFERENCE **fref)
  60. {
  61. int i;
  62. struct text_buffer text;
  63. text_buffer_init (&text);
  64. text_buffer_printf (&text,
  65. "%s File names matching '%s'\n\n"
  66. "Info File Index\n"
  67. "***************\n\n"
  68. "File names that match '%s':\n",
  69. INFO_NODE_LABEL,
  70. term, term);
  71. /* Mark as an index so that destinations are never hidden. */
  72. text_buffer_add_string (&text, "\0\b[index\0\b]", 11);
  73. text_buffer_printf (&text, "\n* Menu:\n\n");
  74. for (i = 0; fref[i]; i++)
  75. {
  76. text_buffer_printf (&text, "* %4i: (%s)", i+1, fref[i]->filename);
  77. if (fref[i]->nodename)
  78. text_buffer_printf (&text, "%s", fref[i]->nodename);
  79. text_buffer_printf (&text, ".\n");
  80. }
  81. allfiles_node = info_create_node ();
  82. allfiles_node->fullpath = xstrdup ("");
  83. allfiles_node->nodename = xstrdup ("*Info File Index*");
  84. allfiles_node->contents = text_buffer_base (&text);
  85. allfiles_node->nodelen = text_buffer_off (&text);
  86. allfiles_node->body_start = strcspn (allfiles_node->contents, "\n");
  87. scan_node_contents (allfiles_node, 0, 0);
  88. }
  89. /* Begin an info session finding the nodes specified by REFERENCES. For
  90. each loaded node, create a new window. Always split the largest of the
  91. available windows. Display ERROR in echo area if non-null. */
  92. static void
  93. begin_multiple_window_info_session (REFERENCE **references, char *error)
  94. {
  95. register int i;
  96. WINDOW *window = 0;
  97. for (i = 0; references && references[i]; i++)
  98. {
  99. if (!window)
  100. {
  101. window = active_window;
  102. info_select_reference (window, references[i]);
  103. if (!window->node)
  104. window = 0;
  105. }
  106. else
  107. {
  108. /* Find the largest window in WINDOWS, and make that be the active
  109. one. Then split it and add our window and node to the list
  110. of remembered windows and nodes. Then tile the windows. */
  111. WINDOW *win, *largest = NULL;
  112. int max_height = 0;
  113. for (win = windows; win; win = win->next)
  114. if (win->height > max_height)
  115. {
  116. max_height = win->height;
  117. largest = win;
  118. }
  119. if (!largest)
  120. {
  121. display_update_display ();
  122. info_error ("%s", msg_cant_find_window);
  123. return;
  124. }
  125. active_window = largest;
  126. window = window_make_window ();
  127. info_select_reference (window, references[i]);
  128. if (!window->node)
  129. {
  130. /* We couldn't find the node referenced. */
  131. window_delete_window (window);
  132. window = 0;
  133. }
  134. if (window)
  135. window_tile_windows (TILE_INTERNALS);
  136. else
  137. {
  138. display_update_display ();
  139. info_error ("%s", msg_win_too_small);
  140. return;
  141. }
  142. }
  143. }
  144. /* Load dir node as a back-up if there were no references given, or if
  145. none of them were valid. */
  146. if (!window)
  147. {
  148. info_set_node_of_window (active_window, get_dir_node ());
  149. return;
  150. }
  151. }
  152. static void
  153. display_startup_message (void)
  154. {
  155. char *format;
  156. format = replace_in_documentation
  157. /* TRANSLATORS: Try to keep this message (when "expanded") at most 79
  158. characters; anything after the 79th character will not actually be
  159. displayed on an 80-column terminal. */
  160. (_("Welcome to Info version %s. Type \\[get-help-window] for help, \\[get-info-help-node] for tutorial."),
  161. 0);
  162. window_message_in_echo_area (format, VERSION, NULL);
  163. }
  164. /* Run an Info session. If USER_FILENAME is null, create a window for each
  165. node referenced in REF_LIST. If USER_FILENAME is not null, "--all" was
  166. used on the command line, and display a file index with entries in
  167. REF_LIST. ERROR is an optional error message to display at start-up. */
  168. void
  169. info_session (REFERENCE **ref_list, char *user_filename, char *error)
  170. {
  171. /* Initialize the Info session. */
  172. initialize_info_session ();
  173. if (!error)
  174. display_startup_message ();
  175. else
  176. show_error_node (error);
  177. if (!user_filename)
  178. begin_multiple_window_info_session (ref_list, error);
  179. else
  180. {
  181. allfiles_create_node (user_filename, ref_list);
  182. info_set_node_of_window (active_window, allfiles_node);
  183. }
  184. info_read_and_dispatch ();
  185. close_info_session ();
  186. }
  187. void info_next_line (WINDOW *, int count);
  188. void info_prev_line (WINDOW *, int count);
  189. static int info_keyseq_displayed_p;
  190. void
  191. info_read_and_dispatch (void)
  192. {
  193. VFunction *cmd;
  194. int count;
  195. for (quit_info_immediately = 0; !quit_info_immediately; )
  196. {
  197. if (!info_any_buffered_input_p ())
  198. display_update_display ();
  199. /* Some redisplay might be necessary if the cursor has moved and
  200. a different reference (or no reference) has to be highlighted. */
  201. if (hl_ref_rendition.mask)
  202. display_update_one_window (active_window);
  203. display_cursor_at_point (active_window);
  204. cmd = read_key_sequence (info_keymap, 1, 1, 0, &count);
  205. if (cmd)
  206. {
  207. if (!info_keyseq_displayed_p)
  208. window_clear_echo_area ();
  209. (*cmd) (active_window, count);
  210. /* Don't change the goal column when going up and down. This
  211. means we can go from a long line to a short line and back to
  212. a long line and end back in the same column. */
  213. if (!(cmd == &info_next_line || cmd == &info_prev_line))
  214. active_window->goal_column = -1; /* Goal is current column. */
  215. }
  216. }
  217. }
  218. /* Found in signals.c */
  219. extern void initialize_info_signal_handler (void );
  220. /* Initialize terminal, read configuration file and set key bindings. */
  221. void
  222. initialize_terminal_and_keymaps (char *init_file)
  223. {
  224. char *term_name = getenv ("TERM");
  225. terminal_initialize_terminal (term_name);
  226. read_init_file (init_file);
  227. }
  228. /* Initialize the first info session by starting the terminal, window,
  229. and display systems. */
  230. void
  231. initialize_info_session (void)
  232. {
  233. if (!terminal_prep_terminal ())
  234. {
  235. /* Terminal too dumb to run interactively. */
  236. char *term_name = getenv ("TERM");
  237. info_error (msg_term_too_dumb, term_name);
  238. exit (EXIT_FAILURE);
  239. }
  240. terminal_clear_screen ();
  241. window_initialize_windows (screenwidth, screenheight);
  242. initialize_info_signal_handler ();
  243. display_initialize_display (screenwidth, screenheight);
  244. /* If input has not been redirected yet, make it come from unbuffered
  245. standard input. */
  246. if (!info_input_stream)
  247. {
  248. setbuf (stdin, NULL);
  249. info_input_stream = stdin;
  250. }
  251. info_windows_initialized_p = 1;
  252. }
  253. /* On program exit, leave the cursor at the bottom of the window, and
  254. restore the terminal I/O. */
  255. void
  256. close_info_session (void)
  257. {
  258. terminal_goto_xy (0, screenheight - 1);
  259. terminal_clear_to_eol ();
  260. fflush (stdout);
  261. terminal_unprep_terminal ();
  262. close_dribble_file ();
  263. }
  264. /* Tell Info that input is coming from the file FILENAME. */
  265. void
  266. info_set_input_from_file (char *filename)
  267. {
  268. FILE *stream;
  269. /* Input may include binary characters. */
  270. stream = fopen (filename, FOPEN_RBIN);
  271. if (!stream)
  272. return;
  273. if ((info_input_stream != NULL) &&
  274. (info_input_stream != stdin))
  275. fclose (info_input_stream);
  276. info_input_stream = stream;
  277. if (stream != stdin)
  278. display_inhibited = 1;
  279. }
  280. /* **************************************************************** */
  281. /* */
  282. /* Input Character Buffering */
  283. /* */
  284. /* **************************************************************** */
  285. static void fill_input_buffer (int wait);
  286. static int info_gather_typeahead (int);
  287. /* Largest number of characters that we can read in advance. */
  288. #define MAX_INFO_INPUT_BUFFERING 512
  289. static int pop_index = 0; /* Where to remove bytes from input buffer. */
  290. static int push_index = 0; /* Where to add bytes to input buffer. */
  291. static unsigned char info_input_buffer[MAX_INFO_INPUT_BUFFERING];
  292. /* Get a key from the buffer of characters to be read.
  293. Return the key in KEY.
  294. Result is non-zero if there was a key, or 0 if there wasn't. */
  295. static int
  296. get_byte_from_input_buffer (unsigned char *key)
  297. {
  298. if (push_index == pop_index)
  299. return 0;
  300. *key = info_input_buffer[pop_index++];
  301. if (pop_index >= MAX_INFO_INPUT_BUFFERING)
  302. pop_index = 0;
  303. return 1;
  304. }
  305. int
  306. info_any_buffered_input_p (void)
  307. {
  308. fill_input_buffer (0);
  309. return push_index != pop_index;
  310. }
  311. /* Wrapper around info_gather_typeahead which handles read errors and reaching
  312. end-of-file. */
  313. static void
  314. fill_input_buffer (int wait)
  315. {
  316. while (1)
  317. {
  318. int success;
  319. do
  320. {
  321. success = info_gather_typeahead (wait);
  322. }
  323. while (!success && errno == EINTR); /* Try again if the read was
  324. interrupted due to a signal. */
  325. if (success || !wait)
  326. return;
  327. /* Reading failed. If we were reading from a dribble file with
  328. --restore, switch to standard input. Otherwise quit. */
  329. if (info_input_stream != stdin)
  330. {
  331. fclose (info_input_stream);
  332. info_input_stream = stdin;
  333. display_inhibited = 0;
  334. display_update_display ();
  335. display_cursor_at_point (active_window);
  336. }
  337. else
  338. {
  339. close_info_session ();
  340. exit (EXIT_SUCCESS);
  341. }
  342. }
  343. }
  344. /* Read bytes and stuff them into info_input_buffer. If WAIT is true, wait
  345. for input; otherwise don't do anything if there is no input waiting.
  346. Return 1 on success, 0 on error. ERRNO may be set by read(). */
  347. static int
  348. info_gather_typeahead (int wait)
  349. {
  350. register int i = 0;
  351. int tty, space_avail;
  352. long chars_avail;
  353. unsigned char input[MAX_INFO_INPUT_BUFFERING];
  354. tty = fileno (info_input_stream);
  355. chars_avail = 0;
  356. /* Clear errno. */
  357. errno = 0;
  358. /* There may be characters left over from last time, in which case we don't
  359. want to wait for another key to be pressed. */
  360. if (wait && pop_index == push_index)
  361. {
  362. char c;
  363. /* Wait until there is a byte waiting, and then stuff it into the input
  364. buffer. */
  365. if (read (tty, &c, 1) <= 0)
  366. return 0;
  367. if (info_dribble_file)
  368. dribble (c);
  369. info_input_buffer[push_index++] = c;
  370. if (push_index >= MAX_INFO_INPUT_BUFFERING)
  371. push_index = 0;
  372. /* Continue to see if there are more bytes waiting. */
  373. }
  374. /* Get the amount of space available in INFO_INPUT_BUFFER for new chars. */
  375. if (pop_index > push_index)
  376. space_avail = pop_index - push_index;
  377. else
  378. space_avail = sizeof (info_input_buffer) - (push_index - pop_index);
  379. /* If we can just find out how many characters there are to read, do so. */
  380. #if defined (FIONREAD)
  381. {
  382. ioctl (tty, FIONREAD, &chars_avail);
  383. if (chars_avail > space_avail)
  384. chars_avail = space_avail;
  385. if (chars_avail)
  386. chars_avail = read (tty, &input[0], chars_avail);
  387. }
  388. #else /* !FIONREAD */
  389. # if defined (O_NDELAY) && defined (F_GETFL) && defined (F_SETFL)
  390. {
  391. int flags;
  392. flags = fcntl (tty, F_GETFL, 0);
  393. fcntl (tty, F_SETFL, (flags | O_NDELAY));
  394. chars_avail = read (tty, &input[0], space_avail);
  395. fcntl (tty, F_SETFL, flags);
  396. if (chars_avail == -1)
  397. chars_avail = 0;
  398. }
  399. # else /* !O_NDELAY */
  400. # ifdef __DJGPP__
  401. {
  402. extern long pc_term_chars_avail (void);
  403. if (isatty (tty))
  404. chars_avail = pc_term_chars_avail ();
  405. else
  406. {
  407. /* We could be more accurate by calling ltell, but we have no idea
  408. whether tty is buffered by stdio functions, and if so, how many
  409. characters are already waiting in the buffer. So we punt. */
  410. struct stat st;
  411. if (fstat (tty, &st) < 0)
  412. chars_avail = 1;
  413. else
  414. chars_avail = st.st_size;
  415. }
  416. if (chars_avail > space_avail)
  417. chars_avail = space_avail;
  418. if (chars_avail)
  419. chars_avail = read (tty, &input[0], chars_avail);
  420. }
  421. # else
  422. # ifdef __MINGW32__
  423. {
  424. extern long w32_chars_avail (int);
  425. chars_avail = w32_chars_avail (tty);
  426. if (chars_avail > space_avail)
  427. chars_avail = space_avail;
  428. if (chars_avail)
  429. chars_avail = read (tty, &input[0], chars_avail);
  430. }
  431. # endif /* _WIN32 */
  432. # endif/* __DJGPP__ */
  433. # endif /* O_NDELAY */
  434. #endif /* !FIONREAD */
  435. while (i < chars_avail)
  436. {
  437. if (info_dribble_file)
  438. dribble (input[i]);
  439. /* Add KEY to the buffer of characters to be read. */
  440. if (input[i] != Control ('g'))
  441. {
  442. info_input_buffer[push_index++] = input[i];
  443. if (push_index >= MAX_INFO_INPUT_BUFFERING)
  444. push_index = 0;
  445. }
  446. else
  447. /* Flush all pending input in the case of C-g pressed. */
  448. push_index = pop_index;
  449. i++;
  450. }
  451. /* If wait is true, there is at least one byte left in the input buffer. */
  452. if (chars_avail <= 0 && !wait)
  453. return 0;
  454. return 1;
  455. }
  456. static int get_input_key_internal (void);
  457. /* Whether to process or skip mouse events in the input stream. */
  458. unsigned char mouse_cb, mouse_cx, mouse_cy;
  459. /* Handle mouse event given that mouse_cb, mouse_cx and mouse_cy contain the
  460. data from the event. See the "XTerm Control Sequences" document for their
  461. meanings. */
  462. void
  463. mouse_event_handler (void)
  464. {
  465. if (mouse_cb & 0x40)
  466. {
  467. void info_up_line (WINDOW *, int count);
  468. void info_down_line (WINDOW *, int count);
  469. switch (mouse_cb & 0x03)
  470. {
  471. case 0: /* Mouse button 4 (scroll up). */
  472. info_up_line (active_window, 3);
  473. break;
  474. case 1: /* Mouse button 5 (scroll down). */
  475. info_down_line (active_window, 3);
  476. break;
  477. }
  478. }
  479. }
  480. /* Return number representing a key that has been pressed, which is an index
  481. into info_keymap and echo_area_keymap. */
  482. int
  483. get_input_key (void)
  484. {
  485. int ret = -1;
  486. while (ret == -1)
  487. {
  488. ret = get_input_key_internal ();
  489. if (ret == KEY_MOUSE)
  490. {
  491. get_byte_from_input_buffer (&mouse_cb);
  492. get_byte_from_input_buffer (&mouse_cx);
  493. get_byte_from_input_buffer (&mouse_cy);
  494. }
  495. }
  496. return ret;
  497. }
  498. /* Time in milliseconds to wait for the next byte of a byte sequence
  499. corresponding to a key or key chord. Settable with the 'key-time' user
  500. variable. */
  501. int key_time = 100;
  502. /* Read bytes from input and return what key has been pressed. Return -1 on
  503. reading an unrecognized key. */
  504. static int
  505. get_input_key_internal (void)
  506. {
  507. BYTEMAP_ENTRY *b;
  508. unsigned char c;
  509. int esc_seen = 0;
  510. int pop_start;
  511. int byte_count = 0;
  512. fill_input_buffer (1);
  513. if (pop_index == push_index)
  514. return -1; /* No input waiting. This shouldn't happen. */
  515. b = byte_seq_to_key;
  516. pop_start = pop_index;
  517. while (pop_index != push_index)
  518. {
  519. int in_map = 0;
  520. int unknown = 0;
  521. if (!get_byte_from_input_buffer (&c))
  522. break; /* Incomplete byte sequence. */
  523. byte_count++;
  524. switch (b[c].type)
  525. {
  526. case BYTEMAP_KEY:
  527. return b[c].key;
  528. case BYTEMAP_ESC:
  529. esc_seen = 1;
  530. /* Fall through. */
  531. case BYTEMAP_MAP:
  532. in_map = 1;
  533. b = b[c].next;
  534. break;
  535. case BYTEMAP_NONE:
  536. unknown = 1;
  537. break;
  538. }
  539. if (unknown)
  540. break;
  541. /* If we read an incomplete byte sequence, pause a short while to
  542. see if more bytes follow. */
  543. if (in_map && pop_index == push_index)
  544. {
  545. int ready = 0;
  546. #if defined (FD_SET)
  547. struct timeval timer, *timerp = 0;
  548. fd_set readfds;
  549. FD_ZERO (&readfds);
  550. FD_SET (fileno (info_input_stream), &readfds);
  551. timer.tv_sec = 0;
  552. timer.tv_usec = key_time * 1000;
  553. timerp = &timer;
  554. ready = select (fileno(info_input_stream)+1, &readfds,
  555. NULL, NULL, timerp);
  556. #else
  557. ready = 1;
  558. #endif /* FD_SET */
  559. if (ready)
  560. fill_input_buffer (0);
  561. }
  562. }
  563. /* Incomplete or unknown byte sequence. Start again with the first byte. */
  564. pop_index = pop_start;
  565. if (!esc_seen || (byte_count >= 3 && key_time == 0))
  566. {
  567. /* If the sequence was incomplete, return the first byte.
  568. Also return the first byte for sequences with ESC that are at
  569. least three bytes long if 'key_time' is 0, to give some support for
  570. specifying byte sequences in .infokey for those sent by unrecognized
  571. special keys (which would otherwise be skipped below). */
  572. pop_index = pop_start;
  573. get_byte_from_input_buffer (&c);
  574. return c;
  575. }
  576. else
  577. {
  578. get_byte_from_input_buffer (&c); /* Should be ESC */
  579. /* If there are no more characters, then decide that the escape key
  580. itself has been pressed. */
  581. if (pop_index == push_index)
  582. return 033;
  583. /* Skip byte sequences that look like they could have come from
  584. unrecognized keys, e.g. F3 or C-S-Left, to avoid them as being
  585. interpreted as random garbage. These might produce sequences
  586. that look like "ESC O R" or "ESC [ 1 ; 6 ~", depending on
  587. the terminal. */
  588. /* Check if the sequence starts ESC O. */
  589. get_byte_from_input_buffer (&c);
  590. if (c == 'O')
  591. {
  592. /* If no more bytes, call it M-O. */
  593. if (!info_any_buffered_input_p ())
  594. return 'O' + KEYMAP_META_BASE;
  595. /* Otherwise it could be an unrecognized key producing a sequence
  596. ESC O (byte). Ignore it, discarding the next byte. */
  597. get_byte_from_input_buffer (&c);
  598. return -1;
  599. }
  600. /* Unknown CSI-style sequences. */
  601. else if (c == '[')
  602. {
  603. /* If no more bytes, call it M-[. */
  604. if (!get_byte_from_input_buffer (&c))
  605. return '[' + KEYMAP_META_BASE;
  606. /* Skip a control sequence as defined by ECMA-48. */
  607. while (c >= 0x30 && c <= 0x3f)
  608. if (!get_byte_from_input_buffer (&c))
  609. break;
  610. while (c >= 0x20 && c <= 0x2f)
  611. if (!get_byte_from_input_buffer (&c))
  612. break;
  613. return -1;
  614. }
  615. else
  616. {
  617. /* The sequence started with ESC, but wasn't recognized. Treat it
  618. as introducing a sequence produced by a key chord with the meta
  619. key pressed. */
  620. return c + KEYMAP_META_BASE;
  621. }
  622. }
  623. }
  624. /* **************************************************************** */
  625. /* */
  626. /* Error handling */
  627. /* */
  628. /* **************************************************************** */
  629. /* Debugging level */
  630. unsigned debug_level;
  631. static FILE *debug_file;
  632. static void
  633. close_debugfile (void)
  634. {
  635. fclose (debug_file);
  636. }
  637. #define INFODEBUG_FILE "infodebug"
  638. static void
  639. vinfo_debug (const char *format, va_list ap)
  640. {
  641. FILE *fp;
  642. if (!debug_file)
  643. {
  644. if (!info_windows_initialized_p || display_inhibited)
  645. fp = stderr;
  646. else
  647. {
  648. debug_file = fopen (INFODEBUG_FILE, "w");
  649. if (!debug_file)
  650. {
  651. info_error (_("can't open %s: %s"), INFODEBUG_FILE,
  652. strerror (errno));
  653. exit (EXIT_FAILURE);
  654. }
  655. atexit (close_debugfile);
  656. fp = debug_file;
  657. info_error (_("debugging output diverted to \"%s\""),
  658. INFODEBUG_FILE);
  659. }
  660. }
  661. else
  662. fp = debug_file;
  663. fprintf (fp, "%s: ", program_name);
  664. vfprintf (fp, format, ap);
  665. fprintf (fp, "\n");
  666. fflush (stderr);
  667. }
  668. void
  669. info_debug (const char *format, ...)
  670. {
  671. va_list ap;
  672. va_start (ap, format);
  673. vinfo_debug (format, ap);
  674. va_end (ap);
  675. }
  676. /* Non-zero means ring terminal bell on errors. */
  677. int info_error_rings_bell_p = 1;
  678. /* Print AP according to FORMAT. If the window system was initialized,
  679. then the message is printed in the echo area. Otherwise, a message is
  680. output to stderr. */
  681. static void
  682. vinfo_error (const char *format, va_list ap)
  683. {
  684. if (!info_windows_initialized_p || display_inhibited)
  685. {
  686. fprintf (stderr, "%s: ", program_name);
  687. vfprintf (stderr, format, ap);
  688. fprintf (stderr, "\n");
  689. fflush (stderr);
  690. }
  691. else
  692. {
  693. if (!echo_area_is_active)
  694. {
  695. if (info_error_rings_bell_p)
  696. terminal_ring_bell ();
  697. vwindow_message_in_echo_area (format, ap);
  698. }
  699. else
  700. {
  701. NODE *temp = build_message_node (format, ap);
  702. if (info_error_rings_bell_p)
  703. terminal_ring_bell ();
  704. inform_in_echo_area (temp->contents);
  705. free (temp->contents);
  706. free (temp);
  707. }
  708. }
  709. }
  710. void
  711. info_error (const char *format, ...)
  712. {
  713. va_list ap;
  714. va_start (ap, format);
  715. vinfo_error (format, ap);
  716. va_end (ap);
  717. }
  718. void
  719. show_error_node (char *error)
  720. {
  721. if (info_error_rings_bell_p)
  722. terminal_ring_bell ();
  723. if (!info_windows_initialized_p)
  724. {
  725. info_error ("%s", error);
  726. }
  727. else if (!echo_area_is_active)
  728. {
  729. window_message_in_echo_area ("%s", error);
  730. }
  731. else
  732. inform_in_echo_area (error);
  733. }
  734. /* **************************************************************** */
  735. /* */
  736. /* Window node history */
  737. /* */
  738. /* **************************************************************** */
  739. /* Free a NODE object that is suitable for being placed in a window. */
  740. void
  741. free_history_node (NODE *n)
  742. {
  743. if (n && (n->flags & N_IsInternal))
  744. {
  745. free (n->contents);
  746. info_free_references (n->references);
  747. free (n->next); free (n->prev); free (n->up);
  748. free (n->nodename);
  749. }
  750. free (n);
  751. }
  752. static void
  753. put_node_in_window (WINDOW *win, NODE *node)
  754. {
  755. win->node = node;
  756. win->pagetop = 0;
  757. win->point = 0;
  758. free_matches (&win->matches);
  759. free (win->line_starts); win->line_starts = 0;
  760. free (win->log_line_no); win->log_line_no = 0;
  761. win->flags |= W_UpdateWindow;
  762. }
  763. /* Go back one in the node history. */
  764. int
  765. forget_node_fast (WINDOW *win)
  766. {
  767. int i = win->hist_index;
  768. if (i == 0)
  769. return 0;
  770. free_history_node (win->hist[i - 1]->node);
  771. free (win->hist[i - 1]);
  772. win->hist[i - 1] = 0;
  773. i = --win->hist_index;
  774. if (i == 0)
  775. /* Window history is empty. */
  776. win->node = 0;
  777. else
  778. {
  779. put_node_in_window (win, win->hist[i - 1]->node);
  780. win->point = win->hist[i - 1]->point;
  781. }
  782. return i;
  783. }
  784. void
  785. forget_node (WINDOW *win)
  786. {
  787. int i = forget_node_fast (win);
  788. if (i == 0)
  789. {
  790. win->node = 0;
  791. return; /* Window history is empty. */
  792. }
  793. window_set_node_of_window (win, win->hist[i - 1]->node);
  794. if (auto_footnotes_p)
  795. info_get_or_remove_footnotes (win);
  796. set_window_pagetop (win, win->hist[i - 1]->pagetop);
  797. win->point = win->hist[i - 1]->point;
  798. window_compute_line_map (win);
  799. win->node->display_pos = win->point;
  800. }
  801. /* Remove associated list of nodes of WINDOW. */
  802. void
  803. forget_window_and_nodes (WINDOW *win)
  804. {
  805. int i;
  806. for (i = 0; i < win->hist_index; i++)
  807. {
  808. free_history_node (win->hist[i]->node);
  809. free (win->hist[i]);
  810. }
  811. free (win->hist);
  812. }
  813. /* Like info_set_node_of_window, but only do enough so to extend the
  814. window history, avoiding calculating line starts. */
  815. void
  816. info_set_node_of_window_fast (WINDOW *win, NODE *node)
  817. {
  818. WINDOW_STATE *new;
  819. if (win->hist_index && win->hist[win->hist_index - 1]->node == win->node)
  820. {
  821. win->hist[win->hist_index - 1]->pagetop = win->pagetop;
  822. win->hist[win->hist_index - 1]->point = win->point;
  823. }
  824. put_node_in_window (win, node);
  825. new = xmalloc (sizeof (WINDOW_STATE));
  826. new->node = win->node;
  827. new->pagetop = win->pagetop;
  828. new->point = win->point;
  829. add_pointer_to_array (new, win->hist_index, win->hist, win->hist_slots, 16);
  830. }
  831. /* Set WINDOW to show NODE. Remember the new window in our list of
  832. Info windows. If we are doing automatic footnote display, try to display
  833. the footnotes for this window. */
  834. void
  835. info_set_node_of_window (WINDOW *win, NODE *node)
  836. {
  837. WINDOW_STATE *new;
  838. /* Remember the current values of pagetop and point if the remembered node
  839. is the same as the current one being displayed. */
  840. if (win->hist_index && win->hist[win->hist_index - 1]->node == win->node)
  841. {
  842. win->hist[win->hist_index - 1]->pagetop = win->pagetop;
  843. win->hist[win->hist_index - 1]->point = win->point;
  844. }
  845. /* Put this node into the window. */
  846. window_set_node_of_window (win, node);
  847. /* Remember this node, the currently displayed pagetop, and the current
  848. location of point in this window. */
  849. new = xmalloc (sizeof (WINDOW_STATE));
  850. new->node = win->node;
  851. new->pagetop = win->pagetop;
  852. new->point = win->point;
  853. add_pointer_to_array (new, win->hist_index, win->hist, win->hist_slots, 16);
  854. /* If doing auto-footnote display/undisplay, show the footnotes belonging
  855. to this window's node. Don't do that if it is a footnote node itself. */
  856. if (auto_footnotes_p
  857. && !((win->node->flags & N_IsInternal)
  858. && !strcmp (win->node->nodename, "*Footnotes*")))
  859. info_get_or_remove_footnotes (win);
  860. }
  861. /* **************************************************************** */
  862. /* */
  863. /* Cursor movement within a node */
  864. /* */
  865. /* **************************************************************** */
  866. static int forward_move_node_structure (WINDOW *window, int behaviour);
  867. static int backward_move_node_structure (WINDOW *window, int behaviour);
  868. /* Controls whether scroll-behavior affects line movement commands */
  869. int cursor_movement_scrolls_p = 1;
  870. /* Variable controlling redisplay of scrolled windows. If non-zero,
  871. it is the desired number of lines to scroll the window in order to
  872. make point visible. A value of 1 produces smooth scrolling. If set
  873. to zero, the line containing point is centered within the window. */
  874. int window_scroll_step = 1;
  875. /* Used after cursor movement commands. Scroll window so that point is
  876. visible, and move the terminal cursor there. */
  877. static void
  878. info_show_point (WINDOW *window)
  879. {
  880. if (window_scroll_step == 0)
  881. window_adjust_pagetop (window);
  882. else
  883. {
  884. int new_pagetop = window->pagetop;
  885. int line = window_line_of_point (window);
  886. if (line < window->pagetop)
  887. new_pagetop -= window_scroll_step;
  888. else if (line >= window->pagetop + window->height)
  889. new_pagetop += window_scroll_step;
  890. /* It's possible that moving by 'scroll-step' still won't show the
  891. point. If so, call window_adjust_pagetop as a backup. */
  892. if (line >= new_pagetop && line < new_pagetop + window->height)
  893. set_window_pagetop (window, new_pagetop);
  894. else
  895. window_adjust_pagetop (window);
  896. }
  897. if (window->flags & W_UpdateWindow)
  898. display_update_one_window (window);
  899. display_cursor_at_point (window);
  900. }
  901. /* Advance point of WIN to the beginning of the next logical line. Compute
  902. line map of new line. Return 0 if we can't go any further. */
  903. static int
  904. point_next_line (WINDOW *win)
  905. {
  906. int line = window_line_of_point (win);
  907. if (line + 1 < win->line_count)
  908. {
  909. win->point = win->line_starts[line + 1];
  910. window_compute_line_map (win);
  911. return 1;
  912. }
  913. if (cursor_movement_scrolls_p
  914. && forward_move_node_structure (win, info_scroll_behaviour) == 0)
  915. {
  916. win->point = 0;
  917. window_compute_line_map (win);
  918. return 1;
  919. }
  920. win->point = win->node->nodelen - 1;
  921. return 0;
  922. }
  923. /* Move point of WIN to the end of the previous logical line. Compute
  924. line map of new line. Return 0 if we can't go any further. */
  925. static int
  926. point_prev_line (WINDOW *win)
  927. {
  928. int line = window_line_of_point (win);
  929. if (line > 0)
  930. {
  931. win->point = win->line_starts[line - 1];
  932. window_compute_line_map (win);
  933. win->point = win->line_map.map[win->line_map.used - 1];
  934. return 1;
  935. }
  936. if (cursor_movement_scrolls_p
  937. && backward_move_node_structure (win, info_scroll_behaviour) == 0)
  938. {
  939. win->point = win->node->nodelen - 1;
  940. if (win->line_count > win->height)
  941. set_window_pagetop (win, win->line_count - win->height);
  942. window_compute_line_map (win);
  943. return 1;
  944. }
  945. win->point = 0;
  946. return 0;
  947. }
  948. /* Return true if POINT sits on a newline character. */
  949. static int
  950. looking_at_newline (WINDOW *win, long point)
  951. {
  952. mbi_iterator_t iter;
  953. mbi_init (iter, win->node->contents + point,
  954. win->node->nodelen - point);
  955. mbi_avail (iter);
  956. return mbi_cur (iter).wc_valid && mbi_cur (iter).wc == '\n';
  957. }
  958. /* Return true if WIN->point sits on an alphanumeric character. */
  959. static int
  960. looking_at_alnum (WINDOW *win)
  961. {
  962. mbi_iterator_t iter;
  963. mbi_init (iter, win->node->contents + win->point,
  964. win->node->nodelen - win->point);
  965. mbi_avail (iter);
  966. return mbi_cur (iter).wc_valid && iswalnum (mbi_cur (iter).wc);
  967. }
  968. /* Advance point to the next multibyte character. */
  969. static void
  970. point_forward_char (WINDOW *win)
  971. {
  972. long point = win->point;
  973. int col;
  974. col = window_point_to_column (win, point, 0);
  975. for (; col < win->line_map.used && win->line_map.map[col] == point; col++)
  976. ;
  977. if (col < win->line_map.used)
  978. win->point = win->line_map.map[col];
  979. else
  980. point_next_line (win);
  981. }
  982. /* Set point to the previous multibyte character. */
  983. static void
  984. point_backward_char (WINDOW *win)
  985. {
  986. long point = win->point;
  987. int col;
  988. col = window_point_to_column (win, point, 0);
  989. for (; col >= 0 && win->line_map.map[col] == point; col--)
  990. ;
  991. if (col >= 0)
  992. win->point = win->line_map.map[col];
  993. else
  994. point_prev_line (win);
  995. }
  996. /* Advance window point to the beginning of the next word. */
  997. static void
  998. point_forward_word (WINDOW *win)
  999. {
  1000. int col;
  1001. col = window_point_to_column (win, win->point, &win->point);
  1002. /* Skip white space forwards. */
  1003. while (1)
  1004. {
  1005. for (; col < win->line_map.used; col++)
  1006. {
  1007. win->point = win->line_map.map[col];
  1008. if (looking_at_alnum (win))
  1009. goto skipped_whitespace;
  1010. }
  1011. if (!point_next_line (win))
  1012. return;
  1013. col = 0;
  1014. }
  1015. skipped_whitespace:
  1016. while (1)
  1017. {
  1018. for (; col < win->line_map.used; col++)
  1019. {
  1020. win->point = win->line_map.map[col];
  1021. if (!looking_at_alnum (win))
  1022. return;
  1023. }
  1024. if (!point_next_line (win))
  1025. return;
  1026. col = 0;
  1027. }
  1028. }
  1029. /* Set window point to the beginning of the previous word. */
  1030. static void
  1031. point_backward_word (WINDOW *win)
  1032. {
  1033. int col;
  1034. point_backward_char (win);
  1035. col = window_point_to_column (win, win->point, &win->point);
  1036. /* Skip white space backwards. */
  1037. while (1)
  1038. {
  1039. for (; col >= 0; col--)
  1040. {
  1041. win->point = win->line_map.map[col];
  1042. if (looking_at_alnum (win))
  1043. goto skipped_whitespace;
  1044. }
  1045. if (!point_prev_line (win))
  1046. return;
  1047. col = win->line_map.used - 1;
  1048. }
  1049. skipped_whitespace:
  1050. while (1)
  1051. {
  1052. for (; col >= 0; col--)
  1053. {
  1054. win->point = win->line_map.map[col];
  1055. if (win->point == 0)
  1056. return;
  1057. if (!looking_at_alnum (win))
  1058. {
  1059. point_forward_char (win);
  1060. return;
  1061. }
  1062. }
  1063. if (!point_prev_line (win))
  1064. return;
  1065. col = win->line_map.used - 1;
  1066. if (looking_at_newline (win, win->point))
  1067. {
  1068. point_forward_char (win);
  1069. return;
  1070. }
  1071. }
  1072. }
  1073. void info_prev_line (WINDOW *, int count);
  1074. /* Move to goal column, or end of line. */
  1075. static void
  1076. move_to_goal_column (WINDOW *window)
  1077. {
  1078. long goal;
  1079. goal = window->goal_column;
  1080. if (goal >= window->line_map.used)
  1081. goal = window->line_map.used - 1;
  1082. window->point = window->line_map.map[goal];
  1083. info_show_point (window);
  1084. }
  1085. /* Move WINDOW's point down to the next line if possible. */
  1086. DECLARE_INFO_COMMAND (info_next_line, _("Move down to the next line"))
  1087. {
  1088. if (count < 0)
  1089. info_prev_line (window, -count);
  1090. else
  1091. {
  1092. if (window->goal_column == -1)
  1093. window->goal_column = window_get_cursor_column (window);
  1094. while (count--)
  1095. point_next_line (window);
  1096. move_to_goal_column (window);
  1097. }
  1098. }
  1099. /* Move WINDOW's point up to the previous line if possible. */
  1100. DECLARE_INFO_COMMAND (info_prev_line, _("Move up to the previous line"))
  1101. {
  1102. if (count < 0)
  1103. info_next_line (window, -count);
  1104. else
  1105. {
  1106. if (window->goal_column == -1)
  1107. window->goal_column = window_get_cursor_column (window);
  1108. while (count--)
  1109. point_prev_line (window);
  1110. move_to_goal_column (window);
  1111. }
  1112. }
  1113. /* Move the cursor to the desired line of the window. */
  1114. DECLARE_INFO_COMMAND (info_move_to_window_line,
  1115. _("Move the cursor to a specific line of the window"))
  1116. {
  1117. int line;
  1118. /* With no numeric argument of any kind, default to the center line. */
  1119. if (!info_explicit_arg && count == 1)
  1120. line = (window->height / 2) + window->pagetop;
  1121. else
  1122. {
  1123. if (count < 0)
  1124. line = (window->height + count) + window->pagetop;
  1125. else
  1126. line = window->pagetop + count;
  1127. }
  1128. /* If the line doesn't appear in this window, make it do so. */
  1129. if (line - window->pagetop >= window->height)
  1130. line = window->pagetop + (window->height - 1);
  1131. /* If the line is too small, make it fit. */
  1132. if (line < window->pagetop)
  1133. line = window->pagetop;
  1134. /* If the selected line is past the bottom of the node, force it back. */
  1135. if (line >= window->line_count)
  1136. line = window->line_count - 1;
  1137. window->point = window->line_starts[line];
  1138. info_show_point (window);
  1139. }
  1140. /* Move WINDOW's point to the end of the logical line. */
  1141. DECLARE_INFO_COMMAND (info_end_of_line, _("Move to the end of the line"))
  1142. {
  1143. long point;
  1144. if (!window->node)
  1145. return;
  1146. /* Find physical line with end of logical line in it. */
  1147. while (!looking_at_newline (window,
  1148. window->line_map.map[window->line_map.used - 1]))
  1149. point_next_line (window);
  1150. if (window->line_map.used == 0)
  1151. return; /* This shouldn't happen. */
  1152. /* Return offset of terminating newline. */
  1153. point = window->line_map.map[window->line_map.used - 1];
  1154. if (point != window->point)
  1155. {
  1156. window->point = point;
  1157. info_show_point (window);
  1158. }
  1159. }
  1160. /* Move WINDOW's point to the beginning of the logical line. */
  1161. DECLARE_INFO_COMMAND (info_beginning_of_line, _("Move to the start of the line"))
  1162. {
  1163. int old_point = window->point;
  1164. int point;
  1165. while (1)
  1166. {
  1167. point = window->line_map.map[0];
  1168. if (point == 0 || looking_at_newline (window, point-1))
  1169. break;
  1170. point_prev_line (window);
  1171. }
  1172. if (point != old_point)
  1173. {
  1174. window->point = point;
  1175. info_show_point (window);
  1176. }
  1177. else
  1178. window->point = old_point;
  1179. }
  1180. void info_backward_char (WINDOW *, int count);
  1181. /* Move point forward in the node. */
  1182. DECLARE_INFO_COMMAND (info_forward_char, _("Move forward a character"))
  1183. {
  1184. if (count < 0)
  1185. info_backward_char (window, -count);
  1186. else
  1187. {
  1188. while (count--)
  1189. point_forward_char (window);
  1190. info_show_point (window);
  1191. }
  1192. }
  1193. /* Move point backward in the node. */
  1194. DECLARE_INFO_COMMAND (info_backward_char, _("Move backward a character"))
  1195. {
  1196. if (count < 0)
  1197. info_forward_char (window, -count);
  1198. else
  1199. {
  1200. while (count--)
  1201. point_backward_char (window);
  1202. info_show_point (window);
  1203. }
  1204. }
  1205. void info_backward_word (WINDOW *, int count);
  1206. /* Move forward a word in this node. */
  1207. DECLARE_INFO_COMMAND (info_forward_word, _("Move forward a word"))
  1208. {
  1209. if (count < 0)
  1210. {
  1211. info_backward_word (window, -count);
  1212. return;
  1213. }
  1214. while (count--)
  1215. point_forward_word (window);
  1216. info_show_point (window);
  1217. }
  1218. DECLARE_INFO_COMMAND (info_backward_word, _("Move backward a word"))
  1219. {
  1220. if (count < 0)
  1221. {
  1222. info_forward_word (window, -count);
  1223. return;
  1224. }
  1225. while (count--)
  1226. point_backward_word (window);
  1227. info_show_point (window);
  1228. }
  1229. /* Move to the beginning of the node. */
  1230. DECLARE_INFO_COMMAND (info_beginning_of_node, _("Move to the start of this node"))
  1231. {
  1232. window->point = 0;
  1233. info_show_point (window);
  1234. }
  1235. /* Move to the end of the node. */
  1236. DECLARE_INFO_COMMAND (info_end_of_node, _("Move to the end of this node"))
  1237. {
  1238. window->point = window->node->nodelen - 1;
  1239. info_show_point (window);
  1240. }
  1241. /* **************************************************************** */
  1242. /* */
  1243. /* Scrolling window */
  1244. /* */
  1245. /* **************************************************************** */
  1246. /* Variable controlling the behaviour of default scrolling when you are
  1247. already at the bottom of a node. Possible values are defined in session.h.
  1248. The meanings are:
  1249. IS_Continuous Try to get first menu item, or failing that, the
  1250. "Next:" pointer, or failing that, the "Up:" and
  1251. "Next:" of the up.
  1252. IS_NextOnly Try to get "Next:" menu item.
  1253. IS_PageOnly Simply give up at the bottom of a node. */
  1254. int info_scroll_behaviour = IS_Continuous;
  1255. static void _scroll_forward (WINDOW *window, int count, int nodeonly);
  1256. static void _scroll_backward (WINDOW *window, int count, int nodeonly);
  1257. static void
  1258. _scroll_forward (WINDOW *window, int count, int nodeonly)
  1259. {
  1260. if (count < 0)
  1261. _scroll_backward (window, -count, nodeonly);
  1262. else
  1263. {
  1264. if (window->pagetop >= window->line_count - window->height)
  1265. {
  1266. if (!nodeonly)
  1267. {
  1268. /* If there are no more lines to scroll here, error, or get
  1269. another node. */
  1270. if (forward_move_node_structure (window, info_scroll_behaviour)
  1271. == 0)
  1272. window->point = 0;
  1273. else
  1274. info_end_of_node (window, 1);
  1275. }
  1276. return;
  1277. }
  1278. set_window_pagetop (window, window->pagetop + count);
  1279. }
  1280. }
  1281. static void
  1282. _scroll_backward (WINDOW *window, int count, int nodeonly)
  1283. {
  1284. if (count < 0)
  1285. _scroll_backward (window, -count, nodeonly);
  1286. else
  1287. {
  1288. int desired_top;
  1289. if (window->pagetop <= 0)
  1290. {
  1291. if (!nodeonly)
  1292. {
  1293. /* If there are no more lines to scroll here, error, or get
  1294. another node. */
  1295. if (backward_move_node_structure (window, info_scroll_behaviour)
  1296. == 0)
  1297. {
  1298. info_end_of_node (window, 1);
  1299. window->point = window->line_starts[window->pagetop];
  1300. }
  1301. else
  1302. window->point = 0;
  1303. }
  1304. return;
  1305. }
  1306. desired_top = window->pagetop - count;
  1307. if (desired_top < 0)
  1308. desired_top = 0;
  1309. set_window_pagetop (window, desired_top);
  1310. }
  1311. }
  1312. /* Lines to scroll by. -1 means scroll by screen size. */
  1313. int default_window_size = -1;
  1314. /* Show the next screen of WINDOW's node. */
  1315. DECLARE_INFO_COMMAND (info_scroll_forward, _("Scroll forward in this window"))
  1316. {
  1317. int lines;
  1318. if (info_explicit_arg)
  1319. lines = count;
  1320. else if (default_window_size > 0)
  1321. lines = default_window_size * count;
  1322. else
  1323. lines = (window->height - 2) * count;
  1324. _scroll_forward (window, lines, 0);
  1325. }
  1326. /* Show the previous screen of WINDOW's node. */
  1327. DECLARE_INFO_COMMAND (info_scroll_backward, _("Scroll backward in this window"))
  1328. {
  1329. info_scroll_forward (window, -count);
  1330. }
  1331. /* Like info_scroll_forward, but sets default_window_size as a side
  1332. effect. */
  1333. DECLARE_INFO_COMMAND (info_scroll_forward_set_window,
  1334. _("Scroll forward in this window and set default window size"))
  1335. {
  1336. if (info_explicit_arg)
  1337. {
  1338. default_window_size = count;
  1339. if (default_window_size < 0)
  1340. default_window_size *= -1;
  1341. }
  1342. info_scroll_forward (window, count);
  1343. }
  1344. /* Like info_scroll_backward, but sets default_window_size as a side
  1345. effect. */
  1346. DECLARE_INFO_COMMAND (info_scroll_backward_set_window,
  1347. _("Scroll backward in this window and set default window size"))
  1348. {
  1349. info_scroll_forward_set_window (window, -count);
  1350. }
  1351. /* Show the next screen of WINDOW's node but never advance to next node. */
  1352. DECLARE_INFO_COMMAND (info_scroll_forward_page_only, _("Scroll forward in this window staying within node"))
  1353. {
  1354. int lines;
  1355. if (info_explicit_arg)
  1356. lines = count;
  1357. else if (default_window_size > 0)
  1358. lines = default_window_size * count;
  1359. else
  1360. lines = (window->height - 2) * count;
  1361. _scroll_forward (window, lines, 1);
  1362. }
  1363. /* Show the previous screen of WINDOW's node but never move to previous
  1364. node. */
  1365. DECLARE_INFO_COMMAND (info_scroll_backward_page_only, _("Scroll backward in this window staying within node"))
  1366. {
  1367. info_scroll_forward_page_only (window, -count);
  1368. }
  1369. /* Like info_scroll_forward_page_only, but sets default_window_size as a side
  1370. effect. */
  1371. DECLARE_INFO_COMMAND (info_scroll_forward_page_only_set_window,
  1372. _("Scroll forward in this window staying within node and set default window size"))
  1373. {
  1374. int lines;
  1375. if (info_explicit_arg)
  1376. {
  1377. default_window_size = count;
  1378. count = 1;
  1379. if (default_window_size < 0)
  1380. {
  1381. default_window_size *= -1;
  1382. count = -1;
  1383. }
  1384. }
  1385. if (default_window_size > 0)
  1386. lines = default_window_size * count;
  1387. else
  1388. lines = (window->height - 2) * count;
  1389. _scroll_forward (window, lines, 1);
  1390. }
  1391. /* Like info_scroll_backward_page_only, but sets default_window_size as a side
  1392. effect. */
  1393. DECLARE_INFO_COMMAND (info_scroll_backward_page_only_set_window,
  1394. _("Scroll backward in this window staying within node and set default window size"))
  1395. {
  1396. info_scroll_forward_page_only_set_window (window, -count);
  1397. }
  1398. /* Scroll the window forward by N lines. */
  1399. DECLARE_INFO_COMMAND (info_down_line, _("Scroll down by lines"))
  1400. {
  1401. _scroll_forward (window, count, 1);
  1402. }
  1403. /* Scroll the window backward by N lines. */
  1404. DECLARE_INFO_COMMAND (info_up_line, _("Scroll up by lines"))
  1405. {
  1406. _scroll_backward (window, count, 1);
  1407. }
  1408. /* Lines to scroll when using commands that scroll by half screen size
  1409. by default. 0 means scroll by half screen size. */
  1410. int default_scroll_size = 0;
  1411. /* Scroll the window forward by N lines and remember N as default for
  1412. subsequent commands. */
  1413. DECLARE_INFO_COMMAND (info_scroll_half_screen_down,
  1414. _("Scroll down by half screen size"))
  1415. {
  1416. int lines;
  1417. if (info_explicit_arg)
  1418. {
  1419. default_scroll_size = count;
  1420. count = 1;
  1421. if (default_scroll_size < 0)
  1422. {
  1423. default_scroll_size *= -1;
  1424. count = -1;
  1425. }
  1426. }
  1427. if (default_scroll_size != 0)
  1428. lines = default_scroll_size * count;
  1429. else
  1430. lines = (window->height + 1) / 2 * count;
  1431. _scroll_forward (window, lines, 1);
  1432. }
  1433. /* Scroll the window backward by N lines and remember N as default for
  1434. subsequent commands. */
  1435. DECLARE_INFO_COMMAND (info_scroll_half_screen_up,
  1436. _("Scroll up by half screen size"))
  1437. {
  1438. info_scroll_half_screen_down (window, -count);
  1439. }
  1440. /* Scroll the "other" window of WINDOW. */
  1441. DECLARE_INFO_COMMAND (info_scroll_other_window, _("Scroll the other window"))
  1442. {
  1443. WINDOW *other;
  1444. /* If only one window, give up. */
  1445. if (!windows->next)
  1446. {
  1447. info_error ("%s", msg_one_window);
  1448. return;
  1449. }
  1450. other = window->next;
  1451. if (!other)
  1452. other = window->prev;
  1453. info_scroll_forward (other, count);
  1454. }
  1455. /* Scroll the "other" window of WINDOW. */
  1456. DECLARE_INFO_COMMAND (info_scroll_other_window_backward,
  1457. _("Scroll the other window backward"))
  1458. {
  1459. info_scroll_other_window (window, -count);
  1460. }
  1461. /* **************************************************************** */
  1462. /* */
  1463. /* Garbage collection */
  1464. /* */
  1465. /* **************************************************************** */
  1466. static void
  1467. gc_file_buffers_and_nodes (void)
  1468. {
  1469. /* Array to record whether each file buffer was referenced or not. */
  1470. int *fb_referenced = xcalloc (info_loaded_files_index, sizeof (int));
  1471. WINDOW *win;
  1472. int i;
  1473. int fb_index;
  1474. /* Loop over nodes in the history of displayed windows recording
  1475. which file buffers were referenced. */
  1476. for (win = windows; win; win = win->next)
  1477. {
  1478. if (!win->hist)
  1479. continue;
  1480. for (i = 0; win->hist[i]; i++)
  1481. {
  1482. NODE *n = win->hist[i]->node;
  1483. /* Loop over file buffers. */
  1484. for (fb_index = 0; fb_index < info_loaded_files_index; fb_index++)
  1485. {
  1486. FILE_BUFFER *fb = info_loaded_files[fb_index];
  1487. /* Each node should match at most one file, either a subfile or a
  1488. non-split file. */
  1489. if (fb->flags & N_Subfile)
  1490. {
  1491. if (n->subfile && !FILENAME_CMP (fb->fullpath, n->subfile))
  1492. {
  1493. fb_referenced[fb_index] = 1;
  1494. break;
  1495. }
  1496. }
  1497. else if (!(fb->flags & N_TagsIndirect))
  1498. {
  1499. if (n->fullpath && !FILENAME_CMP (fb->fullpath, n->fullpath))
  1500. {
  1501. fb_referenced[fb_index] = 1;
  1502. break;
  1503. }
  1504. }
  1505. }
  1506. }
  1507. }
  1508. /* Free unreferenced file buffers. */
  1509. for (i = 0; i < info_loaded_files_index; i++)
  1510. {
  1511. if (!fb_referenced[i])
  1512. {
  1513. FILE_BUFFER *fb = info_loaded_files[i];
  1514. TAG **t;
  1515. if (fb->flags & N_TagsIndirect)
  1516. continue;
  1517. /* If already gc-ed, do nothing. */
  1518. if (!fb->contents)
  1519. continue;
  1520. /* If this file had to be uncompressed, check to see if we should
  1521. gc it. This means that the user-variable "gc-compressed-files"
  1522. is non-zero. */
  1523. if ((fb->flags & N_IsCompressed) && !gc_compressed_files)
  1524. continue;
  1525. /* If this file's contents are not gc-able, move on. */
  1526. if (fb->flags & N_CannotGC)
  1527. continue;
  1528. /* Don't free file buffers corresponding to files that aren't there
  1529. any more, because a node may still refer to them. */
  1530. if (fb->flags & N_Gone)
  1531. continue;
  1532. free (fb->contents);
  1533. fb->contents = 0;
  1534. /* Clear pointers into the file contents in the tags table. */
  1535. if (fb->tags)
  1536. for (t = fb->tags; (*t); t++)
  1537. {
  1538. if (!((*t)->cache.flags & N_WasRewritten))
  1539. (*t)->cache.contents = 0;
  1540. }
  1541. }
  1542. }
  1543. free (fb_referenced);
  1544. }
  1545. /* **************************************************************** */
  1546. /* */
  1547. /* Commands for Manipulating Windows */
  1548. /* */
  1549. /* **************************************************************** */
  1550. void info_prev_window (WINDOW *, int count);
  1551. /* Make the next window in the chain be the active window. */
  1552. DECLARE_INFO_COMMAND (info_next_window, _("Select the next window"))
  1553. {
  1554. if (count < 0)
  1555. {
  1556. info_prev_window (window, -count);
  1557. return;
  1558. }
  1559. /* If no other window, error now. */
  1560. if (!windows->next || echo_area_is_active)
  1561. {
  1562. info_error ("%s", msg_one_window);
  1563. return;
  1564. }
  1565. while (count--)
  1566. {
  1567. if (window->next)
  1568. window = window->next;
  1569. else
  1570. window = windows;
  1571. }
  1572. if (active_window != window)
  1573. {
  1574. if (auto_footnotes_p)
  1575. info_get_or_remove_footnotes (window);
  1576. /* Point may not be within part of node displayed in window if there
  1577. has been resizing of this window since the last time it was active. */
  1578. window_adjust_pagetop (window);
  1579. window->flags |= W_UpdateWindow;
  1580. active_window = window;
  1581. }
  1582. }
  1583. /* Make the previous window in the chain be the active window. */
  1584. DECLARE_INFO_COMMAND (info_prev_window, _("Select the previous window"))
  1585. {
  1586. if (count < 0)
  1587. {
  1588. info_next_window (window, -count);
  1589. return;
  1590. }
  1591. /* Only one window? */
  1592. if (!windows->next || echo_area_is_active)
  1593. {
  1594. info_error ("%s", msg_one_window);
  1595. return;
  1596. }
  1597. while (count--)
  1598. {
  1599. if (window != windows && window->prev)
  1600. window = window->prev;
  1601. else if (window == windows)
  1602. {
  1603. /* If we are in the first window, find the last window in the
  1604. chain. */
  1605. while (window->next)
  1606. window = window->next;
  1607. }
  1608. }
  1609. if (active_window != window)
  1610. {
  1611. if (auto_footnotes_p)
  1612. info_get_or_remove_footnotes (window);
  1613. /* Point may not be within part of node displayed in window if there
  1614. has been resizing of this window since the last time it was active. */
  1615. window_adjust_pagetop (window);
  1616. window->flags |= W_UpdateWindow;
  1617. active_window = window;
  1618. }
  1619. }
  1620. /* Split active window into two windows, both showing the same node. If we
  1621. are automatically tiling windows, re-tile after the split. */
  1622. DECLARE_INFO_COMMAND (info_split_window, _("Split the current window"))
  1623. {
  1624. WINDOW *split = window_make_window ();
  1625. if (!split)
  1626. info_error ("%s", msg_win_too_small);
  1627. else
  1628. {
  1629. NODE *copy = xmalloc (sizeof (NODE));
  1630. *copy = *window->node; /* Field-by-field copy of structure. */
  1631. if (copy->flags & N_IsInternal)
  1632. {
  1633. /* This allows us to free nodes without checking if these fields
  1634. are shared by NODE objects in other windows. For non-internal
  1635. nodes, this data is stored in the tag table. */
  1636. copy->references = info_copy_references (copy->references);
  1637. copy->nodename = xstrdup (copy->nodename);
  1638. if (copy->up)
  1639. copy->up = xstrdup (copy->up);
  1640. if (copy->next)
  1641. copy->next = xstrdup (copy->next);
  1642. if (copy->prev)
  1643. copy->prev = xstrdup (copy->prev);
  1644. copy->contents = xstrdup (copy->contents);
  1645. }
  1646. info_set_node_of_window (split, copy);
  1647. /* Make sure point still appears in the active window. */
  1648. info_show_point (window);
  1649. split->pagetop = window->pagetop;
  1650. if (auto_tiling_p)
  1651. window_tile_windows (DONT_TILE_INTERNALS);
  1652. else
  1653. window_adjust_pagetop (split);
  1654. }
  1655. }
  1656. /* Delete WINDOW, forgetting the list of last visited nodes. If we are
  1657. automatically displaying footnotes, show or remove the footnotes
  1658. window. If we are automatically tiling windows, re-tile after the
  1659. deletion. */
  1660. DECLARE_INFO_COMMAND (info_delete_window, _("Delete the current window"))
  1661. {
  1662. if (!windows->next)
  1663. info_error ("%s", msg_cant_kill_last);
  1664. else if (window->flags & W_WindowIsPerm)
  1665. info_error ("%s", _("Cannot delete a permanent window"));
  1666. else
  1667. {
  1668. info_delete_window_internal (window);
  1669. if (auto_footnotes_p)
  1670. info_get_or_remove_footnotes (active_window);
  1671. if (auto_tiling_p)
  1672. window_tile_windows (DONT_TILE_INTERNALS);
  1673. gc_file_buffers_and_nodes ();
  1674. }
  1675. }
  1676. /* Do the physical deletion of WINDOW, and forget this window and
  1677. associated nodes. */
  1678. void
  1679. info_delete_window_internal (WINDOW *window)
  1680. {
  1681. if (windows->next && ((window->flags & W_WindowIsPerm) == 0))
  1682. {
  1683. forget_window_and_nodes (window);
  1684. window_delete_window (window);
  1685. if (echo_area_is_active)
  1686. echo_area_inform_of_deleted_window (window);
  1687. }
  1688. }
  1689. /* Just keep WINDOW, deleting all others. */
  1690. DECLARE_INFO_COMMAND (info_keep_one_window, _("Delete all other windows"))
  1691. {
  1692. int num_deleted; /* The number of windows we deleted. */
  1693. int pagetop, start, end;
  1694. /* Remember a few things about this window. We may be able to speed up
  1695. redisplay later by scrolling its contents. */
  1696. pagetop = window->pagetop;
  1697. start = window->first_row;
  1698. end = start + window->height;
  1699. num_deleted = 0;
  1700. while (1)
  1701. {
  1702. WINDOW *win;
  1703. /* Find an eligible window and delete it. If no eligible windows
  1704. are found, we are done. A window is eligible for deletion if
  1705. is it not permanent, and it is not WINDOW. */
  1706. for (win = windows; win; win = win->next)
  1707. if (win != window && ((win->flags & W_WindowIsPerm) == 0))
  1708. break;
  1709. if (!win)
  1710. break;
  1711. info_delete_window_internal (win);
  1712. num_deleted++;
  1713. }
  1714. /* Scroll the contents of this window into the right place so that the
  1715. user doesn't have to wait any longer than necessary for redisplay. */
  1716. if (num_deleted)
  1717. {
  1718. int amount;
  1719. amount = (window->first_row - start);
  1720. amount -= (window->pagetop - pagetop);
  1721. display_scroll_display (start, end, amount);
  1722. }
  1723. window->flags |= W_UpdateWindow;
  1724. gc_file_buffers_and_nodes ();
  1725. }
  1726. /* Change the size of WINDOW by AMOUNT. */
  1727. DECLARE_INFO_COMMAND (info_grow_window, _("Grow (or shrink) this window"))
  1728. {
  1729. window_change_window_height (window, count);
  1730. }
  1731. /* When non-zero, tiling takes place automatically when info_split_window
  1732. is called. */
  1733. int auto_tiling_p = 0;
  1734. /* Tile all of the visible windows. */
  1735. DECLARE_INFO_COMMAND (info_tile_windows,
  1736. _("Divide the available screen space among the visible windows"))
  1737. {
  1738. window_tile_windows (TILE_INTERNALS);
  1739. }
  1740. /* Toggle the state of this window's wrapping of lines. */
  1741. DECLARE_INFO_COMMAND (info_toggle_wrap,
  1742. _("Toggle the state of line wrapping in the current window"))
  1743. {
  1744. window_toggle_wrap (window);
  1745. }
  1746. /* **************************************************************** */
  1747. /* */
  1748. /* Cross-references and menus */
  1749. /* */
  1750. /* **************************************************************** */
  1751. /* Using WINDOW for various defaults, select the node referenced by ENTRY
  1752. in it. If the node is selected, the window and node are remembered.
  1753. Display an error message if reference couldn't be selected and return 0. */
  1754. int
  1755. info_select_reference (WINDOW *window, REFERENCE *entry)
  1756. {
  1757. NODE *node;
  1758. char *file_system_error = NULL;
  1759. /* We need to copy everything from entry because the call to
  1760. info_get_node_with_defaults can free it if it came from
  1761. the tag table of a file. */
  1762. char *filename = entry->filename;
  1763. char *nodename = entry->nodename;
  1764. char *label = entry->label;
  1765. int line_number = entry->line_number;
  1766. node = info_get_node_with_defaults (filename, nodename, window->node);
  1767. /* Try something a little weird. If the node couldn't be found, and the
  1768. reference was of the form "foo::", see if the entry->label can be found
  1769. as a file, with a node of "Top". */
  1770. if (!node)
  1771. {
  1772. if (info_recent_file_error)
  1773. file_system_error = xstrdup (info_recent_file_error);
  1774. if (nodename && label && !strcmp (nodename, label))
  1775. {
  1776. free (file_system_error);
  1777. file_system_error = NULL;
  1778. node = info_get_node (label, "Top");
  1779. if (!node && info_recent_file_error)
  1780. file_system_error = xstrdup (info_recent_file_error);
  1781. }
  1782. }
  1783. if (!node)
  1784. {
  1785. if (file_system_error)
  1786. {
  1787. info_error ("%s", file_system_error);
  1788. free (file_system_error);
  1789. }
  1790. else
  1791. info_error (msg_cant_find_node, nodename ? nodename : "Top");
  1792. return 0;
  1793. }
  1794. /* If in a footnotes window, try to switch to a window containing a
  1795. node from the file. */
  1796. if (window->node && (window->node->flags & N_IsInternal)
  1797. && !strcmp (window->node->nodename, "*Footnotes*"))
  1798. {
  1799. WINDOW *w;
  1800. for (w = windows; w; w = windows->next)
  1801. {
  1802. if (!strcmp (w->node->fullpath, window->node->fullpath)
  1803. && !(w->flags & W_TempWindow))
  1804. {
  1805. /* Switch to this window. */
  1806. active_window = window = w;
  1807. break;
  1808. }
  1809. }
  1810. }
  1811. info_set_node_of_window (window, node);
  1812. if (line_number > 0)
  1813. {
  1814. /* Go to the line given by entry->line_number. */
  1815. long line = window_log_to_phys_line (window, line_number - 1);
  1816. if (line >= 0 && line < window->line_count)
  1817. {
  1818. window->point = window->line_starts[line];
  1819. window_adjust_pagetop (window);
  1820. }
  1821. }
  1822. return 1;
  1823. }
  1824. /* Parse the node specification in LINE using WINDOW to default the filename.
  1825. Select the parsed node in WINDOW and remember it, or error if the node
  1826. couldn't be found. */
  1827. static void
  1828. info_parse_and_select (char *line, WINDOW *window)
  1829. {
  1830. REFERENCE entry;
  1831. info_parse_node (line);
  1832. entry.filename = info_parsed_filename;
  1833. entry.nodename = info_parsed_nodename;
  1834. entry.line_number = 0;
  1835. entry.label = "*info-parse-and-select*";
  1836. info_select_reference (window, &entry);
  1837. }
  1838. /* Return menu entry indexed by KEY, where '1' is the first menu item, '2' is
  1839. the second, etc., and '0' is the last. Return value should not be freed. */
  1840. static REFERENCE *
  1841. select_menu_digit (WINDOW *window, unsigned char key)
  1842. {
  1843. register int i, item;
  1844. register REFERENCE **menu;
  1845. menu = window->node->references;
  1846. if (!menu)
  1847. return 0;
  1848. item = key - '0';
  1849. /* Special case. Item "0" is the last item in this menu. */
  1850. if (item == 0)
  1851. {
  1852. int j;
  1853. i = -1; /* Not found */
  1854. for (j = 0; menu[j]; j++)
  1855. if (menu[j]->type == REFERENCE_MENU_ITEM)
  1856. i = j;
  1857. }
  1858. else
  1859. {
  1860. int k = 0;
  1861. for (i = 0; menu[i]; i++)
  1862. {
  1863. if (menu[i]->type == REFERENCE_MENU_ITEM)
  1864. k++;
  1865. if (k == item)
  1866. break;
  1867. }
  1868. }
  1869. if (i == -1)
  1870. return 0;
  1871. return menu[i];
  1872. }
  1873. DECLARE_INFO_COMMAND (info_menu_digit, _("Select this menu item"))
  1874. {} /* Declaration only. */
  1875. /* Use KEY (a digit) to select the Nth menu item in WINDOW->node. */
  1876. void
  1877. menu_digit (WINDOW *window, int key)
  1878. {
  1879. int item = key - '0';
  1880. REFERENCE *entry;
  1881. REFERENCE **references = window->node->references;
  1882. /* Check if there is a menu in this node. */
  1883. if (references)
  1884. {
  1885. int i;
  1886. for (i = 0; references[i]; i++)
  1887. if (references[i]->type == REFERENCE_MENU_ITEM)
  1888. goto has_menu;
  1889. }
  1890. info_error ("%s", msg_no_menu_node);
  1891. return;
  1892. has_menu:
  1893. if (entry = select_menu_digit (window, key))
  1894. info_select_reference (window, entry);
  1895. else if (key == '0')
  1896. /* Don't print "There aren't 0 items in this menu." */
  1897. info_error ("%s", msg_no_menu_node);
  1898. else
  1899. info_error (ngettext ("There isn't %d item in this menu.",
  1900. "There aren't %d items in this menu.",
  1901. item),
  1902. item);
  1903. return;
  1904. }
  1905. /* Select the last menu item in WINDOW->node. */
  1906. DECLARE_INFO_COMMAND (info_last_menu_item,
  1907. _("Select the last item in this node's menu"))
  1908. {
  1909. menu_digit (window, '0');
  1910. }
  1911. static int exclude_cross_references (REFERENCE *r)
  1912. {
  1913. return r->type == REFERENCE_XREF;
  1914. }
  1915. static int exclude_menu_items (REFERENCE *r)
  1916. {
  1917. return r->type == REFERENCE_MENU_ITEM;
  1918. }
  1919. static int exclude_nothing (REFERENCE *r)
  1920. {
  1921. return 1;
  1922. }
  1923. /* Read a menu or followed reference from the user defaulting to the
  1924. reference found on the current line, and select that node. The
  1925. reading is done with completion. ASK_P is non-zero if the user should
  1926. be prompted, or zero to select the item on the current line. MENU_ITEM
  1927. and XREF control whether menu items and cross-references are eligible
  1928. for selection. */
  1929. static void
  1930. info_menu_or_ref_item (WINDOW *window, int menu_item, int xref, int ask_p)
  1931. {
  1932. REFERENCE *defentry = NULL; /* Default link */
  1933. REFERENCE **refs = window->node->references;
  1934. REFERENCE *entry;
  1935. /* Name of destination */
  1936. char *line;
  1937. int line_no;
  1938. int this_line, next_line;
  1939. int which, closest = -1;
  1940. reference_bool_fn exclude;
  1941. if (!refs)
  1942. return;
  1943. if (menu_item && !xref)
  1944. {
  1945. exclude = &exclude_cross_references;
  1946. }
  1947. else if (!menu_item && xref)
  1948. {
  1949. exclude = &exclude_menu_items;
  1950. }
  1951. else if (menu_item && xref)
  1952. {
  1953. exclude = &exclude_nothing;
  1954. }
  1955. else /* !menu_item && !xref */
  1956. return;
  1957. line_no = window_line_of_point (window);
  1958. this_line = window->line_starts[line_no];
  1959. if (window->line_starts[line_no + 1])
  1960. next_line = window->line_starts[line_no + 1];
  1961. else
  1962. next_line = window->node->nodelen;
  1963. /* Look for a reference in the current line, preferring one that
  1964. the point is in, otherwise preferring after the point. */
  1965. for (which = 0; refs[which]; which++)
  1966. {
  1967. /* If we got to the next line without finding an eligible reference. */
  1968. if (refs[which]->start >= next_line)
  1969. break;
  1970. /* Check the type of reference is one we are looking for. */
  1971. if (!( (menu_item && refs[which]->type == REFERENCE_MENU_ITEM)
  1972. || (xref && refs[which]->type == REFERENCE_XREF)))
  1973. continue;
  1974. /* Reference is eligible if any part of it is in the line. */
  1975. if (refs[which]->start >= this_line && refs[which]->start < next_line
  1976. || refs[which]->start < this_line && refs[which]->end > this_line)
  1977. {
  1978. closest = which;
  1979. /* If a reference contains the point, prefer it. Otherwise prefer a
  1980. reference after the cursor to one before it. */
  1981. if (window->point >= refs[which]->start
  1982. && window->point < refs[which]->end)
  1983. break;
  1984. }
  1985. }
  1986. if (closest != -1)
  1987. defentry = refs[closest];
  1988. if (ask_p)
  1989. {
  1990. char *prompt;
  1991. /* Build the prompt string. */
  1992. if (menu_item && !xref)
  1993. {
  1994. if (defentry)
  1995. {
  1996. prompt = xmalloc (strlen (defentry->label)
  1997. + strlen (_("Menu item (%s): ")));
  1998. sprintf (prompt, _("Menu item (%s): "), defentry->label);
  1999. }
  2000. else
  2001. prompt = xstrdup (_("Menu item: "));
  2002. }
  2003. else
  2004. {
  2005. if (defentry)
  2006. {
  2007. prompt = xmalloc (strlen (defentry->label)
  2008. + strlen (_("Follow xref (%s): ")));
  2009. sprintf (prompt, _("Follow xref (%s): "), defentry->label);
  2010. }
  2011. else
  2012. prompt = xstrdup (_("Follow xref: "));
  2013. }
  2014. line = info_read_completing_in_echo_area_with_exclusions (prompt, refs,
  2015. exclude);
  2016. free (prompt);
  2017. window = active_window;
  2018. /* User aborts, just quit. */
  2019. if (!line)
  2020. {
  2021. info_abort_key (window, 0);
  2022. return;
  2023. }
  2024. /* If we had a default and the user accepted it, use that. */
  2025. if (!*line)
  2026. {
  2027. free (line);
  2028. if (defentry)
  2029. line = xstrdup (defentry->label);
  2030. else
  2031. line = NULL;
  2032. }
  2033. }
  2034. else
  2035. {
  2036. /* Not going to ask any questions. If we have a default entry, use
  2037. that, otherwise return. */
  2038. if (!defentry)
  2039. return;
  2040. else
  2041. line = xstrdup (defentry->label);
  2042. }
  2043. if (line)
  2044. {
  2045. /* It is possible that the references have more than a single
  2046. entry with the same label, and also LINE is down-cased, which
  2047. complicates matters even more. Try to be as accurate as we
  2048. can: if they've chosen the default, use defentry directly. */
  2049. if (defentry && strcmp (line, defentry->label) == 0)
  2050. entry = defentry;
  2051. else
  2052. /* Find the selected label in the references. If there are
  2053. more than one label which matches, find the one that's
  2054. closest to point. */
  2055. {
  2056. register int i;
  2057. int best = -1, min_dist = window->node->nodelen;
  2058. REFERENCE *ref;
  2059. for (i = 0; refs && (ref = refs[i]); i++)
  2060. {
  2061. /* Need to use mbscasecmp because LINE is downcased
  2062. inside info_read_completing_in_echo_area. */
  2063. if (mbscasecmp (line, ref->label) == 0)
  2064. {
  2065. /* ref->end is more accurate estimate of position
  2066. for menus than ref->start. Go figure. */
  2067. int dist = abs (window->point - ref->end);
  2068. if (dist < min_dist)
  2069. {
  2070. min_dist = dist;
  2071. best = i;
  2072. }
  2073. }
  2074. }
  2075. if (best != -1)
  2076. entry = refs[best];
  2077. else
  2078. entry = NULL;
  2079. }
  2080. if (!entry && defentry)
  2081. info_error (_("The reference disappeared! (%s)."), line);
  2082. else
  2083. {
  2084. info_select_reference (window, entry);
  2085. }
  2086. free (line);
  2087. }
  2088. }
  2089. /* Read a line (with completion) which is the name of a menu item,
  2090. and select that item. */
  2091. DECLARE_INFO_COMMAND (info_menu_item, _("Read a menu item and select its node"))
  2092. {
  2093. if (window->node->references)
  2094. {
  2095. REFERENCE **r;
  2096. /* Check if there is a menu in this node. */
  2097. for (r = window->node->references; *r; r++)
  2098. if ((*r)->type == REFERENCE_MENU_ITEM)
  2099. break;
  2100. if (*r)
  2101. {
  2102. info_menu_or_ref_item (window, 1, 0, 1);
  2103. return;
  2104. }
  2105. }
  2106. info_error ("%s", msg_no_menu_node);
  2107. return;
  2108. }
  2109. /* Read a line (with completion) which is the name of a reference to
  2110. follow, and select the node. */
  2111. DECLARE_INFO_COMMAND
  2112. (info_xref_item, _("Read a footnote or cross reference and select its node"))
  2113. {
  2114. info_menu_or_ref_item (window, 0, 1, 1);
  2115. }
  2116. /* Position the cursor at the start of this node's menu. */
  2117. DECLARE_INFO_COMMAND (info_find_menu, _("Move to the start of this node's menu"))
  2118. {
  2119. SEARCH_BINDING binding;
  2120. long position;
  2121. binding.buffer = window->node->contents;
  2122. binding.start = 0;
  2123. binding.end = window->node->nodelen;
  2124. binding.flags = S_FoldCase | S_SkipDest;
  2125. if (search (INFO_MENU_LABEL, &binding, &position) == search_success)
  2126. {
  2127. window->point = position;
  2128. window_adjust_pagetop (window);
  2129. window->flags |= W_UpdateWindow;
  2130. }
  2131. else
  2132. info_error ("%s", msg_no_menu_node);
  2133. }
  2134. /* Visit as many menu items as is possible, each in a separate window. */
  2135. DECLARE_INFO_COMMAND (info_visit_menu,
  2136. _("Visit as many menu items at once as possible"))
  2137. {
  2138. register int i;
  2139. REFERENCE *entry, **menu;
  2140. menu = window->node->references;
  2141. if (!menu)
  2142. {
  2143. info_error ("%s", msg_no_menu_node);
  2144. return;
  2145. }
  2146. for (i = 0; (entry = menu[i]); i++)
  2147. {
  2148. WINDOW *new;
  2149. if (entry->type != REFERENCE_MENU_ITEM) continue;
  2150. new = window_make_window ();
  2151. info_set_node_of_window (new, window->node);
  2152. window_tile_windows (TILE_INTERNALS);
  2153. if (!new)
  2154. {
  2155. info_error ("%s", msg_win_too_small);
  2156. break;
  2157. }
  2158. else
  2159. {
  2160. active_window = new;
  2161. if (!info_select_reference (new, entry))
  2162. break;
  2163. }
  2164. }
  2165. }
  2166. /* Move to the next or previous cross reference in this node. Return 0 if
  2167. there aren't any. */
  2168. static int
  2169. info_move_to_xref (WINDOW *window, int dir)
  2170. {
  2171. long placement = -1;
  2172. NODE *node = window->node;
  2173. REFERENCE **ref;
  2174. /* Fail if there are no references in node */
  2175. if (!node->references || !node->references[0])
  2176. return 0;
  2177. if (dir == 1) /* Search forwards */
  2178. for (ref = node->references; *ref != 0; ref++)
  2179. {
  2180. if ((*ref)->start > window->point)
  2181. {
  2182. placement = (*ref)->start;
  2183. break;
  2184. }
  2185. }
  2186. else /* Search backwards */
  2187. for (ref = node->references; *ref != 0; ref++)
  2188. {
  2189. if ((*ref)->start >= window->point) break;
  2190. placement = (*ref)->start;
  2191. }
  2192. if (placement == -1)
  2193. {
  2194. /* There was neither a menu or xref entry appearing in this node
  2195. after point. */
  2196. return 0;
  2197. }
  2198. window->point = placement;
  2199. window_adjust_pagetop (window);
  2200. return 1;
  2201. }
  2202. void info_move_to_next_xref (WINDOW *, int count);
  2203. /* Remove history entries from START inclusive to END exclusive.
  2204. Warning: be careful about removing the last history entry, as
  2205. info_set_node_of_window includes the currently displayed node in
  2206. the history. */
  2207. static void
  2208. cleanup_history (WINDOW *window, int start, int end)
  2209. {
  2210. int i;
  2211. for (i = start; i < end; i++)
  2212. {
  2213. free_history_node (window->hist[i]->node);
  2214. free (window->hist[i]);
  2215. }
  2216. memmove (&window->hist[start], &window->hist[end],
  2217. (window->hist_index - end) * sizeof (WINDOW_STATE *));
  2218. window->hist_index -= end - start;
  2219. window->hist[window->hist_index] = 0;
  2220. }
  2221. DECLARE_INFO_COMMAND (info_move_to_prev_xref,
  2222. _("Move to the previous cross reference"))
  2223. {
  2224. if (count < 0)
  2225. info_move_to_next_xref (window, -count);
  2226. else
  2227. {
  2228. size_t last_hist_index, starting_hist_index;
  2229. char *initial_nodename = window->node->nodename;
  2230. last_hist_index = starting_hist_index = window->hist_index - 1;
  2231. while (count > 0)
  2232. {
  2233. if (info_move_to_xref (window, -1))
  2234. {
  2235. last_hist_index = window->hist_index - 1;
  2236. count--;
  2237. continue;
  2238. }
  2239. /* When cursor-movement-scrolls=Off, cycle round the node's
  2240. references. */
  2241. if (!cursor_movement_scrolls_p)
  2242. {
  2243. REFERENCE **r = window->node->references;
  2244. if (r && r[0])
  2245. {
  2246. int i = 0;
  2247. /* Choose the last menu or xref entry appearing in this
  2248. node. */
  2249. while (r[i + 1])
  2250. i++;
  2251. window->point = r[i]->start;
  2252. window_adjust_pagetop (window);
  2253. count--;
  2254. continue;
  2255. }
  2256. info_error ("%s", msg_no_xref_node);
  2257. return;
  2258. }
  2259. if (backward_move_node_structure (window, info_scroll_behaviour != 0)
  2260. || !strcmp (window->node->nodename, initial_nodename))
  2261. {
  2262. break; /* No earlier nodes in file, or we are back where we
  2263. started. */
  2264. }
  2265. window->point = window->node->nodelen - 1;
  2266. }
  2267. /* Go back to the last place a reference was found, or
  2268. the starting place. */
  2269. while (window->hist_index > last_hist_index + 1)
  2270. forget_node (window);
  2271. /* Remove any intermediate nodes. */
  2272. if (last_hist_index != starting_hist_index)
  2273. cleanup_history (window, starting_hist_index + 1, last_hist_index);
  2274. }
  2275. }
  2276. DECLARE_INFO_COMMAND (info_move_to_next_xref,
  2277. _("Move to the next cross reference"))
  2278. {
  2279. if (count < 0)
  2280. info_move_to_prev_xref (window, -count);
  2281. else
  2282. {
  2283. size_t last_hist_index, starting_hist_index;
  2284. char *initial_nodename = window->node->nodename;
  2285. last_hist_index = starting_hist_index = window->hist_index - 1;
  2286. while (count > 0)
  2287. {
  2288. if (info_move_to_xref (window, 1))
  2289. {
  2290. last_hist_index = window->hist_index - 1;
  2291. count--;
  2292. continue;
  2293. }
  2294. /* When cursor-movement-scrolls=Off, cycle round the node's
  2295. references. */
  2296. if (!cursor_movement_scrolls_p)
  2297. {
  2298. REFERENCE **r = window->node->references;
  2299. if (r && r[0])
  2300. {
  2301. /* Choose the first menu or xref entry appearing in this
  2302. node. */
  2303. window->point = r[0]->start;
  2304. window_adjust_pagetop (window);
  2305. count--;
  2306. continue;
  2307. }
  2308. info_error ("%s", msg_no_xref_node);
  2309. return;
  2310. }
  2311. if (forward_move_node_structure (window, info_scroll_behaviour) != 0
  2312. || !strcmp (window->node->nodename, initial_nodename))
  2313. {
  2314. /*TODO: Print an error. */
  2315. break; /* No later nodes in file, or we are back where we
  2316. started. */
  2317. }
  2318. }
  2319. /* Go back to the last place a reference was found, or
  2320. the starting place. */
  2321. while (window->hist_index > last_hist_index + 1)
  2322. forget_node (window);
  2323. /* Remove any intermediate nodes. */
  2324. if (last_hist_index != starting_hist_index)
  2325. cleanup_history (window, starting_hist_index + 1, last_hist_index);
  2326. }
  2327. }
  2328. /* Select the menu item or reference that appears on this line. */
  2329. DECLARE_INFO_COMMAND (info_select_reference_this_line,
  2330. _("Select reference or menu item appearing on this line"))
  2331. {
  2332. REFERENCE **ref = window->node->references;
  2333. if (!ref || !*ref) return; /* No references in node */
  2334. info_menu_or_ref_item (window, 1, 1, 0);
  2335. }
  2336. /* Follow the menu list in MENUS (list of strings terminated by a NULL
  2337. entry) from INITIAL_NODE. If there is an error, place a message
  2338. in ERROR. STRICT says whether to accept incomplete strings as
  2339. menu entries, and whether to return the node so far if we can't
  2340. continue at any point (that might be INITIAL_NODE itself), or to
  2341. return null. This function frees INITIAL_NODE. */
  2342. NODE *
  2343. info_follow_menus (NODE *initial_node, char **menus, char **error,
  2344. int strict)
  2345. {
  2346. NODE *node = NULL;
  2347. for (; *menus; menus++)
  2348. {
  2349. REFERENCE *entry;
  2350. char *arg = *menus; /* Remember the name of the menu entry we want. */
  2351. debug (3, ("looking for %s in %s:%s", arg, initial_node->fullpath,
  2352. initial_node->nodename));
  2353. if (!initial_node->references)
  2354. {
  2355. if (error)
  2356. {
  2357. free (*error);
  2358. asprintf (error, _("No menu in node '%s'."),
  2359. node_printed_rep (initial_node));
  2360. }
  2361. debug (3, ("no menu found"));
  2362. if (!strict)
  2363. return initial_node;
  2364. else
  2365. {
  2366. free_history_node (initial_node);
  2367. return 0;
  2368. }
  2369. }
  2370. /* Find the specified menu item. */
  2371. entry = info_get_menu_entry_by_label (initial_node, arg, !strict);
  2372. /* If we failed to find the reference: */
  2373. if (!entry)
  2374. {
  2375. if (error)
  2376. {
  2377. free (*error);
  2378. asprintf (error, _("No menu item '%s' in node '%s'."),
  2379. arg, node_printed_rep (initial_node));
  2380. }
  2381. debug (3, ("no entry found"));
  2382. if (!strict)
  2383. return initial_node;
  2384. else
  2385. {
  2386. free_history_node (initial_node);
  2387. return 0;
  2388. }
  2389. }
  2390. debug (3, ("entry: %s, %s", entry->filename, entry->nodename));
  2391. /* Try to find this node. */
  2392. node = info_get_node_with_defaults (entry->filename, entry->nodename,
  2393. initial_node);
  2394. if (!node)
  2395. {
  2396. debug (3, ("no matching node found"));
  2397. if (error)
  2398. {
  2399. free (*error);
  2400. asprintf (error,
  2401. _("Unable to find node referenced by '%s' in '%s'."),
  2402. entry->label,
  2403. node_printed_rep (initial_node));
  2404. }
  2405. if (strict)
  2406. {
  2407. free_history_node (initial_node);
  2408. return 0;
  2409. }
  2410. else
  2411. return initial_node;
  2412. }
  2413. debug (3, ("node: %s, %s", node->fullpath, node->nodename));
  2414. /* Success. Go round the loop again. */
  2415. free_history_node (initial_node);
  2416. initial_node = node;
  2417. }
  2418. return initial_node;
  2419. }
  2420. /* Split STR into individual node names by writing null bytes in wherever
  2421. there are commas and constructing a list of the resulting pointers.
  2422. (We can do this since STR has had canonicalize_whitespace called on it.)
  2423. Return array terminated with NULL. */
  2424. static char **
  2425. split_list_of_nodenames (char *str)
  2426. {
  2427. unsigned len = 2;
  2428. char **nodes = xmalloc (len * sizeof (char *));
  2429. nodes[len - 2] = str;
  2430. while (*str++)
  2431. {
  2432. if (*str == ',')
  2433. {
  2434. *str++ = 0; /* get past the null byte */
  2435. len++;
  2436. nodes = xrealloc (nodes, len * sizeof (char *));
  2437. nodes[len - 2] = str;
  2438. }
  2439. }
  2440. nodes[len - 1] = NULL;
  2441. return nodes;
  2442. }
  2443. /* Read a line of input which is a sequence of menus (starting from
  2444. dir), and follow them. */
  2445. DECLARE_INFO_COMMAND (info_menu_sequence,
  2446. _("Read a list of menus starting from dir and follow them"))
  2447. {
  2448. char *line = info_read_in_echo_area (_("Follow menus: "));
  2449. /* If the user aborted, quit now. */
  2450. if (!line)
  2451. {
  2452. info_abort_key (window, 0);
  2453. return;
  2454. }
  2455. canonicalize_whitespace (line);
  2456. if (*line)
  2457. {
  2458. char *error = 0;
  2459. NODE *dir_node = get_dir_node ();
  2460. char **nodes = split_list_of_nodenames (line);
  2461. NODE *node;
  2462. /* If DIR_NODE is NULL, they might be reading a file directly,
  2463. like in "info -d . -f ./foo". Try using "Top" instead. */
  2464. if (!dir_node)
  2465. dir_node = info_get_node (window->node->fullpath, 0);
  2466. /* If we still cannot find the starting point, give up. */
  2467. if (!dir_node)
  2468. info_error (msg_cant_find_node, "Top");
  2469. else
  2470. {
  2471. node = info_follow_menus (dir_node, nodes, &error, 0);
  2472. info_set_node_of_window (window, node);
  2473. if (error)
  2474. show_error_node (error);
  2475. }
  2476. free (nodes);
  2477. }
  2478. free (line);
  2479. }
  2480. /* **************************************************************** */
  2481. /* */
  2482. /* Navigation of document structure */
  2483. /* */
  2484. /* **************************************************************** */
  2485. /* Get the node pointed to by the LABEL pointer of WINDOW->node into WINDOW.
  2486. Display error message if there is no such pointer, and return zero. */
  2487. static int
  2488. info_handle_pointer (char *label, WINDOW *window)
  2489. {
  2490. char *description;
  2491. NODE *node;
  2492. if (!strcmp (label, "Up"))
  2493. description = window->node->up;
  2494. else if (!strcmp (label, "Next"))
  2495. description = window->node->next;
  2496. else if (!strcmp (label, "Prev"))
  2497. description = window->node->prev;
  2498. if (!description)
  2499. {
  2500. info_error (msg_no_pointer, label);
  2501. return 0;
  2502. }
  2503. info_parse_node (description);
  2504. node = info_get_node_with_defaults (info_parsed_filename,
  2505. info_parsed_nodename,
  2506. window->node);
  2507. if (!node)
  2508. {
  2509. if (info_recent_file_error)
  2510. info_error ("%s", info_recent_file_error);
  2511. else
  2512. info_error (msg_cant_find_node, description);
  2513. return 0;
  2514. }
  2515. /* If we are going up, set the cursor position to the last place it
  2516. was in the node. */
  2517. if (strcmp (label, "Up") == 0)
  2518. {
  2519. int i;
  2520. for (i = window->hist_index - 1; i >= 0; i--)
  2521. {
  2522. NODE *p = window->hist[i]->node;
  2523. if (p->fullpath && !strcmp (p->fullpath, node->fullpath)
  2524. && p->nodename && !strcmp (p->nodename, node->nodename))
  2525. break;
  2526. }
  2527. if (i >= 0)
  2528. node->display_pos = window->hist[i]->point;
  2529. }
  2530. info_set_node_of_window (window, node);
  2531. return 1;
  2532. }
  2533. /* Make WINDOW display the "Next:" node of the node currently being
  2534. displayed. */
  2535. DECLARE_INFO_COMMAND (info_next_node, _("Select the Next node"))
  2536. {
  2537. info_handle_pointer ("Next", window);
  2538. }
  2539. /* Make WINDOW display the "Prev:" node of the node currently being
  2540. displayed. */
  2541. DECLARE_INFO_COMMAND (info_prev_node, _("Select the Prev node"))
  2542. {
  2543. info_handle_pointer ("Prev", window);
  2544. }
  2545. /* Make WINDOW display the "Up:" node of the node currently being
  2546. displayed. */
  2547. DECLARE_INFO_COMMAND (info_up_node, _("Select the Up node"))
  2548. {
  2549. info_handle_pointer ("Up", window);
  2550. }
  2551. /* Make WINDOW display the last node of this info file. */
  2552. DECLARE_INFO_COMMAND (info_last_node, _("Select the last node in this file"))
  2553. {
  2554. register int i;
  2555. FILE_BUFFER *fb = file_buffer_of_window (window);
  2556. NODE *node = NULL;
  2557. if (fb && fb->tags)
  2558. {
  2559. int last_node_tag_idx = -1;
  2560. /* If no explicit argument, or argument of zero, default to the
  2561. last node. */
  2562. if (count == 0 || (count == 1 && !info_explicit_arg))
  2563. count = -1;
  2564. for (i = 0; count && fb->tags[i]; i++)
  2565. if (fb->tags[i]->cache.nodelen != 0) /* don't count anchor tags */
  2566. {
  2567. count--;
  2568. last_node_tag_idx = i;
  2569. }
  2570. if (count > 0)
  2571. i = last_node_tag_idx + 1;
  2572. if (i > 0)
  2573. node = info_get_node (fb->filename, fb->tags[i - 1]->nodename);
  2574. }
  2575. if (!node)
  2576. info_error ("%s", _("This window has no additional nodes"));
  2577. else
  2578. info_set_node_of_window (window, node);
  2579. }
  2580. /* Make WINDOW display the first node of this info file. */
  2581. DECLARE_INFO_COMMAND (info_first_node, _("Select the first node in this file"))
  2582. {
  2583. FILE_BUFFER *fb = file_buffer_of_window (window);
  2584. NODE *node = NULL;
  2585. /* If no explicit argument, or argument of zero, default to the
  2586. first node. */
  2587. if (count == 0)
  2588. count = 1;
  2589. if (fb && fb->tags)
  2590. {
  2591. register int i;
  2592. int last_node_tag_idx = -1;
  2593. for (i = 0; count && fb->tags[i]; i++)
  2594. if (fb->tags[i]->cache.nodelen != 0) /* don't count anchor tags */
  2595. {
  2596. count--;
  2597. last_node_tag_idx = i;
  2598. }
  2599. if (count > 0)
  2600. i = last_node_tag_idx + 1;
  2601. if (i > 0)
  2602. node = info_get_node (fb->filename, fb->tags[i - 1]->nodename);
  2603. }
  2604. if (!node)
  2605. info_error ("%s", _("This window has no additional nodes"));
  2606. else
  2607. info_set_node_of_window (window, node);
  2608. }
  2609. /* Controls what to do when a scrolling command is issued at the end of the
  2610. last node. */
  2611. int scroll_last_node = SLN_Stop;
  2612. /* Move to 1st menu item, Next, Up/Next, or error in this window. Return
  2613. non-zero on error, 0 on success. Display an error message if there is an
  2614. error. */
  2615. static int
  2616. forward_move_node_structure (WINDOW *window, int behaviour)
  2617. {
  2618. if (window->node->flags & (N_IsInternal | N_IsManPage))
  2619. return 1;
  2620. switch (behaviour)
  2621. {
  2622. case IS_PageOnly:
  2623. info_error ("%s", msg_at_node_bottom);
  2624. return 1;
  2625. case IS_NextOnly:
  2626. return !info_handle_pointer ("Next", window);
  2627. break;
  2628. case IS_Continuous:
  2629. {
  2630. /* If this node contains a menu, select its first entry. Indices
  2631. are an exception, as their menus lead nowhere meaningful. Likewise
  2632. for dir nodes. */
  2633. if (!(window->node->flags & N_IsIndex)
  2634. && !(window->node->flags & N_IsDir))
  2635. {
  2636. REFERENCE *entry;
  2637. if (entry = select_menu_digit (window, '1'))
  2638. {
  2639. info_select_reference (window, entry);
  2640. return 0;
  2641. }
  2642. }
  2643. /* Okay, this node does not contain a menu. If it contains a
  2644. "Next:" pointer, use that. */
  2645. if (window->node->next)
  2646. {
  2647. info_handle_pointer ("Next", window);
  2648. return 0;
  2649. }
  2650. /* Okay, there wasn't a "Next:" for this node. Move "Up:" until we
  2651. can move "Next:". If that isn't possible, complain that there
  2652. are no more nodes. */
  2653. {
  2654. int up_counter;
  2655. int starting_hist_index = window->hist_index;
  2656. /* Back up through the "Up:" pointers until we have found a "Next:"
  2657. that isn't the same as the first menu item found in that node. */
  2658. up_counter = 0;
  2659. while (1)
  2660. {
  2661. if (window->node->up)
  2662. {
  2663. REFERENCE *entry;
  2664. if (!info_handle_pointer ("Up", window))
  2665. return 1;
  2666. up_counter++;
  2667. /* If no "Next" pointer, keep backing up. */
  2668. if (!window->node->next)
  2669. continue;
  2670. /* If this node's first menu item is the same as this node's
  2671. Next pointer, keep backing up. */
  2672. entry = select_menu_digit (window, '1');
  2673. if (entry && !strcmp (window->node->next, entry->nodename))
  2674. continue;
  2675. /* This node has a "Next" pointer, and it is not the
  2676. same as the first menu item found in this node. */
  2677. info_handle_pointer ("Next", window);
  2678. /* Don't include intermediate nodes in the window's
  2679. history. */
  2680. cleanup_history (window, starting_hist_index,
  2681. window->hist_index - 1);
  2682. return 0;
  2683. }
  2684. else
  2685. {
  2686. /* No more "Up" pointers. We are at the last node in the
  2687. file. */
  2688. register int i;
  2689. for (i = 0; i < up_counter; i++)
  2690. forget_node (window);
  2691. switch (scroll_last_node)
  2692. {
  2693. case SLN_Stop:
  2694. info_error ("%s",
  2695. _("No more nodes within this document."));
  2696. return 1;
  2697. case SLN_Top:
  2698. info_parse_and_select ("Top", window);
  2699. return 0;
  2700. default:
  2701. abort ();
  2702. }
  2703. }
  2704. }
  2705. }
  2706. break;
  2707. }
  2708. }
  2709. return 0;
  2710. }
  2711. /* Move to earlier node in node hierarchy in WINDOW depending on BEHAVIOUR.
  2712. Display an error message if node wasn't changed. */
  2713. static int
  2714. backward_move_node_structure (WINDOW *window, int behaviour)
  2715. {
  2716. if (window->node->flags & (N_IsInternal | N_IsManPage))
  2717. return 1;
  2718. switch (behaviour)
  2719. {
  2720. case IS_PageOnly:
  2721. info_error ("%s", msg_at_node_top);
  2722. return 1;
  2723. case IS_NextOnly:
  2724. return !info_handle_pointer ("Prev", window);
  2725. break;
  2726. case IS_Continuous:
  2727. if (window->node->up)
  2728. {
  2729. /* If up is the dir node, we are at the top node.
  2730. Don't do anything. */
  2731. if (!strncasecmp (window->node->up, "(dir)", strlen ("(dir)")))
  2732. {
  2733. info_error ("%s",
  2734. _("No 'Prev' or 'Up' for this node within this document."));
  2735. return 1;
  2736. }
  2737. /* If 'Prev' and 'Up' are the same, we are at the first node
  2738. of the 'Up' node's menu. Go to up node. */
  2739. else if (window->node->prev
  2740. && !strcmp(window->node->prev, window->node->up))
  2741. {
  2742. info_handle_pointer ("Up", window);
  2743. }
  2744. /* Otherwise, go to 'Prev' node and go down the last entry
  2745. in the menus as far as possible. */
  2746. else if (window->node->prev)
  2747. {
  2748. int starting_hist_index = window->hist_index;
  2749. info_handle_pointer ("Prev", window);
  2750. if (!(window->node->flags & N_IsIndex))
  2751. {
  2752. while (1)
  2753. {
  2754. REFERENCE *entry = select_menu_digit (window, '0');
  2755. if (!entry)
  2756. break;
  2757. if (!info_select_reference (window, entry))
  2758. break;
  2759. }
  2760. /* Don't include intermediate nodes in the window's
  2761. history. */
  2762. cleanup_history (window, starting_hist_index,
  2763. window->hist_index - 1);
  2764. }
  2765. }
  2766. else /* 'Up' but no 'Prev' */
  2767. info_handle_pointer ("Up", window);
  2768. }
  2769. else if (window->node->prev) /* 'Prev' but no 'Up' */
  2770. info_handle_pointer ("Prev", window);
  2771. else
  2772. {
  2773. info_error ("%s",
  2774. _("No 'Prev' or 'Up' for this node within this document."));
  2775. return 1;
  2776. }
  2777. break; /* case IS_Continuous: */
  2778. }
  2779. return 0;
  2780. }
  2781. void info_global_prev_node (WINDOW *, int count);
  2782. /* Move continuously forward through the node structure of this info file. */
  2783. DECLARE_INFO_COMMAND (info_global_next_node,
  2784. _("Move forwards or down through node structure"))
  2785. {
  2786. if (count < 0)
  2787. info_global_prev_node (window, -count);
  2788. else
  2789. {
  2790. while (count)
  2791. {
  2792. if (forward_move_node_structure (window, IS_Continuous))
  2793. break;
  2794. count--;
  2795. }
  2796. }
  2797. }
  2798. /* Move continuously backward through the node structure of this info file. */
  2799. DECLARE_INFO_COMMAND (info_global_prev_node,
  2800. _("Move backwards or up through node structure"))
  2801. {
  2802. if (count < 0)
  2803. info_global_next_node (window, -count);
  2804. else
  2805. {
  2806. while (count)
  2807. {
  2808. if (backward_move_node_structure (window, IS_Continuous))
  2809. break;
  2810. count--;
  2811. }
  2812. }
  2813. }
  2814. /* **************************************************************** */
  2815. /* */
  2816. /* Info Node Commands */
  2817. /* */
  2818. /* **************************************************************** */
  2819. /* Read a line of input which is a node name, and go to that node. */
  2820. DECLARE_INFO_COMMAND (info_goto_node, _("Read a node name and select it"))
  2821. {
  2822. char *line;
  2823. #define GOTO_COMPLETES
  2824. #if defined (GOTO_COMPLETES)
  2825. /* Build a completion list of all of the known nodes. */
  2826. {
  2827. register int fbi, i;
  2828. FILE_BUFFER *current;
  2829. REFERENCE **items = NULL;
  2830. size_t items_index = 0;
  2831. size_t items_slots = 0;
  2832. current = file_buffer_of_window (window);
  2833. for (fbi = 0; info_loaded_files && info_loaded_files[fbi]; fbi++)
  2834. {
  2835. FILE_BUFFER *fb;
  2836. REFERENCE *entry;
  2837. int this_is_the_current_fb;
  2838. fb = info_loaded_files[fbi];
  2839. this_is_the_current_fb = (current == fb);
  2840. entry = xmalloc (sizeof (REFERENCE));
  2841. entry->filename = entry->nodename = NULL;
  2842. entry->label = xmalloc (4 + strlen (fb->filename));
  2843. sprintf (entry->label, "(%s)*", fb->filename);
  2844. add_pointer_to_array (entry, items_index, items, items_slots, 10);
  2845. if (fb->tags)
  2846. {
  2847. for (i = 0; fb->tags[i]; i++)
  2848. {
  2849. entry = xmalloc (sizeof (REFERENCE));
  2850. entry->filename = entry->nodename = NULL;
  2851. if (this_is_the_current_fb)
  2852. entry->label = xstrdup (fb->tags[i]->nodename);
  2853. else
  2854. {
  2855. entry->label = xmalloc
  2856. (4 + strlen (fb->filename) +
  2857. strlen (fb->tags[i]->nodename));
  2858. sprintf (entry->label, "(%s)%s",
  2859. fb->filename, fb->tags[i]->nodename);
  2860. }
  2861. add_pointer_to_array (entry, items_index, items,
  2862. items_slots, 100);
  2863. }
  2864. }
  2865. }
  2866. line = info_read_maybe_completing (_("Goto node: "), items);
  2867. info_free_references (items);
  2868. }
  2869. #else /* !GOTO_COMPLETES */
  2870. line = info_read_in_echo_area (_("Goto node: "));
  2871. #endif /* !GOTO_COMPLETES */
  2872. /* If the user aborted, quit now. */
  2873. if (!line)
  2874. {
  2875. info_abort_key (window, 0);
  2876. return;
  2877. }
  2878. canonicalize_whitespace (line);
  2879. if (*line)
  2880. info_parse_and_select (line, window);
  2881. free (line);
  2882. }
  2883. static NODE *
  2884. find_invocation_node_by_nodename (FILE_BUFFER *fb, char *program)
  2885. {
  2886. NODE *node = 0;
  2887. TAG **n;
  2888. char *try1, *try2;
  2889. n = fb->tags;
  2890. if (!n)
  2891. return 0;
  2892. asprintf (&try1, "Invoking %s", program);
  2893. asprintf (&try2, "%s invocation", program);
  2894. for (; *n; n++)
  2895. {
  2896. if ((*n)->nodename
  2897. && (!strcasecmp ((*n)->nodename, try1)
  2898. || !strcasecmp ((*n)->nodename, try2)))
  2899. {
  2900. node = info_get_node_of_file_buffer (fb, (*n)->nodename);
  2901. break;
  2902. }
  2903. }
  2904. free (try1); free (try2);
  2905. return node;
  2906. }
  2907. /* Find the node in the file with name FILE that is the best candidate to
  2908. list the PROGRAM's invocation info and its command-line options, by looking
  2909. for menu items and chains of menu items with characteristic names. This
  2910. function frees NODE. Return value should be freed by caller with
  2911. info_reference_free. */
  2912. REFERENCE *
  2913. info_intuit_options_node (NODE *node, char *program)
  2914. {
  2915. /* The list of node names typical for GNU manuals where the program
  2916. usage and specifically the command-line arguments are described.
  2917. This is pure heuristics. I gathered these node names by looking
  2918. at all the Info files I could put my hands on. If you are
  2919. looking for evidence to complain to the GNU project about
  2920. non-uniform style of documentation, here you have your case! */
  2921. static const char *invocation_nodes[] = {
  2922. "%s invocation",
  2923. "Invoking %s",
  2924. "Preliminaries", /* m4 has Invoking under Preliminaries! */
  2925. "Invocation",
  2926. "Command Arguments",/* Emacs */
  2927. "Invoking `%s'",
  2928. "%s options",
  2929. "Options",
  2930. "Option ", /* e.g. "Option Summary" */
  2931. "Invoking",
  2932. "All options", /* tar, paxutils */
  2933. "Arguments",
  2934. "%s cmdline", /* ar */
  2935. "%s", /* last resort */
  2936. (const char *)0
  2937. };
  2938. char *filename = node->fullpath;
  2939. if (!strcmp ("Top", node->nodename))
  2940. {
  2941. /* Look through the list of nodes (and anchors) in the file for a node to
  2942. start at. There may be an invocation node that is not listed in the
  2943. top-level menu (this is the case for the Bash 4.2 manual), or it may
  2944. be referred to with an anchor ("Invoking makeinfo" in Texinfo
  2945. manual). */
  2946. FILE_BUFFER *fb;
  2947. NODE *n;
  2948. fb = info_find_file (filename);
  2949. if (!fb)
  2950. return 0;
  2951. n = find_invocation_node_by_nodename (fb, program);
  2952. if (n)
  2953. {
  2954. free_history_node (node);
  2955. node = n;
  2956. }
  2957. }
  2958. /* We keep looking deeper and deeper in the menu structure until
  2959. there are no more menus or no menu items from the above list.
  2960. Some manuals have the invocation node sitting 3 or 4 levels deep
  2961. in the menu hierarchy... */
  2962. while (1)
  2963. {
  2964. const char **try_node;
  2965. REFERENCE *entry = NULL;
  2966. /* If no menu in this node, stop here. Perhaps this node
  2967. is the one they need. */
  2968. if (!node->references)
  2969. break;
  2970. /* Look for node names typical for usage nodes in this menu. */
  2971. for (try_node = invocation_nodes; *try_node; try_node++)
  2972. {
  2973. char *nodename;
  2974. nodename = xmalloc (strlen (program) + strlen (*try_node));
  2975. sprintf (nodename, *try_node, program);
  2976. /* The last resort "%s" is dangerous, so we restrict it
  2977. to exact matches here. */
  2978. entry = info_get_menu_entry_by_label
  2979. (node, nodename, strcmp (*try_node, "%s"));
  2980. free (nodename);
  2981. if (entry)
  2982. break;
  2983. }
  2984. if (!entry)
  2985. break;
  2986. /* Go down into menu, and repeat. */
  2987. if (!entry->filename)
  2988. entry->filename = xstrdup (filename);
  2989. {
  2990. NODE *node2;
  2991. node2 = info_get_node (entry->filename, entry->nodename);
  2992. if (!node2)
  2993. break;
  2994. free_history_node (node);
  2995. node = node2;
  2996. }
  2997. }
  2998. {
  2999. char *n = node->nodename;
  3000. node->nodename = 0;
  3001. free_history_node (node);
  3002. return info_new_reference (filename, n);
  3003. }
  3004. }
  3005. /* Given a name of an Info file, find the name of the package it describes by
  3006. removing the leading directories and extensions. Return value should be
  3007. freed by caller. */
  3008. char *
  3009. program_name_from_file_name (char *file_name)
  3010. {
  3011. int i;
  3012. char *program_name = xstrdup (filename_non_directory (file_name));
  3013. for (i = strlen (program_name) - 1; i > 0; i--)
  3014. if (program_name[i] == '.'
  3015. && (FILENAME_CMPN (program_name + i, ".info", 5) == 0
  3016. || FILENAME_CMPN (program_name + i, ".inf", 4) == 0
  3017. #ifdef __MSDOS__
  3018. || FILENAME_CMPN (program_name + i, ".i", 2) == 0
  3019. #endif
  3020. || isdigit (program_name[i + 1]))) /* a man page foo.1 */
  3021. {
  3022. program_name[i] = 0;
  3023. break;
  3024. }
  3025. return program_name;
  3026. }
  3027. DECLARE_INFO_COMMAND (info_goto_invocation_node,
  3028. _("Find the node describing program invocation"))
  3029. {
  3030. const char *invocation_prompt = _("Find Invocation node of [%s]: ");
  3031. char *program_name, *line;
  3032. char *default_program_name, *prompt, *file_name;
  3033. NODE *top_node;
  3034. REFERENCE *invocation_ref;
  3035. /* Intuit the name of the program they are likely to want.
  3036. We use the file name of the current Info file as a hint. */
  3037. file_name = window->node->fullpath;
  3038. default_program_name = program_name_from_file_name (file_name);
  3039. prompt = xmalloc (strlen (default_program_name) +
  3040. strlen (invocation_prompt));
  3041. sprintf (prompt, invocation_prompt, default_program_name);
  3042. line = info_read_in_echo_area (prompt);
  3043. free (prompt);
  3044. if (!line)
  3045. {
  3046. info_abort_key (window, 0);
  3047. return;
  3048. }
  3049. if (*line)
  3050. program_name = line;
  3051. else
  3052. program_name = default_program_name;
  3053. /* In interactive usage they'd probably expect us to begin looking
  3054. from the Top node. */
  3055. top_node = info_get_node (file_name, 0);
  3056. if (!top_node)
  3057. info_error (msg_cant_find_node, "Top");
  3058. invocation_ref = info_intuit_options_node (top_node, program_name);
  3059. /* We've got our best shot at the invocation node. Now select it. */
  3060. if (invocation_ref)
  3061. {
  3062. info_select_reference (window, invocation_ref);
  3063. info_reference_free (invocation_ref);
  3064. }
  3065. free (line);
  3066. free (default_program_name);
  3067. }
  3068. DECLARE_INFO_COMMAND (info_man, _("Read a manpage reference and select it"))
  3069. {
  3070. char *line;
  3071. line = info_read_in_echo_area (_("Get Manpage: "));
  3072. if (!line)
  3073. {
  3074. info_abort_key (window, 0);
  3075. return;
  3076. }
  3077. canonicalize_whitespace (line);
  3078. if (*line)
  3079. {
  3080. NODE *manpage = info_get_node (MANPAGE_FILE_BUFFER_NAME, line);
  3081. if (manpage)
  3082. info_set_node_of_window (window, manpage);
  3083. }
  3084. free (line);
  3085. }
  3086. /* Move to the "Top" node in this file. */
  3087. DECLARE_INFO_COMMAND (info_top_node, _("Select the node 'Top' in this file"))
  3088. {
  3089. info_parse_and_select ("Top", window);
  3090. }
  3091. /* Move to the node "(dir)Top". */
  3092. DECLARE_INFO_COMMAND (info_dir_node, _("Select the node '(dir)'"))
  3093. {
  3094. info_parse_and_select ("(dir)Top", window);
  3095. }
  3096. DECLARE_INFO_COMMAND (info_display_file_info,
  3097. _("Show full file name of node being displayed"))
  3098. {
  3099. if (window->node->fullpath && *window->node->fullpath)
  3100. {
  3101. int line = window_line_of_point (window);
  3102. window_message_in_echo_area ("File name: %s, line %d of %ld (%ld%%)",
  3103. window->node->subfile
  3104. ? window->node->subfile
  3105. : window->node->fullpath,
  3106. line, window->line_count,
  3107. line * 100 / window->line_count);
  3108. }
  3109. else
  3110. window_message_in_echo_area ("Internal node");
  3111. }
  3112. DECLARE_INFO_COMMAND (info_history_node,
  3113. _("Select the most recently selected node"))
  3114. {
  3115. if (window->hist_index > 1)
  3116. forget_node (window);
  3117. else
  3118. info_error (_("No earlier node in history"));
  3119. }
  3120. /* Read the name of a file and select the entire file. */
  3121. DECLARE_INFO_COMMAND (info_view_file, _("Read the name of a file and select it"))
  3122. {
  3123. char *line;
  3124. line = info_read_in_echo_area (_("Find file: "));
  3125. if (!line)
  3126. {
  3127. info_abort_key (active_window, 1);
  3128. return;
  3129. }
  3130. if (*line)
  3131. {
  3132. NODE *node;
  3133. node = info_get_node (line, "*");
  3134. if (!node)
  3135. {
  3136. if (info_recent_file_error)
  3137. info_error ("%s", info_recent_file_error);
  3138. else
  3139. info_error (_("Cannot find '%s'."), line);
  3140. }
  3141. else
  3142. info_set_node_of_window (window, node);
  3143. free (line);
  3144. }
  3145. }
  3146. /* **************************************************************** */
  3147. /* */
  3148. /* Dumping and Printing Nodes */
  3149. /* */
  3150. /* **************************************************************** */
  3151. enum
  3152. {
  3153. DUMP_SUCCESS,
  3154. DUMP_INFO_ERROR,
  3155. DUMP_SYS_ERROR
  3156. };
  3157. static int dump_node_to_stream (char *filename, char *nodename,
  3158. FILE *stream, int dump_subnodes);
  3159. static void initialize_dumping (void);
  3160. /* Dump the nodes specified with REFERENCES to the file named
  3161. in OUTPUT_FILENAME. If DUMP_SUBNODES is set, recursively dump
  3162. the nodes which appear in the menu of each node dumped. */
  3163. void
  3164. dump_nodes_to_file (REFERENCE **references,
  3165. char *output_filename, int dump_subnodes)
  3166. {
  3167. int i;
  3168. FILE *output_stream;
  3169. if (!references)
  3170. return;
  3171. /* Get the stream to print the nodes to. Special case of an output
  3172. filename of "-" means to dump the nodes to stdout. */
  3173. if (strcmp (output_filename, "-") == 0)
  3174. output_stream = stdout;
  3175. else
  3176. output_stream = fopen (output_filename, "w");
  3177. if (!output_stream)
  3178. {
  3179. info_error (_("Could not create output file '%s'."), output_filename);
  3180. return;
  3181. }
  3182. initialize_dumping ();
  3183. /* Print each node to stream. */
  3184. for (i = 0; references[i]; i++)
  3185. {
  3186. if (dump_node_to_stream (references[i]->filename,
  3187. references[i]->nodename,
  3188. output_stream,
  3189. dump_subnodes) == DUMP_SYS_ERROR)
  3190. {
  3191. info_error (_("error writing to %s: %s"), output_filename,
  3192. strerror (errno));
  3193. exit (EXIT_FAILURE);
  3194. }
  3195. }
  3196. if (output_stream != stdout)
  3197. fclose (output_stream);
  3198. debug (1, (_("closing %s"), output_filename));
  3199. }
  3200. /* A place to remember already dumped nodes. */
  3201. static struct info_namelist_entry *dumped_already;
  3202. static void
  3203. initialize_dumping (void)
  3204. {
  3205. info_namelist_free (dumped_already);
  3206. dumped_already = NULL;
  3207. }
  3208. /* Get and print the node specified by FILENAME and NODENAME to STREAM.
  3209. If DUMP_SUBNODES is non-zero, recursively dump the nodes which appear
  3210. in the menu of each node dumped. */
  3211. static int
  3212. dump_node_to_stream (char *filename, char *nodename,
  3213. FILE *stream, int dump_subnodes)
  3214. {
  3215. register int i;
  3216. NODE *node;
  3217. node = info_get_node (filename, nodename);
  3218. if (!node)
  3219. {
  3220. if (info_recent_file_error)
  3221. info_error ("%s", info_recent_file_error);
  3222. else
  3223. {
  3224. if (filename && *nodename != '(')
  3225. info_error (msg_cant_file_node,
  3226. filename_non_directory (filename),
  3227. nodename);
  3228. else
  3229. info_error (msg_cant_find_node, nodename);
  3230. }
  3231. return DUMP_INFO_ERROR;
  3232. }
  3233. /* If we have already dumped this node, don't dump it again. */
  3234. if (info_namelist_add (&dumped_already, node->nodename))
  3235. {
  3236. free_history_node (node);
  3237. return DUMP_SUCCESS;
  3238. }
  3239. /* Maybe we should print some information about the node being output. */
  3240. debug (1, (_("writing node %s..."), node_printed_rep (node)));
  3241. if (write_node_to_stream (node, stream))
  3242. {
  3243. free_history_node (node);
  3244. return DUMP_SYS_ERROR;
  3245. }
  3246. /* If we are dumping subnodes, get the list of menu items in this node,
  3247. and dump each one recursively. */
  3248. if (dump_subnodes)
  3249. {
  3250. REFERENCE **menu = NULL;
  3251. /* If this node is an Index, do not dump the menu references. */
  3252. if (string_in_line ("Index", node->nodename) == -1)
  3253. menu = node->references;
  3254. if (menu)
  3255. {
  3256. for (i = 0; menu[i]; i++)
  3257. {
  3258. if (REFERENCE_MENU_ITEM != menu[i]->type) continue;
  3259. /* We don't dump Info files which are different than the
  3260. current one. */
  3261. if (!menu[i]->filename)
  3262. if (dump_node_to_stream (filename, menu[i]->nodename,
  3263. stream, dump_subnodes) == DUMP_SYS_ERROR)
  3264. {
  3265. free_history_node (node);
  3266. return DUMP_SYS_ERROR;
  3267. }
  3268. }
  3269. }
  3270. }
  3271. free_history_node (node);
  3272. return DUMP_SUCCESS;
  3273. }
  3274. #if !defined (DEFAULT_INFO_PRINT_COMMAND)
  3275. # define DEFAULT_INFO_PRINT_COMMAND "lpr"
  3276. #endif /* !DEFAULT_INFO_PRINT_COMMAND */
  3277. DECLARE_INFO_COMMAND (info_print_node,
  3278. _("Pipe the contents of this node through INFO_PRINT_COMMAND"))
  3279. {
  3280. FILE *printer_pipe;
  3281. char *print_command = getenv ("INFO_PRINT_COMMAND");
  3282. int piping = 0;
  3283. if (!print_command || !*print_command)
  3284. print_command = DEFAULT_INFO_PRINT_COMMAND;
  3285. /* Note that on MS-DOS/MS-Windows, this MUST open the pipe in the
  3286. (default) text mode, since the printer drivers there need to see
  3287. DOS-style CRLF pairs at the end of each line.
  3288. FIXME: if we are to support Mac-style text files, we might need
  3289. to convert the text here. */
  3290. /* INFO_PRINT_COMMAND which says ">file" means write to that file.
  3291. Presumably, the name of the file is the local printer device. */
  3292. if (*print_command == '>')
  3293. printer_pipe = fopen (++print_command, "w");
  3294. else
  3295. {
  3296. printer_pipe = popen (print_command, "w");
  3297. piping = 1;
  3298. }
  3299. if (!printer_pipe)
  3300. {
  3301. info_error (_("Cannot open pipe to '%s'."), print_command);
  3302. return;
  3303. }
  3304. /* Maybe we should print some information about the node being output. */
  3305. debug (1, (_("printing node %s..."), node_printed_rep (window->node)));
  3306. write_node_to_stream (window->node, printer_pipe);
  3307. if (piping)
  3308. pclose (printer_pipe);
  3309. else
  3310. fclose (printer_pipe);
  3311. debug (1, (_("finished printing node %s"), node_printed_rep (window->node)));
  3312. }
  3313. int
  3314. write_node_to_stream (NODE *node, FILE *stream)
  3315. {
  3316. return fwrite (node->contents, node->nodelen, 1, stream) != 1;
  3317. }
  3318. /* **************************************************************** */
  3319. /* */
  3320. /* Info Searching Commands */
  3321. /* */
  3322. /* **************************************************************** */
  3323. /* Variable controlling the garbage collection of files briefly visited
  3324. during searches. Such files are normally gc'ed, unless they were
  3325. compressed to begin with. If this variable is non-zero, it says
  3326. to gc even those file buffer contents which had to be uncompressed. */
  3327. int gc_compressed_files = 0;
  3328. static char *search_string = NULL;
  3329. static int isearch_is_active = 0;
  3330. static int last_search_direction = 0;
  3331. static int last_search_case_sensitive = 0;
  3332. /* Whether to use regexps or not for search. */
  3333. static int use_regex = 1;
  3334. /* Toggle the usage of regular expressions in searches. */
  3335. DECLARE_INFO_COMMAND (info_toggle_regexp,
  3336. _("Toggle the usage of regular expressions in searches"))
  3337. {
  3338. use_regex = !use_regex;
  3339. window_message_in_echo_area (use_regex
  3340. ? _("Using regular expressions for searches.")
  3341. : _("Using literal strings for searches."));
  3342. }
  3343. /* Search for STRING in NODE starting at START. The DIR argument says which
  3344. direction to search in. If it is positive, search forward, else backwards.
  3345. If the string was found, return its location in POFF, set the
  3346. window's node, its point to be the found string, and readjust
  3347. the window's pagetop. NODE can be retained as a field within WINDOW.
  3348. WINDOW->matches should be a list of matches for NODE->contents, or null.
  3349. If new matches are calculated, they are saved in WINDOW->matches. */
  3350. static enum search_result
  3351. info_search_in_node_internal (WINDOW *window, NODE *node,
  3352. char *string, long start,
  3353. int dir, int case_sensitive,
  3354. int match_regexp, long *poff)
  3355. {
  3356. enum search_result result = search_not_found;
  3357. long start1, end1;
  3358. int match_index;
  3359. long new_point;
  3360. MATCH_STATE matches;
  3361. /* Check if we need to calculate new results. */
  3362. if (!matches_ready (&window->matches)
  3363. || strcmp (window->search_string, string)
  3364. || window->search_is_case_sensitive != case_sensitive)
  3365. {
  3366. free_matches (&window->matches);
  3367. free (window->search_string);
  3368. window->search_string = xstrdup (string);
  3369. window->search_is_case_sensitive = case_sensitive;
  3370. result = regexp_search (string, !match_regexp, !case_sensitive,
  3371. node->contents, node->nodelen, &matches);
  3372. }
  3373. else
  3374. {
  3375. matches = window->matches;
  3376. result = search_success;
  3377. }
  3378. if (result != search_success)
  3379. return result;
  3380. if (node->flags & N_Simple)
  3381. {
  3382. /* There are matches in the node, but it hasn't been scanned yet. Get
  3383. the node again, because its contents may differ. */
  3384. enum search_result subresult;
  3385. NODE *full_node;
  3386. free_matches (&matches);
  3387. full_node = info_get_node (node->fullpath, node->nodename);
  3388. subresult = info_search_in_node_internal (window, full_node,
  3389. string, start,
  3390. dir, case_sensitive,
  3391. match_regexp, poff);
  3392. if (window->node != full_node)
  3393. free (full_node);
  3394. return subresult;
  3395. }
  3396. if (dir > 0)
  3397. {
  3398. start1 = start;
  3399. end1 = node->nodelen;
  3400. }
  3401. else
  3402. {
  3403. start1 = 0;
  3404. end1 = start + 1; /* include start byte in search area */
  3405. }
  3406. if (start1 < node->body_start)
  3407. start1 = node->body_start;
  3408. if (end1 < node->body_start)
  3409. end1 = node->body_start;
  3410. result = match_in_match_list (&matches, start1, end1, dir, &match_index);
  3411. if (result != search_success)
  3412. return result;
  3413. *poff = match_by_index (&matches, match_index).rm_so;
  3414. window->flags |= W_UpdateWindow;
  3415. if (window->node != node)
  3416. info_set_node_of_window (window, node);
  3417. window->matches = matches;
  3418. if (isearch_is_active && dir > 0)
  3419. new_point = match_by_index (&matches, match_index).rm_eo;
  3420. else
  3421. new_point = match_by_index (&matches, match_index).rm_so;
  3422. window->point = new_point;
  3423. return result;
  3424. }
  3425. /* Search for STRING starting in WINDOW, starting at *START_OFF.
  3426. If the string is found in this node, set point to that position.
  3427. Otherwise, get the file buffer associated with WINDOW's node, and search
  3428. through each node in that file.
  3429. If the search succeeds, return non-zero. *START_OFF is given the start of
  3430. the found string instance. Set node and point of window to where the string
  3431. was found. (If the variable ISEARCH_IS_ACTIVE is non-zero and the search is
  3432. forwards, the point is set to the end of the search.)
  3433. Return non-zero if the search fails. */
  3434. static int
  3435. info_search_internal (char *string, WINDOW *window,
  3436. int dir, int case_sensitive,
  3437. long *start_off)
  3438. {
  3439. register int i;
  3440. FILE_BUFFER *file_buffer;
  3441. long start;
  3442. enum search_result result;
  3443. int search_other_nodes = 1;
  3444. int number_of_tags = -1, starting_tag = -1, current_tag = -1;
  3445. NODE *node = window->node; /* Node to search in. */
  3446. char *subfile_name = 0;
  3447. TAG *tag;
  3448. char *msg = 0;
  3449. int first_time = 1;
  3450. /* If this node isn't part of a larger file, search this node only. */
  3451. file_buffer = file_buffer_of_window (window);
  3452. if (!file_buffer || !file_buffer->tags
  3453. || !strcmp (window->node->nodename, "*"))
  3454. search_other_nodes = 0;
  3455. /* Find number of tags and current tag. */
  3456. if (search_other_nodes)
  3457. {
  3458. char *initial_nodename = window->node->nodename;
  3459. for (i = 0; file_buffer->tags[i]; i++)
  3460. if (strcmp (initial_nodename, file_buffer->tags[i]->nodename) == 0)
  3461. {
  3462. starting_tag = i;
  3463. subfile_name = file_buffer->tags[i]->filename;
  3464. }
  3465. number_of_tags = i;
  3466. /* Our tag wasn't found. This shouldn't happen. */
  3467. if (starting_tag == -1)
  3468. return -1;
  3469. current_tag = starting_tag;
  3470. }
  3471. /* Set starting position of search. */
  3472. start = *start_off;
  3473. /* Search through subsequent nodes, wrapping around to the top
  3474. of the Info file until we find the string or return to this
  3475. window's node and point. */
  3476. while (1)
  3477. {
  3478. result = info_search_in_node_internal (window, node, string, start, dir,
  3479. case_sensitive, use_regex, start_off);
  3480. if (node != window->node)
  3481. free_history_node (node);
  3482. if (result == search_invalid)
  3483. return 1;
  3484. if (result == search_success)
  3485. {
  3486. if (!echo_area_is_active)
  3487. {
  3488. if (msg)
  3489. window_message_in_echo_area ("%s", _(msg));
  3490. else
  3491. window_clear_echo_area ();
  3492. }
  3493. return 0;
  3494. }
  3495. if (!search_other_nodes)
  3496. break;
  3497. /* If we've searched our starting node twice, there are no matches.
  3498. Bail out. (We searched the second time in case there were matches
  3499. before the starting offset.) */
  3500. if (current_tag == starting_tag && !first_time)
  3501. break;
  3502. first_time = 0;
  3503. /* Find the next tag that isn't an anchor. */
  3504. for (i = current_tag + dir; ; i += dir)
  3505. {
  3506. if (i < 0)
  3507. {
  3508. msg = N_("Search continued from the end of the document.");
  3509. i = number_of_tags - 1;
  3510. }
  3511. else if (i == number_of_tags)
  3512. {
  3513. msg = N_("Search continued from the beginning of the document.");
  3514. i = 0;
  3515. }
  3516. tag = file_buffer->tags[i];
  3517. if (tag->cache.nodelen != 0)
  3518. break;
  3519. }
  3520. current_tag = i;
  3521. /* Display message when searching a new subfile. */
  3522. if (!echo_area_is_active && tag->filename != subfile_name)
  3523. {
  3524. subfile_name = tag->filename;
  3525. window_message_in_echo_area
  3526. (_("Searching subfile %s ..."),
  3527. filename_non_directory (subfile_name));
  3528. }
  3529. /* Get a new node to search in. */
  3530. free_matches (&window->matches);
  3531. node = info_node_of_tag_fast (file_buffer, &tag);
  3532. if (!node)
  3533. {
  3534. /* If not doing i-search... */
  3535. if (!echo_area_is_active)
  3536. {
  3537. if (info_recent_file_error)
  3538. info_error ("%s", info_recent_file_error);
  3539. else
  3540. info_error (msg_cant_file_node,
  3541. filename_non_directory (file_buffer->filename),
  3542. tag->nodename);
  3543. }
  3544. return -1;
  3545. }
  3546. if (dir < 0)
  3547. start = tag->cache.nodelen;
  3548. else
  3549. start = 0;
  3550. /* Allow C-g to quit the search, failing it if pressed. */
  3551. fill_input_buffer (0); \
  3552. if (info_input_buffer[pop_index] == Control ('g'))
  3553. goto funexit;
  3554. }
  3555. /* Not in interactive search. */
  3556. if (!echo_area_is_active)
  3557. info_error ("%s", _("Search failed."));
  3558. funexit:
  3559. return -1;
  3560. }
  3561. /* Minimal length of a search string */
  3562. int min_search_length = 1;
  3563. /* Read a string from the user, storing the result in SEARCH_STRING. Return 0
  3564. if the user aborted. */
  3565. static int
  3566. ask_for_search_string (int case_sensitive, int use_regex, int direction)
  3567. {
  3568. char *line, *prompt;
  3569. if (search_string)
  3570. asprintf (&prompt, _("%s%s%s [%s]: "),
  3571. use_regex ? _("Regexp search") : _("Search"),
  3572. case_sensitive ? _(" case-sensitively") : "",
  3573. direction < 0 ? _(" backward") : "",
  3574. search_string);
  3575. else
  3576. asprintf (&prompt, _("%s%s%s: "),
  3577. use_regex ? _("Regexp search") : _("Search"),
  3578. case_sensitive ? _(" case-sensitively") : "",
  3579. direction < 0 ? _(" backward") : "");
  3580. line = info_read_in_echo_area (prompt);
  3581. free (prompt);
  3582. if (!line) /* User aborted. */
  3583. {
  3584. return 0;
  3585. }
  3586. if (!*line)
  3587. {
  3588. free (line);
  3589. return 1;
  3590. }
  3591. if (mbslen (line) < min_search_length)
  3592. {
  3593. info_error ("%s", _("Search string too short"));
  3594. free (line);
  3595. return 1;
  3596. }
  3597. free (search_string);
  3598. search_string = line;
  3599. return 1;
  3600. }
  3601. /* Common entry point for the search functions. Arguments:
  3602. WINDOW The window to search in
  3603. COUNT The sign of this argument defines the search
  3604. direction (negative for searching backwards);
  3605. its absolute value gives number of repetitions.
  3606. CASE_SENSITIVE Whether the search is case-sensitive or not.
  3607. */
  3608. static void
  3609. info_search_1 (WINDOW *window, int count, int case_sensitive)
  3610. {
  3611. int result;
  3612. int direction;
  3613. long start_off;
  3614. char *p;
  3615. if (count < 0)
  3616. {
  3617. direction = -1;
  3618. count = -count;
  3619. }
  3620. else
  3621. {
  3622. direction = 1;
  3623. if (count == 0)
  3624. count = 1; /* for backward compatibility */
  3625. }
  3626. if (!ask_for_search_string (case_sensitive, use_regex, direction)
  3627. || !search_string)
  3628. return;
  3629. start_off = window->point + direction;
  3630. /* If the search string includes upper-case letters, make the search
  3631. case-sensitive. */
  3632. if (case_sensitive == 0)
  3633. for (p = search_string; *p; p++)
  3634. if (isupper (*p))
  3635. {
  3636. case_sensitive = 1;
  3637. break;
  3638. }
  3639. last_search_direction = direction;
  3640. last_search_case_sensitive = case_sensitive;
  3641. for (result = 0; result == 0 && count--; )
  3642. result = info_search_internal (search_string,
  3643. active_window, direction, case_sensitive,
  3644. &start_off);
  3645. window_adjust_pagetop (window);
  3646. /* Perhaps free the unreferenced file buffers that were searched, but
  3647. not retained. */
  3648. gc_file_buffers_and_nodes ();
  3649. }
  3650. /* Set *T to an offset within the tags table of the node referenced by R,
  3651. using WINDOW for defaults. *FB is set to the file buffer structure for
  3652. the node. */
  3653. static int
  3654. tag_of_reference (REFERENCE *r, WINDOW *window, FILE_BUFFER **fb, TAG ***t)
  3655. {
  3656. char *filename, *nodename;
  3657. int i;
  3658. filename = r->filename;
  3659. nodename = r->nodename;
  3660. if (!filename)
  3661. filename = window->node->fullpath;
  3662. if (!nodename || !*nodename)
  3663. nodename = "Top";
  3664. *fb = info_find_file (filename);
  3665. if (!*fb)
  3666. return 0;
  3667. for (i = 0; *(*t = &(*fb)->tags[i]); i++)
  3668. if (!strcmp (nodename, (**t)->nodename))
  3669. goto found_tag;
  3670. return 0;
  3671. found_tag: ;
  3672. return 1;
  3673. }
  3674. /* Value for NODE.active_menu */
  3675. #define BEFORE_MENU -99
  3676. static void tree_search_check_node (WINDOW *window);
  3677. static void tree_search_check_node_backwards (WINDOW *window);
  3678. /* Search in WINDOW->node, and nodes reachable by menus from it, for
  3679. WINDOW->search_string. */
  3680. static void
  3681. tree_search_check_node (WINDOW *window)
  3682. {
  3683. long start_off;
  3684. enum search_result result;
  3685. char *string;
  3686. int previous_match;
  3687. if (window->node->active_menu != 0)
  3688. previous_match = 1;
  3689. else
  3690. {
  3691. previous_match = 0;
  3692. window->node->active_menu = BEFORE_MENU;
  3693. }
  3694. string = xstrdup (window->search_string);
  3695. goto check_node;
  3696. check_node:
  3697. result = info_search_in_node_internal (window, window->node,
  3698. string,
  3699. window->point + 1,
  3700. 1, /* Search forwards */
  3701. 1, /* Case-sensitive */
  3702. 0, /* No regular expressions. */
  3703. &start_off);
  3704. if (result == search_success)
  3705. {
  3706. info_show_point (window);
  3707. goto funexit;
  3708. }
  3709. /* Otherwise, try each menu entry in turn. */
  3710. if (matches_ready (&window->matches))
  3711. window->point++; /* Find this match again if/when we come back. */
  3712. goto check_menus;
  3713. /* At this juncture, window->node->active_menu is the index of the last
  3714. reference in the node to have been checked, plus one. BEFORE_MENU is a
  3715. special code to say that none of them have been checked. */
  3716. check_menus:
  3717. if (!(window->node->flags & N_IsIndex)) /* Don't go down menus in index */
  3718. { /* nodes, because this leads to loops. */
  3719. REFERENCE *r;
  3720. int ref_index;
  3721. if (window->node->active_menu != BEFORE_MENU)
  3722. ref_index = window->node->active_menu;
  3723. else
  3724. ref_index = 0;
  3725. for (; (r = window->node->references[ref_index]); ref_index++)
  3726. if (r->type == REFERENCE_MENU_ITEM)
  3727. {
  3728. FILE_BUFFER *file_buffer;
  3729. TAG **tag;
  3730. NODE *node;
  3731. if (!tag_of_reference (r, window, &file_buffer, &tag))
  3732. continue;
  3733. if ((*tag)->flags & N_SeenBySearch)
  3734. continue;
  3735. (*tag)->flags |= N_SeenBySearch;
  3736. window->node->active_menu = ref_index + 1;
  3737. node = info_node_of_tag (file_buffer, tag);
  3738. if (!node)
  3739. continue;
  3740. info_set_node_of_window_fast (window, node);
  3741. window->node->active_menu = BEFORE_MENU;
  3742. goto check_node;
  3743. }
  3744. }
  3745. goto go_up;
  3746. go_up:
  3747. /* If no more menu entries, try going back. */
  3748. if (window->hist_index >= 2
  3749. && window->hist[window->hist_index - 2]->node->active_menu != 0)
  3750. {
  3751. forget_node_fast (window);
  3752. goto check_menus;
  3753. }
  3754. /* Go back to the final match. */
  3755. if (previous_match)
  3756. {
  3757. REFERENCE *mentry;
  3758. message_in_echo_area (_("Going back to last match from %s"),
  3759. window->node->nodename);
  3760. /* This is a trick. Add an arbitrary node to the window history,
  3761. but set active_menu to one more than the number of references. When
  3762. we call tree_search_check_node_backwards, this will repeatedly go down
  3763. the last menu entry for us. */
  3764. {
  3765. int n = 0;
  3766. while (window->node->references[n])
  3767. n++;
  3768. window->node->active_menu = n + 1;
  3769. mentry = select_menu_digit (window, '1');
  3770. if (!mentry)
  3771. goto funexit;
  3772. if (!info_select_reference (window, mentry))
  3773. goto funexit;
  3774. window->node->active_menu = BEFORE_MENU;
  3775. }
  3776. window->point = window->node->body_start;
  3777. tree_search_check_node_backwards (window);
  3778. }
  3779. info_error (previous_match ? _("No more matches.") : _("Search failed."));
  3780. funexit:
  3781. free (string);
  3782. } /*********** end tree_search_check_node *************/
  3783. /* The same as tree_search_check_node, but backwards. */
  3784. static void
  3785. tree_search_check_node_backwards (WINDOW *window)
  3786. {
  3787. long start_off;
  3788. enum search_result result;
  3789. char *string;
  3790. int previous_match;
  3791. previous_match = (window->node->active_menu != 0);
  3792. string = xstrdup (window->search_string);
  3793. goto check_node;
  3794. check_node:
  3795. result = info_search_in_node_internal (window, window->node,
  3796. string,
  3797. window->point - 1,
  3798. -1, /* Search backwards */
  3799. 1, /* Case-sensitive */
  3800. 0, /* No regular expressions. */
  3801. &start_off);
  3802. if (result == search_success)
  3803. {
  3804. info_show_point (window);
  3805. goto funexit;
  3806. }
  3807. goto go_up;
  3808. /* Check through menus in current node, in reverse order.
  3809. At this juncture, window->node->active_menu is the index of the last
  3810. reference in the node to have been checked, plus one. BEFORE_MENU is a
  3811. special code to say that none of them have been checked. */
  3812. check_menus:
  3813. if (!(window->node->flags & N_IsIndex)) /* Don't go down menus in index */
  3814. { /* nodes, because this leads to loops. */
  3815. REFERENCE *r;
  3816. int ref_index;
  3817. if (window->node->active_menu == BEFORE_MENU)
  3818. goto check_node;
  3819. else
  3820. ref_index = window->node->active_menu - 2;
  3821. for (; ref_index >= 0; ref_index--)
  3822. {
  3823. r = window->node->references[ref_index];
  3824. if (r->type == REFERENCE_MENU_ITEM)
  3825. {
  3826. TAG **tag;
  3827. FILE_BUFFER *file_buffer;
  3828. NODE *node;
  3829. if (!tag_of_reference (r, window, &file_buffer, &tag))
  3830. continue;
  3831. /* This inverts what is done for the forwards search. It's
  3832. possible that we will visit the nodes in a different order if
  3833. there is more than one reference to a node. */
  3834. if (!((*tag)->flags & N_SeenBySearch))
  3835. continue;
  3836. node = info_node_of_tag (file_buffer, tag);
  3837. if (!node)
  3838. continue;
  3839. window->node->active_menu = ref_index + 1;
  3840. info_set_node_of_window_fast (window, node);
  3841. window->point = window->node->nodelen;
  3842. {
  3843. /* Start at the last menu entry in the subordinate node. */
  3844. int i;
  3845. i = 0;
  3846. while(window->node->references[i])
  3847. i++;
  3848. window->node->active_menu = i + 1;
  3849. }
  3850. goto check_menus;
  3851. }
  3852. }
  3853. }
  3854. window->node->active_menu = BEFORE_MENU;
  3855. goto check_node;
  3856. /* Try going back. */
  3857. go_up:
  3858. if (window->hist_index >= 2
  3859. && window->hist[window->hist_index - 2]->node->active_menu != 0)
  3860. {
  3861. TAG **tag;
  3862. REFERENCE *r;
  3863. FILE_BUFFER *file_buffer;
  3864. forget_node_fast (window);
  3865. r = window->node->references[window->node->active_menu - 1];
  3866. /* Clear the flag to say we've been to the node we just came back
  3867. from. This reverse the order from the forwards search, where
  3868. we set this flag just before going down. */
  3869. if (r && tag_of_reference (r, window, &file_buffer, &tag))
  3870. {
  3871. (*tag)->flags &= ~N_SeenBySearch;
  3872. }
  3873. goto check_menus;
  3874. }
  3875. /* Otherwise, no result. */
  3876. info_error (previous_match ? _("No more matches.") : _("Search failed."));
  3877. funexit:
  3878. free (string);
  3879. } /*********** end tree_search_check_node_backwards *************/
  3880. /* Clear N_SeenBySearch for all nodes. */
  3881. void
  3882. wipe_seen_flags (void)
  3883. {
  3884. int fb_index;
  3885. TAG **t;
  3886. for (fb_index = 0; fb_index < info_loaded_files_index; fb_index++)
  3887. {
  3888. t = info_loaded_files[fb_index]->tags;
  3889. if (!t)
  3890. continue; /* Probably a sub-file of a split file. */
  3891. for (; *t; t++)
  3892. {
  3893. (*t)->flags &= ~N_SeenBySearch;
  3894. }
  3895. }
  3896. }
  3897. DECLARE_INFO_COMMAND (info_tree_search,
  3898. _("Search this node and subnodes for a string."))
  3899. {
  3900. char *prompt, *line;
  3901. int i;
  3902. /* TODO: Display manual name */
  3903. /* TRANSLATORS: %s is the title of a node. */
  3904. asprintf (&prompt, _("Search under %s: "),
  3905. window->node->nodename);
  3906. line = info_read_in_echo_area (prompt); free (prompt);
  3907. if (!line)
  3908. return;
  3909. /* Remove relics of previous tree searches. */
  3910. wipe_seen_flags ();
  3911. for (i = 0; i < window->hist_index; i++)
  3912. window->hist[i]->node->active_menu = 0;
  3913. window->search_string = line;
  3914. tree_search_check_node (window);
  3915. }
  3916. DECLARE_INFO_COMMAND (info_tree_search_next,
  3917. _("Go to next match in Info sub-tree"))
  3918. {
  3919. if (!window->search_string || window->node->active_menu == 0)
  3920. {
  3921. info_error (_("No active search"));
  3922. return;
  3923. }
  3924. tree_search_check_node (window);
  3925. }
  3926. DECLARE_INFO_COMMAND (info_tree_search_previous,
  3927. _("Go to previous match in Info sub-tree"))
  3928. {
  3929. if (!window->search_string || window->node->active_menu == 0)
  3930. {
  3931. info_error (_("No active search"));
  3932. return;
  3933. }
  3934. tree_search_check_node_backwards (window);
  3935. }
  3936. #undef BEFORE_MENU
  3937. DECLARE_INFO_COMMAND (info_search_case_sensitively,
  3938. _("Read a string and search for it case-sensitively"))
  3939. {
  3940. info_search_1 (window, count, 1);
  3941. }
  3942. DECLARE_INFO_COMMAND (info_search, _("Read a string and search for it"))
  3943. {
  3944. info_search_1 (window, count, 0);
  3945. }
  3946. DECLARE_INFO_COMMAND (info_search_backward,
  3947. _("Read a string and search backward for it"))
  3948. {
  3949. info_search_1 (window, -count, 0);
  3950. }
  3951. int search_skip_screen_p = 0;
  3952. DECLARE_INFO_COMMAND (info_search_next,
  3953. _("Repeat last search in the same direction"))
  3954. {
  3955. long start_off = window->point + 1;
  3956. NODE *starting_node = window->node;
  3957. int result;
  3958. if (!last_search_direction || !search_string)
  3959. {
  3960. info_error ("%s", _("No previous search string"));
  3961. return;
  3962. }
  3963. if (search_skip_screen_p)
  3964. {
  3965. /* Find window bottom */
  3966. long n = window->height + window->pagetop;
  3967. if (n < window->line_count)
  3968. start_off = window->line_starts[n];
  3969. else
  3970. start_off = window->node->nodelen;
  3971. }
  3972. for (result = 0; result == 0 && count--; )
  3973. result = info_search_internal (search_string,
  3974. active_window, 1,
  3975. last_search_case_sensitive,
  3976. &start_off);
  3977. if (result == 0 && window->node == starting_node && search_skip_screen_p)
  3978. {
  3979. long match_line = window_line_of_point (window);
  3980. long new_pagetop;
  3981. /* Scroll down a whole number of screenfulls to make match visible. */
  3982. new_pagetop = window->pagetop;
  3983. new_pagetop += (match_line - window->pagetop) / window->height
  3984. * window->height;
  3985. set_window_pagetop (window, new_pagetop);
  3986. }
  3987. else
  3988. window_adjust_pagetop (window);
  3989. }
  3990. DECLARE_INFO_COMMAND (info_search_previous,
  3991. _("Repeat last search in the reverse direction"))
  3992. {
  3993. long start_off = window->point - 1;
  3994. NODE *starting_node = window->node;
  3995. int result;
  3996. if (!last_search_direction || !search_string)
  3997. {
  3998. info_error ("%s", _("No previous search string"));
  3999. return;
  4000. }
  4001. if (search_skip_screen_p)
  4002. start_off = window->line_starts[window->pagetop] - 1;
  4003. /* start_off can be negative here, in which case info_search_internal
  4004. will go to the previous node straight away. */
  4005. for (result = 0; result == 0 && count--; )
  4006. result = info_search_internal (search_string,
  4007. active_window, -1,
  4008. last_search_case_sensitive,
  4009. &start_off);
  4010. if (result == 0 && window->node == starting_node && search_skip_screen_p)
  4011. {
  4012. long match_line = window_line_of_point (window);
  4013. long new_pagetop;
  4014. /* Scroll up a whole number of screenfulls to make match visible.
  4015. This means if 'info_search_next' was the last command, we'll
  4016. go back to the same place. */
  4017. new_pagetop = window->pagetop - window->height;
  4018. new_pagetop -= (window->pagetop - match_line - 1) / window->height
  4019. * window->height;
  4020. if (new_pagetop < 0)
  4021. new_pagetop = 0;
  4022. set_window_pagetop (window, new_pagetop);
  4023. }
  4024. else
  4025. window_adjust_pagetop (window);
  4026. }
  4027. /* If highlight-searches=On, this will clear any highlighted regions on the
  4028. screen. */
  4029. DECLARE_INFO_COMMAND (info_clear_search,
  4030. _("Clear displayed search matches"))
  4031. {
  4032. free_matches (&window->matches);
  4033. window->flags |= W_UpdateWindow;
  4034. }
  4035. /* **************************************************************** */
  4036. /* */
  4037. /* Incremental Searching */
  4038. /* */
  4039. /* **************************************************************** */
  4040. static void incremental_search (WINDOW *window, int count);
  4041. DECLARE_INFO_COMMAND (isearch_forward,
  4042. _("Search interactively for a string as you type it"))
  4043. {
  4044. incremental_search (window, count);
  4045. }
  4046. DECLARE_INFO_COMMAND (isearch_backward,
  4047. _("Search interactively for a string as you type it"))
  4048. {
  4049. incremental_search (window, -count);
  4050. }
  4051. /* Structure defining the current state of an incremental search. */
  4052. typedef struct {
  4053. char *fullpath;
  4054. char *nodename;
  4055. long pagetop; /* LINE_STARTS[PAGETOP] is first line in WINDOW. */
  4056. long point; /* Point in window. */
  4057. long start; /* Offset in node contents where search started. */
  4058. int search_index; /* Offset of the last char in the search string. */
  4059. int direction; /* The direction that this search is heading in. */
  4060. enum search_result failing; /* Whether or not this search failed. */
  4061. } SEARCH_STATE;
  4062. /* Incrementally search for a string as it is typed. */
  4063. /* The last accepted incremental search string. */
  4064. static char *last_isearch_accepted = NULL;
  4065. /* The current incremental search string. */
  4066. static char *isearch_string = NULL;
  4067. static int isearch_string_index = 0;
  4068. static int isearch_string_size = 0;
  4069. /* Array of search states. */
  4070. static SEARCH_STATE **isearch_states = NULL;
  4071. static size_t isearch_states_index = 0;
  4072. static size_t isearch_states_slots = 0;
  4073. /* Get the state of WINDOW, and save it in STATE. */
  4074. static void
  4075. window_get_state (WINDOW *window, SEARCH_STATE *state)
  4076. {
  4077. state->fullpath = window->node->fullpath;
  4078. state->nodename = window->node->nodename;
  4079. state->pagetop = window->pagetop;
  4080. state->point = window->point;
  4081. }
  4082. /* Set the node, pagetop, and point of WINDOW. */
  4083. static void
  4084. window_set_state (WINDOW *window, SEARCH_STATE *state)
  4085. {
  4086. if (strcmp(window->node->fullpath, state->fullpath)
  4087. || strcmp(window->node->nodename, state->nodename))
  4088. {
  4089. NODE *n = info_get_node (state->fullpath, state->nodename);
  4090. info_set_node_of_window (window, n);
  4091. }
  4092. window->pagetop = state->pagetop;
  4093. window->point = state->point;
  4094. }
  4095. /* Push the state of this search. */
  4096. static void
  4097. push_isearch (WINDOW *window, int search_index, int direction,
  4098. enum search_result failing, long start_off)
  4099. {
  4100. SEARCH_STATE *state;
  4101. state = xmalloc (sizeof (SEARCH_STATE));
  4102. window_get_state (window, state);
  4103. state->search_index = search_index;
  4104. state->direction = direction;
  4105. state->failing = failing;
  4106. state->start = start_off;
  4107. add_pointer_to_array (state, isearch_states_index, isearch_states,
  4108. isearch_states_slots, 20);
  4109. }
  4110. /* Pop the state of this search to WINDOW, SEARCH_INDEX, and DIRECTION. */
  4111. static void
  4112. pop_isearch (WINDOW *window, int *search_index, int *direction,
  4113. enum search_result *failing, long *start_off)
  4114. {
  4115. SEARCH_STATE *state;
  4116. if (isearch_states_index)
  4117. {
  4118. isearch_states_index--;
  4119. state = isearch_states[isearch_states_index];
  4120. window_set_state (window, state);
  4121. *search_index = state->search_index;
  4122. *direction = state->direction;
  4123. *failing = state->failing;
  4124. *start_off = state->start;
  4125. free (state);
  4126. isearch_states[isearch_states_index] = NULL;
  4127. }
  4128. }
  4129. /* Free the memory used by isearch_states. */
  4130. static void
  4131. free_isearch_states (void)
  4132. {
  4133. register int i;
  4134. for (i = 0; i < isearch_states_index; i++)
  4135. {
  4136. free (isearch_states[i]);
  4137. isearch_states[i] = NULL;
  4138. }
  4139. isearch_states_index = 0;
  4140. }
  4141. /* Display the current search in the echo area. */
  4142. static void
  4143. show_isearch_prompt (int dir, unsigned char *string,
  4144. enum search_result failing)
  4145. {
  4146. register int i;
  4147. const char *prefix;
  4148. char *prompt, *p_rep;
  4149. unsigned int prompt_len, p_rep_index, p_rep_size;
  4150. if (dir < 0)
  4151. prefix = use_regex ? _("Regexp I-search backward: ")
  4152. : _("I-search backward: ");
  4153. else
  4154. prefix = use_regex ? _("Regexp I-search: ")
  4155. : _("I-search: ");
  4156. p_rep_index = p_rep_size = 0;
  4157. p_rep = NULL;
  4158. for (i = 0; string[i]; i++)
  4159. {
  4160. char *rep;
  4161. switch (string[i])
  4162. {
  4163. case ' ': rep = " "; break;
  4164. case LFD: rep = "\\n"; break;
  4165. case TAB: rep = "\\t"; break;
  4166. default:
  4167. rep = pretty_keyname (string[i]);
  4168. }
  4169. if ((p_rep_index + strlen (rep) + 1) >= p_rep_size)
  4170. p_rep = xrealloc (p_rep, p_rep_size += 100);
  4171. strcpy (p_rep + p_rep_index, rep);
  4172. p_rep_index += strlen (rep);
  4173. }
  4174. prompt_len = strlen (prefix) + p_rep_index + 1;
  4175. if (failing != search_success)
  4176. prompt_len += strlen (_("Failing "));
  4177. prompt = xmalloc (prompt_len);
  4178. sprintf (prompt, "%s%s%s",
  4179. failing != search_success ? _("Failing ") : "",
  4180. prefix, p_rep ? p_rep : "");
  4181. window_message_in_echo_area ("%s", prompt);
  4182. free (p_rep);
  4183. free (prompt);
  4184. display_cursor_at_point (active_window);
  4185. }
  4186. /* Read and dispatch loop for incremental search mode. */
  4187. static void
  4188. incremental_search (WINDOW *window, int count)
  4189. {
  4190. int key;
  4191. enum search_result last_search_result, search_result;
  4192. int dir;
  4193. SEARCH_STATE orig_state; /* Window state at start of incremental search. */
  4194. SEARCH_STATE mystate; /* State before each search. */
  4195. char *p;
  4196. int case_sensitive;
  4197. long start_off = window->point;
  4198. int starting_history_entry = window->hist_index - 1;
  4199. if (count < 0)
  4200. dir = -1;
  4201. else
  4202. dir = 1;
  4203. last_search_result = search_result = search_success;
  4204. window_get_state (window, &orig_state);
  4205. isearch_string_index = 0;
  4206. if (!isearch_string_size)
  4207. isearch_string = xmalloc (isearch_string_size = 50);
  4208. isearch_string[isearch_string_index] = '\0';
  4209. isearch_is_active = 1;
  4210. /* Save starting position of search. */
  4211. push_isearch (window, isearch_string_index, dir, search_result, start_off);
  4212. while (isearch_is_active)
  4213. {
  4214. VFunction *func = NULL;
  4215. int quoted = 0;
  4216. /* Show the search string in the echo area. */
  4217. show_isearch_prompt (dir, (unsigned char *) isearch_string,
  4218. search_result);
  4219. /* If a recent display was interrupted, then do the redisplay now if
  4220. it is convenient. */
  4221. if (!info_any_buffered_input_p () && display_was_interrupted_p)
  4222. {
  4223. display_update_display ();
  4224. display_cursor_at_point (active_window);
  4225. }
  4226. /* Read keys, looking in both keymaps for a recognized key sequence. */
  4227. {
  4228. Keymap info_kp, ea_kp;
  4229. info_kp = info_keymap;
  4230. ea_kp = echo_area_keymap;
  4231. key = get_input_key ();
  4232. while (1)
  4233. {
  4234. if (key >= 32 && key < 256)
  4235. break;
  4236. if (info_kp && info_kp[key].type == ISFUNC)
  4237. {
  4238. if (info_kp[key].value.function)
  4239. func = info_kp[key].value.function->func;
  4240. if (func == &isearch_forward
  4241. || func == &isearch_backward
  4242. || func == &info_abort_key)
  4243. {
  4244. goto gotfunc;
  4245. }
  4246. else
  4247. {
  4248. func = 0;
  4249. info_kp = 0;
  4250. }
  4251. }
  4252. else if (info_kp) /* ISKMAP */
  4253. info_kp = info_kp[key].value.keymap;
  4254. if (ea_kp && ea_kp[key].type == ISFUNC)
  4255. {
  4256. if (ea_kp[key].value.function)
  4257. func = ea_kp[key].value.function->func;
  4258. if (func == &ea_abort
  4259. || func == &ea_quoted_insert
  4260. || func == &ea_rubout)
  4261. {
  4262. func = ea_kp[key].value.function->func;
  4263. goto gotfunc;
  4264. }
  4265. else
  4266. {
  4267. func = 0;
  4268. ea_kp = 0;
  4269. }
  4270. }
  4271. else if (ea_kp) /* ISKMAP */
  4272. ea_kp = ea_kp[key].value.keymap;
  4273. if (!info_kp && !ea_kp)
  4274. break;
  4275. key = get_input_key ();
  4276. }
  4277. }
  4278. gotfunc:
  4279. if (func == &ea_quoted_insert)
  4280. {
  4281. /* User wants to insert a character. */
  4282. key = get_input_key ();
  4283. if (key < 0 || key >= 256)
  4284. continue; /* The user pressed a key like an arrow key. */
  4285. quoted = 1;
  4286. }
  4287. if (quoted || (!func && key >= 32 && key < 256))
  4288. {
  4289. push_isearch (window, isearch_string_index, dir,
  4290. search_result, start_off);
  4291. if (isearch_string_index + 2 >= isearch_string_size)
  4292. isearch_string = xrealloc
  4293. (isearch_string, isearch_string_size += 100);
  4294. isearch_string[isearch_string_index++] = key;
  4295. isearch_string[isearch_string_index] = '\0';
  4296. if (search_result != search_success && !use_regex)
  4297. continue;
  4298. }
  4299. else if (func == &ea_rubout)
  4300. {
  4301. /* User wants to delete one level of search? */
  4302. if (!isearch_states_index)
  4303. {
  4304. terminal_ring_bell ();
  4305. continue;
  4306. }
  4307. else
  4308. {
  4309. int end = isearch_string_index;
  4310. /* Remove a complete multi-byte character from the end of the
  4311. search string. */
  4312. do
  4313. {
  4314. pop_isearch (window, &isearch_string_index,
  4315. &dir, &search_result, &start_off);
  4316. }
  4317. while (isearch_string_index > 0
  4318. && (long) mbrlen (isearch_string + isearch_string_index,
  4319. end - isearch_string_index, NULL) <= 0);
  4320. isearch_string[isearch_string_index] = '\0';
  4321. if (isearch_string_index == 0)
  4322. {
  4323. /* Don't search for an empty string. Clear the search. */
  4324. free_matches (&window->matches);
  4325. display_update_one_window (window);
  4326. continue;
  4327. }
  4328. if (search_result != search_success)
  4329. {
  4330. display_update_one_window (window);
  4331. continue;
  4332. }
  4333. }
  4334. }
  4335. else if (func == &isearch_forward || func == &isearch_backward)
  4336. {
  4337. /* If this key invokes an incremental search, then this
  4338. means that we will either search again in the same
  4339. direction, search again in the reverse direction, or
  4340. insert the last search string that was accepted through
  4341. incremental searching. */
  4342. if (func == &isearch_forward && dir > 0
  4343. || func == &isearch_backward && dir < 0)
  4344. {
  4345. /* If the user has typed no characters, then insert the
  4346. last successful search into the current search string. */
  4347. if (isearch_string_index == 0)
  4348. {
  4349. /* Of course, there must be something to insert. */
  4350. if (last_isearch_accepted)
  4351. {
  4352. if (strlen ((char *) last_isearch_accepted) + 1
  4353. >= (unsigned int) isearch_string_size)
  4354. isearch_string = (char *)
  4355. xrealloc (isearch_string,
  4356. isearch_string_size += 10 +
  4357. strlen (last_isearch_accepted));
  4358. strcpy (isearch_string, last_isearch_accepted);
  4359. isearch_string_index = strlen (isearch_string);
  4360. }
  4361. else
  4362. continue;
  4363. }
  4364. else
  4365. {
  4366. /* Search again in the same direction. This means start
  4367. from a new place if the last search was successful. */
  4368. if (search_result == search_success)
  4369. {
  4370. start_off = window->point;
  4371. if (dir < 0)
  4372. /* Position before match to avoid finding same match
  4373. agin. */
  4374. start_off--;
  4375. }
  4376. }
  4377. }
  4378. else
  4379. {
  4380. /* Reverse the direction of the search. */
  4381. dir = -dir;
  4382. }
  4383. }
  4384. else if (func == &info_abort_key
  4385. && isearch_states_index && search_result != search_success)
  4386. {
  4387. /* If C-g pressed, and the search is failing, pop the search
  4388. stack back to the last unfailed search. */
  4389. terminal_ring_bell ();
  4390. while (isearch_states_index && search_result != search_success)
  4391. pop_isearch (window, &isearch_string_index, &dir,
  4392. &search_result, &start_off);
  4393. isearch_string[isearch_string_index] = '\0';
  4394. show_isearch_prompt (dir, (unsigned char *) isearch_string,
  4395. search_result);
  4396. continue;
  4397. }
  4398. else if (func == &info_abort_key || func == &ea_abort || !func)
  4399. {
  4400. /* The character is not printable, or it has a function which is
  4401. non-null. Exit the search, remembering the search string. */
  4402. if (isearch_string_index && func != &info_abort_key)
  4403. {
  4404. free (last_isearch_accepted);
  4405. last_isearch_accepted = xstrdup (isearch_string);
  4406. }
  4407. if (func == &info_abort_key)
  4408. {
  4409. if (isearch_states_index)
  4410. window_set_state (window, &orig_state);
  4411. }
  4412. if (!echo_area_is_active)
  4413. window_clear_echo_area ();
  4414. if (auto_footnotes_p)
  4415. info_get_or_remove_footnotes (active_window);
  4416. isearch_is_active = 0;
  4417. continue;
  4418. }
  4419. /* Show the new search string in the prompt. */
  4420. show_isearch_prompt (dir, (unsigned char *) isearch_string,
  4421. search_result);
  4422. /* Make the search case-sensitive only if the search string includes
  4423. upper-case letters. */
  4424. case_sensitive = 0;
  4425. for (p = isearch_string; *p; p++)
  4426. if (isupper (*p))
  4427. {
  4428. case_sensitive = 1;
  4429. break;
  4430. }
  4431. last_search_result = search_result;
  4432. window_get_state (window, &mystate);
  4433. search_result = info_search_internal (isearch_string,
  4434. window, dir, case_sensitive,
  4435. &start_off);
  4436. /* If this search failed, and we didn't already have a failed search,
  4437. then ring the terminal bell. */
  4438. if (search_result != search_success
  4439. && last_search_result == search_success)
  4440. {
  4441. terminal_ring_bell ();
  4442. }
  4443. else if (search_result == search_success)
  4444. {
  4445. /* Make sure the match is visible, and update the display. */
  4446. if (!strcmp(window->node->fullpath, mystate.fullpath)
  4447. && !strcmp(window->node->nodename, mystate.nodename)
  4448. && mystate.pagetop != window->pagetop)
  4449. {
  4450. int newtop = window->pagetop;
  4451. window->pagetop = mystate.pagetop;
  4452. set_window_pagetop (window, newtop);
  4453. }
  4454. window_adjust_pagetop (window);
  4455. /* Call display_update_display to update the window and an automatic
  4456. footnotes window if present. */
  4457. display_update_display ();
  4458. display_cursor_at_point (window);
  4459. }
  4460. }
  4461. /* Free the memory used to remember each search state. */
  4462. free_isearch_states ();
  4463. /* Alter the window history so that we have added at most one node in the
  4464. incremental search, so that going back once with "l" goes to where we
  4465. started the incremental search if the match was in a different node. */
  4466. {
  4467. int i = window->hist_index - 1;
  4468. int j = starting_history_entry;
  4469. if (i > j)
  4470. {
  4471. if (!strcmp(window->hist[i]->node->nodename,
  4472. window->hist[j]->node->nodename)
  4473. && !strcmp(window->hist[j]->node->fullpath,
  4474. window->hist[i]->node->fullpath))
  4475. {
  4476. /* If we end up at the same node we started at, don't extend
  4477. the history at all. */
  4478. cleanup_history (window, j, i);
  4479. }
  4480. else
  4481. {
  4482. cleanup_history (window, j + 1, i);
  4483. }
  4484. }
  4485. }
  4486. /* Perhaps GC some file buffers. */
  4487. gc_file_buffers_and_nodes ();
  4488. /* After searching, leave the window in the correct state. */
  4489. if (!echo_area_is_active)
  4490. window_clear_echo_area ();
  4491. }
  4492. /* **************************************************************** */
  4493. /* */
  4494. /* Miscellaneous Info Commands */
  4495. /* */
  4496. /* **************************************************************** */
  4497. /* What to do when C-g is pressed in a window. */
  4498. DECLARE_INFO_COMMAND (info_abort_key, _("Cancel current operation"))
  4499. {
  4500. /* If error printing doesn't oridinarily ring the bell, do it now,
  4501. since C-g always rings the bell. Otherwise, let the error printer
  4502. do it. */
  4503. if (!info_error_rings_bell_p)
  4504. terminal_ring_bell ();
  4505. info_error ("%s", _("Quit"));
  4506. info_initialize_numeric_arg ();
  4507. }
  4508. DECLARE_INFO_COMMAND (info_info_version, _("Display version of Info being run"))
  4509. {
  4510. window_message_in_echo_area (_("GNU Info version %s"), VERSION);
  4511. }
  4512. /* Clear the screen and redraw its contents. Given a numeric argument,
  4513. move the line the cursor is on to the COUNT'th line of the window. */
  4514. DECLARE_INFO_COMMAND (info_redraw_display, _("Redraw the display"))
  4515. {
  4516. if ((!info_explicit_arg && count == 1) || echo_area_is_active)
  4517. {
  4518. terminal_clear_screen ();
  4519. display_clear_display (the_display);
  4520. window_mark_chain (windows, W_UpdateWindow);
  4521. display_update_display ();
  4522. }
  4523. else
  4524. {
  4525. int desired_line, point_line;
  4526. int new_pagetop;
  4527. point_line = window_line_of_point (window) - window->pagetop;
  4528. if (count < 0)
  4529. desired_line = window->height + count;
  4530. else
  4531. desired_line = count;
  4532. if (desired_line < 0)
  4533. desired_line = 0;
  4534. if (desired_line >= window->height)
  4535. desired_line = window->height - 1;
  4536. if (desired_line == point_line)
  4537. return;
  4538. new_pagetop = window->pagetop + (point_line - desired_line);
  4539. set_window_pagetop (window, new_pagetop);
  4540. }
  4541. }
  4542. /* Exit from info */
  4543. DECLARE_INFO_COMMAND (info_quit, _("Quit using Info"))
  4544. {
  4545. if (window->next || window->prev)
  4546. info_delete_window (window, count);
  4547. else
  4548. quit_info_immediately = 1;
  4549. }
  4550. /* **************************************************************** */
  4551. /* */
  4552. /* Reading Keys and Dispatching on Them */
  4553. /* */
  4554. /* **************************************************************** */
  4555. DECLARE_INFO_COMMAND (info_do_lowercase_version,
  4556. _("Run command bound to this key's lowercase variant"))
  4557. {} /* Declaration only. */
  4558. static void
  4559. dispatch_error (int *keyseq)
  4560. {
  4561. char *rep;
  4562. rep = pretty_keyseq (keyseq);
  4563. if (!echo_area_is_active)
  4564. info_error (_("Unknown command (%s)."), rep);
  4565. else
  4566. {
  4567. char *temp = xmalloc (1 + strlen (rep) + strlen (_("\"%s\" is invalid")));
  4568. sprintf (temp, _("'%s' is invalid"), rep);
  4569. terminal_ring_bell ();
  4570. inform_in_echo_area (temp);
  4571. free (temp);
  4572. }
  4573. }
  4574. /* Keeping track of key sequences. */
  4575. static int *info_keyseq = NULL;
  4576. static int info_keyseq_index = 0;
  4577. static int info_keyseq_size = 0;
  4578. static int info_keyseq_displayed_p = 0;
  4579. /* Initialize the length of the current key sequence. */
  4580. void
  4581. initialize_keyseq (void)
  4582. {
  4583. info_keyseq_index = 0;
  4584. info_keyseq_displayed_p = 0;
  4585. }
  4586. /* Add CHARACTER to the current key sequence. */
  4587. void
  4588. add_char_to_keyseq (int character)
  4589. {
  4590. if (info_keyseq_index + 2 >= info_keyseq_size)
  4591. info_keyseq = xrealloc (info_keyseq,
  4592. sizeof (int) * (info_keyseq_size += 10));
  4593. info_keyseq[info_keyseq_index++] = character;
  4594. info_keyseq[info_keyseq_index] = '\0';
  4595. }
  4596. /* Display the current value of info_keyseq. If argument EXPECTING is
  4597. non-zero, input is expected to be read after the key sequence is
  4598. displayed, so add an additional prompting character to the sequence. */
  4599. static void
  4600. display_info_keyseq (int expecting_future_input)
  4601. {
  4602. char *rep;
  4603. if (!info_keyseq || info_keyseq_index == 0)
  4604. return;
  4605. rep = pretty_keyseq (info_keyseq);
  4606. if (expecting_future_input)
  4607. strcat (rep, "-");
  4608. if (echo_area_is_active)
  4609. inform_in_echo_area (rep);
  4610. else
  4611. {
  4612. window_message_in_echo_area (rep, NULL, NULL);
  4613. display_cursor_at_point (active_window);
  4614. }
  4615. info_keyseq_displayed_p = 1;
  4616. }
  4617. /* Called by interactive commands to read another key when keys have already
  4618. been read as part of the current command (and possibly displayed in status
  4619. line with display_info_keyseq). */
  4620. int
  4621. get_another_input_key (void)
  4622. {
  4623. int ready = !info_keyseq_displayed_p; /* ready if new and pending key */
  4624. /* If there isn't any input currently available, then wait a
  4625. moment looking for input. If we don't get it fast enough,
  4626. prompt a little bit with the current key sequence. */
  4627. if (!info_keyseq_displayed_p)
  4628. {
  4629. ready = 1;
  4630. if (!info_any_buffered_input_p ())
  4631. {
  4632. #if defined (FD_SET)
  4633. struct timeval timer;
  4634. fd_set readfds;
  4635. FD_ZERO (&readfds);
  4636. FD_SET (fileno (info_input_stream), &readfds);
  4637. timer.tv_sec = 1;
  4638. timer.tv_usec = 750;
  4639. ready = select (fileno(info_input_stream)+1, &readfds,
  4640. NULL, NULL, &timer);
  4641. #else
  4642. ready = 0;
  4643. #endif /* FD_SET */
  4644. }
  4645. }
  4646. if (!ready)
  4647. display_info_keyseq (1);
  4648. return get_input_key ();
  4649. }
  4650. /* Non-zero means that an explicit argument has been passed to this
  4651. command, as in C-u C-v. */
  4652. int info_explicit_arg = 0;
  4653. /* As above, but used when C-u is typed in the echo area to avoid
  4654. overwriting this information when "C-u ARG M-x" is typed. */
  4655. int ea_explicit_arg = 0;
  4656. void info_universal_argument (WINDOW *, int count);
  4657. void info_add_digit_to_numeric_arg (WINDOW *, int count);
  4658. /* Read a key sequence and look up its command in MAP. Handle C-u style
  4659. numeric args, as well as M--, and M-digits. Return argument in COUNT if it
  4660. is non-null.
  4661. Some commands can be executed directly, in which case null is returned
  4662. instead:
  4663. If MENU, call info_menu_digit on ACTIVE_WINDOW if a number key was
  4664. pressed.
  4665. If MOUSE, call mouse_event_handler if a mouse event occurred.
  4666. If INSERT, call ea_insert if a printable character was input.
  4667. */
  4668. VFunction *
  4669. read_key_sequence (Keymap map, int menu, int mouse,
  4670. int insert, int *count)
  4671. {
  4672. int key;
  4673. int reading_universal_argument = 0;
  4674. int numeric_arg = 1, numeric_arg_sign = 1, *which_explicit_arg;
  4675. VFunction *func;
  4676. /* Process the right numeric argument. */
  4677. if (!echo_area_is_active)
  4678. which_explicit_arg = &info_explicit_arg;
  4679. else
  4680. which_explicit_arg = &ea_explicit_arg;
  4681. *which_explicit_arg = 0;
  4682. initialize_keyseq ();
  4683. key = get_input_key ();
  4684. if (key == KEY_MOUSE)
  4685. {
  4686. if (mouse)
  4687. mouse_event_handler ();
  4688. return 0;
  4689. }
  4690. if (insert
  4691. && (key >= 040 && key < 0200
  4692. || ISO_Latin_p && key >= 0200 && key < 0400))
  4693. {
  4694. ea_insert (the_echo_area, 1, key);
  4695. return 0;
  4696. }
  4697. add_char_to_keyseq (key);
  4698. while (1)
  4699. {
  4700. int dash_typed = 0, digit_typed = 0;
  4701. func = 0;
  4702. if (display_was_interrupted_p && !info_any_buffered_input_p ())
  4703. display_update_display ();
  4704. if (active_window != the_echo_area)
  4705. display_cursor_at_point (active_window);
  4706. /* If reading a universal argument, both <digit> and M-<digit> help form
  4707. the argument. Don't look up the pressed key in the key map. */
  4708. if (reading_universal_argument)
  4709. {
  4710. int k = key;
  4711. if (k >= KEYMAP_META_BASE)
  4712. k -= KEYMAP_META_BASE;
  4713. if (k == '-')
  4714. {
  4715. dash_typed = 1;
  4716. }
  4717. else if (isdigit (k))
  4718. {
  4719. digit_typed = 1;
  4720. }
  4721. else
  4722. /* Note: we may still read another C-u after this. */
  4723. reading_universal_argument = 0;
  4724. }
  4725. if (!dash_typed && !digit_typed && map[key].type == ISFUNC)
  4726. {
  4727. func = map[key].value.function ? map[key].value.function->func : 0;
  4728. if (!func)
  4729. {
  4730. dispatch_error (info_keyseq);
  4731. return 0;
  4732. }
  4733. }
  4734. if (dash_typed || digit_typed || func == &info_add_digit_to_numeric_arg)
  4735. {
  4736. int k = key;
  4737. if (k > KEYMAP_META_BASE)
  4738. k -= KEYMAP_META_BASE;
  4739. reading_universal_argument = 1;
  4740. if (dash_typed || k == '-')
  4741. {
  4742. if (!*which_explicit_arg)
  4743. {
  4744. numeric_arg_sign = -1;
  4745. numeric_arg = 1;
  4746. }
  4747. }
  4748. else if (digit_typed || isdigit (k))
  4749. {
  4750. if (*which_explicit_arg)
  4751. numeric_arg = numeric_arg * 10 + (k - '0');
  4752. else
  4753. numeric_arg = (k - '0');
  4754. *which_explicit_arg = 1;
  4755. }
  4756. }
  4757. else if (func == info_do_lowercase_version)
  4758. {
  4759. int lowerkey;
  4760. if (key >= KEYMAP_META_BASE)
  4761. {
  4762. lowerkey = key;
  4763. lowerkey -= KEYMAP_META_BASE;
  4764. lowerkey = tolower (lowerkey);
  4765. lowerkey += KEYMAP_META_BASE;
  4766. }
  4767. else
  4768. lowerkey = tolower (key);
  4769. if (lowerkey == key)
  4770. {
  4771. dispatch_error (info_keyseq);
  4772. return 0;
  4773. }
  4774. key = lowerkey;
  4775. continue;
  4776. }
  4777. else if (func == &info_universal_argument)
  4778. {
  4779. /* Multiply by 4. */
  4780. /* TODO: Maybe C-u should also terminate the universal argument
  4781. sequence, as in Emacs. (C-u 6 4 C-u 1 inserts 64 1's.) */
  4782. if (!*which_explicit_arg)
  4783. numeric_arg *= 4;
  4784. reading_universal_argument = 1;
  4785. }
  4786. else if (menu && func == &info_menu_digit)
  4787. {
  4788. /* key can either be digit, or M-digit for --vi-keys. */
  4789. int k = key;
  4790. if (k > KEYMAP_META_BASE)
  4791. k -= KEYMAP_META_BASE;
  4792. window_clear_echo_area ();
  4793. menu_digit (active_window, k);
  4794. return 0;
  4795. }
  4796. else if (insert
  4797. && (func == &ea_possible_completions || func == &ea_complete)
  4798. && !echo_area_completion_items)
  4799. {
  4800. ea_insert (the_echo_area, 1, key);
  4801. return 0;
  4802. }
  4803. else if (func)
  4804. {
  4805. /* Don't update the key sequence if we have finished reading a key
  4806. sequence in the echo area. This means that a key sequence like
  4807. "C-u 2 Left" appears to take effect immediately, instead of there
  4808. being a delay while the message is displayed. */
  4809. if (!echo_area_is_active && info_keyseq_displayed_p)
  4810. display_info_keyseq (0);
  4811. if (count)
  4812. *count = numeric_arg * numeric_arg_sign;
  4813. /* *which_explicit_arg has not been set yet if only a sequence of
  4814. C-u's was typed (each of which has multiplied the argument by
  4815. four). */
  4816. if (*count != 1 && !*which_explicit_arg)
  4817. *which_explicit_arg = 1;
  4818. return func;
  4819. }
  4820. else if (map[key].type == ISKMAP)
  4821. {
  4822. if (map[key].value.keymap != NULL)
  4823. map = map[key].value.keymap;
  4824. else
  4825. {
  4826. dispatch_error (info_keyseq);
  4827. return 0;
  4828. }
  4829. if (info_keyseq_displayed_p)
  4830. display_info_keyseq (1);
  4831. }
  4832. do
  4833. key = get_another_input_key ();
  4834. while (key == KEY_MOUSE);
  4835. add_char_to_keyseq (key);
  4836. }
  4837. return 0;
  4838. }
  4839. /* Add the current digit to the argument in progress. */
  4840. DECLARE_INFO_COMMAND (info_add_digit_to_numeric_arg,
  4841. _("Add this digit to the current numeric argument"))
  4842. {} /* Declaration only. */
  4843. /* C-u, universal argument. Multiply the current argument by 4.
  4844. Read a key. If the key has nothing to do with arguments, then
  4845. dispatch on it. If the key is the abort character then abort. */
  4846. DECLARE_INFO_COMMAND (info_universal_argument,
  4847. _("Start (or multiply by 4) the current numeric argument"))
  4848. {} /* Declaration only. */
  4849. /* Create a default argument. */
  4850. void
  4851. info_initialize_numeric_arg (void)
  4852. {
  4853. if (!echo_area_is_active)
  4854. {
  4855. info_explicit_arg = 0;
  4856. }
  4857. else
  4858. {
  4859. ea_explicit_arg = 0;
  4860. }
  4861. }