Could Emacs be rewritten in Python?

Neil Hodgson nhodgson at bigpond.net.au
Mon Apr 7 08:48:56 EDT 2003


Paul Rubin:

> I think you're going to need some C extensions to represent buffers
> similar to how Emacs does it.  It's hard to move blocks of characters
> around in Python with the array module without constantly re-allocating
> the arrays.

   The only addition to array that is really needed to provide all the
building blocks used in Scintilla is a method for moving blocks around in an
array. Others that would be nice to have but are not as important are fast
ways to add space to the end of an array and to copy between arrays.
However, the real time sink in a GUI editor is most commonly in measuring
and drawing the text, not in the speed of the underlying text storage data
structures.

   Here is a copy_internal method that can be added to the array module that
takes (dest, source,len) parameters. When I have time I'll package this up
for inclusion in Python.

static PyObject *
array_copy_internal(arrayobject *self, PyObject *args)
{
 int dest = -1;
 int source = -1;
 int len = -1;
 if (!PyArg_ParseTuple(args, "iii", &dest, &source, &len))
  return NULL;

 if ((dest != source) && (len > 0)) {
  if ((dest < 0) || (source < 0)) {
   PyErr_SetString(PyExc_RuntimeError,
       "can't use negative index in copy_internal");
   return NULL;
  } else if (((dest + len) >= self->ob_size) ||
   ((source + len) >= self->ob_size)) {
   PyErr_SetString(PyExc_RuntimeError,
       "can't index past the end of an array");
   return NULL;
  } else {
   int itemsize = self->ob_descr->itemsize;
   memmove(self->ob_item + dest*itemsize,
    self->ob_item + source*itemsize,
    len*itemsize);
  }
 }

 Py_INCREF(Py_None);
 return Py_None;
}

PyDoc_STRVAR(copy_internal_doc,
"copy_internal(dest,source,len)\n\
\n\
Copy a range within an array to another place within that array.");

// And in the methods table array_methods goes:

 {"copy_internal", (PyCFunction)array_copy_internal, METH_VARARGS,
  copy_internal_doc},

   Neil






More information about the Python-list mailing list