- int speccount = specpdl_depth ();
- record_unwind_protect (change_function_restore, Fcurrent_buffer ());
- set_buffer_internal (buf);
- inside_change_hook = 1;
- va_run_hook_with_args (Qbefore_change_functions, 2,
- make_int (start), make_int (end));
- /* Obsolete, for compatibility */
- va_run_hook_with_args (Qbefore_change_function, 2,
- make_int (start), make_int (end));
- unbind_to (speccount, Qnil);
+ XSETBUFFER (buffer, mbuf);
+ if (!NILP (symbol_value_in_buffer (Qbefore_change_functions, buffer))
+ /* Obsolete, for compatibility */
+ || !NILP (symbol_value_in_buffer (Qbefore_change_function, buffer)))
+ {
+ set_buffer_internal (buf);
+ va_run_hook_with_args (Qbefore_change_functions, 2,
+ make_int (start), make_int (end));
+ /* Obsolete, for compatibility */
+ va_run_hook_with_args (Qbefore_change_function, 2,
+ make_int (start), make_int (end));
+ }
+ }
+
+ /* Make sure endpoints remain valid. before-change-functions
+ might have modified the buffer. */
+ if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
+ if (start > BUF_ZV (buf)) start = BUF_ZV (buf);
+ if (end < BUF_BEGV (buf)) end = BUF_BEGV (buf);
+ if (end > BUF_ZV (buf)) end = BUF_ZV (buf);
+
+ MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ {
+ XSETBUFFER (buffer, mbuf);
+ report_extent_modification (buffer, start, end, 0);