您的位置:首页 > 其它

让emacs完美显示BMP文件的办法

2010-11-30 01:20 381 查看
首先感谢http://emacser.com/的ahei,

这个想法是在和TA谈话时想到,

由于是边试边写,程序中用到了本人以前写的ppm-gen( http://www.emacswiki.org/emacs/ppm-gen.el )

 

基本思路是使用emacs lisp强大的advice功能,

 

在.emacs中加入以下代码:

 

(require 'ppm-gen)
(defadvice image-type (around image-type-bmp first (source &optional type data-p) activate)
(setq ad-return-value
(cond (data-p (if (string-match "^BM" source) 'bmp ad-do-it))
(t (if (string-match ".*//.bmp" source) 'bmp ad-do-it)))))
(defadvice create-image (around create-image-bmp (file-or-data &optional type data-p &rest props) activate)
(setq ad-return-value
(cond ((eq 'bmp type) (let (ppm-obj ppm-data)
(setq ppm-obj (ppm-from-bmp file-or-data))
(setq ppm-data (with-temp-buffer
(set-buffer-multibyte nil)
(with-slots ((w width)
(h height)
pixels) ppm-obj
(insert (format "P6 %d %d 255/r" w h))
(insert (concat pixels)))
(buffer-substring-no-properties (point-min) (point-max))))
(create-image ppm-data 'pbm t)))
(t ad-do-it))))
(add-to-list 'auto-mode-alist '("//.//(bmp//|BMP//)$" . image-mode))
 

 

然后你就把BMP文件往emacs里拖就可以拉。 C-cC-c也是可用,所谓完美就指这个。

 

善用这个例子,可以让emacs显示任何图片的喔。

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  emacs insert lisp rest