Fix `org-display-inline-images' with "clickable images"

* lisp/org.el (org-display-inline-images): Even though Org syntax
  doesn't support nested links, display an image when the function is
  called on a link that contains a single file name in its
  description.

Reported-by: "Dietrich Foethke" <foethke@web.de>
<http://lists.gnu.org/r/emacs-orgmode/2019-02/msg00280.html>
This commit is contained in:
Nicolas Goaziou 2019-02-22 14:15:33 +01:00
parent 29fe5a7d7f
commit 93c3d9d281

View file

@ -18752,7 +18752,8 @@ conventions:
from `image-file-name-regexp' and it has no contents. from `image-file-name-regexp' and it has no contents.
2. Its description consists in a single link of the previous 2. Its description consists in a single link of the previous
type. type. In this case, that link must be a well-formed plain
or angle link, i.e., it must have an explicit \"file\" type.
When optional argument INCLUDE-LINKED is non-nil, also links with When optional argument INCLUDE-LINKED is non-nil, also links with
a text description part will be inlined. This can be nice for a text description part will be inlined. This can be nice for
@ -18768,8 +18769,7 @@ boundaries."
(unless refresh (unless refresh
(org-remove-inline-images) (org-remove-inline-images)
(when (fboundp 'clear-image-cache) (clear-image-cache))) (when (fboundp 'clear-image-cache) (clear-image-cache)))
(org-with-wide-buffer (org-with-point-at (or beg 1)
(goto-char (or beg (point-min)))
(let* ((case-fold-search t) (let* ((case-fold-search t)
(file-extension-re (image-file-name-regexp)) (file-extension-re (image-file-name-regexp))
(link-abbrevs (mapcar #'car (link-abbrevs (mapcar #'car
@ -18778,23 +18778,47 @@ boundaries."
;; Check absolute, relative file names and explicit ;; Check absolute, relative file names and explicit
;; "file:" links. Also check link abbreviations since ;; "file:" links. Also check link abbreviations since
;; some might expand to "file" links. ;; some might expand to "file" links.
(file-types-re (format "[][]\\[\\(?:file\\|[./~]%s\\)" (file-types-re
(format "\\[\\[\\(?:file%s:\\|[./~]\\)\\|\\]\\[\\(<?file:\\)"
(if (not link-abbrevs) "" (if (not link-abbrevs) ""
(format "\\|\\(?:%s:\\)" (concat "\\|" (regexp-opt link-abbrevs))))))
(regexp-opt link-abbrevs))))))
(while (re-search-forward file-types-re end t) (while (re-search-forward file-types-re end t)
(let ((link (save-match-data (org-element-context)))) (let* ((link (org-element-lineage
;; Check if we're at an inline image, i.e., an image file (save-match-data (org-element-context))
;; link without a description (unless INCLUDE-LINKED is '(link) t))
;; non-nil). (inner-start (match-beginning 1))
(when (and (equal "file" (org-element-property :type link)) (path
(or include-linked (cond
(null (org-element-contents link))) ;; No link at point; no inline image.
(string-match-p file-extension-re ((not link) nil)
;; File link without a description. Also handle
;; INCLUDE-LINKED here since it should have
;; precedence over the next case. I.e., if link
;; contains filenames in both the path and the
;; description, prioritize the path only when
;; INCLUDE-LINKED is non-nil.
((or (not (org-element-property :contents-begin link))
include-linked)
(and (equal "file" (org-element-property :type link))
(org-element-property :path link))) (org-element-property :path link)))
(let ((file (expand-file-name ;; Link with a description. Check if description
(org-link-unescape ;; is a filename. Even if Org doesn't have syntax
(org-element-property :path link))))) ;; for those -- clickable image -- constructs, fake
;; them, as in `org-export-insert-image-links'.
((not inner-start) nil)
(t
(org-with-point-at inner-start
(and (looking-at
(if (char-equal ?< (char-after inner-start))
org-angle-link-re
org-plain-link-re))
;; File name must fill the whole
;; description.
(= (org-element-property :contents-end link)
(match-end 0))
(match-string 2)))))))
(when (and path (string-match-p file-extension-re path))
(let ((file (expand-file-name (org-link-unescape path))))
(when (file-exists-p file) (when (file-exists-p file)
(let ((width (let ((width
;; Apply `org-image-actual-width' specifications. ;; Apply `org-image-actual-width' specifications.