nxml-mode.el 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448
  1. ;;; nxml-mode.el --- a new XML mode -*- lexical-binding:t -*-
  2. ;; Copyright (C) 2003-2004, 2007-2017 Free Software Foundation, Inc.
  3. ;; Author: James Clark
  4. ;; Keywords: wp, hypermedia, languages, XML
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; See nxml-rap.el for description of parsing strategy.
  18. ;;; Code:
  19. (eval-when-compile (require 'cl-lib))
  20. (require 'xmltok)
  21. (require 'nxml-enc)
  22. (require 'nxml-util)
  23. (require 'nxml-rap)
  24. (require 'nxml-outln)
  25. ;; nxml-mode calls rng-nxml-mode-init, which is autoloaded from rng-nxml.
  26. ;; So we might as well just require it and silence the compiler.
  27. (provide 'nxml-mode) ; avoid recursive require
  28. (require 'rng-nxml)
  29. (require 'sgml-mode)
  30. ;;; Customization
  31. (defgroup nxml nil
  32. "New XML editing mode."
  33. :link '(custom-manual "(nxml-mode) Top")
  34. :group 'languages)
  35. (defgroup nxml-faces nil
  36. "Faces for XML syntax highlighting."
  37. :group 'nxml)
  38. (defcustom nxml-char-ref-display-glyph-flag t
  39. "Non-nil means display glyph following character reference.
  40. The glyph is displayed in face `nxml-glyph'."
  41. :group 'nxml
  42. :type 'boolean)
  43. (defcustom nxml-sexp-element-flag nil
  44. "Non-nil means sexp commands treat an element as a single expression."
  45. :group 'nxml
  46. :type 'boolean)
  47. (defcustom nxml-slash-auto-complete-flag nil
  48. "Non-nil means typing a slash automatically completes the end-tag.
  49. This is used by `nxml-electric-slash'."
  50. :group 'nxml
  51. :type 'boolean)
  52. (defcustom nxml-child-indent 2
  53. "Indentation for the children of an element relative to the start-tag.
  54. This only applies when the line or lines containing the start-tag contains
  55. nothing else other than that start-tag."
  56. :group 'nxml
  57. :type 'integer)
  58. (defcustom nxml-attribute-indent 4
  59. "Indentation for the attributes of an element relative to the start-tag.
  60. This only applies when the first attribute of a tag starts a line.
  61. In other cases, the first attribute on one line is indented the same
  62. as the first attribute on the previous line."
  63. :group 'nxml
  64. :type 'integer)
  65. (defcustom nxml-bind-meta-tab-to-complete-flag t
  66. "Non-nil means to use nXML completion in \\[completion-at-point]."
  67. :group 'nxml
  68. :type 'boolean)
  69. (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
  70. "Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
  71. This is used only when a buffer does not contain an encoding declaration
  72. and when its current `buffer-file-coding-system' specifies neither UTF-16
  73. nor UTF-8."
  74. :group 'nxml
  75. :type 'boolean)
  76. (defcustom nxml-prefer-utf-16-little-to-big-endian-flag (eq system-type
  77. 'windows-nt)
  78. "Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
  79. This is used only for saving a buffer; when reading the byte-order is
  80. auto-detected. It may be relevant both when there is no encoding declaration
  81. and when the encoding declaration specifies `UTF-16'."
  82. :group 'nxml
  83. :type 'boolean)
  84. (defcustom nxml-default-buffer-file-coding-system nil
  85. "Default value for `buffer-file-coding-system' for a buffer for a new file.
  86. A value of nil means use the default value of `buffer-file-coding-system' as normal.
  87. A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
  88. :group 'nxml
  89. :type 'coding-system)
  90. (defcustom nxml-auto-insert-xml-declaration-flag nil
  91. "Non-nil means automatically insert an XML declaration in a new file.
  92. The XML declaration is inserted using `nxml-insert-xml-declaration'."
  93. :group 'nxml
  94. :type 'boolean)
  95. (defface nxml-delimited-data
  96. '((t (:inherit font-lock-doc-face)))
  97. "Face used to highlight data enclosed between delimiters.
  98. This is not used directly, but only via inheritance by other faces."
  99. :group 'nxml-faces)
  100. (defface nxml-name
  101. '((t (:inherit font-lock-builtin-face)))
  102. "Face used to highlight various names.
  103. This includes element and attribute names, processing
  104. instruction targets and the CDATA keyword in a CDATA section.
  105. This is not used directly, but only via inheritance by other faces."
  106. :group 'nxml-faces)
  107. (defface nxml-ref
  108. '((t (:inherit font-lock-constant-face)))
  109. "Face used to highlight character and entity references.
  110. This is not used directly, but only via inheritance by other faces."
  111. :group 'nxml-faces)
  112. (defface nxml-delimiter
  113. nil
  114. "Face used to highlight delimiters.
  115. This is not used directly, but only via inheritance by other faces."
  116. :group 'nxml-faces)
  117. (defface nxml-text
  118. nil
  119. "Face used to highlight text."
  120. :group 'nxml-faces)
  121. (defface nxml-processing-instruction-delimiter
  122. '((t (:inherit nxml-delimiter)))
  123. "Face used for the delimiters of processing instructions, i.e., <? and ?>."
  124. :group 'nxml-faces)
  125. (defface nxml-processing-instruction-target
  126. '((t (:inherit font-lock-keyword-face)))
  127. "Face used for the target of processing instructions."
  128. :group 'nxml-faces)
  129. (defface nxml-processing-instruction-content
  130. '((t (:inherit nxml-delimited-data)))
  131. "Face used for the content of processing instructions."
  132. :group 'nxml-faces)
  133. (defface nxml-cdata-section-delimiter
  134. '((t (:inherit nxml-delimiter)))
  135. "Face used for the delimiters of CDATA sections, i.e., <![, [, and ]]>."
  136. :group 'nxml-faces)
  137. (defface nxml-cdata-section-CDATA
  138. '((t (:inherit nxml-name)))
  139. "Face used for the CDATA keyword in CDATA sections."
  140. :group 'nxml-faces)
  141. (defface nxml-cdata-section-content
  142. '((t (:inherit nxml-text)))
  143. "Face used for the content of CDATA sections."
  144. :group 'nxml-faces)
  145. (defface nxml-char-ref-number
  146. '((t (:inherit nxml-ref)))
  147. "Face used for the number in character references.
  148. This includes ths `x' in hex references."
  149. :group 'nxml-faces)
  150. (defface nxml-char-ref-delimiter
  151. '((t (:inherit nxml-ref)))
  152. "Face used for the delimiters of character references, i.e., &# and ;."
  153. :group 'nxml-faces)
  154. (defface nxml-entity-ref-name
  155. '((t (:inherit nxml-ref)))
  156. "Face used for the entity name in general entity references."
  157. :group 'nxml-faces)
  158. (defface nxml-entity-ref-delimiter
  159. '((t (:inherit nxml-ref)))
  160. "Face used for the delimiters of entity references, i.e., & and ;."
  161. :group 'nxml-faces)
  162. (defface nxml-tag-delimiter
  163. '((t (:inherit nxml-delimiter)))
  164. "Face used for the angle brackets delimiting tags.
  165. `nxml-tag-slash' is used for slashes."
  166. :group 'nxml-faces)
  167. (defface nxml-tag-slash
  168. '((t (:inherit nxml-tag-delimiter)))
  169. "Face used for slashes in tags, both in end-tags and empty-elements."
  170. :group 'nxml-faces)
  171. (defface nxml-element-prefix
  172. '((t (:inherit nxml-name)))
  173. "Face used for the prefix of elements."
  174. :group 'nxml-faces)
  175. (defface nxml-element-colon
  176. nil
  177. "Face used for the colon in element names."
  178. :group 'nxml-faces)
  179. (defface nxml-element-local-name
  180. '((t (:inherit font-lock-function-name-face)))
  181. "Face used for the local name of elements."
  182. :group 'nxml-faces)
  183. (defface nxml-attribute-prefix
  184. '((t (:inherit nxml-name)))
  185. "Face used for the prefix of attributes."
  186. :group 'nxml-faces)
  187. (defface nxml-attribute-colon
  188. '((t (:inherit nxml-delimiter)))
  189. "Face used for the colon in attribute names."
  190. :group 'nxml-faces)
  191. (defface nxml-attribute-local-name
  192. '((t (:inherit font-lock-variable-name-face)))
  193. "Face used for the local name of attributes."
  194. :group 'nxml-faces)
  195. (defface nxml-namespace-attribute-xmlns
  196. '((t (:inherit nxml-attribute-prefix)))
  197. "Face used for `xmlns' in namespace attributes."
  198. :group 'nxml-faces)
  199. (defface nxml-namespace-attribute-colon
  200. '((t (:inherit nxml-attribute-colon)))
  201. "Face used for the colon in namespace attributes."
  202. :group 'nxml-faces)
  203. (defface nxml-namespace-attribute-prefix
  204. '((t (:inherit nxml-attribute-local-name)))
  205. "Face used for the prefix declared in namespace attributes."
  206. :group 'nxml-faces)
  207. (defface nxml-attribute-value
  208. '((t (:inherit font-lock-string-face)))
  209. "Face used for the value of attributes."
  210. :group 'nxml-faces)
  211. (defface nxml-attribute-value-delimiter
  212. '((t (:inherit nxml-attribute-value)))
  213. "Face used for the delimiters of attribute values."
  214. :group 'nxml-faces)
  215. (defface nxml-prolog-literal-delimiter
  216. '((t (:inherit nxml-delimited-data)))
  217. "Face used for the delimiters of literals in the prolog."
  218. :group 'nxml-faces)
  219. (defface nxml-prolog-literal-content
  220. '((t (:inherit nxml-delimited-data)))
  221. "Face used for the content of literals in the prolog."
  222. :group 'nxml-faces)
  223. (defface nxml-prolog-keyword
  224. '((t (:inherit font-lock-keyword-face)))
  225. "Face used for keywords in the prolog."
  226. :group 'nxml-faces)
  227. (defface nxml-markup-declaration-delimiter
  228. '((t (:inherit nxml-delimiter)))
  229. "Face used for the delimiters of markup declarations in the prolog.
  230. The delimiters are <! and >."
  231. :group 'nxml-faces)
  232. (defface nxml-hash
  233. '((t (:inherit nxml-name)))
  234. "Face used for # before a name in the prolog."
  235. :group 'nxml-faces)
  236. (defface nxml-glyph
  237. '((((type x))
  238. (:family
  239. "misc-fixed"
  240. :background
  241. "light grey"
  242. :foreground
  243. "black"
  244. :weight
  245. normal
  246. :slant
  247. normal))
  248. (t
  249. (:background
  250. "light grey"
  251. :foreground
  252. "black"
  253. :weight
  254. normal
  255. :slant
  256. normal)))
  257. "Face used for glyph for char references."
  258. :group 'nxml-faces)
  259. ;;; Global variables
  260. (defvar-local nxml-parent-document nil
  261. "The parent document for a part of a modular document.
  262. Use `nxml-parent-document-set' to set it.")
  263. (put 'nxml-parent-document 'safe-local-variable 'stringp)
  264. (defvar-local nxml-prolog-regions nil
  265. "List of regions in the prolog to be fontified.
  266. See the function `xmltok-forward-prolog' for more information.")
  267. (defvar-local nxml-degraded nil
  268. "Non-nil if currently operating in degraded mode.
  269. Degraded mode is enabled when an internal error is encountered in the
  270. fontification or after-change functions.")
  271. (defvar nxml-completion-hook nil
  272. "Hook run by `nxml-complete'.
  273. This hook is run until success.")
  274. (defvar nxml-in-mixed-content-hook nil
  275. "Hook to determine whether point is in mixed content.
  276. The hook is called without arguments. It should return nil if it is
  277. definitely not mixed; non-nil otherwise. The hook will be run until
  278. one of the functions returns nil.")
  279. (defvar nxml-mixed-scan-distance 4000
  280. "Maximum distance from point to scan when checking for mixed content.")
  281. (defvar nxml-end-tag-indent-scan-distance 4000
  282. "Maximum distance from point to scan backwards when indenting end-tag.")
  283. (defvar-local nxml-char-ref-extra-display t
  284. "Non-nil means display extra information for character references.
  285. The extra information consists of a tooltip with the character name
  286. and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
  287. corresponding to the referenced character following the character
  288. reference.")
  289. (defvar nxml-mode-map
  290. (let ((map (make-sparse-keymap)))
  291. (define-key map "\M-\C-u" 'nxml-backward-up-element)
  292. (define-key map "\M-\C-d" 'nxml-down-element)
  293. (define-key map "\M-\C-n" 'nxml-forward-element)
  294. (define-key map "\M-\C-p" 'nxml-backward-element)
  295. (define-key map "\M-{" 'nxml-backward-paragraph)
  296. (define-key map "\M-}" 'nxml-forward-paragraph)
  297. (define-key map "\M-h" 'nxml-mark-paragraph)
  298. (define-key map "\C-c\C-f" 'nxml-finish-element)
  299. (define-key map "\C-c]" 'nxml-finish-element)
  300. (define-key map "\C-c/" 'nxml-finish-element)
  301. (define-key map "\C-c\C-m" 'nxml-split-element)
  302. (define-key map "\C-c\C-b" 'nxml-balanced-close-start-tag-block)
  303. (define-key map "\C-c\C-i" 'nxml-balanced-close-start-tag-inline)
  304. (define-key map "\C-c\C-x" 'nxml-insert-xml-declaration)
  305. (define-key map "\C-c\C-d" 'nxml-dynamic-markup-word)
  306. ;; u is for Unicode
  307. (define-key map "\C-c\C-u" 'nxml-insert-named-char)
  308. (define-key map "\C-c\C-o" nxml-outline-prefix-map)
  309. (define-key map [S-mouse-2] 'nxml-mouse-hide-direct-text-content)
  310. (define-key map "/" 'nxml-electric-slash)
  311. (define-key map "\M-\t" 'completion-at-point)
  312. map)
  313. "Keymap for nxml-mode.")
  314. (defvar nxml-font-lock-keywords
  315. '(nxml-fontify-matcher)
  316. "Default font lock keywords for nxml-mode.")
  317. (defsubst nxml-set-face (start end face)
  318. (when (and face (< start end))
  319. ;; Prepend, so the character reference highlighting takes precedence over
  320. ;; the string highlighting applied syntactically.
  321. (font-lock-prepend-text-property start end 'face face)))
  322. (defun nxml-parent-document-set (parent-document)
  323. "Set `nxml-parent-document' and inherit the DTD &c."
  324. ;; FIXME: this does not work.
  325. ;; the idea is that by inheriting some variables from the parent,
  326. ;; `rng-validate-mode' will validate entities declared in the parent.
  327. ;; alas, the most interesting variables (`rng-compile-table' et al)
  328. ;; are circular and cannot be printed even with `print-circle'.
  329. (interactive "fParent document")
  330. (let (dtd current-schema current-schema-file-name compile-table
  331. ipattern-table last-ipattern-index)
  332. (when (string= (file-truename parent-document)
  333. (file-truename buffer-file-name))
  334. (error "Parent document cannot be the same as the document"))
  335. (with-current-buffer (find-file-noselect parent-document)
  336. (setq dtd rng-dtd
  337. current-schema rng-current-schema
  338. current-schema-file-name rng-current-schema-file-name
  339. compile-table rng-compile-table
  340. ipattern-table rng-ipattern-table
  341. last-ipattern-index rng-last-ipattern-index
  342. parent-document buffer-file-name))
  343. (setq rng-dtd dtd
  344. rng-current-schema current-schema
  345. rng-current-schema-file-name current-schema-file-name
  346. rng-compile-table compile-table
  347. rng-ipattern-table ipattern-table
  348. rng-last-ipattern-index last-ipattern-index
  349. nxml-parent-document parent-document)
  350. (message "Set parent document to %s" parent-document)
  351. (when rng-validate-mode
  352. (rng-validate-while-idle (current-buffer)))))
  353. (defvar tildify-space-string)
  354. (defvar tildify-foreach-region-function)
  355. ;;;###autoload
  356. (define-derived-mode nxml-mode text-mode "nXML"
  357. ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
  358. ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
  359. ;; not mnemonic.
  360. "Major mode for editing XML.
  361. \\[nxml-finish-element] finishes the current element by inserting an end-tag.
  362. C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
  363. leaving point between the start-tag and end-tag.
  364. \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
  365. the start-tag, point, and end-tag are all left on separate lines.
  366. If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
  367. automatically inserts the rest of the end-tag.
  368. \\[completion-at-point] performs completion on the symbol preceding point.
  369. \\[nxml-dynamic-markup-word] uses the contents of the current buffer
  370. to choose a tag to put around the word preceding point.
  371. Sections of the document can be displayed in outline form. The
  372. variable `nxml-section-element-name-regexp' controls when an element
  373. is recognized as a section. The same key sequences that change
  374. visibility in outline mode are used except that they start with C-c C-o
  375. instead of C-c.
  376. Validation is provided by the related minor-mode `rng-validate-mode'.
  377. This also makes completion schema- and context- sensitive. Element
  378. names, attribute names, attribute values and namespace URIs can all be
  379. completed. By default, `rng-validate-mode' is automatically enabled.
  380. You can toggle it using \\[rng-validate-mode] or change the default by
  381. customizing `rng-nxml-auto-validate-flag'.
  382. \\[indent-for-tab-command] indents the current line appropriately.
  383. This can be customized using the variable `nxml-child-indent'
  384. and the variable `nxml-attribute-indent'.
  385. \\[nxml-insert-named-char] inserts a character reference using
  386. the character's name (by default, the Unicode name).
  387. \\[universal-argument] \\[nxml-insert-named-char] inserts the character directly.
  388. The Emacs commands that normally operate on balanced expressions will
  389. operate on XML markup items. Thus \\[forward-sexp] will move forward
  390. across one markup item; \\[backward-sexp] will move backward across
  391. one markup item; \\[kill-sexp] will kill the following markup item;
  392. \\[mark-sexp] will mark the following markup item. By default, each
  393. tag each treated as a single markup item; to make the complete element
  394. be treated as a single markup item, set the variable
  395. `nxml-sexp-element-flag' to t. For more details, see the function
  396. `nxml-forward-balanced-item'.
  397. \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
  398. Many aspects this mode can be customized using
  399. \\[customize-group] nxml RET."
  400. ;; (kill-all-local-variables)
  401. ;; If encoding does not allow non-break space character, use reference.
  402. ;; FIXME: This duplicates code from sgml-mode, perhaps derive from it?
  403. ;; FIXME: Perhaps use &nbsp; if possible (e.g. XHTML)?
  404. (setq-local tildify-space-string
  405. (if (equal (decode-coding-string
  406. (encode-coding-string " " buffer-file-coding-system)
  407. buffer-file-coding-system) " ")
  408. " " "&#160;"))
  409. ;; FIXME: Use the fact that we're parsing the document already
  410. ;; rather than using regex-based filtering.
  411. (setq-local tildify-foreach-region-function
  412. (apply-partially #'tildify-foreach-ignore-environments
  413. '(("<! *--" . "-- *>") ("<" . ">"))))
  414. (setq-local mode-line-process '((nxml-degraded "/degraded")))
  415. ;; We'll determine the fill prefix ourselves
  416. (setq-local adaptive-fill-mode nil)
  417. (setq-local forward-sexp-function #'nxml-forward-balanced-item)
  418. (setq-local indent-line-function #'nxml-indent-line)
  419. (setq-local fill-paragraph-function #'nxml-do-fill-paragraph)
  420. ;; Comment support
  421. ;; This doesn't seem to work too well;
  422. ;; I think we should probably roll our own nxml-comment-dwim function.
  423. (setq-local comment-indent-function #'nxml-indent-line)
  424. (setq-local comment-start "<!--")
  425. (setq-local comment-start-skip "<!--[ \t\r\n]*")
  426. (setq-local comment-end "-->")
  427. (setq-local comment-end-skip "[ \t\r\n]*-->")
  428. (setq-local comment-line-break-function #'nxml-newline-and-indent)
  429. (setq-local comment-quote-nested-function #'nxml-comment-quote-nested)
  430. (save-excursion
  431. (save-restriction
  432. (widen)
  433. (with-silent-modifications
  434. (nxml-with-invisible-motion
  435. (nxml-scan-prolog)))))
  436. (setq-local syntax-ppss-table sgml-tag-syntax-table)
  437. (setq-local syntax-propertize-function #'sgml-syntax-propertize)
  438. (add-hook 'change-major-mode-hook #'nxml-cleanup nil t)
  439. ;; Emacs 23 handles the encoding attribute on the xml declaration
  440. ;; transparently to nxml-mode, so there is no longer a need for the below
  441. ;; hook. The hook also had the drawback of overriding explicit user
  442. ;; instruction to save as some encoding other than utf-8.
  443. ;;(add-hook 'write-contents-hooks #'nxml-prepare-to-save)
  444. (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
  445. (when (and nxml-default-buffer-file-coding-system
  446. (not (local-variable-p 'buffer-file-coding-system)))
  447. (setq buffer-file-coding-system nxml-default-buffer-file-coding-system))
  448. (when nxml-auto-insert-xml-declaration-flag
  449. (nxml-insert-xml-declaration)))
  450. (setq font-lock-defaults
  451. '(nxml-font-lock-keywords
  452. nil ; highlight comments and strings based on syntax-tables
  453. nil ; font-lock-keywords-case-fold-search. XML is case sensitive
  454. nil ; no special syntax table
  455. (font-lock-extend-region-functions . (nxml-extend-region))
  456. (jit-lock-contextually . t)
  457. (font-lock-unfontify-region-function . nxml-unfontify-region)))
  458. (with-demoted-errors (rng-nxml-mode-init)))
  459. (defun nxml-cleanup ()
  460. "Clean up after nxml-mode."
  461. ;; Disable associated minor modes.
  462. (rng-validate-mode -1)
  463. ;; Clean up fontification.
  464. (save-excursion
  465. (widen)
  466. (with-silent-modifications
  467. (nxml-with-invisible-motion
  468. (remove-text-properties (point-min) (point-max) '(face)))))
  469. (remove-hook 'change-major-mode-hook #'nxml-cleanup t))
  470. (defun nxml-degrade (context err)
  471. (message "Internal nXML mode error in %s (%s), degrading"
  472. context
  473. (error-message-string err))
  474. (ding)
  475. (setq nxml-degraded t)
  476. (setq nxml-prolog-end 1))
  477. ;;; Change management
  478. (defvar font-lock-beg) (defvar font-lock-end)
  479. (defun nxml-debug-region (start end)
  480. (interactive "r")
  481. (let ((font-lock-beg start)
  482. (font-lock-end end))
  483. (nxml-extend-region)
  484. (goto-char font-lock-beg)
  485. (set-mark font-lock-end)))
  486. ;;; Encodings
  487. (defun nxml-insert-xml-declaration ()
  488. "Insert an XML declaration at the beginning of buffer.
  489. The XML declaration will declare an encoding depending on the buffer's
  490. `buffer-file-coding-system'."
  491. (interactive "*")
  492. (let ((coding-system
  493. (if (and buffer-file-coding-system
  494. (coding-system-p buffer-file-coding-system)
  495. (coding-system-get buffer-file-coding-system
  496. 'mime-charset))
  497. buffer-file-coding-system
  498. (nxml-choose-utf-coding-system))))
  499. (goto-char (point-min))
  500. (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
  501. (nxml-coding-system-name coding-system)))))
  502. (defun nxml-prepare-to-save ()
  503. (unless (and (not enable-multibyte-characters)
  504. (local-variable-p 'buffer-file-coding-system)
  505. buffer-file-coding-system
  506. (or (eq (coding-system-type buffer-file-coding-system) 5)
  507. (eq buffer-file-coding-system 'no-conversion)))
  508. (save-excursion
  509. (setq buffer-file-coding-system (nxml-select-coding-system))))
  510. ;; nil from a function in `write-contents-hooks' means
  511. ;; to continue and write the file as normal
  512. nil)
  513. (defun nxml-select-coding-system ()
  514. (let* ((suitable-coding-systems
  515. (find-coding-systems-region (point-min) (point-max)))
  516. (enc-pos (progn
  517. (goto-char (point-min))
  518. (xmltok-get-declared-encoding-position)))
  519. (enc-name
  520. (and (consp enc-pos)
  521. (buffer-substring-no-properties (car enc-pos)
  522. (cdr enc-pos))))
  523. (coding-system
  524. (cond (enc-name
  525. (if (string= (downcase enc-name) "utf-16")
  526. (nxml-choose-utf-16-coding-system)
  527. (nxml-mime-charset-coding-system enc-name)))
  528. (enc-pos (nxml-choose-utf-coding-system)))))
  529. ;; Make sure we have a coding-system
  530. (unless coding-system
  531. (setq coding-system
  532. (and (not buffer-read-only)
  533. (nxml-choose-suitable-coding-system
  534. suitable-coding-systems)))
  535. (let ((message
  536. (if enc-name
  537. (format "Unknown encoding %s" enc-name)
  538. "XML declaration is not well-formed")))
  539. (cond ((not coding-system)
  540. (error "%s" message))
  541. ((y-or-n-p
  542. (concat message
  543. ". "
  544. (format (if enc-name
  545. "Save with %s"
  546. "Modify and save with encoding %s")
  547. (nxml-coding-system-name coding-system))
  548. " "))
  549. (nxml-fix-encoding-declaration enc-pos coding-system))
  550. (t (signal 'quit nil)))))
  551. ;; Make sure it can encode all the characters in the buffer
  552. (unless (or (memq (coding-system-base coding-system)
  553. suitable-coding-systems)
  554. (equal suitable-coding-systems '(undecided)))
  555. (let ((message
  556. (nxml-unsuitable-coding-system-message coding-system
  557. enc-name)))
  558. (setq coding-system
  559. (and (not buffer-read-only)
  560. (nxml-choose-suitable-coding-system
  561. suitable-coding-systems)))
  562. (cond ((not coding-system) (error "%s" message))
  563. ((y-or-n-p (concat message
  564. (format ". Save with %s "
  565. (nxml-coding-system-name
  566. coding-system))))
  567. (nxml-fix-encoding-declaration enc-pos coding-system))
  568. (t (signal 'quit nil)))))
  569. ;; Merge the newline type of our existing encoding
  570. (let ((current-eol-type
  571. (coding-system-eol-type buffer-file-coding-system)))
  572. (when (and current-eol-type (integerp current-eol-type))
  573. (setq coding-system
  574. (coding-system-change-eol-conversion coding-system
  575. current-eol-type))))
  576. coding-system))
  577. (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name)
  578. (if (nxml-coding-system-unicode-p coding-system)
  579. "Cannot translate some characters to Unicode"
  580. (format "Cannot encode some characters with %s"
  581. (or enc-name
  582. (nxml-coding-system-name coding-system)))))
  583. (defconst nxml-utf-16-coding-systems (and (coding-system-p 'utf-16-be)
  584. (coding-system-p 'utf-16-le)
  585. '(utf-16-be utf-16-le)))
  586. (defconst nxml-utf-coding-systems (cons 'utf-8 nxml-utf-16-coding-systems))
  587. (defun nxml-coding-system-unicode-p (coding-system)
  588. (nxml-coding-system-member (coding-system-base coding-system)
  589. nxml-utf-coding-systems))
  590. (defun nxml-coding-system-name (coding-system)
  591. (setq coding-system (coding-system-base coding-system))
  592. (symbol-name
  593. (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems)
  594. 'utf-16
  595. (or (coding-system-get coding-system 'mime-charset)
  596. coding-system))))
  597. (defun nxml-fix-encoding-declaration (enc-pos coding-system)
  598. (let ((charset (nxml-coding-system-name coding-system)))
  599. (cond ((consp enc-pos)
  600. (delete-region (car enc-pos) (cdr enc-pos))
  601. (goto-char (car enc-pos))
  602. (insert charset))
  603. ((integerp enc-pos)
  604. (goto-char enc-pos)
  605. (insert " encoding=\"" charset ?\"))
  606. (t
  607. (goto-char (point-min))
  608. (insert "<?xml version=\"1.0\" encoding=\""
  609. charset
  610. "\"?>\n")
  611. (when (and (not enc-pos)
  612. (let ((case-fold-search t))
  613. (looking-at xmltok-bad-xml-decl-regexp)))
  614. (delete-region (point) (match-end 0)))))))
  615. (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
  616. (let (ret coding-system)
  617. (if (and buffer-file-coding-system
  618. (memq (coding-system-base buffer-file-coding-system)
  619. suitable-coding-systems))
  620. buffer-file-coding-system
  621. (while (and suitable-coding-systems (not ret))
  622. (setq coding-system (car suitable-coding-systems))
  623. (if (coding-system-get coding-system 'mime-charset)
  624. (setq ret coding-system)
  625. (setq suitable-coding-systems (cdr suitable-coding-systems))))
  626. ret)))
  627. (defun nxml-choose-utf-coding-system ()
  628. (let ((cur (and (local-variable-p 'buffer-file-coding-system)
  629. buffer-file-coding-system
  630. (coding-system-base buffer-file-coding-system))))
  631. (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems)))
  632. ((and nxml-prefer-utf-16-to-utf-8-flag
  633. (coding-system-p 'utf-16-le)
  634. (coding-system-p 'utf-16-be))
  635. (if nxml-prefer-utf-16-little-to-big-endian-flag
  636. 'utf-16-le
  637. 'utf-16-be))
  638. (t 'utf-8))))
  639. (defun nxml-choose-utf-16-coding-system ()
  640. (let ((cur (and (local-variable-p 'buffer-file-coding-system)
  641. buffer-file-coding-system
  642. (coding-system-base buffer-file-coding-system))))
  643. (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems)))
  644. (nxml-prefer-utf-16-little-to-big-endian-flag
  645. (and (coding-system-p 'utf-16-le) 'utf-16-le))
  646. (t (and (coding-system-p 'utf-16-be) 'utf-16-be)))))
  647. (defun nxml-coding-system-member (coding-system coding-systems)
  648. (let (ret)
  649. (while (and coding-systems (not ret))
  650. (if (coding-system-equal coding-system
  651. (car coding-systems))
  652. (setq ret coding-systems)
  653. (setq coding-systems (cdr coding-systems))))
  654. ret))
  655. ;;; Fontification
  656. (defun nxml-unfontify-region (start end)
  657. (font-lock-default-unfontify-region start end)
  658. (nxml-clear-char-ref-extra-display start end))
  659. (defun nxml-extend-region ()
  660. "Extend the region to hold the minimum area we can fontify with nXML.
  661. Called with `font-lock-beg' and `font-lock-end' dynamically bound."
  662. (let ((start font-lock-beg)
  663. (end font-lock-end))
  664. (nxml-debug-change "nxml-extend-region(input)" start end)
  665. (when (< start nxml-prolog-end)
  666. (setq start (point-min)))
  667. (cond ((<= end nxml-prolog-end)
  668. (setq end nxml-prolog-end))
  669. (t
  670. (goto-char start)
  671. ;; some font-lock backends (like Emacs 22 jit-lock) snap
  672. ;; the region to the beginning of the line no matter what
  673. ;; we say here. To mitigate the resulting excess
  674. ;; fontification, ignore leading whitespace.
  675. (skip-syntax-forward " ")
  676. ;; find the beginning of the previous tag
  677. (when (not (equal (char-after) ?\<))
  678. (search-backward "<" nxml-prolog-end t))
  679. (nxml-ensure-scan-up-to-date)
  680. (nxml-move-outside-backwards)
  681. (setq start (point))
  682. (while (< (point) end)
  683. (nxml-tokenize-forward))
  684. (setq end (point))))
  685. (when (or (< start font-lock-beg)
  686. (> end font-lock-end))
  687. (setq font-lock-beg start
  688. font-lock-end end)
  689. (nxml-debug-change "nxml-extend-region" start end)
  690. t)))
  691. (defun nxml-fontify-matcher (bound)
  692. "Called as font-lock keyword matcher."
  693. (syntax-propertize bound)
  694. (unless nxml-degraded
  695. (nxml-debug-change "nxml-fontify-matcher" (point) bound)
  696. (when (< (point) nxml-prolog-end)
  697. ;; Prolog needs to be fontified in one go, and
  698. ;; nxml-extend-region makes sure we start at BOB.
  699. (cl-assert (bobp))
  700. (nxml-fontify-prolog)
  701. (goto-char nxml-prolog-end))
  702. (let (xmltok-errors)
  703. (while (and (nxml-tokenize-forward)
  704. (<= (point) bound)) ; Intervals are open-ended.
  705. (nxml-apply-fontify-rule)))
  706. )
  707. ;; Since we did the fontification internally, tell font-lock to not
  708. ;; do anything itself.
  709. nil)
  710. (defun nxml-fontify-prolog ()
  711. "Fontify the prolog.
  712. The buffer is assumed to be prepared for fontification.
  713. This does not set the fontified property, but it does clear
  714. faces appropriately."
  715. (let ((regions nxml-prolog-regions))
  716. (while regions
  717. (let ((region (car regions)))
  718. (nxml-apply-fontify-rule (aref region 0)
  719. (aref region 1)
  720. (aref region 2)))
  721. (setq regions (cdr regions)))))
  722. ;; Vectors identify a substring of the token to be highlighted in some face.
  723. ;; Token types returned by xmltok-forward.
  724. (put 'start-tag
  725. 'nxml-fontify-rule
  726. '([nil 1 nxml-tag-delimiter]
  727. [-1 nil nxml-tag-delimiter]
  728. (element-qname . 1)
  729. attributes))
  730. (put 'partial-start-tag
  731. 'nxml-fontify-rule
  732. '([nil 1 nxml-tag-delimiter]
  733. (element-qname . 1)
  734. attributes))
  735. (put 'end-tag
  736. 'nxml-fontify-rule
  737. '([nil 1 nxml-tag-delimiter]
  738. [1 2 nxml-tag-slash]
  739. [-1 nil nxml-tag-delimiter]
  740. (element-qname . 2)))
  741. (put 'partial-end-tag
  742. 'nxml-fontify-rule
  743. '([nil 1 nxml-tag-delimiter]
  744. [1 2 nxml-tag-slash]
  745. (element-qname . 2)))
  746. (put 'empty-element
  747. 'nxml-fontify-rule
  748. '([nil 1 nxml-tag-delimiter]
  749. [-2 -1 nxml-tag-slash]
  750. [-1 nil nxml-tag-delimiter]
  751. (element-qname . 1)
  752. attributes))
  753. (put 'partial-empty-element
  754. 'nxml-fontify-rule
  755. '([nil 1 nxml-tag-delimiter]
  756. [-1 nil nxml-tag-slash]
  757. (element-qname . 1)
  758. attributes))
  759. (put 'char-ref
  760. 'nxml-fontify-rule
  761. '([nil 2 nxml-char-ref-delimiter]
  762. [2 -1 nxml-char-ref-number]
  763. [-1 nil nxml-char-ref-delimiter]
  764. char-ref))
  765. (put 'entity-ref
  766. 'nxml-fontify-rule
  767. '([nil 1 nxml-entity-ref-delimiter]
  768. [1 -1 nxml-entity-ref-name]
  769. [-1 nil nxml-entity-ref-delimiter]))
  770. ;; (put 'comment
  771. ;; 'nxml-fontify-rule
  772. ;; '([nil 4 nxml-comment-delimiter]
  773. ;; [4 -3 nxml-comment-content]
  774. ;; [-3 nil nxml-comment-delimiter]))
  775. (put 'processing-instruction
  776. 'nxml-fontify-rule
  777. '([nil 2 nxml-processing-instruction-delimiter]
  778. [-2 nil nxml-processing-instruction-delimiter]
  779. processing-instruction-content))
  780. (put 'cdata-section
  781. 'nxml-fontify-rule
  782. '([nil 3 nxml-cdata-section-delimiter] ; <![
  783. [3 8 nxml-cdata-section-CDATA] ; CDATA
  784. [8 9 nxml-cdata-section-delimiter] ; [
  785. [9 -3 nxml-cdata-section-content] ; ]]>
  786. [-3 nil nxml-cdata-section-delimiter]))
  787. (put 'data
  788. 'nxml-fontify-rule
  789. '([nil nil nxml-text]))
  790. ;; Prolog region types in list returned by xmltok-forward-prolog.
  791. (put 'xml-declaration
  792. 'nxml-fontify-rule
  793. '([nil 2 nxml-processing-instruction-delimiter]
  794. [2 5 nxml-processing-instruction-target]
  795. [-2 nil nxml-processing-instruction-delimiter]))
  796. (put 'xml-declaration-attribute-name
  797. 'nxml-fontify-rule
  798. '([nil nil nxml-attribute-local-name]))
  799. (put 'xml-declaration-attribute-value ;FIXME: What is this for?
  800. 'nxml-fontify-rule
  801. '([nil 1 nxml-attribute-value-delimiter]
  802. [1 -1 nxml-attribute-value]
  803. [-1 nil nxml-attribute-value-delimiter]))
  804. (put 'processing-instruction-left
  805. 'nxml-fontify-rule
  806. '([nil 2 nxml-processing-instruction-delimiter]
  807. [2 nil nxml-processing-instruction-target]))
  808. (put 'processing-instruction-right
  809. 'nxml-fontify-rule
  810. '([nil -2 nxml-processing-instruction-content]
  811. [-2 nil nxml-processing-instruction-delimiter]))
  812. (put 'literal
  813. 'nxml-fontify-rule
  814. '([nil 1 nxml-prolog-literal-delimiter]
  815. [1 -1 nxml-prolog-literal-content]
  816. [-1 nil nxml-prolog-literal-delimiter]))
  817. (put 'keyword
  818. 'nxml-fontify-rule
  819. '([nil nil nxml-prolog-keyword]))
  820. (put 'markup-declaration-open
  821. 'nxml-fontify-rule
  822. '([0 2 nxml-markup-declaration-delimiter]
  823. [2 nil nxml-prolog-keyword]))
  824. (put 'markup-declaration-close
  825. 'nxml-fontify-rule
  826. '([nil nil nxml-markup-declaration-delimiter]))
  827. (put 'internal-subset-open
  828. 'nxml-fontify-rule
  829. '([nil nil nxml-markup-declaration-delimiter]))
  830. (put 'internal-subset-close
  831. 'nxml-fontify-rule
  832. '([nil 1 nxml-markup-declaration-delimiter]
  833. [-1 nil nxml-markup-declaration-delimiter]))
  834. (put 'hash-name
  835. 'nxml-fontify-rule
  836. '([nil 1 nxml-hash]
  837. [1 nil nxml-prolog-keyword]))
  838. (defun nxml-apply-fontify-rule (&optional type start end)
  839. (let ((rule (get (or type xmltok-type) 'nxml-fontify-rule)))
  840. (unless start (setq start xmltok-start))
  841. (unless end (setq end (point)))
  842. (while rule
  843. (let* ((action (car rule)))
  844. (setq rule (cdr rule))
  845. (cond ((vectorp action)
  846. (nxml-set-face (let ((offset (aref action 0)))
  847. (cond ((not offset) start)
  848. ((< offset 0) (+ end offset))
  849. (t (+ start offset))))
  850. (let ((offset (aref action 1)))
  851. (cond ((not offset) end)
  852. ((< offset 0) (+ end offset))
  853. (t (+ start offset))))
  854. (aref action 2)))
  855. ((and (consp action)
  856. (eq (car action) 'element-qname))
  857. (when xmltok-name-end ; maybe nil in partial-end-tag case
  858. (nxml-fontify-qname (+ start (cdr action))
  859. xmltok-name-colon
  860. xmltok-name-end
  861. 'nxml-element-prefix
  862. 'nxml-element-colon
  863. 'nxml-element-local-name)))
  864. ((eq action 'attributes)
  865. (nxml-fontify-attributes))
  866. ((eq action 'processing-instruction-content)
  867. (nxml-set-face (+ start 2)
  868. xmltok-name-end
  869. 'nxml-processing-instruction-target)
  870. (nxml-set-face (save-excursion
  871. (goto-char xmltok-name-end)
  872. (skip-chars-forward " \t\r\n")
  873. (point))
  874. (- end 2)
  875. 'nxml-processing-instruction-content))
  876. ((eq action 'char-ref)
  877. (nxml-char-ref-display-extra start
  878. end
  879. (xmltok-char-number start end)))
  880. (t (error "Invalid nxml-fontify-rule action %s" action)))))))
  881. (defun nxml-fontify-attributes ()
  882. (while xmltok-namespace-attributes
  883. (nxml-fontify-attribute (car xmltok-namespace-attributes)
  884. 'namespace)
  885. (setq xmltok-namespace-attributes
  886. (cdr xmltok-namespace-attributes)))
  887. (while xmltok-attributes
  888. (nxml-fontify-attribute (car xmltok-attributes))
  889. (setq xmltok-attributes
  890. (cdr xmltok-attributes))))
  891. (defun nxml-fontify-attribute (att &optional namespace-declaration)
  892. (if namespace-declaration
  893. (nxml-fontify-qname (xmltok-attribute-name-start att)
  894. (xmltok-attribute-name-colon att)
  895. (xmltok-attribute-name-end att)
  896. 'nxml-namespace-attribute-xmlns
  897. 'nxml-namespace-attribute-colon
  898. 'nxml-namespace-attribute-prefix
  899. 'nxml-namespace-attribute-xmlns)
  900. (nxml-fontify-qname (xmltok-attribute-name-start att)
  901. (xmltok-attribute-name-colon att)
  902. (xmltok-attribute-name-end att)
  903. 'nxml-attribute-prefix
  904. 'nxml-attribute-colon
  905. 'nxml-attribute-local-name))
  906. (dolist (ref (xmltok-attribute-refs att))
  907. (let* ((ref-type (aref ref 0))
  908. (ref-start (aref ref 1))
  909. (ref-end (aref ref 2)))
  910. (nxml-apply-fontify-rule ref-type ref-start ref-end))))
  911. (defun nxml-fontify-qname (start
  912. colon
  913. end
  914. prefix-face
  915. colon-face
  916. local-name-face
  917. &optional
  918. unprefixed-face)
  919. (cond (colon (nxml-set-face start colon prefix-face)
  920. (nxml-set-face colon (1+ colon) colon-face)
  921. (nxml-set-face (1+ colon) end local-name-face))
  922. (t (nxml-set-face start end (or unprefixed-face
  923. local-name-face)))))
  924. ;;; Editing
  925. (defun nxml-electric-slash (arg)
  926. "Insert a slash.
  927. With a prefix ARG, do nothing other than insert the slash.
  928. Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
  929. rest of the end-tag or empty-element if the slash is potentially part
  930. of an end-tag or the close of an empty-element.
  931. If the slash is part of an end-tag that is the first non-whitespace
  932. on the line, reindent the line."
  933. (interactive "*P")
  934. (nxml-ensure-scan-up-to-date)
  935. (let* ((slash-pos (point))
  936. (end-tag-p (and (eq (char-before slash-pos) ?<)
  937. (not (nxml-get-inside slash-pos))))
  938. (at-indentation (save-excursion
  939. (back-to-indentation)
  940. (eq (point) (1- slash-pos)))))
  941. (self-insert-command (prefix-numeric-value arg))
  942. (unless arg
  943. (if nxml-slash-auto-complete-flag
  944. (if end-tag-p
  945. (condition-case nil
  946. (let ((start-tag-end
  947. (nxml-scan-element-backward (1- slash-pos) t)))
  948. (when start-tag-end
  949. (insert (xmltok-start-tag-qname) ">")
  950. ;; copy the indentation of the start-tag
  951. (when (and at-indentation
  952. (save-excursion
  953. (goto-char xmltok-start)
  954. (back-to-indentation)
  955. (eq (point) xmltok-start)))
  956. (save-excursion
  957. (indent-line-to (save-excursion
  958. (goto-char xmltok-start)
  959. (current-column)))))))
  960. (nxml-scan-error nil))
  961. (when (and (eq (nxml-token-before) (point))
  962. (eq xmltok-type 'partial-empty-element))
  963. (insert ">"))))
  964. (when (and end-tag-p at-indentation)
  965. (nxml-indent-line)))))
  966. (defun nxml-balanced-close-start-tag-block ()
  967. "Close the start-tag before point with `>' and insert a balancing end-tag.
  968. Point is left between the start-tag and the end-tag.
  969. If there is nothing but whitespace before the `<' that opens the
  970. start-tag, then put point on a blank line, and put the end-tag on
  971. another line aligned with the start-tag."
  972. (interactive "*")
  973. (nxml-balanced-close-start-tag 'block))
  974. (defun nxml-balanced-close-start-tag-inline ()
  975. "Close the start-tag before point with `>' and insert a balancing end-tag.
  976. Point is left between the start-tag and the end-tag.
  977. No extra whitespace is inserted."
  978. (interactive "*")
  979. (nxml-balanced-close-start-tag 'inline))
  980. (defun nxml-balanced-close-start-tag (block-or-inline)
  981. (let ((token-end (nxml-token-before))
  982. (pos (1+ (point)))
  983. (token-start xmltok-start))
  984. (unless (or (eq xmltok-type 'partial-start-tag)
  985. (and (memq xmltok-type '(start-tag
  986. empty-element
  987. partial-empty-element))
  988. (>= token-end pos)))
  989. (error "Not in a start-tag"))
  990. ;; Note that this insertion changes xmltok-start.
  991. (insert "></"
  992. (buffer-substring-no-properties (+ xmltok-start 1)
  993. (min xmltok-name-end (point)))
  994. ">")
  995. (if (eq block-or-inline 'inline)
  996. (goto-char pos)
  997. (goto-char token-start)
  998. (back-to-indentation)
  999. (if (= (point) token-start)
  1000. (let ((indent (current-column)))
  1001. (goto-char pos)
  1002. (insert "\n")
  1003. (indent-line-to indent)
  1004. (goto-char pos)
  1005. (insert "\n")
  1006. (indent-line-to (+ nxml-child-indent indent)))
  1007. (goto-char pos)))))
  1008. (defun nxml-finish-element ()
  1009. "Finish the current element by inserting an end-tag."
  1010. (interactive "*")
  1011. (nxml-finish-element-1 nil))
  1012. (defvar nxml-last-split-position nil
  1013. "Position where `nxml-split-element' split the current element.")
  1014. (defun nxml-split-element ()
  1015. "Split the current element by inserting an end-tag and a start-tag.
  1016. Point is left after the newly inserted start-tag. When repeated,
  1017. split immediately before the previously inserted start-tag and leave
  1018. point unchanged."
  1019. (interactive "*")
  1020. (setq nxml-last-split-position
  1021. (if (and (eq last-command this-command)
  1022. nxml-last-split-position)
  1023. (save-excursion
  1024. (goto-char nxml-last-split-position)
  1025. (nxml-finish-element-1 t))
  1026. (nxml-finish-element-1 t))))
  1027. (defun nxml-finish-element-1 (startp)
  1028. "Insert an end-tag for the current element and optionally a start-tag.
  1029. The start-tag is inserted if STARTP is non-nil. Return the position
  1030. of the inserted start-tag or nil if none was inserted."
  1031. (interactive "*")
  1032. (let* ((token-end (nxml-token-before))
  1033. (start-tag-end
  1034. (save-excursion
  1035. (when (and (< (point) token-end)
  1036. (memq xmltok-type
  1037. '(cdata-section
  1038. processing-instruction
  1039. comment
  1040. start-tag
  1041. end-tag
  1042. empty-element)))
  1043. (error "Point is inside a %s"
  1044. (nxml-token-type-friendly-name xmltok-type)))
  1045. (nxml-scan-element-backward token-end t)))
  1046. (starts-line
  1047. (save-excursion
  1048. (unless (eq xmltok-type 'start-tag)
  1049. (error "No matching start-tag"))
  1050. (goto-char xmltok-start)
  1051. (back-to-indentation)
  1052. (eq (point) xmltok-start)))
  1053. (ends-line
  1054. (save-excursion
  1055. (goto-char start-tag-end)
  1056. (looking-at "[ \t\r\n]*$")))
  1057. (start-tag-indent (save-excursion
  1058. (goto-char xmltok-start)
  1059. (current-column)))
  1060. (qname (xmltok-start-tag-qname))
  1061. inserted-start-tag-pos)
  1062. (when (and starts-line ends-line)
  1063. ;; start-tag is on a line by itself
  1064. ;; => put the end-tag on a line by itself
  1065. (unless (<= (point)
  1066. (save-excursion
  1067. (back-to-indentation)
  1068. (point)))
  1069. (insert "\n"))
  1070. (indent-line-to start-tag-indent))
  1071. (insert "</" qname ">")
  1072. (when startp
  1073. (when starts-line
  1074. (insert "\n")
  1075. (indent-line-to start-tag-indent))
  1076. (setq inserted-start-tag-pos (point))
  1077. (insert "<" qname ">")
  1078. (when (and starts-line ends-line)
  1079. (insert "\n")
  1080. (indent-line-to (save-excursion
  1081. (goto-char xmltok-start)
  1082. (forward-line 1)
  1083. (back-to-indentation)
  1084. (if (= (current-column)
  1085. (+ start-tag-indent nxml-child-indent))
  1086. (+ start-tag-indent nxml-child-indent)
  1087. start-tag-indent)))))
  1088. inserted-start-tag-pos))
  1089. (defun nxml-comment-quote-nested (_cs _ce unp)
  1090. "Quote nested comments in buffer.
  1091. See `comment-quote-nested-function' for more information."
  1092. (goto-char (point-min))
  1093. (save-match-data
  1094. (while (re-search-forward "-[\\]*-" nil t)
  1095. (goto-char (match-beginning 0))
  1096. (forward-char 1)
  1097. (if unp
  1098. (delete-char 1)
  1099. (insert "\\")))))
  1100. ;;; Indentation
  1101. (defun nxml-indent-line ()
  1102. "Indent current line as XML."
  1103. (let* ((savep (point))
  1104. (indent (condition-case nil
  1105. (save-excursion
  1106. (forward-line 0)
  1107. (skip-chars-forward " \t")
  1108. (if (>= (point) savep) (setq savep nil))
  1109. (or (nxml-compute-indent) 0))
  1110. (error 0))))
  1111. (if (not (numberp indent))
  1112. ;; If something funny is used (e.g. `noindent'), return it.
  1113. indent
  1114. (if (< indent 0) (setq indent 0)) ;Just in case.
  1115. (if savep
  1116. (save-excursion (indent-line-to indent))
  1117. (indent-line-to indent)))))
  1118. (defun nxml-compute-indent ()
  1119. "Return the indent for the line containing point."
  1120. (or (nxml-compute-indent-from-matching-start-tag)
  1121. (nxml-compute-indent-from-previous-line)))
  1122. (defun nxml-compute-indent-from-matching-start-tag ()
  1123. "Compute the indent for a line with an end-tag using the matching start-tag.
  1124. When the line containing point ends with an end-tag and does not start
  1125. in the middle of a token, return the indent of the line containing the
  1126. matching start-tag, if there is one and it occurs at the beginning of
  1127. its line. Otherwise return nil."
  1128. (save-excursion
  1129. (back-to-indentation)
  1130. (let ((bol (point)))
  1131. (let ((inhibit-field-text-motion t))
  1132. (end-of-line))
  1133. (skip-chars-backward " \t")
  1134. (and (= (nxml-token-before) (point))
  1135. (memq xmltok-type '(end-tag partial-end-tag))
  1136. ;; start of line must not be inside a token
  1137. (or (= xmltok-start bol)
  1138. (save-excursion
  1139. (goto-char bol)
  1140. (nxml-token-after)
  1141. (= xmltok-start bol))
  1142. (eq xmltok-type 'data))
  1143. (condition-case nil
  1144. (nxml-scan-element-backward
  1145. (point)
  1146. nil
  1147. (- (point)
  1148. nxml-end-tag-indent-scan-distance))
  1149. (nxml-scan-error nil))
  1150. (< xmltok-start bol)
  1151. (progn
  1152. (goto-char xmltok-start)
  1153. (skip-chars-backward " \t")
  1154. (bolp))
  1155. (current-indentation)))))
  1156. (defun nxml-compute-indent-from-previous-line ()
  1157. "Compute the indent for a line using the indentation of a previous line."
  1158. (save-excursion
  1159. (end-of-line)
  1160. (let ((eol (point))
  1161. bol prev-bol ref
  1162. before-context after-context)
  1163. (back-to-indentation)
  1164. (setq bol (point))
  1165. (catch 'indent
  1166. ;; Move backwards until the start of a non-blank line that is
  1167. ;; not inside a token.
  1168. (while (progn
  1169. (when (= (forward-line -1) -1)
  1170. (throw 'indent 0))
  1171. (back-to-indentation)
  1172. (if (looking-at "[ \t]*$")
  1173. t
  1174. (or prev-bol
  1175. (setq prev-bol (point)))
  1176. (nxml-token-after)
  1177. (not (or (= xmltok-start (point))
  1178. (eq xmltok-type 'data))))))
  1179. (setq ref (point))
  1180. ;; Now scan over tokens until the end of the line to be indented.
  1181. ;; Determine the context before and after the beginning of the
  1182. ;; line.
  1183. (while (< (point) eol)
  1184. (nxml-tokenize-forward)
  1185. (cond ((<= bol xmltok-start)
  1186. (setq after-context
  1187. (nxml-merge-indent-context-type after-context)))
  1188. ((and (<= (point) bol)
  1189. (not (and (eq xmltok-type 'partial-start-tag)
  1190. (= (point) bol))))
  1191. (setq before-context
  1192. (nxml-merge-indent-context-type before-context)))
  1193. ((eq xmltok-type 'data)
  1194. (setq before-context
  1195. (nxml-merge-indent-context-type before-context))
  1196. (setq after-context
  1197. (nxml-merge-indent-context-type after-context)))
  1198. ;; If in the middle of a token that looks inline,
  1199. ;; then indent relative to the previous non-blank line
  1200. ((eq (nxml-merge-indent-context-type before-context)
  1201. 'mixed)
  1202. (goto-char prev-bol)
  1203. (throw 'indent (current-column)))
  1204. (t
  1205. (throw 'indent
  1206. (nxml-compute-indent-in-token bol))))
  1207. (skip-chars-forward " \t\r\n"))
  1208. (goto-char ref)
  1209. (+ (current-column)
  1210. (* nxml-child-indent
  1211. (+ (if (eq before-context 'start-tag) 1 0)
  1212. (if (eq after-context 'end-tag) -1 0))))))))
  1213. (defun nxml-merge-indent-context-type (context)
  1214. "Merge the indent context type CONTEXT with the token in `xmltok-type'.
  1215. Return the merged indent context type. An indent context type is
  1216. either nil or one of the symbols `start-tag', `end-tag', `markup',
  1217. `comment', `mixed'."
  1218. (cond ((memq xmltok-type '(start-tag partial-start-tag))
  1219. (if (memq context '(nil start-tag comment))
  1220. 'start-tag
  1221. 'mixed))
  1222. ((memq xmltok-type '(end-tag partial-end-tag))
  1223. (if (memq context '(nil end-tag comment))
  1224. 'end-tag
  1225. 'mixed))
  1226. ((eq xmltok-type 'comment)
  1227. (cond ((memq context '(start-tag end-tag comment))
  1228. context)
  1229. (context 'mixed)
  1230. (t 'comment)))
  1231. (context 'mixed)
  1232. (t 'markup)))
  1233. (defun nxml-compute-indent-in-token (pos)
  1234. "Return the indent for a line that starts inside a token.
  1235. POS is the position of the first non-whitespace character of the line.
  1236. This expects the xmltok-* variables to be set up as by `xmltok-forward'."
  1237. (cond ((memq xmltok-type '(start-tag
  1238. partial-start-tag
  1239. empty-element
  1240. partial-empty-element))
  1241. (nxml-compute-indent-in-start-tag pos))
  1242. ((eq xmltok-type 'comment)
  1243. (nxml-compute-indent-in-delimited-token pos "<!--" "-->"))
  1244. ((eq xmltok-type 'cdata-section)
  1245. (nxml-compute-indent-in-delimited-token pos "<![CDATA[" "]]>"))
  1246. ((eq xmltok-type 'processing-instruction)
  1247. (nxml-compute-indent-in-delimited-token pos "<?" "?>"))
  1248. (t
  1249. (goto-char pos)
  1250. (if (and (= (forward-line -1) 0)
  1251. (< xmltok-start (point)))
  1252. (back-to-indentation)
  1253. (goto-char xmltok-start))
  1254. (current-column))))
  1255. (defun nxml-compute-indent-in-start-tag (pos)
  1256. "Return the indent for a line that starts inside a start-tag.
  1257. Also for a line that starts inside an empty element.
  1258. POS is the position of the first non-whitespace character of the line.
  1259. This expects the xmltok-* variables to be set up as by `xmltok-forward'."
  1260. (let ((value-boundary (nxml-attribute-value-boundary pos))
  1261. (off 0))
  1262. (if value-boundary
  1263. ;; inside an attribute value
  1264. (let ((value-start (car value-boundary)))
  1265. (goto-char pos)
  1266. (forward-line -1)
  1267. (if (< (point) value-start)
  1268. (goto-char value-start)
  1269. (back-to-indentation)))
  1270. ;; outside an attribute value
  1271. (goto-char pos)
  1272. (while (and (= (forward-line -1) 0)
  1273. (nxml-attribute-value-boundary (point))))
  1274. (cond ((<= (point) xmltok-start)
  1275. (goto-char xmltok-start)
  1276. (setq off nxml-attribute-indent)
  1277. (let ((atts (xmltok-merge-attributes)))
  1278. (when atts
  1279. (let* ((att (car atts))
  1280. (start (xmltok-attribute-name-start att)))
  1281. (when (< start pos)
  1282. (goto-char start)
  1283. (setq off 0))))))
  1284. (t
  1285. (back-to-indentation))))
  1286. (+ (current-column) off)))
  1287. (defun nxml-attribute-value-boundary (pos)
  1288. "Return a pair (START . END) if POS is inside an attribute value.
  1289. Otherwise return nil. START and END are the positions of the start
  1290. and end of the attribute value containing POS. This expects the
  1291. xmltok-* variables to be set up as by `xmltok-forward'."
  1292. (let ((atts (xmltok-merge-attributes))
  1293. att value-start value-end value-boundary)
  1294. (while atts
  1295. (setq att (car atts))
  1296. (setq value-start (xmltok-attribute-value-start att))
  1297. (setq value-end (xmltok-attribute-value-end att))
  1298. (cond ((and value-start (< pos value-start))
  1299. (setq atts nil))
  1300. ((and value-start value-end (<= pos value-end))
  1301. (setq value-boundary (cons value-start value-end))
  1302. (setq atts nil))
  1303. (t (setq atts (cdr atts)))))
  1304. value-boundary))
  1305. (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim)
  1306. "Return the indent for a line that starts inside a token with delimiters.
  1307. OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
  1308. delimiters. POS is the position of the first non-whitespace character
  1309. of the line. This expects the xmltok-* variables to be set up as by
  1310. `xmltok-forward'."
  1311. (cond ((let ((end (+ pos (length close-delim))))
  1312. (and (<= end (point-max))
  1313. (string= (buffer-substring-no-properties pos end)
  1314. close-delim)))
  1315. (goto-char xmltok-start))
  1316. ((progn
  1317. (goto-char pos)
  1318. (forward-line -1)
  1319. (<= (point) xmltok-start))
  1320. (goto-char (+ xmltok-start (length open-delim)))
  1321. (when (and (string= open-delim "<!--")
  1322. (looking-at " "))
  1323. (goto-char (1+ (point)))))
  1324. (t (back-to-indentation)))
  1325. (current-column))
  1326. (define-obsolete-function-alias 'nxml-complete #'completion-at-point "26.1")
  1327. ;;; Movement
  1328. (defun nxml-forward-balanced-item (&optional arg)
  1329. "Move forward across one balanced item.
  1330. With ARG, do it that many times. Negative arg -N means
  1331. move backward across N balanced expressions.
  1332. This is the equivalent of `forward-sexp' for XML.
  1333. An element contains as items strings with no markup, tags, processing
  1334. instructions, comments, CDATA sections, entity references and
  1335. characters references. However, if the variable
  1336. `nxml-sexp-element-flag' is non-nil, then an element is treated as a
  1337. single markup item. A start-tag contains an element name followed by
  1338. one or more attributes. An end-tag contains just an element name.
  1339. An attribute value literals contains strings with no markup, entity
  1340. references and character references. A processing instruction
  1341. consists of a target and a content string. A comment or a CDATA
  1342. section contains a single string. An entity reference contains a
  1343. single name. A character reference contains a character number."
  1344. (interactive "^p")
  1345. (or arg (setq arg 1))
  1346. (cond ((> arg 0)
  1347. (while (progn
  1348. (nxml-forward-single-balanced-item)
  1349. (> (setq arg (1- arg)) 0))))
  1350. ((< arg 0)
  1351. (while (progn
  1352. (nxml-backward-single-balanced-item)
  1353. (< (setq arg (1+ arg)) 0))))))
  1354. (defun nxml-forward-single-balanced-item ()
  1355. (condition-case err
  1356. (goto-char (let ((end (nxml-token-after)))
  1357. (save-excursion
  1358. (while (eq xmltok-type 'space)
  1359. (goto-char end)
  1360. (setq end (nxml-token-after)))
  1361. (cond ((/= (point) xmltok-start)
  1362. (nxml-scan-forward-within end))
  1363. ((and nxml-sexp-element-flag
  1364. (eq xmltok-type 'start-tag))
  1365. ;; can't ever return nil here
  1366. (nxml-scan-element-forward xmltok-start))
  1367. ((and nxml-sexp-element-flag
  1368. (memq xmltok-type
  1369. '(end-tag partial-end-tag)))
  1370. (error "Already at end of element"))
  1371. (t end)))))
  1372. (nxml-scan-error
  1373. (goto-char (cadr err))
  1374. (apply #'error (cddr err)))))
  1375. (defun nxml-backward-single-balanced-item ()
  1376. (condition-case err
  1377. (goto-char (let ((end (nxml-token-before)))
  1378. (save-excursion
  1379. (while (eq xmltok-type 'space)
  1380. (goto-char xmltok-start)
  1381. (setq end (nxml-token-before)))
  1382. (cond ((/= (point) end)
  1383. (nxml-scan-backward-within end))
  1384. ((and nxml-sexp-element-flag
  1385. (eq xmltok-type 'end-tag))
  1386. ;; can't ever return nil here
  1387. (nxml-scan-element-backward end)
  1388. xmltok-start)
  1389. ((and nxml-sexp-element-flag
  1390. (eq xmltok-type 'start-tag))
  1391. (error "Already at start of element"))
  1392. (t xmltok-start)))))
  1393. (nxml-scan-error
  1394. (goto-char (cadr err))
  1395. (apply #'error (cddr err)))))
  1396. (defun nxml-scan-forward-within (end)
  1397. (setq end (- end (nxml-end-delimiter-length xmltok-type)))
  1398. (when (<= end (point))
  1399. (error "Already at end of %s"
  1400. (nxml-token-type-friendly-name xmltok-type)))
  1401. (cond ((memq xmltok-type '(start-tag
  1402. empty-element
  1403. partial-start-tag
  1404. partial-empty-element))
  1405. (if (< (point) xmltok-name-end)
  1406. xmltok-name-end
  1407. (let ((att (nxml-find-following-attribute)))
  1408. (cond ((not att) end)
  1409. ((and (xmltok-attribute-value-start att)
  1410. (<= (xmltok-attribute-value-start att)
  1411. (point)))
  1412. (nxml-scan-forward-in-attribute-value att))
  1413. ((xmltok-attribute-value-end att)
  1414. (1+ (xmltok-attribute-value-end att)))
  1415. ((save-excursion
  1416. (goto-char (xmltok-attribute-name-end att))
  1417. (looking-at "[ \t\r\n]*="))
  1418. (match-end 0))
  1419. (t (xmltok-attribute-name-end att))))))
  1420. ((and (eq xmltok-type 'processing-instruction)
  1421. (< (point) xmltok-name-end))
  1422. xmltok-name-end)
  1423. (t end)))
  1424. (defun nxml-scan-backward-within (_end)
  1425. (setq xmltok-start
  1426. (+ xmltok-start
  1427. (nxml-start-delimiter-length xmltok-type)))
  1428. (when (<= (point) xmltok-start)
  1429. (error "Already at start of %s"
  1430. (nxml-token-type-friendly-name xmltok-type)))
  1431. (cond ((memq xmltok-type '(start-tag
  1432. empty-element
  1433. partial-start-tag
  1434. partial-empty-element))
  1435. (let ((att (nxml-find-preceding-attribute)))
  1436. (cond ((not att) xmltok-start)
  1437. ((and (xmltok-attribute-value-start att)
  1438. (<= (xmltok-attribute-value-start att)
  1439. (point))
  1440. (<= (point)
  1441. (xmltok-attribute-value-end att)))
  1442. (nxml-scan-backward-in-attribute-value att))
  1443. (t (xmltok-attribute-name-start att)))))
  1444. ((and (eq xmltok-type 'processing-instruction)
  1445. (let ((content-start (save-excursion
  1446. (goto-char xmltok-name-end)
  1447. (skip-chars-forward " \r\t\n")
  1448. (point))))
  1449. (and (< content-start (point))
  1450. content-start))))
  1451. (t xmltok-start)))
  1452. (defun nxml-scan-forward-in-attribute-value (att)
  1453. (when (= (point) (xmltok-attribute-value-end att))
  1454. (error "Already at end of attribute value"))
  1455. (let ((refs (xmltok-attribute-refs att))
  1456. ref)
  1457. (while refs
  1458. (setq ref (car refs))
  1459. (if (< (point) (aref ref 2))
  1460. (setq refs nil)
  1461. (setq ref nil)
  1462. (setq refs (cdr refs))))
  1463. (cond ((not ref)
  1464. (xmltok-attribute-value-end att))
  1465. ((< (point) (aref ref 1))
  1466. (aref ref 1))
  1467. ((= (point) (aref ref 1))
  1468. (aref ref 2))
  1469. (t
  1470. (let ((end (- (aref ref 2)
  1471. (nxml-end-delimiter-length (aref ref 0)))))
  1472. (if (< (point) end)
  1473. end
  1474. (error "Already at end of %s"
  1475. (nxml-token-type-friendly-name (aref ref 0)))))))))
  1476. (defun nxml-scan-backward-in-attribute-value (att)
  1477. (when (= (point) (xmltok-attribute-value-start att))
  1478. (error "Already at start of attribute value"))
  1479. (let ((refs (reverse (xmltok-attribute-refs att)))
  1480. ref)
  1481. (while refs
  1482. (setq ref (car refs))
  1483. (if (< (aref ref 1) (point))
  1484. (setq refs nil)
  1485. (setq ref nil)
  1486. (setq refs (cdr refs))))
  1487. (cond ((not ref)
  1488. (xmltok-attribute-value-start att))
  1489. ((< (aref ref 2) (point))
  1490. (aref ref 2))
  1491. ((= (point) (aref ref 2))
  1492. (aref ref 1))
  1493. (t
  1494. (let ((start (+ (aref ref 1)
  1495. (nxml-start-delimiter-length (aref ref 0)))))
  1496. (if (< start (point))
  1497. start
  1498. (error "Already at start of %s"
  1499. (nxml-token-type-friendly-name (aref ref 0)))))))))
  1500. (defun nxml-find-following-attribute ()
  1501. (let ((ret nil)
  1502. (atts (or xmltok-attributes xmltok-namespace-attributes))
  1503. (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
  1504. (while atts
  1505. (let* ((att (car atts))
  1506. (name-start (xmltok-attribute-name-start att)))
  1507. (cond ((and (<= name-start (point))
  1508. (xmltok-attribute-value-end att)
  1509. ;; <= because end is before quote
  1510. (<= (point) (xmltok-attribute-value-end att)))
  1511. (setq atts nil)
  1512. (setq ret att))
  1513. ((and (< (point) name-start)
  1514. (or (not ret)
  1515. (< name-start
  1516. (xmltok-attribute-name-start ret))))
  1517. (setq ret att))))
  1518. (setq atts (cdr atts))
  1519. (unless atts
  1520. (setq atts more-atts)
  1521. (setq more-atts nil)))
  1522. ret))
  1523. (defun nxml-find-preceding-attribute ()
  1524. (let ((ret nil)
  1525. (atts (or xmltok-attributes xmltok-namespace-attributes))
  1526. (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
  1527. (while atts
  1528. (let* ((att (car atts))
  1529. (name-start (xmltok-attribute-name-start att)))
  1530. (cond ((and (< name-start (point))
  1531. (xmltok-attribute-value-end att)
  1532. ;; <= because end is before quote
  1533. (<= (point) (xmltok-attribute-value-end att)))
  1534. (setq atts nil)
  1535. (setq ret att))
  1536. ((and (< name-start (point))
  1537. (or (not ret)
  1538. (< (xmltok-attribute-name-start ret)
  1539. name-start)))
  1540. (setq ret att))))
  1541. (setq atts (cdr atts))
  1542. (unless atts
  1543. (setq atts more-atts)
  1544. (setq more-atts nil)))
  1545. ret))
  1546. (defun nxml-up-element (&optional arg)
  1547. (interactive "^p")
  1548. (or arg (setq arg 1))
  1549. (if (< arg 0)
  1550. (nxml-backward-up-element (- arg))
  1551. (condition-case err
  1552. (while (and (> arg 0)
  1553. (< (point) (point-max)))
  1554. (let ((token-end (nxml-token-after)))
  1555. (goto-char (cond ((or (memq xmltok-type '(end-tag
  1556. partial-end-tag))
  1557. (and (memq xmltok-type
  1558. '(empty-element
  1559. partial-empty-element))
  1560. (< xmltok-start (point))))
  1561. token-end)
  1562. ((nxml-scan-element-forward
  1563. (if (and (eq xmltok-type 'start-tag)
  1564. (= (point) xmltok-start))
  1565. xmltok-start
  1566. token-end)
  1567. t))
  1568. (t (error "No parent element")))))
  1569. (setq arg (1- arg)))
  1570. (nxml-scan-error
  1571. (goto-char (cadr err))
  1572. (apply #'error (cddr err))))))
  1573. (defun nxml-backward-up-element (&optional arg)
  1574. (interactive "^p")
  1575. (or arg (setq arg 1))
  1576. (if (< arg 0)
  1577. (nxml-up-element (- arg))
  1578. (condition-case err
  1579. (while (and (> arg 0)
  1580. (< (point-min) (point)))
  1581. (let ((token-end (nxml-token-before)))
  1582. (goto-char (cond ((or (memq xmltok-type '(start-tag
  1583. partial-start-tag))
  1584. (and (memq xmltok-type
  1585. '(empty-element
  1586. partial-empty-element))
  1587. (< (point) token-end)))
  1588. xmltok-start)
  1589. ((nxml-scan-element-backward
  1590. (if (and (eq xmltok-type 'end-tag)
  1591. (= (point) token-end))
  1592. token-end
  1593. xmltok-start)
  1594. t)
  1595. xmltok-start)
  1596. (t (error "No parent element")))))
  1597. (setq arg (1- arg)))
  1598. (nxml-scan-error
  1599. (goto-char (cadr err))
  1600. (apply #'error (cddr err))))))
  1601. (defun nxml-down-element (&optional arg)
  1602. "Move forward down into the content of an element.
  1603. With ARG, do this that many times.
  1604. Negative ARG means move backward but still down."
  1605. (interactive "^p")
  1606. (or arg (setq arg 1))
  1607. (if (< arg 0)
  1608. (nxml-backward-down-element (- arg))
  1609. (while (> arg 0)
  1610. (goto-char
  1611. (let ((token-end (nxml-token-after)))
  1612. (save-excursion
  1613. (goto-char token-end)
  1614. (while (progn
  1615. (when (memq xmltok-type '(nil end-tag partial-end-tag))
  1616. (error "No following start-tags in this element"))
  1617. (not (memq xmltok-type '(start-tag partial-start-tag))))
  1618. (nxml-tokenize-forward))
  1619. (point))))
  1620. (setq arg (1- arg)))))
  1621. (defun nxml-backward-down-element (&optional arg)
  1622. (interactive "^p")
  1623. (or arg (setq arg 1))
  1624. (if (< arg 0)
  1625. (nxml-down-element (- arg))
  1626. (while (> arg 0)
  1627. (goto-char
  1628. (save-excursion
  1629. (nxml-token-before)
  1630. (goto-char xmltok-start)
  1631. (while (progn
  1632. (when (memq xmltok-type '(start-tag
  1633. partial-start-tag
  1634. prolog
  1635. nil))
  1636. (error "No preceding end-tags in this element"))
  1637. (not (memq xmltok-type '(end-tag partial-end-tag))))
  1638. (if (or (<= (point) nxml-prolog-end)
  1639. (not (search-backward "<" nxml-prolog-end t)))
  1640. (setq xmltok-type nil)
  1641. (nxml-move-outside-backwards)
  1642. (xmltok-forward)))
  1643. xmltok-start))
  1644. (setq arg (1- arg)))))
  1645. (defun nxml-forward-element (&optional arg)
  1646. "Move forward over one element.
  1647. With ARG, do it that many times.
  1648. Negative ARG means move backward."
  1649. (interactive "^p")
  1650. (or arg (setq arg 1))
  1651. (if (< arg 0)
  1652. (nxml-backward-element (- arg))
  1653. (condition-case err
  1654. (while (and (> arg 0)
  1655. (< (point) (point-max)))
  1656. (goto-char
  1657. (or (nxml-scan-element-forward (nxml-token-before))
  1658. (error "No more elements")))
  1659. (setq arg (1- arg)))
  1660. (nxml-scan-error
  1661. (goto-char (cadr err))
  1662. (apply #'error (cddr err))))))
  1663. (defun nxml-backward-element (&optional arg)
  1664. "Move backward over one element.
  1665. With ARG, do it that many times.
  1666. Negative ARG means move forward."
  1667. (interactive "^p")
  1668. (or arg (setq arg 1))
  1669. (if (< arg 0)
  1670. (nxml-forward-element (- arg))
  1671. (condition-case err
  1672. (while (and (> arg 0)
  1673. (< (point-min) (point)))
  1674. (goto-char
  1675. (or (and (nxml-scan-element-backward (progn
  1676. (nxml-token-after)
  1677. xmltok-start))
  1678. xmltok-start)
  1679. (error "No preceding elements")))
  1680. (setq arg (1- arg)))
  1681. (nxml-scan-error
  1682. (goto-char (cadr err))
  1683. (apply #'error (cddr err))))))
  1684. (defun nxml-mark-token-after ()
  1685. (interactive)
  1686. (push-mark (nxml-token-after) nil t)
  1687. (goto-char xmltok-start)
  1688. (message "Marked %s" xmltok-type))
  1689. ;;; Paragraphs
  1690. (defun nxml-mark-paragraph ()
  1691. "Put point at beginning of this paragraph, mark at end.
  1692. The paragraph marked is the one that contains point or follows point."
  1693. (interactive)
  1694. (nxml-forward-paragraph)
  1695. (push-mark nil t t)
  1696. (nxml-backward-paragraph))
  1697. (defun nxml-forward-paragraph (&optional arg)
  1698. (interactive "^p")
  1699. (or arg (setq arg 1))
  1700. (cond ((< arg 0)
  1701. (nxml-backward-paragraph (- arg)))
  1702. ((> arg 0)
  1703. (forward-line 0)
  1704. (while (and (nxml-forward-single-paragraph)
  1705. (> (setq arg (1- arg)) 0))))))
  1706. (defun nxml-backward-paragraph (&optional arg)
  1707. (interactive "^p")
  1708. (or arg (setq arg 1))
  1709. (cond ((< arg 0)
  1710. (nxml-forward-paragraph (- arg)))
  1711. ((> arg 0)
  1712. (unless (bolp)
  1713. (let ((inhibit-field-text-motion t))
  1714. (end-of-line)))
  1715. (while (and (nxml-backward-single-paragraph)
  1716. (> (setq arg (1- arg)) 0))))))
  1717. (defun nxml-forward-single-paragraph ()
  1718. "Move forward over a single paragraph.
  1719. Return nil at end of buffer, t otherwise."
  1720. (let* ((token-end (nxml-token-after))
  1721. (offset (- (point) xmltok-start))
  1722. pos had-data)
  1723. (goto-char token-end)
  1724. (while (and (< (point) (point-max))
  1725. (not (setq pos
  1726. (nxml-paragraph-end-pos had-data offset))))
  1727. (when (nxml-token-contains-data-p offset)
  1728. (setq had-data t))
  1729. (nxml-tokenize-forward)
  1730. (setq offset 0))
  1731. (when pos (goto-char pos))))
  1732. (defun nxml-backward-single-paragraph ()
  1733. "Move backward over a single paragraph.
  1734. Return nil at start of buffer, t otherwise."
  1735. (let* ((token-end (nxml-token-before))
  1736. (offset (- token-end (point)))
  1737. (last-tag-pos xmltok-start)
  1738. pos had-data last-data-pos)
  1739. (goto-char token-end)
  1740. (unless (setq pos (nxml-paragraph-start-pos nil offset))
  1741. (setq had-data (nxml-token-contains-data-p nil offset))
  1742. (goto-char xmltok-start)
  1743. (while (and (not pos) (< (point-min) (point)))
  1744. (cond ((search-backward "<" nxml-prolog-end t)
  1745. (nxml-move-outside-backwards)
  1746. (save-excursion
  1747. (while (< (point) last-tag-pos)
  1748. (xmltok-forward)
  1749. (when (and (not had-data) (nxml-token-contains-data-p))
  1750. (setq pos nil)
  1751. (setq last-data-pos xmltok-start))
  1752. (let ((tem (nxml-paragraph-start-pos had-data 0)))
  1753. (when tem (setq pos tem)))))
  1754. (when (and (not had-data) last-data-pos (not pos))
  1755. (setq had-data t)
  1756. (save-excursion
  1757. (while (< (point) last-data-pos)
  1758. (xmltok-forward))
  1759. (let ((tem (nxml-paragraph-start-pos had-data 0)))
  1760. (when tem (setq pos tem)))))
  1761. (setq last-tag-pos (point)))
  1762. (t (goto-char (point-min))))))
  1763. (when pos (goto-char pos))))
  1764. (defun nxml-token-contains-data-p (&optional start end)
  1765. (setq start (+ xmltok-start (or start 0)))
  1766. (setq end (- (point) (or end 0)))
  1767. (when (eq xmltok-type 'cdata-section)
  1768. (setq start (max start (+ xmltok-start 9)))
  1769. (setq end (min end (- (point) 3))))
  1770. (or (and (eq xmltok-type 'data)
  1771. (eq start xmltok-start)
  1772. (eq end (point)))
  1773. (eq xmltok-type 'char-ref)
  1774. (and (memq xmltok-type '(data cdata-section))
  1775. (< start end)
  1776. (save-excursion
  1777. (goto-char start)
  1778. (re-search-forward "[^ \t\r\n]" end t)))))
  1779. (defun nxml-paragraph-end-pos (had-data offset)
  1780. "Return the position of the paragraph end if contained in the current token.
  1781. Return nil if the current token does not contain the paragraph end.
  1782. Only characters after OFFSET from the start of the token are eligible.
  1783. HAD-DATA says whether there have been non-whitespace data characters yet."
  1784. (cond ((not had-data)
  1785. (cond ((memq xmltok-type '(data cdata-section))
  1786. (save-excursion
  1787. (let ((end (point)))
  1788. (goto-char (+ xmltok-start
  1789. (max (if (eq xmltok-type 'cdata-section)
  1790. 9
  1791. 0)
  1792. offset)))
  1793. (and (re-search-forward "[^ \t\r\n]" end t)
  1794. (re-search-forward "^[ \t]*$" end t)
  1795. (match-beginning 0)))))
  1796. ((and (eq xmltok-type 'comment)
  1797. (nxml-token-begins-line-p)
  1798. (nxml-token-ends-line-p))
  1799. (save-excursion
  1800. (let ((end (point)))
  1801. (goto-char (+ xmltok-start (max 4 offset)))
  1802. (when (re-search-forward "[^ \t\r\n]" (- end 3) t)
  1803. (if (re-search-forward "^[ \t]*$" end t)
  1804. (match-beginning 0)
  1805. (goto-char (- end 3))
  1806. (skip-chars-backward " \t")
  1807. (unless (bolp)
  1808. (beginning-of-line 2))
  1809. (point))))))))
  1810. ((memq xmltok-type '(data space cdata-section))
  1811. (save-excursion
  1812. (let ((end (point)))
  1813. (goto-char (+ xmltok-start offset))
  1814. (and (re-search-forward "^[ \t]*$" end t)
  1815. (match-beginning 0)))))
  1816. ((and (memq xmltok-type '(start-tag
  1817. end-tag
  1818. empty-element
  1819. comment
  1820. processing-instruction
  1821. entity-ref))
  1822. (nxml-token-begins-line-p)
  1823. (nxml-token-ends-line-p))
  1824. (save-excursion
  1825. (goto-char xmltok-start)
  1826. (skip-chars-backward " \t")
  1827. (point)))
  1828. ((and (eq xmltok-type 'end-tag)
  1829. (looking-at "[ \t]*$")
  1830. (not (nxml-in-mixed-content-p t)))
  1831. (save-excursion
  1832. (or (search-forward "\n" nil t)
  1833. (point-max))))))
  1834. (defun nxml-paragraph-start-pos (had-data offset)
  1835. "Return the position of the paragraph start if contained in the current token.
  1836. Return nil if the current token does not contain the paragraph start.
  1837. Only characters before OFFSET from the end of the token are eligible.
  1838. HAD-DATA says whether there have been non-whitespace data characters yet."
  1839. (cond ((not had-data)
  1840. (cond ((memq xmltok-type '(data cdata-section))
  1841. (save-excursion
  1842. (goto-char (- (point)
  1843. (max (if (eq xmltok-type 'cdata-section)
  1844. 3
  1845. 0)
  1846. offset)))
  1847. (and (re-search-backward "[^ \t\r\n]" xmltok-start t)
  1848. (re-search-backward "^[ \t]*$" xmltok-start t)
  1849. (match-beginning 0))))
  1850. ((and (eq xmltok-type 'comment)
  1851. (nxml-token-ends-line-p)
  1852. (nxml-token-begins-line-p))
  1853. (save-excursion
  1854. (goto-char (- (point) (max 3 offset)))
  1855. (when (and (< (+ xmltok-start 4) (point))
  1856. (re-search-backward "[^ \t\r\n]"
  1857. (+ xmltok-start 4)
  1858. t))
  1859. (if (re-search-backward "^[ \t]*$" xmltok-start t)
  1860. (match-beginning 0)
  1861. (goto-char xmltok-start)
  1862. (if (looking-at "<!--[ \t]*\n")
  1863. (match-end 0)
  1864. (skip-chars-backward " \t")
  1865. (point))))))))
  1866. ((memq xmltok-type '(data space cdata-section))
  1867. (save-excursion
  1868. (goto-char (- (point) offset))
  1869. (and (re-search-backward "^[ \t]*$" xmltok-start t)
  1870. (match-beginning 0))))
  1871. ((and (memq xmltok-type '(start-tag
  1872. end-tag
  1873. empty-element
  1874. comment
  1875. processing-instruction
  1876. entity-ref))
  1877. (nxml-token-ends-line-p)
  1878. (nxml-token-begins-line-p))
  1879. (or (search-forward "\n" nil t)
  1880. (point-max)))
  1881. ((and (eq xmltok-type 'start-tag)
  1882. (nxml-token-begins-line-p)
  1883. (not (save-excursion
  1884. (goto-char xmltok-start)
  1885. (nxml-in-mixed-content-p nil))))
  1886. (save-excursion
  1887. (goto-char xmltok-start)
  1888. (skip-chars-backward " \t")
  1889. ;; include any blank line before
  1890. (or (and (eq (char-before) ?\n)
  1891. (save-excursion
  1892. (goto-char (1- (point)))
  1893. (skip-chars-backward " \t")
  1894. (and (bolp) (point))))
  1895. (point))))))
  1896. (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
  1897. (defun nxml-token-begins-line-p ()
  1898. (save-excursion
  1899. (goto-char xmltok-start)
  1900. (skip-chars-backward " \t")
  1901. (bolp)))
  1902. (defun nxml-in-mixed-content-p (endp)
  1903. "Return non-nil if point is in mixed content.
  1904. Point must be after an end-tag or before a start-tag.
  1905. ENDP is t in the former case, nil in the latter."
  1906. (let (matching-tag-pos)
  1907. (cond ((not (run-hook-with-args-until-failure
  1908. 'nxml-in-mixed-content-hook))
  1909. nil)
  1910. ;; See if the matching tag does not start or end a line.
  1911. ((condition-case nil
  1912. (progn
  1913. (setq matching-tag-pos
  1914. (xmltok-save
  1915. (if endp
  1916. (and (nxml-scan-element-backward (point))
  1917. xmltok-start)
  1918. (nxml-scan-element-forward (point)))))
  1919. (and matching-tag-pos
  1920. (save-excursion
  1921. (goto-char matching-tag-pos)
  1922. (not (if endp
  1923. (progn
  1924. (skip-chars-backward " \t")
  1925. (bolp))
  1926. (looking-at "[ \t]*$"))))))
  1927. (nxml-scan-error nil))
  1928. t)
  1929. ;; See if there's data at the same level.
  1930. ((let (start end)
  1931. (if endp
  1932. (setq start matching-tag-pos
  1933. end (point))
  1934. (setq start (point)
  1935. end matching-tag-pos))
  1936. (save-excursion
  1937. (or (when start
  1938. (goto-char start)
  1939. (nxml-preceding-sibling-data-p))
  1940. (when end
  1941. (goto-char end)
  1942. (nxml-following-sibling-data-p)))))
  1943. t)
  1944. ;; Otherwise, treat as not mixed
  1945. (t nil))))
  1946. (defun nxml-preceding-sibling-data-p ()
  1947. "Return non-nil if there is a previous sibling that is data."
  1948. (let ((lim (max (- (point) nxml-mixed-scan-distance)
  1949. nxml-prolog-end))
  1950. (level 0)
  1951. found end)
  1952. (xmltok-save
  1953. (save-excursion
  1954. (while (and (< lim (point))
  1955. (>= level 0)
  1956. (not found)
  1957. (progn
  1958. (setq end (point))
  1959. (search-backward "<" lim t)))
  1960. (nxml-move-outside-backwards)
  1961. (save-excursion
  1962. (xmltok-forward)
  1963. (let ((prev-level level))
  1964. (cond ((eq xmltok-type 'end-tag)
  1965. (setq level (1+ level)))
  1966. ((eq xmltok-type 'start-tag)
  1967. (setq level (1- level))))
  1968. (when (eq prev-level 0)
  1969. (while (and (< (point) end) (not found))
  1970. (xmltok-forward)
  1971. (when (memq xmltok-type '(data cdata-section char-ref))
  1972. (setq found t)))))))))
  1973. found))
  1974. (defun nxml-following-sibling-data-p ()
  1975. (let ((lim (min (+ (point) nxml-mixed-scan-distance)
  1976. (point-max)))
  1977. (level 0)
  1978. found)
  1979. (xmltok-save
  1980. (save-excursion
  1981. (while (and (< (point) lim)
  1982. (>= level 0)
  1983. (nxml-tokenize-forward)
  1984. (not found))
  1985. (cond ((eq xmltok-type 'start-tag)
  1986. (setq level (1+ level)))
  1987. ((eq xmltok-type 'end-tag)
  1988. (setq level (1- level)))
  1989. ((and (eq level 0)
  1990. (memq xmltok-type '(data cdata-section char-ref)))
  1991. (setq found t))))))
  1992. found))
  1993. ;;; Filling
  1994. (defun nxml-do-fill-paragraph (arg)
  1995. (let (fill-paragraph-function
  1996. fill-prefix
  1997. start end)
  1998. (save-excursion
  1999. (nxml-forward-paragraph)
  2000. (setq end (point))
  2001. (nxml-backward-paragraph)
  2002. (skip-chars-forward " \t\r\n")
  2003. (setq start (point))
  2004. (beginning-of-line)
  2005. (setq fill-prefix (buffer-substring-no-properties (point) start))
  2006. (when (and (not (nxml-get-inside (point)))
  2007. (looking-at "[ \t]*<!--"))
  2008. (setq fill-prefix (concat fill-prefix " ")))
  2009. (fill-region-as-paragraph start end arg))
  2010. (skip-line-prefix fill-prefix)
  2011. fill-prefix))
  2012. (defun nxml-newline-and-indent (soft)
  2013. (delete-horizontal-space)
  2014. (if soft (insert-and-inherit ?\n) (newline 1))
  2015. (nxml-indent-line))
  2016. ;;; Dynamic markup
  2017. (defvar nxml-dynamic-markup-prev-pos nil)
  2018. (defvar nxml-dynamic-markup-prev-lengths nil)
  2019. (defvar nxml-dynamic-markup-prev-found-marker nil)
  2020. (defvar nxml-dynamic-markup-prev-start-tags (make-hash-table :test 'equal))
  2021. (defun nxml-dynamic-markup-word ()
  2022. "Dynamically markup the word before point.
  2023. This attempts to find a tag to put around the word before point based
  2024. on the contents of the current buffer. The end-tag will be inserted at
  2025. point. The start-tag will be inserted at or before the beginning of
  2026. the word before point; the contents of the current buffer is used to
  2027. decide where.
  2028. It works in a similar way to \\[dabbrev-expand]. It searches first
  2029. backwards from point, then forwards from point for an element whose
  2030. content is a string which matches the contents of the buffer before
  2031. point and which includes at least the word before point. It then
  2032. copies the start- and end-tags from that element and uses them to
  2033. surround the matching string before point.
  2034. Repeating \\[nxml-dynamic-markup-word] immediately after successful
  2035. \\[nxml-dynamic-markup-word] removes the previously inserted markup
  2036. and attempts to find another possible way to do the markup."
  2037. (interactive "*")
  2038. (let (search-start-pos)
  2039. (if (and (integerp nxml-dynamic-markup-prev-pos)
  2040. (= nxml-dynamic-markup-prev-pos (point))
  2041. (eq last-command this-command)
  2042. nxml-dynamic-markup-prev-lengths)
  2043. (let* ((end-tag-open-pos
  2044. (- nxml-dynamic-markup-prev-pos
  2045. (nth 2 nxml-dynamic-markup-prev-lengths)))
  2046. (start-tag-close-pos
  2047. (- end-tag-open-pos
  2048. (nth 1 nxml-dynamic-markup-prev-lengths)))
  2049. (start-tag-open-pos
  2050. (- start-tag-close-pos
  2051. (nth 0 nxml-dynamic-markup-prev-lengths))))
  2052. (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos)
  2053. (delete-region start-tag-open-pos start-tag-close-pos)
  2054. (setq search-start-pos
  2055. (marker-position nxml-dynamic-markup-prev-found-marker)))
  2056. (clrhash nxml-dynamic-markup-prev-start-tags))
  2057. (setq nxml-dynamic-markup-prev-pos nil)
  2058. (setq nxml-dynamic-markup-prev-lengths nil)
  2059. (setq nxml-dynamic-markup-prev-found-marker nil)
  2060. (goto-char
  2061. (save-excursion
  2062. (let* ((pos (point))
  2063. (word (progn
  2064. (backward-word 1)
  2065. (unless (< (point) pos)
  2066. (error "No word to markup"))
  2067. (buffer-substring-no-properties (point) pos)))
  2068. (search (concat word "</"))
  2069. done)
  2070. (when search-start-pos
  2071. (goto-char search-start-pos))
  2072. (while (and (not done)
  2073. (or (and (< (point) pos)
  2074. (or (search-backward search nil t)
  2075. (progn (goto-char pos) nil)))
  2076. (search-forward search nil t)))
  2077. (goto-char (- (match-end 0) 2))
  2078. (setq done (nxml-try-copy-markup pos)))
  2079. (or done
  2080. (error (if (zerop (hash-table-count
  2081. nxml-dynamic-markup-prev-start-tags))
  2082. "No possible markup found for `%s'"
  2083. "No more markup possibilities found for `%s'")
  2084. word)))))))
  2085. (defun nxml-try-copy-markup (word-end-pos)
  2086. (save-excursion
  2087. (let ((end-tag-pos (point)))
  2088. (when (and (not (nxml-get-inside end-tag-pos))
  2089. (search-backward "<" nil t)
  2090. (not (nxml-get-inside (point))))
  2091. (xmltok-forward)
  2092. (when (and (eq xmltok-type 'start-tag)
  2093. (< (point) end-tag-pos))
  2094. (let* ((start-tag-close-pos (point))
  2095. (start-tag
  2096. (buffer-substring-no-properties xmltok-start
  2097. start-tag-close-pos))
  2098. (words
  2099. (nreverse
  2100. (split-string
  2101. (buffer-substring-no-properties start-tag-close-pos
  2102. end-tag-pos)
  2103. "[ \t\r\n]+"))))
  2104. (goto-char word-end-pos)
  2105. (while (and words
  2106. (re-search-backward (concat
  2107. (regexp-quote (car words))
  2108. "\\=")
  2109. nil
  2110. t))
  2111. (setq words (cdr words))
  2112. (skip-chars-backward " \t\r\n"))
  2113. (when (and (not words)
  2114. (progn
  2115. (skip-chars-forward " \t\r\n")
  2116. (not (gethash (cons (point) start-tag)
  2117. nxml-dynamic-markup-prev-start-tags)))
  2118. (or (< end-tag-pos (point))
  2119. (< word-end-pos xmltok-start)))
  2120. (setq nxml-dynamic-markup-prev-found-marker
  2121. (copy-marker end-tag-pos t))
  2122. (puthash (cons (point) start-tag)
  2123. t
  2124. nxml-dynamic-markup-prev-start-tags)
  2125. (setq nxml-dynamic-markup-prev-lengths
  2126. (list (- start-tag-close-pos xmltok-start)
  2127. (- word-end-pos (point))
  2128. (+ (- xmltok-name-end xmltok-start) 2)))
  2129. (let ((name (xmltok-start-tag-qname)))
  2130. (insert start-tag)
  2131. (goto-char (+ word-end-pos
  2132. (- start-tag-close-pos xmltok-start)))
  2133. (insert "</" name ">")
  2134. (setq nxml-dynamic-markup-prev-pos (point))))))))))
  2135. ;;; Character names
  2136. (defun nxml-insert-named-char (arg)
  2137. "Insert a character using its name.
  2138. The name is read from the minibuffer.
  2139. Normally, inserts the character as a numeric character reference.
  2140. With a prefix argument, inserts the character directly."
  2141. (interactive "*P")
  2142. (let ((code (read-char-by-name "Character name: ")))
  2143. (when code
  2144. (insert (if arg code (format "&#x%X;" code))))))
  2145. (defun nxml-toggle-char-ref-extra-display (arg)
  2146. "Toggle the display of extra information for character references."
  2147. (interactive "P")
  2148. (let ((new (if (null arg)
  2149. (not nxml-char-ref-extra-display)
  2150. (> (prefix-numeric-value arg) 0))))
  2151. (when (not (eq new nxml-char-ref-extra-display))
  2152. (setq nxml-char-ref-extra-display new)
  2153. (font-lock-flush))))
  2154. (put 'nxml-char-ref 'evaporate t)
  2155. (defun nxml-char-ref-display-extra (start end n)
  2156. (when nxml-char-ref-extra-display
  2157. (let ((name (or (get-char-code-property n 'name)
  2158. (get-char-code-property n 'old-name)))
  2159. (glyph-string (and nxml-char-ref-display-glyph-flag
  2160. (char-displayable-p n)
  2161. (string n)))
  2162. ov)
  2163. (when (or name glyph-string)
  2164. (setq ov (make-overlay start end nil t))
  2165. (overlay-put ov 'category 'nxml-char-ref)
  2166. (when name
  2167. (overlay-put ov 'help-echo name))
  2168. (when glyph-string
  2169. (overlay-put ov
  2170. 'after-string
  2171. (propertize glyph-string 'face 'nxml-glyph)))))))
  2172. (defun nxml-clear-char-ref-extra-display (start end)
  2173. (let ((ov (overlays-in start end)))
  2174. (while ov
  2175. (when (eq (overlay-get (car ov) 'category) 'nxml-char-ref)
  2176. (delete-overlay (car ov)))
  2177. (setq ov (cdr ov)))))
  2178. (defun nxml-start-delimiter-length (type)
  2179. (or (get type 'nxml-start-delimiter-length)
  2180. 0))
  2181. (put 'cdata-section 'nxml-start-delimiter-length 9)
  2182. (put 'comment 'nxml-start-delimiter-length 4)
  2183. (put 'processing-instruction 'nxml-start-delimiter-length 2)
  2184. (put 'start-tag 'nxml-start-delimiter-length 1)
  2185. (put 'empty-element 'nxml-start-delimiter-length 1)
  2186. (put 'partial-empty-element 'nxml-start-delimiter-length 1)
  2187. (put 'entity-ref 'nxml-start-delimiter-length 1)
  2188. (put 'char-ref 'nxml-start-delimiter-length 2)
  2189. (defun nxml-end-delimiter-length (type)
  2190. (or (get type 'nxml-end-delimiter-length)
  2191. 0))
  2192. (put 'cdata-section 'nxml-end-delimiter-length 3)
  2193. (put 'comment 'nxml-end-delimiter-length 3)
  2194. (put 'processing-instruction 'nxml-end-delimiter-length 2)
  2195. (put 'start-tag 'nxml-end-delimiter-length 1)
  2196. (put 'empty-element 'nxml-end-delimiter-length 2)
  2197. (put 'partial-empty-element 'nxml-end-delimiter-length 1)
  2198. (put 'entity-ref 'nxml-end-delimiter-length 1)
  2199. (put 'char-ref 'nxml-end-delimiter-length 1)
  2200. (defun nxml-token-type-friendly-name (type)
  2201. (or (get type 'nxml-friendly-name)
  2202. (symbol-name type)))
  2203. (put 'cdata-section 'nxml-friendly-name "CDATA section")
  2204. (put 'processing-instruction 'nxml-friendly-name "processing instruction")
  2205. (put 'entity-ref 'nxml-friendly-name "entity reference")
  2206. (put 'char-ref 'nxml-friendly-name "character reference")
  2207. ;; Only do this in loaddefs, so that if someone defines a different
  2208. ;; alias in .emacs, loading this file afterwards does not clobber it.
  2209. ;;;###autoload(defalias 'xml-mode 'nxml-mode)
  2210. (provide 'nxml-mode)
  2211. ;;; nxml-mode.el ends here