1 ;;; inlimg.el --- Display images inline
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
5 (defconst inlimg:version "0.7") ;; Version:
6 ;; Last-Updated: 2009-07-14 Tue
11 ;; Features that might be required by this library:
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 ;; Display images inline. See `inlimg-mode' for more information.
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This program is free software; you can redistribute it and/or
29 ;; modify it under the terms of the GNU General Public License as
30 ;; published by the Free Software Foundation; either version 2, or
31 ;; (at your option) any later version.
33 ;; This program is distributed in the hope that it will be useful,
34 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
35 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 ;; General Public License for more details.
38 ;; You should have received a copy of the GNU General Public License
39 ;; along with this program; see the file COPYING. If not, write to
40 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
41 ;; Floor, Boston, MA 02110-1301, USA.
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 (eval-when-compile (require 'cl))
48 (eval-when-compile (require 'mumamo nil t))
49 (eval-when-compile (require 'ourcomments-util nil t))
51 (defvar inlimg-assoc-ext
55 (jpeg (".jpg" ".jpeg"))
60 (defvar inlimg-img-regexp nil)
61 (make-variable-buffer-local 'inlimg-img-regexp)
62 (put 'inlimg-img-regexp 'permanent-local t)
64 (defvar inlimg-img-regexp-html
67 (0+ (1+ (not (any " <>")))
70 (group (1+ (not (any "\""))))
76 (group (1+ (not (any "\)"))))
81 (group (+? (not (any ")"))))
86 (defvar inlimg-img-regexp-org
89 (group (+? (not (any "\]")))
91 (dolist (typ image-types)
92 (when (image-type-available-p typ)
93 (dolist (ext (cadr (assoc typ inlimg-assoc-ext)))
94 (setq types (cons ext types)))))
103 (defconst inlimg-modes-img-values
105 (html-mode inlimg-img-regexp-html)
106 (org-mode inlimg-img-regexp-org)
109 (defun inlimg-img-spec-p (spec)
110 (assoc spec inlimg-modes-img-values))
114 "Customization group for inlimg."
117 (defcustom inlimg-margins '(50 . 5)
118 "Margins when displaying image."
119 :type '(cons (integer :tag "Left margin")
120 (integer :tag "Top margin"))
121 :set (lambda (sym val)
122 (set-default sym val)
123 (when (fboundp 'inlimg-update-all-buffers)
124 (inlimg-update-all-buffers)))
127 (defcustom inlimg-slice '(0 0 400 100)
128 "How to slice images."
129 :type '(choice (const :tag "Show whole images" nil)
130 (list :tag "Show slice of image"
132 (integer :tag "Left")
133 (integer :tag "Width")
134 (integer :tag "Height")))
135 :set (lambda (sym val)
136 (set-default sym val)
137 (when (fboundp 'inlimg-update-all-buffers)
138 (inlimg-update-all-buffers)))
141 (define-widget 'inlimg-spec-widget 'symbol
142 "An inline image specification."
143 :complete-function (lambda ()
145 (lisp-complete-symbol 'inlimg-img-spec-p))
146 :prompt-match 'inlimg-img-spec-p
147 :prompt-history 'widget-function-prompt-value-history
148 :match-alternatives '(inlimg-img-spec-p)
149 :validate (lambda (widget)
150 (unless (inlimg-img-spec-p (widget-value widget))
151 (widget-put widget :error (format "Invalid function: %S"
152 (widget-value widget)))
155 :tag "Inlimg image values spec name")
157 ;; (customize-option 'inlimg-mode-specs)
158 (defcustom inlimg-mode-specs
161 (sgml-mode html-mode)
162 (nxml-mode html-mode)
166 "Equivalent mode for image tag search.
167 Note that derived modes \(see info) are recognized by default.
169 To add new image tag patterns modify `inlimg-modes-img-values'."
171 (list (major-mode-function :tag "Major mode")
172 (inlimg-spec-widget :tag "Use tags as specified in")))
175 (defface inlimg-img-tag '((t :inherit 'lazy-highlight))
176 "Face added to img tag when displaying image."
179 (defface inlimg-img-remote '((t :inherit 'isearch-fail))
180 "Face used for notes telling image is remote."
183 (defface inlimg-img-missing '((t :inherit 'trailing-whitespace))
184 "Face used for notes telling image is missing."
187 (defvar inlimg-img-keymap
188 (let ((map (make-sparse-keymap)))
189 (define-key map [(control ?c) ?+] 'inlimg-toggle-display)
190 (define-key map [(control ?c) ?%] 'inlimg-toggle-slicing)
192 "Keymap on image overlay.")
194 (eval-after-load 'gimp
195 '(gimp-add-point-bindings inlimg-img-keymap))
197 (defsubst inlimg-ovl-p (ovl)
198 "Return non-nil if OVL is an inlimg image overlay."
199 (overlay-get ovl 'inlimg-img))
201 (defun inlimg-ovl-valid-p (ovl)
202 (and (overlay-get ovl 'inlimg-img)
205 (let ((here (point)))
206 (goto-char (overlay-start ovl))
208 (looking-at (symbol-value inlimg-img-regexp))
209 (goto-char here))))))
211 (defun inlimg-next (pt display-image)
212 "Display or hide next image after point PT.
213 If DISPLAY-IMAGE is non-nil then display image, otherwise hide it.
215 Return non-nil if an img tag was found."
216 (when inlimg-img-regexp
217 (let (src dir beg end img ovl remote beg-face)
220 (when (re-search-forward (symbol-value inlimg-img-regexp) nil t)
221 (setq src (or (match-string-no-properties 1)
222 (match-string-no-properties 2)
223 (match-string-no-properties 3)))
224 (setq beg (match-beginning 0))
225 (setq beg-face (get-text-property beg 'face))
226 (setq remote (string-match "^https?://" src))
227 (setq end (- (line-end-position) 0))
228 (setq ovl (catch 'old-ovl
229 (dolist (ovl (overlays-at beg))
230 (when (inlimg-ovl-p ovl)
231 (throw 'old-ovl ovl)))
234 (setq ovl (make-overlay beg end))
235 (overlay-put ovl 'inlimg-img t)
236 (overlay-put ovl 'priority 100)
237 (overlay-put ovl 'face 'inlimg-img-tag)
238 (overlay-put ovl 'keymap inlimg-img-keymap))
239 (overlay-put ovl 'image-file src)
240 (overlay-put ovl 'inlimg-slice inlimg-slice)
242 (unless (memq beg-face '(font-lock-comment-face font-lock-string-face))
244 (setq dir (if (buffer-file-name)
245 (file-name-directory (buffer-file-name))
247 (setq src (expand-file-name src dir)))
248 (if (or remote (not (file-exists-p src)))
249 (setq img (propertize
250 (if remote " Image is on the web " " Image not found ")
251 'face (if remote 'inlimg-img-remote 'inlimg-img-missing)))
252 (setq img (create-image src nil nil
254 :margin inlimg-margins))
255 (setq img (inlimg-slice-img img inlimg-slice)))
256 (let ((str (copy-sequence "\nX")))
257 (setq str (propertize str 'face 'inlimg-img-tag))
258 (put-text-property 1 2 'display img str)
259 (overlay-put ovl 'after-string str)))
260 (overlay-put ovl 'after-string nil))))
263 (defun inlimg-slice-img (img slice)
266 (let* ((sizes (image-size img t))
269 (sl-left (nth 0 slice))
270 (sl-top (nth 1 slice))
271 (sl-width (nth 2 slice))
272 (sl-height (nth 3 slice)))
273 (when (> sl-left width) (setq sl-left 0))
274 (when (> (+ sl-left sl-width) width) (setq sl-width (- width sl-left)))
275 (when (> sl-top height) (setq sl-top 0))
276 (when (> (+ sl-top sl-height) height) (setq sl-height (- height sl-top)))
277 (setq img (list img))
278 (setq img (cons (append '(slice)
280 (list sl-top sl-left sl-width sl-height)
285 (define-minor-mode inlimg-mode
286 "Display images inline.
287 Search buffer for image tags. Display found images.
289 Image tags are setup per major mode in `inlimg-mode-specs'.
291 Images are displayed on a line below the tag referencing them.
292 The whole image or a slice of it may be displayed, see
293 `inlimg-slice'. Margins relative text are specified in
296 See also the commands `inlimg-toggle-display' and
297 `inlimg-toggle-slicing'.
299 Note: This minor mode uses `font-lock-mode'."
304 (let ((major-mode (or (and (boundp 'mumamo-multi-major-mode)
305 mumamo-multi-major-mode
306 (fboundp 'mumamo-main-major-mode)
307 (mumamo-main-major-mode))
309 (inlimg-get-buffer-img-values)
310 (unless inlimg-img-regexp
311 (message "inlim-mode: No image spec, can't do anything"))
312 (add-hook 'font-lock-mode-hook 'inlimg-on-font-lock-off))
313 (inlimg-font-lock t))
314 (inlimg-font-lock nil)
315 (inlimg-delete-overlays)))
316 (put 'inlimg-mode 'permanent-local t)
318 (defun inlimg-delete-overlays ()
322 (dolist (ovl (overlays-in (point-min) (point-max)))
323 (when (inlimg-ovl-p ovl)
324 (delete-overlay ovl))))))
326 (defun inlimg-get-buffer-img-values ()
328 (spec (or (catch 'spec
329 (dolist (rec inlimg-mode-specs)
330 (when (derived-mode-p (car rec))
331 (throw 'spec (nth 1 rec)))))
333 (values (when spec (nth 1 (assoc spec inlimg-modes-img-values))))
335 (setq inlimg-img-regexp values)
338 (defun inlimg--global-turn-on ()
339 (inlimg-get-buffer-img-values)
340 (when inlimg-img-regexp
344 (define-globalized-minor-mode inlimg-global-mode inlimg-mode inlimg--global-turn-on)
347 (defun inlimg-toggle-display (point)
348 "Toggle display of image at point POINT.
349 See also the command `inlimg-mode'."
350 (interactive (list (point)))
354 (dolist (ovl (overlays-at (point)))
355 (when (inlimg-ovl-p ovl)
359 (message "No image at point %s" here)
360 (setq is-displayed (overlay-get ovl 'after-string))
361 (inlimg-next (overlay-start ovl) (not is-displayed))
365 (defun inlimg-toggle-slicing (point)
366 "Toggle slicing of image at point POINT.
367 See also the command `inlimg-mode'."
368 (interactive (list (point)))
369 (let* ((here (point))
372 (dolist (ovl (overlays-at (point)))
373 (when (inlimg-ovl-p ovl)
375 (inlimg-slice inlimg-slice)
378 (message "No image at point %s" here)
379 (setq is-displayed (overlay-get ovl 'after-string))
380 (when (overlay-get ovl 'inlimg-slice)
381 (setq inlimg-slice nil))
382 (inlimg-next (overlay-start ovl) is-displayed)
386 (defun inlimg-font-lock-fun (bound)
388 old-ovls new-ovls ovl)
389 (goto-char (line-beginning-position))
390 (dolist (ovl (overlays-in (point) bound))
391 (when (inlimg-ovl-p ovl)
392 (setq old-ovls (cons ovl old-ovls))))
393 (while (and (< (point) bound)
394 (setq ovl (inlimg-next (point) t)))
395 (setq new-ovls (cons ovl new-ovls)))
396 (dolist (ovl old-ovls)
397 (unless (inlimg-ovl-valid-p ovl)
401 ;; Fix-me: This stops working for changes with nxhtml-mumamo-mode, but
402 ;; works for nxhtml-mode and html-mumamo-mode...
403 (defvar inlimg-this-is-not-font-lock-off nil)
404 (defun inlimg-font-lock (on)
405 (let ((add-or-remove (if on 'font-lock-add-keywords 'font-lock-remove-keywords))
407 (funcall add-or-remove nil
408 `((inlimg-font-lock-fun
412 (let ((inlimg-this-is-not-font-lock-off t)
413 (mumamo-multi-major-mode nil))
415 (font-lock-mode 1))))
417 (defun inlimg-on-font-lock-off ()
418 (unless (or inlimg-this-is-not-font-lock-off
419 (and (boundp 'mumamo-multi-major-mode)
420 mumamo-multi-major-mode))
424 (put 'inlimg-on-font-lock-off 'permanent-local-hook t)
428 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429 ;;; inlimg.el ends here