update watch file
[cl-readline.git] / cl-readline.c
1 /*
2  *   Copyright (c) 2003 Nikodemus Siivola
3  *
4  *   Permission is hereby granted, free of charge, to any person obtaining
5  *   a copy of this software and associated documentation files (the
6  *   "Software"), to deal in the Software without restriction, including
7  *   without limitation the rights to use, copy, modify, merge, publish,
8  *   distribute, sublicense, and/or sell copies of the Software, and to
9  *   permit persons to whom the Software is furnished to do so, subject to
10  *   the following conditions:
11  *
12  *   The above copyright notice and this permission notice shall be included
13  *   in all copies or substantial portions of the Software.
14  *
15  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  *   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  *   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  *   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  *   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <readline/readline.h>
27
28 typedef struct {
29   char * str;
30   void * next;
31 }
32 node_t;
33
34 node_t * collection = NULL;
35
36 int
37 add_completion (char * str)
38 {
39   node_t * tmp = collection;
40   node_t * pre = NULL;
41   node_t * node;
42   while (tmp)
43     {
44       int cmp = strcmp (str, tmp->str);
45       if (0 == cmp)
46         return 1;
47
48       if (0 < cmp)
49         break;
50
51       /* printf ("-skip- (%s)\n", tmp->str);*/
52
53       pre = tmp;
54       tmp = tmp->next;
55     }
56
57   node = malloc (sizeof (node_t));
58   if (!node)
59     return 0;
60
61   node->next = tmp;
62   node->str = strdup (str);
63
64   if (! pre)
65     collection = node;
66   else
67     pre->next = node;
68
69   return 1;
70 }
71
72 node_t * root = NULL;
73
74 char *
75 custom_completer (const char * str, int target)
76 {
77   size_t len = strlen (str);
78   if (0 == target)
79     {
80       root = NULL;
81       node_t * tmp = collection;
82       while (len && tmp)
83         {
84           if (0 == strncmp (str, tmp->str, len))
85             {
86               root = tmp;
87               return strdup (root->str);
88             }
89           else
90             tmp = tmp->next;
91         }
92       return (char *)NULL;
93     }
94   else if (root && (0 == strncmp (str, root->str, len)))
95     {
96       node_t * tmp = root;
97       root = root->next;
98       return strdup (tmp->str);
99     }
100   else
101     return (char *)NULL;
102 }
103
104 void
105 clear_completions ()
106 {
107   node_t * tmp;
108   while (collection)
109     {
110       /* printf ("-del- (%s)\n", collection->str); */
111       tmp = collection->next;
112       free (collection->str);
113       free (collection);
114       collection = tmp;
115     }
116 }
117
118 void
119 use_custom_complete (void)
120 {
121   rl_completion_entry_function = custom_completer;
122 }
123
124 void
125 use_filename_complete (void)
126 {
127   rl_completion_entry_function = rl_filename_completion_function;
128 }