1 ## Copyright (C) 2009-2012 Søren Hauberg
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Command} {} lookfor @var{str}
21 ## @deftypefnx {Command} {} lookfor -all @var{str}
22 ## @deftypefnx {Function File} {[@var{func}, @var{helpstring}] =} lookfor (@var{str})
23 ## @deftypefnx {Function File} {[@var{func}, @var{helpstring}] =} lookfor ('-all', @var{str})
24 ## Search for the string @var{str} in all functions found in the current
25 ## function search path. By default, @code{lookfor} searches for @var{str}
26 ## in the first sentence of the help string of each function found. The entire
27 ## help text of each function can be searched if the '-all' argument is
28 ## supplied. All searches are case insensitive.
30 ## Called with no output arguments, @code{lookfor} prints the list of
31 ## matching functions to the terminal. Otherwise, the output arguments
32 ## @var{func} and @var{helpstring} define the matching functions and the
33 ## first sentence of each of their help strings.
35 ## The ability of @code{lookfor} to correctly identify the first
36 ## sentence of the help text is dependent on the format of the
37 ## function's help. All Octave core functions are correctly
38 ## formatted, but the same can not be guaranteed for external packages and
39 ## user-supplied functions. Therefore, the use of the '-all' argument may
40 ## be necessary to find related functions that are not a part of Octave.
41 ## @seealso{help, doc, which}
44 function [out_fun, out_help_text] = lookfor (str, arg2)
46 if (strcmpi (str, "-all"))
47 ## The difference between using '-all' and not, is which part of the caches
48 ## we search. The cache is organized such that the first column contains
49 ## the function name, the second column contains the full help text, and
50 ## the third column contains the first sentence of the help text.
52 search_type = 2; # when using caches, search the second column
54 search_type = 3; # when using caches, search the third column
56 str = lower (str); # Compare is case insensitive
58 ## Search functions, operators, and keywords that come with Octave
59 cache_file = doc_cache_file ();
60 if (exist (cache_file, "file"))
61 [fun, help_text] = search_cache (str, cache_file, search_type);
62 had_core_cache = true;
65 had_core_cache = false;
68 ## Search functions in new path dirs.
69 orig_path = strsplit (__pathorig__ (), pathsep ());
72 new_path = strsplit (path (), pathsep ());
74 ## scratch out directories already covered by orig_path.
76 new_path = setdiff (new_path, orig_path);
79 for n = 1:numel (new_path)
81 cache_file = fullfile (elt, "doc-cache");
82 if (exist (cache_file, "file"))
83 ## We have a cache in the directory, then read it and search it!
84 [funs, hts] = search_cache (str, cache_file, search_type);
85 fun(end+1:end+length (funs)) = funs;
86 help_text(end+1:end+length (hts)) = hts;
88 ## We don't have a cache. Search files
89 funs_in_f = __list_functions__ (elt);
90 for m = 1:length (funs_in_f)
93 ## Skip files that start with __
94 if (length (fn) > 2 && strcmp (fn(1:2), "__"))
98 ## Extract first sentence
100 warn_state = warning ();
103 first_sentence = get_first_help_sentence (fn, 1024);
105 unwind_protect_cleanup
106 warning (warn_state);
112 if (search_type == 2) # search entire help text
114 warn_state = warning ();
117 [text, fmt] = get_help_text (fn);
119 unwind_protect_cleanup
120 warning (warn_state);
126 ## Take action depending on help text fmt
131 [text, status] = __makeinfo__ (text, "plain text");
133 [text, status] = strip_html_tags (text);
138 elseif (status == 0) # only search the first sentence of the help text
139 text = first_sentence;
142 ## Search the help text, if we can
143 if (status == 0 && ! isempty (strfind (lower (text), str)))
145 help_text(end+1) = first_sentence;
152 ## Print the results (FIXME: it would be nice to break at word boundaries)
154 term_width = (terminal_size ())(2);
155 desc_width = term_width - indent - 2;
156 indent_space = blanks (indent);
157 for k = 1:length (fun)
159 f(end+1:indent-1) = " ";
162 desc = strtrim (strrep (help_text{k}, "\n", " "));
163 ldesc = length (desc);
164 printf ("%s\n", desc(1:min (ldesc, desc_width - (lf - indent))));
165 for start = (desc_width - (lf - indent) + 1):desc_width:ldesc
166 stop = min (start + desc_width, ldesc);
167 printf ("%s%s\n", indent_space, strtrim (desc (start:stop)));
172 ## Return the results instead of displaying them
174 out_help_text = help_text;
179 function [funs, help_texts] = search_cache (str, cache_file, search_type)
181 if (! isempty (cache))
182 t1 = strfind (lower (cache (1, :)), str);
183 t2 = strfind (lower (cache (search_type, :)), str);
184 cache_idx = find (! (cellfun ("isempty", t1) & cellfun ("isempty", t2)));
185 funs = cache(1, cache_idx);
186 help_texts = cache(3, cache_idx);
188 funs = help_texts = {};