Mercurial > cpython-lru_cache-weakref
annotate Modules/readline.c @ 16798:258dcafe2d0b legacy-trunk
Gustavo Niemeyer <niemeyer@conectiva.com>:
Fixed recno support (keys are integers rather than strings).
Work around DB bug that cause stdin to be closed by rnopen() when the
DB file needed to exist but did not (no longer segfaults).
This closes SF tracker patch #403445.
Also wrapped some long lines and added whitespace around operators -- FLD.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Tue, 27 Feb 2001 18:56:46 +0000 |
| parents | 9b6814bfb2c0 |
| children | 55ee15c4eee6 |
| rev | line source |
|---|---|
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
1 /* This module makes GNU readline available to Python. It has ideas |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
3 * Center. The completer interface was inspired by Lele Gaifax. |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
4 * |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
5 * More recently, it was largely rewritten by Guido van Rossum who is |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
6 * now maintaining it. |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 */ |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
9 /* Standard definitions */ |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 #include "Python.h" |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 #include <setjmp.h> |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 #include <signal.h> |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
13 #include <errno.h> |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
7116
41514d1ed664
Add unistd.h for isatty().
Guido van Rossum <guido@python.org>
parents:
6478
diff
changeset
|
15 #ifdef HAVE_UNISTD_H |
|
41514d1ed664
Add unistd.h for isatty().
Guido van Rossum <guido@python.org>
parents:
6478
diff
changeset
|
16 #include <unistd.h> /* For isatty() */ |
|
41514d1ed664
Add unistd.h for isatty().
Guido van Rossum <guido@python.org>
parents:
6478
diff
changeset
|
17 #endif |
|
41514d1ed664
Add unistd.h for isatty().
Guido van Rossum <guido@python.org>
parents:
6478
diff
changeset
|
18 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
19 /* GNU readline definitions */ |
|
8823
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8391
diff
changeset
|
20 /* If you have string.h, you might need to add yourself to this #if... [cjh] */ |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8391
diff
changeset
|
21 #if defined(__BEOS__) |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8391
diff
changeset
|
22 #undef HAVE_CONFIG_H |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8391
diff
changeset
|
23 /* At max warnings, we need protos for everything. [cjh] */ |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8391
diff
changeset
|
24 #include <readline/readline.h> |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8391
diff
changeset
|
25 #include <readline/history.h> |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8391
diff
changeset
|
26 #include <unistd.h> |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8391
diff
changeset
|
27 #else |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
28 #include <readline/readline.h> /* You may need to add an -I option to Setup */ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
29 |
|
15803
9b6814bfb2c0
Part of SF patch #102409 by jlt63 to support building these modules
Guido van Rossum <guido@python.org>
parents:
14972
diff
changeset
|
30 #ifdef __CYGWIN__ |
|
9b6814bfb2c0
Part of SF patch #102409 by jlt63 to support building these modules
Guido van Rossum <guido@python.org>
parents:
14972
diff
changeset
|
31 #include <readline/history.h> |
|
9b6814bfb2c0
Part of SF patch #102409 by jlt63 to support building these modules
Guido van Rossum <guido@python.org>
parents:
14972
diff
changeset
|
32 #else /* !__CYGWIN__ */ |
|
14277
984abd20eec2
Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents:
13867
diff
changeset
|
33 extern int rl_parse_and_bind(char *); |
|
984abd20eec2
Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents:
13867
diff
changeset
|
34 extern int rl_read_init_file(char *); |
|
984abd20eec2
Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents:
13867
diff
changeset
|
35 extern int rl_insert_text(char *); |
|
984abd20eec2
Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents:
13867
diff
changeset
|
36 extern int rl_bind_key(int, Function *); |
|
984abd20eec2
Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents:
13867
diff
changeset
|
37 extern int rl_bind_key_in_map(int, Function *, Keymap); |
|
984abd20eec2
Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents:
13867
diff
changeset
|
38 extern int rl_initialize(void); |
|
984abd20eec2
Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents:
13867
diff
changeset
|
39 extern int add_history(char *); |
|
14329
b57e360a6d16
Add three prototypes for functions in history.h to shut up gcc -Wall.
Guido van Rossum <guido@python.org>
parents:
14277
diff
changeset
|
40 extern int read_history(char *); |
|
b57e360a6d16
Add three prototypes for functions in history.h to shut up gcc -Wall.
Guido van Rossum <guido@python.org>
parents:
14277
diff
changeset
|
41 extern int write_history(char *); |
|
b57e360a6d16
Add three prototypes for functions in history.h to shut up gcc -Wall.
Guido van Rossum <guido@python.org>
parents:
14277
diff
changeset
|
42 extern int history_truncate_file(char *, int); |
|
9970
8efa37a770c6
Patches by William Lewis for Nextstep descendants.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
43 extern Function *rl_event_hook; |
|
15803
9b6814bfb2c0
Part of SF patch #102409 by jlt63 to support building these modules
Guido van Rossum <guido@python.org>
parents:
14972
diff
changeset
|
44 #endif /* !__CYGWIN__ */ |
|
8823
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8391
diff
changeset
|
45 #endif |
|
7982
a909a2869ac5
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
7116
diff
changeset
|
46 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
47 /* Pointers needed from outside (but not declared in a header file). */ |
|
15803
9b6814bfb2c0
Part of SF patch #102409 by jlt63 to support building these modules
Guido van Rossum <guido@python.org>
parents:
14972
diff
changeset
|
48 extern DL_IMPORT(int) (*PyOS_InputHook)(void); |
|
9b6814bfb2c0
Part of SF patch #102409 by jlt63 to support building these modules
Guido van Rossum <guido@python.org>
parents:
14972
diff
changeset
|
49 extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(char *); |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
51 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
52 /* Exported function to send one line to readline's init file parser */ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
53 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
54 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
55 parse_and_bind(PyObject *self, PyObject *args) |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
56 { |
|
9584
e66e88f52606
Bernard Herzog pointed out that rl_parse_and_bind modifies its
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
57 char *s, *copy; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11485
diff
changeset
|
58 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
59 return NULL; |
|
9584
e66e88f52606
Bernard Herzog pointed out that rl_parse_and_bind modifies its
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
60 /* Make a copy -- rl_parse_and_bind() modifies its argument */ |
|
e66e88f52606
Bernard Herzog pointed out that rl_parse_and_bind modifies its
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
61 /* Bernard Herzog */ |
|
e66e88f52606
Bernard Herzog pointed out that rl_parse_and_bind modifies its
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
62 copy = malloc(1 + strlen(s)); |
|
e66e88f52606
Bernard Herzog pointed out that rl_parse_and_bind modifies its
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
63 if (copy == NULL) |
|
e66e88f52606
Bernard Herzog pointed out that rl_parse_and_bind modifies its
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
64 return PyErr_NoMemory(); |
|
e66e88f52606
Bernard Herzog pointed out that rl_parse_and_bind modifies its
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
65 strcpy(copy, s); |
|
e66e88f52606
Bernard Herzog pointed out that rl_parse_and_bind modifies its
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
66 rl_parse_and_bind(copy); |
|
e66e88f52606
Bernard Herzog pointed out that rl_parse_and_bind modifies its
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
67 free(copy); /* Free the copy */ |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
68 Py_INCREF(Py_None); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
69 return Py_None; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
70 } |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
71 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
72 static char doc_parse_and_bind[] = "\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
73 parse_and_bind(string) -> None\n\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
74 Parse and execute single line of a readline init file.\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
75 "; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
76 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
77 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
78 /* Exported function to parse a readline init file */ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
79 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
80 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
81 read_init_file(PyObject *self, PyObject *args) |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
82 { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
83 char *s = NULL; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11485
diff
changeset
|
84 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s)) |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
85 return NULL; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
86 errno = rl_read_init_file(s); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
87 if (errno) |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
88 return PyErr_SetFromErrno(PyExc_IOError); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
89 Py_INCREF(Py_None); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
90 return Py_None; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
91 } |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
92 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
93 static char doc_read_init_file[] = "\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
94 read_init_file([filename]) -> None\n\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
95 Parse a readline initialization file.\n\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
96 The default filename is the last filename used.\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
97 "; |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
99 |
|
13387
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
100 /* Exported function to load a readline history file */ |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
101 |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
102 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
103 read_history_file(PyObject *self, PyObject *args) |
|
13387
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
104 { |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
105 char *s = NULL; |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
106 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s)) |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
107 return NULL; |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
108 errno = read_history(s); |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
109 if (errno) |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
110 return PyErr_SetFromErrno(PyExc_IOError); |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
111 Py_INCREF(Py_None); |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
112 return Py_None; |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
113 } |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
114 |
|
13644
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
115 static int history_length = -1; /* do not truncate history by default */ |
|
13387
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
116 static char doc_read_history_file[] = "\ |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
117 read_history_file([filename]) -> None\n\ |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
118 Load a readline history file.\n\ |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
119 The default filename is ~/.history.\ |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
120 "; |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
121 |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
122 |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
123 /* Exported function to save a readline history file */ |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
124 |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
125 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
126 write_history_file(PyObject *self, PyObject *args) |
|
13387
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
127 { |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
128 char *s = NULL; |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
129 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s)) |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
130 return NULL; |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
131 errno = write_history(s); |
|
13644
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
132 if (!errno && history_length >= 0) |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
133 history_truncate_file(s, history_length); |
|
13387
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
134 if (errno) |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
135 return PyErr_SetFromErrno(PyExc_IOError); |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
136 Py_INCREF(Py_None); |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
137 return Py_None; |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
138 } |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
139 |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
140 static char doc_write_history_file[] = "\ |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
141 write_history_file([filename]) -> None\n\ |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
142 Save a readline history file.\n\ |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
143 The default filename is ~/.history.\ |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
144 "; |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
145 |
|
1be4665190f0
added read_history_file and write_history_file
Skip Montanaro <skip@pobox.com>
parents:
13084
diff
changeset
|
146 |
|
13644
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
147 static char set_history_length_doc[] = "\ |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
148 set_history_length(length) -> None\n\ |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
149 set the maximal number of items which will be written to\n\ |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
150 the history file. A negative length is used to inhibit\n\ |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
151 history truncation.\n\ |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
152 "; |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
153 |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
154 static PyObject* |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
155 set_history_length(PyObject *self, PyObject *args) |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
156 { |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
157 int length = history_length; |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
158 if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
159 return NULL; |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
160 history_length = length; |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
161 Py_INCREF(Py_None); |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
162 return Py_None; |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
163 } |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
164 |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
165 |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
166 |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
167 static char get_history_length_doc[] = "\ |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
168 get_history_length() -> int\n\ |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
169 return the current history length value.\n\ |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
170 "; |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
171 |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
172 static PyObject* |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
173 get_history_length(PyObject *self, PyObject *args) |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
174 { |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
175 if (!PyArg_ParseTuple(args, ":get_history_length")) |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
176 return NULL; |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
177 return Py_BuildValue("i", history_length); |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
178 } |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
179 |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
180 |
|
2d5aa212a322
added history file truncation based upon code from Johannes Zellner.
Skip Montanaro <skip@pobox.com>
parents:
13496
diff
changeset
|
181 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
182 /* Exported function to specify a word completer in Python */ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
183 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
184 static PyObject *completer = NULL; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
185 static PyThreadState *tstate = NULL; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
186 |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
187 static PyObject *begidx = NULL; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
188 static PyObject *endidx = NULL; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
189 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
190 /* get the beginning index for the scope of the tab-completion */ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
191 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
192 get_begidx(PyObject *self, PyObject *args) |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
193 { |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
194 if(!PyArg_NoArgs(args)) { |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
195 return NULL; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
196 } |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
197 Py_INCREF(begidx); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
198 return begidx; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
199 } |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
200 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
201 static char doc_get_begidx[] = "\ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
202 get_begidx() -> int\n\ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
203 get the beginning index of the readline tab-completion scope"; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
204 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
205 /* get the ending index for the scope of the tab-completion */ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
206 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
207 get_endidx(PyObject *self, PyObject *args) |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
208 { |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
209 if(!PyArg_NoArgs(args)) { |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
210 return NULL; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
211 } |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
212 Py_INCREF(endidx); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
213 return endidx; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
214 } |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
215 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
216 static char doc_get_endidx[] = "\ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
217 get_endidx() -> int\n\ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
218 get the ending index of the readline tab-completion scope"; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
219 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
220 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
221 /* set the tab-completion word-delimiters that readline uses */ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
222 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
223 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
224 set_completer_delims(PyObject *self, PyObject *args) |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
225 { |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
226 char *break_chars; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
227 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11485
diff
changeset
|
228 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
229 return NULL; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
230 } |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
231 free(rl_completer_word_break_characters); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
232 rl_completer_word_break_characters = strdup(break_chars); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
233 Py_INCREF(Py_None); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
234 return Py_None; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
235 } |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
236 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
237 static char doc_set_completer_delims[] = "\ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
238 set_completer_delims(string) -> None\n\ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
239 set the readline word delimiters for tab-completion"; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
240 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
241 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
242 /* get the tab-completion word-delimiters that readline uses */ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
243 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
244 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
245 get_completer_delims(PyObject *self, PyObject *args) |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
246 { |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
247 if(!PyArg_NoArgs(args)) { |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
248 return NULL; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
249 } |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
250 return PyString_FromString(rl_completer_word_break_characters); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
251 } |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
252 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
253 static char doc_get_completer_delims[] = "\ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
254 get_completer_delims() -> string\n\ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
255 get the readline word delimiters for tab-completion"; |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
256 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
257 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
258 set_completer(PyObject *self, PyObject *args) |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
259 { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
260 PyObject *function = Py_None; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11485
diff
changeset
|
261 if (!PyArg_ParseTuple(args, "|O:set_completer", &function)) |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
262 return NULL; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
263 if (function == Py_None) { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
264 Py_XDECREF(completer); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
265 completer = NULL; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
266 tstate = NULL; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
267 } |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
268 else if (PyCallable_Check(function)) { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
269 PyObject *tmp = completer; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
270 Py_INCREF(function); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
271 completer = function; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
272 Py_XDECREF(tmp); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
273 tstate = PyThreadState_Get(); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
274 } |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
275 else { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
276 PyErr_SetString(PyExc_TypeError, |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
277 "set_completer(func): argument not callable"); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
278 return NULL; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
279 } |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
280 Py_INCREF(Py_None); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
281 return Py_None; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
282 } |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
283 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
284 static char doc_set_completer[] = "\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
285 set_completer([function]) -> None\n\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
286 Set or remove the completer function.\n\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
287 The function is called as function(text, state),\n\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
288 for i in [0, 1, 2, ...] until it returns a non-string.\n\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
289 It should return the next possible completion starting with 'text'.\ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
290 "; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
291 |
|
6429
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
292 /* Exported function to read the current line buffer */ |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
293 |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
294 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
295 get_line_buffer(PyObject *self, PyObject *args) |
|
6429
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
296 { |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
297 if (!PyArg_NoArgs(args)) |
|
6429
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
298 return NULL; |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
299 return PyString_FromString(rl_line_buffer); |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
300 } |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
301 |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
302 static char doc_get_line_buffer[] = "\ |
|
8391
a582d70e312f
Correct bug doc string (discovered by Berthold Hoellmann):
Guido van Rossum <guido@python.org>
parents:
7982
diff
changeset
|
303 get_line_buffer() -> string\n\ |
|
6429
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
304 return the current contents of the line buffer.\ |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
305 "; |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
306 |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
307 /* Exported function to insert text into the line buffer */ |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
308 |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
309 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
310 insert_text(PyObject *self, PyObject *args) |
|
6429
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
311 { |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
312 char *s; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11485
diff
changeset
|
313 if (!PyArg_ParseTuple(args, "s:insert_text", &s)) |
|
6429
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
314 return NULL; |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
315 rl_insert_text(s); |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
316 Py_INCREF(Py_None); |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
317 return Py_None; |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
318 } |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
319 |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
320 |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
321 static char doc_insert_text[] = "\ |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
322 insert_text(string) -> None\n\ |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
323 Insert text into the command line.\ |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
324 "; |
|
14a711268c51
Add get_line_buffer() and insert_text(), suggested by Michael McLay.
Guido van Rossum <guido@python.org>
parents:
6338
diff
changeset
|
325 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
326 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
327 /* Table of functions exported by the module */ |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 static struct PyMethodDef readline_methods[] = |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
330 { |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
331 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
332 {"get_line_buffer", get_line_buffer, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
333 METH_OLDARGS, doc_get_line_buffer}, |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
334 {"insert_text", insert_text, METH_VARARGS, doc_insert_text}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
335 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
336 {"read_history_file", read_history_file, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
337 METH_VARARGS, doc_read_history_file}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
338 {"write_history_file", write_history_file, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
339 METH_VARARGS, doc_write_history_file}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
340 {"set_history_length", set_history_length, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
341 METH_VARARGS, set_history_length_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
342 {"get_history_length", get_history_length, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
343 METH_VARARGS, get_history_length_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13676
diff
changeset
|
344 {"set_completer", set_completer, METH_VARARGS, doc_set_completer}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
345 {"get_begidx", get_begidx, METH_OLDARGS, doc_get_begidx}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
346 {"get_endidx", get_endidx, METH_OLDARGS, doc_get_endidx}, |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
347 |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
348 {"set_completer_delims", set_completer_delims, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
349 METH_VARARGS, doc_set_completer_delims}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
350 {"get_completer_delims", get_completer_delims, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
351 METH_OLDARGS, doc_get_completer_delims}, |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
352 {0, 0} |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 }; |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
355 /* C function to call the Python completer. */ |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
357 static char * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
358 on_completion(char *text, int state) |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
359 { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
360 char *result = NULL; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
361 if (completer != NULL) { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
362 PyObject *r; |
|
6478
297a541eb865
Darn. When thread support is disabled, the BEGIN/END macros don't
Guido van Rossum <guido@python.org>
parents:
6429
diff
changeset
|
363 PyThreadState *save_tstate; |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
364 /* Note that readline is called with the interpreter |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
365 lock released! */ |
|
6478
297a541eb865
Darn. When thread support is disabled, the BEGIN/END macros don't
Guido van Rossum <guido@python.org>
parents:
6429
diff
changeset
|
366 save_tstate = PyThreadState_Swap(NULL); |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
367 PyEval_RestoreThread(tstate); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
368 r = PyObject_CallFunction(completer, "si", text, state); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
369 if (r == NULL) |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
370 goto error; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
371 if (r == Py_None) { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
372 result = NULL; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
373 } |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
374 else { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
375 char *s = PyString_AsString(r); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
376 if (s == NULL) |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
377 goto error; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
378 result = strdup(s); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
379 } |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
380 Py_DECREF(r); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
381 goto done; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
382 error: |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
383 PyErr_Clear(); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
384 Py_XDECREF(r); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
385 done: |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
386 PyEval_SaveThread(); |
|
6478
297a541eb865
Darn. When thread support is disabled, the BEGIN/END macros don't
Guido van Rossum <guido@python.org>
parents:
6429
diff
changeset
|
387 PyThreadState_Swap(save_tstate); |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
388 } |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
389 return result; |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 } |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
391 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
392 |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
393 /* a more flexible constructor that saves the "begidx" and "endidx" |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
394 * before calling the normal completer */ |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
395 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
396 char ** |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
397 flex_complete(char *text, int start, int end) |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
398 { |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
399 Py_XDECREF(begidx); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
400 Py_XDECREF(endidx); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
401 begidx = PyInt_FromLong((long) start); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
402 endidx = PyInt_FromLong((long) end); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
403 return completion_matches(text, *on_completion); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
404 } |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
405 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
406 /* Helper to initialize GNU readline properly. */ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
407 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
408 static void |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13644
diff
changeset
|
409 setup_readline(void) |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
410 { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
411 rl_readline_name = "python"; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
412 /* Force rebind of TAB to insert-tab */ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
413 rl_bind_key('\t', rl_insert); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
414 /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
415 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
416 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
417 /* Set our completion function */ |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
418 rl_attempted_completion_function = (CPPFunction *)flex_complete; |
|
6338
4b076fc5ba40
Set the completer's word break characters appropriate for Python.
Guido van Rossum <guido@python.org>
parents:
6335
diff
changeset
|
419 /* Set Python word break characters */ |
|
4b076fc5ba40
Set the completer's word break characters appropriate for Python.
Guido van Rossum <guido@python.org>
parents:
6335
diff
changeset
|
420 rl_completer_word_break_characters = |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
421 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); |
|
6338
4b076fc5ba40
Set the completer's word break characters appropriate for Python.
Guido van Rossum <guido@python.org>
parents:
6335
diff
changeset
|
422 /* All nonalphanums except '.' */ |
|
11485
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
423 |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
424 begidx = PyInt_FromLong(0L); |
|
9cdf3fadda92
Patches by Kannan Vijayan:
Guido van Rossum <guido@python.org>
parents:
10029
diff
changeset
|
425 endidx = PyInt_FromLong(0L); |
|
10029
034b42ba68a6
setup_readline(): Added a comment about memory leak (reported by
Barry Warsaw <barry@python.org>
parents:
9970
diff
changeset
|
426 /* Initialize (allows .inputrc to override) |
|
034b42ba68a6
setup_readline(): Added a comment about memory leak (reported by
Barry Warsaw <barry@python.org>
parents:
9970
diff
changeset
|
427 * |
|
034b42ba68a6
setup_readline(): Added a comment about memory leak (reported by
Barry Warsaw <barry@python.org>
parents:
9970
diff
changeset
|
428 * XXX: A bug in the readline-2.2 library causes a memory leak |
|
034b42ba68a6
setup_readline(): Added a comment about memory leak (reported by
Barry Warsaw <barry@python.org>
parents:
9970
diff
changeset
|
429 * inside this function. Nothing we can do about it. |
|
034b42ba68a6
setup_readline(): Added a comment about memory leak (reported by
Barry Warsaw <barry@python.org>
parents:
9970
diff
changeset
|
430 */ |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
431 rl_initialize(); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
432 } |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
433 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
434 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
435 /* Interrupt handler */ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
436 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
437 static jmp_buf jbuf; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
438 |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
439 /* ARGSUSED */ |
|
13676
5423b132bc97
Removed all instances of RETSIGTYPE from the source code: signal
Tim Peters <tim.peters@gmail.com>
parents:
13665
diff
changeset
|
440 static void |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
441 onintr(int sig) |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
442 { |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
443 longjmp(jbuf, 1); |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 } |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
445 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
446 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
447 /* Wrapper around GNU readline that handles signals differently. */ |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
448 |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
449 static char * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
450 call_readline(char *prompt) |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
451 { |
|
13084
08174bf23702
Trent Mick: use size_t instead of int where appropriate (call_readline()).
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
452 size_t n; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
453 char *p, *q; |
|
14639
133624abc525
Use PyOS_setsig() instead of directly calling signal() or sigaction().
Guido van Rossum <guido@python.org>
parents:
14329
diff
changeset
|
454 PyOS_sighandler_t old_inthandler; |
|
133624abc525
Use PyOS_setsig() instead of directly calling signal() or sigaction().
Guido van Rossum <guido@python.org>
parents:
14329
diff
changeset
|
455 |
|
133624abc525
Use PyOS_setsig() instead of directly calling signal() or sigaction().
Guido van Rossum <guido@python.org>
parents:
14329
diff
changeset
|
456 old_inthandler = PyOS_setsig(SIGINT, onintr); |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
457 if (setjmp(jbuf)) { |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 #ifdef HAVE_SIGRELSE |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
459 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
460 sigrelse(SIGINT); |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
461 #endif |
|
14639
133624abc525
Use PyOS_setsig() instead of directly calling signal() or sigaction().
Guido van Rossum <guido@python.org>
parents:
14329
diff
changeset
|
462 PyOS_setsig(SIGINT, old_inthandler); |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
463 return NULL; |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
464 } |
|
5973
e77847e1158e
Renamed Py_input_hook to PyOS_InputHook.
Guido van Rossum <guido@python.org>
parents:
5947
diff
changeset
|
465 rl_event_hook = PyOS_InputHook; |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
466 p = readline(prompt); |
|
14639
133624abc525
Use PyOS_setsig() instead of directly calling signal() or sigaction().
Guido van Rossum <guido@python.org>
parents:
14329
diff
changeset
|
467 PyOS_setsig(SIGINT, old_inthandler); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
468 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
469 /* We must return a buffer allocated with PyMem_Malloc. */ |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
470 if (p == NULL) { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
471 p = PyMem_Malloc(1); |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
472 if (p != NULL) |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
473 *p = '\0'; |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
474 return p; |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
475 } |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
476 n = strlen(p); |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
477 if (n > 0) |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
478 add_history(p); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
479 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
480 release the original. */ |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
481 q = p; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
482 p = PyMem_Malloc(n+2); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
483 if (p != NULL) { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
484 strncpy(p, q, n); |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
485 p[n] = '\n'; |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
486 p[n+1] = '\0'; |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
487 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
488 free(q); |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
489 return p; |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
490 } |
|
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
491 |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
492 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
493 /* Initialize the module */ |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
494 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
495 static char doc_module[] = |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
496 "Importing this module enables command line editing using GNU readline."; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
497 |
|
9588
38023a425e48
Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents:
9584
diff
changeset
|
498 DL_EXPORT(void) |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13644
diff
changeset
|
499 initreadline(void) |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
500 { |
|
14972
3eecda2ba93a
Supporting rl_library_version is more trouble than it's worth --
Guido van Rossum <guido@python.org>
parents:
14707
diff
changeset
|
501 PyObject *m; |
|
6335
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
502 |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
503 m = Py_InitModule4("readline", readline_methods, doc_module, |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
504 (PyObject *)NULL, PYTHON_API_VERSION); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
505 if (isatty(fileno(stdin))) { |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
506 PyOS_ReadlineFunctionPointer = call_readline; |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
507 setup_readline(); |
|
575f6b910f88
Reordered and reformatted, and added some cool new features:
Guido van Rossum <guido@python.org>
parents:
5973
diff
changeset
|
508 } |
|
5947
7ca6a912d455
New mechanism for GNU readline interface, via module
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
509 } |
