Release 6.36c
[org-mode/org-jambu.git] / lisp / org-compat.el
blob8f39c0006c4a3a219f5bf6fad693611900044ccd
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.36c
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 ;;;; Emacs/XEmacs compatibility
94 ;; Keys
95 (defconst org-xemacs-key-equivalents
96 '(([mouse-1] . [button1])
97 ([mouse-2] . [button2])
98 ([mouse-3] . [button3])
99 ([C-mouse-4] . [(control mouse-4)])
100 ([C-mouse-5] . [(control mouse-5)]))
101 "Translation alist for a couple of keys")
103 ;; Overlay compatibility functions
104 (defun org-detach-overlay (ovl)
105 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
106 (defun org-overlay-display (ovl text &optional face evap)
107 "Make overlay OVL display TEXT with face FACE."
108 (if (featurep 'xemacs)
109 (let ((gl (make-glyph text)))
110 (and face (set-glyph-face gl face))
111 (set-extent-property ovl 'invisible t)
112 (set-extent-property ovl 'end-glyph gl))
113 (overlay-put ovl 'display text)
114 (if face (overlay-put ovl 'face face))
115 (if evap (overlay-put ovl 'evaporate t))))
116 (defun org-overlay-before-string (ovl text &optional face evap)
117 "Make overlay OVL display TEXT with face FACE."
118 (if (featurep 'xemacs)
119 (let ((gl (make-glyph text)))
120 (and face (set-glyph-face gl face))
121 (set-extent-property ovl 'begin-glyph gl))
122 (if face (org-add-props text nil 'face face))
123 (overlay-put ovl 'before-string text)
124 (if evap (overlay-put ovl 'evaporate t))))
125 (defun org-find-overlays (prop &optional pos delete)
126 "Find all overlays specifying PROP at POS or point.
127 If DELETE is non-nil, delete all those overlays."
128 (let ((overlays (overlays-at (or pos (point))))
129 ov found)
130 (while (setq ov (pop overlays))
131 (if (overlay-get ov prop)
132 (if delete (delete-overlay ov) (push ov found))))
133 found))
135 ;; Miscellaneous functions
137 (defun org-add-hook (hook function &optional append local)
138 "Add-hook, compatible with both Emacsen."
139 (if (and local (featurep 'xemacs))
140 (add-local-hook hook function append)
141 (add-hook hook function append local)))
143 (defun org-add-props (string plist &rest props)
144 "Add text properties to entire string, from beginning to end.
145 PLIST may be a list of properties, PROPS are individual properties and values
146 that will be added to PLIST. Returns the string that was modified."
147 (add-text-properties
148 0 (length string) (if props (append plist props) plist) string)
149 string)
150 (put 'org-add-props 'lisp-indent-function 2)
152 (defun org-fit-window-to-buffer (&optional window max-height min-height
153 shrink-only)
154 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
155 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
156 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
157 `shrink-window-if-larger-than-buffer' instead, the hight limit are
158 ignored in this case."
159 (cond ((if (fboundp 'window-full-width-p)
160 (not (window-full-width-p window))
161 (> (frame-width) (window-width window)))
162 ;; do nothing if another window would suffer
164 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
165 (fit-window-to-buffer window max-height min-height))
166 ((fboundp 'shrink-window-if-larger-than-buffer)
167 (shrink-window-if-larger-than-buffer window)))
168 (or window (selected-window)))
170 ;; Region compatibility
172 (defvar org-ignore-region nil
173 "To temporarily disable the active region.")
175 (defun org-region-active-p ()
176 "Is `transient-mark-mode' on and the region active?
177 Works on both Emacs and XEmacs."
178 (if org-ignore-region
180 (if (featurep 'xemacs)
181 (and zmacs-regions (region-active-p))
182 (if (fboundp 'use-region-p)
183 (use-region-p)
184 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
186 (defun org-cursor-to-region-beginning ()
187 (when (and (org-region-active-p)
188 (> (point) (region-beginning)))
189 (exchange-point-and-mark)))
191 ;; Invisibility compatibility
193 (defun org-remove-from-invisibility-spec (arg)
194 "Remove elements from `buffer-invisibility-spec'."
195 (if (fboundp 'remove-from-invisibility-spec)
196 (remove-from-invisibility-spec arg)
197 (if (consp buffer-invisibility-spec)
198 (setq buffer-invisibility-spec
199 (delete arg buffer-invisibility-spec)))))
201 (defun org-in-invisibility-spec-p (arg)
202 "Is ARG a member of `buffer-invisibility-spec'?"
203 (if (consp buffer-invisibility-spec)
204 (member arg buffer-invisibility-spec)
205 nil))
207 (defmacro org-xemacs-without-invisibility (&rest body)
208 "Turn off exents with invisibility while executing BODY."
209 `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
210 'all-extents-closed-open 'invisible))
211 ext-inv-specs)
212 (dolist (ext ext-inv)
213 (when (extent-property ext 'invisible)
214 (add-to-list 'ext-inv-specs (list ext (extent-property
215 ext 'invisible)))
216 (set-extent-property ext 'invisible nil)))
217 ,@body
218 (dolist (ext-inv-spec ext-inv-specs)
219 (set-extent-property (car ext-inv-spec) 'invisible
220 (cadr ext-inv-spec)))))
222 (defun org-indent-to-column (column &optional minimum buffer)
223 "Work around a bug with extents with invisibility in XEmacs."
224 (if (featurep 'xemacs)
225 (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
226 (indent-to-column column minimum)))
228 (defun org-indent-line-to (column)
229 "Work around a bug with extents with invisibility in XEmacs."
230 (if (featurep 'xemacs)
231 (org-xemacs-without-invisibility (indent-line-to column))
232 (indent-line-to column)))
234 (defun org-move-to-column (column &optional force buffer)
235 (if (featurep 'xemacs)
236 (org-xemacs-without-invisibility (move-to-column column force buffer))
237 (move-to-column column force)))
239 (defun org-get-x-clipboard-compat (value)
240 "Get the clipboard value on XEmacs or Emacs 21"
241 (cond ((featurep 'xemacs)
242 (org-no-warnings (get-selection-no-error value)))
243 ((fboundp 'x-get-selection)
244 (condition-case nil
245 (or (x-get-selection value 'UTF8_STRING)
246 (x-get-selection value 'COMPOUND_TEXT)
247 (x-get-selection value 'STRING)
248 (x-get-selection value 'TEXT))
249 (error nil)))))
251 (defun org-propertize (string &rest properties)
252 (if (featurep 'xemacs)
253 (progn
254 (add-text-properties 0 (length string) properties string)
255 string)
256 (apply 'propertize string properties)))
258 (defun org-substring-no-properties (string &optional from to)
259 (if (featurep 'xemacs)
260 (org-no-properties (substring string (or from 0) to))
261 (substring-no-properties string from to)))
263 (defun org-find-library-name (library)
264 (if (fboundp 'find-library-name)
265 (file-name-directory (find-library-name library))
266 ; XEmacs does not have `find-library-name'
267 (flet ((find-library-name-helper (filename ignored-codesys)
268 filename)
269 (find-library-name (library)
270 (find-library library nil 'find-library-name-helper)))
271 (file-name-directory (find-library-name library)))))
273 (defun org-count-lines (s)
274 "How many lines in string S?"
275 (let ((start 0) (n 1))
276 (while (string-match "\n" s start)
277 (setq start (match-end 0) n (1+ n)))
278 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
279 (setq n (1- n)))
282 (defun org-kill-new (string &rest args)
283 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
284 string)
285 (apply 'kill-new string args))
287 (defun org-select-frame-set-input-focus (frame)
288 "Select FRAME, raise it, and set input focus, if possible."
289 (cond ((featurep 'xemacs)
290 (if (fboundp 'select-frame-set-input-focus)
291 (select-frame-set-input-focus frame)
292 (raise-frame frame)
293 (select-frame frame)
294 (focus-frame frame)))
295 ;; `select-frame-set-input-focus' defined in Emacs 21 will not
296 ;; set the input focus.
297 ((>= emacs-major-version 22)
298 (select-frame-set-input-focus frame))
300 (raise-frame frame)
301 (select-frame frame)
302 (cond ((memq window-system '(x ns mac))
303 (x-focus-frame frame))
304 ((eq window-system 'w32)
305 (w32-focus-frame frame)))
306 (when focus-follows-mouse
307 (set-mouse-position frame (1- (frame-width frame)) 0)))))
309 (defun org-float-time (&optional time)
310 "Convert time value TIME to a floating point number.
311 TIME defaults to the current time."
312 (if (featurep 'xemacs)
313 (time-to-seconds (or time (current-time)))
314 (float-time time)))
316 ; XEmacs does not have `looking-back'.
317 (if (fboundp 'looking-back)
318 (defalias 'org-looking-back 'looking-back)
319 (defun org-looking-back (regexp &optional limit greedy)
320 "Return non-nil if text before point matches regular expression REGEXP.
321 Like `looking-at' except matches before point, and is slower.
322 LIMIT if non-nil speeds up the search by specifying a minimum
323 starting position, to avoid checking matches that would start
324 before LIMIT.
326 If GREEDY is non-nil, extend the match backwards as far as
327 possible, stopping when a single additional previous character
328 cannot be part of a match for REGEXP. When the match is
329 extended, its starting position is allowed to occur before
330 LIMIT."
331 (let ((start (point))
332 (pos
333 (save-excursion
334 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
335 (point)))))
336 (if (and greedy pos)
337 (save-restriction
338 (narrow-to-region (point-min) start)
339 (while (and (> pos (point-min))
340 (save-excursion
341 (goto-char pos)
342 (backward-char 1)
343 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
344 (setq pos (1- pos)))
345 (save-excursion
346 (goto-char pos)
347 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
348 (not (null pos)))))
350 (provide 'org-compat)
352 ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
354 ;;; org-compat.el ends here