initial commit
[emacs-init.git] / nxhtml / util / gimpedit.el
1 ;;; gimpedit.el --- Edit files with GIMP
2 ;;
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: Wed May 23 14:59:50 2007
5 (defconst gimpedit:version "0.31") ;;Version:
6 ;; Last-Updated: 2009-11-03 Tue
7 ;; URL:
8 ;; Keywords:
9 ;; Compatibility:
10 ;;
11 ;; Features that might be required by this library:
12 ;;
13   ;; `setup-helper', `w32-reg-iface', `w32-regdat'.
14 ;;
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 ;;
17 ;;; Commentary:
18 ;;
19 ;; Simple interface to start editing with GIMP.
20 ;;
21 ;; If you want to edit files from within Emacs see the doc string of
22 ;; `gimpedit-edit-buffer'.
23 ;;
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;
26 ;;; Change log:
27 ;;
28 ;;
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;;
31 ;; This program is free software; you can redistribute it and/or
32 ;; modify it under the terms of the GNU General Public License as
33 ;; published by the Free Software Foundation; either version 2, or
34 ;; (at your option) any later version.
35 ;;
36 ;; This program is distributed in the hope that it will be useful,
37 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
38 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39 ;; General Public License for more details.
40 ;;
41 ;; You should have received a copy of the GNU General Public License
42 ;; along with this program; see the file COPYING.  If not, write to
43 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
44 ;; Floor, Boston, MA 02110-1301, USA.
45 ;;
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 ;;
48 ;;; Code:
49
50 (eval-and-compile (require 'w32-regdat nil t))
51
52 ;; (message "%S" (gimpedit-get-remote-command))
53 (defun gimpedit-get-remote-command ()
54   (if (featurep 'w32-regdat)
55       (save-match-data
56         (let ((cmd (w32-regdat-gimp-win-remote-cmd))
57               cmd-list)
58           (while (< 0 (length cmd))
59             (cond
60              ((or (string-match (rx string-start
61                                     ?\"
62                                     (submatch
63                                      (0+ (not (any ?\"))))
64                                     ?\"
65                                     (0+ space))
66                                 cmd)
67                   (string-match (rx string-start
68                                     (submatch
69                                      (0+ (not (any space))))
70                                     (0+ space))
71                                 cmd))
72               (setq cmd-list (cons (match-string-no-properties 1 cmd) cmd-list))
73               (setq cmd (substring cmd (match-end 0))))))
74           (cadr cmd-list)))
75     (if (memq system-type '(windows-nt))
76         (let (prog)
77           (catch 'found-prog
78             (dolist (num '(2 3 4 5 6 7 8 9))
79               (setq prog (concat (getenv "ProgramFiles")
80                                  "\\GIMP-2.0\\bin\\gimp-2."
81                                  (number-to-string num)
82                                  ".exe"))
83               (when (file-exists-p prog)
84                 (throw 'found-prog prog)))))
85       "gimp")))
86
87 ;;;###autoload
88 (defgroup gimpedit nil
89   "Customization group for GIMP."
90   :group 'external
91   :group 'nxhtml)
92
93 (defcustom gimpedit-remote-command (gimpedit-get-remote-command)
94   "Program name to use when calling GIMP remotely.
95 This could be be the full path to the program used when opening
96 files with GIMP or a just the program file name if it is in the
97 executables path.
98
99 Example:
100
101   The value is fetched from the registry on MS Windows if
102   possible or is else given the default value:
103
104     \"C:\\Program Files\\GIMP-2.0\\bin\\gimp-2.6.exe\"
105
106   On other system it has the default value
107
108     \"gimp\"."
109   :type '(choice (file :tag "Full file name" :must-match t)
110                  (string :tag "File name (must be in path)"))
111   :group 'gimpedit)
112
113 ;;;###autoload
114 (defun gimpedit-edit-file (image-file &optional extra-args)
115   "Edit IMAGE-FILE with GIMP.
116 See also `gimpedit-edit-file'."
117   (interactive (list (or (get-char-property (point) 'image-file)
118                          (read-file-name "Image to edit in GIMP: "))))
119   (setq image-file (expand-file-name image-file))
120   (apply 'call-process gimpedit-remote-command
121          nil
122          0
123          nil
124          (reverse (cons image-file (reverse extra-args))))
125   (let ((msg " Asked GIMP to open %s - you may have to switch to GIMP"))
126     (put-text-property 0 (length msg) 'face 'highlight msg)
127     (message msg (file-name-nondirectory image-file))))
128
129 ;;;###autoload
130 (defun gimpedit-edit-buffer ()
131   "Edit image file in current buffer with GIMP.
132 See also `gimpedit-edit-file'.
133
134 You may also be interested in gimpedit-mode with which you can edit
135 gimp files from within Emacs using GIMP's scripting
136 possibilities. See
137
138   URL `http://www.emacswiki.org/emacs/GimpMode'"
139   (interactive)
140   (unless (buffer-file-name)
141     (error
142      "Can't edit in GIMP because this buffer does not have a file name."))
143   (gimpedit-edit-file (buffer-file-name)))
144
145 ;;;###autoload
146 (defun gimpedit-can-edit (file-name)
147   (and file-name
148        (member (downcase (file-name-extension file-name))
149                '("png" "gif" "jpg" "jpeg"))))
150
151 ;; (defcustom gimpedit-point-key-bindings '(([(control ?c) ?&] gimpedit-edit-file))
152 ;;   "Key bindings suggested for image links etc."
153 ;;   :type '(repeat (list key-sequence function))
154 ;;   :group 'gimpedit)
155
156 ;; (defun gimpedit-add-point-bindings (map)
157 ;;   "Add `gimpedit-point-key-bindings' to point keymap MAP.
158 ;; Set it up like this:
159
160 ;;   (eval-after-load 'gimpedit
161 ;;     '(gimpedit-add-point-bindings MY-MAP))
162
163 ;; There must also be a character property `image-file' at point for this
164 ;; to work."
165 ;;   (dolist (binding gimpedit-point-key-bindings)
166 ;;     (let ((key (nth 0 binding))
167 ;;           (fun (nth 1 binding)))
168 ;;       (define-key map key fun))))
169
170 (provide 'gimpedit)
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 ;;; gimpedit.el ends here