comparison lisp/image.el @ 69360:e5e4303f8050

(image-load-path-for-library): Merge at least three functions from Gnus and MH-E into this one function that can now be shared.
author Bill Wohler <wohler@newt.com>
date Fri, 10 Mar 2006 22:52:26 +0000
parents 3bd95f4f2941
children 0a2d89023fde 494bf720eaf0
comparison
equal deleted inserted replaced
69359:e184d816a2b1 69360:e5e4303f8050
74 74
75 (eval-at-startup 75 (eval-at-startup
76 (setq image-load-path 76 (setq image-load-path
77 (list (file-name-as-directory (expand-file-name "images" data-directory)) 77 (list (file-name-as-directory (expand-file-name "images" data-directory))
78 'data-directory 'load-path))) 78 'data-directory 'load-path)))
79
80 (defun image-load-path-for-library (library image &optional path)
81 "Return a suitable search path for images relative to LIBRARY.
82
83 Images for LIBRARY are searched for in \"../../etc/images\" and
84 \"../etc/images\" relative to the files in \"lisp/LIBRARY\" as
85 well as in `image-load-path' and `load-path'.
86
87 This function returns the value of `load-path' augmented with the
88 path to IMAGE. If PATH is given, it is used instead of
89 `load-path'.
90
91 Here is an example that uses a common idiom to provide
92 compatibility with versions of Emacs that lack the variable
93 `image-load-path':
94
95 (let ((load-path
96 (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
97 (image-load-path
98 (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\" 'image-load-path)))
99 (mh-tool-bar-folder-buttons-init))"
100 (unless library (error "No library specified"))
101 (unless image (error "No image specified"))
102 (let ((image-directory))
103 (cond
104 ;; Try relative setting.
105 ((let (library-name d1ei d2ei)
106 ;; First, find library in the load-path.
107 (setq library-name (locate-library library))
108 (if (not library-name)
109 (error "Cannot find library %s in load-path" library))
110 ;; And then set image-directory relative to that.
111 (setq
112 ;; Go down 2 levels.
113 d2ei (expand-file-name
114 (concat (file-name-directory library-name) "../../etc/images"))
115 ;; Go down 1 level.
116 d1ei (expand-file-name
117 (concat (file-name-directory library-name) "../etc/images")))
118 (setq image-directory
119 ;; Set it to nil if image is not found.
120 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
121 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
122 ;; Check for images in image-load-path or load-path.
123 ((let ((img image)
124 (dir (or
125 ;; Images in image-load-path.
126 (image-search-load-path image)
127 ;; Images in load-path.
128 (locate-library image)))
129 parent)
130 ;; Since the image might be in a nested directory (for
131 ;; example, mail/attach.pbm), adjust `image-directory'
132 ;; accordingly.
133 (and dir
134 (setq dir (file-name-directory dir))
135 (progn
136 (while (setq parent (file-name-directory img))
137 (setq img (directory-file-name parent)
138 dir (expand-file-name "../" dir)))
139 (setq image-directory dir)))))
140 (t
141 (error "Could not find image %s for library %s" image library)))
142
143 ;; Return augmented `image-load-path' or `load-path'.
144 (cond ((and path (symbolp path))
145 (nconc (list image-directory)
146 (delete image-directory
147 (if (boundp path)
148 (copy-sequence (symbol-value path))
149 nil))))
150 (t
151 (nconc (list image-directory)
152 (delete image-directory (copy-sequence load-path)))))))
79 153
80 (defun image-jpeg-p (data) 154 (defun image-jpeg-p (data)
81 "Value is non-nil if DATA, a string, consists of JFIF image data. 155 "Value is non-nil if DATA, a string, consists of JFIF image data.
82 We accept the tag Exif because that is the same format." 156 We accept the tag Exif because that is the same format."
83 (when (string-match "\\`\xff\xd8" data) 157 (when (string-match "\\`\xff\xd8" data)