(nxml-mode): Call rng-nxml-mode-init directly.
[bpt/emacs.git] / lisp / nxml / rng-util.el
1 ;;; rng-util.el --- utility functions for RELAX NG library
2
3 ;; Copyright (C) 2003, 2007, 2008 Free Software Foundation, Inc.
4
5 ;; Author: James Clark
6 ;; Keywords: XML, RelaxNG
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (defun rng-make-datatypes-uri (uri)
30 (if (string-equal uri "")
31 ;; The spec doesn't say to do this, but it's perfectly conformant
32 ;; and better than using nil, I think.
33 'http://relaxng.org/ns/structure/1.0
34 (intern uri)))
35
36 (defconst rng-xsd-datatypes-uri
37 (rng-make-datatypes-uri "http://www.w3.org/2001/XMLSchema-datatypes"))
38
39 (defconst rng-builtin-datatypes-uri (rng-make-datatypes-uri ""))
40
41 (defun rng-uniquify-eq (list)
42 "Destructively remove any element from LIST that is eq to
43 its predecessor."
44 (and list
45 (let ((head list))
46 (while (cdr head)
47 (if (eq (car head) (cadr head))
48 (setcdr head (cddr head)))
49 (setq head (cdr head)))
50 list)))
51
52 (defun rng-uniquify-equal (list)
53 "Destructively remove any element from LIST that is equal to
54 its predecessor."
55 (and list
56 (let ((head list))
57 (while (cdr head)
58 (if (equal (car head) (cadr head))
59 (setcdr head (cddr head)))
60 (setq head (cdr head)))
61 list)))
62
63 (defun rng-blank-p (str) (string-match "\\`[ \t\n\r]*\\'" str))
64
65 (defun rng-substq (new old list)
66 "Replace first member of LIST (if any) that is eq to OLD by NEW.
67 LIST is not modified."
68 (cond ((null list) nil)
69 ((eq (car list) old)
70 (cons new (cdr list)))
71 (t
72 (let ((tail (cons (car list)
73 nil))
74 (rest (cdr list)))
75 (setq list tail)
76 (while rest
77 (let ((item (car rest)))
78 (setq rest (cdr rest))
79 (cond ((eq item old)
80 (setcdr tail
81 (cons new rest))
82 (setq rest nil))
83 (t
84 (setq tail
85 (setcdr tail
86 (cons item nil))))))))
87 list)))
88
89 (defun rng-complete-before-point (start table prompt &optional predicate hist)
90 "Complete text between START and point.
91 Replaces the text between START and point with a string chosen using a
92 completion table and, when needed, input read from the user with the
93 minibuffer.
94 Returns the new string if either a complete and unique completion was
95 determined automatically or input was read from the user. Otherwise,
96 returns nil.
97 TABLE is an alist, a symbol bound to a function or an obarray as with
98 the function `completing-read'.
99 PROMPT is the string to prompt with if user input is needed.
100 PREDICATE is nil or a function as with `completing-read'.
101 HIST, if non-nil, specifies a history list as with `completing-read'."
102 (let* ((orig (buffer-substring-no-properties start (point)))
103 (completion (try-completion orig table predicate)))
104 (cond ((not completion)
105 (if (string= orig "")
106 (message "No completions available")
107 (message "No completion for %s" (rng-quote-string orig)))
108 (ding)
109 nil)
110 ((eq completion t) orig)
111 ((not (string= completion orig))
112 (delete-region start (point))
113 (insert completion)
114 (cond ((not (rng-completion-exact-p completion table predicate))
115 (message "Incomplete")
116 nil)
117 ((eq (try-completion completion table predicate) t)
118 completion)
119 (t
120 (message "Complete but not unique")
121 nil)))
122 (t
123 (setq completion
124 (let ((saved-minibuffer-setup-hook
125 (default-value 'minibuffer-setup-hook)))
126 (add-hook 'minibuffer-setup-hook
127 'minibuffer-completion-help
128 t)
129 (unwind-protect
130 (completing-read prompt
131 table
132 predicate
133 nil
134 orig
135 hist)
136 (setq-default minibuffer-setup-hook
137 saved-minibuffer-setup-hook))))
138 (delete-region start (point))
139 (insert completion)
140 completion))))
141
142 (defun rng-completion-exact-p (string table predicate)
143 (cond ((symbolp table)
144 (funcall table string predicate 'lambda))
145 ((vectorp table)
146 (intern-soft string table))
147 (t (assoc string table))))
148
149 (defun rng-quote-string (s)
150 (concat "\"" s "\""))
151
152 (defun rng-escape-string (s)
153 (replace-regexp-in-string "[&\"<>]"
154 (lambda (match)
155 (cdr (assoc match
156 '(("&" . "&amp;")
157 ("\"" . "&quot;")
158 (">" . "&gt;")
159 ("<" . "&lt;")))))
160 s
161 t))
162
163 (defun rng-collapse-space (string)
164 (setq string
165 (replace-regexp-in-string "[ \t\r\n]+" " " string t t))
166 (when (string-match "\\` " string)
167 (setq string (substring string 1)))
168 (when (string-match " \\'" string)
169 (setq string (substring string 0 -1)))
170 string)
171
172 (provide 'rng-util)
173
174 ;; arch-tag: 2dc233e0-5e7a-488f-bfc4-5909512dbaf0
175 ;;; rng-util.el ends here