annotate scripts/help/lookfor.m @ 8941:b8ed0262b11e

lookfor.m: also match function names
author John W. Eaton <jwe@octave.org>
date Mon, 09 Mar 2009 14:52:59 -0400
parents 6994961bf1f4
children 57649dcecb55
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
1 ## Copyright (C) 2009 S�ren Hauberg
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
2 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify it
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
4 ## under the terms of the GNU General Public License as published by
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
5 ## the Free Software Foundation; either version 3 of the License, or (at
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
6 ## your option) any later version.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
7 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful, but
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
11 ## General Public License for more details.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
12 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
14 ## along with this program; see the file COPYING. If not, see
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
15 ## <http://www.gnu.org/licenses/>.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
16
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
17 ## -*- texinfo -*-
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
18 ## @deftypefn {Command} lookfor @var{str}
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
19 ## @deftypefnx {Command} lookfor -all @var{str}
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
20 ## @deftypefnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor (@var{str})
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
21 ## @deftypefnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor ('-all', @var{str})
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
22 ## Search for the string @var{str} in all of the functions found in the
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
23 ## function search path. By default @code{lookfor} searches for @var{str}
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
24 ## in the first sentence of the help string of each function found. The entire
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
25 ## help string of each function found in the path can be searched if
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
26 ## the '-all' argument is supplied. All searches are case insensitive.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
27 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
28 ## Called with no output arguments, @code{lookfor} prints the list of matching
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
29 ## functions to the terminal. Otherwise the output arguments @var{fun} and
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
30 ## @var{helpstring} define the matching functions and the first sentence of
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
31 ## each of their help strings.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
32 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
33 ## Note that the ability of @code{lookfor} to correctly identify the first
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
34 ## sentence of the help of the functions is dependent on the format of the
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
35 ## functions help. All of the functions in Octave itself will correctly
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
36 ## find the first sentence, but the same can not be guaranteed for other
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
37 ## functions. Therefore the use of the '-all' argument might be necessary
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
38 ## to find related functions that are not part of Octave.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
39 ## @seealso{help, which}
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
40 ## @end deftypefn
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
41
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
42 function [out_fun, out_help_text] = lookfor (str, extra)
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
43 if (strcmpi (str, "-all"))
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
44 ## The difference between using '-all' and not, is which part of the caches
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
45 ## we search. The cache is organised such that its first column contains
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
46 ## the function name, its second column contains the full help text, and its
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
47 ## third column contains the first sentence of the help text.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
48 str = extra;
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
49 search_type = 2; # when using caches, search its second column
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
50 else
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
51 search_type = 3; # when using caches, search its third column
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
52 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
53 str = lower (str);
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
54
8727
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
55 ## Search functions, operators, and keywords that come with Octave
8861
31f864877246 doc and lookfor fixes
John W. Eaton <jwe@octave.org>
parents: 8768
diff changeset
56 cache_file = doc_cache_file ();
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
57 if (exist (cache_file, "file"))
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
58 [fun, help_text] = search_cache (str, cache_file, search_type);
8727
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
59 had_core_cache = true;
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
60 else
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
61 fun = help_text = {};
8727
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
62 had_core_cache = false;
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
63 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
64
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
65 ## Search functions in new path dirs.
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents: 8870
diff changeset
66 orig_path = strsplit (__pathorig__ (), pathsep ());
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
67
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
68 ## ditto for path.
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents: 8870
diff changeset
69 new_path = strsplit (path (), pathsep ());
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
70
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
71 ## scratch out directories already covered by orig_path.
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
72 if (had_core_cache)
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
73 new_path = setdiff (new_path, orig_path);
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
74 endif
8866
3149112b9412 improve lookfor performance
John W. Eaton <jwe@octave.org>
parents: 8861
diff changeset
75
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
76 for n = 1:numel (new_path)
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
77 elt = new_path{n};
8940
6994961bf1f4 use doc-cache instead of DOC for doc cache file name
John W. Eaton <jwe@octave.org>
parents: 8877
diff changeset
78 cache_file = fullfile (elt, "doc-cache");
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
79 if (exist (cache_file, "file"))
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
80 ## We have a cache in the directory, then read it and search it!
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
81 [funs, hts] = search_cache (str, cache_file, search_type);
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
82 fun (end+1:end+length (funs)) = funs;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
83 help_text (end+1:end+length (hts)) = hts;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
84 else
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
85 ## We don't have a cache. Search files
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
86 funs_in_f = __list_functions__ (elt);
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
87 for m = 1:length (funs_in_f)
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
88 fn = funs_in_f {m};
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
89
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
90 ## Skip files that start with __
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
91 if (length (fn) > 2 && strcmp (fn (1:2), "__"))
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
92 continue;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
93 endif
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
94
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
95 ## Extract first sentence
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
96 try
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
97 warn_state = warning ();
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
98 unwind_protect
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
99 warning ("off");
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
100 first_sentence = get_first_help_sentence (fn);
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
101 status = 0;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
102 unwind_protect_cleanup
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
103 warning (warn_state);
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
104 end_unwind_protect
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
105 catch
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
106 status = 1;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
107 end_try_catch
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
108
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
109 if (search_type == 2) # search entire help text
8727
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
110 try
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
111 warn_state = warning ();
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
112 unwind_protect
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
113 warning ("off");
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
114 [text, fmt] = get_help_text (fn);
8866
3149112b9412 improve lookfor performance
John W. Eaton <jwe@octave.org>
parents: 8861
diff changeset
115 status = 0;
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
116 unwind_protect_cleanup
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
117 warning (warn_state);
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
118 end_unwind_protect
8727
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
119 catch
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
120 status = 1;
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
121 end_try_catch
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
122
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
123 ## Take action depending on help text fmt
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
124 switch (lower (fmt))
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
125 case "plain text"
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
126 status = 0;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
127 case "texinfo"
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
128 [text, status] = __makeinfo__ (text, "plain text");
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
129 case "html"
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
130 [text, status] = strip_html_tags (text);
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
131 otherwise
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
132 status = 1;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
133 endswitch
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
134
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
135 elseif (status == 0) # only search the first sentence of the help text
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
136 text = first_sentence;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
137 endif
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
138
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
139 ## Search the help text, if we can
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
140 if (status == 0 && !isempty (strfind (text, str)))
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
141 fun (end+1) = fn;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
142 help_text (end+1) = first_sentence;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
143 endif
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
144 endfor
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
145 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
146 endfor
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
147
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
148 if (nargout == 0)
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
149 ## Print the results (FIXME: improve this to make it look better.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
150 indent = 20;
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
151 term_width = terminal_size() (2);
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
152 desc_width = term_width - indent - 2;
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
153 indent_space = repmat (" ", 1, indent);
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
154 for k = 1:length (fun)
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
155 f = fun {k};
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
156 f (end+1:indent) = " ";
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
157 printf (f);
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
158 desc = strtrim (strrep (help_text {k}, "\n", " "));
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
159 ldesc = length (desc);
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
160 printf ("%s\n", desc (1:min (desc_width, ldesc)));
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
161 for start = desc_width+1:desc_width:ldesc
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
162 stop = min (start + desc_width, ldesc);
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
163 printf ("%s%s\n", indent_space, strtrim (desc (start:stop)));
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
164 endfor
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
165 endfor
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
166
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
167 else
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
168 ## Return the results instead of displaying them
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
169 out_fun = fun;
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
170 out_help_text = help_text;
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
171 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
172 endfunction
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
173
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
174 function [funs, help_texts] = search_cache (str, cache_file, search_type)
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
175 load (cache_file);
8941
b8ed0262b11e lookfor.m: also match function names
John W. Eaton <jwe@octave.org>
parents: 8940
diff changeset
176 if (! isempty (cache))
b8ed0262b11e lookfor.m: also match function names
John W. Eaton <jwe@octave.org>
parents: 8940
diff changeset
177 t1 = strfind (cache (1, :), str);
b8ed0262b11e lookfor.m: also match function names
John W. Eaton <jwe@octave.org>
parents: 8940
diff changeset
178 t2 = strfind (cache (search_type, :), str);
b8ed0262b11e lookfor.m: also match function names
John W. Eaton <jwe@octave.org>
parents: 8940
diff changeset
179 cache_idx = find (! (cellfun ("isempty", t1) & cellfun ("isempty", t2)));
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
180 funs = cache (1, cache_idx);
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
181 help_texts = cache (3, cache_idx);
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
182 else
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
183 funs = help_texts = {};
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
184 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
185 endfunction