nxml-util.el 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ;;; nxml-util.el --- utility functions for nxml-*.el -*- lexical-binding:t -*-
  2. ;; Copyright (C) 2003, 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. ;;; 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. (defun nxml-make-namespace (str)
  28. "Return a symbol for the namespace URI STR.
  29. STR must be a string. If STR is the empty string, return nil.
  30. Otherwise, return the symbol whose name is STR prefixed with a colon."
  31. (if (string-equal str "")
  32. nil
  33. (intern (concat ":" str))))
  34. (defun nxml-namespace-name (ns)
  35. "Return the namespace URI corresponding to the symbol NS.
  36. This is the inverse of `nxml-make-namespace'."
  37. (and ns (substring (symbol-name ns) 1)))
  38. (defconst nxml-xml-namespace-uri
  39. (nxml-make-namespace "http://www.w3.org/XML/1998/namespace"))
  40. (defconst nxml-xmlns-namespace-uri
  41. (nxml-make-namespace "http://www.w3.org/2000/xmlns/"))
  42. (defmacro nxml-with-degradation-on-error (context &rest body)
  43. (declare (indent 1) (debug t))
  44. (if (not nxml-debug)
  45. (let ((error-symbol (make-symbol "err")))
  46. `(condition-case ,error-symbol
  47. (progn ,@body)
  48. (error
  49. (nxml-degrade ,context ,error-symbol))))
  50. `(progn ,@body)))
  51. (defmacro nxml-with-invisible-motion (&rest body)
  52. "Evaluate body without calling any point motion hooks."
  53. (declare (indent 0) (debug t))
  54. `(let ((inhibit-point-motion-hooks t))
  55. ,@body))
  56. (defun nxml-display-file-parse-error (err)
  57. (let* ((filename (nth 1 err))
  58. (buffer (find-file-noselect filename))
  59. (pos (nth 2 err))
  60. (message (nth 3 err)))
  61. (pop-to-buffer buffer)
  62. ;; What's the right thing to do if the buffer's modified?
  63. ;; The position in the saved file could be completely different.
  64. (goto-char (if (buffer-modified-p) 1 pos))
  65. (error "%s" message)))
  66. (defun nxml-signal-file-parse-error (file pos message &optional error-symbol)
  67. (signal (or error-symbol 'nxml-file-parse-error)
  68. (list file pos message)))
  69. (define-error 'nxml-error nil)
  70. (define-error 'nxml-file-parse-error "Error parsing file" 'nxml-error)
  71. (provide 'nxml-util)
  72. ;;; nxml-util.el ends here