更改bash中内置读取的制表符完成

时间:2016-01-07 16:36:43

标签: bash readline complete

当bash中“read -e”处于活动状态时,当前标签完成似乎只是匹配文件名:

read -e
[[TabTab]]
abc.txt  bcd.txt  cde.txt  

我希望完成是由我定义的一组字符串,而file / dir / hostname-completion等应该在“read -e”的持续时间内被停用。

脚本之外

complete -W 'string1 string2 string3' -E

效果很好,但是在使用“read -e”时,我无法在脚本内完成这种完成。

1 个答案:

答案 0 :(得分:3)

虽然这似乎是一个合理的要求,但我不相信这是可能的。

read builtin的现有实现将readline完成环境设置为一个相当基本的配置,然后再调用readline来处理-e输入。

您可以在builtins/read.def中的edit_line function中看到代码:在NULL的通话期间,它会将rl_attempted_completion_function设置为readlinereadline有几个完成覆盖,因此重置整个完成环境并不是100%明显,但据我所知,这是用于根据complete命令实现可编程完成的函数

通过一些工作,您可以修改read命令的定义,以允许特定的完成函数,而不是readline标准文件名完成函数。这需要对bash内部的非平凡理解,但如果你想熟悉那些内部,这将是一个合理的项目。

作为一种更简单但效率更低的替代方案,您可以编写自己的小实用程序,它只接受一行键盘输入readline并将其回显到stdout。然后调用read将其stdin重定向到您的实用程序:

read -r < <(my_reader string1 string2 string3)

(假设my_reader使用其命令行参数构建readline库的潜在完成列表。您可能希望该选项也提供提示。)

readline文档包括an example of an application,它可以完成简单的自定义;一旦您从K&amp; R函数原型语法中进行翻译,就可以很容易地适应您的需求。

编辑:在我再次查看该示例之后,我认为它有很多不必要的细节,因此我编写了以下示例,其中包含较少的不必要细节。我可能会把它上传到github,但是现在它就在这里,即使它已接近100行:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <readline/readline.h>

static void version(const char* progname) {
  fprintf(stderr, "%s 0.1\n", progname);
}
static void usage(const char* progname) {
  fprintf(stderr, "Usage: %s [-fhv] [-p PROMPT] [-n PROGNAME] [COMPLETION...]\n", progname);
  fprintf(stderr,
          "Reads one line using readline, and prints it to stdout.\n"
          "Returns success if a line was read.\n"
          "  -p PROMPT    Output PROMPT before requesting input.\n"
          "  -n PROGNAME  Set application name to PROGNAME for readline config file\n"
          "               (Default: %s).\n"
          "  -f           Use filename completion as well as specified completions.\n"
          "  -h           Print this help text and exit.\n"
          "  -v           Print version number and exit.\n"
          "  COMPLETION   word to add to the list of possible completions.\n",
          progname);
}

/* Readline really likes globals, so none of its hooks take a context parameter. */
static char** completions = NULL;
static char* generate_next_completion(const char* text, int state) {
  static int index = 0;
  if (state == 0) index = 0; /* reset index if we're starting */
  size_t textlen = strlen(text);
  while (completions[index++])
    if (strncmp(completions[index - 1], text, textlen) == 0)
      return strdup(completions[index - 1]);
  return NULL;
}

/* We use this if we will fall back to filename completion */
static char** generate_completions(const char* text, int start, int end) {
  return rl_completion_matches(text, generate_next_completion);
}

int main (int argc, char **argv) {
  const char* prompt = "";
  const char* progname = strrchr(argv[0], '/');
  progname = progname ? progname + 1 : argv[0];
  rl_readline_name = progname;

  bool use_file_completion = false;

  for (;;) {
    int opt = getopt(argc, argv, "+fp:n:hv");
    switch (opt) {
      case -1:  break;
      case 'f': use_file_completion = true; continue;
      case 'p': prompt = optarg; continue;
      case 'n': rl_readline_name = optarg; continue;
      case 'h': usage(progname); return 0;
      case 'v': version(progname); return 0;
      default:  usage(progname); return 2;
    }
    break;
  }

  /* The default is stdout, which would interfere with capturing output. */
  rl_outstream = stderr;

  completions = argv + optind;
  rl_completion_entry_function = rl_filename_completion_function;
  if (*completions) {
    if (use_file_completion)
      rl_attempted_completion_function = generate_completions; 
    else
      rl_completion_entry_function = generate_next_completion;
  } else {
    /* No specified strings */ 
    if (!use_file_completion)
      rl_inhibit_completion = true; 
  }

  char* line = readline(prompt);

  if (line) {
    puts(line);
    free(line);
    return 0;
  } else {
    fputc('\n', rl_outstream);
    return 1;
  }
}