Merge branch 'fix-orgstruct'
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-compat.el
blobd468942b5b9ffaa8f270ad85cc4e4841e4ada80e
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 'org-macs)
37 (declare-function w32-focus-frame "term/w32-win" (frame))
39 ;; The following constant is for backward compatibility. We do not use
40 ;; it in org-mode, because the Byte compiler evaluates (featurep 'xemacs)
41 ;; at compilation time and can therefore optimize code better.
42 (defconst org-xemacs-p (featurep 'xemacs))
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 (defun org-version-check (version feature level)
90 (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
91 (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
92 (rmaj (or (nth 0 v1) 99))
93 (rmin (or (nth 1 v1) 99))
94 (rbld (or (nth 2 v1) 99))
95 (maj (or (nth 0 v2) 0))
96 (min (or (nth 1 v2) 0))
97 (bld (or (nth 2 v2) 0)))
98 (if (or (< maj rmaj)
99 (and (= maj rmaj)
100 (< min rmin))
101 (and (= maj rmaj)
102 (= min rmin)
103 (< bld rbld)))
104 (if (eq level :predicate)
105 ;; just return if we have the version
107 (let ((msg (format "Emacs %s or greater is recommended for %s"
108 version feature)))
109 (display-warning 'org msg level)
111 t)))
113 ;;;; Emacs/XEmacs compatibility
115 ;; Keys
116 (defconst org-xemacs-key-equivalents
117 '(([mouse-1] . [button1])
118 ([mouse-2] . [button2])
119 ([mouse-3] . [button3])
120 ([C-mouse-4] . [(control mouse-4)])
121 ([C-mouse-5] . [(control mouse-5)]))
122 "Translation alist for a couple of keys.")
124 ;; Overlay compatibility functions
125 (defun org-detach-overlay (ovl)
126 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
127 (defun org-overlay-display (ovl text &optional face evap)
128 "Make overlay OVL display TEXT with face FACE."
129 (if (featurep 'xemacs)
130 (let ((gl (make-glyph text)))
131 (and face (set-glyph-face gl face))
132 (set-extent-property ovl 'invisible t)
133 (set-extent-property ovl 'end-glyph gl))
134 (overlay-put ovl 'display text)
135 (if face (overlay-put ovl 'face face))
136 (if evap (overlay-put ovl 'evaporate t))))
137 (defun org-overlay-before-string (ovl text &optional face evap)
138 "Make overlay OVL display TEXT with face FACE."
139 (if (featurep 'xemacs)
140 (let ((gl (make-glyph text)))
141 (and face (set-glyph-face gl face))
142 (set-extent-property ovl 'begin-glyph gl))
143 (if face (org-add-props text nil 'face face))
144 (overlay-put ovl 'before-string text)
145 (if evap (overlay-put ovl 'evaporate t))))
146 (defun org-find-overlays (prop &optional pos delete)
147 "Find all overlays specifying PROP at POS or point.
148 If DELETE is non-nil, delete all those overlays."
149 (let ((overlays (overlays-at (or pos (point))))
150 ov found)
151 (while (setq ov (pop overlays))
152 (if (overlay-get ov prop)
153 (if delete (delete-overlay ov) (push ov found))))
154 found))
156 (defun org-get-x-clipboard (value)
157 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
158 (if (eq window-system 'x)
159 (let ((x (org-get-x-clipboard-compat value)))
160 (if x (org-no-properties x)))))
162 (defsubst org-decompose-region (beg end)
163 "Decompose from BEG to END."
164 (if (featurep 'xemacs)
165 (let ((modified-p (buffer-modified-p))
166 (buffer-read-only nil))
167 (remove-text-properties beg end '(composition nil))
168 (set-buffer-modified-p modified-p))
169 (decompose-region beg end)))
171 ;; Miscellaneous functions
173 (defun org-add-hook (hook function &optional append local)
174 "Add-hook, compatible with both Emacsen."
175 (if (and local (featurep 'xemacs))
176 (add-local-hook hook function append)
177 (add-hook hook function append local)))
179 (defun org-add-props (string plist &rest props)
180 "Add text properties to entire string, from beginning to end.
181 PLIST may be a list of properties, PROPS are individual properties and values
182 that will be added to PLIST. Returns the string that was modified."
183 (add-text-properties
184 0 (length string) (if props (append plist props) plist) string)
185 string)
186 (put 'org-add-props 'lisp-indent-function 2)
188 (defun org-fit-window-to-buffer (&optional window max-height min-height
189 shrink-only)
190 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
191 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
192 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
193 `shrink-window-if-larger-than-buffer' instead, the height limit is
194 ignored in this case."
195 (cond ((if (fboundp 'window-full-width-p)
196 (not (window-full-width-p window))
197 (> (frame-width) (window-width window)))
198 ;; do nothing if another window would suffer
200 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
201 (fit-window-to-buffer window max-height min-height))
202 ((fboundp 'shrink-window-if-larger-than-buffer)
203 (shrink-window-if-larger-than-buffer window)))
204 (or window (selected-window)))
206 (defun org-number-sequence (from &optional to inc)
207 "Call `number-sequence or emulate it."
208 (if (fboundp 'number-sequence)
209 (number-sequence from to inc)
210 (if (or (not to) (= from to))
211 (list from)
212 (or inc (setq inc 1))
213 (when (zerop inc) (error "The increment can not be zero"))
214 (let (seq (n 0) (next from))
215 (if (> inc 0)
216 (while (<= next to)
217 (setq seq (cons next seq)
218 n (1+ n)
219 next (+ from (* n inc))))
220 (while (>= next to)
221 (setq seq (cons next seq)
222 n (1+ n)
223 next (+ from (* n inc)))))
224 (nreverse seq)))))
226 ;; Region compatibility
228 (defvar org-ignore-region nil
229 "To temporarily disable the active region.")
231 (defun org-region-active-p ()
232 "Is `transient-mark-mode' on and the region active?
233 Works on both Emacs and XEmacs."
234 (if org-ignore-region
236 (if (featurep 'xemacs)
237 (and zmacs-regions (region-active-p))
238 (if (fboundp 'use-region-p)
239 (use-region-p)
240 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
242 (defun org-cursor-to-region-beginning ()
243 (when (and (org-region-active-p)
244 (> (point) (region-beginning)))
245 (exchange-point-and-mark)))
247 ;; Emacs 22 misses `activate-mark'
248 (if (fboundp 'activate-mark)
249 (defalias 'org-activate-mark 'activate-mark)
250 (defun org-activate-mark ()
251 (when (mark t)
252 (setq mark-active t)
253 (when (and (boundp 'transient-mark-mode)
254 (not transient-mark-mode))
255 (setq transient-mark-mode 'lambda))
256 (when (boundp 'zmacs-regions)
257 (setq zmacs-regions t)))))
260 ;; Invisibility compatibility
262 (defun org-remove-from-invisibility-spec (arg)
263 "Remove elements from `buffer-invisibility-spec'."
264 (if (fboundp 'remove-from-invisibility-spec)
265 (remove-from-invisibility-spec arg)
266 (if (consp buffer-invisibility-spec)
267 (setq buffer-invisibility-spec
268 (delete arg buffer-invisibility-spec)))))
270 (defun org-in-invisibility-spec-p (arg)
271 "Is ARG a member of `buffer-invisibility-spec'?"
272 (if (consp buffer-invisibility-spec)
273 (member arg buffer-invisibility-spec)
274 nil))
276 (defmacro org-xemacs-without-invisibility (&rest body)
277 "Turn off extents with invisibility while executing BODY."
278 `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
279 'all-extents-closed-open 'invisible))
280 ext-inv-specs)
281 (dolist (ext ext-inv)
282 (when (extent-property ext 'invisible)
283 (add-to-list 'ext-inv-specs (list ext (extent-property
284 ext 'invisible)))
285 (set-extent-property ext 'invisible nil)))
286 ,@body
287 (dolist (ext-inv-spec ext-inv-specs)
288 (set-extent-property (car ext-inv-spec) 'invisible
289 (cadr ext-inv-spec)))))
290 (def-edebug-spec org-xemacs-without-invisibility (body))
292 (defun org-indent-to-column (column &optional minimum buffer)
293 "Work around a bug with extents with invisibility in XEmacs."
294 (if (featurep 'xemacs)
295 (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
296 (indent-to-column column minimum)))
298 (defun org-indent-line-to (column)
299 "Work around a bug with extents with invisibility in XEmacs."
300 (if (featurep 'xemacs)
301 (org-xemacs-without-invisibility (indent-line-to column))
302 (indent-line-to column)))
304 (defun org-move-to-column (column &optional force buffer)
305 (if (featurep 'xemacs)
306 (org-xemacs-without-invisibility (move-to-column column force buffer))
307 (move-to-column column force)))
309 (defun org-get-x-clipboard-compat (value)
310 "Get the clipboard value on XEmacs or Emacs 21."
311 (cond ((featurep 'xemacs)
312 (org-no-warnings (get-selection-no-error value)))
313 ((fboundp 'x-get-selection)
314 (condition-case nil
315 (or (x-get-selection value 'UTF8_STRING)
316 (x-get-selection value 'COMPOUND_TEXT)
317 (x-get-selection value 'STRING)
318 (x-get-selection value 'TEXT))
319 (error nil)))))
321 (defun org-propertize (string &rest properties)
322 (if (featurep 'xemacs)
323 (progn
324 (add-text-properties 0 (length string) properties string)
325 string)
326 (apply 'propertize string properties)))
328 (defun org-substring-no-properties (string &optional from to)
329 (if (featurep 'xemacs)
330 (org-no-properties (substring string (or from 0) to))
331 (substring-no-properties string from to)))
333 (defmacro org-find-library-name (library)
334 (if (fboundp 'find-library-name)
335 `(file-name-directory (find-library-name ,library))
336 ; XEmacs does not have `find-library-name'
337 `(flet ((flnh (lib ignore) lib))
338 (file-name-directory (find-library ,library nil 'flnh)))))
340 (defun org-count-lines (s)
341 "How many lines in string S?"
342 (let ((start 0) (n 1))
343 (while (string-match "\n" s start)
344 (setq start (match-end 0) n (1+ n)))
345 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
346 (setq n (1- n)))
349 (defun org-kill-new (string &rest args)
350 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
351 string)
352 (apply 'kill-new string args))
354 (defun org-select-frame-set-input-focus (frame)
355 "Select FRAME, raise it, and set input focus, if possible."
356 (cond ((featurep 'xemacs)
357 (if (fboundp 'select-frame-set-input-focus)
358 (select-frame-set-input-focus frame)
359 (raise-frame frame)
360 (select-frame frame)
361 (focus-frame frame)))
362 ;; `select-frame-set-input-focus' defined in Emacs 21 will not
363 ;; set the input focus.
364 ((>= emacs-major-version 22)
365 (select-frame-set-input-focus frame))
367 (raise-frame frame)
368 (select-frame frame)
369 (cond ((memq window-system '(x ns mac))
370 (x-focus-frame frame))
371 ((eq window-system 'w32)
372 (w32-focus-frame frame)))
373 (when focus-follows-mouse
374 (set-mouse-position frame (1- (frame-width frame)) 0)))))
376 (defun org-float-time (&optional time)
377 "Convert time value TIME to a floating point number.
378 TIME defaults to the current time."
379 (if (featurep 'xemacs)
380 (time-to-seconds (or time (current-time)))
381 (float-time time)))
383 (if (fboundp 'string-match-p)
384 (defalias 'org-string-match-p 'string-match-p)
385 (defun org-string-match-p (regexp string &optional start)
386 (save-match-data
387 (funcall 'string-match regexp string start))))
389 (if (fboundp 'looking-at-p)
390 (defalias 'org-looking-at-p 'looking-at-p)
391 (defun org-looking-at-p (&rest args)
392 (save-match-data
393 (apply 'looking-at args))))
395 ; XEmacs does not have `looking-back'.
396 (if (fboundp 'looking-back)
397 (defalias 'org-looking-back 'looking-back)
398 (defun org-looking-back (regexp &optional limit greedy)
399 "Return non-nil if text before point matches regular expression REGEXP.
400 Like `looking-at' except matches before point, and is slower.
401 LIMIT if non-nil speeds up the search by specifying a minimum
402 starting position, to avoid checking matches that would start
403 before LIMIT.
405 If GREEDY is non-nil, extend the match backwards as far as
406 possible, stopping when a single additional previous character
407 cannot be part of a match for REGEXP. When the match is
408 extended, its starting position is allowed to occur before
409 LIMIT."
410 (let ((start (point))
411 (pos
412 (save-excursion
413 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
414 (point)))))
415 (if (and greedy pos)
416 (save-restriction
417 (narrow-to-region (point-min) start)
418 (while (and (> pos (point-min))
419 (save-excursion
420 (goto-char pos)
421 (backward-char 1)
422 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
423 (setq pos (1- pos)))
424 (save-excursion
425 (goto-char pos)
426 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
427 (not (null pos)))))
429 (defun org-floor* (x &optional y)
430 "Return a list of the floor of X and the fractional part of X.
431 With two arguments, return floor and remainder of their quotient."
432 (let ((q (floor x y)))
433 (list q (- x (if y (* y q) q)))))
435 ;; `pop-to-buffer-same-window' has been introduced with Emacs 24.1.
436 (defun org-pop-to-buffer-same-window
437 (&optional buffer-or-name norecord label)
438 "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
439 (if (fboundp 'pop-to-buffer-same-window)
440 (funcall
441 'pop-to-buffer-same-window buffer-or-name norecord)
442 (funcall 'switch-to-buffer buffer-or-name norecord)))
444 (provide 'org-compat)
446 ;;; org-compat.el ends here