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