+ charpos = glyph->charpos;
+ if (charpos == pos)
+ {
+ *hpos = i;
+ *vpos = row->y;
+ return 1;
+ }
+ else if (charpos > pos)
+ break;
+ else if (charpos > 0)
+ lastcol = i;
+ }
+
+ /* If we're looking for the end of the buffer,
+ and we didn't find it in the line we scanned,
+ use the start of the following line. */
+ if (maybe_next_line_p)
+ {
+ ++row;
+ lastcol = 0;
+ }
+
+ *vpos = row->y;
+ *hpos = lastcol + 1;
+ return 0;
+}
+
+/* Take proper action when mouse has moved to the mode or top line of
+ window W, x-position X. MODE_LINE_P non-zero means mouse is on the
+ mode line. X is relative to the start of the text display area of
+ W, so the width of bitmap areas and scroll bars must be subtracted
+ to get a position relative to the start of the mode line. */
+static void
+IT_note_mode_line_highlight (struct window *w, int x, int mode_line_p)
+{
+ struct frame *f = XFRAME (w->frame);
+ struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
+ struct glyph_row *row;
+
+ if (mode_line_p)
+ row = MATRIX_MODE_LINE_ROW (w->current_matrix);
+ else
+ row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
+
+ if (row->enabled_p)
+ {
+ extern Lisp_Object Qhelp_echo;
+ struct glyph *glyph, *end;
+ Lisp_Object help, map;
+
+ /* Find the glyph under X. */
+ glyph = row->glyphs[TEXT_AREA]
+ + x - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
+ end = glyph + row->used[TEXT_AREA];
+ if (glyph < end
+ && STRINGP (glyph->object)
+ && XSTRING (glyph->object)->intervals
+ && glyph->charpos >= 0
+ && glyph->charpos < XSTRING (glyph->object)->size)
+ {
+ /* If we're on a string with `help-echo' text property,
+ arrange for the help to be displayed. This is done by
+ setting the global variable help_echo to the help string. */
+ help = Fget_text_property (make_number (glyph->charpos),
+ Qhelp_echo, glyph->object);
+ if (!NILP (help))
+ {
+ help_echo = help;
+ XSETWINDOW (help_echo_window, w);
+ help_echo_object = glyph->object;
+ help_echo_pos = glyph->charpos;
+ }
+ }
+ }
+}
+
+/* Take proper action when the mouse has moved to position X, Y on
+ frame F as regards highlighting characters that have mouse-face
+ properties. Also de-highlighting chars where the mouse was before.
+ X and Y can be negative or out of range. */
+static void
+IT_note_mouse_highlight (struct frame *f, int x, int y)
+{
+ struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
+ int portion;
+ Lisp_Object window;
+ struct window *w;
+
+ /* When a menu is active, don't highlight because this looks odd. */
+ if (mouse_preempted)
+ return;
+
+ if (disable_mouse_highlight
+ || !f->glyphs_initialized_p)
+ return;
+
+ dpyinfo->mouse_face_mouse_x = x;
+ dpyinfo->mouse_face_mouse_y = y;
+ dpyinfo->mouse_face_mouse_frame = f;
+
+ if (dpyinfo->mouse_face_defer)
+ return;
+
+ if (gc_in_progress)
+ {
+ dpyinfo->mouse_face_deferred_gc = 1;
+ return;
+ }
+
+ /* Which window is that in? */
+ window = window_from_coordinates (f, x, y, &portion, 0);
+
+ /* If we were displaying active text in another window, clear that. */
+ if (! EQ (window, dpyinfo->mouse_face_window))
+ clear_mouse_face (dpyinfo);
+
+ /* Not on a window -> return. */
+ if (!WINDOWP (window))
+ return;
+
+ /* Convert to window-relative coordinates. */
+ w = XWINDOW (window);
+ x -= WINDOW_DISPLAY_LEFT_EDGE_PIXEL_X (w);
+ y -= WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
+
+ if (portion == 1 || portion == 3)
+ {
+ /* Mouse is on the mode or top line. */
+ IT_note_mode_line_highlight (w, x, portion == 1);
+ return;
+ }
+ else
+ IT_set_mouse_pointer (0);
+
+ /* Are we in a window whose display is up to date?
+ And verify the buffer's text has not changed. */
+ if (/* Within text portion of the window. */
+ portion == 0
+ && EQ (w->window_end_valid, w->buffer)
+ && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
+ && (XFASTINT (w->last_overlay_modified)
+ == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
+ {
+ int pos, i, area;
+ struct glyph_row *row;
+ struct glyph *glyph;
+
+ /* Find the glyph under X/Y. */
+ glyph = NULL;
+ if (y < w->current_matrix->nrows)
+ {
+ row = MATRIX_ROW (w->current_matrix, y);
+ if (row->enabled_p
+ && row->displays_text_p
+ && x < window_box_width (w, TEXT_AREA))
+ {
+ glyph = row->glyphs[TEXT_AREA];
+ if (x >= row->used[TEXT_AREA])
+ glyph = NULL;
+ else
+ {
+ glyph += x;
+ if (!BUFFERP (glyph->object))
+ glyph = NULL;
+ }
+ }
+ }
+
+ /* Clear mouse face if X/Y not over text. */
+ if (glyph == NULL)
+ {
+ clear_mouse_face (dpyinfo);
+ return;
+ }
+
+ if (!BUFFERP (glyph->object))
+ abort ();
+ pos = glyph->charpos;
+
+ /* Check for mouse-face and help-echo. */
+ {
+ extern Lisp_Object Qmouse_face;
+ Lisp_Object mouse_face, overlay, position;
+ Lisp_Object *overlay_vec;
+ int len, noverlays;
+ struct buffer *obuf;
+ int obegv, ozv;
+
+ /* If we get an out-of-range value, return now; avoid an error. */
+ if (pos > BUF_Z (XBUFFER (w->buffer)))
+ return;
+
+ /* Make the window's buffer temporarily current for
+ overlays_at and compute_char_face. */
+ obuf = current_buffer;
+ current_buffer = XBUFFER (w->buffer);
+ obegv = BEGV;
+ ozv = ZV;
+ BEGV = BEG;
+ ZV = Z;
+
+ /* Is this char mouse-active or does it have help-echo? */
+ XSETINT (position, pos);
+
+ /* Put all the overlays we want in a vector in overlay_vec.
+ Store the length in len. If there are more than 10, make
+ enough space for all, and try again. */
+ len = 10;
+ overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
+ noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
+ if (noverlays > len)
+ {
+ len = noverlays;
+ overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
+ noverlays = overlays_at (pos,
+ 0, &overlay_vec, &len, NULL, NULL, 0);
+ }
+
+ /* Sort overlays into increasing priority order. */
+ noverlays = sort_overlays (overlay_vec, noverlays, w);
+
+ /* Check mouse-face highlighting. */
+ if (! (EQ (window, dpyinfo->mouse_face_window)
+ && y >= dpyinfo->mouse_face_beg_row
+ && y <= dpyinfo->mouse_face_end_row
+ && (y > dpyinfo->mouse_face_beg_row
+ || x >= dpyinfo->mouse_face_beg_col)
+ && (y < dpyinfo->mouse_face_end_row
+ || x < dpyinfo->mouse_face_end_col
+ || dpyinfo->mouse_face_past_end)))
+ {
+ /* Clear the display of the old active region, if any. */
+ clear_mouse_face (dpyinfo);
+
+ /* Find highest priority overlay that has a mouse-face prop. */
+ overlay = Qnil;
+ for (i = noverlays - 1; i >= 0; --i)
+ {
+ mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
+ if (!NILP (mouse_face))
+ {
+ overlay = overlay_vec[i];
+ break;
+ }
+ }
+
+ /* If no overlay applies, get a text property. */
+ if (NILP (overlay))
+ mouse_face = Fget_text_property (position, Qmouse_face,
+ w->buffer);
+
+ /* Handle the overlay case. */
+ if (! NILP (overlay))
+ {
+ /* Find the range of text around this char that
+ should be active. */
+ Lisp_Object before, after;
+ int ignore;
+
+ before = Foverlay_start (overlay);
+ after = Foverlay_end (overlay);
+ /* Record this as the current active region. */
+ fast_find_position (w, XFASTINT (before),
+ &dpyinfo->mouse_face_beg_col,
+ &dpyinfo->mouse_face_beg_row);
+ dpyinfo->mouse_face_past_end
+ = !fast_find_position (w, XFASTINT (after),
+ &dpyinfo->mouse_face_end_col,
+ &dpyinfo->mouse_face_end_row);
+ dpyinfo->mouse_face_window = window;
+ dpyinfo->mouse_face_face_id
+ = face_at_buffer_position (w, pos, 0, 0,
+ &ignore, pos + 1, 1);
+
+ /* Display it as active. */
+ show_mouse_face (dpyinfo, 1);
+ }
+ /* Handle the text property case. */
+ else if (! NILP (mouse_face))
+ {
+ /* Find the range of text around this char that
+ should be active. */
+ Lisp_Object before, after, beginning, end;
+ int ignore;
+
+ beginning = Fmarker_position (w->start);
+ XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
+ - XFASTINT (w->window_end_pos)));
+ before
+ = Fprevious_single_property_change (make_number (pos + 1),
+ Qmouse_face,
+ w->buffer, beginning);
+ after
+ = Fnext_single_property_change (position, Qmouse_face,
+ w->buffer, end);
+ /* Record this as the current active region. */
+ fast_find_position (w, XFASTINT (before),
+ &dpyinfo->mouse_face_beg_col,
+ &dpyinfo->mouse_face_beg_row);
+ dpyinfo->mouse_face_past_end
+ = !fast_find_position (w, XFASTINT (after),
+ &dpyinfo->mouse_face_end_col,
+ &dpyinfo->mouse_face_end_row);
+ dpyinfo->mouse_face_window = window;
+ dpyinfo->mouse_face_face_id
+ = face_at_buffer_position (w, pos, 0, 0,
+ &ignore, pos + 1, 1);
+
+ /* Display it as active. */
+ show_mouse_face (dpyinfo, 1);
+ }
+ }
+
+ /* Look for a `help-echo' property. */
+ {
+ Lisp_Object help;
+ extern Lisp_Object Qhelp_echo;
+
+ /* Check overlays first. */
+ help = Qnil;
+ for (i = noverlays - 1; i >= 0 && NILP (help); --i)
+ {
+ overlay = overlay_vec[i];
+ help = Foverlay_get (overlay, Qhelp_echo);
+ }
+
+ if (!NILP (help))
+ {
+ help_echo = help;
+ help_echo_window = window;
+ help_echo_object = overlay;
+ help_echo_pos = pos;
+ }
+ /* Try text properties. */
+ else if (NILP (help)
+ && ((STRINGP (glyph->object)
+ && glyph->charpos >= 0
+ && glyph->charpos < XSTRING (glyph->object)->size)
+ || (BUFFERP (glyph->object)
+ && glyph->charpos >= BEGV
+ && glyph->charpos < ZV)))
+ {
+ help = Fget_text_property (make_number (glyph->charpos),
+ Qhelp_echo, glyph->object);
+ if (!NILP (help))
+ {
+ help_echo = help;
+ help_echo_window = window;
+ help_echo_object = glyph->object;
+ help_echo_pos = glyph->charpos;
+ }
+ }
+ }
+
+ BEGV = obegv;
+ ZV = ozv;
+ current_buffer = obuf;
+ }
+ }