Function: image-toggle-display-image

image-toggle-display-image is a byte-compiled function defined in image-mode.el.gz.

Signature

(image-toggle-display-image)

Documentation

Show the image of the image file.

Turn the image data into a real image, but only if the whole file was inserted.

Probably introduced at or before Emacs version 23.2.

Source Code

;; Defined in /usr/src/emacs/lisp/image-mode.el.gz
(defun image-toggle-display-image ()
  "Show the image of the image file.
Turn the image data into a real image, but only if the whole file
was inserted."
  (unless (derived-mode-p 'image-mode)
    (error "The buffer is not in Image mode"))
  (let* ((filename (buffer-file-name))
	 (data-p (not (and filename
			   (file-readable-p filename)
			   (not (file-remote-p filename))
			   (not (buffer-modified-p))
			   (not (and (boundp 'archive-superior-buffer)
				     archive-superior-buffer))
			   (not (and (boundp 'tar-superior-buffer)
				     tar-superior-buffer))
                           ;; This means the buffer holds the contents
                           ;; of a file uncompressed by jka-compr.el.
                           (not (and (local-variable-p
                                      'jka-compr-really-do-compress)
                                     jka-compr-really-do-compress))
                           ;; This means the buffer holds the
                           ;; decrypted content (bug#21870).
                           (not (local-variable-p
                                 'epa-file-encrypt-to)))))
	 (file-or-data
          (if data-p
	      (let ((str
		     (buffer-substring-no-properties (point-min) (point-max))))
                (if enable-multibyte-characters
                    (encode-coding-string str buffer-file-coding-system)
                  str))
	    filename))
	 ;; If we have a `fit-width' or a `fit-height', don't limit
	 ;; the size of the image to the window size.
         (edges (when (or (eq image-transform-resize t)
                          (eq image-transform-resize 'fit-window))
		  (window-inside-pixel-edges (get-buffer-window))))
	 (max-width (when edges
		      (- (nth 2 edges) (nth 0 edges))))
	 (max-height (when edges
		       (- (nth 3 edges) (nth 1 edges))))
	 (inhibit-read-only t)
	 (buffer-undo-list t)
	 (modified (buffer-modified-p))
	 props image type)

    ;; If the data in the current buffer isn't from an existing file,
    ;; but we have a file name (this happens when visiting images from
    ;; a zip file, for instance), provide a type hint based on the
    ;; suffix.
    (when (and data-p filename)
      (setq data-p (intern (format "image/%s"
                                   (file-name-extension filename)))))
    (setq type (if (image--imagemagick-wanted-p filename)
		   'imagemagick
		 (image-type file-or-data nil data-p)))

    ;; Get the rotation data from the file, if any.
    (when (zerop image-transform-rotation) ; don't reset modified value
      (setq image-transform-rotation
            (or (exif-orientation
                 (ignore-error exif-error
                   ;; exif-parse-buffer can move point, so preserve it.
                   (save-excursion
                     (exif-parse-buffer))))
                0.0)))
    ;; Swap width and height when changing orientation
    ;; between portrait and landscape.
    (when (and edges (zerop (mod (+ image-transform-rotation 90) 180)))
      (setq max-width (prog1 max-height (setq max-height max-width))))

    ;; :scale 1: If we do not set this, create-image will apply
    ;; default scaling based on font size.
    (setq image (if (not edges)
		    (create-image file-or-data type data-p :scale 1
                                  :format (and filename data-p))
		  (create-image file-or-data type data-p :scale 1
				:max-width max-width
				:max-height max-height
                                ;; Type hint.
                                :format (and filename data-p))))

    ;; Handle `fit-window'.
    (when (and (eq image-transform-resize 'fit-window)
               (image--scale-within-limits-p image))
      (setq image
            (cons (car image)
                  (plist-put (cdr image) :width
                             (plist-get (cdr image) :max-width)))))

    ;; Discard any stale image data before looking it up again.
    (image-flush image)
    (setq image (image--update-properties image (image-transform-properties image)))
    (setq props
	  `(display ,image
		    ;; intangible ,image
		    rear-nonsticky (display) ;; intangible
		    read-only t front-sticky (read-only)))

    (let ((create-lockfiles nil)) ; avoid changing dir mtime by lock_file
      (add-text-properties (point-min) (point-max) props)
      (restore-buffer-modified-p modified))
    ;; Inhibit the cursor when the buffer contains only an image,
    ;; because cursors look very strange on top of images.
    (setq cursor-type nil)
    ;; This just makes the arrow displayed in the right fringe
    ;; area look correct when the image is wider than the window.
    (setq truncate-lines t)
    ;; Disable adding a newline at the end of the image file when it
    ;; is written with, e.g., C-x C-w.
    (if (coding-system-equal (coding-system-base buffer-file-coding-system)
			     'no-conversion)
	(setq-local find-file-literally t))
    ;; Allow navigation of large images.
    (setq-local auto-hscroll-mode nil)
    (setq image-type type)
    (if (eq major-mode 'image-mode)
	(setq mode-name (format "Image[%s]" type)))
    (image-transform-check-size)
    (if (called-interactively-p 'any)
	(message "Repeat this command to go back to displaying the file as text"))))