nxml-util.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. ;;; nxml-util.el --- utility functions for nxml-*.el
  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. ;;; Code:
  18. (defconst nxml-debug nil
  19. "Enable nxml debugging. Effective only at compile time.")
  20. (defsubst nxml-debug (format &rest args)
  21. (when nxml-debug
  22. (apply #'message format args)))
  23. (defmacro nxml-debug-change (name start end)
  24. (when nxml-debug
  25. `(nxml-debug "%s: %S" ,name
  26. (buffer-substring-no-properties ,start ,end))))
  27. (defmacro nxml-debug-set-inside (start end)
  28. (when nxml-debug
  29. `(let ((overlay (make-overlay ,start ,end)))
  30. (overlay-put overlay 'face '(:background "red"))
  31. (overlay-put overlay 'nxml-inside-debug t)
  32. (nxml-debug-change "nxml-set-inside" ,start ,end))))
  33. (defmacro nxml-debug-clear-inside (start end)
  34. (when nxml-debug
  35. `(loop for overlay in (overlays-in ,start ,end)
  36. if (overlay-get overlay 'nxml-inside-debug)
  37. do (delete-overlay overlay)
  38. finally (nxml-debug-change "nxml-clear-inside" ,start ,end))))
  39. (defun nxml-make-namespace (str)
  40. "Return a symbol for the namespace URI STR.
  41. STR must be a string. If STR is the empty string, return nil.
  42. Otherwise, return the symbol whose name is STR prefixed with a colon."
  43. (if (string-equal str "")
  44. nil
  45. (intern (concat ":" str))))
  46. (defun nxml-namespace-name (ns)
  47. "Return the namespace URI corresponding to the symbol NS.
  48. This is the inverse of `nxml-make-namespace'."
  49. (and ns (substring (symbol-name ns) 1)))
  50. (defconst nxml-xml-namespace-uri
  51. (nxml-make-namespace "http://www.w3.org/XML/1998/namespace"))
  52. (defconst nxml-xmlns-namespace-uri
  53. (nxml-make-namespace "http://www.w3.org/2000/xmlns/"))
  54. (defmacro nxml-with-degradation-on-error (context &rest body)
  55. (if (not nxml-debug)
  56. (let ((error-symbol (make-symbol "err")))
  57. `(condition-case ,error-symbol
  58. (progn ,@body)
  59. (error
  60. (nxml-degrade ,context ,error-symbol))))
  61. `(progn ,@body)))
  62. (defmacro nxml-with-unmodifying-text-property-changes (&rest body)
  63. "Evaluate BODY without any text property changes modifying the buffer.
  64. Any text properties changes happen as usual but the changes are not treated as
  65. modifications to the buffer."
  66. (let ((modified (make-symbol "modified")))
  67. `(let ((,modified (buffer-modified-p))
  68. (inhibit-read-only t)
  69. (inhibit-modification-hooks t)
  70. (buffer-undo-list t)
  71. (deactivate-mark nil)
  72. ;; Apparently these avoid file locking problems.
  73. (buffer-file-name nil)
  74. (buffer-file-truename nil))
  75. (unwind-protect
  76. (progn ,@body)
  77. (unless ,modified
  78. (restore-buffer-modified-p nil))))))
  79. (put 'nxml-with-unmodifying-text-property-changes 'lisp-indent-function 0)
  80. (def-edebug-spec nxml-with-unmodifying-text-property-changes t)
  81. (defmacro nxml-with-invisible-motion (&rest body)
  82. "Evaluate body without calling any point motion hooks."
  83. `(let ((inhibit-point-motion-hooks t))
  84. ,@body))
  85. (put 'nxml-with-invisible-motion 'lisp-indent-function 0)
  86. (def-edebug-spec nxml-with-invisible-motion t)
  87. (defun nxml-display-file-parse-error (err)
  88. (let* ((filename (nth 1 err))
  89. (buffer (find-file-noselect filename))
  90. (pos (nth 2 err))
  91. (message (nth 3 err)))
  92. (pop-to-buffer buffer)
  93. ;; What's the right thing to do if the buffer's modified?
  94. ;; The position in the saved file could be completely different.
  95. (goto-char (if (buffer-modified-p) 1 pos))
  96. (error "%s" message)))
  97. (defun nxml-signal-file-parse-error (file pos message &optional error-symbol)
  98. (signal (or error-symbol 'nxml-file-parse-error)
  99. (list file pos message)))
  100. (put 'nxml-file-parse-error
  101. 'error-conditions
  102. '(error nxml-file-parse-error))
  103. (put 'nxml-parse-file-error
  104. 'error-message
  105. "Error parsing file")
  106. (provide 'nxml-util)
  107. ;;; nxml-util.el ends here