No linger bind obsolete calendar variables
[org-mode.git] / lisp / org-compat.el
blobe226923d382244732d1bbf3ccb9e06e4d39fccb0
1 ;;; org-compat.el --- Compatibility code for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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.35trans
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))
40 (declare-function w32-focus-frame "term/w32-win" (frame))
42 (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
43 (defconst org-format-transports-properties-p
44 (let ((x "a"))
45 (add-text-properties 0 1 '(test t) x)
46 (get-text-property 0 'test (format "%s" x)))
47 "Does format transport text properties?")
49 (defun org-compatible-face (inherits specs)
50 "Make a compatible face specification.
51 If INHERITS is an existing face and if the Emacs version supports it,
52 just inherit the face. If INHERITS is set and the Emacs version does
53 not support it, copy the face specification from the inheritance face.
54 If INHERITS is not given and SPECS is, use SPECS to define the face.
55 XEmacs and Emacs 21 do not know about the `min-colors' attribute.
56 For them we convert a (min-colors 8) entry to a `tty' entry and move it
57 to the top of the list. The `min-colors' attribute will be removed from
58 any other entries, and any resulting duplicates will be removed entirely."
59 (when (and inherits (facep inherits) (not specs))
60 (setq specs (or specs
61 (get inherits 'saved-face)
62 (get inherits 'face-defface-spec))))
63 (cond
64 ((and inherits (facep inherits)
65 (not (featurep 'xemacs))
66 (>= emacs-major-version 22)
67 ;; do not inherit outline faces before Emacs 23
68 (or (>= emacs-major-version 23)
69 (not (string-match "\\`outline-[0-9]+"
70 (symbol-name inherits)))))
71 (list (list t :inherit inherits)))
72 ((or (featurep 'xemacs) (< emacs-major-version 22))
73 ;; These do not understand the `min-colors' attribute.
74 (let (r e a)
75 (while (setq e (pop specs))
76 (cond
77 ((memq (car e) '(t default)) (push e r))
78 ((setq a (member '(min-colors 8) (car e)))
79 (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
80 (cdr e)))))
81 ((setq a (assq 'min-colors (car e)))
82 (setq e (cons (delq a (car e)) (cdr e)))
83 (or (assoc (car e) r) (push e r)))
84 (t (or (assoc (car e) r) (push e r)))))
85 (nreverse r)))
86 (t specs)))
87 (put 'org-compatible-face 'lisp-indent-function 1)
89 ;;;; Emacs/XEmacs compatibility
91 ;; Overlay compatibility functions
92 (defun org-detach-overlay (ovl)
93 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
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-overlays-at (pos)
114 (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
115 (defun org-overlays-in (&optional start end)
116 (if (featurep 'xemacs)
117 (extent-list nil start end)
118 (overlays-in start end)))
119 (defun org-find-overlays (prop &optional pos delete)
120 "Find all overlays specifying PROP at POS or point.
121 If DELETE is non-nil, delete all those overlays."
122 (let ((overlays (org-overlays-at (or pos (point))))
123 ov found)
124 (while (setq ov (pop overlays))
125 (if (overlay-get ov prop)
126 (if delete (delete-overlay ov) (push ov found))))
127 found))
129 (defun org-add-hook (hook function &optional append local)
130 "Add-hook, compatible with both Emacsen."
131 (if (and local (featurep 'xemacs))
132 (add-local-hook hook function append)
133 (add-hook hook function append local)))
135 (defun org-add-props (string plist &rest props)
136 "Add text properties to entire string, from beginning to end.
137 PLIST may be a list of properties, PROPS are individual properties and values
138 that will be added to PLIST. Returns the string that was modified."
139 (add-text-properties
140 0 (length string) (if props (append plist props) plist) string)
141 string)
142 (put 'org-add-props 'lisp-indent-function 2)
144 (defun org-fit-window-to-buffer (&optional window max-height min-height
145 shrink-only)
146 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
147 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
148 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
149 `shrink-window-if-larger-than-buffer' instead, the hight limit are
150 ignored in this case."
151 (cond ((if (fboundp 'window-full-width-p)
152 (not (window-full-width-p window))
153 (> (frame-width) (window-width window)))
154 ;; do nothing if another window would suffer
156 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
157 (fit-window-to-buffer window max-height min-height))
158 ((fboundp 'shrink-window-if-larger-than-buffer)
159 (shrink-window-if-larger-than-buffer window)))
160 (or window (selected-window)))
162 ;; Region compatibility
164 (defvar org-ignore-region nil
165 "To temporarily disable the active region.")
167 (defun org-region-active-p ()
168 "Is `transient-mark-mode' on and the region active?
169 Works on both Emacs and XEmacs."
170 (if org-ignore-region
172 (if (featurep 'xemacs)
173 (and zmacs-regions (region-active-p))
174 (if (fboundp 'use-region-p)
175 (use-region-p)
176 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
178 (defun org-cursor-to-region-beginning ()
179 (when (and (org-region-active-p)
180 (> (point) (region-beginning)))
181 (exchange-point-and-mark)))
183 ;; Invisibility compatibility
185 (defun org-add-to-invisibility-spec (arg)
186 "Add elements to `buffer-invisibility-spec'.
187 See documentation for `buffer-invisibility-spec' for the kind of elements
188 that can be added."
189 (cond
190 ((fboundp 'add-to-invisibility-spec)
191 (add-to-invisibility-spec arg))
192 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
193 (setq buffer-invisibility-spec (list arg)))
195 (setq buffer-invisibility-spec
196 (cons arg buffer-invisibility-spec)))))
198 (defun org-remove-from-invisibility-spec (arg)
199 "Remove elements from `buffer-invisibility-spec'."
200 (if (fboundp 'remove-from-invisibility-spec)
201 (remove-from-invisibility-spec arg)
202 (if (consp buffer-invisibility-spec)
203 (setq buffer-invisibility-spec
204 (delete arg buffer-invisibility-spec)))))
206 (defun org-in-invisibility-spec-p (arg)
207 "Is ARG a member of `buffer-invisibility-spec'?"
208 (if (consp buffer-invisibility-spec)
209 (member arg buffer-invisibility-spec)
210 nil))
212 (defun org-indent-to-column (column &optional minimum buffer)
213 "Work around a bug with extents with invisibility in XEmacs."
214 (if (featurep 'xemacs)
215 (let ((ext-inv (extent-list
216 nil (point-at-bol) (point-at-eol)
217 'all-extents-closed-open 'invisible))
218 ext-inv-specs)
219 (dolist (ext ext-inv)
220 (when (extent-property ext 'invisible)
221 (add-to-list 'ext-inv-specs (list ext (extent-property
222 ext 'invisible)))
223 (set-extent-property ext 'invisible nil)))
224 (indent-to-column column minimum buffer)
225 (dolist (ext-inv-spec ext-inv-specs)
226 (set-extent-property (car ext-inv-spec) 'invisible
227 (cadr ext-inv-spec))))
228 (indent-to-column column minimum)))
230 (defun org-indent-line-to (column)
231 "Work around a bug with extents with invisibility in XEmacs."
232 (if (featurep 'xemacs)
233 (let ((ext-inv (extent-list
234 nil (point-at-bol) (point-at-eol)
235 'all-extents-closed-open 'invisible))
236 ext-inv-specs)
237 (dolist (ext ext-inv)
238 (when (extent-property ext 'invisible)
239 (add-to-list 'ext-inv-specs (list ext (extent-property
240 ext 'invisible)))
241 (set-extent-property ext 'invisible nil)))
242 (indent-line-to column)
243 (dolist (ext-inv-spec ext-inv-specs)
244 (set-extent-property (car ext-inv-spec) 'invisible
245 (cadr ext-inv-spec))))
246 (indent-line-to column)))
248 (defun org-move-to-column (column &optional force buffer)
249 (if (featurep 'xemacs)
250 (let ((ext-inv (extent-list
251 nil (point-at-bol) (point-at-eol)
252 'all-extents-closed-open 'invisible))
253 ext-inv-specs)
254 (dolist (ext ext-inv)
255 (when (extent-property ext 'invisible)
256 (add-to-list 'ext-inv-specs (list ext (extent-property ext
257 'invisible)))
258 (set-extent-property ext 'invisible nil)))
259 (move-to-column column force buffer)
260 (dolist (ext-inv-spec ext-inv-specs)
261 (set-extent-property (car ext-inv-spec) 'invisible
262 (cadr ext-inv-spec))))
263 (move-to-column column force)))
265 (defun org-get-x-clipboard-compat (value)
266 "Get the clipboard value on XEmacs or Emacs 21"
267 (cond (org-xemacs-p (org-no-warnings (get-selection-no-error value)))
268 ((fboundp 'x-get-selection)
269 (condition-case nil
270 (or (x-get-selection value 'UTF8_STRING)
271 (x-get-selection value 'COMPOUND_TEXT)
272 (x-get-selection value 'STRING)
273 (x-get-selection value 'TEXT))
274 (error nil)))))
276 (defun org-propertize (string &rest properties)
277 (if (featurep 'xemacs)
278 (progn
279 (add-text-properties 0 (length string) properties string)
280 string)
281 (apply 'propertize string properties)))
283 (defun org-substring-no-properties (string &optional from to)
284 (if (featurep 'xemacs)
285 (org-no-properties (substring string (or from 0) to))
286 (substring-no-properties string from to)))
288 (defun org-find-library-name (library)
289 (if (fboundp 'find-library-name)
290 (file-name-directory (find-library-name library))
291 ; XEmacs does not have `find-library-name'
292 (flet ((find-library-name-helper (filename ignored-codesys)
293 filename)
294 (find-library-name (library)
295 (find-library library nil 'find-library-name-helper)))
296 (file-name-directory (find-library-name library)))))
298 (defun org-count-lines (s)
299 "How many lines in string S?"
300 (let ((start 0) (n 1))
301 (while (string-match "\n" s start)
302 (setq start (match-end 0) n (1+ n)))
303 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
304 (setq n (1- n)))
307 (defun org-kill-new (string &rest args)
308 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
309 string)
310 (apply 'kill-new string args))
312 (defun org-select-frame-set-input-focus (frame)
313 "Select FRAME, raise it, and set input focus, if possible."
314 (cond ((featurep 'xemacs)
315 (if (fboundp 'select-frame-set-input-focus)
316 (select-frame-set-input-focus frame)
317 (raise-frame frame)
318 (select-frame frame)
319 (focus-frame frame)))
320 ;; `select-frame-set-input-focus' defined in Emacs 21 will not
321 ;; set the input focus.
322 ((>= emacs-major-version 22)
323 (select-frame-set-input-focus frame))
325 (raise-frame frame)
326 (select-frame frame)
327 (cond ((memq window-system '(x ns mac))
328 (x-focus-frame frame))
329 ((eq window-system 'w32)
330 (w32-focus-frame frame)))
331 (when focus-follows-mouse
332 (set-mouse-position frame (1- (frame-width frame)) 0)))))
334 (defun org-float-time (&optional time)
335 "Convert time value TIME to a floating point number.
336 TIME defaults to the current time."
337 (if (featurep 'xemacs)
338 (time-to-seconds (or time (current-time)))
339 (float-time time)))
341 ; XEmacs does not have `looking-back'.
342 (if (fboundp 'looking-back)
343 (defalias 'org-looking-back 'looking-back)
344 (defun org-looking-back (regexp &optional limit greedy)
345 "Return non-nil if text before point matches regular expression REGEXP.
346 Like `looking-at' except matches before point, and is slower.
347 LIMIT if non-nil speeds up the search by specifying a minimum
348 starting position, to avoid checking matches that would start
349 before LIMIT.
351 If GREEDY is non-nil, extend the match backwards as far as
352 possible, stopping when a single additional previous character
353 cannot be part of a match for REGEXP. When the match is
354 extended, its starting position is allowed to occur before
355 LIMIT."
356 (let ((start (point))
357 (pos
358 (save-excursion
359 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
360 (point)))))
361 (if (and greedy pos)
362 (save-restriction
363 (narrow-to-region (point-min) start)
364 (while (and (> pos (point-min))
365 (save-excursion
366 (goto-char pos)
367 (backward-char 1)
368 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
369 (setq pos (1- pos)))
370 (save-excursion
371 (goto-char pos)
372 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
373 (not (null pos)))))
375 (provide 'org-compat)
377 ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
379 ;;; org-compat.el ends here