[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

My advice-add :filter-args does not work

From: address@hidden
Subject: My advice-add :filter-args does not work
Date: Sat, 6 Jan 2018 19:12:33 +0800

As this question described

I'm going to modify the Org-mode inline images background colour. The
question's solution is define the original function again.

;;; set default Org-mode inline image background color.
(defcustom org-inline-image-background nil
  "The color used as the default background for inline images.
When nil, use the default face background."
  :group 'org
  :type '(choice color (const nil)))

;;; set default background color based on different stategies.
(defun my-org-mode-image-set-face (&args)
  (setq org-inline-image-background
        ;; a static color.
        ;; "#FFFFFF"
        ;; same color with color-theme background.
        (face-background 'default)

(add-hook 'circadian-after-load-theme-hook #'my-org-mode-image-set-face)

(defun org-display-inline-images (&optional include-linked refresh beg end)
  "Modified version of original `org-display-inline-images'.
Append `:background' in `create-image'."
  (interactive "P")
  (when (display-graphic-p)
    (unless refresh
      (when (fboundp 'clear-image-cache) (clear-image-cache)))
     (goto-char (or beg (point-min)))
     (let ((case-fold-search t)
           (file-extension-re (image-file-name-regexp)))
       (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
         (let ((link (save-match-data (org-element-context))))
           ;; Check if we're at an inline image.
           (when (and (equal (org-element-property :type link) "file")
                      (or include-linked
                          (not (org-element-property :contents-begin link)))
                      (let ((parent (org-element-property :parent link)))
                        (or (not (eq (org-element-type parent) 'link))
                            (not (cdr (org-element-contents parent)))))
                      (org-string-match-p file-extension-re
                                          (org-element-property :path
             (let ((file (expand-file-name
                           (org-element-property :path link)))))
               (when (file-exists-p file)
                 (let ((width
                        ;; Apply `org-image-actual-width' specifications.
                         ((not (image-type-available-p 'imagemagick)) nil)
                         ((eq org-image-actual-width t) nil)
                         ((listp org-image-actual-width)
                           ;; First try to find a width among
                           ;; attributes associated to the paragraph
                           ;; containing link.
                           (let ((paragraph
                                  (let ((e link))
                                    (while (and (setq e
                                                         :parent e))
                                                (not (eq (org-element-type
                             (when paragraph
                                 (goto-char (org-element-property :begin
                                      "^[ \t]*#\\+attr_.*?: +.*?:width
                                       :post-affiliated paragraph)
                                   (string-to-number (match-string 1))))))
                           ;; Otherwise, fall-back to provided number.
                           (car org-image-actual-width)))
                         ((numberp org-image-actual-width)
                       (old (get-char-property-and-overlay
                             (org-element-property :begin link)
                   (if (and (car-safe old) refresh)
                       (image-refresh (overlay-get (cdr old) 'display))
                     (let ((image (create-image file
                                                (and width 'imagemagick)
                                                :width width
org-inline-image-background))) ; added here.
                       (when image
                         (let* ((link
                                 ;; If inline image is the description
                                 ;; of another link, be sure to
                                 ;; consider the latter as the one to
                                 ;; apply the overlay on.
                                 (let ((parent
                                        (org-element-property :parent
                                   (if (eq (org-element-type parent) 'link)
                                (ov (make-overlay
                                     (org-element-property :begin link)
                                        (org-element-property :end link))
                                       (skip-chars-backward " \t")
                           (overlay-put ov 'display image)
                           (overlay-put ov 'face 'default)
                           (overlay-put ov 'org-image-overlay t)
                            ov 'modification-hooks
                            (list 'org-display-inline-remove-overlay))
                           (push ov org-inline-image-overlays)))))))))))))))

I don't like this solution. Try to use advice. But I'm not good at Elisp.
So I tried some search on Google and GitHub code. Try to understand some
examples. I try this code snippet:

(defun org-display-inline-images--with-color-theme-background-color (args)
; (file-or-data &optional type data-p &rest props)
  "Specify background color of Org-mode inline image through modify `ARGS'."
  (let* ((file (car args))
         (type (cadr args))
         (data-p (caddr args))
         (props (cdddr args)))
    (list file type data-p (append (list :background (face-background
'default)) props))))

(advice-add 'create-image :filter-args

But it does not work.

Hope someone can help me to fix this advice.

[stardiviner]           <Hack this world!>      GPG key ID: 47C32433
IRC(freeenode): stardiviner                     Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433

reply via email to

[Prev in Thread] Current Thread [Next in Thread]