big dialogs: dlg_format_text: no need to pass the term.
[elinks.git] / src / scripting / python / open.c
blobffb0cd85c1bbc7e9a502a3dbed83989d9bd8acff
1 /* Document viewing for Python. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <Python.h>
9 #include "elinks.h"
11 #include "intl/gettext/libintl.h"
12 #include "protocol/uri.h"
13 #include "scripting/python/core.h"
14 #include "session/task.h"
15 #include "terminal/tab.h"
16 #include "util/error.h"
18 /* Python interface for viewing a document. */
20 static char python_open_doc[] =
21 PYTHON_DOCSTRING("open(url, new_tab=False, background=False) -> None\n\
22 \n\
23 View a document in either the current tab or a new tab.\n\
24 \n\
25 Arguments:\n\
26 \n\
27 url -- A string containing the URL to view.\n\
28 \n\
29 Optional keyword arguments:\n\
30 \n\
31 new_tab -- By default the URL is opened in the current tab. If this\n\
32 argument's value is the boolean True then the URL is instead\n\
33 opened in a new tab.\n\
34 background -- By default a new tab is opened in the foreground. If\n\
35 this argument's value is the boolean True then a new tab is\n\
36 instead opened in the background. This argument is ignored\n\
37 unless new_tab's value is True.\n");
39 static PyObject *
40 python_open(PyObject *self, PyObject *args, PyObject *kwargs)
42 unsigned char *url;
43 int new_tab = 0, background = 0;
44 struct uri *uri;
45 static char *kwlist[] = {"url", "new_tab", "background", NULL};
47 if (!python_ses) {
48 PyErr_SetString(python_elinks_err, "No session");
49 return NULL;
52 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ii:open",
53 kwlist, &url,
54 &new_tab, &background))
55 return NULL;
57 assert(url);
58 if_assert_failed {
59 PyErr_SetString(python_elinks_err, N_("Internal error"));
60 return NULL;
63 uri = get_translated_uri(url, python_ses->tab->term->cwd);
64 if (!uri) {
65 PyErr_SetString(python_elinks_err, N_("Bad URL syntax"));
66 return NULL;
69 if (new_tab)
70 open_uri_in_new_tab(python_ses, uri, background, 0);
71 else
72 goto_uri(python_ses, uri);
74 done_uri(uri);
76 Py_INCREF(Py_None);
77 return Py_None;
80 static PyMethodDef open_methods[] = {
81 {"open", (PyCFunction) python_open,
82 METH_VARARGS | METH_KEYWORDS,
83 python_open_doc},
85 {NULL, NULL, 0, NULL}
88 int
89 python_init_open_interface(PyObject *dict, PyObject *name)
91 return add_python_methods(dict, name, open_methods);