1 /* Key routines extracted from:
2 * scwmrepl.c,v 1.19 2000/01/22 21:13:16 gjb Exp $
3 * Copyright (C) 1997-2000, Maciej Stachowiak and Greg J. Badros
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING.GPL. If not, write to
17 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
35 #include <readline/readline.h>
37 #include <readline/history.h>
38 #endif /* HAVE_HISTORY */
39 #endif /* HAVE_READLINE */
42 char *scwm_complete(const char *text
, int state
)
44 static char *result
=NULL
;
45 static char *last
=NULL
;
46 static char *completions
[1024];
48 extern Display
*display
;
53 char *query
, *output
, *error
;
56 if (!last
|| strcmp(last
,text
)!=0) {
65 query
= malloc(strlen(text
)+30);
66 sprintf(query
,"(apropos-internal \"^%s\")",text
);
67 result
= xgexec_exec_full(display
, w
, query
, &output
, &error
);
69 if (strlen(error
) > 0)
70 fprintf(stderr
,"Got error querying apropos-internal for completion: %s",error
);
82 /* result is something like "(documentation documentation-debug doc-files)" */
83 szSymbol
= strtok(result
+1," \t)");
85 while (n
<1023 && szSymbol
) {
86 completions
[n
++] = strdup(szSymbol
);
87 szSymbol
= strtok(NULL
," \t)");
91 return completions
[state
];
96 rl_completion_entry_function
= scwm_complete
;
100 int appending_fgets(char **sofar
)
106 buffer
=readline("gwave> ");
117 *sofar
=g_realloc(*sofar
,pos
+len
+2);
118 strncpy(*sofar
+pos
,buffer
,len
);
119 (*sofar
)[pos
+len
]='\n';
120 (*sofar
)[pos
+len
+1]=0;
124 fputs("gwave> ", stdout
);
126 fgets(buffer
, 512, stdin
);
127 if (strlen(buffer
)==0) {
130 *sofar
=g_realloc(*sofar
,strlen(*sofar
)+strlen(buffer
)+1);
131 strcat(*sofar
,buffer
);
132 } while (buffer
[strlen(buffer
)-1]!='\n');
138 int check_balance(char *expr
) {
139 /* If you think _this_ is hairy, try doing it for C statements. */
142 int non_whitespace_p
=0;
144 int prev_separator
=1;
152 /* skip till newline. */
155 } while (expr
[i
]!='\n' && i
<end
);
161 if (non_whitespace_p
&& paren_count
==0 && !quote_wait
) {
169 if (non_whitespace_p
&& paren_count
==0 &&
173 /* skip past ", ignoring \" */
176 if (i
< end
&& expr
[i
]=='\\') {
179 } while (i
< end
&& expr
[i
]!='\"');
181 if (paren_count
==0) {
195 if (non_whitespace_p
&& paren_count
==0 &&
199 if (prev_separator
&& i
+1<end
&& expr
[i
+1]=='{') {
200 /* skip past }#, ignoring \} */
203 if (i
< end
&& expr
[i
]=='\\') {
206 } while (i
< end
&& !(expr
[i
]=='}' && i
+1<end
209 if (paren_count
==0) {
220 /* MS:FIXME:: Handle #\) properly! */
230 if (non_whitespace_p
&& paren_count
==0 &&!quote_wait
) {
242 if (non_whitespace_p
&& paren_count
==0) {
252 if (prev_separator
) {
274 char *split_at(char **to_split
, int i
) {
278 out
=strdup((*to_split
)+i
);
281 ret
=strdup(*to_split
);