org-clock.el: Fix bug in `org-clocktable-write-default'.
[org-mode.git] / lisp / org-compat.el
blob735f62a72e3f7c5b582ba2e6eb87d4cbb5d08436
1 ;;; org-compat.el --- Compatibility code for Org-mode
3 ;; Copyright (C) 2004-2012 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 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains code needed for compatibility with XEmacs and older
28 ;; versions of GNU Emacs.
30 ;;; Code:
32 (eval-when-compile
33 (require 'cl))
35 (require 'find-func)
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 (when (and (boundp 'transient-mark-mode)
256 (not transient-mark-mode))
257 (setq transient-mark-mode 'lambda))
258 (when (boundp 'zmacs-regions)
259 (setq zmacs-regions t)))))
262 ;; Invisibility compatibility
264 (defun org-remove-from-invisibility-spec (arg)
265 "Remove elements from `buffer-invisibility-spec'."
266 (if (fboundp 'remove-from-invisibility-spec)
267 (remove-from-invisibility-spec arg)
268 (if (consp buffer-invisibility-spec)
269 (setq buffer-invisibility-spec
270 (delete arg buffer-invisibility-spec)))))
272 (defun org-in-invisibility-spec-p (arg)
273 "Is ARG a member of `buffer-invisibility-spec'?"
274 (if (consp buffer-invisibility-spec)
275 (member arg buffer-invisibility-spec)
276 nil))
278 (defmacro org-xemacs-without-invisibility (&rest body)
279 "Turn off extents with invisibility while executing BODY."
280 `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
281 'all-extents-closed-open 'invisible))
282 ext-inv-specs)
283 (dolist (ext ext-inv)
284 (when (extent-property ext 'invisible)
285 (add-to-list 'ext-inv-specs (list ext (extent-property
286 ext 'invisible)))
287 (set-extent-property ext 'invisible nil)))
288 ,@body
289 (dolist (ext-inv-spec ext-inv-specs)
290 (set-extent-property (car ext-inv-spec) 'invisible
291 (cadr ext-inv-spec)))))
292 (def-edebug-spec org-xemacs-without-invisibility (body))
294 (defun org-indent-to-column (column &optional minimum buffer)
295 "Work around a bug with extents with invisibility in XEmacs."
296 (if (featurep 'xemacs)
297 (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
298 (indent-to-column column minimum)))
300 (defun org-indent-line-to (column)
301 "Work around a bug with extents with invisibility in XEmacs."
302 (if (featurep 'xemacs)
303 (org-xemacs-without-invisibility (indent-line-to column))
304 (indent-line-to column)))
306 (defun org-move-to-column (column &optional force buffer)
307 (if (featurep 'xemacs)
308 (org-xemacs-without-invisibility (move-to-column column force buffer))
309 (move-to-column column force)))
311 (defun org-get-x-clipboard-compat (value)
312 "Get the clipboard value on XEmacs or Emacs 21."
313 (cond ((featurep 'xemacs)
314 (org-no-warnings (get-selection-no-error value)))
315 ((fboundp 'x-get-selection)
316 (condition-case nil
317 (or (x-get-selection value 'UTF8_STRING)
318 (x-get-selection value 'COMPOUND_TEXT)
319 (x-get-selection value 'STRING)
320 (x-get-selection value 'TEXT))
321 (error nil)))))
323 (defun org-propertize (string &rest properties)
324 (if (featurep 'xemacs)
325 (progn
326 (add-text-properties 0 (length string) properties string)
327 string)
328 (apply 'propertize string properties)))
330 (defun org-substring-no-properties (string &optional from to)
331 (if (featurep 'xemacs)
332 (org-no-properties (substring string (or from 0) to))
333 (substring-no-properties string from to)))
335 (defun org-find-library-name (library)
336 (if (fboundp 'find-library-name)
337 (file-name-directory (find-library-name library))
338 ; XEmacs does not have `find-library-name'
339 (flet ((find-library-name-helper (filename ignored-codesys)
340 filename)
341 (find-library-name
342 (library)
343 ;; Prevent Emacs compiler to complain about calling
344 ;; find-library-name with three arguments, as we need need
345 ;; to do in Xemacs
346 (org-no-warnings
347 (find-library library nil 'find-library-name-helper))))
348 (file-name-directory (find-library-name library)))))
350 (defun org-count-lines (s)
351 "How many lines in string S?"
352 (let ((start 0) (n 1))
353 (while (string-match "\n" s start)
354 (setq start (match-end 0) n (1+ n)))
355 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
356 (setq n (1- n)))
359 (defun org-kill-new (string &rest args)
360 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
361 string)
362 (apply 'kill-new string args))
364 (defun org-select-frame-set-input-focus (frame)
365 "Select FRAME, raise it, and set input focus, if possible."
366 (cond ((featurep 'xemacs)
367 (if (fboundp 'select-frame-set-input-focus)
368 (select-frame-set-input-focus frame)
369 (raise-frame frame)
370 (select-frame frame)
371 (focus-frame frame)))
372 ;; `select-frame-set-input-focus' defined in Emacs 21 will not
373 ;; set the input focus.
374 ((>= emacs-major-version 22)
375 (select-frame-set-input-focus frame))
377 (raise-frame frame)
378 (select-frame frame)
379 (cond ((memq window-system '(x ns mac))
380 (x-focus-frame frame))
381 ((eq window-system 'w32)
382 (w32-focus-frame frame)))
383 (when focus-follows-mouse
384 (set-mouse-position frame (1- (frame-width frame)) 0)))))
386 (defun org-float-time (&optional time)
387 "Convert time value TIME to a floating point number.
388 TIME defaults to the current time."
389 (if (featurep 'xemacs)
390 (time-to-seconds (or time (current-time)))
391 (float-time time)))
393 (if (fboundp 'string-match-p)
394 (defalias 'org-string-match-p 'string-match-p)
395 (defun org-string-match-p (regexp string &optional start)
396 (save-match-data
397 (funcall 'string-match regexp string start))))
399 (if (fboundp 'looking-at-p)
400 (defalias 'org-looking-at-p 'looking-at-p)
401 (defun org-looking-at-p (&rest args)
402 (save-match-data
403 (apply 'looking-at args))))
405 ; XEmacs does not have `looking-back'.
406 (if (fboundp 'looking-back)
407 (defalias 'org-looking-back 'looking-back)
408 (defun org-looking-back (regexp &optional limit greedy)
409 "Return non-nil if text before point matches regular expression REGEXP.
410 Like `looking-at' except matches before point, and is slower.
411 LIMIT if non-nil speeds up the search by specifying a minimum
412 starting position, to avoid checking matches that would start
413 before LIMIT.
415 If GREEDY is non-nil, extend the match backwards as far as
416 possible, stopping when a single additional previous character
417 cannot be part of a match for REGEXP. When the match is
418 extended, its starting position is allowed to occur before
419 LIMIT."
420 (let ((start (point))
421 (pos
422 (save-excursion
423 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
424 (point)))))
425 (if (and greedy pos)
426 (save-restriction
427 (narrow-to-region (point-min) start)
428 (while (and (> pos (point-min))
429 (save-excursion
430 (goto-char pos)
431 (backward-char 1)
432 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
433 (setq pos (1- pos)))
434 (save-excursion
435 (goto-char pos)
436 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
437 (not (null pos)))))
439 (defun org-floor* (x &optional y)
440 "Return a list of the floor of X and the fractional part of X.
441 With two arguments, return floor and remainder of their quotient."
442 (let ((q (floor x y)))
443 (list q (- x (if y (* y q) q)))))
445 ;; `pop-to-buffer-same-window' has been introduced with Emacs 24.1.
446 (defun org-pop-to-buffer-same-window
447 (&optional buffer-or-name norecord label)
448 "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
449 (if (fboundp 'pop-to-buffer-same-window)
450 (funcall
451 'pop-to-buffer-same-window buffer-or-name norecord)
452 (funcall 'switch-to-buffer buffer-or-name norecord)))
454 (provide 'org-compat)
456 ;;; org-compat.el ends here