如何使用defimage?

时间:2012-11-04 17:23:58

标签: emacs elisp

我认为我按照字面意思来遵循这个例子,但它不起作用。如果我使用defimage宏,则不会创建图像描述符,但是当我使用create-image时,它将执行所有相同的参数。以下是我的尝试:

(defimage test ((:type png :file "/home/wvxvw/Projects/haxe-mode/trunk/ede/etc/images/interface.png")))
test                                    ; nil
(defimage test (:type png :file "/home/wvxvw/Projects/haxe-mode/trunk/ede/etc/images/interface.png"))
test                                    ; nil
(insert-image test)                     ; error
(setq test (create-image "/home/wvxvw/Projects/haxe-mode/trunk/ede/etc/images/interface.png" 'png nil))
(image :type png :file "/home/wvxvw/Projects/haxe-mode/trunk/ede/etc/images/interface.png")
(insert-image test)                     ; shows image

任何提示?

编辑:

虽然上面的代码应该说明问题,但我正在尝试运行的实际代码更多涉及。发布以防万一:

(require 'cl)

(defvar haxe-images-dir
  (concat (file-name-directory load-file-name) "etc/images/"))

(defmacro haxe-define-images (images)
  (append
   '(progn)
   (loop for image in images
         collect
         `(defimage ,(intern (concat "haxe-" image "-icon"))
            ((:type png :file
                    (concat haxe-images-dir ,(concat image ".png"))))))))

(haxe-define-images
 ("private" "public" "static" "instance" "inline"
  "volatile" "variable" "class" "interface" "macro"
  "enum" "deftype" "function"))

EDIT2:

这就是它最终的运作方式。也许我编写了一些代码部分,因此从不同的地方或某些神秘的东西加载......

(require 'cl)
(require 'haxe-project)

(defmacro haxe-define-images (images)
  (append
   `(progn)
   (loop for image in images
         with images-root = (concat haxe-install-dir "etc/images/")
         collect
         `(defimage ,(intern (concat "haxe-" image "-icon"))
            ((:type png :file
                    ,(concat images-root image ".png")))))))

(haxe-define-images 
 ("private" "public" "static" "instance" "inline"
  "volatile" "variable" "class" "interface" "macro"
  "enum" "deftype" "function"))

1 个答案:

答案 0 :(得分:1)

这是defimage和其他def-表单的一个功能,它们只设置变量(如果尚未设置)。来自documentation for defvar

  

defvar特殊表单与setq类似,因为它设置变量的值。它与setq在两个方面不同:首先,如果变量还没有值,它只设置变量的值。如果变量已有值,则defvar不会覆盖现有值。第二,[...]

所以我假设你已经为test分配了一些东西,所以defimage形式什么也没做。如果您正在编辑代码,可以通过在表单上放置点并使用命令eval-defun CMx )来强制评估def-表单。

请注意,您应仅将defimage用于声明全局变量。对于其他情况(您将在本地使用图像),请改用find-image


在更新的代码中,您的宏haxe-define-images无法评估表达式(concat haxe-images-dir ...)。您可以通过扩展宏来看到这一点:

 ELISP> (print (macroexpand-all '(haxe-define-images ("foo"))))
 (progn (defvar haxe-address-icon (find-image (quote ((:type png :file (concat haxe-images-dir "address.png"))))) nil))

这不起作用,因为concat位于quote内,因此未评估。你需要写这样的东西:

(defmacro haxe-define-images (images)
  (append
   '(progn)
   (loop for image in images
         collect
         `(defimage ,(intern (concat "haxe-" image "-icon"))
            ((:type png :file
                    ,(concat haxe-images-dir image ".png")))))))

(如果你真的打算推迟对haxe-images-dir的评估,那么宏需要比这更复杂,但我相信你可以从这里弄明白。)