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