*** global/global.c 9 Apr 2015 23:03:42 -0000 1.295 --- global/global.c 24 May 2015 23:21:07 -0000 *************** *** 86,91 **** --- 86,92 ---- int Lflag; /* [option] */ int Mflag; /* [option] */ int nflag; /* [option] */ + int Nflag; /* [option] */ int oflag; /* [option] */ int Oflag; /* [option] */ int pflag; /* command */ *************** *** 175,180 **** --- 176,182 ---- {"file-list", required_argument, NULL, 'L'}, {"match-case", no_argument, NULL, 'M'}, {"nofilter", optional_argument, NULL, 'n'}, + {"nearest", optional_argument, NULL, 'N'}, {"grep", no_argument, NULL, 'g'}, {"basic-regexp", no_argument, NULL, 'G'}, {"ignore-case", no_argument, NULL, 'i'}, *************** *** 391,397 **** openconf(root); setenv_from_config(); logging_arguments(argc, argv); ! while ((optchar = getopt_long(argc, argv, "acde:EifFgGIlL:MnoOpPqrsS:tTuvVx", long_options, &option_index)) != EOF) { switch (optchar) { case 0: break; --- 393,399 ---- openconf(root); setenv_from_config(); logging_arguments(argc, argv); ! while ((optchar = getopt_long(argc, argv, "acde:EifFgGIlL:MnNoOpPqrsS:tTuvVx", long_options, &option_index)) != EOF) { switch (optchar) { case 0: break; *************** *** 455,460 **** --- 457,465 ---- nofilter = BOTH_FILTER; } break; + case 'N': + Nflag++; + break; case 'o': oflag++; break; *************** *** 1760,1768 **** --- 1765,1793 ---- /* * search in current source tree. */ + heuristic_retry: count = search(pattern, root, cwd, dbpath, db); total += count; /* + * Heuristic nearest tag search (-N option) + * + * At the first time, global(1) executes a local search normally, + * but if no entries are found, it goes up one directory and repeat + * this process till at least 1 definition is found. + */ + if (count == 0 && Sflag && Nflag && strlen(localprefix) > strlen("./")) { + /* + * localprefix must start with './' and end with '/'. + */ + char *p = localprefix + strlen(localprefix) - 1; + while (*--p != '/' && p > localprefix) + ; + if (*p != '/') + die("tagsearch: Impossible"); + *(p + 1)= '\0'; + goto heuristic_retry; + } + /* * search in library path. */ if (abslib)