123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- (require 'image)
- (defcustom image-file-name-extensions
- (purecopy '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm" "xpm" "pbm" "pgm" "ppm" "pnm" "svg"))
- "A list of image-file filename extensions.
- Filenames having one of these extensions are considered image files,
- in addition to those matching `image-file-name-regexps'.
- See `auto-image-file-mode'; if `auto-image-file-mode' is enabled,
- setting this variable directly does not take effect unless
- `auto-image-file-mode' is re-enabled; this happens automatically when
- the variable is set using \\[customize]."
- :type '(repeat string)
- :set (lambda (sym val)
- (set-default sym val)
- (when auto-image-file-mode
-
- (auto-image-file-mode t)))
- :initialize 'custom-initialize-default
- :group 'image)
- (defcustom image-file-name-regexps nil
- "List of regexps matching image-file filenames.
- Filenames matching one of these regexps are considered image files,
- in addition to those with an extension in `image-file-name-extensions'.
- See function `auto-image-file-mode'; if `auto-image-file-mode' is
- enabled, setting this variable directly does not take effect unless
- `auto-image-file-mode' is re-enabled; this happens automatically when
- the variable is set using \\[customize]."
- :type '(repeat regexp)
- :set (lambda (sym val)
- (set-default sym val)
- (when auto-image-file-mode
-
- (auto-image-file-mode t)))
- :initialize 'custom-initialize-default
- :group 'image)
- (defun image-file-name-regexp ()
- "Return a regular expression matching image-file filenames."
- (let ((exts-regexp
- (and image-file-name-extensions
- (concat "\\."
- (regexp-opt (nconc (mapcar #'upcase
- image-file-name-extensions)
- image-file-name-extensions)
- t)
- "\\'"))))
- (if image-file-name-regexps
- (mapconcat 'identity
- (if exts-regexp
- (cons exts-regexp image-file-name-regexps)
- image-file-name-regexps)
- "\\|")
- exts-regexp)))
- (defun insert-image-file (file &optional visit beg end replace)
- "Insert the image file FILE into the current buffer.
- Optional arguments VISIT, BEG, END, and REPLACE are interpreted as for
- the command `insert-file-contents'."
- (let ((rval
- (image-file-call-underlying #'insert-file-contents-literally
- 'insert-file-contents
- file visit beg end replace)))
-
-
- (when (and (or (null beg) (zerop beg)) (null end))
- (let* ((ibeg (point))
- (iend (+ (point) (cadr rval)))
- (visitingp (and visit (= ibeg (point-min)) (= iend (point-max))))
- (data
- (string-make-unibyte
- (buffer-substring-no-properties ibeg iend)))
- (image
- (create-image data nil t))
- (props
- `(display ,image
- yank-handler
- (image-file-yank-handler nil t)
- intangible ,image
- rear-nonsticky (display intangible)
-
-
-
- ,@(and visitingp
- '(read-only t front-sticky (read-only))))))
- (add-text-properties ibeg iend props)
- (when visitingp
-
-
- (setq cursor-type nil)
-
-
- (setq truncate-lines t))))
- rval))
- (defun image-file-yank-handler (string)
- "Yank handler for inserting an image into a buffer."
- (let ((len (length string))
- (image (get-text-property 0 'display string)))
- (remove-text-properties 0 len yank-excluded-properties string)
- (if (consp image)
- (add-text-properties 0
- (or (next-single-property-change 0 'image-counter string)
- (length string))
- `(display
- ,(cons (car image) (cdr image))
- yank-handler
- ,(cons 'image-file-yank-handler '(nil t)))
- string))
- (insert string)))
- (put 'image-file-handler 'safe-magic t)
- (defun image-file-handler (operation &rest args)
- "Filename handler for inserting image files.
- OPERATION is the operation to perform, on ARGS.
- See `file-name-handler-alist' for details."
- (if (and (eq operation 'insert-file-contents)
- auto-image-file-mode)
- (apply #'insert-image-file args)
-
- (apply #'image-file-call-underlying operation operation args)))
- (defun image-file-call-underlying (function operation &rest args)
- "Call FUNCTION with `image-file-handler' and OPERATION inhibited.
- Optional argument ARGS are the arguments to call FUNCTION with."
- (let ((inhibit-file-name-handlers
- (cons 'image-file-handler
- (and (eq inhibit-file-name-operation operation)
- inhibit-file-name-handlers)))
- (inhibit-file-name-operation operation))
- (apply function args)))
- (define-minor-mode auto-image-file-mode
- "Toggle visiting of image files as images (Auto Image File mode).
- With a prefix argument ARG, enable Auto Image File mode if ARG is
- positive, and disable it otherwise. If called from Lisp, enable
- the mode if ARG is omitted or nil.
- An image file is one whose name has an extension in
- `image-file-name-extensions', or matches a regexp in
- `image-file-name-regexps'."
- :global t
- :group 'image
-
- (let ((existing-entry
- (rassq 'image-file-handler file-name-handler-alist)))
- (when existing-entry
- (setq file-name-handler-alist
- (delq existing-entry file-name-handler-alist))))
-
- (when auto-image-file-mode
- (push (cons (image-file-name-regexp) 'image-file-handler)
- file-name-handler-alist)))
- (provide 'image-file)
|