woman.el 172 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604
  1. ;;; woman.el --- browse UN*X manual pages `wo (without) man'
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Francis J. Wright <F.J.Wright@qmul.ac.uk>
  4. ;; Maintainer: FSF
  5. ;; Keywords: help, unix
  6. ;; Adapted-By: Eli Zaretskii <eliz@gnu.org>
  7. ;; Version: 0.551
  8. ;; URL: http://centaur.maths.qmul.ac.uk/Emacs/WoMan/
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; WoMan implements a subset of the formatting performed by the Emacs
  22. ;; `man' (or `manual-entry') command to format a UN*X manual `page'
  23. ;; for display, but without calling any external programs. It is
  24. ;; intended to emulate the whole of the -man macro package, plus those
  25. ;; ?roff requests that are most commonly used in man pages. However,
  26. ;; the emulation is modified to include the reformatting done by the
  27. ;; Emacs `man' command. No hyphenation is performed.
  28. ;; Advantages
  29. ;; Much more direct, does not require any external programs.
  30. ;; Supports completion on man page names.
  31. ;; Disadvantages
  32. ;; Not a complete emulation. Currently no support for eqn or tbl.
  33. ;; Slightly slower for large man pages (but usually faster for
  34. ;; small- and medium-size pages).
  35. ;; This browser works quite well on simple well-written man files. It
  36. ;; works less well on idiosyncratic files that `break the rules' or
  37. ;; use the more obscure ?roff requests directly. Current test results
  38. ;; are available in the file woman.status.
  39. ;; WoMan supports the use of compressed man files via
  40. ;; `auto-compression-mode' by turning it on if necessary. But you may
  41. ;; need to adjust the user option `woman-file-compression-regexp'.
  42. ;; Read on for (currently) the only documentation for WoMan!
  43. ;; See also the documentation for the WoMan interactive commands and
  44. ;; user option variables, all of which begin with the prefix `woman-'.
  45. ;; This can be done most easily by loading WoMan and then running the
  46. ;; command `woman-mini-help', or selecting the WoMan menu option `Mini
  47. ;; Help' when WoMan is running.
  48. ;; WoMan is still under development! Please let me know what doesn't
  49. ;; work -- I am adding and improving functionality as testing shows
  50. ;; that it is necessary. See below for guidance on reporting bugs.
  51. ;; Recommended use
  52. ;; ===============
  53. ;; Put this in your .emacs:
  54. ;; (autoload 'woman "woman"
  55. ;; "Decode and browse a UN*X man page." t)
  56. ;; (autoload 'woman-find-file "woman"
  57. ;; "Find, decode and browse a specific UN*X man-page file." t)
  58. ;; Then either (1 -- *RECOMMENDED*): If the `MANPATH' environment
  59. ;; variable is set then WoMan will use it; otherwise you may need to
  60. ;; reset the Lisp variable `woman-manpath', and you may also want to
  61. ;; set the Lisp variable `woman-path'. Please see the online
  62. ;; documentation for these variables. Now you can execute the
  63. ;; extended command `woman', enter or select a manual entry topic,
  64. ;; using completion, and if necessary select a filename, using
  65. ;; completion. By default, WoMan suggests the word nearest to the
  66. ;; cursor in the current buffer as the topic.
  67. ;; Or (2): Execute the extended command `woman-find-file' and enter a
  68. ;; filename, using completion. This mode of execution may be useful
  69. ;; for temporary files outside the standard UN*X manual directory
  70. ;; structure.
  71. ;; Or (3): Put the next two sexpr's in your .emacs:
  72. ;; (autoload 'woman-dired-find-file "woman"
  73. ;; "In dired, run the WoMan man-page browser on this file." t)
  74. ;; (add-hook 'dired-mode-hook
  75. ;; (lambda ()
  76. ;; (define-key dired-mode-map "W" 'woman-dired-find-file)))
  77. ;; and open the directory containing the man page file using dired,
  78. ;; put the cursor on the file, and press `W'.
  79. ;; In each case, the result should (!) be a buffer in Man mode showing
  80. ;; a formatted manual entry. When called from WoMan, Man mode should
  81. ;; work as advertised, but modified where necessary in the context of
  82. ;; WoMan. (However, `Man' will still invoke the standard Emacs
  83. ;; manual-browsing facility rather than `WoMan' -- this is
  84. ;; intentional!)
  85. ;; (By default, WoMan will automatically define the dired keys "W" and
  86. ;; "w" when it loads, but only if they are not already defined. This
  87. ;; behavior is controlled by the user option `woman-dired-keys'.
  88. ;; Note that the `dired-x' (dired extra) package binds
  89. ;; `dired-copy-filename-as-kill' to the key "w" (as pointed out by Jim
  90. ;; Davidson), although "W" appears to be really unused. The `dired-x'
  91. ;; package will over-write the WoMan binding to "w", whereas (by
  92. ;; default) WoMan will not overwrite the `dired-x' binding.)
  93. ;; The following is based on suggestions by Guy Gascoigne-Piggford and
  94. ;; Juanma Barranquero. If you really want to square the man-woman
  95. ;; circle then you might care to define the following bash function in
  96. ;; .bashrc:
  97. ;; man() { gnudoit -q '(raise-frame (selected-frame)) (woman' \"$1\" ')' ; }
  98. ;; If you use Microsoft COMMAND.COM then you can create a file called
  99. ;; man.bat somewhere in your path containing the two lines:
  100. ;; @echo off
  101. ;; gnudoit -q (raise-frame (selected-frame)) (woman \"%1\")
  102. ;; and then (e.g. from a command prompt or the Run... option in the
  103. ;; Start menu) just execute
  104. ;; man man_page_name
  105. ;; Using the word at point as the default topic
  106. ;; ============================================
  107. ;; The `woman' command uses the word nearest to point in the current
  108. ;; buffer as the default topic to look up if it matches the name of a
  109. ;; manual page installed on the system. The default topic can also be
  110. ;; used without confirmation by setting the user-option
  111. ;; `woman-use-topic-at-point' to t; thanks to Benjamin Riefenstahl for
  112. ;; suggesting this functionality.
  113. ;; The variable `woman-use-topic-at-point' can be rebound locally,
  114. ;; which may be useful to provide special private key bindings, e.g.
  115. ;; (global-set-key "\C-cw"
  116. ;; (lambda ()
  117. ;; (interactive)
  118. ;; (let ((woman-use-topic-at-point t))
  119. ;; (woman)))))
  120. ;; Customization, Hooks and Imenu
  121. ;; ==============================
  122. ;; WoMan supports the GNU Emacs 20+ customization facility, and puts
  123. ;; a customization group called `WoMan' in the `Help' group under the
  124. ;; top-level `Emacs' group. In order to be able to customize WoMan
  125. ;; without first loading it, add the following sexp to your .emacs:
  126. ;; (defgroup woman nil
  127. ;; "Browse UNIX manual pages `wo (without) man'."
  128. ;; :tag "WoMan" :group 'help :load "woman")
  129. ;; WoMan currently runs two hooks: `woman-pre-format-hook' immediately
  130. ;; before formatting a buffer and `woman-post-format-hook' immediately
  131. ;; after formatting a buffer. These hooks can be used for special
  132. ;; customizations that require code to be executed, etc., although
  133. ;; most customization should be possible by setting WoMan user option
  134. ;; variables, e.g. in `.emacs' and should NOT require the use of the
  135. ;; hooks. `woman-pre-format-hook' might be appropriate for face
  136. ;; customization, whereas `woman-post-format-hook' might be
  137. ;; appropriate for installing a dynamic menu using `imenu' (although
  138. ;; it is better to use the built-in WoMan imenu support).
  139. ;; The WoMan menu provides an option to make a contents menu for the
  140. ;; current man page (using imenu). Alternatively, if you set the
  141. ;; variable `woman-imenu' to `t' then WoMan will do it automatically
  142. ;; for every man page. The menu title is the value of the variable
  143. ;; `woman-imenu-title', which is "CONTENTS" by default. By default,
  144. ;; the menu shows manual sections and subsections, but you can change
  145. ;; this by changing the value of `woman-imenu-generic-expression'.
  146. ;; This facility is not yet widely tested and may be fooled by obscure
  147. ;; man pages that `break the rules'.
  148. ;; WoMan is configured not to replace spaces in an imenu *Completion*
  149. ;; buffer. For further documentation of the use of imenu, such as
  150. ;; menu sorting, see the source file imenu.el, which is distributed
  151. ;; with GNU Emacs.
  152. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  153. ;; Howard Melman made (essentially) the following suggestions, which
  154. ;; are slightly different from the expression that I currently use.
  155. ;; You may prefer one of Howard's suggestions, which I think assume
  156. ;; that `case-fold-search' is `t' (which it is by default):
  157. ;; (setq woman-imenu-generic-expression
  158. ;; '((nil "^\\( \\)?\\([A-Z][A-Z ]+[A-Z]\\)[ \t]*$" 2)))
  159. ;; will give support for .SH and .SS, though it won't show the heading
  160. ;; name hierarchy. If you just want .SH in the imenu then use:
  161. ;; (setq woman-imenu-generic-expression
  162. ;; '((nil "^\\([A-Z][A-Z ]+[A-Z]\\)[ \t]*$" 1)))
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;; Vertical spacing and blank lines
  165. ;; ================================
  166. ;; The number of consecutive blank lines in the formatted buffer
  167. ;; should be either 0 or 1. A blank line should leave a space like
  168. ;; .sp 1 (p. 14). Current policy is to output vertical space only
  169. ;; immediately before text is output.
  170. ;; Horizontal and vertical spacing and resolution
  171. ;; ==============================================
  172. ;; WoMan currently assumes 10 characters per inch horizontally, hence
  173. ;; a horizontal resolution of 24 basic units, and 5 lines per inch
  174. ;; vertically, hence a vertical resolution of 48 basic units. (nroff
  175. ;; uses 240 per inch).
  176. ;; The *WoMan-Log* buffer
  177. ;; ======================
  178. ;; This is modeled on the byte-compiler. It logs all files formatted
  179. ;; by WoMan, and if WoMan finds anything that it cannot handle then it
  180. ;; writes a warning to this buffer. If the variable `woman-show-log'
  181. ;; is non-nil (by default it is `nil') then WoMan automatically
  182. ;; displays this buffer. Many WoMan warnings can be completely
  183. ;; ignored, because they are reporting the fact that WoMan has ignored
  184. ;; requests that it is correct to ignore. In some future version this
  185. ;; level of paranoia will be reduced, but not until WoMan is more
  186. ;; reliable. At present, all warnings should be treated with some
  187. ;; suspicion. Uninterpreted escape sequences are also logged (in some
  188. ;; cases).
  189. ;; Uninterpreted ?roff requests can optionally be left in the
  190. ;; formatted buffer to indicate precisely where they occur by
  191. ;; resetting the variable `woman-ignore' to `nil' (by default it is
  192. ;; `t').
  193. ;; Automatic initiation of woman decoding
  194. ;; (Probably not a good idea. If you use it, be careful!)
  195. ;; Put something like this in your .emacs. The call to
  196. ;; set-visited-file-name is to avoid font-locking triggered by
  197. ;; automatic major mode selection.
  198. ;; (autoload 'woman-decode-region "woman")
  199. ;; (setq format-alist
  200. ;; (cons
  201. ;; '(man "UN*X man-page source format" "\\.\\(TH\\|ig\\) "
  202. ;; woman-decode-region nil nil
  203. ;; (lambda (arg)
  204. ;; set-visited-file-name
  205. ;; (file-name-sans-extension buffer-file-name)))))
  206. ;; format-alist))
  207. ;; Reporting Bugs
  208. ;; ==============
  209. ;; If WoMan fails completely, or formats a file incorrectly
  210. ;; (i.e. obviously wrongly or significantly differently from man) or
  211. ;; inelegantly, then please
  212. ;; (a) check that you are running the latest version of woman.el
  213. ;; available from my web site (see above),
  214. ;; (b) check that the problem is not already described in the file
  215. ;; woman.status, also available from my web site.
  216. ;; If both of the above are true then please email me the entry from
  217. ;; the *WoMan-Log* buffer relating to the problem file, together with
  218. ;; a brief description of the problem. Please indicate where you got
  219. ;; the source file from, but do not send it to me unless I ask you to!
  220. ;; Thanks. (There is at present no automated bug-reporting facility
  221. ;; for WoMan.)
  222. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  223. ;; NOTE:
  224. ;; CASE-DEPENDENCE OF FILENAMES. By default, WoMan ignores case in
  225. ;; file pathnames only when it seems appropriate. MS-Windows users
  226. ;; who want complete case independence should set the NTEmacs variable
  227. ;; `w32-downcase-file-names' to `t' and use all lower case when
  228. ;; setting WoMan file paths.
  229. ;; (1) INCOMPATIBLE CHANGE! WoMan no longer uses a persistent topic
  230. ;; cache by default. (It caused too much confusion!) Explicitly set
  231. ;; the variable `woman-cache-filename' to save the cache between Emacs
  232. ;; sessions. This is recommended only if the command `woman' is too
  233. ;; slow the first time that it is run in an Emacs session, while it
  234. ;; builds its cache in main memory, which MAY be VERY slow.
  235. ;; (2) The user option `woman-cache-level' controls the amount of
  236. ;; information cached (in main memory and, optionally, saved to disc).
  237. ;; (3) UPDATING THE CACHE. A prefix argument always causes the
  238. ;; `woman' command (only) to rebuild its topic cache, and to re-save
  239. ;; it to `woman-cache-filename' if this variable has a non-nil value.
  240. ;; This is necessary if the NAMES (not contents) of any of the
  241. ;; directories or files in the paths specified by `woman-manpath' or
  242. ;; `woman-path' change. If WoMan user options that affect the cache
  243. ;; are changed then WoMan will automatically update its cache file on
  244. ;; disc (if one is in use) the next time it is run in a new Emacs
  245. ;; session.
  246. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  247. ;; TO DO
  248. ;; =====
  249. ;; Reconsider case sensitivity of file names.
  250. ;; MUST PROCESS .if, .nr IN ORDER ENCOUNTERED IN FILE! (rcsfile, mf).
  251. ;; Allow general delimiter in `\v', cf. `\h'.
  252. ;; Improve major-mode documentation.
  253. ;; Pre-process conditionals in macro bodies if possible for speed?
  254. ;; Emulate more complete preprocessor support for tbl (.TS/.TE)
  255. ;; Emulate some preprocessor support for eqn (.EQ/.EN)
  256. ;; Re-write filling and adjusting code!
  257. ;; Allow word wrap at comma (for long option lists)?
  258. ;; Buffer list handling not quite right.
  259. ;; Make 10 or 12 pitch (cpi) optional -- 12 => ll = 78
  260. ;; Use unpaddable space for tabbing?
  261. ;; Tidy up handling of fonts when filling and adjusting
  262. ;; -- see text/text properties?
  263. ;; Improve speed
  264. ;; Add font-lock support (for quoted strings, etc.)?
  265. ;; Optionally save large files in enriched format?
  266. ;; Add apropos facility by searching NAME (?) entry in man files?
  267. ;; Documentation -- optional auto-display of formatted WoMan man page?
  268. ;; Implement a bug reporter?
  269. ;; Support diversion and traps (to some extent) - for Tcl/tk pages?
  270. ;; Add a menu of WoMan buffers?
  271. ;; Fix .fc properly?
  272. ;; Implementation strategy [this description is now well out of date!]
  273. ;; -- three main passes, each to process respectively:
  274. ;; 1) non-breaking `.' requests including font macros
  275. ;; 2) \ escape sequences, mainly special characters and font changes
  276. ;; 3) breaking `.' requests, mainly filling and justification
  277. ;; For each pass, a control function finds and pre-processes the
  278. ;; escape or request and then calls the appropriate function to
  279. ;; perform the required formatting. Based originally on enriched.el
  280. ;; and format.el.
  281. ;; The background information that made this project possible is
  282. ;; freely available courtesy of Bell Labs from
  283. ;; http://cm.bell-labs.com/7thEdMan/
  284. ;; Acknowledgements
  285. ;; ================
  286. ;; For Heather, Kathryn and Madelyn, the women in my life
  287. ;; (although they will probably never use it)!
  288. ;; I also thank the following for helpful suggestions, bug reports,
  289. ;; code fragments, general interest, etc.:
  290. ;; Jari Aalto <jari.aalto@cs.tpu.fi>
  291. ;; Dean Andrews <dean@dra.com>
  292. ;; Juanma Barranquero <lekktu@gmail.com>
  293. ;; Karl Berry <kb@cs.umb.edu>
  294. ;; Jim Chapman <jchapman@netcomuk.co.uk>
  295. ;; Kin Cho <kin@neoscale.com>
  296. ;; Frederic Corne <frederic.corne@erli.fr>
  297. ;; Peter Craft <craft@alacritech.com>
  298. ;; Charles Curley <ccurley@trib.com>
  299. ;; Jim Davidson <jdavidso@teknowledge.com>
  300. ;; Kevin D'Elia <Kevin.DElia@mci.com>
  301. ;; John Fitch <jpff@maths.bath.ac.uk>
  302. ;; Hans Frosch <jwfrosch@rish.b17c.ingr.com>
  303. ;; Guy Gascoigne-Piggford <ggp@informix.com>
  304. ;; Brian Gorka <gorkab@sanchez.com>
  305. ;; Nicolai Henriksen <nhe@lyngso-industri.dk>
  306. ;; Thomas Herchenroeder <the@software-ag.de>
  307. ;; Alexander Hinds <ahinds@thegrid.net>
  308. ;; Stefan Hornburg <sth@hacon.de>
  309. ;; Theodore Jump <tjump@cais.com>
  310. ;; David Kastrup <dak@gnu.org>
  311. ;; Paul Kinnucan <paulk@mathworks.com>
  312. ;; Jonas Linde <jonas@init.se>
  313. ;; Andrew McRae <andrewm@optimation.co.nz>
  314. ;; Howard Melman <howard@silverstream.com>
  315. ;; Dennis Pixton <dennis@math.binghamton.edu>
  316. ;; T. V. Raman <raman@Adobe.COM>
  317. ;; Bruce Ravel <bruce.ravel@nist.gov>
  318. ;; Benjamin Riefenstahl <benny@crocodial.de>
  319. ;; Kevin Ruland <kruland@seistl.com>
  320. ;; Tom Schutter <tom@platte.com>
  321. ;; Wei-Xue Shi <wxshi@ma.neweb.ne.jp>
  322. ;; Fabio Somenzi <fabio@joplin.colorado.edu>
  323. ;; Karel Sprenger <ks@ic.uva.nl>
  324. ;; Chris Szurgot <szurgot@itribe.net>
  325. ;; Paul A. Thompson <pat@po.cwru.edu>
  326. ;; Arrigo Triulzi <arrigo@maths.qmw.ac.uk>
  327. ;; Geoff Voelker <voelker@cs.washington.edu>
  328. ;; Eli Zaretskii <eliz@gnu.org>
  329. ;;; Code:
  330. (defvar woman-version "0.551 (beta)" "WoMan version information.")
  331. (require 'man)
  332. (require 'button)
  333. (define-button-type 'WoMan-xref-man-page
  334. :supertype 'Man-abstract-xref-man-page
  335. 'func (lambda (arg)
  336. (woman
  337. ;; `woman' cannot deal with arguments that contain a
  338. ;; section name, like close(2), so strip the section name.
  339. (if (string-match Man-reference-regexp arg)
  340. (substring arg 0 (match-end 1))
  341. arg))))
  342. (eval-when-compile ; to avoid compiler warnings
  343. (require 'dired)
  344. (require 'cl)
  345. (require 'apropos))
  346. (defun woman-mapcan (fn x)
  347. "Return concatenated list of FN applied to successive `car' elements of X.
  348. FN must return a list, cons or nil. Useful for splicing into a list."
  349. ;; Based on the Standard Lisp function MAPCAN but with args swapped!
  350. ;; More concise implementation than the recursive one. -- dak
  351. (apply #'nconc (mapcar fn x)))
  352. (defun woman-parse-colon-path (paths)
  353. "Explode search path string PATHS into a list of directory names.
  354. Allow Cygwin colon-separated search paths on Microsoft platforms.
  355. Replace null components by calling `woman-parse-man.conf'.
  356. As a special case, if PATHS is nil then replace it by calling
  357. `woman-parse-man.conf'."
  358. ;; Based on suggestions by Jari Aalto and Eli Zaretskii.
  359. ;; parse-colon-path returns nil for a null path component and
  360. ;; an empty substring of MANPATH denotes the default list.
  361. (if (memq system-type '(windows-nt ms-dos))
  362. (cond ((null paths)
  363. (mapcar 'woman-Cyg-to-Win (woman-parse-man.conf)))
  364. ((string-match ";" paths)
  365. ;; Assume DOS-style path-list...
  366. (woman-mapcan ; splice list into list
  367. (lambda (x)
  368. (if x
  369. (list x)
  370. (mapcar 'woman-Cyg-to-Win (woman-parse-man.conf))))
  371. (parse-colon-path paths)))
  372. ((string-match "\\`[a-zA-Z]:" paths)
  373. ;; Assume single DOS-style path...
  374. (list paths))
  375. (t
  376. ;; Assume UNIX/Cygwin-style path-list...
  377. (woman-mapcan ; splice list into list
  378. (lambda (x)
  379. (mapcar 'woman-Cyg-to-Win
  380. (if x (list x) (woman-parse-man.conf))))
  381. (let ((path-separator ":"))
  382. (parse-colon-path paths)))))
  383. ;; Assume host-default-style path-list...
  384. (woman-mapcan ; splice list into list
  385. (lambda (x) (if x (list x) (woman-parse-man.conf)))
  386. (parse-colon-path (or paths "")))))
  387. (defun woman-Cyg-to-Win (file)
  388. "Convert an absolute filename FILE from Cygwin to Windows form."
  389. ;; MANPATH_MAP conses are not converted since they presumably map
  390. ;; Cygwin to Cygwin form.
  391. (if (consp file)
  392. file
  393. ;; Code taken from w32-symlinks.el
  394. (if (eq (aref file 0) ?/)
  395. ;; Try to use Cygwin mount table via `cygpath.exe'.
  396. (condition-case nil
  397. (with-temp-buffer
  398. ;; cygpath -m file
  399. (call-process "cygpath" nil t nil "-m" file)
  400. (buffer-substring 1 (buffer-size)))
  401. (error
  402. ;; Assume no `cygpath' program available.
  403. ;; Hack /cygdrive/x/ or /x/ or (obsolete) //x/ to x:/
  404. (when (string-match "\\`\\(/cygdrive\\|/\\)?/./" file)
  405. (if (match-beginning 1) ; /cygdrive/x/ or //x/ -> /x/
  406. (setq file (substring file (match-end 1))))
  407. (aset file 0 (aref file 1)) ; /x/ -> xx/
  408. (aset file 1 ?:)) ; xx/ -> x:/
  409. file))
  410. file)))
  411. ;;; User options:
  412. ;; NB: Group identifiers must be lowercase!
  413. (defgroup woman nil
  414. "Browse UNIX manual pages `wo (without) man'."
  415. :tag "WoMan"
  416. :group 'help)
  417. (defcustom woman-show-log nil
  418. "If non-nil then show the *WoMan-Log* buffer if appropriate.
  419. I.e. if any warning messages are written to it. Default is nil."
  420. :type 'boolean
  421. :group 'woman)
  422. (defcustom woman-pre-format-hook nil
  423. "Hook run by WoMan immediately before formatting a buffer.
  424. Change only via `Customization' or the function `add-hook'."
  425. :type 'hook
  426. :group 'woman)
  427. (defcustom woman-post-format-hook nil
  428. "Hook run by WoMan immediately after formatting a buffer.
  429. Change only via `Customization' or the function `add-hook'."
  430. :type 'hook
  431. :group 'woman)
  432. ;; Interface options
  433. (defgroup woman-interface nil
  434. "Interface options for browsing UNIX manual pages `wo (without) man'."
  435. :tag "WoMan Interface"
  436. :group 'woman)
  437. (defcustom woman-man.conf-path
  438. (let ((path '("/usr/lib" "/etc")))
  439. (cond ((eq system-type 'windows-nt)
  440. (mapcar 'woman-Cyg-to-Win path))
  441. ((eq system-type 'darwin)
  442. (cons "/usr/share/misc" path))
  443. (t path)))
  444. "List of dirs to search and/or files to try for man config file.
  445. A trailing separator (`/' for UNIX etc.) on directories is
  446. optional, and the filename is used if a directory specified is
  447. the first to start with \"man\" and has an extension starting
  448. with \".conf\". If MANPATH is not set but a config file is found
  449. then it is parsed instead to provide a default value for
  450. `woman-manpath'."
  451. :type '(repeat string)
  452. :group 'woman-interface)
  453. (defun woman-parse-man.conf ()
  454. "Parse if possible configuration file for man command.
  455. Used only if MANPATH is not set or contains null components.
  456. Look in `woman-man.conf-path' and return a value for `woman-manpath'.
  457. Concatenate data from all lines in the config file of the form
  458. MANPATH /usr/man
  459. or
  460. MANDATORY_MANPATH /usr/man
  461. or
  462. OPTIONAL_MANPATH /usr/man
  463. or
  464. MANPATH_MAP /opt/bin /opt/man"
  465. ;; Functionality suggested by Charles Curley.
  466. (let ((path woman-man.conf-path)
  467. file manpath)
  468. (while (and
  469. path
  470. (not (and
  471. (file-readable-p (setq file (car path)))
  472. ;; If not a file then find the file:
  473. (or (not (file-directory-p file))
  474. (and
  475. (setq file
  476. (directory-files file t "\\`man.*\\.conf[a-z]*\\'" t))
  477. (file-readable-p (setq file (car file)))))
  478. ;; Parse the file -- if no MANPATH data ignore it:
  479. (with-temp-buffer
  480. (insert-file-contents file)
  481. (while (re-search-forward
  482. ;; `\(?: ... \)' is a "shy group"
  483. "\
  484. ^[ \t]*\\(?:\\(?:MANDATORY_\\|OPTIONAL_\\)?MANPATH[ \t]+\\(\\S-+\\)\\|\
  485. MANPATH_MAP[ \t]+\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\)" nil t)
  486. (add-to-list 'manpath
  487. (if (match-beginning 1)
  488. (match-string 1)
  489. (cons (match-string 2)
  490. (match-string 3)))))
  491. manpath))
  492. ))
  493. (setq path (cdr path)))
  494. (nreverse manpath)))
  495. ;; Autoload so set-locale-environment can operate on it.
  496. ;;;###autoload
  497. (defcustom woman-locale nil
  498. "String specifying a manual page locale, or nil.
  499. If a manual page is available in the specified locale
  500. \(e.g. \"sv_SE.ISO8859-1\"), it will be offered in preference to the
  501. default version. Normally, `set-locale-environment' sets this at startup."
  502. :type '(choice string (const nil))
  503. :group 'woman-interface
  504. :version "23.1")
  505. ;; FIXME Is this a sensible list of alternatives?
  506. (defun woman-expand-locale (locale)
  507. "Expand a locale into a list suitable for man page lookup.
  508. Expands a locale of the form LANGUAGE_TERRITORY.CHARSET into the list:
  509. LANGUAGE_TERRITORY.CHARSET LANGUAGE_TERRITORY LANGUAGE.CHARSET LANGUAGE.
  510. The TERRITORY and CHARSET portions may be absent."
  511. (string-match "\\([^._]*\\)\\(_[^.]*\\)?\\(\\..*\\)?" locale)
  512. (let ((lang (match-string 1 locale))
  513. (terr (match-string 2 locale))
  514. (charset (match-string 3 locale)))
  515. (delq nil (list locale
  516. (and charset terr (concat lang terr))
  517. (and charset terr (concat lang charset))
  518. (if (or charset terr) lang)))))
  519. (defun woman-manpath-add-locales (manpath)
  520. "Add locale-specific subdirectories to the elements of MANPATH.
  521. MANPATH is a list of the form of `woman-manpath'. Returns a list
  522. with those locale-specific subdirectories specified by the action
  523. of `woman-expand-locale' on `woman-locale' added, where they exist."
  524. (if (zerop (length woman-locale))
  525. manpath
  526. (let ((subdirs (woman-expand-locale woman-locale))
  527. lst dir)
  528. (dolist (elem manpath (nreverse lst))
  529. (dolist (sub subdirs)
  530. (when (file-directory-p
  531. (setq dir
  532. ;; Use f-n-a-d because parse-colon-path does.
  533. (file-name-as-directory
  534. (expand-file-name sub (substitute-in-file-name
  535. (if (consp elem)
  536. (cdr elem)
  537. elem))))))
  538. (add-to-list 'lst (if (consp elem)
  539. (cons (car elem) dir)
  540. dir))))
  541. ;; Non-locale-specific has lowest precedence.
  542. (add-to-list 'lst elem)))))
  543. (defcustom woman-manpath
  544. ;; Locales could also be added in woman-expand-directory-path.
  545. (or (woman-manpath-add-locales
  546. (woman-parse-colon-path (getenv "MANPATH")))
  547. '("/usr/man" "/usr/share/man" "/usr/local/man"))
  548. "List of DIRECTORY TREES to search for UN*X manual files.
  549. Each element should be the name of a directory that contains
  550. subdirectories of the form `man?', or more precisely subdirectories
  551. selected by the value of `woman-manpath-man-regexp'. Non-directory
  552. and unreadable files are ignored.
  553. Elements can also be a cons cell indicating a mapping from PATH
  554. to manual trees: if such an element's car is equal to a path
  555. element of the environment variable PATH, the cdr of the cons
  556. cell is included in the directory tree search.
  557. If not set then the environment variable MANPATH is used. If no such
  558. environment variable is found, the default list is determined by
  559. consulting the man configuration file if found, which is determined by
  560. the user option `woman-man.conf-path'. An empty substring of MANPATH
  561. denotes the default list.
  562. Any environment variables (names must have the UN*X-style form $NAME,
  563. e.g. $HOME, $EMACSDATA, $emacs_dir) are evaluated first but each
  564. element must evaluate to a SINGLE directory name. Trailing `/'s are
  565. ignored. (Specific directories in `woman-path' are also searched.)
  566. Microsoft platforms:
  567. I recommend including drive letters explicitly, e.g.
  568. (\"C:/Cygwin/usr/man/\" \"C:/Cygwin/usr/local/man\").
  569. The MANPATH environment variable may be set using DOS semi-colon-
  570. separated or UN*X/Cygwin colon-separated syntax (but not mixed)."
  571. :type '(repeat (choice string (cons string string)))
  572. :version "23.1" ; added woman-manpath-add-locales
  573. :group 'woman-interface)
  574. (defcustom woman-manpath-man-regexp "[Mm][Aa][Nn]"
  575. "Regexp to match man directories UNDER `woman-manpath' directories.
  576. These normally have names of the form `man?'. Its default value is
  577. \"[Mm][Aa][Nn]\", which is case-insensitive mainly for the benefit of
  578. Microsoft platforms. Its purpose is to avoid `cat?', `.', `..', etc."
  579. ;; Based on a suggestion by Wei-Xue Shi.
  580. :type 'string
  581. :group 'woman-interface)
  582. (defcustom woman-path
  583. (if (eq system-type 'ms-dos) '("$DJDIR/info" "$DJDIR/man/cat[1-9onlp]"))
  584. "List of SPECIFIC DIRECTORIES to search for UN*X manual files.
  585. For example
  586. (\"/emacs/etc\").
  587. These directories are searched in addition to the directory trees
  588. specified in `woman-manpath'. Each element should be a directory
  589. string or nil, which represents the current directory when the path is
  590. expanded and cached. However, the last component (only) of each
  591. directory string is treated as a regexp \(Emacs, not shell) and the
  592. string is expanded into a list of matching directories. Non-directory
  593. and unreadable files are ignored. The default value is nil.
  594. Any environment variables (which must have the UN*X-style form $NAME,
  595. e.g. $HOME, $EMACSDATA, $emacs_dir) are evaluated first but each
  596. element must evaluate to a SINGLE directory name (regexp, see above).
  597. For example
  598. (\"$EMACSDATA\") [or equivalently (\"$emacs_dir/etc\")].
  599. Trailing `/'s are discarded. (The directory trees in `woman-manpath'
  600. are also searched.) On Microsoft platforms I recommend including
  601. drive letters explicitly."
  602. :type '(repeat (choice string (const nil)))
  603. :group 'woman-interface)
  604. (defcustom woman-cache-level 2
  605. "The level of topic caching.
  606. 1 - cache only the topic and directory lists
  607. (the only level before version 0.34 - only for compatibility);
  608. 2 - cache also the directories for each topic
  609. (faster, without using much more memory);
  610. 3 - cache also the actual filenames for each topic
  611. (fastest, but uses twice as much memory).
  612. The default value is currently 2, a good general compromise.
  613. If the `woman' command is slow to find files then try 3, which may be
  614. particularly beneficial with large remote-mounted man directories.
  615. Run the `woman' command with a prefix argument or delete the cache
  616. file `woman-cache-filename' for a change to take effect.
  617. \(Values < 1 behave like 1; values > 3 behave like 3.)"
  618. :type '(choice (const :tag "Minimal" 1)
  619. (const :tag "Default" 2)
  620. (const :tag "Maximal" 3))
  621. :group 'woman-interface)
  622. (defcustom woman-cache-filename nil
  623. "The full pathname of the WoMan directory and topic cache file.
  624. It is used to save and restore the cache between sessions. This is
  625. especially useful with remote-mounted man page files! The default
  626. value of nil suppresses this action. The `standard' non-nil
  627. filename is \"~/.wmncach.el\". Remember that a prefix argument forces
  628. the `woman' command to update and re-write the cache."
  629. :type '(choice (const :tag "None" nil)
  630. (const :tag "~/.wmncach.el" "~/.wmncach.el")
  631. file)
  632. :group 'woman-interface)
  633. (defcustom woman-dired-keys t
  634. "List of `dired' mode keys to define to run WoMan on current file.
  635. E.g. '(\"w\" \"W\"), or any non-null atom to automatically define
  636. \"w\" and \"W\" if they are unbound, or nil to do nothing.
  637. Default is t."
  638. :type '(choice (const :tag "None" nil)
  639. (repeat string)
  640. (other :tag "Auto" t))
  641. :group 'woman-interface)
  642. (defcustom woman-imenu-generic-expression
  643. '((nil "\n\\([A-Z].*\\)" 1) ; SECTION, but not TITLE
  644. ("*Subsections*" "^ \\([A-Z].*\\)" 1))
  645. "Imenu support for Sections and Subsections.
  646. An alist with elements of the form (MENU-TITLE REGEXP INDEX) --
  647. see the documentation for `imenu-generic-expression'."
  648. :type 'sexp
  649. :group 'woman-interface)
  650. (defcustom woman-imenu nil
  651. "If non-nil then WoMan adds a Contents menu to the menubar.
  652. It does this by calling `imenu-add-to-menubar'. Default is nil."
  653. :type 'boolean
  654. :group 'woman-interface)
  655. (defcustom woman-imenu-title "CONTENTS"
  656. "The title to use if WoMan adds a Contents menu to the menubar.
  657. Default is \"CONTENTS\"."
  658. :type 'string
  659. :group 'woman-interface)
  660. (defcustom woman-use-topic-at-point-default nil
  661. ;; `woman-use-topic-at-point' may be let-bound when woman is loaded,
  662. ;; in which case its global value does not get defined.
  663. ;; `woman-file-name' sets it to this value if it is unbound.
  664. "Default value for `woman-use-topic-at-point'."
  665. :type '(choice (const :tag "Yes" t)
  666. (const :tag "No" nil))
  667. :group 'woman-interface)
  668. (defcustom woman-use-topic-at-point woman-use-topic-at-point-default
  669. "Control use of the word at point as the default topic.
  670. If non-nil the `woman' command uses the word at point automatically,
  671. without interactive confirmation, if it exists as a topic."
  672. :type '(choice (const :tag "Yes" t)
  673. (const :tag "No" nil))
  674. :group 'woman-interface)
  675. (defvar woman-file-regexp nil
  676. "Regexp used to select (possibly compressed) man source files, e.g.
  677. \"\\.\\([0-9lmnt]\\w*\\)\\(\\.\\(g?z\\|bz2\\|xz\\)\\)?\\'\".
  678. Built automatically from the customizable user options
  679. `woman-uncompressed-file-regexp' and `woman-file-compression-regexp'.")
  680. (defvar woman-uncompressed-file-regexp) ; for the compiler
  681. (defvar woman-file-compression-regexp) ; for the compiler
  682. (defun set-woman-file-regexp (symbol value)
  683. "Bind SYMBOL to VALUE and set `woman-file-regexp' as per user customizations.
  684. Used as :set cookie by Customize when customizing the user options
  685. `woman-uncompressed-file-regexp' and `woman-file-compression-regexp'."
  686. (set-default symbol value)
  687. (and (boundp 'woman-uncompressed-file-regexp)
  688. (boundp 'woman-file-compression-regexp)
  689. (setq woman-file-regexp
  690. (concat woman-uncompressed-file-regexp
  691. "\\("
  692. (substring woman-file-compression-regexp 0 -2)
  693. "\\)?\\'"))))
  694. (defcustom woman-uncompressed-file-regexp
  695. "\\.\\([0-9lmnt]\\w*\\)" ; disallow no extension
  696. "Do not change this unless you are sure you know what you are doing!
  697. Regexp used to select man source files (ignoring any compression extension).
  698. The SysV standard man pages use two character suffixes, and this is
  699. becoming more common in the GNU world. For example, the man pages
  700. in the ncurses package include `toe.1m', `form.3x', etc.
  701. Note: an optional compression regexp will be appended, so this regexp
  702. MUST NOT end with any kind of string terminator such as $ or \\'."
  703. :type 'regexp
  704. :set 'set-woman-file-regexp
  705. :group 'woman-interface)
  706. (defcustom woman-file-compression-regexp
  707. "\\.\\(g?z\\|bz2\\|xz\\)\\'"
  708. "Do not change this unless you are sure you know what you are doing!
  709. Regexp used to match compressed man file extensions for which
  710. decompressors are available and handled by auto-compression mode,
  711. e.g. \"\\\\.\\\\(g?z\\\\|bz2\\\\|xz\\\\)\\\\'\" for `gzip', `bzip2', or `xz'.
  712. Should begin with \\. and end with \\' and MUST NOT be optional."
  713. ;; Should be compatible with car of
  714. ;; `jka-compr-file-name-handler-entry', but that is unduly
  715. ;; complicated, includes an inappropriate extension (.tgz) and is
  716. ;; not loaded by default!
  717. :version "24.1" ; added xz
  718. :type 'regexp
  719. :set 'set-woman-file-regexp
  720. :group 'woman-interface)
  721. (defcustom woman-use-own-frame nil
  722. "If non-nil then use a dedicated frame for displaying WoMan windows.
  723. Only useful when run on a graphic display such as X or MS-Windows."
  724. :type 'boolean
  725. :group 'woman-interface)
  726. ;; Formatting options
  727. (defgroup woman-formatting nil
  728. "Formatting options for browsing UNIX manual pages `wo (without) man'."
  729. :tag "WoMan Formatting"
  730. :group 'woman)
  731. (defcustom woman-fill-column 65
  732. "Right margin for formatted text -- default is 65."
  733. :type 'integer
  734. :group 'woman-formatting)
  735. (defcustom woman-fill-frame nil
  736. ;; Based loosely on a suggestion by Theodore Jump:
  737. "If non-nil then most of the window width is used."
  738. :type 'boolean
  739. :group 'woman-formatting)
  740. (defcustom woman-default-indent 5
  741. "Default prevailing indent set by -man macros -- default is 5.
  742. Set this variable to 7 to emulate GNU man formatting."
  743. :type 'integer
  744. :group 'woman-formatting)
  745. (defcustom woman-bold-headings t
  746. "If non-nil then embolden section and subsection headings. Default is t.
  747. Heading emboldening is NOT standard `man' behavior."
  748. :type 'boolean
  749. :group 'woman-formatting)
  750. (defcustom woman-ignore t
  751. "If non-nil then unrecognized requests etc. are ignored. Default is t.
  752. This gives the standard ?roff behavior. If nil then they are left in
  753. the buffer, which may aid debugging."
  754. :type 'boolean
  755. :group 'woman-formatting)
  756. (defcustom woman-preserve-ascii t
  757. "If non-nil, preserve ASCII characters in the WoMan buffer.
  758. Otherwise, to save time, some backslashes and spaces may be
  759. represented differently (as the values of the variables
  760. `woman-escaped-escape-char' and `woman-unpadded-space-char'
  761. respectively) so that the buffer content is strictly wrong even though
  762. it should display correctly. This should be irrelevant unless the
  763. buffer text is searched, copied or saved to a file."
  764. ;; This option should probably be removed!
  765. :type 'boolean
  766. :group 'woman-formatting)
  767. (defcustom woman-emulation 'nroff
  768. "WoMan emulation, currently either nroff or troff. Default is nroff.
  769. Troff emulation is experimental and largely untested.
  770. \(Add groff later?)"
  771. :type '(choice (const nroff) (const troff))
  772. :group 'woman-formatting)
  773. ;; Faces:
  774. (defgroup woman-faces nil
  775. "Face options for browsing UNIX manual pages `wo (without) man'."
  776. :tag "WoMan Faces"
  777. :group 'woman
  778. :group 'faces)
  779. (defcustom woman-fontify
  780. (or (and (fboundp 'display-color-p) (display-color-p))
  781. (and (fboundp 'display-graphic-p) (display-graphic-p))
  782. (x-display-color-p))
  783. "If non-nil then WoMan assumes that face support is available.
  784. It defaults to a non-nil value if the display supports either colors
  785. or different fonts."
  786. :type 'boolean
  787. :group 'woman-faces)
  788. (defface woman-italic
  789. '((t :inherit italic))
  790. "Face for italic font in man pages."
  791. :group 'woman-faces)
  792. (define-obsolete-face-alias 'woman-italic-face 'woman-italic "22.1")
  793. (defface woman-bold
  794. '((t :inherit bold))
  795. "Face for bold font in man pages."
  796. :group 'woman-faces)
  797. (define-obsolete-face-alias 'woman-bold-face 'woman-bold "22.1")
  798. (defface woman-unknown
  799. '((t :inherit font-lock-warning-face))
  800. "Face for all unknown fonts in man pages."
  801. :group 'woman-faces)
  802. (define-obsolete-face-alias 'woman-unknown-face 'woman-unknown "22.1")
  803. (defface woman-addition
  804. '((t :inherit font-lock-builtin-face))
  805. "Face for all WoMan additions to man pages."
  806. :group 'woman-faces)
  807. (define-obsolete-face-alias 'woman-addition-face 'woman-addition "22.1")
  808. (defun woman-default-faces ()
  809. "Set foreground colors of italic and bold faces to their default values."
  810. (interactive)
  811. (face-spec-set 'woman-italic (face-user-default-spec 'woman-italic))
  812. (face-spec-set 'woman-bold (face-user-default-spec 'woman-bold)))
  813. (defun woman-monochrome-faces ()
  814. "Set foreground colors of italic and bold faces to that of the default face.
  815. This is usually either black or white."
  816. (interactive)
  817. (set-face-foreground 'woman-italic 'unspecified)
  818. (set-face-foreground 'woman-bold 'unspecified))
  819. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  820. ;; Experimental font support, initially only for MS-Windows.
  821. (defconst woman-font-support
  822. (eq window-system 'w32) ; Support X later!
  823. "If non-nil then non-ASCII characters and symbol font supported.")
  824. (defun woman-select-symbol-fonts (fonts)
  825. "Select symbol fonts from a list FONTS of font name strings."
  826. (let (symbol-fonts)
  827. ;; With NTEmacs 20.5, the PATTERN option to `x-list-fonts' does
  828. ;; not seem to work and fonts may be repeated, so ...
  829. (dolist (font fonts)
  830. (and (string-match "-Symbol-" font)
  831. (not (member font symbol-fonts))
  832. (setq symbol-fonts (cons font symbol-fonts))))
  833. symbol-fonts))
  834. (declare-function x-list-fonts "xfaces.c"
  835. (pattern &optional face frame maximum width))
  836. (when woman-font-support
  837. (make-face 'woman-symbol)
  838. ;; Set the symbol font only if `woman-use-symbol-font' is true, to
  839. ;; avoid unnecessarily upsetting the line spacing in NTEmacs 20.5!
  840. (defcustom woman-use-extended-font t
  841. "If non-nil then may use non-ASCII characters from the default font."
  842. :type 'boolean
  843. :group 'woman-faces)
  844. (defcustom woman-use-symbol-font nil
  845. "If non-nil then may use the symbol font.
  846. It is off by default, mainly because it may change the line spacing
  847. \(in NTEmacs 20.5)."
  848. :type 'boolean
  849. :group 'woman-faces)
  850. (defconst woman-symbol-font-list
  851. (or (woman-select-symbol-fonts (x-list-fonts "*" 'default))
  852. (woman-select-symbol-fonts (x-list-fonts "*")))
  853. "Symbol font(s), preferably same size as default when WoMan was loaded.")
  854. (defcustom woman-symbol-font (car woman-symbol-font-list)
  855. "A string describing the symbol font to use for special characters.
  856. It should be compatible with, and the same size as, the default text font.
  857. Under MS-Windows, the default is
  858. \"-*-Symbol-normal-r-*-*-*-*-96-96-p-*-ms-symbol\"."
  859. :type `(choice
  860. ,@(mapcar (lambda (x) (list 'const x))
  861. woman-symbol-font-list)
  862. string)
  863. :group 'woman-faces)
  864. )
  865. ;; For non windows-nt ...
  866. (defvar woman-use-extended-font nil)
  867. (defvar woman-use-symbol-font nil)
  868. (defvar woman-symbol-font nil)
  869. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  870. ;;; Internal variables:
  871. (defconst woman-justify-list
  872. '(left right center full)
  873. "Justify styles for `fill-region-as-paragraph'.")
  874. (defconst woman-adjust-left 0 ; == adjust off, noadjust
  875. "Adjustment indicator `l' -- adjust left margin only.")
  876. (defconst woman-adjust-right 1
  877. "Adjustment indicator `r' -- adjust right margin only.")
  878. (defconst woman-adjust-center 2
  879. "Adjustment indicator `c' -- center.")
  880. (defconst woman-adjust-both 3 ; default -- adj,both
  881. "Adjustment indicator `b' or `n' -- adjust both margins.")
  882. (defvar woman-adjust woman-adjust-both
  883. "Current adjustment number-register value.")
  884. (defvar woman-adjust-previous woman-adjust
  885. "Previous adjustment number-register value.")
  886. (defvar woman-justify
  887. (nth woman-adjust woman-justify-list) ; use vector?
  888. "Current justification style for `fill-region-as-paragraph'.")
  889. (defvar woman-justify-previous woman-justify
  890. "Previous justification style for `fill-region-as-paragraph'.")
  891. (defvar woman-left-margin woman-default-indent
  892. "Current left margin.")
  893. (defvar woman-prevailing-indent woman-default-indent
  894. "Current prevailing indent.")
  895. (defvar woman-interparagraph-distance 1
  896. "Interparagraph distance in lines.
  897. Set by .PD; used by .SH, .SS, .TP, .LP, .PP, .P, .IP, .HP.")
  898. (defvar woman-leave-blank-lines nil
  899. "Blank lines to leave as vertical space.")
  900. (defconst woman-tab-width 5
  901. "Default tab width set by -man macros.")
  902. (defvar woman-nofill nil
  903. "Current fill mode: nil for filling.")
  904. (defvar woman-RS-left-margin nil
  905. "Left margin stack for nested use of `.RS/.RE'.")
  906. (defvar woman-RS-prevailing-indent nil
  907. "Prevailing indent stack for nested use of `.RS/.RE'.")
  908. (defvar woman-nospace nil
  909. "Current no-space mode: nil for normal spacing.
  910. Set by `.ns' request; reset by any output or `.rs' request")
  911. ;; Used for message logging
  912. (defvar WoMan-current-file nil) ; bound in woman-really-find-file
  913. (defvar WoMan-Log-header-point-max nil)
  914. (defsubst woman-reset-nospace ()
  915. "Set `woman-nospace' to nil."
  916. (setq woman-nospace nil))
  917. (defconst woman-request-regexp "^[.'][ \t]*\\(\\S +\\) *"
  918. ;; Was "^\\.[ \t]*\\([a-z0-9]+\\) *" but cvs.1 uses a macro named
  919. ;; "`" and CGI.man uses a macro named "''"!
  920. ;; CGI.man uses ' as control character in places -- it *should*
  921. ;; suppress breaks!
  922. ;; Could end with "\\( +\\|$\\)" instead of " *"
  923. "Regexp to match a ?roff request plus trailing white space.")
  924. (defvar woman-imenu-done nil
  925. "Buffer-local: set to true if function `woman-imenu' has been called.")
  926. (make-variable-buffer-local 'woman-imenu-done)
  927. ;; From imenu.el -- needed when reformatting a file in its old buffer.
  928. ;; The latest buffer index used to update the menu bar menu.
  929. (eval-when-compile
  930. (require 'imenu))
  931. (make-variable-buffer-local 'imenu--last-menubar-index-alist)
  932. (defvar woman-buffer-alist nil
  933. "An alist representing WoMan buffers that are already decoded.
  934. Each element is of the form (FILE-NAME . BUFFER-NAME).")
  935. (defvar woman-buffer-number 0
  936. "Ordinal number of current buffer entry in `woman-buffer-alist'.
  937. The ordinal numbers start from 0.")
  938. (defvar woman-if-conditions-true '(?n ?e ?o)
  939. "List of one-character built-in condition names that are true.
  940. Should include ?e, ?o (page even/odd) and either ?n (nroff) or ?t (troff).
  941. Default is '(?n ?e ?o). Set via `woman-emulation'.")
  942. ;;; Specialized utility functions:
  943. ;;; Fast deletion without saving on the kill ring (cf. simple.el):
  944. (defun woman-delete-line (&optional arg)
  945. "Delete rest of current line; if all blank then delete thru newline.
  946. With a numeric argument ARG, delete that many lines from point.
  947. Negative arguments delete lines backward."
  948. ;; This is a non-interactive version of kill-line in simple.el that
  949. ;; deletes instead of killing and assumes kill-whole-line is nil,
  950. ;; which is essential!
  951. (delete-region (point)
  952. (progn
  953. (if arg
  954. (forward-line arg)
  955. (if (eobp)
  956. (signal 'end-of-buffer nil))
  957. (if (looking-at "[ \t]*$")
  958. (forward-line 1)
  959. (end-of-line)))
  960. (point))))
  961. (defsubst woman-delete-whole-line ()
  962. "Delete current line from beginning including eol."
  963. (beginning-of-line)
  964. (woman-delete-line 1))
  965. (defsubst woman-delete-following-space ()
  966. "Delete all spaces and tabs FOLLOWING point (cf. `delete-horizontal-space')."
  967. ;; cf. delete-horizontal-space in simple.el:
  968. (delete-region (point) (progn (skip-chars-forward " \t") (point))))
  969. (defsubst woman-delete-match (subexp)
  970. "Delete subexpression SUBEXP of buffer text matched by last search."
  971. (delete-region (match-beginning subexp) (match-end subexp)))
  972. ;; delete-char does not kill by default
  973. ;; delete-backward-char does not kill by default
  974. ;; delete-horizontal-space does not kill
  975. ;; delete-blank-lines does not kill
  976. ;;; File handling:
  977. (defvar woman-expanded-directory-path nil
  978. "Expanded directory list cache. Resetting to nil forces update.")
  979. (defvar woman-topic-all-completions nil
  980. "Expanded topic alist cache. Resetting to nil forces update.")
  981. ;;;###autoload
  982. (defun woman (&optional topic re-cache)
  983. "Browse UN*X man page for TOPIC (Without using external Man program).
  984. The major browsing mode used is essentially the standard Man mode.
  985. Choose the filename for the man page using completion, based on the
  986. topic selected from the directories specified in `woman-manpath' and
  987. `woman-path'. The directory expansions and topics are cached for
  988. speed, but a non-nil interactive argument forces the caches to be
  989. updated (e.g. to re-interpret the current directory).
  990. Used non-interactively, arguments are optional: if given then TOPIC
  991. should be a topic string and non-nil RE-CACHE forces re-caching."
  992. (interactive (list nil current-prefix-arg))
  993. ;; The following test is for non-interactive calls via gnudoit etc.
  994. (if (or (not (stringp topic)) (string-match "\\S " topic))
  995. (let ((file-name (woman-file-name topic re-cache)))
  996. (if file-name
  997. (woman-find-file file-name)
  998. (message
  999. "WoMan Error: No matching manual files found in search path")
  1000. (ding)))
  1001. (message "WoMan Error: No topic specified in non-interactive call")
  1002. (ding)))
  1003. ;; Allow WoMan to be called via the standard Help menu:
  1004. (define-key-after menu-bar-manuals-menu [woman]
  1005. '(menu-item "Read Man Page (WoMan)..." woman
  1006. :help "Man-page documentation Without Man") t)
  1007. (defvar woman-cached-data nil
  1008. "A list of cached data used to determine cache validity.
  1009. Set from the cache by `woman-read-directory-cache'.")
  1010. (defun woman-cached-data ()
  1011. "Generate a list of data used to determine cache validity.
  1012. Called both to generate and to check the cache!"
  1013. ;; Must use substituted paths because values of env vars may change!
  1014. (list woman-cache-level
  1015. (let (lst path)
  1016. (dolist (dir woman-manpath (nreverse lst))
  1017. (when (consp dir)
  1018. (unless path
  1019. (setq path
  1020. (split-string (getenv "PATH") path-separator t)))
  1021. (setq dir (and (member (car dir) path) (cdr dir))))
  1022. (when dir (add-to-list 'lst (substitute-in-file-name dir)))))
  1023. (mapcar 'substitute-in-file-name woman-path)))
  1024. (defun woman-read-directory-cache ()
  1025. "Load the directory and topic cache.
  1026. It is loaded from the file named by the variable `woman-cache-filename'.
  1027. Return t if the file exists, nil otherwise."
  1028. (and
  1029. woman-cache-filename
  1030. (load woman-cache-filename t nil t) ; file exists
  1031. (equal woman-cached-data (woman-cached-data)))) ; cache valid
  1032. (defun woman-write-directory-cache ()
  1033. "Save the directory and topic cache.
  1034. It is saved to the file named by the variable `woman-cache-filename'."
  1035. (if woman-cache-filename
  1036. (with-current-buffer (generate-new-buffer "WoMan tmp buffer")
  1037. ;; Make a temporary buffer; name starting with space "hides" it.
  1038. (let ((standard-output (current-buffer))
  1039. (backup-inhibited t))
  1040. ;; (switch-to-buffer standard-output t) ; only for debugging
  1041. (buffer-disable-undo standard-output)
  1042. (princ
  1043. ";;; WoMan directory and topic cache -- generated automatically\n")
  1044. (print
  1045. ;; For data validity check:
  1046. `(setq woman-cached-data ',(woman-cached-data)))
  1047. (print
  1048. `(setq woman-expanded-directory-path
  1049. ',woman-expanded-directory-path))
  1050. (print
  1051. `(setq woman-topic-all-completions
  1052. ',woman-topic-all-completions))
  1053. (write-file woman-cache-filename) ; write CURRENT buffer
  1054. (kill-buffer standard-output)
  1055. ))))
  1056. (defvaralias 'woman-topic-history 'Man-topic-history)
  1057. (defvar woman-file-history nil "File-name read history.")
  1058. (defun woman-file-name (topic &optional re-cache)
  1059. "Get the name of the UN*X man-page file describing a chosen TOPIC.
  1060. When `woman' is called interactively, the word at point may be
  1061. automatically used as the topic, if the value of the user option
  1062. `woman-use-topic-at-point' is non-nil. Return nil if no file can
  1063. be found. Optional argument RE-CACHE, if non-nil, forces the
  1064. cache to be re-read."
  1065. ;; Handle the caching of the directory and topic lists:
  1066. (unless (and (not re-cache)
  1067. (or
  1068. (and woman-expanded-directory-path woman-topic-all-completions)
  1069. (woman-read-directory-cache)))
  1070. (message "Building list of manual directory expansions...")
  1071. (setq woman-expanded-directory-path
  1072. (woman-expand-directory-path woman-manpath woman-path))
  1073. (message "Building completion list of all manual topics...")
  1074. (setq woman-topic-all-completions
  1075. (woman-topic-all-completions woman-expanded-directory-path))
  1076. (woman-write-directory-cache))
  1077. ;; There is a problem in that I want to offer case-insensitive
  1078. ;; completions, but to return only a case-sensitive match. This
  1079. ;; does not seem to work properly by default, so I re-do the
  1080. ;; completion if necessary.
  1081. (let (files)
  1082. (or (stringp topic)
  1083. (and (if (boundp 'woman-use-topic-at-point)
  1084. woman-use-topic-at-point
  1085. ;; Was let-bound when file loaded, so ...
  1086. (setq woman-use-topic-at-point woman-use-topic-at-point-default))
  1087. (setq topic (or (current-word t) "")) ; only within or adjacent to word
  1088. (test-completion topic woman-topic-all-completions))
  1089. (setq topic
  1090. (let* ((word-at-point (current-word))
  1091. (default
  1092. (when (and word-at-point
  1093. (test-completion
  1094. word-at-point woman-topic-all-completions))
  1095. word-at-point)))
  1096. (completing-read
  1097. (if default
  1098. (format "Manual entry (default %s): " default)
  1099. "Manual entry: ")
  1100. woman-topic-all-completions nil 1
  1101. nil
  1102. 'woman-topic-history
  1103. default))))
  1104. ;; Note that completing-read always returns a string.
  1105. (unless (= (length topic) 0)
  1106. (cond
  1107. ((setq files (woman-file-name-all-completions topic)))
  1108. ;; Complete topic more carefully, i.e. use the completion
  1109. ;; rather than the string entered by the user:
  1110. ((setq files (all-completions topic woman-topic-all-completions))
  1111. (while (/= (length topic) (length (car files)))
  1112. (setq files (cdr files)))
  1113. (setq files (woman-file-name-all-completions (car files)))))
  1114. (cond
  1115. ((null files) nil) ; no file found for topic.
  1116. ((null (cdr files)) (car (car files))) ; only 1 file for topic.
  1117. (t
  1118. ;; Multiple files for topic, so must select 1.
  1119. ;; Unread the command event (TAB = ?\t = 9) that runs the command
  1120. ;; `minibuffer-complete' in order to automatically complete the
  1121. ;; minibuffer contents as far as possible.
  1122. (setq unread-command-events '(9)) ; and delete any type-ahead!
  1123. (completing-read "Manual file: " files nil 1
  1124. (try-completion "" files) 'woman-file-history))))))
  1125. (defun woman-select (predicate list)
  1126. "Select unique elements for which PREDICATE is true in LIST.
  1127. \(Note that this function changes the value of LIST.)"
  1128. ;; Intended to be fast by avoiding recursion and list copying.
  1129. (while (and list
  1130. (or
  1131. (member (car list) (cdr list))
  1132. (not (funcall predicate (car list)))))
  1133. (setq list (cdr list)))
  1134. (if list
  1135. (let ((newlist list) cdr_list)
  1136. (while (setq cdr_list (cdr list))
  1137. (if (and
  1138. (not (member (car cdr_list) (cdr cdr_list)))
  1139. (funcall predicate (car cdr_list)))
  1140. (setq list cdr_list)
  1141. (setcdr list (cdr cdr_list))))
  1142. newlist)))
  1143. (defun woman-file-readable-p (dir)
  1144. "Return t if DIR is readable, otherwise log a warning."
  1145. (or (file-readable-p dir)
  1146. (WoMan-warn "Ignoring unreadable `manpath' directory tree `%s'!" dir)))
  1147. (defun woman-directory-files (head dir)
  1148. "Return a sorted list of files in directory HEAD matching regexp in DIR.
  1149. Value is a sorted list of the absolute pathnames of all the files in
  1150. directory HEAD, or the current directory if HEAD is nil, that match the
  1151. regexp that is the final component of DIR. Log a warning if list is empty."
  1152. (or (directory-files
  1153. (or head (directory-file-name default-directory)) ; was "."
  1154. t
  1155. (file-name-nondirectory dir))
  1156. (WoMan-warn "No directories match `woman-path' entry `%s'!" dir)))
  1157. (defun woman-file-accessible-directory-p (dir)
  1158. "Return t if DIR is accessible, otherwise log a warning."
  1159. (or (file-accessible-directory-p dir)
  1160. (WoMan-warn "Ignoring inaccessible `man-page' directory `%s'!" dir)))
  1161. (defun woman-expand-directory-path (path-dirs path-regexps)
  1162. "Expand the manual directories in PATH-DIRS and PATH-REGEXPS.
  1163. PATH-DIRS should be a list of general manual directories (like
  1164. `woman-manpath'), while PATH-REGEXPS should be a list of specific
  1165. manual directory regexps (like `woman-path').
  1166. Ignore any paths that are unreadable or not directories."
  1167. ;; Allow each path to be a single string or a list of strings:
  1168. (if (not (listp path-dirs)) (setq path-dirs (list path-dirs)))
  1169. (if (not (listp path-regexps)) (setq path-regexps (list path-regexps)))
  1170. (let (head dirs path)
  1171. (dolist (dir path-dirs)
  1172. (when (consp dir)
  1173. (unless path
  1174. (setq path (split-string (getenv "PATH") path-separator t)))
  1175. (setq dir (and (member (car dir) path)
  1176. (cdr dir))))
  1177. (if (and dir (woman-file-readable-p dir))
  1178. ;; NB: `parse-colon-path' creates null elements for
  1179. ;; redundant (semi-)colons and trailing `/'s!
  1180. ;; If does not actually matter here if dir ends with `/'.
  1181. ;; Need regexp "man" here to avoid "cat?", `.', `..', etc.
  1182. (setq dir (woman-canonicalize-dir dir)
  1183. dirs (nconc dirs (directory-files
  1184. dir t woman-manpath-man-regexp)))))
  1185. (dolist (dir path-regexps)
  1186. (if (or (null dir)
  1187. (null (setq dir (woman-canonicalize-dir dir)
  1188. head (file-name-directory dir)))
  1189. (woman-file-readable-p head))
  1190. (setq dirs
  1191. (if dir
  1192. (nconc dirs (woman-directory-files head dir))
  1193. (cons (directory-file-name default-directory) dirs))
  1194. ;; was "." -- at head of list for later filtering
  1195. )))
  1196. (woman-select 'woman-file-accessible-directory-p dirs)))
  1197. (defun woman-canonicalize-dir (dir)
  1198. "Canonicalize the directory name DIR.
  1199. Any UN*X-style environment variables are evaluated first."
  1200. (setq dir (expand-file-name (substitute-in-file-name dir)))
  1201. ;; A path that ends with / matches all directories in it,
  1202. ;; including `.' and `..', so remove any trailing / !!!
  1203. (if (string= (substring dir -1) "/")
  1204. (setq dir (substring dir 0 -1)))
  1205. (if (memq system-type '(windows-nt ms-dos cygwin)) ; what else?
  1206. ;; Match capitalization used by `file-name-directory':
  1207. (setq dir (concat (file-name-directory dir)
  1208. (file-name-nondirectory dir))))
  1209. dir)
  1210. (defsubst woman-not-member (dir path)
  1211. "Return t if DIR is not a member of the list PATH, nil otherwise.
  1212. If DIR is `.' it is first replaced by the current directory."
  1213. (not (member dir path)))
  1214. (defun woman-topic-all-completions (path)
  1215. "Return an alist of the man files in all man directories in the list PATH.
  1216. The cdr of each alist element is the path-index / filename."
  1217. ;; Support 3 levels of caching: each element of the alist `files'
  1218. ;; will be a list of the first `woman-cache-level' elements of the
  1219. ;; following list: (topic path-index filename). This alist `files'
  1220. ;; is re-processed by `woman-topic-all-completions-merge'.
  1221. (let (dir files (path-index 0)) ; indexing starts at zero
  1222. (while path
  1223. (setq dir (pop path))
  1224. (if (woman-not-member dir path) ; use each directory only once!
  1225. (push (woman-topic-all-completions-1 dir path-index)
  1226. files))
  1227. (setq path-index (1+ path-index)))
  1228. ;; Uniquify topics:
  1229. ;; Concatenate all lists with a single nconc call to
  1230. ;; avoid retraversing the first lists repeatedly -- dak
  1231. (woman-topic-all-completions-merge
  1232. (apply #'nconc files))))
  1233. (defun woman-topic-all-completions-1 (dir path-index)
  1234. "Return an alist of the man topics in directory DIR with index PATH-INDEX.
  1235. A topic is a filename sans type-related extensions.
  1236. Support 3 levels of caching: each element of the alist will be a list
  1237. of the first `woman-cache-level' elements from the following list:
  1238. \(topic path-index filename)."
  1239. ;; This function used to check that each file in the directory was
  1240. ;; not itself a directory, but this is very slow and should be
  1241. ;; unnecessary. So let us assume that `woman-file-regexp' will
  1242. ;; filter out any directories, which probably should not be there
  1243. ;; anyway, i.e. it is a user error!
  1244. ;;
  1245. ;; Don't sort files: we do that when merging, anyway. -- dak
  1246. (let (newlst (lst (directory-files dir nil woman-file-regexp t))
  1247. ;; Make an explicit regexp for stripping extension and
  1248. ;; compression extension: file-name-sans-extension is a
  1249. ;; far too costly function. -- dak
  1250. (ext (format "\\(\\.[^.\\/]*\\)?\\(%s\\)?\\'"
  1251. woman-file-compression-regexp)))
  1252. ;; Use a loop instead of mapcar in order to avoid the speed
  1253. ;; penalty of binding function arguments. -- dak
  1254. (dolist (file lst newlst)
  1255. (push
  1256. (cons
  1257. (if (string-match ext file)
  1258. (substring file 0 (match-beginning 0))
  1259. file)
  1260. (and (> woman-cache-level 1)
  1261. (cons
  1262. path-index
  1263. (and (> woman-cache-level 2)
  1264. (list file)))))
  1265. newlst))))
  1266. (defun woman-topic-all-completions-merge (alist)
  1267. "Merge the alist ALIST so that the keys are unique.
  1268. Also make each path-info component into a list.
  1269. \(Note that this function changes the value of ALIST.)"
  1270. ;; Replaces unreadably "optimized" O(n^2) implementation.
  1271. ;; Instead we use sorting to merge stuff efficiently. -- dak
  1272. (let (newalist)
  1273. ;; Sort list into reverse order
  1274. (setq alist (sort alist (lambda(x y) (string< (car y) (car x)))))
  1275. ;; merge duplicate keys.
  1276. (if (> woman-cache-level 1)
  1277. (dolist (elt alist)
  1278. (if (equal (car elt) (caar newalist))
  1279. (unless (member (cdr elt) (cdar newalist))
  1280. (setcdr (car newalist) (cons (cdr elt)
  1281. (cdar newalist))))
  1282. (setcdr elt (list (cdr elt)))
  1283. (push elt newalist)))
  1284. ;; woman-cache-level = 1 => elements are single-element lists ...
  1285. (dolist (elt alist)
  1286. (unless (equal (car elt) (caar newalist))
  1287. (push elt newalist))))
  1288. newalist))
  1289. (defun woman-file-name-all-completions (topic)
  1290. "Return an alist of the files in all man directories that match TOPIC."
  1291. ;; Support 3 levels of caching: each element of
  1292. ;; woman-topic-all-completions is a list of one of the forms:
  1293. ;; (topic)
  1294. ;; (topic (path-index) (path-index) ... )
  1295. ;; (topic (path-index filename) (path-index filename) ... )
  1296. ;; where there are no duplicates in the value lists.
  1297. ;; Topic must match first `word' of filename, so ...
  1298. (let ((topic-regexp
  1299. (concat
  1300. "\\`" (regexp-quote topic) ; first `word'
  1301. "\\(\\..+\\)*" ; optional subsequent `words'
  1302. woman-file-regexp)) ; extension
  1303. (topics woman-topic-all-completions)
  1304. (path woman-expanded-directory-path)
  1305. dir files)
  1306. (if (cdr (car topics))
  1307. ;; Use cached path-info to locate files for each topic:
  1308. (let ((path-info (cdr (assoc topic topics)))
  1309. filename)
  1310. (dolist (elt path-info)
  1311. (setq dir (nth (car elt) path)
  1312. filename (car (cdr elt))
  1313. files (nconc files
  1314. ;; Find the actual file name:
  1315. (if filename
  1316. (list (concat dir "/" filename))
  1317. (directory-files dir t topic-regexp)
  1318. )))))
  1319. ;; Search path for the files for each topic:
  1320. (while path
  1321. (setq dir (car path)
  1322. path (cdr path))
  1323. (if (woman-not-member dir path) ; use each directory only once!
  1324. (setq files (nconc files
  1325. (directory-files dir t topic-regexp))))))
  1326. (mapcar 'list files)))
  1327. ;;; dired support
  1328. (defun woman-dired-define-key (key)
  1329. "Bind the argument KEY to the command `woman-dired-find-file'."
  1330. (define-key dired-mode-map key 'woman-dired-find-file))
  1331. (defsubst woman-dired-define-key-maybe (key)
  1332. "If KEY is undefined in Dired, bind it to command `woman-dired-find-file'."
  1333. (if (or (eq (lookup-key dired-mode-map key) 'undefined)
  1334. (null (lookup-key dired-mode-map key)))
  1335. (woman-dired-define-key key)))
  1336. (defun woman-dired-define-keys ()
  1337. "Define dired keys to run WoMan according to `woman-dired-keys'."
  1338. (if woman-dired-keys
  1339. (if (listp woman-dired-keys)
  1340. (mapc 'woman-dired-define-key woman-dired-keys)
  1341. (woman-dired-define-key-maybe "w")
  1342. (woman-dired-define-key-maybe "W")))
  1343. (define-key-after (lookup-key dired-mode-map [menu-bar immediate])
  1344. [woman] '("Read Man Page (WoMan)" . woman-dired-find-file) 'view))
  1345. (if (featurep 'dired)
  1346. (woman-dired-define-keys)
  1347. (add-hook 'dired-mode-hook 'woman-dired-define-keys))
  1348. ;;;###autoload
  1349. (defun woman-dired-find-file ()
  1350. "In dired, run the WoMan man-page browser on this file."
  1351. (interactive)
  1352. ;; dired-get-filename is defined in dired.el
  1353. (woman-find-file (dired-get-filename)))
  1354. ;;; tar-mode support
  1355. (defvar global-font-lock-mode) ; defined in font-core.el
  1356. (defun woman-tar-extract-file ()
  1357. "In tar mode, run the WoMan man-page browser on this file."
  1358. (interactive)
  1359. (or (eq major-mode 'tar-mode)
  1360. (error "`woman-tar-extract-file' can be used only in `tar-mode'"))
  1361. (buffer-disable-undo)
  1362. (let (global-font-lock-mode)
  1363. (funcall (symbol-function 'tar-extract)) ; defined in tar-mode
  1364. (let ((WoMan-current-file buffer-file-name)) ; used for message logging
  1365. (rename-buffer
  1366. (woman-make-bufname (file-name-nondirectory buffer-file-name)))
  1367. (woman-process-buffer)
  1368. (goto-char (point-min)))))
  1369. ;; There is currently no `tar-mode-hook' so use ...
  1370. (eval-after-load "tar-mode"
  1371. '(progn
  1372. (define-key tar-mode-map "w" 'woman-tar-extract-file)
  1373. (define-key-after (lookup-key tar-mode-map [menu-bar immediate])
  1374. [woman] '("Read Man Page (WoMan)" . woman-tar-extract-file) 'view)))
  1375. (defvar woman-last-file-name nil
  1376. "The full pathname of the last file formatted by WoMan.")
  1377. (defun woman-reformat-last-file ()
  1378. "Reformat last file, e.g. after changing fill column."
  1379. (interactive)
  1380. (if woman-last-file-name
  1381. (woman-find-file woman-last-file-name t)
  1382. (call-interactively 'woman-find-file)))
  1383. ;;;###autoload
  1384. (defun woman-find-file (file-name &optional reformat)
  1385. "Find, decode and browse a specific UN*X man-page source file FILE-NAME.
  1386. Use existing buffer if possible; reformat only if prefix arg given.
  1387. When called interactively, optional argument REFORMAT forces reformatting
  1388. of an existing WoMan buffer formatted earlier.
  1389. No external programs are used, except that `gunzip' will be used to
  1390. decompress the file if appropriate. See the documentation for the
  1391. `woman' command for further details."
  1392. (interactive "fBrowse UN*X manual file: \nP")
  1393. (setq woman-last-file-name
  1394. (setq file-name (expand-file-name file-name))) ; to canonicalize
  1395. (let ((alist-tail woman-buffer-alist) exists)
  1396. (setq woman-buffer-number 0)
  1397. (while (and alist-tail (not (string= file-name (car (car alist-tail)))))
  1398. (setq alist-tail (cdr alist-tail)
  1399. woman-buffer-number (1+ woman-buffer-number)))
  1400. (or (and (setq exists
  1401. (and alist-tail (WoMan-find-buffer))) ; buffer exists
  1402. (not reformat))
  1403. ;; Format new buffer or reformat current buffer:
  1404. (let* ((bufname (file-name-nondirectory file-name))
  1405. (case-fold-search t)
  1406. (compressed
  1407. (not (not (string-match woman-file-compression-regexp bufname)))))
  1408. (if compressed
  1409. (setq bufname (file-name-sans-extension bufname)))
  1410. (setq bufname (if exists
  1411. (buffer-name)
  1412. (woman-make-bufname bufname)))
  1413. (woman-really-find-file file-name compressed bufname)
  1414. (or exists
  1415. (setq woman-buffer-alist
  1416. (cons (cons file-name bufname) woman-buffer-alist)
  1417. woman-buffer-number 0)))))
  1418. (Man-build-section-alist)
  1419. (Man-build-references-alist)
  1420. (goto-char (point-min)))
  1421. (defun woman-make-bufname (bufname)
  1422. "Create an unambiguous buffer name from BUFNAME."
  1423. ;; See Bug#5038. Any compression extension has already been removed.
  1424. ;; Go from eg "host.conf.5" to "5 host.conf".
  1425. (let ((dot (string-match "\\.[^.]*\\'" bufname)))
  1426. (if dot (setq bufname (concat
  1427. (substring bufname (1+ dot)) " "
  1428. (substring bufname 0 dot))))
  1429. (generate-new-buffer-name ; ensure uniqueness
  1430. (concat "*WoMan " bufname "*"))))
  1431. (defvar woman-frame nil
  1432. "Dedicated frame used for displaying WoMan windows.")
  1433. (defun woman-really-find-file (filename compressed bufname)
  1434. "Find, decompress, and decode a UN*X man page FILENAME.
  1435. If COMPRESSED is non-nil, turn on auto-compression mode to decompress
  1436. the file if necessary. Set buffer name BUFNAME and major mode.
  1437. Do not call directly!"
  1438. (let ((WoMan-current-file filename)) ; used for message logging
  1439. (if woman-use-own-frame
  1440. (select-frame
  1441. (or (and (frame-live-p woman-frame) woman-frame)
  1442. (setq woman-frame (make-frame)))))
  1443. (set-buffer (get-buffer-create bufname))
  1444. (condition-case nil
  1445. (switch-to-buffer (current-buffer))
  1446. (error (pop-to-buffer (current-buffer))))
  1447. (buffer-disable-undo)
  1448. (setq buffer-read-only nil)
  1449. (erase-buffer) ; NEEDED for reformat
  1450. (woman-insert-file-contents filename compressed)
  1451. ;; Set buffer's default directory to that of the file.
  1452. (setq default-directory (file-name-directory filename))
  1453. (set (make-local-variable 'backup-inhibited) t)
  1454. (set-visited-file-name "")
  1455. (woman-process-buffer)))
  1456. (defun woman-process-buffer ()
  1457. "The second half of `woman-really-find-file'!"
  1458. (interactive)
  1459. ;; Check (crudely) that this really is likely to be in UN*X
  1460. ;; man-page source format, assuming we are at point-min:
  1461. (goto-char (point-min))
  1462. (if (re-search-forward "^[.']" 1000 t)
  1463. (woman-decode-buffer)
  1464. (message
  1465. "File appears to be pre-formatted -- using source file may be better.")
  1466. (woman-man-buffer))
  1467. (woman-mode))
  1468. (defun woman-man-buffer ()
  1469. "Post-process an nroff-preformatted man buffer."
  1470. ;; Kill all leading whitespace:
  1471. (if (looking-at "\\s-+") (woman-delete-match 0))
  1472. ;; Delete all page footer/header pairs:
  1473. (re-search-forward ".*") ; match header
  1474. ;; Footer conventionally has page number at right, so ...
  1475. (let ((regex (concat
  1476. "^.*[0-9]\n\\s-*" ; footer and following blank lines
  1477. (regexp-quote (match-string 0)) ; header
  1478. "\\s-*\n"))) ; following blank lines
  1479. (while (re-search-forward regex nil 1) ; finish at eob
  1480. (woman-delete-match 0)))
  1481. ;; Delete last text line (footer) and all following blank lines:
  1482. (re-search-backward "\\S-")
  1483. (beginning-of-line)
  1484. (if (looking-at ".*[0-9]$")
  1485. (delete-region (point) (point-max)))
  1486. ;; Squeeze multiple blank lines:
  1487. (goto-char (point-min))
  1488. (while (re-search-forward "^[ \t]*\n\\([ \t]*\n\\)+" nil t)
  1489. (replace-match "\n" t t))
  1490. ;; CJK characters are underlined by double-sized "__".
  1491. ;; (Code lifted from man.el, with trivial changes.)
  1492. (if (< (buffer-size) (position-bytes (point-max)))
  1493. ;; Multibyte characters exist.
  1494. (progn
  1495. (goto-char (point-min))
  1496. (while (search-forward "__\b\b" nil t)
  1497. (backward-delete-char 4)
  1498. (woman-set-face (point) (1+ (point)) 'woman-italic))
  1499. (goto-char (point-min))
  1500. (while (search-forward "\b\b__" nil t)
  1501. (backward-delete-char 4)
  1502. (woman-set-face (1- (point)) (point) 'woman-italic))))
  1503. ;; Interpret overprinting to indicate bold face:
  1504. (goto-char (point-min))
  1505. (while (re-search-forward "\\(.\\)\\(\\(+\\1\\)+\\)" nil t)
  1506. (woman-delete-match 2)
  1507. (woman-set-face (1- (point)) (point) 'woman-bold))
  1508. ;; Interpret underlining to indicate italic face:
  1509. ;; (Must be AFTER emboldening to interpret bold _ correctly!)
  1510. (goto-char (point-min))
  1511. (while (search-forward "_" nil t)
  1512. (delete-char -2)
  1513. (woman-set-face (point) (1+ (point)) 'woman-italic))
  1514. ;; Leave any other uninterpreted ^H's in the buffer for now! (They
  1515. ;; might indicate composite special characters, which could be
  1516. ;; interpreted if I knew what to expect.)
  1517. ;; Optionally embolden section and subsection headings
  1518. ;; (cf. `woman-imenu-generic-expression'):
  1519. (cond
  1520. (woman-bold-headings
  1521. (goto-char (point-min))
  1522. (forward-line)
  1523. (while (re-search-forward "^\\( \\)?\\([A-Z].*\\)" nil t)
  1524. (woman-set-face (match-beginning 2) (match-end 2) 'woman-bold)))))
  1525. (defun woman-insert-file-contents (filename compressed)
  1526. "Insert file FILENAME into the current buffer.
  1527. If COMPRESSED is t, or is non-nil and the filename implies compression,
  1528. then turn on auto-compression mode to decompress the file.
  1529. Leave point at end of new text. Return length of inserted text."
  1530. ;; Leaves point at end of inserted text in GNU Emacs 20.3, but at
  1531. ;; start in 19.34!
  1532. (save-excursion
  1533. (let ((case-fold-search t))
  1534. ;; Co-operate with auto-compression mode:
  1535. (if (and compressed
  1536. (or (eq compressed t)
  1537. (string-match woman-file-compression-regexp filename))
  1538. ;; (not auto-compression-mode)
  1539. (not (rassq 'jka-compr-handler file-name-handler-alist)) )
  1540. ;; (error "Compressed file requires Auto File Decompression turned on")
  1541. (auto-compression-mode 1))
  1542. (nth 1
  1543. (condition-case ()
  1544. (insert-file-contents filename nil)
  1545. (file-error
  1546. ;; Run find-file-not-found-hooks until one returns non-nil.
  1547. ;; (run-hook-with-args-until-success 'find-file-not-found-hooks)
  1548. (insert "\n***** File " filename " not found! *****\n\n")))))))
  1549. ;;; Major mode (Man) interface:
  1550. (defvar woman-mode-map
  1551. (let ((map (make-sparse-keymap)))
  1552. (set-keymap-parent map Man-mode-map)
  1553. (define-key map "R" 'woman-reformat-last-file)
  1554. (define-key map "w" 'woman)
  1555. (define-key map "\en" 'WoMan-next-manpage)
  1556. (define-key map "\ep" 'WoMan-previous-manpage)
  1557. (define-key map [M-mouse-2] 'woman-follow-word)
  1558. ;; We don't need to call `man' when we are in `woman-mode'.
  1559. (define-key map [remap man] 'woman)
  1560. (define-key map [remap man-follow] 'woman-follow)
  1561. map)
  1562. "Keymap for woman mode.")
  1563. (defun woman-follow (topic)
  1564. "Get a Un*x manual page of the item under point and put it in a buffer."
  1565. (interactive (list (Man-default-man-entry)))
  1566. (if (or (not topic)
  1567. (string= topic ""))
  1568. (error "No item under point")
  1569. (woman (if (string-match Man-reference-regexp topic)
  1570. (substring topic 0 (match-end 1))
  1571. topic))))
  1572. (defun woman-follow-word (event)
  1573. "Run WoMan with word under mouse as topic.
  1574. Argument EVENT is the invoking mouse event."
  1575. (interactive "e") ; mouse event
  1576. (goto-char (posn-point (event-start event)))
  1577. (woman (or (current-word t) "")))
  1578. ;; WoMan menu bar and pop-up menu:
  1579. (easy-menu-define
  1580. woman-menu ; (SYMBOL MAPS DOC MENU)
  1581. ;; That comment was moved after the symbol `woman-menu' to make
  1582. ;; find-function-search-for-symbol work. -- rost
  1583. woman-mode-map
  1584. "WoMan Menu"
  1585. `("WoMan"
  1586. ["WoMan..." woman t] ; [NAME CALLBACK ENABLE]
  1587. "--"
  1588. ["Next Section" Man-next-section t]
  1589. ["Previous Section" Man-previous-section t]
  1590. ["Goto Section..." Man-goto-section t]
  1591. ["Goto See-Also Section" Man-goto-see-also-section t]
  1592. ["Follow Reference..." Man-follow-manual-reference t]
  1593. "--"
  1594. ["Previous WoMan Buffer" WoMan-previous-manpage t]
  1595. ["Next WoMan Buffer" WoMan-next-manpage t]
  1596. ["Bury WoMan Buffer" Man-quit t]
  1597. ["Kill WoMan Buffer" Man-kill t]
  1598. "--"
  1599. ;; ["Toggle Fill Frame Width" woman-toggle-fill-frame t]
  1600. ["Use Full Frame Width" woman-toggle-fill-frame
  1601. :active t :style toggle :selected woman-fill-frame]
  1602. ["Reformat Last Man Page" woman-reformat-last-file t]
  1603. ["Use Monochrome Main Faces" woman-monochrome-faces t]
  1604. ["Use Default Main Faces" woman-default-faces t]
  1605. ["Make Contents Menu" (woman-imenu t) (not woman-imenu-done)]
  1606. "--"
  1607. ["Describe (Wo)Man Mode" describe-mode t]
  1608. ["Mini Help" woman-mini-help t]
  1609. ,@(if (fboundp 'customize-group)
  1610. '(["Customize..." (customize-group 'woman) t]))
  1611. ["Show Version" (message "WoMan %s" woman-version) t]
  1612. "--"
  1613. ("Advanced"
  1614. ["View Source" (view-file woman-last-file-name) woman-last-file-name]
  1615. ["Show Log" (switch-to-buffer-other-window "*WoMan-Log*" t) t]
  1616. ["Extended Font" woman-toggle-use-extended-font
  1617. :included woman-font-support
  1618. :active t :style toggle :selected woman-use-extended-font]
  1619. ["Symbol Font" woman-toggle-use-symbol-font
  1620. :included woman-font-support
  1621. :active t :style toggle :selected woman-use-symbol-font]
  1622. ["Font Map" woman-display-extended-fonts
  1623. :included woman-font-support
  1624. :active woman-use-symbol-font]
  1625. "--"
  1626. "Emulation"
  1627. ["nroff" (woman-reset-emulation 'nroff)
  1628. :active t :style radio :selected (eq woman-emulation 'nroff)]
  1629. ["troff" (woman-reset-emulation 'troff)
  1630. :active t :style radio :selected (eq woman-emulation 'troff)]
  1631. )
  1632. ))
  1633. (defun woman-toggle-use-extended-font ()
  1634. "Toggle `woman-use-extended-font' and reformat, for menu use."
  1635. (interactive)
  1636. (setq woman-use-extended-font (not woman-use-extended-font))
  1637. (woman-reformat-last-file))
  1638. (defun woman-toggle-use-symbol-font ()
  1639. "Toggle `woman-use-symbol-font' and reformat, for menu use."
  1640. (interactive)
  1641. (setq woman-use-symbol-font (not woman-use-symbol-font))
  1642. (woman-reformat-last-file))
  1643. (defun woman-reset-emulation (value)
  1644. "Reset `woman-emulation' to VALUE and reformat, for menu use."
  1645. (interactive)
  1646. (setq woman-emulation value)
  1647. (woman-reformat-last-file))
  1648. (defvar bookmark-make-record-function)
  1649. (put 'woman-mode 'mode-class 'special)
  1650. (defun woman-mode ()
  1651. "Turn on (most of) Man mode to browse a buffer formatted by WoMan.
  1652. WoMan is an ELisp emulation of much of the functionality of the Emacs
  1653. `man' command running the standard UN*X man and ?roff programs.
  1654. WoMan author: F.J.Wright@Maths.QMW.ac.uk
  1655. WoMan version: see `woman-version'.
  1656. See `Man-mode' for additional details."
  1657. (let ((Man-build-page-list (symbol-function 'Man-build-page-list))
  1658. (Man-strip-page-headers (symbol-function 'Man-strip-page-headers))
  1659. (Man-unindent (symbol-function 'Man-unindent))
  1660. (Man-goto-page (symbol-function 'Man-goto-page)))
  1661. ;; Prevent inappropriate operations:
  1662. (fset 'Man-build-page-list 'ignore)
  1663. (fset 'Man-strip-page-headers 'ignore)
  1664. (fset 'Man-unindent 'ignore)
  1665. (fset 'Man-goto-page 'ignore)
  1666. (unwind-protect
  1667. (delay-mode-hooks (Man-mode))
  1668. ;; Restore the status quo:
  1669. (fset 'Man-build-page-list Man-build-page-list)
  1670. (fset 'Man-strip-page-headers Man-strip-page-headers)
  1671. (fset 'Man-unindent Man-unindent)
  1672. (fset 'Man-goto-page Man-goto-page)
  1673. (setq tab-width woman-tab-width)))
  1674. (setq major-mode 'woman-mode
  1675. mode-name "WoMan")
  1676. ;; Don't show page numbers like Man-mode does. (Online documents do
  1677. ;; not have pages)
  1678. (kill-local-variable 'mode-line-buffer-identification)
  1679. (use-local-map woman-mode-map)
  1680. ;; Imenu support:
  1681. (set (make-local-variable 'imenu-generic-expression)
  1682. ;; `make-local-variable' in case imenu not yet loaded!
  1683. woman-imenu-generic-expression)
  1684. (set (make-local-variable 'imenu-space-replacement) " ")
  1685. ;; Bookmark support.
  1686. (set (make-local-variable 'bookmark-make-record-function)
  1687. 'woman-bookmark-make-record)
  1688. ;; For reformat ...
  1689. ;; necessary when reformatting a file in its old buffer:
  1690. (setq imenu--last-menubar-index-alist nil)
  1691. ;; necessary to avoid re-installing the same imenu:
  1692. (setq woman-imenu-done nil)
  1693. (if woman-imenu (woman-imenu))
  1694. (let ((inhibit-read-only t))
  1695. (Man-highlight-references 'WoMan-xref-man-page))
  1696. (set-buffer-modified-p nil)
  1697. (run-mode-hooks 'woman-mode-hook))
  1698. (defun woman-imenu (&optional redraw)
  1699. "Add a \"Contents\" menu to the menubar.
  1700. Optional argument REDRAW, if non-nil, forces mode line to be updated."
  1701. (interactive)
  1702. (if woman-imenu-done
  1703. ;; This is PRIMARILY to avoid a bug in imenu-add-to-menubar that
  1704. ;; causes it to corrupt the menu bar if it is run more than once
  1705. ;; in the same buffer.
  1706. ()
  1707. (setq woman-imenu-done t)
  1708. (imenu-add-to-menubar woman-imenu-title)
  1709. (if redraw (force-mode-line-update))))
  1710. (defun woman-toggle-fill-frame ()
  1711. "Toggle formatting to fill (most of) the width of the current frame."
  1712. (interactive)
  1713. (setq woman-fill-frame (not woman-fill-frame))
  1714. (message "Woman fill column set to %s."
  1715. (if woman-fill-frame "frame width" woman-fill-column)))
  1716. (defun woman-mini-help ()
  1717. "Display WoMan commands and user options in an `apropos' buffer."
  1718. ;; Based on apropos-command in apropos.el
  1719. (interactive)
  1720. (require 'apropos)
  1721. (let ((message
  1722. (let ((standard-output (get-buffer-create "*Apropos*")))
  1723. (help-print-return-message 'identity))))
  1724. (setq apropos-accumulator
  1725. (apropos-internal "woman"
  1726. (lambda (symbol)
  1727. (and
  1728. (or (commandp symbol)
  1729. (user-variable-p symbol))
  1730. (not (get symbol 'apropos-inhibit))))))
  1731. ;; Find documentation strings:
  1732. (let ((p apropos-accumulator)
  1733. doc symbol)
  1734. (while p
  1735. (setcar p (list ; must have 3 elements:
  1736. (setq symbol (car p)) ; 1. name
  1737. (if (functionp symbol) ; 2. command doc
  1738. (if (setq doc (documentation symbol t))
  1739. (substring doc 0 (string-match "\n" doc))
  1740. "(not documented)"))
  1741. (if (user-variable-p symbol) ; 3. variable doc
  1742. (if (setq doc (documentation-property
  1743. symbol 'variable-documentation t))
  1744. (substring doc 0 (string-match "\n" doc))))))
  1745. (setq p (cdr p))))
  1746. ;; Output the result:
  1747. (and (apropos-print t nil)
  1748. message
  1749. (message "%s" message))))
  1750. (defun WoMan-getpage-in-background (topic)
  1751. "Use TOPIC to start WoMan from `Man-follow-manual-reference'."
  1752. ;; topic is a string, generally of the form "section topic"
  1753. (let ((s (string-match " " topic)))
  1754. (if s (setq topic (substring topic (1+ s))))
  1755. (woman topic)))
  1756. (defvar WoMan-Man-start-time nil
  1757. "Used to record formatting time used by the `man' command.")
  1758. ;; Both advices are disabled because "a file in Emacs should not put
  1759. ;; advice on a function in Emacs" (see Info node "(elisp)Advising
  1760. ;; Functions"). Counting the formatting time is useful for
  1761. ;; developing, but less applicable for daily use. The advice for
  1762. ;; `Man-getpage-in-background' can be discarded, because the
  1763. ;; key-binding in `woman-mode-map' has been remapped to call `woman'
  1764. ;; but `man'. Michael Albinus <michael.albinus@gmx.de>
  1765. ;; (defadvice Man-getpage-in-background
  1766. ;; (around Man-getpage-in-background-advice (topic) activate)
  1767. ;; "Use WoMan unless invoked outside a WoMan buffer or invoked explicitly.
  1768. ;; Otherwise use Man and record start of formatting time."
  1769. ;; (if (and (eq major-mode 'woman-mode)
  1770. ;; (not (eq (caar command-history) 'man)))
  1771. ;; (WoMan-getpage-in-background topic)
  1772. ;; ;; Initiates man processing
  1773. ;; (setq WoMan-Man-start-time (current-time))
  1774. ;; ad-do-it))
  1775. ;; (defadvice Man-bgproc-sentinel
  1776. ;; (after Man-bgproc-sentinel-advice activate)
  1777. ;; ;; Terminates man processing
  1778. ;; "Report formatting time."
  1779. ;; (let* ((time (current-time))
  1780. ;; (time (+ (* (- (car time) (car WoMan-Man-start-time)) 65536)
  1781. ;; (- (cadr time) (cadr WoMan-Man-start-time)))))
  1782. ;; (message "Man formatting done in %d seconds" time)))
  1783. ;;; Buffer handling:
  1784. (defun WoMan-previous-manpage ()
  1785. "Find the previous WoMan buffer."
  1786. ;; Assumes currently in a WoMan buffer!
  1787. (interactive)
  1788. (WoMan-find-buffer) ; find current existing buffer
  1789. (if (null (cdr woman-buffer-alist))
  1790. (error "No previous WoMan buffer"))
  1791. (if (>= (setq woman-buffer-number (1+ woman-buffer-number))
  1792. (length woman-buffer-alist))
  1793. (setq woman-buffer-number 0))
  1794. (if (WoMan-find-buffer)
  1795. ()
  1796. (if (< (setq woman-buffer-number (1- woman-buffer-number)) 0)
  1797. (setq woman-buffer-number (1- (length woman-buffer-alist))))
  1798. (WoMan-previous-manpage)))
  1799. (defun WoMan-next-manpage ()
  1800. "Find the next WoMan buffer."
  1801. ;; Assumes currently in a WoMan buffer!
  1802. (interactive)
  1803. (WoMan-find-buffer) ; find current existing buffer
  1804. (if (null (cdr woman-buffer-alist))
  1805. (error "No next WoMan buffer"))
  1806. (if (< (setq woman-buffer-number (1- woman-buffer-number)) 0)
  1807. (setq woman-buffer-number (1- (length woman-buffer-alist))))
  1808. (if (WoMan-find-buffer)
  1809. ()
  1810. (WoMan-next-manpage)))
  1811. (defun WoMan-find-buffer ()
  1812. "Switch to buffer corresponding to `woman-buffer-number' and return it.
  1813. If such a buffer does not exist then remove its association from the
  1814. alist in `woman-buffer-alist' and return nil."
  1815. (if (zerop woman-buffer-number)
  1816. (let ((buffer (get-buffer (cdr (car woman-buffer-alist)))))
  1817. (if buffer
  1818. (switch-to-buffer buffer)
  1819. ;; Delete alist element:
  1820. (setq woman-buffer-alist (cdr woman-buffer-alist))
  1821. nil))
  1822. (let* ((prev-ptr (nthcdr (1- woman-buffer-number) woman-buffer-alist))
  1823. (buffer (get-buffer (cdr (car (cdr prev-ptr))))))
  1824. (if buffer
  1825. (switch-to-buffer buffer)
  1826. ;; Delete alist element:
  1827. (setcdr prev-ptr (cdr (cdr prev-ptr)))
  1828. (if (>= woman-buffer-number (length woman-buffer-alist))
  1829. (setq woman-buffer-number 0))
  1830. nil))))
  1831. ;;; Syntax and display tables:
  1832. (defconst woman-escaped-escape-char ?
  1833. ;; An arbitrary unused control character
  1834. "Internal character representation of escaped escape characters.")
  1835. (defconst woman-escaped-escape-string
  1836. (char-to-string woman-escaped-escape-char)
  1837. "Internal string representation of escaped escape characters.")
  1838. (defconst woman-unpadded-space-char ?
  1839. ;; An arbitrary unused control character
  1840. "Internal character representation of unpadded space characters.")
  1841. (defconst woman-unpadded-space-string
  1842. (char-to-string woman-unpadded-space-char)
  1843. "Internal string representation of unpadded space characters.")
  1844. (defvar woman-syntax-table
  1845. (let ((st (make-syntax-table)))
  1846. ;; The following internal chars must NOT have whitespace syntax:
  1847. (modify-syntax-entry woman-unpadded-space-char "." st)
  1848. (modify-syntax-entry woman-escaped-escape-char "." st)
  1849. st)
  1850. "Syntax table to support special characters used internally by WoMan.")
  1851. (defun woman-set-buffer-display-table ()
  1852. "Set up a display table for a WoMan buffer.
  1853. This display table is used for displaying internal special characters, but
  1854. does not interfere with any existing display table, e.g. for displaying
  1855. European characters."
  1856. (setq buffer-display-table
  1857. ;; The following test appears to be necessary on some
  1858. ;; non-Windows platforms, e.g. Solaris 2.6 when running on a
  1859. ;; tty. Thanks to T. V. Raman <raman@Adobe.COM>.
  1860. ;; The MS-DOS terminal also sets standard-display-table to
  1861. ;; a non-nil value.
  1862. (if standard-display-table ; default is nil !!!
  1863. (copy-sequence standard-display-table)
  1864. (make-display-table)))
  1865. ;; Display the following internal chars correctly:
  1866. (aset buffer-display-table woman-unpadded-space-char [?\ ])
  1867. (aset buffer-display-table woman-escaped-escape-char [?\\]))
  1868. ;;; The main decoding driver:
  1869. (defvar font-lock-mode) ; for the compiler
  1870. (defun woman-decode-buffer ()
  1871. "Decode a buffer in UN*X man-page source format.
  1872. No external programs are used."
  1873. (interactive) ; mainly for testing
  1874. (WoMan-log-begin)
  1875. (run-hooks 'woman-pre-format-hook)
  1876. (and (boundp 'font-lock-mode) font-lock-mode (font-lock-mode -1))
  1877. ;; (fundamental-mode)
  1878. (let ((start-time (current-time))
  1879. time)
  1880. (message "WoMan formatting buffer...")
  1881. ; (goto-char (point-min))
  1882. ; (cond
  1883. ; ((re-search-forward "^\\.[ \t]*TH" nil t) ; wrong format if not found?
  1884. ; (beginning-of-line)
  1885. ; (delete-region (point-min) (point))) ; potentially dangerous!
  1886. ; (t (message "WARNING: .TH request not found -- not man-page format?")))
  1887. (woman-decode-region (point-min) (point-max))
  1888. (setq time (float-time (time-since start-time)))
  1889. (message "WoMan formatting buffer...done in %g seconds" time)
  1890. (WoMan-log-end time))
  1891. (run-hooks 'woman-post-format-hook))
  1892. (defvar woman-string-alist ; rebound in woman-decode-region
  1893. '(("S" . "") ("R" . "(Reg.)") ("Tm" . "(TM)")
  1894. ("lq" . "\"") ("rq" . "\"")
  1895. ("''" . "\"") ; needed for gcc.1
  1896. (".T" . "") ; output device from -T option?
  1897. )
  1898. "Alist of strings predefined in the -man macro package `tmac.an'.")
  1899. (defvar woman-negative-vertical-space nil ; rebound in woman-decode-region
  1900. "Set to t if .sp N with N < 0 encountered.")
  1901. (defun woman-pre-process-region (from to)
  1902. "Pre-process escapes and comments in the region of text between FROM and TO.
  1903. To be called on original buffer and any .so insertions."
  1904. ;; Hide escaped escapes \\ and printable escapes \e very early
  1905. ;; (to be re-instated as just \ very late!):
  1906. (goto-char from)
  1907. ;; .eo turns off escape character processing
  1908. (while (re-search-forward "\\(\\\\[\\e]\\)\\|^\\.eo" to t) ; \\
  1909. (if (match-beginning 1)
  1910. (replace-match woman-escaped-escape-string t t)
  1911. (woman-delete-whole-line)
  1912. ;; .ec turns on escape character processing (and sets the
  1913. ;; escape character to its argument, if any, which I'm ignoring
  1914. ;; for now!)
  1915. (while (and (re-search-forward "\\(\\\\\\)\\|^\\.ec" to t) ; \
  1916. (match-beginning 1))
  1917. (replace-match woman-escaped-escape-string t t))
  1918. ;; ***** Need test for .ec arg and warning here! *****
  1919. (woman-delete-whole-line)))
  1920. ;; Delete comments .\"<anything>, \"<anything> and null requests.
  1921. ;; (However, should null . requests cause a break?)
  1922. (goto-char from)
  1923. (while (re-search-forward "^[.'][ \t]*\\(\\\\\".*\\)?\n\\|\\\\\".*" to t)
  1924. (woman-delete-match 0)))
  1925. (defun woman-non-underline-faces ()
  1926. "Prepare non-underlined versions of underlined faces."
  1927. (let ((face-list (face-list)))
  1928. (dolist (face face-list)
  1929. (let ((face-name (symbol-name face)))
  1930. (if (and (string-match "\\`woman-" face-name)
  1931. (face-underline-p face))
  1932. (let ((face-no-ul (intern (concat face-name "-no-ul"))))
  1933. (copy-face face face-no-ul)
  1934. (set-face-underline-p face-no-ul nil)))))))
  1935. ;; Preprocessors
  1936. ;; =============
  1937. ;; This information is based on documentation for the man command by
  1938. ;; Graeme W. Wilford <G.Wilford@ee.surrey.ac.uk>
  1939. ;; First, the environment variable $MANROFFSEQ is interrogated, and if
  1940. ;; not set then the initial line of the nroff file is parsed for a
  1941. ;; preprocessor string. To contain a valid preprocessor string, the
  1942. ;; first line must resemble
  1943. ;;
  1944. ;; '\" <string>
  1945. ;;
  1946. ;; where string can be any combination of the following letters that
  1947. ;; specify the sequence of preprocessors to run before nroff or
  1948. ;; troff/groff. Not all installations will have a full set of
  1949. ;; preprocessors. Some of the preprocessors and the letters used to
  1950. ;; designate them are: eqn (e), grap (g), pic (p), tbl (t), vgrind
  1951. ;; (v), refer (r). This option overrides the $MANROFFSEQ environment
  1952. ;; variable. zsoelim is always run as the very first preprocessor.
  1953. (defvar woman-emulate-tbl nil
  1954. "True if WoMan should emulate the tbl preprocessor.
  1955. This applies to text between .TE and .TS directives.
  1956. Currently set only from '\" t in the first line of the source file.")
  1957. (defun woman-decode-region (from _to)
  1958. "Decode the region between FROM and TO in UN*X man-page source format."
  1959. ;; Suitable for use in format-alist.
  1960. ;; But this requires care to control major mode implied font locking.
  1961. ;; Must return the new end of file. See format.el for details.
  1962. ;; NB: The `to' argument is bogus: it is not currently used, and if
  1963. ;; it were it would need to be a marker rather than a position!
  1964. ;; First force the correct environment:
  1965. (let ((case-fold-search nil) ; This is necessary!
  1966. (woman-string-alist woman-string-alist)
  1967. (woman-fill-column woman-fill-column)
  1968. woman-negative-vertical-space)
  1969. (setq woman-left-margin woman-default-indent
  1970. woman-prevailing-indent woman-default-indent
  1971. woman-interparagraph-distance 1
  1972. woman-leave-blank-lines nil
  1973. woman-RS-left-margin nil
  1974. woman-RS-prevailing-indent nil
  1975. woman-adjust woman-adjust-both
  1976. woman-justify (nth woman-adjust woman-justify-list)
  1977. woman-nofill nil)
  1978. (setq woman-if-conditions-true
  1979. (cons (string-to-char (symbol-name woman-emulation)) '(?e ?o)))
  1980. ;; Prepare non-underlined versions of underlined faces:
  1981. (woman-non-underline-faces)
  1982. ;; Set font of `woman-symbol' face to `woman-symbol-font' if
  1983. ;; `woman-symbol-font' is well defined.
  1984. (and woman-use-symbol-font
  1985. (stringp woman-symbol-font)
  1986. (set-face-font 'woman-symbol woman-symbol-font
  1987. (and (frame-live-p woman-frame) woman-frame)))
  1988. ;; Set syntax and display tables:
  1989. (set-syntax-table woman-syntax-table)
  1990. (woman-set-buffer-display-table)
  1991. ;; Based loosely on a suggestion by Theodore Jump:
  1992. (if (or woman-fill-frame
  1993. (not (and (integerp woman-fill-column) (> woman-fill-column 0))))
  1994. (setq woman-fill-column (- (window-width) woman-default-indent)))
  1995. ;; Check for preprocessor requests:
  1996. (goto-char from)
  1997. (if (looking-at "'\\\\\"[ \t]*\\([a-z]+\\)")
  1998. (let ((letters (append (match-string 1) nil)))
  1999. (if (memq ?t letters)
  2000. (setq woman-emulate-tbl t
  2001. letters (delete ?t letters)))
  2002. (if letters
  2003. (WoMan-warn "Unhandled preprocessor request letters %s"
  2004. (concat letters)))
  2005. (woman-delete-line 1)))
  2006. (woman-pre-process-region from nil)
  2007. ;; Process ignore requests, macro definitions,
  2008. ;; conditionals and switch source requests:
  2009. (woman0-roff-buffer from)
  2010. ;; Check for macro sets that woman cannot handle. We can only
  2011. ;; because do this after processing source-switch directives.
  2012. (goto-char (point-min))
  2013. (let ((case-fold-search nil))
  2014. (unless (and (re-search-forward "^\\.SH[ \n]" (point-max) t)
  2015. (progn (goto-char (point-min))
  2016. (re-search-forward "^\\.TH[ \n]" (point-max) t))
  2017. (progn (goto-char (point-min))
  2018. (not (re-search-forward "^\\.\\([pnil]p\\|sh\\)[ \n]"
  2019. (point-max) t))))
  2020. (error "WoMan can only format man pages written with the usual `-man' macros")))
  2021. ;; Process \k escapes BEFORE changing tab width (?):
  2022. (goto-char from)
  2023. (woman-mark-horizonal-position)
  2024. ;; Set buffer-local variables:
  2025. (setq fill-column woman-fill-column
  2026. tab-width woman-tab-width)
  2027. ;; Hide unpaddable and digit-width spaces \(space) and \0:
  2028. (goto-char from)
  2029. (while (re-search-forward "\\\\[ 0]" nil t)
  2030. (replace-match woman-unpadded-space-string t t))
  2031. ;; Discard optional hyphen \%; concealed newlines \<newline>;
  2032. ;; point-size change function \sN,\s+N, \s-N:
  2033. (goto-char from)
  2034. (while (re-search-forward "\\\\\\([%\n]\\|s[-+]?[0-9]+\\)" nil t)
  2035. (woman-delete-match 0))
  2036. ;; BEWARE: THIS SHOULD PROBABLY ALL BE DONE MUCH LATER!!!!!
  2037. ;; Process trivial escapes \-, \`, \.
  2038. ;; (\' must be done after tab processing!):
  2039. (goto-char from)
  2040. (while (re-search-forward "\\\\\\([-`.]\\)" nil t)
  2041. (replace-match "\\1"))
  2042. ;; NB: Must keep ALL zero-width characters \&, \|, and \^ until
  2043. ;; ALL requests processed!
  2044. ;; Process no-break requests and macros (including font-change macros):
  2045. (goto-char from)
  2046. (woman1-roff-buffer)
  2047. ;; Process strings and special character escapes \(xx:
  2048. ;; (Must do this BEFORE fontifying!)
  2049. (goto-char from)
  2050. (woman-strings)
  2051. ;; Special chars moved after translation in
  2052. ;; `woman2-process-escapes' (for pic.1):
  2053. ; (goto-char from)
  2054. ; (woman-special-characters)
  2055. ;; Process standard font-change requests and escapes:
  2056. (goto-char from)
  2057. (woman-change-fonts)
  2058. ;; 1/2 em vertical motion \d, \u and general local vertical motion
  2059. ;; \v'+/-N' simulated using TeX ^ and _ symbols for now.
  2060. (goto-char from)
  2061. (let ((first t)) ; assume no nesting!
  2062. (while (re-search-forward "\\\\\\([du]\\|v'[^']*'\\)" nil t)
  2063. (let* ((esc (match-string 1))
  2064. (repl (if (or (= (aref esc 0) ?u)
  2065. (and (>= (length esc) 2) (= (aref esc 2) ?-)))
  2066. "^" "_")))
  2067. (cond (first
  2068. (replace-match repl nil t)
  2069. (put-text-property (1- (point)) (point) 'face 'woman-addition)
  2070. (WoMan-warn
  2071. "Initial vertical motion escape \\%s simulated" esc)
  2072. (WoMan-log
  2073. " by TeX `%s' in woman-addition-face!" repl))
  2074. (t
  2075. (woman-delete-match 0)
  2076. (WoMan-warn
  2077. "Terminal vertical motion escape \\%s ignored!" esc)))
  2078. (setq first (not first)))))
  2079. ;; Process formatting macros
  2080. (goto-char from)
  2081. (woman2-roff-buffer)
  2082. ;; Go back and process negative vertical space if necessary:
  2083. (if woman-negative-vertical-space
  2084. (woman-negative-vertical-space from))
  2085. (if woman-preserve-ascii
  2086. ;; Re-instate escaped escapes to just `\' and unpaddable
  2087. ;; spaces to just `space', without inheriting any text
  2088. ;; properties. This is not necessary, UNLESS the buffer is to
  2089. ;; be saved as ASCII.
  2090. (progn
  2091. (goto-char from)
  2092. (while (search-forward woman-escaped-escape-string nil t)
  2093. (delete-char -1) (insert ?\\))
  2094. (goto-char from)
  2095. (while (search-forward woman-unpadded-space-string nil t)
  2096. (delete-char -1) (insert ?\ ))))
  2097. ;; Must return the new end of file if used in format-alist.
  2098. (point-max)))
  2099. (defun woman-horizontal-escapes (to)
  2100. "Process \\h'+/-N' local horizontal motion escapes upto TO.
  2101. Implements arbitrary forward and non-overlapping backward motion.
  2102. Preserves location of `point'."
  2103. ;; Moved from `woman-decode-region' for version 0.50.
  2104. ;; N may include width escape \w'...' (but may already be processed!
  2105. (let ((from (point)))
  2106. (while (re-search-forward
  2107. ;; Delimiter can be a special char escape sequence \(.. or
  2108. ;; a single normal char (usually '):
  2109. "\\\\h\\(\\\\(..\\|.\\)\\(|\\)?"
  2110. to t)
  2111. (let ((from (match-beginning 0))
  2112. (delim (regexp-quote (match-string 1)))
  2113. (absolute (match-beginning 2)) ; absolute position?
  2114. (N (woman-parse-numeric-arg)) ; distance
  2115. to
  2116. msg) ; for warning
  2117. (if (not (looking-at delim))
  2118. ;; Warn but leave escape in buffer unprocessed:
  2119. (WoMan-warn
  2120. "Local horizontal motion (%s) delimiter error!"
  2121. (buffer-substring from (1+ (point)))) ; point at end of arg
  2122. (setq to (match-end 0)
  2123. ;; For possible warning -- save before deleting:
  2124. msg (buffer-substring from to))
  2125. (delete-region from to)
  2126. (if absolute ; make relative
  2127. (setq N (- N (current-column))))
  2128. (if (>= N 0)
  2129. ;; Move forward by inserting hard spaces:
  2130. (insert-char woman-unpadded-space-char N)
  2131. ;; Move backwards by deleting space,
  2132. ;; first backwards then forwards:
  2133. (while (and
  2134. (<= (setq N (1+ N)) 0)
  2135. (cond ((memq (preceding-char) '(?\ ?\t))
  2136. (delete-char -1) t)
  2137. ((memq (following-char) '(?\ ?\t))
  2138. (delete-char 1) t)
  2139. (t nil))))
  2140. (if (<= N 0)
  2141. (WoMan-warn
  2142. "Negative horizontal motion (%s) would overwrite!" msg))))))
  2143. (goto-char from)))
  2144. ;; Process ignore requests (.ig), conditionals (.if etc.),
  2145. ;; source-switch (.so), macro definitions (.de etc.) and macro
  2146. ;; expansions.
  2147. (defvar woman0-if-to) ; marker bound in woman0-roff-buffer
  2148. (defvar woman0-macro-alist) ; bound in woman0-roff-buffer
  2149. (defvar woman0-search-regex) ; bound in woman0-roff-buffer
  2150. (defvar woman0-search-regex-start ; bound in woman0-roff-buffer
  2151. "^[.'][ \t]*\\(ig\\|if\\|ie\\|el\\|so\\|rn\\|de\\|am")
  2152. (defconst woman0-search-regex-end "\\)\\([ \t]+\\|$\\)")
  2153. ;; May need other terminal characters, e.g. \, but NOT \n!
  2154. ;; Alternatively, force maximal match (Posix?)
  2155. (defvar woman0-rename-alist) ; bound in woman0-roff-buffer
  2156. (defun woman0-roff-buffer (from)
  2157. "Process conditional-type requests and user-defined macros.
  2158. Start at FROM and re-scan new text as appropriate."
  2159. (goto-char from)
  2160. (let ((woman0-if-to (make-marker))
  2161. woman-request woman0-macro-alist
  2162. (woman0-search-regex-start woman0-search-regex-start)
  2163. (woman0-search-regex
  2164. (concat woman0-search-regex-start woman0-search-regex-end))
  2165. processed-first-hunk
  2166. woman0-rename-alist)
  2167. (set-marker-insertion-type woman0-if-to t)
  2168. (while (re-search-forward woman0-search-regex nil t)
  2169. (setq woman-request (match-string 1))
  2170. ;; Process escape sequences prior to first request (Bug#7843).
  2171. (unless processed-first-hunk
  2172. (setq processed-first-hunk t)
  2173. (let ((process-escapes-to-marker (point-marker)))
  2174. (set-marker-insertion-type process-escapes-to-marker t)
  2175. (save-match-data
  2176. (save-excursion
  2177. (goto-char from)
  2178. (woman2-process-escapes process-escapes-to-marker)))))
  2179. (cond ((string= woman-request "ig") (woman0-ig))
  2180. ((string= woman-request "if") (woman0-if "if"))
  2181. ((string= woman-request "ie") (woman0-if "ie"))
  2182. ((string= woman-request "el") (woman0-el))
  2183. ((string= woman-request "so") (woman0-so))
  2184. ((string= woman-request "rn") (woman0-rn))
  2185. ((string= woman-request "de") (woman0-de))
  2186. ((string= woman-request "am") (woman0-de 'append))
  2187. (t (woman0-macro woman-request))))
  2188. (set-marker woman0-if-to nil)
  2189. (woman0-rename)
  2190. ;; Should now re-run `woman0-roff-buffer' if any renaming was
  2191. ;; done, but let's just hope this is not necessary for now!
  2192. ))
  2193. (defun woman0-ig ()
  2194. ".ig yy -- Discard input up to `.yy', which defaults to `..')."
  2195. ;; The terminal request MUST begin with . (not ')!
  2196. (looking-at "\\(\\S +\\)?")
  2197. (beginning-of-line)
  2198. (let ((yy (or (match-string 1) "."))
  2199. (from (point)))
  2200. (if (re-search-forward
  2201. (concat "^\\.[ \t]*" (regexp-quote yy) ".*\n") nil t)
  2202. (delete-region from (point))
  2203. (WoMan-warn
  2204. "ig request ignored -- terminator `.%s' not found!" yy)
  2205. (woman-delete-line 1))))
  2206. (defsubst woman0-process-escapes (from to)
  2207. "Process escapes within an if/ie condition between FROM and TO."
  2208. (woman-strings to)
  2209. (goto-char from) ; necessary!
  2210. ;; Strip font-change escapes:
  2211. (while (re-search-forward "\\\\f\\(\\[[^]]+\\]\\|(..\\|.\\)" to t)
  2212. (woman-delete-match 0))
  2213. (goto-char from) ; necessary!
  2214. (woman2-process-escapes to 'numeric))
  2215. ;; request does not appear to be used dynamically by any callees.
  2216. (defun woman0-if (request)
  2217. ".if/ie c anything -- Discard unless c evaluates to true.
  2218. Remember condition for use by a subsequent `.el'.
  2219. REQUEST is the invoking directive without the leading dot."
  2220. ;; c evaluates to a one-character built-in condition name or
  2221. ;; 'string1'string2' or a number > 0, prefix ! negates.
  2222. ;; \{ ... \} for multi-line use.
  2223. ;; Leaves point at start of new text.
  2224. (woman-delete-match 0)
  2225. ;; (delete-horizontal-space)
  2226. ;; Process escapes in condition:
  2227. (let ((from (point)) negated n (c 0))
  2228. (set-marker woman0-if-to
  2229. (save-excursion (skip-syntax-forward "^ ") (point)))
  2230. ;; Process condition:
  2231. (if (setq negated (= (following-char) ?!)) (delete-char 1))
  2232. (cond
  2233. ;; ((looking-at "[no]") (setq c t)) ; accept n(roff) and o(dd page)
  2234. ;; ((looking-at "[te]") (setq c nil)) ; reject t(roff) and e(ven page)
  2235. ((looking-at "[ntoe]")
  2236. (setq c (memq (following-char) woman-if-conditions-true)))
  2237. ;; Unrecognized letter so reject:
  2238. ((looking-at "[A-Za-z]") (setq c nil)
  2239. (WoMan-warn "%s %s -- unrecognized condition name rejected!"
  2240. request (match-string 0)))
  2241. ;; Accept strings if identical:
  2242. ((save-restriction
  2243. (narrow-to-region from woman0-if-to)
  2244. ;; String delimiter can be any non-numeric character,
  2245. ;; including a special character escape:
  2246. (looking-at "\\(\\\\(..\\|[^0-9]\\)\\(.*\\)\\1\\(.*\\)\\1\\'"))
  2247. (let ((end1 (copy-marker (match-end 2) t))) ; End of first string.
  2248. ;; Delete 2nd and 3rd delimiters to avoid processing them:
  2249. (delete-region (match-end 3) woman0-if-to)
  2250. (delete-region (match-end 2) (match-beginning 3))
  2251. (goto-char (match-end 1))
  2252. (woman0-process-escapes (point) woman0-if-to)
  2253. (setq c (string= (buffer-substring (point) end1)
  2254. (buffer-substring end1 woman0-if-to)))
  2255. (set-marker end1 nil)
  2256. (goto-char from)))
  2257. ;; Accept numeric value if > 0:
  2258. ((numberp (setq n (progn
  2259. (woman0-process-escapes from woman0-if-to)
  2260. (woman-parse-numeric-arg))))
  2261. (setq c (> n 0))
  2262. (goto-char from)))
  2263. (if (eq c 0)
  2264. (woman-if-ignore woman0-if-to request) ; ERROR!
  2265. (woman-if-body request woman0-if-to (eq c negated)))))
  2266. ;; request is not used dynamically by any callees.
  2267. (defun woman-if-body (request to delete) ; should be reversed as `accept'?
  2268. "Process if-body, including \\{ ... \\}.
  2269. REQUEST is the invoking directive without the leading dot.
  2270. If TO is non-nil then delete the if-body.
  2271. If DELETE is non-nil then delete from point."
  2272. ;; Assume concealed newlines already processed.
  2273. (let ((from (point)))
  2274. (if to (delete-region (point) to))
  2275. (delete-horizontal-space)
  2276. (cond (;;(looking-at "[^{\n]*\\\\{\\s *") ; multi-line
  2277. ;; allow escaped newlines:
  2278. (looking-at "[^{\n]*\\(\\\\\n\\)*\\\\{\\s *\\(\\\\\n\\)*") ; multi-line
  2279. ;; including preceding .if(s) and following newline
  2280. (let ((from (point)))
  2281. (woman-delete-match 0)
  2282. ;; Allow for nested \{ ... \} -- BUT BEWARE that this
  2283. ;; algorithm only supports one level of nesting!
  2284. (while
  2285. (and (re-search-forward
  2286. ;; "\\(\\\\{\\)\\|\\(\n[.']\\)?[ \t]*\\\\}[ \t]*"
  2287. ;; Interpret bogus `el \}' as `el \{',
  2288. ;; especially for Tcl/Tk man pages:
  2289. "\\(\\\\{\\|el[ \t]*\\\\}\\)\\|\\(\n[.']\\)?[ \t]*\\\\}[ \t]*")
  2290. (match-beginning 1))
  2291. (re-search-forward "\\\\}"))
  2292. (delete-region (if delete from (match-beginning 0)) (point))
  2293. (if (looking-at "^$") (delete-char 1))
  2294. ))
  2295. (delete (woman-delete-line 1))) ; single-line
  2296. ;; Process matching .el anything:
  2297. (cond ((string= request "ie")
  2298. ;; Discard unless previous .ie c `evaluated to false'.
  2299. ;; IIUC, an .ie must be followed by an .el.
  2300. ;; (An if with no else uses .if rather than .ie.)
  2301. ;; TODO warn if no .el found?
  2302. ;; The .el should come immediately after the .ie (modulo
  2303. ;; comments etc), but this searches to eob.
  2304. (cond ((re-search-forward "^[.'][ \t]*el[ \t]*" nil t)
  2305. (woman-delete-match 0)
  2306. (woman-if-body "el" nil (not delete)))))
  2307. ;;; FIXME neither the comment nor the code here make sense to me.
  2308. ;;; This branch was executed for an else (any else, AFAICS).
  2309. ;;; At this point, the else in question has already been processed above.
  2310. ;;; The re-search will find the _next_ else, if there is one, and
  2311. ;;; delete it. If there is one, it belongs to another if block. (Bug#9447)
  2312. ;;; woman0-el does not need this bit either.
  2313. ;; Got here after processing a single-line `.ie' as a body
  2314. ;; clause to be discarded:
  2315. ;;; ((string= request "el")
  2316. ;;; (cond ((re-search-forward "^[.'][ \t]*el[ \t]*" nil t)
  2317. ;;; (woman-delete-match 0)
  2318. ;;; (woman-if-body "el" nil t)))))
  2319. )
  2320. (goto-char from)))
  2321. (defun woman0-el ()
  2322. "Isolated .el request -- should not happen!"
  2323. (WoMan-warn "el request without matching `ie' rejected!")
  2324. (cond (woman-ignore
  2325. (woman-delete-match 0)
  2326. (delete-horizontal-space)
  2327. (woman-if-body "el" nil t))
  2328. (t ; Ignore -- leave in buffer
  2329. ;; This does not work too well, but it's only for debugging!
  2330. (skip-chars-forward "^ \t")
  2331. (if (looking-at "[ \t]*\\{") (search-forward "\\}"))
  2332. (forward-line 1))))
  2333. ;; request is not used dynamically by any callees.
  2334. (defun woman-if-ignore (to request)
  2335. "Ignore but warn about an if request ending at TO, named REQUEST."
  2336. (WoMan-warn-ignored request "ignored -- condition not handled!")
  2337. (if woman-ignore
  2338. (woman-if-body request to t)
  2339. ;; Ignore -- leave in buffer
  2340. ;; This does not work too well, but it's only for debugging!
  2341. (skip-chars-forward "^ \t")
  2342. (if (looking-at "[ \t]*\\{") (search-forward "\\}"))
  2343. (forward-line 1)))
  2344. (defun woman0-so ()
  2345. ".so filename -- Switch source file. `.so' requests may be nested."
  2346. ;; Leaves point at start of new text.
  2347. ;; (skip-chars-forward " \t")
  2348. (let* ((beg (point))
  2349. (end (progn (woman-forward-arg 'unquote) (point)))
  2350. (name (buffer-substring beg end))
  2351. (filename name))
  2352. ;; If the specified file does not exist in this ...
  2353. (or (file-exists-p filename)
  2354. ;; or the parent directory ...
  2355. (file-exists-p
  2356. (setq filename (concat "../" name)))
  2357. ;; then use the WoMan search mechanism to find the filename ...
  2358. (setq filename
  2359. (woman-file-name
  2360. (file-name-sans-extension
  2361. (file-name-nondirectory name))))
  2362. ;; Cannot find the file, so ...
  2363. (kill-buffer (current-buffer))
  2364. (error "File `%s' not found" name))
  2365. (beginning-of-line)
  2366. (woman-delete-line 1)
  2367. (let* ((from (point))
  2368. (length (woman-insert-file-contents filename 0))
  2369. (to (copy-marker (+ from length) t)))
  2370. (woman-pre-process-region from to)
  2371. (set-marker to nil)
  2372. (goto-char from))))
  2373. ;;; Process macro definitions:
  2374. (defun woman0-rn ()
  2375. "Process .rn xx yy -- rename macro xx to yy."
  2376. ;; For now, done backwards AFTER all macro expansion.
  2377. ;; Should also allow requests and strings to be renamed!
  2378. (if (eolp) ; ignore if no argument
  2379. ()
  2380. (let* ((beg (point))
  2381. (end (progn (woman-forward-arg 'unquote 'concat) (point)))
  2382. (old (buffer-substring beg end))
  2383. new)
  2384. (if (eolp) ; ignore if no argument
  2385. ()
  2386. (setq beg (point)
  2387. end (progn (woman-forward-arg 'unquote) (point))
  2388. new (buffer-substring beg end)
  2389. woman0-rename-alist (cons (cons new old) woman0-rename-alist)))))
  2390. (woman-delete-whole-line))
  2391. (defun woman0-rename ()
  2392. "Effect renaming required by .rn requests."
  2393. ;; For now, do this backwards AFTER all macro expansion.
  2394. (dolist (new woman0-rename-alist)
  2395. (let ((old (cdr new))
  2396. (new (car new)))
  2397. (goto-char (point-min))
  2398. (setq new (concat "^[.'][ \t]*" (regexp-quote new)))
  2399. (setq old (concat "." old))
  2400. (while (re-search-forward new nil t)
  2401. (replace-match old nil t)))))
  2402. (defconst woman-unescape-regex
  2403. (concat woman-escaped-escape-string
  2404. "\\(" woman-escaped-escape-string "\\)?"))
  2405. (defsubst woman-unescape (macro)
  2406. "Replace escape sequences in the body of MACRO.
  2407. Replaces || by |, but | by \, where | denotes the internal escape."
  2408. (let (start)
  2409. (while (setq start (string-match woman-unescape-regex macro start))
  2410. (setq macro
  2411. (if (match-beginning 1)
  2412. (replace-match "" t t macro 1)
  2413. (replace-match "\\" t t macro))
  2414. start (1+ start)))
  2415. macro))
  2416. (defun woman0-de (&optional append)
  2417. "Process .de/am xx yy -- (re)define/append macro xx; end at `..'.
  2418. \(Should be up to call of yy, which defaults to `.')
  2419. Optional argument APPEND, if non-nil, means append macro."
  2420. ;; Modeled on woman-strings. BEWARE: Processing of .am is a hack!
  2421. ;; Add support for .rm?
  2422. ;; (skip-chars-forward " \t")
  2423. (if (eolp) ; ignore if no argument
  2424. ()
  2425. (looking-at "[^ \t\n]+") ; macro name
  2426. (let* ((macro (match-string 0)) from
  2427. (previous (assoc macro woman0-macro-alist)))
  2428. (if (not previous)
  2429. (setq woman0-search-regex-start
  2430. (concat woman0-search-regex-start "\\|" (regexp-quote macro))
  2431. woman0-search-regex
  2432. (concat woman0-search-regex-start woman0-search-regex-end)
  2433. ))
  2434. ;; Macro body runs from start of next line to line
  2435. ;; beginning with `..'."
  2436. ;; The terminal request MUST begin with `.' (not ')!
  2437. (forward-line)
  2438. (setq from (point))
  2439. (re-search-forward "^\\.[ \t]*\\.")
  2440. (beginning-of-line)
  2441. (let ((body (woman-unescape (buffer-substring from (point)))))
  2442. (if (and append previous)
  2443. (setq previous (cdr previous)
  2444. body (concat body (cdr previous))
  2445. append (car previous)
  2446. ))
  2447. (setq macro (cons macro (cons append body))))
  2448. ;; This should be an update, but consing a new string
  2449. ;; onto the front of the alist has the same effect:
  2450. (setq woman0-macro-alist (cons macro woman0-macro-alist))
  2451. (forward-line)
  2452. (delete-region from (point))
  2453. (backward-char))) ; return to end of .de/am line
  2454. (beginning-of-line) ; delete .de/am line
  2455. (woman-delete-line 1))
  2456. ;; request may be used dynamically (woman-interpolate-macro calls
  2457. ;; woman-forward-arg).
  2458. (defun woman0-macro (woman-request)
  2459. "Process the macro call named WOMAN-REQUEST."
  2460. ;; Leaves point at start of new text.
  2461. (let ((macro (assoc woman-request woman0-macro-alist)))
  2462. (if macro
  2463. (woman-interpolate-macro (cdr macro))
  2464. ;; SHOULD DELETE THE UNINTERPRETED REQUEST!!!!!
  2465. ;; Output this message once only per call (cf. strings)?
  2466. (WoMan-warn "Undefined macro %s not interpolated!" woman-request))))
  2467. (defun woman-interpolate-macro (macro)
  2468. "Interpolate (.de) or append (.am) expansion of MACRO into the buffer."
  2469. ;; Could make this more efficient by checking which arguments are
  2470. ;; actually used in the expansion!
  2471. (skip-chars-forward " \t")
  2472. ;; Process arguments:
  2473. (let ((argno 0) (append (car macro))
  2474. argno-string formal-arg from actual-arg start)
  2475. (setq macro (cdr macro))
  2476. (while (not (eolp))
  2477. ;; Get next actual arg:
  2478. (setq argno (1+ argno))
  2479. (setq argno-string (format "%d" argno))
  2480. (setq formal-arg (concat "\\\\\\$" argno-string)) ; regexp
  2481. (setq from (point))
  2482. (woman-forward-arg 'unquote 'noskip)
  2483. (setq actual-arg (buffer-substring from (point)))
  2484. (skip-chars-forward " \t") ; now skip following whitespace!
  2485. ;; Replace formal arg with actual arg:
  2486. (setq start nil)
  2487. (while (setq start (string-match formal-arg macro start))
  2488. (setq macro (replace-match actual-arg t t macro))))
  2489. ;; Delete any remaining formal arguments:
  2490. (setq start nil)
  2491. (while
  2492. (setq start (string-match "\\\\\\$." macro start))
  2493. (setq macro (replace-match "" t t macro)))
  2494. ;; Replace .$ number register with actual arg:
  2495. ;; (Do this properly via register mechanism later!)
  2496. (setq start nil)
  2497. (while
  2498. (setq start (string-match "\\\\n(\\.\\$" macro start)) ; regexp
  2499. (setq macro (replace-match argno-string t t macro)))
  2500. (if append
  2501. (forward-char)
  2502. (beginning-of-line)
  2503. (woman-delete-line 1))
  2504. (save-excursion ; leave point at start of new text
  2505. (insert macro))))
  2506. ;;; Process strings:
  2507. (defun woman-match-name ()
  2508. "Match and move over name of form: x, (xx or [xxx...].
  2509. Applies to number registers, fonts, strings/macros/diversions, and
  2510. special characters."
  2511. (cond ((= (following-char) ?\[ )
  2512. (forward-char)
  2513. (re-search-forward "[^]]+")
  2514. (forward-char)) ; skip closing ]
  2515. ((= (following-char) ?\( )
  2516. (forward-char)
  2517. (re-search-forward ".."))
  2518. (t (re-search-forward "."))))
  2519. (defun woman-strings (&optional to)
  2520. "Process ?roff string requests and escape sequences up to buffer position TO.
  2521. Strings are defined/updated by `.ds xx string' requests and
  2522. interpolated by `\*x' and `\*(xx' escapes."
  2523. ;; Add support for .as and .rm?
  2524. (while
  2525. ;; Find .ds requests and \* escapes:
  2526. (re-search-forward "\\(^[.'][ \t]*ds\\)\\|\\\\\\*" to t)
  2527. (cond ((match-beginning 1) ; .ds
  2528. (skip-chars-forward " \t")
  2529. (if (eolp) ; ignore if no argument
  2530. ()
  2531. (re-search-forward "[^ \t\n]+")
  2532. (let ((string (match-string 0)))
  2533. (skip-chars-forward " \t")
  2534. ; (setq string
  2535. ; (cons string
  2536. ; ;; hack (?) for CGI.man!
  2537. ; (cond ((looking-at "\"\"") "\"")
  2538. ; ((looking-at ".*") (match-string 0)))
  2539. ; ))
  2540. ;; Above hack causes trouble in arguments!
  2541. (looking-at ".*")
  2542. (setq string (cons string (match-string 0)))
  2543. ;; This should be an update, but consing a new string
  2544. ;; onto the front of the alist has the same effect:
  2545. (setq woman-string-alist (cons string woman-string-alist))
  2546. ))
  2547. (beginning-of-line)
  2548. (woman-delete-line 1))
  2549. (t ; \*
  2550. (let ((beg (match-beginning 0)))
  2551. (woman-match-name)
  2552. (let* ((stringname (match-string 0))
  2553. (string (assoc stringname woman-string-alist)))
  2554. (cond (string
  2555. (delete-region beg (point))
  2556. ;; Temporary hack in case string starts with a
  2557. ;; control character:
  2558. (if (bolp) (insert-before-markers "\\&"))
  2559. (insert-before-markers (cdr string)))
  2560. (t
  2561. (WoMan-warn "Undefined string %s not interpolated!"
  2562. stringname)
  2563. (cond (woman-ignore
  2564. ;; Output above message once only per call
  2565. (delete-region beg (point))
  2566. (setq woman-string-alist
  2567. (cons (cons stringname "")
  2568. woman-string-alist))))))))))))
  2569. ;;; Process special character escapes \(xx:
  2570. (defconst woman-special-characters
  2571. ;; To be built heuristically as required!
  2572. ;; MUST insert all characters as strings for correct conversion to
  2573. ;; multibyte representation!
  2574. '(("em" "--" "\276" . t) ; 3/4 Em dash
  2575. ("bu" "*" "\267" . t) ; bullet
  2576. ("fm" "'") ; foot mark
  2577. ("co" "(C)" "\251") ; copyright
  2578. ("pl" "+" "+" . t) ; math plus
  2579. ("mi" "-" "-" . t) ; math minus
  2580. ("**" "*" "*" . t) ; math star
  2581. ("aa" "'" "\242" . t) ; acute accent
  2582. ("ul" "_") ; underrule
  2583. ("*S" "Sigma" "S" . t) ; Sigma
  2584. (">=" ">=" "\263" . t) ; >=
  2585. ("<=" "<=" "\243" . t) ; <=
  2586. ("->" "->" "\256" . t) ; right arrow
  2587. ("<-" "<-" "\254" . t) ; left arrow
  2588. ("mu" " x " "\264" . t) ; multiply
  2589. ("+-" "+/-" "\261" . t) ; plus-minus
  2590. ("bv" "|") ; bold vertical
  2591. ;; groff etc. extensions:
  2592. ;; List these via eg man -Tdvi groff_char > groff_char.dvi.
  2593. ("lq" "\"")
  2594. ("rq" "\"")
  2595. ("aq" "'")
  2596. ("ha" "^")
  2597. ("ti" "~")
  2598. ("oq" "‘") ; u2018
  2599. ("cq" "’") ; u2019
  2600. ("hy" "‐") ; u2010
  2601. )
  2602. "Alist of special character codes with ASCII and extended-font equivalents.
  2603. Each alist elements has the form
  2604. (input-string ascii-string extended-font-string . use-symbol-font)
  2605. where
  2606. * `\\(input-string' is the ?roff encoding,
  2607. * `ascii-string' is the (multi-character) ASCII simulation,
  2608. * `extended-font-string' is the single-character string representing
  2609. the character position in the extended 256-character font, and
  2610. * `use-symbol-font' is t to indicate use of the symbol font or nil,
  2611. i.e. omitted, to indicate use of the default font.
  2612. Any element may be nil. Avoid control character codes (0 to \\37, \\180
  2613. to \\237) in `extended-font-string' for now, since they can be
  2614. displayed only with a modified display table.
  2615. Use the WoMan command `woman-display-extended-fonts' or a character
  2616. map accessory to help construct this alist.")
  2617. (defsubst woman-replace-match (newtext &optional face)
  2618. "Replace text matched by last search with NEWTEXT and return t.
  2619. Set NEWTEXT in face FACE if specified."
  2620. (woman-delete-match 0)
  2621. (insert-before-markers newtext)
  2622. (if face (put-text-property (1- (point)) (point) 'face 'woman-symbol))
  2623. t)
  2624. (defun woman-special-characters (to)
  2625. "Process special character escapes \\(xx, \\[xxx] up to buffer position TO.
  2626. \(This must be done AFTER translation, which may use special characters.)"
  2627. (while (re-search-forward "\\\\\\(?:(\\(..\\)\\|\\[\\([[^]]+\\)\\]\\)" to t)
  2628. (let* ((name (or (match-string-no-properties 1)
  2629. (match-string-no-properties 2)))
  2630. (replacement (assoc name woman-special-characters)))
  2631. (unless
  2632. (and
  2633. replacement
  2634. (cond ((and (cddr replacement)
  2635. (if (nthcdr 3 replacement)
  2636. ;; Need symbol font:
  2637. (if woman-use-symbol-font
  2638. (woman-replace-match (nth 2 replacement)
  2639. 'woman-symbol))
  2640. ;; Need extended font:
  2641. (if woman-use-extended-font
  2642. (woman-replace-match (nth 2 replacement))))))
  2643. ((cadr replacement) ; Use ASCII simulation
  2644. (woman-replace-match (cadr replacement)))))
  2645. (WoMan-warn (concat "Special character "
  2646. (if (match-beginning 1) "\\(%s" "\\[%s]")
  2647. " not interpolated!") name)
  2648. (if woman-ignore (woman-delete-match 0))))))
  2649. (defun woman-display-extended-fonts ()
  2650. "Display table of glyphs of graphic characters and their octal codes.
  2651. All the octal codes in the ranges [32..127] and [160..255] are displayed
  2652. together with the corresponding glyphs from the default and symbol fonts.
  2653. Useful for constructing the alist variable `woman-special-characters'."
  2654. (interactive)
  2655. (with-output-to-temp-buffer "*WoMan Extended Font Map*"
  2656. (with-current-buffer standard-output
  2657. (let ((i 32))
  2658. (while (< i 256)
  2659. (insert (format "\\%03o " i) (string i) " " (string i))
  2660. (put-text-property (1- (point)) (point)
  2661. 'face 'woman-symbol)
  2662. (insert " ")
  2663. (setq i (1+ i))
  2664. (when (= i 128) (setq i 160) (insert "\n"))
  2665. (if (zerop (% i 8)) (insert "\n")))))
  2666. (help-print-return-message)))
  2667. ;;; Formatting macros that do not cause a break:
  2668. ;; Bound locally by woman[012]-roff-buffer, and also, annoyingly and
  2669. ;; confusingly, as a function argument. Use dynamically in
  2670. ;; woman-unquote and woman-forward-arg.
  2671. (defvar woman-request)
  2672. (defun woman-unquote (to)
  2673. "Delete any double-quote characters between point and TO.
  2674. Leave point at TO (which should be a marker)."
  2675. (let (in-quote)
  2676. (while (search-forward "\"" to 1)
  2677. (if (and in-quote (looking-at "\""))
  2678. ;; Repeated double-quote represents single double-quote
  2679. (delete-char 1)
  2680. (if (or in-quote (looking-at ".*\"")) ; paired
  2681. (delete-char -1))
  2682. (setq in-quote (not in-quote))
  2683. ))
  2684. (if in-quote
  2685. (WoMan-warn "Unpaired \" in .%s arguments." woman-request))))
  2686. (defsubst woman-unquote-args ()
  2687. "Delete any double-quote characters up to the end of the line."
  2688. (woman-unquote (save-excursion (end-of-line) (point-marker))))
  2689. (defun woman1-roff-buffer ()
  2690. "Process non-breaking requests."
  2691. (let ((case-fold-search t)
  2692. woman-request fn woman1-unquote)
  2693. (while
  2694. ;; Find next control line:
  2695. (re-search-forward woman-request-regexp nil t)
  2696. (cond
  2697. ;; Construct woman function to call:
  2698. ((setq fn (intern-soft
  2699. (concat "woman1-"
  2700. (setq woman-request (match-string 1)))))
  2701. (if (get fn 'notfont) ; not a font-change request
  2702. (funcall fn)
  2703. ;; Delete request or macro name:
  2704. (woman-delete-match 0)
  2705. ;; If no args then apply to next line else unquote args
  2706. ;; (woman1-unquote is used by called function):
  2707. (setq woman1-unquote (not (eolp)))
  2708. (if (eolp) (delete-char 1))
  2709. ; ;; Hide leading control character in unquoted argument:
  2710. ; (cond ((memq (following-char) '(?. ?'))
  2711. ; (insert "\\&")
  2712. ; (beginning-of-line)))
  2713. ;; Call the appropriate function:
  2714. (funcall fn)
  2715. ;; Hide leading control character in quoted argument (only):
  2716. (if (and woman1-unquote (memq (following-char) '(?. ?')))
  2717. (insert "\\&"))))))))
  2718. ;;; Font-changing macros:
  2719. (defun woman1-B ()
  2720. ".B -- Set words of current line in bold font."
  2721. (woman1-B-or-I ".ft B\n"))
  2722. (defun woman1-I ()
  2723. ".I -- Set words of current line in italic font."
  2724. (woman1-B-or-I ".ft I\n"))
  2725. (defvar woman1-unquote) ; bound locally by woman1-roff-buffer
  2726. (defun woman1-B-or-I (B-or-I)
  2727. ".B/I -- Set words of current line in bold/italic font.
  2728. B-OR-I is the appropriate complete control line."
  2729. ;; Should NOT concatenate the arguments!
  2730. (insert B-or-I) ; because it might be a control line
  2731. ;; Return to bol to process .SM/.B, .B/.if etc.
  2732. ;; or start of first arg to hide leading control char.
  2733. (save-excursion
  2734. (if woman1-unquote
  2735. (woman-unquote-args)
  2736. (while (looking-at "^[.']") (forward-line))
  2737. (end-of-line)
  2738. (delete-horizontal-space))
  2739. (insert "\\fR")))
  2740. (defun woman1-SM ()
  2741. ".SM -- Set the current line in small font, i.e. IGNORE!"
  2742. nil)
  2743. (defalias 'woman1-SB 'woman1-B)
  2744. ;; .SB -- Set the current line in small bold font, i.e. just embolden!
  2745. ;; (This is what /usr/local/share/groff/tmac/tmac.an does. The
  2746. ;; Linux man.7 is wrong about this!)
  2747. (defun woman1-BI ()
  2748. ".BI -- Join words of current line alternating bold and italic fonts."
  2749. (woman1-alt-fonts (list "\\fB" "\\fI")))
  2750. (defun woman1-BR ()
  2751. ".BR -- Join words of current line alternating bold and Roman fonts."
  2752. (woman1-alt-fonts (list "\\fB" "\\fR")))
  2753. (defun woman1-IB ()
  2754. ".IB -- Join words of current line alternating italic and bold fonts."
  2755. (woman1-alt-fonts (list "\\fI" "\\fB")))
  2756. (defun woman1-IR ()
  2757. ".IR -- Join words of current line alternating italic and Roman fonts."
  2758. (woman1-alt-fonts (list "\\fI" "\\fR")))
  2759. (defun woman1-RB ()
  2760. ".RB -- Join words of current line alternating Roman and bold fonts."
  2761. (woman1-alt-fonts (list "\\fR" "\\fB")))
  2762. (defun woman1-RI ()
  2763. ".RI -- Join words of current line alternating Roman and italic fonts."
  2764. (woman1-alt-fonts (list "\\fR" "\\fI")))
  2765. (defun woman1-alt-fonts (fonts)
  2766. "Join words using alternating fonts in FONTS, which MUST be a dynamic list."
  2767. (nconc fonts fonts) ; circular list!
  2768. (insert (car fonts))
  2769. ;; Return to start of first arg to hide leading control char:
  2770. (save-excursion
  2771. (setq fonts (cdr fonts))
  2772. ;; woman1-unquote is bound in woman1-roff-buffer.
  2773. (woman-forward-arg woman1-unquote 'concat)
  2774. (while (not (eolp))
  2775. (insert (car fonts))
  2776. (setq fonts (cdr fonts))
  2777. (woman-forward-arg woman1-unquote 'concat))
  2778. (insert "\\fR")))
  2779. (defun woman-forward-arg (&optional unquote concat)
  2780. "Move forward over one ?roff argument, optionally unquoting and/or joining.
  2781. If optional arg UNQUOTE is non-nil then delete any argument quotes.
  2782. If optional arg CONCAT is non-nil then join arguments."
  2783. (if (eq (following-char) ?\")
  2784. (progn
  2785. (if unquote (delete-char 1) (forward-char))
  2786. (re-search-forward "\"\\|$")
  2787. ;; Repeated double-quote represents single double-quote
  2788. (while (eq (following-char) ?\") ; paired
  2789. (if unquote (delete-char 1) (forward-char))
  2790. (re-search-forward "\"\\|$"))
  2791. (if (eq (preceding-char) ?\")
  2792. (if unquote (delete-char -1))
  2793. (WoMan-warn "Unpaired \" in .%s arguments." woman-request)))
  2794. ;; (re-search-forward "[^\\\n] \\|$") ; inconsistent
  2795. (skip-syntax-forward "^ "))
  2796. (cond ((null concat) (skip-chars-forward " \t")) ; don't skip eol!
  2797. ((eq concat 'noskip)) ; do not skip following whitespace
  2798. (t (woman-delete-following-space))))
  2799. ;; The following requests are not explicit font-change requests and
  2800. ;; so are flagged `notfont' to turn off automatic request deletion
  2801. ;; and further processing.
  2802. (put 'woman1-TP 'notfont t)
  2803. (defun woman1-TP ()
  2804. ".TP -- After tag line, reset font to Roman for paragraph body."
  2805. ;; Same for .IP, but forward only 1 line?
  2806. (save-excursion
  2807. ;; May be an `irrelevant' control line in the way, so ...
  2808. (forward-line)
  2809. (forward-line (if (looking-at "\\.\\S-+[ \t]*$") 2 1))
  2810. ;; May be looking at control line, so ...
  2811. (insert ".ft R\n")))
  2812. (put 'woman1-ul 'notfont t)
  2813. (defun woman1-ul ()
  2814. ".ul N -- Underline (italicize) the next N input lines, default N = 1."
  2815. (let ((N (if (eolp) 1 (woman-parse-numeric-arg)))) ; woman-get-numeric-arg ?
  2816. (woman-delete-whole-line)
  2817. (insert ".ft I\n")
  2818. (forward-line N)
  2819. (insert ".ft R\n")))
  2820. ;;; Other non-breaking requests:
  2821. ;; Hyphenation
  2822. ;; Warnings commented out.
  2823. (put 'woman1-nh 'notfont t)
  2824. (defun woman1-nh ()
  2825. ".nh -- No hyphenation, i.e. IGNORE!"
  2826. ;; Must be handled here to avoid breaking!
  2827. ;; (WoMan-log-1 ".nh request ignored -- hyphenation not supported!")
  2828. (woman-delete-whole-line))
  2829. (put 'woman1-hy 'notfont t)
  2830. (defun woman1-hy ()
  2831. ".hy N -- Set hyphenation mode to N, i.e. IGNORE!"
  2832. ;; (WoMan-log-1 ".hy request ignored -- hyphenation not supported!")
  2833. (woman-delete-whole-line))
  2834. (put 'woman1-hc 'notfont t)
  2835. (defun woman1-hc ()
  2836. ".hc c -- Set hyphenation character to c, i.e. delete it!"
  2837. (let ((c (char-to-string (following-char))))
  2838. ;; (WoMan-log
  2839. ;; "Hyphenation character %s deleted -- hyphenation not supported!" c)
  2840. (woman-delete-whole-line)
  2841. (setq c (concat "\\(" c "\\)\\|^[.'][ \t]*hc"))
  2842. (save-excursion
  2843. (while (and (re-search-forward c nil t)
  2844. (match-beginning 1))
  2845. (delete-char -1)))))
  2846. (put 'woman1-hw 'notfont t)
  2847. (defun woman1-hw ()
  2848. ".hw words -- Set hyphenation exception words, i.e. IGNORE!"
  2849. ;; (WoMan-log-1 ".hw request ignored -- hyphenation not supported!")
  2850. (woman-delete-whole-line))
  2851. ;;; Other non-breaking requests correctly ignored by nroff:
  2852. (put 'woman1-ps 'notfont t)
  2853. (defalias 'woman1-ps 'woman-delete-whole-line)
  2854. ;; .ps -- Point size -- IGNORE!
  2855. (put 'woman1-ss 'notfont t)
  2856. (defalias 'woman1-ss 'woman-delete-whole-line)
  2857. ;; .ss -- Space-character size -- IGNORE!
  2858. (put 'woman1-cs 'notfont t)
  2859. (defalias 'woman1-cs 'woman-delete-whole-line)
  2860. ;; .cs -- Constant character space (width) mode -- IGNORE!
  2861. (put 'woman1-ne 'notfont t)
  2862. (defalias 'woman1-ne 'woman-delete-whole-line)
  2863. ;; .ne -- Need vertical space -- IGNORE!
  2864. (put 'woman1-vs 'notfont t)
  2865. (defalias 'woman1-vs 'woman-delete-whole-line)
  2866. ;; .vs -- Vertical base line spacing -- IGNORE!
  2867. (put 'woman1-bd 'notfont t)
  2868. (defalias 'woman1-bd 'woman-delete-whole-line)
  2869. ;; .bd -- Embolden font -- IGNORE!
  2870. ;;; Non-breaking SunOS-specific macros:
  2871. (defun woman1-TX ()
  2872. ".TX t p -- Resolve SunOS abbrev t and join to p (usually punctuation)."
  2873. (insert "SunOS ")
  2874. (woman-forward-arg 'unquote 'concat))
  2875. (put 'woman1-IX 'notfont t)
  2876. (defalias 'woman1-IX 'woman-delete-whole-line)
  2877. ;; .IX -- Index macro, for Sun internal use -- IGNORE!
  2878. ;;; Direct font selection:
  2879. (defconst woman-font-alist
  2880. '(("R" . default)
  2881. ("I" . woman-italic)
  2882. ("B" . woman-bold)
  2883. ("P" . previous)
  2884. ("1" . default)
  2885. ("2" . woman-italic)
  2886. ("3" . woman-bold) ; used in bash.1
  2887. )
  2888. "Alist of ?roff font indicators and woman font variables and names.")
  2889. (defun woman-change-fonts ()
  2890. "Process font changes."
  2891. ;; ***** NEEDS REVISING IF IT WORKS OK *****
  2892. ;; Paragraph .LP/PP/HP/IP/TP and font .B/.BI etc. macros reset font.
  2893. ;; Should .SH/.SS reset font?
  2894. ;; Font size setting macros (?) should reset font.
  2895. (let ((font-alist woman-font-alist) ; for local updating
  2896. (previous-pos (point))
  2897. (previous-font 'default)
  2898. (current-font 'default))
  2899. (while
  2900. ;; Find font requests, paragraph macros and font escapes:
  2901. (re-search-forward
  2902. "^[.'][ \t]*\\(\\(\\ft\\)\\|\\(.P\\)\\)\\|\\(\\\\f\\)" nil 1)
  2903. (let (font beg notfont fescape)
  2904. ;; Match font indicator and leave point at end of sequence:
  2905. (cond ((match-beginning 2)
  2906. ;; .ft request found
  2907. (setq beg (match-beginning 0))
  2908. (skip-chars-forward " \t")
  2909. (if (eolp) ; default is previous font
  2910. (setq font previous-font)
  2911. (looking-at "[^ \t\n]+"))
  2912. (forward-line)) ; end of control line and \n
  2913. ((match-beginning 3)
  2914. ;; Macro that resets font found
  2915. (setq font 'default))
  2916. ((match-beginning 4)
  2917. ;; \f escape found
  2918. (setq beg (match-beginning 0)
  2919. fescape t)
  2920. (woman-match-name))
  2921. (t (setq notfont t)))
  2922. (unless notfont
  2923. ;; Get font name:
  2924. (or font
  2925. (let ((fontstring (match-string 0)))
  2926. (setq font (assoc fontstring font-alist)
  2927. ;; NB: font-alist contains VARIABLE NAMES.
  2928. font (if font
  2929. (cdr font)
  2930. (WoMan-warn "Unknown font %s." fontstring)
  2931. ;; Output this message once only per call ...
  2932. (setq font-alist
  2933. (cons (cons fontstring 'woman-unknown)
  2934. font-alist))
  2935. 'woman-unknown)
  2936. )))
  2937. ;; Delete font control line or escape sequence:
  2938. (cond (beg (delete-region beg (point))
  2939. (if (eq font 'previous) (setq font previous-font))))
  2940. ;; Deal with things like \fB.cvsrc\fR at the start of a line.
  2941. ;; After removing the font control codes, this would
  2942. ;; otherwise match woman-request-regexp. The "\\&" which is
  2943. ;; inserted to prevent this is removed by woman2-process-escapes.
  2944. (and fescape
  2945. (looking-at woman-request-regexp)
  2946. (insert "\\&"))
  2947. (woman-set-face previous-pos (point) current-font)
  2948. (if beg
  2949. ;; Explicit font control
  2950. (setq previous-pos (point)
  2951. previous-font current-font)
  2952. ;; Macro that resets font
  2953. ;; (forward-line) ; DOES NOT WORK! but unnecessary?
  2954. ;; Must process font changes in any paragraph tag!
  2955. (setq previous-pos (point)
  2956. previous-font 'default))
  2957. (setq current-font font)
  2958. )))
  2959. ;; Set font after last request up to eob:
  2960. (woman-set-face previous-pos (point) current-font)))
  2961. (defun woman-set-face (from to face)
  2962. "Set the face of the text from FROM to TO to face FACE.
  2963. Ignore the default face and underline only word characters."
  2964. (or (eq face 'default) ; ignore
  2965. (not woman-fontify)
  2966. (if (face-underline-p face)
  2967. (save-excursion
  2968. (let ((face-no-ul (intern (concat (symbol-name face) "-no-ul"))))
  2969. (goto-char from)
  2970. (while (< (point) to)
  2971. (skip-syntax-forward "w" to)
  2972. (put-text-property from (point) 'face face)
  2973. (setq from (point))
  2974. (skip-syntax-forward "^w" to)
  2975. (put-text-property from (point) 'face face-no-ul)
  2976. (setq from (point))
  2977. )))
  2978. (put-text-property from to 'face face))))
  2979. ;;; Output translation:
  2980. ;; This is only set by woman2-tr. It is bound locally in woman2-roff-buffer.
  2981. ;; It is also used by woman-translate. woman-translate may be called
  2982. ;; outside the scope of woman2-roff-buffer (by experiment). Therefore
  2983. ;; this used to be globally bound to nil, to avoid an error. Instead
  2984. ;; we can use bound-and-true-p in woman-translate.
  2985. (defvar woman-translations)
  2986. ;; A list of the form (\"[ace]\" (a . b) (c . d) (e . ?\ )) or nil.
  2987. (defun woman-get-next-char ()
  2988. "Return and delete next char in buffer, including special chars."
  2989. (if ;;(looking-at "\\\\(\\(..\\)")
  2990. ;; Match special \(xx and strings \*[xxx], \*(xx, \*x:
  2991. (looking-at "\\\\\\((..\\|\\*\\(\\[[^]]+\\]\\|(..\\|.\\)\\)")
  2992. (prog1 (match-string 0)
  2993. (woman-delete-match 0))
  2994. (prog1 (char-to-string (following-char))
  2995. (delete-char 1))))
  2996. (defun woman2-tr (to)
  2997. ".tr abcde -- Translate a -> b, c -> d, ..., e -> space.
  2998. Format paragraphs upto TO. Supports special chars.
  2999. \(Breaks, but should not.)"
  3000. ;; This should be an update, but consing onto the front of the alist
  3001. ;; has the same effect and match duplicates should not matter.
  3002. ;; Initialize translation data structures:
  3003. (let ((matches (car woman-translations))
  3004. (alist (cdr woman-translations))
  3005. a b)
  3006. ;; `matches' must be a string:
  3007. (setq matches
  3008. (concat (if matches (substring matches 1 -1)) "]"))
  3009. ;; Process .tr arguments:
  3010. (while (not (eolp)) ; (looking-at "[ \t]*$") ???
  3011. (setq a (woman-get-next-char))
  3012. (if (eolp)
  3013. (setq b " ")
  3014. (setq b (woman-get-next-char)))
  3015. (setq matches
  3016. (if (= (length a) 1)
  3017. (concat a matches)
  3018. (concat matches "\\|\\" a))
  3019. alist (cons (cons a b) alist)))
  3020. (delete-char 1) ; no blank line
  3021. ;; Rebuild translations list:
  3022. (setq matches
  3023. (if (= (string-to-char matches) ?\])
  3024. (substring matches 3)
  3025. (concat "[" matches))
  3026. woman-translations (cons matches alist))
  3027. ;; Format any following text:
  3028. (woman2-format-paragraphs to)))
  3029. (defsubst woman-translate (to)
  3030. "Translate up to marker TO. Do this last of all transformations."
  3031. (if (bound-and-true-p woman-translations)
  3032. (let ((matches (car woman-translations))
  3033. (alist (cdr woman-translations))
  3034. ;; Translations are case-sensitive, eg ".tr ab" does not
  3035. ;; affect "A" (bug#6849).
  3036. (case-fold-search nil))
  3037. (while (re-search-forward matches to t)
  3038. ;; Done like this to retain text properties and
  3039. ;; support translation of special characters:
  3040. (insert-before-markers-and-inherit
  3041. (cdr (assoc
  3042. (buffer-substring-no-properties
  3043. (match-beginning 0) (match-end 0))
  3044. alist)))
  3045. (woman-delete-match 0)))))
  3046. ;;; Registers:
  3047. (defvar woman-registers ; these are all read-only
  3048. '((".H" 24) (".V" 48) ; resolution in basic units
  3049. (".g" 0) ; not groff
  3050. ;; (Iff emulating groff need to implement groff italic correction
  3051. ;; \/, e.g. for pic.1)
  3052. (".i" left-margin) ; current indent
  3053. (".j" woman-adjust) ; current adjustment
  3054. (".l" fill-column) ; current line length
  3055. (".s" 12) ; current point size
  3056. (".u" (if woman-nofill 0 1)) ; 1/0 in fill/nofill mode
  3057. (".v" 48) ; current vertical line spacing
  3058. )
  3059. "Register alist: the key is the register name as a string.
  3060. Each element has the form (KEY VALUE . INC) -- inc may be nil.
  3061. Also bound locally in `woman2-roff-buffer'.")
  3062. (defun woman-mark-horizonal-position ()
  3063. "\\kx -- Store current horizontal position in INPUT LINE in register x."
  3064. (while (re-search-forward "\\\\k\\(.\\)" nil t)
  3065. (goto-char (match-beginning 0))
  3066. (setq woman-registers
  3067. (cons (list (match-string 1) (current-column))
  3068. woman-registers))
  3069. (woman-delete-match 0)))
  3070. (defsubst woman2-process-escapes-to-eol (&optional numeric)
  3071. "Process remaining escape sequences up to eol.
  3072. Handle numeric arguments specially if optional argument NUMERIC is non-nil."
  3073. (woman2-process-escapes (copy-marker (line-end-position) t) numeric))
  3074. (defun woman2-nr (to)
  3075. ".nr R +/-N M -- Assign +/-N (wrt to previous value, if any) to register R.
  3076. The increment for auto-incrementing is set to M.
  3077. Format paragraphs upto TO. (Breaks, but should not!)"
  3078. (let* ((name (buffer-substring
  3079. (point)
  3080. (progn (skip-syntax-forward "^ ") (point))))
  3081. (pm (progn ; increment
  3082. (skip-chars-forward " \t")
  3083. (when (memq (char-after) '(?+ ?-))
  3084. (forward-char) (char-before))))
  3085. (value (if (eolp) ; no value
  3086. nil ; to be interpreted as zero
  3087. (woman2-process-escapes-to-eol 'numeric)
  3088. (woman-parse-numeric-arg)))
  3089. (inc (progn ; auto-increment
  3090. (skip-chars-forward " \t")
  3091. (if (eolp) ; no value
  3092. nil ; to be interpreted as zero ???
  3093. (woman-parse-numeric-arg))))
  3094. (oldvalue (assoc name woman-registers)))
  3095. (when oldvalue
  3096. (setq oldvalue (cdr oldvalue)) ; (value . inc)
  3097. (unless inc (setq inc (cdr oldvalue))))
  3098. (cond ((null value)
  3099. (setq value 0) ; correct?
  3100. (WoMan-warn "nr %s -- null value assigned as zero!" name))
  3101. ((symbolp value)
  3102. (setq value (list 'quote value))))
  3103. (if pm ; increment old value
  3104. (setq oldvalue (if oldvalue (car oldvalue) 0)
  3105. value (if (eq pm ?+)
  3106. (+ oldvalue value)
  3107. (- oldvalue value))))
  3108. (setq woman-registers
  3109. (cons (cons name (cons value inc)) woman-registers))
  3110. (woman-delete-whole-line)
  3111. (woman2-format-paragraphs to)))
  3112. ;;; Numeric (and "non-text") request arguments:
  3113. (defsubst woman-get-numeric-arg ()
  3114. "Get the value of a numeric argument at or after point.
  3115. The argument can include the width function and scale indicators.
  3116. Assumes 10 characters per inch. Does not move point."
  3117. (woman2-process-escapes-to-eol 'numeric)
  3118. (save-excursion (woman-parse-numeric-arg)))
  3119. (defun woman-parse-numeric-arg ()
  3120. "Get the value of a numeric expression at or after point.
  3121. Unlike `woman-get-numeric-arg', leaves point after the argument.
  3122. The expression may be an argument in quotes."
  3123. (if (= (following-char) ?\") (forward-char))
  3124. ;; Allow leading +/-:
  3125. (let ((value (if (looking-at "[+-]") 0 (woman-parse-numeric-value)))
  3126. op)
  3127. (while (cond
  3128. ((looking-at "[+-/*%]") ; arithmetic operators
  3129. (forward-char)
  3130. (setq op (intern-soft (match-string 0)))
  3131. (setq value (funcall op value (woman-parse-numeric-value))))
  3132. ((looking-at "[<=>]=?") ; relational operators
  3133. (goto-char (match-end 0))
  3134. (setq op (intern-soft
  3135. (if (string-equal (match-string 0) "==")
  3136. "="
  3137. (match-string 0))))
  3138. (setq value (if (funcall op value (woman-parse-numeric-value))
  3139. 1 0)))
  3140. ((memq (setq op (following-char)) '(?& ?:)) ; Boolean and / or
  3141. (forward-char)
  3142. (setq value
  3143. ;; and / or are special forms, not functions, in ELisp
  3144. (if (eq op ?&)
  3145. ;; and
  3146. (if (> value 0)
  3147. (if (> (woman-parse-numeric-value) 0) 1 0)
  3148. ;; skip second operand
  3149. (prog1 0 (woman-parse-numeric-value)))
  3150. ;; or
  3151. (if (> value 0)
  3152. ;; skip second operand
  3153. (prog1 1 (woman-parse-numeric-value))
  3154. (if (> (woman-parse-numeric-value) 0) 1 0))
  3155. )))
  3156. ))
  3157. ; (if (looking-at "[ \t\nRC\)\"]") ; R, C are tab types
  3158. ; ()
  3159. ; (WoMan-warn "Unimplemented numerical operator `%c' in %s"
  3160. ; (following-char)
  3161. ; (buffer-substring
  3162. ; (line-beginning-position)
  3163. ; (line-end-position)))
  3164. ; (skip-syntax-forward "^ "))
  3165. value
  3166. ))
  3167. (defun woman-parse-numeric-value ()
  3168. "Get a single numeric value at or after point.
  3169. The value can be a number register or width function (which assumes 10
  3170. characters per inch) and can include scale indicators. It may be an
  3171. expression in parentheses. Leaves point after the value."
  3172. ;; Must replace every \' by some different single character first
  3173. ;; before calling this function by calling
  3174. ;; (woman2-process-escapes-to-eol 'numeric)
  3175. (if (eq (following-char) ?\()
  3176. ;; Treat parenthesized expression as a single value.
  3177. (let (n)
  3178. (forward-char)
  3179. (setq n (woman-parse-numeric-arg))
  3180. (skip-syntax-forward " ")
  3181. (if (eq (following-char) ?\))
  3182. (forward-char)
  3183. (WoMan-warn "Parenthesis confusion in numeric expression!"))
  3184. n)
  3185. (let ((n (cond ((looking-at "[-+]?[.0-9]+") ; single number
  3186. ;; currently needed to set match-end, even though
  3187. ;; string-to-number returns 0 if number not parsed.
  3188. (string-to-number (match-string 0)))
  3189. ((looking-at "\\\\n\\([-+]\\)?\\(?:\
  3190. \\[\\([^]]+\\)\\]\\|\(\\(..\\)\\|\\(.\\)\\)")
  3191. ;; interpolate number register, maybe auto-incremented
  3192. (let* ((pm (match-string-no-properties 1))
  3193. (name (or (match-string-no-properties 2)
  3194. (match-string-no-properties 3)
  3195. (match-string-no-properties 4)))
  3196. (value (assoc name woman-registers)))
  3197. (if value
  3198. (let (inc)
  3199. (setq value (cdr value) ; (value . inc)
  3200. inc (cdr value)
  3201. ;; eval internal (.X) registers
  3202. ;; stored as lisp variable names:
  3203. value (eval (car value)))
  3204. (if (and pm inc) ; auto-increment
  3205. (setq value
  3206. (funcall (intern-soft pm) value inc)
  3207. woman-registers
  3208. (cons (cons name (cons value inc))
  3209. woman-registers)))
  3210. value)
  3211. (WoMan-warn "Undefined register %s defaulted to 0."
  3212. name)
  3213. 0) ; default to zero
  3214. ))
  3215. ((re-search-forward
  3216. ;; Delimiter can be special char escape \[xxx],
  3217. ;; \(xx or single normal char (usually '):
  3218. "\\=\\\\w\\(\\\\\\[[^]]+\\]\\|\\\\(..\\|.\\)" nil t)
  3219. (let ((from (match-end 0))
  3220. (delim (regexp-quote (match-string 1))))
  3221. (if (re-search-forward delim nil t)
  3222. ;; Return width of string:
  3223. (- (match-beginning 0) from)
  3224. (WoMan-warn "Width escape delimiter error!")))))))
  3225. (if (null n)
  3226. ;; ERROR -- should handle this better!
  3227. (progn
  3228. (WoMan-warn "Numeric/register argument error: %s"
  3229. (buffer-substring
  3230. (point)
  3231. (line-end-position)))
  3232. (skip-syntax-forward "^ ")
  3233. 0)
  3234. (goto-char (match-end 0))
  3235. ;; Check for scale factor:
  3236. (if
  3237. (cond
  3238. ((looking-at "\\s ") nil) ; stay put!
  3239. ((looking-at "[mnuv]")) ; ignore for now
  3240. ((looking-at "i") (setq n (* n 10))) ; inch
  3241. ((looking-at "c") (setq n (* n 3.9))) ; cm
  3242. ((looking-at "P") (setq n (* n 1.7))) ; Pica
  3243. ((looking-at "p") (setq n (* n 0.14))) ; point
  3244. ;; NB: May be immediately followed by + or -, etc.,
  3245. ;; in which case do nothing and return nil.
  3246. )
  3247. (goto-char (match-end 0)))
  3248. (if (numberp n) (round n) n)))))
  3249. ;;; VERTICAL FORMATTING -- Formatting macros that cause a break:
  3250. ;; Vertical spacing philosophy:
  3251. ;; Delete all vertical space as it is encountered. Then insert
  3252. ;; vertical space only before text, as required.
  3253. (defun woman2-roff-buffer ()
  3254. "Process breaks. Format paragraphs and headings."
  3255. (let ((case-fold-search t)
  3256. (to (make-marker))
  3257. (canonically-space-region
  3258. (symbol-function 'canonically-space-region))
  3259. (insert-and-inherit (symbol-function 'insert-and-inherit))
  3260. (set-text-properties (symbol-function 'set-text-properties))
  3261. (woman-registers woman-registers)
  3262. fn woman-request woman-translations
  3263. tab-stop-list)
  3264. (set-marker-insertion-type to t)
  3265. ;; ?roff does not squeeze multiple spaces, but does fill, so...
  3266. (fset 'canonically-space-region 'ignore)
  3267. ;; Try to avoid spaces inheriting underlines from preceding text!
  3268. (fset 'insert-and-inherit (symbol-function 'insert))
  3269. (fset 'set-text-properties 'ignore)
  3270. (unwind-protect
  3271. (while
  3272. ;; Find next control line:
  3273. (re-search-forward woman-request-regexp nil t)
  3274. (cond
  3275. ;; Construct woman function to call:
  3276. ((setq fn (intern-soft
  3277. (concat "woman2-"
  3278. (setq woman-request (match-string 1)))))
  3279. ;; Delete request or macro name:
  3280. (woman-delete-match 0))
  3281. ;; Unrecognized request:
  3282. ((prog1 nil
  3283. ;; (WoMan-warn ".%s request ignored!" woman-request)
  3284. (WoMan-warn-ignored woman-request "ignored!")
  3285. ;; (setq fn 'woman2-LP)
  3286. ;; AVOID LEAVING A BLANK LINE!
  3287. ;; (setq fn 'woman2-format-paragraphs)
  3288. ))
  3289. ;; .LP assumes it is at eol and leaves a (blank) line,
  3290. ;; so leave point at end of line before paragraph:
  3291. ((or (looking-at "[ \t]*$") ; no argument
  3292. woman-ignore) ; ignore all
  3293. ;; (beginning-of-line) (kill-line)
  3294. ;; AVOID LEAVING A BLANK LINE!
  3295. (beginning-of-line) (woman-delete-line 1))
  3296. (t (end-of-line) (insert ?\n))
  3297. )
  3298. (if (not (or fn
  3299. (and (not (memq (following-char) '(?. ?')))
  3300. (setq fn 'woman2-format-paragraphs))))
  3301. ()
  3302. ;; Find next control line:
  3303. (set-marker to (woman-find-next-control-line))
  3304. ;; Call the appropriate function:
  3305. (funcall fn to)))
  3306. (if (not (eobp)) ; This should not happen, but ...
  3307. (woman2-format-paragraphs (copy-marker (point-max) t)
  3308. woman-left-margin))
  3309. (fset 'canonically-space-region canonically-space-region)
  3310. (fset 'set-text-properties set-text-properties)
  3311. (fset 'insert-and-inherit insert-and-inherit)
  3312. (set-marker to nil))))
  3313. (defun woman-find-next-control-line ()
  3314. "Find and return start of next control line."
  3315. ; (let ((to (save-excursion
  3316. ; (re-search-forward "^\\." nil t))))
  3317. ; (if to (1- to) (point-max)))
  3318. (let (to)
  3319. (save-excursion
  3320. ;; Must handle
  3321. ;; ...\c
  3322. ;; .br (and other requests?)
  3323. ;; by deleting both the \c and the following request.
  3324. ;; BEWARE THAT THIS CODE MAY BE UNRELIABLE!!!!!
  3325. (while
  3326. (and
  3327. (setq to (re-search-forward "\\(\\\\c\\)?\n[.']" nil t))
  3328. (match-beginning 1)
  3329. (looking-at "br"))
  3330. (goto-char (match-beginning 0))
  3331. (woman-delete-line 2)))
  3332. (if to (1- to) (point-max))))
  3333. (defun woman2-PD (to)
  3334. ".PD d -- Set the interparagraph distance to d.
  3335. Round to whole lines, default 1 line. Format paragraphs upto TO.
  3336. \(Breaks, but should not.)"
  3337. ;; .ie \\n[.$] .nr PD (v;\\$1)
  3338. ;; .el .nr PD .4v>?\n[.V]
  3339. (woman-set-interparagraph-distance)
  3340. (woman2-format-paragraphs to))
  3341. (defun woman-set-interparagraph-distance ()
  3342. "Set the interparagraph distance from a .PD request at point."
  3343. (setq woman-interparagraph-distance
  3344. (if (eolp) 1 (woman-get-numeric-arg)))
  3345. ;; Should allow .PD 0 to set zero line spacing
  3346. (woman-delete-line 1)) ; ignore remaining args
  3347. (defsubst woman-interparagraph-space ()
  3348. "Set variable `woman-leave-blank-lines' from `woman-interparagraph-distance'."
  3349. (setq woman-leave-blank-lines woman-interparagraph-distance))
  3350. (defun woman2-TH (to)
  3351. ".TH n c x v m -- Begin a man page. Format paragraphs upto TO.
  3352. n is the name of the page in chapter c\; x is extra commentary\;
  3353. v alters page foot left; m alters page head center.
  3354. \(Should set prevailing indent and tabs to 5.)"
  3355. (woman-forward-arg 'unquote 'concat)
  3356. (insert ?\()
  3357. (woman-forward-arg 'unquote 'concat)
  3358. (insert ?\))
  3359. (let ((start (point)) here)
  3360. (while (not (eolp))
  3361. (cond ((looking-at "\"\"[ \t]")
  3362. (delete-char 2)))
  3363. (delete-horizontal-space)
  3364. (setq here (point))
  3365. (insert " -- ")
  3366. (woman-forward-arg 'unquote 'concat)
  3367. ;; Delete repeated arguments:
  3368. (if (string-equal (buffer-substring here (point))
  3369. (buffer-substring start here))
  3370. (delete-region here (point)))))
  3371. ;; Embolden heading (point is at end of heading):
  3372. (woman-set-face (line-beginning-position) (point) 'woman-bold)
  3373. (forward-line)
  3374. (delete-blank-lines)
  3375. (setq woman-left-margin woman-default-indent)
  3376. (setq woman-prevailing-indent woman-default-indent)
  3377. (woman2-format-paragraphs to woman-left-margin))
  3378. (defun woman2-SH (to)
  3379. ".SH -- Sub-head. Leave blank line and subhead.
  3380. Format paragraphs upto TO. Set prevailing indent to 5."
  3381. (if (eolp) ; If no args then
  3382. (delete-char 1) ; apply to next line
  3383. (woman-unquote-args) ; else unquote to end of heading
  3384. (beginning-of-line))
  3385. (woman2-process-escapes-to-eol)
  3386. (woman-leave-blank-lines woman-interparagraph-distance)
  3387. (setq woman-leave-blank-lines nil)
  3388. ;; Optionally embolden heading (point is at beginning of heading):
  3389. (if woman-bold-headings
  3390. (woman-set-face (point) (line-end-position) 'woman-bold))
  3391. (forward-line)
  3392. (setq woman-left-margin woman-default-indent
  3393. woman-nofill nil) ; fill output lines
  3394. (setq woman-prevailing-indent woman-default-indent)
  3395. (woman2-format-paragraphs to woman-left-margin))
  3396. (defun woman2-SS (to)
  3397. ".SS -- Sub-sub-head. Like .SH but indent heading 3 spaces.
  3398. Format paragraphs upto TO."
  3399. (if (eolp) ; If no args then
  3400. (delete-char 1)) ; apply to next line.
  3401. (insert " ")
  3402. (beginning-of-line)
  3403. (woman2-SH to))
  3404. (defun woman2-LP (to)
  3405. ".LP,.PP -- Begin paragraph. Set prevailing indent to 5.
  3406. Leave 1 blank line. Format paragraphs upto TO."
  3407. (woman-delete-line 1) ; ignore any arguments
  3408. (woman-interparagraph-space)
  3409. (setq woman-prevailing-indent woman-default-indent)
  3410. (woman2-format-paragraphs to woman-left-margin))
  3411. (defalias 'woman2-PP 'woman2-LP)
  3412. (defalias 'woman2-P 'woman2-LP)
  3413. (defun woman2-ns (to)
  3414. ".ns -- Turn on no-space mode. Format paragraphs upto TO."
  3415. ;; Should not cause a break!
  3416. (woman-delete-line 1) ; ignore argument(s)
  3417. (setq woman-nospace t)
  3418. (woman2-format-paragraphs to))
  3419. (defun woman2-rs (to)
  3420. ".rs -- Turn off no-space mode. Format paragraphs upto TO."
  3421. ;; Should not cause a break!
  3422. (woman-delete-line 1) ; ignore argument(s)
  3423. (setq woman-nospace nil)
  3424. (woman2-format-paragraphs to))
  3425. (defun woman2-sp (to)
  3426. ".sp N -- If N > 0 then leave 1 blank line. Format paragraphs upto TO."
  3427. (let ((N (if (eolp) 1 (woman-get-numeric-arg))))
  3428. (if (>= N 0)
  3429. (woman-delete-line 1) ; ignore argument(s)
  3430. (setq woman-negative-vertical-space t)
  3431. (insert ".sp ")
  3432. (forward-line))
  3433. (setq woman-leave-blank-lines N)
  3434. (woman2-format-paragraphs to)))
  3435. (defun woman-negative-vertical-space (from)
  3436. ".sp N with N < 0 => overlap following with preceding lines at FROM."
  3437. ;; Run by woman-decode-region if necessary -- not usually required.
  3438. (WoMan-warn "Negative vertical spacing support is experimental!")
  3439. (goto-char from)
  3440. (while
  3441. ;; Find next control line:
  3442. (re-search-forward "^\\.sp " nil t)
  3443. (let ((N (woman-get-numeric-arg))
  3444. overlap overwritten)
  3445. (woman-delete-whole-line)
  3446. (setq from (point)
  3447. overlap (buffer-substring from
  3448. (progn (forward-line (- N)) (point))))
  3449. (delete-region from (point))
  3450. (forward-line N)
  3451. (let ((imax (length overlap))
  3452. (i 0) c)
  3453. (while (< i imax)
  3454. (setq c (aref overlap i))
  3455. (cond ((eq c ?\n) ; skip
  3456. (forward-line))
  3457. ((eolp) ; extend line
  3458. ;; Insert character INCLUDING TEXT PROPERTIES:
  3459. ;; (insert (substring overlap i (1+ i)))
  3460. (let ((eol (string-match "\n" overlap i)))
  3461. (insert (substring overlap i eol))
  3462. (setq i (or eol imax)))
  3463. )
  3464. ((eq c ?\ ) ; skip
  3465. (forward-char))
  3466. ((eq c ?\t) ; skip
  3467. (if (eq (following-char) ?\t)
  3468. (forward-char) ; both tabs, just skip
  3469. (dotimes (i woman-tab-width)
  3470. (if (eolp)
  3471. (insert ?\ ) ; extend line
  3472. (forward-char)) ; skip
  3473. )))
  3474. (t
  3475. (if (or (eq (following-char) ?\ ) ; overwrite OK
  3476. overwritten) ; warning only once per ".sp -"
  3477. ()
  3478. (setq overwritten t)
  3479. (WoMan-warn
  3480. "Character(s) overwritten by negative vertical spacing in line %d"
  3481. (count-lines 1 (point))))
  3482. (delete-char 1) (insert (substring overlap i (1+ i)))))
  3483. (setq i (1+ i)))))))
  3484. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3485. ;; The following function should probably do ALL width and number
  3486. ;; register interpolation.
  3487. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3488. (defun woman2-process-escapes (to &optional numeric)
  3489. "Process remaining escape sequences up to marker TO, preserving point.
  3490. Optional argument NUMERIC, if non-nil, means the argument is numeric."
  3491. (assert (and (markerp to) (marker-insertion-type to)))
  3492. ;; The first two cases below could be merged (maybe)!
  3493. (let ((from (point)))
  3494. ;; Discard zero width filler character used to hide leading dots
  3495. ;; and zero width characters.
  3496. (while (re-search-forward "\\\\[&|^]" to t)
  3497. (woman-delete-match 0)
  3498. ;; If on a line by itself, consume newline as well (Bug#3651).
  3499. (and (eq (char-before (match-beginning 0)) ?\n)
  3500. (eq (char-after (match-beginning 0)) ?\n)
  3501. (delete-char 1)))
  3502. (goto-char from)
  3503. ;; Interrupt text processing -- CONTINUE current text with the
  3504. ;; next text line (after any control lines, unless processing to
  3505. ;; eol):
  3506. (while (re-search-forward "\\\\c.*\n?" to t)
  3507. (woman-delete-match 0))
  3508. ;; but do not delete the final newline ...
  3509. (if (and (or (eobp) (= (point) to)) (not (bolp)))
  3510. (insert-before-markers ?\n))
  3511. (goto-char from)
  3512. (woman-translate to)
  3513. (goto-char from)
  3514. (woman-special-characters to)
  3515. (goto-char from)
  3516. ;; Printable version of the current escape character, ASSUMED to be `\'
  3517. ;; This must be done LAST of all escape processing!
  3518. ;; Done like this to preserve any text properties of the `\'
  3519. (while (search-forward "\\" to t)
  3520. (let ((c (following-char)))
  3521. ;; Some other escapes, such as \f, are handled in
  3522. ;; `woman0-process-escapes'.
  3523. (cond ((eq c ?') ; \' -> '
  3524. (delete-char -1)
  3525. (cond (numeric ; except in numeric args, \' -> `
  3526. (delete-char 1)
  3527. (insert ?`))))
  3528. ((eq c ?\( )) ; uninterpreted special character
  3529. ; \(.. -- do nothing
  3530. ((eq c ?t) ; non-interpreted tab \t
  3531. (delete-char 1)
  3532. (delete-char -1)
  3533. (insert "\t"))
  3534. ((and numeric
  3535. (memq c '(?w ?n ?h)))) ; leave \w, \n, \h (?????)
  3536. ((eq c ?l) (woman-horizontal-line)))))
  3537. (goto-char from)
  3538. ;; Process non-default tab settings:
  3539. (cond (tab-stop-list
  3540. (while (search-forward "\t" to t)
  3541. (woman-tab-to-tab-stop))
  3542. (goto-char from)))
  3543. ;; Must replace \' by something before processing \w, done above.
  3544. ;; Replace all `\w' and `\n' escapes:
  3545. ;; (This may be a bit too recursive!)
  3546. (while (re-search-forward "\\\\[nw]" to t)
  3547. (let ((from (match-beginning 0)) N)
  3548. (goto-char from)
  3549. (setq N (woman-parse-numeric-value))
  3550. (delete-region from (point))
  3551. ;; Interpolate value:
  3552. (insert-before-markers (number-to-string N))))
  3553. (goto-char from)))
  3554. (defun woman-horizontal-line ()
  3555. "\\l'Nc' -- Draw a horizontal line of length N using character c, default _."
  3556. (delete-char -1)
  3557. (delete-char 1)
  3558. (looking-at "\\(.\\)\\(.*\\)\\1")
  3559. (forward-char 1)
  3560. (let* ((to (match-end 2))
  3561. (from (match-beginning 0))
  3562. (N (woman-parse-numeric-arg))
  3563. (c (if (< (point) to) (following-char) ?_)))
  3564. (delete-region from to)
  3565. (delete-char 1)
  3566. (insert (make-string N c))))
  3567. ;;; 4. Text Filling, Adjusting, and Centering
  3568. (defun woman2-br (to)
  3569. ".br -- Break. Leave no blank line. Format paragraphs upto TO."
  3570. (woman-delete-line 1) ; ignore any arguments
  3571. (woman2-format-paragraphs to))
  3572. (defun woman2-fi (to)
  3573. ".fi -- Fill subsequent output lines. Leave no blank line.
  3574. Format paragraphs upto TO."
  3575. (setq woman-nofill nil)
  3576. (woman-delete-line 1) ; ignore any arguments
  3577. ;; Preserve any final blank line in the nofill region:
  3578. (save-excursion
  3579. (forward-line -1)
  3580. (if (looking-at "[ \t]*$") (setq woman-leave-blank-lines 1)))
  3581. (woman2-format-paragraphs to))
  3582. (defun woman2-nf (to)
  3583. ".nf -- Nofill. Subsequent lines are neither filled nor adjusted.
  3584. Input text lines are copied directly to output lines without regard
  3585. for the current line length. Format paragraphs up to TO."
  3586. (setq woman-nofill t)
  3587. (woman-delete-line 1) ; ignore any arguments
  3588. (woman2-format-paragraphs to))
  3589. (defun woman2-ad (to)
  3590. ".ad c -- Line adjustment is begun (once fill mode is on).
  3591. Set justification mode to c if specified.
  3592. Format paragraphs upto TO. (Breaks, but should not.)"
  3593. ;; c = l -- left, r -- right, c -- center, b or n -- both,
  3594. ;; absent -- unchanged. Initial mode adj,both.
  3595. (setq woman-adjust
  3596. (cond ((eolp) woman-adjust-previous)
  3597. ((eq (following-char) ?l) woman-adjust-left)
  3598. ((eq (following-char) ?r) woman-adjust-right)
  3599. ((eq (following-char) ?c) woman-adjust-center)
  3600. ((memq (following-char) '(?b ?n)) woman-adjust-both)
  3601. (t (woman-get-numeric-arg))
  3602. )
  3603. woman-justify (nth woman-adjust woman-justify-list))
  3604. (woman-delete-line 1) ; ignore any remaining arguments
  3605. (woman2-format-paragraphs to))
  3606. (defun woman2-na (to)
  3607. ".na -- No adjusting. Format paragraphs upto TO.
  3608. \(Breaks, but should not.)"
  3609. (setq woman-adjust-previous woman-adjust
  3610. woman-justify-previous woman-justify
  3611. woman-adjust woman-adjust-left ; fill but do not adjust
  3612. woman-justify (nth woman-adjust woman-justify-list))
  3613. (woman-delete-line 1) ; ignore any arguments
  3614. (woman2-format-paragraphs to))
  3615. ;;; The main formatting functions:
  3616. (defun woman-leave-blank-lines (&optional leave)
  3617. "Delete all blank lines around point.
  3618. Leave one blank line if optional argument LEAVE is non-nil and
  3619. non-zero, or if LEAVE is nil and variable `woman-leave-blank-lines' is
  3620. non-nil and non-zero."
  3621. ;; ***** It may suffice to delete only lines ABOVE point! *****
  3622. ;; NOTE: Function arguments are evaluated left to right
  3623. ;; (*note (elisp)Function Forms::.).
  3624. (delete-region
  3625. (save-excursion
  3626. (if (not (eq (skip-syntax-backward " ") 0))
  3627. (forward-line)) ; forward-char ?
  3628. (point))
  3629. (progn (skip-syntax-forward " ")
  3630. (beginning-of-line)
  3631. (point)))
  3632. (unless woman-nospace
  3633. (if (or (null leave) (eq leave 0))
  3634. ;; output any `pending' vertical space ...
  3635. (setq leave woman-leave-blank-lines))
  3636. (if (and leave (> leave 0)) (insert-before-markers ?\n)))
  3637. (setq woman-leave-blank-lines nil))
  3638. ;; `fill-region-as-paragraph' in `fill.el' appears to be the principal
  3639. ;; text filling function, so that is what I use here.
  3640. (defvar woman-temp-indent nil)
  3641. (defun woman2-format-paragraphs (to &optional new-left)
  3642. "Indent, fill and adjust paragraphs upto TO to current left margin.
  3643. If optional arg NEW-LEFT is non-nil then reset current left margin.
  3644. If `woman-nofill' is non-nil then indent without filling or adjusting."
  3645. ;; Blank space should only ever be output before text.
  3646. (if new-left (setq left-margin new-left))
  3647. (if (looking-at "^\\s *$")
  3648. ;; A blank line should leave a space like .sp 1 (p. 14).
  3649. (setq woman-leave-blank-lines 1))
  3650. (skip-syntax-forward " ")
  3651. ;; Successive control lines are sufficiently common to be worth a
  3652. ;; special case (maybe):
  3653. (unless (>= (point) to)
  3654. (woman-reset-nospace)
  3655. (woman2-process-escapes to 'numeric)
  3656. (if woman-nofill
  3657. ;; Indent without filling or adjusting ...
  3658. (progn
  3659. (woman-leave-blank-lines)
  3660. (when woman-temp-indent
  3661. (indent-to woman-temp-indent)
  3662. (forward-line))
  3663. (indent-rigidly (point) to left-margin)
  3664. (woman-horizontal-escapes to))
  3665. ;; Fill and justify ...
  3666. ;; Blank lines and initial spaces cause a break.
  3667. (while (< (point) to)
  3668. (woman-leave-blank-lines)
  3669. (let ((from (point)))
  3670. ;; Indent first lin of paragraph:
  3671. (indent-to (or woman-temp-indent left-margin))
  3672. (woman-horizontal-escapes to) ; 7 October 1999
  3673. ;; Find the beginning of the next paragraph:
  3674. (forward-line)
  3675. (and (re-search-forward "\\(^\\s *$\\)\\|\\(^\\s +\\)" to 1)
  3676. ;; A blank line should leave a space like .sp 1 (p. 14).
  3677. (eolp)
  3678. (skip-syntax-forward " ")
  3679. (setq woman-leave-blank-lines 1))
  3680. ;; This shouldn't happen, but in case it does (e.g. for
  3681. ;; badly-formatted manfiles with no terminating newline),
  3682. ;; avoid an infinite loop.
  3683. (unless (and (eolp) (eobp))
  3684. (beginning-of-line))
  3685. ;; If a single short line then just leave it.
  3686. ;; This is necessary to preserve some table layouts.
  3687. ;; PROBABLY NOT NECESSARY WITH SQUEEZE MODIFICATION !!!!!
  3688. (when (or (> (count-lines from (point)) 1)
  3689. (save-excursion
  3690. (backward-char)
  3691. (> (current-column) fill-column)))
  3692. ;; NOSQUEEZE has no effect if JUSTIFY is full, so redefine
  3693. ;; canonically-space-region, see above.
  3694. (if (and woman-temp-indent (< woman-temp-indent left-margin))
  3695. (let ((left-margin woman-temp-indent))
  3696. (fill-region-as-paragraph from (point) woman-justify)
  3697. (save-excursion
  3698. (goto-char from)
  3699. (forward-line)
  3700. (setq from (point)))))
  3701. (fill-region-as-paragraph from (point) woman-justify)))))
  3702. (setq woman-temp-indent nil)))
  3703. ;;; Tagged, indented and hanging paragraphs:
  3704. (defun woman2-TP (to)
  3705. ".TP i -- Set prevailing indent to i. Format paragraphs upto TO.
  3706. Begin indented paragraph with hanging tag given by next text line.
  3707. If tag doesn't fit, place it on a separate line."
  3708. (let ((i (woman2-get-prevailing-indent)))
  3709. (woman-leave-blank-lines woman-interparagraph-distance)
  3710. (woman2-tagged-paragraph to i)))
  3711. (defun woman2-IP (to)
  3712. ".IP x i -- Same as .TP with tag x. Format paragraphs upto TO."
  3713. (woman-interparagraph-space)
  3714. (if (eolp) ; no args
  3715. ;; Like LP without resetting prevailing indent
  3716. (woman2-format-paragraphs to (+ woman-left-margin
  3717. woman-prevailing-indent))
  3718. (woman-forward-arg 'unquote)
  3719. (let ((i (woman2-get-prevailing-indent 'leave-eol)))
  3720. (beginning-of-line)
  3721. (woman-leave-blank-lines) ; must be here,
  3722. ;;
  3723. ;; The cvs.1 manpage contains some (possibly buggy) syntax that
  3724. ;; confuses woman, although the man program displays it ok.
  3725. ;; Most problems are caused by IP followed by another request on
  3726. ;; the next line. Without the following hack, the second request
  3727. ;; gets displayed raw in the output. Note that
  3728. ;; woman2-tagged-paragraph also contains a hack for similar
  3729. ;; issues (eg IP followed by SP).
  3730. ;;
  3731. ;; i) For IP followed by one or more IPs, we ignore all but the
  3732. ;; last (mimic man). The hack in w-t-p would only work for two
  3733. ;; consecutive IPs, and would use the first.
  3734. ;; ii) For IP followed by SP followed by one or more requests,
  3735. ;; do nothing. At least in cvs.1, there is usually another IP in
  3736. ;; there somewhere.
  3737. (unless (or (looking-at "^\\.IP")
  3738. (and (looking-at "^\\.sp")
  3739. (save-excursion
  3740. (and (zerop (forward-line 1))
  3741. (looking-at woman-request-regexp)))))
  3742. (woman2-tagged-paragraph to i)))))
  3743. (defun woman-find-next-control-line-carefully ()
  3744. "Find and return start of next control line, even if already there!"
  3745. (if (looking-at "^[.']")
  3746. (point)
  3747. (woman-find-next-control-line)))
  3748. (defun woman2-tagged-paragraph (to i)
  3749. "Begin indented paragraph with hanging tag given by current text line.
  3750. If tag doesn't fit, leave it on separate line.
  3751. Format paragraphs upto TO. Set prevailing indent to I."
  3752. (if (not (looking-at "\\s *$")) ; non-empty tag
  3753. (setq woman-leave-blank-lines nil))
  3754. ;; Temporary hack for bash.1, cvs.1 and groff_mmse.7 until code is revised
  3755. ;; to process all requests uniformly.
  3756. ;; This hack deals with IP requests followed by other requests (eg
  3757. ;; SP) on the very next line. We skip over the SP, otherwise it gets
  3758. ;; inserted raw in the rendered output.
  3759. (cond ((and (= (point) to)
  3760. (looking-at "^[.'][ \t]*\\(PD\\|br\\|ta\\|sp\\) *"))
  3761. (if (member (match-string 1) '("br" "sp"))
  3762. (woman-delete-line 1)
  3763. (woman-delete-match 0)
  3764. (if (string= (match-string 1) "ta") ; for GetInt.3
  3765. (woman2-ta to)
  3766. (woman-set-interparagraph-distance)))
  3767. (set-marker to (woman-find-next-control-line-carefully))))
  3768. (let ((tag (point)))
  3769. (woman-reset-nospace)
  3770. ;; Format the tag:
  3771. (woman2-process-escapes-to-eol)
  3772. ;; TIDY UP THE FOLLOWING CODE
  3773. ;; (indent-to woman-left-margin)
  3774. (setq left-margin woman-left-margin)
  3775. (forward-line)
  3776. (fill-region-as-paragraph (save-excursion (forward-line -1) (point))
  3777. (point) woman-justify)
  3778. ;; Temporary hack for bash.1 until all requests processed uniformly:
  3779. (cond ((and (= (point) to) (looking-at "^[.'][ \t]*PD *"))
  3780. (woman-delete-match 0)
  3781. (woman-set-interparagraph-distance)
  3782. (set-marker to (woman-find-next-control-line-carefully))
  3783. ))
  3784. ;; Format the paragraph body, if there is one! Set indented left
  3785. ;; margin anyway, because the paragraph body may begin with a
  3786. ;; control line:
  3787. (setq left-margin (+ woman-left-margin i))
  3788. (cond ((< (point) to)
  3789. (woman2-format-paragraphs to)
  3790. (goto-char tag) (end-of-line)
  3791. (cond ((> (setq i (- left-margin (current-column))) 0)
  3792. (delete-char 1)
  3793. (delete-horizontal-space)
  3794. ;; Necessary to avoid spaces inheriting underlines.
  3795. ;; Cannot simply delete (current-column) whitespace
  3796. ;; characters because some may be tabs!
  3797. (insert-char ?\s i)))
  3798. (goto-char to)))))
  3799. (defun woman2-HP (to)
  3800. ".HP i -- Set prevailing indent to i. Format paragraphs upto TO.
  3801. Begin paragraph with hanging indent."
  3802. (let ((i (woman2-get-prevailing-indent)))
  3803. (woman-interparagraph-space)
  3804. (setq woman-temp-indent woman-left-margin)
  3805. (woman2-format-paragraphs to (+ woman-left-margin i))))
  3806. (defun woman2-get-prevailing-indent (&optional leave-eol)
  3807. "Set prevailing indent to integer argument at point, and return it.
  3808. If no argument then return the existing prevailing indent.
  3809. Delete line from point and eol unless LEAVE-EOL is non-nil."
  3810. (if (eolp)
  3811. (or leave-eol (delete-char 1))
  3812. (let ((i (woman-get-numeric-arg)))
  3813. (woman-delete-line) (or leave-eol (delete-char 1))
  3814. ;; i = 0 if the argument was not a number
  3815. ;; FIXME should this be >= 0? How else to reset to 0 indent?
  3816. (if (> i 0) (setq woman-prevailing-indent i))))
  3817. woman-prevailing-indent)
  3818. (defmacro woman-push (value stack)
  3819. "Push VALUE onto STACK."
  3820. `(setq ,stack (cons ,value ,stack)))
  3821. (defmacro woman-pop (variable stack)
  3822. "Pop into VARIABLE the value at the top of STACK.
  3823. Allow for mismatched requests!"
  3824. `(if ,stack
  3825. (setq ,variable (car ,stack)
  3826. ,stack (cdr ,stack))))
  3827. (defun woman2-RS (to)
  3828. ".RS i -- Start relative indent, move left margin in distance i.
  3829. Set prevailing indent to 5 for nested indents. Format paragraphs upto TO."
  3830. (woman-push woman-left-margin woman-RS-left-margin)
  3831. (woman-push woman-prevailing-indent woman-RS-prevailing-indent)
  3832. (setq woman-left-margin (+ woman-left-margin
  3833. (woman2-get-prevailing-indent))
  3834. woman-prevailing-indent woman-default-indent)
  3835. (woman2-format-paragraphs to woman-left-margin))
  3836. (defun woman2-RE (to)
  3837. ".RE -- End of relative indent. Format paragraphs upto TO.
  3838. Set prevailing indent to amount of starting .RS."
  3839. (woman-pop woman-left-margin woman-RS-left-margin)
  3840. (woman-pop woman-prevailing-indent woman-RS-prevailing-indent)
  3841. (woman-delete-line 1) ; ignore any arguments
  3842. (woman2-format-paragraphs to woman-left-margin))
  3843. ;;; Line Length and Indenting:
  3844. (defun woman-set-arg (arg &optional previous)
  3845. "Reset, increment or decrement argument ARG, which must be quoted.
  3846. If no argument then use value of optional arg PREVIOUS if non-nil,
  3847. otherwise set PREVIOUS. Delete the whole remaining control line."
  3848. (if (eolp) ; space already skipped
  3849. (set arg (if previous (eval previous) 0))
  3850. (if previous (set previous (eval arg)))
  3851. (woman2-process-escapes-to-eol 'numeric)
  3852. (let ((pm (if (looking-at "[+-]")
  3853. (prog1 (following-char)
  3854. (forward-char 1))))
  3855. (i (woman-parse-numeric-arg)))
  3856. (cond ((null pm) (set arg i))
  3857. ((= pm ?+) (set arg (+ (eval arg) i)))
  3858. ((= pm ?-) (set arg (- (eval arg) i)))
  3859. ))
  3860. (beginning-of-line))
  3861. (woman-delete-line 1)) ; ignore any remaining arguments
  3862. ;; NEED TO RATIONALIZE NAMES FOR PREVIOUS VALUES!
  3863. (defvar woman-ll-fill-column woman-fill-column)
  3864. (defvar woman-in-left-margin woman-left-margin)
  3865. (defun woman2-ll (to)
  3866. ".ll +/-N -- Set, increment or decrement line length.
  3867. Format paragraphs upto TO. (Breaks, but should not.)"
  3868. (woman-set-arg 'fill-column 'woman-ll-fill-column)
  3869. (woman2-format-paragraphs to))
  3870. (defun woman2-in (to)
  3871. ".in +/-N -- Set, increment or decrement the indent.
  3872. Format paragraphs upto TO."
  3873. (woman-set-arg 'left-margin 'woman-in-left-margin)
  3874. (woman2-format-paragraphs to))
  3875. (defun woman2-ti (to)
  3876. ".ti +/-N -- Temporary indent. Format paragraphs upto TO."
  3877. ;; Ignore if no argument.
  3878. ;; Indent next output line only wrt current indent.
  3879. ;; Current indent is not changed.
  3880. (setq woman-temp-indent left-margin)
  3881. (woman-set-arg 'woman-temp-indent)
  3882. (woman2-format-paragraphs to nil))
  3883. ;;; Tabs, Leaders, and Fields:
  3884. (defun woman2-ta (to)
  3885. ".ta Nt ... -- Set tabs, left type, unless t=R(right), C(centered).
  3886. \(Breaks, but should not.) The tab stops are separated by spaces\;
  3887. a value preceded by + represents an increment to the previous stop value.
  3888. Format paragraphs upto TO."
  3889. (setq tab-stop-list nil)
  3890. (woman2-process-escapes-to-eol 'numeric)
  3891. (save-excursion
  3892. (let ((tab-stop 0))
  3893. (while (not (eolp))
  3894. (let ((plus (cond ((eq (following-char) ?+) (forward-char 1) t)))
  3895. (i (woman-parse-numeric-arg)))
  3896. (setq tab-stop (if plus (+ tab-stop i) i)))
  3897. (if (memq (following-char) '(?R ?C))
  3898. (setq tab-stop (cons tab-stop (following-char))))
  3899. (setq tab-stop-list (cons tab-stop tab-stop-list))
  3900. (skip-syntax-forward "^ ") ; skip following R, C, `;', etc.
  3901. (skip-chars-forward " \t")
  3902. )))
  3903. (woman-delete-line 1) ; ignore any remaining arguments
  3904. (setq tab-stop-list (reverse tab-stop-list))
  3905. (woman2-format-paragraphs to))
  3906. (defsubst woman-get-tab-stop (tab-stops)
  3907. "If TAB-STOPS is a cons, return its car, else return TAB-STOPS."
  3908. (if (consp tab-stops) (car tab-stops) tab-stops))
  3909. (defun woman-tab-to-tab-stop ()
  3910. "Insert spaces to next defined tab-stop column.
  3911. The variable `tab-stop-list' is a list whose elements are either left
  3912. tab stop columns or pairs (COLUMN . TYPE) where TYPE is R or C."
  3913. ;; Based on tab-to-tab-stop in indent.el.
  3914. ;; R & C tabs probably not quite right!
  3915. (delete-char -1)
  3916. (let ((tabs tab-stop-list))
  3917. (while (and tabs (>= (current-column)
  3918. (woman-get-tab-stop (car tabs))))
  3919. (setq tabs (cdr tabs)))
  3920. (if tabs
  3921. (let* ((tab (car tabs))
  3922. (type (and (consp tab) (cdr tab)))
  3923. eol n)
  3924. (if type
  3925. (setq tab (woman-get-tab-stop tab)
  3926. eol (line-end-position)
  3927. n (save-excursion
  3928. (search-forward "\t" eol t))
  3929. n (- (if n (1- n) eol) (point))
  3930. tab (- tab (if (eq type ?C) (/ n 2) n))) )
  3931. (setq n (- tab (current-column)))
  3932. (insert-char ?\s n))
  3933. (insert ?\ ))))
  3934. (defun woman2-DT (to)
  3935. ".DT -- Restore default tabs. Format paragraphs upto TO.
  3936. \(Breaks, but should not.)"
  3937. ;; Currently just terminates special tab processing.
  3938. (setq tab-stop-list nil)
  3939. (woman-delete-line 1) ; ignore any arguments
  3940. (woman2-format-paragraphs to))
  3941. (defun woman2-fc (to)
  3942. ".fc a b -- Set field delimiter a and pad character b.
  3943. Format paragraphs upto TO.
  3944. A VERY FIRST ATTEMPT to make fields at least readable!
  3945. Needs doing properly!"
  3946. (if (eolp)
  3947. (woman-delete-whole-line) ; ignore!
  3948. (let ((delim (following-char))
  3949. (pad ?\ ) end) ; pad defaults to space
  3950. (forward-char)
  3951. (skip-chars-forward " \t")
  3952. (or (eolp) (setq pad (following-char)))
  3953. (woman-delete-whole-line)
  3954. (save-excursion
  3955. (if (re-search-forward "^[.'][ \t]*fc\\s " nil t)
  3956. (setq end (match-beginning 0))))
  3957. ;; A field is contained between a pair of field delimiter
  3958. ;; characters and consists of sub-strings separated by padding
  3959. ;; indicator characters:
  3960. (setq delim (string delim ?[ ?^ delim ?] ?* delim))
  3961. (save-excursion
  3962. (while (re-search-forward delim end t)
  3963. (goto-char (match-beginning 0))
  3964. (delete-char 1)
  3965. (insert woman-unpadded-space-char)
  3966. (goto-char (match-end 0))
  3967. (delete-char -1)
  3968. (insert-before-markers woman-unpadded-space-char)
  3969. (subst-char-in-region
  3970. (match-beginning 0) (match-end 0)
  3971. pad woman-unpadded-space-char t)))))
  3972. (woman2-format-paragraphs to))
  3973. ;;; Preliminary table support (.TS/.TE)
  3974. (defun woman2-TS (to)
  3975. ".TS -- Start of table code for the tbl processor.
  3976. Format paragraphs upto TO."
  3977. ;; This is a preliminary hack that seems to suffice for lilo.8.
  3978. (woman-delete-line 1) ; ignore any arguments
  3979. (when woman-emulate-tbl
  3980. ;; Assumes column separator is \t and intercolumn spacing is 3.
  3981. ;; The first line may optionally be a list of options terminated by
  3982. ;; a semicolon. Currently, just delete it:
  3983. (if (looking-at ".*;[ \t]*$") (woman-delete-line 1)) ;
  3984. ;; The following lines must specify the format of each line of the
  3985. ;; table and end with a period. Currently, just delete them:
  3986. (while (not (looking-at ".*\\.[ \t]*$")) (woman-delete-line 1))
  3987. (woman-delete-line 1)
  3988. ;; For each column, find its width and align it:
  3989. (let ((start (point)) (col 1))
  3990. (while (prog1 (search-forward "\t" to t) (goto-char start))
  3991. ;; Find current column width:
  3992. (while (< (point) to)
  3993. (when (search-forward "\t" to t)
  3994. (backward-char)
  3995. (if (> (current-column) col) (setq col (current-column))))
  3996. (forward-line))
  3997. ;; Align current column:
  3998. (goto-char start)
  3999. (setq col (+ col 3)) ; intercolumn space
  4000. (while (< (point) to)
  4001. (when (search-forward "\t" to t)
  4002. (delete-char -1)
  4003. (insert-char ?\ (- col (current-column))))
  4004. (forward-line))
  4005. (goto-char start))))
  4006. ;; Format table with no filling or adjusting (cf. woman2-nf):
  4007. (setq woman-nofill t)
  4008. (woman2-format-paragraphs to))
  4009. (defalias 'woman2-TE 'woman2-fi)
  4010. ;; ".TE -- End of table code for the tbl processor."
  4011. ;; Turn filling and adjusting back on.
  4012. ;;; WoMan message logging:
  4013. ;; The basis for this logging code was shamelessly pirated from bytecomp.el
  4014. ;; by Jamie Zawinski <jwz@lucid.com> & Hallvard Furuseth <hbf@ulrik.uio.no>
  4015. (defun WoMan-log-begin ()
  4016. "Log the beginning of formatting in *WoMan-Log*."
  4017. (let ((WoMan-current-buffer (buffer-name)))
  4018. (with-current-buffer (get-buffer-create "*WoMan-Log*")
  4019. (or (eq major-mode 'view-mode) (view-mode 1))
  4020. (setq buffer-read-only nil)
  4021. (goto-char (point-max))
  4022. (insert "\n\^L\nFormatting "
  4023. (if (stringp WoMan-current-file)
  4024. (concat "file " WoMan-current-file)
  4025. (concat "buffer " WoMan-current-buffer))
  4026. " at " (current-time-string) "\n")
  4027. (setq WoMan-Log-header-point-max (point-max)))))
  4028. (defun WoMan-log (format &rest args)
  4029. "Log a message out of FORMAT control string and optional ARGS."
  4030. (WoMan-log-1 (apply 'format format args)))
  4031. (defun WoMan-warn (format &rest args)
  4032. "Log a warning message out of FORMAT control string and optional ARGS."
  4033. (setq format (apply 'format format args))
  4034. (WoMan-log-1 (concat "** " format)))
  4035. ;; request is not used dynamically by any callees.
  4036. (defun WoMan-warn-ignored (request ignored)
  4037. "Log a warning message about ignored directive REQUEST.
  4038. IGNORED is a string appended to the log message."
  4039. (let ((tail
  4040. (buffer-substring (point)
  4041. (line-end-position))))
  4042. (if (and (> (length tail) 0)
  4043. (/= (string-to-char tail) ?\ ))
  4044. (setq tail (concat " " tail)))
  4045. (WoMan-log-1
  4046. (concat "** " request tail " request " ignored))))
  4047. (defun WoMan-log-end (time)
  4048. "Log the end of formatting in *WoMan-Log*.
  4049. TIME specifies the time it took to format the man page, to be printed
  4050. with the message."
  4051. (WoMan-log-1 (format "Formatting time %g seconds." time) 'end))
  4052. (defun WoMan-log-1 (string &optional end)
  4053. "Log a message STRING in *WoMan-Log*.
  4054. If optional argument END is non-nil then make buffer read-only after
  4055. logging the message."
  4056. (with-current-buffer (get-buffer-create "*WoMan-Log*")
  4057. (setq buffer-read-only nil)
  4058. (goto-char (point-max))
  4059. (or end (insert " ")) (insert string "\n")
  4060. (if end
  4061. (setq buffer-read-only t)
  4062. (if woman-show-log
  4063. (select-window ; to return to
  4064. (prog1 (selected-window) ; WoMan window
  4065. (select-window (display-buffer (current-buffer)))
  4066. (cond (WoMan-Log-header-point-max
  4067. (goto-char WoMan-Log-header-point-max)
  4068. (forward-line -1)
  4069. (recenter 0))))))))
  4070. nil) ; for woman-file-readable-p etc.
  4071. ;;; Bookmark Woman support.
  4072. (declare-function bookmark-make-record-default
  4073. "bookmark" (&optional no-file no-context posn))
  4074. (declare-function bookmark-prop-get "bookmark" (bookmark prop))
  4075. (declare-function bookmark-default-handler "bookmark" (bmk))
  4076. (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
  4077. ;; FIXME: woman.el and man.el should be better integrated so, for
  4078. ;; example, bookmarks of one can be used with the other.
  4079. (defun woman-bookmark-make-record ()
  4080. "Make a bookmark entry for a Woman buffer."
  4081. `(,(Man-default-bookmark-title)
  4082. ,@(bookmark-make-record-default 'no-file)
  4083. (location . ,(concat "woman " woman-last-file-name))
  4084. ;; Use the same form as man's bookmarks, as much as possible.
  4085. (man-args . ,woman-last-file-name)
  4086. (handler . woman-bookmark-jump)))
  4087. ;;;###autoload
  4088. (defun woman-bookmark-jump (bookmark)
  4089. "Default bookmark handler for Woman buffers."
  4090. (let* ((file (bookmark-prop-get bookmark 'man-args))
  4091. ;; FIXME: we need woman-find-file-noselect, since
  4092. ;; save-window-excursion can't protect us from the case where
  4093. ;; woman-find-file creates a new frame.
  4094. (buf (save-window-excursion
  4095. (woman-find-file file) (current-buffer))))
  4096. (bookmark-default-handler
  4097. `("" (buffer . ,buf) . ,(bookmark-get-bookmark-record bookmark)))))
  4098. (provide 'woman)
  4099. ;; Local Variables:
  4100. ;; coding: utf-8
  4101. ;; End:
  4102. ;;; woman.el ends here