indices.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /* indices.c -- deal with an Info file index.
  2. $Id$
  3. Copyright 1993, 1997, 1998, 1999, 2002, 2003, 2004, 2007, 2008, 2011,
  4. 2013, 2014, 2015, 2016, 2017 Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Originally written by Brian Fox. */
  16. #include "info.h"
  17. #include "info-utils.h"
  18. #include "session.h"
  19. #include "echo-area.h"
  20. #include "indices.h"
  21. #include "variables.h"
  22. /* User-visible variable controls the output of info-index-next. */
  23. int show_index_match = 1;
  24. /* The combined indices of the last file processed by
  25. info_indices_of_file_buffer. */
  26. static REFERENCE **index_index = NULL;
  27. /* The offset of the most recently selected index element. */
  28. static int index_offset = 0;
  29. /* Whether we are doing initial index search. */
  30. static int index_initial = 0;
  31. /* Whether we are doing partial index search */
  32. static int index_partial = 0;
  33. /* Variable which holds the last string searched for. */
  34. static char *index_search = NULL;
  35. /* A couple of "globals" describing where the initial index was found. */
  36. static char *initial_index_filename = NULL;
  37. static char *initial_index_nodename = NULL;
  38. /* A structure associating index names with index offset ranges. */
  39. typedef struct {
  40. char *name; /* The nodename of this index. */
  41. int first; /* The index in our list of the first entry. */
  42. int last; /* The index in our list of the last entry. */
  43. } INDEX_NAME_ASSOC;
  44. /* An array associating index nodenames with index offset ranges. Used
  45. for reporting to the user which index node an index entry was found
  46. in. */
  47. static INDEX_NAME_ASSOC **index_nodenames = NULL;
  48. static size_t index_nodenames_index = 0;
  49. static size_t index_nodenames_slots = 0;
  50. /* Add the name of NODE, and the range of the associated index elements
  51. (passed in ARRAY) to index_nodenames. ARRAY must have at least one
  52. element. */
  53. static void
  54. add_index_to_index_nodenames (REFERENCE **array, NODE *node)
  55. {
  56. register int i, last;
  57. INDEX_NAME_ASSOC *assoc;
  58. for (last = 0; array[last + 1]; last++);
  59. assoc = xmalloc (sizeof (INDEX_NAME_ASSOC));
  60. assoc->name = xstrdup (node->nodename);
  61. if (!index_nodenames_index)
  62. {
  63. assoc->first = 0;
  64. assoc->last = last;
  65. }
  66. else
  67. {
  68. for (i = 0; index_nodenames[i + 1]; i++);
  69. assoc->first = 1 + index_nodenames[i]->last;
  70. assoc->last = assoc->first + last;
  71. }
  72. add_pointer_to_array (assoc, index_nodenames_index, index_nodenames,
  73. index_nodenames_slots, 10);
  74. }
  75. /* Find and concatenate the indices of FILE_BUFFER, saving the result in
  76. INDEX_INDEX. The indices are defined as the first node in the file
  77. containing the word "Index" and any immediately following nodes whose names
  78. also contain "Index". All such indices are concatenated and the result
  79. returned. */
  80. static void
  81. info_indices_of_file_buffer (FILE_BUFFER *file_buffer)
  82. {
  83. register int i;
  84. REFERENCE **result = NULL;
  85. /* No file buffer, no indices. */
  86. if (!file_buffer)
  87. {
  88. free (index_index);
  89. index_index = 0;
  90. return;
  91. }
  92. /* If the file is the same as the one that we last built an
  93. index for, don't do anything. */
  94. if (initial_index_filename
  95. && FILENAME_CMP (initial_index_filename, file_buffer->filename) == 0)
  96. {
  97. return;
  98. }
  99. /* Display a message because finding the index entries might take a while. */
  100. if (info_windows_initialized_p)
  101. window_message_in_echo_area (_("Finding index entries..."));
  102. /* Reset globals describing where the index was found. */
  103. free (initial_index_filename);
  104. free (initial_index_nodename);
  105. initial_index_filename = NULL;
  106. initial_index_nodename = NULL;
  107. if (index_nodenames)
  108. {
  109. for (i = 0; index_nodenames[i]; i++)
  110. {
  111. free (index_nodenames[i]->name);
  112. free (index_nodenames[i]);
  113. }
  114. index_nodenames_index = 0;
  115. index_nodenames[0] = NULL;
  116. }
  117. /* Grovel the names of the nodes found in this file. */
  118. if (file_buffer->tags)
  119. {
  120. TAG *tag;
  121. for (i = 0; (tag = file_buffer->tags[i]); i++)
  122. {
  123. if (strcasestr (tag->nodename, "Index")
  124. && tag->cache.nodelen != 0) /* Not an anchor. */
  125. {
  126. NODE *node;
  127. REFERENCE **menu;
  128. node = info_node_of_tag (file_buffer, &file_buffer->tags[i]);
  129. if (!node)
  130. continue;
  131. if (!initial_index_filename)
  132. {
  133. /* Remember the filename and nodename of this index. */
  134. initial_index_filename = xstrdup (file_buffer->filename);
  135. initial_index_nodename = xstrdup (tag->nodename);
  136. }
  137. menu = node->references;
  138. /* If we have a non-empty menu, add this index's nodename
  139. and range to our list of index_nodenames. */
  140. if (menu && menu[0])
  141. {
  142. add_index_to_index_nodenames (menu, node);
  143. /* Concatenate the references found so far. */
  144. {
  145. REFERENCE **old_result = result;
  146. result = info_concatenate_references (result, menu);
  147. free (old_result);
  148. }
  149. }
  150. free_history_node (node);
  151. }
  152. }
  153. }
  154. /* If there is a result, clean it up so that every entry has a filename. */
  155. for (i = 0; result && result[i]; i++)
  156. if (!result[i]->filename)
  157. result[i]->filename = xstrdup (file_buffer->filename);
  158. free (index_index);
  159. index_index = result;
  160. if (info_windows_initialized_p)
  161. window_clear_echo_area ();
  162. }
  163. void info_next_index_match (WINDOW *window, int count);
  164. DECLARE_INFO_COMMAND (info_index_search,
  165. _("Look up a string in the index for this file"))
  166. {
  167. FILE_BUFFER *fb;
  168. char *line;
  169. int old_offset;
  170. fb = file_buffer_of_window (window);
  171. if (fb)
  172. info_indices_of_file_buffer (fb); /* Sets index_index. */
  173. if (!fb || !index_index)
  174. {
  175. info_error (_("No indices found."));
  176. return;
  177. }
  178. line = info_read_maybe_completing (_("Index entry: "), index_index);
  179. /* User aborted? */
  180. if (!line)
  181. {
  182. info_abort_key (window, 1);
  183. return;
  184. }
  185. /* Empty line means move to the Index node. */
  186. if (!*line)
  187. {
  188. free (line);
  189. if (initial_index_filename && initial_index_nodename)
  190. {
  191. NODE *node;
  192. node = info_get_node (initial_index_filename,
  193. initial_index_nodename);
  194. info_set_node_of_window (window, node);
  195. }
  196. return;
  197. }
  198. /* Start the search either at the first or last index entry. */
  199. if (count < 0)
  200. {
  201. register int i;
  202. for (i = 0; index_index[i]; i++);
  203. index_offset = i;
  204. }
  205. else
  206. {
  207. index_offset = -1;
  208. index_initial = 0;
  209. index_partial = 0;
  210. }
  211. old_offset = index_offset;
  212. /* The "last" string searched for is this one. */
  213. free (index_search);
  214. index_search = line;
  215. info_next_index_match (window, count);
  216. /* If the search failed, return the index offset to where it belongs. */
  217. if (index_offset == old_offset)
  218. index_offset = 0;
  219. }
  220. /* Return true if ENT->label matches "S( <[0-9]+>)?", where S stands
  221. for the first LEN characters from STR. */
  222. static int
  223. index_entry_matches (REFERENCE *ent, const char *str, size_t len)
  224. {
  225. char *p;
  226. if (strncmp (ent->label, str, len))
  227. return 0;
  228. p = ent->label + len;
  229. if (!*p)
  230. return 1;
  231. if (p[0] == ' ' && p[1] == '<')
  232. {
  233. for (p += 2; *p; p++)
  234. {
  235. if (p[0] == '>' && p[1] == 0)
  236. return 1;
  237. else if (!isdigit (*p))
  238. return 0;
  239. }
  240. }
  241. return 0;
  242. }
  243. /* Search for the next occurence of STRING in FB's indices starting at OFFSET
  244. in direction DIR.
  245. Try to get an exact match, If no match found, progress onto looking for
  246. initial matches, then non-initial substrings, updating the values of
  247. INDEX_INITIAL and INDEX_PARTIAL.
  248. If a match is found, return a pointer to the matching index entry, and
  249. set *FOUND_OFFSET to its offset in INDEX_INDEX. Otherwise, return null.
  250. If we found a partial match, set *MATCH_OFFSET to the end of the match
  251. within the index entry text, else to 0. */
  252. REFERENCE *
  253. next_index_match (FILE_BUFFER *fb, char *string, int offset, int dir,
  254. int *found_offset, int *match_offset)
  255. {
  256. int i;
  257. int partial_match;
  258. size_t search_len;
  259. REFERENCE *result;
  260. partial_match = 0;
  261. search_len = strlen (string);
  262. info_indices_of_file_buffer (fb); /* Sets index_index. */
  263. if (!index_index)
  264. {
  265. info_error (_("No indices found."));
  266. return 0;
  267. }
  268. if (index_search != string)
  269. {
  270. free (index_search); index_search = string;
  271. }
  272. if (!index_initial && !index_partial)
  273. {
  274. /* First try to find an exact match. */
  275. for (i = offset + dir; i > -1 && index_index[i]; i += dir)
  276. if (index_entry_matches (index_index[i], string, search_len))
  277. {
  278. *match_offset = 0;
  279. break;
  280. }
  281. if (i < 0 || !index_index[i])
  282. {
  283. offset = 0;
  284. index_initial = 1;
  285. }
  286. }
  287. if (index_initial)
  288. {
  289. for (i = offset + dir; i > -1 && index_index[i]; i += dir)
  290. if (!index_entry_matches (index_index[i], string, search_len)
  291. && !strncmp (index_index[i]->label, string, search_len))
  292. {
  293. *match_offset = search_len;
  294. break;
  295. }
  296. if (i < 0 || !index_index[i])
  297. {
  298. offset = 0;
  299. index_initial = 0;
  300. index_partial = 1;
  301. }
  302. }
  303. if (index_partial)
  304. {
  305. /* Look for substrings, excluding case-matching inital matches. */
  306. for (i = offset + dir; i > -1 && index_index[i]; i += dir)
  307. {
  308. if (strncmp (index_index[i]->label, string, search_len) != 0)
  309. {
  310. partial_match = string_in_line (string, index_index[i]->label);
  311. if (partial_match != -1)
  312. {
  313. *match_offset = partial_match;
  314. break;
  315. }
  316. }
  317. }
  318. if (partial_match <= 0)
  319. index_partial = 0;
  320. }
  321. if (i < 0 || !index_index[i])
  322. result = 0;
  323. else
  324. {
  325. index_offset = i;
  326. result = index_index[i];
  327. }
  328. *found_offset = i;
  329. return result;
  330. }
  331. /* Display a message saying where the index match was found. */
  332. void
  333. report_index_match (int i, int match_offset)
  334. {
  335. register int j;
  336. const char *name = "CAN'T SEE THIS";
  337. char *match;
  338. for (j = 0; index_nodenames[j]; j++)
  339. {
  340. if ((i >= index_nodenames[j]->first) &&
  341. (i <= index_nodenames[j]->last))
  342. {
  343. name = index_nodenames[j]->name;
  344. break;
  345. }
  346. }
  347. /* If we had a partial match, indicate to the user which part of the
  348. string matched. */
  349. match = xstrdup (index_index[i]->label);
  350. if (match_offset > 0 && show_index_match)
  351. {
  352. int k, ls, start, upper;
  353. ls = strlen (index_search);
  354. start = match_offset - ls;
  355. upper = isupper (match[start]) ? 1 : 0;
  356. for (k = 0; k < ls; k++)
  357. if (upper)
  358. match[k + start] = tolower (match[k + start]);
  359. else
  360. match[k + start] = toupper (match[k + start]);
  361. }
  362. {
  363. char *format;
  364. format = replace_in_documentation
  365. (_("Found '%s' in %s. ('\\[next-index-match]' tries to find next.)"),
  366. 0);
  367. window_message_in_echo_area (format, match, (char *) name);
  368. }
  369. free (match);
  370. }
  371. DECLARE_INFO_COMMAND (info_next_index_match,
  372. _("Go to the next matching index item from the last '\\[index-search]' command"))
  373. {
  374. int i;
  375. int match_offset;
  376. int dir;
  377. REFERENCE *result;
  378. /* If there is no previous search string, the user hasn't built an index
  379. yet. */
  380. if (!index_search)
  381. {
  382. info_error (_("No previous index search string."));
  383. return;
  384. }
  385. /* The direction of this search is controlled by the value of the
  386. numeric argument. */
  387. if (count < 0)
  388. dir = -1;
  389. else
  390. dir = 1;
  391. result = next_index_match (file_buffer_of_window (window), index_search,
  392. index_offset, dir, &i, &match_offset);
  393. /* If that failed, print an error. */
  394. if (!result)
  395. {
  396. info_error (index_offset > 0 ?
  397. _("No more index entries containing '%s'.") :
  398. _("No index entries containing '%s'."),
  399. index_search);
  400. index_offset = 0;
  401. return;
  402. }
  403. /* Report to the user on what we have found. */
  404. report_index_match (i, match_offset);
  405. info_select_reference (window, result);
  406. }
  407. /* Look for the best match of STRING in the indices of FB. If SLOPPY, allow
  408. case-insensitive initial substrings to match. Return null if no match is
  409. found. Return value should not be freed or modified. This differs from the
  410. behaviour of next_index_match in that only _initial_ substrings are
  411. considered. */
  412. REFERENCE *
  413. look_in_indices (FILE_BUFFER *fb, char *string, int sloppy)
  414. {
  415. REFERENCE **index_ptr;
  416. REFERENCE *nearest = 0;
  417. /* Remember the search string so we can use it as the default for
  418. 'virtual-index' or 'next-index-match'. */
  419. free (index_search);
  420. index_search = xstrdup (string);
  421. info_indices_of_file_buffer (fb); /* Sets index_index. */
  422. if (!index_index)
  423. return 0;
  424. for (index_ptr = index_index; *index_ptr; index_ptr++)
  425. {
  426. if (!strcmp (string, (*index_ptr)->label))
  427. {
  428. nearest = *index_ptr;
  429. break;
  430. }
  431. /* Case-insensitive initial substring. */
  432. if (sloppy && !nearest && !mbsncasecmp (string, (*index_ptr)->label,
  433. mbslen (string)))
  434. {
  435. nearest = *index_ptr;
  436. }
  437. }
  438. return nearest;
  439. }
  440. /* **************************************************************** */
  441. /* */
  442. /* Info APROPOS: Search every known index. */
  443. /* */
  444. /* **************************************************************** */
  445. /* For every menu item in DIR, search the indices of that file for
  446. SEARCH_STRING. */
  447. REFERENCE **
  448. apropos_in_all_indices (char *search_string, int inform)
  449. {
  450. size_t i, dir_index;
  451. REFERENCE **all_indices = NULL;
  452. REFERENCE **dir_menu = NULL;
  453. NODE *dir_node;
  454. dir_node = get_dir_node ();
  455. /* It should be safe to assume that dir nodes do not contain any
  456. cross-references, i.e., its references list only contains
  457. menu items. */
  458. if (dir_node)
  459. dir_menu = dir_node->references;
  460. if (!dir_menu)
  461. {
  462. free (dir_node);
  463. return NULL;
  464. }
  465. /* For every menu item in DIR, get the associated file buffer and
  466. read the indices of that file buffer. Gather all of the indices into
  467. one large one. */
  468. for (dir_index = 0; dir_menu[dir_index]; dir_index++)
  469. {
  470. REFERENCE **this_index, *this_item;
  471. FILE_BUFFER *this_fb, *loaded_file = 0;
  472. this_item = dir_menu[dir_index];
  473. if (!this_item->filename)
  474. continue;
  475. /* If we already scanned this file, don't do that again.
  476. In addition to being faster, this also avoids having
  477. multiple identical entries in the *Apropos* menu. */
  478. for (i = 0; i < dir_index; i++)
  479. if (dir_menu[i]->filename
  480. && FILENAME_CMP (this_item->filename, dir_menu[i]->filename) == 0)
  481. break;
  482. if (i < dir_index)
  483. continue;
  484. this_fb = check_loaded_file (this_item->filename);
  485. if (!this_fb)
  486. this_fb = loaded_file = info_find_file (this_item->filename);
  487. if (!this_fb)
  488. continue; /* Couldn't load file. */
  489. if (this_fb && inform)
  490. message_in_echo_area (_("Scanning indices of '%s'..."), this_item->filename);
  491. info_indices_of_file_buffer (this_fb);
  492. this_index = index_index;
  493. if (this_fb && inform)
  494. unmessage_in_echo_area ();
  495. if (this_index)
  496. {
  497. /* Remember the filename which contains this set of references. */
  498. for (i = 0; this_index && this_index[i]; i++)
  499. if (!this_index[i]->filename)
  500. this_index[i]->filename = xstrdup (this_fb->filename);
  501. /* Concatenate with the other indices. */
  502. {
  503. REFERENCE **old_indices = all_indices;
  504. all_indices = info_concatenate_references (all_indices, this_index);
  505. free (old_indices);
  506. }
  507. }
  508. /* Try to avoid running out of memory by not loading all of the
  509. Info files on the system into memory. This is risky because we
  510. may have a pointer into the file buffer, so only free the contents
  511. if we have just loaded the file. */
  512. if (loaded_file)
  513. {
  514. free (loaded_file->contents);
  515. loaded_file->contents = NULL;
  516. }
  517. }
  518. /* Build a list of the references which contain SEARCH_STRING. */
  519. if (all_indices)
  520. {
  521. REFERENCE *entry, **apropos_list = NULL;
  522. size_t apropos_list_index = 0;
  523. size_t apropos_list_slots = 0;
  524. for (i = 0; (entry = all_indices[i]); i++)
  525. {
  526. if (string_in_line (search_string, entry->label) != -1)
  527. {
  528. add_pointer_to_array (entry, apropos_list_index, apropos_list,
  529. apropos_list_slots, 100);
  530. }
  531. }
  532. free (all_indices);
  533. all_indices = apropos_list;
  534. }
  535. free (dir_node);
  536. return all_indices;
  537. }
  538. static char *apropos_list_nodename = "*Apropos*";
  539. DECLARE_INFO_COMMAND (info_index_apropos,
  540. _("Grovel all known info file's indices for a string and build a menu"))
  541. {
  542. char *line, *prompt;
  543. REFERENCE **apropos_list;
  544. NODE *apropos_node;
  545. struct text_buffer message;
  546. if (index_search)
  547. asprintf (&prompt, "%s [%s]: ", _("Index apropos"), index_search);
  548. else
  549. asprintf (&prompt, "%s: ", _("Index apropos"));
  550. line = info_read_in_echo_area (prompt);
  551. free (prompt);
  552. window = active_window;
  553. /* User aborted? */
  554. if (!line)
  555. {
  556. info_abort_key (window, 1);
  557. return;
  558. }
  559. /* User typed something? */
  560. if (*line)
  561. {
  562. free (index_search);
  563. index_search = line;
  564. }
  565. else
  566. free (line); /* Try to use the last search string. */
  567. if (index_search && *index_search)
  568. {
  569. apropos_list = apropos_in_all_indices (index_search, 1);
  570. if (!apropos_list)
  571. {
  572. info_error (_(APROPOS_NONE), index_search);
  573. return;
  574. }
  575. else
  576. {
  577. /* Create the node. FIXME: Labels and node names taken from the
  578. indices of Info files may be in a different character encoding to
  579. the one currently being used.
  580. This problem is reduced by makeinfo not putting quotation marks
  581. from @samp, etc., into node names and index entries. */
  582. register int i;
  583. text_buffer_init (&message);
  584. text_buffer_add_char (&message, '\n');
  585. text_buffer_printf (&message, _("Index entries containing "
  586. "'%s':\n"), index_search);
  587. text_buffer_printf (&message, "\n* Menu:");
  588. text_buffer_add_string (&message, "\0\b[index\0\b]", 11);
  589. text_buffer_add_char (&message, '\n');
  590. for (i = 0; apropos_list[i]; i++)
  591. {
  592. int line_start = text_buffer_off (&message);
  593. char *filename;
  594. /* Remove file extension. */
  595. filename = program_name_from_file_name
  596. (apropos_list[i]->filename);
  597. /* The label might be identical to that of another index
  598. entry in another Info file. Therefore, we make the file
  599. name part of the menu entry, to make them all distinct. */
  600. text_buffer_printf (&message, "* %s [%s]: ",
  601. apropos_list[i]->label, filename);
  602. while (text_buffer_off (&message) - line_start < 40)
  603. text_buffer_add_char (&message, ' ');
  604. text_buffer_printf (&message, "(%s)%s.",
  605. filename, apropos_list[i]->nodename);
  606. text_buffer_printf (&message, " (line %ld)\n",
  607. apropos_list[i]->line_number);
  608. free (filename);
  609. }
  610. }
  611. apropos_node = text_buffer_to_node (&message);
  612. {
  613. char *old_contents = apropos_node->contents;
  614. scan_node_contents (apropos_node, 0, 0);
  615. if (old_contents != apropos_node->contents)
  616. free (old_contents);
  617. }
  618. name_internal_node (apropos_node, xstrdup (apropos_list_nodename));
  619. /* Find/Create a window to contain this node. */
  620. {
  621. WINDOW *new;
  622. NODE *node;
  623. /* If a window is visible and showing an apropos list already,
  624. re-use it. */
  625. for (new = windows; new; new = new->next)
  626. {
  627. node = new->node;
  628. if (internal_info_node_p (node) &&
  629. (strcmp (node->nodename, apropos_list_nodename) == 0))
  630. break;
  631. }
  632. /* If we couldn't find an existing window, try to use the next window
  633. in the chain. */
  634. if (!new && window->next)
  635. new = window->next;
  636. /* If we still don't have a window, make a new one to contain
  637. the list. */
  638. if (!new)
  639. new = window_make_window ();
  640. /* If we couldn't make a new window, use this one. */
  641. if (!new)
  642. new = window;
  643. /* Lines do not wrap in this window. */
  644. new->flags |= W_NoWrap;
  645. info_set_node_of_window (new, apropos_node);
  646. active_window = new;
  647. }
  648. free (apropos_list);
  649. }
  650. }
  651. #define NODECOL 41
  652. #define LINECOL 62
  653. static void
  654. format_reference (REFERENCE *ref, const char *filename, struct text_buffer *buf)
  655. {
  656. size_t n;
  657. n = text_buffer_printf (buf, "* %s: ", ref->label);
  658. if (n < NODECOL)
  659. n += text_buffer_fill (buf, ' ', NODECOL - n);
  660. if (ref->filename && strcmp (ref->filename, filename))
  661. n += text_buffer_printf (buf, "(%s)", ref->filename);
  662. n += text_buffer_printf (buf, "%s. ", ref->nodename);
  663. if (n < LINECOL)
  664. n += text_buffer_fill (buf, ' ', LINECOL - n);
  665. else
  666. {
  667. text_buffer_add_char (buf, '\n');
  668. text_buffer_fill (buf, ' ', LINECOL);
  669. }
  670. text_buffer_printf (buf, "(line %4d)\n", ref->line_number);
  671. }
  672. NODE *
  673. create_virtual_index (FILE_BUFFER *file_buffer, char *index_search)
  674. {
  675. struct text_buffer text;
  676. int i;
  677. size_t cnt;
  678. NODE *node;
  679. text_buffer_init (&text);
  680. text_buffer_printf (&text,
  681. "File: %s, Node: Index for '%s'\n\n",
  682. file_buffer->filename, index_search);
  683. text_buffer_printf (&text, _("Virtual Index\n"
  684. "*************\n\n"
  685. "Index entries that match '%s':\n"),
  686. index_search);
  687. text_buffer_add_string (&text, "\0\b[index\0\b]", 11);
  688. text_buffer_printf (&text, "\n* Menu:\n\n");
  689. cnt = 0;
  690. index_offset = 0;
  691. index_initial = 0;
  692. index_partial = 0;
  693. while (1)
  694. {
  695. REFERENCE *result;
  696. int match_offset;
  697. result = next_index_match (file_buffer, index_search, index_offset, 1,
  698. &i, &match_offset);
  699. if (!result)
  700. break;
  701. format_reference (index_index[i],
  702. file_buffer->filename, &text);
  703. cnt++;
  704. }
  705. text_buffer_add_char (&text, '\0');
  706. if (cnt == 0)
  707. {
  708. text_buffer_free (&text);
  709. return 0;
  710. }
  711. node = info_create_node ();
  712. asprintf (&node->nodename, "Index for '%s'", index_search);
  713. node->fullpath = file_buffer->filename;
  714. node->contents = text_buffer_base (&text);
  715. node->nodelen = text_buffer_off (&text) - 1;
  716. node->body_start = strcspn (node->contents, "\n");
  717. node->flags |= N_IsInternal | N_WasRewritten;
  718. scan_node_contents (node, 0, 0);
  719. return node;
  720. }
  721. DECLARE_INFO_COMMAND (info_virtual_index,
  722. _("List all matches of a string in the index"))
  723. {
  724. char *prompt, *line;
  725. FILE_BUFFER *fb;
  726. NODE *node;
  727. fb = file_buffer_of_window (window);
  728. if (!initial_index_filename ||
  729. !fb ||
  730. (FILENAME_CMP (initial_index_filename, fb->filename) != 0))
  731. {
  732. window_message_in_echo_area (_("Finding index entries..."));
  733. info_indices_of_file_buffer (fb);
  734. }
  735. if (!index_index)
  736. {
  737. info_error (_("No indices found."));
  738. return;
  739. }
  740. /* Default to last search if there is one. */
  741. if (index_search)
  742. asprintf (&prompt, "%s [%s]: ", _("Index topic"), index_search);
  743. else
  744. asprintf (&prompt, "%s: ", _("Index topic"));
  745. line = info_read_maybe_completing (prompt, index_index);
  746. free (prompt);
  747. /* User aborted? */
  748. if (!line)
  749. {
  750. info_abort_key (window, 1);
  751. return;
  752. }
  753. if (*line)
  754. {
  755. free (index_search);
  756. index_search = line;
  757. }
  758. else if (!index_search)
  759. {
  760. free (line);
  761. return; /* No previous search string, and no string given. */
  762. }
  763. node = create_virtual_index (fb, index_search);
  764. if (!node)
  765. {
  766. info_error (_("No index entries containing '%s'."), index_search);
  767. return;
  768. }
  769. info_set_node_of_window (window, node);
  770. }