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.

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));
	}
    }

  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.  */

	  if (POSN_INBUFFER_P (position))
	    {
	      Lisp_Object pos = POSN_BUFFER_POSN (position);
	      if (FIXNUMP (pos)
		  && XFIXNUM (pos) >= BEG && XFIXNUM (pos) <= Z)
		{
		  local_map = get_local_map (XFIXNUM (pos),
					     current_buffer, Qlocal_map);

		  keymap = get_local_map (XFIXNUM (pos),
					  current_buffer, Qkeymap);
		}
	    }

	  /* 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,
	     consider `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))
		{
		  Lisp_Object map = Fget_text_property (pos, Qlocal_map, string);
		  if (!NILP (map))
		    local_map = map;

		  map = Fget_text_property (pos, Qkeymap, string);
		  if (!NILP (map))
		    keymap = map;
		}
	    }

	}

      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);
}