]> code.delx.au - gnu-emacs/commitdiff
Merge from trunk.
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 28 Sep 2011 20:07:17 +0000 (13:07 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 28 Sep 2011 20:07:17 +0000 (13:07 -0700)
1  2 
src/ChangeLog
src/bidi.c
src/lread.c
src/xdisp.c

diff --combined src/ChangeLog
index 4f1edc956680b0922bccac2a5f60fd27eb880641,f26754d1135ff324605cae48dc147918265529ae..4f0a0b9ab74c5cbf249ae0cdb1d194cf32f3b238
- 2011-09-27  Paul Eggert  <eggert@cs.ucla.edu>
++2011-09-28  Paul Eggert  <eggert@cs.ucla.edu>
 +
 +      * alloc.c (pure_bytes_used_lisp, pure_bytes_used_non_lisp):
 +      (allocate_vectorlike, buffer_memory_full, struct sdata, SDATA_SIZE)
 +      (string_bytes, check_sblock, allocate_string_data):
 +      (compact_small_strings, Fmake_bool_vector, make_string)
 +      (make_unibyte_string, make_multibyte_string)
 +      (make_string_from_bytes, make_specified_string)
 +      (allocate_vectorlike, Fmake_vector, find_string_data_in_pure)
 +      (make_pure_string, make_pure_c_string, make_pure_vector, Fpurecopy)
 +      (mark_vectorlike):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (allocate_pseudovector):
 +      Use int, not EMACS_INT, where int is wide enough.
 +      (inhibit_garbage_collection, Fgarbage_collect):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * bidi.c (bidi_mirror_char): Use EMACS_INT, not int, where
 +      int might not be wide enough.
 +      (bidi_cache_search, bidi_cache_find, bidi_init_it)
 +      (bidi_count_bytes, bidi_char_at_pos, bidi_fetch_char)
 +      (bidi_at_paragraph_end, bidi_find_paragraph_start)
 +      (bidi_paragraph_init, bidi_resolve_explicit, bidi_resolve_weak)
 +      (bidi_level_of_next_char, bidi_move_to_visually_next):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * buffer.c (copy_overlays, Fgenerate_new_buffer_name)
 +      (Fkill_buffer, Fset_buffer_major_mode)
 +      (advance_to_char_boundary, Fbuffer_swap_text)
 +      (Fset_buffer_multibyte, overlays_at, overlays_in)
 +      (overlay_touches_p, struct sortvec, record_overlay_string)
 +      (overlay_strings, recenter_overlay_lists)
 +      (adjust_overlays_for_insert, adjust_overlays_for_delete)
 +      (fix_start_end_in_overlays, fix_overlays_before, modify_overlay)
 +      (Fmove_overlay, Fnext_overlay_change, Fprevious_overlay_change)
 +      (Foverlay_recenter, last_overlay_modification_hooks_used)
 +      (report_overlay_modification, evaporate_overlays, enlarge_buffer_text):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (validate_region): Omit unnecessary test for b <= e, since
 +      that's guaranteed by the previous test.
 +      (adjust_overlays_for_delete): Avoid pos + length overflow.
 +      (Fmove_overlay, Fdelete_overlay, add_overlay_mod_hooklist)
 +      (report_overlay_modification):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (Foverlays_at, Fnext_overlay_change, Fprevious_overlay_change):
 +      Omit pointer cast, which isn't needed anyway, and doesn't work
 +      after the EMACS_INT -> ptrdiff_t change.
 +      * buffer.h: Adjust decls to match defn changes elsewhere.
 +      (struct buffer_text, struct buffer):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      Use EMACS_INT, not int, where int might not be wide enough.
 +      * bytecode.c (exec_byte_code): Use ptrdiff_t, not int, to avoid
 +      needless 32-bit limit on 64-bit hosts.  Remove unnecessary
 +      memory-full test.  Use EMACS_INT, not ptrdiff_t or int, where
 +      ptrdiff_t or int might not be wide enough.
 +      * callint.c (Fcall_interactively):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * callproc.c (call_process_kill, Fcall_process):
 +      Don't assume pid_t fits into an Emacs fixnum.
 +      (call_process_cleanup, Fcall_process, child_setup):
 +      Don't assume pid_t fits into int.
 +      (call_process_cleanup, Fcall_process, delete_temp_file)
 +      (Fcall_process_region):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (Fcall_process): Simplify handling of volatile integers.
 +      Use int, not EMACS_INT, where int will do.
 +      * casefiddle.c (casify_object, casify_region, operate_on_word)
 +      (Fupcase_word, Fdowncase_word, Fcapitalize_word):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (casify_object): Avoid integer overflow when overallocating buffer.
 +      * casetab.c (set_identity, shuffle): Prefer int to unsigned when
 +      either works.  Use lint_assume to convince GCC 4.6.1 that it's OK.
 +      * category.c (Fchar_category_set): Don't assume fixnum fits in int.
 +      * category.h (CATEGORYP): Don't assume arg is nonnegative.
 +      * ccl.c (GET_CCL_INT): Remove; no longer needed, since the
 +      integers are now checked earlier.  All uses replaced with XINT.
 +      (ccl_driver):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      For CCL_MapSingle, check that content and value are in int range.
 +      (ccl_driver, Fregister_code_conversion_map):
 +      Check that Vcode_version_map_vector is a vector.
 +      (resolve_symbol_ccl_program): Check that vector header is in range.
 +      Always copy the vector, so that we can check its contents reliably
 +      now rather than having to recheck each instruction as it's being
 +      executed.  Check that vector words fit in 'int'.
 +      (ccl_get_compiled_code, Fregister_ccl_program)
 +      (Fregister_code_conversion_map): Use ptrdiff_t, not int, for
 +      program indexes, to avoid needless 32-bit limit on 64-bit hosts.
 +      (Fccl_execute, Fccl_execute_on_string): Check that initial reg
 +      contents are in range.
 +      (Fccl_execute_on_string): Check that status is in range.
 +      * ccl.h (struct ccl_program.idx): Now ptrdiff_t, not int.
 +      * character.c (char_resolve_modifier_mask, Fchar_resolve_modifiers):
 +      Accept and return EMACS_INT, not int, because callers can pass values
 +      out of 'int' range.
 +      (c_string_width, strwidth, lisp_string_width, chars_in_text)
 +      (multibyte_chars_in_text, parse_str_as_multibyte)
 +      (str_as_multibyte, count_size_as_multibyte, str_to_multibyte)
 +      (str_as_unibyte, str_to_unibyte, string_count_byte8)
 +      (string_escape_byte8, Fget_byte):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Funibyte_string): Use CHECK_CHARACTER, not CHECK_NATNUM, to
 +      avoid mishandling large integers.
 +      * character.h: Adjust decls to match defn changes elsewhere.
 +      * charset.c (load_charset_map_from_file, find_charsets_in_text)
 +      (Ffind_charset_region):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (load_charset_map_from_file): Redo idx calculation to avoid overflow.
 +      (load_charset_map_from_vector, Fdefine_charset_internal):
 +      Don't assume fixnum fits in int.
 +      (load_charset_map_from_vector, Fmap_charset_chars):
 +      Remove now-unnecessary CHECK_NATNUMs.
 +      (Fdefine_charset_internal): Check ranges here, more carefully.
 +      Don't rely on undefined behavior with signed left shift overflow.
 +      Don't assume unsigned int fits into fixnum, or that fixnum fits
 +      into unsigned int.  Don't require max_code to be a valid fixnum;
 +      that's not true for gb10830 4-byte on a 32-bit host.  Allow
 +      invalid_code to be a cons, for the same reason.  Require code_offset
 +      to be a character.  Avoid int overflow if max_char is close
 +      to INT_MAX.
 +      (CODE_POINT_TO_INDEX): On 32-bit hosts, return int, not unsigned;
 +      this is intended anyway and avoids some undefined behavior.
 +      (load_charset_map): Pass unsigned, not int, as 2nd arg of
 +      INDEX_TO_CODE_POINT, as that's what it expects.
 +      (Funify_charset, encode_char): Don't stuff unsigned vals into int vars.
 +      * charset.h (DECODE_CHAR): Return int, not unsigned;
 +      this is what was intended anyway, and it avoids undefined behavior.
 +      (CHARSET_OFFSET): Remove unused macro, instead of fixing its
 +      integer-overflow issues.
 +      (ENCODE_CHAR): Return unsigned on all hosts, not just on 32-bit hosts.
 +      Formerly, it returned EMACS_INT on 64-bit hosts in the common case
 +      where the argument is EMACS_INT, and this behavior is not intended.
 +      * chartab.c (Fmake_char_table, Fset_char_table_range)
 +      (uniprop_get_decoder, uniprop_get_encoder):
 +      Don't assume fixnum fits in int.
 +      * cmds.c (move_point): New function, that does the gist of
 +      Fforward_char and Fbackward_char, but does so while checking
 +      for integer overflow more accurately.
 +      (Fforward_char, Fbackward_char): Use it.
 +      (Fforward_line, Fend_of_line, internal_self_insert)
 +      (internal_self_insert):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      Fix a FIXME, by checking for integer overflow when calculating
 +      target_clm and actual_clm.
 +      * coding.c (detect_coding_XXX, encode_coding_XXX, CODING_DECODE_CHAR)
 +      (ASSURE_DESTINATION, coding_alloc_by_realloc)
 +      (coding_alloc_by_making_gap, alloc_destination)
 +      (detect_coding_utf_8, encode_coding_utf_8, decode_coding_utf_16)
 +      (encode_coding_utf_16, detect_coding_emacs_mule)
 +      (decode_coding_emacs_mule, encode_coding_emacs_mule)
 +      (detect_coding_iso_2022, decode_coding_iso_2022)
 +      (encode_invocation_designation, encode_designation_at_bol)
 +      (encode_coding_iso_2022, detect_coding_sjis, detect_coding_big5)
 +      (decode_coding_sjis, decode_coding_big5, encode_coding_sjis)
 +      (encode_coding_big5, detect_coding_ccl, decode_coding_ccl)
 +      (encode_coding_ccl, encode_coding_raw_text)
 +      (detect_coding_charset, decode_coding_charset)
 +      (encode_coding_charset, detect_eol, decode_eol, produce_chars)
 +      (produce_composition, produce_charset, produce_annotation)
 +      (decode_coding, handle_composition_annotation)
 +      (handle_charset_annotation, consume_chars, decode_coding_gap)
 +      (decode_coding_object, encode_coding_object, detect_coding_system)
 +      (Ffind_coding_systems_region_internal, Fcheck_coding_systems_region)
 +      (code_convert_region, code_convert_string)
 +      (Fdefine_coding_system_internal):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (setup_iso_safe_charsets, consume_chars, Funencodable_char_position)
 +      (Fdefine_coding_system_internal):
 +      Don't assume fixnums fit in int.
 +      (decode_coding_gap, decode_coding_object, encode_coding_object)
 +      (Fread_coding_system, Fdetect_coding_region)
 +      (Funencodable_char_position, Fcheck_coding_systems_region)
 +      (get_translation, handle_composition_annotation, consume_chars):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (consume_chars): Rewrite to avoid calculating an address outside buffer.
 +      (Ffind_operation_coding_system): NATNUMP can eval its arg twice.
 +      Don't access memory outside of the args array.
 +      (Fdefine_coding_system_internal): Check for charset-id overflow.
 +      (ENCODE_ISO_CHARACTER): Use unsigned, not int, to store the unsigned
 +      result of ENCODE_CHAR.
 +      * coding.h: Adjust decls to match defn changes elsewhere.
 +      (struct coding_system):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * composite.c (get_composition_id, find_composition)
 +      (run_composition_function, update_compositions)
 +      (compose_text, composition_gstring_put_cache)
 +      (composition_gstring_p, composition_gstring_width)
 +      (fill_gstring_header, fill_gstring_body, autocmp_chars)
 +      (composition_compute_stop_pos, composition_reseat_it)
 +      (composition_update_it, struct position_record)
 +      (find_automatic_composition, composition_adjust_point)
 +      (Fcomposition_get_gstring, Ffind_composition_internal):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (update_compositions):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * composite.h: Adjust decls to match defn changes elsewhere.
 +      (struct composition):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * data.c (let_shadows_buffer_binding_p, let_shadows_global_binding_p):
 +      Do not attempt to compute the address of the object just before a
 +      buffer; this is not portable.
 +      (Faref, Faset):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Faset): Use int, not EMACS_INT, where int is wide enough.
 +      (Fstring_to_number): Don't assume fixnums fit in int.
 +      (Frem): Don't assume arg is nonnegative.
 +      * dbusbind.c (xd_append_arg): Check for integers out of range.
 +      (Fdbus_call_method): Don't overflow the timeout int.
 +      * dired.c (directory_files_internal, file_name_completion, scmp)
 +      (file_name_completion_stat):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (file_name_completion): Don't overflow matchcount.
 +      (file_name_completion_stat): Use SAFE_ALLOCA, not alloca.
 +      * dispextern.h: Adjust decls to match defn changes elsewhere.
 +      (struct text_pos, struct glyph, struct bidi_saved_info)
 +      (struct bidi_string_data, struct bidi_it, struct composition_it)
 +      (struct it):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (struct display_pos, struct composition_it, struct it):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * dispnew.c (increment_matrix_positions)
 +      (increment_row_positions, mode_line_string)
 +      (marginal_area_string):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (change_frame_size_1, Fredisplay, Fframe_or_buffer_changed_p):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (duration_to_sec_usec): New function, to check for overflow better.
 +      (Fsleep_for, sit_for): Use it.
 +      * doc.c (get_doc_string, store_function_docstring):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (get_doc_string, Fsnarf_documentation):
 +      Use int, not EMACS_INT, where int is wide enough.
 +      (get_doc_string):
 +      Use SAFE_ALLOCA, not alloca.
 +      Check for overflow when converting EMACS_INT to off_t.
 +      * doprnt.c (doprnt):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * editfns.c (init_editfns, Fuser_uid, Fuser_real_uid):
 +      Don't assume uid_t fits into fixnum.
 +      (buildmark, Fgoto_char, overlays_around, find_field, Fdelete_field)
 +      (Ffield_string, Ffield_string_no_properties, Ffield_beginning)
 +      (Ffield_end, Fconstrain_to_field, Fline_beginning_position)
 +      (Fline_end_position, Fprevious_char, Fchar_after, Fchar_before)
 +      (general_insert_function)
 +      (Finsert_char, make_buffer_string, make_buffer_string_both)
 +      (update_buffer_properties, Fbuffer_substring)
 +      (Fbuffer_substring_no_properties, Fcompare_buffer_substrings)
 +      (Fsubst_char_in_region, check_translation)
 +      (Ftranslate_region_internal, save_restriction_restore, Fformat)
 +      (transpose_markers, Ftranspose_regions):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (clip_to_bounds): Move to lisp.h as an inline function).
 +      (Fconstrain_to_field): Don't assume integers are nonnegative.
 +      (Fline_beginning_position, Fsave_excursion, Fsave_current_buffer):
 +      (Fsubst_char_in_region, Fsave_restriction):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (Femacs_pid): Don't assume pid_t fits into fixnum.
 +      (lo_time): Use int, not EMACS_INT, when int suffices.
 +      (lisp_time_argument): Check for usec out of range.
 +      (Fencode_time): Don't assume fixnum fits in int.
 +      * emacs.c (gdb_valbits, gdb_gctypebits): Now int, not EMACS_INT.
 +      (gdb_data_seg_bits): Now uintptr_t, not EMACS_INT.
 +      (PVEC_FLAG, gdb_array_mark_flag): Now ptrdiff_t, not EMACS_INT.
 +      (init_cmdargs, Fdump_emacs):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (Fkill_emacs): Don't assume fixnum fits in int; instead, take just
 +      the bottom (typically) 32 bits of the fixnum.
 +      * eval.c (specpdl_size, call_debugger):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (when_entered_debugger, Fbacktrace_debug):
 +      Don't assume fixnum can fit in int.
 +      (Fdefvaralias, Fdefvar): Do not attempt to compute the address of
 +      the object just before a buffer; this is not portable.
 +      (FletX, Flet, Funwind_protect, do_autoload, Feval, funcall_lambda)
 +      (grow_specpdl, unbind_to):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (Fapply, apply_lambda): Don't assume ptrdiff_t can hold fixnum.
 +      (grow_specpdl): Simplify allocation by using xpalloc.
 +      (Fprog1, Fprog2): Don't assume list length fits in int.  Simplify.
 +      * fileio.c (Ffind_file_name_handler, Fcopy_file, Frename_file)
 +      (Finsert_file_contents, Fwrite_region, Fdo_auto_save):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (Ffind_file_name_handler, non_regular_inserted, Finsert_file_contents)
 +      (a_write, e_write):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Fcopy_file, non_regular_nbytes, read_non_regular)
 +      (Finsert_file_contents):
 +      Use int, not EMACS_INT, where int is wide enough.
 +      (READ_BUF_SIZE): Verify that it fits in int.
 +      (Finsert_file_contents): Check that counts are in proper range,
 +      rather than assuming fixnums fit into ptrdiff_t etc.
 +      Don't assume fixnums fit into int.
 +      (Fdo_auto_save, Fset_buffer_auto_saved)
 +      (Fclear_buffer_auto_save_failure):
 +      Don't assume time_t is signed, or that it fits in int.
 +      * floatfns.c (Fexpt): Avoid undefined signed * signed overflow.
 +      * fns.c (Fcompare_strings, Fstring_lessp, struct textprop_rec, concat)
 +      (string_char_byte_cache_charpos, string_char_byte_cache_bytepos)
 +      (string_char_to_byte, string_byte_to_char)
 +      (string_make_multibyte, string_to_multibyte)
 +      (string_make_unibyte, Fstring_as_unibyte, Fstring_as_multibyte)
 +      (Fstring_to_unibyte, Fsubstring, Fsubstring_no_properties)
 +      (substring_both, Fdelete, internal_equal, Ffillarray)
 +      (Fclear_string, mapcar1)
 +      (Fbase64_encode_region, Fbase64_encode_string, base64_encode_1)
 +      (Fbase64_decode_region, Fbase64_decode_string, base64_decode_1)
 +      (larger_vector, make_hash_table, maybe_resize_hash_table)
 +      (hash_lookup, hash_remove_from_table, hash_clear, sweep_weak_table)
 +      (Fmaphash, secure_hash):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (concat): Check for string index and length overflow.
 +      (Fmapconcat): Don't assume fixnums fit into ptrdiff_t.
 +      (Frequire):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (larger_vector): New API (vec, incr_min, size_max) replaces old
 +      one (vec, new_size, init).  This catches size overflow.
 +      INIT was removed because it was always Qnil.
 +      All callers changed.
 +      (INDEX_SIZE_BOUND): New macro, which calculates more precisely
 +      the upper bound on a hash table index size.
 +      (make_hash_table, maybe_resize_hash_table): Use it.
 +      (secure_hash): Computer start_byte and end_byte only after
 +      they're known to be in ptrdiff_t range.
 +      * font.c (font_intern_prop, font_at, font_range, Ffont_shape_gstring)
 +      (Ffont_get_glyphs, Ffont_at):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (font_style_to_value, font_prop_validate_style, font_expand_wildcards)
 +      (Flist_fonts, Fopen_font):
 +      Don't assume fixnum can fit in int.
 +      (check_gstring): Don't assume index can fit in int.
 +      (font_match_p): Check that fixnum is a character, not a nonnegative
 +      fixnum, since the later code needs to stuff it into an int.
 +      (font_find_for_lface): Use SAFE_ALLOCA_LISP, not alloca.
 +      (font_fill_lglyph_metrics): Use unsigned, not EMACS_INT, to avoid
 +      conversion overflow issues.
 +      (Fopen_font): Check for integer out of  range.
 +      (Ffont_get_glyphs): Don't assume index can fit in int.
 +      * font.h: Adjust decls to match defn changes elsewhere.
 +      * fontset.c (reorder_font_vector): Redo score calculation to avoid
 +      integer overflow.
 +      (num_auto_fontsets, fontset_from_font): Use ptrdiff_t, not
 +      printmax_t, where ptrdiff_t is wide enough.
 +      (Finternal_char_font):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * frame.c (Fset_mouse_position, Fset_mouse_pixel_position)
 +      (Fset_frame_height, Fset_frame_width, Fset_frame_size)
 +      (Fset_frame_position, x_set_frame_parameters)
 +      (x_set_line_spacing, x_set_border_width)
 +      (x_set_internal_border_width, x_set_alpha, x_figure_window_size):
 +      Check that fixnums are in proper range for system types.
 +      (frame_name_fnn_p, Fframe_parameter, Fmodify_frame_parameters):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Fmodify_frame_parameters): Don't assume fixnum fits in int.
 +      Use SAFE_ALLOCA_LISP, not alloca.
 +      * frame.h (struct frame): Use intptr_t, not EMACS_INT, where
 +      intptr_t is wide enough.
 +      * fringe.c (lookup_fringe_bitmap, get_logical_fringe_bitmap)
 +      (Fdefine_fringe_bitmap): Don't assume fixnum fits in int.
 +      (Ffringe_bitmaps_at_pos): Don't assume index fits in int.
 +      Check for fixnum out of range.
 +      * ftfont.c (ftfont_list): Don't assume index fits in int.
 +      Check that fixnums are in proper range for system types.
 +      (ftfont_shape_by_flt):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * gnutls.c (emacs_gnutls_write, emacs_gnutls_read):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Fgnutls_error_fatalp, Fgnutls_error_string, Fgnutls_boot):
 +      Check that fixnums are in proper range for system types.
 +      * gnutls.h: Adjust decls to match defn changes elsewhere.
 +      * gtkutil.c (xg_dialog_run):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (update_frame_tool_bar):
 +      Check that fixnums are in proper range for system types.
 +      * image.c (parse_image_spec): Redo count calculation to avoid overflow.
 +      (lookup_image): Check that fixnums are in range for system types.
 +      * indent.c (last_known_column, last_known_column_point):
 +      (current_column_bol_cache):
 +      (skip_invisible, current_column, check_display_width):
 +      (check_display_width, scan_for_column, current_column_1)
 +      (Findent_to, Fcurrent_indentation, position_indentation)
 +      (indented_beyond_p, Fmove_to_column, compute_motion):
 +      (Fcompute_motion, Fvertical_motion):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (last_known_column_modified): Use EMACS_INT, not int.
 +      (check_display_width):
 +      (Fcompute_motion):
 +      Check that fixnums and floats are in proper range for system types.
 +      (compute_motion): Don't assume index or fixnum fits in int.
 +      (compute_motion, Fcompute_motion):
 +      Use int, not EMACS_INT, when it is wide enough.
 +      (vmotion): Omit local var start_hpos that is always 0; that way
 +      we don't need to worry about overflow in expressions involving it.
 +      * indent.h: Adjust decls to match defn changes elsewhere.
 +      (struct position):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      Use int, not EMACS_INT, where int is wide enough.
 +      Remove unused members ovstring_chars_done and tab_offset;
 +      all uses removed.
 +      * insdel.c (move_gap, move_gap_both, gap_left, gap_right)
 +      (adjust_markers_for_delete, adjust_markers_for_insert, adjust_point)
 +      (adjust_markers_for_replace, make_gap_larger, make_gap_smaller)
 +      (make_gap, copy_text, insert, insert_and_inherit)
 +      (insert_before_markers, insert_before_markers_and_inherit)
 +      (insert_1, count_combining_before, count_combining_after)
 +      (insert_1_both, insert_from_string)
 +      (insert_from_string_before_markers, insert_from_string_1)
 +      (insert_from_gap, insert_from_buffer, insert_from_buffer_1)
 +      (adjust_after_replace, adjust_after_insert, replace_range)
 +      (replace_range_2, del_range, del_range_1, del_range_byte)
 +      (del_range_both, del_range_2, modify_region)
 +      (prepare_to_modify_buffer, signal_before_change)
 +      (signal_after_change, Fcombine_after_change_execute):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * intervals.c (traverse_intervals, rotate_right, rotate_left)
 +      (balance_an_interval, split_interval_right, split_interval_left)
 +      (find_interval, next_interval, update_interval)
 +      (adjust_intervals_for_insertion, delete_node, delete_interval)
 +      (interval_deletion_adjustment, adjust_intervals_for_deletion)
 +      (static_offset_intervals, offset_intervals)
 +      (merge_interval_right, merge_interval_left, make_new_interval)
 +      (graft_intervals_into_buffer, temp_set_point_both)
 +      (temp_set_point, set_point, adjust_for_invis_intang)
 +      (set_point_both, move_if_not_intangible, get_property_and_range)
 +      (get_local_map, copy_intervals, copy_intervals_to_string)
 +      (compare_string_intervals, set_intervals_multibyte_1):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * intervals.h: Adjust decls to match defn changes elsewhere.
 +      (struct interval):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * keyboard.c (this_command_key_count, this_single_command_key_start)
 +      (before_command_key_count, before_command_echo_length, echo_now)
 +      (echo_length, recursive_edit_1, Frecursive_edit, Ftrack_mouse)
 +      (command_loop_1, safe_run_hooks, read_char, timer_check_2)
 +      (menu_item_eval_property, read_key_sequence, Fread_key_sequence)
 +      (Fread_key_sequence_vector, Fexecute_extended_command, Fsuspend_emacs):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (last_non_minibuf_size, last_point_position, echo_truncate)
 +      (command_loop_1, adjust_point_for_property, read_char, gen_help_event)
 +      (make_lispy_position, make_lispy_event, parse_modifiers_uncached)
 +      (parse_modifiers, modify_event_symbol, Fexecute_extended_command)
 +      (stuff_buffered_input):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (last_auto_save, command_loop_1, read_char):
 +      Use EMACS_INT, not int, to avoid integer overflow.
 +      (record_char): Avoid overflow in total_keys computation.
 +      (parse_modifiers_uncached): Redo index calculation to avoid overflow.
 +      * keyboard.h: Adjust decls to match defn changes elsewhere.
 +      * keymap.c (Fdefine_key, Fcurrent_active_maps, accessible_keymaps_1)
 +      (Fkey_description, Fdescribe_vector, Flookup_key):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (click_position): New function, to check that positions are in range.
 +      (Fcurrent_active_maps):
 +      (describe_command):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Faccessible_keymaps, Fkey_description):
 +      (preferred_sequence_p):
 +      Don't assume fixnum can fit into int.
 +      (Fkey_description): Use SAFE_ALLOCA_LISP, not alloca.
 +      Check for integer overflow in size calculations.
 +      (Ftext_char_description): Use CHECK_CHARACTER, not CHECK_NUMBER, to
 +      avoid mishandling large integers.
 +      * lisp.h: Adjust decls to match defn changes elsewhere.
 +      (ARRAY_MARK_FLAG, PSEUDOVECTOR_FLAG, struct Lisp_String)
 +      (struct vectorlike_header, struct Lisp_Subr, struct Lisp_Hash_Table)
 +      (struct Lisp_Marker):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (clip_to_bounds): Now an inline function, moved here from editfns.c.
 +      (GLYPH_CODE_P): Check for overflow in system types, subsuming the
 +      need for GLYPH_CODE_CHAR_VALID_P and doing proper checking ourselves.
 +      All callers changed.
 +      (GLYPH_CODE_CHAR, GLYPH_CODE_FACE):
 +      Assume the arg has valid form, since it always does.
 +      (TYPE_RANGED_INTEGERP): Avoid bug when checking against a wide
 +      unsigned integer system type.
 +      (CHECK_RANGED_INTEGER, CHECK_TYPE_RANGED_INTEGER): New macros.
 +      (struct catchtag, specpdl_size, SPECPDL_INDEX, USE_SAFE_ALLOCA):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (struct catchtag): Use EMACS_INT, not int, since it may be a fixnum.
 +      (duration_to_sec_usec): New decl.
 +      * lread.c (read_from_string_index, read_from_string_index_byte)
 +      (read_from_string_limit, readchar, unreadchar, openp)
 +      (read_internal_start, read1, oblookup):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Fload, readevalloop, Feval_buffer, Feval_region):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (openp): Check for out-of-range argument to 'access'.
 +      (read1): Use int, not EMACS_INT, where int is wide enough.
 +      Don't assume fixnum fits into int.
 +      (read_filtered_event): Use duration_to_sec_usec
 +      to do proper overflow checking on durations.
 +      * macros.c (Fstart_kbd_macro): Use xpalloc to check for overflow
 +      in size calculation.
 +      (Fexecute_kbd_macro):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * marker.c (cached_charpos, cached_bytepos, CONSIDER)
 +      (byte_char_debug_check, buf_charpos_to_bytepos, verify_bytepos)
 +      (buf_bytepos_to_charpos, Fset_marker, set_marker_restricted)
 +      (set_marker_both, set_marker_restricted_both, marker_position)
 +      (marker_byte_position, Fbuffer_has_markers_at):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Fset_marker, set_marker_restricted): Don't assume fixnum fits in int.
 +      * menu.c (ensure_menu_items): Renamed from grow_menu_items.
 +      It now merely ensures that the menu is large enough, without
 +      necessarily growing it, as this avoids some integer overflow issues.
 +      All callers changed.
 +      (keymap_panes, parse_single_submenu, Fx_popup_menu):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (parse_single_submenu, Fx_popup_menu): Don't assume fixnum fits in int.
 +      Use SAFE_ALLOCA_LISP, not alloca.
 +      (find_and_return_menu_selection): Avoid unnecessary casts of pointers
 +      to EMACS_INT.  Check that fixnums are in proper range for system types.
 +      * minibuf.c (minibuf_prompt_width, string_to_object)
 +      (Fminibuffer_contents, Fminibuffer_contents_no_properties)
 +      (Fminibuffer_completion_contents, Ftry_completion, Fall_completions):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (get_minibuffer, read_minibuf_unwind):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (read_minibuf): Omit unnecessary arg BACKUP_N, which is always nil;
 +      this simplifies overflow checking.  All callers changed.
 +      (read_minibuf, Fread_buffer, Ftry_completion, Fall_completions)
 +      (Ftest_completion):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * nsfns.m (check_ns_display_info): Don't assume fixnum fits in long.
 +      (x_set_menu_bar_lines, x_set_tool_bar_lines, Fx_create_frame):
 +      Check that fixnums are in proper range for system types.
 +      (Fx_create_frame, Fx_show_tip):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * nsfont.m (ns_findfonts, nsfont_list_family):
 +      Don't assume fixnum fits in long.
 +      * nsmenu.m (ns_update_menubar, ns_menu_show, ns_popup_dialog):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (ns_update_menubar): Use intptr_t, not EMACS_INT, when intptr_t is
 +      wide enough.
 +      * nsselect.m (ns_get_local_selection, clean_local_selection_data):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * print.c (print_buffer_size, print_buffer_pos, print_buffer_pos_byte)
 +      (PRINTDECLARE, PRINTPREPARE):
 +      (strout, print_string):
 +      (print, print_preprocess, print_check_string_charset_prop)
 +      (print_object):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (PRINTDECLARE):
 +      (temp_output_buffer_setup, Fprin1_to_string, print_object):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (PRINTPREPARE): Use int, not ptrdiff_t, where int is wide enough.
 +      (PRINTFINISH): Use SAFE_ALLOCA, not alloca.
 +      (printchar, strout): Use xpalloc to catch size calculation overflow.
 +      (Fexternal_debugging_output): Use CHECK_CHARACTER, not CHECK_NUMBER,
 +      to avoid mishandling large integers.
 +      (print_error_message): Use SAFE_ALLOCA, not alloca.
 +      (print_object): Use int, not EMACS_INT, where int is wide enough.
 +      * process.c (Fdelete_process): Don't assume pid fits into EMACS_INT.
 +      (Fset_process_window_size, Fformat_network_address)
 +      (get_lisp_to_sockaddr_size, set_socket_option, Fmake_network_process)
 +      (Fsignal_process, sigchld_handler):
 +      Check that fixnums are in proper range for system types.
 +      (Fformat_network_address, read_process_output, send_process)
 +      (Fprocess_send_region, status_notify):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Fformat_network_address, Fmake_serial_process, Fmake_network_process)
 +      (wait_reading_process_output, read_process_output, exec_sentinel):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (conv_lisp_to_sockaddr): Don't assume fixnums fit into int.
 +      (Faccept_process_output): Use duration_to_sec_usec to do proper
 +      overflow checking on durations.
 +      * puresize.h (PURESIZE_RATIO): Shrink this to 8/6 on 32-bit hosts
 +      configured --with-wide-int.
 +      * scroll.c (calculate_scrolling, calculate_direct_scrolling)
 +      (line_ins_del): Use int, not EMACS_INT, where int is wide enough.
 +      * search.c (looking_at_1, string_match_1):
 +      (fast_string_match, fast_c_string_match_ignore_case)
 +      (fast_string_match_ignore_case, fast_looking_at, scan_buffer)
 +      (scan_newline, find_before_next_newline, search_command)
 +      (trivial_regexp_p, search_buffer, simple_search, boyer_moore)
 +      (set_search_regs, wordify):
 +      (Freplace_match):
 +      (Fmatch_data):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (string_match_1, search_buffer, set_search_regs):
 +      (Fmatch_data):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (wordify): Check for overflow in size calculation.
 +      (Freplace_match): Avoid potential buffer overflow in search_regs.start.
 +      (Fset_match_data): Don't assume fixnum fits in ptrdiff_t.
 +      Check that fixnums are in proper range for system types.
 +      * sound.c (struct sound_device)
 +      (wav_play, au_play, vox_write, alsa_period_size, alsa_write):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Fplay_sound_internal):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * syntax.c (ST_COMMENT_STYLE, ST_STRING_STYLE):
 +      In definitions, make it clearer that these values must be out of range
 +      for the respective integer ranges.  This fixes a bug with
 +      ST_STRING_STYLE and non-ASCII characters.
 +      (struct lisp_parse_state, find_start_modiff)
 +      (Finternal_describe_syntax_value, scan_lists, scan_sexps_forward):
 +      (Fparse_partial_sexp):
 +      Don't assume fixnums can fit in int.
 +      (struct lisp_parse_state, find_start_pos, find_start_value)
 +      (find_start_value_byte, find_start_begv)
 +      (update_syntax_table, char_quoted, dec_bytepos)
 +      (find_defun_start, prev_char_comend_first, back_comment):
 +      (scan_words, skip_chars, skip_syntaxes, forw_comment, Fforward_comment)
 +      (scan_lists, Fbackward_prefix_chars, scan_sexps_forward):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Finternal_describe_syntax_value): Check that match_lisp is a
 +      character, not an integer, since the code stuffs it into int.
 +      (scan_words, scan_sexps_forward):
 +      Check that fixnums are in proper range for system types.
 +      (Fforward_word):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (scan_sexps_forward):
 +      Use CHARACTERP, not INTEGERP, since the value must fit into int.
 +      (Fparse_partial_sexp): Fix doc; element 8 is not ignored.
 +      * syntax.h: Adjust decls to match defn changes elsewhere.
 +      (struct gl_state_s):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * sysdep.c (wait_for_termination_1, wait_for_termination)
 +      (interruptible_wait_for_termination, mkdir):
 +      Don't assume pid_t fits in int; on 64-bit AIX pid_t is 64-bit.
 +      (emacs_read, emacs_write):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (system_process_attributes): Don't assume uid_t, gid_t, and
 +      double all fit in int or even EMACS_INT.
 +      * term.c (set_tty_color_mode):
 +      Check that fixnums are in proper range for system types.
 +      * termhooks.h (struct input_event):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * textprop.c (validate_interval_range, interval_of)
 +      (Fadd_text_properties, set_text_properties_1)
 +      (Fremove_text_properties, Fremove_list_of_text_properties)
 +      (Ftext_property_any, Ftext_property_not_all)
 +      (copy_text_properties, text_property_list, extend_property_ranges)
 +      (verify_interval_modification):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Fnext_single_char_property_change)
 +      (Fprevious_single_char_property_change):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (copy_text_properties):
 +      Check for integer overflow in index calculation.
 +      * undo.c (last_boundary_position, record_point, record_insert)
 +      (record_delete, record_marker_adjustment, record_change)
 +      (record_property_change):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (truncate_undo_list, Fprimitive_undo): Don't assume fixnum fits in int.
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * w32fns.c (Fx_create_frame, x_create_tip_frame, Fx_show_tip)
 +      (Fx_hide_tip, Fx_file_dialog):
 +      * w32menu.c (set_frame_menubar):
 +      Use ptrdiff_t, not int, for consistency with rest of code.
 +      * window.c (window_scroll_preserve_hpos, window_scroll_preserve_vpos)
 +      (select_window, Fdelete_other_windows_internal)
 +      (window_scroll_pixel_based, window_scroll_line_based)
 +      (Frecenter, Fset_window_configuration):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (Fset_window_hscroll, run_window_configuration_change_hook)
 +      (set_window_buffer, temp_output_buffer_show, scroll_command)
 +      (Fscroll_other_window, Frecenter):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (Fwindow_line_height, window_scroll, Fscroll_left, Fscroll_right):
 +      Don't assume fixnum fits in int.
 +      (Fset_window_scroll_bars):
 +      Check that fixnums are in proper range for system types.
 +      * xdisp.c (help_echo_pos, pos_visible_p, string_pos_nchars_ahead)
 +      (string_pos, c_string_pos, number_of_chars, init_iterator)
 +      (in_ellipses_for_invisible_text_p, init_from_display_pos)
 +      (compute_stop_pos, next_overlay_change, compute_display_string_pos)
 +      (compute_display_string_end, handle_face_prop)
 +      (face_before_or_after_it_pos, handle_invisible_prop)
 +      (handle_display_prop, handle_display_spec, handle_single_display_spec)
 +      (display_prop_intangible_p, string_buffer_position_lim)
 +      (string_buffer_position, handle_composition_prop, load_overlay_strings)
 +      (get_overlay_strings_1, get_overlay_strings)
 +      (iterate_out_of_display_property, forward_to_next_line_start)
 +      (back_to_previous_visible_line_start, reseat, reseat_to_string)
 +      (get_next_display_element, set_iterator_to_next)
 +      (get_visually_first_element, compute_stop_pos_backwards)
 +      (handle_stop_backwards, next_element_from_buffer)
 +      (move_it_in_display_line_to, move_it_in_display_line)
 +      (move_it_to, move_it_vertically_backward, move_it_by_lines)
 +      (add_to_log, message_dolog, message_log_check_duplicate)
 +      (message2, message2_nolog, message3, message3_nolog
 +      (with_echo_area_buffer, display_echo_area_1, resize_mini_window_1)
 +      (current_message_1, truncate_echo_area, truncate_message_1)
 +      (set_message, set_message_1, store_mode_line_noprop)
 +      (hscroll_window_tree, debug_delta, debug_delta_bytes, debug_end_vpos)
 +      (text_outside_line_unchanged_p, check_point_in_composition)
 +      (reconsider_clip_changes)
 +      (redisplay_internal, set_cursor_from_row, try_scrolling)
 +      (try_cursor_movement, set_vertical_scroll_bar, redisplay_window)
 +      (redisplay_window, find_last_unchanged_at_beg_row)
 +      (find_first_unchanged_at_end_row, row_containing_pos, try_window_id)
 +      (trailing_whitespace_p, find_row_edges, display_line)
 +      (RECORD_MAX_MIN_POS, Fcurrent_bidi_paragraph_direction)
 +      (display_mode_element, store_mode_line_string)
 +      (pint2str, pint2hrstr, decode_mode_spec)
 +      (display_count_lines, display_string, draw_glyphs)
 +      (x_produce_glyphs, x_insert_glyphs)
 +      (rows_from_pos_range, mouse_face_from_buffer_pos)
 +      (fast_find_string_pos, mouse_face_from_string_pos)
 +      (note_mode_line_or_margin_highlight, note_mouse_highlight):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (safe_call, init_from_display_pos, handle_fontified_prop)
 +      (handle_single_display_spec, load_overlay_strings)
 +      (with_echo_area_buffer, setup_echo_area_for_printing)
 +      (display_echo_area, echo_area_display)
 +      (x_consider_frame_title, prepare_menu_bars, update_menu_bar)
 +      (update_tool_bar, hscroll_window_tree, redisplay_internal)
 +      (redisplay_window, dump_glyph_row, display_mode_line)
 +      (Fformat_mode_line, decode_mode_spec, on_hot_spot_p):
 +      (handle_display_spec, display_prop_string_p):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (handle_single_display_spec, build_desired_tool_bar_string)
 +      (redisplay_tool_bar, scroll_window_tree, Fdump_glyph_matrix)
 +      (get_specified_cursor_type):
 +      Check that fixnums are in proper range for system types.
 +      (struct overlay_entry, resize_mini_window, Fdump_glyph_row)
 +      (Flookup_image_map):
 +      Don't assume fixnums fit in int.
 +      (compare_overlay_entries):
 +      Avoid mishandling comparisons due to subtraction overflow.
 +      (load_overlay_strings): Use SAFE_NALLOCA, not alloca.
 +      (last_escape_glyph_face_id, last_glyphless_glyph_face_id):
 +      (handle_tool_bar_click):
 +      Use int, not unsigned, since we prefer signed and the signedness
 +      doesn't matter here.
 +      (get_next_display_element, next_element_from_display_vector):
 +      Use int, not EMACS_INT, when int is wide enough.
 +      (start_hourglass): Use duration_to_sec_usec to do proper
 +      overflow checking on durations.
 +      * xfaces.c (Fbitmap_spec_p):
 +      Check that fixnums are in proper range for system types.
 +      (compare_fonts_by_sort_order):
 +      Avoid mishandling comparisons due to subtraction overflow.
 +      (Fx_family_fonts, realize_basic_faces):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (Fx_family_fonts):
 +      Don't assume fixnum fits in int.
 +      Use SAFE_ALLOCA_LISP, not alloca.
 +      (merge_face_heights): Remove unnecessary cast to EMACS_INT.
 +      (Finternal_make_lisp_face): Don't allocate more than MAX_FACE_ID.
 +      (face_at_buffer_position, face_for_overlay_string)
 +      (face_at_string_position):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      (merge_faces): Use int, not EMACS_INT, where int is wide enough.
 +      * xfns.c (x_set_menu_bar_lines, x_set_tool_bar_lines, x_icon_verify)
 +      (Fx_show_tip):
 +      Check that fixnums are in proper range for system types.
 +      (Fx_create_frame, x_create_tip_frame, Fx_show_tip)
 +      (Fx_hide_tip, Fx_file_dialog, Fx_select_font):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (Fx_change_window_property): Don't assume fixnums fit in int.
 +      * xfont.c (xfont_chars_supported):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * xmenu.c (Fx_popup_dialog, set_frame_menubar)
 +      (create_and_show_popup_menu, create_and_show_dialog, xmenu_show):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * xml.c (parse_region):
 +      * xrdb.c (magic_file_p):
 +      Use ptrdiff_t, not EMACS_INT, where ptrdiff_t is wide enough.
 +      * xselect.c (TRACE1): Don't assume pid_t promotes to int.
 +      (x_get_local_selection, x_reply_selection_request)
 +      (x_handle_selection_request, wait_for_property_change):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      (selection_data_to_lisp_data): Use short, not EMACS_INT, where
 +      short is wide enough.
 +      (x_send_client_event): Don't assume fixnum fits in int.
 +      * xterm.c (x_x_to_emacs_modifiers):
 +      Don't assume EMACS_INT overflows nicely into int.
 +      (x_emacs_to_x_modifiers): Use EMACS_INT, not int, because values
 +      may come from Lisp.
 +      (handle_one_xevent): NATNUMP can eval its arg twice.
 +      (x_connection_closed):
 +      Use ptrdiff_t, not int, to avoid needless 32-bit limit on 64-bit hosts.
 +      * xterm.h: Adjust decls to match defn changes elsewhere.
 +      (struct scroll_bar): Use struct vectorlike_header
 +      rather than rolling our own approximation.
 +      (SCROLL_BAR_VEC_SIZE): Remove; not used.
 +
+ 2011-09-28  Eli Zaretskii  <eliz@gnu.org>
+       * xdisp.c (compute_display_string_end): If there's no display
+       string at CHARPOS, return -1.
+       * bidi.c (bidi_fetch_char): When compute_display_string_end
+       returns a negative value, treat the character as a normal
+       character not covered by a display string.  (Bug#9624)
+ 2011-09-28  Juanma Barranquero  <lekktu@gmail.com>
+       * lread.c (Fread_from_string): Fix typo in docstring.
+ 2011-09-27  Eli Zaretskii  <eliz@gnu.org>
+       * xdisp.c (handle_invisible_prop): If invisible text ends on a
+       newline, reseat the iterator instead of bidi-iterating there one
+       character at a time.  (Bug#9610)
+       (BUFFER_POS_REACHED_P, move_it_in_display_line_to): Bail when past
+       TO_CHARPOS if the bidi iterator is at base embedding level.
  2011-09-27  Andreas Schwab  <schwab@linux-m68k.org>
  
        * lread.c (readevalloop): Use correct code for NBSP.
diff --combined src/bidi.c
index c7b3a77059721baa9f565d02860c5ef6a916dc7d,e3fc03f4a9bc274f57835fc430529b0ea36f293d..a43490f4110c19ffd58d54323bdcc1e6d5c30342
@@@ -204,7 -204,7 +204,7 @@@ bidi_mirror_char (int c
    val = CHAR_TABLE_REF (bidi_mirror_table, c);
    if (INTEGERP (val))
      {
 -      int v = XINT (val);
 +      EMACS_INT v = XINT (val);
  
        if (v < 0 || v > MAX_CHAR)
        abort ();
@@@ -378,7 -378,7 +378,7 @@@ bidi_cache_fetch_state (ptrdiff_t idx, 
     resolved levels in cached states.  DIR, if non-zero, means search
     in that direction from the last cache hit.  */
  static inline ptrdiff_t
 -bidi_cache_search (EMACS_INT charpos, int level, int dir)
 +bidi_cache_search (ptrdiff_t charpos, int level, int dir)
  {
    ptrdiff_t i, i_start;
  
@@@ -562,7 -562,7 +562,7 @@@ bidi_cache_iterator_state (struct bidi_
  }
  
  static inline bidi_type_t
 -bidi_cache_find (EMACS_INT charpos, int level, struct bidi_it *bidi_it)
 +bidi_cache_find (ptrdiff_t charpos, int level, struct bidi_it *bidi_it)
  {
    ptrdiff_t i = bidi_cache_search (charpos, level, bidi_it->scan_dir);
  
@@@ -795,7 -795,7 +795,7 @@@ bidi_set_paragraph_end (struct bidi_it 
  
  /* Initialize the bidi iterator from buffer/string position CHARPOS.  */
  void
 -bidi_init_it (EMACS_INT charpos, EMACS_INT bytepos, int frame_window_p,
 +bidi_init_it (ptrdiff_t charpos, ptrdiff_t bytepos, int frame_window_p,
              struct bidi_it *bidi_it)
  {
    if (! bidi_initialized)
@@@ -864,11 -864,11 +864,11 @@@ bidi_line_init (struct bidi_it *bidi_it
     are zero-based character positions in S, BEGBYTE is byte position
     corresponding to BEG.  UNIBYTE, if non-zero, means S is a unibyte
     string.  */
 -static inline EMACS_INT
 -bidi_count_bytes (const unsigned char *s, const EMACS_INT beg,
 -                const EMACS_INT begbyte, const EMACS_INT end, int unibyte)
 +static inline ptrdiff_t
 +bidi_count_bytes (const unsigned char *s, const ptrdiff_t beg,
 +                const ptrdiff_t begbyte, const ptrdiff_t end, int unibyte)
  {
 -  EMACS_INT pos = beg;
 +  ptrdiff_t pos = beg;
    const unsigned char *p = s + begbyte, *start = p;
  
    if (unibyte)
     character from the current buffer.  UNIBYTE non-zero means S is a
     unibyte string.  */
  static inline int
 -bidi_char_at_pos (EMACS_INT bytepos, const unsigned char *s, int unibyte)
 +bidi_char_at_pos (ptrdiff_t bytepos, const unsigned char *s, int unibyte)
  {
    if (s)
      {
     string to iterate, or NULL if iterating over a buffer or a Lisp
     string; in the latter case, STRING->lstring is the Lisp string.  */
  static inline int
 -bidi_fetch_char (EMACS_INT bytepos, EMACS_INT charpos, EMACS_INT *disp_pos,
 +bidi_fetch_char (ptrdiff_t bytepos, ptrdiff_t charpos, ptrdiff_t *disp_pos,
                 int *disp_prop, struct bidi_string_data *string,
 -               int frame_window_p, EMACS_INT *ch_len, EMACS_INT *nchars)
 +               int frame_window_p, ptrdiff_t *ch_len, ptrdiff_t *nchars)
  {
    int ch;
 -  EMACS_INT endpos
 +  ptrdiff_t endpos
      = (string->s || STRINGP (string->lstring)) ? string->schars : ZV;
    struct text_pos pos;
  
      }
    else if (charpos >= *disp_pos && *disp_prop)
      {
 -      EMACS_INT disp_end_pos;
 +      ptrdiff_t disp_end_pos;
  
        /* We don't expect to find ourselves in the middle of a display
         property.  Hopefully, it will never be needed.  */
          ch = 0xFFFC;
        }
        disp_end_pos = compute_display_string_end (*disp_pos, string);
+       if (disp_end_pos < 0)
+       {
+         /* Somebody removed the display string from the buffer
+            behind our back.  Recover by processing this buffer
+            position as if no display property were present there to
+            begin with.  */
+         *disp_prop = 0;
+         goto normal_char;
+       }
        *nchars = disp_end_pos - *disp_pos;
        if (*nchars <= 0)
        abort ();
      }
    else
      {
+     normal_char:
        if (string->s)
        {
          int len;
     following the buffer position, -1 if position is at the beginning
     of a new paragraph, or -2 if position is neither at beginning nor
     at end of a paragraph.  */
 -static EMACS_INT
 -bidi_at_paragraph_end (EMACS_INT charpos, EMACS_INT bytepos)
 +static ptrdiff_t
 +bidi_at_paragraph_end (ptrdiff_t charpos, ptrdiff_t bytepos)
  {
    Lisp_Object sep_re;
    Lisp_Object start_re;
 -  EMACS_INT val;
 +  ptrdiff_t val;
  
    sep_re = paragraph_separate_re;
    start_re = paragraph_start_re;
     Value is the byte position of the paragraph's beginning, or
     BEGV_BYTE if paragraph_start_re is still not found after looking
     back MAX_PARAGRAPH_SEARCH lines in the buffer.  */
 -static EMACS_INT
 -bidi_find_paragraph_start (EMACS_INT pos, EMACS_INT pos_byte)
 +static ptrdiff_t
 +bidi_find_paragraph_start (ptrdiff_t pos, ptrdiff_t pos_byte)
  {
    Lisp_Object re = paragraph_start_re;
 -  EMACS_INT limit = ZV, limit_byte = ZV_BYTE;
 -  EMACS_INT n = 0;
 +  ptrdiff_t limit = ZV, limit_byte = ZV_BYTE;
 +  ptrdiff_t n = 0;
  
    while (pos_byte > BEGV_BYTE
         && n++ < MAX_PARAGRAPH_SEARCH
  void
  bidi_paragraph_init (bidi_dir_t dir, struct bidi_it *bidi_it, int no_default_p)
  {
 -  EMACS_INT bytepos = bidi_it->bytepos;
 +  ptrdiff_t bytepos = bidi_it->bytepos;
    int string_p = bidi_it->string.s != NULL || STRINGP (bidi_it->string.lstring);
 -  EMACS_INT pstartbyte;
 +  ptrdiff_t pstartbyte;
    /* Note that begbyte is a byte position, while end is a character
       position.  Yes, this is ugly, but we are trying to avoid costly
       calls to BYTE_TO_CHAR and its ilk.  */
 -  EMACS_INT begbyte = string_p ? 0 : BEGV_BYTE;
 -  EMACS_INT end = string_p ? bidi_it->string.schars : ZV;
 +  ptrdiff_t begbyte = string_p ? 0 : BEGV_BYTE;
 +  ptrdiff_t end = string_p ? bidi_it->string.schars : ZV;
  
    /* Special case for an empty buffer. */
    if (bytepos == begbyte && bidi_it->charpos == end)
    else if (dir == NEUTRAL_DIR)        /* P2 */
      {
        int ch;
 -      EMACS_INT ch_len, nchars;
 -      EMACS_INT pos, disp_pos = -1;
 +      ptrdiff_t ch_len, nchars;
 +      ptrdiff_t pos, disp_pos = -1;
        int disp_prop = 0;
        bidi_type_t type;
        const unsigned char *s;
              bidi_it->paragraph_dir = L2R; /* P3 and HL1 */
            else
              {
 -              EMACS_INT prevpbyte = pstartbyte;
 -              EMACS_INT p = BYTE_TO_CHAR (pstartbyte), pbyte = pstartbyte;
 +              ptrdiff_t prevpbyte = pstartbyte;
 +              ptrdiff_t p = BYTE_TO_CHAR (pstartbyte), pbyte = pstartbyte;
  
                /* Find the beginning of the previous paragraph, if any.  */
                while (pbyte > BEGV_BYTE && prevpbyte >= pstartbyte)
@@@ -1511,7 -1521,7 +1521,7 @@@ bidi_resolve_explicit (struct bidi_it *
  {
    int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level;
    int new_level  = bidi_resolve_explicit_1 (bidi_it);
 -  EMACS_INT eob = bidi_it->string.s ? bidi_it->string.schars : ZV;
 +  ptrdiff_t eob = bidi_it->string.s ? bidi_it->string.schars : ZV;
    const unsigned char *s
      = (STRINGP (bidi_it->string.lstring)
         ? SDATA (bidi_it->string.lstring)
@@@ -1599,7 -1609,7 +1609,7 @@@ bidi_resolve_weak (struct bidi_it *bidi
    int next_char;
    bidi_type_t type_of_next;
    struct bidi_it saved_it;
 -  EMACS_INT eob
 +  ptrdiff_t eob
      = ((STRINGP (bidi_it->string.lstring) || bidi_it->string.s)
         ? bidi_it->string.schars : ZV);
  
            type = WEAK_EN;
          else                  /* W5: ET/BN with EN after it.  */
            {
 -            EMACS_INT en_pos = bidi_it->charpos + bidi_it->nchars;
 +            ptrdiff_t en_pos = bidi_it->charpos + bidi_it->nchars;
              const unsigned char *s = (STRINGP (bidi_it->string.lstring)
                                        ? SDATA (bidi_it->string.lstring)
                                        : bidi_it->string.s);
@@@ -1972,11 -1982,11 +1982,11 @@@ bidi_level_of_next_char (struct bidi_i
    bidi_type_t type;
    int level, prev_level = -1;
    struct bidi_saved_info next_for_neutral;
 -  EMACS_INT next_char_pos = -2;
 +  ptrdiff_t next_char_pos = -2;
  
    if (bidi_it->scan_dir == 1)
      {
 -      EMACS_INT eob
 +      ptrdiff_t eob
        = ((bidi_it->string.s || STRINGP (bidi_it->string.lstring))
           ? bidi_it->string.schars : ZV);
  
        && bidi_it->next_for_ws.type == UNKNOWN_BT)
      {
        int ch;
 -      EMACS_INT clen = bidi_it->ch_len;
 -      EMACS_INT bpos = bidi_it->bytepos;
 -      EMACS_INT cpos = bidi_it->charpos;
 -      EMACS_INT disp_pos = bidi_it->disp_pos;
 -      EMACS_INT nc = bidi_it->nchars;
 +      ptrdiff_t clen = bidi_it->ch_len;
 +      ptrdiff_t bpos = bidi_it->bytepos;
 +      ptrdiff_t cpos = bidi_it->charpos;
 +      ptrdiff_t disp_pos = bidi_it->disp_pos;
 +      ptrdiff_t nc = bidi_it->nchars;
        struct bidi_string_data bs = bidi_it->string;
        bidi_type_t chtype;
        int fwp = bidi_it->frame_window_p;
@@@ -2349,7 -2359,7 +2359,7 @@@ bidi_move_to_visually_next (struct bidi
        bidi_it->separator_limit = bidi_it->string.schars;
        else if (bidi_it->bytepos < ZV_BYTE)
        {
 -        EMACS_INT sep_len
 +        ptrdiff_t sep_len
            = bidi_at_paragraph_end (bidi_it->charpos + bidi_it->nchars,
                                     bidi_it->bytepos + bidi_it->ch_len);
          if (bidi_it->nchars <= 0)
@@@ -2415,6 -2425,6 +2425,6 @@@ bidi_dump_cached_states (void
    fputs ("\n", stderr);
    fputs ("pos ", stderr);
    for (i = 0; i < bidi_cache_idx; i++)
 -    fprintf (stderr, "%*"pI"d", ndigits, bidi_cache[i].charpos);
 +    fprintf (stderr, "%*"pD"d", ndigits, bidi_cache[i].charpos);
    fputs ("\n", stderr);
  }
diff --combined src/lread.c
index 1179d63436f567d9f2987d2c3116ab41ff6fa505,af737d27070a296c2c252eff7e5d75e5c3e740ef..91195a88e631ed6e9329bc0ba368e2169f2eb032
@@@ -109,9 -109,9 +109,9 @@@ static FILE *instream
  static int read_pure;
  
  /* For use within read-from-string (this reader is non-reentrant!!)  */
 -static EMACS_INT read_from_string_index;
 -static EMACS_INT read_from_string_index_byte;
 -static EMACS_INT read_from_string_limit;
 +static ptrdiff_t read_from_string_index;
 +static ptrdiff_t read_from_string_index_byte;
 +static ptrdiff_t read_from_string_limit;
  
  /* Number of characters read in the current call to Fread or
     Fread_from_string. */
@@@ -209,7 -209,7 +209,7 @@@ readchar (Lisp_Object readcharfun, int 
      {
        register struct buffer *inbuffer = XBUFFER (readcharfun);
  
 -      EMACS_INT pt_byte = BUF_PT_BYTE (inbuffer);
 +      ptrdiff_t pt_byte = BUF_PT_BYTE (inbuffer);
  
        if (pt_byte >= BUF_ZV_BYTE (inbuffer))
        return -1;
      {
        register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
  
 -      EMACS_INT bytepos = marker_byte_position (readcharfun);
 +      ptrdiff_t bytepos = marker_byte_position (readcharfun);
  
        if (bytepos >= BUF_ZV_BYTE (inbuffer))
        return -1;
@@@ -372,8 -372,8 +372,8 @@@ unreadchar (Lisp_Object readcharfun, in
    else if (BUFFERP (readcharfun))
      {
        struct buffer *b = XBUFFER (readcharfun);
 -      EMACS_INT charpos = BUF_PT (b);
 -      EMACS_INT bytepos = BUF_PT_BYTE (b);
 +      ptrdiff_t charpos = BUF_PT (b);
 +      ptrdiff_t bytepos = BUF_PT_BYTE (b);
  
        if (! NILP (BVAR (b, enable_multibyte_characters)))
        BUF_DEC_POS (b, bytepos);
    else if (MARKERP (readcharfun))
      {
        struct buffer *b = XMARKER (readcharfun)->buffer;
 -      EMACS_INT bytepos = XMARKER (readcharfun)->bytepos;
 +      ptrdiff_t bytepos = XMARKER (readcharfun)->bytepos;
  
        XMARKER (readcharfun)->charpos--;
        if (! NILP (BVAR (b, enable_multibyte_characters)))
@@@ -608,11 -608,8 +608,11 @@@ read_filtered_event (int no_switch_fram
        int sec, usec;
        double duration = extract_float (seconds);
  
 -      sec  = (int) duration;
 -      usec = (duration - sec) * 1000000;
 +      if (0 < duration)
 +      duration_to_sec_usec (duration, &sec, &usec);
 +      else
 +      sec = usec = 0;
 +
        EMACS_GET_TIME (end_time);
        EMACS_SET_SECS_USECS (wait_time, sec, usec);
        EMACS_ADD_TIME (end_time, end_time, wait_time);
@@@ -1026,7 -1023,7 +1026,7 @@@ Return t if the file exists and loads s
  {
    register FILE *stream;
    register int fd = -1;
 -  int count = SPECPDL_INDEX ();
 +  ptrdiff_t count = SPECPDL_INDEX ();
    struct gcpro gcpro1, gcpro2, gcpro3;
    Lisp_Object found, efound, hist_file_name;
    /* 1 means we printed the ".el is newer" message.  */
  openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes, Lisp_Object *storeptr, Lisp_Object predicate)
  {
    register int fd;
 -  EMACS_INT fn_size = 100;
 +  ptrdiff_t fn_size = 100;
    char buf[100];
    register char *fn = buf;
    int absolute = 0;
 -  EMACS_INT want_length;
 +  ptrdiff_t want_length;
    Lisp_Object filename;
    struct stat st;
    struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
    Lisp_Object string, tail, encoded_fn;
 -  EMACS_INT max_suffix_len = 0;
 +  ptrdiff_t max_suffix_len = 0;
  
    CHECK_STRING (str);
  
                {
                  /* Check that we can access or open it.  */
                  if (NATNUMP (predicate))
 -                  fd = (access (pfn, XFASTINT (predicate)) == 0) ? 1 : -1;
 +                  fd = (((XFASTINT (predicate) & ~INT_MAX) == 0
 +                         && access (pfn, XFASTINT (predicate)) == 0)
 +                        ? 1 : -1);
                  else
                    fd = emacs_open (pfn, O_RDONLY, 0);
  
@@@ -1701,7 -1696,7 +1701,7 @@@ readevalloop (Lisp_Object readcharfun
  {
    register int c;
    register Lisp_Object val;
 -  int count = SPECPDL_INDEX ();
 +  ptrdiff_t count = SPECPDL_INDEX ();
    struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
    struct buffer *b = 0;
    int continue_reading_p;
    continue_reading_p = 1;
    while (continue_reading_p)
      {
 -      int count1 = SPECPDL_INDEX ();
 +      ptrdiff_t count1 = SPECPDL_INDEX ();
  
        if (b != 0 && NILP (BVAR (b, name)))
        error ("Reading from killed buffer");
@@@ -1878,7 -1873,7 +1878,7 @@@ DO-ALLOW-PRINT, if non-nil, specifies t
  This function preserves the position of point.  */)
    (Lisp_Object buffer, Lisp_Object printflag, Lisp_Object filename, Lisp_Object unibyte, Lisp_Object do_allow_print)
  {
 -  int count = SPECPDL_INDEX ();
 +  ptrdiff_t count = SPECPDL_INDEX ();
    Lisp_Object tem, buf;
  
    if (NILP (buffer))
@@@ -1923,7 -1918,7 +1923,7 @@@ This function does not move point.  */
    (Lisp_Object start, Lisp_Object end, Lisp_Object printflag, Lisp_Object read_function)
  {
    /* FIXME: Do the eval-sexp-add-defvars danse!  */
 -  int count = SPECPDL_INDEX ();
 +  ptrdiff_t count = SPECPDL_INDEX ();
    Lisp_Object tem, cbuf;
  
    cbuf = Fcurrent_buffer ();
@@@ -1971,7 -1966,7 +1971,7 @@@ DEFUN ("read-from-string", Fread_from_s
         doc: /* Read one Lisp expression which is represented as text by STRING.
  Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
  FINAL-STRING-INDEX is an integer giving the position of the next
-  remaining chararacter in STRING.
+  remaining character in STRING.
  START and END optionally delimit a substring of STRING from which to read;
   they default to 0 and (length STRING) respectively.  */)
    (Lisp_Object string, Lisp_Object start, Lisp_Object end)
@@@ -2001,7 -1996,7 +2001,7 @@@ read_internal_start (Lisp_Object stream
    if (STRINGP (stream)
        || ((CONSP (stream) && STRINGP (XCAR (stream)))))
      {
 -      EMACS_INT startval, endval;
 +      ptrdiff_t startval, endval;
        Lisp_Object string;
  
        if (STRINGP (stream))
        else
        {
          CHECK_NUMBER (end);
 -        endval = XINT (end);
 -        if (endval < 0 || endval > SCHARS (string))
 +        if (! (0 <= XINT (end) && XINT (end) <= SCHARS (string)))
            args_out_of_range (string, end);
 +        endval = XINT (end);
        }
  
        if (NILP (start))
        else
        {
          CHECK_NUMBER (start);
 -        startval = XINT (start);
 -        if (startval < 0 || startval > endval)
 +        if (! (0 <= XINT (start) && XINT (start) <= endval))
            args_out_of_range (string, start);
 +        startval = XINT (start);
        }
        read_from_string_index = startval;
        read_from_string_index_byte = string_char_to_byte (string, startval);
@@@ -2502,13 -2497,14 +2502,13 @@@ read1 (register Lisp_Object readcharfun
              if (c == '[')
                {
                  Lisp_Object tmp;
 -                EMACS_INT depth, size;
 +                int depth;
 +                ptrdiff_t size;
  
                  tmp = read_vector (readcharfun, 0);
 -                if (!INTEGERP (AREF (tmp, 0)))
 +                if (! RANGED_INTEGERP (1, AREF (tmp, 0), 3))
                    error ("Invalid depth in char-table");
                  depth = XINT (AREF (tmp, 0));
 -                if (depth < 1 || depth > 3)
 -                  error ("Invalid depth in char-table");
                  size = ASIZE (tmp) - 2;
                  if (chartab_size [depth] != size)
                    error ("Invalid size char-table");
          }
        {
          Lisp_Object name, result;
 -        EMACS_INT nbytes = p - read_buffer;
 -        EMACS_INT nchars
 +        ptrdiff_t nbytes = p - read_buffer;
 +        ptrdiff_t nchars
            = (multibyte
               ? multibyte_chars_in_text ((unsigned char *) read_buffer,
                                          nbytes)
@@@ -3891,7 -3887,7 +3891,7 @@@ OBARRAY defaults to the value of the va
     Also store the bucket number in oblookup_last_bucket_number.  */
  
  Lisp_Object
 -oblookup (Lisp_Object obarray, register const char *ptr, EMACS_INT size, EMACS_INT size_byte)
 +oblookup (Lisp_Object obarray, register const char *ptr, ptrdiff_t size, ptrdiff_t size_byte)
  {
    size_t hash;
    size_t obsize;
diff --combined src/xdisp.c
index e6a38f76dc569059bf2a6ca08f76d1a2203e64e8,a072ff2c171bb9d92353e74d4bd451b5e8dcffaf..ce2409220109fa9b6729665e290953c995d8069b
@@@ -748,7 -748,7 +748,7 @@@ static Lisp_Object Qinhibit_free_realiz
  Lisp_Object help_echo_string;
  Lisp_Object help_echo_window;
  Lisp_Object help_echo_object;
 -EMACS_INT help_echo_pos;
 +ptrdiff_t help_echo_pos;
  
  /* Temporary variable for XTread_socket.  */
  
@@@ -795,33 -795,33 +795,33 @@@ static Lisp_Object get_it_property (str
  
  static void handle_line_prefix (struct it *);
  
 -static void pint2str (char *, int, EMACS_INT);
 -static void pint2hrstr (char *, int, EMACS_INT);
 +static void pint2str (char *, int, ptrdiff_t);
 +static void pint2hrstr (char *, int, ptrdiff_t);
  static struct text_pos run_window_scroll_functions (Lisp_Object,
                                                      struct text_pos);
  static void reconsider_clip_changes (struct window *, struct buffer *);
  static int text_outside_line_unchanged_p (struct window *,
 -                                        EMACS_INT, EMACS_INT);
 +                                        ptrdiff_t, ptrdiff_t);
  static void store_mode_line_noprop_char (char);
  static int store_mode_line_noprop (const char *, int, int);
  static void handle_stop (struct it *);
 -static void handle_stop_backwards (struct it *, EMACS_INT);
 +static void handle_stop_backwards (struct it *, ptrdiff_t);
  static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
  static void ensure_echo_area_buffers (void);
  static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
  static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
  static int with_echo_area_buffer (struct window *, int,
 -                                  int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
 -                                  EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
 +                                  int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
 +                                  ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
  static void clear_garbaged_frames (void);
 -static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
 +static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
  static void pop_message (void);
 -static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
 -static void set_message (const char *, Lisp_Object, EMACS_INT, int);
 -static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
 +static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
 +static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
 +static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
  static int display_echo_area (struct window *);
 -static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
 -static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
 +static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
 +static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
  static Lisp_Object unwind_redisplay (Lisp_Object);
  static int string_char_and_length (const unsigned char *, int *);
  static struct text_pos display_prop_end (struct it *, Lisp_Object,
@@@ -834,10 -834,10 +834,10 @@@ static struct glyph_row *get_overlay_ar
  static void extend_face_to_end_of_line (struct it *);
  static int append_space_for_newline (struct it *, int);
  static int cursor_row_fully_visible_p (struct window *, int, int);
 -static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
 +static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
  static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
 -static int trailing_whitespace_p (EMACS_INT);
 -static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
 +static int trailing_whitespace_p (ptrdiff_t);
 +static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
  static void push_it (struct it *, struct text_pos *);
  static void pop_it (struct it *);
  static void sync_frame_with_window_matrix_rows (struct window *);
@@@ -850,7 -850,7 +850,7 @@@ static Lisp_Object redisplay_window_err
  static Lisp_Object redisplay_window_0 (Lisp_Object);
  static Lisp_Object redisplay_window_1 (Lisp_Object);
  static int set_cursor_from_row (struct window *, struct glyph_row *,
 -                              struct glyph_matrix *, EMACS_INT, EMACS_INT,
 +                              struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
                                int, int);
  static int update_menu_bar (struct frame *, int, int);
  static int try_window_reusing_current_matrix (struct window *);
@@@ -862,14 -862,14 +862,14 @@@ static int display_mode_element (struc
  static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
  static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
  static void display_menu_bar (struct window *);
 -static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
 -                                    EMACS_INT *);
 +static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
 +                                    ptrdiff_t *);
  static int display_string (const char *, Lisp_Object, Lisp_Object,
 -                           EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
 +                           ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
  static void compute_line_metrics (struct it *);
  static void run_redisplay_end_trigger_hook (struct it *);
 -static int get_overlay_strings (struct it *, EMACS_INT);
 -static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
 +static int get_overlay_strings (struct it *, ptrdiff_t);
 +static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
  static void next_overlay_string (struct it *);
  static void reseat (struct it *, struct text_pos, int);
  static void reseat_1 (struct it *, struct text_pos, int);
@@@ -884,14 -884,14 +884,14 @@@ static int next_element_from_buffer (st
  static int next_element_from_composition (struct it *);
  static int next_element_from_image (struct it *);
  static int next_element_from_stretch (struct it *);
 -static void load_overlay_strings (struct it *, EMACS_INT);
 +static void load_overlay_strings (struct it *, ptrdiff_t);
  static int init_from_display_pos (struct it *, struct window *,
                                    struct display_pos *);
  static void reseat_to_string (struct it *, const char *,
 -                              Lisp_Object, EMACS_INT, EMACS_INT, int, int);
 +                              Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
  static int get_next_display_element (struct it *);
  static enum move_it_result
 -       move_it_in_display_line_to (struct it *, EMACS_INT, int,
 +       move_it_in_display_line_to (struct it *, ptrdiff_t, int,
                                   enum move_operation_enum);
  void move_it_vertically_backward (struct it *, int);
  static void init_to_row_start (struct it *, struct window *,
@@@ -901,20 -901,20 +901,20 @@@ static int init_to_row_end (struct it *
  static void back_to_previous_line_start (struct it *);
  static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
  static struct text_pos string_pos_nchars_ahead (struct text_pos,
 -                                                Lisp_Object, EMACS_INT);
 -static struct text_pos string_pos (EMACS_INT, Lisp_Object);
 -static struct text_pos c_string_pos (EMACS_INT, const char *, int);
 -static EMACS_INT number_of_chars (const char *, int);
 +                                                Lisp_Object, ptrdiff_t);
 +static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
 +static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
 +static ptrdiff_t number_of_chars (const char *, int);
  static void compute_stop_pos (struct it *);
  static void compute_string_pos (struct text_pos *, struct text_pos,
                                  Lisp_Object);
  static int face_before_or_after_it_pos (struct it *, int);
 -static EMACS_INT next_overlay_change (EMACS_INT);
 +static ptrdiff_t next_overlay_change (ptrdiff_t);
  static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
 -                              Lisp_Object, struct text_pos *, EMACS_INT, int);
 +                              Lisp_Object, struct text_pos *, ptrdiff_t, int);
  static int handle_single_display_spec (struct it *, Lisp_Object,
                                         Lisp_Object, Lisp_Object,
 -                                       struct text_pos *, EMACS_INT, int, int);
 +                                       struct text_pos *, ptrdiff_t, int, int);
  static int underlying_face_id (struct it *);
  static int in_ellipses_for_invisible_text_p (struct display_pos *,
                                               struct window *);
@@@ -1246,7 -1246,7 +1246,7 @@@ string_from_display_spec (Lisp_Object s
     Set *ROWH and *VPOS to row's visible height and VPOS (row number).  */
  
  int
 -pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
 +pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
               int *rtop, int *rbot, int *rowh, int *vpos)
  {
    struct it it;
@@@ -1550,7 -1550,7 +1550,7 @@@ string_char_and_length (const unsigned 
     in STRING, return the position NCHARS ahead (NCHARS >= 0).  */
  
  static struct text_pos
 -string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
 +string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
  {
    xassert (STRINGP (string) && nchars >= 0);
  
     for character position CHARPOS in STRING.  */
  
  static inline struct text_pos
 -string_pos (EMACS_INT charpos, Lisp_Object string)
 +string_pos (ptrdiff_t charpos, Lisp_Object string)
  {
    struct text_pos pos;
    xassert (STRINGP (string));
     means recognize multibyte characters.  */
  
  static struct text_pos
 -c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
 +c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
  {
    struct text_pos pos;
  
  /* Value is the number of characters in C string S.  MULTIBYTE_P
     non-zero means recognize multibyte characters.  */
  
 -static EMACS_INT
 +static ptrdiff_t
  number_of_chars (const char *s, int multibyte_p)
  {
 -  EMACS_INT nchars;
 +  ptrdiff_t nchars;
  
    if (multibyte_p)
      {
 -      EMACS_INT rest = strlen (s);
 +      ptrdiff_t rest = strlen (s);
        int len;
        const unsigned char *p = (const unsigned char *) s;
  
@@@ -2346,7 -2346,7 +2346,7 @@@ safe_call (ptrdiff_t nargs, Lisp_Objec
      val = Qnil;
    else
      {
 -      int count = SPECPDL_INDEX ();
 +      ptrdiff_t count = SPECPDL_INDEX ();
        struct gcpro gcpro1;
  
        GCPRO1 (args[0]);
@@@ -2496,7 -2496,7 +2496,7 @@@ check_window_end (struct window *w
  
  void
  init_iterator (struct it *it, struct window *w,
 -             EMACS_INT charpos, EMACS_INT bytepos,
 +             ptrdiff_t charpos, ptrdiff_t bytepos,
               struct glyph_row *row, enum face_id base_face_id)
  {
    int highlight_region_p;
       is invisible.  >0 means lines indented more than this value are
       invisible.  */
    it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
 -                 ? XINT (BVAR (current_buffer, selective_display))
 +                 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
 +                                                   selective_display)),
 +                                   PTRDIFF_MAX)
                   : (!NILP (BVAR (current_buffer, selective_display))
                      ? -1 : 0));
    it->selective_display_ellipsis_p
              && WINDOWP (minibuf_selected_window)
              && w == XWINDOW (minibuf_selected_window))))
      {
 -      EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
 +      ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
        it->region_beg_charpos = min (PT, markpos);
        it->region_end_charpos = max (PT, markpos);
      }
      it->redisplay_end_trigger_charpos
        = marker_position (w->redisplay_end_trigger);
    else if (INTEGERP (w->redisplay_end_trigger))
 -    it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
 +    it->redisplay_end_trigger_charpos =
 +      clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
  
    it->tab_width = SANE_TAB_WIDTH (current_buffer);
  
@@@ -2877,7 -2874,7 +2877,7 @@@ in_ellipses_for_invisible_text_p (struc
  {
    Lisp_Object prop, window;
    int ellipses_p = 0;
 -  EMACS_INT charpos = CHARPOS (pos->pos);
 +  ptrdiff_t charpos = CHARPOS (pos->pos);
  
    /* If POS specifies a position in a display vector, this might
       be for an ellipsis displayed for invisible text.  We won't
  static int
  init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
  {
 -  EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
 +  ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
    int i, overlay_strings_with_newlines = 0;
  
    /* If POS specifies a position in a display vector, this might
         pos->overlay_string_index is in IT->overlay_strings.  */
        if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
        {
 -        int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
 +        ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
          it->current.overlay_string_index = 0;
          while (n--)
            {
@@@ -3156,7 -3153,7 +3156,7 @@@ compute_stop_pos (struct it *it
  {
    register INTERVAL iv, next_iv;
    Lisp_Object object, limit, position;
 -  EMACS_INT charpos, bytepos;
 +  ptrdiff_t charpos, bytepos;
  
    /* If nowhere else, stop at the end.  */
    it->stop_charpos = it->end_charpos;
      }
    else
      {
 -      EMACS_INT pos;
 +      ptrdiff_t pos;
  
        /* If next overlay change is in front of the current stop pos
         (which is IT->end_charpos), stop there.  Note: value of
  
    if (it->cmp_it.id < 0)
      {
 -      EMACS_INT stoppos = it->end_charpos;
 +      ptrdiff_t stoppos = it->end_charpos;
  
        if (it->bidi_p && it->bidi_it.scan_dir < 0)
        stoppos = -1;
     follows.  This is like `next-overlay-change' but doesn't use
     xmalloc.  */
  
 -static EMACS_INT
 -next_overlay_change (EMACS_INT pos)
 +static ptrdiff_t
 +next_overlay_change (ptrdiff_t pos)
  {
    ptrdiff_t i, noverlays;
 -  EMACS_INT endpos;
 +  ptrdiff_t endpos;
    Lisp_Object *overlays;
  
    /* Get all overlays at the given position.  */
    for (i = 0; i < noverlays; ++i)
      {
        Lisp_Object oend;
 -      EMACS_INT oendpos;
 +      ptrdiff_t oendpos;
  
        oend = OVERLAY_END (overlays[i]);
        oendpos = OVERLAY_POSITION (oend);
     strings, non-zero otherwise.  It is set to 2 if the display string
     uses any kind of `(space ...)' spec that will produce a stretch of
     white space in the text area.  */
 -EMACS_INT
 +ptrdiff_t
  compute_display_string_pos (struct text_pos *position,
                            struct bidi_string_data *string,
                            int frame_window_p, int *disp_prop)
      (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
    Lisp_Object pos, spec, limpos;
    int string_p = (string && (STRINGP (string->lstring) || string->s));
 -  EMACS_INT eob = string_p ? string->schars : ZV;
 -  EMACS_INT begb = string_p ? 0 : BEGV;
 -  EMACS_INT bufpos, charpos = CHARPOS (*position);
 -  EMACS_INT lim =
 +  ptrdiff_t eob = string_p ? string->schars : ZV;
 +  ptrdiff_t begb = string_p ? 0 : BEGV;
 +  ptrdiff_t bufpos, charpos = CHARPOS (*position);
 +  ptrdiff_t lim =
      (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
    struct text_pos tpos;
    int rv = 0;
  }
  
  /* Return the character position of the end of the display string that
-    started at CHARPOS.  A display string is either an overlay with
-    `display' property whose value is a string or a `display' text
-    property whose value is a string.  */
+    started at CHARPOS.  If there's no display string at CHARPOS,
+    return -1.  A display string is either an overlay with `display'
+    property whose value is a string or a `display' text property whose
+    value is a string.  */
 -EMACS_INT
 -compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
 +ptrdiff_t
 +compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
  {
    /* OBJECT = nil means current buffer.  */
    Lisp_Object object =
      (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
    Lisp_Object pos = make_number (charpos);
 -  EMACS_INT eob =
 +  ptrdiff_t eob =
      (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
  
    if (charpos >= eob || (string->s && !STRINGP (object)))
      return eob;
  
+   /* It could happen that the display property or overlay was removed
+      since we found it in compute_display_string_pos above.  One way
+      this can happen is if JIT font-lock was called (through
+      handle_fontified_prop), and jit-lock-functions remove text
+      properties or overlays from the portion of buffer that includes
+      CHARPOS.  Muse mode is known to do that, for example.  In this
+      case, we return -1 to the caller, to signal that no display
+      string is actually present at CHARPOS.  See bidi_fetch_char for
+      how this is handled.
+      An alternative would be to never look for display properties past
+      it->stop_charpos.  But neither compute_display_string_pos nor
+      bidi_fetch_char that calls it know or care where the next
+      stop_charpos is.  */
    if (NILP (Fget_char_property (pos, Qdisplay, object)))
-     abort ();
+     return -1;
  
    /* Look forward for the first character where the `display' property
       changes.  */
@@@ -3448,7 -3460,7 +3463,7 @@@ handle_fontified_prop (struct it *it
             no amount of fontifying will be able to change it.  */
          NILP (prop) && IT_CHARPOS (*it) < Z))
      {
 -      int count = SPECPDL_INDEX ();
 +      ptrdiff_t count = SPECPDL_INDEX ();
        Lisp_Object val;
        struct buffer *obuf = current_buffer;
        int begv = BEGV, zv = ZV;
@@@ -3545,7 -3557,7 +3560,7 @@@ static enum prop_handle
  handle_face_prop (struct it *it)
  {
    int new_face_id;
 -  EMACS_INT next_stop;
 +  ptrdiff_t next_stop;
  
    if (!STRINGP (it->string))
      {
    else
      {
        int base_face_id;
 -      EMACS_INT bufpos;
 +      ptrdiff_t bufpos;
        int i;
        Lisp_Object from_overlay
        = (it->current.overlay_string_index >= 0
@@@ -3704,7 -3716,7 +3719,7 @@@ static in
  face_before_or_after_it_pos (struct it *it, int before_p)
  {
    int face_id, limit;
 -  EMACS_INT next_check_charpos;
 +  ptrdiff_t next_check_charpos;
    struct it it_copy;
    void *it_copy_data = NULL;
  
  
    if (STRINGP (it->string))
      {
 -      EMACS_INT bufpos, charpos;
 +      ptrdiff_t bufpos, charpos;
        int base_face_id;
  
        /* No face change past the end of the string (for the case
@@@ -3925,7 -3937,7 +3940,7 @@@ handle_invisible_prop (struct it *it
        if (!NILP (prop)
          && IT_STRING_CHARPOS (*it) < it->end_charpos)
        {
 -        EMACS_INT endpos;
 +        ptrdiff_t endpos;
  
          handled = HANDLED_RECOMPUTE_PROPS;
  
              && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
            {
              struct text_pos old;
 -            EMACS_INT oldpos;
 +            ptrdiff_t oldpos;
  
              old = it->current.string_pos;
              oldpos = CHARPOS (old);
    else
      {
        int invis_p;
 -      EMACS_INT newpos, next_stop, start_charpos, tem;
 +      ptrdiff_t newpos, next_stop, start_charpos, tem;
        Lisp_Object pos, prop, overlay;
  
        /* First of all, is there invisible text at this position?  */
          /* The position newpos is now either ZV or on visible text.  */
          if (it->bidi_p && newpos < ZV)
            {
-             /* With bidi iteration, the region of invisible text
-                could start and/or end in the middle of a non-base
-                embedding level.  Therefore, we need to skip
-                invisible text using the bidi iterator, starting at
-                IT's current position, until we find ourselves
-                outside the invisible text.  Skipping invisible text
-                _after_ bidi iteration avoids affecting the visual
-                order of the displayed text when invisible properties
-                are added or removed.  */
-             if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
 -            EMACS_INT bpos = CHAR_TO_BYTE (newpos);
++            ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
+             if (FETCH_BYTE (bpos) == '\n'
+                 || (newpos > BEGV && FETCH_BYTE (bpos - 1) == '\n'))
                {
-                 /* If we were `reseat'ed to a new paragraph,
-                    determine the paragraph base direction.  We need
-                    to do it now because next_element_from_buffer may
-                    not have a chance to do it, if we are going to
-                    skip any text at the beginning, which resets the
-                    FIRST_ELT flag.  */
-                 bidi_paragraph_init (it->paragraph_embedding,
-                                      &it->bidi_it, 1);
+                 /* If the invisible text ends on a newline or the
+                    character after a newline, we can avoid the
+                    costly, character by character, bidi iteration to
+                    newpos, and instead simply reseat the iterator
+                    there.  That's because all bidi reordering
+                    information is tossed at the newline.  This is a
+                    big win for modes that hide complete lines, like
+                    Outline, Org, etc.  (Implementation note: the
+                    call to reseat_1 is necessary, because it signals
+                    to the bidi iterator that it needs to reinit its
+                    internal information when the next element for
+                    display is requested.  */
+                 struct text_pos tpos;
+                 SET_TEXT_POS (tpos, newpos, bpos);
+                 reseat_1 (it, tpos, 0);
                }
-             do
+             else      /* Must use the slow method.  */
                {
-                 bidi_move_to_visually_next (&it->bidi_it);
+                 /* With bidi iteration, the region of invisible text
+                    could start and/or end in the middle of a
+                    non-base embedding level.  Therefore, we need to
+                    skip invisible text using the bidi iterator,
+                    starting at IT's current position, until we find
+                    ourselves outside the invisible text.  Skipping
+                    invisible text _after_ bidi iteration avoids
+                    affecting the visual order of the displayed text
+                    when invisible properties are added or
+                    removed.  */
+                 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
+                   {
+                     /* If we were `reseat'ed to a new paragraph,
+                        determine the paragraph base direction.  We
+                        need to do it now because
+                        next_element_from_buffer may not have a
+                        chance to do it, if we are going to skip any
+                        text at the beginning, which resets the
+                        FIRST_ELT flag.  */
+                     bidi_paragraph_init (it->paragraph_embedding,
+                                          &it->bidi_it, 1);
+                   }
+                 do
+                   {
+                     bidi_move_to_visually_next (&it->bidi_it);
+                   }
+                 while (it->stop_charpos <= it->bidi_it.charpos
+                        && it->bidi_it.charpos < newpos);
+                 IT_CHARPOS (*it) = it->bidi_it.charpos;
+                 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
+                 /* If we overstepped NEWPOS, record its position in
+                    the iterator, so that we skip invisible text if
+                    later the bidi iteration lands us in the
+                    invisible region again. */
+                 if (IT_CHARPOS (*it) >= newpos)
+                   it->prev_stop = newpos;
                }
-             while (it->stop_charpos <= it->bidi_it.charpos
-                    && it->bidi_it.charpos < newpos);
-             IT_CHARPOS (*it) = it->bidi_it.charpos;
-             IT_BYTEPOS (*it) = it->bidi_it.bytepos;
-             /* If we overstepped NEWPOS, record its position in the
-                iterator, so that we skip invisible text if later the
-                bidi iteration lands us in the invisible region
-                again. */
-             if (IT_CHARPOS (*it) >= newpos)
-               it->prev_stop = newpos;
            }
          else
            {
@@@ -4196,7 -4235,7 +4238,7 @@@ handle_display_prop (struct it *it
  {
    Lisp_Object propval, object, overlay;
    struct text_pos *position;
 -  EMACS_INT bufpos;
 +  ptrdiff_t bufpos;
    /* Nonzero if some property replaces the display of the text itself.  */
    int display_replaced_p = 0;
  
  static int
  handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
                     Lisp_Object overlay, struct text_pos *position,
 -                   EMACS_INT bufpos, int frame_window_p)
 +                   ptrdiff_t bufpos, int frame_window_p)
  {
    int replacing_p = 0;
    int rv;
      }
    else if (VECTORP (spec))
      {
 -      int i;
 +      ptrdiff_t i;
        for (i = 0; i < ASIZE (spec); ++i)
        if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
                                              overlay, position, bufpos,
@@@ -4368,7 -4407,7 +4410,7 @@@ display_prop_end (struct it *it, Lisp_O
  static int
  handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
                            Lisp_Object overlay, struct text_pos *position,
 -                          EMACS_INT bufpos, int display_replaced_p,
 +                          ptrdiff_t bufpos, int display_replaced_p,
                            int frame_window_p)
  {
    Lisp_Object form;
  
    if (!NILP (form) && !EQ (form, Qt))
      {
 -      int count = SPECPDL_INDEX ();
 +      ptrdiff_t count = SPECPDL_INDEX ();
        struct gcpro gcpro1;
  
        /* Bind `object' to the object having the `display' property, a
                  && (EQ (XCAR (it->font_height), Qplus)
                      || EQ (XCAR (it->font_height), Qminus))
                  && CONSP (XCDR (it->font_height))
 -                && INTEGERP (XCAR (XCDR (it->font_height))))
 +                && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
                {
                  /* `(+ N)' or `(- N)' where N is an integer.  */
                  int steps = XINT (XCAR (XCDR (it->font_height)));
                {
                  /* Evaluate IT->font_height with `height' bound to the
                     current specified height to get the new height.  */
 -                int count = SPECPDL_INDEX ();
 +                ptrdiff_t count = SPECPDL_INDEX ();
  
                  specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
                  value = safe_eval (it->font_height);
  
  int
  display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
 -                         EMACS_INT charpos, EMACS_INT bytepos)
 +                         ptrdiff_t charpos, ptrdiff_t bytepos)
  {
    int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
    struct text_pos position;
@@@ -4874,7 -4913,7 +4916,7 @@@ display_prop_string_p (Lisp_Object prop
    else if (VECTORP (prop))
      {
        /* A vector of sub-properties.  */
 -      int i;
 +      ptrdiff_t i;
        for (i = 0; i < ASIZE (prop); ++i)
        if (single_display_spec_string_p (AREF (prop, i), string))
          return 1;
     This function may only use code that doesn't eval because it is
     called asynchronously from note_mouse_highlight.  */
  
 -static EMACS_INT
 +static ptrdiff_t
  string_buffer_position_lim (Lisp_Object string,
 -                          EMACS_INT from, EMACS_INT to, int back_p)
 +                          ptrdiff_t from, ptrdiff_t to, int back_p)
  {
    Lisp_Object limit, prop, pos;
    int found = 0;
     This function may only use code that doesn't eval because it is
     called asynchronously from note_mouse_highlight.  */
  
 -static EMACS_INT
 -string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
 +static ptrdiff_t
 +string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
  {
    const int MAX_DISTANCE = 1000;
 -  EMACS_INT found = string_buffer_position_lim (string, around_charpos,
 +  ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
                                                around_charpos + MAX_DISTANCE,
                                                0);
  
@@@ -4970,7 -5009,7 +5012,7 @@@ static enum prop_handle
  handle_composition_prop (struct it *it)
  {
    Lisp_Object prop, string;
 -  EMACS_INT pos, pos_byte, start, end;
 +  ptrdiff_t pos, pos_byte, start, end;
  
    if (STRINGP (it->string))
      {
@@@ -5036,7 -5075,7 +5078,7 @@@ struct overlay_entr
  {
    Lisp_Object overlay;
    Lisp_Object string;
 -  int priority;
 +  EMACS_INT priority;
    int after_string_p;
  };
  
@@@ -5162,17 -5201,12 +5204,17 @@@ compare_overlay_entries (const void *e1
        else
        result = entry1->after_string_p ? -1 : 1;
      }
 -  else if (entry1->after_string_p)
 -    /* After-strings sorted in order of decreasing priority.  */
 -    result = entry2->priority - entry1->priority;
 +  else if (entry1->priority != entry2->priority)
 +    {
 +      if (entry1->after_string_p)
 +      /* After-strings sorted in order of decreasing priority.  */
 +      result = entry2->priority < entry1->priority ? -1 : 1;
 +      else
 +      /* Before-strings sorted in order of increasing priority.  */
 +      result = entry1->priority < entry2->priority ? -1 : 1;
 +    }
    else
 -    /* Before-strings sorted in order of increasing priority.  */
 -    result = entry1->priority - entry2->priority;
 +    result = 0;
  
    return result;
  }
     compare_overlay_entries.  */
  
  static void
 -load_overlay_strings (struct it *it, EMACS_INT charpos)
 +load_overlay_strings (struct it *it, ptrdiff_t charpos)
  {
    Lisp_Object overlay, window, str, invisible;
    struct Lisp_Overlay *ov;
 -  EMACS_INT start, end;
 -  int size = 20;
 -  int n = 0, i, j, invis_p;
 +  ptrdiff_t start, end;
 +  ptrdiff_t size = 20;
 +  ptrdiff_t n = 0, i, j;
 +  int invis_p;
    struct overlay_entry *entries
      = (struct overlay_entry *) alloca (size * sizeof *entries);
 +  USE_SAFE_ALLOCA;
  
    if (charpos <= 0)
      charpos = IT_CHARPOS (*it);
                                                                        \
        if (n == size)                                                  \
        {                                                               \
 -        int new_size = 2 * size;                                      \
          struct overlay_entry *old = entries;                          \
 -        entries =                                                     \
 -            (struct overlay_entry *) alloca (new_size                 \
 -                                           * sizeof *entries);        \
 +        SAFE_NALLOCA (entries, 2, size);                              \
          memcpy (entries, old, size * sizeof *entries);                \
 -        size = new_size;                                              \
 +        size *= 2;                                                    \
        }                                                               \
                                                                        \
        entries[n].string = (STRING);                                   \
      }
  
    CHECK_IT (it);
 +  SAFE_FREE ();
  }
  
  
     least one overlay string was found.  */
  
  static int
 -get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
 +get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
  {
    /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
       process.  This fills IT->overlay_strings with strings, and sets
        /* Set up the bidi iterator for this overlay string.  */
        if (it->bidi_p)
        {
 -        EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
 +        ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
  
          it->bidi_it.string.lstring = it->string;
          it->bidi_it.string.s = NULL;
  }
  
  static int
 -get_overlay_strings (struct it *it, EMACS_INT charpos)
 +get_overlay_strings (struct it *it, ptrdiff_t charpos)
  {
    it->string = Qnil;
    it->method = GET_FROM_BUFFER;
@@@ -5509,8 -5543,8 +5551,8 @@@ static voi
  iterate_out_of_display_property (struct it *it)
  {
    int buffer_p = BUFFERP (it->object);
 -  EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
 -  EMACS_INT bob = (buffer_p ? BEGV : 0);
 +  ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
 +  ptrdiff_t bob = (buffer_p ? BEGV : 0);
  
    xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
  
@@@ -5671,7 -5705,7 +5713,7 @@@ static in
  forward_to_next_line_start (struct it *it, int *skipped_p,
                            struct bidi_it *bidi_it_prev)
  {
 -  EMACS_INT old_selective;
 +  ptrdiff_t old_selective;
    int newline_found_p, n;
    const int MAX_NEWLINE_DISTANCE = 500;
  
       short-cut.  */
    if (!newline_found_p)
      {
 -      EMACS_INT start = IT_CHARPOS (*it);
 -      EMACS_INT limit = find_next_newline_no_quit (start, 1);
 +      ptrdiff_t start = IT_CHARPOS (*it);
 +      ptrdiff_t limit = find_next_newline_no_quit (start, 1);
        Lisp_Object pos;
  
        xassert (!STRINGP (it->string));
@@@ -5812,8 -5846,8 +5854,8 @@@ back_to_previous_visible_line_start (st
        {
        struct it it2;
        void *it2data = NULL;
 -      EMACS_INT pos;
 -      EMACS_INT beg, end;
 +      ptrdiff_t pos;
 +      ptrdiff_t beg, end;
        Lisp_Object val, overlay;
  
        SAVE_IT (it2, *it, it2data);
@@@ -5966,7 -6000,7 +6008,7 @@@ reseat_at_next_visible_line_start (stru
  static void
  reseat (struct it *it, struct text_pos pos, int force_p)
  {
 -  EMACS_INT original_pos = IT_CHARPOS (*it);
 +  ptrdiff_t original_pos = IT_CHARPOS (*it);
  
    reseat_1 (it, pos, 0);
  
@@@ -6074,7 -6108,7 +6116,7 @@@ reseat_1 (struct it *it, struct text_po
  
  static void
  reseat_to_string (struct it *it, const char *s, Lisp_Object string,
 -                EMACS_INT charpos, EMACS_INT precision, int field_width,
 +                ptrdiff_t charpos, ptrdiff_t precision, int field_width,
                  int multibyte)
  {
    /* No region in strings.  */
      }
    if (s == NULL && it->multibyte_p)
      {
 -      EMACS_INT endpos = SCHARS (it->string);
 +      ptrdiff_t endpos = SCHARS (it->string);
        if (endpos > it->end_charpos)
        endpos = it->end_charpos;
        composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
@@@ -6296,11 -6330,11 +6338,11 @@@ lookup_glyphless_char_display (int c, s
     end of buffer (or C string) is reached.  */
  
  static struct frame *last_escape_glyph_frame = NULL;
 -static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
 +static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
  static int last_escape_glyph_merged_face_id = 0;
  
  struct frame *last_glyphless_glyph_frame = NULL;
 -unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
 +int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
  int last_glyphless_glyph_merged_face_id = 0;
  
  static int
@@@ -6425,7 -6459,7 +6467,7 @@@ get_next_display_element (struct it *it
              Lisp_Object gc;
              int ctl_len;
              int face_id;
 -            EMACS_INT lface_id = 0;
 +            int lface_id = 0;
              int escape_glyph;
  
              /* Handle control characters with ^.  */
                  g = '^';           /* default glyph for Control */
                  /* Set IT->ctl_chars[0] to the glyph for `^'.  */
                  if (it->dp
 -                    && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
 -                    && GLYPH_CODE_CHAR_VALID_P (gc))
 +                    && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
                    {
                      g = GLYPH_CODE_CHAR (gc);
                      lface_id = GLYPH_CODE_FACE (gc);
              escape_glyph = '\\';
  
              if (it->dp
 -                && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
 -                && GLYPH_CODE_CHAR_VALID_P (gc))
 +                && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
                {
                  escape_glyph = GLYPH_CODE_CHAR (gc);
                  lface_id = GLYPH_CODE_FACE (gc);
        }
        else
        {
 -        EMACS_INT pos = (it->s ? -1
 +        ptrdiff_t pos = (it->s ? -1
                     : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
                     : IT_CHARPOS (*it));
          int c;
                       && (it->current.overlay_string_index
                           == it->n_overlay_strings - 1))
                {
 -                EMACS_INT ignore;
 +                ptrdiff_t ignore;
                  int next_face_id;
                  struct text_pos pos = it->current.pos;
                  INC_TEXT_POS (pos, it->multibyte_p);
@@@ -6746,7 -6782,7 +6788,7 @@@ set_iterator_to_next (struct it *it, in
                {
                  /* No more grapheme clusters in this composition.
                     Find the next stop position.  */
 -                EMACS_INT stop = it->end_charpos;
 +                ptrdiff_t stop = it->end_charpos;
                  if (it->bidi_it.scan_dir < 0)
                    /* Now we are scanning backward and don't know
                       where to stop.  */
                {
                  /* No more grapheme clusters in this composition.
                     Find the next stop position.  */
 -                EMACS_INT stop = it->end_charpos;
 +                ptrdiff_t stop = it->end_charpos;
                  if (it->bidi_it.scan_dir < 0)
                    /* Now we are scanning backward and don't know
                       where to stop.  */
                {
                  /* As the scan direction was changed, we must
                     re-compute the stop position for composition.  */
 -                EMACS_INT stop = it->end_charpos;
 +                ptrdiff_t stop = it->end_charpos;
                  if (it->bidi_it.scan_dir < 0)
                    stop = -1;
                  composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
                it->cmp_it.from = it->cmp_it.to;
              else
                {
 -                EMACS_INT stop = it->end_charpos;
 +                ptrdiff_t stop = it->end_charpos;
                  if (it->bidi_it.scan_dir < 0)
                    stop = -1;
                  composition_compute_stop_pos (&it->cmp_it,
                it->cmp_it.to = it->cmp_it.from;
              else
                {
 -                EMACS_INT stop = it->end_charpos;
 +                ptrdiff_t stop = it->end_charpos;
                  if (it->bidi_it.scan_dir < 0)
                    stop = -1;
                  composition_compute_stop_pos (&it->cmp_it,
              IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
              if (prev_scan_dir != it->bidi_it.scan_dir)
                {
 -                EMACS_INT stop = it->end_charpos;
 +                ptrdiff_t stop = it->end_charpos;
  
                  if (it->bidi_it.scan_dir < 0)
                    stop = -1;
@@@ -7054,7 -7090,7 +7096,7 @@@ next_element_from_display_vector (struc
       That seemed totally bogus - so I changed it...  */
    gc = it->dpvec[it->current.dpvec_index];
  
 -  if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
 +  if (GLYPH_CODE_P (gc))
      {
        it->c = GLYPH_CODE_CHAR (gc);
        it->len = CHAR_BYTES (it->c);
        it->face_id = it->dpvec_face_id;
        else
        {
 -        EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
 +        int lface_id = GLYPH_CODE_FACE (gc);
          if (lface_id > 0)
            it->face_id = merge_faces (it->f, Qt, lface_id,
                                       it->saved_face_id);
@@@ -7089,8 -7125,8 +7131,8 @@@ static voi
  get_visually_first_element (struct it *it)
  {
    int string_p = STRINGP (it->string) || it->s;
 -  EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
 -  EMACS_INT bob = (string_p ? 0 : BEGV);
 +  ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
 +  ptrdiff_t bob = (string_p ? 0 : BEGV);
  
    if (STRINGP (it->string))
      {
      }
    else
      {
 -      EMACS_INT orig_bytepos = it->bidi_it.bytepos;
 +      ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
  
        /* We need to prime the bidi iterator starting at the line's or
         string's beginning, before we will be able to produce the
  
    if (STRINGP (it->string) || !it->s)
      {
 -      EMACS_INT stop, charpos, bytepos;
 +      ptrdiff_t stop, charpos, bytepos;
  
        if (STRINGP (it->string))
        {
@@@ -7462,10 -7498,10 +7504,10 @@@ compute_stop_pos_backwards (struct it *
    struct text_pos pos;
    struct display_pos save_current = it->current;
    struct text_pos save_position = it->position;
 -  EMACS_INT charpos = IT_CHARPOS (*it);
 -  EMACS_INT where_we_are = charpos;
 -  EMACS_INT save_stop_pos = it->stop_charpos;
 -  EMACS_INT save_end_pos = it->end_charpos;
 +  ptrdiff_t charpos = IT_CHARPOS (*it);
 +  ptrdiff_t where_we_are = charpos;
 +  ptrdiff_t save_stop_pos = it->stop_charpos;
 +  ptrdiff_t save_end_pos = it->end_charpos;
  
    xassert (NILP (it->string) && !it->s);
    xassert (it->bidi_p);
     position.  */
  
  static void
 -handle_stop_backwards (struct it *it, EMACS_INT charpos)
 +handle_stop_backwards (struct it *it, ptrdiff_t charpos)
  {
    int bufp = !STRINGP (it->string);
 -  EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
 +  ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
    struct display_pos save_current = it->current;
    struct text_pos save_position = it->position;
    struct text_pos pos1;
 -  EMACS_INT next_stop;
 +  ptrdiff_t next_stop;
  
    /* Scan in strict logical order.  */
    xassert (it->bidi_p);
@@@ -7654,7 -7690,7 +7696,7 @@@ next_element_from_buffer (struct it *it
        /* No face changes, overlays etc. in sight, so just return a
         character from current_buffer.  */
        unsigned char *p;
 -      EMACS_INT stop;
 +      ptrdiff_t stop;
  
        /* Maybe run the redisplay end trigger hook.  Performance note:
         This doesn't seem to cost measurable time.  */
@@@ -7843,7 -7879,7 +7885,7 @@@ next_element_from_composition (struct i
  
  static enum move_it_result
  move_it_in_display_line_to (struct it *it,
 -                          EMACS_INT to_charpos, int to_x,
 +                          ptrdiff_t to_charpos, int to_x,
                            enum move_operation_enum op)
  {
    enum move_it_result result = MOVE_UNDEFINED;
    void *ppos_data = NULL;
    int may_wrap = 0;
    enum it_method prev_method = it->method;
 -  EMACS_INT prev_pos = IT_CHARPOS (*it);
 +  ptrdiff_t prev_pos = IT_CHARPOS (*it);
    int saw_smaller_pos = prev_pos < to_charpos;
  
    /* Don't produce glyphs in produce_glyphs.  */
    ((op & MOVE_TO_POS) != 0                                    \
     && BUFFERP (it->object)                                    \
     && (IT_CHARPOS (*it) == to_charpos                         \
-        || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)      \
+        || ((!it->bidi_p                                               \
+           || BIDI_AT_BASE_LEVEL (it->bidi_it))                \
+          && IT_CHARPOS (*it) > to_charpos)                    \
         || (it->what == IT_COMPOSITION                         \
           && ((IT_CHARPOS (*it) > to_charpos                   \
                && to_charpos >= it->cmp_it.charpos)            \
        if ((op & MOVE_TO_POS) != 0
          && BUFFERP (it->object)
          && it->method == GET_FROM_BUFFER
-         && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
+         && (((!it->bidi_p
+               /* When the iterator is at base embedding level, we
+                  are guaranteed that characters are delivered for
+                  display in strictly increasing order of their
+                  buffer positions.  */
+               || BIDI_AT_BASE_LEVEL (it->bidi_it))
+              && IT_CHARPOS (*it) > to_charpos)
              || (it->bidi_p
                  && (prev_method == GET_FROM_IMAGE
                      || prev_method == GET_FROM_STRETCH
  /* For external use.  */
  void
  move_it_in_display_line (struct it *it,
 -                       EMACS_INT to_charpos, int to_x,
 +                       ptrdiff_t to_charpos, int to_x,
                         enum move_operation_enum op)
  {
    if (it->line_wrap == WORD_WRAP
     displayed to the right of TO_CHARPOS on the screen.  */
  
  void
 -move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
 +move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
  {
    enum move_it_result skip, skip2 = MOVE_X_REACHED;
    int line_height, line_start_x = 0, reached = 0;
@@@ -8649,7 -8693,7 +8699,7 @@@ move_it_vertically_backward (struct it 
    int nlines, h;
    struct it it2, it3;
    void *it2data = NULL, *it3data = NULL;
 -  EMACS_INT start_pos;
 +  ptrdiff_t start_pos;
  
   move_further_back:
    xassert (dy >= 0);
        if (it->bidi_p && IT_CHARPOS (*it) > BEGV
          && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
        {
 -        EMACS_INT nl_pos =
 +        ptrdiff_t nl_pos =
            find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
  
          move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
@@@ -8838,7 -8882,7 +8888,7 @@@ move_it_past_eol (struct it *it
     truncate-lines nil.  */
  
  void
 -move_it_by_lines (struct it *it, int dvpos)
 +move_it_by_lines (struct it *it, ptrdiff_t dvpos)
  {
  
    /* The commented-out optimization uses vmotion on terminals.  This
      {
        struct it it2;
        void *it2data = NULL;
 -      EMACS_INT start_charpos, i;
 +      ptrdiff_t start_charpos, i;
  
        /* Start at the beginning of the screen line containing IT's
         position.  This may actually move vertically backwards,
@@@ -8961,7 -9005,7 +9011,7 @@@ add_to_log (const char *format, Lisp_Ob
    Lisp_Object args[3];
    Lisp_Object msg, fmt;
    char *buffer;
 -  EMACS_INT len;
 +  ptrdiff_t len;
    struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
    USE_SAFE_ALLOCA;
  
@@@ -9010,7 -9054,7 +9060,7 @@@ message_log_maybe_newline (void
     so the buffer M must NOT point to a Lisp string.  */
  
  void
 -message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
 +message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
  {
    const unsigned char *msg = (const unsigned char *) m;
  
        struct buffer *oldbuf;
        Lisp_Object oldpoint, oldbegv, oldzv;
        int old_windows_or_buffers_changed = windows_or_buffers_changed;
 -      EMACS_INT point_at_end = 0;
 -      EMACS_INT zv_at_end = 0;
 +      ptrdiff_t point_at_end = 0;
 +      ptrdiff_t zv_at_end = 0;
        Lisp_Object old_deactivate_mark, tem;
        struct gcpro gcpro1;
  
        if (multibyte
          && NILP (BVAR (current_buffer, enable_multibyte_characters)))
        {
 -        EMACS_INT i;
 +        ptrdiff_t i;
          int c, char_bytes;
          char work[1];
  
        else if (! multibyte
               && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
        {
 -        EMACS_INT i;
 +        ptrdiff_t i;
          int c, char_bytes;
          unsigned char str[MAX_MULTIBYTE_LENGTH];
          /* Convert a single-byte string to multibyte
  
        if (nlflag)
        {
 -        EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
 +        ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
          printmax_t dups;
          insert_1 ("\n", 1, 1, 0, 0);
  
     value N > 1 if we should also append " [N times]".  */
  
  static intmax_t
 -message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
 +message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
  {
 -  EMACS_INT i;
 -  EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
 +  ptrdiff_t i;
 +  ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
    int seen_dots = 0;
    unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
    unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
     This may GC, so the buffer M must NOT point to a Lisp string.  */
  
  void
 -message2 (const char *m, EMACS_INT nbytes, int multibyte)
 +message2 (const char *m, ptrdiff_t nbytes, int multibyte)
  {
    /* First flush out any partial line written with print.  */
    message_log_maybe_newline ();
  /* The non-logging counterpart of message2.  */
  
  void
 -message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
 +message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
  {
    struct frame *sf = SELECTED_FRAME ();
    message_enable_multibyte = multibyte;
     This function cancels echoing.  */
  
  void
 -message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
 +message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
  {
    struct gcpro gcpro1;
  
     and make this cancel echoing.  */
  
  void
 -message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
 +message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
  {
    struct frame *sf = SELECTED_FRAME ();
    message_enable_multibyte = multibyte;
@@@ -9626,12 -9670,12 +9676,12 @@@ ensure_echo_area_buffers (void
  
  static int
  with_echo_area_buffer (struct window *w, int which,
 -                     int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
 -                     EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
 +                     int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
 +                     ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
  {
    Lisp_Object buffer;
    int this_one, the_other, clear_buffer_p, rc;
 -  int count = SPECPDL_INDEX ();
 +  ptrdiff_t count = SPECPDL_INDEX ();
  
    /* If buffers aren't live, make new ones.  */
    ensure_echo_area_buffers ();
@@@ -9807,7 -9851,7 +9857,7 @@@ setup_echo_area_for_printing (int multi
  
        if (Z > BEG)
        {
 -        int count = SPECPDL_INDEX ();
 +        ptrdiff_t count = SPECPDL_INDEX ();
          specbind (Qinhibit_read_only, Qt);
          /* Note that undo recording is always disabled.  */
          del_range (BEG, Z);
  static int
  display_echo_area (struct window *w)
  {
 -  int i, no_message_p, window_height_changed_p, count;
 +  int i, no_message_p, window_height_changed_p;
  
    /* Temporarily disable garbage collections while displaying the echo
       area.  This is done because a GC can print a message itself.
       That message would modify the echo area buffer's contents while a
       redisplay of the buffer is going on, and seriously confuse
       redisplay.  */
 -  count = inhibit_garbage_collection ();
 +  ptrdiff_t count = inhibit_garbage_collection ();
  
    /* If there is no message, we must call display_echo_area_1
       nevertheless because it resizes the window.  But we will have to
     Value is non-zero if height of W was changed.  */
  
  static int
 -display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
 +display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
  {
    intptr_t i1 = a1;
    struct window *w = (struct window *) i1;
@@@ -9960,7 -10004,7 +10010,7 @@@ resize_echo_area_exactly (void
     resize_mini_window returns.  */
  
  static int
 -resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
 +resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
  {
    intptr_t i1 = a1;
    return resize_mini_window ((struct window *) i1, !NILP (exactly));
@@@ -10010,8 -10054,7 +10060,8 @@@ resize_mini_window (struct window *w, i
        struct it it;
        struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
        int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
 -      int height, max_height;
 +      int height;
 +      EMACS_INT max_height;
        int unit = FRAME_LINE_HEIGHT (f);
        struct text_pos start;
        struct buffer *old_current_buffer = NULL;
@@@ -10140,7 -10183,7 +10190,7 @@@ current_message (void
  
  
  static int
 -current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
 +current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
  {
    intptr_t i1 = a1;
    Lisp_Object *msg = (Lisp_Object *) i1;
@@@ -10219,7 -10262,7 +10269,7 @@@ check_message_stack (void
     time we display it---but don't redisplay it now.  */
  
  void
 -truncate_echo_area (EMACS_INT nchars)
 +truncate_echo_area (ptrdiff_t nchars)
  {
    if (nchars == 0)
      echo_area_buffer[0] = Qnil;
     message to at most NCHARS characters.  */
  
  static int
 -truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
 +truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
  {
    if (BEG + nchars < Z)
      del_range (BEG + nchars, Z);
  
  static void
  set_message (const char *s, Lisp_Object string,
 -           EMACS_INT nbytes, int multibyte_p)
 +           ptrdiff_t nbytes, int multibyte_p)
  {
    message_enable_multibyte
      = ((s && multibyte_p)
     current.  */
  
  static int
 -set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
 +set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
  {
    intptr_t i1 = a1;
    const char *s = (const char *) i1;
  
    if (STRINGP (string))
      {
 -      EMACS_INT nchars;
 +      ptrdiff_t nchars;
  
        if (nbytes == 0)
        nbytes = SBYTES (string);
        if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
        {
          /* Convert from multi-byte to single-byte.  */
 -        EMACS_INT i;
 +        ptrdiff_t i;
          int c, n;
          char work[1];
  
               && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
        {
          /* Convert from single-byte to multi-byte.  */
 -        EMACS_INT i;
 +        ptrdiff_t i;
          int c, n;
          unsigned char str[MAX_MULTIBYTE_LENGTH];
  
@@@ -10489,7 -10532,7 +10539,7 @@@ echo_area_display (int update_frame_p
              /* Must update other windows.  Likewise as in other
                 cases, don't let this update be interrupted by
                 pending input.  */
 -            int count = SPECPDL_INDEX ();
 +            ptrdiff_t count = SPECPDL_INDEX ();
              specbind (Qredisplay_dont_pause, Qt);
              windows_or_buffers_changed = 1;
              redisplay_internal ();
@@@ -10666,7 -10709,7 +10716,7 @@@ store_mode_line_noprop (const char *str
  {
    const unsigned char *str = (const unsigned char *) string;
    int n = 0;
 -  EMACS_INT dummy, nbytes;
 +  ptrdiff_t dummy, nbytes;
  
    /* Copy at most PRECISION chars from STR.  */
    nbytes = strlen (string);
@@@ -10711,7 -10754,7 +10761,7 @@@ x_consider_frame_title (Lisp_Object fra
        char *title;
        ptrdiff_t len;
        struct it it;
 -      int count = SPECPDL_INDEX ();
 +      ptrdiff_t count = SPECPDL_INDEX ();
  
        for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
        {
@@@ -10814,7 -10857,7 +10864,7 @@@ prepare_menu_bars (void
    if (all_windows)
      {
        Lisp_Object tail, frame;
 -      int count = SPECPDL_INDEX ();
 +      ptrdiff_t count = SPECPDL_INDEX ();
        /* 1 means that update_menu_bar has run its hooks
         so any further calls to update_menu_bar shouldn't do so again.  */
        int menu_bar_hooks_run = 0;
@@@ -10931,7 -10974,7 +10981,7 @@@ update_menu_bar (struct frame *f, int s
              != !NILP (w->region_showing)))
        {
          struct buffer *prev = current_buffer;
 -        int count = SPECPDL_INDEX ();
 +        ptrdiff_t count = SPECPDL_INDEX ();
  
          specbind (Qinhibit_menubar_update, Qt);
  
@@@ -11129,7 -11172,7 +11179,7 @@@ update_tool_bar (struct frame *f, int s
              != !NILP (w->region_showing)))
        {
          struct buffer *prev = current_buffer;
 -        int count = SPECPDL_INDEX ();
 +        ptrdiff_t count = SPECPDL_INDEX ();
          Lisp_Object frame, new_tool_bar;
            int new_n_tool_bar;
          struct gcpro gcpro1;
@@@ -11267,20 -11310,20 +11317,20 @@@ build_desired_tool_bar_string (struct f
                : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
        hmargin = vmargin = relief;
  
 -      if (INTEGERP (Vtool_bar_button_margin)
 -        && XINT (Vtool_bar_button_margin) > 0)
 +      if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
 +                         INT_MAX - max (hmargin, vmargin)))
        {
          hmargin += XFASTINT (Vtool_bar_button_margin);
          vmargin += XFASTINT (Vtool_bar_button_margin);
        }
        else if (CONSP (Vtool_bar_button_margin))
        {
 -        if (INTEGERP (XCAR (Vtool_bar_button_margin))
 -            && XINT (XCAR (Vtool_bar_button_margin)) > 0)
 +        if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
 +                             INT_MAX - hmargin))
            hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
  
 -        if (INTEGERP (XCDR (Vtool_bar_button_margin))
 -            && XINT (XCDR (Vtool_bar_button_margin)) > 0)
 +        if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
 +                             INT_MAX - vmargin))
            vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
        }
  
@@@ -11626,7 -11669,7 +11676,7 @@@ redisplay_tool_bar (struct frame *f
      {
        int border, rows, height, extra;
  
 -      if (INTEGERP (Vtool_bar_border))
 +      if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
        border = XINT (Vtool_bar_border);
        else if (EQ (Vtool_bar_border, Qinternal_border_width))
        border = FRAME_INTERNAL_BORDER_WIDTH (f);
@@@ -11814,7 -11857,7 +11864,7 @@@ get_tool_bar_item (struct frame *f, in
  
  void
  handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
 -                     unsigned int modifiers)
 +                     int modifiers)
  {
    Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
    struct window *w = XWINDOW (f->tool_bar_window);
@@@ -11993,7 -12036,7 +12043,7 @@@ hscroll_window_tree (Lisp_Object window
          hscroll_step_abs = 0;
        }
      }
 -  else if (INTEGERP (Vhscroll_step))
 +  else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
      {
        hscroll_step_abs = XINT (Vhscroll_step);
        if (hscroll_step_abs < 0)
                      && (w->cursor.x >= text_area_width - h_margin))))
            {
              struct it it;
 -            int hscroll;
 +            ptrdiff_t hscroll;
              struct buffer *saved_current_buffer;
 -            EMACS_INT pt;
 +            ptrdiff_t pt;
              int wanted_x;
  
              /* Find point in a display of infinite width.  */
@@@ -12151,12 -12194,12 +12201,12 @@@ static int debug_dvpos, debug_dy
  
  /* Delta in characters and bytes for try_window_id.  */
  
 -static EMACS_INT debug_delta, debug_delta_bytes;
 +static ptrdiff_t debug_delta, debug_delta_bytes;
  
  /* Values of window_end_pos and window_end_vpos at the end of
     try_window_id.  */
  
 -static EMACS_INT debug_end_vpos;
 +static ptrdiff_t debug_end_vpos;
  
  /* Append a string to W->desired_matrix->method.  FMT is a printf
     format string.  If trace_redisplay_p is non-zero also printf the
@@@ -12206,7 -12249,7 +12256,7 @@@ debug_method_add (struct window *w, cha
  
  static inline int
  text_outside_line_unchanged_p (struct window *w,
 -                             EMACS_INT start, EMACS_INT end)
 +                             ptrdiff_t start, ptrdiff_t end)
  {
    int unchanged_p = 1;
  
@@@ -12429,10 -12472,10 +12479,10 @@@ overlay_arrow_at_row (struct it *it, st
     position.  BUF and PT are the current point buffer and position.  */
  
  static int
 -check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
 -                          struct buffer *buf, EMACS_INT pt)
 +check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
 +                          struct buffer *buf, ptrdiff_t pt)
  {
 -  EMACS_INT start, end;
 +  ptrdiff_t start, end;
    Lisp_Object prop;
    Lisp_Object buffer;
  
@@@ -12483,7 -12526,7 +12533,7 @@@ reconsider_clip_changes (struct window 
    if (!b->clip_changed
        && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
      {
 -      EMACS_INT pt;
 +      ptrdiff_t pt;
  
        if (w == XWINDOW (selected_window))
        pt = PT;
@@@ -12552,7 -12595,7 +12602,7 @@@ redisplay_internal (void
    int must_finish = 0;
    struct text_pos tlbufpos, tlendpos;
    int number_of_visible_frames;
 -  int count, count1;
 +  ptrdiff_t count, count1;
    struct frame *sf;
    int polling_stopped_here = 0;
    Lisp_Object old_frame = selected_frame;
                {
                  struct glyph_row *row
                    = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
 -                EMACS_INT delta, delta_bytes;
 +                ptrdiff_t delta, delta_bytes;
  
                  /* We used to distinguish between two cases here,
                     conditioned by Z - CHARPOS (tlendpos) == ZV, for
@@@ -13518,18 -13561,18 +13568,18 @@@ redisplay_window_1 (Lisp_Object window
  static int
  set_cursor_from_row (struct window *w, struct glyph_row *row,
                     struct glyph_matrix *matrix,
 -                   EMACS_INT delta, EMACS_INT delta_bytes,
 +                   ptrdiff_t delta, ptrdiff_t delta_bytes,
                     int dy, int dvpos)
  {
    struct glyph *glyph = row->glyphs[TEXT_AREA];
    struct glyph *end = glyph + row->used[TEXT_AREA];
    struct glyph *cursor = NULL;
    /* The last known character position in row.  */
 -  EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
 +  ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
    int x = row->x;
 -  EMACS_INT pt_old = PT - delta;
 -  EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
 -  EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 +  ptrdiff_t pt_old = PT - delta;
 +  ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
 +  ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
    struct glyph *glyph_before = glyph - 1, *glyph_after = end;
    /* A glyph beyond the edge of TEXT_AREA which we should never
       touch.  */
    int string_seen = 0;
    /* Largest and smalles buffer positions seen so far during scan of
       glyph row.  */
 -  EMACS_INT bpos_max = pos_before;
 -  EMACS_INT bpos_min = pos_after;
 +  ptrdiff_t bpos_max = pos_before;
 +  ptrdiff_t bpos_min = pos_after;
    /* Last buffer position covered by an overlay string with an integer
       `cursor' property.  */
 -  EMACS_INT bpos_covered = 0;
 +  ptrdiff_t bpos_covered = 0;
    /* Non-zero means the display string on which to display the cursor
       comes from a text property, not from an overlay.  */
    int string_from_text_prop = 0;
        {
        if (BUFFERP (glyph->object))
          {
 -          EMACS_INT dpos = glyph->charpos - pt_old;
 +          ptrdiff_t dpos = glyph->charpos - pt_old;
  
            if (glyph->charpos > bpos_max)
              bpos_max = glyph->charpos;
        else if (STRINGP (glyph->object))
          {
            Lisp_Object chprop;
 -          EMACS_INT glyph_pos = glyph->charpos;
 +          ptrdiff_t glyph_pos = glyph->charpos;
  
            chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
                                         glyph->object);
        {
        if (BUFFERP (glyph->object))
          {
 -          EMACS_INT dpos = glyph->charpos - pt_old;
 +          ptrdiff_t dpos = glyph->charpos - pt_old;
  
            if (glyph->charpos > bpos_max)
              bpos_max = glyph->charpos;
        else if (STRINGP (glyph->object))
          {
            Lisp_Object chprop;
 -          EMACS_INT glyph_pos = glyph->charpos;
 +          ptrdiff_t glyph_pos = glyph->charpos;
  
            chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
                                         glyph->object);
  
        if (row->ends_in_ellipsis_p && pos_after == last_pos)
        {
 -        EMACS_INT ellipsis_pos;
 +        ptrdiff_t ellipsis_pos;
  
          /* Scan back over the ellipsis glyphs.  */
          if (!row->reversed_p)
             positioned between POS_BEFORE and POS_AFTER in the
             buffer.  */
          struct glyph *start, *stop;
 -        EMACS_INT pos = pos_before;
 +        ptrdiff_t pos = pos_before;
  
          x = -1;
  
              if (STRINGP (glyph->object))
                {
                  Lisp_Object str;
 -                EMACS_INT tem;
 +                ptrdiff_t tem;
                  /* If the display property covers the newline, we
                     need to search for it one position farther.  */
 -                EMACS_INT lim = pos_after
 +                ptrdiff_t lim = pos_after
                    + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
  
                  string_from_text_prop = 0;
                             be a character in the string with the
                             `cursor' property, which means display
                             cursor on that character's glyph.  */
 -                        EMACS_INT strpos = glyph->charpos;
 +                        ptrdiff_t strpos = glyph->charpos;
  
                          if (tem)
                            {
                               glyph += incr)
                            {
                              Lisp_Object cprop;
 -                            EMACS_INT gpos = glyph->charpos;
 +                            ptrdiff_t gpos = glyph->charpos;
  
                              cprop = Fget_char_property (make_number (gpos),
                                                          Qcursor,
  
  static int
  try_scrolling (Lisp_Object window, int just_this_one_p,
 -             EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
 +             ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
               int temp_scroll_step, int last_line_misfit)
  {
    struct window *w = XWINDOW (window);
@@@ -14826,7 -14869,7 +14876,7 @@@ try_cursor_movement (Lisp_Object window
                        MATRIX_ROW (w->current_matrix, w->cursor.vpos);
                      struct glyph *g =
                        candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
 -                    EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
 +                    ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
  
                      exact_match_p =
                        (BUFFERP (g->object) && g->charpos == PT)
@@@ -14886,7 -14929,7 +14936,7 @@@ stati
  void
  set_vertical_scroll_bar (struct window *w)
  {
 -  EMACS_INT start, end, whole;
 +  ptrdiff_t start, end, whole;
  
    /* Calculate the start and end positions for the current window.
       At some point, it would be nice to choose between scrollbars
@@@ -14946,11 -14989,11 +14996,11 @@@ redisplay_window (Lisp_Object window, i
       It indictes that the buffer contents and narrowing are unchanged.  */
    int buffer_unchanged_p = 0;
    int temp_scroll_step = 0;
 -  int count = SPECPDL_INDEX ();
 +  ptrdiff_t count = SPECPDL_INDEX ();
    int rc;
    int centering_position = -1;
    int last_line_misfit = 0;
 -  EMACS_INT beg_unchanged, end_unchanged;
 +  ptrdiff_t beg_unchanged, end_unchanged;
  
    SET_TEXT_POS (lpoint, PT, PT_BYTE);
    opoint = lpoint;
       window, set up appropriate value.  */
    if (!EQ (window, selected_window))
      {
 -      EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
 -      EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
 +      ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
 +      ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
        if (new_pt < BEGV)
        {
          new_pt = BEGV;
        scroll_margin > 0
        ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
        : 0;
 -      EMACS_INT margin_pos = CHARPOS (startp);
 +      ptrdiff_t margin_pos = CHARPOS (startp);
        int scrolling_up;
        Lisp_Object aggressive;
  
@@@ -16332,7 -16375,7 +16382,7 @@@ try_window_reusing_current_matrix (stru
  
  static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
  static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
 -                                                          EMACS_INT *, EMACS_INT *);
 +                                                          ptrdiff_t *, ptrdiff_t *);
  static struct glyph_row *
  find_last_row_displaying_text (struct glyph_matrix *, struct it *,
                                 struct glyph_row *);
@@@ -16380,7 -16423,7 +16430,7 @@@ find_last_row_displaying_text (struct g
  static struct glyph_row *
  find_last_unchanged_at_beg_row (struct window *w)
  {
 -  EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
 +  ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
    struct glyph_row *row;
    struct glyph_row *row_found = NULL;
    int yb = window_text_bottom_y (w);
  
  static struct glyph_row *
  find_first_unchanged_at_end_row (struct window *w,
 -                               EMACS_INT *delta, EMACS_INT *delta_bytes)
 +                               ptrdiff_t *delta, ptrdiff_t *delta_bytes)
  {
    struct glyph_row *row;
    struct glyph_row *row_found = NULL;
         corresponds to window_end_pos.  This allows us to translate
         buffer positions in the current matrix to current buffer
         positions for characters not in changed text.  */
 -      EMACS_INT Z_old =
 +      ptrdiff_t Z_old =
        MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
 -      EMACS_INT Z_BYTE_old =
 +      ptrdiff_t Z_BYTE_old =
        MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 -      EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
 +      ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
        struct glyph_row *first_text_row
        = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
  
@@@ -16547,12 -16590,12 +16597,12 @@@ sync_frame_with_window_matrix_rows (str
     containing CHARPOS or null.  */
  
  struct glyph_row *
 -row_containing_pos (struct window *w, EMACS_INT charpos,
 +row_containing_pos (struct window *w, ptrdiff_t charpos,
                    struct glyph_row *start, struct glyph_row *end, int dy)
  {
    struct glyph_row *row = start;
    struct glyph_row *best_row = NULL;
 -  EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
 +  ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
    int last_y;
  
    /* If we happen to start on a header-line, skip that.  */
@@@ -16666,14 -16709,14 +16716,14 @@@ try_window_id (struct window *w
    struct glyph_row *bottom_row;
    int bottom_vpos;
    struct it it;
 -  EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
 +  ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
    int dvpos, dy;
    struct text_pos start_pos;
    struct run run;
    int first_unchanged_at_end_vpos = 0;
    struct glyph_row *last_text_row, *last_text_row_at_end;
    struct text_pos start;
 -  EMACS_INT first_changed_charpos, last_changed_charpos;
 +  ptrdiff_t first_changed_charpos, last_changed_charpos;
  
  #if GLYPH_DEBUG
    if (inhibit_try_window_id)
          || (last_changed_charpos < CHARPOS (start) - 1
              && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
      {
 -      EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
 +      ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
        struct glyph_row *r0;
  
        /* Compute how many chars/bytes have been added to or removed
@@@ -17544,7 -17587,7 +17594,7 @@@ dump_glyph_row (struct glyph_row *row, 
               row->visible_height,
               row->ascent,
               row->phys_ascent);
 -      fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
 +      fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
               row->end.overlay_string_index,
               row->continuation_lines_width);
        fprintf (stderr, "%9"pI"d %5"pI"d\n",
@@@ -17618,7 -17661,7 +17668,7 @@@ glyphs in short form, otherwise show gl
           w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
    fprintf (stderr, "=============================================\n");
    dump_glyph_matrix (w->current_matrix,
 -                   NILP (glyphs) ? 0 : XINT (glyphs));
 +                   TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
    return Qnil;
  }
  
@@@ -17641,7 -17684,7 +17691,7 @@@ GLYPH > 1 or omitted means dump glyphs 
    (Lisp_Object row, Lisp_Object glyphs)
  {
    struct glyph_matrix *matrix;
 -  int vpos;
 +  EMACS_INT vpos;
  
    CHECK_NUMBER (row);
    matrix = XWINDOW (selected_window)->current_matrix;
    if (vpos >= 0 && vpos < matrix->nrows)
      dump_glyph_row (MATRIX_ROW (matrix, vpos),
                    vpos,
 -                  INTEGERP (glyphs) ? XINT (glyphs) : 2);
 +                  TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
    return Qnil;
  }
  
@@@ -17663,13 -17706,13 +17713,13 @@@ GLYPH > 1 or omitted means dump glyphs 
  {
    struct frame *sf = SELECTED_FRAME ();
    struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
 -  int vpos;
 +  EMACS_INT vpos;
  
    CHECK_NUMBER (row);
    vpos = XINT (row);
    if (vpos >= 0 && vpos < m->nrows)
      dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 -                  INTEGERP (glyphs) ? XINT (glyphs) : 2);
 +                  TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
    return Qnil;
  }
  
@@@ -18182,9 -18225,9 +18232,9 @@@ extend_face_to_end_of_line (struct it *
     trailing whitespace.  */
  
  static int
 -trailing_whitespace_p (EMACS_INT charpos)
 +trailing_whitespace_p (ptrdiff_t charpos)
  {
 -  EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
 +  ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
    int c = 0;
  
    while (bytepos < ZV_BYTE
@@@ -18500,8 -18543,8 +18550,8 @@@ unproduce_glyphs (struct it *it, int n
     and ROW->maxpos.  */
  static void
  find_row_edges (struct it *it, struct glyph_row *row,
 -              EMACS_INT min_pos, EMACS_INT min_bpos,
 -              EMACS_INT max_pos, EMACS_INT max_bpos)
 +              ptrdiff_t min_pos, ptrdiff_t min_bpos,
 +              ptrdiff_t max_pos, ptrdiff_t max_bpos)
  {
    /* FIXME: Revisit this when glyph ``spilling'' in continuation
       lines' rows is implemented for bidi-reordered rows.  */
@@@ -18663,11 -18706,11 +18713,11 @@@ display_line (struct it *it
    int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
    int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
    int wrap_row_extra_line_spacing IF_LINT (= 0);
 -  EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
 -  EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
 +  ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
 +  ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
    int cvpos;
 -  EMACS_INT min_pos = ZV + 1, max_pos = 0;
 -  EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
 +  ptrdiff_t min_pos = ZV + 1, max_pos = 0;
 +  ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
  
    /* We always start displaying at hpos zero even if hscrolled.  */
    xassert (it->hpos == 0 && it->current_x == 0);
    do                                                          \
      {                                                         \
        int composition_p = (IT)->what == IT_COMPOSITION;               \
 -      EMACS_INT current_pos =                                 \
 +      ptrdiff_t current_pos =                                 \
        composition_p ? (IT)->cmp_it.charpos                    \
                      : IT_CHARPOS (*(IT));                     \
 -      EMACS_INT current_bpos =                                        \
 +      ptrdiff_t current_bpos =                                        \
        composition_p ? CHAR_TO_BYTE (current_pos)              \
                      : IT_BYTEPOS (*(IT));                     \
        if (current_pos < min_pos)                              \
@@@ -19377,8 -19420,8 +19427,8 @@@ See also `bidi-paragraph-direction'.  *
         use current_matrix if it is up to date, but this seems fast
         enough as it is.  */
        struct bidi_it itb;
 -      EMACS_INT pos = BUF_PT (buf);
 -      EMACS_INT bytepos = BUF_PT_BYTE (buf);
 +      ptrdiff_t pos = BUF_PT (buf);
 +      ptrdiff_t bytepos = BUF_PT_BYTE (buf);
        int c;
        void *itb_data = bidi_shelve_cache ();
  
@@@ -19665,7 -19708,7 +19715,7 @@@ display_mode_line (struct window *w, en
  {
    struct it it;
    struct face *face;
 -  int count = SPECPDL_INDEX ();
 +  ptrdiff_t count = SPECPDL_INDEX ();
  
    init_iterator (&it, w, -1, -1, NULL, face_id);
    /* Don't extend on a previously drawn mode-line.
@@@ -19801,7 -19844,7 +19851,7 @@@ display_mode_element (struct it *it, in
        {
        /* A string: output it and check for %-constructs within it.  */
        unsigned char c;
 -      EMACS_INT offset = 0;
 +      ptrdiff_t offset = 0;
  
        if (SCHARS (elt) > 0
            && (!NILP (props) || risky))
               && (mode_line_target != MODE_LINE_DISPLAY
                   || it->current_x < it->last_visible_x))
          {
 -          EMACS_INT last_offset = offset;
 +          ptrdiff_t last_offset = offset;
  
            /* Advance to end of string or next format specifier.  */
            while ((c = SREF (elt, offset++)) != '\0' && c != '%')
  
            if (offset - 1 != last_offset)
              {
 -              EMACS_INT nchars, nbytes;
 +              ptrdiff_t nchars, nbytes;
  
                /* Output to end of string or up to '%'.  Field width
                   is length of string.  Don't output more than
                    break;
                  case MODE_LINE_STRING:
                    {
 -                    EMACS_INT bytepos = last_offset;
 -                    EMACS_INT charpos = string_byte_to_char (elt, bytepos);
 -                    EMACS_INT endpos = (precision <= 0
 +                    ptrdiff_t bytepos = last_offset;
 +                    ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 +                    ptrdiff_t endpos = (precision <= 0
                                          ? string_byte_to_char (elt, offset)
                                          : charpos + nchars);
  
                    break;
                  case MODE_LINE_DISPLAY:
                    {
 -                    EMACS_INT bytepos = last_offset;
 -                    EMACS_INT charpos = string_byte_to_char (elt, bytepos);
 +                    ptrdiff_t bytepos = last_offset;
 +                    ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
  
                      if (precision <= 0)
                        nchars = string_byte_to_char (elt, offset) - charpos;
              }
            else /* c == '%' */
              {
 -              EMACS_INT percent_position = offset;
 +              ptrdiff_t percent_position = offset;
  
                /* Get the specified minimum width.  Zero means
                   don't pad.  */
                else if (c != 0)
                  {
                    int multibyte;
 -                  EMACS_INT bytepos, charpos;
 +                  ptrdiff_t bytepos, charpos;
                    const char *spec;
                    Lisp_Object string;
  
@@@ -20248,7 -20291,7 +20298,7 @@@ static in
  store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
                        int field_width, int precision, Lisp_Object props)
  {
 -  EMACS_INT len;
 +  ptrdiff_t len;
    int n = 0;
  
    if (string != NULL)
@@@ -20349,7 -20392,7 +20399,7 @@@ are the selected window and the WINDOW'
    struct buffer *old_buffer = NULL;
    int face_id;
    int no_props = INTEGERP (face);
 -  int count = SPECPDL_INDEX ();
 +  ptrdiff_t count = SPECPDL_INDEX ();
    Lisp_Object str;
    int string_start = 0;
  
     the positive integer D to BUF using a minimal field width WIDTH.  */
  
  static void
 -pint2str (register char *buf, register int width, register EMACS_INT d)
 +pint2str (register char *buf, register int width, register ptrdiff_t d)
  {
    register char *p = buf;
  
@@@ -20479,11 -20522,11 +20529,11 @@@ static const char power_letter[] 
    };
  
  static void
 -pint2hrstr (char *buf, int width, EMACS_INT d)
 +pint2hrstr (char *buf, int width, ptrdiff_t d)
  {
    /* We aim to represent the nonnegative integer D as
       QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
 -  EMACS_INT quotient = d;
 +  ptrdiff_t quotient = d;
    int remainder = 0;
    /* -1 means: do not use TENTHS. */
    int tenths = -1;
@@@ -20763,7 -20806,7 +20813,7 @@@ decode_mode_spec (struct window *w, reg
        return "";
        else
        {
 -        EMACS_INT col = current_column ();
 +        ptrdiff_t col = current_column ();
          w->column_number_displayed = make_number (col);
          pint2str (decode_mode_spec_buf, field_width, col);
          return decode_mode_spec_buf;
  
      case 'i':
        {
 -      EMACS_INT size = ZV - BEGV;
 +      ptrdiff_t size = ZV - BEGV;
        pint2str (decode_mode_spec_buf, field_width, size);
        return decode_mode_spec_buf;
        }
  
      case 'I':
        {
 -      EMACS_INT size = ZV - BEGV;
 +      ptrdiff_t size = ZV - BEGV;
        pint2hrstr (decode_mode_spec_buf, field_width, size);
        return decode_mode_spec_buf;
        }
  
      case 'l':
        {
 -      EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
 -      EMACS_INT topline, nlines, height;
 -      EMACS_INT junk;
 +      ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
 +      ptrdiff_t topline, nlines, height;
 +      ptrdiff_t junk;
  
        /* %c and %l are ignored in `frame-title-format'.  */
        if (mode_line_target == MODE_LINE_TITLE)
        else if (nlines < height + 25 || nlines > height * 3 + 50
                 || linepos == BUF_BEGV (b))
          {
 -          EMACS_INT limit = BUF_BEGV (b);
 -          EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
 -          EMACS_INT position;
 -          EMACS_INT distance =
 +          ptrdiff_t limit = BUF_BEGV (b);
 +          ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
 +          ptrdiff_t position;
 +          ptrdiff_t distance =
              (height * 2 + 30) * line_number_display_limit_width;
  
            if (startpos - distance > limit)
  
      case 'p':
        {
 -      EMACS_INT pos = marker_position (w->start);
 -      EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
 +      ptrdiff_t pos = marker_position (w->start);
 +      ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
  
        if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
          {
               so get us a 2-digit number that is close.  */
            if (total == 100)
              total = 99;
 -          sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
 +          sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
            return decode_mode_spec_buf;
          }
        }
        /* Display percentage of size above the bottom of the screen.  */
      case 'P':
        {
 -      EMACS_INT toppos = marker_position (w->start);
 -      EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
 -      EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
 +      ptrdiff_t toppos = marker_position (w->start);
 +      ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
 +      ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
  
        if (botpos >= BUF_ZV (b))
          {
            if (total == 100)
              total = 99;
            if (toppos <= BUF_BEGV (b))
 -            sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
 +            sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
            else
 -            sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
 +            sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
            return decode_mode_spec_buf;
          }
        }
  
      case '@':
        {
 -      int count = inhibit_garbage_collection ();
 +      ptrdiff_t count = inhibit_garbage_collection ();
        Lisp_Object val = call1 (intern ("file-remote-p"),
                                 BVAR (current_buffer, directory));
        unbind_to (count, Qnil);
  
     Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT.  */
  
 -static EMACS_INT
 -display_count_lines (EMACS_INT start_byte,
 -                   EMACS_INT limit_byte, EMACS_INT count,
 -                   EMACS_INT *byte_pos_ptr)
 +static ptrdiff_t
 +display_count_lines (ptrdiff_t start_byte,
 +                   ptrdiff_t limit_byte, ptrdiff_t count,
 +                   ptrdiff_t *byte_pos_ptr)
  {
    register unsigned char *cursor;
    unsigned char *base;
  
 -  register EMACS_INT ceiling;
 +  register ptrdiff_t ceiling;
    register unsigned char *ceiling_addr;
 -  EMACS_INT orig_count = count;
 +  ptrdiff_t orig_count = count;
  
    /* If we are not in selective display mode,
       check only for newlines.  */
  
  static int
  display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
 -              EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
 +              ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
                int field_width, int precision, int max_x, int multibyte)
  {
    int hpos_at_start = it->hpos;
    int saved_face_id = it->face_id;
    struct glyph_row *row = it->glyph_row;
 -  EMACS_INT it_charpos;
 +  ptrdiff_t it_charpos;
  
    /* Initialize the iterator IT for iteration over STRING beginning
       with index START.  */
       FACE_STRING, if that's given.  */
    if (STRINGP (face_string))
      {
 -      EMACS_INT endptr;
 +      ptrdiff_t endptr;
        struct face *face;
  
        it->face_id
@@@ -22725,7 -22768,7 +22775,7 @@@ compute_overhangs_and_x (struct glyph_s
  
  static int
  draw_glyphs (struct window *w, int x, struct glyph_row *row,
 -           enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
 +           enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
             enum draw_glyphs_face hl, int overlaps)
  {
    struct glyph_string *head, *tail;
@@@ -24189,7 -24232,7 +24239,7 @@@ x_produce_glyphs (struct it *it
          XChar2b char2b;
          struct font_metrics *pcm;
          int font_not_found_p;
 -        EMACS_INT pos;
 +        ptrdiff_t pos;
  
          for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
            if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
@@@ -24589,7 -24632,7 +24639,7 @@@ x_insert_glyphs (struct glyph *start, i
    struct glyph_row *row;
    struct glyph *glyph;
    int frame_x, frame_y;
 -  EMACS_INT hpos;
 +  ptrdiff_t hpos;
  
    xassert (updated_window && updated_row);
    BLOCK_INPUT;
@@@ -24736,7 -24779,8 +24786,7 @@@ get_specified_cursor_type (Lisp_Object 
  
    if (CONSP (arg)
        && EQ (XCAR (arg), Qbar)
 -      && INTEGERP (XCDR (arg))
 -      && XINT (XCDR (arg)) >= 0)
 +      && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
      {
        *width = XINT (XCDR (arg));
        return BAR_CURSOR;
  
    if (CONSP (arg)
        && EQ (XCAR (arg), Qhbar)
 -      && INTEGERP (XCDR (arg))
 -      && XINT (XCDR (arg)) >= 0)
 +      && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
      {
        *width = XINT (XCDR (arg));
        return HBAR_CURSOR;
@@@ -25590,7 -25635,7 +25640,7 @@@ cursor_in_mouse_face_p (struct window *
     change non-linearly with glyph rows.  */
  static void
  rows_from_pos_range (struct window *w,
 -                   EMACS_INT start_charpos, EMACS_INT end_charpos,
 +                   ptrdiff_t start_charpos, ptrdiff_t end_charpos,
                     struct glyph_row **start, struct glyph_row **end)
  {
    struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
  static void
  mouse_face_from_buffer_pos (Lisp_Object window,
                            Mouse_HLInfo *hlinfo,
 -                          EMACS_INT mouse_charpos,
 -                          EMACS_INT start_charpos,
 -                          EMACS_INT end_charpos,
 +                          ptrdiff_t mouse_charpos,
 +                          ptrdiff_t start_charpos,
 +                          ptrdiff_t end_charpos,
                            Lisp_Object before_string,
                            Lisp_Object after_string,
                            Lisp_Object cover_string)
    struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
    struct glyph_row *r1, *r2;
    struct glyph *glyph, *end;
 -  EMACS_INT ignore, pos;
 +  ptrdiff_t ignore, pos;
    int x;
  
    xassert (NILP (cover_string) || STRINGP (cover_string));
     Value is non-zero if a glyph was found.  */
  
  static int
 -fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
 +fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
                      int *hpos, int *vpos, int *x, int *y, int right_p)
  {
    int yb = window_text_bottom_y (w);
  static void
  mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
                            Lisp_Object object,
 -                          EMACS_INT startpos, EMACS_INT endpos)
 +                          ptrdiff_t startpos, ptrdiff_t endpos)
  {
    int yb = window_text_bottom_y (w);
    struct glyph_row *r;
@@@ -26291,8 -26336,8 +26341,8 @@@ on_hot_spot_p (Lisp_Object hot_spot, in
        {
          struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
          Lisp_Object *poly = v->contents;
 -        int n = v->header.size;
 -        int i;
 +        ptrdiff_t n = v->header.size;
 +        ptrdiff_t i;
          int inside = 0;
          Lisp_Object lx, ly;
          int x0, y0;
@@@ -26370,9 -26415,7 +26420,9 @@@ Returns the alist element for the firs
    CHECK_NUMBER (x);
    CHECK_NUMBER (y);
  
 -  return find_hot_spot (map, XINT (x), XINT (y));
 +  return find_hot_spot (map,
 +                      clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
 +                      clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
  }
  
  
@@@ -26431,7 -26474,7 +26481,7 @@@ note_mode_line_or_margin_highlight (Lis
    Cursor cursor = No_Cursor;
    Lisp_Object pointer = Qnil;
    int dx, dy, width, height;
 -  EMACS_INT charpos;
 +  ptrdiff_t charpos;
    Lisp_Object string, object = Qnil;
    Lisp_Object pos, help;
  
          int gpos;
          int gseq_length;
          int total_pixel_width;
 -        EMACS_INT begpos, endpos, ignore;
 +        ptrdiff_t begpos, endpos, ignore;
  
          int vpos, hpos;
  
@@@ -26798,14 -26841,14 +26848,14 @@@ note_mouse_highlight (struct frame *f, 
        && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
      {
        int hpos, vpos, dx, dy, area;
 -      EMACS_INT pos;
 +      ptrdiff_t pos;
        struct glyph *glyph;
        Lisp_Object object;
        Lisp_Object mouse_face = Qnil, position;
        Lisp_Object *overlay_vec = NULL;
        ptrdiff_t i, noverlays;
        struct buffer *obuf;
 -      EMACS_INT obegv, ozv;
 +      ptrdiff_t obegv, ozv;
        int same_region;
  
        /* Find the glyph under X/Y.  */
              /* The mouse-highlighting comes from a display string
                 with a mouse-face.  */
              Lisp_Object s, e;
 -            EMACS_INT ignore;
 +            ptrdiff_t ignore;
  
              s = Fprevious_single_property_change
                (make_number (pos + 1), Qmouse_face, object, Qnil);
                  /* If we are on a display string with no mouse-face,
                     check if the text under it has one.  */
                  struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
 -                EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
 +                ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
                  pos = string_buffer_position (object, start);
                  if (pos > 0)
                    {
        else
          {
            Lisp_Object obj = glyph->object;
 -          EMACS_INT charpos = glyph->charpos;
 +          ptrdiff_t charpos = glyph->charpos;
  
            /* Try text properties.  */
            if (STRINGP (obj)
                       see if the buffer text ``under'' it does.  */
                    struct glyph_row *r
                      = MATRIX_ROW (w->current_matrix, vpos);
 -                  EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
 -                  EMACS_INT p = string_buffer_position (obj, start);
 +                  ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 +                  ptrdiff_t p = string_buffer_position (obj, start);
                    if (p > 0)
                      {
                        help = Fget_char_property (make_number (p),
          if (NILP (pointer))
            {
              Lisp_Object obj = glyph->object;
 -            EMACS_INT charpos = glyph->charpos;
 +            ptrdiff_t charpos = glyph->charpos;
  
              /* Try text properties.  */
              if (STRINGP (obj)
                         see if the buffer text ``under'' it does.  */
                      struct glyph_row *r
                        = MATRIX_ROW (w->current_matrix, vpos);
 -                    EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
 -                    EMACS_INT p = string_buffer_position (obj, start);
 +                    ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 +                    ptrdiff_t p = string_buffer_position (obj, start);
                      if (p > 0)
                        pointer = Fget_char_property (make_number (p),
                                                      Qpointer, w->buffer);
@@@ -28449,16 -28492,23 +28499,16 @@@ start_hourglass (void
  {
  #if defined (HAVE_WINDOW_SYSTEM)
    EMACS_TIME delay;
 -  int secs, usecs = 0;
 +  int secs = DEFAULT_HOURGLASS_DELAY, usecs = 0;
  
    cancel_hourglass ();
  
 -  if (INTEGERP (Vhourglass_delay)
 -      && XINT (Vhourglass_delay) > 0)
 -    secs = XFASTINT (Vhourglass_delay);
 -  else if (FLOATP (Vhourglass_delay)
 -         && XFLOAT_DATA (Vhourglass_delay) > 0)
 +  if (NUMBERP (Vhourglass_delay))
      {
 -      Lisp_Object tem;
 -      tem = Ftruncate (Vhourglass_delay, Qnil);
 -      secs = XFASTINT (tem);
 -      usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
 +      double duration = extract_float (Vhourglass_delay);
 +      if (0 < duration)
 +      duration_to_sec_usec (duration, &secs, &usecs);
      }
 -  else
 -    secs = DEFAULT_HOURGLASS_DELAY;
  
    EMACS_SET_SECS_USECS (delay, secs, usecs);
    hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,