home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / macros.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-10  |  9.3 KB  |  339 lines

  1. /* Keyboard macros.
  2.    Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* A keyboard macro is a string of ASCII characters, or a vector of event
  21.    objects.  Only key-press, mouse-press, mouse-release, and menu-selection
  22.    events ever get into a keyboard macro.
  23.  
  24.    When interactively defining a keyboard macro, it will always be a vector
  25.    of events; strings may be executed for backwards compatibility.
  26.  */
  27.  
  28. #include "config.h"
  29. #include "lisp.h"
  30. #include "events.h"
  31. #include "macros.h"
  32. #include "commands.h"
  33. #include "buffer.h"
  34. #include "window.h"
  35.  
  36. Lisp_Object Qexecute_kbd_macro;
  37.  
  38. int defining_kbd_macro;
  39.  
  40. /* This is a lisp vector, which contains the events of the keyboard macro
  41.    currently being read.  It is reallocated when the macro gets too large.
  42.  */
  43. static Lisp_Object kbd_macro_builder;
  44. int kbd_macro_end;  /* index into the above vector where new events go */
  45. int kbd_macro_ptr;  /* index into the above vector which is for real */
  46.  
  47. int pre_command_kbd_macro_end;    /* used for backing up kbd_macro_end */
  48.  
  49.  
  50. Lisp_Object Vlast_kbd_macro;
  51.  
  52. /* The current macro and our position in it.  When executing nested kbd
  53.    macros, previous values for these are wound through the execution stack
  54.    with unwind-protect.
  55.  */
  56. Lisp_Object Vexecuting_macro;
  57. int executing_macro_index;
  58.  
  59. Lisp_Object Fexecute_kbd_macro ();
  60.  
  61. DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 1, "P",
  62.   "Record subsequent keyboard and menu input, defining a keyboard macro.\n\
  63. The commands are recorded even as they are executed.\n\
  64. Use \\[end-kbd-macro] to finish recording and make the macro available.\n\
  65. Use \\[name-last-kbd-macro] to give it a permanent name.\n\
  66. Non-nil arg (prefix arg) means append to last macro defined;\n\
  67.  This begins by re-executing that macro as if you typed it again.")
  68.   (append)
  69.      Lisp_Object append;
  70. {
  71.   if (defining_kbd_macro)
  72.       error ("Already defining kbd macro!");
  73.  
  74.   redraw_mode_line++;
  75.   if (NILP (append))
  76.     {
  77.       kbd_macro_ptr = 0;
  78.       kbd_macro_end = 0;
  79.       pre_command_kbd_macro_end = 0;
  80.       message ("Defining kbd macro...");
  81.     }
  82.   else
  83.     {
  84.       message ("Appending to kbd macro...");
  85.       kbd_macro_ptr = kbd_macro_end;
  86.       Fexecute_kbd_macro (Vlast_kbd_macro, make_number (1));
  87.     }
  88.   defining_kbd_macro++;
  89.  
  90.   return Qnil;
  91. }
  92.  
  93. DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 1, "p",
  94.   "Finish defining a keyboard macro.\n\
  95. The definition was started by \\[start-kbd-macro].\n\
  96. The macro is now available for use via \\[call-last-kbd-macro],\n\
  97. or it can be given a name with \\[name-last-kbd-macro] and then invoked\n\
  98. under that name.\n\
  99. \n\
  100. With numeric arg, repeat macro now that many times,\n\
  101. counting the definition just completed as the first repetition.\n\
  102. An argument of zero means repeat until error.")
  103.   (arg)
  104.      Lisp_Object arg;
  105. {
  106.   if (!defining_kbd_macro)
  107.       error ("Not defining kbd macro.");
  108.  
  109.   if (NILP (arg))
  110.     XFASTINT (arg) = 1;
  111.   else
  112.     CHECK_FIXNUM (arg, 0);
  113.  
  114.   if (defining_kbd_macro)
  115.     {
  116.       int i;
  117.       int size = kbd_macro_end;
  118.       Vlast_kbd_macro = Fmake_vector (size, Qnil);
  119.       for (i = 0; i < kbd_macro_end; i++)
  120.     XVECTOR (Vlast_kbd_macro)->contents [i] =
  121.       XVECTOR (kbd_macro_builder)->contents [i];
  122.       defining_kbd_macro = 0;
  123.       redraw_mode_line++;
  124.       message ("Keyboard macro defined");
  125.     }
  126.  
  127.   if (XFASTINT (arg) == 0)
  128.     Fexecute_kbd_macro (Vlast_kbd_macro, arg);
  129.   else
  130.     {
  131.       XFASTINT (arg)--;
  132.       if (XFASTINT (arg) > 0)
  133.     Fexecute_kbd_macro (Vlast_kbd_macro, arg);
  134.     }
  135.   return Qnil;
  136. }
  137.  
  138.  
  139. /* Store event into kbd macro being defined
  140.  */
  141. void
  142. store_kbd_macro_event (event)
  143.      Lisp_Object event;
  144. {
  145.   if (kbd_macro_ptr == XVECTOR (kbd_macro_builder)->size)
  146.     {
  147.       int i;
  148.       int old_size = XVECTOR (kbd_macro_builder)->size;
  149.       int new_size = old_size * 2;
  150.       Lisp_Object new = Fmake_vector (new_size, Qnil);
  151.       for (i = 0; i < old_size; i++)
  152.     XVECTOR (new)->contents [i] =
  153.       XVECTOR (kbd_macro_builder)->contents [i];
  154.       kbd_macro_builder = new;
  155.     }
  156.   XVECTOR (kbd_macro_builder)->contents [kbd_macro_ptr++] =
  157.     Fcopy_event (event, Qnil);
  158. }
  159.  
  160.  
  161. extern void key_desc_list_to_event (); /* from keymap.c */
  162.  
  163. /* Extract the next kbd-macro element into the given event.
  164.    If we're done, throws to the catch in Fexecute_kbd_macro().
  165.  */
  166. void
  167. pop_kbd_macro_event (event)
  168.      Lisp_Object event;
  169. {
  170.   if (NILP (Vexecuting_macro)) abort ();
  171.  
  172.   switch (XTYPE (Vexecuting_macro)) {
  173.   case Lisp_String:
  174.     if (XSTRING (Vexecuting_macro)->size > executing_macro_index)
  175.       {
  176.     unsigned int c = 
  177.           (unsigned char)
  178.             (XSTRING (Vexecuting_macro)->data [executing_macro_index++]);
  179.         Lisp_Object c_as_int;
  180.         XSET (c_as_int, Lisp_Int, c);
  181.     Fcharacter_to_event (c_as_int, event);
  182.     return;
  183.       }
  184.     break;
  185.   case Lisp_Vector:
  186.     if (XVECTOR (Vexecuting_macro)->size > executing_macro_index)
  187.       {
  188.     Lisp_Object macro_event =
  189.       XVECTOR (Vexecuting_macro)->contents [executing_macro_index++];
  190.     switch (XTYPE (macro_event)) {
  191.     case Lisp_Int:
  192.       character_to_event (XINT (macro_event), XEVENT (event));
  193.       break;
  194.     case Lisp_Cons:
  195.     case Lisp_Symbol:
  196.       key_desc_list_to_event (macro_event, event, 1);
  197.       break;
  198.     default:
  199.       Fcopy_event (macro_event, event);
  200.     }
  201.     return;
  202.       }
  203.     break;
  204.   default:
  205.     error ("junk in executing-macro");
  206.   }
  207.  
  208.   Fthrow (Qexecute_kbd_macro, Qt);
  209. }
  210.  
  211.  
  212. /* Declare that all chars stored so far in the kbd macro being defined
  213.  really belong to it.  This is done in between editor commands.  */
  214.  
  215. extern Lisp_Object unread_command_event;
  216.  
  217. void
  218. finalize_kbd_macro_chars ()
  219. {
  220.   kbd_macro_end = kbd_macro_ptr;
  221. }
  222.  
  223. DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro,
  224.   0, 1, "p",
  225.   "Call the last keyboard macro that you defined with \\[start-kbd-macro].\n\
  226. \n\
  227. A prefix argument serves as a repeat count.  Zero means repeat until error.\n\
  228. \n\
  229. To make a macro permanent so you can call it even after\n\
  230. defining others, use \\[name-last-kbd-macro].")
  231.   (prefix)
  232.      Lisp_Object prefix;
  233. {
  234.   if (defining_kbd_macro)
  235.     error ("Can't execute anonymous macro while defining one");
  236.   else if (NILP (Vlast_kbd_macro))
  237.     error ("No kbd macro has been defined");
  238.   else
  239.     Fexecute_kbd_macro (Vlast_kbd_macro, prefix);
  240.   return Qnil;
  241. }
  242.  
  243. static Lisp_Object
  244. pop_kbd_macro (info)
  245.      Lisp_Object info;
  246. {
  247.   Lisp_Object tem;
  248.   Vexecuting_macro = Fcar (info);
  249.   tem = Fcdr (info);
  250.   executing_macro_index = XINT (tem);
  251.   return Qnil;
  252. }
  253.  
  254. extern Lisp_Object command_loop_1 ();
  255. extern int reset_this_command_keys;
  256.  
  257. DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 2, 0,
  258.   "Execute MACRO as string of editor command characters.\n\
  259. If MACRO is a symbol, its function definition is used.\n\
  260. COUNT is a repeat count, or nil for once, or 0 for infinite loop.")
  261.   (macro, prefixarg)
  262.      Lisp_Object macro, prefixarg;
  263. {
  264.   Lisp_Object final;
  265.   Lisp_Object tem;
  266.   int count = specpdl_ptr - specpdl;
  267.   int repeat = 1;
  268.   struct gcpro gcpro1;
  269.  
  270.   if (!NILP (prefixarg))
  271.     prefixarg = Fprefix_numeric_value (prefixarg),
  272.     repeat = XINT (prefixarg);
  273.  
  274.   final = macro;
  275.   while (SYMBOLP (final) && !EQ (final, Qunbound))
  276.     final = XSYMBOL (final)->function;
  277.   if (!STRINGP (final) && !VECTORP (final))
  278.     CHECK_STRING (final, 0);
  279.  
  280.   XFASTINT (tem) = executing_macro_index;
  281.   tem = Fcons (Vexecuting_macro, tem);
  282.   record_unwind_protect (pop_kbd_macro, tem);
  283.  
  284.   GCPRO1 (final);
  285.   do
  286.     {
  287.       Vexecuting_macro = final;
  288.       executing_macro_index = 0;
  289.       reset_this_command_keys = 1;
  290.       internal_catch (Qexecute_kbd_macro, command_loop_1, Qnil);
  291.     }
  292.   while (--repeat &&
  293.      (STRINGP (Vexecuting_macro) ||
  294.       VECTORP (Vexecuting_macro)));
  295.  
  296.   UNGCPRO;
  297.   return unbind_to (count, Qnil);
  298. }
  299.  
  300.  
  301. void
  302. init_macros ()
  303. {
  304.   Vlast_kbd_macro = Qnil;
  305.   defining_kbd_macro = 0;
  306.   reset_this_command_keys = 1;
  307.  
  308.   Vexecuting_macro = Qnil;
  309. }
  310.  
  311. void
  312. syms_of_macros ()
  313. {
  314.   kbd_macro_builder = Fmake_vector (100, Qnil);
  315.   kbd_macro_end = kbd_macro_ptr = pre_command_kbd_macro_end = 0;
  316.   staticpro (&kbd_macro_builder);
  317.  
  318.   defsubr (&Sstart_kbd_macro);
  319.   defsubr (&Send_kbd_macro);
  320.   defsubr (&Scall_last_kbd_macro);
  321.   defsubr (&Sexecute_kbd_macro);
  322.   Qexecute_kbd_macro = intern ("execute-kbd-macro");
  323.   staticpro (&Qexecute_kbd_macro);
  324.  
  325.   DEFVAR_BOOL ("defining-kbd-macro", &defining_kbd_macro,
  326.     "Non-nil while a keyboard macro is being defined.  Don't set this!");
  327.  
  328.   DEFVAR_LISP ("executing-macro", &Vexecuting_macro,
  329.     "Currently executing keyboard macro (a vector of events);\n\
  330. nil if none executing.");
  331.  
  332.   DEFVAR_LISP ("executing-kbd-macro", &Vexecuting_macro,
  333.     "Currently executing keyboard macro (a vector of events);\n\
  334. nil if none executing.");
  335.  
  336.   DEFVAR_LISP ("last-kbd-macro", &Vlast_kbd_macro,
  337.     "Last kbd macro defined, as a vector of events; nil if none defined.");
  338. }
  339.