You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

462 lines
12 KiB

  1. %%
  2. headers
  3. #include <pygobject.h>
  4. #include <pygtk/pygtk.h>
  5. #include <xed/xed-language-manager.h>
  6. #include <xed/xed-plugin.h>
  7. #include <xed/xed-app.h>
  8. #include <xed/xed-encodings.h>
  9. #include <xed/xed-enum-types.h>
  10. #include <xed/xed-statusbar.h>
  11. #include <xed/xed-debug.h>
  12. #include <xed/xed-help.h>
  13. #include "xed-plugin-python.h"
  14. void pyxed_register_classes (PyObject *d);
  15. void pyxed_add_constants (PyObject *module, const gchar *strip_prefix);
  16. static PyObject *
  17. _helper_wrap_gobject_glist (const GList *list)
  18. {
  19. PyObject *py_list;
  20. const GList *tmp;
  21. if ((py_list = PyList_New(0)) == NULL) {
  22. return NULL;
  23. }
  24. for (tmp = list; tmp != NULL; tmp = tmp->next) {
  25. PyObject *py_obj = pygobject_new(G_OBJECT(tmp->data));
  26. if (py_obj == NULL) {
  27. Py_DECREF(py_list);
  28. return NULL;
  29. }
  30. PyList_Append(py_list, py_obj);
  31. Py_DECREF(py_obj);
  32. }
  33. return py_list;
  34. }
  35. static PyObject *
  36. _helper_wrap_gobject_gslist (const GSList *list)
  37. {
  38. PyObject *py_list;
  39. const GSList *tmp;
  40. if ((py_list = PyList_New(0)) == NULL) {
  41. return NULL;
  42. }
  43. for (tmp = list; tmp != NULL; tmp = tmp->next) {
  44. PyObject *py_obj = pygobject_new(G_OBJECT(tmp->data));
  45. if (py_obj == NULL) {
  46. Py_DECREF(py_list);
  47. return NULL;
  48. }
  49. PyList_Append(py_list, py_obj);
  50. Py_DECREF(py_obj);
  51. }
  52. return py_list;
  53. }
  54. %%
  55. include
  56. xedplugin.override
  57. xedmessage.override
  58. %%
  59. modulename xed
  60. %%
  61. import gtk.Widget as PyGtkWidget_Type
  62. import gobject.GObject as PyGObject_Type
  63. import gtk.gdk.Screen as PyGdkScreen_Type
  64. import gtk.VBox as PyGtkVBox_Type
  65. import gtk.Window as PyGtkWindow_Type
  66. import gtk.Image as PyGtkImage_Type
  67. import gtk.Statusbar as PyGtkStatusbar_Type
  68. import gtksourceview2.Buffer as PyGtkSourceBuffer_Type
  69. import gtksourceview2.View as PyGtkSourceView_Type
  70. import gtksourceview2.Language as PyGtkSourceLanguage_Type
  71. import gtksourceview2.LanguageManager as PyGtkSourceLanguageManager_Type
  72. %%
  73. ignore-glob
  74. *_get_type
  75. xed_document_error_quark
  76. xed_panel_add_item_with_stock_icon
  77. %%
  78. override xed_app_create_window kwargs
  79. static PyObject *
  80. _wrap_xed_app_create_window(PyGObject *self, PyObject *args, PyObject *kwargs)
  81. {
  82. static char *kwlist[] = { "screen", NULL};
  83. PyGObject *screen = NULL;
  84. XedWindow *ret;
  85. if (!PyArg_ParseTupleAndKeywords(args, kwargs,
  86. "|O!", kwlist,
  87. &PyGdkScreen_Type, &screen))
  88. return NULL;
  89. ret = xed_app_create_window(XED_APP(self->obj),
  90. screen ? GDK_SCREEN(screen->obj) : NULL);
  91. /* pygobject_new handles NULL checking */
  92. return pygobject_new((GObject *)ret);
  93. }
  94. %%
  95. override xed_app_get_windows
  96. static PyObject *
  97. _wrap_xed_app_get_windows(PyGObject *self)
  98. {
  99. const GList *list;
  100. PyObject *py_list;
  101. list = xed_app_get_windows (XED_APP (self->obj));
  102. py_list = _helper_wrap_gobject_glist (list);
  103. return py_list;
  104. }
  105. %%
  106. override xed_app_get_views
  107. static PyObject *
  108. _wrap_xed_app_get_views(PyGObject *self)
  109. {
  110. GList *list;
  111. PyObject *py_list;
  112. list = xed_app_get_views (XED_APP (self->obj));
  113. py_list = _helper_wrap_gobject_glist (list);
  114. g_list_free (list);
  115. return py_list;
  116. }
  117. %%
  118. override xed_app_get_documents
  119. static PyObject *
  120. _wrap_xed_app_get_documents(PyGObject *self)
  121. {
  122. GList *list;
  123. PyObject *py_list;
  124. list = xed_app_get_documents (XED_APP (self->obj));
  125. py_list = _helper_wrap_gobject_glist (list);
  126. g_list_free (list);
  127. return py_list;
  128. }
  129. %%
  130. override xed_window_get_documents
  131. static PyObject *
  132. _wrap_xed_window_get_documents(PyGObject *self)
  133. {
  134. GList *list;
  135. PyObject *py_list;
  136. list = xed_window_get_documents (XED_WINDOW (self->obj));
  137. py_list = _helper_wrap_gobject_glist (list);
  138. g_list_free(list);
  139. return py_list;
  140. }
  141. %%
  142. override xed_window_get_unsaved_documents
  143. static PyObject *
  144. _wrap_xed_window_get_unsaved_documents(PyGObject *self)
  145. {
  146. GList *list;
  147. PyObject *py_list;
  148. list = xed_window_get_unsaved_documents (XED_WINDOW (self->obj));
  149. py_list = _helper_wrap_gobject_glist (list);
  150. g_list_free(list);
  151. return py_list;
  152. }
  153. %%
  154. override xed_window_get_views
  155. static PyObject *
  156. _wrap_xed_window_get_views(PyGObject *self)
  157. {
  158. GList *list;
  159. PyObject *py_list;
  160. list = xed_window_get_views (XED_WINDOW (self->obj));
  161. py_list = _helper_wrap_gobject_glist (list);
  162. g_list_free(list);
  163. return py_list;
  164. }
  165. %%
  166. override xed_window_close_tabs kwargs
  167. static PyObject *
  168. _wrap_xed_window_close_tabs (PyGObject *self,
  169. PyObject *args,
  170. PyObject *kwargs)
  171. {
  172. static char *kwlist[] = { "tabs", NULL };
  173. PyObject *list, *item;
  174. GList *glist = NULL;
  175. int len, i;
  176. if (!PyArg_ParseTupleAndKeywords (args, kwargs,
  177. "O:XedWindow.close_tabs", kwlist,
  178. &list))
  179. return NULL;
  180. if (!PySequence_Check (list))
  181. {
  182. PyErr_SetString (PyExc_TypeError,
  183. "first argument must be a sequence");
  184. return NULL;
  185. }
  186. len = PySequence_Length (list);
  187. for (i = 0; i < len; i++)
  188. {
  189. item = PySequence_GetItem (list, i);
  190. Py_DECREF(item);
  191. if (!pygobject_check (item, &PyXedTab_Type))
  192. {
  193. PyErr_SetString (PyExc_TypeError,
  194. "sequence item not a Gtkwidget object");
  195. g_list_free (glist);
  196. return NULL;
  197. }
  198. glist = g_list_append (glist, pygobject_get (item));
  199. }
  200. xed_window_close_tabs (XED_WINDOW (self->obj), glist);
  201. g_list_free (glist);
  202. Py_INCREF (Py_None);
  203. return Py_None;
  204. }
  205. %%
  206. override xed_document_get_search_text
  207. static PyObject *
  208. _wrap_xed_document_get_search_text(PyGObject *self)
  209. {
  210. PyObject *tuple, *string;
  211. guint flags;
  212. gchar *ret;
  213. ret = xed_document_get_search_text (XED_DOCUMENT (self->obj), &flags);
  214. tuple = PyTuple_New(2);
  215. if (ret) {
  216. string = PyString_FromString(ret);
  217. PyTuple_SetItem(tuple, 0, string);
  218. } else {
  219. Py_INCREF(Py_None);
  220. PyTuple_SetItem(tuple, 0, Py_None);
  221. }
  222. PyTuple_SetItem(tuple, 1, PyInt_FromLong(flags));
  223. g_free(ret);
  224. return tuple;
  225. }
  226. %%
  227. override xed_panel_add_item kwargs
  228. static PyObject *
  229. _wrap_xed_panel_add_item(PyGObject *self, PyObject *args, PyObject *kwargs)
  230. {
  231. static char *kwlist1[] = { "item", "name", "image", NULL };
  232. static char *kwlist2[] = { "item", "name", "stock_id", NULL };
  233. PyGObject *item, *image;
  234. char *name = NULL;
  235. char *stock_id = NULL;
  236. if (PyArg_ParseTupleAndKeywords(args, kwargs, "O!sO!:XedPanel.add_item", kwlist1, &PyGtkWidget_Type, &item, &name, &PyGtkImage_Type, &image)) {
  237. xed_panel_add_item(XED_PANEL(self->obj), GTK_WIDGET(item->obj), name, GTK_WIDGET(image->obj));
  238. Py_INCREF(Py_None);
  239. return Py_None;
  240. }
  241. PyErr_Clear();
  242. if (PyArg_ParseTupleAndKeywords(args, kwargs, "O!ss:XedPanel.add_item", kwlist2, &PyGtkWidget_Type, &item, &name, &stock_id)) {
  243. xed_panel_add_item_with_stock_icon(XED_PANEL(self->obj), GTK_WIDGET(item->obj), name, stock_id);
  244. Py_INCREF(Py_None);
  245. return Py_None;
  246. }
  247. PyErr_Clear();
  248. PyErr_SetString(PyExc_TypeError, "the last arg should be either a gtk.Image or a stock_id string");
  249. return NULL;
  250. }
  251. %%
  252. override xed_app_get_default_deprecated
  253. /* deprecated wrappers */
  254. static PyObject *
  255. _wrap_xed_app_get_default_deprecated(PyObject *self)
  256. {
  257. if (PyErr_Warn(PyExc_DeprecationWarning, "use xed.app_get_default instead") < 0)
  258. return NULL;
  259. return _wrap_xed_app_get_default(self);
  260. }
  261. %%
  262. override xed_encoding_get_from_charset_deprecated kwargs
  263. static PyObject *
  264. _wrap_xed_encoding_get_from_charset_deprecated(PyObject *self, PyObject *args, PyObject *kwargs)
  265. {
  266. if (PyErr_Warn(PyExc_DeprecationWarning, "use xed.encoding_get_from_charset instead") < 0)
  267. return NULL;
  268. return _wrap_xed_encoding_get_from_charset(self, args, kwargs);
  269. }
  270. %%
  271. override xed_encoding_get_from_index_deprecated kwargs
  272. static PyObject *
  273. _wrap_xed_encoding_get_from_index_deprecated(PyObject *self, PyObject *args, PyObject *kwargs)
  274. {
  275. if (PyErr_Warn(PyExc_DeprecationWarning, "use xed.encoding_get_from_index instead") < 0)
  276. return NULL;
  277. return _wrap_xed_encoding_get_from_index(self, args, kwargs);
  278. }
  279. %%
  280. override xed_encoding_get_utf8_deprecated
  281. static PyObject *
  282. _wrap_xed_encoding_get_utf8_deprecated(PyObject *self)
  283. {
  284. if (PyErr_Warn(PyExc_DeprecationWarning, "use xed.encoding_get_utf8 instead") < 0)
  285. return NULL;
  286. return _wrap_xed_encoding_get_utf8(self);
  287. }
  288. %%
  289. override xed_encoding_get_current_deprecated
  290. static PyObject *
  291. _wrap_xed_encoding_get_current_deprecated(PyObject *self)
  292. {
  293. if (PyErr_Warn(PyExc_DeprecationWarning, "use xed.encoding_get_current instead") < 0)
  294. return NULL;
  295. return _wrap_xed_encoding_get_current(self);
  296. }
  297. %%
  298. override xed_tab_get_from_document_deprecated kwargs
  299. static PyObject *
  300. _wrap_xed_tab_get_from_document_deprecated(PyObject *self, PyObject *args, PyObject *kwargs)
  301. {
  302. if (PyErr_Warn(PyExc_DeprecationWarning, "use xed.tab_get_from_document instead") < 0)
  303. return NULL;
  304. return _wrap_xed_tab_get_from_document(self, args, kwargs);
  305. }
  306. %%
  307. override xed_language_manager_list_languages_sorted kwargs
  308. static PyObject *
  309. _wrap_xed_language_manager_list_languages_sorted(PyObject *self, PyObject *args, PyObject *kwargs)
  310. {
  311. static char *kwlist[] = { "lm", "include_hidden", NULL };
  312. PyGObject *lm;
  313. int include_hidden;
  314. PyObject *py_list;
  315. GSList *list;
  316. if (!PyArg_ParseTupleAndKeywords (args, kwargs,
  317. "O!i:language_manager_list_languages_sorted",
  318. kwlist, &PyGtkSourceLanguageManager_Type, &lm,
  319. &include_hidden))
  320. return NULL;
  321. list = xed_language_manager_list_languages_sorted (GTK_SOURCE_LANGUAGE_MANAGER (lm->obj),
  322. include_hidden);
  323. py_list = _helper_wrap_gobject_gslist (list);
  324. g_slist_free (list);
  325. return py_list;
  326. }
  327. %%
  328. override xed_debug kwargs
  329. static PyObject *
  330. _wrap_xed_debug(PyObject *self, PyObject *args, PyObject *kwargs)
  331. {
  332. static char *kwlist[] = { "message", NULL };
  333. PyObject *traceback_module, *mdict, *func, *traceback, *tuple;
  334. PyObject *filename, *lineno, *funcname;
  335. char *message = NULL;
  336. if (g_getenv ("XED_DEBUG_PLUGINS") == NULL)
  337. {
  338. Py_INCREF (Py_None);
  339. return Py_None;
  340. }
  341. if (!PyArg_ParseTupleAndKeywords (args, kwargs, "|s", kwlist, &message))
  342. return NULL;
  343. traceback_module = PyImport_ImportModule ("traceback");
  344. if (traceback_module == NULL)
  345. {
  346. g_warning ("traceback module cannot be imported");
  347. Py_INCREF (Py_None);
  348. return Py_None;
  349. }
  350. mdict = PyModule_GetDict (traceback_module);
  351. func = PyDict_GetItemString (mdict, "extract_stack");
  352. traceback = PyObject_CallFunction (func, "zi", NULL, 1);
  353. tuple = PyList_GetItem (traceback, 0);
  354. if (tuple == NULL || !PyTuple_Check (tuple))
  355. {
  356. g_warning ("traceback tuple is null!");
  357. }
  358. else
  359. {
  360. filename = PyTuple_GetItem (tuple, 0);
  361. lineno = PyTuple_GetItem (tuple, 1);
  362. funcname = PyTuple_GetItem (tuple, 2);
  363. if (message == NULL)
  364. xed_debug (XED_DEBUG_PLUGINS,
  365. PyString_AsString (filename),
  366. PyInt_AsLong (lineno),
  367. PyString_AsString (funcname));
  368. else
  369. xed_debug_message (XED_DEBUG_PLUGINS,
  370. PyString_AsString (filename),
  371. PyInt_AsLong (lineno),
  372. PyString_AsString (funcname),
  373. "%s",
  374. message);
  375. }
  376. Py_DECREF (traceback);
  377. Py_DECREF (traceback_module);
  378. Py_INCREF (Py_None);
  379. return Py_None;
  380. }
  381. %%
  382. override xed_statusbar_flash_message kwargs
  383. static PyObject *
  384. _wrap_xed_statusbar_flash_message(PyGObject *self, PyObject *args, PyObject *kwargs)
  385. {
  386. static char *kwlist[] = { "context_id", "message", NULL };
  387. int context_id;
  388. char *message;
  389. if (!PyArg_ParseTupleAndKeywords(args, kwargs,"is:XedStatusbar.flash_message", kwlist, &context_id, &message))
  390. return NULL;
  391. xed_statusbar_flash_message(XED_STATUSBAR(self->obj), context_id, "%s", message);
  392. Py_INCREF(Py_None);
  393. return Py_None;
  394. }
  395. %%