Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.4: leaving Xml plists as Xml' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: leaving Xml plists as Xml
Authored by: chris2 on Oct 30, '05 01:48:58PM
It appears jka-compr only uses magic numbers (ie. "bplist") when saving a file to see if it has already been compressed. Thus there is no way to use it as is to discriminate between original-xml and original-binary plists. One could modify jka-compr to check files before uncompressing, and accordingly set a flag to recompress on write. (perhaps an extra state of 'jka-compr-really-do-compress') This could conflict with other compression modes with optional magic numbers. Since is the first filetype with the same extension for both formats, it may be easier to just extend the previous hint than to generalize jka-compr. So here is a .emacs snippet that recognizes both formats and reconverts only initially-binary files on save:

(add-to-list 'auto-mode-alist '(".plist'" . visit-bplist))
(defvar plist-converted-binary nil
  "Buffer local variable indicating if file came from binary-plist.")
(make-variable-buffer-local 'plist-converted-binary)

(defun visit-bplist ()
  (when (string-match "^bplist" (buffer-string))
    (message "Reading in binary plist")
    (shell-command-on-region 
     (point-min) (point-max)
     (format "/usr/bin/plutil -convert xml1 -o /dev/stdout %s"
	     (shell-quote-argument (buffer-file-name))) t t)
    (save-excursion
      (goto-char (point-max)) (previous-line 1)
      (if (looking-at "/dev/stdout: Operation not supported") (kill-line 1)))
    (xml-mode)
    (set-buffer-modified-p nil)
    (setq buffer-undo-list nil)
    (setq plist-converted-binary t)))

(defadvice save-buffer (after convert-plist (&optional args))
  (when plist-converted-binary
    (shell-command
     (format "/usr/bin/plutil -convert binary1 %s"
	     (shell-quote-argument (buffer-file-name))) nil nil)
    (message "Wrote bplist %s" (buffer-file-name))))
(ad-activate 'save-buffer)

;; (defadvice write-file (after convert-plist (filename &optional confirm))
;;   (if plist-converted-binary
;;       (shell-command
;;        (format "/usr/bin/plutil -convert binary1 %s"
;; 	       (shell-quote-argument filename))
;;        nil nil)))
;; (ad-activate 'write-file)


[ Reply to This | # ]