home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / macros.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-04  |  9.8 KB  |  343 lines

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