如何让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) 1) (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) 0 -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 1) 'org-image-overlay) file (substring-no-properties (match-string 0) 2 -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 1)))))) 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 0) ,(match-end 0) ,type ,old ,width ,(current-buffer)))))))))

这样, M-x org-display-inline-images-async 就能显示http url中的图片了.

注意由于读取http url图片的速度可能会很慢,因此这里采用了异步的方式来显示.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值