initial commit
[emacs-init.git] / nxhtml / etc / schema / schema-path-patch.el
1 ;;; schema-path-patch.el --- Patch schema paths
2 ;;
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: 2008-08-08T20:21:31+0200 Fri
5 ;; Version: 0.2
6 ;; Last-Updated: 2008-08-19T00:21:25+0200 Mon
7 ;; URL:
8 ;; Keywords:
9 ;; Compatibility:
10 ;;
11 ;; Features that might be required by this library:
12 ;;
13 ;;   Cannot open load file: schema-path-patch.
14 ;;
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 ;;
17 ;;; Commentary:
18 ;;
19 ;; Schemas here may include parts from nxml and need to know the path.
20 ;; This file can be used to patch the paths.
21 ;;
22 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23 ;;
24 ;;; Change log:
25 ;;
26 ;;
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;
29 ;; This program is free software; you can redistribute it and/or
30 ;; modify it under the terms of the GNU General Public License as
31 ;; published by the Free Software Foundation; either version 2, or
32 ;; (at your option) any later version.
33 ;;
34 ;; This program is distributed in the hope that it will be useful,
35 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
36 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37 ;; General Public License for more details.
38 ;;
39 ;; You should have received a copy of the GNU General Public License
40 ;; along with this program; see the file COPYING.  If not, write to
41 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
42 ;; Floor, Boston, MA 02110-1301, USA.
43 ;;
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;;
46 ;;; Code:
47
48 (defvar rncpp-this-dir
49   (file-name-as-directory
50    (file-name-directory
51     (if load-file-name load-file-name buffer-file-name))))
52
53 (defun rncpp-get-nxml-schema-dir ()
54   ;; First look for nxml-mode included with Emacs
55   (let ((schema-dir (file-name-as-directory
56                      (expand-file-name "schema" data-directory))))
57     (unless (file-directory-p schema-dir)
58       ;; This is an old nxml-mode, look for its schemas dir.
59       (let ((nxml-mode-dir (file-name-as-directory
60                             (file-name-directory (locate-library "nxml-mode")))))
61         (setq schema-dir (file-name-as-directory
62                           (expand-file-name "schema" nxml-mode-dir)))))
63     (unless (file-directory-p schema-dir)
64       (error "Can't find schema-dir=%s" schema-dir))
65     schema-dir))
66
67 ;; Use xhtml-loader.rnc (an idea from Bryan Waite):
68 (defun rncpp-patch-xhtml-loader ()
69   "Patch xhtml-loader.rnc so genshi and mjt rnc files works."
70   ;;(interactive)
71   (let* ((default-directory rncpp-this-dir)
72          (loader-path (expand-file-name "xhtml-loader.rnc"))
73          (loader-buf (find-buffer-visiting loader-path))
74          (schema-dir (rncpp-get-nxml-schema-dir))
75          (schema-relative-dir (file-relative-name schema-dir))
76          (loader-string (concat "include \""
77                                 schema-relative-dir
78                                 "xhtml.rnc\"\n")))
79     (when loader-buf (kill-buffer loader-buf))
80     (setq loader-buf (find-file-noselect loader-path))
81     (with-current-buffer loader-buf
82       (unless (file-exists-p loader-path)
83         (insert loader-string))
84       ;; Test if correct
85       (if (string= (buffer-substring-no-properties (point-min) (point-max))
86                        loader-string)
87           (message "xhtml-loader.rnc was ok")
88         (message "Patching xhtml-loader.rnc")
89         (delete-region (point-min) (point-max))
90         (insert loader-string))
91       (basic-save-buffer)
92       (kill-buffer (current-buffer)))))
93
94 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
95 ;;; schema-path-patch.el ends here