Improve XHTML validation.
[org-mode.git] / lisp / org-compat.el
blobfdfcf356555881bd41703ca7842707419c5f7b3a
1 ;;; org-compat.el --- Compatibility code for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 6.10c
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains code needed for compatibility with XEmacs and older
29 ;; versions of GNU Emacs.
31 ;;; Code:
33 (require 'org-macs)
35 (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
36 (defconst org-format-transports-properties-p
37 (let ((x "a"))
38 (add-text-properties 0 1 '(test t) x)
39 (get-text-property 0 'test (format "%s" x)))
40 "Does format transport text properties?")
42 (defun org-compatible-face (inherits specs)
43 "Make a compatible face specification.
44 If INHERITS is an existing face and if the Emacs version supports it,
45 just inherit the face. If not, use SPECS to define the face.
46 XEmacs and Emacs 21 do not know about the `min-colors' attribute.
47 For them we convert a (min-colors 8) entry to a `tty' entry and move it
48 to the top of the list. The `min-colors' attribute will be removed from
49 any other entries, and any resulting duplicates will be removed entirely."
50 (cond
51 ((and inherits (facep inherits)
52 (not (featurep 'xemacs)) (> emacs-major-version 22))
53 ;; In Emacs 23, we use inheritance where possible.
54 ;; We only do this in Emacs 23, because only there the outline
55 ;; faces have been changed to the original org-mode-level-faces.
56 (list (list t :inherit inherits)))
57 ((or (featurep 'xemacs) (< emacs-major-version 22))
58 ;; These do not understand the `min-colors' attribute.
59 (let (r e a)
60 (while (setq e (pop specs))
61 (cond
62 ((memq (car e) '(t default)) (push e r))
63 ((setq a (member '(min-colors 8) (car e)))
64 (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
65 (cdr e)))))
66 ((setq a (assq 'min-colors (car e)))
67 (setq e (cons (delq a (car e)) (cdr e)))
68 (or (assoc (car e) r) (push e r)))
69 (t (or (assoc (car e) r) (push e r)))))
70 (nreverse r)))
71 (t specs)))
72 (put 'org-compatible-face 'lisp-indent-function 1)
74 ;;;; Emacs/XEmacs compatibility
76 ;; Overlay compatibility functions
77 (defun org-make-overlay (beg end &optional buffer)
78 (if (featurep 'xemacs)
79 (make-extent beg end buffer)
80 (make-overlay beg end buffer)))
81 (defun org-delete-overlay (ovl)
82 (if (featurep 'xemacs) (progn (delete-extent ovl) nil) (delete-overlay ovl)))
83 (defun org-detach-overlay (ovl)
84 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
85 (defun org-move-overlay (ovl beg end &optional buffer)
86 (if (featurep 'xemacs)
87 (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
88 (move-overlay ovl beg end buffer)))
89 (defun org-overlay-put (ovl prop value)
90 (if (featurep 'xemacs)
91 (set-extent-property ovl prop value)
92 (overlay-put ovl prop value)))
93 (defun org-overlay-display (ovl text &optional face evap)
94 "Make overlay OVL display TEXT with face FACE."
95 (if (featurep 'xemacs)
96 (let ((gl (make-glyph text)))
97 (and face (set-glyph-face gl face))
98 (set-extent-property ovl 'invisible t)
99 (set-extent-property ovl 'end-glyph gl))
100 (overlay-put ovl 'display text)
101 (if face (overlay-put ovl 'face face))
102 (if evap (overlay-put ovl 'evaporate t))))
103 (defun org-overlay-before-string (ovl text &optional face evap)
104 "Make overlay OVL display TEXT with face FACE."
105 (if (featurep 'xemacs)
106 (let ((gl (make-glyph text)))
107 (and face (set-glyph-face gl face))
108 (set-extent-property ovl 'begin-glyph gl))
109 (if face (org-add-props text nil 'face face))
110 (overlay-put ovl 'before-string text)
111 (if evap (overlay-put ovl 'evaporate t))))
112 (defun org-overlay-get (ovl prop)
113 (if (featurep 'xemacs)
114 (extent-property ovl prop)
115 (overlay-get ovl prop)))
116 (defun org-overlays-at (pos)
117 (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
118 (defun org-overlays-in (&optional start end)
119 (if (featurep 'xemacs)
120 (extent-list nil start end)
121 (overlays-in start end)))
122 (defun org-overlay-start (o)
123 (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
124 (defun org-overlay-end (o)
125 (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
126 (defun org-overlay-buffer (o)
127 (if (featurep 'xemacs) (extent-buffer o) (overlay-buffer o)))
128 (defun org-find-overlays (prop &optional pos delete)
129 "Find all overlays specifying PROP at POS or point.
130 If DELETE is non-nil, delete all those overlays."
131 (let ((overlays (org-overlays-at (or pos (point))))
132 ov found)
133 (while (setq ov (pop overlays))
134 (if (org-overlay-get ov prop)
135 (if delete (org-delete-overlay ov) (push ov found))))
136 found))
138 (defun org-add-hook (hook function &optional append local)
139 "Add-hook, compatible with both Emacsen."
140 (if (and local (featurep 'xemacs))
141 (add-local-hook hook function append)
142 (add-hook hook function append local)))
144 (defun org-add-props (string plist &rest props)
145 "Add text properties to entire string, from beginning to end.
146 PLIST may be a list of properties, PROPS are individual properties and values
147 that will be added to PLIST. Returns the string that was modified."
148 (add-text-properties
149 0 (length string) (if props (append plist props) plist) string)
150 string)
151 (put 'org-add-props 'lisp-indent-function 2)
153 (defun org-fit-window-to-buffer (&optional window)
154 "Fit the window to the buffer, but only if it is not a side-by-side window."
155 (cond ((> (frame-width) (window-width window))
156 ;; do nothing if another window would suffer
158 ((fboundp 'fit-window-to-buffer)
159 (fit-window-to-buffer window))
160 ((fboundp 'shrink-window-if-larger-than-buffer)
161 (shrink-window-if-larger-than-buffer window))))
163 ;; Region compatibility
165 (defvar org-ignore-region nil
166 "To temporarily disable the active region.")
168 (defun org-region-active-p ()
169 "Is `transient-mark-mode' on and the region active?
170 Works on both Emacs and XEmacs."
171 (if org-ignore-region
173 (if (featurep 'xemacs)
174 (and zmacs-regions (region-active-p))
175 (if (fboundp 'use-region-p)
176 (use-region-p)
177 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
179 ;; Invisibility compatibility
181 (defun org-add-to-invisibility-spec (arg)
182 "Add elements to `buffer-invisibility-spec'.
183 See documentation for `buffer-invisibility-spec' for the kind of elements
184 that can be added."
185 (cond
186 ((fboundp 'add-to-invisibility-spec)
187 (add-to-invisibility-spec arg))
188 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
189 (setq buffer-invisibility-spec (list arg)))
191 (setq buffer-invisibility-spec
192 (cons arg buffer-invisibility-spec)))))
194 (defun org-remove-from-invisibility-spec (arg)
195 "Remove elements from `buffer-invisibility-spec'."
196 (if (fboundp 'remove-from-invisibility-spec)
197 (remove-from-invisibility-spec arg)
198 (if (consp buffer-invisibility-spec)
199 (setq buffer-invisibility-spec
200 (delete arg buffer-invisibility-spec)))))
202 (defun org-in-invisibility-spec-p (arg)
203 "Is ARG a member of `buffer-invisibility-spec'?"
204 (if (consp buffer-invisibility-spec)
205 (member arg buffer-invisibility-spec)
206 nil))
208 (defun org-indent-to-column (column &optional minimum buffer)
209 "Work around a bug with extents with invisibility in XEmacs."
210 (if (featurep 'xemacs)
211 (let ((ext-inv (extent-list
212 nil (point-at-bol) (point-at-eol)
213 'all-extents-closed-open 'invisible))
214 ext-inv-specs)
215 (dolist (ext ext-inv)
216 (when (extent-property ext 'invisible)
217 (add-to-list 'ext-inv-specs (list ext (extent-property
218 ext 'invisible)))
219 (set-extent-property ext 'invisible nil)))
220 (indent-to-column column minimum buffer)
221 (dolist (ext-inv-spec ext-inv-specs)
222 (set-extent-property (car ext-inv-spec) 'invisible
223 (cadr ext-inv-spec))))
224 (indent-to-column column minimum)))
226 (defun org-indent-line-to (column)
227 "Work around a bug with extents with invisibility in XEmacs."
228 (if (featurep 'xemacs)
229 (let ((ext-inv (extent-list
230 nil (point-at-bol) (point-at-eol)
231 'all-extents-closed-open 'invisible))
232 ext-inv-specs)
233 (dolist (ext ext-inv)
234 (when (extent-property ext 'invisible)
235 (add-to-list 'ext-inv-specs (list ext (extent-property
236 ext 'invisible)))
237 (set-extent-property ext 'invisible nil)))
238 (indent-line-to column)
239 (dolist (ext-inv-spec ext-inv-specs)
240 (set-extent-property (car ext-inv-spec) 'invisible
241 (cadr ext-inv-spec))))
242 (indent-line-to column)))
244 (defun org-move-to-column (column &optional force buffer)
245 (if (featurep 'xemacs)
246 (let ((ext-inv (extent-list
247 nil (point-at-bol) (point-at-eol)
248 'all-extents-closed-open 'invisible))
249 ext-inv-specs)
250 (dolist (ext ext-inv)
251 (when (extent-property ext 'invisible)
252 (add-to-list 'ext-inv-specs (list ext (extent-property ext
253 'invisible)))
254 (set-extent-property ext 'invisible nil)))
255 (move-to-column column force buffer)
256 (dolist (ext-inv-spec ext-inv-specs)
257 (set-extent-property (car ext-inv-spec) 'invisible
258 (cadr ext-inv-spec))))
259 (move-to-column column force)))
261 (defun org-get-x-clipboard-compat (value)
262 "Get the clipboard value on XEmacs or Emacs 21"
263 (cond (org-xemacs-p (org-no-warnings (get-selection-no-error value)))
264 ((fboundp 'x-get-selection)
265 (condition-case nil
266 (or (x-get-selection value 'UTF8_STRING)
267 (x-get-selection value 'COMPOUND_TEXT)
268 (x-get-selection value 'STRING)
269 (x-get-selection value 'TEXT))
270 (error nil)))))
272 (defun org-propertize (string &rest properties)
273 (if (featurep 'xemacs)
274 (add-text-properties 0 (length string) properties string)
275 (apply 'propertize string properties)))
277 (provide 'org-compat)
279 ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
281 ;;; org-compat.el ends here