;;; Code:
+(require 'wid-edit)
+
(defgroup gmm nil
"Utility functions for Gnus, Message and MML"
:prefix "gmm-"
;; version will provide customizable tool bar buttons using a different
;; interface.
+;; TODO: Extend API so that the "Command" entry can be a function or a plist.
+;; In case of a list it should have the format...
+;;
+;; (:none command-without-modifier
+;; :shift command-with-shift-pressed
+;; :control command-with-ctrl-pressed
+;; :control-shift command-with-control-and-shift-pressed
+;; ;; mouse-2 and mouse-3 can't be used in Emacs yet.
+;; :mouse-2 command-on-mouse-2-press
+;; :mouse-3 command-on-mouse-3-press) ;; typically a menu of related commands
+;;
+;; Combinations of mouse-[23] plus shift and/or controll might be overkill.
+;;
+;; Then use (plist-get rs-command :none), (plist-get rs-command :shift)
+
(define-widget 'gmm-tool-bar-item (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
"Tool bar list item."
:tag "Tool bar item"
:tag "Other"
(symbol :tag "Icon item")))))
+;; (defun gmm-color-cells (&optional display)
+;; "Return the number of color cells supported by DISPLAY.
+;; Compatibility function."
+;; ;; `display-color-cells' doesn't return more than 256 even if color depth is
+;; ;; > 8 in Emacs 21.
+;; ;;
+;; ;; Feel free to add proper XEmacs support.
+;; (let* ((cells (and (fboundp 'display-color-cells)
+;; (display-color-cells display)))
+;; (plane (and (fboundp 'x-display-planes)
+;; (ash 1 (x-display-planes))))
+;; (none -1))
+;; (max (if (integerp cells) cells none)
+;; (if (integerp plane) plane none))))
+
+(defcustom gmm-tool-bar-style
+ (if (and (boundp 'tool-bar-mode)
+ tool-bar-mode
+ (and (fboundp 'display-visual-class)
+ (not (memq (display-visual-class)
+ (list 'static-gray 'gray-scale
+ 'static-color 'pseudo-color)))))
+ 'gnome
+ 'retro)
+ "Prefered tool bar style."
+ :type '(choice (const :tag "GNOME style" 'gnome)
+ (const :tag "Retro look" 'retro))
+ :group 'gmm)
+
(defvar tool-bar-map)
;;;###autoload
icon-list))
tool-bar-map))
-;; WARNING: The following is subject to change. Don't rely on it yet.
-
-;; From MH-E without modifications:
-
-(defmacro gmm-defun-compat (name function arg-list &rest body)
+(defmacro defun-gmm (name function arg-list &rest body)
"Create function NAME.
If FUNCTION exists, then NAME becomes an alias for FUNCTION.
Otherwise, create function NAME with ARG-LIST and BODY."
`(defalias ',name ',function)
`(defun ,name ,arg-list ,@body))))
-(gmm-defun-compat gmm-image-search-load-path
+(defun-gmm gmm-image-search-load-path
image-search-load-path (file &optional path)
"Emacs 21 and XEmacs don't have `image-search-load-path'.
This function returns nil on those systems."
nil)
-;; From MH-E with modifications:
+;; Cf. `mh-image-load-path-for-library' in `mh-compat.el'.
-(defvar gmm-image-load-path nil
- "Directory where images are found.
-See the function `gmm-image-load-path'.")
+(defun-gmm gmm-image-load-path-for-library
+ image-load-path-for-library (library image &optional path no-error)
+ "Return a suitable search path for images used by LIBRARY.
-(defun gmm-image-load-path (library image &optional path)
- "Return a suitable search path for images of LIBRARY.
+It searches for IMAGE in `image-load-path' (excluding
+\"`data-directory'/images\") and `load-path', followed by a path
+suitable for LIBRARY, which includes \"../../etc/images\" and
+\"../etc/images\" relative to the library file itself, and then
+in \"`data-directory'/images\".
-Images for LIBRARY are found in \"../../etc/images\" relative to
-the files in \"lisp/LIBRARY\", in `image-load-path', or in
+Then this function returns a list of directories which contains
+first the directory in which IMAGE was found, followed by the
+value of `load-path'. If PATH is given, it is used instead of
`load-path'.
-This function returns value of `load-path' augmented with the
-path to IMAGE. If PATH is given, it is used instead of
-`load-path'."
- (unless library (error "No library specified."))
- (unless image (error "No image specified."))
- (cond (gmm-image-load-path) ;; User setting exists.
- ((let (gmm-library-name) ;; Try relative setting
- ;; First, find library in the load-path.
- (setq gmm-library-name (locate-library library))
- (if (not gmm-library-name)
- (error "Cannot find library `%s' in load-path" library))
- ;; And then set gmm-image-load-path relative to that.
- (setq gmm-image-load-path
- (expand-file-name (concat
- (file-name-directory gmm-library-name)
- "../../etc/images")))
- (file-exists-p (expand-file-name image gmm-image-load-path))))
- ((gmm-image-search-load-path image)
- ;; Images in image-load-path.
- (setq gmm-image-load-path
- (file-name-directory (gmm-image-search-load-path image))))
- ((locate-library image)
- ;; Images in load-path.
- (setq gmm-image-load-path
- (file-name-directory (locate-library image)))))
- ;;
- (unless (file-exists-p gmm-image-load-path)
- (error "Directory `%s' in gmm-image-load-path does not exist"
- gmm-image-load-path))
- (unless (file-exists-p (expand-file-name image gmm-image-load-path))
- (error "Directory `%s' in gmm-image-load-path does not contain image `%s'."
- gmm-image-load-path image))
- ;; Return augmented `image-load-path' or `load-path'.
- (cond ((and path (symbolp path))
- (nconc (list gmm-image-load-path)
- (delete gmm-image-load-path (if (boundp path)
- (symbol-value path)
- nil))))
- (t
- (nconc (list gmm-image-load-path)
- (delete gmm-image-load-path load-path)))))
+If NO-ERROR is non-nil and a suitable path can't be found, don't
+signal an error. Instead, return a list of directories as before,
+except that nil appears in place of the image directory.
+
+Here is an example that uses a common idiom to provide
+compatibility with versions of Emacs that lack the variable
+`image-load-path':
+
+ ;; Shush compiler.
+ (defvar image-load-path)
+
+ (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
+ (image-load-path (cons (car load-path)
+ (when (boundp 'image-load-path)
+ image-load-path))))
+ (mh-tool-bar-folder-buttons-init))"
+ (unless library (error "No library specified"))
+ (unless image (error "No image specified"))
+ (let (image-directory image-directory-load-path)
+ ;; Check for images in image-load-path or load-path.
+ (let ((img image)
+ (dir (or
+ ;; Images in image-load-path.
+ (gmm-image-search-load-path image) ;; "gmm-" prefix!
+ ;; Images in load-path.
+ (locate-library image)))
+ parent)
+ ;; Since the image might be in a nested directory (for
+ ;; example, mail/attach.pbm), adjust `image-directory'
+ ;; accordingly.
+ (when dir
+ (setq dir (file-name-directory dir))
+ (while (setq parent (file-name-directory img))
+ (setq img (directory-file-name parent)
+ dir (expand-file-name "../" dir))))
+ (setq image-directory-load-path dir))
+
+ ;; If `image-directory-load-path' isn't Emacs' image directory,
+ ;; it's probably a user preference, so use it. Then use a
+ ;; relative setting if possible; otherwise, use
+ ;; `image-directory-load-path'.
+ (cond
+ ;; User-modified image-load-path?
+ ((and image-directory-load-path
+ (not (equal image-directory-load-path
+ (file-name-as-directory
+ (expand-file-name "images" data-directory)))))
+ (setq image-directory image-directory-load-path))
+ ;; Try relative setting.
+ ((let (library-name d1ei d2ei)
+ ;; First, find library in the load-path.
+ (setq library-name (locate-library library))
+ (if (not library-name)
+ (error "Cannot find library %s in load-path" library))
+ ;; And then set image-directory relative to that.
+ (setq
+ ;; Go down 2 levels.
+ d2ei (file-name-as-directory
+ (expand-file-name
+ (concat (file-name-directory library-name) "../../etc/images")))
+ ;; Go down 1 level.
+ d1ei (file-name-as-directory
+ (expand-file-name
+ (concat (file-name-directory library-name) "../etc/images"))))
+ (setq image-directory
+ ;; Set it to nil if image is not found.
+ (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
+ ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
+ ;; Use Emacs' image directory.
+ (image-directory-load-path
+ (setq image-directory image-directory-load-path))
+ (no-error
+ (message "Could not find image %s for library %s" image library))
+ (t
+ (error "Could not find image %s for library %s" image library)))
+
+ ;; Return an augmented `path' or `load-path'.
+ (nconc (list image-directory)
+ (delete image-directory (copy-sequence (or path load-path))))))
+
+(defun gmm-customize-mode (&optional mode)
+ "Customize customization group for MODE.
+If mode is nil, use `major-mode' of the curent buffer."
+ (interactive)
+ (customize-group
+ (or mode
+ (intern (let ((mode (symbol-name major-mode)))
+ (string-match "^\\(.+\\)-mode$" mode)
+ (match-string 1 mode))))))
(provide 'gmm-utils)