iimage.el 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ;;; iimage.el --- Inline image minor mode.
  2. ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
  3. ;; Author: KOSEKI Yoshinori <kose@meadowy.org>
  4. ;; Maintainer: emacs-devel@gnu.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. ;;
  21. ;; ** Display images in *Info* buffer.
  22. ;;
  23. ;; (add-hook 'info-mode-hook 'iimage-mode)
  24. ;;
  25. ;; .texinfo: @file{file://foo.png}
  26. ;; .info: `file://foo.png'
  27. ;;
  28. ;; ** Display images in Wiki buffer.
  29. ;;
  30. ;; (add-hook 'wiki-mode-hook 'iimage-mode)
  31. ;;
  32. ;; wiki-file: [[foo.png]]
  33. ;;; Code:
  34. (eval-when-compile
  35. (require 'image-file))
  36. (defgroup iimage nil
  37. "Support for inline images."
  38. :version "22.1"
  39. :group 'image)
  40. (defcustom iimage-mode-image-search-path nil
  41. "List of directories to search for image files for iimage-mode."
  42. :type '(choice (const nil) (repeat directory))
  43. :group 'iimage)
  44. (defvar iimage-mode-image-filename-regex
  45. (concat "[-+./_0-9a-zA-Z]+\\."
  46. (regexp-opt (nconc (mapcar #'upcase
  47. image-file-name-extensions)
  48. image-file-name-extensions)
  49. t)))
  50. (defcustom iimage-mode-image-regex-alist
  51. `((,(concat "\\(`?file://\\|\\[\\[\\|<\\|`\\)?"
  52. "\\(" iimage-mode-image-filename-regex "\\)"
  53. "\\(\\]\\]\\|>\\|'\\)?") . 2))
  54. "Alist of filename REGEXP vs NUM.
  55. Each element looks like (REGEXP . NUM).
  56. NUM specifies which parenthesized expression in the regexp.
  57. Examples of image filename patterns to match:
  58. file://foo.png
  59. \\=`file://foo.png\\='
  60. \\[\\[foo.gif]]
  61. <foo.png>
  62. foo.JPG
  63. "
  64. :type '(alist :key-type regexp :value-type integer)
  65. :group 'iimage)
  66. (defvar iimage-mode-map
  67. (let ((map (make-sparse-keymap)))
  68. (define-key map "\C-l" 'iimage-recenter)
  69. map)
  70. "Keymap used in `iimage-mode'.")
  71. (defun iimage-recenter (&optional arg)
  72. "Re-draw images and recenter."
  73. (interactive "P")
  74. (iimage-mode-buffer nil)
  75. (iimage-mode-buffer t)
  76. (recenter arg))
  77. ;;;###autoload
  78. (define-obsolete-function-alias 'turn-on-iimage-mode 'iimage-mode "24.1")
  79. (defun turn-off-iimage-mode ()
  80. "Unconditionally turn off iimage mode."
  81. (interactive)
  82. (iimage-mode 0))
  83. (defun iimage-modification-hook (beg end)
  84. "Remove display property if a display region is modified."
  85. ;;(debug-print "ii1 begin %d, end %d\n" beg end)
  86. (let ((inhibit-modification-hooks t)
  87. (beg (previous-single-property-change end 'display
  88. nil (line-beginning-position)))
  89. (end (next-single-property-change beg 'display
  90. nil (line-end-position))))
  91. (when (and beg end (plist-get (text-properties-at beg) 'display))
  92. ;;(debug-print "ii2 begin %d, end %d\n" beg end)
  93. (remove-text-properties beg end
  94. '(display nil modification-hooks nil)))))
  95. (defun iimage-mode-buffer (arg)
  96. "Display images if ARG is non-nil, undisplay them otherwise."
  97. (let ((image-path (cons default-directory iimage-mode-image-search-path))
  98. file)
  99. (with-silent-modifications
  100. (save-excursion
  101. (goto-char (point-min))
  102. (dolist (pair iimage-mode-image-regex-alist)
  103. (while (re-search-forward (car pair) nil t)
  104. (when (and (setq file (match-string (cdr pair)))
  105. (setq file (locate-file file image-path)))
  106. ;; FIXME: we don't mark our images, so we can't reliably
  107. ;; remove them either (we may leave some of ours, and we
  108. ;; may remove other packages's display properties).
  109. (if arg
  110. (add-text-properties (match-beginning 0) (match-end 0)
  111. `(display ,(create-image file)
  112. modification-hooks
  113. (iimage-modification-hook)))
  114. (remove-text-properties (match-beginning 0) (match-end 0)
  115. '(display modification-hooks))))))))))
  116. ;;;###autoload
  117. (define-minor-mode iimage-mode nil
  118. :group 'iimage :lighter " iImg" :keymap iimage-mode-map
  119. (iimage-mode-buffer iimage-mode))
  120. (provide 'iimage)
  121. ;;; iimage.el ends here