Function: set-buffer-major-mode

set-buffer-major-mode is a function defined in buffer.c.

Signature

(set-buffer-major-mode BUFFER)

Documentation

Set an appropriate major mode for BUFFER.

For the *scratch* buffer, use initial-major-mode, otherwise choose a mode according to the default value of major-mode. Use this function before selecting the buffer, since it may need to inspect the current buffer's major mode.

Probably introduced at or before Emacs version 19.29.

Source Code

// Defined in /usr/src/emacs/src/buffer.c
{
  ptrdiff_t count;
  Lisp_Object function;

  CHECK_BUFFER (buffer);

  if (!BUFFER_LIVE_P (XBUFFER (buffer)))
    error ("Attempt to set major mode for a dead buffer");

  if (strcmp (SSDATA (BVAR (XBUFFER (buffer), name)), "*scratch*") == 0)
    function = find_symbol_value (intern ("initial-major-mode"));
  else
    {
      function = BVAR (&buffer_defaults, major_mode);
      if (NILP (function)
	  && NILP (Fget (BVAR (current_buffer, major_mode), Qmode_class)))
	function = BVAR (current_buffer, major_mode);
    }

  if (NILP (function)) /* If function is `fundamental-mode', allow it to run
                          so that `run-mode-hooks' and thus
                          `hack-local-variables' get run. */
    return Qnil;

  count = SPECPDL_INDEX ();

  /* To select a nonfundamental mode,
     select the buffer temporarily and then call the mode function.  */

  record_unwind_current_buffer ();

  Fset_buffer (buffer);
  call0 (function);

  return unbind_to (count, Qnil);
}