iimage.el 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;;; iimage.el --- Inline image minor mode.
  2. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. ;; Author: KOSEKI Yoshinori <kose@meadowy.org>
  4. ;; Maintainer: KOSEKI Yoshinori <kose@meadowy.org>
  5. ;; Keywords: multimedia
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Iimage is a minor mode that displays images, when image-filename
  19. ;; exists in the buffer.
  20. ;; http://www.netlaputa.ne.jp/~kose/Emacs/iimage.html
  21. ;;
  22. ;; ** Display images in *Info* buffer.
  23. ;;
  24. ;; (add-hook 'info-mode-hook 'iimage-mode)
  25. ;;
  26. ;; .texinfo: @file{file://foo.png}
  27. ;; .info: `file://foo.png'
  28. ;;
  29. ;; ** Display images in Wiki buffer.
  30. ;;
  31. ;; (add-hook 'wiki-mode-hook 'iimage-mode)
  32. ;;
  33. ;; wiki-file: [[foo.png]]
  34. ;;; Code:
  35. (eval-when-compile
  36. (require 'image-file))
  37. (defgroup iimage nil
  38. "Support for inline images."
  39. :version "22.1"
  40. :group 'image)
  41. (defcustom iimage-mode-image-search-path nil
  42. "List of directories to search for image files for iimage-mode."
  43. :type '(choice (const nil) (repeat directory))
  44. :group 'iimage)
  45. (defvar iimage-mode-image-filename-regex
  46. (concat "[-+./_0-9a-zA-Z]+\\."
  47. (regexp-opt (nconc (mapcar #'upcase
  48. image-file-name-extensions)
  49. image-file-name-extensions)
  50. t)))
  51. (defcustom iimage-mode-image-regex-alist
  52. `((,(concat "\\(`?file://\\|\\[\\[\\|<\\|`\\)?"
  53. "\\(" iimage-mode-image-filename-regex "\\)"
  54. "\\(\\]\\]\\|>\\|'\\)?") . 2))
  55. "Alist of filename REGEXP vs NUM.
  56. Each element looks like (REGEXP . NUM).
  57. NUM specifies which parenthesized expression in the regexp.
  58. Examples of image filename patterns to match:
  59. file://foo.png
  60. `file://foo.png'
  61. \\[\\[foo.gif]]
  62. <foo.png>
  63. foo.JPG
  64. "
  65. :type '(alist :key-type regexp :value-type integer)
  66. :group 'iimage)
  67. (defvar iimage-mode-map
  68. (let ((map (make-sparse-keymap)))
  69. (define-key map "\C-l" 'iimage-recenter)
  70. map)
  71. "Keymap used in `iimage-mode'.")
  72. (defun iimage-recenter (&optional arg)
  73. "Re-draw images and recenter."
  74. (interactive "P")
  75. (iimage-mode-buffer nil)
  76. (iimage-mode-buffer t)
  77. (recenter arg))
  78. ;;;###autoload
  79. (define-obsolete-function-alias 'turn-on-iimage-mode 'iimage-mode "24.1")
  80. (defun turn-off-iimage-mode ()
  81. "Unconditionally turn off iimage mode."
  82. (interactive)
  83. (iimage-mode 0))
  84. (defun iimage-modification-hook (beg end)
  85. "Remove display property if a display region is modified."
  86. ;;(debug-print "ii1 begin %d, end %d\n" beg end)
  87. (let ((inhibit-modification-hooks t)
  88. (beg (previous-single-property-change end 'display
  89. nil (line-beginning-position)))
  90. (end (next-single-property-change beg 'display
  91. nil (line-end-position))))
  92. (when (and beg end (plist-get (text-properties-at beg) 'display))
  93. ;;(debug-print "ii2 begin %d, end %d\n" beg end)
  94. (remove-text-properties beg end
  95. '(display nil modification-hooks nil)))))
  96. (defun iimage-mode-buffer (arg)
  97. "Display images if ARG is non-nil, undisplay them otherwise."
  98. (let ((image-path (cons default-directory iimage-mode-image-search-path))
  99. file)
  100. (with-silent-modifications
  101. (save-excursion
  102. (goto-char (point-min))
  103. (dolist (pair iimage-mode-image-regex-alist)
  104. (while (re-search-forward (car pair) nil t)
  105. (when (and (setq file (match-string (cdr pair)))
  106. (setq file (locate-file file image-path)))
  107. ;; FIXME: we don't mark our images, so we can't reliably
  108. ;; remove them either (we may leave some of ours, and we
  109. ;; may remove other packages's display properties).
  110. (if arg
  111. (add-text-properties (match-beginning 0) (match-end 0)
  112. `(display ,(create-image file)
  113. modification-hooks
  114. (iimage-modification-hook)))
  115. (remove-text-properties (match-beginning 0) (match-end 0)
  116. '(display modification-hooks))))))))))
  117. ;;;###autoload
  118. (define-minor-mode iimage-mode nil
  119. :group 'iimage :lighter " iImg" :keymap iimage-mode-map
  120. (iimage-mode-buffer iimage-mode))
  121. (provide 'iimage)
  122. ;;; iimage.el ends here