📄 cooledit.i
字号:
/* cooledit.i - python module for builtin Python interpretor Copyright (C) 1998 Paul Sheer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */%module cooledit%{#define GLOBAL_STARTUP_FILE "global.py"#include <config.h>#include <stdio.h>#include <stdlib.h>#include <signal.h>#include <sys/types.h>#if HAVE_SYS_WAIT_H#include <sys/wait.h>#endif#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/Xatom.h>#include "lkeysym.h"#include "stringtools.h"#include "coolwidget.h"#include "edit.h"#include "editcmddef.h"#include "editoptions.h"#include "shell.h" /* MAX_NUM_SCRIPTS */#include "find.h"#include "mad.h"void coolpython_init (int argc, char **argv);void coolpython_shut (void);int coolpython_key (unsigned int state, unsigned int keycode, KeySym keysym);void coolpython_command (WEdit * edit, int i);void coolpython_typechange (Window win);void initcooledit ();static void coolpython_display_error (int set_sys_last_vars);#define CHECK_CURRENT if (!last_edit) return;#define CHECK_CURRENT_RETURN(x) if (!last_edit) return (x);extern CWidget *edit[];extern long current_edit;extern long last_edit;static PyObject *name_space = 0;/* {{{ bindings */#define MAX_GLOBAL_BINDINGS 1000#define MAX_CURRENT_BINDINGS 1000#define GLOBAL_BINDING(i) ((i) + MAX_CURRENT_BINDINGS)#define CURRENT_BINDING(i) ((i) + 0)#define BINDING_GLOBAL(i) ((i) - MAX_CURRENT_BINDINGS)#define BINDING_CURRENT(i) ((i) - 0)struct python_binding { PyObject *function; char *statement; KeySym keysym; int modifiers; struct python_binding *next;};static struct { char *statement; KeySym keysym; int modifiers;} bindings[MAX_GLOBAL_BINDINGS];int last_binding = 0;static void free_current_bindings (void *x){ struct python_binding *b, *n; for (b = (struct python_binding *) x; b; b = n) { n = b->next; if (b->statement) free (b->statement); Py_XDECREF (b->function); free (b); }}/* returns non-zero on error: i.e. max bindings reached */static int add_binding (PyObject * function, char *statement, KeySym keysym, int modifiers){ struct python_binding **b, *p; int i; b = (struct python_binding **) &(edit[current_edit]->user); for (i = 0; *b; b = &((*b)->next), i++) { if (i >= MAX_CURRENT_BINDINGS) return 1; p = *b; if (p->keysym == keysym && p->modifiers == modifiers) { Py_XDECREF (p->function); p->function = 0; if (p->statement) { free (p->statement); p->statement = 0; } if (!function && !statement) { p = (*b)->next; free (*b); *b = p; return 0; } else { goto no_alloc; } } } p = *b = malloc (sizeof (struct python_binding)); p->next = 0; no_alloc: if (function) { Py_INCREF (function); p->function = function; p->statement = 0; } else if (statement) { p->function = 0; p->statement = (char *) strdup (statement); } else { free (p); return 0; } p->keysym = keysym; p->modifiers = modifiers; edit[current_edit]->free_user = free_current_bindings; return 0;}static int add_bindings (PyObject *function, KeySym keysym1, KeySym keysym2, int modifiers){ int r = 0; while (keysym1 <= keysym2) if ((r = add_binding (function, 0, keysym1++, modifiers))) break; return r;}int coolpython_key (unsigned int state, unsigned int keycode, KeySym keysym){ struct python_binding *b; int i; CWidget *w;/* make sure we are really an editor since bindings should not apply in input/text/other widgets */ w = CWidgetOfWindow (CGetFocus ()); if (!w) return -1; if (w->kind != C_EDITOR_WIDGET) return -1;/* check bindings for current editor */ for (i = 0, b = (struct python_binding *) edit[current_edit]->user; b; b = b->next, i++) if (b->keysym == keysym && b->modifiers == state) return CURRENT_BINDING (i);/* check global bindings */ for (i = 0; i < last_binding; i++) if (bindings[i].keysym == keysym && bindings[i].modifiers == state) return GLOBAL_BINDING (i); return -1;}/* }}} bindings */static void move (long i){ CHECK_CURRENT; edit_cursor_move (edit[current_edit]->editor, i);}static void move_lines (long i){ CHECK_CURRENT; if (i > 0) edit_move_up (edit[current_edit]->editor, i, 0); else if (i < 0) edit_move_down (edit[current_edit]->editor, i, 0);}static void move_to (long i){ CHECK_CURRENT; edit_cursor_move (edit[current_edit]->editor, i - edit[current_edit]->editor->curs1);}static void insert (char *s){ CHECK_CURRENT; for (; *s; s++) edit_insert (edit[current_edit]->editor, (long) *s);}static void delete (long i){ CHECK_CURRENT; if (i > 0) while (i-- && edit[current_edit]->editor->curs2 > 0) edit_delete (edit[current_edit]->editor);}static void back_space (long i){ CHECK_CURRENT; if (i > 0) while (i-- && edit[current_edit]->editor->curs1 > 0) edit_backspace (edit[current_edit]->editor);}static void insert_ahead (char *s){ long l, r; CHECK_CURRENT; r = l = strlen (s); s += l - 1; for (; l >= 0; s--, l--) edit_insert_ahead (edit[current_edit]->editor, (long) *s);}static long current (void){ CHECK_CURRENT_RETURN (-1); return edit[current_edit]->editor->curs1;}static long current_line (void){ CHECK_CURRENT_RETURN (-1); return edit[current_edit]->editor->curs_line;}static long bol (long i){ CHECK_CURRENT_RETURN (-1); if (i > edit[current_edit]->editor->total_lines || i < 0) return -1; return edit_find_line (edit[current_edit]->editor, i);}static long eol (long i){ CHECK_CURRENT_RETURN (-1); if (i > edit[current_edit]->editor->total_lines || i < 0) return -1; return edit_eol (edit[current_edit]->editor, bol (i));}static long find_forwards (long from, char *s){ CHECK_CURRENT_RETURN (-1); if (!*s) return -1; for (; from < edit[current_edit]->editor->last_byte; from++) { if (*s == edit_get_byte (edit[current_edit]->editor, from)) { int i; for (i = 1;; i++) { if (!s[i]) return from; if (s[i] == edit_get_byte (edit[current_edit]->editor, from + i)) continue; break; } } } return -1;}static long find_backwards (long from, char *s){ CHECK_CURRENT_RETURN (-1); if (!*s) return -1; for (; from >= 0; from--) { if (*s == edit_get_byte (edit[current_edit]->editor, from)) { int i; for (i = 1;; i++) { if (!s[i]) return from; if (s[i] == edit_get_byte (edit[current_edit]->editor, from + i)) continue; break; } } } return -1;}static PyObject *edit__get_text (PyObject * self, PyObject * args){ PyObject *result; char *r, *p; long i, j = -1, l; if (!PyArg_ParseTuple (args, "|ll:get_text", &i, &j)) return NULL; if (i == -1) i = edit[current_edit]->editor->curs1; if (j == -1) j = i + 1; l = j - i; if (l < 0) { PyErr_SetString (PyExc_ValueError, _("second offset is less than first")); return NULL; } p = r = malloc (l + 1); if (i < 0) i = 0; if (j >= edit[current_edit]->editor->last_byte) j = edit[current_edit]->editor->last_byte; for (; i < j; i++) *p++ = edit_get_byte (edit[current_edit]->editor, i); *p = '\0'; result = PyString_FromStringAndSize (r, l); free (r); return result;}%}%native (get_text) PyObject *edit__get_text (PyObject * self, PyObject * args);%{static PyObject *edit__get_line (PyObject * self, PyObject * args){ PyObject *result; char *r, *p; int i, j, l, l1 = -1, l2 = -1; if (!PyArg_ParseTuple (args, "|ii:get_line", &l1, &l2)) return NULL; if (l1 == -1) l1 = edit[current_edit]->editor->curs_line; if (l2 == -1) l2 = l1; i = bol (l1); j = eol (l2); l = j - i; if (l < 0) { PyErr_SetString (PyExc_ValueError, _("second offset is less than first")); return NULL; } p = r = malloc (l + 1); if (i < 0) i = 0; if (j >= edit[current_edit]->editor->last_byte) j = edit[current_edit]->editor->last_byte; for (; i < j; i++) *p++ = edit_get_byte (edit[current_edit]->editor, i); *p = '\0'; result = PyString_FromStringAndSize (r, l); free (r); return result;}%}%native (get_line) PyObject *edit__get_line (PyObject * self, PyObject * args);%{static long line (long i){ CHECK_CURRENT_RETURN (-1); if (i > edit[current_edit]->editor->curs1) return edit[current_edit]->editor->curs_line + edit_count_lines (edit[current_edit]->editor, edit[current_edit]->editor->curs1, i); return edit[current_edit]->editor->curs_line - edit_count_lines (edit[current_edit]->editor, i, edit[current_edit]->editor->curs1);}static void command (long i){ CHECK_CURRENT; edit_execute_cmd (edit[current_edit]->editor, i, -1);}/* window functions */static void focus (void){ CHECK_CURRENT; CFocus (edit[current_edit]); XRaiseWindow (CDisplay, edit[current_edit]->parentid); CRaiseWindows (); current_to_top ();}static long set_editor (char *filename_with_path){ char *f, *d, *p; int i, r = 1; d = pathdup (filename_with_path); p = strrchr (d, '/'); if (p) { p++; f = (char *) strdup (p); *p = '\0'; } else { free (d); return 1; } for (i = 0; i < last_edit; i++) { if (strcmp (f, edit[i]->editor->filename)) continue; if (strcmp (d, edit[i]->editor->dir)) continue; current_edit = i; r = 0; break; } free (d); free (f); return r;}static void close_window (long force){ if (force) edit[current_edit]->editor->stopped = 1; else edit_execute_command (edit[current_edit]->editor, CK_Exit, -1);}static void new_window (void){ new_window_callback (0);}static int load (char *filename){ return edit_load_file_from_filename (edit[current_edit]->editor, filename);}static void status (char *text){ char id[33]; CWidget *w; strcpy (id, CIdentOf (edit[current_edit])); strcat (id, ".text"); w = CIdent (id); free (w->text); w->text = (char *) strdup (text); render_status (w, 0);}static char *file (void){ return edit[current_edit]->editor->filename;}static char *directory (void){ return edit[current_edit]->editor->dir;}static long modified (void){ return (long) edit[current_edit]->editor->modified;}static char *input_dialog (char *title, char *prompt, char *def){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -