Release 6.28e
[org-mode/org-tableheadings.git] / lisp / org-compat.el
blob617cd7bb2f3cde84f3ef8ae362b5779613d6765f
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.28e
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 (eval-when-compile
35 (require 'cl))
37 (require 'org-macs)
39 (declare-function find-library-name "find-func" (library))
41 (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
42 (defconst org-format-transports-properties-p
43 (let ((x "a"))
44 (add-text-properties 0 1 '(test t) x)
45 (get-text-property 0 'test (format "%s" x)))
46 "Does format transport text properties?")
48 (defun org-compatible-face (inherits specs)
49 "Make a compatible face specification.
50 If INHERITS is an existing face and if the Emacs version supports it,
51 just inherit the face. If INHERITS is set and the Emacs version does
52 not support it, copy the face specification from the inheritance face.
53 If INHERITS is not given and SPECS is, use SPECS to define the face.
54 XEmacs and Emacs 21 do not know about the `min-colors' attribute.
55 For them we convert a (min-colors 8) entry to a `tty' entry and move it
56 to the top of the list. The `min-colors' attribute will be removed from
57 any other entries, and any resulting duplicates will be removed entirely."
58 (when (and inherits (facep inherits) (not specs))
59 (setq specs (or specs
60 (get inherits 'saved-face)
61 (get inherits 'face-defface-spec))))
62 (cond
63 ((and inherits (facep inherits)
64 (not (featurep 'xemacs))
65 (>= emacs-major-version 22)
66 ;; do not inherit outline faces before Emacs 23
67 (or (>= emacs-major-version 23)
68 (not (string-match "\\`outline-[0-9]+"
69 (symbol-name inherits)))))
70 (list (list t :inherit inherits)))
71 ((or (featurep 'xemacs) (< emacs-major-version 22))
72 ;; These do not understand the `min-colors' attribute.
73 (let (r e a)
74 (while (setq e (pop specs))
75 (cond
76 ((memq (car e) '(t default)) (push e r))
77 ((setq a (member '(min-colors 8) (car e)))
78 (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
79 (cdr e)))))
80 ((setq a (assq 'min-colors (car e)))
81 (setq e (cons (delq a (car e)) (cdr e)))
82 (or (assoc (car e) r) (push e r)))
83 (t (or (assoc (car e) r) (push e r)))))
84 (nreverse r)))
85 (t specs)))
86 (put 'org-compatible-face 'lisp-indent-function 1)
88 ;;;; Emacs/XEmacs compatibility
90 ;; Overlay compatibility functions
91 (defun org-make-overlay (beg end &optional buffer)
92 (if (featurep 'xemacs)
93 (make-extent beg end buffer)
94 (make-overlay beg end buffer)))
95 (defun org-delete-overlay (ovl)
96 (if (featurep 'xemacs) (progn (delete-extent ovl) nil) (delete-overlay ovl)))
97 (defun org-detach-overlay (ovl)
98 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
99 (defun org-move-overlay (ovl beg end &optional buffer)
100 (if (featurep 'xemacs)
101 (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
102 (move-overlay ovl beg end buffer)))
103 (defun org-overlay-put (ovl prop value)
104 (if (featurep 'xemacs)
105 (set-extent-property ovl prop value)
106 (overlay-put ovl prop value)))
107 (defun org-overlay-display (ovl text &optional face evap)
108 "Make overlay OVL display TEXT with face FACE."
109 (if (featurep 'xemacs)
110 (let ((gl (make-glyph text)))
111 (and face (set-glyph-face gl face))
112 (set-extent-property ovl 'invisible t)
113 (set-extent-property ovl 'end-glyph gl))
114 (overlay-put ovl 'display text)
115 (if face (overlay-put ovl 'face face))
116 (if evap (overlay-put ovl 'evaporate t))))
117 (defun org-overlay-before-string (ovl text &optional face evap)
118 "Make overlay OVL display TEXT with face FACE."
119 (if (featurep 'xemacs)
120 (let ((gl (make-glyph text)))
121 (and face (set-glyph-face gl face))
122 (set-extent-property ovl 'begin-glyph gl))
123 (if face (org-add-props text nil 'face face))
124 (overlay-put ovl 'before-string text)
125 (if evap (overlay-put ovl 'evaporate t))))
126 (defun org-overlay-get (ovl prop)
127 (if (featurep 'xemacs)
128 (extent-property ovl prop)
129 (overlay-get ovl prop)))
130 (defun org-overlays-at (pos)
131 (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
132 (defun org-overlays-in (&optional start end)
133 (if (featurep 'xemacs)
134 (extent-list nil start end)
135 (overlays-in start end)))
136 (defun org-overlay-start (o)
137 (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
138 (defun org-overlay-end (o)
139 (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
140 (defun org-overlay-buffer (o)
141 (if (featurep 'xemacs) (extent-buffer o) (overlay-buffer o)))
142 (defun org-find-overlays (prop &optional pos delete)
143 "Find all overlays specifying PROP at POS or point.
144 If DELETE is non-nil, delete all those overlays."
145 (let ((overlays (org-overlays-at (or pos (point))))
146 ov found)
147 (while (setq ov (pop overlays))
148 (if (org-overlay-get ov prop)
149 (if delete (org-delete-overlay ov) (push ov found))))
150 found))
152 (defun org-add-hook (hook function &optional append local)
153 "Add-hook, compatible with both Emacsen."
154 (if (and local (featurep 'xemacs))
155 (add-local-hook hook function append)
156 (add-hook hook function append local)))
158 (defun org-add-props (string plist &rest props)
159 "Add text properties to entire string, from beginning to end.
160 PLIST may be a list of properties, PROPS are individual properties and values
161 that will be added to PLIST. Returns the string that was modified."
162 (add-text-properties
163 0 (length string) (if props (append plist props) plist) string)
164 string)
165 (put 'org-add-props 'lisp-indent-function 2)
167 (defun org-fit-window-to-buffer (&optional window max-height min-height
168 shrink-only)
169 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
170 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
171 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
172 `shrink-window-if-larger-than-buffer' instead, the hight limit are
173 ignored in this case."
174 (cond ((if (fboundp 'window-full-width-p)
175 (not (window-full-width-p window))
176 (> (frame-width) (window-width window)))
177 ;; do nothing if another window would suffer
179 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
180 (fit-window-to-buffer window max-height min-height))
181 ((fboundp 'shrink-window-if-larger-than-buffer)
182 (shrink-window-if-larger-than-buffer window)))
183 (or window (selected-window)))
185 ;; Region compatibility
187 (defvar org-ignore-region nil
188 "To temporarily disable the active region.")
190 (defun org-region-active-p ()
191 "Is `transient-mark-mode' on and the region active?
192 Works on both Emacs and XEmacs."
193 (if org-ignore-region
195 (if (featurep 'xemacs)
196 (and zmacs-regions (region-active-p))
197 (if (fboundp 'use-region-p)
198 (use-region-p)
199 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
201 (defun org-cursor-to-region-beginning ()
202 (when (and (org-region-active-p)
203 (> (point) (region-beginning)))
204 (exchange-point-and-mark)))
206 ;; Invisibility compatibility
208 (defun org-add-to-invisibility-spec (arg)
209 "Add elements to `buffer-invisibility-spec'.
210 See documentation for `buffer-invisibility-spec' for the kind of elements
211 that can be added."
212 (cond
213 ((fboundp 'add-to-invisibility-spec)
214 (add-to-invisibility-spec arg))
215 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
216 (setq buffer-invisibility-spec (list arg)))
218 (setq buffer-invisibility-spec
219 (cons arg buffer-invisibility-spec)))))
221 (defun org-remove-from-invisibility-spec (arg)
222 "Remove elements from `buffer-invisibility-spec'."
223 (if (fboundp 'remove-from-invisibility-spec)
224 (remove-from-invisibility-spec arg)
225 (if (consp buffer-invisibility-spec)
226 (setq buffer-invisibility-spec
227 (delete arg buffer-invisibility-spec)))))
229 (defun org-in-invisibility-spec-p (arg)
230 "Is ARG a member of `buffer-invisibility-spec'?"
231 (if (consp buffer-invisibility-spec)
232 (member arg buffer-invisibility-spec)
233 nil))
235 (defun org-indent-to-column (column &optional minimum buffer)
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-to-column column minimum buffer)
248 (dolist (ext-inv-spec ext-inv-specs)
249 (set-extent-property (car ext-inv-spec) 'invisible
250 (cadr ext-inv-spec))))
251 (indent-to-column column minimum)))
253 (defun org-indent-line-to (column)
254 "Work around a bug with extents with invisibility in XEmacs."
255 (if (featurep 'xemacs)
256 (let ((ext-inv (extent-list
257 nil (point-at-bol) (point-at-eol)
258 'all-extents-closed-open 'invisible))
259 ext-inv-specs)
260 (dolist (ext ext-inv)
261 (when (extent-property ext 'invisible)
262 (add-to-list 'ext-inv-specs (list ext (extent-property
263 ext 'invisible)))
264 (set-extent-property ext 'invisible nil)))
265 (indent-line-to column)
266 (dolist (ext-inv-spec ext-inv-specs)
267 (set-extent-property (car ext-inv-spec) 'invisible
268 (cadr ext-inv-spec))))
269 (indent-line-to column)))
271 (defun org-move-to-column (column &optional force buffer)
272 (if (featurep 'xemacs)
273 (let ((ext-inv (extent-list
274 nil (point-at-bol) (point-at-eol)
275 'all-extents-closed-open 'invisible))
276 ext-inv-specs)
277 (dolist (ext ext-inv)
278 (when (extent-property ext 'invisible)
279 (add-to-list 'ext-inv-specs (list ext (extent-property ext
280 'invisible)))
281 (set-extent-property ext 'invisible nil)))
282 (move-to-column column force buffer)
283 (dolist (ext-inv-spec ext-inv-specs)
284 (set-extent-property (car ext-inv-spec) 'invisible
285 (cadr ext-inv-spec))))
286 (move-to-column column force)))
288 (defun org-get-x-clipboard-compat (value)
289 "Get the clipboard value on XEmacs or Emacs 21"
290 (cond (org-xemacs-p (org-no-warnings (get-selection-no-error value)))
291 ((fboundp 'x-get-selection)
292 (condition-case nil
293 (or (x-get-selection value 'UTF8_STRING)
294 (x-get-selection value 'COMPOUND_TEXT)
295 (x-get-selection value 'STRING)
296 (x-get-selection value 'TEXT))
297 (error nil)))))
299 (defun org-propertize (string &rest properties)
300 (if (featurep 'xemacs)
301 (progn
302 (add-text-properties 0 (length string) properties string)
303 string)
304 (apply 'propertize string properties)))
306 (defun org-substring-no-properties (string &optional from to)
307 (if (featurep 'xemacs)
308 (org-no-properties (substring string (or from 0) to))
309 (substring-no-properties string from to)))
311 (defun org-find-library-name (library)
312 (if (fboundp 'find-library-name)
313 (file-name-directory (find-library-name library))
314 ; XEmacs does not have `find-library-name'
315 (flet ((find-library-name-helper (filename ignored-codesys)
316 filename)
317 (find-library-name (library)
318 (find-library library nil 'find-library-name-helper)))
319 (file-name-directory (find-library-name library)))))
321 (defun org-count-lines (s)
322 "How many lines in string S?"
323 (let ((start 0) (n 1))
324 (while (string-match "\n" s start)
325 (setq start (match-end 0) n (1+ n)))
326 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
327 (setq n (1- n)))
330 (provide 'org-compat)
332 ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
334 ;;; org-compat.el ends here