Article View: news.software.readers
Article #71511Re: [slrn] tab compleation of filenames
From: Jurriaan (note: this is a workin...
Date: Fri, 02 Feb 2001 21:17
Date: Fri, 02 Feb 2001 21:17
193 lines
7430 bytes
7430 bytes
On 1 Feb 2001 19:54:01 GMT, Thomas Schultz
<tststs@gmx.de> wrote:
>On 01 February 2001 at 12:11 GMT, jwk <_hate_spam_thunder7@xs4all.nl> wrote:
>[...]
>> I think I'm going to start to look at Vim.
>
>You should be aware that Vim isn't available under the terms of the
>GPL, so I won't be able to accept a patch containing code from it.
I have a working version here, but have a few questions left before I
release it to the world at large.
First of all, the design question in my other post.
Second, how do you un-define a key after SLkm_define_key() has set it?
The slang-manual isn't clear on that one. Is redefining it again to NULL
ok?
the simple setup:
take the current input buffer.
split in dir/ and filename part
open dir, read all files
if we find one that matches the filename-part, insert that into the
buffer.
and of course, some state is saved, so you can walk through all the
files in the directory by pressing tab. If you select a file that's
actually a directory, nothing happens. Several microseconds later, slrn
tells you it's unable to open that file :-)
The full buffer is taken into account, not everything upto and including
the cursor position.
I have tested regexp matching, but cannot imagine the benefits of that
really; it wouldn't be tab-completion anymore....
Here is the main part of the code, it's rather unpolished, I'm not sure
about the large number of static char[]'s, I work on a terminal with a
line-width of 133 characters, so the lines are too long and there are
probably all sorts of nasty overflows hidden. If you have to ask how to
include this in your slrn, it won't work. Please wait for the final
thing.
But perhaps some knowledgeable persons could shoot a few holes in it
first.
Jurriaan
#v+
#if SLRN_HAS_TAB_COMPLETION
static int tab_complete(void)
{
static Slrn_Dir_Type *dp;
Slrn_Dirent_Type *df;
static char dir[SLRN_MAX_PATH_LEN+1], filepattern[SLRN_MAX_PATH_LEN+1];
static char pattern[SLRN_MAX_PATH_LEN+1];
static char prev_file[SLRN_MAX_PATH_LEN+1];
static char prev_buffer[SLRN_MAX_PATH_LEN+1];
#if 0
/* regexps may be planned for the future, but don't work right at the moment */
SLRegexp_Type re;
unsigned char compiled_pattern_buf[SLRN_MAX_PATH_LEN+1];
#endif
char *s;
static int found, pos, i;
/* split the current buf in a dir and a file part */
dir[0]='\0';
filepattern[0]='\0';
s = Slrn_Keymap_RLI->buf + Slrn_Keymap_RLI->len - 1;
pos = Slrn_Keymap_RLI->len - 1;
while (pos >= 0)
{
if (*s == SLRN_PATH_SLASH_CHAR)
{
strcpy (filepattern, (char *)(s+1));
strncpy (dir, Slrn_Keymap_RLI->buf, pos+1);
dir[pos + 1] = '\0';
break;
}
pos--;
s--;
}
/* there should be a directory and a file in the buffer */
if (pos == 0)
{
SLtt_beep();
return 0;
}
/* a new pattern:
* - when Slrn_New_Tab_Completion_Pattern is set
* - when the buffer isn't what we returned the previous time,
* meaning the user has changed something in it.
* perhaps we also should take the cursor-position into account.
* if the cursor has wandered left, more files could be proposed.
*/
if ( (Slrn_New_Tab_Completion_Pattern) ||
(strlen(prev_buffer) != strlen(Slrn_Keymap_RLI->buf)) ||
(strncmp(prev_buffer, Slrn_Keymap_RLI->buf, strlen(Slrn_Keymap_RLI->buf) ) ) )
{
/* close previous stream */
if (pattern[0] != '\0')
slrn_close_dir (dp);
strcpy (pattern, filepattern);
prev_file[0] = '\0';
Slrn_New_Tab_Completion_Pattern = 0;
dp = slrn_open_dir (dir);
found = 0;
}
if (dp == NULL)
return -1;
#if 0
re.pat=pattern;
re.buf = compiled_pattern_buf;
re.case_sensitive = 0;
re.buf_len = sizeof (compiled_pattern_buf);
(void)SLang_regexp_compile(&re);
#endif
while (1)
{
while (NULL != (df = slrn_read_dir (dp)))
{
char *filename;
filename = df->name;
if (!slrn_case_strncmp (filename, pattern, strlen (pattern)))
#if 0
if (NULL != slrn_regexp_match(&re, filename))
#endif
{
/* if this is the exact same filename as the previous time, go on */
if ( ( prev_file[0] != '\0') &&
(!slrn_case_strncmp (filename, prev_file, strlen (prev_file))))
{
continue;
}
/* new file wouldn't fit in buffer? */
if ( (strlen (filename) + pos) >
Slrn_Keymap_RLI->buf_len)
{
SLtt_beep();
return -1;
}
found++;
/* skip the last separator */
s++;
for (i=0; i < strlen (filename); i++)
{
*s++ = filename[i];
}
*s = '\0';
Slrn_Keymap_RLI->len = pos + strlen (filename) + 1;
Slrn_Keymap_RLI->point = Slrn_Keymap_RLI->len;
strcpy (prev_file, filename);
strcpy (prev_buffer, Slrn_Keymap_RLI->buf);
return 1;
}
}
/* full scan, none found? */
if ((found == 0) && (prev_file[0] == '\0'))
break;
/* full scan, only the same file as the previous time found? */
if (found == 1)
break;
slrn_close_dir (dp);
dp = slrn_open_dir (dir);
if (dp == NULL)
{
SLtt_beep();
return -1;
}
}
SLtt_beep();
return -1;
}
#endif
#v-
--
I am the special news bulletin that interrupts your favorite show
Darkwing Duck
GNU/Linux 2.2.19pre8 SMP 2x1402 bogomips load av: 0.03 0.07 0.08
Technical Details
Message-ID:
<slrn97m95q.ke1._hate_spam_thunder7@middle.of.nowhere>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.mbox.zip!supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!194.109.6.150!transit.news.xs4all.nl!no_spam_hereReferences:
<d1wd6.14046$E6.544876@news1.rdc1.sdca.home.com> <slrn97e5p3.q5.tststs@starflower.tststs.ddns.org> <slrn97fa6s.3v6._hate_spam_thunder7@middle.of.nowhere> <slrn97gm2t.b8.tststs@starflower.tststs.ddns.org> <slrn97ikqu.2g0._hate_spam_thunder7@middle.of.nowhere> <slrn97jfmp.1jn.tststs@starflower.tststs.ddns.org>Show Full Headers
Complete Article Headers
Message-ID: <slrn97m95q.ke1._hate_spam_thunder7@middle.of.nowhere> Subject: Re: [slrn] tab compleation of filenames Date: Fri, 02 Feb 2001 21:17:42 +0000 From: no_spam_here@rivierenland.xs4all.nl (Jurriaan (note: this is a working address!)) References: <d1wd6.14046$E6.544876@news1.rdc1.sdca.home.com> <slrn97e5p3.q5.tststs@starflower.tststs.ddns.org> <slrn97fa6s.3v6._hate_spam_thunder7@middle.of.nowhere> <slrn97gm2t.b8.tststs@starflower.tststs.ddns.org> <slrn97ikqu.2g0._hate_spam_thunder7@middle.of.nowhere> <slrn97jfmp.1jn.tststs@starflower.tststs.ddns.org> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.mbox.zip!supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!194.109.6.150!transit.news.xs4all.nl!no_spam_here Newsgroups: news.software.readers Organization: XS4ALL Internet BV Lines: 192 NNTP-Posting-Host: rivierenland.xs4all.nl X-Trace: news1.xs4all.nl 981148662 25017 194.109.14.47 (2 Feb 2001 21:17:42 GMT) X-Complaints-To: abuse@xs4all.nl NNTP-Posting-Date: 2 Feb 2001 21:17:42 GMT User-Agent: slrn/0.9.6.3 (GNU/Linux)