Make org-publish-attachment honor directory structure
[org-mode.git] / lisp / org-compat.el
blob4383bec674797a25c1fa73f33f6ccdcbee5b5343
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.01trans
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 ;; Miscellaneous functions
167 (defun org-add-hook (hook function &optional append local)
168 "Add-hook, compatible with both Emacsen."
169 (if (and local (featurep 'xemacs))
170 (add-local-hook hook function append)
171 (add-hook hook function append local)))
173 (defun org-add-props (string plist &rest props)
174 "Add text properties to entire string, from beginning to end.
175 PLIST may be a list of properties, PROPS are individual properties and values
176 that will be added to PLIST. Returns the string that was modified."
177 (add-text-properties
178 0 (length string) (if props (append plist props) plist) string)
179 string)
180 (put 'org-add-props 'lisp-indent-function 2)
182 (defun org-fit-window-to-buffer (&optional window max-height min-height
183 shrink-only)
184 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
185 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
186 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
187 `shrink-window-if-larger-than-buffer' instead, the height limit is
188 ignored in this case."
189 (cond ((if (fboundp 'window-full-width-p)
190 (not (window-full-width-p window))
191 (> (frame-width) (window-width window)))
192 ;; do nothing if another window would suffer
194 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
195 (fit-window-to-buffer window max-height min-height))
196 ((fboundp 'shrink-window-if-larger-than-buffer)
197 (shrink-window-if-larger-than-buffer window)))
198 (or window (selected-window)))
200 ;; Region compatibility
202 (defvar org-ignore-region nil
203 "To temporarily disable the active region.")
205 (defun org-region-active-p ()
206 "Is `transient-mark-mode' on and the region active?
207 Works on both Emacs and XEmacs."
208 (if org-ignore-region
210 (if (featurep 'xemacs)
211 (and zmacs-regions (region-active-p))
212 (if (fboundp 'use-region-p)
213 (use-region-p)
214 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
216 (defun org-cursor-to-region-beginning ()
217 (when (and (org-region-active-p)
218 (> (point) (region-beginning)))
219 (exchange-point-and-mark)))
221 ;; Invisibility compatibility
223 (defun org-remove-from-invisibility-spec (arg)
224 "Remove elements from `buffer-invisibility-spec'."
225 (if (fboundp 'remove-from-invisibility-spec)
226 (remove-from-invisibility-spec arg)
227 (if (consp buffer-invisibility-spec)
228 (setq buffer-invisibility-spec
229 (delete arg buffer-invisibility-spec)))))
231 (defun org-in-invisibility-spec-p (arg)
232 "Is ARG a member of `buffer-invisibility-spec'?"
233 (if (consp buffer-invisibility-spec)
234 (member arg buffer-invisibility-spec)
235 nil))
237 (defmacro org-xemacs-without-invisibility (&rest body)
238 "Turn off exents with invisibility while executing BODY."
239 `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
240 'all-extents-closed-open 'invisible))
241 ext-inv-specs)
242 (dolist (ext ext-inv)
243 (when (extent-property ext 'invisible)
244 (add-to-list 'ext-inv-specs (list ext (extent-property
245 ext 'invisible)))
246 (set-extent-property ext 'invisible nil)))
247 ,@body
248 (dolist (ext-inv-spec ext-inv-specs)
249 (set-extent-property (car ext-inv-spec) 'invisible
250 (cadr ext-inv-spec)))))
252 (defun org-indent-to-column (column &optional minimum buffer)
253 "Work around a bug with extents with invisibility in XEmacs."
254 (if (featurep 'xemacs)
255 (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
256 (indent-to-column column minimum)))
258 (defun org-indent-line-to (column)
259 "Work around a bug with extents with invisibility in XEmacs."
260 (if (featurep 'xemacs)
261 (org-xemacs-without-invisibility (indent-line-to column))
262 (indent-line-to column)))
264 (defun org-move-to-column (column &optional force buffer)
265 (if (featurep 'xemacs)
266 (org-xemacs-without-invisibility (move-to-column column force buffer))
267 (move-to-column column force)))
269 (defun org-get-x-clipboard-compat (value)
270 "Get the clipboard value on XEmacs or Emacs 21."
271 (cond ((featurep 'xemacs)
272 (org-no-warnings (get-selection-no-error value)))
273 ((fboundp 'x-get-selection)
274 (condition-case nil
275 (or (x-get-selection value 'UTF8_STRING)
276 (x-get-selection value 'COMPOUND_TEXT)
277 (x-get-selection value 'STRING)
278 (x-get-selection value 'TEXT))
279 (error nil)))))
281 (defun org-propertize (string &rest properties)
282 (if (featurep 'xemacs)
283 (progn
284 (add-text-properties 0 (length string) properties string)
285 string)
286 (apply 'propertize string properties)))
288 (defun org-substring-no-properties (string &optional from to)
289 (if (featurep 'xemacs)
290 (org-no-properties (substring string (or from 0) to))
291 (substring-no-properties string from to)))
293 (defun org-find-library-name (library)
294 (if (fboundp 'find-library-name)
295 (file-name-directory (find-library-name library))
296 ; XEmacs does not have `find-library-name'
297 (flet ((find-library-name-helper (filename ignored-codesys)
298 filename)
299 (find-library-name (library)
300 (find-library library nil 'find-library-name-helper)))
301 (file-name-directory (find-library-name library)))))
303 (defun org-count-lines (s)
304 "How many lines in string S?"
305 (let ((start 0) (n 1))
306 (while (string-match "\n" s start)
307 (setq start (match-end 0) n (1+ n)))
308 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
309 (setq n (1- n)))
312 (defun org-kill-new (string &rest args)
313 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
314 string)
315 (apply 'kill-new string args))
317 (defun org-select-frame-set-input-focus (frame)
318 "Select FRAME, raise it, and set input focus, if possible."
319 (cond ((featurep 'xemacs)
320 (if (fboundp 'select-frame-set-input-focus)
321 (select-frame-set-input-focus frame)
322 (raise-frame frame)
323 (select-frame frame)
324 (focus-frame frame)))
325 ;; `select-frame-set-input-focus' defined in Emacs 21 will not
326 ;; set the input focus.
327 ((>= emacs-major-version 22)
328 (select-frame-set-input-focus frame))
330 (raise-frame frame)
331 (select-frame frame)
332 (cond ((memq window-system '(x ns mac))
333 (x-focus-frame frame))
334 ((eq window-system 'w32)
335 (w32-focus-frame frame)))
336 (when focus-follows-mouse
337 (set-mouse-position frame (1- (frame-width frame)) 0)))))
339 (defun org-float-time (&optional time)
340 "Convert time value TIME to a floating point number.
341 TIME defaults to the current time."
342 (if (featurep 'xemacs)
343 (time-to-seconds (or time (current-time)))
344 (float-time time)))
346 (defun org-string-match-p (&rest args)
347 (if (fboundp 'string-match-p)
348 (apply 'string-match-p args)
349 (save-match-data
350 (apply 'string-match args))))
352 (defun org-looking-at-p (&rest args)
353 (if (fboundp 'looking-at-p)
354 (apply 'looking-at-p args)
355 (save-match-data
356 (apply 'looking-at args))))
358 ; XEmacs does not have `looking-back'.
359 (if (fboundp 'looking-back)
360 (defalias 'org-looking-back 'looking-back)
361 (defun org-looking-back (regexp &optional limit greedy)
362 "Return non-nil if text before point matches regular expression REGEXP.
363 Like `looking-at' except matches before point, and is slower.
364 LIMIT if non-nil speeds up the search by specifying a minimum
365 starting position, to avoid checking matches that would start
366 before LIMIT.
368 If GREEDY is non-nil, extend the match backwards as far as
369 possible, stopping when a single additional previous character
370 cannot be part of a match for REGEXP. When the match is
371 extended, its starting position is allowed to occur before
372 LIMIT."
373 (let ((start (point))
374 (pos
375 (save-excursion
376 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
377 (point)))))
378 (if (and greedy pos)
379 (save-restriction
380 (narrow-to-region (point-min) start)
381 (while (and (> pos (point-min))
382 (save-excursion
383 (goto-char pos)
384 (backward-char 1)
385 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
386 (setq pos (1- pos)))
387 (save-excursion
388 (goto-char pos)
389 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
390 (not (null pos)))))
392 (provide 'org-compat)
394 ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
396 ;;; org-compat.el ends here