⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 macro.c

📁 功能强大的文本编辑器
💻 C
📖 第 1 页 / 共 5 页
字号:

#if (VAR_EOLN)
   if (key == EOLN)
      return "\\NL";
#endif

   delta = start_index0;

   for (index = delta-1 ; delta > 0 ;  /* */ )
   {
      if (index >= lengthof(key_tab))
      {
         diff = -1;          /* outside range */
      }
      else
      {
         diff = key - sorted_by_key[index].key;
         if (diff == 0)
            return (sorted_by_key[index].string);
      }

      delta >>= 1;           /* shift right 1 bit */

      if (diff > 0)  /* succesive approximation */
         index += delta;
      else
         index -= delta;
   }  /* for loop */


/* key is not in table: build 'special string' with 1 single char + eos */
   help [0] = (char) key;
   help [1] = '\0';         /* eos: end of string */

   return help;

}  /* key_2_string */


int string_2_key (char **string)
{
/* translate with sorted key table */

int index, delta, help;


/* <backslash> sequence ? */
   if (**string == '\\')
   {
#if (VAR_EOLN)
      if (strnicmp (*string, "\\NL", strlen ("\\NL")) == 0)
      {
         *string += strlen ("\\NL"); /* next index */
         return EOLN;
      }
#endif

      delta = start_index0;
   
      for (index = delta-1 ; delta > 0 ;  /* */ )
      {
         if (index >= lengthof(key_tab))
         {
            help = -1;          /* outside range */
         }
         else
         {
            help = strnicmp (*string, sorted_by_string[index].string,
                              strlen (sorted_by_string[index].string));
         
            if (help == 0)
            {
               *string += strlen (sorted_by_string[index].string); /* next index */
               return (sorted_by_string[index].key);
            }
         }

         delta >>= 1;           /* shift right 1 bit */
   
         if (help > 0)          /* successive approximation */
            index += delta;
         else
            index -= delta;
      }  /* for loop */
   }  /* if '\' */


/* single char or unknown sequence */
   help = (int) **string;
   (*string)++;                /* next index */
   return help;                /* single char */

}  /* string_2_key */

#else

/* -FF-  */

char * key_2_string (int key)
{
/* translate with key table */

int ii;
static char help [2];

#if (VAR_EOLN)
   if (key == EOLN)
      return "\\NL";
#endif

/* search for key in table */
   for (ii = 0 ; ii < lengthof(key_tab) ; ii++)
   {
      if (key == key_tab[ii].key)        /* found key */
         return key_tab[ii].string;      /* return pointer to string */
   }

/* key is not in table: build 'special string' with 1 single char + eos */
   help [0] = (char) key;
   help [1] = '\0';         /* eos: end of string */

   return help;

}  /* key_2_string */


int string_2_key (char **string)
{
/* translate with key table */

int ii, help;

/* <backslash> sequence ? */
   if (**string == '\\')
   {
#if (VAR_EOLN)
      if (strnicmp (*string, "\\NL", strlen ("\\NL")) == 0)
      {
         *string += strlen ("\\NL"); /* next index */
         return EOLN;
      }
#endif

   /* search for key in table */
      for (ii = 0 ; ii < lengthof(key_tab) ; ii++)
      {
         if (strnicmp (*string, key_tab[ii].string,
                        strlen (key_tab[ii].string)) == 0)  /* found string */
         {
            *string += strlen (key_tab[ii].string);        /* next index */
            return key_tab[ii].key;                        /* return key */
         }
      }
   }


/* single char or unknown sequence */
   help = (int) **string;
   (*string)++;                /* next index */
   return help;                /* single char */

}  /* string_2_key */

#endif

/* -FF-  */

struct MACRO *init_macro (char *macro_name)
{
struct MACRO *macro;
size_t nam_len;

   macro = (struct MACRO *) mac_malloc (MACRO_SIZE);
   if (macro != NULL)
   {
     macro->next_macro = NULL;
     
  /* store macro name in buffer */
     nam_len = min (strlen (macro_name), 
                    (sizeof (macro->name)-1));
     macro_name [nam_len] = '\0';  /* limit */
     strcpy (macro->name, macro_name);

  /* store macro sequence */
     macro->length = sizeof (struct MACRO);
     macro->i_ptr = macro->sequence;

  /* aktivate macro store */
     push_macro_stack (macro, MACRO_CREATE, 1L, 0);
   }

   return macro;
}  /* init_macro */

/* -FF-  */

struct MACRO *get_macro_adress (char *macro_name)
{
/* search for macro name in chain */

struct MACRO *act_buf;

   act_buf = top_of_macros;

   while (act_buf != NULL)
   {
   /* check this macro name */
      if (namcmp (act_buf->name, macro_name) == 0)
      {
         return act_buf;   /* found ! */
      }

   /* next turn */
      act_buf = act_buf->next_macro;
   }

/* reached end of chain */
   return NULL;  /* name not found in chain */
}  /* get_macro_adress */

/* -FF-  */

#if 0

size_t mac_len (struct MACRO *macro)
{
size_t length;
int *ptr;

   length = 0;
   ptr = macro->sequence;

   while (*ptr != 0)
   {
      length += sizeof(int);
      ptr++;
   }

   return sizeof(struct MACRO) + length;
}  /* mac_len */


int get_macro_aborted (void)
{
   return (macro_aborted)
}  /* get_macro_aborted */

#endif

/* -FF-  */

int free_current_macro (struct MACRO *macro)
{
/* ^C aborted current macro create */

   if (macro != 0)
   {
      mac_free (macro);
      return 1;       /* o.k. */
   }
   else
   {
      return -1;      /* error */
   }
   
}  /* free_current_macro */

/* -FF-  */

int unchain_macro (char *macro_name)
{
/* release old macro from chain */

struct MACRO *act_buf;
struct MACRO *old_buf;

   old_buf = (struct MACRO *) &top_of_macros;
   act_buf =                   top_of_macros;

   while (act_buf != NULL)
   {
   /* check this macro name */
      if (namcmp (act_buf->name, macro_name) == 0)
      {
      /* macro ausfaedeln */
         old_buf->next_macro = act_buf->next_macro;
         mac_free (act_buf);
         return 1;            /* found ! abort search */
      }

   /* next turn */
      old_buf = act_buf;
      act_buf = act_buf->next_macro;
   }

/* reached end of chain */
   return 0;  /* name not found in chain */

}  /* unchain_macro */

/* -FF-  */

struct MACRO *chain_macro (struct MACRO *macro, int mode)
{
size_t buf_size;
long repeat;
struct MACRO *new_buf;
struct MACRO *act_buf;
struct MACRO *old_buf;

/* modeless or non-modeless macro ? */
   macro->mode = mode;

/* ggf. ausfaedeln des alten macro aus der kette */
   unchain_macro (macro->name);

/* get new buffer */
   buf_size = macro->length;

   new_buf = (struct MACRO *) mac_malloc (buf_size);

   if (new_buf == NULL)  /* out of memory ? */
   {
      new_buf = macro;   /* keep old buffer */
   }
   else                  /* take new (smaller) buffer */
   {
      memcpy (new_buf, macro, buf_size);
      repeat = pop_macro_stack (macro, 0);
      push_macro_stack (new_buf, MACRO_CREATE, repeat, 0);
      mac_free (macro);
   }

/* chain at end of thread */
   old_buf = (struct MACRO *) &top_of_macros;
   act_buf =                   top_of_macros;

   while (act_buf != NULL)
   {
      old_buf = act_buf;
      act_buf = act_buf->next_macro;
   }

/* reached end of chain */
   old_buf->next_macro = new_buf;
   new_buf->next_macro = NULL;

   return new_buf;
}  /* chain_macro */

/* -FF-  */

void reset_macro_stack (void)
{
   stack_index = 0;
   macro_aborted = 1;

   macro_stack [1].cntmac = macro_stack [1].cntexe;
   macro_stack [1].cntexe = 0L;

   return;
}  /* reset_macro_stack */


void stop_this_macro (void)
{
   macro_stack[stack_index].repeat = 1L;   /* letzter durchlauf */
   pop_macro_stack (macro_stack[stack_index].macro, 1);

   return;
}  /* stop_this_macro */

/* -FF-  */

int push_macro_stack (struct MACRO *macro, enum MACRO_STATUS status,
                      long repeat, int save_mode_flag)
{
   if (stack_index >= (MAX_STACK_NUM-1))
   {
      return -1;   /* error, stack overflow */
   }
   else
   {
      stack_index++;
      macro_stack[stack_index].repeat = repeat;
      macro_stack[stack_index].cntexe = 1L;
      macro_stack[stack_index].cntmac = 0L;
      macro_stack[stack_index].macro  = macro;
      macro_stack[stack_index].status = status;
      macro_stack[stack_index].save_mode_flag = save_mode_flag;

      macro->i_ptr = macro->sequence;

      macro_aborted = 0;
      return stack_index;   /* o.k. */
   }

}  /* push_macro_stack */

/* -FF-  */

long pop_macro_stack (struct MACRO *macro, int dekr_repeat)
{
#define REPEAT macro_stack[stack_index].repeat

   macro->i_ptr = macro->sequence;  /* restore pointer to begin of sequence */

   if (stack_index > 0)
   {
      if (dekr_repeat)
      {
          macro_stack[stack_index].repeat--;

          if (macro_stack[stack_index].repeat == 0L)
          {
             macro_stack[stack_index].cntmac = macro_stack[stack_index].cntexe;
             macro_stack[stack_index].cntexe = 0L;
          }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -