1 ;;; org-compat.el --- Compatibility code for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains code needed for compatibility with XEmacs and older
29 ;; versions of GNU Emacs.
35 (defconst org-xemacs-p
(featurep 'xemacs
)) ; not used by org.el itself
36 (defconst org-format-transports-properties-p
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."
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.
60 (while (setq e
(pop specs
))
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
)))
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
)))))
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))))
133 (while (setq ov
(pop overlays
))
134 (if (org-overlay-get ov prop
)
135 (if delete
(org-delete-overlay ov
) (push ov 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."
149 0 (length string
) (if props
(append plist props
) plist
) string
)
151 (put 'org-add-props
'lisp-indent-function
2)
153 (defun org-fit-window-to-buffer (&optional window max-height min-height
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 ((if (fboundp 'window-full-width-p
)
161 (not (window-full-width-p window
))
162 (> (frame-width) (window-width window
)))
163 ;; do nothing if another window would suffer
165 ((and (fboundp 'fit-window-to-buffer
) (not shrink-only
))
166 (fit-window-to-buffer window max-height min-height
))
167 ((fboundp 'shrink-window-if-larger-than-buffer
)
168 (shrink-window-if-larger-than-buffer window
)))
169 (or window
(selected-window)))
171 ;; Region compatibility
173 (defvar org-ignore-region nil
174 "To temporarily disable the active region.")
176 (defun org-region-active-p ()
177 "Is `transient-mark-mode' on and the region active?
178 Works on both Emacs and XEmacs."
179 (if org-ignore-region
181 (if (featurep 'xemacs
)
182 (and zmacs-regions
(region-active-p))
183 (if (fboundp 'use-region-p
)
185 (and transient-mark-mode mark-active
))))) ; Emacs 22 and before
187 ;; Invisibility compatibility
189 (defun org-add-to-invisibility-spec (arg)
190 "Add elements to `buffer-invisibility-spec'.
191 See documentation for `buffer-invisibility-spec' for the kind of elements
194 ((fboundp 'add-to-invisibility-spec
)
195 (add-to-invisibility-spec arg
))
196 ((or (null buffer-invisibility-spec
) (eq buffer-invisibility-spec t
))
197 (setq buffer-invisibility-spec
(list arg
)))
199 (setq buffer-invisibility-spec
200 (cons arg buffer-invisibility-spec
)))))
202 (defun org-remove-from-invisibility-spec (arg)
203 "Remove elements from `buffer-invisibility-spec'."
204 (if (fboundp 'remove-from-invisibility-spec
)
205 (remove-from-invisibility-spec arg
)
206 (if (consp buffer-invisibility-spec
)
207 (setq buffer-invisibility-spec
208 (delete arg buffer-invisibility-spec
)))))
210 (defun org-in-invisibility-spec-p (arg)
211 "Is ARG a member of `buffer-invisibility-spec'?"
212 (if (consp buffer-invisibility-spec
)
213 (member arg buffer-invisibility-spec
)
216 (defun org-indent-to-column (column &optional minimum buffer
)
217 "Work around a bug with extents with invisibility in XEmacs."
218 (if (featurep 'xemacs
)
219 (let ((ext-inv (extent-list
220 nil
(point-at-bol) (point-at-eol)
221 'all-extents-closed-open
'invisible
))
223 (dolist (ext ext-inv
)
224 (when (extent-property ext
'invisible
)
225 (add-to-list 'ext-inv-specs
(list ext
(extent-property
227 (set-extent-property ext
'invisible nil
)))
228 (indent-to-column column minimum buffer
)
229 (dolist (ext-inv-spec ext-inv-specs
)
230 (set-extent-property (car ext-inv-spec
) 'invisible
231 (cadr ext-inv-spec
))))
232 (indent-to-column column minimum
)))
234 (defun org-indent-line-to (column)
235 "Work around a bug with extents with invisibility in XEmacs."
236 (if (featurep 'xemacs
)
237 (let ((ext-inv (extent-list
238 nil
(point-at-bol) (point-at-eol)
239 'all-extents-closed-open
'invisible
))
241 (dolist (ext ext-inv
)
242 (when (extent-property ext
'invisible
)
243 (add-to-list 'ext-inv-specs
(list ext
(extent-property
245 (set-extent-property ext
'invisible nil
)))
246 (indent-line-to column
)
247 (dolist (ext-inv-spec ext-inv-specs
)
248 (set-extent-property (car ext-inv-spec
) 'invisible
249 (cadr ext-inv-spec
))))
250 (indent-line-to column
)))
252 (defun org-move-to-column (column &optional force buffer
)
253 (if (featurep 'xemacs
)
254 (let ((ext-inv (extent-list
255 nil
(point-at-bol) (point-at-eol)
256 'all-extents-closed-open
'invisible
))
258 (dolist (ext ext-inv
)
259 (when (extent-property ext
'invisible
)
260 (add-to-list 'ext-inv-specs
(list ext
(extent-property ext
262 (set-extent-property ext
'invisible nil
)))
263 (move-to-column column force buffer
)
264 (dolist (ext-inv-spec ext-inv-specs
)
265 (set-extent-property (car ext-inv-spec
) 'invisible
266 (cadr ext-inv-spec
))))
267 (move-to-column column force
)))
269 (defun org-get-x-clipboard-compat (value)
270 "Get the clipboard value on XEmacs or Emacs 21"
271 (cond (org-xemacs-p (org-no-warnings (get-selection-no-error value
)))
272 ((fboundp 'x-get-selection
)
274 (or (x-get-selection value
'UTF8_STRING
)
275 (x-get-selection value
'COMPOUND_TEXT
)
276 (x-get-selection value
'STRING
)
277 (x-get-selection value
'TEXT
))
280 (defun org-propertize (string &rest properties
)
281 (if (featurep 'xemacs
)
283 (add-text-properties 0 (length string
) properties string
)
285 (apply 'propertize string properties
)))
287 (defun org-substring-no-properties (string &optional from to
)
288 (if (featurep 'xemacs
)
289 (org-no-properties (substring string
(or from
0) to
))
290 (substring-no-properties string from to
)))
292 (defun org-count-lines (s)
293 "How many lines in string S?"
294 (let ((start 0) (n 1))
295 (while (string-match "\n" s start
)
296 (setq start
(match-end 0) n
(1+ n
)))
297 (if (and (> (length s
) 0) (= (aref s
(1- (length s
))) ?
\n))
301 (provide 'org-compat
)
303 ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
305 ;;; org-compat.el ends here