nxml-enc.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ;;; nxml-enc.el --- XML encoding auto-detection
  2. ;; Copyright (C) 2003, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: James Clark
  4. ;; Keywords: 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. ;; User entry points are nxml-start-auto-coding and
  18. ;; nxml-stop-auto-coding. This is separate from nxml-mode, because
  19. ;; this cannot be autoloaded. It may use
  20. ;; `xmltok-get-declared-encoding-position' which can be autoloaded.
  21. ;; It's separate from rng-auto.el so it can be byte-compiled, and
  22. ;; because it provides independent, useful functionality.
  23. ;;; Code:
  24. (defvar nxml-file-name-ignore-case
  25. (memq system-type '(windows-nt)))
  26. (defvar nxml-cached-file-name-auto-coding-regexp nil)
  27. (defvar nxml-cached-auto-mode-alist nil)
  28. (defun nxml-file-name-auto-coding-regexp ()
  29. "Return regexp for filenames for which XML auto-coding should be done."
  30. (if (eq auto-mode-alist nxml-cached-auto-mode-alist)
  31. nxml-cached-file-name-auto-coding-regexp
  32. (let ((alist auto-mode-alist)
  33. (case-fold-search nxml-file-name-ignore-case)
  34. regexps)
  35. (setq nxml-cached-auto-mode-alist alist)
  36. (while alist
  37. (when (eq (cdar alist) 'nxml-mode)
  38. (setq regexps (cons (caar alist) regexps)))
  39. (setq alist (cdr alist)))
  40. (setq nxml-cached-file-name-auto-coding-regexp
  41. (if (null (cdr regexps))
  42. (car regexps)
  43. (mapconcat (lambda (r)
  44. (concat "\\(?:" r "\\)"))
  45. regexps
  46. "\\|"))))))
  47. (defvar nxml-non-xml-set-auto-coding-function nil
  48. "The function that `set-auto-coding-function' should call for non-XML files.")
  49. (defun nxml-set-auto-coding (file-name size)
  50. (if (let ((case-fold-search nxml-file-name-ignore-case)
  51. (regexp (nxml-file-name-auto-coding-regexp)))
  52. (and regexp
  53. (string-match regexp file-name)))
  54. (nxml-set-xml-coding file-name size)
  55. (and nxml-non-xml-set-auto-coding-function
  56. (funcall nxml-non-xml-set-auto-coding-function file-name size))))
  57. (defun nxml-set-xml-coding (file-name size)
  58. "Function to use as `set-auto-coding-function' when file is known to be XML."
  59. (nxml-detect-coding-system (+ (point) (min size 1024))))
  60. (declare-function xmltok-get-declared-encoding-position "xmltok"
  61. (&optional limit)) ; autoloaded
  62. (defun nxml-detect-coding-system (limit)
  63. (if (< limit (+ (point) 2))
  64. (if (eq (char-after) 0) 'no-conversion 'utf-8)
  65. (let ((first-two-chars (list (char-after)
  66. (char-after (1+ (point))))))
  67. (cond ((equal first-two-chars '(#xFE #xFF))
  68. (and (coding-system-p 'utf-16-be) 'utf-16-be))
  69. ((equal first-two-chars '(#xFF #xFE))
  70. (and (coding-system-p 'utf-16-le) 'utf-16-le))
  71. ((memq 0 first-two-chars)
  72. ;; Certainly not well-formed XML;
  73. ;; perhaps UTF-16 without BOM.
  74. ;; In any case, we can't handle it.
  75. ;; no-conversion gives the user a chance to fix it.
  76. 'no-conversion)
  77. ;; There are other things we might try here in the future
  78. ;; eg UTF-8 BOM, UTF-16 with no BOM
  79. ;; translate to EBCDIC
  80. (t
  81. (let ((enc-pos (xmltok-get-declared-encoding-position limit)))
  82. (cond ((consp enc-pos)
  83. (or (nxml-mime-charset-coding-system
  84. (buffer-substring-no-properties (car enc-pos)
  85. (cdr enc-pos)))
  86. ;; We have an encoding whose name we don't recognize.
  87. ;; What to do?
  88. ;; raw-text seems the best bet: since we got
  89. ;; the XML decl it must be a superset of ASCII,
  90. ;; so we don't need to go to no-conversion
  91. 'raw-text))
  92. (enc-pos 'utf-8)
  93. ;; invalid XML declaration
  94. (t nil))))))))
  95. (defun nxml-mime-charset-coding-system (charset)
  96. (let ((charset-sym (intern (downcase charset)))
  97. (coding-systems (coding-system-list t))
  98. coding-system ret)
  99. (while (and coding-systems (not ret))
  100. (setq coding-system (car coding-systems))
  101. (if (eq (coding-system-get coding-system 'mime-charset)
  102. charset-sym)
  103. (setq ret coding-system)
  104. (setq coding-systems (cdr coding-systems))))
  105. ret))
  106. (defun nxml-start-auto-coding ()
  107. "Do encoding auto-detection as specified in the XML standard.
  108. Applied to any files that `auto-mode-alist' says should be handled by
  109. `nxml-mode'."
  110. (interactive)
  111. (unless (eq set-auto-coding-function 'nxml-set-auto-coding)
  112. (let ((inhibit-quit t))
  113. (setq nxml-non-xml-set-auto-coding-function set-auto-coding-function)
  114. (setq set-auto-coding-function 'nxml-set-auto-coding))))
  115. (defun nxml-stop-auto-coding ()
  116. "Stop doing encoding auto-detection as specified in the XML standard."
  117. (interactive)
  118. (when (eq set-auto-coding-function 'nxml-set-auto-coding)
  119. (let ((inhibit-quit t))
  120. (setq set-auto-coding-function nxml-non-xml-set-auto-coding-function)
  121. (setq nxml-non-xml-set-auto-coding-function nil))))
  122. ;; Emacs 22 makes us-ascii an alias for iso-safe without
  123. ;; giving it a mime-charset property.
  124. (unless (coding-system-get 'us-ascii 'mime-charset)
  125. (coding-system-put 'us-ascii 'mime-charset 'us-ascii))
  126. (provide 'nxml-enc)
  127. ;;; nxml-enc.el ends here