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

📄 coolpython.c

📁 具有IDE功能的编辑器
💻 C
📖 第 1 页 / 共 5 页
字号:
	    break;	}/* check for an empty space in the array */    if (!found && statement)	for (i = 0; i < last_binding; i++)	    if (!bindings[i].statement) {		found = 1;		break;	    }/* extend the array */    if (!found && statement) {	if (last_binding >= MAX_GLOBAL_BINDINGS) {	    PyErr_SetString (PyExc_ValueError, _ ("maximum number of key bindings reached"));	    return NULL;	}	i = last_binding;    }    if (statement) {	bindings[i].statement = (char *) strdup (statement);	bindings[i].modifiers = modifiers;	bindings[i].keysym = keysym;	if (i == last_binding)	    last_binding++;    }    Py_INCREF (Py_None);    return Py_None;}PyObject *edit__bind (PyObject * self, PyObject * args){    char e[128];    int modifiers;    char *key1 = 0, *key2 = 0;    KeySym keysym1, keysym2;    PyObject *function = 0;    if (!PyArg_ParseTuple (args, "ssi|O!:bind", &key1, &key2, &modifiers, &PyFunction_Type, &function))	return NULL;    if (!strncmp (key1, "XK_", 3))	key1 += 3;    if (!strncmp (key2, "XK_", 3))	key2 += 3;    keysym1 = XStringToKeysym (key1);    keysym2 = XStringToKeysym (key2);    if (keysym1 == NoSymbol) {	sprintf (e, _ ("No such key \"%s\".\n\tCheck the header file keysymdef.h on your\n\tsystem for a list of valid keys."), key1);	PyErr_SetString (PyExc_ValueError, e);	return NULL;    }    if (keysym2 == NoSymbol) {	sprintf (e, _ ("No such key \"%s\".\n\tCheck the header file keysymdef.h on your\n\tsystem for a list of valid keys."), key2);	PyErr_SetString (PyExc_ValueError, e);	return NULL;    }    if (add_bindings (function, keysym1, keysym2, modifiers)) {	sprintf (e, _ ("Reached max number of bindings. Some key in range \"%s\" - \"%s\" not bound."), key1, key2);	PyErr_SetString (PyExc_ValueError, e);	return NULL;    }    Py_INCREF (Py_None);    return Py_None;}static void coolpython_constants (PyObject * d){    struct key_list *k;    for (k = get_command_list (); *k->key_name; k++)	if (k->command)	    PyDict_SetItemString (d, k->key_name, PyInt_FromLong ((long) k->command));    PyDict_SetItemString (d, "ShiftMask", PyInt_FromLong ((long) ShiftMask));    PyDict_SetItemString (d, "LockMask", PyInt_FromLong ((long) LockMask));    PyDict_SetItemString (d, "ControlMask", PyInt_FromLong ((long) ControlMask));    PyDict_SetItemString (d, "AltMask", PyInt_FromLong ((long) MyAltMask));    PyDict_SetItemString (d, "Mod1Mask", PyInt_FromLong ((long) Mod1Mask));    PyDict_SetItemString (d, "Mod2Mask", PyInt_FromLong ((long) Mod2Mask));    PyDict_SetItemString (d, "Mod3Mask", PyInt_FromLong ((long) Mod3Mask));    PyDict_SetItemString (d, "Mod4Mask", PyInt_FromLong ((long) Mod4Mask));    PyDict_SetItemString (d, "Mod5Mask", PyInt_FromLong ((long) Mod5Mask));    PyDict_SetItemString (d, "LIBDIR", PyString_FromString (LIBDIR));    PyDict_SetItemString (d, "Mod5Mask", PyInt_FromLong ((long) Mod5Mask));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_LOAD_1", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_LOAD_1));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_LOAD_2", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_LOAD_2));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_LOAD_3", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_LOAD_3));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_LOAD_4", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_LOAD_4));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_LOAD_5", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_LOAD_5));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_LOAD_6", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_LOAD_6));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_SAVE_1", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_SAVE_1));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_SAVE_2", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_SAVE_2));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_SAVE_3", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_SAVE_3));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_SAVE_4", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_SAVE_4));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_SAVE_5", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_SAVE_5));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_SAVE_6", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_SAVE_6));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_DIR_1", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_DIR_1));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_DIR_2", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_DIR_2));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_DIR_3", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_DIR_3));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_DIR_4", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_DIR_4));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_DIR_5", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_DIR_5));    PyDict_SetItemString (d, "INPUTS_WITH_OPTIONS_BROWSE_DIR_6", PyInt_FromLong ((long) INPUTS_WITH_OPTIONS_BROWSE_DIR_6));}void coolpython_run_file (char *filename){    FILE *f;    PyObject *e;    f = fopen (filename, "r");    if (!f) {	CErrorDialog (0, 0, 0, _("Python Error"), get_sys_error (_(" Error trying to open %s ")), filename);	return;    }    e = PyRun_File (f, filename, Py_file_input, name_space, name_space);    fclose (f);    if (!e) {	coolpython_display_error (1);	return;    }    Py_DECREF (e);}static int coolpython_argc = 0;static char **coolpython_argv = 0;void coolpython_shut (void){    int i;#if 0    Py_Finalize ();#endif    for (i = 0; i < last_binding; i++)	if (bindings[i].statement) {	    free (bindings[i].statement);	    memset (&bindings[i], 0, sizeof (bindings[0]));	}    last_binding = 0;}void coolpython_init (int argc, char **argv){    PyObject *e;    char s[1024];    int fd = -1;    Py_Initialize ();    if (argv) {	PySys_SetArgv (coolpython_argc = argc, coolpython_argv = argv);    } else {	if (coolpython_argv)	    PySys_SetArgv (coolpython_argc, coolpython_argv);    }#if 0    Py_XDECREF (name_space);#endif    name_space = PyModule_GetDict (PyImport_AddModule ("__main__"));    initcooledit ();    sprintf (s, "from cooledit import *\n\import sys\n\sys.path.append('%s')\n\sys.path.append('%s%s')\n\", LIBDIR, home_dir, EDIT_DIR);    e = PyRun_String (s, Py_file_input, name_space, name_space);    if (!e) {	PyErr_Print ();	CErrorDialog (0, 0, 0, _ ("Python Error"), _ (" Error initialising Python "));	return;    }    Py_DECREF (e);    coolpython_constants (name_space);    sprintf (s, "%s/%s", LIBDIR, GLOBAL_STARTUP_FILE);    coolpython_run_file (s);    sprintf (s, "%s%s/%s", home_dir, EDIT_DIR, GLOBAL_STARTUP_FILE);    if ((fd = open (s, O_RDONLY)) >= 0)	coolpython_run_file (s);    if (fd >= 0)	close (fd);}int coolpython_run_function (PyObject * function, char *key_name, int modifiers){    PyObject *p, *ret;    p = PyTuple_New (2);    PyTuple_SetItem (p, 0, PyString_FromString (key_name));    PyTuple_SetItem (p, 1, PyInt_FromLong ((long) modifiers));    ret = PyObject_CallObject (function, p);    edit[current_edit]->editor->prev_col = edit_get_col (edit[current_edit]->editor);    Py_DECREF (p);    if (ret == NULL) {	coolpython_display_error (1);	return -1;    }    Py_DECREF (ret);    PyErr_Clear ();    return 0;}int coolpython_run_statement (char *statement){    PyObject *v;    v = PyRun_String (statement, Py_file_input, name_space, name_space);    if (v == NULL) {	coolpython_display_error (1);	return -1;    }    edit[current_edit]->editor->prev_col = edit_get_col (edit[current_edit]->editor);    Py_DECREF (v);    PyErr_Clear ();    return 0;}static void type_change (int i){    char s[256];    sprintf (s, "type_change ('%s')", edit[i]->editor->syntax_type ? edit[i]->editor->syntax_type : "");    free_current_bindings (edit[i]->user);    edit[i]->user = 0;    coolpython_run_statement (s);}void coolpython_typechange (Window win){    int temp_current_edit;    temp_current_edit = current_edit;    for (current_edit = 0; current_edit < last_edit; current_edit++)	if (win == edit[current_edit]->winid) {	    type_change (current_edit);	    break;	}    current_edit = temp_current_edit;}void menu_python_reload (unsigned long ignored){    int temp_current_edit;    coolpython_shut ();    coolpython_init (0, 0);    temp_current_edit = current_edit;    for (current_edit = 0; current_edit < last_edit; current_edit++)	type_change (current_edit);    current_edit = temp_current_edit;}/* this is similar to the function edit__menu - if you change here, change there as well */static PyObject *edit__replace_insert_menu (PyObject * self, PyObject * args, int insert){    char *menu_name, *old_item = 0, *new_item = 0, *statement = 0;    char ident[33];    CWidget *w;    if (!PyArg_ParseTuple (args, "ssss:replace_menu", &menu_name, &old_item, &new_item, &statement))	return NULL;    if (!strcasecmp (menu_name, "util")) {	strcpy (ident, edit[current_edit]->ident);	strcat (ident, ".util");    } else if (!strcasecmp (menu_name, "command")) {	strcpy (ident, "menu.commandmenu");    } else if (!strcasecmp (menu_name, "readme")) {	strcpy (ident, "menu.readme");    } else if (!strcasecmp (menu_name, "search")) {	strcpy (ident, "menu.searchmenu");    } else if (!strcasecmp (menu_name, "edit")) {	strcpy (ident, "menu.editmenu");    } else if (!strcasecmp (menu_name, "file")) {	strcpy (ident, "menu.filemenu");    } else {	char e[160];	sprintf (e, "%s \"%s\". %s\n\"Util\", \"File\", \"Edit\", \"Search\", \"Command\", \"Options\" or \"Readme\".", _ ("No such menu:"), _ ("Possible menus are:"), menu_name);	PyErr_SetString (PyExc_ValueError, e);	return NULL;    }    {	char *p;	int i;	w = CIdent (ident);	if (w) {	    if (insert)		CInsertMenuItem (ident, old_item, new_item, '~', (callfn) coolpython_run_statement, 0);	    else		CReplaceMenuItem (ident, old_item, new_item, '~', (callfn) coolpython_run_statement, 0);	    i = CHasMenuItem (ident, new_item);	    if (i < 0) {		char e[160];		sprintf (e, "%s: %s", _ ("Menu replace failed: Check that there is such a menu item"), old_item);		PyErr_SetString (PyExc_ValueError, e);		return NULL;	    }/* hack: we want statement to free with destruction of the menu item */	    p = w->menu[i].text;	    w->menu[i].text = malloc (strlen (p) + strlen (statement) + 2);	    strcpy (w->menu[i].text, p);	    strcpy (w->menu[i].text + strlen (p) + 1, statement);	    w->menu[i].data = (unsigned long) (w->menu[i].text + strlen (p) + 1);	    free (p);	}    }    Py_INCREF (Py_None);    return Py_None;}PyObject *edit__insert_menu (PyObject * self, PyObject * args){    return edit__replace_insert_menu (self, args, 1);}PyObject *edit__replace_menu (PyObject * self, PyObject * args){    return edit__replace_insert_menu (self, args, 0);}/* this is similar to the function edit__replace_menu - if you change here, change there as well */PyObject *edit__menu (PyObject * self, PyObject * args){    char *menu_name, *menu_item = 0, *statement = 0;    char ident[33];    CWidget *w;    if (!PyArg_ParseTuple (args, "s|ss:menu", &menu_name, &menu_item, &statement))	return NULL;    if (!strcasecmp (menu_name, "util")) {	strcpy (ident, edit[current_edit]->ident);	strcat (ident, ".util");    } else if (!strcasecmp (menu_name, "command")) {	strcpy (ident, "menu.commandmenu");    } else if (!strcasecmp (menu_name, "readme")) {	strcpy (ident, "menu.readme");    } else if (!strcasecmp (menu_name, "search")) {	strcpy (ident, "menu.searchmenu");    } else if (!strcasecmp (menu_name, "edit")) {	strcpy (ident, "menu.editmenu");    } else if (!strcasecmp (menu_name, "file")) {	strcpy (ident, "menu.filemenu");    } else {	char e[160];	sprintf (e, "%s \"%s\". %s\n\"Util\", \"File\", \"Edit\", \"Search\", \"Command\", \"Options\" or \"Readme\".", _ ("No such menu:"), _ ("Possible menus are:"), menu_name);	PyErr_SetString (PyExc_ValueError, e);	return NULL;    }    if (!menu_item) {	w = CIdent (ident);	if (w) {	    int i;	    for (i = w->numlines - 1; i >= 0; i--)		CRemoveMenuItemNumber (ident, i);	}    } else if (!statement) {	CRemoveMenuItem (ident, menu_item);    } else {	char *p;	int i;	w = CIdent (ident);	if (w) {	    CAddMenuItem (ident, menu_item, '~', (callfn) coolpython_run_statement, 0);	    i = w->numlines - 1;/* hack: we want statement to free with destruction of the menu item */	    p = w->menu[i].text;	    w->menu[i].text = malloc (strlen (p) + strlen (statement) + 2);	    strcpy (w->menu[i].text, p);	    strcpy (w->menu[i].text + strlen (p) + 1, statement);	    w->menu[i].data = (unsigned long) (w->menu[i].text + strlen (p) + 1);	    free (p);	}    }    Py_INCREF (Py_None);    return Py_None;}PyObject *edit__gettext (PyObject * self, PyObject * args){    char *s;    if (!PyArg_ParseTuple (args, "s:gettext", &s))	return NULL;    return PyString_FromString ((char *) _ ((char *) s));}void coolpython_command (WEdit * edit, int i){    int k;    struct python_binding *b;    if (i >= GLOBAL_BINDING (0) && i < GLOBAL_BINDING (MAX_GLOBAL_BINDINGS)) {/* lcc breaks here */	int j;	j = BINDING_GLOBAL (i);	i = j;	if (i >= 0 || i < last_binding)	    coolpython_run_statement (bindings[i].statement);    } else if (i >= CURRENT_BINDING (0) && i < CURRENT_BINDING (MAX_CURRENT_BINDINGS)) {	i = BINDING_CURRENT (i);	for (k = 0, b = (struct python_binding *) edit->widget->user; b && k <= i; b = b->next, k++) {	    if (i == k) {		if (b->statement)		    coolpython_run_statement (b->statement);		else		    coolpython_run_function (b->function, XKeysymToString (b->keysym), b->modifiers);	    }	}    }}static void coolpython_display_error (int set_sys_last_vars){    PyObject *e;    char t[1024];    PyObject *exception, *v, *tb, *d;    PyErr_Fetch (&exception, &v, &tb);    PyErr_NormalizeException (&exception, &v, &tb);    if (exception == NULL)	return;    if (set_sys_last_vars) {	PySys_SetObject ("last_type", exception);	PySys_SetObject ("last_value", v);	PySys_SetObject ("last_traceback", tb);    }    d = PyModule_GetDict (PyImport_AddModule ("__main__"));    PyDict_SetItemString (d, "exc_type", exception);    PyDict_SetItemString (d, "exc_value", v);    PyDict_SetItemString (d, "exc_traceback", tb ? tb : Py_None);    sprintf (t, "\n\import cooledit\n\import traceback\n\s = \"\"\n\for filename, line, function, text in traceback.extract_tb(exc_traceback):\n\    s = s + ' File \"%%s\", line %%d, in %%s\\n    %%s' %% (filename, line, function, text)\n\    if s[-1] != '\\n':\n\	s = s + '\\n'\n\for l in traceback.format_exception_only(exc_type, exc_value):\n\    s = s + ' ' + l\n\    if s[-1] != '\\n':\n\	s = s + '\\n'\n\cooledit.error_dialog (\"%s\", s)\n\", _("Python Interpretor Error"));    e = PyRun_String (t, Py_file_input, d, d);    if (!e) {	PyErr_Print();	CErrorDialog (0, 0, 0, _("Python Error"), _(" Error trying to display traceback. \n Message dumped to stderr "));    }    Py_XDECREF (e);    Py_XDECREF (d);    Py_XDECREF (exception);    Py_XDECREF (v);    Py_XDECREF (tb);}static PyObject *_wrap_buffer_size(PyObject *self, PyObject *args) {    PyObject * _resultobj;    long  _result;    self = self;    if(!PyArg_ParseTuple(args,":buffer_size"))         return NULL;    _result = (long )buffer_size();    _resultobj = Py_BuildValue("l",_result);    return _resultobj;}static PyObject *_wrap_move(PyObject *self, PyObject *args) {    PyObject * _resultobj;    long  _arg0;    self = self;    if(!PyArg_ParseTuple(args,"l:move",&_arg0))         return NULL;    move(_arg0);    Py_INCREF(Py_None);    _resultobj = Py_None;    return _resultobj;}static PyObject *_wrap_move_lines(PyObject *self, PyObject *args) {    PyObject * _resultobj;    long  _arg0;    self = self;

⌨️ 快捷键说明

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