comparison scripts/help/lookfor.m @ 13772:ebefc477607b

lookfor.m: Make search case insensitive. * lookfor.m: Make search case insensitive.
author Rik <octave@nomad.inbox5.com>
date Sun, 30 Oct 2011 09:14:07 -0700
parents c792872f8942
children 72c96de7a403
comparison
equal deleted inserted replaced
13771:80b30e186b73 13772:ebefc477607b
40 ## be necessary to find related functions that are not a part of Octave. 40 ## be necessary to find related functions that are not a part of Octave.
41 ## @seealso{help, doc, which} 41 ## @seealso{help, doc, which}
42 ## @end deftypefn 42 ## @end deftypefn
43 43
44 function [out_fun, out_help_text] = lookfor (str, arg2) 44 function [out_fun, out_help_text] = lookfor (str, arg2)
45
45 if (strcmpi (str, "-all")) 46 if (strcmpi (str, "-all"))
46 ## The difference between using '-all' and not, is which part of the caches 47 ## 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 ## we search. The cache is organized such that the first column contains
48 ## the function name, its second column contains the full help text, and its 49 ## the function name, the second column contains the full help text, and
49 ## third column contains the first sentence of the help text. 50 ## the third column contains the first sentence of the help text.
50 str = arg2; 51 str = arg2;
51 search_type = 2; # when using caches, search its second column 52 search_type = 2; # when using caches, search the second column
52 else 53 else
53 search_type = 3; # when using caches, search its third column 54 search_type = 3; # when using caches, search the third column
54 endif 55 endif
55 str = lower (str); 56 str = lower (str); # Compare is case insensitive
56 57
57 ## Search functions, operators, and keywords that come with Octave 58 ## Search functions, operators, and keywords that come with Octave
58 cache_file = doc_cache_file (); 59 cache_file = doc_cache_file ();
59 if (exist (cache_file, "file")) 60 if (exist (cache_file, "file"))
60 [fun, help_text] = search_cache (str, cache_file, search_type); 61 [fun, help_text] = search_cache (str, cache_file, search_type);
79 elt = new_path{n}; 80 elt = new_path{n};
80 cache_file = fullfile (elt, "doc-cache"); 81 cache_file = fullfile (elt, "doc-cache");
81 if (exist (cache_file, "file")) 82 if (exist (cache_file, "file"))
82 ## We have a cache in the directory, then read it and search it! 83 ## We have a cache in the directory, then read it and search it!
83 [funs, hts] = search_cache (str, cache_file, search_type); 84 [funs, hts] = search_cache (str, cache_file, search_type);
84 fun (end+1:end+length (funs)) = funs; 85 fun(end+1:end+length (funs)) = funs;
85 help_text (end+1:end+length (hts)) = hts; 86 help_text(end+1:end+length (hts)) = hts;
86 else 87 else
87 ## We don't have a cache. Search files 88 ## We don't have a cache. Search files
88 funs_in_f = __list_functions__ (elt); 89 funs_in_f = __list_functions__ (elt);
89 for m = 1:length (funs_in_f) 90 for m = 1:length (funs_in_f)
90 fn = funs_in_f {m}; 91 fn = funs_in_f{m};
91 92
92 ## Skip files that start with __ 93 ## Skip files that start with __
93 if (length (fn) > 2 && strcmp (fn (1:2), "__")) 94 if (length (fn) > 2 && strcmp (fn(1:2), "__"))
94 continue; 95 continue;
95 endif 96 endif
96 97
97 ## Extract first sentence 98 ## Extract first sentence
98 try 99 try
99 warn_state = warning (); 100 warn_state = warning ();
100 unwind_protect 101 unwind_protect
101 warning ("off"); 102 warning ("off");
102 first_sentence = get_first_help_sentence (fn); 103 first_sentence = get_first_help_sentence (fn, 1024);
103 status = 0; 104 status = 0;
104 unwind_protect_cleanup 105 unwind_protect_cleanup
105 warning (warn_state); 106 warning (warn_state);
106 end_unwind_protect 107 end_unwind_protect
107 catch 108 catch
137 elseif (status == 0) # only search the first sentence of the help text 138 elseif (status == 0) # only search the first sentence of the help text
138 text = first_sentence; 139 text = first_sentence;
139 endif 140 endif
140 141
141 ## Search the help text, if we can 142 ## Search the help text, if we can
142 if (status == 0 && !isempty (strfind (text, str))) 143 if (status == 0 && ! isempty (strfind (lower (text), str)))
143 fun (end+1) = fn; 144 fun(end+1) = fn;
144 help_text (end+1) = first_sentence; 145 help_text(end+1) = first_sentence;
145 endif 146 endif
146 endfor 147 endfor
147 endif 148 endif
148 endfor 149 endfor
149 150
150 if (nargout == 0) 151 if (nargout == 0)
151 ## Print the results (FIXME: improve this to make it look better. 152 ## Print the results (FIXME: it would be nice to break at word boundaries)
152 indent = 20; 153 indent = 20;
153 term_width = terminal_size() (2); 154 term_width = (terminal_size ())(2);
154 desc_width = term_width - indent - 2; 155 desc_width = term_width - indent - 2;
155 indent_space = repmat (" ", 1, indent); 156 indent_space = blanks (indent);
156 for k = 1:length (fun) 157 for k = 1:length (fun)
157 f = fun {k}; 158 f = fun{k};
158 f (end+1:indent) = " "; 159 f(end+1:indent-1) = " ";
159 printf (f); 160 puts ([f " "]);
160 desc = strtrim (strrep (help_text {k}, "\n", " ")); 161 lf = length (f);
162 desc = strtrim (strrep (help_text{k}, "\n", " "));
161 ldesc = length (desc); 163 ldesc = length (desc);
162 printf ("%s\n", desc (1:min (desc_width, ldesc))); 164 printf ("%s\n", desc(1:min (ldesc, desc_width - (lf - indent))));
163 for start = desc_width+1:desc_width:ldesc 165 for start = (desc_width - (lf - indent) + 1):desc_width:ldesc
164 stop = min (start + desc_width, ldesc); 166 stop = min (start + desc_width, ldesc);
165 printf ("%s%s\n", indent_space, strtrim (desc (start:stop))); 167 printf ("%s%s\n", indent_space, strtrim (desc (start:stop)));
166 endfor 168 endfor
167 endfor 169 endfor
168 170
169 else 171 else
170 ## Return the results instead of displaying them 172 ## Return the results instead of displaying them
171 out_fun = fun; 173 out_fun = fun;
172 out_help_text = help_text; 174 out_help_text = help_text;
173 endif 175 endif
176
174 endfunction 177 endfunction
175 178
176 function [funs, help_texts] = search_cache (str, cache_file, search_type) 179 function [funs, help_texts] = search_cache (str, cache_file, search_type)
177 load (cache_file); 180 load (cache_file);
178 if (! isempty (cache)) 181 if (! isempty (cache))
179 t1 = strfind (cache (1, :), str); 182 t1 = strfind (lower (cache (1, :)), str);
180 t2 = strfind (cache (search_type, :), str); 183 t2 = strfind (lower (cache (search_type, :)), str);
181 cache_idx = find (! (cellfun ("isempty", t1) & cellfun ("isempty", t2))); 184 cache_idx = find (! (cellfun ("isempty", t1) & cellfun ("isempty", t2)));
182 funs = cache (1, cache_idx); 185 funs = cache(1, cache_idx);
183 help_texts = cache (3, cache_idx); 186 help_texts = cache(3, cache_idx);
184 else 187 else
185 funs = help_texts = {}; 188 funs = help_texts = {};
186 endif 189 endif
187 endfunction 190 endfunction
191