New option `org-properties-postprocess-alist'.
[org-mode.git] / lisp / org-compat.el
blob75fea240a352133905ade9c9cbcf1be45ae401c5
1 ;;; org-compat.el --- Compatibility code for Org-mode
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.7
9 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains code needed for compatibility with XEmacs and older
29 ;; versions of GNU Emacs.
31 ;;; Code:
33 (eval-when-compile
34 (require 'cl))
36 (require 'org-macs)
38 (declare-function find-library-name "find-func" (library))
39 (declare-function w32-focus-frame "term/w32-win" (frame))
41 ;; The following constant is for backward compatibility. We do not use
42 ;; it in org-mode, because the Byte compiler evaluates (featurep 'xemacs)
43 ;; at compilation time and can therefore optimize code better.
44 (defconst org-xemacs-p (featurep 'xemacs))
45 (defconst org-format-transports-properties-p
46 (let ((x "a"))
47 (add-text-properties 0 1 '(test t) x)
48 (get-text-property 0 'test (format "%s" x)))
49 "Does format transport text properties?")
51 (defun org-compatible-face (inherits specs)
52 "Make a compatible face specification.
53 If INHERITS is an existing face and if the Emacs version supports it,
54 just inherit the face. If INHERITS is set and the Emacs version does
55 not support it, copy the face specification from the inheritance face.
56 If INHERITS is not given and SPECS is, use SPECS to define the face.
57 XEmacs and Emacs 21 do not know about the `min-colors' attribute.
58 For them we convert a (min-colors 8) entry to a `tty' entry and move it
59 to the top of the list. The `min-colors' attribute will be removed from
60 any other entries, and any resulting duplicates will be removed entirely."
61 (when (and inherits (facep inherits) (not specs))
62 (setq specs (or specs
63 (get inherits 'saved-face)
64 (get inherits 'face-defface-spec))))
65 (cond
66 ((and inherits (facep inherits)
67 (not (featurep 'xemacs))
68 (>= emacs-major-version 22)
69 ;; do not inherit outline faces before Emacs 23
70 (or (>= emacs-major-version 23)
71 (not (string-match "\\`outline-[0-9]+"
72 (symbol-name inherits)))))
73 (list (list t :inherit inherits)))
74 ((or (featurep 'xemacs) (< emacs-major-version 22))
75 ;; These do not understand the `min-colors' attribute.
76 (let (r e a)
77 (while (setq e (pop specs))
78 (cond
79 ((memq (car e) '(t default)) (push e r))
80 ((setq a (member '(min-colors 8) (car e)))
81 (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
82 (cdr e)))))
83 ((setq a (assq 'min-colors (car e)))
84 (setq e (cons (delq a (car e)) (cdr e)))
85 (or (assoc (car e) r) (push e r)))
86 (t (or (assoc (car e) r) (push e r)))))
87 (nreverse r)))
88 (t specs)))
89 (put 'org-compatible-face 'lisp-indent-function 1)
91 (defun org-version-check (version feature level)
92 (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
93 (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
94 (rmaj (or (nth 0 v1) 99))
95 (rmin (or (nth 1 v1) 99))
96 (rbld (or (nth 2 v1) 99))
97 (maj (or (nth 0 v2) 0))
98 (min (or (nth 1 v2) 0))
99 (bld (or (nth 2 v2) 0)))
100 (if (or (< maj rmaj)
101 (and (= maj rmaj)
102 (< min rmin))
103 (and (= maj rmaj)
104 (= min rmin)
105 (< bld rbld)))
106 (if (eq level :predicate)
107 ;; just return if we have the version
109 (let ((msg (format "Emacs %s or greater is recommended for %s"
110 version feature)))
111 (display-warning 'org msg level)
113 t)))
115 ;;;; Emacs/XEmacs compatibility
117 ;; Keys
118 (defconst org-xemacs-key-equivalents
119 '(([mouse-1] . [button1])
120 ([mouse-2] . [button2])
121 ([mouse-3] . [button3])
122 ([C-mouse-4] . [(control mouse-4)])
123 ([C-mouse-5] . [(control mouse-5)]))
124 "Translation alist for a couple of keys.")
126 ;; Overlay compatibility functions
127 (defun org-detach-overlay (ovl)
128 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
129 (defun org-overlay-display (ovl text &optional face evap)
130 "Make overlay OVL display TEXT with face FACE."
131 (if (featurep 'xemacs)
132 (let ((gl (make-glyph text)))
133 (and face (set-glyph-face gl face))
134 (set-extent-property ovl 'invisible t)
135 (set-extent-property ovl 'end-glyph gl))
136 (overlay-put ovl 'display text)
137 (if face (overlay-put ovl 'face face))
138 (if evap (overlay-put ovl 'evaporate t))))
139 (defun org-overlay-before-string (ovl text &optional face evap)
140 "Make overlay OVL display TEXT with face FACE."
141 (if (featurep 'xemacs)
142 (let ((gl (make-glyph text)))
143 (and face (set-glyph-face gl face))
144 (set-extent-property ovl 'begin-glyph gl))
145 (if face (org-add-props text nil 'face face))
146 (overlay-put ovl 'before-string text)
147 (if evap (overlay-put ovl 'evaporate t))))
148 (defun org-find-overlays (prop &optional pos delete)
149 "Find all overlays specifying PROP at POS or point.
150 If DELETE is non-nil, delete all those overlays."
151 (let ((overlays (overlays-at (or pos (point))))
152 ov found)
153 (while (setq ov (pop overlays))
154 (if (overlay-get ov prop)
155 (if delete (delete-overlay ov) (push ov found))))
156 found))
158 (defun org-get-x-clipboard (value)
159 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
160 (if (eq window-system 'x)
161 (let ((x (org-get-x-clipboard-compat value)))
162 (if x (org-no-properties x)))))
164 (defsubst org-decompose-region (beg end)
165 "Decompose from BEG to END."
166 (if (featurep 'xemacs)
167 (let ((modified-p (buffer-modified-p))
168 (buffer-read-only nil))
169 (remove-text-properties beg end '(composition nil))
170 (set-buffer-modified-p modified-p))
171 (decompose-region beg end)))
173 ;; Miscellaneous functions
175 (defun org-add-hook (hook function &optional append local)
176 "Add-hook, compatible with both Emacsen."
177 (if (and local (featurep 'xemacs))
178 (add-local-hook hook function append)
179 (add-hook hook function append local)))
181 (defun org-add-props (string plist &rest props)
182 "Add text properties to entire string, from beginning to end.
183 PLIST may be a list of properties, PROPS are individual properties and values
184 that will be added to PLIST. Returns the string that was modified."
185 (add-text-properties
186 0 (length string) (if props (append plist props) plist) string)
187 string)
188 (put 'org-add-props 'lisp-indent-function 2)
190 (defun org-fit-window-to-buffer (&optional window max-height min-height
191 shrink-only)
192 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
193 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
194 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
195 `shrink-window-if-larger-than-buffer' instead, the height limit is
196 ignored in this case."
197 (cond ((if (fboundp 'window-full-width-p)
198 (not (window-full-width-p window))
199 (> (frame-width) (window-width window)))
200 ;; do nothing if another window would suffer
202 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
203 (fit-window-to-buffer window max-height min-height))
204 ((fboundp 'shrink-window-if-larger-than-buffer)
205 (shrink-window-if-larger-than-buffer window)))
206 (or window (selected-window)))
208 (defun org-number-sequence (from &optional to inc)
209 "Call `number-sequence or emulate it."
210 (if (fboundp 'number-sequence)
211 (number-sequence from to inc)
212 (if (or (not to) (= from to))
213 (list from)
214 (or inc (setq inc 1))
215 (when (zerop inc) (error "The increment can not be zero"))
216 (let (seq (n 0) (next from))
217 (if (> inc 0)
218 (while (<= next to)
219 (setq seq (cons next seq)
220 n (1+ n)
221 next (+ from (* n inc))))
222 (while (>= next to)
223 (setq seq (cons next seq)
224 n (1+ n)
225 next (+ from (* n inc)))))
226 (nreverse seq)))))
228 ;; Region compatibility
230 (defvar org-ignore-region nil
231 "To temporarily disable the active region.")
233 (defun org-region-active-p ()
234 "Is `transient-mark-mode' on and the region active?
235 Works on both Emacs and XEmacs."
236 (if org-ignore-region
238 (if (featurep 'xemacs)
239 (and zmacs-regions (region-active-p))
240 (if (fboundp 'use-region-p)
241 (use-region-p)
242 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
244 (defun org-cursor-to-region-beginning ()
245 (when (and (org-region-active-p)
246 (> (point) (region-beginning)))
247 (exchange-point-and-mark)))
249 ;; Emacs 22 misses `activate-mark'
250 (if (fboundp 'activate-mark)
251 (defalias 'org-activate-mark 'activate-mark)
252 (defun org-activate-mark ()
253 (when (mark t)
254 (setq mark-active t)
255 (unless transient-mark-mode
256 (setq transient-mark-mode 'lambda)))))
258 ;; Invisibility compatibility
260 (defun org-remove-from-invisibility-spec (arg)
261 "Remove elements from `buffer-invisibility-spec'."
262 (if (fboundp 'remove-from-invisibility-spec)
263 (remove-from-invisibility-spec arg)
264 (if (consp buffer-invisibility-spec)
265 (setq buffer-invisibility-spec
266 (delete arg buffer-invisibility-spec)))))
268 (defun org-in-invisibility-spec-p (arg)
269 "Is ARG a member of `buffer-invisibility-spec'?"
270 (if (consp buffer-invisibility-spec)
271 (member arg buffer-invisibility-spec)
272 nil))
274 (defmacro org-xemacs-without-invisibility (&rest body)
275 "Turn off exents with invisibility while executing BODY."
276 `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
277 'all-extents-closed-open 'invisible))
278 ext-inv-specs)
279 (dolist (ext ext-inv)
280 (when (extent-property ext 'invisible)
281 (add-to-list 'ext-inv-specs (list ext (extent-property
282 ext 'invisible)))
283 (set-extent-property ext 'invisible nil)))
284 ,@body
285 (dolist (ext-inv-spec ext-inv-specs)
286 (set-extent-property (car ext-inv-spec) 'invisible
287 (cadr ext-inv-spec)))))
288 (def-edebug-spec org-xemacs-without-invisibility (body))
290 (defun org-indent-to-column (column &optional minimum buffer)
291 "Work around a bug with extents with invisibility in XEmacs."
292 (if (featurep 'xemacs)
293 (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
294 (indent-to-column column minimum)))
296 (defun org-indent-line-to (column)
297 "Work around a bug with extents with invisibility in XEmacs."
298 (if (featurep 'xemacs)
299 (org-xemacs-without-invisibility (indent-line-to column))
300 (indent-line-to column)))
302 (defun org-move-to-column (column &optional force buffer)
303 (if (featurep 'xemacs)
304 (org-xemacs-without-invisibility (move-to-column column force buffer))
305 (move-to-column column force)))
307 (defun org-get-x-clipboard-compat (value)
308 "Get the clipboard value on XEmacs or Emacs 21."
309 (cond ((featurep 'xemacs)
310 (org-no-warnings (get-selection-no-error value)))
311 ((fboundp 'x-get-selection)
312 (condition-case nil
313 (or (x-get-selection value 'UTF8_STRING)
314 (x-get-selection value 'COMPOUND_TEXT)
315 (x-get-selection value 'STRING)
316 (x-get-selection value 'TEXT))
317 (error nil)))))
319 (defun org-propertize (string &rest properties)
320 (if (featurep 'xemacs)
321 (progn
322 (add-text-properties 0 (length string) properties string)
323 string)
324 (apply 'propertize string properties)))
326 (defun org-substring-no-properties (string &optional from to)
327 (if (featurep 'xemacs)
328 (org-no-properties (substring string (or from 0) to))
329 (substring-no-properties string from to)))
331 (defun org-find-library-name (library)
332 (if (fboundp 'find-library-name)
333 (file-name-directory (find-library-name library))
334 ; XEmacs does not have `find-library-name'
335 (flet ((find-library-name-helper (filename ignored-codesys)
336 filename)
337 (find-library-name (library)
338 (find-library library nil 'find-library-name-helper)))
339 (file-name-directory (find-library-name library)))))
341 (defun org-count-lines (s)
342 "How many lines in string S?"
343 (let ((start 0) (n 1))
344 (while (string-match "\n" s start)
345 (setq start (match-end 0) n (1+ n)))
346 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
347 (setq n (1- n)))
350 (defun org-kill-new (string &rest args)
351 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
352 string)
353 (apply 'kill-new string args))
355 (defun org-select-frame-set-input-focus (frame)
356 "Select FRAME, raise it, and set input focus, if possible."
357 (cond ((featurep 'xemacs)
358 (if (fboundp 'select-frame-set-input-focus)
359 (select-frame-set-input-focus frame)
360 (raise-frame frame)
361 (select-frame frame)
362 (focus-frame frame)))
363 ;; `select-frame-set-input-focus' defined in Emacs 21 will not
364 ;; set the input focus.
365 ((>= emacs-major-version 22)
366 (select-frame-set-input-focus frame))
368 (raise-frame frame)
369 (select-frame frame)
370 (cond ((memq window-system '(x ns mac))
371 (x-focus-frame frame))
372 ((eq window-system 'w32)
373 (w32-focus-frame frame)))
374 (when focus-follows-mouse
375 (set-mouse-position frame (1- (frame-width frame)) 0)))))
377 (defun org-float-time (&optional time)
378 "Convert time value TIME to a floating point number.
379 TIME defaults to the current time."
380 (if (featurep 'xemacs)
381 (time-to-seconds (or time (current-time)))
382 (float-time time)))
384 (if (fboundp 'string-match-p)
385 (defalias 'org-string-match-p 'string-match-p)
386 (defun org-string-match-p (regexp string &optional start)
387 (save-match-data
388 (funcall 'string-match regexp string start))))
390 (if (fboundp 'looking-at-p)
391 (defalias 'org-looking-at-p 'looking-at-p)
392 (defun org-looking-at-p (&rest args)
393 (save-match-data
394 (apply 'looking-at args))))
396 ; XEmacs does not have `looking-back'.
397 (if (fboundp 'looking-back)
398 (defalias 'org-looking-back 'looking-back)
399 (defun org-looking-back (regexp &optional limit greedy)
400 "Return non-nil if text before point matches regular expression REGEXP.
401 Like `looking-at' except matches before point, and is slower.
402 LIMIT if non-nil speeds up the search by specifying a minimum
403 starting position, to avoid checking matches that would start
404 before LIMIT.
406 If GREEDY is non-nil, extend the match backwards as far as
407 possible, stopping when a single additional previous character
408 cannot be part of a match for REGEXP. When the match is
409 extended, its starting position is allowed to occur before
410 LIMIT."
411 (let ((start (point))
412 (pos
413 (save-excursion
414 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
415 (point)))))
416 (if (and greedy pos)
417 (save-restriction
418 (narrow-to-region (point-min) start)
419 (while (and (> pos (point-min))
420 (save-excursion
421 (goto-char pos)
422 (backward-char 1)
423 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
424 (setq pos (1- pos)))
425 (save-excursion
426 (goto-char pos)
427 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
428 (not (null pos)))))
430 (defun org-floor* (x &optional y)
431 "Return a list of the floor of X and the fractional part of X.
432 With two arguments, return floor and remainder of their quotient."
433 (let ((q (floor x y)))
434 (list q (- x (if y (* y q) q)))))
436 ;; `pop-to-buffer-same-window' has been introduced with Emacs 24.1.
437 (defun org-pop-to-buffer-same-window
438 (&optional buffer-or-name norecord label)
439 "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
440 (if (fboundp 'pop-to-buffer-same-window)
441 (funcall
442 'pop-to-buffer-same-window buffer-or-name norecord label)
443 (funcall 'switch-to-buffer buffer-or-name norecord)))
445 (provide 'org-compat)
449 ;;; org-compat.el ends here