【问题标题】:GNU Auto completion on variablesGNU 自动完成变量
【发布时间】:2018-02-09 19:08:45
【问题描述】:

我正在用 C 语言开发一个带有提示符的 Linux 工具。我已经使用 GNU 库启用了“函数”和“文件”完成功能。现在我想将支持扩展到“变量”名称完成。下面是一个更好理解的例子:

工作输出:

build2:/u/anitha> linux_tool
loading history
tool> 
tool> ***On pressing TAB twice, the functions are prompted***
Display all 251 possibilities? (y or n)
byte(        
dim(    
fncc_fft2d(   
HasValue(      
ifft(
load_csv(

预期的额外支持:

tool> **var_name**=create(2,2)
2x2x1 array of int, bsq format [16 bytes]
0       1
2       3
tool>
tool> ***on pressing TAB twice, I want the variable name to be prompted for auto completion. i.e.**,*
tool> var_
**var_name**
Version(

为自动完成添加代码:

char ** dv_complete_func(char *text, int start, int end);
char *dv_complete_func_generator(const char*, int);
extern struct _vfuncptr vfunclist[];
  /* This array has the list of functions that the tool can support */

char **
dv_complete_func(char *text, int start, int end)
{
  return rl_completion_matches(text, dv_complete_func_generator);
}


char *dv_complete_func_generator(const char *text, int state) 
{
    static int list_index, len;
    char *name;

    if(!state) {
        list_index =0; 
        len=strlen(text);
    }   

    while((name = vfunclist[list_index++].name)) {
        if(strncmp(name,text,len)==0) {
             char *namedup = strdup(name);
             strcat(namedup,"(");
            return strdup(namedup);
        }   

    }   
    return NULL;
}

如果问题不清楚,请告诉我。请帮我解决一下。

【问题讨论】:

  • 是的,问题不清楚。
  • 显示您的实际代码。你在正确的轨道上。 显示绑定到rl_attempted_completion_function 的函数的源代码。所以编辑你的问题来改进它。
  • 我也希望为变量扩展“选项卡完成”。即,如果我在提示符中创建一个变量,那么“在按下选项卡时”它还应该显示除函数名称之外的变量。
  • 请编辑您的问题(可能应该更多更长)以提供您的实际源代码
  • 在编辑时,您可能会发现这很有趣stackoverflow.com/editing-help 此外,在编辑时可以预览问题的外观。

标签: c linux autocomplete gnu libreadline


【解决方案1】:

(只是一个猜测,因为你没有像你应该的那样显示绑定到rl_attempted_completion_function 的函数)

您可能还应该使用rl_line_bufferrl_pointrl_end 全局变量(由readline 修改,您只想对它们进行读取访问)。

仔细阅读(并多次阅读)custom completers 上的章节。

顺便说一句,您几乎可以肯定应该使用 dv_complete_funcstartend 参数(假设您将其放在 rl_attempted_completion_function 中)

注意。我在 github 上的(已失效的)minil 项目中围绕这些想法进行了实验。可悲的是,所有 cmets 和变量都是法语的(因为它是为了说服一位法国退休的 AI 研究员而写的)。是的,readline API 是非常 巴洛克式的(你还应该研究使用它的程序的源代码,尤其是 GNU bash)。也许您应该考虑使用其他库...(查看ncurses 并查看this 问题和其他库)

【讨论】:

  • 刚才添加了sn-p的代码。你能检查一下吗?
  • 感谢您的回复。我一定会读你提到的那一章。我有 2 个问题: 1.我的主要疑问是从哪里选择变量名?对于函数的自动完成,我有一个要遍历的数组“vfunclist”。但是对于已经声明/定义的变量,我不确定应该遍历哪个列表以在自动完成时列出它们。 2. 你能推荐任何我可以使用的更好的库吗?
  • 只有你写的函数才会选择变量名。该功能的作用取决于您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-28
  • 2018-05-13
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多