tex-mode.el 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473
  1. ;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1985-1986, 1989, 1992, 1994-1999, 2001-2017 Free
  3. ;; Software Foundation, Inc.
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: tex
  6. ;; Contributions over the years by William F. Schelter, Dick King,
  7. ;; Stephen Gildea, Michael Prange, Jacob Gore, and Edward M. Reingold.
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;; Code:
  21. ;; Pacify the byte-compiler
  22. (eval-when-compile
  23. (require 'compare-w)
  24. (require 'cl-lib)
  25. (require 'skeleton))
  26. (defvar font-lock-comment-face)
  27. (defvar font-lock-doc-face)
  28. (require 'shell)
  29. (require 'compile)
  30. (defgroup tex-file nil
  31. "TeX files and directories."
  32. :prefix "tex-"
  33. :group 'tex)
  34. (defgroup tex-run nil
  35. "Running external commands from TeX mode."
  36. :prefix "tex-"
  37. :group 'tex)
  38. (defgroup tex-view nil
  39. "Viewing and printing TeX files."
  40. :prefix "tex-"
  41. :group 'tex)
  42. ;;;###autoload
  43. (defcustom tex-shell-file-name nil
  44. "If non-nil, the shell file name to run in the subshell used to run TeX."
  45. :type '(choice (const :tag "None" nil)
  46. string)
  47. :group 'tex-run)
  48. ;;;###autoload
  49. (defcustom tex-directory (purecopy ".")
  50. "Directory in which temporary files are written.
  51. You can make this `/tmp' if your TEXINPUTS has no relative directories in it
  52. and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are
  53. `\\input' commands with relative directories."
  54. :type 'directory
  55. :group 'tex-file)
  56. ;;;###autoload
  57. (defcustom tex-first-line-header-regexp nil
  58. "Regexp for matching a first line which `tex-region' should include.
  59. If this is non-nil, it should be a regular expression string;
  60. if it matches the first line of the file,
  61. `tex-region' always includes the first line in the TeX run."
  62. :type '(choice (const :tag "None" nil)
  63. regexp)
  64. :group 'tex-file)
  65. ;;;###autoload
  66. (defcustom tex-main-file nil
  67. "The main TeX source file which includes this buffer's file.
  68. The command `tex-file' runs TeX on the file specified by `tex-main-file'
  69. if the variable is non-nil."
  70. :type '(choice (const :tag "None" nil)
  71. file)
  72. :group 'tex-file)
  73. ;;;###autoload
  74. (defcustom tex-offer-save t
  75. "If non-nil, ask about saving modified buffers before \\[tex-file] is run."
  76. :type 'boolean
  77. :group 'tex-file)
  78. ;;;###autoload
  79. (defcustom tex-run-command (purecopy "tex")
  80. "Command used to run TeX subjob.
  81. TeX Mode sets `tex-command' to this string.
  82. See the documentation of that variable."
  83. :type 'string
  84. :group 'tex-run)
  85. ;;;###autoload
  86. (defcustom latex-run-command (purecopy "latex")
  87. "Command used to run LaTeX subjob.
  88. LaTeX Mode sets `tex-command' to this string.
  89. See the documentation of that variable."
  90. :type 'string
  91. :group 'tex-run)
  92. ;;;###autoload
  93. (defcustom slitex-run-command (purecopy "slitex")
  94. "Command used to run SliTeX subjob.
  95. SliTeX Mode sets `tex-command' to this string.
  96. See the documentation of that variable."
  97. :type 'string
  98. :group 'tex-run)
  99. ;;;###autoload
  100. (defcustom tex-start-options (purecopy "")
  101. "TeX options to use when starting TeX.
  102. These immediately precede the commands in `tex-start-commands'
  103. and the input file name, with no separating space and are not shell-quoted.
  104. If nil, TeX runs with no options. See the documentation of `tex-command'."
  105. :type 'string
  106. :group 'tex-run
  107. :version "22.1")
  108. ;;;###autoload
  109. (defcustom tex-start-commands (purecopy "\\nonstopmode\\input")
  110. "TeX commands to use when starting TeX.
  111. They are shell-quoted and precede the input file name, with a separating space.
  112. If nil, no commands are used. See the documentation of `tex-command'."
  113. :type '(radio (const :tag "Interactive (nil)" nil)
  114. (const :tag "Nonstop (\"\\nonstopmode\\input\")"
  115. "\\nonstopmode\\input")
  116. (string :tag "String at your choice"))
  117. :group 'tex-run
  118. :version "22.1")
  119. (defvar latex-standard-block-names
  120. '("abstract" "array" "center" "description"
  121. "displaymath" "document" "enumerate" "eqnarray"
  122. "eqnarray*" "equation" "figure" "figure*"
  123. "flushleft" "flushright" "itemize" "letter"
  124. "list" "minipage" "picture" "quotation"
  125. "quote" "slide" "sloppypar" "tabbing"
  126. "table" "table*" "tabular" "tabular*"
  127. "thebibliography" "theindex*" "titlepage" "trivlist"
  128. "verbatim" "verbatim*" "verse" "math")
  129. "Standard LaTeX block names.")
  130. ;;;###autoload
  131. (defcustom latex-block-names nil
  132. "User defined LaTeX block names.
  133. Combined with `latex-standard-block-names' for minibuffer completion."
  134. :type '(repeat string)
  135. :group 'tex-run)
  136. ;;;###autoload
  137. (defcustom tex-bibtex-command (purecopy "bibtex")
  138. "Command used by `tex-bibtex-file' to gather bibliographic data.
  139. If this string contains an asterisk (`*'), that is replaced by the file name;
  140. otherwise, the file name, preceded by blank, is added at the end."
  141. :type 'string
  142. :group 'tex-run)
  143. ;;;###autoload
  144. (defcustom tex-dvi-print-command (purecopy "lpr -d")
  145. "Command used by \\[tex-print] to print a .dvi file.
  146. If this string contains an asterisk (`*'), that is replaced by the file name;
  147. otherwise, the file name, preceded by blank, is added at the end."
  148. :type 'string
  149. :group 'tex-view)
  150. ;;;###autoload
  151. (defcustom tex-alt-dvi-print-command (purecopy "lpr -d")
  152. "Command used by \\[tex-print] with a prefix arg to print a .dvi file.
  153. If this string contains an asterisk (`*'), that is replaced by the file name;
  154. otherwise, the file name, preceded by blank, is added at the end.
  155. If two printers are not enough of a choice, you can set the variable
  156. `tex-alt-dvi-print-command' to an expression that asks what you want;
  157. for example,
  158. (setq tex-alt-dvi-print-command
  159. \\='(format \"lpr -P%s\" (read-string \"Use printer: \")))
  160. would tell \\[tex-print] with a prefix argument to ask you which printer to
  161. use."
  162. :type '(choice (string :tag "Command")
  163. (sexp :tag "Expression"))
  164. :group 'tex-view)
  165. ;;;###autoload
  166. (defcustom tex-dvi-view-command
  167. `(cond
  168. ((eq window-system 'x) ,(purecopy "xdvi"))
  169. ((eq window-system 'w32) ,(purecopy "yap"))
  170. (t ,(purecopy "dvi2tty * | cat -s")))
  171. "Command used by \\[tex-view] to display a `.dvi' file.
  172. If it is a string, that specifies the command directly.
  173. If this string contains an asterisk (`*'), that is replaced by the file name;
  174. otherwise, the file name, preceded by a space, is added at the end.
  175. If the value is a form, it is evaluated to get the command to use."
  176. :type '(choice (const nil) string sexp)
  177. :group 'tex-view)
  178. ;;;###autoload
  179. (defcustom tex-show-queue-command (purecopy "lpq")
  180. "Command used by \\[tex-show-print-queue] to show the print queue.
  181. Should show the queue(s) that \\[tex-print] puts jobs on."
  182. :type 'string
  183. :group 'tex-view)
  184. ;;;###autoload
  185. (defcustom tex-default-mode 'latex-mode
  186. "Mode to enter for a new file that might be either TeX or LaTeX.
  187. This variable is used when it can't be determined whether the file
  188. is plain TeX or LaTeX or what because the file contains no commands.
  189. Normally set to either `plain-tex-mode' or `latex-mode'."
  190. :type 'function
  191. :group 'tex)
  192. ;;;###autoload
  193. (defcustom tex-open-quote (purecopy "``")
  194. "String inserted by typing \\[tex-insert-quote] to open a quotation."
  195. :type 'string
  196. :options '("``" "\"<" "\"`" "<<" "«")
  197. :group 'tex)
  198. ;;;###autoload
  199. (defcustom tex-close-quote (purecopy "''")
  200. "String inserted by typing \\[tex-insert-quote] to close a quotation."
  201. :type 'string
  202. :options '("''" "\">" "\"'" ">>" "»")
  203. :group 'tex)
  204. (defcustom tex-fontify-script t
  205. "If non-nil, fontify subscript and superscript strings."
  206. :type 'boolean
  207. :group 'tex
  208. :version "23.1")
  209. (put 'tex-fontify-script 'safe-local-variable 'booleanp)
  210. (defcustom tex-font-script-display '(-0.2 0.2)
  211. "How much to lower and raise subscript and superscript content.
  212. This is a list of two floats. The first is negative and
  213. specifies how much subscript is lowered, the second is positive
  214. and specifies how much superscript is raised. Heights are
  215. measured relative to that of the normal text."
  216. :group 'tex
  217. :type '(list (float :tag "Subscript")
  218. (float :tag "Superscript"))
  219. :version "23.1")
  220. (defvar tex-last-temp-file nil
  221. "Latest temporary file generated by \\[tex-region] and \\[tex-buffer].
  222. Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the
  223. tex shell terminates.")
  224. (defvar tex-command "tex"
  225. "Command to run TeX.
  226. If this string contains an asterisk \(`*'), that is replaced by the file name;
  227. otherwise the value of `tex-start-options', the \(shell-quoted)
  228. value of `tex-start-commands', and the file name are added at the end
  229. with blanks as separators.
  230. In TeX, LaTeX, and SliTeX Mode this variable becomes buffer local.")
  231. (defvar tex-trailer nil
  232. "String appended after the end of a region sent to TeX by \\[tex-region].")
  233. (defvar tex-start-of-header nil
  234. "Regular expression used by \\[tex-region] to find start of file's header.")
  235. (defvar tex-end-of-header nil
  236. "Regular expression used by \\[tex-region] to find end of file's header.")
  237. (defvar tex-shell-cd-command "cd"
  238. "Command to give to shell running TeX to change directory.
  239. The value of `tex-directory' is appended to this, separated by a space.")
  240. (defvar tex-zap-file nil
  241. "Temporary file name used for text being sent as input to TeX.
  242. Should be a simple file name with no extension or directory specification.")
  243. (defvar tex-last-buffer-texed nil
  244. "Buffer which was last TeXed.")
  245. (defvar tex-print-file nil
  246. "File name that \\[tex-print] prints.
  247. Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
  248. (defvar tex-mode-syntax-table
  249. (let ((st (make-syntax-table)))
  250. (modify-syntax-entry ?% "<" st)
  251. (modify-syntax-entry ?\n ">" st)
  252. (modify-syntax-entry ?\f ">" st)
  253. (modify-syntax-entry ?\C-@ "w" st)
  254. (modify-syntax-entry ?' "w" st)
  255. (modify-syntax-entry ?@ "_" st)
  256. (modify-syntax-entry ?* "_" st)
  257. (modify-syntax-entry ?\t " " st)
  258. ;; ~ is printed by TeX as a space, but it's semantics in the syntax
  259. ;; of TeX is not `whitespace' (i.e. it's just like \hspace{foo}).
  260. (modify-syntax-entry ?~ "." st)
  261. (modify-syntax-entry ?$ "$$" st)
  262. (modify-syntax-entry ?\\ "/" st)
  263. (modify-syntax-entry ?\" "." st)
  264. (modify-syntax-entry ?& "." st)
  265. (modify-syntax-entry ?_ "." st)
  266. (modify-syntax-entry ?^ "." st)
  267. st)
  268. "Syntax table used while in TeX mode.")
  269. ;;;;
  270. ;;;; Imenu support
  271. ;;;;
  272. (defcustom latex-imenu-indent-string ". "
  273. "String to add repeated in front of nested sectional units for Imenu.
  274. An alternative value is \" . \", if you use a font with a narrow period."
  275. :type 'string
  276. :group 'tex)
  277. (defvar latex-section-alist
  278. '(("part" . 0) ("chapter" . 1)
  279. ("section" . 2) ("subsection" . 3)
  280. ("subsubsection" . 4)
  281. ("paragraph" . 5) ("subparagraph" . 6)))
  282. (defvar latex-metasection-list
  283. '("documentstyle" "documentclass"
  284. "begin{document}" "end{document}"
  285. "appendix" "frontmatter" "mainmatter" "backmatter"))
  286. (defun latex-imenu-create-index ()
  287. "Generate an alist for imenu from a LaTeX buffer."
  288. (let ((section-regexp
  289. (concat "\\\\" (regexp-opt (mapcar #'car latex-section-alist) t)
  290. "\\*?[ \t]*{"))
  291. (metasection-regexp
  292. (concat "\\\\" (regexp-opt latex-metasection-list t)))
  293. i0 menu case-fold-search)
  294. (save-excursion
  295. ;; Find the top-most level in this file but don't allow it to be
  296. ;; any deeper than "section" (which is top-level in an article).
  297. (goto-char (point-min))
  298. (if (search-forward-regexp "\\\\part\\*?[ \t]*{" nil t)
  299. (setq i0 0)
  300. (if (search-forward-regexp "\\\\chapter\\*?[ \t]*{" nil t)
  301. (setq i0 1)
  302. (setq i0 2)))
  303. ;; Look for chapters and sections.
  304. (goto-char (point-min))
  305. (while (search-forward-regexp section-regexp nil t)
  306. (let ((start (match-beginning 0))
  307. (here (point))
  308. (i (cdr (assoc (buffer-substring-no-properties
  309. (match-beginning 1)
  310. (match-end 1))
  311. latex-section-alist))))
  312. (backward-char 1)
  313. (condition-case nil
  314. (progn
  315. ;; Using sexps allows some use of matching {...} inside
  316. ;; titles.
  317. (forward-sexp 1)
  318. (push (cons (concat (apply #'concat
  319. (make-list
  320. (max 0 (- i i0))
  321. latex-imenu-indent-string))
  322. (buffer-substring-no-properties
  323. here (1- (point))))
  324. start)
  325. menu))
  326. (error nil))))
  327. ;; Look for included material.
  328. (goto-char (point-min))
  329. (while (search-forward-regexp
  330. "\\\\\\(include\\|input\\|verbatiminput\\|bibliography\\)\
  331. [ \t]*{\\([^}\n]+\\)}"
  332. nil t)
  333. (push (cons (concat "<<" (buffer-substring-no-properties
  334. (match-beginning 2)
  335. (match-end 2))
  336. (if (= (char-after (match-beginning 1)) ?b)
  337. ".bbl"
  338. ".tex"))
  339. (match-beginning 0))
  340. menu))
  341. ;; Look for \frontmatter, \mainmatter, \backmatter, and \appendix.
  342. (goto-char (point-min))
  343. (while (search-forward-regexp metasection-regexp nil t)
  344. (push (cons "--" (match-beginning 0)) menu))
  345. ;; Sort in increasing buffer position order.
  346. (sort menu (function (lambda (a b) (< (cdr a) (cdr b))))))))
  347. ;;;;
  348. ;;;; Outline support
  349. ;;;;
  350. (defvar latex-outline-regexp
  351. (concat "\\\\"
  352. (regexp-opt (append latex-metasection-list
  353. (mapcar #'car latex-section-alist))
  354. t)))
  355. (defun latex-outline-level ()
  356. (if (looking-at latex-outline-regexp)
  357. (1+ (or (cdr (assoc (match-string 1) latex-section-alist)) -1))
  358. 1000))
  359. (defun tex-current-defun-name ()
  360. "Return the name of the TeX section/paragraph/chapter at point, or nil."
  361. (save-excursion
  362. (when (re-search-backward
  363. "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
  364. nil t)
  365. (goto-char (match-beginning 0))
  366. (buffer-substring-no-properties
  367. (1+ (point)) ; without initial backslash
  368. (line-end-position)))))
  369. ;;;;
  370. ;;;; Font-Lock support
  371. ;;;;
  372. ;(defvar tex-font-lock-keywords
  373. ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
  374. ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
  375. ; 2 font-lock-function-name-face)
  376. ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
  377. ; 2 font-lock-constant-face)
  378. ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
  379. ; ;; not be able to display those fonts.
  380. ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
  381. ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
  382. ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
  383. ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
  384. ; ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
  385. ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
  386. ; 2 font-lock-function-name-face)
  387. ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
  388. ; 2 font-lock-constant-face)
  389. ; ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
  390. ; "\\\\\\([a-zA-Z@]+\\|.\\)"
  391. ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
  392. ; ;; not be able to display those fonts.
  393. ; ;; LaTeX2e: \emph{This is emphasized}.
  394. ; ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
  395. ; ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
  396. ; ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
  397. ; 3 (if (match-beginning 2) 'bold 'italic) keep)
  398. ; ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
  399. ; ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
  400. ; 3 (if (match-beginning 2) 'bold 'italic) keep))
  401. ;; Rewritten with the help of Alexandra Bac <abac@welcome.disi.unige.it>.
  402. (defconst tex-font-lock-keywords-1
  403. (eval-when-compile
  404. (let* (;; Names of commands whose arg should be fontified as heading, etc.
  405. (headings (regexp-opt
  406. '("title" "begin" "end" "chapter" "part"
  407. "section" "subsection" "subsubsection"
  408. "paragraph" "subparagraph" "subsubparagraph"
  409. "newcommand" "renewcommand" "providecommand"
  410. "newenvironment" "renewenvironment"
  411. "newtheorem" "renewtheorem")
  412. t))
  413. (variables (regexp-opt
  414. '("newcounter" "newcounter*" "setcounter" "addtocounter"
  415. "setlength" "addtolength" "settowidth")
  416. t))
  417. (includes (regexp-opt
  418. '("input" "include" "includeonly" "bibliography"
  419. "epsfig" "psfig" "epsf" "nofiles" "usepackage"
  420. "documentstyle" "documentclass" "verbatiminput"
  421. "includegraphics" "includegraphics*")
  422. t))
  423. (verbish (regexp-opt '("url" "nolinkurl" "path") t))
  424. ;; Miscellany.
  425. (slash "\\\\")
  426. (opt " *\\(\\[[^]]*\\] *\\)*")
  427. ;; This would allow highlighting \newcommand\CMD but requires
  428. ;; adapting subgroup numbers below.
  429. ;; (arg "\\(?:{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)\\|\\\\[a-z*]+\\)"))
  430. (inbraces-re (lambda (re)
  431. (concat "\\(?:[^{}\\]\\|\\\\.\\|" re "\\)")))
  432. (arg (concat "{\\(" (funcall inbraces-re "{[^}]*}") "+\\)")))
  433. `( ;; Highlight $$math$$ and $math$.
  434. ;; This is done at the very beginning so as to interact with the other
  435. ;; keywords in the same way as comments and strings.
  436. (,(concat "\\$\\$?\\(?:[^$\\{}]\\|\\\\.\\|{"
  437. (funcall inbraces-re
  438. (concat "{" (funcall inbraces-re "{[^}]*}") "*}"))
  439. "*}\\)+\\$?\\$")
  440. (0 'tex-math))
  441. ;; Heading args.
  442. (,(concat slash headings "\\*?" opt arg)
  443. ;; If ARG ends up matching too much (if the {} don't match, e.g.)
  444. ;; jit-lock will do funny things: when updating the buffer
  445. ;; the re-highlighting is only done locally so it will just
  446. ;; match the local line, but defer-contextually will
  447. ;; match more lines at a time, so ARG will end up matching
  448. ;; a lot more, which might suddenly include a comment
  449. ;; so you get things highlighted bold when you type them
  450. ;; but they get turned back to normal a little while later
  451. ;; because "there's already a face there".
  452. ;; Using `keep' works around this un-intuitive behavior as well
  453. ;; as improves the behavior in the very rare case where you do
  454. ;; have a comment in ARG.
  455. 3 font-lock-function-name-face keep)
  456. (,(concat slash "\\(?:provide\\|\\(?:re\\)?new\\)command\\** *\\(\\\\[A-Za-z@]+\\)")
  457. 1 font-lock-function-name-face keep)
  458. ;; Variable args.
  459. (,(concat slash variables " *" arg) 2 font-lock-variable-name-face)
  460. ;; Include args.
  461. (,(concat slash includes opt arg) 3 font-lock-builtin-face)
  462. ;; Verbatim-like args.
  463. (,(concat slash verbish opt arg) 3 'tex-verbatim)
  464. ;; Definitions. I think.
  465. ("^[ \t]*\\\\def *\\\\\\(\\(\\w\\|@\\)+\\)"
  466. 1 font-lock-function-name-face))))
  467. "Subdued expressions to highlight in TeX modes.")
  468. (defun tex-font-lock-append-prop (prop)
  469. (unless (memq (get-text-property (match-end 1) 'face)
  470. '(font-lock-comment-face tex-verbatim))
  471. prop))
  472. (defconst tex-font-lock-keywords-2
  473. (append tex-font-lock-keywords-1
  474. (eval-when-compile
  475. (let* (;;
  476. ;; Names of commands whose arg should be fontified with fonts.
  477. (bold (regexp-opt '("textbf" "textsc" "textup"
  478. "boldsymbol" "pmb")
  479. t))
  480. (italic (regexp-opt '("textit" "textsl" "emph") t))
  481. ;; FIXME: unimplemented yet.
  482. ;; (type (regexp-opt '("texttt" "textmd" "textrm" "textsf") t))
  483. ;;
  484. ;; Names of commands whose arg should be fontified as a citation.
  485. (citations (regexp-opt
  486. '("label" "ref" "pageref" "vref" "eqref"
  487. "cite" "nocite" "index" "glossary" "bibitem"
  488. ;; natbib's two variants of \cite:
  489. "citep" "citet"
  490. ;; These are text, rather than citations.
  491. ;; "caption" "footnote" "footnotemark" "footnotetext"
  492. )
  493. t))
  494. ;;
  495. ;; Names of commands that should be fontified.
  496. (specials-1 (regexp-opt '("\\" "\\*") t)) ;; "-"
  497. (specials-2 (regexp-opt
  498. '("linebreak" "nolinebreak" "pagebreak" "nopagebreak"
  499. "newline" "newpage" "clearpage" "cleardoublepage"
  500. "displaybreak" "allowdisplaybreaks"
  501. "enlargethispage")
  502. t))
  503. (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)")
  504. ;;
  505. ;; Miscellany.
  506. (slash "\\\\")
  507. (opt " *\\(\\[[^]]*\\] *\\)*")
  508. (args "\\(\\(?:[^{}&\\]+\\|\\\\.\\|{[^}]*}\\)+\\)")
  509. (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)"))
  510. (list
  511. ;;
  512. ;; Citation args.
  513. (list (concat slash citations opt arg) 3 'font-lock-constant-face)
  514. ;;
  515. ;; Text between `` quotes ''.
  516. (cons (concat (regexp-opt `("``" "\"<" "\"`" "<<" "«") t)
  517. "[^'\">{]+" ;a bit pessimistic
  518. (regexp-opt `("''" "\">" "\"'" ">>" "»") t))
  519. 'font-lock-string-face)
  520. ;;
  521. ;; Command names, special and general.
  522. (cons (concat slash specials-1) 'font-lock-warning-face)
  523. (list (concat "\\(" slash specials-2 "\\)\\([^a-zA-Z@]\\|\\'\\)")
  524. 1 'font-lock-warning-face)
  525. (concat slash general)
  526. ;;
  527. ;; Font environments. It seems a bit dubious to use `bold' etc. faces
  528. ;; since we might not be able to display those fonts.
  529. (list (concat slash bold " *" arg) 2
  530. '(tex-font-lock-append-prop 'bold) 'append)
  531. (list (concat slash italic " *" arg) 2
  532. '(tex-font-lock-append-prop 'italic) 'append)
  533. ;; (list (concat slash type arg) 2 '(quote bold-italic) 'append)
  534. ;;
  535. ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
  536. (list (concat "\\\\\\(em\\|it\\|sl\\)\\>" args)
  537. 2 '(tex-font-lock-append-prop 'italic) 'append)
  538. ;; This is separate from the previous one because of cases like
  539. ;; {\em foo {\bf bar} bla} where both match.
  540. (list (concat "\\\\\\(bf\\(series\\)?\\)\\>" args)
  541. 3 '(tex-font-lock-append-prop 'bold) 'append)))))
  542. "Gaudy expressions to highlight in TeX modes.")
  543. (defun tex-font-lock-suscript (pos)
  544. (unless (or (memq (get-text-property pos 'face)
  545. '(font-lock-constant-face font-lock-builtin-face
  546. font-lock-comment-face tex-verbatim))
  547. ;; Check for backslash quoting
  548. (let ((odd nil)
  549. (pos pos))
  550. (while (eq (char-before pos) ?\\)
  551. (setq pos (1- pos) odd (not odd)))
  552. odd))
  553. (if (eq (char-after pos) ?_)
  554. `(face subscript display (raise ,(car tex-font-script-display)))
  555. `(face superscript display (raise ,(cadr tex-font-script-display))))))
  556. (defun tex-font-lock-match-suscript (limit)
  557. "Match subscript and superscript patterns up to LIMIT."
  558. (when (and tex-fontify-script
  559. (re-search-forward "[_^] *\\([^\n\\{}]\\|\
  560. \\\\\\([a-zA-Z@]+\\|[^ \t\n]\\)\\|\\({\\)\\)" limit t))
  561. (when (match-end 3)
  562. (let ((beg (match-beginning 3))
  563. (end (save-restriction
  564. (narrow-to-region (point-min) limit)
  565. (condition-case nil (scan-lists (point) 1 1) (error nil)))))
  566. (store-match-data (if end
  567. (list (match-beginning 0) end beg end)
  568. (list beg beg beg beg)))))
  569. t))
  570. (defconst tex-font-lock-keywords-3
  571. (append tex-font-lock-keywords-2
  572. '((tex-font-lock-match-suscript
  573. (1 (tex-font-lock-suscript (match-beginning 0)) append))))
  574. "Experimental expressions to highlight in TeX modes.")
  575. (defconst tex-font-lock-keywords tex-font-lock-keywords-1
  576. "Default expressions to highlight in TeX modes.")
  577. (defvar tex-verbatim-environments
  578. '("verbatim" "verbatim*"))
  579. (put 'tex-verbatim-environments 'safe-local-variable
  580. (lambda (x) (null (delq t (mapcar #'stringp x)))))
  581. (eval-when-compile
  582. (defconst tex-syntax-propertize-rules
  583. (syntax-propertize-precompile-rules
  584. ("\\\\verb\\**\\([^a-z@*]\\)"
  585. (1 (prog1 "\""
  586. (tex-font-lock-verb
  587. (match-beginning 0) (char-after (match-beginning 1))))))))
  588. (defconst latex-syntax-propertize-rules
  589. (syntax-propertize-precompile-rules
  590. tex-syntax-propertize-rules
  591. ("\\\\\\(?:end\\|begin\\) *\\({[^\n{}]*}\\)"
  592. (1 (ignore
  593. (tex-env-mark (match-beginning 0)
  594. (match-beginning 1) (match-end 1))))))))
  595. (defun tex-env-mark (cmd start end)
  596. (when (= cmd (line-beginning-position))
  597. (let ((arg (buffer-substring-no-properties (1+ start) (1- end))))
  598. (when (member arg tex-verbatim-environments)
  599. (if (eq ?b (char-after (1+ cmd)))
  600. ;; \begin
  601. (put-text-property (line-end-position)
  602. (line-beginning-position 2)
  603. 'syntax-table (string-to-syntax "< c"))
  604. ;; In the case of an empty verbatim env, the \n after the \begin is
  605. ;; the same as the \n before the \end. Lucky for us, the "> c"
  606. ;; property associated to the \end will be placed afterwards, so it
  607. ;; will override the "< c".
  608. (put-text-property (1- cmd) cmd
  609. 'syntax-table (string-to-syntax "> c"))
  610. ;; The text between \end{verbatim} and \n is ignored, so we'll treat
  611. ;; it as a comment.
  612. (put-text-property end (min (1+ end) (line-end-position))
  613. 'syntax-table (string-to-syntax "<"))))))
  614. ;; Mark env args for possible electric pairing.
  615. (unless (get-char-property (1+ start) 'text-clones) ;Already paired-up.
  616. (put-text-property start end 'latex-env-pair t)))
  617. (define-minor-mode latex-electric-env-pair-mode
  618. "Toggle Latex Electric Env Pair mode.
  619. With a prefix argument ARG, enable the mode if ARG is positive,
  620. and disable it otherwise. If called from Lisp, enable it if ARG
  621. is omitted or nil.
  622. Latex Electric Env Pair mode is a buffer-local minor mode for use
  623. with `latex-mode'. When enabled, typing a \\begin or \\end tag
  624. automatically inserts its partner."
  625. :lighter "/e"
  626. (if latex-electric-env-pair-mode
  627. (add-hook 'before-change-functions
  628. #'latex-env-before-change nil 'local)
  629. (remove-hook 'before-change-functions
  630. #'latex-env-before-change 'local)))
  631. (defun latex-env-before-change (start end)
  632. (when (get-text-property start 'latex-env-pair)
  633. (condition-case err
  634. (with-silent-modifications
  635. ;; Remove properties even if don't find a pair.
  636. (remove-text-properties
  637. (previous-single-property-change (1+ start) 'latex-env-pair)
  638. (next-single-property-change start 'latex-env-pair)
  639. '(latex-env-pair))
  640. (unless (or (get-char-property start 'text-clones)
  641. (get-char-property (1+ start) 'text-clones)
  642. (save-excursion
  643. (goto-char start)
  644. (not (re-search-backward
  645. "\\\\\\(?:end\\|begi\\(n\\)\\) *{"
  646. (line-beginning-position) t))))
  647. (let ((cmd-start (match-beginning 0))
  648. (type (match-end 1)) ;nil for \end, else \begin.
  649. (arg-start (1- (match-end 0))))
  650. (save-excursion
  651. (goto-char (match-end 0))
  652. (when (and (looking-at "[^\n{}]*}")
  653. (> (match-end 0) end))
  654. (let ((arg-end (match-end 0)))
  655. (if (null type) ;\end
  656. (progn (goto-char arg-end)
  657. (latex-forward-sexp -1)
  658. (forward-word-strictly 1))
  659. (goto-char cmd-start)
  660. (latex-forward-sexp 1)
  661. (let (forward-sexp-function) (backward-sexp)))
  662. (when (looking-at
  663. (regexp-quote (buffer-substring arg-start arg-end)))
  664. (text-clone-create arg-start arg-end))))))))
  665. (scan-error nil)
  666. (error (message "Error in latex-env-before-change: %s" err)))))
  667. (defun tex-font-lock-unfontify-region (beg end)
  668. (font-lock-default-unfontify-region beg end)
  669. (while (< beg end)
  670. (let ((next (next-single-property-change beg 'display nil end))
  671. (prop (get-text-property beg 'display)))
  672. (if (and (eq (car-safe prop) 'raise)
  673. (member (car-safe (cdr prop)) tex-font-script-display)
  674. (null (cddr prop)))
  675. (put-text-property beg next 'display nil))
  676. (setq beg next))))
  677. (defcustom tex-suscript-height-ratio 0.8
  678. "Ratio of subscript/superscript height to that of the preceding text.
  679. In nested subscript/superscript, this factor is applied repeatedly,
  680. subject to the limit set by `tex-suscript-height-minimum'."
  681. :type 'float
  682. :group 'tex
  683. :version "23.1")
  684. (defcustom tex-suscript-height-minimum 0.0
  685. "Integer or float limiting the minimum size of subscript/superscript text.
  686. An integer is an absolute height in units of 1/10 point, a float
  687. is a height relative to that of the default font. Zero means no minimum."
  688. :type '(choice (integer :tag "Integer height in 1/10 point units")
  689. (float :tag "Fraction of default font height"))
  690. :group 'tex
  691. :version "23.1")
  692. (defun tex-suscript-height (height)
  693. "Return the integer height of subscript/superscript font in 1/10 points.
  694. Not smaller than the value set by `tex-suscript-height-minimum'."
  695. (ceiling (max (if (integerp tex-suscript-height-minimum)
  696. tex-suscript-height-minimum
  697. ;; For bootstrapping.
  698. (condition-case nil
  699. (* tex-suscript-height-minimum
  700. (face-attribute 'default :height))
  701. (error 0)))
  702. ;; NB assumes height is integer.
  703. (* height tex-suscript-height-ratio))))
  704. (defface superscript
  705. '((t :height tex-suscript-height)) ;; :raise 0.2
  706. "Face used for superscripts."
  707. :group 'tex)
  708. (defface subscript
  709. '((t :height tex-suscript-height)) ;; :raise -0.2
  710. "Face used for subscripts."
  711. :group 'tex)
  712. (defface tex-math
  713. '((t :inherit font-lock-string-face))
  714. "Face used to highlight TeX math expressions."
  715. :group 'tex)
  716. (defface tex-verbatim
  717. '((t :inherit fixed-pitch-serif))
  718. "Face used to highlight TeX verbatim environments."
  719. :group 'tex)
  720. (defun tex-font-lock-verb (start delim)
  721. "Place syntax table properties on the \\verb construct.
  722. START is the position of the \\ and DELIM is the delimiter char."
  723. ;; Do nothing if the \verb construct is itself inside a comment or
  724. ;; verbatim env.
  725. (unless (nth 8 (save-excursion (syntax-ppss start)))
  726. ;; Let's find the end and mark it.
  727. (let ((afterdelim (point)))
  728. (skip-chars-forward (string ?^ delim) (line-end-position))
  729. (if (eolp)
  730. ;; "LaTeX Error: \verb ended by end of line."
  731. ;; Remove the syntax-table property we've just put on the
  732. ;; start-delimiter, so it doesn't spill over subsequent lines.
  733. (put-text-property (1- afterdelim) afterdelim
  734. 'syntax-table nil)
  735. (when (eq (char-syntax (preceding-char)) ?/)
  736. (put-text-property (1- (point)) (point)
  737. 'syntax-table (string-to-syntax ".")))
  738. (put-text-property (point) (1+ (point))
  739. 'syntax-table (string-to-syntax "\""))))))
  740. ;; Use string syntax but math face for $...$.
  741. (defun tex-font-lock-syntactic-face-function (state)
  742. (let ((char (nth 3 state)))
  743. (cond
  744. ((not char)
  745. (if (eq 2 (nth 7 state)) 'tex-verbatim font-lock-comment-face))
  746. ((eq char ?$) 'tex-math)
  747. ;; A \verb element.
  748. (t 'tex-verbatim))))
  749. (defun tex-define-common-keys (keymap)
  750. "Define the keys that we want defined both in TeX mode and in the TeX shell."
  751. (define-key keymap "\C-c\C-k" 'tex-kill-job)
  752. (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer)
  753. (define-key keymap "\C-c\C-q" 'tex-show-print-queue)
  754. (define-key keymap "\C-c\C-p" 'tex-print)
  755. (define-key keymap "\C-c\C-v" 'tex-view)
  756. (define-key keymap [menu-bar tex] (cons "TeX" (make-sparse-keymap "TeX")))
  757. (define-key keymap [menu-bar tex tex-kill-job]
  758. '(menu-item "Tex Kill" tex-kill-job :enable (tex-shell-running)))
  759. (define-key keymap [menu-bar tex tex-recenter-output-buffer]
  760. '(menu-item "Tex Recenter" tex-recenter-output-buffer
  761. :enable (get-buffer "*tex-shell*")))
  762. (define-key keymap [menu-bar tex tex-show-print-queue]
  763. '("Show Print Queue" . tex-show-print-queue))
  764. (define-key keymap [menu-bar tex tex-alt-print]
  765. '(menu-item "Tex Print (alt printer)" tex-alt-print
  766. :enable (stringp tex-print-file)))
  767. (define-key keymap [menu-bar tex tex-print]
  768. '(menu-item "Tex Print" tex-print :enable (stringp tex-print-file)))
  769. (define-key keymap [menu-bar tex tex-view]
  770. '(menu-item "Tex View" tex-view :enable (stringp tex-print-file))))
  771. (defvar tex-mode-map
  772. (let ((map (make-sparse-keymap)))
  773. (set-keymap-parent map text-mode-map)
  774. (tex-define-common-keys map)
  775. (define-key map "\"" 'tex-insert-quote)
  776. (define-key map "\n" 'tex-handle-newline)
  777. (define-key map "\M-\r" 'latex-insert-item)
  778. (define-key map "\C-c}" 'up-list)
  779. (define-key map "\C-c{" 'tex-insert-braces)
  780. (define-key map "\C-c\C-r" 'tex-region)
  781. (define-key map "\C-c\C-b" 'tex-buffer)
  782. (define-key map "\C-c\C-f" 'tex-file)
  783. (define-key map "\C-c\C-c" 'tex-compile)
  784. (define-key map "\C-c\C-i" 'tex-bibtex-file)
  785. (define-key map "\C-c\C-o" 'latex-insert-block)
  786. ;; Redundant keybindings, for consistency with SGML mode.
  787. (define-key map "\C-c\C-t" 'latex-insert-block)
  788. (define-key map "\C-c]" 'latex-close-block)
  789. (define-key map "\C-c/" 'latex-close-block)
  790. (define-key map "\C-c\C-e" 'latex-close-block)
  791. (define-key map "\C-c\C-u" 'tex-goto-last-unclosed-latex-block)
  792. (define-key map "\C-c\C-m" 'tex-feed-input)
  793. (define-key map [(control return)] 'tex-feed-input)
  794. (define-key map [menu-bar tex tex-bibtex-file]
  795. '("BibTeX File" . tex-bibtex-file))
  796. (define-key map [menu-bar tex tex-validate-region]
  797. '(menu-item "Validate Region" tex-validate-region :enable mark-active))
  798. (define-key map [menu-bar tex tex-validate-buffer]
  799. '("Validate Buffer" . tex-validate-buffer))
  800. (define-key map [menu-bar tex tex-region]
  801. '(menu-item "TeX Region" tex-region :enable mark-active))
  802. (define-key map [menu-bar tex tex-buffer]
  803. '("TeX Buffer" . tex-buffer))
  804. (define-key map [menu-bar tex tex-file] '("TeX File" . tex-file))
  805. map)
  806. "Keymap shared by TeX modes.")
  807. (defvar latex-mode-map
  808. (let ((map (make-sparse-keymap)))
  809. (set-keymap-parent map tex-mode-map)
  810. (define-key map "\C-c\C-s" 'latex-split-block)
  811. map)
  812. "Keymap for `latex-mode'. See also `tex-mode-map'.")
  813. (defvar plain-tex-mode-map
  814. (let ((map (make-sparse-keymap)))
  815. (set-keymap-parent map tex-mode-map)
  816. map)
  817. "Keymap for `plain-tex-mode'. See also `tex-mode-map'.")
  818. (defvar tex-shell-map
  819. (let ((m (make-sparse-keymap)))
  820. (set-keymap-parent m shell-mode-map)
  821. (tex-define-common-keys m)
  822. m)
  823. "Keymap for the TeX shell.
  824. Inherits `shell-mode-map' with a few additions.")
  825. (defvar tex-face-alist
  826. '((bold . "{\\bf ")
  827. (italic . "{\\it ")
  828. (bold-italic . "{\\bi ") ; hypothetical
  829. (underline . "\\underline{")
  830. (default . "{\\rm "))
  831. "Alist of face and TeX font name for facemenu.")
  832. (defvar tex-latex-face-alist
  833. `((italic . "{\\em ")
  834. ,@tex-face-alist)
  835. "Alist of face and LaTeX font name for facemenu.")
  836. (defun tex-facemenu-add-face-function (face _end)
  837. (or (cdr (assq face tex-face-alist))
  838. (or (and (consp face)
  839. (consp (car face))
  840. (null (cdr face))
  841. (eq major-mode 'latex-mode)
  842. ;; This actually requires the `color' LaTeX package.
  843. (cond ((eq (caar face) :foreground)
  844. (format "{\\color{%s} " (cadr (car face))))
  845. ((eq (caar face) :background)
  846. (format "\\colorbox{%s}{" (cadr (car face))))))
  847. (error "Face %s not configured for %s mode" face mode-name))))
  848. ;; This would be a lot simpler if we just used a regexp search,
  849. ;; but then it would be too slow.
  850. (defun tex-guess-mode ()
  851. (let ((mode tex-default-mode) slash comment)
  852. (save-excursion
  853. (goto-char (point-min))
  854. (while (and (setq slash (search-forward "\\" nil t))
  855. (setq comment (let ((search-end (point)))
  856. (save-excursion
  857. (beginning-of-line)
  858. (search-forward "%" search-end t))))))
  859. (when (and slash (not comment))
  860. (setq mode
  861. (if (looking-at
  862. (eval-when-compile
  863. (concat
  864. (regexp-opt '("documentstyle" "documentclass"
  865. "begin" "subsection" "section"
  866. "part" "chapter" "newcommand"
  867. "renewcommand" "RequirePackage") 'words)
  868. "\\|NeedsTeXFormat{LaTeX")))
  869. (if (and (looking-at
  870. "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}")
  871. ;; SliTeX is almost never used any more nowadays.
  872. (tex-executable-exists-p slitex-run-command))
  873. 'slitex-mode
  874. 'latex-mode)
  875. 'plain-tex-mode))))
  876. (funcall mode)))
  877. ;; `tex-mode' plays two roles: it's the parent of several sub-modes
  878. ;; but it's also the function that chooses between those submodes.
  879. ;; To tell the difference between those two cases where the function
  880. ;; might be called, we check `delay-mode-hooks'.
  881. (define-derived-mode tex-mode text-mode "generic-TeX"
  882. (tex-common-initialization))
  883. ;; We now move the function and define it again. This gives a warning
  884. ;; in the byte-compiler :-( but it's difficult to avoid because
  885. ;; `define-derived-mode' will necessarily define the function once
  886. ;; and we need to define it a second time for `autoload' to get the
  887. ;; proper docstring.
  888. (defalias 'tex-mode-internal (symbol-function 'tex-mode))
  889. ;; Suppress the byte-compiler warning about multiple definitions.
  890. ;; This is a) ugly, and b) cheating, but this was the last
  891. ;; remaining warning from byte-compiling all of Emacs...
  892. (eval-when-compile
  893. (setq byte-compile-function-environment
  894. (delq (assq 'tex-mode byte-compile-function-environment)
  895. byte-compile-function-environment)))
  896. ;;;###autoload
  897. (defun tex-mode ()
  898. "Major mode for editing files of input for TeX, LaTeX, or SliTeX.
  899. Tries to determine (by looking at the beginning of the file) whether
  900. this file is for plain TeX, LaTeX, or SliTeX and calls `plain-tex-mode',
  901. `latex-mode', or `slitex-mode', respectively. If it cannot be determined,
  902. such as if there are no commands in the file, the value of `tex-default-mode'
  903. says which mode to use."
  904. (interactive)
  905. (if delay-mode-hooks
  906. ;; We're called from one of the children already.
  907. (tex-mode-internal)
  908. (tex-guess-mode)))
  909. ;; The following three autoloaded aliases appear to conflict with
  910. ;; AUCTeX. However, even though AUCTeX uses the mixed case variants
  911. ;; for all mode relevant variables and hooks, the invocation function
  912. ;; and setting of `major-mode' themselves need to be lowercase for
  913. ;; AUCTeX to provide a fully functional user-level replacement. So
  914. ;; these aliases should remain as they are, in particular since AUCTeX
  915. ;; users are likely to use them.
  916. ;;;###autoload
  917. (defalias 'TeX-mode 'tex-mode)
  918. ;;;###autoload
  919. (defalias 'plain-TeX-mode 'plain-tex-mode)
  920. ;;;###autoload
  921. (defalias 'LaTeX-mode 'latex-mode)
  922. ;;;###autoload
  923. (define-derived-mode plain-tex-mode tex-mode "TeX"
  924. "Major mode for editing files of input for plain TeX.
  925. Makes $ and } display the characters they match.
  926. Makes \" insert \\=`\\=` when it seems to be the beginning of a quotation,
  927. and \\='\\=' when it appears to be the end; it inserts \" only after a \\.
  928. Use \\[tex-region] to run TeX on the current region, plus a \"header\"
  929. copied from the top of the file (containing macro definitions, etc.),
  930. running TeX under a special subshell. \\[tex-buffer] does the whole buffer.
  931. \\[tex-file] saves the buffer and then processes the file.
  932. \\[tex-print] prints the .dvi file made by any of these.
  933. \\[tex-view] previews the .dvi file made by any of these.
  934. \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
  935. Use \\[tex-validate-buffer] to check buffer for paragraphs containing
  936. mismatched $'s or braces.
  937. Special commands:
  938. \\{plain-tex-mode-map}
  939. Mode variables:
  940. tex-run-command
  941. Command string used by \\[tex-region] or \\[tex-buffer].
  942. tex-directory
  943. Directory in which to create temporary files for TeX jobs
  944. run by \\[tex-region] or \\[tex-buffer].
  945. tex-dvi-print-command
  946. Command string used by \\[tex-print] to print a .dvi file.
  947. tex-alt-dvi-print-command
  948. Alternative command string used by \\[tex-print] (when given a prefix
  949. argument) to print a .dvi file.
  950. tex-dvi-view-command
  951. Command string used by \\[tex-view] to preview a .dvi file.
  952. tex-show-queue-command
  953. Command string used by \\[tex-show-print-queue] to show the print
  954. queue that \\[tex-print] put your job on.
  955. Entering Plain-tex mode runs the hook `text-mode-hook', then the hook
  956. `tex-mode-hook', and finally the hook `plain-tex-mode-hook'. When the
  957. special subshell is initiated, the hook `tex-shell-hook' is run."
  958. (setq-local tex-command tex-run-command)
  959. (setq-local tex-start-of-header "%\\*\\*start of header")
  960. (setq-local tex-end-of-header "%\\*\\*end of header")
  961. (setq-local tex-trailer "\\bye\n"))
  962. ;;;###autoload
  963. (define-derived-mode latex-mode tex-mode "LaTeX"
  964. "Major mode for editing files of input for LaTeX.
  965. Makes $ and } display the characters they match.
  966. Makes \" insert \\=`\\=` when it seems to be the beginning of a quotation,
  967. and \\='\\=' when it appears to be the end; it inserts \" only after a \\.
  968. Use \\[tex-region] to run LaTeX on the current region, plus the preamble
  969. copied from the top of the file (containing \\documentstyle, etc.),
  970. running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer.
  971. \\[tex-file] saves the buffer and then processes the file.
  972. \\[tex-print] prints the .dvi file made by any of these.
  973. \\[tex-view] previews the .dvi file made by any of these.
  974. \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
  975. Use \\[tex-validate-buffer] to check buffer for paragraphs containing
  976. mismatched $'s or braces.
  977. Special commands:
  978. \\{latex-mode-map}
  979. Mode variables:
  980. latex-run-command
  981. Command string used by \\[tex-region] or \\[tex-buffer].
  982. tex-directory
  983. Directory in which to create temporary files for LaTeX jobs
  984. run by \\[tex-region] or \\[tex-buffer].
  985. tex-dvi-print-command
  986. Command string used by \\[tex-print] to print a .dvi file.
  987. tex-alt-dvi-print-command
  988. Alternative command string used by \\[tex-print] (when given a prefix
  989. argument) to print a .dvi file.
  990. tex-dvi-view-command
  991. Command string used by \\[tex-view] to preview a .dvi file.
  992. tex-show-queue-command
  993. Command string used by \\[tex-show-print-queue] to show the print
  994. queue that \\[tex-print] put your job on.
  995. Entering Latex mode runs the hook `text-mode-hook', then
  996. `tex-mode-hook', and finally `latex-mode-hook'. When the special
  997. subshell is initiated, `tex-shell-hook' is run."
  998. (setq-local tex-command latex-run-command)
  999. (setq-local tex-start-of-header "\\\\document\\(style\\|class\\)")
  1000. (setq-local tex-end-of-header "\\\\begin\\s-*{document}")
  1001. (setq-local tex-trailer "\\end{document}\n")
  1002. ;; A line containing just $$ is treated as a paragraph separator.
  1003. ;; A line starting with $$ starts a paragraph,
  1004. ;; but does not separate paragraphs if it has more stuff on it.
  1005. (setq paragraph-start
  1006. (concat "[ \t]*\\(\\$\\$\\|"
  1007. "\\\\[][]\\|"
  1008. "\\\\" (regexp-opt (append
  1009. (mapcar #'car latex-section-alist)
  1010. '("begin" "label" "end"
  1011. "item" "bibitem" "newline" "noindent"
  1012. "newpage" "footnote" "marginpar"
  1013. "parbox" "caption"))
  1014. t)
  1015. "\\>\\|\\\\[a-z]*" (regexp-opt '("space" "skip" "page") t)
  1016. "\\>\\)"))
  1017. (setq paragraph-separate
  1018. (concat "[\f%]\\|[ \t]*\\($\\|"
  1019. "\\\\[][]\\|"
  1020. "\\\\" (regexp-opt (append
  1021. (mapcar #'car latex-section-alist)
  1022. '("begin" "label" "end" ))
  1023. t)
  1024. "\\>\\|\\\\\\(" (regexp-opt '("item" "bibitem" "newline"
  1025. "noindent" "newpage" "footnote"
  1026. "marginpar" "parbox" "caption"))
  1027. "\\|\\$\\$\\|[a-z]*\\(space\\|skip\\|page[a-z]*\\)"
  1028. "\\>\\)[ \t]*\\($\\|%\\)\\)"))
  1029. (setq-local imenu-create-index-function #'latex-imenu-create-index)
  1030. (setq-local tex-face-alist tex-latex-face-alist)
  1031. (add-hook 'fill-nobreak-predicate #'latex-fill-nobreak-predicate nil t)
  1032. (setq-local indent-line-function #'latex-indent)
  1033. (setq-local fill-indent-according-to-mode t)
  1034. (add-hook 'completion-at-point-functions
  1035. #'latex-complete-data nil 'local)
  1036. (setq-local outline-regexp latex-outline-regexp)
  1037. (setq-local outline-level #'latex-outline-level)
  1038. (setq-local forward-sexp-function #'latex-forward-sexp)
  1039. (setq-local skeleton-end-hook nil))
  1040. ;;;###autoload
  1041. (define-derived-mode slitex-mode latex-mode "SliTeX"
  1042. "Major mode for editing files of input for SliTeX.
  1043. Makes $ and } display the characters they match.
  1044. Makes \" insert \\=`\\=` when it seems to be the beginning of a quotation,
  1045. and \\='\\=' when it appears to be the end; it inserts \" only after a \\.
  1046. Use \\[tex-region] to run SliTeX on the current region, plus the preamble
  1047. copied from the top of the file (containing \\documentstyle, etc.),
  1048. running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer.
  1049. \\[tex-file] saves the buffer and then processes the file.
  1050. \\[tex-print] prints the .dvi file made by any of these.
  1051. \\[tex-view] previews the .dvi file made by any of these.
  1052. \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
  1053. Use \\[tex-validate-buffer] to check buffer for paragraphs containing
  1054. mismatched $'s or braces.
  1055. Special commands:
  1056. \\{slitex-mode-map}
  1057. Mode variables:
  1058. slitex-run-command
  1059. Command string used by \\[tex-region] or \\[tex-buffer].
  1060. tex-directory
  1061. Directory in which to create temporary files for SliTeX jobs
  1062. run by \\[tex-region] or \\[tex-buffer].
  1063. tex-dvi-print-command
  1064. Command string used by \\[tex-print] to print a .dvi file.
  1065. tex-alt-dvi-print-command
  1066. Alternative command string used by \\[tex-print] (when given a prefix
  1067. argument) to print a .dvi file.
  1068. tex-dvi-view-command
  1069. Command string used by \\[tex-view] to preview a .dvi file.
  1070. tex-show-queue-command
  1071. Command string used by \\[tex-show-print-queue] to show the print
  1072. queue that \\[tex-print] put your job on.
  1073. Entering SliTeX mode runs the hook `text-mode-hook', then the hook
  1074. `tex-mode-hook', then the hook `latex-mode-hook', and finally the hook
  1075. `slitex-mode-hook'. When the special subshell is initiated, the hook
  1076. `tex-shell-hook' is run."
  1077. (setq tex-command slitex-run-command)
  1078. (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}"))
  1079. (defvar tildify-space-string)
  1080. (defvar tildify-foreach-region-function)
  1081. (declare-function tildify-foreach-ignore-environments
  1082. "tildify" (pairs callback _beg end))
  1083. (defvar tex--prettify-symbols-alist)
  1084. (defun tex-common-initialization ()
  1085. ;; Regexp isearch should accept newline and formfeed as whitespace.
  1086. (setq-local search-whitespace-regexp "[ \t\r\n\f]+")
  1087. ;; Use tilde as hard-space character in tildify package.
  1088. (setq-local tildify-space-string "~")
  1089. ;; FIXME: Use the fact that we're parsing the document already
  1090. ;; rather than using regex-based filtering.
  1091. (setq-local tildify-foreach-region-function
  1092. (apply-partially
  1093. #'tildify-foreach-ignore-environments
  1094. `(("\\\\\\\\" . "") ; do not remove this
  1095. (,(eval-when-compile
  1096. (concat "\\\\begin{\\("
  1097. (regexp-opt '("verbatim" "math" "displaymath"
  1098. "equation" "eqnarray" "eqnarray*"))
  1099. "\\)}"))
  1100. . ("\\\\end{" 1 "}"))
  1101. ("\\\\verb\\*?\\(.\\)" . (1))
  1102. ("\\$\\$?" . (0))
  1103. ("\\\\(" . "\\\\)")
  1104. ("\\\\[[]" . "\\\\[]]")
  1105. ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" . "")
  1106. ("%" . "$"))))
  1107. ;; A line containing just $$ is treated as a paragraph separator.
  1108. (setq-local paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
  1109. ;; A line starting with $$ starts a paragraph,
  1110. ;; but does not separate paragraphs if it has more stuff on it.
  1111. (setq-local paragraph-separate "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$[ \t]*$")
  1112. (setq-local add-log-current-defun-function #'tex-current-defun-name)
  1113. (setq-local comment-start "%")
  1114. (setq-local comment-add 1)
  1115. (setq-local comment-start-skip
  1116. "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)")
  1117. (setq-local parse-sexp-ignore-comments t)
  1118. (setq-local compare-windows-whitespace 'tex-categorize-whitespace)
  1119. (setq-local facemenu-add-face-function 'tex-facemenu-add-face-function)
  1120. (setq-local facemenu-end-add-face "}")
  1121. (setq-local facemenu-remove-face-function t)
  1122. (setq-local font-lock-defaults
  1123. '((tex-font-lock-keywords tex-font-lock-keywords-1
  1124. tex-font-lock-keywords-2 tex-font-lock-keywords-3)
  1125. nil nil nil nil
  1126. ;; Who ever uses that anyway ???
  1127. (font-lock-mark-block-function . mark-paragraph)
  1128. (font-lock-syntactic-face-function
  1129. . tex-font-lock-syntactic-face-function)
  1130. (font-lock-unfontify-region-function
  1131. . tex-font-lock-unfontify-region)))
  1132. (setq-local prettify-symbols-alist tex--prettify-symbols-alist)
  1133. (add-function :override (local 'prettify-symbols-compose-predicate)
  1134. #'tex--prettify-symbols-compose-p)
  1135. (setq-local syntax-propertize-function
  1136. (syntax-propertize-rules latex-syntax-propertize-rules))
  1137. ;; TABs in verbatim environments don't do what you think.
  1138. (setq-local indent-tabs-mode nil)
  1139. ;; Other vars that should be buffer-local.
  1140. (make-local-variable 'tex-command)
  1141. (make-local-variable 'tex-start-of-header)
  1142. (make-local-variable 'tex-end-of-header)
  1143. (make-local-variable 'tex-trailer))
  1144. (defun tex-categorize-whitespace (backward-limit)
  1145. ;; compare-windows-whitespace is set to this.
  1146. ;; This is basically a finite-state machine.
  1147. ;; Returns a symbol telling how TeX would treat
  1148. ;; the whitespace we are looking at: null, space, or par.
  1149. (let ((category 'null)
  1150. (not-finished t))
  1151. (skip-chars-backward " \t\n\f" backward-limit)
  1152. (while not-finished
  1153. (cond ((looking-at "[ \t]+")
  1154. (goto-char (match-end 0))
  1155. (if (eq category 'null)
  1156. (setq category 'space)))
  1157. ((looking-at "\n")
  1158. (cond ((eq category 'newline)
  1159. (setq category 'par)
  1160. (setq not-finished nil))
  1161. (t
  1162. (setq category 'newline) ;a strictly internal state
  1163. (goto-char (match-end 0)))))
  1164. ((looking-at "\f+")
  1165. (setq category 'par)
  1166. (setq not-finished nil))
  1167. (t
  1168. (setq not-finished nil))))
  1169. (skip-chars-forward " \t\n\f")
  1170. (if (eq category 'newline)
  1171. 'space ;TeX doesn't distinguish
  1172. category)))
  1173. (defun tex-insert-quote (arg)
  1174. "Insert the appropriate quote marks for TeX.
  1175. Inserts the value of `tex-open-quote' (normally \\=`\\=`) or `tex-close-quote'
  1176. \(normally \\='\\=') depending on the context. With prefix argument, always
  1177. inserts \" characters."
  1178. (interactive "*P")
  1179. ;; Discover if we'll be inserting normal double quotes.
  1180. ;;
  1181. (if (or arg (memq (char-syntax (preceding-char)) '(?/ ?\\))
  1182. (eq (get-text-property (point) 'face) 'tex-verbatim)
  1183. (nth 4 (syntax-ppss)) ; non-nil if point is in a TeX comment
  1184. ;; Discover if a preceding occurrence of `tex-open-quote'
  1185. ;; should be morphed to a normal double quote.
  1186. ;;
  1187. (and (>= (point) (+ (point-min) (length tex-open-quote)))
  1188. (save-excursion
  1189. (backward-char (length tex-open-quote))
  1190. (when (or (looking-at (regexp-quote tex-open-quote))
  1191. (looking-at (regexp-quote tex-close-quote)))
  1192. (delete-char (length tex-open-quote))
  1193. (when (looking-at (regexp-quote tex-close-quote))
  1194. (delete-char (length tex-close-quote)))
  1195. t))))
  1196. ;; Insert the normal quote (eventually letting
  1197. ;; `electric-pair-mode' do its thing).
  1198. ;;
  1199. (self-insert-command (prefix-numeric-value arg))
  1200. ;; We'll be inserting fancy TeX quotes, but consider and imitate
  1201. ;; `electric-pair-mode''s two behaviors: pair-insertion and
  1202. ;; region wrapping.
  1203. ;;
  1204. (if (and electric-pair-mode (use-region-p))
  1205. (let* ((saved (point-marker)))
  1206. (goto-char (mark))
  1207. (insert (if (> saved (mark)) tex-open-quote tex-close-quote))
  1208. (goto-char saved)
  1209. (insert (if (> saved (mark)) tex-close-quote tex-open-quote)))
  1210. (if (or (memq (char-syntax (preceding-char)) '(?\( ?> ?\s))
  1211. (memq (preceding-char) '(?~ ?')))
  1212. ;; We're in an "opening" context
  1213. ;;
  1214. (if electric-pair-mode
  1215. (if (looking-at (regexp-quote tex-close-quote))
  1216. (forward-char (length tex-close-quote))
  1217. (insert tex-open-quote)
  1218. (insert tex-close-quote)
  1219. (backward-char (length tex-close-quote)))
  1220. (insert tex-open-quote))
  1221. ;; We're in a "closing" context.
  1222. ;;
  1223. (if (looking-at (regexp-quote tex-close-quote))
  1224. (forward-char (length tex-close-quote))
  1225. (insert tex-close-quote))))))
  1226. (defun tex-validate-buffer ()
  1227. "Check current buffer for paragraphs containing mismatched braces or $s.
  1228. Their positions are recorded in the buffer `*Occur*'.
  1229. To find a particular invalidity from `*Occur*', switch to that buffer
  1230. and type C-c C-c or click with mouse-2
  1231. on the line for the invalidity you want to see."
  1232. (interactive)
  1233. (let ((buffer (current-buffer))
  1234. (prevpos (point-min))
  1235. (linenum nil)
  1236. (num-matches 0))
  1237. (with-output-to-temp-buffer "*Occur*"
  1238. (princ "Mismatches:\n")
  1239. (with-current-buffer standard-output
  1240. (occur-mode)
  1241. ;; This won't actually work...Really, this whole thing should
  1242. ;; be rewritten instead of being a hack on top of occur.
  1243. (setq occur-revert-arguments (list nil 0 (list buffer))))
  1244. (save-excursion
  1245. (goto-char (point-max))
  1246. ;; Do a little shimmy to place point at the end of the last
  1247. ;; "real" paragraph. Need to avoid validating across an \end,
  1248. ;; because that blows up latex-forward-sexp.
  1249. (backward-paragraph)
  1250. (forward-paragraph)
  1251. (while (not (bobp))
  1252. ;; Scan the previous paragraph for invalidities.
  1253. (backward-paragraph)
  1254. (save-excursion
  1255. (or (tex-validate-region (point) (save-excursion
  1256. (forward-paragraph)
  1257. (point)))
  1258. (let ((end (line-beginning-position 2))
  1259. start tem)
  1260. (beginning-of-line)
  1261. (setq start (point))
  1262. ;; Keep track of line number as we scan,
  1263. ;; in a cumulative fashion.
  1264. (if linenum
  1265. (setq linenum (- linenum
  1266. (count-lines prevpos (point))))
  1267. (setq linenum (1+ (count-lines 1 start))))
  1268. (setq prevpos (point))
  1269. ;; Mention this mismatch in *Occur*.
  1270. ;; Since we scan from end of buffer to beginning,
  1271. ;; add each mismatch at the beginning of *Occur*.
  1272. (save-excursion
  1273. (setq tem (point-marker))
  1274. (set-buffer standard-output)
  1275. (goto-char (point-min))
  1276. ;; Skip "Mismatches:" header line.
  1277. (forward-line 1)
  1278. (setq num-matches (1+ num-matches))
  1279. (insert-buffer-substring buffer start end)
  1280. (let (text-beg (text-end (point-marker)))
  1281. (forward-char (- start end))
  1282. (setq text-beg (point-marker))
  1283. (insert (format "%3d: " linenum))
  1284. (add-text-properties
  1285. text-beg (- text-end 1)
  1286. '(mouse-face highlight
  1287. help-echo
  1288. "mouse-2: go to this invalidity"))
  1289. (put-text-property text-beg (- text-end 1)
  1290. 'occur-target tem))))))))
  1291. (with-current-buffer standard-output
  1292. (let ((no-matches (zerop num-matches)))
  1293. (if no-matches
  1294. (insert "None!\n"))
  1295. (if (called-interactively-p 'interactive)
  1296. (message (cond (no-matches "No mismatches found")
  1297. ((= num-matches 1) "1 mismatch found")
  1298. (t "%d mismatches found"))
  1299. num-matches)))))))
  1300. (defun tex-validate-region (start end)
  1301. "Check for mismatched braces or $'s in region.
  1302. Returns t if no mismatches. Returns nil and moves point to suspect
  1303. area if a mismatch is found."
  1304. (interactive "r")
  1305. (let ((failure-point nil) (max-possible-sexps (- end start)))
  1306. (save-excursion
  1307. (condition-case ()
  1308. (save-restriction
  1309. (narrow-to-region start end)
  1310. ;; First check that the open and close parens balance in numbers.
  1311. (goto-char start)
  1312. (while (and (not (eobp))
  1313. (<= 0 (setq max-possible-sexps
  1314. (1- max-possible-sexps))))
  1315. (forward-sexp 1))
  1316. ;; Now check that like matches like.
  1317. (goto-char start)
  1318. (while (re-search-forward "\\s(" nil t)
  1319. (save-excursion
  1320. (let ((pos (match-beginning 0)))
  1321. (goto-char pos)
  1322. (skip-chars-backward "\\\\") ; escaped parens
  1323. (forward-sexp 1)
  1324. (or (eq (preceding-char) (cdr (syntax-after pos)))
  1325. (eq (char-after pos) (cdr (syntax-after (1- (point)))))
  1326. (error "Mismatched parentheses"))))))
  1327. (error
  1328. (skip-syntax-forward " .>")
  1329. (setq failure-point (point)))))
  1330. (if failure-point (goto-char failure-point))
  1331. (not failure-point)))
  1332. (defun tex-handle-newline (inhibit-validation)
  1333. "Break a TeX paragraph with two newlines, or continue a comment.
  1334. If not in a comment, insert two newlines, breaking a paragraph for TeX,
  1335. and check for mismatched braces or $s in the paragraph being terminated
  1336. unless prefix arg INHIBIT-VALIDATION is non-nil to inhibit the checking.
  1337. Otherwise (in a comment), just insert a single continued comment line."
  1338. (interactive "*P")
  1339. (if (nth 4 (syntax-ppss)) ; non-nil if point is in a TeX comment
  1340. (comment-indent-new-line)
  1341. (tex-terminate-paragraph inhibit-validation)))
  1342. (defun tex-terminate-paragraph (inhibit-validation)
  1343. "Insert two newlines, breaking a paragraph for TeX.
  1344. Check for mismatched braces or $s in paragraph being terminated.
  1345. A prefix arg inhibits the checking."
  1346. (interactive "*P")
  1347. (or inhibit-validation
  1348. (save-excursion
  1349. ;; For the purposes of this, a "paragraph" is a block of text
  1350. ;; wherein all the brackets etc are expected to be balanced. It
  1351. ;; may start after a blank line (ie a "proper" paragraph), or
  1352. ;; a begin{} or end{} block, etc.
  1353. (tex-validate-region
  1354. (save-excursion
  1355. (backward-paragraph)
  1356. (point))
  1357. (point)))
  1358. (message "Paragraph being closed appears to contain a mismatch"))
  1359. (insert "\n\n"))
  1360. (define-skeleton tex-insert-braces
  1361. "Make a pair of braces and be poised to type inside of them."
  1362. nil
  1363. ?\{ _ ?})
  1364. ;; This function is used as the value of fill-nobreak-predicate
  1365. ;; in LaTeX mode. Its job is to prevent line-breaking inside
  1366. ;; of a \verb construct.
  1367. (defun latex-fill-nobreak-predicate ()
  1368. (save-excursion
  1369. (skip-chars-backward " ")
  1370. ;; Don't break after \ since `\ ' has special meaning.
  1371. (or (and (not (bobp)) (memq (char-syntax (char-before)) '(?\\ ?/)))
  1372. (let ((opoint (point))
  1373. inside)
  1374. (beginning-of-line)
  1375. (while (re-search-forward "\\\\verb\\(.\\)" opoint t)
  1376. (unless (re-search-forward (regexp-quote (match-string 1)) opoint t)
  1377. (setq inside t)))
  1378. inside))))
  1379. (defvar latex-block-default "enumerate")
  1380. (defvar latex-block-args-alist
  1381. '(("array" nil ?\{ (skeleton-read "Format: ") ?\})
  1382. ("tabular" nil ?\{ (skeleton-read "Format: ") ?\})
  1383. ("minipage" nil ?\{ (skeleton-read "Size: ") ?\})
  1384. ("picture" nil ?\( (skeleton-read "SizeX,SizeY: ") ?\))
  1385. ;; FIXME: This is right for Prosper, but not for seminar.
  1386. ;; ("slide" nil ?\{ (skeleton-read "Title: ") ?\})
  1387. )
  1388. "Skeleton element to use for arguments to particular environments.
  1389. Every element of the list has the form (NAME . SKEL-ELEM) where NAME is
  1390. the name of the environment and SKEL-ELEM is an element to use in
  1391. a skeleton (see `skeleton-insert').")
  1392. (defvar latex-block-body-alist
  1393. '(("enumerate" nil '(latex-insert-item) > _)
  1394. ("itemize" nil '(latex-insert-item) > _)
  1395. ("table" nil "\\caption{" > (skeleton-read "Caption: ") "}" > \n
  1396. '(if (and (boundp 'reftex-mode) reftex-mode) (reftex-label "table"))
  1397. \n _)
  1398. ("figure" nil > _ \n "\\caption{" > (skeleton-read "Caption: ") "}" > \n
  1399. '(if (and (boundp 'reftex-mode) reftex-mode) (reftex-label "table"))))
  1400. "Skeleton element to use for the body of particular environments.
  1401. Every element of the list has the form (NAME . SKEL-ELEM) where NAME is
  1402. the name of the environment and SKEL-ELEM is an element to use in
  1403. a skeleton (see `skeleton-insert').")
  1404. ;; Like tex-insert-braces, but for LaTeX.
  1405. (defalias 'tex-latex-block 'latex-insert-block)
  1406. (define-skeleton latex-insert-block
  1407. "Create a matching pair of lines \\begin{NAME} and \\end{NAME} at point.
  1408. Puts point on a blank line between them."
  1409. (let ((choice (completing-read (format "LaTeX block name [%s]: "
  1410. latex-block-default)
  1411. (latex-complete-envnames)
  1412. nil nil nil nil latex-block-default)))
  1413. (setq latex-block-default choice)
  1414. (unless (or (member choice latex-standard-block-names)
  1415. (member choice latex-block-names))
  1416. ;; Remember new block names for later completion.
  1417. (push choice latex-block-names))
  1418. choice)
  1419. \n "\\begin{" str "}"
  1420. (cdr (assoc str latex-block-args-alist))
  1421. > \n (or (cdr (assoc str latex-block-body-alist)) '(nil > _))
  1422. (unless (bolp) '\n)
  1423. "\\end{" str "}" > \n)
  1424. (define-skeleton latex-insert-item
  1425. "Insert an \\item macro."
  1426. nil
  1427. \n "\\item " >)
  1428. ;;;; LaTeX completion.
  1429. (defvar latex-complete-bibtex-cache nil)
  1430. (define-obsolete-function-alias 'latex-string-prefix-p
  1431. 'string-prefix-p "24.3")
  1432. (defvar bibtex-reference-key)
  1433. (declare-function reftex-get-bibfile-list "reftex-cite.el" ())
  1434. (defun latex-complete-bibtex-keys ()
  1435. (when (bound-and-true-p reftex-mode)
  1436. (lambda (key pred action)
  1437. (let ((re (concat "^[ \t]*@\\([a-zA-Z]+\\)[ \t\n]*\\([{(][ \t\n]*\\)"
  1438. (regexp-quote key)))
  1439. (files (reftex-get-bibfile-list))
  1440. keys)
  1441. (if (and (eq (car latex-complete-bibtex-cache)
  1442. (reftex-get-bibfile-list))
  1443. (string-prefix-p (nth 1 latex-complete-bibtex-cache)
  1444. key))
  1445. ;; Use the cache.
  1446. (setq keys (nth 2 latex-complete-bibtex-cache))
  1447. (dolist (file files)
  1448. (with-current-buffer (find-file-noselect file)
  1449. (goto-char (point-min))
  1450. (while (re-search-forward re nil t)
  1451. (goto-char (match-end 2))
  1452. (when (and (not (member-ignore-case (match-string 1)
  1453. '("c" "comment" "string")))
  1454. (looking-at bibtex-reference-key))
  1455. (push (match-string-no-properties 0) keys)))))
  1456. ;; Fill the cache.
  1457. (setq-local latex-complete-bibtex-cache (list files key keys)))
  1458. (complete-with-action action keys key pred)))))
  1459. (defun latex-complete-envnames ()
  1460. (completion-table-in-turn
  1461. (append latex-block-names latex-standard-block-names)
  1462. (completion-table-dynamic
  1463. (lambda (str)
  1464. (with-current-buffer (if (and (minibufferp) (minibuffer-selected-window))
  1465. (window-buffer (minibuffer-selected-window))
  1466. (current-buffer))
  1467. (save-excursion
  1468. (let ((comps '())
  1469. (pos (point)))
  1470. (goto-char (point-min))
  1471. (while (re-search-forward (concat "\\\\begin{\\(" str "[^}\n ]*\\)")
  1472. nil t)
  1473. (unless (and (<= (match-beginning 0) pos)
  1474. (>= (match-end 0) pos))
  1475. (push (match-string 1) comps)))
  1476. comps)))))))
  1477. (defun latex-complete-refkeys ()
  1478. (when (boundp 'reftex-docstruct-symbol)
  1479. (symbol-value reftex-docstruct-symbol)))
  1480. (defvar latex-complete-alist
  1481. `(("\\`\\\\\\(short\\)?cite\\'" . ,#'latex-complete-bibtex-keys)
  1482. ("\\`\\\\\\(begin\\|end\\)\\'" . ,#'latex-complete-envnames)
  1483. ("\\`\\\\[vf]?ref\\'" . ,#'latex-complete-refkeys)))
  1484. (defun latex-complete-data ()
  1485. "Get completion-data at point."
  1486. (save-excursion
  1487. (let ((pt (point)))
  1488. (skip-chars-backward "^ {}\n\t\\\\")
  1489. (pcase (char-before)
  1490. ((or `nil ?\s ?\n ?\t ?\}) nil)
  1491. (?\\
  1492. ;; TODO: Complete commands.
  1493. nil)
  1494. (?\{
  1495. ;; Complete args to commands.
  1496. (let* ((cmd
  1497. (save-excursion
  1498. (forward-char -1)
  1499. (skip-chars-backward " \n")
  1500. (buffer-substring (point)
  1501. (progn
  1502. (skip-chars-backward "a-zA-Z@*")
  1503. (let ((n (skip-chars-backward "\\\\")))
  1504. (forward-char (* 2 (/ n 2))))
  1505. (point)))))
  1506. (start (point))
  1507. (_ (progn (goto-char pt) (skip-chars-backward "^," start)))
  1508. (comp-beg (point))
  1509. (_ (progn (goto-char pt) (skip-chars-forward "^, {}\n\t\\\\")))
  1510. (comp-end (point))
  1511. (table
  1512. (funcall
  1513. (let ((f (lambda () t)))
  1514. (dolist (comp latex-complete-alist)
  1515. (if (string-match (car comp) cmd)
  1516. (setq f (cdr comp))))
  1517. f))))
  1518. (if (eq table t)
  1519. ;; Unknown command.
  1520. nil
  1521. (list comp-beg comp-end table))))))))
  1522. ;;;;
  1523. ;;;; LaTeX syntax navigation
  1524. ;;;;
  1525. (defmacro tex-search-noncomment (&rest body)
  1526. "Execute BODY as long as it return non-nil and point is in a comment.
  1527. Return the value returned by the last execution of BODY."
  1528. (declare (debug t))
  1529. (let ((res-sym (make-symbol "result")))
  1530. `(let (,res-sym)
  1531. (while
  1532. (and (setq ,res-sym (progn ,@body))
  1533. (save-excursion (skip-chars-backward "^\n%") (not (bolp)))))
  1534. ,res-sym)))
  1535. (defun tex-last-unended-begin ()
  1536. "Leave point at the beginning of the last `\\begin{...}' that is unended."
  1537. (condition-case nil
  1538. (while (and (tex-search-noncomment
  1539. (re-search-backward "\\\\\\(begin\\|end\\)\\s *{"))
  1540. (looking-at "\\\\end"))
  1541. (tex-last-unended-begin))
  1542. (search-failed (error "Couldn't find unended \\begin"))))
  1543. (defun tex-next-unmatched-end ()
  1544. "Leave point at the end of the next `\\end' that is unmatched."
  1545. (while (and (tex-search-noncomment
  1546. (re-search-forward "\\\\\\(begin\\|end\\)\\s *{[^}]+}"))
  1547. (save-excursion (goto-char (match-beginning 0))
  1548. (looking-at "\\\\begin")))
  1549. (tex-next-unmatched-end)))
  1550. (defun tex-next-unmatched-eparen (otype)
  1551. "Leave point after the next unmatched escaped closing parenthesis.
  1552. The string OTYPE is an opening parenthesis type: `(', `{', or `['."
  1553. (condition-case nil
  1554. (let ((ctype (char-to-string (cdr (aref (syntax-table)
  1555. (string-to-char otype))))))
  1556. (while (and (tex-search-noncomment
  1557. (re-search-forward (format "\\\\[%s%s]" ctype otype)))
  1558. (save-excursion
  1559. (goto-char (match-beginning 0))
  1560. (looking-at (format "\\\\%s" (regexp-quote otype)))))
  1561. (tex-next-unmatched-eparen otype)))
  1562. (wrong-type-argument (error "Unknown opening parenthesis type: %s" otype))
  1563. (search-failed (error "Couldn't find closing escaped paren"))))
  1564. (defun tex-last-unended-eparen (ctype)
  1565. "Leave point at the start of the last unended escaped opening parenthesis.
  1566. The string CTYPE is a closing parenthesis type: `)', `}', or `]'."
  1567. (condition-case nil
  1568. (let ((otype (char-to-string (cdr (aref (syntax-table)
  1569. (string-to-char ctype))))))
  1570. (while (and (tex-search-noncomment
  1571. (re-search-backward (format "\\\\[%s%s]" ctype otype)))
  1572. (looking-at (format "\\\\%s" (regexp-quote ctype))))
  1573. (tex-last-unended-eparen ctype)))
  1574. (wrong-type-argument (error "Unknown opening parenthesis type: %s" ctype))
  1575. (search-failed (error "Couldn't find unended escaped paren"))))
  1576. (defun tex-goto-last-unclosed-latex-block ()
  1577. "Move point to the last unclosed \\begin{...}.
  1578. Mark is left at original location."
  1579. (interactive)
  1580. (let ((spot))
  1581. (save-excursion
  1582. (tex-last-unended-begin)
  1583. (setq spot (point)))
  1584. (push-mark)
  1585. (goto-char spot)))
  1586. (defvar latex-handle-escaped-parens t)
  1587. ;; Don't think this one actually _needs_ (for the purposes of
  1588. ;; tex-mode) to handle escaped parens.
  1589. ;; Does not handle escaped parens when latex-handle-escaped-parens is nil.
  1590. (defun latex-backward-sexp-1 ()
  1591. "Like (backward-sexp 1) but aware of multi-char elements and escaped parens."
  1592. (let ((pos (point))
  1593. (forward-sexp-function))
  1594. (backward-sexp 1)
  1595. (cond ((looking-at
  1596. (if latex-handle-escaped-parens
  1597. "\\\\\\(begin\\>\\|[[({]\\)"
  1598. "\\\\begin\\>"))
  1599. (signal 'scan-error
  1600. (list "Containing expression ends prematurely"
  1601. (point) (prog1 (point) (goto-char pos)))))
  1602. ((and latex-handle-escaped-parens
  1603. (looking-at "\\\\\\([])}]\\)"))
  1604. (tex-last-unended-eparen (match-string 1)))
  1605. ((eq (char-after) ?{)
  1606. (let ((newpos (point)))
  1607. (when (ignore-errors (backward-sexp 1) t)
  1608. (if (or (looking-at "\\\\end\\>")
  1609. ;; In case the \\ ends a verbatim section.
  1610. (and (looking-at "end\\>") (eq (char-before) ?\\)))
  1611. (tex-last-unended-begin)
  1612. (goto-char newpos))))))))
  1613. ;; Note this does not handle things like mismatched brackets inside
  1614. ;; begin/end blocks.
  1615. ;; Needs to handle escaped parens for tex-validate-*.
  1616. ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-09/msg00038.html
  1617. ;; Does not handle escaped parens when latex-handle-escaped-parens is nil.
  1618. (defun latex-forward-sexp-1 ()
  1619. "Like (forward-sexp 1) but aware of multi-char elements and escaped parens."
  1620. (let ((pos (point))
  1621. (forward-sexp-function))
  1622. (forward-sexp 1)
  1623. (let ((newpos (point)))
  1624. (skip-syntax-backward "/w")
  1625. (cond
  1626. ((looking-at "\\\\end\\>")
  1627. (signal 'scan-error
  1628. (list "Containing expression ends prematurely"
  1629. (point)
  1630. (prog1
  1631. (progn (ignore-errors (forward-sexp 2)) (point))
  1632. (goto-char pos)))))
  1633. ((looking-at "\\\\begin\\>")
  1634. (goto-char (match-end 0))
  1635. (tex-next-unmatched-end))
  1636. ;; A better way to handle this, \( .. \) etc, is probably to
  1637. ;; temporarily change the syntax of the \ in \( to punctuation.
  1638. ((and latex-handle-escaped-parens
  1639. (looking-back "\\\\[])}]" (- (point) 2)))
  1640. (signal 'scan-error
  1641. (list "Containing expression ends prematurely"
  1642. (- (point) 2) (prog1 (point)
  1643. (goto-char pos)))))
  1644. ((and latex-handle-escaped-parens
  1645. (looking-back "\\\\\\([({[]\\)" (- (point) 2)))
  1646. (tex-next-unmatched-eparen (match-string 1)))
  1647. (t (goto-char newpos))))))
  1648. (defun latex-forward-sexp (&optional arg)
  1649. "Like `forward-sexp' but aware of multi-char elements and escaped parens."
  1650. (interactive "P")
  1651. (unless arg (setq arg 1))
  1652. (let ((pos (point))
  1653. (opoint 0))
  1654. (condition-case err
  1655. (while (and (/= (point) opoint)
  1656. (/= arg 0))
  1657. (setq opoint (point))
  1658. (setq arg
  1659. (if (> arg 0)
  1660. (progn (latex-forward-sexp-1) (1- arg))
  1661. (progn (latex-backward-sexp-1) (1+ arg)))))
  1662. (scan-error
  1663. (goto-char pos)
  1664. (signal (car err) (cdr err))))))
  1665. (defun latex-syntax-after ()
  1666. "Like (char-syntax (char-after)) but aware of multi-char elements."
  1667. (if (looking-at "\\\\end\\>") ?\) (char-syntax (following-char))))
  1668. (defun latex-skip-close-parens ()
  1669. "Like (skip-syntax-forward \" )\") but aware of multi-char elements."
  1670. (let ((forward-sexp-function nil))
  1671. (while (progn (skip-syntax-forward " )")
  1672. (looking-at "\\\\end\\>"))
  1673. (forward-sexp 2))))
  1674. (defun latex-down-list ()
  1675. "Like (down-list 1) but aware of multi-char elements."
  1676. (forward-comment (point-max))
  1677. (let ((forward-sexp-function nil))
  1678. (if (not (looking-at "\\\\begin\\>"))
  1679. (down-list 1)
  1680. (forward-sexp 1)
  1681. ;; Skip arguments.
  1682. (while (looking-at "[ \t]*[[{(]")
  1683. (with-syntax-table tex-mode-syntax-table
  1684. (forward-sexp))))))
  1685. (defalias 'tex-close-latex-block 'latex-close-block)
  1686. (define-skeleton latex-close-block
  1687. "Create an \\end{...} to match the last unclosed \\begin{...}."
  1688. (save-excursion
  1689. (tex-last-unended-begin)
  1690. (if (not (looking-at "\\\\begin\\(\\s *{[^}\n]*}\\)")) '("{" _ "}")
  1691. (match-string 1)))
  1692. \n "\\end" str > \n)
  1693. (define-skeleton latex-split-block
  1694. "Split the enclosing environment by inserting \\end{..}\\begin{..} at point."
  1695. (save-excursion
  1696. (tex-last-unended-begin)
  1697. (if (not (looking-at "\\\\begin\\(\\s *{[^}\n]*}\\)")) '("{" _ "}")
  1698. (prog1 (match-string 1)
  1699. (goto-char (match-end 1))
  1700. (setq v1 (buffer-substring (point)
  1701. (progn
  1702. (while (looking-at "[ \t]*[[{]")
  1703. (forward-sexp 1))
  1704. (point)))))))
  1705. \n "\\end" str > \n _ \n "\\begin" str v1 > \n)
  1706. (defconst tex-discount-args-cmds
  1707. '("begin" "end" "input" "special" "cite" "ref" "include" "includeonly"
  1708. "documentclass" "usepackage" "label")
  1709. "TeX commands whose arguments should not be counted as text.")
  1710. (defun tex-count-words (begin end)
  1711. "Count the number of words in the buffer."
  1712. (interactive
  1713. (if (and transient-mark-mode mark-active)
  1714. (list (region-beginning) (region-end))
  1715. (list (point-min) (point-max))))
  1716. ;; TODO: skip comments and math and maybe some environments.
  1717. (save-excursion
  1718. (goto-char begin)
  1719. (let ((count 0))
  1720. (while (and (< (point) end) (re-search-forward "\\<" end t))
  1721. (if (not (eq (char-syntax (preceding-char)) ?/))
  1722. (progn
  1723. ;; Don't count single-char words.
  1724. (unless (looking-at ".\\>") (cl-incf count))
  1725. (forward-char 1))
  1726. (let ((cmd
  1727. (buffer-substring-no-properties
  1728. (point) (progn (when (zerop (skip-chars-forward "a-zA-Z@"))
  1729. (forward-char 1))
  1730. (point)))))
  1731. (when (member cmd tex-discount-args-cmds)
  1732. (skip-chars-forward "*")
  1733. (forward-comment (point-max))
  1734. (when (looking-at "\\[")
  1735. (forward-sexp 1)
  1736. (forward-comment (point-max)))
  1737. (if (not (looking-at "{"))
  1738. (forward-char 1)
  1739. (forward-sexp 1))))))
  1740. (message "%s words" count))))
  1741. ;;; Invoking TeX in an inferior shell.
  1742. ;; Why use a shell instead of running TeX directly? Because if TeX
  1743. ;; gets stuck, the user can switch to the shell window and type at it.
  1744. (defvar tex-error-parse-syntax-table
  1745. (let ((st (make-syntax-table)))
  1746. (modify-syntax-entry ?\( "()" st)
  1747. (modify-syntax-entry ?\) ")(" st)
  1748. (modify-syntax-entry ?\\ "\\" st)
  1749. (modify-syntax-entry ?\{ "_" st)
  1750. (modify-syntax-entry ?\} "_" st)
  1751. (modify-syntax-entry ?\[ "_" st)
  1752. (modify-syntax-entry ?\] "_" st)
  1753. ;; Single quotations may appear in errors
  1754. (modify-syntax-entry ?\" "_" st)
  1755. st)
  1756. "Syntax-table used while parsing TeX error messages.")
  1757. (defun tex-old-error-file-name ()
  1758. ;; This is unreliable, partly because we don't try very hard, and
  1759. ;; partly because TeX's output format is eminently ambiguous and unfriendly
  1760. ;; to automation.
  1761. (save-excursion
  1762. (save-match-data
  1763. (with-syntax-table tex-error-parse-syntax-table
  1764. (beginning-of-line)
  1765. (backward-up-list 1)
  1766. (skip-syntax-forward "(_")
  1767. (while (not (let ((try-filename (thing-at-point 'filename)))
  1768. (and try-filename
  1769. (not (string= "" try-filename))
  1770. (file-readable-p try-filename))))
  1771. (skip-syntax-backward "(_")
  1772. (backward-up-list 1)
  1773. (skip-syntax-forward "(_"))
  1774. (thing-at-point 'filename)))))
  1775. (defconst tex-error-regexp-alist
  1776. ;; First alternative handles the newer --file-line-error style:
  1777. ;; ./test2.tex:14: Too many }'s.
  1778. '(gnu
  1779. ;; Second handles the old-style, which spans two lines but doesn't include
  1780. ;; any file info:
  1781. ;; ! Too many }'s.
  1782. ;; l.396 toto}
  1783. ("^l\\.\\([1-9][0-9]*\\) \\(?:\\.\\.\\.\\)?\\(.*\\)$"
  1784. tex-old-error-file-name 1 nil nil nil
  1785. ;; Since there's no filename to highlight, let's highlight the message.
  1786. (2 compilation-error-face))
  1787. ;; A few common warning messages.
  1788. ("^\\(?:Und\\|Ov\\)erfull \\\\[hv]box .* at lines? \\(\\([1-9][0-9]*\\)\\(?:--\\([1-9][0-9]*\\)\\)?\\)$"
  1789. tex-old-error-file-name (2 . 3) nil 1 nil
  1790. (1 compilation-warning-face))
  1791. ("^(Font) *\\([^ \n].* on input line \\([1-9][0-9]*\\)\\)\\.$"
  1792. tex-old-error-file-name 2 nil 1 1
  1793. (2 compilation-warning-face))
  1794. ;; Included files get output as (<file> ...).
  1795. ;; FIXME: there tend to be a boatload of them at the beginning of the
  1796. ;; output which aren't that interesting. Maybe we should filter out
  1797. ;; all the file name that start with /usr/share?
  1798. ;; ("(\\.?/\\([^() \n]+\\)" 1 nil nil 0)
  1799. ))
  1800. ;; The utility functions:
  1801. (define-derived-mode tex-shell shell-mode "TeX-Shell"
  1802. (setq-local compilation-error-regexp-alist tex-error-regexp-alist)
  1803. (compilation-shell-minor-mode t))
  1804. ;;;###autoload
  1805. (defun tex-start-shell ()
  1806. (with-current-buffer
  1807. (make-comint
  1808. "tex-shell"
  1809. (or tex-shell-file-name (getenv "ESHELL") shell-file-name)
  1810. nil
  1811. ;; Specify an interactive shell, to make sure it prompts.
  1812. "-i")
  1813. (let ((proc (get-process "tex-shell")))
  1814. (set-process-sentinel proc 'tex-shell-sentinel)
  1815. (set-process-query-on-exit-flag proc nil)
  1816. (tex-shell)
  1817. (while (zerop (buffer-size))
  1818. (sleep-for 1)))))
  1819. (defun tex-feed-input ()
  1820. "Send input to the tex shell process.
  1821. In the tex buffer this can be used to continue an interactive tex run.
  1822. In the tex shell buffer this command behaves like `comint-send-input'."
  1823. (interactive)
  1824. (set-buffer (tex-shell-buf))
  1825. (comint-send-input)
  1826. (tex-recenter-output-buffer nil))
  1827. (defun tex-display-shell ()
  1828. "Make the TeX shell buffer visible in a window."
  1829. (display-buffer (tex-shell-buf))
  1830. (tex-recenter-output-buffer nil))
  1831. (defun tex-shell-sentinel (proc _msg)
  1832. (cond ((null (buffer-name (process-buffer proc)))
  1833. ;; buffer killed
  1834. (set-process-buffer proc nil)
  1835. (tex-delete-last-temp-files))
  1836. ((memq (process-status proc) '(signal exit))
  1837. (tex-delete-last-temp-files))))
  1838. (defun tex-set-buffer-directory (buffer directory)
  1839. "Set BUFFER's default directory to be DIRECTORY."
  1840. (setq directory (file-name-as-directory (expand-file-name directory)))
  1841. (if (not (file-directory-p directory))
  1842. (error "%s is not a directory" directory)
  1843. (with-current-buffer buffer
  1844. (setq default-directory directory))))
  1845. (defvar tex-send-command-modified-tick 0)
  1846. (make-variable-buffer-local 'tex-send-command-modified-tick)
  1847. (defun tex-shell-proc ()
  1848. (or (tex-shell-running) (error "No TeX subprocess")))
  1849. (defun tex-shell-buf ()
  1850. (process-buffer (tex-shell-proc)))
  1851. (defun tex-shell-buf-no-error ()
  1852. (let ((proc (tex-shell-running)))
  1853. (and proc (process-buffer proc))))
  1854. (defun tex-send-command (command &optional file background)
  1855. "Send COMMAND to TeX shell process, substituting optional FILE for *.
  1856. Do this in background if optional BACKGROUND is t. If COMMAND has no *,
  1857. FILE will be appended, preceded by a blank, to COMMAND. If FILE is nil, no
  1858. substitution will be made in COMMAND. COMMAND can be any expression that
  1859. evaluates to a command string.
  1860. Return the process in which TeX is running."
  1861. (save-excursion
  1862. (let* ((cmd (eval command))
  1863. (proc (tex-shell-proc))
  1864. (buf (process-buffer proc))
  1865. (star (string-match "\\*" cmd))
  1866. (string
  1867. (concat
  1868. (if (null file)
  1869. cmd
  1870. (if (file-name-absolute-p file)
  1871. (setq file (convert-standard-filename file)))
  1872. (if star (concat (substring cmd 0 star)
  1873. (shell-quote-argument file)
  1874. (substring cmd (1+ star)))
  1875. (concat cmd " " (shell-quote-argument file))))
  1876. (if background "&" ""))))
  1877. ;; Switch to buffer before checking for subproc output in it.
  1878. (set-buffer buf)
  1879. ;; If text is unchanged since previous tex-send-command,
  1880. ;; we haven't got any output. So wait for output now.
  1881. (if (= (buffer-modified-tick buf) tex-send-command-modified-tick)
  1882. (accept-process-output proc))
  1883. (goto-char (process-mark proc))
  1884. (insert string)
  1885. (comint-send-input)
  1886. (setq tex-send-command-modified-tick (buffer-modified-tick buf))
  1887. proc)))
  1888. (defun tex-delete-last-temp-files (&optional not-all)
  1889. "Delete any junk files from last temp file.
  1890. If NOT-ALL is non-nil, save the `.dvi' file."
  1891. (if tex-last-temp-file
  1892. (let* ((dir (file-name-directory tex-last-temp-file))
  1893. (list (and (file-directory-p dir)
  1894. (file-name-all-completions
  1895. (file-name-base tex-last-temp-file)
  1896. dir))))
  1897. (while list
  1898. (if not-all
  1899. (and
  1900. ;; If arg is non-nil, don't delete the .dvi file.
  1901. (not (string-match "\\.dvi$" (car list)))
  1902. (delete-file (concat dir (car list))))
  1903. (delete-file (concat dir (car list))))
  1904. (setq list (cdr list))))))
  1905. (add-hook 'kill-emacs-hook 'tex-delete-last-temp-files)
  1906. ;;
  1907. ;; Machinery to guess the command that the user wants to execute.
  1908. ;;
  1909. (defvar tex-compile-history nil)
  1910. (defvar tex-input-files-re
  1911. (eval-when-compile
  1912. (concat "\\." (regexp-opt '("tex" "texi" "texinfo"
  1913. "bbl" "ind" "sty" "cls") t)
  1914. ;; Include files with no dots (for directories).
  1915. "\\'\\|\\`[^.]+\\'")))
  1916. (defcustom tex-use-reftex t
  1917. "If non-nil, use RefTeX's list of files to determine what command to use."
  1918. :type 'boolean
  1919. :group 'tex)
  1920. (defvar tex-compile-commands
  1921. `(,@(mapcar (lambda (prefix)
  1922. `((concat ,prefix tex-command
  1923. " " (if (< 0 (length tex-start-commands))
  1924. (shell-quote-argument tex-start-commands))
  1925. " %f")
  1926. t "%r.pdf"))
  1927. '("pdf" "xe" "lua"))
  1928. ((concat tex-command
  1929. " " (if (< 0 (length tex-start-commands))
  1930. (shell-quote-argument tex-start-commands))
  1931. " %f")
  1932. t "%r.dvi")
  1933. ("xdvi %r &" "%r.dvi")
  1934. ("\\doc-view \"%r.pdf\"" "%r.pdf")
  1935. ("xpdf %r.pdf &" "%r.pdf")
  1936. ("gv %r.ps &" "%r.ps")
  1937. ("yap %r &" "%r.dvi")
  1938. ("advi %r &" "%r.dvi")
  1939. ("gv %r.pdf &" "%r.pdf")
  1940. ("bibtex %r" "%r.aux" "%r.bbl")
  1941. ("makeindex %r" "%r.idx" "%r.ind")
  1942. ("texindex %r.??")
  1943. ("dvipdfm %r" "%r.dvi" "%r.pdf")
  1944. ("dvipdf %r" "%r.dvi" "%r.pdf")
  1945. ("dvips -o %r.ps %r" "%r.dvi" "%r.ps")
  1946. ("ps2pdf %r.ps" "%r.ps" "%r.pdf")
  1947. ("lpr %r.ps" "%r.ps"))
  1948. "List of commands for `tex-compile'.
  1949. Each element should be of the form (FORMAT IN OUT) where
  1950. FORMAT is an expression that evaluates to a string that can contain
  1951. - `%r' the main file name without extension.
  1952. - `%f' the main file name.
  1953. IN can be either a string (with the same % escapes in it) indicating
  1954. the name of the input file, or t to indicate that the input is all
  1955. the TeX files of the document, or nil if we don't know.
  1956. OUT describes the output file and is either a %-escaped string
  1957. or nil to indicate that there is no output file.")
  1958. (define-obsolete-function-alias 'tex-string-prefix-p 'string-prefix-p "24.3")
  1959. (defun tex-guess-main-file (&optional all)
  1960. "Find a likely `tex-main-file'.
  1961. Looks for hints in other buffers in the same directory or in
  1962. ALL other buffers. If ALL is `sub' only look at buffers in parent directories
  1963. of the current buffer."
  1964. (let ((dir default-directory)
  1965. (header-re tex-start-of-header))
  1966. (catch 'found
  1967. ;; Look for a buffer with `tex-main-file' set.
  1968. (dolist (buf (if (consp all) all (buffer-list)))
  1969. (with-current-buffer buf
  1970. (when (and (cond
  1971. ((null all) (equal dir default-directory))
  1972. ((eq all 'sub) (string-prefix-p default-directory dir))
  1973. (t))
  1974. (stringp tex-main-file))
  1975. (throw 'found (expand-file-name tex-main-file)))))
  1976. ;; Look for a buffer containing the magic `tex-start-of-header'.
  1977. (dolist (buf (if (consp all) all (buffer-list)))
  1978. (with-current-buffer buf
  1979. (when (and (cond
  1980. ((null all) (equal dir default-directory))
  1981. ((eq all 'sub) (string-prefix-p default-directory dir))
  1982. (t))
  1983. buffer-file-name
  1984. ;; (or (easy-mmode-derived-mode-p 'latex-mode)
  1985. ;; (easy-mmode-derived-mode-p 'plain-tex-mode))
  1986. (save-excursion
  1987. (save-restriction
  1988. (widen)
  1989. (goto-char (point-min))
  1990. (re-search-forward
  1991. header-re (+ (point) 10000) t))))
  1992. (throw 'found (expand-file-name buffer-file-name))))))))
  1993. (defun tex-main-file ()
  1994. "Return the relative name of the main file."
  1995. (let* ((file (or tex-main-file
  1996. ;; Compatibility with AUCTeX.
  1997. (with-no-warnings
  1998. (when (boundp 'TeX-master)
  1999. (cond ((stringp TeX-master)
  2000. (setq-local tex-main-file TeX-master))
  2001. ((and (eq TeX-master t) buffer-file-name)
  2002. (file-relative-name buffer-file-name)))))
  2003. ;; Try to guess the main file.
  2004. (if (not buffer-file-name)
  2005. (error "Buffer is not associated with any file")
  2006. (file-relative-name
  2007. (if (save-excursion
  2008. (goto-char (point-min))
  2009. (re-search-forward tex-start-of-header
  2010. (+ (point) 10000) t))
  2011. ;; This is the main file.
  2012. buffer-file-name
  2013. ;; This isn't the main file, let's try to find better,
  2014. (or (tex-guess-main-file)
  2015. (tex-guess-main-file 'sub)
  2016. ;; (tex-guess-main-file t)
  2017. buffer-file-name)))))))
  2018. (if (or (file-exists-p file) (string-match "\\.tex\\'" file))
  2019. file (concat file ".tex"))))
  2020. (defun tex-summarize-command (cmd)
  2021. (if (not (stringp cmd)) ""
  2022. (mapconcat #'identity
  2023. (mapcar (lambda (s) (car (split-string s)))
  2024. (split-string cmd "\\s-*\\(?:;\\|&&\\)\\s-*"))
  2025. "&")))
  2026. (defun tex-uptodate-p (file)
  2027. "Return non-nil if FILE is not uptodate w.r.t the document source files.
  2028. FILE is typically the output DVI or PDF file."
  2029. ;; We should check all the files included !!!
  2030. (and
  2031. ;; Clearly, the target must exist.
  2032. (file-exists-p file)
  2033. ;; And the last run must not have asked for a rerun.
  2034. ;; FIXME: this should check that the last run was done on the same file.
  2035. (let ((buf (condition-case nil (tex-shell-buf) (error nil))))
  2036. (when buf
  2037. (with-current-buffer buf
  2038. (save-excursion
  2039. (goto-char (point-max))
  2040. (and (re-search-backward
  2041. (concat "(see the transcript file for additional information)"
  2042. "\\|^Output written on .*"
  2043. (regexp-quote (file-name-nondirectory file))
  2044. " (.*)\\.")
  2045. nil t)
  2046. (> (save-excursion
  2047. ;; Usually page numbers are output as [N], but
  2048. ;; I've already seen things like
  2049. ;; [1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}]
  2050. (or (re-search-backward "\\[[0-9]+\\({[^}]*}\\)?\\]"
  2051. nil t)
  2052. (point-min)))
  2053. (save-excursion
  2054. (or (re-search-backward "Rerun" nil t)
  2055. (point-min)))))))))
  2056. ;; And the input files must not have been changed in the meantime.
  2057. (let ((files (if (and tex-use-reftex
  2058. (fboundp 'reftex-scanning-info-available-p)
  2059. (reftex-scanning-info-available-p))
  2060. (reftex-all-document-files)
  2061. (list (file-name-directory (expand-file-name file)))))
  2062. (ignored-dirs-re
  2063. (concat
  2064. (regexp-opt
  2065. (delq nil (mapcar (lambda (s) (if (eq (aref s (1- (length s))) ?/)
  2066. (substring s 0 (1- (length s)))))
  2067. completion-ignored-extensions))
  2068. t) "\\'"))
  2069. (uptodate t))
  2070. (while (and files uptodate)
  2071. (let ((f (pop files)))
  2072. (if (and (file-directory-p f)
  2073. ;; Avoid infinite loops.
  2074. (not (file-symlink-p f)))
  2075. (unless (string-match ignored-dirs-re f)
  2076. (setq files (nconc
  2077. (ignore-errors ;Not readable or something.
  2078. (directory-files f t tex-input-files-re))
  2079. files)))
  2080. (when (file-newer-than-file-p f file)
  2081. (setq uptodate nil)))))
  2082. uptodate)))
  2083. (autoload 'format-spec "format-spec")
  2084. (defvar tex-executable-cache nil)
  2085. (defun tex-executable-exists-p (name)
  2086. "Like `executable-find' but with a cache."
  2087. (let ((f (and (string-match "^\\\\\\([^ \t\n]+\\)" name)
  2088. (intern-soft (concat "tex-cmd-" (match-string 1 name))))))
  2089. (if (fboundp f)
  2090. f
  2091. (let ((cache (assoc name tex-executable-cache)))
  2092. (if cache (cdr cache)
  2093. (let ((executable (executable-find name)))
  2094. (push (cons name executable) tex-executable-cache)
  2095. executable))))))
  2096. (defun tex-command-executable (cmd)
  2097. (let ((s (if (stringp cmd) cmd (eval (car cmd)))))
  2098. (substring s 0 (string-match "[ \t]\\|\\'" s))))
  2099. (defun tex-command-active-p (cmd fspec)
  2100. "Return non-nil if the CMD spec might need to be run."
  2101. (let ((in (nth 1 cmd))
  2102. (out (nth 2 cmd)))
  2103. (if (stringp in)
  2104. (let ((file (format-spec in fspec)))
  2105. (when (file-exists-p file)
  2106. (or (not out)
  2107. (file-newer-than-file-p
  2108. file (format-spec out fspec)))))
  2109. (when (and (eq in t) (stringp out))
  2110. (not (tex-uptodate-p (format-spec out fspec)))))))
  2111. (defcustom tex-cmd-bibtex-args "--min-crossref=100"
  2112. "Extra args to pass to `bibtex' by default."
  2113. :type 'string
  2114. :version "23.1"
  2115. :group 'tex-run)
  2116. (defun tex-format-cmd (format fspec)
  2117. "Like `format-spec' but adds user-specified args to the command.
  2118. Only applies the FSPEC to the args part of FORMAT."
  2119. (if (not (string-match "\\([^ /\\]+\\) " format))
  2120. (format-spec format fspec)
  2121. (let* ((prefix (substring format 0 (match-beginning 0)))
  2122. (cmd (match-string 1 format))
  2123. (args (substring format (match-end 0)))
  2124. (sym (intern-soft (format "tex-cmd-%s-args" cmd)))
  2125. (extra-args (and sym (symbol-value sym))))
  2126. (concat prefix cmd
  2127. (if extra-args (concat " " extra-args))
  2128. " " (format-spec args fspec)))))
  2129. (defun tex-compile-default (fspec)
  2130. "Guess a default command given the `format-spec' FSPEC."
  2131. ;; TODO: Learn to do latex+dvips!
  2132. (let ((cmds nil)
  2133. (unchanged-in nil))
  2134. ;; Only consider active commands.
  2135. (dolist (cmd tex-compile-commands)
  2136. (when (tex-executable-exists-p (tex-command-executable cmd))
  2137. (if (tex-command-active-p cmd fspec)
  2138. (push cmd cmds)
  2139. (push (nth 1 cmd) unchanged-in))))
  2140. ;; If no command seems to be applicable, arbitrarily pick the first one.
  2141. (setq cmds (if cmds (nreverse cmds) (list (car tex-compile-commands))))
  2142. ;; Remove those commands whose input was considered stable for
  2143. ;; some other command (typically if (t . "%.pdf") is inactive
  2144. ;; then we're using pdflatex and the fact that the dvi file
  2145. ;; is nonexistent doesn't matter).
  2146. (let ((tmp nil))
  2147. (dolist (cmd cmds)
  2148. (unless (member (nth 1 cmd) unchanged-in)
  2149. (push cmd tmp)))
  2150. ;; Only remove if there's something left.
  2151. (if tmp (setq cmds (nreverse tmp))))
  2152. ;; Remove commands whose input is not uptodate either.
  2153. (let ((outs (delq nil (mapcar (lambda (x) (nth 2 x)) cmds)))
  2154. (tmp nil))
  2155. (dolist (cmd cmds)
  2156. (unless (member (nth 1 cmd) outs)
  2157. (push cmd tmp)))
  2158. ;; Only remove if there's something left.
  2159. (if tmp (setq cmds (nreverse tmp))))
  2160. ;; Select which file we're going to operate on (the latest).
  2161. (let ((latest (nth 1 (car cmds))))
  2162. (dolist (cmd (prog1 (cdr cmds) (setq cmds (list (car cmds)))))
  2163. (if (equal latest (nth 1 cmd))
  2164. (push cmd cmds)
  2165. (unless (eq latest t) ;Can't beat that!
  2166. (if (or (not (stringp latest))
  2167. (eq (nth 1 cmd) t)
  2168. (and (stringp (nth 1 cmd))
  2169. (file-newer-than-file-p
  2170. (format-spec (nth 1 cmd) fspec)
  2171. (format-spec latest fspec))))
  2172. (setq latest (nth 1 cmd) cmds (list cmd)))))))
  2173. ;; Expand the command spec into the actual text.
  2174. (dolist (cmd (prog1 cmds (setq cmds nil)))
  2175. (push (cons (eval (car cmd)) (cdr cmd)) cmds))
  2176. ;; Select the favorite command from the history.
  2177. (let ((hist tex-compile-history)
  2178. re hist-cmd)
  2179. (while hist
  2180. (setq hist-cmd (pop hist))
  2181. (setq re (concat "\\`"
  2182. (regexp-quote (tex-command-executable hist-cmd))
  2183. "\\([ \t]\\|\\'\\)"))
  2184. (dolist (cmd cmds)
  2185. ;; If the hist entry uses the same command and applies to a file
  2186. ;; of the same type (e.g. `gv %r.pdf' vs `gv %r.ps'), select cmd.
  2187. (and (string-match re (car cmd))
  2188. (or (not (string-match "%[fr]\\([-._[:alnum:]]+\\)" (car cmd)))
  2189. (string-match (regexp-quote (match-string 1 (car cmd)))
  2190. hist-cmd))
  2191. (setq hist nil cmds (list cmd)))))
  2192. ;; Substitute and return.
  2193. (if (and hist-cmd
  2194. (string-match (concat "[' \t\"]" (format-spec "%r" fspec)
  2195. "\\([;&' \t\"]\\|\\'\\)")
  2196. hist-cmd))
  2197. ;; The history command was already applied to the same file,
  2198. ;; so just reuse it.
  2199. hist-cmd
  2200. (if cmds (tex-format-cmd (caar cmds) fspec))))))
  2201. (defun tex-cmd-doc-view (file)
  2202. (pop-to-buffer (find-file-noselect file)))
  2203. (defun tex-compile (dir cmd)
  2204. "Run a command CMD on current TeX buffer's file in DIR."
  2205. ;; FIXME: Use time-stamps on files to decide the next op.
  2206. (interactive
  2207. (let* ((file (tex-main-file))
  2208. (default-directory
  2209. (prog1 (file-name-directory (expand-file-name file))
  2210. (setq file (file-name-nondirectory file))))
  2211. (root (file-name-sans-extension file))
  2212. (fspec (list (cons ?r (shell-quote-argument root))
  2213. (cons ?f (shell-quote-argument file))))
  2214. (default (tex-compile-default fspec)))
  2215. (list default-directory
  2216. (completing-read
  2217. (format "Command [%s]: " (tex-summarize-command default))
  2218. (mapcar (lambda (x)
  2219. (list (tex-format-cmd (eval (car x)) fspec)))
  2220. tex-compile-commands)
  2221. nil nil nil 'tex-compile-history default))))
  2222. (save-some-buffers (not compilation-ask-about-save) nil)
  2223. (let ((f (and (string-match "^\\\\\\([^ \t\n]+\\)" cmd)
  2224. (intern-soft (concat "tex-cmd-" (match-string 1 cmd))))))
  2225. (if (functionp f)
  2226. (condition-case nil
  2227. (let ((default-directory dir))
  2228. (apply f (split-string-and-unquote
  2229. (substring cmd (match-end 0)))))
  2230. (wrong-number-of-arguments
  2231. (error "Wrong number of arguments to %s"
  2232. (substring (symbol-name f) 8))))
  2233. (if (tex-shell-running)
  2234. (tex-kill-job)
  2235. (tex-start-shell))
  2236. (tex-send-tex-command cmd dir))))
  2237. (defun tex-start-tex (command file &optional dir)
  2238. "Start a TeX run, using COMMAND on FILE."
  2239. (let* ((star (string-match "\\*" command))
  2240. (compile-command
  2241. (if star
  2242. (concat (substring command 0 star)
  2243. (shell-quote-argument file)
  2244. (substring command (1+ star)))
  2245. (concat command " "
  2246. tex-start-options
  2247. (if (< 0 (length tex-start-commands))
  2248. (concat
  2249. (shell-quote-argument tex-start-commands) " "))
  2250. (shell-quote-argument file)))))
  2251. (tex-send-tex-command compile-command dir)))
  2252. (defun tex-send-tex-command (cmd &optional dir)
  2253. (unless (or (equal dir (let ((buf (tex-shell-buf-no-error)))
  2254. (and buf (with-current-buffer buf
  2255. default-directory))))
  2256. (not dir))
  2257. (let (shell-dirtrack-verbose)
  2258. (tex-send-command tex-shell-cd-command dir)))
  2259. (with-current-buffer (process-buffer (tex-send-command cmd))
  2260. (setq compilation-last-buffer (current-buffer))
  2261. (compilation-forget-errors)
  2262. ;; Don't parse previous compilations.
  2263. (set-marker compilation-parsing-end (1- (point-max))))
  2264. (tex-display-shell)
  2265. (setq tex-last-buffer-texed (current-buffer)))
  2266. ;;; The commands:
  2267. (defun tex-region (beg end)
  2268. "Run TeX on the current region, via a temporary file.
  2269. The file's name comes from the variable `tex-zap-file' and the
  2270. variable `tex-directory' says where to put it.
  2271. If the buffer has a header, the header is given to TeX before the
  2272. region itself. The buffer's header is all lines between the strings
  2273. defined by `tex-start-of-header' and `tex-end-of-header' inclusive.
  2274. The header must start in the first 100 lines of the buffer.
  2275. The value of `tex-trailer' is given to TeX as input after the region.
  2276. The value of `tex-command' specifies the command to use to run TeX."
  2277. (interactive "r")
  2278. (if (tex-shell-running)
  2279. (tex-kill-job)
  2280. (tex-start-shell))
  2281. (or tex-zap-file
  2282. (setq tex-zap-file (tex-generate-zap-file-name)))
  2283. ;; Temp file will be written and TeX will be run in zap-directory.
  2284. ;; If the TEXINPUTS file has relative directories or if the region has
  2285. ;; \input of files, this must be the same directory as the file for
  2286. ;; TeX to access the correct inputs. That's why it's safest if
  2287. ;; tex-directory is ".".
  2288. (let* ((zap-directory
  2289. (file-name-as-directory (expand-file-name tex-directory)))
  2290. (tex-out-file (expand-file-name (concat tex-zap-file ".tex")
  2291. zap-directory))
  2292. (main-file (expand-file-name (tex-main-file)))
  2293. (ismain (string-equal main-file (buffer-file-name)))
  2294. already-output)
  2295. ;; Don't delete temp files if we do the same buffer twice in a row.
  2296. (or (eq (current-buffer) tex-last-buffer-texed)
  2297. (tex-delete-last-temp-files t))
  2298. (let ((default-directory zap-directory)) ; why?
  2299. ;; We assume the header is fully contained in tex-main-file.
  2300. ;; We use f-f-ns so we get prompted about any changes on disk.
  2301. (with-current-buffer (find-file-noselect main-file)
  2302. (setq already-output (tex-region-header tex-out-file
  2303. (and ismain beg))))
  2304. ;; Write out the specified region (but don't repeat anything
  2305. ;; already written in the header).
  2306. (write-region (if ismain
  2307. (max beg already-output)
  2308. beg)
  2309. end tex-out-file (not (zerop already-output)))
  2310. ;; Write the trailer, if any.
  2311. ;; Precede it with a newline to make sure it
  2312. ;; is not hidden in a comment.
  2313. (if tex-trailer
  2314. (write-region (concat "\n" tex-trailer) nil
  2315. tex-out-file t)))
  2316. ;; Record the file name to be deleted afterward.
  2317. (setq tex-last-temp-file tex-out-file)
  2318. ;; Use a relative file name here because (1) the proper dir
  2319. ;; is already current, and (2) the abs file name is sometimes
  2320. ;; too long and can make tex crash.
  2321. (tex-start-tex tex-command (concat tex-zap-file ".tex") zap-directory)
  2322. (setq tex-print-file tex-out-file)))
  2323. (defun tex-region-header (file &optional beg)
  2324. "If there is a TeX header in the current buffer, write it to FILE.
  2325. Return point at the end of the region so written, or zero. If
  2326. the optional buffer position BEG is specified, then the region
  2327. written out starts at BEG, if this lies before the start of the header.
  2328. If the first line matches `tex-first-line-header-regexp', it is
  2329. also written out. The variables `tex-start-of-header' and
  2330. `tex-end-of-header' are used to locate the header. Note that the
  2331. start of the header is required to be within the first 100 lines."
  2332. (save-excursion
  2333. (save-restriction
  2334. (widen)
  2335. (goto-char (point-min))
  2336. (let ((search-end (save-excursion
  2337. (forward-line 100)
  2338. (point)))
  2339. (already-output 0)
  2340. hbeg hend)
  2341. ;; Maybe copy first line, such as `\input texinfo', to temp file.
  2342. (and tex-first-line-header-regexp
  2343. (looking-at tex-first-line-header-regexp)
  2344. (write-region (point)
  2345. (progn (forward-line 1)
  2346. (setq already-output (point)))
  2347. file))
  2348. ;; Write out the header, if there is one, and any of the
  2349. ;; specified region which extends before it. But don't repeat
  2350. ;; anything already written.
  2351. (and tex-start-of-header
  2352. (re-search-forward tex-start-of-header search-end t)
  2353. (progn
  2354. (beginning-of-line)
  2355. (setq hbeg (point)) ; mark beginning of header
  2356. (when (re-search-forward tex-end-of-header nil t)
  2357. (forward-line 1)
  2358. (setq hend (point)) ; mark end of header
  2359. (write-region
  2360. (max (if beg
  2361. (min hbeg beg)
  2362. hbeg)
  2363. already-output)
  2364. hend file (not (zerop already-output)))
  2365. (setq already-output hend))))
  2366. already-output))))
  2367. (defun tex-buffer ()
  2368. "Run TeX on current buffer. See \\[tex-region] for more information.
  2369. Does not save the buffer, so it's useful for trying experimental versions.
  2370. See \\[tex-file] for an alternative."
  2371. (interactive)
  2372. (tex-region (point-min) (point-max)))
  2373. (defun tex-file ()
  2374. "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file.
  2375. This function is more useful than \\[tex-buffer] when you need the
  2376. `.aux' file of LaTeX to have the correct name."
  2377. (interactive)
  2378. (when tex-offer-save
  2379. (save-some-buffers))
  2380. (let* ((source-file (tex-main-file))
  2381. (file-dir (file-name-directory (expand-file-name source-file))))
  2382. (if (tex-shell-running)
  2383. (tex-kill-job)
  2384. (tex-start-shell))
  2385. (tex-start-tex tex-command source-file file-dir)
  2386. (setq tex-print-file (expand-file-name source-file))))
  2387. (defun tex-generate-zap-file-name ()
  2388. "Generate a unique name suitable for use as a file name."
  2389. ;; Include the shell process number and host name
  2390. ;; in case there are multiple shells (for same or different user).
  2391. ;; Dec 1998: There is a report that some versions of xdvi
  2392. ;; don't work with file names that start with #.
  2393. (format "_TZ_%d-%s"
  2394. (process-id (get-buffer-process "*tex-shell*"))
  2395. (subst-char-in-string ?. ?- (system-name))))
  2396. ;; This will perhaps be useful for modifying TEXINPUTS.
  2397. ;; Expand each file name, separated by colons, in the string S.
  2398. (defun tex-expand-files (s)
  2399. (let (elts (start 0))
  2400. (while (string-match ":" s start)
  2401. (setq elts (cons (substring s start (match-beginning 0)) elts))
  2402. (setq start (match-end 0)))
  2403. (or (= start 0)
  2404. (setq elts (cons (substring s start) elts)))
  2405. (mapconcat (lambda (elt)
  2406. (if (= (length elt) 0) elt (expand-file-name elt)))
  2407. (nreverse elts) ":")))
  2408. (defun tex-shell-running ()
  2409. (let ((proc (get-process "tex-shell")))
  2410. (when proc
  2411. (if (and (eq (process-status proc) 'run)
  2412. (buffer-live-p (process-buffer proc)))
  2413. ;; return the TeX process on success
  2414. proc
  2415. ;; get rid of the process permanently
  2416. ;; this should get rid of the annoying w32 problem with
  2417. ;; dead tex-shell buffer and live process
  2418. (delete-process proc)))))
  2419. (defun tex-kill-job ()
  2420. "Kill the currently running TeX job."
  2421. (interactive)
  2422. ;; `quit-process' leads to core dumps of the tex process (except if
  2423. ;; coredumpsize has limit 0kb as on many environments). One would
  2424. ;; like to use (kill-process proc 'lambda), however that construct
  2425. ;; does not work on some systems and kills the shell itself.
  2426. (let ((proc (get-process "tex-shell")))
  2427. (when proc (quit-process proc t))))
  2428. (defun tex-recenter-output-buffer (linenum)
  2429. "Redisplay buffer of TeX job output so that most recent output can be seen.
  2430. The last line of the buffer is displayed on
  2431. line LINE of the window, or centered if LINE is nil."
  2432. (interactive "P")
  2433. (let ((tex-shell (get-buffer "*tex-shell*"))
  2434. (window))
  2435. (if (null tex-shell)
  2436. (message "No TeX output buffer")
  2437. (setq window (display-buffer tex-shell))
  2438. (with-selected-window window
  2439. (bury-buffer tex-shell)
  2440. (goto-char (point-max))
  2441. (recenter (if linenum
  2442. (prefix-numeric-value linenum)
  2443. (/ (window-height) 2)))))))
  2444. (defcustom tex-print-file-extension ".dvi"
  2445. "The TeX-compiled file extension for viewing and printing.
  2446. If you use pdflatex instead of latex, set this to \".pdf\" and modify
  2447. `tex-dvi-view-command' and `tex-dvi-print-command' appropriately."
  2448. :type 'string
  2449. :group 'tex-view
  2450. :version "25.1")
  2451. (defun tex-print (&optional alt)
  2452. "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
  2453. Runs the shell command defined by `tex-dvi-print-command'. If prefix argument
  2454. is provided, use the alternative command, `tex-alt-dvi-print-command'."
  2455. (interactive "P")
  2456. (let ((print-file-name-dvi (tex-append tex-print-file
  2457. tex-print-file-extension))
  2458. test-name)
  2459. (if (and (not (equal (current-buffer) tex-last-buffer-texed))
  2460. (buffer-file-name)
  2461. ;; Check that this buffer's printed file is up to date.
  2462. (file-newer-than-file-p
  2463. (setq test-name (tex-append (buffer-file-name)
  2464. tex-print-file-extension))
  2465. (buffer-file-name)))
  2466. (setq print-file-name-dvi test-name))
  2467. (if (not (file-exists-p print-file-name-dvi))
  2468. (error "No appropriate `.dvi' file could be found")
  2469. (if (tex-shell-running)
  2470. (tex-kill-job)
  2471. (tex-start-shell))
  2472. (tex-send-command
  2473. (if alt tex-alt-dvi-print-command tex-dvi-print-command)
  2474. print-file-name-dvi
  2475. t))))
  2476. (defun tex-alt-print ()
  2477. "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
  2478. Runs the shell command defined by `tex-alt-dvi-print-command'."
  2479. (interactive)
  2480. (tex-print t))
  2481. (defun tex-view ()
  2482. "Preview the last `.dvi' file made by running TeX under Emacs.
  2483. This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file].
  2484. The variable `tex-dvi-view-command' specifies the shell command for preview.
  2485. You must set that variable yourself before using this command,
  2486. because there is no standard value that would generally work."
  2487. (interactive)
  2488. (or tex-dvi-view-command
  2489. (error "You must set `tex-dvi-view-command'"))
  2490. ;; Restart the TeX shell if necessary.
  2491. (or (tex-shell-running)
  2492. (tex-start-shell))
  2493. (let ((tex-dvi-print-command (eval tex-dvi-view-command)))
  2494. (tex-print)))
  2495. (defun tex-append (file-name suffix)
  2496. "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
  2497. Pascal-based TeX scans for the first period, C TeX uses the last.
  2498. No period is retained immediately before SUFFIX,
  2499. so normally SUFFIX starts with one."
  2500. (if (stringp file-name)
  2501. (let ((file (file-name-nondirectory file-name))
  2502. trial-name)
  2503. ;; Try splitting on last period.
  2504. ;; The first-period split can get fooled when two files
  2505. ;; named a.tex and a.b.tex are both tex'd;
  2506. ;; the last-period split must be right if it matches at all.
  2507. (setq trial-name
  2508. (concat (file-name-directory file-name)
  2509. (substring file 0
  2510. (string-match "\\.[^.]*$" file))
  2511. suffix))
  2512. (if (or (file-exists-p trial-name)
  2513. (file-exists-p (concat trial-name ".aux"))) ;for BibTeX files
  2514. trial-name
  2515. ;; Not found, so split on first period.
  2516. (concat (file-name-directory file-name)
  2517. (substring file 0
  2518. (string-match "\\." file))
  2519. suffix)))
  2520. " "))
  2521. (defun tex-show-print-queue ()
  2522. "Show the print queue that \\[tex-print] put your job on.
  2523. Runs the shell command defined by `tex-show-queue-command'."
  2524. (interactive)
  2525. (if (tex-shell-running)
  2526. (tex-kill-job)
  2527. (tex-start-shell))
  2528. (tex-send-command tex-show-queue-command)
  2529. (tex-display-shell))
  2530. (defun tex-bibtex-file ()
  2531. "Run BibTeX on the current buffer's file."
  2532. (interactive)
  2533. (if (tex-shell-running)
  2534. (tex-kill-job)
  2535. (tex-start-shell))
  2536. (let* (shell-dirtrack-verbose
  2537. (source-file (expand-file-name (tex-main-file)))
  2538. (tex-out-file
  2539. (tex-append (file-name-nondirectory source-file) ""))
  2540. (file-dir (file-name-directory source-file)))
  2541. (tex-send-command tex-shell-cd-command file-dir)
  2542. (tex-send-command tex-bibtex-command tex-out-file))
  2543. (tex-display-shell))
  2544. ;;;;
  2545. ;;;; LaTeX indentation
  2546. ;;;;
  2547. (defvar tex-indent-allhanging t)
  2548. (defvar tex-indent-arg 4)
  2549. (defvar tex-indent-basic 2)
  2550. (defvar tex-indent-item tex-indent-basic)
  2551. (defvar tex-indent-item-re "\\\\\\(bib\\)?item\\>")
  2552. (defvar latex-noindent-environments '("document"))
  2553. (put 'latex-noindent-environments 'safe-local-variable
  2554. (lambda (x) (null (delq t (mapcar #'stringp x)))))
  2555. (defvar tex-latex-indent-syntax-table
  2556. (let ((st (make-syntax-table tex-mode-syntax-table)))
  2557. (modify-syntax-entry ?$ "." st)
  2558. (modify-syntax-entry ?\( "." st)
  2559. (modify-syntax-entry ?\) "." st)
  2560. st)
  2561. "Syntax table used while computing indentation.")
  2562. (defun latex-indent (&optional _arg)
  2563. (if (and (eq (get-text-property (if (and (eobp) (bolp))
  2564. (max (point-min) (1- (point)))
  2565. (line-beginning-position))
  2566. 'face)
  2567. 'tex-verbatim))
  2568. 'noindent
  2569. (with-syntax-table tex-latex-indent-syntax-table
  2570. ;; TODO: Rather than ignore $, we should try to be more clever about it.
  2571. (let ((indent
  2572. (save-excursion
  2573. (beginning-of-line)
  2574. (latex-find-indent))))
  2575. (if (< indent 0) (setq indent 0))
  2576. (if (<= (current-column) (current-indentation))
  2577. (indent-line-to indent)
  2578. (save-excursion (indent-line-to indent)))))))
  2579. (defcustom latex-indent-within-escaped-parens nil
  2580. "Non-nil means add extra indent to text within escaped parens.
  2581. When this is non-nil, text within matching pairs of escaped
  2582. parens is indented at the column following the open paren. The
  2583. default value does not add any extra indent thus providing the
  2584. behavior of Emacs 22 and earlier."
  2585. :type 'boolean
  2586. :group 'tex
  2587. :version "23.1")
  2588. (defun latex-find-indent (&optional virtual)
  2589. "Find the proper indentation of text after point.
  2590. VIRTUAL if non-nil indicates that we're only trying to find the indentation
  2591. in order to determine the indentation of something else.
  2592. There might be text before point."
  2593. (let ((latex-handle-escaped-parens latex-indent-within-escaped-parens))
  2594. (save-excursion
  2595. (skip-chars-forward " \t")
  2596. (or
  2597. ;; Stick the first line at column 0.
  2598. (and (= (point-min) (line-beginning-position)) 0)
  2599. ;; Trust the current indentation, if such info is applicable.
  2600. (and virtual (save-excursion (skip-chars-backward " \t&") (bolp))
  2601. (current-column))
  2602. ;; Stick verbatim environments to the left margin.
  2603. (and (looking-at "\\\\\\(begin\\|end\\) *{\\([^\n}]+\\)")
  2604. (member (match-string 2) tex-verbatim-environments)
  2605. 0)
  2606. ;; Put leading close-paren where the matching open paren would be.
  2607. (let (escaped)
  2608. (and (or (eq (latex-syntax-after) ?\))
  2609. ;; Try to handle escaped close parens but keep
  2610. ;; original position if it doesn't work out.
  2611. (and latex-handle-escaped-parens
  2612. (setq escaped (looking-at "\\\\\\([])}]\\)"))))
  2613. (ignore-errors
  2614. (save-excursion
  2615. (when escaped
  2616. (goto-char (match-beginning 1)))
  2617. (latex-skip-close-parens)
  2618. (latex-backward-sexp-1)
  2619. (latex-find-indent 'virtual)))))
  2620. ;; Default (maybe an argument)
  2621. (let ((pos (point))
  2622. ;; Outdent \item if necessary.
  2623. (indent (if (looking-at tex-indent-item-re) (- tex-indent-item) 0))
  2624. up-list-pos)
  2625. ;; Find the previous point which determines our current indentation.
  2626. (condition-case err
  2627. (progn
  2628. (latex-backward-sexp-1)
  2629. (while (> (current-column) (current-indentation))
  2630. (latex-backward-sexp-1)))
  2631. (scan-error
  2632. (setq up-list-pos (nth 2 err))))
  2633. (cond
  2634. ((= (point-min) pos) 0) ; We're really just indenting the first line.
  2635. ((integerp up-list-pos)
  2636. ;; Have to indent relative to the open-paren.
  2637. (goto-char up-list-pos)
  2638. (if (and (not tex-indent-allhanging)
  2639. (save-excursion
  2640. ;; Make sure we're an argument to a macro and
  2641. ;; that the macro is at the beginning of a line.
  2642. (condition-case nil
  2643. (progn
  2644. (while (eq (char-syntax (char-after)) ?\()
  2645. (forward-sexp -1))
  2646. (and (eq (char-syntax (char-after)) ?/)
  2647. (progn (skip-chars-backward " \t&")
  2648. (bolp))))
  2649. (scan-error nil)))
  2650. (> pos (progn (latex-down-list)
  2651. (forward-comment (point-max))
  2652. (point))))
  2653. ;; Align with the first element after the open-paren.
  2654. (current-column)
  2655. ;; We're the first element after a hanging brace.
  2656. (goto-char up-list-pos)
  2657. (+ (if (and (looking-at "\\\\begin *{\\([^\n}]+\\)")
  2658. (member (match-string 1)
  2659. latex-noindent-environments))
  2660. 0 tex-indent-basic)
  2661. indent (latex-find-indent 'virtual))))
  2662. ;; We're now at the "beginning" of a line.
  2663. ((not (and (not virtual) (eq (char-after) ?\\)))
  2664. ;; Nothing particular here: just keep the same indentation.
  2665. (+ indent (current-column)))
  2666. ;; We're now looking at a macro call.
  2667. ((looking-at tex-indent-item-re)
  2668. ;; Indenting relative to an item, have to re-add the outdenting.
  2669. (+ indent (current-column) tex-indent-item))
  2670. (t
  2671. (let ((col (current-column)))
  2672. (if (or (not (eq (char-syntax (or (char-after pos) ?\s)) ?\())
  2673. ;; Can't be an arg if there's an empty line in between.
  2674. (save-excursion (re-search-forward "^[ \t]*$" pos t)))
  2675. ;; If the first char was not an open-paren, there's
  2676. ;; a risk that this is really not an argument to the
  2677. ;; macro at all.
  2678. (+ indent col)
  2679. (forward-sexp 1)
  2680. (if (< (line-end-position)
  2681. (save-excursion (forward-comment (point-max))
  2682. (point)))
  2683. ;; we're indenting the first argument.
  2684. (min (current-column) (+ tex-indent-arg col))
  2685. (skip-syntax-forward " ")
  2686. (current-column)))))))))))
  2687. ;;; DocTeX support
  2688. (defun doctex-font-lock-^^A ()
  2689. (if (eq (char-after (line-beginning-position)) ?\%)
  2690. (progn
  2691. (put-text-property
  2692. (1- (match-beginning 1)) (match-beginning 1)
  2693. 'syntax-table
  2694. (if (= (1+ (line-beginning-position)) (match-beginning 1))
  2695. ;; The `%' is a single-char comment, which Emacs
  2696. ;; syntax-table can't deal with. We could turn it
  2697. ;; into a non-comment, or use `\n%' or `%^' as the comment.
  2698. ;; Instead, we include it in the ^^A comment.
  2699. (string-to-syntax "< b")
  2700. (string-to-syntax ">")))
  2701. (let ((end (line-end-position)))
  2702. (if (< end (point-max))
  2703. (put-text-property
  2704. end (1+ end)
  2705. 'syntax-table
  2706. (string-to-syntax "> b"))))
  2707. (string-to-syntax "< b"))))
  2708. (defun doctex-font-lock-syntactic-face-function (state)
  2709. ;; Mark DocTeX documentation, which is parsed as a style A comment
  2710. ;; starting in column 0.
  2711. (if (or (nth 3 state) (nth 7 state)
  2712. (not (memq (char-before (nth 8 state))
  2713. '(?\n nil))))
  2714. ;; Anything else is just as for LaTeX.
  2715. (tex-font-lock-syntactic-face-function state)
  2716. font-lock-doc-face))
  2717. (eval-when-compile
  2718. (defconst doctex-syntax-propertize-rules
  2719. (syntax-propertize-precompile-rules
  2720. latex-syntax-propertize-rules
  2721. ;; For DocTeX comment-in-doc.
  2722. ("\\(\\^\\)\\^A" (1 (doctex-font-lock-^^A))))))
  2723. (defvar doctex-font-lock-keywords
  2724. (append tex-font-lock-keywords
  2725. '(("^%<[^>]*>" (0 font-lock-preprocessor-face t)))))
  2726. ;;;###autoload
  2727. (define-derived-mode doctex-mode latex-mode "DocTeX"
  2728. "Major mode to edit DocTeX files."
  2729. (setq font-lock-defaults
  2730. (cons (append (car font-lock-defaults) '(doctex-font-lock-keywords))
  2731. (mapcar
  2732. (lambda (x)
  2733. (pcase (car-safe x)
  2734. (`font-lock-syntactic-face-function
  2735. (cons (car x) 'doctex-font-lock-syntactic-face-function))
  2736. (_ x)))
  2737. (cdr font-lock-defaults))))
  2738. (setq-local syntax-propertize-function
  2739. (syntax-propertize-rules doctex-syntax-propertize-rules)))
  2740. ;;; Prettify Symbols Support
  2741. (defvar tex--prettify-symbols-alist
  2742. '( ;; Lowercase Greek letters.
  2743. ("\\alpha" . ?α)
  2744. ("\\beta" . ?β)
  2745. ("\\gamma" . ?γ)
  2746. ("\\delta" . ?δ)
  2747. ("\\epsilon" . ?ϵ)
  2748. ("\\zeta" . ?ζ)
  2749. ("\\eta" . ?η)
  2750. ("\\theta" . ?θ)
  2751. ("\\iota" . ?ι)
  2752. ("\\kappa" . ?κ)
  2753. ("\\lambda" . ?λ)
  2754. ("\\mu" . ?μ)
  2755. ("\\nu" . ?ν)
  2756. ("\\xi" . ?ξ)
  2757. ;; There is no \omicron because it looks like a latin o.
  2758. ("\\pi" . ?π)
  2759. ("\\rho" . ?ρ)
  2760. ("\\sigma" . ?σ)
  2761. ("\\tau" . ?τ)
  2762. ("\\upsilon" . ?υ)
  2763. ("\\phi" . ?ϕ)
  2764. ("\\chi" . ?χ)
  2765. ("\\psi" . ?ψ)
  2766. ("\\omega" . ?ω)
  2767. ;; Uppercase Greek letters.
  2768. ("\\Gamma" . ?Γ)
  2769. ("\\Delta" . ?Δ)
  2770. ("\\Lambda" . ?Λ)
  2771. ("\\Phi" . ?Φ)
  2772. ("\\Pi" . ?Π)
  2773. ("\\Psi" . ?Ψ)
  2774. ("\\Sigma" . ?Σ)
  2775. ("\\Theta" . ?Θ)
  2776. ("\\Upsilon" . ?Υ)
  2777. ("\\Xi" . ?Ξ)
  2778. ("\\Omega" . ?Ω)
  2779. ;; Other math symbols (taken from leim/quail/latin-ltx.el).
  2780. ("\\Box" . ?□)
  2781. ("\\Bumpeq" . ?≎)
  2782. ("\\Cap" . ?⋒)
  2783. ("\\Cup" . ?⋓)
  2784. ("\\Diamond" . ?◇)
  2785. ("\\Downarrow" . ?⇓)
  2786. ("\\H{o}" . ?ő)
  2787. ("\\Im" . ?ℑ)
  2788. ("\\Join" . ?⋈)
  2789. ("\\Leftarrow" . ?⇐)
  2790. ("\\Leftrightarrow" . ?⇔)
  2791. ("\\Ll" . ?⋘)
  2792. ("\\Lleftarrow" . ?⇚)
  2793. ("\\Longleftarrow" . ?⇐)
  2794. ("\\Longleftrightarrow" . ?⇔)
  2795. ("\\Longrightarrow" . ?⇒)
  2796. ("\\Lsh" . ?↰)
  2797. ("\\Re" . ?ℜ)
  2798. ("\\Rightarrow" . ?⇒)
  2799. ("\\Rrightarrow" . ?⇛)
  2800. ("\\Rsh" . ?↱)
  2801. ("\\Subset" . ?⋐)
  2802. ("\\Supset" . ?⋑)
  2803. ("\\Uparrow" . ?⇑)
  2804. ("\\Updownarrow" . ?⇕)
  2805. ("\\Vdash" . ?⊩)
  2806. ("\\Vert" . ?‖)
  2807. ("\\Vvdash" . ?⊪)
  2808. ("\\aleph" . ?ℵ)
  2809. ("\\amalg" . ?∐)
  2810. ("\\angle" . ?∠)
  2811. ("\\approx" . ?≈)
  2812. ("\\approxeq" . ?≊)
  2813. ("\\ast" . ?∗)
  2814. ("\\asymp" . ?≍)
  2815. ("\\backcong" . ?≌)
  2816. ("\\backepsilon" . ?∍)
  2817. ("\\backprime" . ?‵)
  2818. ("\\backsim" . ?∽)
  2819. ("\\backsimeq" . ?⋍)
  2820. ("\\backslash" . ?\\)
  2821. ("\\barwedge" . ?⊼)
  2822. ("\\because" . ?∵)
  2823. ("\\beth" . ?ℶ)
  2824. ("\\between" . ?≬)
  2825. ("\\bigcap" . ?⋂)
  2826. ("\\bigcirc" . ?◯)
  2827. ("\\bigcup" . ?⋃)
  2828. ("\\bigstar" . ?★)
  2829. ("\\bigtriangledown" . ?▽)
  2830. ("\\bigtriangleup" . ?△)
  2831. ("\\bigvee" . ?⋁)
  2832. ("\\bigwedge" . ?⋀)
  2833. ("\\blacklozenge" . ?✦)
  2834. ("\\blacksquare" . ?▪)
  2835. ("\\blacktriangle" . ?▴)
  2836. ("\\blacktriangledown" . ?▾)
  2837. ("\\blacktriangleleft" . ?◂)
  2838. ("\\blacktriangleright" . ?▸)
  2839. ("\\bot" . ?⊥)
  2840. ("\\bowtie" . ?⋈)
  2841. ("\\boxminus" . ?⊟)
  2842. ("\\boxplus" . ?⊞)
  2843. ("\\boxtimes" . ?⊠)
  2844. ("\\bullet" . ?•)
  2845. ("\\bumpeq" . ?≏)
  2846. ("\\cap" . ?∩)
  2847. ("\\cdots" . ?⋯)
  2848. ("\\centerdot" . ?·)
  2849. ("\\checkmark" . ?✓)
  2850. ("\\chi" . ?χ)
  2851. ("\\cdot" . ?⋅)
  2852. ("\\cdots" . ?⋯)
  2853. ("\\circ" . ?∘)
  2854. ("\\circeq" . ?≗)
  2855. ("\\circlearrowleft" . ?↺)
  2856. ("\\circlearrowright" . ?↻)
  2857. ("\\circledR" . ?®)
  2858. ("\\circledS" . ?Ⓢ)
  2859. ("\\circledast" . ?⊛)
  2860. ("\\circledcirc" . ?⊚)
  2861. ("\\circleddash" . ?⊝)
  2862. ("\\clubsuit" . ?♣)
  2863. ("\\coloneq" . ?≔)
  2864. ("\\complement" . ?∁)
  2865. ("\\cong" . ?≅)
  2866. ("\\coprod" . ?∐)
  2867. ("\\cup" . ?∪)
  2868. ("\\curlyeqprec" . ?⋞)
  2869. ("\\curlyeqsucc" . ?⋟)
  2870. ("\\curlypreceq" . ?≼)
  2871. ("\\curlyvee" . ?⋎)
  2872. ("\\curlywedge" . ?⋏)
  2873. ("\\curvearrowleft" . ?↶)
  2874. ("\\curvearrowright" . ?↷)
  2875. ("\\dag" . ?†)
  2876. ("\\dagger" . ?†)
  2877. ("\\daleth" . ?ℸ)
  2878. ("\\dashv" . ?⊣)
  2879. ("\\ddag" . ?‡)
  2880. ("\\ddagger" . ?‡)
  2881. ("\\ddots" . ?⋱)
  2882. ("\\diamond" . ?⋄)
  2883. ("\\diamondsuit" . ?♢)
  2884. ("\\divideontimes" . ?⋇)
  2885. ("\\doteq" . ?≐)
  2886. ("\\doteqdot" . ?≑)
  2887. ("\\dotplus" . ?∔)
  2888. ("\\dotsquare" . ?⊡)
  2889. ("\\downarrow" . ?↓)
  2890. ("\\downdownarrows" . ?⇊)
  2891. ("\\downleftharpoon" . ?⇃)
  2892. ("\\downrightharpoon" . ?⇂)
  2893. ("\\ell" . ?ℓ)
  2894. ("\\emptyset" . ?∅)
  2895. ("\\eqcirc" . ?≖)
  2896. ("\\eqcolon" . ?≕)
  2897. ("\\eqslantgtr" . ?⋝)
  2898. ("\\eqslantless" . ?⋜)
  2899. ("\\equiv" . ?≡)
  2900. ("\\exists" . ?∃)
  2901. ("\\fallingdotseq" . ?≒)
  2902. ("\\flat" . ?♭)
  2903. ("\\forall" . ?∀)
  2904. ("\\frown" . ?⌢)
  2905. ("\\ge" . ?≥)
  2906. ("\\geq" . ?≥)
  2907. ("\\geqq" . ?≧)
  2908. ("\\geqslant" . ?≥)
  2909. ("\\gets" . ?←)
  2910. ("\\gg" . ?≫)
  2911. ("\\ggg" . ?⋙)
  2912. ("\\gimel" . ?ℷ)
  2913. ("\\gnapprox" . ?⋧)
  2914. ("\\gneq" . ?≩)
  2915. ("\\gneqq" . ?≩)
  2916. ("\\gnsim" . ?⋧)
  2917. ("\\gtrapprox" . ?≳)
  2918. ("\\gtrdot" . ?⋗)
  2919. ("\\gtreqless" . ?⋛)
  2920. ("\\gtreqqless" . ?⋛)
  2921. ("\\gtrless" . ?≷)
  2922. ("\\gtrsim" . ?≳)
  2923. ("\\gvertneqq" . ?≩)
  2924. ("\\hbar" . ?ℏ)
  2925. ("\\heartsuit" . ?♥)
  2926. ("\\hookleftarrow" . ?↩)
  2927. ("\\hookrightarrow" . ?↪)
  2928. ("\\iff" . ?⇔)
  2929. ("\\imath" . ?ı)
  2930. ("\\in" . ?∈)
  2931. ("\\infty" . ?∞)
  2932. ("\\int" . ?∫)
  2933. ("\\intercal" . ?⊺)
  2934. ("\\langle" . 10216) ; Literal ?⟨ breaks indentation.
  2935. ("\\lbrace" . ?{)
  2936. ("\\lbrack" . ?\[)
  2937. ("\\lceil" . ?⌈)
  2938. ("\\ldots" . ?…)
  2939. ("\\le" . ?≤)
  2940. ("\\leadsto" . ?↝)
  2941. ("\\leftarrow" . ?←)
  2942. ("\\leftarrowtail" . ?↢)
  2943. ("\\leftharpoondown" . ?↽)
  2944. ("\\leftharpoonup" . ?↼)
  2945. ("\\leftleftarrows" . ?⇇)
  2946. ;; ("\\leftparengtr" ?〈), see bug#12948.
  2947. ("\\leftrightarrow" . ?↔)
  2948. ("\\leftrightarrows" . ?⇆)
  2949. ("\\leftrightharpoons" . ?⇋)
  2950. ("\\leftrightsquigarrow" . ?↭)
  2951. ("\\leftthreetimes" . ?⋋)
  2952. ("\\leq" . ?≤)
  2953. ("\\leqq" . ?≦)
  2954. ("\\leqslant" . ?≤)
  2955. ("\\lessapprox" . ?≲)
  2956. ("\\lessdot" . ?⋖)
  2957. ("\\lesseqgtr" . ?⋚)
  2958. ("\\lesseqqgtr" . ?⋚)
  2959. ("\\lessgtr" . ?≶)
  2960. ("\\lesssim" . ?≲)
  2961. ("\\lfloor" . ?⌊)
  2962. ("\\lhd" . ?◁)
  2963. ("\\rhd" . ?▷)
  2964. ("\\ll" . ?≪)
  2965. ("\\llcorner" . ?⌞)
  2966. ("\\lnapprox" . ?⋦)
  2967. ("\\lneq" . ?≨)
  2968. ("\\lneqq" . ?≨)
  2969. ("\\lnsim" . ?⋦)
  2970. ("\\longleftarrow" . ?←)
  2971. ("\\longleftrightarrow" . ?↔)
  2972. ("\\longmapsto" . ?↦)
  2973. ("\\longrightarrow" . ?→)
  2974. ("\\looparrowleft" . ?↫)
  2975. ("\\looparrowright" . ?↬)
  2976. ("\\lozenge" . ?✧)
  2977. ("\\lq" . ?‘)
  2978. ("\\lrcorner" . ?⌟)
  2979. ("\\ltimes" . ?⋉)
  2980. ("\\lvertneqq" . ?≨)
  2981. ("\\maltese" . ?✠)
  2982. ("\\mapsto" . ?↦)
  2983. ("\\measuredangle" . ?∡)
  2984. ("\\mho" . ?℧)
  2985. ("\\mid" . ?∣)
  2986. ("\\models" . ?⊧)
  2987. ("\\mp" . ?∓)
  2988. ("\\multimap" . ?⊸)
  2989. ("\\nLeftarrow" . ?⇍)
  2990. ("\\nLeftrightarrow" . ?⇎)
  2991. ("\\nRightarrow" . ?⇏)
  2992. ("\\nVDash" . ?⊯)
  2993. ("\\nVdash" . ?⊮)
  2994. ("\\nabla" . ?∇)
  2995. ("\\napprox" . ?≉)
  2996. ("\\natural" . ?♮)
  2997. ("\\ncong" . ?≇)
  2998. ("\\ne" . ?≠)
  2999. ("\\nearrow" . ?↗)
  3000. ("\\neg" . ?¬)
  3001. ("\\neq" . ?≠)
  3002. ("\\nequiv" . ?≢)
  3003. ("\\newline" . ?
)
  3004. ("\\nexists" . ?∄)
  3005. ("\\ngeq" . ?≱)
  3006. ("\\ngeqq" . ?≱)
  3007. ("\\ngeqslant" . ?≱)
  3008. ("\\ngtr" . ?≯)
  3009. ("\\ni" . ?∋)
  3010. ("\\nleftarrow" . ?↚)
  3011. ("\\nleftrightarrow" . ?↮)
  3012. ("\\nleq" . ?≰)
  3013. ("\\nleqq" . ?≰)
  3014. ("\\nleqslant" . ?≰)
  3015. ("\\nless" . ?≮)
  3016. ("\\nmid" . ?∤)
  3017. ;; ("\\not" ?̸) ;FIXME: conflict with "NOT SIGN" ¬.
  3018. ("\\notin" . ?∉)
  3019. ("\\nparallel" . ?∦)
  3020. ("\\nprec" . ?⊀)
  3021. ("\\npreceq" . ?⋠)
  3022. ("\\nrightarrow" . ?↛)
  3023. ("\\nshortmid" . ?∤)
  3024. ("\\nshortparallel" . ?∦)
  3025. ("\\nsim" . ?≁)
  3026. ("\\nsimeq" . ?≄)
  3027. ("\\nsubset" . ?⊄)
  3028. ("\\nsubseteq" . ?⊈)
  3029. ("\\nsubseteqq" . ?⊈)
  3030. ("\\nsucc" . ?⊁)
  3031. ("\\nsucceq" . ?⋡)
  3032. ("\\nsupset" . ?⊅)
  3033. ("\\nsupseteq" . ?⊉)
  3034. ("\\nsupseteqq" . ?⊉)
  3035. ("\\ntriangleleft" . ?⋪)
  3036. ("\\ntrianglelefteq" . ?⋬)
  3037. ("\\ntriangleright" . ?⋫)
  3038. ("\\ntrianglerighteq" . ?⋭)
  3039. ("\\nvDash" . ?⊭)
  3040. ("\\nvdash" . ?⊬)
  3041. ("\\nwarrow" . ?↖)
  3042. ("\\odot" . ?⊙)
  3043. ("\\oint" . ?∮)
  3044. ("\\ominus" . ?⊖)
  3045. ("\\oplus" . ?⊕)
  3046. ("\\oslash" . ?⊘)
  3047. ("\\otimes" . ?⊗)
  3048. ("\\par" . ?
)
  3049. ("\\parallel" . ?∥)
  3050. ("\\partial" . ?∂)
  3051. ("\\perp" . ?⊥)
  3052. ("\\pitchfork" . ?⋔)
  3053. ("\\prec" . ?≺)
  3054. ("\\precapprox" . ?≾)
  3055. ("\\preceq" . ?≼)
  3056. ("\\precnapprox" . ?⋨)
  3057. ("\\precnsim" . ?⋨)
  3058. ("\\precsim" . ?≾)
  3059. ("\\prime" . ?′)
  3060. ("\\prod" . ?∏)
  3061. ("\\propto" . ?∝)
  3062. ("\\qed" . ?∎)
  3063. ("\\qquad" . ?⧢)
  3064. ("\\quad" . ?␣)
  3065. ("\\rangle" . 10217) ; Literal ?⟩ breaks indentation.
  3066. ("\\rbrace" . ?})
  3067. ("\\rbrack" . ?\])
  3068. ("\\rceil" . ?⌉)
  3069. ("\\rfloor" . ?⌋)
  3070. ("\\rightarrow" . ?→)
  3071. ("\\rightarrowtail" . ?↣)
  3072. ("\\rightharpoondown" . ?⇁)
  3073. ("\\rightharpoonup" . ?⇀)
  3074. ("\\rightleftarrows" . ?⇄)
  3075. ("\\rightleftharpoons" . ?⇌)
  3076. ;; ("\\rightparengtr" ?⦔) ;; Was ?〉, see bug#12948.
  3077. ("\\rightrightarrows" . ?⇉)
  3078. ("\\rightthreetimes" . ?⋌)
  3079. ("\\risingdotseq" . ?≓)
  3080. ("\\rtimes" . ?⋊)
  3081. ("\\times" . ?×)
  3082. ("\\sbs" . ?﹨)
  3083. ("\\searrow" . ?↘)
  3084. ("\\setminus" . ?∖)
  3085. ("\\sharp" . ?♯)
  3086. ("\\shortmid" . ?∣)
  3087. ("\\shortparallel" . ?∥)
  3088. ("\\sim" . ?∼)
  3089. ("\\simeq" . ?≃)
  3090. ("\\smallamalg" . ?∐)
  3091. ("\\smallsetminus" . ?∖)
  3092. ("\\smallsmile" . ?⌣)
  3093. ("\\smile" . ?⌣)
  3094. ("\\spadesuit" . ?♠)
  3095. ("\\sphericalangle" . ?∢)
  3096. ("\\sqcap" . ?⊓)
  3097. ("\\sqcup" . ?⊔)
  3098. ("\\sqsubset" . ?⊏)
  3099. ("\\sqsubseteq" . ?⊑)
  3100. ("\\sqsupset" . ?⊐)
  3101. ("\\sqsupseteq" . ?⊒)
  3102. ("\\square" . ?□)
  3103. ("\\squigarrowright" . ?⇝)
  3104. ("\\star" . ?⋆)
  3105. ("\\straightphi" . ?φ)
  3106. ("\\subset" . ?⊂)
  3107. ("\\subseteq" . ?⊆)
  3108. ("\\subseteqq" . ?⊆)
  3109. ("\\subsetneq" . ?⊊)
  3110. ("\\subsetneqq" . ?⊊)
  3111. ("\\succ" . ?≻)
  3112. ("\\succapprox" . ?≿)
  3113. ("\\succcurlyeq" . ?≽)
  3114. ("\\succeq" . ?≽)
  3115. ("\\succnapprox" . ?⋩)
  3116. ("\\succnsim" . ?⋩)
  3117. ("\\succsim" . ?≿)
  3118. ("\\sum" . ?∑)
  3119. ("\\supset" . ?⊃)
  3120. ("\\supseteq" . ?⊇)
  3121. ("\\supseteqq" . ?⊇)
  3122. ("\\supsetneq" . ?⊋)
  3123. ("\\supsetneqq" . ?⊋)
  3124. ("\\surd" . ?√)
  3125. ("\\swarrow" . ?↙)
  3126. ("\\therefore" . ?∴)
  3127. ("\\thickapprox" . ?≈)
  3128. ("\\thicksim" . ?∼)
  3129. ("\\to" . ?→)
  3130. ("\\top" . ?⊤)
  3131. ("\\triangle" . ?▵)
  3132. ("\\triangledown" . ?▿)
  3133. ("\\triangleleft" . ?◃)
  3134. ("\\trianglelefteq" . ?⊴)
  3135. ("\\triangleq" . ?≜)
  3136. ("\\triangleright" . ?▹)
  3137. ("\\trianglerighteq" . ?⊵)
  3138. ("\\twoheadleftarrow" . ?↞)
  3139. ("\\twoheadrightarrow" . ?↠)
  3140. ("\\ulcorner" . ?⌜)
  3141. ("\\uparrow" . ?↑)
  3142. ("\\updownarrow" . ?↕)
  3143. ("\\upleftharpoon" . ?↿)
  3144. ("\\uplus" . ?⊎)
  3145. ("\\uprightharpoon" . ?↾)
  3146. ("\\upuparrows" . ?⇈)
  3147. ("\\urcorner" . ?⌝)
  3148. ("\\u{i}" . ?ĭ)
  3149. ("\\vDash" . ?⊨)
  3150. ("\\varepsilon" . ?ε)
  3151. ("\\varphi" . ?φ)
  3152. ("\\varprime" . ?′)
  3153. ("\\varpropto" . ?∝)
  3154. ("\\varrho" . ?ϱ)
  3155. ("\\varsigma" ?ς)
  3156. ("\\vartriangleleft" . ?⊲)
  3157. ("\\vartriangleright" . ?⊳)
  3158. ("\\vdash" . ?⊢)
  3159. ("\\vdots" . ?⋮)
  3160. ("\\vee" . ?∨)
  3161. ("\\veebar" . ?⊻)
  3162. ("\\vert" . ?|)
  3163. ("\\wedge" . ?∧)
  3164. ("\\wp" . ?℘)
  3165. ("\\wr" . ?≀)
  3166. ("\\Bbb{N}" . ?ℕ) ; AMS commands for blackboard bold
  3167. ("\\Bbb{P}" . ?ℙ) ; Also sometimes \mathbb.
  3168. ("\\Bbb{Q}" . ?ℚ)
  3169. ("\\Bbb{R}" . ?ℝ)
  3170. ("\\Bbb{Z}" . ?ℤ)
  3171. ("--" . ?–)
  3172. ("---" . ?—)
  3173. ("\\ordfeminine" . ?ª)
  3174. ("\\ordmasculine" . ?º)
  3175. ("\\lambdabar" . ?ƛ)
  3176. ("\\celsius" . ?℃)
  3177. ("\\textmu" . ?µ)
  3178. ("\\textfractionsolidus" . ?⁄)
  3179. ("\\textbigcircle" . ?⃝)
  3180. ("\\textmusicalnote" . ?♪)
  3181. ("\\textdied" . ?✝)
  3182. ("\\textcolonmonetary" . ?₡)
  3183. ("\\textwon" . ?₩)
  3184. ("\\textnaira" . ?₦)
  3185. ("\\textpeso" . ?₱)
  3186. ("\\textlira" . ?₤)
  3187. ("\\textrecipe" . ?℞)
  3188. ("\\textinterrobang" . ?‽)
  3189. ("\\textpertenthousand" . ?‱)
  3190. ("\\textbaht" . ?฿)
  3191. ("\\textnumero" . ?№)
  3192. ("\\textdiscount" . ?⁒)
  3193. ("\\textestimated" . ?℮)
  3194. ("\\textopenbullet" . ?◦)
  3195. ("\\textlquill" . 8261) ; Literal ?⁅ breaks indentation.
  3196. ("\\textrquill" . 8262) ; Literal ?⁆ breaks indentation.
  3197. ("\\textcircledP" . ?℗)
  3198. ("\\textreferencemark" . ?※))
  3199. "A `prettify-symbols-alist' usable for (La)TeX modes.")
  3200. (defun tex--prettify-symbols-compose-p (_start end _match)
  3201. (or
  3202. ;; If the matched symbol doesn't end in a word character, then we
  3203. ;; simply allow composition. The symbol is probably something like
  3204. ;; \|, \(, etc.
  3205. (not (eq ?w (char-syntax (char-before end))))
  3206. ;; Else we look at what follows the match in order to decide.
  3207. (let* ((after-char (char-after end))
  3208. (after-syntax (char-syntax after-char)))
  3209. (not (or
  3210. ;; Don't compose \alpha@foo.
  3211. (eq after-char ?@)
  3212. ;; The \alpha in \alpha2 or \alpha-\beta may be composed but
  3213. ;; of course \alphax may not.
  3214. (and (eq after-syntax ?w)
  3215. (not (memq after-char
  3216. '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?+ ?- ?' ?\"))))
  3217. ;; Don't compose inside verbatim blocks.
  3218. (eq 2 (nth 7 (syntax-ppss))))))))
  3219. (run-hooks 'tex-mode-load-hook)
  3220. (provide 'tex-mode)
  3221. ;;; tex-mode.el ends here