comparison scripts/help/lookfor.m @ 8575:f134925a1cfa

m-file implementation of help system
author Soren Hauberg <soren@hauberg.org>
date Thu, 22 Jan 2009 18:22:52 -0500
parents
children 5a5dbdacbf5d
comparison
equal deleted inserted replaced
8574:83b8c739d626 8575:f134925a1cfa
1 ## Copyright (C) 2009 Søren Hauberg
2 ##
3 ## This program is free software; you can redistribute it and/or modify it
4 ## under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or (at
6 ## your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ## General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; see the file COPYING. If not, see
15 ## <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Command} lookfor @var{str}
19 ## @deftypefnx {Command} lookfor -all @var{str}
20 ## @deftypefnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor (@var{str})
21 ## @deftypefnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor ('-all', @var{str})
22 ## Search for the string @var{str} in all of the functions found in the
23 ## function search path. By default @code{lookfor} searches for @var{str}
24 ## in the first sentence of the help string of each function found. The entire
25 ## help string of each function found in the path can be searched if
26 ## the '-all' argument is supplied. All searches are case insensitive.
27 ##
28 ## Called with no output arguments, @code{lookfor} prints the list of matching
29 ## functions to the terminal. Otherwise the output arguments @var{fun} and
30 ## @var{helpstring} define the matching functions and the first sentence of
31 ## each of their help strings.
32 ##
33 ## Note that the ability of @code{lookfor} to correctly identify the first
34 ## sentence of the help of the functions is dependent on the format of the
35 ## functions help. All of the functions in Octave itself will correctly
36 ## find the first sentence, but the same can not be guaranteed for other
37 ## functions. Therefore the use of the '-all' argument might be necessary
38 ## to find related functions that are not part of Octave.
39 ## @seealso{help, which}
40 ## @end deftypefn
41
42 ## PKG_ADD: mark_as_command lookfor
43
44 function [out_fun, out_help_text] = lookfor (str, extra)
45 if (strcmpi (str, "-all"))
46 ## The difference between using '-all' and not, is which part of the caches
47 ## we search. The cache is organised such that its first column contains
48 ## the function name, its second column contains the full help text, and its
49 ## third column contains the first sentence of the help text.
50 str = extra;
51 search_type = 2; # when using caches, search its second column
52 else
53 search_type = 3; # when using caches, search its third column
54 endif
55 str = lower (str);
56
57 ## Search operators, keywords, and built-ins
58 cache_file = fullfile (octave_config_info.datadir, "builtin_cache.mat");
59 if (exist (cache_file, "file"))
60 [fun, help_text] = search_cache (str, cache_file, search_type);
61 else
62 fun = help_text = {};
63 endif
64
65 ## Search functions in path
66 p = path ();
67 idx = find (p == pathsep ());
68 prev_idx = 1;
69 for n = 1:length (idx)
70 f = p (prev_idx:idx (n)-1);
71 cache_file = fullfile (f, "help_cache.mat");
72 if (exist (cache_file, "file"))
73 ## We have a cache. Read it and search it!
74 [funs, hts] = search_cache (str, cache_file, search_type);
75 fun (end+1:end+length (funs)) = funs;
76 help_text (end+1:end+length (hts)) = hts;
77 else
78 ## We don't have a cache. Search files
79 funs_in_f = __list_functions__ (f);
80 for m = 1:length (funs_in_f)
81 fn = funs_in_f {m};
82
83 ## Skip files that start with __
84 if (length (fn) > 2 && strcmp (fn (1:2), "__"))
85 continue;
86 endif
87
88 ## Extract first sentence
89 try
90 first_sentence = get_first_help_sentence (fn);
91 status = 0;
92 catch
93 status = 1;
94 end_try_catch
95
96 if (search_type == 2) # search entire help text
97 [text, format] = get_help_text (fn);
98
99 ## Take action depending on help text format
100 switch (lower (format))
101 case "plain text"
102 status = 0;
103 case "texinfo"
104 [text, status] = makeinfo (text, "plain text");
105 case "html"
106 [text, status] = strip_html_tags (text);
107 otherwise
108 status = 1;
109 endswitch
110
111 elseif (status == 0) # only search the first sentence of the help text
112 text = first_sentence;
113 endif
114
115 ## Search the help text, if we can
116 if (status == 0 && !isempty (strfind (text, str)))
117 fun (end+1) = fn;
118 help_text (end+1) = first_sentence;
119 endif
120 endfor
121 endif
122 prev_idx = idx (n) + 1;
123 endfor
124
125 if (nargout == 0)
126 ## Print the results (FIXME: improve this to make it look better.
127 indent = 20;
128 term_width = terminal_size() (2);
129 desc_width = term_width - indent - 2;
130 indent_space = repmat (" ", 1, indent);
131 for k = 1:length (fun)
132 f = fun {k};
133 f (end+1:indent) = " ";
134 printf (f);
135 desc = strtrim (strrep (help_text {k}, "\n", " "));
136 ldesc = length (desc);
137 printf ("%s\n", desc (1:min (desc_width, ldesc)));
138 for start = desc_width+1:desc_width:ldesc
139 stop = min (start + desc_width, ldesc);
140 printf ("%s%s\n", indent_space, strtrim (desc (start:stop)));
141 endfor
142 endfor
143
144 else
145 ## Return the results instead of displaying them
146 out_fun = fun;
147 out_help_text = help_text;
148 endif
149 endfunction
150
151 function [funs, help_texts] = search_cache (str, cache_file, search_type)
152 load (cache_file);
153 if (! isempty(cache))
154 tmp = strfind (cache (search_type, :), str);
155 cache_idx = find (!cellfun ("isempty", tmp));
156 funs = cache (1, cache_idx);
157 help_texts = cache (3, cache_idx);
158 else
159 funs = help_texts = {};
160 endif
161 endfunction
162