4

I have a bunch of rather verbose xml files that are named in a certain way like "verbosefile_*.xml". I have a pair of python scripts that transform these xml files into a compact json format and vice versa. Currently my workflow is that I first convert the verbose xml file into a compact json file using my script, make changes or edits to that file, and then convert the compact json version back to the verbose xml format. I wanted this process to happen transparently whenever I visit/open a file in emacs. Basically, my new workflow should be that upon opening the verbose xml files directly from the editor, I should see the compact json representation, and upon writing the file, the json should be converted behind the scenes to the verbose xml format and written. Some hints on how to do this would be great. I am new to elisp and the emacs hooks/functions, even if I have been using the editor for some time.

EDIT: I can change the python scripts to make things easier. For example, if the scripts working on std input makes the problem easier to solve, I can do that.

3
  • 1
    Here is a link to the documentation regarding setting up a sentinel: gnu.org/software/emacs/manual/html_node/elisp/Sentinels.html Here is a link to the documentation regarding setting up a filter for the process (just in case you need to monitor the process output and take certain action based on said output): gnu.org/software/emacs/manual/html_node/elisp/… Here is a link to the documentation discussing using start-process: gnu.org/software/emacs/manual/html_node/elisp/…
    – lawlist
    Nov 28, 2014 at 13:00
  • 1
    Once you get that process stuff out of the way, the rest will be a piece of cake -- using either a major-mode hook or a find-file-hook or something like those. I'd start first with the process stuff -- get that working, and then a simple Google for emacs add-hook and whatever major mode you are using should get you what you need.
    – lawlist
    Nov 28, 2014 at 13:02
  • 2
    Take a look at hexl-mode. This is a hex editor that use an external command to dump a file to a human-readable format and, upon saving, convert it back again. Nov 28, 2014 at 13:39

2 Answers 2

5

There are various ways to do what you ask.

One of them, that was meant to be "the canonical way" (although it seems it is not used very often) is to use format-alist. In your case, the detection of the format is based on the file-name rather than the file-contents, so you'd globally add an entry to format-alist where the REGEXP part is nil. Then you'd setup a special mode via auto-mode-alist and that mode would start by passing the buffer's contents through your first script (with call-process-region), then set buffer-file-format to indicate that you're using the format you've added to format-alist.

Another option is to use a file-name-handler-alist, as we do for .gz files.

Yet another option is to use a special mode (as above) but instead of setting up format-alist and buffer-file-format, you'd simply (add-hook 'write-file-functions #'my-write-hook nil t) and make my-write-hook save the file using the second script.

1

I have a different on-disc format (gjots) and I wanted to edit them using emacs' org-mode.

Since format-alist can't use a file extension like *.gjots, I followed the previous post's description of the technique and (coupled with the code at Model/View-like editing in Emacs) I got to this working solution to the original problem:

(setq format-alist
      (cons '(gjots "gjots" nil "gjots2org" "org2gjots" t nil) format-alist))
(define-derived-mode gjots-mode org-mode "gjots"
  "Major mode for editing gjots files."
  (format-decode-buffer 'gjots))
(autoload 'gjots-mode "gjots-mode" "Major mode to edit gjots files." t)
(setq auto-mode-alist
      (cons '("\\.gjots$" . gjots-mode) auto-mode-alist))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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