Function: current-active-maps

current-active-maps is a function defined in keymap.c.

Signature

(current-active-maps &optional OLP POSITION)

Documentation

Return a list of the currently active keymaps.

OLP if non-nil indicates that we should obey overriding-local-map and overriding-terminal-local-map. POSITION can specify a click position like in the respective argument of key-binding or a live window which means to return the active maps for that window's buffer.

View in manual

Probably introduced at or before Emacs version 22.1.

Source Code

// Defined in /usr/src/emacs/src/keymap.c
{
  specpdl_ref count = SPECPDL_INDEX ();

  Lisp_Object keymaps = list1 (current_global_map);

  /* If a mouse click position is given, our variables are based on
     the buffer clicked on, not the current buffer.  So we may have to
     switch the buffer here.  */

  if (CONSP (position))
    {
      Lisp_Object window = POSN_WINDOW (position);

      if (WINDOWP (window)
	  && BUFFERP (XWINDOW (window)->contents)
	  && XBUFFER (XWINDOW (window)->contents) != current_buffer)
	{
	  /* Arrange to go back to the original buffer once we're done
	     processing the key sequence.  We don't use
	     save_excursion_{save,restore} here, in analogy to
	     `read-key-sequence' to avoid saving point.  Maybe this
	     would not be a problem here, but it is easier to keep
	     things the same.
	  */
	  record_unwind_current_buffer ();
	  set_buffer_internal (XBUFFER (XWINDOW (window)->contents));
	}
    }
  else if (WINDOW_LIVE_P (position))
    {
      if (BUFFERP (XWINDOW (position)->contents)
	  && XBUFFER (XWINDOW (position)->contents) != current_buffer)
	{
	  /* See comment above.  */
	  record_unwind_current_buffer ();
	  set_buffer_internal (XBUFFER (XWINDOW (position)->contents));
	}
    }

  if (!NILP (olp)
      /* The doc said that overriding-terminal-local-map should
	 override overriding-local-map.  The code used them both,
	 but it seems clearer to use just one.  rms, jan 2005.  */
      && NILP (KVAR (current_kboard, Voverriding_terminal_local_map))
      && !NILP (Voverriding_local_map))
    keymaps = Fcons (Voverriding_local_map, keymaps);

  if (NILP (XCDR (keymaps)))
    {
      Lisp_Object *maps;
      int nmaps;
      ptrdiff_t pt = click_position (position);
      /* This usually returns the buffer's local map,
	 but that can be overridden by a `local-map' property.  */
      Lisp_Object local_map = get_local_map (pt, current_buffer, Qlocal_map);
      /* This returns nil unless there is a `keymap' property.  */
      Lisp_Object keymap = get_local_map (pt, current_buffer, Qkeymap);
      Lisp_Object otlp = KVAR (current_kboard, Voverriding_terminal_local_map);

      if (CONSP (position))
	{
	  Lisp_Object string = POSN_STRING (position);

	  /* For a mouse click, get the local text-property keymap
	     of the place clicked on, rather than point.  */

	  local_map = Qnil;
	  keymap = Qnil;

	  /* If on a mode line string with a local keymap, or for a
	     click on a string, i.e. overlay string or a string
	     displayed via the `display' property, first consider the
	     `local-map' and `keymap' properties of that string.  */

	  if (CONSP (string) && STRINGP (XCAR (string)))
	    {
	      Lisp_Object pos = XCDR (string);
	      string = XCAR (string);
	      if (FIXNUMP (pos)
		  && XFIXNUM (pos) >= 0
		  && XFIXNUM (pos) < SCHARS (string))
		{
		  local_map = Fget_text_property (pos, Qlocal_map, string);
		  keymap = Fget_text_property (pos, Qkeymap, string);
		}
	    }

	  Lisp_Object buffer_posn = POSN_BUFFER_POSN (position);
	  /* Then, if the click was in the buffer, get the local
	     text-property keymap of the place clicked on.  */

	  if (FIXNUMP (buffer_posn)
	      && XFIXNUM (buffer_posn) >= BEG && XFIXNUM (buffer_posn) <= Z)
	    {
	      /* The properties in POSN_STRING take precedence, if set. */
	      if (NILP (local_map))
		local_map = get_local_map (XFIXNUM (buffer_posn),
					   current_buffer, Qlocal_map);

	      if (NILP (keymap))
		keymap = get_local_map (XFIXNUM (buffer_posn),
					current_buffer, Qkeymap);
	    }

	  /* Finally, fall back on the buffer's local map. */
	  if (NILP (local_map))
	    local_map = BVAR (current_buffer, keymap);
	}

      if (!NILP (local_map))
	keymaps = Fcons (local_map, keymaps);

      /* Now put all the minor mode keymaps on the list.  */
      nmaps = current_minor_maps (0, &maps);

      for (int i = --nmaps; i >= 0; i--)
	if (!NILP (maps[i]))
	  keymaps = Fcons (maps[i], keymaps);

      if (!NILP (keymap))
	keymaps = Fcons (keymap, keymaps);

      if (!NILP (olp) && !NILP (otlp))
	keymaps = Fcons (otlp, keymaps);
    }

  return unbind_to (count, keymaps);
}