天天看点

如何让org-mode显示http url中的图片如何让org-mode显示http url中的图片.

如何让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图片的速度可能会很慢,因此这里采用了异步的方式来显示.