bibtex.el 242 KB

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