bibtex.el 241 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294
  1. ;;; bibtex.el --- BibTeX mode for GNU Emacs -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1992, 1994-1999, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Stefan Schoef <schoef@offis.uni-oldenburg.de>
  4. ;; Bengt Martensson <bengt@mathematik.uni-Bremen.de>
  5. ;; Marc Shapiro <marc.shapiro@acm.org>
  6. ;; Mike Newton <newton@gumby.cs.caltech.edu>
  7. ;; Aaron Larson <alarson@src.honeywell.com>
  8. ;; Dirk Herrmann <D.Herrmann@tu-bs.de>
  9. ;; Maintainer: Roland Winkler <winkler@gnu.org>
  10. ;; Keywords: BibTeX, LaTeX, TeX
  11. ;; This file is part of GNU Emacs.
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;; Commentary:
  23. ;; Major mode for editing and validating BibTeX files.
  24. ;; Usage:
  25. ;; See documentation for `bibtex-mode' or type "M-x describe-mode"
  26. ;; when you are in BibTeX mode.
  27. ;; Todo:
  28. ;; Distribute texinfo file.
  29. ;;; Code:
  30. (require 'button)
  31. ;; User Options:
  32. (defgroup bibtex nil
  33. "BibTeX mode."
  34. :group 'tex
  35. :prefix "bibtex-")
  36. (defgroup bibtex-autokey nil
  37. "Generate automatically a key from the author/editor and the title field."
  38. :group 'bibtex
  39. :prefix "bibtex-autokey-")
  40. (defcustom bibtex-mode-hook nil
  41. "List of functions to call on entry to BibTeX mode."
  42. :group 'bibtex
  43. :type 'hook)
  44. (defcustom bibtex-field-delimiters 'braces
  45. "Type of field delimiters. Allowed values are `braces' or `double-quotes'."
  46. :group 'bibtex
  47. :type '(choice (const braces)
  48. (const double-quotes)))
  49. (defcustom bibtex-entry-delimiters 'braces
  50. "Type of entry delimiters. Allowed values are `braces' or `parentheses'."
  51. :group 'bibtex
  52. :type '(choice (const braces)
  53. (const parentheses)))
  54. (defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection")
  55. "List of BibTeX entries that get an OPTcrossref field."
  56. :group 'bibtex
  57. :type '(repeat string))
  58. (defcustom bibtex-include-OPTkey t
  59. "If non-nil, all newly created entries get an OPTkey field.
  60. If this is a string, use it as the initial field text.
  61. If this is a function, call it to generate the initial field text."
  62. :group 'bibtex
  63. :type '(choice (const :tag "None" nil)
  64. (string :tag "Initial text")
  65. (function :tag "Initialize Function")
  66. (const :tag "Default" t)))
  67. (put 'bibtex-include-OPTkey 'risky-local-variable t)
  68. (defcustom bibtex-user-optional-fields
  69. '(("annote" "Personal annotation (ignored)"))
  70. "List of optional fields the user wants to have always present.
  71. Entries should be of the same form as the OPTIONAL list
  72. in `bibtex-BibTeX-entry-alist' (which see)."
  73. :group 'bibtex
  74. :type '(repeat (group (string :tag "Field")
  75. (string :tag "Comment")
  76. (option (choice :tag "Init"
  77. (const nil) string function)))))
  78. (put 'bibtex-user-optional-fields 'risky-local-variable t)
  79. (defcustom bibtex-entry-format
  80. '(opts-or-alts required-fields numerical-fields)
  81. "Type of formatting performed by `bibtex-clean-entry'.
  82. It may be t, nil, or a list of symbols out of the following:
  83. opts-or-alts Delete empty optional and alternative fields and
  84. remove OPT and ALT prefixes from used fields.
  85. required-fields Signal an error if a required field is missing.
  86. numerical-fields Delete delimiters around numeral fields.
  87. page-dashes Change double dashes in page field to single dash
  88. (for scribe compatibility).
  89. whitespace Delete whitespace at the beginning and end of fields.
  90. inherit-booktitle If entry contains a crossref field and the booktitle
  91. field is empty, set the booktitle field to the content
  92. of the title field of the crossreferenced entry.
  93. realign Realign entries, so that field texts and perhaps equal
  94. signs (depending on the value of
  95. `bibtex-align-at-equal-sign') begin in the same column.
  96. Also fill fields.
  97. last-comma Add or delete comma on end of last field in entry,
  98. according to value of `bibtex-comma-after-last-field'.
  99. delimiters Change delimiters according to variables
  100. `bibtex-field-delimiters' and `bibtex-entry-delimiters'.
  101. unify-case Change case of entry types and field names.
  102. braces Enclose parts of field entries by braces according to
  103. `bibtex-field-braces-alist'.
  104. strings Replace parts of field entries by string constants
  105. according to `bibtex-field-strings-alist'.
  106. sort-fields Sort fields to match the field order in
  107. `bibtex-BibTeX-entry-alist'.
  108. The value t means do all of the above formatting actions.
  109. The value nil means do no formatting at all."
  110. :group 'bibtex
  111. :type '(choice (const :tag "None" nil)
  112. (const :tag "All" t)
  113. (set :menu-tag "Some"
  114. (const opts-or-alts)
  115. (const required-fields)
  116. (const numerical-fields)
  117. (const page-dashes)
  118. (const whitespace)
  119. (const inherit-booktitle)
  120. (const realign)
  121. (const last-comma)
  122. (const delimiters)
  123. (const unify-case)
  124. (const braces)
  125. (const strings)
  126. (const sort-fields))))
  127. (put 'bibtex-entry-format 'safe-local-variable
  128. (lambda (x)
  129. (or (eq x t)
  130. (let ((OK t))
  131. (while (consp x)
  132. (unless (memq (pop x)
  133. '(opts-or-alts required-fields numerical-fields
  134. page-dashes whitespace inherit-booktitle realign
  135. last-comma delimiters unify-case braces strings
  136. sort-fields))
  137. (setq OK nil)))
  138. (unless (null x) (setq OK nil))
  139. OK))))
  140. (defcustom bibtex-field-braces-alist nil
  141. "Alist of field regexps that \\[bibtex-clean-entry] encloses by braces.
  142. Each element has the form (FIELDS REGEXP), where FIELDS is a list
  143. of BibTeX field names and REGEXP is a regexp.
  144. Space characters in REGEXP will be replaced by \"[ \\t\\n]+\"."
  145. :group 'bibtex
  146. :type '(repeat (list (repeat (string :tag "field name"))
  147. (choice (regexp :tag "regexp")
  148. (sexp :tag "sexp")))))
  149. (defcustom bibtex-field-strings-alist nil
  150. "Alist of regexps that \\[bibtex-clean-entry] replaces by string constants.
  151. Each element has the form (FIELDS REGEXP TO-STR), where FIELDS is a list
  152. of BibTeX field names. In FIELDS search for REGEXP, which are replaced
  153. by the BibTeX string constant TO-STR.
  154. Space characters in REGEXP will be replaced by \"[ \\t\\n]+\"."
  155. :group 'bibtex
  156. :type '(repeat (list (repeat (string :tag "field name"))
  157. (regexp :tag "From regexp")
  158. (regexp :tag "To string constant"))))
  159. (defcustom bibtex-clean-entry-hook nil
  160. "List of functions to call when entry has been cleaned.
  161. Functions are called with point inside the cleaned entry, and the buffer
  162. narrowed to just the entry."
  163. :group 'bibtex
  164. :type 'hook)
  165. (defcustom bibtex-maintain-sorted-entries nil
  166. "If non-nil, BibTeX mode maintains all entries in sorted order.
  167. Allowed non-nil values are:
  168. plain or t All entries are sorted alphabetically.
  169. crossref All entries are sorted alphabetically unless an entry has a
  170. crossref field. These crossrefed entries are placed in
  171. alphabetical order immediately preceding the main entry.
  172. entry-class The entries are divided into classes according to their
  173. entry type, see `bibtex-sort-entry-class'. Within each class
  174. the entries are sorted alphabetically.
  175. See also `bibtex-sort-ignore-string-entries'."
  176. :group 'bibtex
  177. :type '(choice (const nil)
  178. (const plain)
  179. (const crossref)
  180. (const entry-class)
  181. (const t)))
  182. (put 'bibtex-maintain-sorted-entries 'safe-local-variable
  183. (lambda (a) (memq a '(nil t plain crossref entry-class))))
  184. (defcustom bibtex-sort-entry-class
  185. '(("String")
  186. (catch-all)
  187. ("Book" "Proceedings"))
  188. "List of classes of BibTeX entry types, used for sorting entries.
  189. If value of `bibtex-maintain-sorted-entries' is `entry-class'
  190. entries are ordered according to the classes they belong to. Each
  191. class contains a list of entry types. An entry `catch-all' applies
  192. to all entries not explicitly mentioned."
  193. :group 'bibtex
  194. :type '(repeat (choice :tag "Class"
  195. (const :tag "catch-all" (catch-all))
  196. (repeat :tag "Entry type" string))))
  197. (put 'bibtex-sort-entry-class 'safe-local-variable
  198. (lambda (x) (let ((OK t))
  199. (while (consp x)
  200. (let ((y (pop x)))
  201. (while (consp y)
  202. (let ((z (pop y)))
  203. (unless (or (stringp z) (eq z 'catch-all))
  204. (setq OK nil))))
  205. (unless (null y) (setq OK nil))))
  206. (unless (null x) (setq OK nil))
  207. OK)))
  208. (defcustom bibtex-sort-ignore-string-entries t
  209. "If non-nil, BibTeX @String entries are not sort-significant.
  210. That means they are ignored when determining ordering of the buffer
  211. \(e.g., sorting, locating alphabetical position for new entries, etc.)."
  212. :group 'bibtex
  213. :type 'boolean)
  214. (defcustom bibtex-field-kill-ring-max 20
  215. "Max length of `bibtex-field-kill-ring' before discarding oldest elements."
  216. :group 'bibtex
  217. :type 'integer)
  218. (defcustom bibtex-entry-kill-ring-max 20
  219. "Max length of `bibtex-entry-kill-ring' before discarding oldest elements."
  220. :group 'bibtex
  221. :type 'integer)
  222. (defcustom bibtex-parse-keys-timeout 60
  223. "Time interval in seconds for parsing BibTeX buffers during idle time.
  224. Parsing initializes `bibtex-reference-keys' and `bibtex-strings'."
  225. :group 'bibtex
  226. :type 'integer)
  227. (defcustom bibtex-parse-keys-fast t
  228. "If non-nil, use fast but simplified algorithm for parsing BibTeX keys.
  229. If parsing fails, try to set this variable to nil."
  230. :group 'bibtex
  231. :type 'boolean)
  232. (define-widget 'bibtex-entry-alist 'lazy
  233. "Format of `bibtex-BibTeX-entry-alist' and friends."
  234. :type '(repeat (group (string :tag "Entry type")
  235. (string :tag "Documentation")
  236. (repeat :tag "Required fields"
  237. (group (string :tag "Field")
  238. (option (choice :tag "Comment" :value nil
  239. (const nil) string))
  240. (option (choice :tag "Init" :value nil
  241. (const nil) string function))
  242. (option (choice :tag "Alternative" :value nil
  243. (const nil) integer))))
  244. (repeat :tag "Crossref fields"
  245. (group (string :tag "Field")
  246. (option (choice :tag "Comment" :value nil
  247. (const nil) string))
  248. (option (choice :tag "Init" :value nil
  249. (const nil) string function))
  250. (option (choice :tag "Alternative" :value nil
  251. (const nil) integer))))
  252. (repeat :tag "Optional fields"
  253. (group (string :tag "Field")
  254. (option (choice :tag "Comment" :value nil
  255. (const nil) string))
  256. (option (choice :tag "Init" :value nil
  257. (const nil) string function)))))))
  258. (define-obsolete-variable-alias 'bibtex-entry-field-alist
  259. 'bibtex-BibTeX-entry-alist "24.1")
  260. (defcustom bibtex-BibTeX-entry-alist
  261. '(("Article" "Article in Journal"
  262. (("author")
  263. ("title" "Title of the article (BibTeX converts it to lowercase)"))
  264. (("journal") ("year"))
  265. (("volume" "Volume of the journal")
  266. ("number" "Number of the journal (only allowed if entry contains volume)")
  267. ("pages" "Pages in the journal")
  268. ("month") ("note")))
  269. ("InProceedings" "Article in Conference Proceedings"
  270. (("author")
  271. ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)"))
  272. (("booktitle" "Name of the conference proceedings")
  273. ("year"))
  274. (("editor")
  275. ("volume" "Volume of the conference proceedings in the series")
  276. ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
  277. ("series" "Series in which the conference proceedings appeared")
  278. ("pages" "Pages in the conference proceedings")
  279. ("month") ("address")
  280. ("organization" "Sponsoring organization of the conference")
  281. ("publisher" "Publishing company, its location")
  282. ("note")))
  283. ("InCollection" "Article in a Collection"
  284. (("author")
  285. ("title" "Title of the article in book (BibTeX converts it to lowercase)")
  286. ("booktitle" "Name of the book"))
  287. (("publisher") ("year"))
  288. (("editor")
  289. ("volume" "Volume of the book in the series")
  290. ("number" "Number of the book in a small series (overwritten by volume)")
  291. ("series" "Series in which the book appeared")
  292. ("type" "Word to use instead of \"chapter\"")
  293. ("chapter" "Chapter in the book")
  294. ("pages" "Pages in the book")
  295. ("edition" "Edition of the book as a capitalized English word")
  296. ("month") ("address") ("note")))
  297. ("InBook" "Chapter or Pages in a Book"
  298. (("author" nil nil 0)
  299. ("editor" nil nil 0)
  300. ("title" "Title of the book")
  301. ("chapter" "Chapter in the book"))
  302. (("publisher") ("year"))
  303. (("volume" "Volume of the book in the series")
  304. ("number" "Number of the book in a small series (overwritten by volume)")
  305. ("series" "Series in which the book appeared")
  306. ("type" "Word to use instead of \"chapter\"")
  307. ("address")
  308. ("edition" "Edition of the book as a capitalized English word")
  309. ("month")
  310. ("pages" "Pages in the book")
  311. ("note")))
  312. ("Proceedings" "Conference Proceedings"
  313. (("title" "Title of the conference proceedings")
  314. ("year"))
  315. nil
  316. (("booktitle" "Title of the proceedings for cross references")
  317. ("editor")
  318. ("volume" "Volume of the conference proceedings in the series")
  319. ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
  320. ("series" "Series in which the conference proceedings appeared")
  321. ("address")
  322. ("month")
  323. ("organization" "Sponsoring organization of the conference")
  324. ("publisher" "Publishing company, its location")
  325. ("note")))
  326. ("Book" "Book"
  327. (("author" nil nil 0)
  328. ("editor" nil nil 0)
  329. ("title" "Title of the book"))
  330. (("publisher") ("year"))
  331. (("volume" "Volume of the book in the series")
  332. ("number" "Number of the book in a small series (overwritten by volume)")
  333. ("series" "Series in which the book appeared")
  334. ("address")
  335. ("edition" "Edition of the book as a capitalized English word")
  336. ("month") ("note")))
  337. ("Booklet" "Booklet (Bound, but no Publisher)"
  338. (("title" "Title of the booklet (BibTeX converts it to lowercase)"))
  339. nil
  340. (("author")
  341. ("howpublished" "The way in which the booklet was published")
  342. ("address") ("month") ("year") ("note")))
  343. ("PhdThesis" "PhD. Thesis"
  344. (("author")
  345. ("title" "Title of the PhD. thesis")
  346. ("school" "School where the PhD. thesis was written")
  347. ("year"))
  348. nil
  349. (("type" "Type of the PhD. thesis")
  350. ("address" "Address of the school (if not part of field \"school\") or country")
  351. ("month") ("note")))
  352. ("MastersThesis" "Master's Thesis"
  353. (("author")
  354. ("title" "Title of the master's thesis (BibTeX converts it to lowercase)")
  355. ("school" "School where the master's thesis was written")
  356. ("year"))
  357. nil
  358. (("type" "Type of the master's thesis (if other than \"Master's thesis\")")
  359. ("address" "Address of the school (if not part of field \"school\") or country")
  360. ("month") ("note")))
  361. ("TechReport" "Technical Report"
  362. (("author")
  363. ("title" "Title of the technical report (BibTeX converts it to lowercase)")
  364. ("institution" "Sponsoring institution of the report")
  365. ("year"))
  366. nil
  367. (("type" "Type of the report (if other than \"technical report\")")
  368. ("number" "Number of the technical report")
  369. ("address") ("month") ("note")))
  370. ("Manual" "Technical Manual"
  371. (("title" "Title of the manual"))
  372. nil
  373. (("author")
  374. ("organization" "Publishing organization of the manual")
  375. ("address")
  376. ("edition" "Edition of the manual as a capitalized English word")
  377. ("month") ("year") ("note")))
  378. ("Unpublished" "Unpublished"
  379. (("author")
  380. ("title" "Title of the unpublished work (BibTeX converts it to lowercase)")
  381. ("note"))
  382. nil
  383. (("month") ("year")))
  384. ("Misc" "Miscellaneous" nil nil
  385. (("author")
  386. ("title" "Title of the work (BibTeX converts it to lowercase)")
  387. ("howpublished" "The way in which the work was published")
  388. ("month") ("year") ("note"))))
  389. "Alist of BibTeX entry types and their associated fields.
  390. Elements are lists (ENTRY-TYPE DOC REQUIRED CROSSREF OPTIONAL).
  391. ENTRY-TYPE is the type of a BibTeX entry.
  392. DOC is a brief doc string used for menus. If nil ENTRY-TYPE is used.
  393. REQUIRED is a list of required fields.
  394. CROSSREF is a list of fields that are optional if a crossref field
  395. is present; but these fields are required otherwise.
  396. OPTIONAL is a list of optional fields.
  397. Each element of these lists is a list of the form
  398. \(FIELD COMMENT INIT ALTERNATIVE).
  399. COMMENT, INIT, and ALTERNATIVE are optional.
  400. FIELD is the name of the field.
  401. COMMENT is the comment string that appears in the echo area.
  402. If COMMENT is nil use `bibtex-BibTeX-field-alist' if possible.
  403. INIT is either the initial content of the field or a function,
  404. which is called to determine the initial content of the field.
  405. ALTERNATIVE if non-nil is an integer that numbers sets of
  406. alternatives, starting from zero."
  407. :group 'BibTeX
  408. :version "24.1"
  409. :type 'bibtex-entry-alist)
  410. (put 'bibtex-BibTeX-entry-alist 'risky-local-variable t)
  411. (defcustom bibtex-biblatex-entry-alist
  412. ;; Compare in biblatex documentation:
  413. ;; Sec. 2.1.1 Regular types (required and optional fields)
  414. ;; Appendix A Default Crossref setup
  415. '(("Article" "Article in Journal"
  416. (("author") ("title") ("journaltitle")
  417. ("year" nil nil 0) ("date" nil nil 0))
  418. nil
  419. (("translator") ("annotator") ("commentator") ("subtitle") ("titleaddon")
  420. ("editor") ("editora") ("editorb") ("editorc")
  421. ("journalsubtitle") ("issuetitle") ("issuesubtitle")
  422. ("language") ("origlanguage") ("series") ("volume") ("number") ("eid")
  423. ("issue") ("month") ("pages") ("version") ("note") ("issn")
  424. ("addendum") ("pubstate") ("doi") ("eprint") ("eprintclass")
  425. ("eprinttype") ("url") ("urldate")))
  426. ("Book" "Single-Volume Book"
  427. (("author") ("title") ("year" nil nil 0) ("date" nil nil 0))
  428. nil
  429. (("editor") ("editora") ("editorb") ("editorc")
  430. ("translator") ("annotator") ("commentator")
  431. ("introduction") ("foreword") ("afterword") ("titleaddon")
  432. ("maintitle") ("mainsubtitle") ("maintitleaddon")
  433. ("language") ("origlanguage") ("volume") ("part") ("edition") ("volumes")
  434. ("series") ("number") ("note") ("publisher") ("location") ("isbn")
  435. ("chapter") ("pages") ("pagetotal") ("addendum") ("pubstate") ("doi")
  436. ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  437. ("MVBook" "Multi-Volume Book"
  438. (("author") ("title") ("year" nil nil 0) ("date" nil nil 0))
  439. nil
  440. (("editor") ("editora") ("editorb") ("editorc")
  441. ("translator") ("annotator") ("commentator")
  442. ("introduction") ("foreword") ("afterword") ("subtitle")
  443. ("titleaddon") ("language") ("origlanguage") ("edition") ("volumes")
  444. ("series") ("number") ("note") ("publisher")
  445. ("location") ("isbn") ("pagetotal") ("addendum") ("pubstate") ("doi")
  446. ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  447. ("InBook" "Chapter or Pages in a Book"
  448. (("title") ("year" nil nil 0) ("date" nil nil 0))
  449. (("author") ("booktitle"))
  450. (("bookauthor") ("editor") ("editora") ("editorb") ("editorc")
  451. ("translator") ("annotator") ("commentator") ("introduction") ("foreword")
  452. ("afterword") ("subtitle") ("titleaddon") ("maintitle") ("mainsubtitle")
  453. ("maintitleaddon") ("booksubtitle") ("booktitleaddon")
  454. ("language") ("origlanguage") ("volume") ("part") ("edition") ("volumes")
  455. ("series") ("number") ("note") ("publisher") ("location") ("isbn")
  456. ("chapter") ("pages") ("addendum") ("pubstate")
  457. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  458. ("BookInBook" "Book in Collection" ; same as @inbook
  459. (("title") ("year" nil nil 0) ("date" nil nil 0))
  460. (("author") ("booktitle"))
  461. (("bookauthor") ("editor") ("editora") ("editorb") ("editorc")
  462. ("translator") ("annotator") ("commentator") ("introduction") ("foreword")
  463. ("afterword") ("subtitle") ("titleaddon") ("maintitle") ("mainsubtitle")
  464. ("maintitleaddon") ("booksubtitle") ("booktitleaddon")
  465. ("language") ("origlanguage") ("volume") ("part") ("edition") ("volumes")
  466. ("series") ("number") ("note") ("publisher") ("location") ("isbn")
  467. ("chapter") ("pages") ("addendum") ("pubstate")
  468. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  469. ("SuppBook" "Supplemental Material in a Book" ; same as @inbook
  470. (("title") ("year" nil nil 0) ("date" nil nil 0))
  471. (("author") ("booktitle"))
  472. (("bookauthor") ("editor") ("editora") ("editorb") ("editorc")
  473. ("translator") ("annotator") ("commentator") ("introduction") ("foreword")
  474. ("afterword") ("subtitle") ("titleaddon") ("maintitle") ("mainsubtitle")
  475. ("maintitleaddon") ("booksubtitle") ("booktitleaddon")
  476. ("language") ("origlanguage") ("volume") ("part") ("edition") ("volumes")
  477. ("series") ("number") ("note") ("publisher") ("location") ("isbn")
  478. ("chapter") ("pages") ("addendum") ("pubstate")
  479. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  480. ("Booklet" "Booklet (Bound, but no Publisher)"
  481. (("author" nil nil 0) ("editor" nil nil 0) ("title")
  482. ("year" nil nil 1) ("date" nil nil 1))
  483. nil
  484. (("subtitle") ("titleaddon") ("language") ("howpublished") ("type")
  485. ("note") ("location") ("chapter") ("pages") ("pagetotal") ("addendum")
  486. ("pubstate") ("doi") ("eprint") ("eprintclass") ("eprinttype")
  487. ("url") ("urldate")))
  488. ("Collection" "Single-Volume Collection"
  489. (("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  490. nil
  491. (("editora") ("editorb") ("editorc") ("translator") ("annotator")
  492. ("commentator") ("introduction") ("foreword") ("afterword")
  493. ("subtitle") ("titleaddon") ("maintitle") ("mainsubtitle")
  494. ("maintitleaddon") ("language") ("origlanguage") ("volume")
  495. ("part") ("edition") ("volumes") ("series") ("number") ("note")
  496. ("publisher") ("location") ("isbn") ("chapter") ("pages") ("pagetotal")
  497. ("addendum") ("pubstate") ("doi") ("eprint") ("eprintclass")
  498. ("eprinttype") ("url") ("urldate")))
  499. ("MVCollection" "Multi-Volume Collection"
  500. (("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  501. nil
  502. (("editora") ("editorb") ("editorc") ("translator") ("annotator")
  503. ("commentator") ("introduction") ("foreword") ("afterword")
  504. ("subtitle") ("titleaddon") ("language") ("origlanguage") ("edition")
  505. ("volumes") ("series") ("number") ("note") ("publisher")
  506. ("location") ("isbn") ("pagetotal") ("addendum") ("pubstate") ("doi")
  507. ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  508. ("InCollection" "Article in a Collection"
  509. (("author") ("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  510. (("booktitle"))
  511. (("editora") ("editorb") ("editorc") ("translator") ("annotator")
  512. ("commentator") ("introduction") ("foreword") ("afterword")
  513. ("subtitle") ("titleaddon") ("maintitle") ("mainsubtitle")
  514. ("maintitleaddon") ("booksubtitle") ("booktitleaddon")
  515. ("language") ("origlanguage") ("volume") ("part") ("edition")
  516. ("volumes") ("series") ("number") ("note") ("publisher") ("location")
  517. ("isbn") ("chapter") ("pages") ("addendum") ("pubstate") ("doi")
  518. ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  519. ("SuppCollection" "Supplemental Material in a Collection" ; same as @incollection
  520. (("author") ("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  521. (("booktitle"))
  522. (("editora") ("editorb") ("editorc") ("translator") ("annotator")
  523. ("commentator") ("introduction") ("foreword") ("afterword")
  524. ("subtitle") ("titleaddon") ("maintitle") ("mainsubtitle")
  525. ("maintitleaddon") ("booksubtitle") ("booktitleaddon")
  526. ("language") ("origlanguage") ("volume") ("part") ("edition")
  527. ("volumes") ("series") ("number") ("note") ("publisher") ("location")
  528. ("isbn") ("chapter") ("pages") ("addendum") ("pubstate") ("doi")
  529. ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  530. ("Manual" "Technical Manual"
  531. (("author" nil nil 0) ("editor" nil nil 0) ("title")
  532. ("year" nil nil 1) ("date" nil nil 1))
  533. nil
  534. (("subtitle") ("titleaddon") ("language") ("edition")
  535. ("type") ("series") ("number") ("version") ("note")
  536. ("organization") ("publisher") ("location") ("isbn") ("chapter")
  537. ("pages") ("pagetotal") ("addendum") ("pubstate")
  538. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  539. ("Misc" "Miscellaneous"
  540. (("author" nil nil 0) ("editor" nil nil 0) ("title")
  541. ("year" nil nil 1) ("date" nil nil 1))
  542. nil
  543. (("subtitle") ("titleaddon") ("language") ("howpublished") ("type")
  544. ("version") ("note") ("organization") ("location")
  545. ("date") ("month") ("year") ("addendum") ("pubstate")
  546. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  547. ("Online" "Online Resource"
  548. (("author" nil nil 0) ("editor" nil nil 0) ("title")
  549. ("year" nil nil 1) ("date" nil nil 1) ("url"))
  550. nil
  551. (("subtitle") ("titleaddon") ("language") ("version") ("note")
  552. ("organization") ("date") ("month") ("year") ("addendum")
  553. ("pubstate") ("urldate")))
  554. ("Patent" "Patent"
  555. (("author") ("title") ("number") ("year" nil nil 0) ("date" nil nil 0))
  556. nil
  557. (("holder") ("subtitle") ("titleaddon") ("type") ("version") ("location")
  558. ("note") ("date") ("month") ("year") ("addendum") ("pubstate")
  559. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  560. ("Periodical" "Complete Issue of a Periodical"
  561. (("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  562. nil
  563. (("editora") ("editorb") ("editorc") ("subtitle") ("issuetitle")
  564. ("issuesubtitle") ("language") ("series") ("volume") ("number") ("issue")
  565. ("date") ("month") ("year") ("note") ("issn") ("addendum") ("pubstate")
  566. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  567. ("SuppPeriodical" "Supplemental Material in a Periodical" ; same as @article
  568. (("author") ("title") ("journaltitle")
  569. ("year" nil nil 0) ("date" nil nil 0))
  570. nil
  571. (("translator") ("annotator") ("commentator") ("subtitle") ("titleaddon")
  572. ("editor") ("editora") ("editorb") ("editorc")
  573. ("journalsubtitle") ("issuetitle") ("issuesubtitle")
  574. ("language") ("origlanguage") ("series") ("volume") ("number") ("eid")
  575. ("issue") ("month") ("pages") ("version") ("note") ("issn")
  576. ("addendum") ("pubstate") ("doi") ("eprint") ("eprintclass")
  577. ("eprinttype") ("url") ("urldate")))
  578. ("Proceedings" "Single-Volume Conference Proceedings"
  579. (("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  580. nil
  581. (("subtitle") ("titleaddon") ("maintitle") ("mainsubtitle")
  582. ("maintitleaddon") ("eventtitle") ("eventdate") ("venue") ("language")
  583. ("volume") ("part") ("volumes") ("series") ("number") ("note")
  584. ("organization") ("publisher") ("location") ("month")
  585. ("isbn") ("chapter") ("pages") ("pagetotal") ("addendum") ("pubstate")
  586. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  587. ("MVProceedings" "Multi-Volume Conference Proceedings"
  588. (("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  589. nil
  590. (("subtitle") ("titleaddon") ("eventtitle") ("eventdate") ("venue")
  591. ("language") ("volumes") ("series") ("number") ("note")
  592. ("organization") ("publisher") ("location") ("month")
  593. ("isbn") ("pagetotal") ("addendum") ("pubstate")
  594. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  595. ("InProceedings" "Article in Conference Proceedings"
  596. (("author") ("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  597. (("booktitle"))
  598. (("subtitle") ("titleaddon") ("maintitle") ("mainsubtitle")
  599. ("maintitleaddon") ("booksubtitle") ("booktitleaddon")
  600. ("eventtitle") ("eventdate") ("venue") ("language")
  601. ("volume") ("part") ("volumes") ("series") ("number") ("note")
  602. ("organization") ("publisher") ("location") ("month") ("isbn")
  603. ("chapter") ("pages") ("addendum") ("pubstate")
  604. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  605. ("Reference" "Single-Volume Work of Reference" ; same as @collection
  606. (("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  607. nil
  608. (("editora") ("editorb") ("editorc") ("translator") ("annotator")
  609. ("commentator") ("introduction") ("foreword") ("afterword")
  610. ("subtitle") ("titleaddon") ("maintitle") ("mainsubtitle")
  611. ("maintitleaddon") ("language") ("origlanguage") ("volume")
  612. ("part") ("edition") ("volumes") ("series") ("number") ("note")
  613. ("publisher") ("location") ("isbn") ("chapter") ("pages") ("pagetotal")
  614. ("addendum") ("pubstate") ("doi") ("eprint") ("eprintclass")
  615. ("eprinttype") ("url") ("urldate")))
  616. ("MVReference" "Multi-Volume Work of Reference" ; same as @mvcollection
  617. (("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  618. nil
  619. (("editora") ("editorb") ("editorc") ("translator") ("annotator")
  620. ("commentator") ("introduction") ("foreword") ("afterword")
  621. ("subtitle") ("titleaddon") ("language") ("origlanguage") ("edition")
  622. ("volumes") ("series") ("number") ("note") ("publisher")
  623. ("location") ("isbn") ("pagetotal") ("addendum") ("pubstate") ("doi")
  624. ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  625. ("InReference" "Article in a Work of Reference" ; same as @incollection
  626. (("author") ("editor") ("title") ("year" nil nil 0) ("date" nil nil 0))
  627. (("booktitle"))
  628. (("editora") ("editorb") ("editorc") ("translator") ("annotator")
  629. ("commentator") ("introduction") ("foreword") ("afterword")
  630. ("subtitle") ("titleaddon") ("maintitle") ("mainsubtitle")
  631. ("maintitleaddon") ("booksubtitle") ("booktitleaddon")
  632. ("language") ("origlanguage") ("volume") ("part") ("edition")
  633. ("volumes") ("series") ("number") ("note") ("publisher") ("location")
  634. ("isbn") ("chapter") ("pages") ("addendum") ("pubstate") ("doi")
  635. ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  636. ("Report" "Technical or Research Report"
  637. (("author") ("title") ("type") ("institution")
  638. ("year" nil nil 0) ("date" nil nil 0))
  639. nil
  640. (("subtitle") ("titleaddon") ("language") ("number") ("version") ("note")
  641. ("location") ("month") ("isrn") ("chapter") ("pages") ("pagetotal")
  642. ("addendum") ("pubstate")
  643. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  644. ("Thesis" "PhD. or Master's Thesis"
  645. (("author") ("title") ("type") ("institution")
  646. ("year" nil nil 0) ("date" nil nil 0))
  647. nil
  648. (("subtitle") ("titleaddon") ("language") ("note") ("location")
  649. ("month") ("isbn") ("chapter") ("pages") ("pagetotal")
  650. ("addendum") ("pubstate")
  651. ("doi") ("eprint") ("eprintclass") ("eprinttype") ("url") ("urldate")))
  652. ("Unpublished" "Unpublished"
  653. (("author") ("title") ("year" nil nil 0) ("date" nil nil 0))
  654. nil
  655. (("subtitle") ("titleaddon") ("language") ("howpublished")
  656. ("note") ("location") ("isbn") ("date") ("month") ("year")
  657. ("addendum") ("pubstate") ("url") ("urldate"))))
  658. "Alist of biblatex entry types and their associated fields.
  659. It has the same format as `bibtex-BibTeX-entry-alist'."
  660. :group 'bibtex
  661. :version "24.1"
  662. :type 'bibtex-entry-alist)
  663. (put 'bibtex-biblatex-entry-alist 'risky-local-variable t)
  664. (define-widget 'bibtex-field-alist 'lazy
  665. "Format of `bibtex-BibTeX-entry-alist' and friends."
  666. :type '(repeat (group (string :tag "Field type")
  667. (string :tag "Comment"))))
  668. (defcustom bibtex-BibTeX-field-alist
  669. '(("author" "Author1 [and Author2 ...] [and others]")
  670. ("editor" "Editor1 [and Editor2 ...] [and others]")
  671. ("journal" "Name of the journal (use string, remove braces)")
  672. ("year" "Year of publication")
  673. ("month" "Month of the publication as a string (remove braces)")
  674. ("note" "Remarks to be put at the end of the \\bibitem")
  675. ("publisher" "Publishing company")
  676. ("address" "Address of the publisher"))
  677. "Alist of BibTeX fields.
  678. Each element is a list (FIELD COMMENT). COMMENT is used as a default
  679. if `bibtex-BibTeX-entry-alist' does not define a comment for FIELD."
  680. :group 'bibtex
  681. :version "24.1"
  682. :type 'bibtex-field-alist)
  683. (defcustom bibtex-biblatex-field-alist
  684. ;; See 2.2.2 Data Fields
  685. '(("abstract" "Abstract of the work")
  686. ("addendum" "Miscellaneous bibliographic data")
  687. ("afterword" "Author(s) of an afterword to the work")
  688. ("annotation" "Annotation")
  689. ("annotator" "Author(s) of annotations to the work")
  690. ("author" "Author(s) of the title")
  691. ("bookauthor" "Author(s) of the booktitle.")
  692. ("bookpagination" "Pagination scheme of the enclosing work")
  693. ("booksubtitle" "Subtitle related to the booktitle")
  694. ("booktitle" "Title of the book")
  695. ("booktitleaddon" "Annex to the booktitle")
  696. ("chapter" "Chapter, section, or any other unit of a work")
  697. ("commentator" "Author(s) of a commentary to the work")
  698. ("date" "Publication date")
  699. ("doi" "Digital Object Identifier")
  700. ("edition" "Edition of a printed publication")
  701. ("editor" "Editor(s) of the title, booktitle, or maintitle")
  702. ("editora" "Secondary editor")
  703. ("editorb" "Secondary editor")
  704. ("editorc" "Secondary editor")
  705. ("editortype" "Type of editorial role performed by the editor")
  706. ("editoratype" "Type of editorial role performed by editora")
  707. ("editorbtype" "Type of editorial role performed by editorb")
  708. ("editorctype" "Type of editorial role performed by editorc")
  709. ("eid" "Electronic identifier of an article")
  710. ("eprint" "Electronic identifier of an online publication")
  711. ("eprintclass" "Additional information related to the eprinttype")
  712. ("eprinttype" "Type of eprint identifier")
  713. ("eventdate" "Date of a conference or some other event")
  714. ("eventtitle" "Title of a conference or some other event")
  715. ("file" "Local link to an electronic version of the work")
  716. ("foreword" "Author(s) of a foreword to the work")
  717. ("holder" "Holder(s) of a patent")
  718. ("howpublished" "Publication notice for unusual publications")
  719. ("indextitle" "Title to use for indexing instead of the regular title")
  720. ("institution" "Name of a university or some other institution")
  721. ("introduction" "Author(s) of an introduction to the work")
  722. ("isan" "International Standard Audiovisual Number of an audiovisual work")
  723. ("isbn" "International Standard Book Number of a book.")
  724. ("ismn" "International Standard Music Number for printed music")
  725. ("isrn" "International Standard Technical Report Number")
  726. ("issn" "International Standard Serial Number of a periodical.")
  727. ("issue" "Issue of a journal")
  728. ("issuesubtitle" "Subtitle of a specific issue of a journal or other periodical.")
  729. ("issuetitle" "Title of a specific issue of a journal or other periodical.")
  730. ("iswc" "International Standard Work Code of a musical work")
  731. ("journalsubtitle" "Subtitle of a journal, a newspaper, or some other periodical.")
  732. ("journaltitle" "Name of a journal, a newspaper, or some other periodical.")
  733. ("label" "Substitute for the regular label to be used by the citation style")
  734. ("language" "Language(s) of the work")
  735. ("library" "Library name and a call number")
  736. ("location" "Place(s) of publication")
  737. ("mainsubtitle" "Subtitle related to the maintitle")
  738. ("maintitle" "Main title of a multi-volume book, such as Collected Works")
  739. ("maintitleaddon" "Annex to the maintitle")
  740. ("month" "Publication month")
  741. ("nameaddon" "Addon to be printed immediately after the author name")
  742. ("note" "Miscellaneous bibliographic data")
  743. ("number" "Number of a journal or the volume/number of a book in a series")
  744. ("organization" "Organization(s) that published a work")
  745. ("origdate" "Publication date of the original edition")
  746. ("origlanguage" "Original publication language of a translated edition")
  747. ("origlocation" "Location of the original edition")
  748. ("origpublisher" "Publisher of the original edition")
  749. ("origtitle" "Title of the original work")
  750. ("pages" "Page number(s) or page range(s)")
  751. ("pagetotal" "Total number of pages of the work.")
  752. ("pagination" "Pagination of the work")
  753. ("part" "Number of a partial volume")
  754. ("publisher" "Name(s) of the publisher(s)")
  755. ("pubstate" "Publication state of the work, e. g.,'in press'")
  756. ("reprinttitle" "Title of a reprint of the work")
  757. ("series" "Name of a publication series")
  758. ("shortauthor" "Author(s) of the work, given in an abbreviated form")
  759. ("shorteditor" "Editor(s) of the work, given in an abbreviated form")
  760. ("shortjournal" "Short version or an acronym of the journal title")
  761. ("shortseries" "Short version or an acronym of the series field")
  762. ("shorttitle" "Title in an abridged form")
  763. ("subtitle" "Subtitle of the work")
  764. ("title" "Title of the work")
  765. ("titleaddon" "Annex to the title")
  766. ("translator" "Translator(s) of the work")
  767. ("type" "Type of a manual, patent, report, or thesis")
  768. ("url" " URL of an online publication.")
  769. ("urldate" "Access date of the address specified in the url field")
  770. ("venue" "Location of a conference, a symposium, or some other event")
  771. ("version" "Revision number of a piece of software, a manual, etc.")
  772. ("volume" "Volume of a multi-volume book or a periodical")
  773. ("volumes" "Total number of volumes of a multi-volume work")
  774. ("year" "Year of publication"))
  775. "Alist of biblatex fields.
  776. It has the same format as `bibtex-BibTeX-entry-alist'."
  777. :group 'bibtex
  778. :version "24.1"
  779. :type 'bibtex-field-alist)
  780. (defcustom bibtex-dialect-list '(BibTeX biblatex)
  781. "List of BibTeX dialects known to BibTeX mode.
  782. For each DIALECT (a symbol) a variable bibtex-DIALECT-entry-alist defines
  783. the allowed entries and bibtex-DIALECT-field-alist defines known field types.
  784. Predefined dialects include BibTeX and biblatex."
  785. :group 'bibtex
  786. :version "24.1"
  787. :type '(repeat (symbol :tag "Dialect")))
  788. (defcustom bibtex-dialect 'BibTeX
  789. "Current BibTeX dialect. For allowed values see `bibtex-dialect-list'.
  790. To interactively change the dialect use the command `bibtex-set-dialect'."
  791. :group 'bibtex
  792. :version "24.1"
  793. :set '(lambda (symbol value)
  794. (set-default symbol value)
  795. ;; `bibtex-set-dialect' is undefined during loading (no problem)
  796. (if (fboundp 'bibtex-set-dialect)
  797. (bibtex-set-dialect value)))
  798. :type '(choice (const BibTeX)
  799. (const biblatex)
  800. (symbol :tag "Custom")))
  801. (put 'bibtex-dialect 'safe-local-variable 'symbolp)
  802. (defcustom bibtex-no-opt-remove-re "\\`option"
  803. "If a field name matches this regexp, the prefix OPT is not removed.
  804. If nil prefix OPT is always removed"
  805. :group 'bibtex
  806. :version "24.1"
  807. :type '(choice (regexp) (const nil)))
  808. (defcustom bibtex-comment-start "@Comment"
  809. "String starting a BibTeX comment."
  810. :group 'bibtex
  811. :type 'string)
  812. (defcustom bibtex-add-entry-hook nil
  813. "List of functions to call when BibTeX entry has been inserted."
  814. :group 'bibtex
  815. :type 'hook)
  816. (defcustom bibtex-predefined-month-strings
  817. '(("jan" . "January")
  818. ("feb" . "February")
  819. ("mar" . "March")
  820. ("apr" . "April")
  821. ("may" . "May")
  822. ("jun" . "June")
  823. ("jul" . "July")
  824. ("aug" . "August")
  825. ("sep" . "September")
  826. ("oct" . "October")
  827. ("nov" . "November")
  828. ("dec" . "December"))
  829. "Alist of month string definitions used in the BibTeX style files.
  830. Each element is a pair of strings (ABBREVIATION . EXPANSION)."
  831. :group 'bibtex
  832. :type '(repeat (cons (string :tag "Month abbreviation")
  833. (string :tag "Month expansion"))))
  834. (defcustom bibtex-predefined-strings
  835. (append
  836. bibtex-predefined-month-strings
  837. '(("acmcs" . "ACM Computing Surveys")
  838. ("acta" . "Acta Informatica")
  839. ("cacm" . "Communications of the ACM")
  840. ("ibmjrd" . "IBM Journal of Research and Development")
  841. ("ibmsj" . "IBM Systems Journal")
  842. ("ieeese" . "IEEE Transactions on Software Engineering")
  843. ("ieeetc" . "IEEE Transactions on Computers")
  844. ("ieeetcad" . "IEEE Transactions on Computer-Aided Design of Integrated Circuits")
  845. ("ipl" . "Information Processing Letters")
  846. ("jacm" . "Journal of the ACM")
  847. ("jcss" . "Journal of Computer and System Sciences")
  848. ("scp" . "Science of Computer Programming")
  849. ("sicomp" . "SIAM Journal on Computing")
  850. ("tcs" . "Theoretical Computer Science")
  851. ("tocs" . "ACM Transactions on Computer Systems")
  852. ("tods" . "ACM Transactions on Database Systems")
  853. ("tog" . "ACM Transactions on Graphics")
  854. ("toms" . "ACM Transactions on Mathematical Software")
  855. ("toois" . "ACM Transactions on Office Information Systems")
  856. ("toplas" . "ACM Transactions on Programming Languages and Systems")))
  857. "Alist of string definitions used in the BibTeX style files.
  858. Each element is a pair of strings (ABBREVIATION . EXPANSION)."
  859. :group 'bibtex
  860. :type '(repeat (cons (string :tag "String")
  861. (string :tag "String expansion"))))
  862. (defcustom bibtex-string-files nil
  863. "List of BibTeX files containing string definitions.
  864. List elements can be absolute file names or file names relative
  865. to the directories specified in `bibtex-string-file-path'."
  866. :group 'bibtex
  867. :type '(repeat file))
  868. (defvar bibtex-string-file-path (getenv "BIBINPUTS")
  869. "*Colon separated list of paths to search for `bibtex-string-files'.")
  870. (defcustom bibtex-files nil
  871. "List of BibTeX files that are searched for entry keys.
  872. List elements can be absolute file names or file names relative to the
  873. directories specified in `bibtex-file-path'. If an element is a directory,
  874. check all BibTeX files in this directory. If an element is the symbol
  875. `bibtex-file-path', check all BibTeX files in `bibtex-file-path'.
  876. See also `bibtex-search-entry-globally'."
  877. :group 'bibtex
  878. :type '(repeat (choice (const :tag "bibtex-file-path" bibtex-file-path)
  879. directory file)))
  880. (defvar bibtex-file-path (getenv "BIBINPUTS")
  881. "*Colon separated list of paths to search for `bibtex-files'.")
  882. (defcustom bibtex-search-entry-globally nil
  883. "If non-nil, interactive calls of `bibtex-search-entry' search globally.
  884. A global search includes all files in `bibtex-files'."
  885. :group 'bibtex
  886. :version "24.1"
  887. :type 'boolean)
  888. (defcustom bibtex-help-message t
  889. "If non-nil print help messages in the echo area on entering a new field."
  890. :group 'bibtex
  891. :type 'boolean)
  892. (defcustom bibtex-autokey-prefix-string ""
  893. "String prefix for automatically generated reference keys.
  894. See `bibtex-generate-autokey' for details."
  895. :group 'bibtex-autokey
  896. :type 'string)
  897. (defcustom bibtex-autokey-names 1
  898. "Number of names to use for the automatically generated reference key.
  899. Possibly more names are used according to `bibtex-autokey-names-stretch'.
  900. If this variable is nil, all names are used.
  901. See `bibtex-generate-autokey' for details."
  902. :group 'bibtex-autokey
  903. :type '(choice (const :tag "All" infty)
  904. integer))
  905. (defcustom bibtex-autokey-names-stretch 0
  906. "Number of names that can additionally be used for reference keys.
  907. These names are used only, if all names are used then.
  908. See `bibtex-generate-autokey' for details."
  909. :group 'bibtex-autokey
  910. :type 'integer)
  911. (defcustom bibtex-autokey-additional-names ""
  912. "String to append to the generated key if not all names could be used.
  913. See `bibtex-generate-autokey' for details."
  914. :group 'bibtex-autokey
  915. :type 'string)
  916. (defcustom bibtex-autokey-expand-strings nil
  917. "If non-nil, expand strings when extracting the content of a BibTeX field.
  918. See `bibtex-generate-autokey' for details."
  919. :group 'bibtex-autokey
  920. :type 'boolean)
  921. (defvar bibtex-autokey-transcriptions
  922. '(;; language specific characters
  923. ("\\\\aa" . "a") ; \aa -> a
  924. ("\\\\AA" . "A") ; \AA -> A
  925. ("\\\"a\\|\\\\\\\"a\\|\\\\ae" . "ae") ; "a,\"a,\ae -> ae
  926. ("\\\"A\\|\\\\\\\"A\\|\\\\AE" . "Ae") ; "A,\"A,\AE -> Ae
  927. ("\\\\i" . "i") ; \i -> i
  928. ("\\\\j" . "j") ; \j -> j
  929. ("\\\\l" . "l") ; \l -> l
  930. ("\\\\L" . "L") ; \L -> L
  931. ("\\\"o\\|\\\\\\\"o\\|\\\\o\\|\\\\oe" . "oe") ; "o,\"o,\o,\oe -> oe
  932. ("\\\"O\\|\\\\\\\"O\\|\\\\O\\|\\\\OE" . "Oe") ; "O,\"O,\O,\OE -> Oe
  933. ("\\\"s\\|\\\\\\\"s\\|\\\\3" . "ss") ; "s,\"s,\3 -> ss
  934. ("\\\"u\\|\\\\\\\"u" . "ue") ; "u,\"u -> ue
  935. ("\\\"U\\|\\\\\\\"U" . "Ue") ; "U,\"U -> Ue
  936. ;; accents
  937. ("\\\\`\\|\\\\'\\|\\\\\\^\\|\\\\~\\|\\\\=\\|\\\\\\.\\|\\\\u\\|\\\\v\\|\\\\H\\|\\\\t\\|\\\\c\\|\\\\d\\|\\\\b" . "")
  938. ;; braces, quotes, concatenation.
  939. ("[`'\"{}#]" . "")
  940. ;; spaces
  941. ("\\\\?[ \t\n]+\\|~" . " "))
  942. "Alist of (OLD-REGEXP . NEW-STRING) pairs.
  943. Used by the default values of `bibtex-autokey-name-change-strings' and
  944. `bibtex-autokey-titleword-change-strings'. Defaults to translating some
  945. language specific characters to their ASCII transcriptions, and
  946. removing any character accents.")
  947. (defcustom bibtex-autokey-name-change-strings
  948. bibtex-autokey-transcriptions
  949. "Alist of (OLD-REGEXP . NEW-STRING) pairs.
  950. Any part of a name matching OLD-REGEXP is replaced by NEW-STRING.
  951. Case is significant in OLD-REGEXP. All regexps are tried in the
  952. order in which they appear in the list.
  953. See `bibtex-generate-autokey' for details."
  954. :group 'bibtex-autokey
  955. :type '(repeat (cons (regexp :tag "Old")
  956. (string :tag "New"))))
  957. (defcustom bibtex-autokey-name-case-convert-function 'downcase
  958. "Function called for each name to perform case conversion.
  959. See `bibtex-generate-autokey' for details."
  960. :group 'bibtex-autokey
  961. :type '(choice (const :tag "Preserve case" identity)
  962. (const :tag "Downcase" downcase)
  963. (const :tag "Capitalize" capitalize)
  964. (const :tag "Upcase" upcase)
  965. (function :tag "Conversion function")))
  966. (put 'bibtex-autokey-name-case-convert-function 'safe-local-variable
  967. (lambda (x) (memq x '(upcase downcase capitalize identity))))
  968. (defvaralias 'bibtex-autokey-name-case-convert
  969. 'bibtex-autokey-name-case-convert-function)
  970. (defcustom bibtex-autokey-name-length 'infty
  971. "Number of characters from name to incorporate into key.
  972. If this is set to anything but a number, all characters are used.
  973. See `bibtex-generate-autokey' for details."
  974. :group 'bibtex-autokey
  975. :type '(choice (const :tag "All" infty)
  976. integer))
  977. (defcustom bibtex-autokey-name-separator ""
  978. "String that comes between any two names in the key.
  979. See `bibtex-generate-autokey' for details."
  980. :group 'bibtex-autokey
  981. :type 'string)
  982. (defcustom bibtex-autokey-year-length 2
  983. "Number of rightmost digits from the year field to incorporate into key.
  984. See `bibtex-generate-autokey' for details."
  985. :group 'bibtex-autokey
  986. :type 'integer)
  987. (defcustom bibtex-autokey-use-crossref t
  988. "If non-nil use fields from crossreferenced entry if necessary.
  989. If this variable is non-nil and some field has no entry, but a
  990. valid crossref entry, the field from the crossreferenced entry is used.
  991. See `bibtex-generate-autokey' for details."
  992. :group 'bibtex-autokey
  993. :type 'boolean)
  994. (defcustom bibtex-autokey-titlewords 5
  995. "Number of title words to use for the automatically generated reference key.
  996. If this is set to anything but a number, all title words are used.
  997. Possibly more words from the title are used according to
  998. `bibtex-autokey-titlewords-stretch'.
  999. See `bibtex-generate-autokey' for details."
  1000. :group 'bibtex-autokey
  1001. :type '(choice (const :tag "All" infty)
  1002. integer))
  1003. (defcustom bibtex-autokey-title-terminators "[.!?:;]\\|--"
  1004. "Regexp defining the termination of the main part of the title.
  1005. Case of the regexps is ignored. See `bibtex-generate-autokey' for details."
  1006. :group 'bibtex-autokey
  1007. :type 'regexp)
  1008. (defcustom bibtex-autokey-titlewords-stretch 2
  1009. "Number of words that can additionally be used from the title.
  1010. These words are used only, if a sentence from the title can be ended then.
  1011. See `bibtex-generate-autokey' for details."
  1012. :group 'bibtex-autokey
  1013. :type 'integer)
  1014. (defcustom bibtex-autokey-titleword-ignore
  1015. '("A" "An" "On" "The" "Eine?" "Der" "Die" "Das"
  1016. "[^[:upper:]].*" ".*[^[:upper:][:lower:]0-9].*")
  1017. "Determines words from the title that are not to be used in the key.
  1018. Each item of the list is a regexp. If a word of the title matches a
  1019. regexp from that list, it is not included in the title part of the key.
  1020. Case is significant. See `bibtex-generate-autokey' for details."
  1021. :group 'bibtex-autokey
  1022. :type '(repeat regexp))
  1023. (defcustom bibtex-autokey-titleword-case-convert-function 'downcase
  1024. "Function called for each titleword to perform case conversion.
  1025. See `bibtex-generate-autokey' for details."
  1026. :group 'bibtex-autokey
  1027. :type '(choice (const :tag "Preserve case" identity)
  1028. (const :tag "Downcase" downcase)
  1029. (const :tag "Capitalize" capitalize)
  1030. (const :tag "Upcase" upcase)
  1031. (function :tag "Conversion function")))
  1032. (defvaralias 'bibtex-autokey-titleword-case-convert
  1033. 'bibtex-autokey-titleword-case-convert-function)
  1034. (defcustom bibtex-autokey-titleword-abbrevs nil
  1035. "Determines exceptions to the usual abbreviation mechanism.
  1036. An alist of (OLD-REGEXP . NEW-STRING) pairs. Case is ignored
  1037. in matching against OLD-REGEXP, and the first matching pair is used.
  1038. See `bibtex-generate-autokey' for details."
  1039. :group 'bibtex-autokey
  1040. :type '(repeat (cons (regexp :tag "Old")
  1041. (string :tag "New"))))
  1042. (defcustom bibtex-autokey-titleword-change-strings
  1043. bibtex-autokey-transcriptions
  1044. "Alist of (OLD-REGEXP . NEW-STRING) pairs.
  1045. Any part of title word matching a OLD-REGEXP is replaced by NEW-STRING.
  1046. Case is significant in OLD-REGEXP. All regexps are tried in the
  1047. order in which they appear in the list.
  1048. See `bibtex-generate-autokey' for details."
  1049. :group 'bibtex-autokey
  1050. :type '(repeat (cons (regexp :tag "Old")
  1051. (string :tag "New"))))
  1052. (defcustom bibtex-autokey-titleword-length 5
  1053. "Number of characters from title words to incorporate into key.
  1054. If this is set to anything but a number, all characters are used.
  1055. See `bibtex-generate-autokey' for details."
  1056. :group 'bibtex-autokey
  1057. :type '(choice (const :tag "All" infty)
  1058. integer))
  1059. (defcustom bibtex-autokey-titleword-separator "_"
  1060. "String to be put between the title words.
  1061. See `bibtex-generate-autokey' for details."
  1062. :group 'bibtex-autokey
  1063. :type 'string)
  1064. (defcustom bibtex-autokey-name-year-separator ""
  1065. "String to be put between name part and year part of key.
  1066. See `bibtex-generate-autokey' for details."
  1067. :group 'bibtex-autokey
  1068. :type 'string)
  1069. (defcustom bibtex-autokey-year-title-separator ":_"
  1070. "String to be put between year part and title part of key.
  1071. See `bibtex-generate-autokey' for details."
  1072. :group 'bibtex-autokey
  1073. :type 'string)
  1074. (defcustom bibtex-autokey-edit-before-use t
  1075. "If non-nil, user is allowed to edit the generated key before it is used."
  1076. :group 'bibtex-autokey
  1077. :type 'boolean)
  1078. (defcustom bibtex-autokey-before-presentation-function nil
  1079. "If non-nil, function to call before generated key is presented.
  1080. The function must take one argument (the automatically generated key),
  1081. and must return a string (the key to use)."
  1082. :group 'bibtex-autokey
  1083. :type '(choice (const nil) function))
  1084. (defcustom bibtex-entry-offset 0
  1085. "Offset for BibTeX entries.
  1086. Added to the value of all other variables which determine columns."
  1087. :group 'bibtex
  1088. :type 'integer)
  1089. (defcustom bibtex-field-indentation 2
  1090. "Starting column for the name part in BibTeX fields."
  1091. :group 'bibtex
  1092. :type 'integer)
  1093. (defcustom bibtex-text-indentation
  1094. (+ bibtex-field-indentation
  1095. (length "organization = "))
  1096. "Starting column for the text part in BibTeX fields.
  1097. Should be equal to the space needed for the longest name part."
  1098. :group 'bibtex
  1099. :type 'integer)
  1100. (defcustom bibtex-contline-indentation
  1101. (+ bibtex-text-indentation 1)
  1102. "Starting column for continuation lines of BibTeX fields."
  1103. :group 'bibtex
  1104. :type 'integer)
  1105. (defcustom bibtex-align-at-equal-sign nil
  1106. "If non-nil, align fields at equal sign instead of field text.
  1107. If non-nil, the column for the equal sign is the value of
  1108. `bibtex-text-indentation', minus 2."
  1109. :group 'bibtex
  1110. :type 'boolean)
  1111. (defcustom bibtex-comma-after-last-field nil
  1112. "If non-nil, a comma is put at end of last field in the entry template."
  1113. :group 'bibtex
  1114. :type 'boolean)
  1115. (defcustom bibtex-autoadd-commas t
  1116. "If non-nil automatically add missing commas at end of BibTeX fields."
  1117. :group 'bibtex
  1118. :type 'boolean)
  1119. (defcustom bibtex-autofill-types '("Proceedings")
  1120. "Automatically fill fields if possible for those BibTeX entry types."
  1121. :group 'bibtex
  1122. :type '(repeat string))
  1123. (defcustom bibtex-summary-function 'bibtex-summary
  1124. "Function to call for generating a summary of current BibTeX entry.
  1125. It takes no arguments. Point must be at beginning of entry.
  1126. Used by `bibtex-complete-crossref-cleanup' and `bibtex-copy-summary-as-kill'."
  1127. :group 'bibtex
  1128. :type '(choice (const :tag "Default" bibtex-summary)
  1129. (function :tag "Personalized function")))
  1130. (defcustom bibtex-generate-url-list
  1131. '((("url" . ".*:.*")))
  1132. "List of schemes for generating the URL of a BibTeX entry.
  1133. These schemes are used by `bibtex-url'.
  1134. Each scheme should have one of these forms:
  1135. ((FIELD . REGEXP))
  1136. ((FIELD . REGEXP) STEP...)
  1137. ((FIELD . REGEXP) STRING STEP...)
  1138. FIELD is a field name as returned by `bibtex-parse-entry'.
  1139. REGEXP is matched against the text of FIELD. If the match succeeds,
  1140. then this scheme is used. If no STRING and STEPs are specified
  1141. the matched text is used as the URL, otherwise the URL is built
  1142. by evaluating STEPs. If no STRING is specified the STEPs must result
  1143. in strings which are concatenated. Otherwise the resulting objects
  1144. are passed through `format' using STRING as format control string.
  1145. A STEP is a list (FIELD REGEXP REPLACE). The text of FIELD
  1146. is matched against REGEXP, and is replaced with REPLACE.
  1147. REPLACE can be a string, or a number (which selects the corresponding
  1148. submatch), or a function called with the field's text as argument
  1149. and with the `match-data' properly set.
  1150. Case is always ignored. Always remove the field delimiters.
  1151. If `bibtex-expand-strings' is non-nil, BibTeX strings are expanded
  1152. for generating the URL.
  1153. Set this variable before loading BibTeX mode.
  1154. The following is a complex example, see URL `http://link.aps.org/'.
  1155. (((\"journal\" . \"\\\\=<\\(PR[ABCDEL]?\\|RMP\\)\\\\=>\")
  1156. \"http://link.aps.org/abstract/%s/v%s/p%s\"
  1157. (\"journal\" \".*\" upcase)
  1158. (\"volume\" \".*\" 0)
  1159. (\"pages\" \"\\`[A-Z]?[0-9]+\" 0)))"
  1160. :group 'bibtex
  1161. :type '(repeat
  1162. (cons :tag "Scheme"
  1163. (cons :tag "Matcher" :extra-offset 4
  1164. (string :tag "BibTeX field")
  1165. (regexp :tag "Regexp"))
  1166. (choice
  1167. (const :tag "Take match as is" nil)
  1168. (cons :tag "Formatted"
  1169. (string :tag "Format control string")
  1170. (repeat :tag "Steps to generate URL"
  1171. (list (string :tag "BibTeX field")
  1172. (regexp :tag "Regexp")
  1173. (choice (string :tag "Replacement")
  1174. (integer :tag "Sub-match")
  1175. (function :tag "Filter")))))
  1176. (repeat :tag "Concatenated"
  1177. (list (string :tag "BibTeX field")
  1178. (regexp :tag "Regexp")
  1179. (choice (string :tag "Replacement")
  1180. (integer :tag "Sub-match")
  1181. (function :tag "Filter"))))))))
  1182. (put 'bibtex-generate-url-list 'risky-local-variable t)
  1183. (defcustom bibtex-cite-matcher-alist
  1184. '(("\\\\cite[ \t\n]*{\\([^}]+\\)}" . 1))
  1185. "Alist of rules to identify cited keys in a BibTeX entry.
  1186. Each rule should be of the form (REGEXP . SUBEXP), where SUBEXP
  1187. specifies which parenthesized expression in REGEXP is a cited key.
  1188. Case is significant.
  1189. Used by `bibtex-search-crossref' and for font-locking.
  1190. Set this variable before loading BibTeX mode."
  1191. :group 'bibtex
  1192. :type '(repeat (cons (regexp :tag "Regexp")
  1193. (integer :tag "Number")))
  1194. :version "23.1")
  1195. (defcustom bibtex-expand-strings nil
  1196. "If non-nil, expand strings when extracting the content of a BibTeX field."
  1197. :group 'bibtex
  1198. :type 'boolean)
  1199. (defcustom bibtex-search-buffer "*BibTeX Search*"
  1200. "Buffer for BibTeX search results."
  1201. :group 'bibtex
  1202. :version "24.1"
  1203. :type 'string)
  1204. ;; `bibtex-font-lock-keywords' is a user option, too. But since the
  1205. ;; patterns used to define this variable are defined in a later
  1206. ;; section of this file, it is defined later.
  1207. ;; Syntax Table and Keybindings
  1208. (defvar bibtex-mode-syntax-table
  1209. (let ((st (make-syntax-table)))
  1210. (modify-syntax-entry ?\" "\"" st)
  1211. (modify-syntax-entry ?$ "$$ " st)
  1212. (modify-syntax-entry ?% "< " st)
  1213. (modify-syntax-entry ?' "w " st) ;FIXME: Not allowed in @string keys.
  1214. (modify-syntax-entry ?@ "w " st)
  1215. (modify-syntax-entry ?\\ "\\" st)
  1216. (modify-syntax-entry ?\f "> " st)
  1217. (modify-syntax-entry ?\n "> " st)
  1218. ;; Keys cannot have = in them (wrong font-lock of @string{foo=bar}).
  1219. (modify-syntax-entry ?= "." st)
  1220. (modify-syntax-entry ?~ " " st)
  1221. st)
  1222. "Syntax table used in BibTeX mode buffers.")
  1223. (defvar bibtex-mode-map
  1224. (let ((km (make-sparse-keymap)))
  1225. ;; The Key `C-c&' is reserved for reftex.el
  1226. (define-key km "\t" 'bibtex-find-text)
  1227. (define-key km "\n" 'bibtex-next-field)
  1228. (define-key km "\M-\t" 'completion-at-point)
  1229. (define-key km "\C-c\"" 'bibtex-remove-delimiters)
  1230. (define-key km "\C-c{" 'bibtex-remove-delimiters)
  1231. (define-key km "\C-c}" 'bibtex-remove-delimiters)
  1232. (define-key km "\C-c\C-c" 'bibtex-clean-entry)
  1233. (define-key km "\C-c\C-q" 'bibtex-fill-entry)
  1234. (define-key km "\C-c\C-s" 'bibtex-search-entry)
  1235. (define-key km "\C-c\C-x" 'bibtex-search-crossref)
  1236. (define-key km "\C-c\C-t" 'bibtex-copy-summary-as-kill)
  1237. (define-key km "\C-c?" 'bibtex-print-help-message)
  1238. (define-key km "\C-c\C-p" 'bibtex-pop-previous)
  1239. (define-key km "\C-c\C-n" 'bibtex-pop-next)
  1240. (define-key km "\C-c\C-k" 'bibtex-kill-field)
  1241. (define-key km "\C-c\M-k" 'bibtex-copy-field-as-kill)
  1242. (define-key km "\C-c\C-w" 'bibtex-kill-entry)
  1243. (define-key km "\C-c\M-w" 'bibtex-copy-entry-as-kill)
  1244. (define-key km "\C-c\C-y" 'bibtex-yank)
  1245. (define-key km "\C-c\M-y" 'bibtex-yank-pop)
  1246. (define-key km "\C-c\C-d" 'bibtex-empty-field)
  1247. (define-key km "\C-c\C-f" 'bibtex-make-field)
  1248. (define-key km "\C-c\C-u" 'bibtex-entry-update)
  1249. (define-key km "\C-c$" 'bibtex-ispell-abstract)
  1250. (define-key km "\M-\C-a" 'bibtex-beginning-of-entry)
  1251. (define-key km "\M-\C-e" 'bibtex-end-of-entry)
  1252. (define-key km "\C-\M-l" 'bibtex-reposition-window)
  1253. (define-key km "\C-\M-h" 'bibtex-mark-entry)
  1254. (define-key km "\C-c\C-b" 'bibtex-entry)
  1255. (define-key km "\C-c\C-rn" 'bibtex-narrow-to-entry)
  1256. (define-key km "\C-c\C-rw" 'widen)
  1257. (define-key km "\C-c\C-l" 'bibtex-url)
  1258. (define-key km "\C-c\C-a" 'bibtex-search-entries)
  1259. (define-key km "\C-c\C-o" 'bibtex-remove-OPT-or-ALT)
  1260. (define-key km "\C-c\C-e\C-i" 'bibtex-InProceedings)
  1261. (define-key km "\C-c\C-ei" 'bibtex-InCollection)
  1262. (define-key km "\C-c\C-eI" 'bibtex-InBook)
  1263. (define-key km "\C-c\C-e\C-a" 'bibtex-Article)
  1264. (define-key km "\C-c\C-e\C-b" 'bibtex-InBook)
  1265. (define-key km "\C-c\C-eb" 'bibtex-Book)
  1266. (define-key km "\C-c\C-eB" 'bibtex-Booklet)
  1267. (define-key km "\C-c\C-e\C-c" 'bibtex-InCollection)
  1268. (define-key km "\C-c\C-e\C-m" 'bibtex-Manual)
  1269. (define-key km "\C-c\C-em" 'bibtex-MastersThesis)
  1270. (define-key km "\C-c\C-eM" 'bibtex-Misc)
  1271. (define-key km "\C-c\C-e\C-p" 'bibtex-InProceedings)
  1272. (define-key km "\C-c\C-ep" 'bibtex-Proceedings)
  1273. (define-key km "\C-c\C-eP" 'bibtex-PhdThesis)
  1274. (define-key km "\C-c\C-e\M-p" 'bibtex-Preamble)
  1275. (define-key km "\C-c\C-e\C-s" 'bibtex-String)
  1276. (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport)
  1277. (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished)
  1278. km)
  1279. "Keymap used in BibTeX mode.")
  1280. (easy-menu-define
  1281. bibtex-edit-menu bibtex-mode-map "BibTeX-Edit Menu in BibTeX mode"
  1282. '("BibTeX-Edit"
  1283. ("Moving inside an Entry"
  1284. ["End of Field" bibtex-find-text t]
  1285. ["Next Field" bibtex-next-field t]
  1286. ["Beginning of Entry" bibtex-beginning-of-entry t]
  1287. ["End of Entry" bibtex-end-of-entry t]
  1288. "--"
  1289. ["Make Entry Visible" bibtex-reposition-window t])
  1290. ("Moving in BibTeX Buffers"
  1291. ["Search Entry" bibtex-search-entry t]
  1292. ["Search Crossref Entry" bibtex-search-crossref t])
  1293. "--"
  1294. ("Operating on Current Field"
  1295. ["Fill Field" fill-paragraph t]
  1296. ["Remove Delimiters" bibtex-remove-delimiters t]
  1297. ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t]
  1298. ["Clear Field" bibtex-empty-field t]
  1299. "--"
  1300. ["Kill Field" bibtex-kill-field t]
  1301. ["Copy Field to Kill Ring" bibtex-copy-field-as-kill t]
  1302. ["Paste Most Recently Killed Field" bibtex-yank t]
  1303. ["Paste Previously Killed Field" bibtex-yank-pop t]
  1304. "--"
  1305. ["Make New Field" bibtex-make-field t]
  1306. "--"
  1307. ["Snatch from Similar Following Field" bibtex-pop-next t]
  1308. ["Snatch from Similar Preceding Field" bibtex-pop-previous t]
  1309. "--"
  1310. ["String or Key Complete" bibtex-complete t]
  1311. "--"
  1312. ["Help about Current Field" bibtex-print-help-message t])
  1313. ("Operating on Current Entry"
  1314. ["Fill Entry" bibtex-fill-entry t]
  1315. ["Clean Entry" bibtex-clean-entry t]
  1316. ["Update Entry" bibtex-entry-update t]
  1317. "--"
  1318. ["Kill Entry" bibtex-kill-entry t]
  1319. ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t]
  1320. ["Paste Most Recently Killed Entry" bibtex-yank t]
  1321. ["Paste Previously Killed Entry" bibtex-yank-pop t]
  1322. "--"
  1323. ["Copy Summary to Kill Ring" bibtex-copy-summary-as-kill t]
  1324. ["Browse URL" bibtex-url t]
  1325. "--"
  1326. ["Ispell Entry" bibtex-ispell-entry t]
  1327. ["Ispell Entry Abstract" bibtex-ispell-abstract t]
  1328. "--"
  1329. ["Narrow to Entry" bibtex-narrow-to-entry t]
  1330. ["Mark Entry" bibtex-mark-entry t]
  1331. "--"
  1332. ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex
  1333. (fboundp 'reftex-view-crossref-from-bibtex)])
  1334. ("Operating on Buffer or Region"
  1335. ["Search Entries" bibtex-search-entries t]
  1336. "--"
  1337. ["Validate Entries" bibtex-validate t]
  1338. ["Sort Entries" bibtex-sort-buffer t]
  1339. ["Reformat Entries" bibtex-reformat t]
  1340. ["Count Entries" bibtex-count-entries t]
  1341. "--"
  1342. ["Convert Alien Buffer" bibtex-convert-alien t])
  1343. ("Operating on Multiple Buffers"
  1344. ["(Re)Initialize BibTeX Buffers" bibtex-initialize t]
  1345. ["Validate Entries" bibtex-validate-globally t])))
  1346. ;; Internal Variables
  1347. (defvar bibtex-entry-alist nil
  1348. "Alist of currently active entry types.
  1349. Initialized by `bibtex-set-dialect'.")
  1350. (defvar bibtex-field-alist nil
  1351. "Alist of currently active field types.
  1352. Initialized by `bibtex-set-dialect'.")
  1353. (defvar bibtex-field-braces-opt nil
  1354. "Optimized value of `bibtex-field-braces-alist'.
  1355. Created by `bibtex-field-re-init'.
  1356. It is an alist with elements (FIELD . REGEXP).")
  1357. (defvar bibtex-field-strings-opt nil
  1358. "Optimized value of `bibtex-field-strings-alist'.
  1359. Created by `bibtex-field-re-init'.
  1360. It is an alist with elements (FIELD RULE1 RULE2 ...),
  1361. where each RULE is (REGEXP . TO-STR).")
  1362. (defvar bibtex-pop-previous-search-point nil
  1363. "Next point where `bibtex-pop-previous' starts looking for a similar entry.")
  1364. (defvar bibtex-pop-next-search-point nil
  1365. "Next point where `bibtex-pop-next' starts looking for a similar entry.")
  1366. (defvar bibtex-field-kill-ring nil
  1367. "Ring of least recently killed fields.
  1368. At most `bibtex-field-kill-ring-max' items are kept here.")
  1369. (defvar bibtex-field-kill-ring-yank-pointer nil
  1370. "The tail of `bibtex-field-kill-ring' whose car is the last item yanked.")
  1371. (defvar bibtex-entry-kill-ring nil
  1372. "Ring of least recently killed entries.
  1373. At most `bibtex-entry-kill-ring-max' items are kept here.")
  1374. (defvar bibtex-entry-kill-ring-yank-pointer nil
  1375. "The tail of `bibtex-entry-kill-ring' whose car is the last item yanked.")
  1376. (defvar bibtex-last-kill-command nil
  1377. "Type of the last kill command (either 'field or 'entry).")
  1378. (defvar bibtex-strings
  1379. (lazy-completion-table bibtex-strings
  1380. (lambda ()
  1381. (bibtex-parse-strings (bibtex-string-files-init))))
  1382. "Completion table for BibTeX string keys.
  1383. Initialized from `bibtex-predefined-strings' and `bibtex-string-files'.")
  1384. (make-variable-buffer-local 'bibtex-strings)
  1385. (put 'bibtex-strings 'risky-local-variable t)
  1386. (defvar bibtex-reference-keys
  1387. (lazy-completion-table bibtex-reference-keys
  1388. (lambda () (bibtex-parse-keys nil t)))
  1389. "Completion table for BibTeX reference keys.
  1390. The CDRs of the elements are t for header keys and nil for crossref keys.")
  1391. (make-variable-buffer-local 'bibtex-reference-keys)
  1392. (put 'bibtex-reference-keys 'risky-local-variable t)
  1393. (defvar bibtex-buffer-last-parsed-tick nil
  1394. "Value of `buffer-modified-tick' last time buffer was parsed for keys.")
  1395. (defvar bibtex-parse-idle-timer nil
  1396. "Stores if timer is already installed.")
  1397. (defvar bibtex-progress-lastperc nil
  1398. "Last reported percentage for the progress message.")
  1399. (defvar bibtex-progress-lastmes nil
  1400. "Last reported progress message.")
  1401. (defvar bibtex-progress-interval nil
  1402. "Interval for progress messages.")
  1403. (defvar bibtex-key-history nil
  1404. "History list for reading keys.")
  1405. (defvar bibtex-entry-type-history nil
  1406. "History list for reading entry types.")
  1407. (defvar bibtex-field-history nil
  1408. "History list for reading field names.")
  1409. (defvar bibtex-reformat-previous-options nil
  1410. "Last reformat options given.")
  1411. (defvar bibtex-reformat-previous-reference-keys nil
  1412. "Last reformat reference keys option given.")
  1413. (defconst bibtex-field-name "[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*"
  1414. "Regexp matching the name of a BibTeX field.")
  1415. (defconst bibtex-name-part
  1416. (concat ",[ \t\n]*\\(" bibtex-field-name "\\)")
  1417. "Regexp matching the name part of a BibTeX field.")
  1418. (defconst bibtex-reference-key "[][[:alnum:].:;?!`'/*@+|()<>&_^$-]+"
  1419. "Regexp matching the reference key part of a BibTeX entry.")
  1420. (defconst bibtex-field-const "[][[:alnum:].:;?!`'/*@+=|<>&_^$-]+"
  1421. "Regexp matching a BibTeX field constant.")
  1422. (defvar bibtex-entry-type nil
  1423. "Regexp matching the type of a BibTeX entry.
  1424. Initialized by `bibtex-set-dialect'.")
  1425. (defvar bibtex-entry-head nil
  1426. "Regexp matching the header line of a BibTeX entry (including key).
  1427. Initialized by `bibtex-set-dialect'.")
  1428. (defvar bibtex-entry-maybe-empty-head nil
  1429. "Regexp matching the header line of a BibTeX entry (possibly without key).
  1430. Initialized by `bibtex-set-dialect'.")
  1431. (defconst bibtex-any-entry-maybe-empty-head
  1432. (concat "^[ \t]*\\(@[ \t]*" bibtex-field-name "\\)[ \t]*[({][ \t\n]*\\("
  1433. bibtex-reference-key "\\)?")
  1434. "Regexp matching the header line of any BibTeX entry (possibly without key).")
  1435. (defvar bibtex-any-valid-entry-type nil
  1436. "Regexp matching any valid BibTeX entry (including String and Preamble).
  1437. Initialized by `bibtex-set-dialect'.")
  1438. (defconst bibtex-type-in-head 1
  1439. "Regexp subexpression number of the type part in `bibtex-entry-head'.")
  1440. (defconst bibtex-key-in-head 2
  1441. "Regexp subexpression number of the key part in `bibtex-entry-head'.")
  1442. (defconst bibtex-string-type "^[ \t]*\\(@[ \t]*String\\)[ \t]*[({][ \t\n]*"
  1443. "Regexp matching the name of a BibTeX String entry.")
  1444. (defconst bibtex-string-maybe-empty-head
  1445. (concat bibtex-string-type "\\(" bibtex-reference-key "\\)?")
  1446. "Regexp matching the header line of a BibTeX String entry.")
  1447. (defconst bibtex-preamble-prefix
  1448. "[ \t]*\\(@[ \t]*Preamble\\)[ \t]*[({][ \t\n]*"
  1449. "Regexp matching the prefix part of a BibTeX Preamble entry.")
  1450. (defconst bibtex-font-lock-syntactic-keywords
  1451. `((,(concat "^[ \t]*\\(" (substring bibtex-comment-start 0 1) "\\)"
  1452. (substring bibtex-comment-start 1) "\\>")
  1453. 1 '(11))))
  1454. (defvar bibtex-font-lock-keywords
  1455. ;; entry type and reference key
  1456. `((,bibtex-any-entry-maybe-empty-head
  1457. (,bibtex-type-in-head font-lock-function-name-face)
  1458. (,bibtex-key-in-head font-lock-constant-face nil t))
  1459. ;; optional field names (treated as comments)
  1460. (,(concat "^[ \t]*\\(OPT" bibtex-field-name "\\)[ \t]*=")
  1461. 1 font-lock-comment-face)
  1462. ;; field names
  1463. (,(concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=")
  1464. 1 font-lock-variable-name-face)
  1465. ;; url
  1466. (bibtex-font-lock-url) (bibtex-font-lock-crossref)
  1467. ;; cite
  1468. ,@(mapcar (lambda (matcher)
  1469. `((lambda (bound) (bibtex-font-lock-cite ',matcher bound))))
  1470. bibtex-cite-matcher-alist))
  1471. "*Default expressions to highlight in BibTeX mode.")
  1472. (defvar bibtex-font-lock-url-regexp
  1473. ;; Assume that field names begin at the beginning of a line.
  1474. (concat "^[ \t]*"
  1475. (regexp-opt (delete-dups (mapcar 'caar bibtex-generate-url-list)) t)
  1476. "[ \t]*=[ \t]*")
  1477. "Regexp for `bibtex-font-lock-url' derived from `bibtex-generate-url-list'.")
  1478. (defvar bibtex-string-empty-key nil
  1479. "If non-nil, `bibtex-parse-string' accepts empty key.")
  1480. (defvar bibtex-sort-entry-class-alist nil
  1481. "Alist mapping entry types to their sorting index.
  1482. Auto-generated from `bibtex-sort-entry-class'.
  1483. Used when `bibtex-maintain-sorted-entries' is `entry-class'.")
  1484. (defun bibtex-parse-association (parse-lhs parse-rhs)
  1485. "Parse a string of the format <left-hand-side = right-hand-side>.
  1486. The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding
  1487. substrings. These functions are expected to return nil if parsing is not
  1488. successful. If the returned values of both functions are non-nil,
  1489. return a cons pair of these values. Do not move point."
  1490. (save-match-data
  1491. (save-excursion
  1492. (let ((left (funcall parse-lhs))
  1493. right)
  1494. (if (and left
  1495. (looking-at "[ \t\n]*=[ \t\n]*")
  1496. (goto-char (match-end 0))
  1497. (setq right (funcall parse-rhs)))
  1498. (cons left right))))))
  1499. (defun bibtex-parse-field-name ()
  1500. "Parse the name part of a BibTeX field.
  1501. If the field name is found, return a triple consisting of the position of the
  1502. very first character of the match, the actual starting position of the name
  1503. part and end position of the match. Move point to end of field name.
  1504. If `bibtex-autoadd-commas' is non-nil add missing comma at end of preceding
  1505. BibTeX field as necessary."
  1506. (cond ((looking-at bibtex-name-part)
  1507. (goto-char (match-end 0))
  1508. (list (match-beginning 0) (match-beginning 1) (match-end 0)))
  1509. ;; Maybe add a missing comma.
  1510. ((and bibtex-autoadd-commas
  1511. (looking-at (concat "[ \t\n]*\\(?:" bibtex-field-name
  1512. "\\)[ \t\n]*=")))
  1513. (skip-chars-backward " \t\n")
  1514. ;; It can be confusing if non-editing commands try to
  1515. ;; modify the buffer.
  1516. (if buffer-read-only
  1517. (error "Comma missing at buffer position %s" (point)))
  1518. (insert ",")
  1519. (forward-char -1)
  1520. ;; Now try again.
  1521. (bibtex-parse-field-name))))
  1522. (defconst bibtex-braced-string-syntax-table
  1523. (let ((st (make-syntax-table)))
  1524. (modify-syntax-entry ?\{ "(}" st)
  1525. (modify-syntax-entry ?\} "){" st)
  1526. (modify-syntax-entry ?\[ "." st)
  1527. (modify-syntax-entry ?\] "." st)
  1528. (modify-syntax-entry ?\( "." st)
  1529. (modify-syntax-entry ?\) "." st)
  1530. (modify-syntax-entry ?\\ "." st)
  1531. (modify-syntax-entry ?\" "." st)
  1532. st)
  1533. "Syntax-table to parse matched braces.")
  1534. (defconst bibtex-quoted-string-syntax-table
  1535. (let ((st (make-syntax-table)))
  1536. (modify-syntax-entry ?\\ "\\" st)
  1537. (modify-syntax-entry ?\" "\"" st)
  1538. st)
  1539. "Syntax-table to parse matched quotes.")
  1540. (defun bibtex-parse-field-string ()
  1541. "Parse a BibTeX field string enclosed by braces or quotes.
  1542. If a syntactically correct string is found, a pair containing the start and
  1543. end position of the field string is returned, nil otherwise.
  1544. Do not move point."
  1545. (let ((end-point
  1546. (or (and (eq (following-char) ?\")
  1547. (save-excursion
  1548. (with-syntax-table bibtex-quoted-string-syntax-table
  1549. (forward-sexp 1))
  1550. (point)))
  1551. (and (eq (following-char) ?\{)
  1552. (save-excursion
  1553. (with-syntax-table bibtex-braced-string-syntax-table
  1554. (forward-sexp 1))
  1555. (point))))))
  1556. (if end-point
  1557. (cons (point) end-point))))
  1558. (defun bibtex-parse-field-text ()
  1559. "Parse the text part of a BibTeX field.
  1560. The text part is either a string, or an empty string, or a constant followed
  1561. by one or more <# (string|constant)> pairs. If a syntactically correct text
  1562. is found, a pair containing the start and end position of the text is
  1563. returned, nil otherwise. Move point to end of field text."
  1564. (let ((starting-point (point))
  1565. end-point failure boundaries)
  1566. (while (not (or end-point failure))
  1567. (cond ((looking-at bibtex-field-const)
  1568. (goto-char (match-end 0)))
  1569. ((setq boundaries (bibtex-parse-field-string))
  1570. (goto-char (cdr boundaries)))
  1571. ((setq failure t)))
  1572. (if (looking-at "[ \t\n]*#[ \t\n]*")
  1573. (goto-char (match-end 0))
  1574. (setq end-point (point))))
  1575. (skip-chars-forward " \t\n")
  1576. (if (and (not failure)
  1577. end-point)
  1578. (list starting-point end-point (point)))))
  1579. (defun bibtex-parse-field ()
  1580. "Parse the BibTeX field beginning at the position of point.
  1581. If a syntactically correct field is found, return a cons pair containing
  1582. the boundaries of the name and text parts of the field. Do not move point."
  1583. (bibtex-parse-association 'bibtex-parse-field-name
  1584. 'bibtex-parse-field-text))
  1585. (defsubst bibtex-start-of-field (bounds)
  1586. (nth 0 (car bounds)))
  1587. (defsubst bibtex-start-of-name-in-field (bounds)
  1588. (nth 1 (car bounds)))
  1589. (defsubst bibtex-end-of-name-in-field (bounds)
  1590. (nth 2 (car bounds)))
  1591. (defsubst bibtex-start-of-text-in-field (bounds)
  1592. (nth 1 bounds))
  1593. (defsubst bibtex-end-of-text-in-field (bounds)
  1594. (nth 2 bounds))
  1595. (defsubst bibtex-end-of-field (bounds)
  1596. (nth 3 bounds))
  1597. (defun bibtex-search-forward-field (name &optional bound)
  1598. "Search forward to find a BibTeX field of name NAME.
  1599. If a syntactically correct field is found, return a pair containing
  1600. the boundaries of the name and text parts of the field. The search
  1601. is limited by optional arg BOUND. If BOUND is t the search is limited
  1602. by the end of the current entry. Do not move point."
  1603. (save-match-data
  1604. (save-excursion
  1605. (if (eq bound t)
  1606. (let ((regexp (concat bibtex-name-part "[ \t\n]*=\\|"
  1607. bibtex-any-entry-maybe-empty-head))
  1608. (case-fold-search t) bounds)
  1609. (catch 'done
  1610. (if (looking-at "[ \t]*@") (goto-char (match-end 0)))
  1611. (while (and (not bounds)
  1612. (re-search-forward regexp nil t))
  1613. (if (match-beginning 2)
  1614. ;; We found a new entry
  1615. (throw 'done nil)
  1616. ;; We found a field
  1617. (goto-char (match-beginning 0))
  1618. (setq bounds (bibtex-parse-field))))
  1619. ;; Step through all fields so that we cannot overshoot.
  1620. (while bounds
  1621. (goto-char (bibtex-start-of-name-in-field bounds))
  1622. (if (looking-at name) (throw 'done bounds))
  1623. (goto-char (bibtex-end-of-field bounds))
  1624. (setq bounds (bibtex-parse-field)))))
  1625. ;; Bounded search or bound is nil (i.e. we cannot overshoot).
  1626. ;; Indeed, the search is bounded when `bibtex-search-forward-field'
  1627. ;; is called many times. So we optimize this part of this function.
  1628. (let ((name-part (concat ",[ \t\n]*\\(" name "\\)[ \t\n]*=[ \t\n]*"))
  1629. (case-fold-search t) left right)
  1630. (while (and (not right)
  1631. (re-search-forward name-part bound t))
  1632. (setq left (list (match-beginning 0) (match-beginning 1)
  1633. (match-end 1))
  1634. ;; Don't worry that the field text could be past bound.
  1635. right (bibtex-parse-field-text)))
  1636. (if right (cons left right)))))))
  1637. (defun bibtex-search-backward-field (name &optional bound)
  1638. "Search backward to find a BibTeX field of name NAME.
  1639. If a syntactically correct field is found, return a pair containing
  1640. the boundaries of the name and text parts of the field. The search
  1641. is limited by the optional arg BOUND. If BOUND is t the search is
  1642. limited by the beginning of the current entry. Do not move point."
  1643. (save-match-data
  1644. (if (eq bound t)
  1645. (setq bound (save-excursion (bibtex-beginning-of-entry))))
  1646. (let ((name-part (concat ",[ \t\n]*\\(" name "\\)[ \t\n]*=[ \t\n]*"))
  1647. (case-fold-search t) left right)
  1648. (save-excursion
  1649. ;; the parsing functions are not designed for parsing backwards :-(
  1650. (when (search-backward "," bound t)
  1651. (or (save-excursion
  1652. (when (looking-at name-part)
  1653. (setq left (list (match-beginning 0) (match-beginning 1)
  1654. (match-end 1)))
  1655. (goto-char (match-end 0))
  1656. (setq right (bibtex-parse-field-text))))
  1657. (while (and (not right)
  1658. (re-search-backward name-part bound t))
  1659. (setq left (list (match-beginning 0) (match-beginning 1)
  1660. (match-end 1)))
  1661. (save-excursion
  1662. (goto-char (match-end 0))
  1663. (setq right (bibtex-parse-field-text)))))
  1664. (if right (cons left right)))))))
  1665. (defun bibtex-name-in-field (bounds &optional remove-opt-alt)
  1666. "Get content of name in BibTeX field defined via BOUNDS.
  1667. If optional arg REMOVE-OPT-ALT is non-nil remove \"OPT\" and \"ALT\"."
  1668. (let ((name (buffer-substring-no-properties
  1669. (bibtex-start-of-name-in-field bounds)
  1670. (bibtex-end-of-name-in-field bounds))))
  1671. (if (and remove-opt-alt
  1672. (string-match "\\`\\(OPT\\|ALT\\)" name)
  1673. (not (and bibtex-no-opt-remove-re
  1674. (string-match bibtex-no-opt-remove-re name))))
  1675. (substring name 3)
  1676. name)))
  1677. (defun bibtex-text-in-field-bounds (bounds &optional content)
  1678. "Get text in BibTeX field defined via BOUNDS.
  1679. If optional arg CONTENT is non-nil extract content of field
  1680. by removing field delimiters and concatenating the resulting string.
  1681. If `bibtex-expand-strings' is non-nil, also expand BibTeX strings."
  1682. (if content
  1683. (save-excursion
  1684. (goto-char (bibtex-start-of-text-in-field bounds))
  1685. (let ((epoint (bibtex-end-of-text-in-field bounds))
  1686. content)
  1687. (while (< (point) epoint)
  1688. (if (looking-at bibtex-field-const)
  1689. (let ((mtch (match-string-no-properties 0)))
  1690. (push (or (if bibtex-expand-strings
  1691. (cdr (assoc-string mtch (bibtex-strings) t)))
  1692. mtch) content)
  1693. (goto-char (match-end 0)))
  1694. (let ((bounds (bibtex-parse-field-string)))
  1695. (push (buffer-substring-no-properties
  1696. (1+ (car bounds)) (1- (cdr bounds))) content)
  1697. (goto-char (cdr bounds))))
  1698. (re-search-forward "\\=[ \t\n]*#[ \t\n]*" nil t))
  1699. (apply 'concat (nreverse content))))
  1700. (buffer-substring-no-properties (bibtex-start-of-text-in-field bounds)
  1701. (bibtex-end-of-text-in-field bounds))))
  1702. (defun bibtex-text-in-field (field &optional follow-crossref)
  1703. "Get content of field FIELD of current BibTeX entry.
  1704. Return nil if not found.
  1705. If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."
  1706. (save-excursion
  1707. (let* ((end (if follow-crossref (bibtex-end-of-entry) t))
  1708. (beg (bibtex-beginning-of-entry)) ; move point
  1709. (bounds (bibtex-search-forward-field field end)))
  1710. (cond (bounds (bibtex-text-in-field-bounds bounds t))
  1711. ((and follow-crossref
  1712. (progn (goto-char beg)
  1713. (setq bounds (bibtex-search-forward-field
  1714. "\\(OPT\\)?crossref" end))))
  1715. (let ((crossref-field (bibtex-text-in-field-bounds bounds t)))
  1716. (if (bibtex-search-crossref crossref-field)
  1717. ;; Do not pass FOLLOW-CROSSREF because we want
  1718. ;; to follow crossrefs only one level of recursion.
  1719. (bibtex-text-in-field field))))))))
  1720. (defun bibtex-parse-string-prefix ()
  1721. "Parse the prefix part of a BibTeX string entry, including reference key.
  1722. If the string prefix is found, return a triple consisting of the position of
  1723. the very first character of the match, the actual starting position of the
  1724. reference key and the end position of the match.
  1725. If `bibtex-string-empty-key' is non-nil accept empty string key."
  1726. (let ((case-fold-search t))
  1727. (if (looking-at bibtex-string-type)
  1728. (let ((start (point)))
  1729. (goto-char (match-end 0))
  1730. (cond ((looking-at bibtex-reference-key)
  1731. (goto-char (match-end 0))
  1732. (list start
  1733. (match-beginning 0)
  1734. (match-end 0)))
  1735. ((and bibtex-string-empty-key
  1736. (looking-at "="))
  1737. (skip-chars-backward " \t\n")
  1738. (list start (point) (point))))))))
  1739. (defun bibtex-parse-string-postfix ()
  1740. "Parse the postfix part of a BibTeX string entry, including the text.
  1741. If the string postfix is found, return a triple consisting of the position of
  1742. the actual starting and ending position of the text and the very last
  1743. character of the string entry. Move point past BibTeX string entry."
  1744. (let* ((case-fold-search t)
  1745. (bounds (bibtex-parse-field-text)))
  1746. (when bounds
  1747. (goto-char (nth 1 bounds))
  1748. (when (looking-at "[ \t\n]*[})]")
  1749. (goto-char (match-end 0))
  1750. (list (car bounds)
  1751. (nth 1 bounds)
  1752. (match-end 0))))))
  1753. (defun bibtex-parse-string (&optional empty-key)
  1754. "Parse a BibTeX string entry beginning at the position of point.
  1755. If a syntactically correct entry is found, return a cons pair containing
  1756. the boundaries of the reference key and text parts of the entry.
  1757. If EMPTY-KEY is non-nil, key may be empty. Do not move point."
  1758. (let ((bibtex-string-empty-key empty-key))
  1759. (bibtex-parse-association 'bibtex-parse-string-prefix
  1760. 'bibtex-parse-string-postfix)))
  1761. (defun bibtex-search-forward-string (&optional empty-key)
  1762. "Search forward to find a BibTeX string entry.
  1763. If a syntactically correct entry is found, a pair containing the boundaries of
  1764. the reference key and text parts of the string is returned.
  1765. If EMPTY-KEY is non-nil, key may be empty. Do not move point."
  1766. (save-excursion
  1767. (save-match-data
  1768. (let ((case-fold-search t) bounds)
  1769. (while (and (not bounds)
  1770. (search-forward-regexp bibtex-string-type nil t))
  1771. (save-excursion (goto-char (match-beginning 0))
  1772. (setq bounds (bibtex-parse-string empty-key))))
  1773. bounds))))
  1774. (defun bibtex-reference-key-in-string (bounds)
  1775. "Return the key part of a BibTeX string defined via BOUNDS."
  1776. (buffer-substring-no-properties (nth 1 (car bounds))
  1777. (nth 2 (car bounds))))
  1778. (defun bibtex-text-in-string (bounds &optional content)
  1779. "Get text in BibTeX string field defined via BOUNDS.
  1780. If optional arg CONTENT is non-nil extract content
  1781. by removing field delimiters and concatenating the resulting string.
  1782. If `bibtex-expand-strings' is non-nil, also expand BibTeX strings."
  1783. (bibtex-text-in-field-bounds bounds content))
  1784. (defsubst bibtex-start-of-text-in-string (bounds)
  1785. (nth 0 (cdr bounds)))
  1786. (defsubst bibtex-end-of-text-in-string (bounds)
  1787. (nth 1 (cdr bounds)))
  1788. (defsubst bibtex-end-of-string (bounds)
  1789. (nth 2 (cdr bounds)))
  1790. (defsubst bibtex-type-in-head ()
  1791. "Extract BibTeX type in head."
  1792. ;; ignore @
  1793. (buffer-substring-no-properties (1+ (match-beginning bibtex-type-in-head))
  1794. (match-end bibtex-type-in-head)))
  1795. (defsubst bibtex-key-in-head (&optional empty)
  1796. "Extract BibTeX key in head. Return optional arg EMPTY if key is empty."
  1797. (or (match-string-no-properties bibtex-key-in-head)
  1798. empty))
  1799. (defun bibtex-parse-preamble ()
  1800. "Parse BibTeX preamble.
  1801. Point must be at beginning of preamble. Do not move point."
  1802. (let ((case-fold-search t))
  1803. (when (looking-at bibtex-preamble-prefix)
  1804. (let ((start (match-beginning 0)) (pref-start (match-beginning 1))
  1805. (bounds (save-excursion (goto-char (match-end 0))
  1806. (bibtex-parse-string-postfix))))
  1807. (if bounds (cons (list start pref-start) bounds))))))
  1808. ;; Helper Functions
  1809. (defsubst bibtex-string= (str1 str2)
  1810. "Return t if STR1 and STR2 are equal, ignoring case."
  1811. (eq t (compare-strings str1 0 nil str2 0 nil t)))
  1812. (defun bibtex-delete-whitespace ()
  1813. "Delete all whitespace starting at point."
  1814. (if (looking-at "[ \t\n]+")
  1815. (delete-region (point) (match-end 0))))
  1816. (defun bibtex-current-line ()
  1817. "Compute line number of point regardless whether the buffer is narrowed."
  1818. (+ (count-lines 1 (point))
  1819. (if (bolp) 1 0)))
  1820. (defun bibtex-valid-entry (&optional empty-key)
  1821. "Parse a valid BibTeX entry (maybe without key if EMPTY-KEY is t).
  1822. A valid entry is a syntactical correct one with type contained in
  1823. `bibtex-BibTeX-entry-alist'. Ignore @String and @Preamble entries.
  1824. Return a cons pair with buffer positions of beginning and end of entry
  1825. if a valid entry is found, nil otherwise. Do not move point.
  1826. After a call to this function `match-data' corresponds to the header
  1827. of the entry, see regexp `bibtex-entry-head'."
  1828. (let ((case-fold-search t) end)
  1829. (if (looking-at (if empty-key bibtex-entry-maybe-empty-head
  1830. bibtex-entry-head))
  1831. (save-excursion
  1832. (save-match-data
  1833. (goto-char (match-end 0))
  1834. (let ((entry-closer
  1835. (if (save-excursion
  1836. (goto-char (match-end bibtex-type-in-head))
  1837. (looking-at "[ \t]*("))
  1838. ",?[ \t\n]*)" ; entry opened with `('
  1839. ",?[ \t\n]*}")) ; entry opened with `{'
  1840. bounds)
  1841. (skip-chars-forward " \t\n")
  1842. ;; loop over all BibTeX fields
  1843. (while (setq bounds (bibtex-parse-field))
  1844. (goto-char (bibtex-end-of-field bounds)))
  1845. ;; This matches the infix* part.
  1846. (if (looking-at entry-closer) (setq end (match-end 0)))))
  1847. (if end (cons (match-beginning 0) end))))))
  1848. (defun bibtex-skip-to-valid-entry (&optional backward)
  1849. "Move point to beginning of the next valid BibTeX entry.
  1850. Do not move if we are already at beginning of a valid BibTeX entry.
  1851. With optional argument BACKWARD non-nil, move backward to
  1852. beginning of previous valid one. A valid entry is a syntactical correct one
  1853. with type contained in `bibtex-BibTeX-entry-alist' or, if
  1854. `bibtex-sort-ignore-string-entries' is nil, a syntactical correct string
  1855. entry. Return buffer position of beginning and end of entry if a valid
  1856. entry is found, nil otherwise."
  1857. (interactive "P")
  1858. (let ((case-fold-search t)
  1859. found bounds)
  1860. (beginning-of-line)
  1861. ;; Loop till we look at a valid entry.
  1862. (while (not (or found (if backward (bobp) (eobp))))
  1863. (cond ((setq found (or (bibtex-valid-entry)
  1864. (and (not bibtex-sort-ignore-string-entries)
  1865. (setq bounds (bibtex-parse-string))
  1866. (cons (bibtex-start-of-field bounds)
  1867. (bibtex-end-of-string bounds))))))
  1868. (backward (re-search-backward "^[ \t]*@" nil 'move))
  1869. (t (if (re-search-forward "\n\\([ \t]*@\\)" nil 'move)
  1870. (goto-char (match-beginning 1))))))
  1871. found))
  1872. (defun bibtex-map-entries (fun)
  1873. "Call FUN for each BibTeX entry in buffer (possibly narrowed).
  1874. FUN is called with three arguments, the key of the entry and the buffer
  1875. positions of beginning and end of entry. Also, point is at beginning of
  1876. entry and `match-data' corresponds to the header of the entry,
  1877. see regexp `bibtex-entry-head'. If `bibtex-sort-ignore-string-entries'
  1878. is non-nil, FUN is not called for @String entries."
  1879. (let ((case-fold-search t)
  1880. (end-marker (make-marker))
  1881. found)
  1882. ;; Use marker to keep track of the buffer position of the end of
  1883. ;; a BibTeX entry as this position may change during reformatting.
  1884. (set-marker-insertion-type end-marker t)
  1885. (save-excursion
  1886. (goto-char (point-min))
  1887. (while (setq found (bibtex-skip-to-valid-entry))
  1888. (set-marker end-marker (cdr found))
  1889. (looking-at bibtex-any-entry-maybe-empty-head)
  1890. (funcall fun (bibtex-key-in-head "") (car found) end-marker)
  1891. (goto-char end-marker)))))
  1892. (defun bibtex-progress-message (&optional flag interval)
  1893. "Echo a message about progress of current buffer.
  1894. If FLAG is a string, the message is initialized (in this case a
  1895. value for INTERVAL may be given as well (if not this is set to 5)).
  1896. If FLAG is `done', the message is deinitialized.
  1897. If FLAG is nil, a message is echoed if point was incremented at least
  1898. `bibtex-progress-interval' percent since last message was echoed."
  1899. (cond ((stringp flag)
  1900. (setq bibtex-progress-lastmes flag
  1901. bibtex-progress-interval (or interval 5)
  1902. bibtex-progress-lastperc 0))
  1903. ((eq flag 'done)
  1904. (message "%s (done)" bibtex-progress-lastmes)
  1905. (setq bibtex-progress-lastmes nil))
  1906. (t
  1907. (let* ((size (- (point-max) (point-min)))
  1908. (perc (if (= size 0)
  1909. 100
  1910. (/ (* 100 (- (point) (point-min))) size))))
  1911. (when (>= perc (+ bibtex-progress-lastperc
  1912. bibtex-progress-interval))
  1913. (setq bibtex-progress-lastperc perc)
  1914. (message "%s (%d%%)" bibtex-progress-lastmes perc))))))
  1915. (defun bibtex-field-left-delimiter ()
  1916. "Return a string dependent on `bibtex-field-delimiters'."
  1917. (if (eq bibtex-field-delimiters 'braces)
  1918. "{"
  1919. "\""))
  1920. (defun bibtex-field-right-delimiter ()
  1921. "Return a string dependent on `bibtex-field-delimiters'."
  1922. (if (eq bibtex-field-delimiters 'braces)
  1923. "}"
  1924. "\""))
  1925. (defun bibtex-entry-left-delimiter ()
  1926. "Return a string dependent on `bibtex-entry-delimiters'."
  1927. (if (eq bibtex-entry-delimiters 'braces)
  1928. "{"
  1929. "("))
  1930. (defun bibtex-entry-right-delimiter ()
  1931. "Return a string dependent on `bibtex-entry-delimiters'."
  1932. (if (eq bibtex-entry-delimiters 'braces)
  1933. "}"
  1934. ")"))
  1935. (defun bibtex-flash-head (prompt)
  1936. "Flash at BibTeX entry head before point, if it exists."
  1937. (let ((case-fold-search t)
  1938. (pnt (point)))
  1939. (save-excursion
  1940. (bibtex-beginning-of-entry)
  1941. (when (and (looking-at bibtex-any-entry-maybe-empty-head)
  1942. (< (point) pnt))
  1943. (goto-char (match-beginning bibtex-type-in-head))
  1944. (if (and (< 0 blink-matching-delay)
  1945. (pos-visible-in-window-p (point)))
  1946. (sit-for blink-matching-delay)
  1947. (message "%s%s" prompt (buffer-substring-no-properties
  1948. (point) (match-end bibtex-key-in-head))))))))
  1949. (defun bibtex-make-optional-field (field)
  1950. "Make an optional field named FIELD in current BibTeX entry."
  1951. (if (consp field)
  1952. (bibtex-make-field (cons (concat "OPT" (car field)) (cdr field)))
  1953. (bibtex-make-field (concat "OPT" field))))
  1954. (defun bibtex-move-outside-of-entry ()
  1955. "Make sure point is outside of a BibTeX entry."
  1956. (let ((orig-point (point)))
  1957. (bibtex-end-of-entry)
  1958. (when (< (point) orig-point)
  1959. ;; We moved backward, so we weren't inside an entry to begin with.
  1960. ;; Leave point at the beginning of a line, and preferably
  1961. ;; at the beginning of a paragraph.
  1962. (goto-char orig-point)
  1963. (beginning-of-line 1)
  1964. (unless (= ?\n (char-before (1- (point))))
  1965. (re-search-forward "^[ \t]*[@\n]" nil 'move)
  1966. (backward-char 1)))
  1967. (skip-chars-forward " \t\n")))
  1968. (defun bibtex-beginning-of-first-entry ()
  1969. "Go to beginning of line of first BibTeX entry in buffer.
  1970. If `bibtex-sort-ignore-string-entries' is non-nil, @String entries
  1971. are ignored. Return point"
  1972. (goto-char (point-min))
  1973. (bibtex-skip-to-valid-entry)
  1974. (point))
  1975. (defun bibtex-enclosing-field (&optional comma noerr)
  1976. "Search for BibTeX field enclosing point.
  1977. For `bibtex-mode''s internal algorithms, a field begins at the comma
  1978. following the preceding field. Usually, this is not what the user expects.
  1979. Thus if COMMA is non-nil, the \"current field\" includes the terminating comma
  1980. as well as the entry delimiter if it appears on the same line.
  1981. Unless NOERR is non-nil, signal an error if no enclosing field is found.
  1982. On success return bounds, nil otherwise. Do not move point."
  1983. (save-excursion
  1984. (when comma
  1985. (end-of-line)
  1986. (skip-chars-backward " \t")
  1987. ;; Ignore entry delimiter and comma at end of line.
  1988. (if (memq (preceding-char) '(?} ?\))) (forward-char -1))
  1989. (if (= (preceding-char) ?,) (forward-char -1)))
  1990. (let ((bounds (bibtex-search-backward-field bibtex-field-name t)))
  1991. (cond ((and bounds
  1992. (<= (bibtex-start-of-field bounds) (point))
  1993. (>= (bibtex-end-of-field bounds) (point)))
  1994. bounds)
  1995. ((not noerr)
  1996. (error "Can't find enclosing BibTeX field"))))))
  1997. (defun bibtex-beginning-first-field (&optional beg)
  1998. "Move point to beginning of first field.
  1999. Optional arg BEG is beginning of entry."
  2000. (if beg (goto-char beg) (bibtex-beginning-of-entry))
  2001. (looking-at bibtex-any-entry-maybe-empty-head)
  2002. (goto-char (match-end 0)))
  2003. (defun bibtex-insert-kill (n &optional comma)
  2004. "Reinsert the Nth stretch of killed BibTeX text (field or entry).
  2005. Optional arg COMMA is as in `bibtex-enclosing-field'."
  2006. (unless bibtex-last-kill-command (error "BibTeX kill ring is empty"))
  2007. (let ((fun (lambda (kryp kr) ; adapted from `current-kill'
  2008. (car (set kryp (nthcdr (mod (- n (length (eval kryp)))
  2009. (length kr)) kr))))))
  2010. ;; We put the mark at the beginning of the inserted field or entry
  2011. ;; and point at its end - a behavior similar to what `yank' does.
  2012. ;; The mark is then used by `bibtex-yank-pop', which needs to know
  2013. ;; what we have inserted.
  2014. (if (eq bibtex-last-kill-command 'field)
  2015. (progn
  2016. ;; insert past the current field
  2017. (goto-char (bibtex-end-of-field (bibtex-enclosing-field comma)))
  2018. (push-mark)
  2019. (bibtex-make-field (funcall fun 'bibtex-field-kill-ring-yank-pointer
  2020. bibtex-field-kill-ring) t nil t))
  2021. ;; insert past the current entry
  2022. (bibtex-skip-to-valid-entry)
  2023. (push-mark)
  2024. (insert (funcall fun 'bibtex-entry-kill-ring-yank-pointer
  2025. bibtex-entry-kill-ring))
  2026. ;; If we copied an entry from a buffer containing only this one entry,
  2027. ;; it can be missing the second "\n".
  2028. (unless (looking-back "\n\n") (insert "\n"))
  2029. (unless (functionp bibtex-reference-keys)
  2030. ;; update `bibtex-reference-keys'
  2031. (save-excursion
  2032. (goto-char (mark t))
  2033. (looking-at bibtex-any-entry-maybe-empty-head)
  2034. (let ((key (bibtex-key-in-head)))
  2035. (if key (push (cons key t) bibtex-reference-keys))))))))
  2036. (defsubst bibtex-vec-push (vec idx newelt)
  2037. "Add NEWELT to the list stored in VEC at index IDX."
  2038. (aset vec idx (cons newelt (aref vec idx))))
  2039. (defsubst bibtex-vec-incr (vec idx)
  2040. "Increment by 1 the counter which is stored in VEC at index IDX."
  2041. (aset vec idx (1+ (aref vec idx))))
  2042. (defun bibtex-format-entry ()
  2043. "Helper function for `bibtex-clean-entry'.
  2044. Formats current entry according to variable `bibtex-entry-format'."
  2045. ;; initialize `bibtex-field-braces-opt' if necessary
  2046. (if (and bibtex-field-braces-alist (not bibtex-field-braces-opt))
  2047. (setq bibtex-field-braces-opt
  2048. (bibtex-field-re-init bibtex-field-braces-alist 'braces)))
  2049. ;; initialize `bibtex-field-strings-opt' if necessary
  2050. (if (and bibtex-field-strings-alist (not bibtex-field-strings-opt))
  2051. (setq bibtex-field-strings-opt
  2052. (bibtex-field-re-init bibtex-field-strings-alist 'strings)))
  2053. (let ((case-fold-search t)
  2054. (format (if (eq bibtex-entry-format t)
  2055. '(realign opts-or-alts required-fields numerical-fields
  2056. page-dashes whitespace inherit-booktitle
  2057. last-comma delimiters unify-case braces
  2058. strings sort-fields)
  2059. bibtex-entry-format))
  2060. (left-delim-re (regexp-quote (bibtex-field-left-delimiter)))
  2061. bounds crossref-key req-field-list default-field-list field-list
  2062. num-alt alt-fields idx error-field-name)
  2063. (unwind-protect
  2064. ;; formatting (undone if error occurs)
  2065. (atomic-change-group
  2066. (save-excursion
  2067. (save-restriction
  2068. (bibtex-narrow-to-entry)
  2069. ;; There are more elegant high-level functions for several tasks
  2070. ;; done by `bibtex-format-entry'. However, they contain some
  2071. ;; redundancy compared with what we need to do anyway.
  2072. ;; So for speed-up we avoid using them.
  2073. ;; (`bibtex-format-entry' is called often by `bibtex-reformat'.)
  2074. ;; identify entry type
  2075. (goto-char (point-min))
  2076. (or (re-search-forward bibtex-entry-type nil t)
  2077. (error "Not inside a BibTeX entry"))
  2078. (let* ((beg-type (1+ (match-beginning 0)))
  2079. (end-type (match-end 0))
  2080. (entry-list (assoc-string (buffer-substring-no-properties
  2081. beg-type end-type)
  2082. bibtex-entry-alist t)))
  2083. ;; unify case of entry type
  2084. (when (memq 'unify-case format)
  2085. (delete-region beg-type end-type)
  2086. (insert (car entry-list)))
  2087. ;; update left entry delimiter
  2088. (when (memq 'delimiters format)
  2089. (goto-char end-type)
  2090. (skip-chars-forward " \t\n")
  2091. (delete-char 1)
  2092. (insert (bibtex-entry-left-delimiter)))
  2093. ;; Do we have a crossref key?
  2094. (goto-char (point-min))
  2095. (if (setq bounds (bibtex-search-forward-field
  2096. "\\(OPT\\)?crossref"))
  2097. (let ((text (bibtex-text-in-field-bounds bounds t)))
  2098. (unless (equal "" text)
  2099. (setq crossref-key text))))
  2100. ;; list of required fields appropriate for an entry with
  2101. ;; or without crossref key.
  2102. (setq req-field-list (if crossref-key (nth 2 entry-list)
  2103. (append (nth 2 entry-list) (nth 3 entry-list)))
  2104. ;; default list of fields that may appear in this entry
  2105. default-field-list (append (nth 2 entry-list) (nth 3 entry-list)
  2106. (nth 4 entry-list)
  2107. bibtex-user-optional-fields)
  2108. ;; number of ALT fields we expect to find
  2109. num-alt (length (delq nil (delete-dups
  2110. (mapcar (lambda (x) (nth 3 x))
  2111. req-field-list))))
  2112. ;; ALT fields of respective groups
  2113. alt-fields (make-vector num-alt nil))
  2114. (when (memq 'sort-fields format)
  2115. (goto-char (point-min))
  2116. (let ((beg-fields (save-excursion (bibtex-beginning-first-field)))
  2117. (fields-alist (bibtex-parse-entry))
  2118. bibtex-help-message elt)
  2119. (delete-region beg-fields (point))
  2120. (dolist (field default-field-list)
  2121. (when (setq elt (assoc-string (car field) fields-alist t))
  2122. (setq fields-alist (delete elt fields-alist))
  2123. (bibtex-make-field (list (car elt) nil (cdr elt)) nil nil t)))
  2124. (dolist (field fields-alist)
  2125. (unless (member (car field) '("=key=" "=type="))
  2126. (bibtex-make-field (list (car field) nil (cdr field)) nil nil t))))))
  2127. ;; process all fields
  2128. (bibtex-beginning-first-field (point-min))
  2129. (while (setq bounds (bibtex-parse-field))
  2130. (let* ((beg-field (copy-marker (bibtex-start-of-field bounds)))
  2131. (end-field (copy-marker (bibtex-end-of-field bounds) t))
  2132. (beg-name (copy-marker (bibtex-start-of-name-in-field bounds)))
  2133. (end-name (copy-marker (bibtex-end-of-name-in-field bounds)))
  2134. (beg-text (copy-marker (bibtex-start-of-text-in-field bounds)))
  2135. (end-text (copy-marker (bibtex-end-of-text-in-field bounds) t))
  2136. (empty-field (equal "" (bibtex-text-in-field-bounds bounds t)))
  2137. (field-name (buffer-substring-no-properties beg-name end-name))
  2138. (opt-alt (and (string-match "\\`\\(OPT\\|ALT\\)" field-name)
  2139. (not (and bibtex-no-opt-remove-re
  2140. (string-match bibtex-no-opt-remove-re
  2141. field-name)))))
  2142. deleted)
  2143. (if opt-alt (setq field-name (substring field-name 3)))
  2144. ;; keep track of alternatives
  2145. (if (setq idx (nth 3 (assoc-string field-name req-field-list t)))
  2146. (bibtex-vec-push alt-fields idx field-name))
  2147. (if (memq 'opts-or-alts format)
  2148. ;; delete empty optional and alternative fields
  2149. ;; (but keep empty required fields)
  2150. (cond ((and empty-field
  2151. (or opt-alt
  2152. (let ((field (assoc-string
  2153. field-name req-field-list t)))
  2154. (or (not field) ; OPT field
  2155. (nth 3 field))))) ; ALT field
  2156. (delete-region beg-field end-field)
  2157. (setq deleted t))
  2158. ;; otherwise nonempty field: delete "OPT" or "ALT"
  2159. (opt-alt
  2160. (goto-char beg-name)
  2161. (delete-char 3))))
  2162. (unless deleted
  2163. (push field-name field-list)
  2164. ;; Remove whitespace at beginning and end of field.
  2165. ;; We do not look at individual parts of the field
  2166. ;; as {foo } # bar # { baz} is a fine field.
  2167. (when (memq 'whitespace format)
  2168. (goto-char beg-text)
  2169. (if (looking-at "\\([{\"]\\)[ \t\n]+")
  2170. (replace-match "\\1"))
  2171. (goto-char end-text)
  2172. (if (looking-back "[ \t\n]+\\([}\"]\\)" beg-text t)
  2173. (replace-match "\\1")))
  2174. ;; remove delimiters from purely numerical fields
  2175. (when (and (memq 'numerical-fields format)
  2176. (progn (goto-char beg-text)
  2177. (looking-at "\"[0-9]+\"\\|{[0-9]+}")))
  2178. (goto-char end-text)
  2179. (delete-char -1)
  2180. (goto-char beg-text)
  2181. (delete-char 1))
  2182. ;; update delimiters
  2183. (when (memq 'delimiters format)
  2184. (goto-char beg-text)
  2185. ;; simplified from `bibtex-parse-field-text', as we
  2186. ;; already checked that the field format is correct
  2187. (while (< (point) end-text)
  2188. (if (looking-at bibtex-field-const)
  2189. (goto-char (match-end 0))
  2190. (let ((boundaries (bibtex-parse-field-string)))
  2191. (if (looking-at left-delim-re)
  2192. (goto-char (cdr boundaries))
  2193. (delete-char 1)
  2194. (insert (bibtex-field-left-delimiter))
  2195. (goto-char (1- (cdr boundaries)))
  2196. (delete-char 1)
  2197. (insert (bibtex-field-right-delimiter)))))
  2198. (if (looking-at "[ \t\n]*#[ \t\n]*")
  2199. (goto-char (match-end 0)))))
  2200. ;; update page dashes
  2201. (if (and (memq 'page-dashes format)
  2202. (bibtex-string= field-name "pages")
  2203. (progn (goto-char beg-text)
  2204. (looking-at
  2205. "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
  2206. (replace-match "\\1-\\2"))
  2207. ;; enclose field text by braces according to
  2208. ;; `bibtex-field-braces-alist'.
  2209. (let (case-fold-search temp) ; Case-sensitive search
  2210. (when (and (memq 'braces format)
  2211. (setq temp (cdr (assoc-string field-name
  2212. bibtex-field-braces-opt t))))
  2213. (goto-char beg-text)
  2214. (while (re-search-forward temp end-text t)
  2215. (let ((beg (match-beginning 0))
  2216. (bounds (bibtex-find-text-internal nil t)))
  2217. (unless (or (nth 4 bounds) ; string constant
  2218. ;; match already surrounded by braces
  2219. ;; (braces are inside field delimiters)
  2220. (and (< (point) (1- (nth 2 bounds)))
  2221. (< (1+ (nth 1 bounds)) beg)
  2222. (looking-at "}")
  2223. (save-excursion (goto-char (1- beg))
  2224. (looking-at "{"))))
  2225. (insert "}")
  2226. (goto-char beg)
  2227. (insert "{")))))
  2228. ;; replace field text by BibTeX string constants
  2229. ;; according to `bibtex-field-strings-alist'.
  2230. (when (and (memq 'strings format)
  2231. (setq temp (cdr (assoc-string field-name
  2232. bibtex-field-strings-opt t))))
  2233. (goto-char beg-text)
  2234. (dolist (re temp)
  2235. (while (re-search-forward (car re) end-text t)
  2236. (let ((bounds (save-match-data
  2237. (bibtex-find-text-internal nil t))))
  2238. (unless (nth 4 bounds)
  2239. ;; if match not at right subfield boundary...
  2240. (if (< (match-end 0) (1- (nth 2 bounds)))
  2241. (insert " # " (bibtex-field-left-delimiter))
  2242. (delete-char 1))
  2243. (replace-match (cdr re))
  2244. (goto-char (match-beginning 0))
  2245. ;; if match not at left subfield boundary...
  2246. (if (< (1+ (nth 1 bounds)) (match-beginning 0))
  2247. (insert (bibtex-field-right-delimiter) " # ")
  2248. (delete-char -1))))))))
  2249. ;; use book title of crossref'd entry
  2250. (if (and (memq 'inherit-booktitle format)
  2251. empty-field
  2252. (bibtex-string= field-name "booktitle")
  2253. crossref-key)
  2254. (let ((title (save-excursion
  2255. (save-restriction
  2256. (widen)
  2257. (if (bibtex-search-entry crossref-key t)
  2258. (bibtex-text-in-field "title"))))))
  2259. (when title
  2260. (setq empty-field nil)
  2261. (goto-char (1+ beg-text))
  2262. (insert title))))
  2263. ;; if empty field is a required field, complain
  2264. (when (and empty-field
  2265. (memq 'required-fields format)
  2266. (assoc-string field-name req-field-list t))
  2267. (setq error-field-name field-name)
  2268. (error "Mandatory field `%s' is empty" field-name))
  2269. ;; unify case of field name
  2270. (if (memq 'unify-case format)
  2271. (let ((fname (car (assoc-string field-name
  2272. default-field-list t))))
  2273. (if fname
  2274. (progn
  2275. (delete-region beg-name end-name)
  2276. (goto-char beg-name)
  2277. (insert fname))
  2278. ;; there are no rules we could follow
  2279. (downcase-region beg-name end-name))))
  2280. ;; update point
  2281. (goto-char end-field))))
  2282. ;; check whether all required fields are present
  2283. (if (memq 'required-fields format)
  2284. (let ((alt-expect (make-vector num-alt nil))
  2285. (alt-found (make-vector num-alt 0)))
  2286. (dolist (fname req-field-list)
  2287. (cond ((setq idx (nth 3 fname))
  2288. ;; t if field has alternative flag
  2289. (bibtex-vec-push alt-expect idx (car fname))
  2290. (if (member-ignore-case (car fname) field-list)
  2291. (bibtex-vec-incr alt-found idx)))
  2292. ((not (member-ignore-case (car fname) field-list))
  2293. ;; If we use the crossref field, a required field
  2294. ;; can have the OPT prefix. So if it was empty,
  2295. ;; we have deleted by now. Nonetheless we can
  2296. ;; move point on this empty field.
  2297. (setq error-field-name (car fname))
  2298. (error "Mandatory field `%s' is missing" (car fname)))))
  2299. (dotimes (idx num-alt)
  2300. (cond ((= 0 (aref alt-found idx))
  2301. (setq error-field-name (car (last (aref alt-fields idx))))
  2302. (error "Alternative mandatory field `%s' is missing"
  2303. (aref alt-expect idx)))
  2304. ((< 1 (aref alt-found idx))
  2305. (setq error-field-name (car (last (aref alt-fields idx))))
  2306. (error "Alternative fields `%s' are defined %s times"
  2307. (aref alt-expect idx)
  2308. (length (aref alt-fields idx))))))))
  2309. ;; update comma after last field
  2310. (if (memq 'last-comma format)
  2311. (cond ((and bibtex-comma-after-last-field
  2312. (not (looking-at ",")))
  2313. (insert ","))
  2314. ((and (not bibtex-comma-after-last-field)
  2315. (looking-at ","))
  2316. (delete-char 1))))
  2317. ;; update right entry delimiter
  2318. (if (looking-at ",")
  2319. (forward-char))
  2320. (when (memq 'delimiters format)
  2321. (skip-chars-forward " \t\n")
  2322. (delete-char 1)
  2323. (insert (bibtex-entry-right-delimiter)))
  2324. ;; realign and fill entry
  2325. (if (memq 'realign format)
  2326. (bibtex-fill-entry)))))
  2327. ;; Unwindform: move point to location where error occurred if possible
  2328. (if error-field-name
  2329. (let (bounds)
  2330. (when (save-excursion
  2331. (bibtex-beginning-of-entry)
  2332. (setq bounds
  2333. (bibtex-search-forward-field
  2334. ;; If we use the crossref field, a required field
  2335. ;; can have the OPT prefix
  2336. (concat "\\(OPT\\|ALT\\)?" error-field-name) t)))
  2337. (goto-char (bibtex-start-of-text-in-field bounds))
  2338. (bibtex-find-text)))))))
  2339. (defun bibtex-field-re-init (regexp-alist type)
  2340. "Calculate optimized value for bibtex-regexp-TYPE-opt.
  2341. This value is based on bibtex-regexp-TYPE-alist. TYPE is 'braces or 'strings.
  2342. Return optimized value to be used by `bibtex-format-entry'."
  2343. (setq regexp-alist
  2344. (mapcar (lambda (e)
  2345. (list (car e)
  2346. (replace-regexp-in-string " +" "[ \t\n]+" (nth 1 e))
  2347. (nth 2 e))) ; nil for 'braces'.
  2348. regexp-alist))
  2349. (let (opt-list)
  2350. ;; Loop over field names
  2351. (dolist (field (delete-dups (apply 'append (mapcar 'car regexp-alist))))
  2352. (let (rules)
  2353. ;; Collect all matches we have for this field name
  2354. (dolist (e regexp-alist)
  2355. (if (assoc-string field (car e) t)
  2356. (push (cons (nth 1 e) (nth 2 e)) rules)))
  2357. (if (eq type 'braces)
  2358. ;; concatenate all regexps to a single regexp
  2359. (setq rules (concat "\\(?:" (mapconcat 'car rules "\\|") "\\)")))
  2360. ;; create list of replacement rules.
  2361. (push (cons field rules) opt-list)))
  2362. opt-list))
  2363. (defun bibtex-autokey-abbrev (string len)
  2364. "Return an abbreviation of STRING with at least LEN characters.
  2365. If LEN is positive the abbreviation is terminated only after a consonant
  2366. or at the word end. If LEN is negative the abbreviation is strictly
  2367. enforced using abs (LEN) characters. If LEN is not a number, STRING
  2368. is returned unchanged."
  2369. (cond ((or (not (numberp len))
  2370. (<= (length string) (abs len)))
  2371. string)
  2372. ((equal len 0)
  2373. "")
  2374. ((< len 0)
  2375. (substring string 0 (abs len)))
  2376. (t (let* ((case-fold-search t)
  2377. (abort-char (string-match "[^aeiou]" string (1- len))))
  2378. (if abort-char
  2379. (substring string 0 (1+ abort-char))
  2380. string)))))
  2381. (defun bibtex-autokey-get-field (field &optional change-list)
  2382. "Get content of BibTeX field FIELD. Return empty string if not found.
  2383. Optional arg CHANGE-LIST is a list of substitution patterns that is
  2384. applied to the content of FIELD. It is an alist with pairs
  2385. \(OLD-REGEXP . NEW-STRING\)."
  2386. (let* ((bibtex-expand-strings bibtex-autokey-expand-strings)
  2387. (content (bibtex-text-in-field field bibtex-autokey-use-crossref))
  2388. case-fold-search)
  2389. (unless content (setq content ""))
  2390. (dolist (pattern change-list)
  2391. (setq content (replace-regexp-in-string (car pattern)
  2392. (cdr pattern)
  2393. content t)))
  2394. content))
  2395. (defun bibtex-autokey-get-names ()
  2396. "Get contents of the name field of the current entry.
  2397. Do some modifications based on `bibtex-autokey-name-change-strings'.
  2398. Return the names as a concatenated string obeying `bibtex-autokey-names'
  2399. and `bibtex-autokey-names-stretch'."
  2400. (let ((names (bibtex-autokey-get-field "author\\|editor"
  2401. bibtex-autokey-name-change-strings)))
  2402. ;; Some entries do not have a name field.
  2403. (if (string= "" names)
  2404. names
  2405. (let* ((case-fold-search t)
  2406. (name-list (mapcar 'bibtex-autokey-demangle-name
  2407. (split-string names "[ \t\n]+and[ \t\n]+")))
  2408. additional-names)
  2409. (unless (or (not (numberp bibtex-autokey-names))
  2410. (<= (length name-list)
  2411. (+ bibtex-autokey-names
  2412. bibtex-autokey-names-stretch)))
  2413. ;; Take `bibtex-autokey-names' elements from beginning of name-list
  2414. (setq name-list (nreverse (nthcdr (- (length name-list)
  2415. bibtex-autokey-names)
  2416. (nreverse name-list)))
  2417. additional-names bibtex-autokey-additional-names))
  2418. (concat (mapconcat 'identity name-list
  2419. bibtex-autokey-name-separator)
  2420. additional-names)))))
  2421. (defun bibtex-autokey-demangle-name (fullname)
  2422. "Get the last part from a well-formed FULLNAME and perform abbreviations."
  2423. (let* (case-fold-search
  2424. (name (cond ((string-match "\\([[:upper:]][^, ]*\\)[^,]*," fullname)
  2425. ;; Name is of the form "von Last, First" or
  2426. ;; "von Last, Jr, First"
  2427. ;; --> Take the first capital part before the comma
  2428. (match-string 1 fullname))
  2429. ((string-match "\\([^, ]*\\)," fullname)
  2430. ;; Strange name: we have a comma, but nothing capital
  2431. ;; So we accept even lowercase names
  2432. (match-string 1 fullname))
  2433. ((string-match "\\(\\<[[:lower:]][^ ]* +\\)+\\([[:upper:]][^ ]*\\)"
  2434. fullname)
  2435. ;; name is of the form "First von Last", "von Last",
  2436. ;; "First von von Last", or "d'Last"
  2437. ;; --> take the first capital part after the "von" parts
  2438. (match-string 2 fullname))
  2439. ((string-match "\\([^ ]+\\) *\\'" fullname)
  2440. ;; name is of the form "First Middle Last" or "Last"
  2441. ;; --> take the last token
  2442. (match-string 1 fullname))
  2443. (t (error "Name `%s' is incorrectly formed" fullname)))))
  2444. (funcall bibtex-autokey-name-case-convert-function
  2445. (bibtex-autokey-abbrev name bibtex-autokey-name-length))))
  2446. (defun bibtex-autokey-get-year ()
  2447. "Return year field contents as a string obeying `bibtex-autokey-year-length'."
  2448. (let ((yearfield (bibtex-autokey-get-field "year")))
  2449. (substring yearfield (max 0 (- (length yearfield)
  2450. bibtex-autokey-year-length)))))
  2451. (defun bibtex-autokey-get-title ()
  2452. "Get title field contents up to a terminator.
  2453. Return the result as a string"
  2454. (let ((case-fold-search t)
  2455. (titlestring
  2456. (bibtex-autokey-get-field "title"
  2457. bibtex-autokey-titleword-change-strings)))
  2458. ;; ignore everything past a terminator
  2459. (if (string-match bibtex-autokey-title-terminators titlestring)
  2460. (setq titlestring (substring titlestring 0 (match-beginning 0))))
  2461. ;; gather words from titlestring into a list. Ignore
  2462. ;; specific words and use only a specific amount of words.
  2463. (let ((counter 0)
  2464. (ignore-re (concat "\\`\\(?:"
  2465. (mapconcat 'identity
  2466. bibtex-autokey-titleword-ignore "\\|")
  2467. "\\)\\'"))
  2468. titlewords titlewords-extra word)
  2469. (while (and (or (not (numberp bibtex-autokey-titlewords))
  2470. (< counter (+ bibtex-autokey-titlewords
  2471. bibtex-autokey-titlewords-stretch)))
  2472. (string-match "\\b\\w+" titlestring))
  2473. (setq word (match-string 0 titlestring)
  2474. titlestring (substring titlestring (match-end 0)))
  2475. ;; Ignore words matched by one of the elements of
  2476. ;; `bibtex-autokey-titleword-ignore'. Case is significant.
  2477. (unless (let (case-fold-search)
  2478. (string-match ignore-re word))
  2479. (setq counter (1+ counter))
  2480. (if (or (not (numberp bibtex-autokey-titlewords))
  2481. (<= counter bibtex-autokey-titlewords))
  2482. (push word titlewords)
  2483. (push word titlewords-extra))))
  2484. ;; Obey `bibtex-autokey-titlewords-stretch':
  2485. ;; If by now we have processed all words in titlestring, we include
  2486. ;; titlewords-extra in titlewords. Otherwise, we ignore titlewords-extra.
  2487. (unless (string-match "\\b\\w+" titlestring)
  2488. (setq titlewords (append titlewords-extra titlewords)))
  2489. (mapconcat 'bibtex-autokey-demangle-title (nreverse titlewords)
  2490. bibtex-autokey-titleword-separator))))
  2491. (defun bibtex-autokey-demangle-title (titleword)
  2492. "Do some abbreviations on TITLEWORD.
  2493. The rules are defined in `bibtex-autokey-titleword-abbrevs'
  2494. and `bibtex-autokey-titleword-length'."
  2495. (let ((case-fold-search t)
  2496. (alist bibtex-autokey-titleword-abbrevs))
  2497. (while (and alist
  2498. (not (string-match (concat "\\`\\(?:" (caar alist) "\\)\\'")
  2499. titleword)))
  2500. (setq alist (cdr alist)))
  2501. (if alist
  2502. (cdar alist)
  2503. (funcall bibtex-autokey-titleword-case-convert-function
  2504. (bibtex-autokey-abbrev titleword bibtex-autokey-titleword-length)))))
  2505. (defun bibtex-generate-autokey ()
  2506. "Generate automatically a key for a BibTeX entry.
  2507. Use the author/editor, the year and the title field.
  2508. The algorithm works as follows.
  2509. The name part:
  2510. 1. Use the author or editor field to generate the name part of the key.
  2511. Expand BibTeX strings if `bibtex-autokey-expand-strings' is non-nil.
  2512. 2. Change the content of the name field according to
  2513. `bibtex-autokey-name-change-strings' (see there for further detail).
  2514. 3. Use the first `bibtex-autokey-names' names in the name field. If there
  2515. are up to `bibtex-autokey-names' + `bibtex-autokey-names-stretch' names,
  2516. use all names.
  2517. 4. Use only the last names to form the name part. From these last names,
  2518. take at least `bibtex-autokey-name-length' characters (truncate only
  2519. after a consonant or at a word end).
  2520. 5. Convert all last names using the function
  2521. `bibtex-autokey-name-case-convert-function'.
  2522. 6. Build the name part of the key by concatenating all abbreviated last
  2523. names with the string `bibtex-autokey-name-separator' between any two.
  2524. If there are more names in the name field than names used in the name
  2525. part, append the string `bibtex-autokey-additional-names'.
  2526. The year part:
  2527. 1. Build the year part of the key by truncating the content of the year
  2528. field to the rightmost `bibtex-autokey-year-length' digits (useful
  2529. values are 2 and 4).
  2530. 2. If the year field (or any other field required to generate the key)
  2531. is absent, but the entry has a valid crossref field and
  2532. `bibtex-autokey-use-crossref' is non-nil, use the field of the
  2533. crossreferenced entry instead.
  2534. The title part
  2535. 1. Change the content of the title field according to
  2536. `bibtex-autokey-titleword-change-strings' (see there for further detail).
  2537. 2. Truncate the title before the first match of
  2538. `bibtex-autokey-title-terminators' and delete those words which appear
  2539. in `bibtex-autokey-titleword-ignore'. Build the title part using the
  2540. first `bibtex-autokey-titlewords' words from this truncated title.
  2541. If the truncated title ends after up to `bibtex-autokey-titlewords' +
  2542. `bibtex-autokey-titlewords-stretch' words, use all words from the
  2543. truncated title.
  2544. 3. For every title word that appears in `bibtex-autokey-titleword-abbrevs'
  2545. use the corresponding abbreviation (see documentation of this variable
  2546. for further detail).
  2547. 4. From every title word not generated by an abbreviation, take at least
  2548. `bibtex-autokey-titleword-length' characters (truncate only after
  2549. a consonant or at a word end).
  2550. 5. Convert all title words using the function
  2551. `bibtex-autokey-titleword-case-convert-function'.
  2552. 6. Build the title part by concatenating all abbreviated title words with
  2553. the string `bibtex-autokey-titleword-separator' between any two.
  2554. Concatenate the key:
  2555. 1. Concatenate `bibtex-autokey-prefix-string', the name part, the year
  2556. part and the title part. If the name part and the year part are both
  2557. non-empty insert `bibtex-autokey-name-year-separator' between the two.
  2558. If the title part and the year (or name) part are non-empty, insert
  2559. `bibtex-autokey-year-title-separator' between the two.
  2560. 2. If `bibtex-autokey-before-presentation-function' is non-nil, it must be
  2561. a function taking one argument. Call this function with the generated
  2562. key as the argument. Use the return value of this function (a string)
  2563. as the key.
  2564. 3. If `bibtex-autokey-edit-before-use' is non-nil, present the key in the
  2565. minibuffer to the user for editing. Insert the key given by the user."
  2566. (let* ((names (bibtex-autokey-get-names))
  2567. (year (bibtex-autokey-get-year))
  2568. (title (bibtex-autokey-get-title))
  2569. (autokey (concat bibtex-autokey-prefix-string
  2570. names
  2571. (unless (or (equal names "")
  2572. (equal year ""))
  2573. bibtex-autokey-name-year-separator)
  2574. year
  2575. (unless (or (and (equal names "")
  2576. (equal year ""))
  2577. (equal title ""))
  2578. bibtex-autokey-year-title-separator)
  2579. title)))
  2580. (if bibtex-autokey-before-presentation-function
  2581. (funcall bibtex-autokey-before-presentation-function autokey)
  2582. autokey)))
  2583. (defun bibtex-global-key-alist ()
  2584. "Return global key alist based on `bibtex-files'."
  2585. (if bibtex-files
  2586. (apply 'append
  2587. (mapcar (lambda (buf)
  2588. (with-current-buffer buf bibtex-reference-keys))
  2589. ;; include current buffer only if it uses `bibtex-mode'
  2590. (bibtex-initialize (eq major-mode 'bibtex-mode))))
  2591. (if (eq major-mode 'bibtex-mode)
  2592. bibtex-reference-keys)))
  2593. (defun bibtex-read-key (prompt &optional key global)
  2594. "Read BibTeX key from minibuffer using PROMPT and default KEY.
  2595. If optional arg GLOBAL is non-nil, completion is based on the keys in
  2596. `bibtex-reference-keys' of `bibtex-files',"
  2597. (let (completion-ignore-case)
  2598. (completing-read prompt (if global (bibtex-global-key-alist)
  2599. bibtex-reference-keys)
  2600. nil nil key 'bibtex-key-history)))
  2601. (defun bibtex-read-string-key (&optional key)
  2602. "Read BibTeX string key from minibuffer using default KEY."
  2603. (let ((completion-ignore-case t))
  2604. (completing-read "String key: " bibtex-strings
  2605. nil nil key 'bibtex-key-history)))
  2606. (defun bibtex-parse-keys (&optional abortable verbose)
  2607. "Set `bibtex-reference-keys' to the keys used in the whole buffer.
  2608. Find both entry keys and crossref entries. If ABORTABLE is non-nil abort
  2609. on user input. If VERBOSE is non-nil give messages about progress.
  2610. Return alist of keys if parsing was completed, `aborted' otherwise.
  2611. If `bibtex-parse-keys-fast' is non-nil, use fast but simplified algorithm
  2612. for parsing BibTeX keys. If parsing fails, try to set this variable to nil."
  2613. (if (eq major-mode 'bibtex-mode)
  2614. (let (ref-keys crossref-keys)
  2615. (save-excursion
  2616. (save-match-data
  2617. (if verbose
  2618. (bibtex-progress-message
  2619. (concat (buffer-name) ": parsing reference keys")))
  2620. (catch 'userkey
  2621. (goto-char (point-min))
  2622. (if bibtex-parse-keys-fast
  2623. (let ((case-fold-search t)
  2624. (re (concat bibtex-entry-head "\\|"
  2625. ",[ \t\n]*crossref[ \t\n]*=[ \t\n]*"
  2626. "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]")))
  2627. (while (re-search-forward re nil t)
  2628. (if (and abortable (input-pending-p))
  2629. ;; user has aborted by typing a key: return `aborted'
  2630. (throw 'userkey 'aborted))
  2631. (cond ((match-end 3)
  2632. ;; This is a crossref.
  2633. (let ((key (buffer-substring-no-properties
  2634. (1+ (match-beginning 3)) (1- (match-end 3)))))
  2635. (unless (assoc key crossref-keys)
  2636. (push (list key) crossref-keys))))
  2637. ;; only keys of known entries
  2638. ((assoc-string (bibtex-type-in-head)
  2639. bibtex-entry-alist t)
  2640. ;; This is an entry.
  2641. (let ((key (bibtex-key-in-head)))
  2642. (unless (assoc key ref-keys)
  2643. (push (cons key t) ref-keys)))))))
  2644. (let (;; ignore @String entries because they are handled
  2645. ;; separately by `bibtex-parse-strings'
  2646. (bibtex-sort-ignore-string-entries t)
  2647. bounds)
  2648. (bibtex-map-entries
  2649. (lambda (key _beg end)
  2650. (if (and abortable
  2651. (input-pending-p))
  2652. ;; user has aborted by typing a key: return `aborted'
  2653. (throw 'userkey 'aborted))
  2654. (if verbose (bibtex-progress-message))
  2655. (unless (assoc key ref-keys)
  2656. (push (cons key t) ref-keys))
  2657. (if (and (setq bounds (bibtex-search-forward-field "crossref" end))
  2658. (setq key (bibtex-text-in-field-bounds bounds t))
  2659. (not (assoc key crossref-keys)))
  2660. (push (list key) crossref-keys))))))
  2661. (dolist (key crossref-keys)
  2662. (unless (assoc (car key) ref-keys) (push key ref-keys)))
  2663. (if verbose
  2664. (bibtex-progress-message 'done))
  2665. ;; successful operation --> return `bibtex-reference-keys'
  2666. (setq bibtex-reference-keys ref-keys)))))))
  2667. (defun bibtex-parse-strings (&optional add abortable)
  2668. "Set `bibtex-strings' to the string definitions in the whole buffer.
  2669. If ADD is non-nil add the new strings to `bibtex-strings' instead of
  2670. simply resetting it. If ADD is an alist of strings, also add ADD to
  2671. `bibtex-strings'. If ABORTABLE is non-nil abort on user input.
  2672. Return alist of strings if parsing was completed, `aborted' otherwise."
  2673. (save-excursion
  2674. (save-match-data
  2675. (goto-char (point-min))
  2676. (let ((strings (if (and add (not (functionp bibtex-strings)))
  2677. bibtex-strings))
  2678. bounds key)
  2679. (if (listp add)
  2680. (dolist (string add)
  2681. (unless (assoc-string (car string) strings t)
  2682. (push string strings))))
  2683. (catch 'userkey
  2684. (while (setq bounds (bibtex-search-forward-string))
  2685. (if (and abortable
  2686. (input-pending-p))
  2687. ;; user has aborted by typing a key --> return `aborted'
  2688. (throw 'userkey 'aborted))
  2689. (setq key (bibtex-reference-key-in-string bounds))
  2690. (unless (assoc-string key strings t)
  2691. (push (cons key (bibtex-text-in-string bounds t))
  2692. strings))
  2693. (goto-char (bibtex-end-of-text-in-string bounds)))
  2694. ;; successful operation --> return `bibtex-strings'
  2695. (setq bibtex-strings strings))))))
  2696. (defun bibtex-strings ()
  2697. "Return `bibtex-strings'. Initialize this variable if necessary."
  2698. (if (functionp bibtex-strings)
  2699. (bibtex-parse-strings (bibtex-string-files-init))
  2700. bibtex-strings))
  2701. (defun bibtex-string-files-init ()
  2702. "Return initialization for `bibtex-strings'.
  2703. Use `bibtex-predefined-strings' and BibTeX files `bibtex-string-files'."
  2704. (save-match-data
  2705. (let ((dirlist (split-string (or bibtex-string-file-path default-directory)
  2706. ":+"))
  2707. (case-fold-search)
  2708. string-files fullfilename compl bounds found)
  2709. ;; collect absolute file names of valid string files
  2710. (dolist (filename bibtex-string-files)
  2711. (unless (string-match "\\.bib\\'" filename)
  2712. (setq filename (concat filename ".bib")))
  2713. ;; test filenames
  2714. (if (file-name-absolute-p filename)
  2715. (if (file-readable-p filename)
  2716. (push filename string-files)
  2717. (error "BibTeX strings file %s not found" filename))
  2718. (dolist (dir dirlist)
  2719. (when (file-readable-p
  2720. (setq fullfilename (expand-file-name filename dir)))
  2721. (push fullfilename string-files)
  2722. (setq found t)))
  2723. (unless found
  2724. (error "File %s not in paths defined via bibtex-string-file-path"
  2725. filename))))
  2726. ;; parse string files
  2727. (dolist (filename string-files)
  2728. (with-temp-buffer
  2729. (insert-file-contents filename)
  2730. (goto-char (point-min))
  2731. (while (setq bounds (bibtex-search-forward-string))
  2732. (push (cons (bibtex-reference-key-in-string bounds)
  2733. (bibtex-text-in-string bounds t))
  2734. compl)
  2735. (goto-char (bibtex-end-of-string bounds)))))
  2736. (append bibtex-predefined-strings (nreverse compl)))))
  2737. (defun bibtex-parse-buffers-stealthily ()
  2738. "Parse buffer in the background during idle time.
  2739. Called by `run-with-idle-timer'. Whenever Emacs has been idle
  2740. for `bibtex-parse-keys-timeout' seconds, parse all BibTeX buffers
  2741. which have been modified after last parsing.
  2742. Parsing initializes `bibtex-reference-keys' and `bibtex-strings'."
  2743. (save-excursion
  2744. (let ((buffers (buffer-list))
  2745. (strings-init (bibtex-string-files-init)))
  2746. (while (and buffers (not (input-pending-p)))
  2747. (set-buffer (car buffers))
  2748. (if (and (eq major-mode 'bibtex-mode)
  2749. (not (eq (buffer-modified-tick)
  2750. bibtex-buffer-last-parsed-tick)))
  2751. (save-restriction
  2752. (widen)
  2753. ;; Output no progress messages in `bibtex-parse-keys'
  2754. ;; because when in `y-or-n-p' that can hide the question.
  2755. (if (and (listp (bibtex-parse-keys t))
  2756. ;; update `bibtex-strings'
  2757. (listp (bibtex-parse-strings strings-init t)))
  2758. ;; remember that parsing was successful
  2759. (setq bibtex-buffer-last-parsed-tick (buffer-modified-tick)))))
  2760. (setq buffers (cdr buffers))))))
  2761. ;;;###autoload
  2762. (defun bibtex-initialize (&optional current force select)
  2763. "(Re)Initialize BibTeX buffers.
  2764. Visit the BibTeX files defined by `bibtex-files' and return a list
  2765. of corresponding buffers.
  2766. Initialize in these buffers `bibtex-reference-keys' if not yet set.
  2767. List of BibTeX buffers includes current buffer if CURRENT is non-nil.
  2768. If FORCE is non-nil, (re)initialize `bibtex-reference-keys' even if
  2769. already set. If SELECT is non-nil interactively select a BibTeX buffer.
  2770. When called interactively, FORCE is t, CURRENT is t if current buffer uses
  2771. `bibtex-mode', and SELECT is t if current buffer does not use `bibtex-mode',"
  2772. (interactive (list (eq major-mode 'bibtex-mode) t
  2773. (not (eq major-mode 'bibtex-mode))))
  2774. (let ((file-path (split-string (or bibtex-file-path default-directory) ":+"))
  2775. file-list dir-list buffer-list)
  2776. ;; generate list of BibTeX files
  2777. (dolist (file bibtex-files)
  2778. (cond ((eq file 'bibtex-file-path)
  2779. (setq dir-list (append dir-list file-path)))
  2780. ((file-accessible-directory-p file)
  2781. (push file dir-list))
  2782. ((progn (unless (string-match "\\.bib\\'" file)
  2783. (setq file (concat file ".bib")))
  2784. (file-name-absolute-p file))
  2785. (push file file-list))
  2786. (t
  2787. (let (expanded-file-name found)
  2788. (dolist (dir file-path)
  2789. (when (file-readable-p
  2790. (setq expanded-file-name (expand-file-name file dir)))
  2791. (push expanded-file-name file-list)
  2792. (setq found t)))
  2793. (unless found
  2794. (error "File `%s' not in paths defined via bibtex-file-path"
  2795. file))))))
  2796. (dolist (file file-list)
  2797. (unless (file-readable-p file)
  2798. (error "BibTeX file `%s' not found" file)))
  2799. ;; expand dir-list
  2800. (dolist (dir dir-list)
  2801. (setq file-list
  2802. (append file-list (directory-files dir t "\\.bib\\'" t))))
  2803. (delete-dups file-list)
  2804. ;; visit files in FILE-LIST
  2805. (dolist (file file-list)
  2806. (if (file-readable-p file)
  2807. (push (find-file-noselect file) buffer-list)))
  2808. ;; Include current buffer iff we want it.
  2809. ;; Exclude current buffer if it doesn't use `bibtex-mode'.
  2810. ;; Thus calling `bibtex-initialize' gives meaningful results for
  2811. ;; any current buffer.
  2812. (unless (and current (eq major-mode 'bibtex-mode)) (setq current nil))
  2813. (cond ((and current (not (memq (current-buffer) buffer-list)))
  2814. (push (current-buffer) buffer-list))
  2815. ((and (not current) (memq (current-buffer) buffer-list))
  2816. (setq buffer-list (delq (current-buffer) buffer-list))))
  2817. ;; parse keys
  2818. (let (string-init)
  2819. (dolist (buffer buffer-list)
  2820. (with-current-buffer buffer
  2821. (if (or force (functionp bibtex-reference-keys))
  2822. (bibtex-parse-keys))
  2823. (when (or force (functionp bibtex-strings))
  2824. (unless string-init (setq string-init (bibtex-string-files-init)))
  2825. (bibtex-parse-strings string-init)))))
  2826. ;; select BibTeX buffer
  2827. (if select
  2828. (if buffer-list
  2829. (switch-to-buffer
  2830. (completing-read "Switch to BibTeX buffer: "
  2831. (mapcar 'buffer-name buffer-list)
  2832. nil t
  2833. (if current (buffer-name (current-buffer)))))
  2834. (message "No BibTeX buffers defined")))
  2835. buffer-list))
  2836. (defun bibtex-complete-string-cleanup (compl) (lambda (str status) ;Curried.
  2837. "Cleanup after inserting string STR.
  2838. Remove enclosing field delimiters for STR. Display message with
  2839. expansion of STR using expansion list COMPL."
  2840. (when (memq status '(exact finished sole))
  2841. (let ((abbr (cdr (assoc-string str compl t))))
  2842. (when abbr
  2843. (message "%s = abbreviation for `%s'" str abbr)))
  2844. (when (eq status 'finished)
  2845. (save-excursion (bibtex-remove-delimiters))))))
  2846. (defun bibtex-complete-crossref-cleanup (buf) (lambda (key status) ;Curried.
  2847. "Display summary message on entry KEY after completion of a crossref key.
  2848. Use `bibtex-summary-function' to generate summary."
  2849. (when (memq status '(exact sole finished))
  2850. (let ((summary
  2851. (with-current-buffer buf
  2852. (save-excursion
  2853. (if (bibtex-search-entry key t)
  2854. (funcall bibtex-summary-function))))))
  2855. (when summary
  2856. (message "%s %s" key summary))))))
  2857. (defun bibtex-copy-summary-as-kill (&optional arg)
  2858. "Push summery of current BibTeX entry to kill ring.
  2859. Use `bibtex-summary-function' to generate summary.
  2860. If prefix ARG is non-nil push BibTeX entry's URL to kill ring
  2861. that is generated by calling `bibtex-url'."
  2862. (interactive "P")
  2863. (if arg (let ((url (bibtex-url nil t)))
  2864. (if url (kill-new (message "%s" url))
  2865. (message "No URL known")))
  2866. (save-excursion
  2867. (bibtex-beginning-of-entry)
  2868. (if (looking-at bibtex-entry-maybe-empty-head)
  2869. (kill-new (message "%s" (funcall bibtex-summary-function)))
  2870. (error "No entry found")))))
  2871. (defun bibtex-summary ()
  2872. "Return summary of current BibTeX entry.
  2873. Used as default value of `bibtex-summary-function'."
  2874. ;; It would be neat to make this function customizable. How?
  2875. (if (looking-at bibtex-entry-maybe-empty-head)
  2876. (let* ((bibtex-autokey-name-case-convert-function 'identity)
  2877. (bibtex-autokey-name-length 'infty)
  2878. (bibtex-autokey-names 1)
  2879. (bibtex-autokey-names-stretch 0)
  2880. (bibtex-autokey-name-separator " ")
  2881. (bibtex-autokey-additional-names " etal")
  2882. (names (bibtex-autokey-get-names))
  2883. (bibtex-autokey-year-length 4)
  2884. (year (bibtex-autokey-get-year))
  2885. (bibtex-autokey-titlewords 5)
  2886. (bibtex-autokey-titlewords-stretch 2)
  2887. (bibtex-autokey-titleword-case-convert-function 'identity)
  2888. (bibtex-autokey-titleword-length 5)
  2889. (bibtex-autokey-titleword-separator " ")
  2890. (title (bibtex-autokey-get-title))
  2891. (journal (bibtex-autokey-get-field
  2892. "journal" bibtex-autokey-transcriptions))
  2893. (volume (bibtex-autokey-get-field "volume"))
  2894. (pages (bibtex-autokey-get-field "pages" '(("-.*\\'" . "")))))
  2895. (mapconcat (lambda (arg)
  2896. (if (not (string= "" (cdr arg)))
  2897. (concat (car arg) (cdr arg))))
  2898. `((" " . ,names) (" " . ,year) (": " . ,title)
  2899. (", " . ,journal) (" " . ,volume) (":" . ,pages))
  2900. ""))
  2901. (error "Entry not found")))
  2902. (defun bibtex-pop (arg direction)
  2903. "Fill current field from the ARGth same field's text in DIRECTION.
  2904. Generic function used by `bibtex-pop-previous' and `bibtex-pop-next'."
  2905. ;; parse current field
  2906. (let* ((bounds (bibtex-enclosing-field t))
  2907. (start-old-field (bibtex-start-of-field bounds))
  2908. (start-old-text (bibtex-start-of-text-in-field bounds))
  2909. (end-old-text (bibtex-end-of-text-in-field bounds))
  2910. (field-name (bibtex-name-in-field bounds t))
  2911. failure)
  2912. (save-excursion
  2913. ;; if executed several times in a row, start each search where
  2914. ;; the last one was finished
  2915. (cond ((eq last-command 'bibtex-pop)
  2916. (goto-char (if (eq direction 'previous)
  2917. bibtex-pop-previous-search-point
  2918. bibtex-pop-next-search-point)))
  2919. ((eq direction 'previous)
  2920. (bibtex-beginning-of-entry))
  2921. (t (bibtex-end-of-entry)))
  2922. ;; Search for arg'th previous/next similar field
  2923. (while (and (not failure)
  2924. (>= (setq arg (1- arg)) 0))
  2925. ;; The search of BibTeX fields is not bounded by entry boundaries
  2926. (if (eq direction 'previous)
  2927. (if (setq bounds (bibtex-search-backward-field field-name))
  2928. (goto-char (bibtex-start-of-field bounds))
  2929. (setq failure t))
  2930. (if (setq bounds (bibtex-search-forward-field field-name))
  2931. (goto-char (bibtex-end-of-field bounds))
  2932. (setq failure t))))
  2933. (if failure
  2934. (error "No %s matching BibTeX field"
  2935. (if (eq direction 'previous) "previous" "next"))
  2936. ;; Found a matching field. Remember boundaries.
  2937. (let ((new-text (bibtex-text-in-field-bounds bounds))
  2938. (nbeg (copy-marker (bibtex-start-of-field bounds)))
  2939. (nend (copy-marker (bibtex-end-of-field bounds))))
  2940. (bibtex-flash-head "From: ")
  2941. ;; Go back to where we started, delete old text, and pop new.
  2942. (goto-char end-old-text)
  2943. (delete-region start-old-text end-old-text)
  2944. (if (= nbeg start-old-field)
  2945. (insert (bibtex-field-left-delimiter)
  2946. (bibtex-field-right-delimiter))
  2947. (insert new-text))
  2948. (setq bibtex-pop-previous-search-point (marker-position nbeg)
  2949. bibtex-pop-next-search-point (marker-position nend))))))
  2950. (bibtex-find-text nil nil nil t)
  2951. (setq this-command 'bibtex-pop))
  2952. (defun bibtex-beginning-of-field ()
  2953. "Move point backward to beginning of field.
  2954. This function uses a simple, fast algorithm assuming that the field
  2955. begins at the beginning of a line. We use this function for font-locking."
  2956. (let ((field-reg (concat "^[ \t]*" bibtex-field-name "[ \t]*=")))
  2957. (beginning-of-line)
  2958. (unless (looking-at field-reg)
  2959. (re-search-backward field-reg nil t))))
  2960. (defun bibtex-font-lock-url (bound &optional no-button)
  2961. "Font-lock for URLs. BOUND limits the search.
  2962. If NO-BUTTON is non-nil do not generate buttons."
  2963. (let ((case-fold-search t)
  2964. (pnt (point))
  2965. name bounds start end found)
  2966. (bibtex-beginning-of-field)
  2967. (while (and (not found)
  2968. (<= (point) bound)
  2969. (prog1 (re-search-forward bibtex-font-lock-url-regexp bound t)
  2970. (setq name (match-string-no-properties 1)))
  2971. (setq bounds (bibtex-parse-field-text))
  2972. (progn
  2973. (setq start (car bounds) end (nth 1 bounds))
  2974. ;; Always ignore field delimiters
  2975. (if (memq (char-before end) '(?\} ?\"))
  2976. (setq end (1- end)))
  2977. (if (memq (char-after start) '(?\{ ?\"))
  2978. (setq start (1+ start)))
  2979. (if (< start pnt) (setq start (min pnt end)))
  2980. (<= start bound)))
  2981. (if (<= pnt start)
  2982. (let ((lst bibtex-generate-url-list) url)
  2983. (while (and (not found) (setq url (car (pop lst))))
  2984. (goto-char start)
  2985. (setq found (and (bibtex-string= name (car url))
  2986. (re-search-forward (cdr url) end t))))))
  2987. (unless found (goto-char end)))
  2988. (if (and found (not no-button))
  2989. (bibtex-button (match-beginning 0) (match-end 0)
  2990. 'bibtex-url (match-beginning 0)))
  2991. found))
  2992. (defun bibtex-font-lock-crossref (bound)
  2993. "Font-lock for crossref fields. BOUND limits the search."
  2994. (let ((case-fold-search t)
  2995. (pnt (point))
  2996. (crossref-reg (concat "^[ \t]*crossref[ \t]*=[ \t\n]*"
  2997. "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]"))
  2998. start end found)
  2999. (bibtex-beginning-of-field)
  3000. (while (and (not found)
  3001. (re-search-forward crossref-reg bound t))
  3002. (setq start (1+ (match-beginning 1))
  3003. end (1- (match-end 1))
  3004. found (>= start pnt)))
  3005. (if found (bibtex-button start end 'bibtex-search-crossref
  3006. (buffer-substring-no-properties start end)
  3007. start t))
  3008. found))
  3009. (defun bibtex-font-lock-cite (matcher bound)
  3010. "Font-lock for cited keys.
  3011. MATCHER identifies the cited key, see `bibtex-cite-matcher-alist'.
  3012. BOUND limits the search."
  3013. (let (case-fold-search)
  3014. (if (re-search-forward (car matcher) bound t)
  3015. (let ((start (match-beginning (cdr matcher)))
  3016. (end (match-end (cdr matcher))))
  3017. (bibtex-button start end 'bibtex-search-crossref
  3018. (buffer-substring-no-properties start end)
  3019. start t t)
  3020. t))))
  3021. (defun bibtex-button-action (button)
  3022. "Call BUTTON's BibTeX function."
  3023. (apply (button-get button 'bibtex-function)
  3024. (button-get button 'bibtex-args)))
  3025. (define-button-type 'bibtex-url
  3026. 'action 'bibtex-button-action
  3027. 'bibtex-function 'bibtex-url
  3028. 'help-echo (purecopy "mouse-2, RET: follow URL"))
  3029. (define-button-type 'bibtex-search-crossref
  3030. 'action 'bibtex-button-action
  3031. 'bibtex-function 'bibtex-search-crossref
  3032. 'help-echo (purecopy "mouse-2, RET: follow crossref"))
  3033. (defun bibtex-button (beg end type &rest args)
  3034. "Make a BibTeX button from BEG to END of type TYPE in the current buffer."
  3035. (make-text-button beg end 'type type 'bibtex-args args))
  3036. ;; Interactive Functions:
  3037. ;;;###autoload
  3038. (define-derived-mode bibtex-mode nil "BibTeX"
  3039. "Major mode for editing BibTeX files.
  3040. General information on working with BibTeX mode:
  3041. Use commands such as \\<bibtex-mode-map>\\[bibtex-Book] to get a template for a specific entry.
  3042. Then fill in all desired fields using \\[bibtex-next-field] to jump from field
  3043. to field. After having filled in all desired fields in the entry, clean the
  3044. new entry with the command \\[bibtex-clean-entry].
  3045. Some features of BibTeX mode are available only by setting the variable
  3046. `bibtex-maintain-sorted-entries' to non-nil. However, then BibTeX mode
  3047. works only with buffers containing valid (syntactically correct) and sorted
  3048. entries. This is usually the case, if you have created a buffer completely
  3049. with BibTeX mode and finished every new entry with \\[bibtex-clean-entry].
  3050. For third party BibTeX files, call the command \\[bibtex-convert-alien]
  3051. to fully take advantage of all features of BibTeX mode.
  3052. Special information:
  3053. A command such as \\[bibtex-Book] outlines the fields for a BibTeX book entry.
  3054. The names of optional fields start with the string OPT, and are thus ignored
  3055. by BibTeX. The names of alternative fields from which only one is required
  3056. start with the string ALT. The OPT or ALT string may be removed from
  3057. the name of a field with \\[bibtex-remove-OPT-or-ALT].
  3058. \\[bibtex-make-field] inserts a new field after the current one.
  3059. \\[bibtex-kill-field] kills the current field entirely.
  3060. \\[bibtex-yank] yanks the last recently killed field after the current field.
  3061. \\[bibtex-remove-delimiters] removes the double-quotes or braces around the text of the current field.
  3062. \\[bibtex-empty-field] replaces the text of the current field with the default \"\" or {}.
  3063. \\[bibtex-find-text] moves point to the end of the current field.
  3064. \\[completion-at-point] completes word fragment before point according to context.
  3065. The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT
  3066. from the names of all non-empty optional or alternative fields, checks that
  3067. no required fields are empty, and does some formatting dependent on the value
  3068. of `bibtex-entry-format'. Furthermore, it can automatically generate a key
  3069. for the BibTeX entry, see `bibtex-generate-autokey'.
  3070. Note: some functions in BibTeX mode depend on entries being in a special
  3071. format (all fields beginning on separate lines), so it is usually a bad
  3072. idea to remove `realign' from `bibtex-entry-format'.
  3073. BibTeX mode supports Imenu and hideshow minor mode (`hs-minor-mode').
  3074. ----------------------------------------------------------
  3075. Entry to BibTeX mode calls the value of `bibtex-mode-hook'
  3076. if that value is non-nil.
  3077. \\{bibtex-mode-map}"
  3078. (add-hook 'completion-at-point-functions
  3079. 'bibtex-completion-at-point-function nil 'local)
  3080. (make-local-variable 'bibtex-buffer-last-parsed-tick)
  3081. ;; Install stealthy parse function if not already installed
  3082. (unless bibtex-parse-idle-timer
  3083. (setq bibtex-parse-idle-timer (run-with-idle-timer
  3084. bibtex-parse-keys-timeout t
  3085. 'bibtex-parse-buffers-stealthily)))
  3086. (set (make-local-variable 'paragraph-start) "[ \f\n\t]*$")
  3087. (set (make-local-variable 'comment-start) bibtex-comment-start)
  3088. (set (make-local-variable 'comment-start-skip)
  3089. (concat (regexp-quote bibtex-comment-start) "\\>[ \t]*"))
  3090. (set (make-local-variable 'comment-column) 0)
  3091. (set (make-local-variable 'defun-prompt-regexp) "^[ \t]*@[[:alnum:]]+[ \t]*")
  3092. (set (make-local-variable 'outline-regexp) "[ \t]*@")
  3093. (set (make-local-variable 'fill-paragraph-function) 'bibtex-fill-field)
  3094. (set (make-local-variable 'fill-prefix)
  3095. (make-string (+ bibtex-entry-offset bibtex-contline-indentation) ?\s))
  3096. (set (make-local-variable 'font-lock-defaults)
  3097. '(bibtex-font-lock-keywords
  3098. nil t ((?$ . "\"")
  3099. ;; Mathematical expressions should be fontified as strings
  3100. (?\" . ".")
  3101. ;; Quotes are field delimiters and quote-delimited
  3102. ;; entries should be fontified in the same way as
  3103. ;; brace-delimited ones
  3104. )
  3105. nil
  3106. (font-lock-extra-managed-props . (category))
  3107. (font-lock-mark-block-function
  3108. . (lambda ()
  3109. (set-mark (bibtex-end-of-entry))
  3110. (bibtex-beginning-of-entry)))))
  3111. (set (make-local-variable 'syntax-propertize-function)
  3112. (syntax-propertize-via-font-lock
  3113. bibtex-font-lock-syntactic-keywords))
  3114. (setq imenu-generic-expression
  3115. (list (list nil bibtex-entry-head bibtex-key-in-head))
  3116. imenu-case-fold-search t)
  3117. ;; Allow `bibtex-dialect' as a file-local variable.
  3118. (add-hook 'hack-local-variables-hook 'bibtex-set-dialect nil t))
  3119. (defun bibtex-entry-alist (dialect)
  3120. "Return entry-alist for DIALECT."
  3121. (let ((var (intern (format "bibtex-%s-entry-alist" dialect)))
  3122. entry-alist)
  3123. (if (boundp var)
  3124. (setq entry-alist (symbol-value var))
  3125. (error "BibTeX dialect `%s' undefined" dialect))
  3126. (if (not (consp (nth 1 (car entry-alist))))
  3127. ;; new format
  3128. entry-alist
  3129. ;; Convert old format of `bibtex-entry-field-alist'
  3130. (unless (get var 'entry-list-format)
  3131. (put var 'entry-list-format "pre-24")
  3132. (message "Old format of `%s' (pre GNU Emacs 24).
  3133. Please convert to the new format."
  3134. (if (eq (indirect-variable 'bibtex-entry-field-alist) var)
  3135. 'bibtex-entry-field-alist var))
  3136. (sit-for 3))
  3137. (let (lst)
  3138. (dolist (entry entry-alist)
  3139. (let ((fl (nth 1 entry)) req xref opt)
  3140. (dolist (field (copy-tree (car fl)))
  3141. (if (nth 3 field) (setcar (nthcdr 3 field) 0))
  3142. (if (or (not (nth 2 entry))
  3143. (assoc-string (car field) (car (nth 2 entry)) t))
  3144. (push field req)
  3145. (push field xref)))
  3146. (dolist (field (nth 1 fl))
  3147. (push field opt))
  3148. (push (list (car entry) nil (nreverse req)
  3149. (nreverse xref) (nreverse opt))
  3150. lst)))
  3151. (nreverse lst)))))
  3152. (defun bibtex-set-dialect (&optional dialect local)
  3153. "Select BibTeX DIALECT for editing BibTeX files.
  3154. This sets the user variable `bibtex-dialect' as well as the dialect-dependent
  3155. internal variables. Allowed dialects are listed in `bibtex-dialect-list'.
  3156. If DIALECT is nil use current value of `bibtex-dialect'.
  3157. If LOCAL is non-nil make buffer-local bindings for these variables rather than
  3158. setting the global values. The dialect-dependent internal variables
  3159. are also bound buffer-locally if `bibtex-dialect' is already buffer-local
  3160. in the current buffer (for example, as a file-local variable).
  3161. LOCAL is t for interactive calls."
  3162. (interactive (list (intern (completing-read "Dialect: "
  3163. (mapcar 'list bibtex-dialect-list)
  3164. nil t)) t))
  3165. (let ((setfun (if (or local (local-variable-p 'bibtex-dialect))
  3166. (lambda (var val) (set (make-local-variable var) val))
  3167. 'set)))
  3168. (if dialect (funcall setfun 'bibtex-dialect dialect))
  3169. ;; Set internal variables
  3170. (funcall setfun 'bibtex-entry-alist (bibtex-entry-alist bibtex-dialect))
  3171. (funcall setfun 'bibtex-field-alist
  3172. (let ((var (intern (format "bibtex-%s-field-alist"
  3173. bibtex-dialect))))
  3174. (if (boundp var)
  3175. (symbol-value var)
  3176. (error "Field types for BibTeX dialect `%s' undefined"
  3177. bibtex-dialect))))
  3178. (funcall setfun 'bibtex-entry-type
  3179. (concat "@[ \t]*\\(?:"
  3180. (regexp-opt (mapcar 'car bibtex-entry-alist)) "\\)"))
  3181. (funcall setfun 'bibtex-entry-head
  3182. (concat "^[ \t]*\\(" bibtex-entry-type "\\)[ \t]*[({][ \t\n]*\\("
  3183. bibtex-reference-key "\\)"))
  3184. (funcall setfun 'bibtex-entry-maybe-empty-head
  3185. (concat bibtex-entry-head "?"))
  3186. (funcall setfun 'bibtex-any-valid-entry-type
  3187. (concat "^[ \t]*@[ \t]*\\(?:"
  3188. (regexp-opt
  3189. (append '("String" "Preamble")
  3190. (mapcar 'car bibtex-entry-alist))) "\\)"))))
  3191. ;; Entry commands and menus for BibTeX dialects
  3192. ;; We do not use `easy-menu-define' here because this gets confused
  3193. ;; if we want to have multiple versions of the "same" menu.
  3194. (let ((select-map (make-sparse-keymap)))
  3195. ;; Submenu for selecting the dialect
  3196. (dolist (dialect (reverse bibtex-dialect-list))
  3197. (define-key select-map (vector dialect)
  3198. `(menu-item ,(symbol-name dialect)
  3199. (lambda () (interactive) (bibtex-set-dialect ',dialect t))
  3200. :button (:radio . (eq bibtex-dialect ',dialect)))))
  3201. ;; We define a menu for each dialect.
  3202. ;; Then we select the menu we want via the :visible keyword
  3203. (dolist (dialect bibtex-dialect-list)
  3204. (let ((entry-alist (bibtex-entry-alist dialect))
  3205. (menu-map (make-sparse-keymap)))
  3206. (define-key menu-map [select]
  3207. `(menu-item "BibTeX dialect" ,select-map))
  3208. (define-key menu-map [nil-2] '(menu-item "--"))
  3209. (define-key menu-map [bibtex-preamble]
  3210. '(menu-item "Preamble" bibtex-Preamble))
  3211. (define-key menu-map [bibtex-String]
  3212. '(menu-item "String" bibtex-String))
  3213. (define-key menu-map [nil-1] '(menu-item "--"))
  3214. (dolist (elt (reverse entry-alist))
  3215. ;; Entry commands
  3216. (let* ((entry (car elt))
  3217. (fname (intern (format "bibtex-%s" entry))))
  3218. (unless (fboundp fname)
  3219. (eval (list 'defun fname nil
  3220. (format "Insert a template for a @%s entry; see also `bibtex-entry'."
  3221. entry)
  3222. '(interactive "*")
  3223. `(bibtex-entry ,entry))))
  3224. ;; Menu entries
  3225. (define-key menu-map (vector fname)
  3226. `(menu-item ,(or (nth 1 elt) (car elt)) ,fname))))
  3227. (define-key bibtex-mode-map
  3228. (vector 'menu-bar dialect)
  3229. `(menu-item "Entry-Types" ,menu-map
  3230. :visible (eq bibtex-dialect ',dialect))))))
  3231. (defun bibtex-field-list (entry-type)
  3232. "Return list of allowed fields for entry ENTRY-TYPE.
  3233. More specifically, the return value is a cons pair (REQUIRED . OPTIONAL),
  3234. where REQUIRED and OPTIONAL are lists of the required and optional field
  3235. names for ENTRY-TYPE according to `bibtex-BibTeX-entry-alist' and friends,
  3236. `bibtex-include-OPTkey', `bibtex-include-OPTcrossref',
  3237. and `bibtex-user-optional-fields'."
  3238. (let ((e-list (assoc-string entry-type bibtex-entry-alist t))
  3239. required optional)
  3240. (unless e-list
  3241. (error "Fields for BibTeX entry type %s not defined" entry-type))
  3242. (if (member-ignore-case entry-type bibtex-include-OPTcrossref)
  3243. (setq required (nth 2 e-list)
  3244. optional (append (nth 3 e-list) (nth 4 e-list)))
  3245. (setq required (append (nth 2 e-list) (nth 3 e-list))
  3246. optional (nth 4 e-list)))
  3247. (if bibtex-include-OPTkey
  3248. (push (list "key"
  3249. "Used for reference key creation if author and editor fields are missing"
  3250. (if (or (stringp bibtex-include-OPTkey)
  3251. (functionp bibtex-include-OPTkey))
  3252. bibtex-include-OPTkey))
  3253. optional))
  3254. (if (member-ignore-case entry-type bibtex-include-OPTcrossref)
  3255. (push '("crossref" "Reference key of the cross-referenced entry")
  3256. optional))
  3257. (setq optional (append optional bibtex-user-optional-fields))
  3258. (cons required optional)))
  3259. (defun bibtex-entry (entry-type)
  3260. "Insert a template for a BibTeX entry of type ENTRY-TYPE.
  3261. After insertion call the value of `bibtex-add-entry-hook' if that value
  3262. is non-nil."
  3263. (interactive
  3264. (let ((completion-ignore-case t))
  3265. (list (completing-read "Entry Type: " bibtex-entry-alist
  3266. nil t nil 'bibtex-entry-type-history))))
  3267. (let ((key (if bibtex-maintain-sorted-entries
  3268. (bibtex-read-key (format "%s key: " entry-type))))
  3269. (field-list (bibtex-field-list entry-type)))
  3270. (unless (bibtex-prepare-new-entry (list key nil entry-type))
  3271. (error "Entry with key `%s' already exists" key))
  3272. (indent-to-column bibtex-entry-offset)
  3273. (insert "@" entry-type (bibtex-entry-left-delimiter))
  3274. (if key (insert key))
  3275. (save-excursion
  3276. (mapc 'bibtex-make-field (car field-list))
  3277. (mapc 'bibtex-make-optional-field (cdr field-list))
  3278. (if bibtex-comma-after-last-field
  3279. (insert ","))
  3280. (insert "\n")
  3281. (indent-to-column bibtex-entry-offset)
  3282. (insert (bibtex-entry-right-delimiter) "\n\n"))
  3283. (bibtex-next-field t)
  3284. (if (member-ignore-case entry-type bibtex-autofill-types)
  3285. (bibtex-autofill-entry))
  3286. (run-hooks 'bibtex-add-entry-hook)))
  3287. (defun bibtex-entry-update (&optional entry-type)
  3288. "Update an existing BibTeX entry.
  3289. In the BibTeX entry at point, make new fields for those items that may occur
  3290. according to `bibtex-field-list', but are not yet present.
  3291. Also, add field delimiters to numerical fields if they are not present.
  3292. If ENTRY-TYPE is non-nil, change first the entry type to ENTRY-TYPE.
  3293. When called interactively with a prefix arg, query for a value of ENTRY-TYPE."
  3294. (interactive
  3295. (list (if current-prefix-arg
  3296. (let ((completion-ignore-case t))
  3297. (completing-read "New entry type: " bibtex-entry-alist
  3298. nil t nil 'bibtex-entry-type-history)))))
  3299. (save-excursion
  3300. (bibtex-beginning-of-entry)
  3301. (when (looking-at bibtex-entry-maybe-empty-head)
  3302. (goto-char (match-end 0))
  3303. (if entry-type
  3304. (save-excursion
  3305. (replace-match (concat "@" entry-type) nil nil nil 1))
  3306. (setq entry-type (bibtex-type-in-head)))
  3307. (let* ((field-list (bibtex-field-list entry-type))
  3308. (required (copy-tree (car field-list)))
  3309. (optional (copy-tree (cdr field-list)))
  3310. bounds)
  3311. (while (setq bounds (bibtex-parse-field))
  3312. (let ((fname (bibtex-name-in-field bounds t))
  3313. (end (copy-marker (bibtex-end-of-field bounds) t)))
  3314. (setq required (delete (assoc-string fname required t) required)
  3315. optional (delete (assoc-string fname optional t) optional))
  3316. (when (string-match "\\`[0-9]+\\'"
  3317. (bibtex-text-in-field-bounds bounds))
  3318. (goto-char (bibtex-end-of-text-in-field bounds))
  3319. (insert (bibtex-field-right-delimiter))
  3320. (goto-char (bibtex-start-of-text-in-field bounds))
  3321. (insert (bibtex-field-left-delimiter)))
  3322. (goto-char end)))
  3323. (skip-chars-backward " \t\n")
  3324. (mapc 'bibtex-make-field required)
  3325. (mapc 'bibtex-make-optional-field optional)))))
  3326. (defun bibtex-parse-entry (&optional content)
  3327. "Parse entry at point, return an alist.
  3328. The alist elements have the form (FIELD . TEXT), where FIELD can also be
  3329. the special strings \"=type=\" and \"=key=\". For the FIELD \"=key=\"
  3330. TEXT may be nil. Remove \"OPT\" and \"ALT\" from FIELD.
  3331. Move point to the end of the last field.
  3332. If optional arg CONTENT is non-nil extract content of text fields."
  3333. (let (alist bounds)
  3334. (when (looking-at bibtex-entry-maybe-empty-head)
  3335. (push (cons "=type=" (bibtex-type-in-head)) alist)
  3336. (push (cons "=key=" (bibtex-key-in-head)) alist)
  3337. (goto-char (match-end 0))
  3338. (while (setq bounds (bibtex-parse-field))
  3339. (push (cons (bibtex-name-in-field bounds t)
  3340. (bibtex-text-in-field-bounds bounds content))
  3341. alist)
  3342. (goto-char (bibtex-end-of-field bounds))))
  3343. (nreverse alist)))
  3344. (defun bibtex-autofill-entry ()
  3345. "Try to fill fields of current BibTeX entry based on neighboring entries.
  3346. The current entry must have a key. Determine the neighboring entry
  3347. \(previous or next\) whose key is more similar to the key of the current
  3348. entry. For all empty fields of the current entry insert the corresponding
  3349. field contents of the neighboring entry. Finally try to update the text
  3350. based on the difference between the keys of the neighboring and the current
  3351. entry (for example, the year parts of the keys)."
  3352. (interactive)
  3353. (bibtex-beginning-of-entry)
  3354. (when (looking-at bibtex-entry-head)
  3355. (let ((type (bibtex-type-in-head))
  3356. (key (bibtex-key-in-head))
  3357. (key-end (match-end bibtex-key-in-head))
  3358. (case-fold-search t)
  3359. (bibtex-sort-ignore-string-entries t)
  3360. tmp other-key other bounds)
  3361. ;; The fields we want to change start right after the key.
  3362. (goto-char key-end)
  3363. ;; First see whether to use the previous or the next entry
  3364. ;; for "inspiration".
  3365. (save-excursion
  3366. (goto-char (1- (match-beginning 0)))
  3367. (bibtex-beginning-of-entry)
  3368. (if (and (looking-at bibtex-entry-head)
  3369. (bibtex-string= type (bibtex-type-in-head))
  3370. ;; In case we found ourselves :-(
  3371. (not (equal key (setq tmp (bibtex-key-in-head)))))
  3372. (setq other-key tmp
  3373. other (point))))
  3374. (save-excursion
  3375. (bibtex-end-of-entry)
  3376. (bibtex-skip-to-valid-entry)
  3377. (if (and (looking-at bibtex-entry-head)
  3378. (bibtex-string= type (bibtex-type-in-head))
  3379. ;; In case we found ourselves :-(
  3380. (not (equal key (setq tmp (bibtex-key-in-head))))
  3381. (or (not other-key)
  3382. ;; Check which is the best match.
  3383. (< (length (try-completion "" (list key other-key)))
  3384. (length (try-completion "" (list key tmp))))))
  3385. (setq other-key tmp
  3386. other (point))))
  3387. ;; Then fill the new entry's fields with the chosen other entry.
  3388. (when other
  3389. (setq other (save-excursion (goto-char other) (bibtex-parse-entry)))
  3390. (setq key-end (point)) ;In case parse-entry changed the buffer.
  3391. (while (setq bounds (bibtex-parse-field))
  3392. (let ((text (assoc-string (bibtex-name-in-field bounds t)
  3393. other t)))
  3394. (if (not (and text
  3395. (equal "" (bibtex-text-in-field-bounds bounds t))))
  3396. (goto-char (bibtex-end-of-field bounds))
  3397. (goto-char (bibtex-start-of-text-in-field bounds))
  3398. (delete-region (point) (bibtex-end-of-text-in-field bounds))
  3399. (insert (cdr text)))))
  3400. ;; Finally try to update the text based on the difference between
  3401. ;; the two keys.
  3402. (let* ((prefix (try-completion "" (list key other-key)))
  3403. ;; If the keys are foo91 and foo92, don't replace 1 for 2
  3404. ;; but 91 for 92 instead.
  3405. (_ (if (string-match "[0-9]+\\'" prefix)
  3406. (setq prefix (substring prefix 0 (match-beginning 0)))))
  3407. (suffix (substring key (length prefix)))
  3408. (other-suffix (substring other-key (length prefix))))
  3409. (while (re-search-backward (regexp-quote other-suffix) key-end 'move)
  3410. (replace-match suffix)))))))
  3411. (defun bibtex-print-help-message (&optional field comma)
  3412. "Print helpful information about current FIELD in current BibTeX entry.
  3413. Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for
  3414. interactive calls."
  3415. (interactive (list nil t))
  3416. (unless field (setq field (car (bibtex-find-text-internal nil nil comma))))
  3417. (if (string-match "@" field)
  3418. (cond ((bibtex-string= field "@string")
  3419. (message "String definition"))
  3420. ((bibtex-string= field "@preamble")
  3421. (message "Preamble definition"))
  3422. (t (message "Entry key")))
  3423. (let* ((case-fold-search t)
  3424. (type (save-excursion
  3425. (bibtex-beginning-of-entry)
  3426. (looking-at bibtex-entry-maybe-empty-head)
  3427. (bibtex-type-in-head)))
  3428. (field-list (bibtex-field-list type))
  3429. (comment (assoc-string field (append (car field-list)
  3430. (cdr field-list)) t)))
  3431. (message "%s" (cond ((nth 1 comment) (nth 1 comment))
  3432. ((setq comment (assoc-string field bibtex-field-alist t))
  3433. (nth 1 comment))
  3434. (t "No comment available"))))))
  3435. (defun bibtex-make-field (field &optional move interactive nodelim)
  3436. "Make a field named FIELD in current BibTeX entry.
  3437. FIELD is either a string or a list of the form
  3438. \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG) as in
  3439. `bibtex-BibTeX-entry-alist' and friends.
  3440. If MOVE is non-nil, move point past the present field before making
  3441. the new field. If INTERACTIVE is non-nil, move point to the end of
  3442. the new field. Otherwise move point past the new field.
  3443. MOVE and INTERACTIVE are t when called interactively.
  3444. INIT is surrounded by field delimiters, unless NODELIM is non-nil."
  3445. (interactive
  3446. (list (let ((completion-ignore-case t)
  3447. (field-list (bibtex-field-list
  3448. (save-excursion
  3449. (bibtex-beginning-of-entry)
  3450. (looking-at bibtex-any-entry-maybe-empty-head)
  3451. (bibtex-type-in-head)))))
  3452. (completing-read "BibTeX field name: "
  3453. (append (car field-list) (cdr field-list))
  3454. nil nil nil bibtex-field-history))
  3455. t t))
  3456. (unless (consp field)
  3457. (setq field (list field)))
  3458. (when move
  3459. (bibtex-find-text)
  3460. (if (looking-at "[}\"]")
  3461. (forward-char)))
  3462. (insert ",\n")
  3463. (indent-to-column (+ bibtex-entry-offset bibtex-field-indentation))
  3464. ;; If there are multiple sets of alternatives, we could use
  3465. ;; the numeric value of (nth 3 field) to number these sets. Useful??
  3466. (if (nth 3 field) (insert "ALT"))
  3467. (insert (car field) " ")
  3468. (if bibtex-align-at-equal-sign
  3469. (indent-to-column (+ bibtex-entry-offset
  3470. (- bibtex-text-indentation 2))))
  3471. (insert "= ")
  3472. (unless bibtex-align-at-equal-sign
  3473. (indent-to-column (+ bibtex-entry-offset
  3474. bibtex-text-indentation)))
  3475. (let ((init (nth 2 field)))
  3476. (if (not init) (setq init "")
  3477. (if (functionp init) (setq init (funcall init)))
  3478. (unless (stringp init) (error "`%s' is not a string" init)))
  3479. ;; NODELIM is required by `bibtex-insert-kill'
  3480. (if nodelim (insert init)
  3481. (insert (bibtex-field-left-delimiter) init
  3482. (bibtex-field-right-delimiter))))
  3483. (when interactive
  3484. ;; (bibtex-find-text nil nil bibtex-help-message)
  3485. (if (memq (preceding-char) '(?} ?\")) (forward-char -1))
  3486. (if bibtex-help-message (bibtex-print-help-message (car field)))))
  3487. (defun bibtex-beginning-of-entry ()
  3488. "Move to beginning of BibTeX entry (beginning of line).
  3489. If inside an entry, move to the beginning of it, otherwise move to the
  3490. beginning of the previous entry. If point is ahead of all BibTeX entries
  3491. move point to the beginning of buffer. Return the new location of point."
  3492. (interactive)
  3493. (skip-chars-forward " \t")
  3494. (if (looking-at "@")
  3495. (forward-char))
  3496. (re-search-backward "^[ \t]*@" nil 'move)
  3497. (point))
  3498. (defun bibtex-end-of-entry ()
  3499. "Move to end of BibTeX entry (past the closing brace).
  3500. If inside an entry, move to the end of it, otherwise move to the end
  3501. of the previous entry. Do not move if ahead of first entry.
  3502. Return the new location of point."
  3503. (interactive)
  3504. (let ((case-fold-search t)
  3505. (pnt (point))
  3506. (_ (bibtex-beginning-of-entry))
  3507. (bounds (bibtex-valid-entry t)))
  3508. (cond (bounds (goto-char (cdr bounds))) ; regular entry
  3509. ;; @String or @Preamble
  3510. ((setq bounds (or (bibtex-parse-string t) (bibtex-parse-preamble)))
  3511. (goto-char (bibtex-end-of-string bounds)))
  3512. ((looking-at bibtex-any-valid-entry-type)
  3513. ;; Parsing of entry failed
  3514. (error "Syntactically incorrect BibTeX entry starts here"))
  3515. (t (if (called-interactively-p 'interactive)
  3516. (message "Not on a known BibTeX entry."))
  3517. (goto-char pnt)))
  3518. (point)))
  3519. (defun bibtex-goto-line (arg)
  3520. "Goto line ARG, counting from beginning of (narrowed) buffer."
  3521. ;; code adapted from `goto-line'
  3522. (goto-char (point-min))
  3523. (if (eq selective-display t)
  3524. (re-search-forward "[\n\C-m]" nil 'end (1- arg))
  3525. (forward-line (1- arg))))
  3526. (defun bibtex-reposition-window ()
  3527. "Make the current BibTeX entry visible.
  3528. If entry is smaller than `window-body-height', entry is centered in window.
  3529. Otherwise display the beginning of entry."
  3530. (interactive)
  3531. (let ((pnt (point))
  3532. (beg (line-number-at-pos (bibtex-beginning-of-entry)))
  3533. (end (line-number-at-pos (bibtex-end-of-entry))))
  3534. (if (> (window-body-height) (- end beg))
  3535. ;; entry fits in current window
  3536. (progn
  3537. (bibtex-goto-line (/ (+ 1 beg end) 2))
  3538. (recenter)
  3539. (goto-char pnt))
  3540. ;; entry too large for current window
  3541. (bibtex-goto-line beg)
  3542. (recenter 0)
  3543. (if (> (1+ (- (line-number-at-pos pnt) beg))
  3544. (window-body-height))
  3545. (bibtex-goto-line beg)
  3546. (goto-char pnt)))))
  3547. (defun bibtex-mark-entry ()
  3548. "Put mark at beginning, point at end of current BibTeX entry."
  3549. (interactive)
  3550. (push-mark (bibtex-beginning-of-entry))
  3551. (bibtex-end-of-entry))
  3552. (defun bibtex-count-entries (&optional count-string-entries)
  3553. "Count number of entries in current buffer or region.
  3554. With prefix argument COUNT-STRING-ENTRIES count all entries,
  3555. otherwise count all entries except @String entries.
  3556. If mark is active count entries in region, if not in whole buffer."
  3557. (interactive "P")
  3558. (let ((number 0)
  3559. (bibtex-sort-ignore-string-entries (not count-string-entries)))
  3560. (save-restriction
  3561. (if mark-active (narrow-to-region (region-beginning) (region-end)))
  3562. (bibtex-map-entries (lambda (_key _beg _end) (setq number (1+ number)))))
  3563. (message "%s contains %d entries."
  3564. (if mark-active "Region" "Buffer")
  3565. number)))
  3566. (defun bibtex-ispell-entry ()
  3567. "Check BibTeX entry for spelling errors."
  3568. (interactive)
  3569. (ispell-region (save-excursion (bibtex-beginning-of-entry))
  3570. (save-excursion (bibtex-end-of-entry))))
  3571. (defun bibtex-ispell-abstract ()
  3572. "Check abstract of BibTeX entry for spelling errors."
  3573. (interactive)
  3574. (let ((bounds (save-excursion
  3575. (bibtex-beginning-of-entry)
  3576. (bibtex-search-forward-field "abstract" t))))
  3577. (if bounds
  3578. (ispell-region (bibtex-start-of-text-in-field bounds)
  3579. (bibtex-end-of-text-in-field bounds))
  3580. (error "No abstract in entry"))))
  3581. (defun bibtex-narrow-to-entry ()
  3582. "Narrow buffer to current BibTeX entry."
  3583. (interactive)
  3584. (save-excursion
  3585. (widen)
  3586. (narrow-to-region (bibtex-beginning-of-entry)
  3587. (bibtex-end-of-entry))))
  3588. (defun bibtex-entry-index ()
  3589. "Return index of BibTeX entry head at or past position of point.
  3590. The index is a list (KEY CROSSREF-KEY ENTRY-TYPE) that is used for sorting
  3591. the entries of the BibTeX buffer. CROSSREF-KEY is nil unless the value
  3592. of `bibtex-maintain-sorted-entries' is `crossref'. Move point to the end
  3593. of the head of the entry found. Return nil if no entry found."
  3594. (let ((case-fold-search t))
  3595. (if (re-search-forward bibtex-entry-maybe-empty-head nil t)
  3596. (let ((key (bibtex-key-in-head))
  3597. ;; all entry types should be downcase (for ease of comparison)
  3598. (entry-type (downcase (bibtex-type-in-head))))
  3599. ;; Don't search CROSSREF-KEY if we don't need it.
  3600. (if (eq bibtex-maintain-sorted-entries 'crossref)
  3601. (let ((bounds (bibtex-search-forward-field
  3602. "\\(OPT\\)?crossref" t)))
  3603. (list key
  3604. (if bounds (bibtex-text-in-field-bounds bounds t))
  3605. entry-type))
  3606. (list key nil entry-type))))))
  3607. (defun bibtex-init-sort-entry-class-alist ()
  3608. "Initialize `bibtex-sort-entry-class-alist' (buffer-local)."
  3609. (unless (local-variable-p 'bibtex-sort-entry-class-alist)
  3610. (set (make-local-variable 'bibtex-sort-entry-class-alist)
  3611. (let ((i -1) alist)
  3612. (dolist (class bibtex-sort-entry-class)
  3613. (setq i (1+ i))
  3614. (dolist (entry class)
  3615. ;; All entry types should be downcase (for ease of comparison).
  3616. (push (cons (if (stringp entry) (downcase entry) entry) i)
  3617. alist)))
  3618. alist))))
  3619. (defun bibtex-lessp (index1 index2)
  3620. "Predicate for sorting BibTeX entries with indices INDEX1 and INDEX2.
  3621. Each index is a list (KEY CROSSREF-KEY ENTRY-TYPE).
  3622. The predicate depends on the variable `bibtex-maintain-sorted-entries'.
  3623. If its value is nil use plain sorting."
  3624. (cond ((not index1) (not index2)) ; indices can be nil
  3625. ((not index2) nil)
  3626. ((eq bibtex-maintain-sorted-entries 'crossref)
  3627. ;; CROSSREF-KEY may be nil or it can point to an entry
  3628. ;; in another BibTeX file. In both cases we ignore CROSSREF-KEY.
  3629. (if (and (nth 1 index1)
  3630. (cdr (assoc-string (nth 1 index1) bibtex-reference-keys)))
  3631. (if (and (nth 1 index2)
  3632. (cdr (assoc-string (nth 1 index2) bibtex-reference-keys)))
  3633. (or (string-lessp (nth 1 index1) (nth 1 index2))
  3634. (and (string-equal (nth 1 index1) (nth 1 index2))
  3635. (string-lessp (nth 0 index1) (nth 0 index2))))
  3636. (not (string-lessp (nth 0 index2) (nth 1 index1))))
  3637. (if (and (nth 1 index2)
  3638. (cdr (assoc-string (nth 1 index2) bibtex-reference-keys)))
  3639. (string-lessp (nth 0 index1) (nth 1 index2))
  3640. (string-lessp (nth 0 index1) (nth 0 index2)))))
  3641. ((eq bibtex-maintain-sorted-entries 'entry-class)
  3642. (let ((n1 (cdr (or (assoc (nth 2 index1) bibtex-sort-entry-class-alist)
  3643. (assoc 'catch-all bibtex-sort-entry-class-alist)
  3644. '(nil . 1000)))) ; if there is nothing else
  3645. (n2 (cdr (or (assoc (nth 2 index2) bibtex-sort-entry-class-alist)
  3646. (assoc 'catch-all bibtex-sort-entry-class-alist)
  3647. '(nil . 1000))))) ; if there is nothing else
  3648. (or (< n1 n2)
  3649. (and (= n1 n2)
  3650. (string-lessp (car index1) (car index2))))))
  3651. (t ; (eq bibtex-maintain-sorted-entries 'plain)
  3652. (string-lessp (car index1) (car index2)))))
  3653. (defun bibtex-sort-buffer ()
  3654. "Sort BibTeX buffer alphabetically by key.
  3655. The predicate for sorting is defined via `bibtex-maintain-sorted-entries'.
  3656. If its value is nil use plain sorting. Text outside of BibTeX entries is not
  3657. affected. If `bibtex-sort-ignore-string-entries' is non-nil, @String entries
  3658. are ignored."
  3659. (interactive)
  3660. (bibtex-beginning-of-first-entry) ; Needed by `sort-subr'
  3661. (bibtex-init-sort-entry-class-alist) ; Needed by `bibtex-lessp'.
  3662. (if (and (eq bibtex-maintain-sorted-entries 'crossref)
  3663. (functionp bibtex-reference-keys))
  3664. (bibtex-parse-keys)) ; Needed by `bibtex-lessp'.
  3665. (sort-subr nil
  3666. 'bibtex-skip-to-valid-entry ; NEXTREC function
  3667. 'bibtex-end-of-entry ; ENDREC function
  3668. 'bibtex-entry-index ; STARTKEY function
  3669. nil ; ENDKEY function
  3670. 'bibtex-lessp)) ; PREDICATE
  3671. (defun bibtex-search-crossref (crossref-key &optional pnt split noerror)
  3672. "Move point to the beginning of BibTeX entry CROSSREF-KEY.
  3673. If `bibtex-files' is non-nil, search all these files.
  3674. Otherwise the search is limited to the current buffer.
  3675. Return position of entry if CROSSREF-KEY is found or nil otherwise.
  3676. If CROSSREF-KEY is in the same buffer like current entry but before it
  3677. an error is signaled. If NOERROR is non-nil this error is suppressed.
  3678. Optional arg PNT is the position of the referencing entry. It defaults
  3679. to position of point. If optional arg SPLIT is non-nil, split window
  3680. so that both the referencing and the crossrefed entry are displayed.
  3681. If called interactively, CROSSREF-KEY defaults to either the crossref key
  3682. of current entry or a key matched by `bibtex-cite-matcher-alist',
  3683. whatever is nearer to the position of point. SPLIT is t. NOERROR is nil
  3684. for a crossref key, t otherwise."
  3685. (interactive
  3686. (save-excursion
  3687. (let* ((pnt (point))
  3688. (_ (bibtex-beginning-of-entry))
  3689. (end (cdr (bibtex-valid-entry t)))
  3690. (_ (unless end (error "Not inside valid entry")))
  3691. (beg (match-end 0)) ; set by `bibtex-valid-entry'
  3692. (bounds (bibtex-search-forward-field "\\(OPT\\)?crossref" end))
  3693. case-fold-search best temp crossref-key)
  3694. (if bounds
  3695. (setq crossref-key (bibtex-text-in-field-bounds bounds t)
  3696. best (cons (bibtex-dist pnt (bibtex-end-of-field bounds)
  3697. (bibtex-start-of-field bounds))
  3698. crossref-key)))
  3699. (dolist (matcher bibtex-cite-matcher-alist)
  3700. (goto-char beg)
  3701. (while (re-search-forward (car matcher) end t)
  3702. (setq temp (bibtex-dist pnt (match-end (cdr matcher))
  3703. (match-beginning (cdr matcher))))
  3704. ;; Accept the key closest to the position of point.
  3705. (if (or (not best) (< temp (car best)))
  3706. (setq best (cons temp (match-string-no-properties
  3707. (cdr matcher)))))))
  3708. (goto-char pnt)
  3709. (setq temp (bibtex-read-key "Find crossref key: " (cdr best) t))
  3710. (list temp (point) t (not (and crossref-key
  3711. (string= temp crossref-key)))))))
  3712. (let (buffer pos eqb)
  3713. (save-excursion
  3714. (setq pos (bibtex-search-entry crossref-key t)
  3715. buffer (current-buffer)))
  3716. (setq eqb (eq buffer (current-buffer)))
  3717. (cond ((not pos)
  3718. (if split (message "Crossref key `%s' not found" crossref-key)))
  3719. (split ; called (quasi) interactively
  3720. (unless pnt (setq pnt (point)))
  3721. (goto-char pnt)
  3722. (if (and eqb (= pos (save-excursion (bibtex-beginning-of-entry))))
  3723. (message "Key `%s' is current entry" crossref-key)
  3724. (if eqb (select-window (split-window))
  3725. (pop-to-buffer buffer))
  3726. (goto-char pos)
  3727. (bibtex-reposition-window)
  3728. (beginning-of-line)
  3729. (if (and eqb (> pnt pos) (not noerror))
  3730. (error "The referencing entry must precede the crossrefed entry!"))))
  3731. ;; `bibtex-search-crossref' is called noninteractively during
  3732. ;; clean-up of an entry. Then it is not possible to check
  3733. ;; whether the current entry and the crossrefed entry have
  3734. ;; the correct sorting order.
  3735. (eqb (goto-char pos))
  3736. (t (set-buffer buffer) (goto-char pos)))
  3737. pos))
  3738. ;; backward compatibility
  3739. (defalias 'bibtex-find-crossref 'bibtex-search-crossref)
  3740. (defun bibtex-dist (pos beg end)
  3741. "Return distance between POS and region delimited by BEG and END."
  3742. (cond ((and (<= beg pos) (<= pos end)) 0)
  3743. ((< pos beg) (- beg pos))
  3744. (t (- pos end))))
  3745. ;;;###autoload
  3746. (defun bibtex-search-entry (key &optional global start display)
  3747. "Move point to the beginning of BibTeX entry named KEY.
  3748. Return position of entry if KEY is found or nil if not found.
  3749. With GLOBAL non-nil, search KEY in `bibtex-files'. Otherwise the search
  3750. is limited to the current buffer. Optional arg START is buffer position
  3751. where the search starts. If it is nil, start search at beginning of buffer.
  3752. If DISPLAY is non-nil, display the buffer containing KEY.
  3753. Otherwise, use `set-buffer'.
  3754. When called interactively, START is nil, DISPLAY is t.
  3755. Also, GLOBAL is t if the current mode is not `bibtex-mode'
  3756. or `bibtex-search-entry-globally' is non-nil.
  3757. A prefix arg negates the value of `bibtex-search-entry-globally'."
  3758. (interactive
  3759. (let ((global (or (not (eq major-mode 'bibtex-mode))
  3760. (if bibtex-search-entry-globally
  3761. (not current-prefix-arg)
  3762. current-prefix-arg))))
  3763. (list (bibtex-read-key "Find key: " nil global) global nil t)))
  3764. (if (and global bibtex-files)
  3765. (let ((buffer-list (bibtex-initialize t))
  3766. buffer found)
  3767. (while (and (not found)
  3768. (setq buffer (pop buffer-list)))
  3769. (with-current-buffer buffer
  3770. (if (cdr (assoc-string key bibtex-reference-keys))
  3771. ;; `bibtex-search-entry' moves point if key found
  3772. (setq found (bibtex-search-entry key)))))
  3773. (cond ((and found display)
  3774. (switch-to-buffer buffer)
  3775. (bibtex-reposition-window))
  3776. (found (set-buffer buffer))
  3777. (display (message "Key `%s' not found" key)))
  3778. found)
  3779. (let* ((case-fold-search t)
  3780. (pnt (save-excursion
  3781. (goto-char (or start (point-min)))
  3782. (if (re-search-forward (concat "^[ \t]*\\("
  3783. bibtex-entry-type
  3784. "\\)[ \t]*[({][ \t\n]*\\("
  3785. (regexp-quote key)
  3786. "\\)[ \t\n]*[,=]")
  3787. nil t)
  3788. (match-beginning 0)))))
  3789. (cond (pnt
  3790. (goto-char pnt)
  3791. (if display (bibtex-reposition-window)))
  3792. (display (message "Key `%s' not found" key)))
  3793. pnt)))
  3794. ;; backward compatibility
  3795. (defalias 'bibtex-find-entry 'bibtex-search-entry)
  3796. (defun bibtex-prepare-new-entry (index)
  3797. "Prepare a new BibTeX entry with index INDEX.
  3798. INDEX is a list (KEY CROSSREF-KEY ENTRY-TYPE).
  3799. Move point where the entry KEY should be placed.
  3800. If `bibtex-maintain-sorted-entries' is non-nil, perform a binary
  3801. search to look for place for KEY. This requires that buffer is sorted,
  3802. see `bibtex-validate'.
  3803. Return t if preparation was successful or nil if entry KEY already exists."
  3804. (bibtex-init-sort-entry-class-alist) ; Needed by `bibtex-lessp'.
  3805. (if (and (eq bibtex-maintain-sorted-entries 'crossref)
  3806. (functionp bibtex-reference-keys))
  3807. (bibtex-parse-keys)) ; Needed by `bibtex-lessp'.
  3808. (let ((key (nth 0 index))
  3809. key-exist)
  3810. (cond ((or (null key)
  3811. (and (stringp key)
  3812. (string-equal key ""))
  3813. (and (not (setq key-exist (bibtex-search-entry key)))
  3814. (not bibtex-maintain-sorted-entries)))
  3815. (bibtex-move-outside-of-entry))
  3816. ;; if key-exist is non-nil due to the previous cond clause
  3817. ;; then point will be at beginning of entry named key.
  3818. (key-exist)
  3819. (t ; `bibtex-maintain-sorted-entries' is non-nil
  3820. (let* ((case-fold-search t)
  3821. (left (save-excursion (bibtex-beginning-of-first-entry)))
  3822. (bounds (save-excursion (goto-char (point-max))
  3823. (bibtex-skip-to-valid-entry t)))
  3824. (right (if bounds (cdr bounds) (point-min)))
  3825. (found (if (>= left right) left))
  3826. actual-index new)
  3827. (save-excursion
  3828. ;; Binary search
  3829. (while (not found)
  3830. (goto-char (/ (+ left right) 2))
  3831. (bibtex-skip-to-valid-entry t)
  3832. (setq actual-index (bibtex-entry-index))
  3833. (cond ((bibtex-lessp index actual-index)
  3834. (setq new (bibtex-beginning-of-entry))
  3835. (if (equal right new)
  3836. (setq found right)
  3837. (setq right new)))
  3838. (t
  3839. (bibtex-end-of-entry)
  3840. (bibtex-skip-to-valid-entry)
  3841. (setq new (point))
  3842. (if (equal left new)
  3843. (setq found right)
  3844. (setq left new))))))
  3845. (goto-char found)
  3846. (bibtex-beginning-of-entry)
  3847. (setq actual-index (save-excursion (bibtex-entry-index)))
  3848. (when (or (not actual-index)
  3849. (bibtex-lessp actual-index index))
  3850. ;; buffer contains no valid entries or
  3851. ;; greater than last entry --> append
  3852. (bibtex-end-of-entry)
  3853. (unless (bobp) (newline (forward-line 2)))
  3854. (beginning-of-line)))))
  3855. (unless key-exist t)))
  3856. (defun bibtex-validate (&optional test-thoroughly)
  3857. "Validate if buffer or region is syntactically correct.
  3858. Check also for duplicate keys and correct sort order provided
  3859. `bibtex-maintain-sorted-entries' is non-nil.
  3860. With optional argument TEST-THOROUGHLY non-nil check also for
  3861. the absence of required fields and for questionable month fields.
  3862. If mark is active, validate current region, if not the whole buffer.
  3863. Only check known entry types, so you can put comments outside of entries.
  3864. Return t if test was successful, nil otherwise."
  3865. (interactive "P")
  3866. (let* ((case-fold-search t)
  3867. error-list syntax-error)
  3868. (save-excursion
  3869. (save-restriction
  3870. (if mark-active (narrow-to-region (region-beginning) (region-end)))
  3871. ;; Check syntactical structure of entries
  3872. (goto-char (point-min))
  3873. (bibtex-progress-message "Checking syntactical structure")
  3874. (let (bounds end)
  3875. (while (setq end (re-search-forward "^[ \t]*@" nil t))
  3876. (bibtex-progress-message)
  3877. (goto-char (match-beginning 0))
  3878. (cond ((setq bounds (bibtex-valid-entry))
  3879. (goto-char (cdr bounds)))
  3880. ((setq bounds (or (bibtex-parse-string)
  3881. (bibtex-parse-preamble)))
  3882. (goto-char (bibtex-end-of-string bounds)))
  3883. ((looking-at bibtex-any-valid-entry-type)
  3884. (push (cons (bibtex-current-line)
  3885. "Syntax error (check esp. commas, braces, and quotes)")
  3886. error-list)
  3887. (goto-char (match-end 0)))
  3888. (t (goto-char end)))))
  3889. (bibtex-progress-message 'done)
  3890. (if error-list
  3891. ;; Continue only if there were no syntax errors.
  3892. (setq syntax-error t)
  3893. ;; Check for duplicate keys and correct sort order
  3894. (bibtex-init-sort-entry-class-alist) ; Needed by `bibtex-lessp'.
  3895. (bibtex-parse-keys) ; Possibly needed by `bibtex-lessp'.
  3896. ; Always needed by subsequent global key check.
  3897. (let (previous current key-list)
  3898. (bibtex-progress-message "Checking for duplicate keys")
  3899. (bibtex-map-entries
  3900. (lambda (key _beg _end)
  3901. (bibtex-progress-message)
  3902. (setq current (bibtex-entry-index))
  3903. (cond ((not previous))
  3904. ((member key key-list)
  3905. (push (cons (bibtex-current-line)
  3906. (format "Duplicate key `%s'" key))
  3907. error-list))
  3908. ((and bibtex-maintain-sorted-entries
  3909. (not (bibtex-lessp previous current)))
  3910. (push (cons (bibtex-current-line)
  3911. "Entries out of order")
  3912. error-list)))
  3913. (push key key-list)
  3914. (setq previous current)))
  3915. (bibtex-progress-message 'done))
  3916. ;; Check for duplicate keys in `bibtex-files'.
  3917. ;; `bibtex-validate' only compares keys in current buffer with keys
  3918. ;; in `bibtex-files'. `bibtex-validate-globally' compares keys for
  3919. ;; each file in `bibtex-files' with keys of all other files in
  3920. ;; `bibtex-files'.
  3921. ;; We don't want to be fooled by outdated `bibtex-reference-keys'.
  3922. (dolist (buffer (bibtex-initialize nil t))
  3923. (dolist (key (with-current-buffer buffer bibtex-reference-keys))
  3924. (when (and (cdr key)
  3925. (cdr (assoc-string (car key) bibtex-reference-keys)))
  3926. (bibtex-search-entry (car key))
  3927. (push (cons (bibtex-current-line)
  3928. (format "Duplicate key `%s' in %s" (car key)
  3929. (abbreviate-file-name (buffer-file-name buffer))))
  3930. error-list))))
  3931. (when test-thoroughly
  3932. (bibtex-progress-message
  3933. "Checking required fields and month fields")
  3934. (let ((bibtex-sort-ignore-string-entries t))
  3935. (bibtex-map-entries
  3936. (lambda (_key beg end)
  3937. (bibtex-progress-message)
  3938. (bibtex-beginning-first-field beg)
  3939. (let* ((beg-line (save-excursion (goto-char beg)
  3940. (bibtex-current-line)))
  3941. (entry-list (assoc-string (bibtex-type-in-head)
  3942. bibtex-entry-alist t))
  3943. (crossref (bibtex-search-forward-field "crossref" end))
  3944. (req (if crossref (copy-sequence (nth 2 entry-list))
  3945. (append (nth 2 entry-list)
  3946. (copy-sequence (nth 3 entry-list)))))
  3947. (num-alt (length (delq nil (delete-dups
  3948. (mapcar (lambda (x) (nth 3 x))
  3949. req)))))
  3950. (alt-fields (make-vector num-alt nil))
  3951. bounds field idx)
  3952. (while (setq bounds (bibtex-parse-field))
  3953. (let ((field-name (bibtex-name-in-field bounds)))
  3954. (if (and (bibtex-string= field-name "month")
  3955. ;; Check only abbreviated month fields.
  3956. (let ((month (bibtex-text-in-field-bounds bounds)))
  3957. (not (or (string-match "\\`[\"{].+[\"}]\\'" month)
  3958. (assoc-string
  3959. month
  3960. bibtex-predefined-month-strings t)))))
  3961. (push (cons (bibtex-current-line)
  3962. "Questionable month field")
  3963. error-list))
  3964. (setq field (assoc-string field-name req t)
  3965. req (delete field req))
  3966. (if (setq idx (nth 3 field))
  3967. (if (aref alt-fields idx)
  3968. (push (cons (bibtex-current-line)
  3969. "More than one non-empty alternative")
  3970. error-list)
  3971. (aset alt-fields idx t))))
  3972. (goto-char (bibtex-end-of-field bounds)))
  3973. (let ((alt-expect (make-vector num-alt nil)))
  3974. (dolist (field req) ; absent required fields
  3975. (if (setq idx (nth 3 field))
  3976. (bibtex-vec-push alt-expect idx (car field))
  3977. (push (cons beg-line
  3978. (format "Required field `%s' missing"
  3979. (car field)))
  3980. error-list)))
  3981. (dotimes (idx num-alt)
  3982. (unless (aref alt-fields idx)
  3983. (push (cons beg-line
  3984. (format "Alternative fields `%s' missing"
  3985. (aref alt-expect idx)))
  3986. error-list))))))))
  3987. (bibtex-progress-message 'done)))))
  3988. (if error-list
  3989. (let ((file (file-name-nondirectory (buffer-file-name)))
  3990. (dir default-directory)
  3991. (err-buf "*BibTeX validation errors*"))
  3992. (setq error-list (sort error-list 'car-less-than-car))
  3993. (with-current-buffer (get-buffer-create err-buf)
  3994. (setq default-directory dir)
  3995. (unless (eq major-mode 'compilation-mode) (compilation-mode))
  3996. (let ((inhibit-read-only t))
  3997. (delete-region (point-min) (point-max))
  3998. (insert "BibTeX mode command `bibtex-validate'\n"
  3999. (if syntax-error
  4000. "Maybe undetected errors due to syntax errors. \
  4001. Correct and validate again.\n"
  4002. "\n"))
  4003. (dolist (err error-list)
  4004. (insert (format "%s:%d: %s\n" file (car err) (cdr err))))
  4005. (set-buffer-modified-p nil))
  4006. (goto-char (point-min))
  4007. (forward-line 2)) ; first error message
  4008. (display-buffer err-buf)
  4009. nil) ; return `nil' (i.e., buffer is invalid)
  4010. (message "%s is syntactically correct"
  4011. (if mark-active "Region" "Buffer"))
  4012. t))) ; return `t' (i.e., buffer is valid)
  4013. (defun bibtex-validate-globally (&optional strings)
  4014. "Check for duplicate keys in `bibtex-files'.
  4015. With optional prefix arg STRINGS, check for duplicate strings, too.
  4016. Return t if test was successful, nil otherwise."
  4017. (interactive "P")
  4018. (let ((buffer-list (bibtex-initialize t))
  4019. buffer-key-list current-buf current-keys error-list)
  4020. ;; Check for duplicate keys within BibTeX buffer
  4021. (dolist (buffer buffer-list)
  4022. (with-current-buffer buffer
  4023. (save-excursion
  4024. (let (entry-type key key-list)
  4025. (goto-char (point-min))
  4026. (while (re-search-forward bibtex-entry-head nil t)
  4027. (setq entry-type (bibtex-type-in-head)
  4028. key (bibtex-key-in-head))
  4029. (if (or (and strings (bibtex-string= entry-type "string"))
  4030. (assoc-string entry-type bibtex-entry-alist t))
  4031. (if (member key key-list)
  4032. (push (format "%s:%d: Duplicate key `%s'\n"
  4033. (buffer-file-name)
  4034. (bibtex-current-line) key)
  4035. error-list)
  4036. (push key key-list))))
  4037. (push (cons buffer key-list) buffer-key-list)))))
  4038. ;; Check for duplicate keys among BibTeX buffers
  4039. (while (setq current-buf (pop buffer-list))
  4040. (setq current-keys (cdr (assq current-buf buffer-key-list)))
  4041. (with-current-buffer current-buf
  4042. (dolist (buffer buffer-list)
  4043. (dolist (key (cdr (assq buffer buffer-key-list)))
  4044. (when (assoc-string key current-keys)
  4045. (bibtex-search-entry key)
  4046. (push (format "%s:%d: Duplicate key `%s' in %s\n"
  4047. (buffer-file-name) (bibtex-current-line) key
  4048. (abbreviate-file-name (buffer-file-name buffer)))
  4049. error-list))))))
  4050. ;; Process error list
  4051. (if error-list
  4052. (let ((err-buf "*BibTeX validation errors*"))
  4053. (with-current-buffer (get-buffer-create err-buf)
  4054. (unless (eq major-mode 'compilation-mode) (compilation-mode))
  4055. (let ((inhibit-read-only t))
  4056. (delete-region (point-min) (point-max))
  4057. (insert "BibTeX mode command `bibtex-validate-globally'\n\n")
  4058. (dolist (err (sort error-list 'string-lessp)) (insert err))
  4059. (set-buffer-modified-p nil))
  4060. (goto-char (point-min))
  4061. (forward-line 2)) ; first error message
  4062. (display-buffer err-buf)
  4063. nil) ; return `nil' (i.e., buffer is invalid)
  4064. (message "No duplicate keys.")
  4065. t))) ; return `t' (i.e., buffer is valid)
  4066. (defun bibtex-next-field (begin &optional comma)
  4067. "Move point to end of text of next BibTeX field or entry head.
  4068. With prefix BEGIN non-nil, move point to its beginning. Optional arg COMMA
  4069. is as in `bibtex-enclosing-field'. It is t for interactive calls."
  4070. (interactive (list current-prefix-arg t))
  4071. (let ((bounds (bibtex-find-text-internal t nil comma))
  4072. end-of-entry)
  4073. (if (not bounds)
  4074. (setq end-of-entry t)
  4075. (goto-char (nth 3 bounds))
  4076. (if (assoc-string (car bounds) '("@String" "@Preamble") t)
  4077. (setq end-of-entry t)
  4078. ;; BibTeX key or field
  4079. (if (looking-at ",[ \t\n]*") (goto-char (match-end 0)))
  4080. ;; end of entry
  4081. (if (looking-at "[)}][ \t\n]*") (setq end-of-entry t))))
  4082. (if (and end-of-entry
  4083. (re-search-forward bibtex-any-entry-maybe-empty-head nil t))
  4084. (goto-char (match-beginning 0)))
  4085. (bibtex-find-text begin nil bibtex-help-message)))
  4086. (defun bibtex-find-text (&optional begin noerror help comma)
  4087. "Move point to end of text of current BibTeX field or entry head.
  4088. With optional prefix BEGIN non-nil, move point to its beginning.
  4089. Unless NOERROR is non-nil, an error is signaled if point is not
  4090. on a BibTeX field. If optional arg HELP is non-nil print help message.
  4091. When called interactively, the value of HELP is `bibtex-help-message'.
  4092. Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for
  4093. interactive calls."
  4094. (interactive (list current-prefix-arg nil bibtex-help-message t))
  4095. (let ((bounds (bibtex-find-text-internal t nil comma)))
  4096. (cond (bounds
  4097. (if begin
  4098. (progn (goto-char (nth 1 bounds))
  4099. (if (looking-at "[{\"]")
  4100. (forward-char)))
  4101. (goto-char (nth 2 bounds))
  4102. (if (memq (preceding-char) '(?} ?\"))
  4103. (forward-char -1)))
  4104. (if help (bibtex-print-help-message (car bounds))))
  4105. ((not noerror) (error "Not on BibTeX field")))))
  4106. (defun bibtex-find-text-internal (&optional noerror subfield comma)
  4107. "Find text part of current BibTeX field or entry head.
  4108. Return list (NAME START-TEXT END-TEXT END STRING-CONST) with field name
  4109. or entry type, start and end of text, and end of field or entry head.
  4110. STRING-CONST is a flag which is non-nil if current subfield delimited by #
  4111. is a BibTeX string constant. Return value is nil if field or entry head
  4112. are not found.
  4113. If optional arg NOERROR is non-nil, an error message is suppressed
  4114. if text is not found. If optional arg SUBFIELD is non-nil START-TEXT
  4115. and END-TEXT correspond to the current subfield delimited by #.
  4116. Optional arg COMMA is as in `bibtex-enclosing-field'."
  4117. (save-excursion
  4118. (let ((pnt (point))
  4119. (bounds (bibtex-enclosing-field comma t))
  4120. (case-fold-search t)
  4121. name start-text end-text end failure done no-sub string-const)
  4122. (bibtex-beginning-of-entry)
  4123. (cond (bounds
  4124. (setq name (bibtex-name-in-field bounds t)
  4125. start-text (bibtex-start-of-text-in-field bounds)
  4126. end-text (bibtex-end-of-text-in-field bounds)
  4127. end (bibtex-end-of-field bounds)))
  4128. ;; @String
  4129. ((setq bounds (bibtex-parse-string t))
  4130. (if (<= pnt (bibtex-end-of-string bounds))
  4131. (setq name "@String" ;; not a field name!
  4132. start-text (bibtex-start-of-text-in-string bounds)
  4133. end-text (bibtex-end-of-text-in-string bounds)
  4134. end (bibtex-end-of-string bounds))
  4135. (setq failure t)))
  4136. ;; @Preamble
  4137. ((setq bounds (bibtex-parse-preamble))
  4138. (if (<= pnt (bibtex-end-of-string bounds))
  4139. (setq name "@Preamble" ;; not a field name!
  4140. start-text (bibtex-start-of-text-in-string bounds)
  4141. end-text (bibtex-end-of-text-in-string bounds)
  4142. end (bibtex-end-of-string bounds))
  4143. (setq failure t)))
  4144. ;; BibTeX head
  4145. ((looking-at bibtex-entry-maybe-empty-head)
  4146. (goto-char (match-end 0))
  4147. (if comma (save-match-data
  4148. (re-search-forward "\\=[ \t\n]*," nil t)))
  4149. (if (<= pnt (point))
  4150. (setq name (match-string-no-properties bibtex-type-in-head)
  4151. start-text (or (match-beginning bibtex-key-in-head)
  4152. (match-end 0))
  4153. end-text (or (match-end bibtex-key-in-head)
  4154. (match-end 0))
  4155. end end-text
  4156. no-sub t) ; subfields do not make sense
  4157. (setq failure t)))
  4158. (t (setq failure t)))
  4159. (when (and subfield (not failure))
  4160. (setq failure no-sub)
  4161. (unless failure
  4162. (goto-char start-text)
  4163. (while (not done)
  4164. (if (or (prog1 (looking-at bibtex-field-const)
  4165. (setq end-text (match-end 0)
  4166. string-const t))
  4167. (prog1 (setq bounds (bibtex-parse-field-string))
  4168. (setq end-text (cdr bounds)
  4169. string-const nil)))
  4170. (progn
  4171. (if (and (<= start-text pnt) (<= pnt end-text))
  4172. (setq done t)
  4173. (goto-char end-text))
  4174. (if (looking-at "[ \t\n]*#[ \t\n]*")
  4175. (setq start-text (goto-char (match-end 0)))))
  4176. (setq done t failure t)))))
  4177. (cond ((not failure)
  4178. (list name start-text end-text end string-const))
  4179. ((and no-sub (not noerror))
  4180. (error "Not on text part of BibTeX field"))
  4181. ((not noerror) (error "Not on BibTeX field"))))))
  4182. (defun bibtex-remove-OPT-or-ALT (&optional comma)
  4183. "Remove the string starting optional/alternative fields.
  4184. Align text and go thereafter to end of text. Optional arg COMMA
  4185. is as in `bibtex-enclosing-field'. It is t for interactive calls."
  4186. (interactive (list t))
  4187. (let ((case-fold-search t)
  4188. (bounds (bibtex-enclosing-field comma)))
  4189. (save-excursion
  4190. (goto-char (bibtex-start-of-name-in-field bounds))
  4191. (when (and (looking-at "OPT\\|ALT")
  4192. (not (and bibtex-no-opt-remove-re
  4193. (string-match
  4194. bibtex-no-opt-remove-re
  4195. (buffer-substring-no-properties
  4196. (bibtex-start-of-name-in-field bounds)
  4197. (bibtex-end-of-name-in-field bounds))))))
  4198. (delete-region (match-beginning 0) (match-end 0))
  4199. ;; make field non-OPT
  4200. (search-forward "=")
  4201. (forward-char -1)
  4202. (delete-horizontal-space)
  4203. (if bibtex-align-at-equal-sign
  4204. (indent-to-column (- bibtex-text-indentation 2))
  4205. (insert " "))
  4206. (search-forward "=")
  4207. (delete-horizontal-space)
  4208. (if bibtex-align-at-equal-sign
  4209. (insert " ")
  4210. (indent-to-column bibtex-text-indentation))))))
  4211. (defun bibtex-remove-delimiters (&optional comma)
  4212. "Remove \"\" or {} around current BibTeX field text.
  4213. Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for
  4214. interactive calls."
  4215. (interactive (list t))
  4216. (let ((bounds (bibtex-find-text-internal nil t comma)))
  4217. (unless (nth 4 bounds)
  4218. (delete-region (1- (nth 2 bounds)) (nth 2 bounds))
  4219. (delete-region (nth 1 bounds) (1+ (nth 1 bounds))))))
  4220. (defun bibtex-kill-field (&optional copy-only comma)
  4221. "Kill the entire enclosing BibTeX field.
  4222. With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring',
  4223. but do not actually kill it. Optional arg COMMA is as in
  4224. `bibtex-enclosing-field'. It is t for interactive calls."
  4225. (interactive (list current-prefix-arg t))
  4226. (save-excursion
  4227. (let* ((case-fold-search t)
  4228. (bounds (bibtex-enclosing-field comma))
  4229. (end (bibtex-end-of-field bounds))
  4230. (beg (bibtex-start-of-field bounds)))
  4231. (goto-char end)
  4232. ;; Preserve white space at end of BibTeX entry
  4233. (if (looking-at "[ \t\n]*[)}]")
  4234. (progn (skip-chars-backward " \t\n")
  4235. (setq end (point)))
  4236. (skip-chars-forward ","))
  4237. (push (list (bibtex-name-in-field bounds) nil
  4238. (bibtex-text-in-field-bounds bounds))
  4239. bibtex-field-kill-ring)
  4240. (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max)
  4241. (setcdr (nthcdr (1- bibtex-field-kill-ring-max)
  4242. bibtex-field-kill-ring)
  4243. nil))
  4244. (setq bibtex-field-kill-ring-yank-pointer bibtex-field-kill-ring)
  4245. (unless copy-only
  4246. (delete-region beg end))))
  4247. (setq bibtex-last-kill-command 'field))
  4248. (defun bibtex-copy-field-as-kill (&optional comma)
  4249. "Copy the BibTeX field at point to `bibtex-field-kill-ring'.
  4250. Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for
  4251. interactive calls."
  4252. (interactive (list t))
  4253. (bibtex-kill-field t comma))
  4254. (defun bibtex-kill-entry (&optional copy-only)
  4255. "Kill the entire enclosing BibTeX entry.
  4256. With prefix arg COPY-ONLY, copy the current entry to `bibtex-entry-kill-ring',
  4257. but do not actually kill it."
  4258. (interactive "P")
  4259. (save-excursion
  4260. (let* ((case-fold-search t)
  4261. (beg (bibtex-beginning-of-entry))
  4262. (key (progn (looking-at bibtex-any-entry-maybe-empty-head)
  4263. (bibtex-key-in-head)))
  4264. (end (progn (bibtex-end-of-entry)
  4265. (if (re-search-forward
  4266. bibtex-any-entry-maybe-empty-head nil 'move)
  4267. (goto-char (match-beginning 0)))
  4268. (point))))
  4269. (push (buffer-substring-no-properties beg end)
  4270. bibtex-entry-kill-ring)
  4271. (if (> (length bibtex-entry-kill-ring) bibtex-entry-kill-ring-max)
  4272. (setcdr (nthcdr (1- bibtex-entry-kill-ring-max)
  4273. bibtex-entry-kill-ring)
  4274. nil))
  4275. (setq bibtex-entry-kill-ring-yank-pointer bibtex-entry-kill-ring)
  4276. (unless copy-only
  4277. (delete-region beg end)
  4278. ;; remove key from `bibtex-reference-keys'.
  4279. (unless (functionp bibtex-reference-keys)
  4280. (setq bibtex-reference-keys
  4281. (delete (cons key t) bibtex-reference-keys))))))
  4282. (setq bibtex-last-kill-command 'entry))
  4283. (defun bibtex-copy-entry-as-kill ()
  4284. "Copy the entire enclosing BibTeX entry to `bibtex-entry-kill-ring'."
  4285. (interactive)
  4286. (bibtex-kill-entry t))
  4287. (defun bibtex-yank (&optional n)
  4288. "Reinsert the last BibTeX item.
  4289. More precisely, reinsert the field or entry killed or yanked most recently.
  4290. With argument N, reinsert the Nth most recently killed BibTeX item.
  4291. See also the command \\[bibtex-yank-pop]."
  4292. (interactive "*p")
  4293. (unless n (setq n 1))
  4294. (bibtex-insert-kill (1- n) t)
  4295. (setq this-command 'bibtex-yank))
  4296. (defun bibtex-yank-pop (n)
  4297. "Replace just-yanked killed BibTeX item with a different item.
  4298. This command is allowed only immediately after a `bibtex-yank' or a
  4299. `bibtex-yank-pop'. In this case, the region contains a reinserted
  4300. previously killed BibTeX item. `bibtex-yank-pop' deletes that item
  4301. and inserts in its place a different killed BibTeX item.
  4302. With no argument, the previous kill is inserted.
  4303. With argument N, insert the Nth previous kill.
  4304. If N is negative, this is a more recent kill.
  4305. The sequence of kills wraps around, so that after the oldest one
  4306. comes the newest one."
  4307. (interactive "*p")
  4308. (unless (eq last-command 'bibtex-yank)
  4309. (error "Previous command was not a BibTeX yank"))
  4310. (setq this-command 'bibtex-yank)
  4311. (let ((inhibit-read-only t) key)
  4312. ;; point is at end of yanked entry
  4313. (unless (functionp bibtex-reference-keys)
  4314. ;; remove key of yanked entry from `bibtex-reference-keys'
  4315. (save-excursion
  4316. (goto-char (mark t))
  4317. (if (and (looking-at bibtex-any-entry-maybe-empty-head)
  4318. (setq key (bibtex-key-in-head)))
  4319. (setq bibtex-reference-keys
  4320. (delete (cons key t) bibtex-reference-keys)))))
  4321. (delete-region (point) (mark t))
  4322. (bibtex-insert-kill n t)))
  4323. (defun bibtex-empty-field (&optional comma)
  4324. "Delete the text part of the current field, replace with empty text.
  4325. Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for
  4326. interactive calls."
  4327. (interactive (list t))
  4328. (let ((bounds (bibtex-enclosing-field comma)))
  4329. (goto-char (bibtex-start-of-text-in-field bounds))
  4330. (delete-region (point) (bibtex-end-of-text-in-field bounds))
  4331. (insert (bibtex-field-left-delimiter)
  4332. (bibtex-field-right-delimiter))
  4333. (bibtex-find-text t nil bibtex-help-message)))
  4334. (defun bibtex-pop-previous (arg)
  4335. "Replace text of current field with the similar field in previous entry.
  4336. With arg, goes up ARG entries. Repeated, goes up so many times. May be
  4337. intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
  4338. (interactive "p")
  4339. (bibtex-pop arg 'previous))
  4340. (defun bibtex-pop-next (arg)
  4341. "Replace text of current field with the text of similar field in next entry.
  4342. With arg, goes down ARG entries. Repeated, goes down so many times. May be
  4343. intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
  4344. (interactive "p")
  4345. (bibtex-pop arg 'next))
  4346. (defun bibtex-clean-entry (&optional new-key called-by-reformat)
  4347. "Finish editing the current BibTeX entry and clean it up.
  4348. Check that no required fields are empty and format entry dependent
  4349. on the value of `bibtex-entry-format'.
  4350. If the reference key of the entry is empty or a prefix argument is given,
  4351. calculate a new reference key. (Note: this works only if fields in entry
  4352. begin on separate lines prior to calling `bibtex-clean-entry' or if
  4353. 'realign is contained in `bibtex-entry-format'.)
  4354. Don't call `bibtex-clean-entry' on @Preamble entries.
  4355. At end of the cleaning process, the functions in
  4356. `bibtex-clean-entry-hook' are called with region narrowed to entry."
  4357. ;; Opt. arg CALLED-BY-REFORMAT is t if `bibtex-clean-entry'
  4358. ;; is called by `bibtex-reformat'
  4359. (interactive "P")
  4360. (let ((case-fold-search t)
  4361. (start (bibtex-beginning-of-entry))
  4362. (_ (or (looking-at bibtex-any-entry-maybe-empty-head)
  4363. (error "Not inside a BibTeX entry")))
  4364. (entry-type (bibtex-type-in-head))
  4365. (key (bibtex-key-in-head)))
  4366. (cond ((bibtex-string= entry-type "preamble")
  4367. ;; (bibtex-format-preamble)
  4368. (error "No clean up of @Preamble entries"))
  4369. ((bibtex-string= entry-type "string")
  4370. (setq entry-type 'string))
  4371. ;; (bibtex-format-string)
  4372. (t (bibtex-format-entry)))
  4373. ;; set key
  4374. (if (or new-key (not key))
  4375. (save-excursion
  4376. ;; First delete the old key so that a customized algorithm
  4377. ;; for generating the new key does not get confused by the
  4378. ;; old key.
  4379. (re-search-forward (if (eq entry-type 'string)
  4380. bibtex-string-maybe-empty-head
  4381. bibtex-entry-maybe-empty-head))
  4382. (if (match-beginning bibtex-key-in-head)
  4383. (delete-region (match-beginning bibtex-key-in-head)
  4384. (match-end bibtex-key-in-head)))
  4385. (setq key (bibtex-generate-autokey))
  4386. ;; Sometimes `bibtex-generate-autokey' returns an empty string
  4387. (if (or bibtex-autokey-edit-before-use (string= "" key))
  4388. (setq key (if (eq entry-type 'string)
  4389. (bibtex-read-string-key key)
  4390. (bibtex-read-key "Key to use: " key))))
  4391. (insert key)))
  4392. (unless called-by-reformat
  4393. (let* ((end (save-excursion
  4394. (bibtex-end-of-entry)
  4395. (if (re-search-forward
  4396. bibtex-entry-maybe-empty-head nil 'move)
  4397. (goto-char (match-beginning 0)))
  4398. (point)))
  4399. (entry (buffer-substring start end))
  4400. ;; include the crossref key in index
  4401. (index (let ((bibtex-maintain-sorted-entries 'crossref))
  4402. (bibtex-entry-index))) ; moves point to end of head
  4403. error)
  4404. ;; sorting
  4405. (if (and bibtex-maintain-sorted-entries
  4406. (not (and bibtex-sort-ignore-string-entries
  4407. (eq entry-type 'string))))
  4408. (progn
  4409. (delete-region start end)
  4410. (setq error (not (bibtex-prepare-new-entry index))
  4411. start (point)) ; update start
  4412. (save-excursion (insert entry)))
  4413. (bibtex-search-entry key)
  4414. (setq error (or (/= (point) start)
  4415. (bibtex-search-entry key nil end))))
  4416. (if error
  4417. (error "New inserted entry yields duplicate key"))
  4418. (dolist (buffer (bibtex-initialize))
  4419. (with-current-buffer buffer
  4420. (if (cdr (assoc-string key bibtex-reference-keys))
  4421. (error "Duplicate key in %s" (buffer-file-name)))))
  4422. ;; Only update `bibtex-strings' and `bibtex-reference-keys'
  4423. ;; if they have been built already.
  4424. (cond ((eq entry-type 'string)
  4425. ;; We have a @String entry.
  4426. (unless (or (functionp bibtex-strings)
  4427. (assoc key bibtex-strings))
  4428. (push (cons key (bibtex-text-in-string
  4429. (bibtex-parse-string) t))
  4430. bibtex-strings)))
  4431. ;; We have a normal entry.
  4432. ((not (functionp bibtex-reference-keys))
  4433. (let ((found (assoc key bibtex-reference-keys)))
  4434. (cond ((not found)
  4435. (push (cons key t) bibtex-reference-keys))
  4436. ((not (cdr found))
  4437. ;; Turn a crossref key into a header key
  4438. (setq bibtex-reference-keys
  4439. (cons (cons key t)
  4440. (delete (list key) bibtex-reference-keys))))))
  4441. ;; If entry has a crossref key, it goes into the list
  4442. ;; `bibtex-reference-keys', too.
  4443. (if (and (nth 1 index)
  4444. (not (assoc (nth 1 index) bibtex-reference-keys)))
  4445. (push (list (nth 1 index)) bibtex-reference-keys)))))
  4446. ;; final clean up
  4447. (if bibtex-clean-entry-hook
  4448. (save-excursion
  4449. (save-restriction
  4450. (bibtex-narrow-to-entry)
  4451. (run-hooks 'bibtex-clean-entry-hook)))))))
  4452. (defun bibtex-fill-field-bounds (bounds justify &optional move)
  4453. "Fill BibTeX field delimited by BOUNDS.
  4454. If JUSTIFY is non-nil justify as well.
  4455. If optional arg MOVE is non-nil move point to end of field."
  4456. (let ((end-field (copy-marker (bibtex-end-of-field bounds))))
  4457. (if (not justify)
  4458. (goto-char (bibtex-start-of-text-in-field bounds))
  4459. (goto-char (bibtex-start-of-field bounds))
  4460. (forward-char) ; leading comma
  4461. (bibtex-delete-whitespace)
  4462. (insert "\n")
  4463. (indent-to-column (+ bibtex-entry-offset
  4464. bibtex-field-indentation))
  4465. (re-search-forward "[ \t\n]*=" end-field)
  4466. (replace-match "=")
  4467. (forward-char -1)
  4468. (if bibtex-align-at-equal-sign
  4469. (indent-to-column
  4470. (+ bibtex-entry-offset (- bibtex-text-indentation 2)))
  4471. (insert " "))
  4472. (forward-char)
  4473. (bibtex-delete-whitespace)
  4474. (if bibtex-align-at-equal-sign
  4475. (insert " ")
  4476. (indent-to-column bibtex-text-indentation)))
  4477. ;; Paragraphs within fields are not preserved. Bother?
  4478. (fill-region-as-paragraph (line-beginning-position) end-field
  4479. default-justification nil (point))
  4480. (if move (goto-char end-field))))
  4481. (defun bibtex-fill-field (&optional justify)
  4482. "Like \\[fill-paragraph], but fill current BibTeX field.
  4483. If optional prefix JUSTIFY is non-nil justify as well.
  4484. In BibTeX mode this function is bound to `fill-paragraph-function'."
  4485. (interactive "*P")
  4486. (let ((pnt (copy-marker (point)))
  4487. (bounds (bibtex-enclosing-field t)))
  4488. (bibtex-fill-field-bounds bounds justify)
  4489. (goto-char pnt)))
  4490. (defun bibtex-fill-entry ()
  4491. "Fill current BibTeX entry.
  4492. Realign entry, so that every field starts on a separate line. Field
  4493. names appear in column `bibtex-field-indentation', field text starts in
  4494. column `bibtex-text-indentation' and continuation lines start here, too.
  4495. If `bibtex-align-at-equal-sign' is non-nil, align equal signs, too."
  4496. (interactive "*")
  4497. (let ((pnt (copy-marker (point)))
  4498. (beg (bibtex-beginning-of-entry)) ; move point
  4499. bounds)
  4500. (bibtex-delete-whitespace)
  4501. (indent-to-column bibtex-entry-offset)
  4502. (bibtex-beginning-first-field beg)
  4503. (while (setq bounds (bibtex-parse-field))
  4504. (bibtex-fill-field-bounds bounds t t))
  4505. (if (looking-at ",")
  4506. (forward-char))
  4507. (skip-chars-backward " \t\n")
  4508. (bibtex-delete-whitespace)
  4509. (insert "\n")
  4510. (indent-to-column bibtex-entry-offset)
  4511. (goto-char pnt)))
  4512. (defun bibtex-realign ()
  4513. "Realign BibTeX entries such that they are separated by one blank line."
  4514. (goto-char (point-min))
  4515. (let ((case-fold-search t)
  4516. (entry-type (concat "[ \t\n]*\\(" bibtex-entry-type "\\)")))
  4517. ;; No blank lines prior to the first entry if there no
  4518. ;; non-white characters in front of it.
  4519. (when (looking-at entry-type)
  4520. (replace-match "\\1"))
  4521. ;; Entries are separated by one blank line.
  4522. (while (re-search-forward entry-type nil t)
  4523. (replace-match "\n\n\\1"))
  4524. ;; One blank line past the last entry if it is followed by
  4525. ;; non-white characters, no blank line otherwise.
  4526. (beginning-of-line)
  4527. (when (re-search-forward bibtex-entry-type nil t)
  4528. (bibtex-end-of-entry)
  4529. (bibtex-delete-whitespace)
  4530. (open-line (if (eobp) 1 2)))))
  4531. (defun bibtex-reformat (&optional read-options)
  4532. "Reformat all BibTeX entries in buffer or region.
  4533. Without prefix argument, reformatting is based on `bibtex-entry-format'.
  4534. With prefix argument, read options for reformatting from minibuffer.
  4535. With \\[universal-argument] \\[universal-argument] prefix argument, reuse previous answers (if any) again.
  4536. If mark is active reformat entries in region, if not in whole buffer."
  4537. (interactive "*P")
  4538. (let* ((pnt (point))
  4539. (use-previous-options
  4540. (and (equal (prefix-numeric-value read-options) 16)
  4541. (or bibtex-reformat-previous-options
  4542. bibtex-reformat-previous-reference-keys)))
  4543. (bibtex-entry-format
  4544. (cond (read-options
  4545. (if use-previous-options
  4546. bibtex-reformat-previous-options
  4547. (setq bibtex-reformat-previous-options
  4548. (mapcar (lambda (option)
  4549. (if (y-or-n-p (car option)) (cdr option)))
  4550. `(("Realign entries (recommended)? " . 'realign)
  4551. ("Remove empty optional and alternative fields? " . 'opts-or-alts)
  4552. ("Remove delimiters around pure numerical fields? " . 'numerical-fields)
  4553. (,(concat (if bibtex-comma-after-last-field "Insert" "Remove")
  4554. " comma at end of entry? ") . 'last-comma)
  4555. ("Replace double page dashes by single ones? " . 'page-dashes)
  4556. ("Delete whitespace at the beginning and end of fields? " . 'whitespace)
  4557. ("Inherit booktitle? " . 'inherit-booktitle)
  4558. ("Force delimiters? " . 'delimiters)
  4559. ("Unify case of entry types and field names? " . 'unify-case)
  4560. ("Enclose parts of field entries by braces? " . 'braces)
  4561. ("Replace parts of field entries by string constants? " . 'strings)
  4562. ("Sort fields? " . 'sort-fields))))))
  4563. ;; Do not include required-fields because `bibtex-reformat'
  4564. ;; cannot handle the error messages of `bibtex-format-entry'.
  4565. ;; Use `bibtex-validate' to check for required fields.
  4566. ((eq t bibtex-entry-format)
  4567. '(realign opts-or-alts numerical-fields delimiters
  4568. last-comma page-dashes unify-case inherit-booktitle
  4569. whitespace braces strings sort-fields))
  4570. (t
  4571. (cons 'realign (remove 'required-fields bibtex-entry-format)))))
  4572. (reformat-reference-keys
  4573. (if read-options
  4574. (if use-previous-options
  4575. bibtex-reformat-previous-reference-keys
  4576. (setq bibtex-reformat-previous-reference-keys
  4577. (y-or-n-p "Generate new reference keys automatically? ")))))
  4578. (bibtex-sort-ignore-string-entries t)
  4579. bibtex-autokey-edit-before-use)
  4580. (save-restriction
  4581. (if mark-active (narrow-to-region (region-beginning) (region-end)))
  4582. (if (memq 'realign bibtex-entry-format)
  4583. (bibtex-realign))
  4584. (bibtex-progress-message "Formatting" 1)
  4585. (bibtex-map-entries (lambda (_key _beg _end)
  4586. (bibtex-progress-message)
  4587. (bibtex-clean-entry reformat-reference-keys t)))
  4588. (bibtex-progress-message 'done))
  4589. (when reformat-reference-keys
  4590. (kill-local-variable 'bibtex-reference-keys)
  4591. (when bibtex-maintain-sorted-entries
  4592. (bibtex-progress-message "Sorting" 1)
  4593. (bibtex-sort-buffer)
  4594. (bibtex-progress-message 'done)))
  4595. (goto-char pnt)))
  4596. (defun bibtex-convert-alien (&optional read-options)
  4597. "Make an alien BibTeX buffer fully usable by BibTeX mode.
  4598. If a file does not conform with all standards used by BibTeX mode,
  4599. some of the high-level features of BibTeX mode are not available.
  4600. This function tries to convert current buffer to conform with these standards.
  4601. With prefix argument READ-OPTIONS non-nil, read options for reformatting
  4602. entries from minibuffer."
  4603. (interactive "*P")
  4604. (message "Starting to validate buffer...")
  4605. (sit-for 1)
  4606. (bibtex-realign)
  4607. (deactivate-mark) ; So `bibtex-validate' works on the whole buffer.
  4608. (if (not (let (bibtex-maintain-sorted-entries)
  4609. (bibtex-validate)))
  4610. (message "Correct errors and call `bibtex-convert-alien' again")
  4611. (message "Starting to reformat entries...")
  4612. (sit-for 2)
  4613. (bibtex-reformat read-options)
  4614. (goto-char (point-max))
  4615. (message "Buffer is now parsable. Please save it.")))
  4616. (define-obsolete-function-alias 'bibtex-complete 'completion-at-point "24.1")
  4617. (defun bibtex-completion-at-point-function ()
  4618. (let ((pnt (point))
  4619. (case-fold-search t)
  4620. (beg (save-excursion
  4621. (re-search-backward "[ \t{\"]")
  4622. (forward-char)
  4623. (point)))
  4624. (end (point))
  4625. bounds name compl)
  4626. (save-excursion
  4627. (if (and (setq bounds (bibtex-enclosing-field nil t))
  4628. (>= pnt (bibtex-start-of-text-in-field bounds))
  4629. (<= pnt (bibtex-end-of-text-in-field bounds)))
  4630. (setq name (bibtex-name-in-field bounds t)
  4631. compl (cond ((bibtex-string= name "crossref")
  4632. ;; point is in crossref field
  4633. 'crossref-key)
  4634. ((bibtex-string= name "month")
  4635. ;; point is in month field
  4636. bibtex-predefined-month-strings)
  4637. ;; point is in other field
  4638. (t (bibtex-strings))))
  4639. (bibtex-beginning-of-entry)
  4640. (cond ((setq bounds (bibtex-parse-string t))
  4641. ;; point is inside a @String key
  4642. (cond ((and (>= pnt (nth 1 (car bounds)))
  4643. (<= pnt (nth 2 (car bounds))))
  4644. (setq compl 'string))
  4645. ;; point is inside a @String field
  4646. ((and (>= pnt (bibtex-start-of-text-in-string bounds))
  4647. (<= pnt (bibtex-end-of-text-in-string bounds)))
  4648. (setq compl (bibtex-strings)))))
  4649. ;; point is inside a @Preamble field
  4650. ((setq bounds (bibtex-parse-preamble))
  4651. (if (and (>= pnt (bibtex-start-of-text-in-string bounds))
  4652. (<= pnt (bibtex-end-of-text-in-string bounds)))
  4653. (setq compl (bibtex-strings))))
  4654. ((and (looking-at bibtex-entry-maybe-empty-head)
  4655. ;; point is inside a key
  4656. (or (and (match-beginning bibtex-key-in-head)
  4657. (>= pnt (match-beginning bibtex-key-in-head))
  4658. (<= pnt (match-end bibtex-key-in-head)))
  4659. ;; or point is on empty key
  4660. (and (not (match-beginning bibtex-key-in-head))
  4661. (= pnt (match-end 0)))))
  4662. (setq compl 'key)))))
  4663. (cond ((eq compl 'key)
  4664. ;; Key completion: no cleanup needed.
  4665. (list beg end
  4666. (lambda (s p a)
  4667. (let (completion-ignore-case)
  4668. (complete-with-action a (bibtex-global-key-alist) s p)))))
  4669. ((eq compl 'crossref-key)
  4670. ;; Crossref key completion.
  4671. (let* ((buf (current-buffer)))
  4672. (list beg end
  4673. (lambda (s p a)
  4674. (cond
  4675. ((eq a 'metadata) `(metadata (category . bibtex-key)))
  4676. (t (let ((completion-ignore-case nil))
  4677. (complete-with-action
  4678. a (bibtex-global-key-alist) s p)))))
  4679. :exit-function (bibtex-complete-crossref-cleanup buf))))
  4680. ((eq compl 'string)
  4681. ;; String key completion: no cleanup needed.
  4682. (list beg end
  4683. (lambda (s p a)
  4684. (let ((completion-ignore-case t))
  4685. (complete-with-action a bibtex-strings s p)))))
  4686. (compl
  4687. ;; String completion.
  4688. (list beg end
  4689. (lambda (s p a)
  4690. (cond
  4691. ((eq a 'metadata) `(metadata (category . bibtex-string)))
  4692. (t (let ((completion-ignore-case t))
  4693. (complete-with-action a compl s p)))))
  4694. :exit-function (bibtex-complete-string-cleanup compl))))))
  4695. (defun bibtex-String (&optional key)
  4696. "Insert a new BibTeX @String entry with key KEY."
  4697. (interactive (list (bibtex-read-string-key)))
  4698. (let ((bibtex-maintain-sorted-entries
  4699. (unless bibtex-sort-ignore-string-entries
  4700. bibtex-maintain-sorted-entries))
  4701. endpos)
  4702. (unless (bibtex-prepare-new-entry (list key nil "String"))
  4703. (error "Entry with key `%s' already exists" key))
  4704. (if (zerop (length key)) (setq key nil))
  4705. (indent-to-column bibtex-entry-offset)
  4706. (insert "@String"
  4707. (bibtex-entry-left-delimiter))
  4708. (if key
  4709. (insert key)
  4710. (setq endpos (point)))
  4711. (insert " = "
  4712. (bibtex-field-left-delimiter))
  4713. (if key
  4714. (setq endpos (point)))
  4715. (insert (bibtex-field-right-delimiter)
  4716. (bibtex-entry-right-delimiter)
  4717. "\n")
  4718. (goto-char endpos)))
  4719. (defun bibtex-Preamble ()
  4720. "Insert a new BibTeX @Preamble entry."
  4721. (interactive "*")
  4722. (bibtex-move-outside-of-entry)
  4723. (indent-to-column bibtex-entry-offset)
  4724. (insert "@Preamble"
  4725. (bibtex-entry-left-delimiter)
  4726. (bibtex-field-left-delimiter))
  4727. (let ((endpos (point)))
  4728. (insert (bibtex-field-right-delimiter)
  4729. (bibtex-entry-right-delimiter)
  4730. "\n")
  4731. (goto-char endpos)))
  4732. (defun bibtex-url (&optional pos no-browse)
  4733. "Browse a URL for the BibTeX entry at point.
  4734. Optional POS is the location of the BibTeX entry.
  4735. The URL is generated using the schemes defined in `bibtex-generate-url-list'
  4736. \(see there\). If multiple schemes match for this entry, or the same scheme
  4737. matches more than once, use the one for which the first step's match is the
  4738. closest to POS. The URL is passed to `browse-url' unless NO-BROWSE is t.
  4739. Return the URL or nil if none can be generated."
  4740. (interactive)
  4741. (unless pos (setq pos (point)))
  4742. (save-excursion
  4743. (goto-char pos)
  4744. (bibtex-beginning-of-entry)
  4745. (let ((end (save-excursion (bibtex-end-of-entry)))
  4746. (fields-alist (save-excursion (bibtex-parse-entry t)))
  4747. ;; Always ignore case,
  4748. (case-fold-search t)
  4749. text url scheme obj fmt fl-match)
  4750. ;; The return value of `bibtex-parse-entry' (i.e., FIELDS-ALIST)
  4751. ;; is always used to generate the URL. However, if the BibTeX
  4752. ;; entry contains more than one URL, we have multiple matches
  4753. ;; for the first step defining the generation of the URL.
  4754. ;; Therefore, we try to initiate the generation of the URL
  4755. ;; based on the match of `bibtex-font-lock-url' that is the
  4756. ;; closest to POS. If that fails (no match found) we try to
  4757. ;; initiate the generation of the URL based on the properly
  4758. ;; concatenated CONTENT of the field as returned by
  4759. ;; `bibtex-text-in-field-bounds'. The latter approach can
  4760. ;; differ from the former because `bibtex-font-lock-url' uses
  4761. ;; the buffer itself.
  4762. (while (bibtex-font-lock-url end t)
  4763. (push (list (bibtex-dist pos (match-beginning 0) (match-end 0))
  4764. (match-beginning 0)
  4765. (buffer-substring-no-properties
  4766. (match-beginning 0) (match-end 0)))
  4767. fl-match)
  4768. ;; `bibtex-font-lock-url' moves point to end of match.
  4769. (forward-char))
  4770. (when fl-match
  4771. (setq fl-match (car (sort fl-match (lambda (x y) (< (car x) (car y))))))
  4772. (goto-char (nth 1 fl-match))
  4773. (bibtex-beginning-of-field) (re-search-backward ",")
  4774. (let* ((bounds (bibtex-parse-field))
  4775. (name (bibtex-name-in-field bounds))
  4776. (content (bibtex-text-in-field-bounds bounds t))
  4777. (lst bibtex-generate-url-list))
  4778. ;; This match can fail when CONTENT differs from text in buffer.
  4779. (when (string-match (regexp-quote (nth 2 fl-match)) content)
  4780. ;; TEXT is the part of CONTENT that starts with the match
  4781. ;; of `bibtex-font-lock-url' we are looking for.
  4782. (setq text (substring content (match-beginning 0)))
  4783. (while (and (not url) (setq scheme (pop lst)))
  4784. ;; Verify the match of `bibtex-font-lock-url' by
  4785. ;; comparing with TEXT.
  4786. (when (and (bibtex-string= (caar scheme) name)
  4787. (string-match (cdar scheme) text))
  4788. (setq url t scheme (cdr scheme)))))))
  4789. ;; If the match of `bibtex-font-lock-url' was not approved
  4790. ;; parse FIELDS-ALIST, i.e., the output of `bibtex-parse-entry'.
  4791. (unless url
  4792. (let ((lst bibtex-generate-url-list))
  4793. (while (and (not url) (setq scheme (pop lst)))
  4794. (when (and (setq text (cdr (assoc-string (caar scheme)
  4795. fields-alist t)))
  4796. (string-match (cdar scheme) text))
  4797. (setq url t scheme (cdr scheme))))))
  4798. (when url
  4799. (setq url (if (null scheme) (match-string 0 text)
  4800. (if (stringp (car scheme))
  4801. (setq fmt (pop scheme)))
  4802. (dolist (step scheme)
  4803. (setq text (cdr (assoc-string (car step) fields-alist t)))
  4804. (if (string-match (nth 1 step) text)
  4805. (push (cond ((functionp (nth 2 step))
  4806. (funcall (nth 2 step) text))
  4807. ((numberp (nth 2 step))
  4808. (match-string (nth 2 step) text))
  4809. (t
  4810. (replace-match (nth 2 step) t nil text)))
  4811. obj)
  4812. ;; If SCHEME is set up correctly,
  4813. ;; we should never reach this point
  4814. (error "Match failed: %s" text)))
  4815. (if fmt (apply 'format fmt (nreverse obj))
  4816. (apply 'concat (nreverse obj)))))
  4817. (if (called-interactively-p 'interactive) (message "%s" url))
  4818. (unless no-browse (browse-url url)))
  4819. (if (and (not url) (called-interactively-p 'interactive))
  4820. (message "No URL known."))
  4821. url)))
  4822. ;; We could combine multiple search results with set operations
  4823. ;; AND, OR, MINUS, and NOT. Would this be useful?
  4824. ;; How complicated are searches in real life?
  4825. ;; We could also have other searches such as "publication year newer than...".
  4826. (defun bibtex-search-entries (field regexp &optional global display)
  4827. "Search BibTeX entries for FIELD matching REGEXP.
  4828. REGEXP may be a regexp to search for.
  4829. If REGEXP is a function, it is called for each entry with two args,
  4830. the buffer positions of beginning and end of entry. Then an entry
  4831. is accepted if this function returns non-nil.
  4832. If FIELD is an empty string perform search for REGEXP in whole entry.
  4833. With GLOBAL non-nil, search in `bibtex-files'. Otherwise the search
  4834. is limited to the current buffer.
  4835. If DISPLAY is non-nil, display search results in `bibtex-search-buffer'.
  4836. When called interactively, DISPLAY is t.
  4837. Also, GLOBAL is t if `bibtex-search-entry-globally' is non-nil.
  4838. A prefix arg negates the value of `bibtex-search-entry-globally'.
  4839. Return alist with elements (KEY FILE ENTRY),
  4840. where FILE is the BibTeX file of ENTRY."
  4841. (interactive
  4842. (list (completing-read
  4843. "Field: "
  4844. (delete-dups
  4845. (apply 'append
  4846. bibtex-user-optional-fields
  4847. (mapcar (lambda (x) (mapcar 'car (apply 'append (nthcdr 2 x))))
  4848. bibtex-entry-alist))) nil t)
  4849. (read-string "Regexp: ")
  4850. (if bibtex-search-entry-globally
  4851. (not current-prefix-arg)
  4852. current-prefix-arg)
  4853. t))
  4854. (let ((funp (functionp regexp))
  4855. entries text file)
  4856. ;; If REGEXP is a function, the value of FIELD is ignored anyway.
  4857. ;; Yet to ensure the code below does not fail, we make FIELD
  4858. ;; a non-empty string.
  4859. (if (and funp (string= "" field)) (setq field "unrestricted"))
  4860. (dolist (buffer (if (and global bibtex-files)
  4861. (bibtex-initialize t)
  4862. (list (current-buffer))))
  4863. (with-current-buffer buffer
  4864. (setq file (if buffer-file-name
  4865. (file-name-nondirectory buffer-file-name)
  4866. (buffer-name buffer)))
  4867. (save-excursion
  4868. (goto-char (point-min))
  4869. (if (string= "" field)
  4870. ;; Unrestricted search.
  4871. (while (re-search-forward regexp nil t)
  4872. (let ((beg (bibtex-beginning-of-entry))
  4873. (end (bibtex-end-of-entry))
  4874. key)
  4875. (if (and (<= beg (match-beginning 0))
  4876. (<= (match-end 0) end)
  4877. (save-excursion
  4878. (goto-char beg)
  4879. (and (looking-at bibtex-entry-head)
  4880. (setq key (bibtex-key-in-head))))
  4881. (not (assoc key entries)))
  4882. (push (list key file
  4883. (buffer-substring-no-properties beg end))
  4884. entries))))
  4885. ;; The following is slow. But it works reliably even in more
  4886. ;; complicated cases with BibTeX string constants and crossrefed
  4887. ;; entries. If you prefer speed over reliability, perform an
  4888. ;; unrestricted search.
  4889. (bibtex-map-entries
  4890. (lambda (key beg end)
  4891. (if (and (cond (funp (funcall regexp beg end))
  4892. ((and (setq text (bibtex-text-in-field field t))
  4893. (string-match regexp text))))
  4894. (not (assoc key entries)))
  4895. (push (list key file
  4896. (buffer-substring-no-properties beg end))
  4897. entries))))))))
  4898. (if display
  4899. (if entries
  4900. (bibtex-display-entries entries)
  4901. (message "No BibTeX entries %smatching `%s'"
  4902. (if (string= "" field) ""
  4903. (format "with field `%s' " field))
  4904. regexp)))
  4905. entries))
  4906. (defun bibtex-display-entries (entries &optional append)
  4907. "Display BibTeX ENTRIES in `bibtex-search-buffer'.
  4908. ENTRIES is an alist with elements (KEY FILE ENTRY),
  4909. where FILE is the BibTeX file of ENTRY.
  4910. If APPEND is non-nil, append ENTRIES to those already displayed."
  4911. (pop-to-buffer (get-buffer-create bibtex-search-buffer))
  4912. ;; It would be nice if this buffer was editable, though editing
  4913. ;; can be meaningful only for individual existing entries
  4914. ;; (unlike reordering or creating new entries).
  4915. ;; Fancy workaround: Editing commands in the virtual buffer could
  4916. ;; jump to the real entry in the real buffer.
  4917. (let (buffer-read-only)
  4918. (if append (goto-char (point-max)) (erase-buffer))
  4919. (dolist (entry (sort entries (lambda (x y) (string< (car x) (car y)))))
  4920. (insert "% " (nth 1 entry) "\n" (nth 2 entry) "\n\n")))
  4921. ;; `bibtex-sort-buffer' fails with the file names associated with
  4922. ;; each entry. Prior to sorting we could make the file name
  4923. ;; a BibTeX field of each entry (using `bibtex-make-field').
  4924. ;; Or we could make it a text property that we unfold afterwards.
  4925. ;; (bibtex-sort-buffer)
  4926. (bibtex-mode)
  4927. (set-buffer-modified-p nil)
  4928. (setq buffer-read-only t)
  4929. (goto-char (point-min)))
  4930. ;; Make BibTeX a Feature
  4931. (provide 'bibtex)
  4932. ;;; bibtex.el ends here