annotate scripts/help/lookfor.m @ 20135:4c6d70a35192

lookfor.m: Overhaul function and fix bug #44924. * lookfor.m: Rewrite docstring. Match variable names in function prototype to docstring. Use more meaningful variable names throughout function. Don't continue to process a function if get_first_help_sentence returns an error (bug #44924). Iterate directly over cell list rather than using a for loop with a range to index into cell list.
author Rik <rik@octave.org>
date Sun, 26 Apr 2015 21:14:53 -0700
parents 4197fc428c7d
children 7503499a252b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 2009-2015 Søren Hauberg
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
2 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
3 ## This file is part of Octave.
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
4 ##
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
6 ## 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
7 ## 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
8 ## your option) any later version.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
9 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
12 ## 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
13 ## General Public License for more details.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
14 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 9724
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
18
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
19 ## -*- texinfo -*-
9724
f22bbc5d56e9 Fix various incorrect usages of TeXinfo deffn and deftypefn macros
Rik <rdrider0-list@yahoo.com>
parents: 9133
diff changeset
20 ## @deftypefn {Command} {} lookfor @var{str}
f22bbc5d56e9 Fix various incorrect usages of TeXinfo deffn and deftypefn macros
Rik <rdrider0-list@yahoo.com>
parents: 9133
diff changeset
21 ## @deftypefnx {Command} {} lookfor -all @var{str}
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
22 ## @deftypefnx {Function File} {[@var{fcn}, @var{help1str}] =} lookfor (@var{str})
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
23 ## @deftypefnx {Function File} {[@var{fcn}, @var{help1str}] =} lookfor ("-all", @var{str})
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
24 ## Search for the string @var{str} in all functions using the current function
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
25 ## search path.
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
26 ##
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
27 ## By default, @code{lookfor} looks for @var{str} in the first sentence of the
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
28 ## help string for each function found. The entire help text of each function
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
29 ## can be searched by using the @qcode{"-all"} argument. All searches are case
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
30 ## insensitive.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
31 ##
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
32 ## When called with no output arguments, @code{lookfor} prints the list of
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
33 ## matching functions to the terminal. Otherwise, the output argument
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
34 ## @var{fcns} contains the function names and @var{help1str} contains the first
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
35 ## sentence from the help string of each function.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
36 ##
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
37 ## Programming Note: The ability of @code{lookfor} to correctly identify the
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
38 ## first sentence of the help text is dependent on the format of the function's
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
39 ## help. All Octave core functions are correctly formatted, but the same can
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
40 ## not be guaranteed for external packages and user-supplied functions.
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
41 ## Therefore, the use of the @qcode{"-all"} argument may be necessary to find
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
42 ## related functions that are not a part of Octave.
9133
c0cef1436788 Update help text for sections 2.2 and 2.3 of basics.txi
Rik <rdrider0-list@yahoo.com>
parents: 9035
diff changeset
43 ## @seealso{help, doc, which}
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
44 ## @end deftypefn
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
45
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
46 function [fcn, help1str] = lookfor (str, arg2)
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
47
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
48 if (strcmpi (str, "-all"))
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
49 ## The difference between using '-all' and not is which part of the caches
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
50 ## we search. The cache is organized such that row
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
51 ## 1) contains the function name
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
52 ## 2) contains the full help text
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
53 ## 3) contains the first sentence of the help text.
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11104
diff changeset
54 str = arg2;
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
55 search_type = 2; # search the second row (full help text)
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
56 else
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
57 search_type = 3; # search the third column (first help sentence)
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
58 endif
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
59 str = lower (str); # Compare is case insensitive
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
60
8727
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
61 ## 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
62 cache_file = doc_cache_file ();
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
63 if (exist (cache_file, "file"))
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
64 [fcnlist, 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
65 had_core_cache = true;
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
66 else
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
67 fcnlist = help_text = {};
8727
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
68 had_core_cache = false;
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
69 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
70
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
71 ## Search functions in new path dirs.
16724
b7667fcb9fbc Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16403
diff changeset
72 orig_path = ostrsplit (__pathorig__ (), pathsep ());
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
73
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
74 ## ditto for path.
16724
b7667fcb9fbc Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16403
diff changeset
75 new_path = ostrsplit (path (), pathsep ());
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
76
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
77 ## remove directories already covered by orig_path.
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
78 if (had_core_cache)
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
79 new_path = setdiff (new_path, orig_path);
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
80 endif
8866
3149112b9412 improve lookfor performance
John W. Eaton <jwe@octave.org>
parents: 8861
diff changeset
81
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
82 for n = 1:numel (new_path)
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
83 fcndir = new_path{n};
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
84 cache_file = fullfile (fcndir, "doc-cache");
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
85 if (exist (cache_file, "file"))
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
86 ## We have a cache in the directory, then read it and search it!
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
87 [fcns, htext] = search_cache (str, cache_file, search_type);
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
88 fcnlist(end+1:end+length (fcns)) = fcns;
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
89 help_text(end+1:end+length (htext)) = htext;
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
90
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
91 else
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
92 ## We don't have a cache. Search files.
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
93 for fcn_in_fcndir = (__list_functions__ (fcndir)).'
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
94 fn = fcn_in_fcndir{1};
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
95
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
96 ## Skip files that start with "__"
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
97 if (strncmp (fn, "__", 2))
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
98 continue;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
99 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
100
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
101 status = 0;
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
102
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
103 ## Extract first sentence
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
104 try
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
105 warn_state = warning ();
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
106 unwind_protect
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
107 warning ("off");
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
108 first_sentence = get_first_help_sentence (fn, 1024);
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
109 unwind_protect_cleanup
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
110 warning (warn_state);
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
111 end_unwind_protect
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
112 catch
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
113 status = 1;
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
114 end_try_catch
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
115
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
116 if (status)
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
117 ## Error getting first help sentence
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
118 elseif (search_type == 3)
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
119 ## only search the first sentence of the help text
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
120 text = first_sentence;
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
121 elseif (search_type == 2)
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
122 ## search entire help text
8727
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
123 try
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
124 warn_state = warning ();
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
125 unwind_protect
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
126 warning ("off");
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
127 [text, fmt] = get_help_text (fn);
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
128 switch (lower (fmt))
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
129 case "plain text"
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
130 status = 0;
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
131 case "texinfo"
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
132 [text, status] = __makeinfo__ (text, "plain text");
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
133 case "html"
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
134 [text, status] = strip_html_tags (text);
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
135 otherwise
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
136 status = 1;
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
137 endswitch
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
138 unwind_protect_cleanup
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
139 warning (warn_state);
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
140 end_unwind_protect
8727
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
141 catch
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
142 status = 1;
5a5dbdacbf5d scripts/help/lookfor.m: Adapt to new cache scheme.
Soren Hauberg <hauberg@gmail.com>
parents: 8575
diff changeset
143 end_try_catch
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
144 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
145
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
146 ## Search the help text
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
147 if (status == 0 && strfind (lower (text), str))
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
148 fcnlist(end+1) = fn;
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
149 help_text(end+1) = first_sentence;
8870
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
150 endif
eea0e1b45ec0 optimize string manipulation in lookfor
Jaroslav Hajek <highegg@gmail.com>
parents: 8866
diff changeset
151 endfor
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
152 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
153 endfor
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
154
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
155 if (nargout == 0)
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
156 ## Print the results (FIXME: it would be nice to break at word boundaries)
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
157 indent = 20;
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
158 term_width = (terminal_size ())(2);
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
159 desc_width = term_width - indent - 2;
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
160 indent_space = blanks (indent);
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
161 for k = 1:length (fcnlist)
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
162 f = fcnlist{k};
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
163 f(end+1:indent-1) = " ";
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
164 puts ([f " "]);
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
165 lf = length (f);
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
166 desc = strtrim (strrep (help_text{k}, "\n", " "));
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
167 ldesc = length (desc);
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
168 printf ("%s\n", desc(1:min (ldesc, desc_width - (lf - indent))));
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
169 for start = (desc_width - (lf - indent) + 1):desc_width:ldesc
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
170 stop = min (start + desc_width, ldesc);
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
171 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
172 endfor
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
173 endfor
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
174 else
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
175 ## Return the results instead of displaying them
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
176 fcn = fcnlist;
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
177 help1str = help_text;
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
178 endif
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
179
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
180 endfunction
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
181
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
182 function [fcns, help_texts] = search_cache (str, cache_file, search_type)
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
183 load (cache_file);
8941
b8ed0262b11e lookfor.m: also match function names
John W. Eaton <jwe@octave.org>
parents: 8940
diff changeset
184 if (! isempty (cache))
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
185 t1 = strfind (lower (cache (1, :)), str);
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
186 t2 = strfind (lower (cache (search_type, :)), str);
8941
b8ed0262b11e lookfor.m: also match function names
John W. Eaton <jwe@octave.org>
parents: 8940
diff changeset
187 cache_idx = find (! (cellfun ("isempty", t1) & cellfun ("isempty", t2)));
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
188 fcns = cache(1, cache_idx);
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
189 help_texts = cache(3, cache_idx);
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
190 else
20135
4c6d70a35192 lookfor.m: Overhaul function and fix bug #44924.
Rik <rik@octave.org>
parents: 19697
diff changeset
191 fcns = help_texts = {};
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
192 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
193 endfunction
13772
ebefc477607b lookfor.m: Make search case insensitive.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
194