1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/* Completion facility functions
Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2008, 2009 Free Software Foundation, Inc.
This file is part of GNU Zile.
GNU Zile is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Zile is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Zile; see the file COPYING. If not, write to the
Free Software Foundation, Fifth Floor, 51 Franklin Street, Boston,
MA 02111-1301, USA. */
#include "config.h"
#include <sys/stat.h>
#include <assert.h>
#include <dirent.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dirname.h"
#include "gl_linked_list.h"
#include "main.h"
#include "extern.h"
/*
* Structure
*/
struct Completion
{
#define FIELD(ty, name) ty name;
#define FIELD_STR(name) const char *name;
#include "completion.h"
#undef FIELD
#undef FIELD_STR
};
#define FIELD(ty, field) \
GETTER (Completion, completion, ty, field) \
SETTER (Completion, completion, ty, field)
#define FIELD_STR(field) \
GETTER (Completion, completion, const char *, field) \
STR_SETTER (Completion, completion, field)
#include "completion.h"
#undef FIELD
#undef FIELD_STR
/*
* List methods for completions and matches
*/
int
completion_strcmp (const void *p1, const void *p2)
{
return strcmp ((char *) p1, (char *) p2);
}
static bool
completion_streq (const void *p1, const void *p2)
{
return strcmp ((char *) p1, (char *) p2) == 0;
}
/*
* Allocate a new completion structure.
*/
Completion *
completion_new (int fileflag)
{
Completion *cp = (Completion *) XZALLOC (Completion);
cp->completions = gl_list_create_empty (GL_LINKED_LIST,
completion_streq, NULL,
(gl_listelement_dispose_fn) free, false);
cp->matches = gl_list_create_empty (GL_LINKED_LIST,
completion_streq, NULL,
NULL, false);
if (fileflag)
{
cp->path = astr_new ();
cp->flags |= CFLAG_FILENAME;
}
return cp;
}
/*
* Dispose an completion structure.
*/
void
free_completion (Completion * cp)
{
gl_list_free (cp->completions);
gl_list_free (cp->matches);
if (cp->flags & CFLAG_FILENAME)
astr_delete (cp->path);
free (cp);
}
/*
* Scroll completions up.
*/
void
completion_scroll_up (void)
{
Window *wp, *old_wp = cur_wp;
Point pt;
wp = find_window ("*Completions*");
assert (wp != NULL);
set_current_window (wp);
pt = get_buffer_pt (cur_bp);
if (pt.n >= get_buffer_last_line (cur_bp) - get_window_eheight (cur_wp) || !FUNCALL (scroll_up))
gotobob ();
set_current_window (old_wp);
term_redisplay ();
}
/*
* Scroll completions down.
*/
void
completion_scroll_down (void)
{
Window *wp, *old_wp = cur_wp;
Point pt;
wp = find_window ("*Completions*");
assert (wp != NULL);
set_current_window (wp);
pt = get_buffer_pt (cur_bp);
if (pt.n == 0 || !FUNCALL (scroll_down))
{
gotoeob ();
resync_redisplay (cur_wp);
}
set_current_window (old_wp);
term_redisplay ();
}
/*
* Calculate the maximum length of the completions.
*/
static size_t
calculate_max_length (gl_list_t l, size_t size)
{
size_t i, maxlen = 0;
for (i = 0; i < MIN (size, gl_list_size (l)); i++)
{
size_t len = strlen ((char *) gl_list_get_at (l, i));
maxlen = MAX (len, maxlen);
}
return maxlen;
}
/*
* Print the list of completions in a set of columns.
*/
static void
completion_print (gl_list_t l, size_t size)
{
size_t i, j, col, max, numcols;
max = calculate_max_length (l, size) + 5;
numcols = (get_window_ewidth (cur_wp) - 1) / max;
bprintf ("Possible completions are:\n");
for (i = col = 0; i < MIN (size, gl_list_size (l)); i++)
{
char *s = (char *) gl_list_get_at (l, i);
size_t len = strlen (s);
if (col >= numcols)
{
col = 0;
insert_newline ();
}
insert_nstring (s, len);
for (j = max - len; j > 0; --j)
insert_char_in_insert_mode (' ');
++col;
}
}
static void
write_completion (va_list ap)
{
Completion *cp = va_arg (ap, Completion *);
int allflag = va_arg (ap, int);
size_t num = va_arg (ap, size_t);
if (allflag)
completion_print (cp->completions, gl_list_size (cp->completions));
else
completion_print (cp->matches, num);
}
/*
* Popup the completion window.
*/
static void
popup_completion (Completion * cp, int allflag, size_t num)
{
cp->flags |= CFLAG_POPPEDUP;
if (get_window_next (head_wp) == NULL)
cp->flags |= CFLAG_CLOSE;
write_temp_buffer ("*Completions*", true, write_completion, cp, allflag, num);
if (!(cp->flags & CFLAG_CLOSE))
cp->old_bp = cur_bp;
term_redisplay ();
}
/*
* Reread directory for completions.
*/
static int
completion_readdir (Completion * cp, astr as)
{
DIR *dir;
char *s1, *s2;
const char *pdir, *base;
struct dirent *d;
struct stat st;
astr bs;
gl_list_free (cp->completions);
cp->completions = gl_list_create_empty (GL_LINKED_LIST,
completion_streq, NULL,
(gl_listelement_dispose_fn) free, false);
if (!expand_path (as))
return false;
bs = astr_new ();
/* Split up path with dirname and basename, unless it ends in `/',
in which case it's considered to be entirely dirname */
s1 = xstrdup (astr_cstr (as));
s2 = xstrdup (astr_cstr (as));
if (astr_get (as, astr_len (as) - 1) != '/')
{
pdir = dir_name (s1);
/* Append `/' to pdir */
astr_cat_cstr (bs, pdir);
if (astr_get (bs, astr_len (bs) - 1) != '/')
astr_cat_char (bs, '/');
free ((char *) pdir);
pdir = astr_cstr (bs);
base = base_name (s2);
}
else
{
pdir = s1;
base = xstrdup ("");
}
astr_cpy_cstr (as, base);
free ((char *) base);
dir = opendir (pdir);
if (dir != NULL)
{
astr buf = astr_new ();
while ((d = readdir (dir)) != NULL)
{
astr_cpy_cstr (buf, pdir);
astr_cat_cstr (buf, d->d_name);
if (stat (astr_cstr (buf), &st) != -1)
{
astr_cpy_cstr (buf, d->d_name);
if (S_ISDIR (st.st_mode))
astr_cat_char (buf, '/');
}
else
astr_cpy_cstr (buf, d->d_name);
gl_sortedlist_add (cp->completions, completion_strcmp,
xstrdup (astr_cstr (buf)));
}
closedir (dir);
astr_delete (cp->path);
cp->path = compact_path (astr_new_cstr (pdir));
astr_delete (buf);
}
astr_delete (bs);
free (s1);
free (s2);
return dir != NULL;
}
/*
* Match completions.
*/
int
completion_try (Completion * cp, astr search, int popup_when_complete)
{
size_t i, j, ssize;
size_t fullmatches = 0, partmatches = 0;
char c;
gl_list_free (cp->matches);
cp->matches = gl_list_create_empty (GL_LINKED_LIST, completion_streq, NULL, NULL, false);
if (cp->flags & CFLAG_FILENAME)
if (!completion_readdir (cp, search))
return COMPLETION_NOTMATCHED;
ssize = astr_len (search);
if (ssize == 0)
{
cp->match = (char *) gl_list_get_at (cp->completions, 0);
if (gl_list_size (cp->completions) > 1)
{
cp->matchsize = 0;
popup_completion (cp, true, 0);
return COMPLETION_NONUNIQUE;
}
else
{
cp->matchsize = strlen (cp->match);
return COMPLETION_MATCHED;
}
}
for (i = 0; i < gl_list_size (cp->completions); i++)
{
char *s = (char *) gl_list_get_at (cp->completions, i);
if (!strncmp (s, astr_cstr (search), ssize))
{
++partmatches;
gl_sortedlist_add (cp->matches, completion_strcmp, s);
if (!strcmp (s, astr_cstr (search)))
++fullmatches;
}
}
if (partmatches == 0)
return COMPLETION_NOTMATCHED;
else if (partmatches == 1)
{
cp->match = (char *) gl_list_get_at (cp->matches, 0);
cp->matchsize = strlen (cp->match);
return COMPLETION_MATCHED;
}
if (fullmatches == 1 && partmatches > 1)
{
cp->match = (char *) gl_list_get_at (cp->matches, 0);
cp->matchsize = strlen (cp->match);
if (popup_when_complete)
popup_completion (cp, false, partmatches);
return COMPLETION_MATCHEDNONUNIQUE;
}
for (j = ssize;; ++j)
{
const char *s = (char *) gl_list_get_at (cp->matches, 0);
c = s[j];
for (i = 1; i < partmatches; ++i)
{
s = gl_list_get_at (cp->matches, i);
if (s[j] != c)
{
cp->match = (char *) gl_list_get_at (cp->matches, 0);
cp->matchsize = j;
popup_completion (cp, false, partmatches);
return COMPLETION_NONUNIQUE;
}
}
}
abort ();
}
|