Subject: 24.4.50; function value for `help-echo': mouseover & frame

From: Drew Adams <[email-masked]>
Date: Thu, 24 Apr 2014 09:05:17 -0700 (PDT)

emacs -Q

1. Set things up so that image-dired can work.  On Windows, I load
library cygwin-mount.el and then setup-cygwin.el (both on Emacs Wiki).

2. Evaluate this code.  It makes it so that a mouseover on an image file
name in Dired shows a thumbnail of the image in a tooltip.

(require 'image-dired)
(tooltip-mode 1)

;; Like `image-dired-create-thumb', but single file & returns thumbnail.
(defun diredp-image-dired-create-thumb (&optional arg)
    "Create thumbnail image file for this file.
With a prefix arg, replace any existing thumbnail for the file.
Return the name of the thumbnail image file, or nil if none."
    (interactive "P")
    (let* ((curr-file   (dired-get-filename))
           (thumb-name  (image-dired-thumb-name curr-file)))
      (when arg (clear-image-cache))
      (when (or arg  (not (file-exists-p thumb-name)))
        (unless (zerop (image-dired-create-thumb
                        curr-file (image-dired-thumb-name curr-file)))
          (error "Thumbnail image file could not be created")))
      (and (file-exists-p thumb-name)  thumb-name)))

;; REPLACE ORIGINAL in `dired.el'.
;; 1. Put `mouse-face' on whole line, not just file name.
;; 2. Add text property `dired-filename' to only the file name.
;; 3. Show image-file preview on mouseover, if `tooltip-mode'.
;;
;; #3 is what is important for this bug report.
;;
(defun dired-insert-set-properties (beg end)
  "Add various text properties to the lines in the region.
Highlight entire line upon mouseover.
Add text property `dired-filename' to the file name.
Handle `dired-hide-details-mode' invisibility spec (Emacs 24.4+)."
  (let ((inhibit-field-text-motion  t)) ; Just in case.
    (save-excursion
      (goto-char beg)
      (while (< (point) end)
        (condition-case nil
            (cond ((dired-move-to-filename)
                   (add-text-properties
                    (line-beginning-position) (line-end-position)
                    '(mouse-face highlight
                      help-echo diredp-mouseover-help)) ; <===========
                   (put-text-property
                    (point) (save-excursion
                              (dired-move-to-end-of-filename) (point))
                    'dired-filename t)
                   (put-text-property
                    (+ (line-beginning-position) 1) (1- (point))
                    'invisible 'dired-hide-details-detail)
                   (dired-move-to-end-of-filename)
                   (when (< (+ (point) 4) (line-end-position))
                     (put-text-property
                      (+ (point) 4) (line-end-position)
                      'invisible 'dired-hide-details-link)))
                  (t
                   (unless (or (looking-at-p "^$")
                               (looking-at-p dired-subdir-regexp))
                     (put-text-property
                      (line-beginning-position) (1+ (line-end-position))
                      'invisible 'dired-hide-details-information))))
          (error nil))
        (forward-line 1)))))

(defun diredp-mouseover-help (window buffer pos)
  "`help-echo' function for Dired."
  (let (file)
    (if (and tooltip-mode
             (with-selected-frame (window-frame window)
               ;; (with-selected-window window
               ;; (select-frame-set-input-focus (window-frame window))
               (with-current-buffer buffer
                 (goto-char pos)
                 (string-match-p
                  (image-file-name-regexp)
                  (setq file  (dired-get-filename nil 'NO-ERROR))))))
        (let ((img-file  (diredp-image-dired-create-thumb file)))
          (propertize " " 'display (create-image img-file)))
      "mouse-2: visit this file in another window")))

3. `C-x 5 b *Messages*', so you can see redisplay error messages.

4. `C-x 5 d' to a directory that has some image files.  Move the mouse
over these file names.  No problem: a thumbnail tooltip is shown for each
file you mouseover.

5. `C-x 5 o', to select another frame, or just click another frame's
title bar.  This takes input focus away from the Dired frame.

6. Now mouseover the same files in the Dired frame, without first
clicking its title bar or otherwise selecting it (giving it focus).
You will see *lots* of error messages such as this in buffer *Messages*:

Error during redisplay: (diredp-mouseover-help #<window 12 on TEST> #<buffer TEST> 426) signaled (error "No file on this line")

(The code could be wrapped in `ignore-errors', but that's not the point
here.)

7. The `help-echo' function is correctly passed the WINDOW, BUFFER, and
POSition.  The problem seems to be that (a) mouseover is active, even
though the frame is not selected (for some meaning of selected, probably
including focus), and (b) when the `help-echo' function is invoked, the
frame is not selected/focused, or the wrong buffer is used somehow (?).

IOW, mouseover is activated and passed the correct WINDOW, BUFFER, and
POSition, but the code above is apparently not sufficient to make Emacs
actually use these arguments as I would expect it to.

8. I tried wrapping the `with-current-buffer' with
`(with-selected-window WINDOW...)'.  That didn't help.  I tried wrapping
it with `(with-selected-frame (window-frame WINDOW)...' (as shown above).
That didn't help.  I tried `(select-frame-set-input-focus (window-frame
WINDOW))', but that didn't help either.

Am I missing something?  Should I add something else here, to make the
mouseover use WINDOW and BUFFER as I have been expecting?  Or is it a
bug that mouseover is active at all when the frame is not selected?

FWIW: Other Window apps seem to behave the same way wrt focus and
mouseover: mouseover is active even if the "frame" is not selected for
input focus (e.g., by clicking its title bar).  So I don't imagine that
Emacs is doing anything wrong in that regard.

I cannot tell whether I am seeing a bug or I'm just missing knowing the
proper code to use to get the `help-echo' function to temporarily make
the WINDOW and BUFFER and WINDOW's frame current/selected/focused.

A guess would be that I need to give the frame the input focus
(temporarily), but using `select-frame-set-input-focus' did not seem to
help.

In GNU Emacs 24.4.50.1 (i686-pc-mingw32)
 of 2014-04-21 on ODIEONE
Bzr revision: 117005 [email-masked]
Windowing system distributor `Microsoft Corp.', version 6.1.7601
Configured using:
 `configure --prefix=/c/Devel/emacs/snapshot/trunk
 --enable-checking=yes,glyphs 'CFLAGS=-O0 -g3'
 LDFLAGS=-Lc:/Devel/emacs/lib 'CPPFLAGS=-DGC_MCHECK=1
 -Ic:/Devel/emacs/include''