~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Modules/readline.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
#if defined(_RL_FUNCTION_TYPEDEF)
39
39
extern char **completion_matches(char *, rl_compentry_func_t *);
40
40
#else
 
41
#if !defined(__APPLE__)
41
42
extern char **completion_matches(char *, CPFunction *);
42
43
#endif
43
44
#endif
 
45
#endif
 
46
 
 
47
#ifdef __APPLE__
 
48
/*
 
49
 * It is possible to link the readline module to the readline
 
50
 * emulation library of editline/libedit. 
 
51
 * 
 
52
 * On OSX this emulation library is not 100% API compatible
 
53
 * with the "real" readline and cannot be detected at compile-time,
 
54
 * hence we use a runtime check to detect if we're using libedit
 
55
 *
 
56
 * Currently there is one know API incompatibility: 
 
57
 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
 
58
 *   index with libedit's emulation.
 
59
 * - Note that replace_history and remove_history use a 0-based index
 
60
 *   with both implementation.
 
61
 */
 
62
static int using_libedit_emulation = 0;
 
63
static const char libedit_version_tag[] = "EditLine wrapper";
 
64
#endif /* __APPLE__ */
44
65
 
45
66
static void
46
67
on_completion_display_matches_hook(char **matches,
478
499
 
479
500
        if (!PyArg_ParseTuple(args, "i:index", &idx))
480
501
                return NULL;
 
502
#ifdef  __APPLE__
 
503
        if (using_libedit_emulation) {
 
504
                /* Libedit emulation uses 0-based indexes,
 
505
                 * the real one uses 1-based indexes,
 
506
                 * adjust the index to ensure that Python
 
507
                 * code doesn't have to worry about the
 
508
                 * difference.
 
509
                 */
 
510
                HISTORY_STATE *hist_st;
 
511
                hist_st = history_get_history_state();
 
512
 
 
513
                idx --;
 
514
 
 
515
                /*
 
516
                 * Apple's readline emulation crashes when
 
517
                 * the index is out of range, therefore 
 
518
                 * test for that and fail gracefully.
 
519
                 */
 
520
                if (idx < 0 || idx >= hist_st->length) {
 
521
                        Py_RETURN_NONE;
 
522
                }
 
523
        }
 
524
#endif /* __APPLE__ */
481
525
        if ((hist_ent = history_get(idx)))
482
526
                return PyString_FromString(hist_ent->line);
483
527
        else {
759
803
static char **
760
804
flex_complete(char *text, int start, int end)
761
805
{
 
806
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
 
807
        rl_completion_append_character ='\0';
 
808
#endif
 
809
#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
 
810
        rl_completion_suppress_append = 0;
 
811
#endif
762
812
        Py_XDECREF(begidx);
763
813
        Py_XDECREF(endidx);
764
814
        begidx = PyInt_FromLong((long) start);
801
851
        rl_completer_word_break_characters =
802
852
                strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
803
853
                /* All nonalphanums except '.' */
804
 
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
805
 
        rl_completion_append_character ='\0';
806
 
#endif
807
854
 
808
855
        begidx = PyInt_FromLong(0L);
809
856
        endidx = PyInt_FromLong(0L);
978
1025
                char *line;
979
1026
                HISTORY_STATE *state = history_get_history_state();
980
1027
                if (state->length > 0)
 
1028
#ifdef __APPLE__
 
1029
                        if (using_libedit_emulation) {
 
1030
                                /* 
 
1031
                                 * Libedit's emulation uses 0-based indexes,
 
1032
                                 * the real readline uses 1-based indexes.
 
1033
                                 */
 
1034
                                line = history_get(state->length - 1)->line;
 
1035
                        } else 
 
1036
#endif /* __APPLE__ */
981
1037
                        line = history_get(state->length)->line;
982
1038
                else
983
1039
                        line = "";
1011
1067
PyDoc_STRVAR(doc_module,
1012
1068
"Importing this module enables command line editing using GNU readline.");
1013
1069
 
 
1070
#ifdef __APPLE__
 
1071
PyDoc_STRVAR(doc_module_le,
 
1072
"Importing this module enables command line editing using libedit readline.");
 
1073
#endif /* __APPLE__ */
 
1074
 
1014
1075
PyMODINIT_FUNC
1015
1076
initreadline(void)
1016
1077
{
1017
1078
        PyObject *m;
1018
1079
 
 
1080
#ifdef __APPLE__
 
1081
        if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
 
1082
                using_libedit_emulation = 1;
 
1083
        }
 
1084
 
 
1085
        if (using_libedit_emulation) 
 
1086
                m = Py_InitModule4("readline", readline_methods, doc_module_le,
 
1087
                           (PyObject *)NULL, PYTHON_API_VERSION);
 
1088
        else
 
1089
 
 
1090
#endif /* __APPLE__ */
 
1091
 
1019
1092
        m = Py_InitModule4("readline", readline_methods, doc_module,
1020
1093
                           (PyObject *)NULL, PYTHON_API_VERSION);
1021
1094
        if (m == NULL)
1022
1095
                return;
1023
1096
 
 
1097
 
 
1098
 
1024
1099
        PyOS_ReadlineFunctionPointer = call_readline;
1025
1100
        setup_readline();
1026
1101
}