0

I have auto-insert-mode configured to insert a basic XML file in nxml-mode, but nxml-mode is then still configured to use an empty schema, as if the file was still empty. How can I automatically make nxml-mode use the correct schema corresponding to the template? Set schema automatically in the menus does work, but I want to run this automatically.

1

I never used that but looking at the documentation and code, I think the function you're talking about is rng-auto-set-schema from rng-validate-mode. This minor mode is enabled by default with nxml-mode.

Here is what auto-insert-mode does:

  (if auto-insert-mode
      (add-hook 'find-file-hook 'auto-insert)
    (remove-hook 'find-file-hook 'auto-insert)))

The problem is probably that find-file-hook is considered only after the major mode has been selected, so rng-auto-set-schema is called before your snippet is actually inserted in the buffer.

Since auto-insert apparently does not provide any hooks, I see two ways to work around that:

  • advise auto-insert so that after inserting, it calls rng-auto-set-schema if major mode is nxml-mode,

  • add a hook at the end of find-file-hook to do the same thing.

Here's a tentative for the second solution:

(defun my-set-schema-if-nxml ()
  (if (eq major-mode 'nxml-mode)
      (rng-auto-set-schema)))

(add-hook 'find-file-hook 'my-set-schema-if-nxml t)
  • I'd love to see someone implement a more generic solution: when the buffer starts empty, setup an after-change-function which tries to set the schema. And remove this after-change-function as soon as we have enough data to determine the schema (i.e. as soon as we see the end of the first opening tag). – Stefan Jan 28 '16 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.