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