如何讓org-mode顯示http url中的圖檔.
UPDATE:
為了友善使用,我将這段代碼封裝成了一個package:Uimage
可以直接從melpa上安裝
預設的org-display-inline-images隻能顯示本地圖檔. 要想顯示http url中的圖檔,可以通過以下代碼來實作
(defun org-display-inline-images-callback (status start end type old width ori-buffer)
(unwind-protect
(let (file-data)
(goto-char (point-min))
(search-forward-regexp "^$")
(setq file-data (buffer-substring-no-properties (+ (point)) (point-max)))
(when file-data
(with-current-buffer ori-buffer (if (and (car-safe old) refresh) (image-refresh (overlay-get (cdr old) 'display)) (setq img (create-image file-data type t :width width)) (when img (setq ov (make-overlay start end)) (overlay-put ov 'display img) (overlay-put ov 'face 'default) (overlay-put ov 'org-image-overlay t) (overlay-put ov 'modification-hooks (list 'org-display-inline-remove-overlay)) (push ov org-inline-image-overlays))))))
(kill-buffer)))
(defun org-display-inline-images-async (&optional include-linked refresh beg end)
"Display inline images asynchronously.
like org-display-inline-images. But it can display http-url-images in a asynchronous way. "
(interactive "P")
(when (display-graphic-p)
(unless refresh
(org-remove-inline-images)
(if (fboundp 'clear-image-cache) (clear-image-cache)))
(save-excursion
(save-restriction
(widen)
(setq beg (or beg (point-min)) end (or end (point-max)))
(goto-char beg)
(let ((re (concat "\\[\\[\\(\\(file:\\|http:\\|https:\\)\\|\\([./~]\\)\\)\\([^]\n]+?" (substring (org-image-file-name-regexp) -2) "\\)\\]" (if include-linked "" "\\]"))) (case-fold-search t) old file ov img type attrwidth width) (while (re-search-forward re end t) (setq old (get-char-property-and-overlay (match-beginning) 'org-image-overlay) file (substring-no-properties (match-string) -2)) (when (image-type-available-p 'imagemagick) (setq attrwidth (if (or (listp org-image-actual-width) (null org-image-actual-width)) (save-excursion (save-match-data (when (re-search-backward "#\\+attr.*:width[ \t]+\\([^ ]+\\)" (save-excursion (re-search-backward "^[ \t]*$\\|\\`" nil t)) t) (string-to-number (match-string)))))) width (cond ((eq org-image-actual-width t) nil) ((null org-image-actual-width) attrwidth) ((numberp org-image-actual-width) org-image-actual-width) ((listp org-image-actual-width) (or attrwidth (car org-image-actual-width)))) type (if width 'imagemagick))) (require 'url) (url-retrieve file #'org-display-inline-images-callback `(,(match-beginning) ,(match-end) ,type ,old ,width ,(current-buffer)))))))))
這樣,
M-x org-display-inline-images-async
就能顯示http url中的圖檔了.
注意由于讀取http url圖檔的速度可能會很慢,是以這裡采用了異步的方式來顯示.