Release 6.13
[org-mode.git] / lisp / org-compat.el
blobf3707cdd4f8a541c8508a9ec816fd2eee2255891
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.13
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 max-height min-height
154 shrink-only)
155 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
156 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
157 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
158 `shrink-window-if-larger-than-buffer' instead, the hight limit are
159 ignored in this case."
160 (cond ((> (frame-width) (window-width window))
161 ;; do nothing if another window would suffer
163 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
164 (fit-window-to-buffer window max-height min-height))
165 ((fboundp 'shrink-window-if-larger-than-buffer)
166 (shrink-window-if-larger-than-buffer window)))
167 (or window (selected-window)))
169 ;; Region compatibility
171 (defvar org-ignore-region nil
172 "To temporarily disable the active region.")
174 (defun org-region-active-p ()
175 "Is `transient-mark-mode' on and the region active?
176 Works on both Emacs and XEmacs."
177 (if org-ignore-region
179 (if (featurep 'xemacs)
180 (and zmacs-regions (region-active-p))
181 (if (fboundp 'use-region-p)
182 (use-region-p)
183 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
185 ;; Invisibility compatibility
187 (defun org-add-to-invisibility-spec (arg)
188 "Add elements to `buffer-invisibility-spec'.
189 See documentation for `buffer-invisibility-spec' for the kind of elements
190 that can be added."
191 (cond
192 ((fboundp 'add-to-invisibility-spec)
193 (add-to-invisibility-spec arg))
194 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
195 (setq buffer-invisibility-spec (list arg)))
197 (setq buffer-invisibility-spec
198 (cons arg buffer-invisibility-spec)))))
200 (defun org-remove-from-invisibility-spec (arg)
201 "Remove elements from `buffer-invisibility-spec'."
202 (if (fboundp 'remove-from-invisibility-spec)
203 (remove-from-invisibility-spec arg)
204 (if (consp buffer-invisibility-spec)
205 (setq buffer-invisibility-spec
206 (delete arg buffer-invisibility-spec)))))
208 (defun org-in-invisibility-spec-p (arg)
209 "Is ARG a member of `buffer-invisibility-spec'?"
210 (if (consp buffer-invisibility-spec)
211 (member arg buffer-invisibility-spec)
212 nil))
214 (defun org-indent-to-column (column &optional minimum buffer)
215 "Work around a bug with extents with invisibility in XEmacs."
216 (if (featurep 'xemacs)
217 (let ((ext-inv (extent-list
218 nil (point-at-bol) (point-at-eol)
219 'all-extents-closed-open 'invisible))
220 ext-inv-specs)
221 (dolist (ext ext-inv)
222 (when (extent-property ext 'invisible)
223 (add-to-list 'ext-inv-specs (list ext (extent-property
224 ext 'invisible)))
225 (set-extent-property ext 'invisible nil)))
226 (indent-to-column column minimum buffer)
227 (dolist (ext-inv-spec ext-inv-specs)
228 (set-extent-property (car ext-inv-spec) 'invisible
229 (cadr ext-inv-spec))))
230 (indent-to-column column minimum)))
232 (defun org-indent-line-to (column)
233 "Work around a bug with extents with invisibility in XEmacs."
234 (if (featurep 'xemacs)
235 (let ((ext-inv (extent-list
236 nil (point-at-bol) (point-at-eol)
237 'all-extents-closed-open 'invisible))
238 ext-inv-specs)
239 (dolist (ext ext-inv)
240 (when (extent-property ext 'invisible)
241 (add-to-list 'ext-inv-specs (list ext (extent-property
242 ext 'invisible)))
243 (set-extent-property ext 'invisible nil)))
244 (indent-line-to column)
245 (dolist (ext-inv-spec ext-inv-specs)
246 (set-extent-property (car ext-inv-spec) 'invisible
247 (cadr ext-inv-spec))))
248 (indent-line-to column)))
250 (defun org-move-to-column (column &optional force buffer)
251 (if (featurep 'xemacs)
252 (let ((ext-inv (extent-list
253 nil (point-at-bol) (point-at-eol)
254 'all-extents-closed-open 'invisible))
255 ext-inv-specs)
256 (dolist (ext ext-inv)
257 (when (extent-property ext 'invisible)
258 (add-to-list 'ext-inv-specs (list ext (extent-property ext
259 'invisible)))
260 (set-extent-property ext 'invisible nil)))
261 (move-to-column column force buffer)
262 (dolist (ext-inv-spec ext-inv-specs)
263 (set-extent-property (car ext-inv-spec) 'invisible
264 (cadr ext-inv-spec))))
265 (move-to-column column force)))
267 (defun org-get-x-clipboard-compat (value)
268 "Get the clipboard value on XEmacs or Emacs 21"
269 (cond (org-xemacs-p (org-no-warnings (get-selection-no-error value)))
270 ((fboundp 'x-get-selection)
271 (condition-case nil
272 (or (x-get-selection value 'UTF8_STRING)
273 (x-get-selection value 'COMPOUND_TEXT)
274 (x-get-selection value 'STRING)
275 (x-get-selection value 'TEXT))
276 (error nil)))))
278 (defun org-propertize (string &rest properties)
279 (if (featurep 'xemacs)
280 (progn
281 (add-text-properties 0 (length string) properties string)
282 string)
283 (apply 'propertize string properties)))
285 (defun org-substring-no-properties (string &optional from to)
286 (if (featurep 'xemacs)
287 (org-no-properties (substring string from to))
288 (substring-no-properties string from to)))
290 (provide 'org-compat)
292 ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
294 ;;; org-compat.el ends here