Mercurial > hg > octave-lyh
annotate src/help.cc @ 8041:a14bdf90be55
Add a search for Contents.m files to the help function
author | David Bateman <dbateman@free.fr> |
---|---|
date | Tue, 19 Aug 2008 16:15:52 -0400 |
parents | 85184151822e |
children | faf0abc5fd51 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
4 2002, 2003, 2004, 2005, 2006, 2007 John W. Eaton | |
1 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
22 */ | |
23 | |
240 | 24 #ifdef HAVE_CONFIG_H |
1192 | 25 #include <config.h> |
1 | 26 #endif |
27 | |
1343 | 28 #include <cstdlib> |
29 #include <cstring> | |
30 | |
5769 | 31 #include <algorithm> |
3503 | 32 #include <iostream> |
33 #include <fstream> | |
5765 | 34 #include <sstream> |
1755 | 35 #include <string> |
36 | |
1350 | 37 #ifdef HAVE_UNISTD_H |
2442 | 38 #ifdef HAVE_SYS_TYPES_H |
1295 | 39 #include <sys/types.h> |
2442 | 40 #endif |
1295 | 41 #include <unistd.h> |
42 #endif | |
1343 | 43 |
3295 | 44 #include "cmd-edit.h" |
45 #include "file-ops.h" | |
6253 | 46 #include "file-stat.h" |
2926 | 47 #include "oct-env.h" |
1755 | 48 #include "str-vec.h" |
49 | |
2492 | 50 #include <defaults.h> |
1352 | 51 #include "defun.h" |
52 #include "dirfns.h" | |
53 #include "error.h" | |
2202 | 54 #include "gripes.h" |
1352 | 55 #include "help.h" |
2177 | 56 #include "input.h" |
5832 | 57 #include "load-path.h" |
1755 | 58 #include "oct-obj.h" |
2976 | 59 #include "ov-usr-fcn.h" |
1352 | 60 #include "pager.h" |
3018 | 61 #include "parse.h" |
1466 | 62 #include "pathsearch.h" |
3295 | 63 #include "procstream.h" |
7336 | 64 #include "pt-pr-code.h" |
529 | 65 #include "sighandlers.h" |
66 #include "symtab.h" | |
2694 | 67 #include "syswait.h" |
1755 | 68 #include "toplev.h" |
242 | 69 #include "utils.h" |
1352 | 70 #include "variables.h" |
3301 | 71 #include "version.h" |
5447 | 72 #include "quit.h" |
529 | 73 |
2202 | 74 // Name of the info file specified on command line. |
75 // (--info-file file) | |
3523 | 76 std::string Vinfo_file; |
2202 | 77 |
78 // Name of the info reader we'd like to use. | |
79 // (--info-program program) | |
5794 | 80 std::string Vinfo_program; |
2202 | 81 |
3686 | 82 // Name of the makeinfo program to run. |
5794 | 83 static std::string Vmakeinfo_program = "makeinfo"; |
3686 | 84 |
2189 | 85 // If TRUE, don't print additional help message in help and usage |
86 // functions. | |
5794 | 87 static bool Vsuppress_verbose_help_message = false; |
2189 | 88 |
5775 | 89 // FIXME -- maybe this should use string instead of char*. |
3016 | 90 |
91 struct help_list | |
92 { | |
93 const char *name; | |
94 const char *help; | |
95 }; | |
96 | |
1 | 97 static help_list operators[] = |
98 { | |
99 { "!", | |
100 "Logical not operator. See also `~'.\n", }, | |
101 | |
102 { "!=", | |
103 "Logical not equals operator. See also `~' and `<>'.\n", }, | |
104 | |
105 { "\"", | |
106 "String delimiter.\n", }, | |
107 | |
108 { "#", | |
928 | 109 "Begin comment character. See also `%'.", }, |
1 | 110 |
111 { "%", | |
928 | 112 "Begin comment charcter. See also `#'.", }, |
1 | 113 |
114 { "&", | |
928 | 115 "Logical and operator. See also `&&'.", }, |
1 | 116 |
117 { "&&", | |
928 | 118 "Logical and operator. See also `&'.", }, |
1 | 119 |
120 { "'", | |
121 "Matrix transpose operator. For complex matrices, computes the\n\ | |
122 complex conjugate (Hermitian) transpose. See also `.''\n\ | |
123 \n\ | |
124 The single quote character may also be used to delimit strings, but\n\ | |
125 it is better to use the double quote character, since that is never\n\ | |
928 | 126 ambiguous", }, |
1 | 127 |
128 { "(", | |
928 | 129 "Array index or function argument delimiter.", }, |
1 | 130 |
131 { ")", | |
928 | 132 "Array index or function argument delimiter.", }, |
1 | 133 |
134 { "*", | |
928 | 135 "Multiplication operator. See also `.*'", }, |
1 | 136 |
137 { "**", | |
928 | 138 "Power operator. See also `^', `.**', and `.^'", }, |
1 | 139 |
140 { "+", | |
928 | 141 "Addition operator.", }, |
1 | 142 |
143 { "++", | |
5339 | 144 "Increment operator. As in C, may be applied as a prefix or postfix\n\ |
145 operator.", }, | |
1 | 146 |
147 { ",", | |
928 | 148 "Array index, function argument, or command separator.", }, |
1 | 149 |
150 { "-", | |
928 | 151 "Subtraction or unary negation operator.", }, |
1 | 152 |
153 { "--", | |
5339 | 154 "Decrement operator. As in C, may be applied as a prefix or postfix\n\ |
155 operator.", }, | |
1 | 156 |
157 { ".'", | |
158 "Matrix transpose operator. For complex matrices, computes the\n\ | |
928 | 159 transpose, *not* the complex conjugate transpose. See also `''.", }, |
1 | 160 |
161 { ".*", | |
928 | 162 "Element by element multiplication operator. See also `*'.", }, |
1 | 163 |
164 { ".**", | |
928 | 165 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1 | 166 |
167 { "./", | |
928 | 168 "Element by element division operator. See also `/' and `\\'.", }, |
1 | 169 |
170 { ".^", | |
928 | 171 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1 | 172 |
173 { "/", | |
928 | 174 "Right division. See also `\\' and `./'.", }, |
1 | 175 |
176 { ":", | |
928 | 177 "Select entire rows or columns of matrices.", }, |
1 | 178 |
179 { ";", | |
928 | 180 "Array row or command separator. See also `,'.", }, |
1 | 181 |
182 { "<", | |
928 | 183 "Less than operator.", }, |
1 | 184 |
185 { "<=", | |
928 | 186 "Less than or equals operator.", }, |
1 | 187 |
188 { "<>", | |
928 | 189 "Logical not equals operator. See also `!=' and `~='.", }, |
1 | 190 |
191 { "=", | |
928 | 192 "Assignment operator.", }, |
1 | 193 |
194 { "==", | |
928 | 195 "Equality test operator.", }, |
1 | 196 |
197 { ">", | |
928 | 198 "Greater than operator.", }, |
1 | 199 |
200 { ">=", | |
928 | 201 "Greater than or equals operator.", }, |
1 | 202 |
203 { "[", | |
928 | 204 "Return list delimiter. See also `]'.", }, |
1 | 205 |
206 { "\\", | |
928 | 207 "Left division operator. See also `/' and `./'.", }, |
1 | 208 |
209 { "]", | |
928 | 210 "Return list delimiter. See also `['.", }, |
1 | 211 |
212 { "^", | |
928 | 213 "Power operator. See also `**', `.^', and `.**.'", }, |
1 | 214 |
215 { "|", | |
928 | 216 "Logical or operator. See also `||'.", }, |
1 | 217 |
218 { "||", | |
928 | 219 "Logical or operator. See also `|'.", }, |
1 | 220 |
221 { "~", | |
928 | 222 "Logical not operator. See also `!' and `~'.", }, |
1 | 223 |
224 { "~=", | |
928 | 225 "Logical not equals operator. See also `<>' and `!='.", }, |
1 | 226 |
529 | 227 { 0, 0, }, |
1 | 228 }; |
229 | |
230 static help_list keywords[] = | |
231 { | |
232 { "break", | |
5818 | 233 "-*- texinfo -*-\n\ |
234 @deffn Keyword break\n\ | |
235 Exit the innermost enclosing do, while or for loop.\n\ | |
236 @seealso{do, while, for, continue}\n\ | |
237 @end deffn", }, | |
5040 | 238 |
239 { "case", | |
5818 | 240 "-*- texinfo -*-\n\ |
241 @deffn Keyword case @{@var{value}@}\n\ | |
242 A case statement in an switch. Octave cases are exclusive and do not\n\ | |
5040 | 243 fall-through as do C-language cases. A switch statement must have at least\n\ |
5818 | 244 one case. See @code{switch} for an example.\n\ |
245 @seealso{switch}\n\ | |
246 @end deffn", }, | |
1 | 247 |
1489 | 248 { "catch", |
5818 | 249 "-*- texinfo -*-\n\ |
250 @deffn Keyword catch\n\ | |
251 Begin the cleanup part of a try-catch block.\n\ | |
252 @seealso{try}\n\ | |
253 @end deffn", }, | |
1489 | 254 |
1 | 255 { "continue", |
5818 | 256 "-*- texinfo -*-\n\ |
257 @deffn Keyword continue\n\ | |
258 Jump to the end of the innermost enclosing do, while or for loop.\n\ | |
259 @seealso{do, while, for, break}\n\ | |
260 @end deffn", }, | |
5040 | 261 |
262 { "do", | |
5818 | 263 "-*- texinfo -*-\n\ |
264 @deffn Keyword do\n\ | |
265 Begin a do-until loop. This differs from a do-while loop in that the\n\ | |
266 body of the loop is executed at least once.\n\ | |
267 @seealso{while}\n\ | |
268 @end deffn", }, | |
1 | 269 |
270 { "else", | |
5818 | 271 "-*- texinfo -*-\n\ |
272 @deffn Keyword else\n\ | |
273 Alternate action for an if block. See @code{if} for an example.\n\ | |
274 @seealso{if}\n\ | |
275 @end deffn", }, | |
1 | 276 |
277 { "elseif", | |
5818 | 278 "-*- texinfo -*-\n\ |
279 @deffn Keyword elseif (@var{condition})\n\ | |
280 Alternate conditional test for an if block. See @code{if} for an example.\n\ | |
281 @seealso{if}\n\ | |
282 @end deffn", }, | |
1 | 283 |
284 { "end", | |
5818 | 285 "-*- texinfo -*-\n\ |
286 @deffn Keyword end\n\ | |
287 Mark the end of any @code{for}, @code{if}, @code{do}, @code{while}, or @code{function} block.\n\ | |
288 @seealso{for, if, do, while, function}\n\ | |
289 @end deffn", }, | |
928 | 290 |
1489 | 291 { "end_try_catch", |
5818 | 292 "-*- texinfo -*-\n\ |
293 @deffn Keyword end_try_catch\n\ | |
294 Mark the end of an @code{try-catch} block.\n\ | |
295 @seealso{try, catch}\n\ | |
296 @end deffn", }, | |
1489 | 297 |
928 | 298 { "end_unwind_protect", |
5818 | 299 "-*- texinfo -*-\n\ |
300 @deffn Keyword end_unwind_protect\n\ | |
301 Mark the end of an unwind_protect block.\n\ | |
302 @seealso{unwind_protect}\n\ | |
303 @end deffn", }, | |
1 | 304 |
305 { "endfor", | |
5818 | 306 "-*- texinfo -*-\n\ |
307 @deffn Keyword endfor\n\ | |
308 Mark the end of a for loop. See @code{for} for an example.\n\ | |
309 @seealso{for}\n\ | |
310 @end deffn", }, | |
1 | 311 |
312 { "endfunction", | |
5818 | 313 "-*- texinfo -*-\n\ |
314 @deffn Keyword endfunction\n\ | |
315 Mark the end of a function.\n\ | |
316 @seealso{function}\n\ | |
317 @end deffn", }, | |
1 | 318 |
319 { "endif", | |
5818 | 320 "-*- texinfo -*-\n\ |
321 @deffn Keyword endif\n\ | |
322 Mark the end of an if block. See @code{if} for an example.\n\ | |
323 @seealso{if}\n\ | |
324 @end deffn", }, | |
1 | 325 |
5122 | 326 { "endswitch", |
5818 | 327 "-*- texinfo -*-\n\ |
328 @deffn Keyword endswitch\n\ | |
329 Mark the end of a switch block. See @code{switch} for an example.\n\ | |
330 @seealso{switch}\n\ | |
331 @end deffn", }, | |
5122 | 332 |
1 | 333 { "endwhile", |
5818 | 334 "-*- texinfo -*-\n\ |
335 @deffn Keyword endwhile\n\ | |
336 Mark the end of a while loop. See @code{while} for an example.\n\ | |
337 @seealso{do, while}\n\ | |
338 @end deffn", }, | |
1 | 339 |
340 { "for", | |
5818 | 341 "-*- texinfo -*-\n\ |
342 @deffn Keyword for @var{i} = @var{range}\n\ | |
343 Begin a for loop.\n\ | |
344 @example\n\ | |
345 for i = 1:10\n\ | |
346 i\n\ | |
347 endfor\n\ | |
348 @end example\n\ | |
349 @seealso{do, while}\n\ | |
350 @end deffn", }, | |
1 | 351 |
352 { "function", | |
5818 | 353 "-*- texinfo -*-\n\ |
354 @deffn Keyword function @var{outputs} = function (@var{input}, ...)\n\ | |
355 @deffnx Keyword function {} function (@var{input}, ...)\n\ | |
356 @deffnx Keyword function @var{outputs} = function\n\ | |
357 Begin a function body with @var{outputs} as results and @var{inputs} as\n\ | |
358 parameters.\n\ | |
359 @seealso{return}\n\ | |
360 @end deffn", }, | |
1 | 361 |
362 { "global", | |
5818 | 363 "-*- texinfo -*-\n\ |
364 @deffn Keyword global\n\ | |
365 Declare variables to have global scope.\n\ | |
366 @example\n\ | |
367 global @var{x};\n\ | |
368 if isempty (@var{x})\n\ | |
369 x = 1;\n\ | |
370 endif\n\ | |
371 @end example\n\ | |
372 @seealso{persistent}\n\ | |
373 @end deffn", }, | |
1 | 374 |
375 { "if", | |
5393 | 376 "-*- texinfo -*-\n\ |
377 @deffn Keyword if (@var{cond}) @dots{} endif\n\ | |
378 @deffnx Keyword if (@var{cond}) @dots{} else @dots{} endif\n\ | |
379 @deffnx Keyword if (@var{cond}) @dots{} elseif (@var{cond}) @dots{} endif\n\ | |
380 @deffnx Keyword if (@var{cond}) @dots{} elseif (@var{cond}) @dots{} else @dots{} endif\n\ | |
381 Begin an if block.\n\ | |
5818 | 382 @example\n\ |
383 x = 1;\n\ | |
384 if (x == 1)\n\ | |
385 disp (\"one\");\n\ | |
386 elseif (x == 2)\n\ | |
387 disp (\"two\");\n\ | |
388 else\n\ | |
389 disp (\"not one or two\");\n\ | |
390 endif\n\ | |
391 @end example\n\ | |
5646 | 392 @seealso{switch}\n\ |
393 @end deffn", }, | |
1 | 394 |
5040 | 395 { "otherwise", |
5818 | 396 "-*- texinfo -*-\n\ |
397 @deffn Keyword otherwise\n\ | |
398 The default statement in a switch block (similar to else in an if block).\n\ | |
399 @seealso{switch}\n\ | |
400 @end deffn", }, | |
5040 | 401 |
4686 | 402 { "persistent", |
5818 | 403 "-*- texinfo -*-\n\ |
404 @deffn Keyword persistent @var{var}\n\ | |
405 Declare variables as persistent. A variable that has been declared\n\ | |
5623 | 406 persistent within a function will retain its contents in memory between\n\ |
407 subsequent calls to the same function. The difference between persistent\n\ | |
5818 | 408 variables and global variables is that persistent variables are local in \n\ |
409 scope to a particular function and are not visible elsewhere.\n\ | |
410 @seealso{global}\n\ | |
411 @end deffn", }, | |
4686 | 412 |
5040 | 413 { "replot", |
5818 | 414 "-*- texinfo -*-\n\ |
415 @deffn Keyword replot\n\ | |
416 Replot a graphic.\n\ | |
417 @seealso{plot}\n\ | |
418 @end deffn", }, | |
5040 | 419 |
1 | 420 { "return", |
5818 | 421 "-*- texinfo -*-\n\ |
422 @deffn Keyword return\n\ | |
423 Return from a function.\n\ | |
424 @seealso{function}\n\ | |
425 @end deffn", }, | |
928 | 426 |
5040 | 427 { "static", |
5818 | 428 "-*- texinfo -*-\n\ |
429 @deffn Keyword static\n\ | |
430 This function has been deprecated in favor of persistent.\n\ | |
431 @seealso{persistent}\n\ | |
432 @end deffn", }, | |
5040 | 433 |
434 { "switch", | |
5818 | 435 "-*- texinfo -*-\n\ |
436 @deffn Keyword switch @var{statement}\n\ | |
437 Begin a switch block.\n\ | |
438 @example\n\ | |
439 yesno = \"yes\"\n\ | |
440 \n\ | |
441 switch yesno\n\ | |
5832 | 442 case @{\"Yes\" \"yes\" \"YES\" \"y\" \"Y\"@}\n\ |
5818 | 443 value = 1;\n\ |
5832 | 444 case @{\"No\" \"no\" \"NO\" \"n\" \"N\"@}\n\ |
5818 | 445 value = 0;\n\ |
446 otherwise\n\ | |
447 error (\"invalid value\");\n\ | |
448 endswitch\n\ | |
449 @end example\n\ | |
450 @seealso{if, case, otherwise}\n\ | |
451 @end deffn", }, | |
5040 | 452 |
1489 | 453 { "try", |
5818 | 454 "-*- texinfo -*-\n\ |
455 @deffn Keyword try\n\ | |
456 Begin a try-catch block.\n\ | |
6138 | 457 \n\ |
458 If an error occurs within a try block, then the catch code will be run and\n\ | |
459 execution will proceed after the catch block (though it is often\n\ | |
460 recommended to use the lasterr function to re-throw the error after cleanup\n\ | |
461 is completed).\n\ | |
462 @seealso{catch,unwind_protect}\n\ | |
5818 | 463 @end deffn", }, |
1489 | 464 |
5040 | 465 { "until", |
5818 | 466 "-*- texinfo -*-\n\ |
467 @deffn Keyword until\n\ | |
468 End a do-until loop.\n\ | |
469 @seealso{do}\n\ | |
470 @end deffn", }, | |
5040 | 471 |
928 | 472 { "unwind_protect", |
5818 | 473 "-*- texinfo -*-\n\ |
474 @deffn Keyword unwind_protect\n\ | |
475 Begin an unwind_protect block.\n\ | |
6138 | 476 \n\ |
477 If an error occurs within the first part of an unwind_protect block\n\ | |
478 the commands within the unwind_protect_cleanup block are executed before\n\ | |
479 the error is thrown. If an error is not thrown, then the\n\ | |
480 unwind_protect_cleanup block is still executed (in other words, the\n\ | |
481 unwind_protect_cleanup will be run with or without an error in the\n\ | |
482 unwind_protect block).\n\ | |
483 @seealso{unwind_protect_cleanup,try}\n\ | |
5818 | 484 @end deffn", }, |
928 | 485 |
486 { "unwind_protect_cleanup", | |
5818 | 487 "-*- texinfo -*-\n\ |
488 @deffn Keyword unwind_protect_cleanup\n\ | |
489 Begin the cleanup section of an unwind_protect block.\n\ | |
490 @seealso{unwind_protect}\n\ | |
491 @end deffn", }, | |
1 | 492 |
5040 | 493 { "varargin", |
5818 | 494 "-*- texinfo -*-\n\ |
495 @deffn Keyword varargin\n\ | |
496 Pass an arbitrary number of arguments into a function.\n\ | |
497 @seealso{varargout, nargin, nargout}\n\ | |
498 @end deffn", }, | |
5040 | 499 |
500 { "varargout", | |
5818 | 501 "-*- texinfo -*-\n\ |
502 @deffn Keyword varargout\n\ | |
503 Pass an arbitrary number of arguments out of a function.\n\ | |
504 @seealso{varargin, nargin, nargout}\n\ | |
505 @end deffn", }, | |
5040 | 506 |
1 | 507 { "while", |
5818 | 508 "-*- texinfo -*-\n\ |
509 @deffn Keyword while\n\ | |
510 Begin a while loop.\n\ | |
511 @seealso{do}\n\ | |
512 @end deffn", }, | |
1 | 513 |
529 | 514 { 0, 0, }, |
1 | 515 }; |
516 | |
581 | 517 // Return a copy of the operator or keyword names. |
518 | |
3016 | 519 static string_vector |
3355 | 520 names (help_list *lst) |
1 | 521 { |
1755 | 522 string_vector retval; |
523 | |
3355 | 524 int count = 0; |
1 | 525 help_list *ptr = lst; |
529 | 526 while (ptr->name) |
1 | 527 { |
528 count++; | |
529 ptr++; | |
530 } | |
531 | |
1755 | 532 if (count > 0) |
533 { | |
534 retval.resize (count); | |
1 | 535 |
1755 | 536 ptr = lst; |
537 for (int i = 0; i < count; i++) | |
538 { | |
539 retval[i] = ptr->name; | |
540 ptr++; | |
541 } | |
1 | 542 } |
543 | |
1755 | 544 return retval; |
1 | 545 } |
546 | |
3014 | 547 static help_list * |
1 | 548 operator_help (void) |
549 { | |
550 return operators; | |
551 } | |
552 | |
3016 | 553 static help_list * |
1 | 554 keyword_help (void) |
555 { | |
556 return keywords; | |
557 } | |
558 | |
5775 | 559 // It's not likely that this does the right thing now. FIXME |
3016 | 560 |
561 string_vector | |
562 make_name_list (void) | |
563 { | |
3355 | 564 string_vector key = names (keyword_help ()); |
565 int key_len = key.length (); | |
3016 | 566 |
7336 | 567 string_vector bif = symbol_table::built_in_function_names (); |
568 int bif_len = bif.length (); | |
4009 | 569 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7438
diff
changeset
|
570 // FIXME -- is this really necessary here? |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7438
diff
changeset
|
571 string_vector glb = symbol_table::global_variable_names (); |
3355 | 572 int glb_len = glb.length (); |
3016 | 573 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7438
diff
changeset
|
574 // FIXME -- is this really necessary here? |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7438
diff
changeset
|
575 string_vector top = symbol_table::top_level_variable_names (); |
3355 | 576 int top_len = top.length (); |
3016 | 577 |
3355 | 578 string_vector lcl; |
7336 | 579 if (! symbol_table::at_top_level ()) |
580 lcl = symbol_table::variable_names (); | |
3355 | 581 int lcl_len = lcl.length (); |
3016 | 582 |
5832 | 583 string_vector ffl = load_path::fcn_names (); |
3016 | 584 int ffl_len = ffl.length (); |
585 | |
5592 | 586 string_vector afl = autoloaded_functions (); |
587 int afl_len = afl.length (); | |
588 | |
7336 | 589 int total_len = key_len + bif_len + glb_len + top_len + lcl_len |
590 + ffl_len + afl_len; | |
3016 | 591 |
592 string_vector list (total_len); | |
593 | |
594 // Put all the symbols in one big list. | |
595 | |
596 int j = 0; | |
597 int i = 0; | |
598 for (i = 0; i < key_len; i++) | |
599 list[j++] = key[i]; | |
600 | |
7336 | 601 for (i = 0; i < bif_len; i++) |
602 list[j++] = bif[i]; | |
4009 | 603 |
3016 | 604 for (i = 0; i < glb_len; i++) |
605 list[j++] = glb[i]; | |
606 | |
607 for (i = 0; i < top_len; i++) | |
608 list[j++] = top[i]; | |
609 | |
610 for (i = 0; i < lcl_len; i++) | |
611 list[j++] = lcl[i]; | |
612 | |
613 for (i = 0; i < ffl_len; i++) | |
614 list[j++] = ffl[i]; | |
615 | |
5592 | 616 for (i = 0; i < afl_len; i++) |
617 list[j++] = afl[i]; | |
618 | |
3016 | 619 return list; |
620 } | |
621 | |
3014 | 622 void |
3523 | 623 additional_help_message (std::ostream& os) |
2470 | 624 { |
625 if (! Vsuppress_verbose_help_message) | |
5641 | 626 os << "\ |
5794 | 627 Additional help for built-in functions and operators is\n\ |
628 available in the on-line version of the manual. Use the command\n\ | |
5672 | 629 `doc <topic>' to search the manual index.\n\ |
3295 | 630 \n\ |
3160 | 631 Help and information about Octave is also available on the WWW\n\ |
5041 | 632 at http://www.octave.org and via the help@octave.org\n\ |
3917 | 633 mailing list.\n"; |
3160 | 634 } |
635 | |
5775 | 636 // FIXME -- this needs a major overhaul to cope with new |
2976 | 637 // symbol table stuff. |
638 | |
529 | 639 static void |
3523 | 640 display_names_from_help_list (std::ostream& os, help_list *list, |
542 | 641 const char *desc) |
529 | 642 { |
3355 | 643 string_vector symbols = names (list); |
3259 | 644 |
1808 | 645 if (! symbols.empty ()) |
646 { | |
2095 | 647 os << "\n*** " << desc << ":\n\n"; |
3259 | 648 |
649 symbols.qsort (); | |
650 | |
2095 | 651 symbols.list_in_columns (os); |
1808 | 652 } |
529 | 653 } |
654 | |
655 static void | |
7336 | 656 display_symtab_names (std::ostream& os, const std::list<std::string>& names, |
3523 | 657 const std::string& desc) |
542 | 658 { |
1808 | 659 if (! names.empty ()) |
660 { | |
2095 | 661 os << "\n*** " << desc << ":\n\n"; |
7336 | 662 |
663 string_vector sv (names); | |
664 | |
665 sv.list_in_columns (os); | |
1808 | 666 } |
542 | 667 } |
668 | |
3014 | 669 static void |
670 simple_help (void) | |
671 { | |
3160 | 672 octave_stdout << "Help is available for the topics listed below.\n"; |
673 | |
674 additional_help_message (octave_stdout); | |
675 | |
3014 | 676 display_names_from_help_list (octave_stdout, operator_help (), |
677 "operators"); | |
678 | |
679 display_names_from_help_list (octave_stdout, keyword_help (), | |
680 "reserved words"); | |
681 | |
7336 | 682 display_symtab_names (octave_stdout, |
683 symbol_table::built_in_function_names (), | |
684 "built-in functions"); | |
542 | 685 |
7336 | 686 // FIXME -- list functions defined on command line? |
529 | 687 |
5832 | 688 load_path::display (octave_stdout); |
5592 | 689 |
690 string_vector autoloaded = autoloaded_functions (); | |
691 | |
692 if (! autoloaded.empty ()) | |
693 { | |
694 octave_stdout << "\n*** autoloaded functions:\n\n"; | |
695 | |
696 autoloaded.qsort (); | |
697 | |
698 autoloaded.list_in_columns (octave_stdout); | |
699 } | |
529 | 700 } |
701 | |
702 static int | |
3523 | 703 try_info (const std::string& nm) |
529 | 704 { |
5672 | 705 int retval = -1; |
1295 | 706 |
5672 | 707 warning ("please use `doc' instead of `help -i'"); |
529 | 708 |
5672 | 709 octave_value_list args; |
710 args(0) = nm; | |
711 octave_value_list result = feval ("doc", args, 1); | |
4051 | 712 |
5672 | 713 if (result.length () > 0) |
714 retval = result(0).int_value (); | |
529 | 715 |
5672 | 716 return retval; |
529 | 717 } |
1140 | 718 |
719 static void | |
1755 | 720 help_from_info (const string_vector& argv, int idx, int argc) |
1140 | 721 { |
1755 | 722 if (idx == argc) |
3523 | 723 try_info (std::string ()); |
1140 | 724 else |
725 { | |
1755 | 726 for (int i = idx; i < argc; i++) |
1140 | 727 { |
1755 | 728 int status = try_info (argv[i]); |
1140 | 729 |
5672 | 730 if (status == 127) |
731 break; | |
732 else if (status != 0) | |
733 message ("help", "`%s' is not indexed in the manual", | |
734 argv[i].c_str ()); | |
1140 | 735 } |
736 } | |
2470 | 737 } |
3014 | 738 |
739 static bool | |
3523 | 740 looks_like_texinfo (const std::string& msg, size_t& p1) |
3295 | 741 { |
742 p1 = msg.find ('\n'); | |
743 | |
3523 | 744 std::string t = msg.substr (0, p1); |
3295 | 745 |
8021 | 746 if (p1 == std::string::npos) |
3295 | 747 p1 = 0; |
748 | |
749 size_t p2 = t.find ("-*- texinfo -*-"); | |
750 | |
8021 | 751 return (p2 != std::string::npos); |
3295 | 752 } |
753 | |
3330 | 754 void |
3523 | 755 display_help_text (std::ostream& os, const std::string& msg) |
3295 | 756 { |
757 // Look for "-*- texinfo -*-" in first line of help message. If it | |
758 // is present, use makeinfo to format the rest of the message before | |
759 // sending it to the output stream. Otherwise, just print the | |
760 // message. | |
761 | |
762 size_t pos; | |
763 | |
764 if (looks_like_texinfo (msg, pos)) | |
765 { | |
5594 | 766 os.flush (); |
767 | |
3523 | 768 std::string tmp_file_name = file_ops::tempnam ("", ""); |
3295 | 769 |
770 int cols = command_editor::terminal_cols (); | |
771 | |
772 if (cols > 16) | |
773 cols--; | |
774 | |
775 if (cols > 64) | |
776 cols -= 7; | |
777 | |
778 if (cols > 80) | |
779 cols = 72; | |
780 | |
5765 | 781 std::ostringstream buf; |
4051 | 782 |
6099 | 783 // Use double quotes to quote the sed patterns for Windows. |
784 | |
785 buf << "sed -e \"s/^[#%][#%]* *//\" -e \"s/^ *@/@/\" | " | |
5794 | 786 << "\"" << Vmakeinfo_program << "\"" |
3303 | 787 << " -D \"VERSION " << OCTAVE_VERSION << "\"" |
788 << " -D \"OCTAVEHOME " << OCTAVE_PREFIX << "\"" | |
3584 | 789 << " -D \"TARGETHOSTTYPE " << OCTAVE_CANONICAL_HOST_TYPE << "\"" |
3301 | 790 << " --fill-column " << cols |
791 << " --no-warn" | |
792 << " --no-validate" | |
793 << " --no-headers" | |
794 << " --force" | |
5765 | 795 << " --output \"" << tmp_file_name << "\""; |
3295 | 796 |
5765 | 797 oprocstream filter (buf.str ()); |
3295 | 798 |
3686 | 799 if (filter && filter.is_open ()) |
3295 | 800 { |
3405 | 801 filter << "@macro seealso {args}\n" |
5644 | 802 << "@sp 1\n" |
3408 | 803 << "@noindent\n" |
3405 | 804 << "See also: \\args\\.\n" |
805 << "@end macro\n"; | |
806 | |
3769 | 807 filter << msg.substr (pos+1) << std::endl; |
3295 | 808 |
3686 | 809 int status = filter.close (); |
3295 | 810 |
3523 | 811 std::ifstream tmp_file (tmp_file_name.c_str ()); |
3295 | 812 |
3686 | 813 if (WIFEXITED (status) && WEXITSTATUS (status) == 0) |
814 { | |
815 int c; | |
816 while ((c = tmp_file.get ()) != EOF) | |
817 os << (char) c; | |
3295 | 818 |
3686 | 819 tmp_file.close (); |
820 } | |
821 else | |
822 { | |
823 warning ("help: Texinfo formatting filter exited abnormally"); | |
824 warning ("help: raw Texinfo source of help text follows..."); | |
5594 | 825 warning ("help:\n\n%s\n\n", msg.c_str ()); |
3686 | 826 } |
3295 | 827 |
828 file_ops::unlink (tmp_file_name); | |
829 } | |
830 else | |
831 os << msg; | |
832 } | |
833 else | |
834 os << msg; | |
835 } | |
836 | |
5800 | 837 void |
838 display_usage_text (std::ostream& os, const std::string& msg) | |
839 { | |
840 std::string filtered_msg = msg; | |
841 | |
842 size_t pos; | |
843 | |
844 if (looks_like_texinfo (msg, pos)) | |
845 { | |
846 std::ostringstream buf; | |
847 | |
848 buf << "-*- texinfo -*-\n"; | |
849 | |
850 bool found_def = false; | |
851 | |
852 size_t msg_len = msg.length (); | |
853 | |
854 while (pos < msg_len) | |
855 { | |
856 size_t new_pos = msg.find_first_of ('\n', pos); | |
857 | |
8021 | 858 if (new_pos == std::string::npos) |
5800 | 859 new_pos = msg_len-1; |
860 | |
861 std::string line = msg.substr (pos, new_pos-pos+1); | |
862 | |
863 if (line.substr (0, 4) == "@def" | |
864 || line.substr (0, 8) == "@end def") | |
865 { | |
866 found_def = true; | |
867 buf << line; | |
868 } | |
869 | |
870 pos = new_pos + 1; | |
871 } | |
872 | |
873 if (found_def) | |
874 filtered_msg = buf.str (); | |
875 } | |
876 | |
877 display_help_text (os, filtered_msg); | |
878 } | |
879 | |
3295 | 880 static bool |
6243 | 881 raw_help_from_list (const help_list *list, const std::string& nm, |
882 std::string& h, bool& symbol_found) | |
542 | 883 { |
5399 | 884 bool retval = false; |
885 | |
2804 | 886 const char *name; |
3014 | 887 |
542 | 888 while ((name = list->name) != 0) |
889 { | |
1755 | 890 if (strcmp (name, nm.c_str ()) == 0) |
542 | 891 { |
5399 | 892 symbol_found = true; |
893 | |
6243 | 894 h = list->help; |
5399 | 895 |
896 if (h.length () > 0) | |
6243 | 897 retval = true; |
5399 | 898 |
899 break; | |
542 | 900 } |
901 list++; | |
902 } | |
3014 | 903 |
5399 | 904 return retval;; |
905 } | |
906 | |
6243 | 907 static bool |
908 help_from_list (std::ostream& os, const help_list *list, | |
909 const std::string& nm, int usage, bool& symbol_found) | |
910 { | |
911 bool retval = false; | |
912 | |
913 std::string h; | |
914 | |
915 if (raw_help_from_list (list, nm, h, symbol_found)) | |
916 { | |
917 if (h.length () > 0) | |
918 { | |
919 if (usage) | |
920 os << "\nusage: "; | |
921 else | |
922 os << "\n*** " << nm << ":\n\n"; | |
923 | |
924 display_help_text (os, h); | |
925 | |
926 os << "\n"; | |
927 | |
928 retval = true; | |
929 } | |
930 } | |
931 | |
932 return retval; | |
933 } | |
934 | |
3355 | 935 static bool |
6243 | 936 raw_help_from_symbol_table (const std::string& nm, std::string& h, |
937 std::string& w, bool& symbol_found) | |
3355 | 938 { |
939 bool retval = false; | |
940 | |
7336 | 941 octave_value val = symbol_table::find_function (nm); |
3355 | 942 |
7336 | 943 if (val.is_defined ()) |
3355 | 944 { |
7336 | 945 octave_function *fcn = val.function_value (); |
946 | |
947 if (fcn) | |
948 { | |
949 symbol_found = true; | |
5399 | 950 |
7336 | 951 h = fcn->doc_string (); |
952 | |
953 if (! h.empty ()) | |
954 { | |
955 retval = true; | |
6243 | 956 |
7336 | 957 w = fcn->fcn_file_name (); |
6243 | 958 |
7336 | 959 if (w.empty ()) |
960 w = fcn->is_user_function () | |
961 ? "command-line function" : "built-in function"; | |
962 } | |
6243 | 963 } |
964 } | |
3355 | 965 |
6243 | 966 return retval; |
967 } | |
968 | |
969 static bool | |
970 help_from_symbol_table (std::ostream& os, const std::string& nm, | |
971 bool& symbol_found) | |
972 { | |
973 bool retval = false; | |
974 | |
975 std::string h; | |
976 std::string w; | |
977 | |
978 if (raw_help_from_symbol_table (nm, h, w, symbol_found)) | |
979 { | |
3355 | 980 if (h.length () > 0) |
981 { | |
7336 | 982 h += "\n\n@noindent\n" + symbol_table::help_for_dispatch (nm); |
6243 | 983 |
3355 | 984 display_help_text (os, h); |
6243 | 985 |
986 if (w.length () > 0 && ! Vsuppress_verbose_help_message) | |
987 os << w << "\n"; | |
988 | |
3355 | 989 os << "\n"; |
6243 | 990 |
3355 | 991 retval = true; |
992 } | |
993 } | |
994 | |
995 return retval; | |
996 } | |
997 | |
998 static bool | |
6243 | 999 raw_help_from_file (const std::string& nm, std::string& h, |
1000 std::string& file, bool& symbol_found) | |
1001 { | |
1002 bool retval = false; | |
1003 | |
1004 h = get_help_from_file (nm, symbol_found, file); | |
1005 | |
1006 if (h.length () > 0) | |
1007 retval = true; | |
8041
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1008 else if (! symbol_found) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1009 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1010 file = contents_file_in_path (nm); |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1011 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1012 if (! file.empty ()) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1013 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1014 h = get_help_from_file (file, symbol_found); |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1015 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1016 if (h.length () > 0) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1017 retval = true; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1018 } |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1019 } |
6243 | 1020 |
1021 return retval; | |
1022 } | |
1023 | |
1024 static bool | |
5399 | 1025 help_from_file (std::ostream& os, const std::string& nm, bool& symbol_found) |
3355 | 1026 { |
1027 bool retval = false; | |
1028 | |
6243 | 1029 std::string h; |
5931 | 1030 std::string file; |
1031 | |
6243 | 1032 if (raw_help_from_file (nm, h, file, symbol_found)) |
5484 | 1033 { |
6243 | 1034 if (h.length () > 0) |
1035 { | |
6253 | 1036 // Strip extension |
1037 size_t l = file.length (); | |
1038 if (l > 2 && file.substr (l-2) == ".m") | |
1039 { | |
1040 std::string tmp = file.substr (0, l - 2); | |
1041 | |
1042 if (file_stat (tmp + ".oct")) | |
1043 file = tmp + ".oct"; | |
1044 else if (file_stat (tmp + ".mex")) | |
1045 file = tmp + ".mex"; | |
1046 } | |
1047 | |
6243 | 1048 os << nm << " is the file " << file << "\n\n"; |
1049 | |
1050 display_help_text (os, h); | |
1051 | |
1052 os << "\n"; | |
1053 | |
1054 retval = true; | |
1055 } | |
3355 | 1056 } |
1057 | |
1058 return retval; | |
1059 } | |
1060 | |
6243 | 1061 std::string |
1062 raw_help (const std::string& nm, bool &symbol_found) | |
1063 { | |
1064 std::string h; | |
1065 std::string w; | |
1066 std::string f; | |
1067 | |
1068 (raw_help_from_list (operator_help (), nm, h, symbol_found) | |
1069 || raw_help_from_list (keyword_help (), nm, h, symbol_found) | |
1070 || raw_help_from_symbol_table (nm, h, w, symbol_found) | |
1071 || raw_help_from_file (nm, h, f, symbol_found)); | |
1072 | |
1073 return h; | |
1074 } | |
1075 | |
1140 | 1076 static void |
1755 | 1077 builtin_help (int argc, const string_vector& argv) |
1140 | 1078 { |
1079 help_list *op_help_list = operator_help (); | |
1080 help_list *kw_help_list = keyword_help (); | |
1081 | |
1755 | 1082 for (int i = 1; i < argc; i++) |
1140 | 1083 { |
5399 | 1084 bool symbol_found = false; |
1085 | |
1086 if (help_from_list (octave_stdout, op_help_list, argv[i], 0, | |
1087 symbol_found)) | |
1140 | 1088 continue; |
1089 | |
5399 | 1090 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0, |
1091 symbol_found)) | |
1140 | 1092 continue; |
1093 | |
5399 | 1094 if (help_from_symbol_table (octave_stdout, argv[i], symbol_found)) |
1095 continue; | |
1096 | |
1097 if (help_from_file (octave_stdout, argv[i], symbol_found)) | |
3355 | 1098 continue; |
1755 | 1099 |
5399 | 1100 if (symbol_found) |
1101 octave_stdout << "\nhelp: `" << argv[i] | |
1102 << "' is not documented\n"; | |
1103 else | |
1104 octave_stdout << "\nhelp: `" << argv[i] | |
1105 << "' not found\n"; | |
1140 | 1106 } |
1107 | |
2095 | 1108 additional_help_message (octave_stdout); |
1140 | 1109 } |
1110 | |
4208 | 1111 DEFCMD (help, args, , |
3332 | 1112 "-*- texinfo -*-\n\ |
6615 | 1113 @deffn {Command} help @var{name}\n\ |
1114 Display the help text for @var{name}.\n\ | |
1115 If invoked without any arguments, @code{help} prints a list\n\ | |
1116 of all the available operators and functions.\n\ | |
3168 | 1117 \n\ |
3332 | 1118 For example, the command @kbd{help help} prints a short message\n\ |
6615 | 1119 describing the @code{help} command.\n\ |
3332 | 1120 \n\ |
6615 | 1121 The help command can give you information about operators, but not the\n\ |
1122 comma and semicolons that are used as command separators. To get help\n\ | |
1123 for those, you must type @kbd{help comma} or @kbd{help semicolon}.\n\ | |
5672 | 1124 @seealso{doc, which, lookfor}\n\ |
3333 | 1125 @end deffn") |
529 | 1126 { |
2086 | 1127 octave_value_list retval; |
529 | 1128 |
1755 | 1129 int argc = args.length () + 1; |
1130 | |
1968 | 1131 string_vector argv = args.make_argv ("help"); |
1755 | 1132 |
1133 if (error_state) | |
1134 return retval; | |
529 | 1135 |
1136 if (argc == 1) | |
3014 | 1137 simple_help (); |
529 | 1138 else |
1139 { | |
1755 | 1140 if (argv[1] == "-i") |
3014 | 1141 help_from_info (argv, 2, argc); |
529 | 1142 else |
3014 | 1143 builtin_help (argc, argv); |
529 | 1144 } |
1145 | |
1146 return retval; | |
1147 } | |
1148 | |
3355 | 1149 static void |
7336 | 1150 display_file (std::ostream& os, const std::string& name, |
1151 const std::string& fname, const std::string& type, | |
1152 bool pr_type_info, bool quiet) | |
1153 { | |
1154 std::ifstream fs (fname.c_str (), std::ios::in); | |
1155 | |
1156 if (fs) | |
1157 { | |
1158 if (pr_type_info && ! quiet) | |
7438 | 1159 os << name << " is the " << type << " defined from the file\n" |
1160 << fname << ":\n\n"; | |
7336 | 1161 |
1162 char ch; | |
1163 | |
1164 while (fs.get (ch)) | |
1165 os << ch; | |
1166 } | |
1167 else | |
1168 os << "unable to open `" << fname << "' for reading!\n"; | |
1169 } | |
1170 | |
1171 static void | |
3523 | 1172 do_type (std::ostream& os, const std::string& name, bool pr_type_info, |
3355 | 1173 bool quiet, bool pr_orig_txt) |
1174 { | |
7336 | 1175 // FIXME -- should we bother with variables here (earlier versions |
1176 // of Octave displayed them)? | |
1177 | |
1178 octave_value val = symbol_table::varval (name); | |
3355 | 1179 |
7336 | 1180 if (val.is_defined ()) |
1181 { | |
1182 if (pr_type_info && ! quiet) | |
1183 os << name << " is a variable\n"; | |
1184 | |
1185 val.print_raw (os, pr_orig_txt); | |
1186 | |
1187 if (pr_type_info) | |
1188 os << "\n"; | |
1189 } | |
3355 | 1190 else |
1191 { | |
7336 | 1192 val = symbol_table::find_function (name); |
3355 | 1193 |
7336 | 1194 if (val.is_defined ()) |
3355 | 1195 { |
7336 | 1196 octave_function *fcn = val.function_value (); |
1197 | |
7438 | 1198 if (fcn) |
1199 { | |
1200 std::string fn = fcn->fcn_file_name (); | |
3355 | 1201 |
7438 | 1202 if (fcn->is_builtin_function ()) |
1203 os << name << " is a built-in function" << std::endl; | |
1204 else if (fcn->is_dld_function () || fcn->is_mex_function ()) | |
1205 os << name | |
1206 << " is a dyanmically loaded function from the file\n" | |
1207 << fn << std::endl; | |
1208 else if (pr_orig_txt && ! fn.empty ()) | |
1209 display_file (os, name, fn, "function", pr_type_info, quiet); | |
1210 else | |
7336 | 1211 { |
7438 | 1212 if (pr_type_info && ! quiet) |
1213 { | |
1214 os << name; | |
3355 | 1215 |
7438 | 1216 if (fcn->is_user_function ()) |
1217 { | |
1218 if (fn.empty ()) | |
1219 os << " is a command-line function:\n\n"; | |
1220 else | |
1221 os << " is a function defined from the file\n" | |
1222 << fn << ":\n\n"; | |
1223 } | |
1224 } | |
1225 | |
1226 tree_print_code tpc (os, "", pr_orig_txt); | |
1227 | |
1228 fcn->accept (tpc); | |
7336 | 1229 } |
3355 | 1230 } |
1231 } | |
1232 else | |
7336 | 1233 { |
1234 std::string fn = fcn_file_in_path (name); | |
1235 | |
1236 if (! fn.empty ()) | |
1237 display_file (os, name, fn, "script", pr_type_info, quiet); | |
1238 else | |
1239 error ("type: `%s' undefined", name.c_str ()); | |
1240 } | |
3355 | 1241 } |
1242 } | |
1243 | |
4208 | 1244 DEFCMD (type, args, nargout, |
3361 | 1245 "-*- texinfo -*-\n\ |
1246 \n\ | |
1247 @deffn {Command} type options name @dots{}\n\ | |
1248 Display the definition of each @var{name} that refers to a function.\n\ | |
1249 \n\ | |
6620 | 1250 Normally also displays whether each @var{name} is user-defined or built-in;\n\ |
3361 | 1251 the @code{-q} option suppresses this behaviour.\n\ |
1252 @end deffn") | |
581 | 1253 { |
3584 | 1254 octave_value retval; |
1588 | 1255 |
1755 | 1256 int argc = args.length () + 1; |
1257 | |
1968 | 1258 string_vector argv = args.make_argv ("type"); |
1755 | 1259 |
3355 | 1260 if (! error_state) |
581 | 1261 { |
3355 | 1262 if (argc > 1) |
1263 { | |
5775 | 1264 // FIXME -- we should really use getopt () |
2532 | 1265 |
3355 | 1266 bool quiet = false; |
1267 bool pr_orig_txt = true; | |
2532 | 1268 |
3355 | 1269 int idx; |
581 | 1270 |
3355 | 1271 for (idx = 1; idx < argc; idx++) |
1281 | 1272 { |
3355 | 1273 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
1274 quiet = true; | |
1275 else if (argv[idx] == "-t" || argv[idx] == "-transformed") | |
1276 pr_orig_txt = false; | |
1277 else | |
1278 break; | |
1281 | 1279 } |
1280 | |
3355 | 1281 if (idx < argc) |
1282 { | |
5765 | 1283 std::ostringstream output_buf; |
581 | 1284 |
3355 | 1285 for (int i = idx; i < argc; i++) |
581 | 1286 { |
3523 | 1287 std::string id = argv[i]; |
2534 | 1288 |
3355 | 1289 if (nargout == 0) |
1290 do_type (octave_stdout, id, true, quiet, pr_orig_txt); | |
1291 else | |
1292 do_type (output_buf, id, false, quiet, pr_orig_txt); | |
581 | 1293 |
3355 | 1294 if (error_state) |
1295 goto abort; | |
581 | 1296 } |
1297 | |
3584 | 1298 if (nargout != 0) |
5765 | 1299 retval = output_buf.str (); |
581 | 1300 } |
1301 else | |
5823 | 1302 print_usage (); |
3355 | 1303 } |
1304 else | |
5823 | 1305 print_usage (); |
3355 | 1306 } |
3141 | 1307 |
3355 | 1308 abort: |
581 | 1309 |
1310 return retval; | |
1311 } | |
1312 | |
7082 | 1313 std::string |
3523 | 1314 do_which (const std::string& name) |
3355 | 1315 { |
7336 | 1316 octave_value val = symbol_table::find_function (name); |
3355 | 1317 |
7336 | 1318 if (val.is_defined ()) |
1319 { | |
1320 octave_function *fcn = val.function_value (); | |
3355 | 1321 |
7336 | 1322 if (fcn) |
1323 { | |
1324 std::string fn = fcn->fcn_file_name (); | |
3355 | 1325 |
7336 | 1326 return fn.empty () |
1327 ? (fcn->is_user_function () | |
1328 ? "command-line function" : "built-in function") | |
1329 : fn; | |
1330 } | |
1331 } | |
1332 | |
1333 return fcn_file_in_path (name); | |
3355 | 1334 } |
1335 | |
1336 static void | |
3523 | 1337 do_which (std::ostream& os, const std::string& name) |
3355 | 1338 { |
7336 | 1339 std::string desc; |
3355 | 1340 |
7336 | 1341 octave_value val = symbol_table::find_function (name); |
1342 | |
1343 if (val.is_defined ()) | |
3355 | 1344 { |
7336 | 1345 octave_function *fcn = val.function_value (); |
1346 | |
1347 if (fcn) | |
1348 { | |
1349 desc = fcn->fcn_file_name (); | |
3355 | 1350 |
7336 | 1351 if (desc.empty ()) |
1352 { | |
1353 if (fcn->is_user_function ()) | |
1354 desc = "is a command-line function"; | |
1355 else | |
1356 desc = "is a built-in function"; | |
1357 } | |
1358 else | |
1359 desc = "is the function from the file " + desc; | |
1360 } | |
1361 } | |
1362 | |
1363 if (desc.empty ()) | |
1364 { | |
1365 std::string fn = fcn_file_in_path (name); | |
1366 | |
1367 if (! fn.empty ()) | |
1368 desc = "is the script file " + fn; | |
3355 | 1369 else |
7336 | 1370 desc = "is undefined"; |
3355 | 1371 } |
7336 | 1372 |
1373 os << "which: `" << name << "' " << desc << std::endl; | |
3355 | 1374 } |
1375 | |
4208 | 1376 DEFCMD (which, args, nargout, |
3361 | 1377 "-*- texinfo -*-\n\ |
1378 @deffn {Command} which name @dots{}\n\ | |
1379 Display the type of each @var{name}. If @var{name} is defined from a\n\ | |
1380 function file, the full name of the file is also displayed.\n\ | |
5667 | 1381 @seealso{help, lookfor}\n\ |
3361 | 1382 @end deffn") |
581 | 1383 { |
2086 | 1384 octave_value_list retval; |
581 | 1385 |
1968 | 1386 string_vector argv = args.make_argv ("which"); |
1755 | 1387 |
3355 | 1388 if (! error_state) |
1389 { | |
1390 int argc = argv.length (); | |
581 | 1391 |
1392 if (nargout > 0) | |
1393 retval.resize (argc-1, Matrix ()); | |
1394 | |
3355 | 1395 if (argc > 1) |
581 | 1396 { |
3355 | 1397 for (int i = 1; i < argc; i++) |
581 | 1398 { |
3523 | 1399 std::string id = argv[i]; |
3141 | 1400 |
3355 | 1401 if (nargout == 0) |
1402 do_which (octave_stdout, id); | |
581 | 1403 else |
3355 | 1404 retval(i-1) = do_which (id); |
581 | 1405 } |
1406 } | |
3355 | 1407 else |
5823 | 1408 print_usage (); |
581 | 1409 } |
1410 | |
1411 return retval; | |
1412 } | |
1413 | |
5775 | 1414 // FIXME |
5447 | 1415 // This function attempts to find the first sentence of a help string, though |
1416 // given that the user can create the help in an arbitrary format, your | |
1417 // success might vary.. it works much better with help string formated in | |
1418 // texinfo. Using regex might make this function much simpler. | |
1419 | |
1420 std::string | |
1421 first_help_sentence (const std::string& h, bool short_sentence = true) | |
1422 { | |
5738 | 1423 std::string retval; |
1424 | |
5447 | 1425 size_t pos = 0; |
1426 | |
1427 if (looks_like_texinfo (h, pos)) | |
5592 | 1428 { |
1429 // Get the parsed help string. | |
5447 | 1430 pos = 0; |
5765 | 1431 std::ostringstream os; |
5447 | 1432 display_help_text (os, h); |
1433 std::string h2 = os.str (); | |
1434 | |
1435 while (1) | |
1436 { | |
1437 // Skip leading whitespace and get new line | |
1438 pos = h2.find_first_not_of ("\n\t ", pos); | |
1439 | |
8021 | 1440 if (pos == std::string::npos) |
5447 | 1441 break; |
1442 | |
1443 size_t new_pos = h2.find_first_of ('\n', pos); | |
1444 std::string line = h2.substr (pos, new_pos-pos); | |
1445 | |
1446 // Skip lines starting in "-" | |
1447 if (line.find_first_of ('-') == 0) | |
1448 { | |
1449 pos = new_pos + 1; | |
1450 continue; | |
1451 } | |
1452 | |
1453 break; | |
1454 } | |
1455 | |
8021 | 1456 if (pos == std::string::npos) |
5738 | 1457 return retval; |
5447 | 1458 |
1459 // At start of real text. Get first line with the sentence | |
1460 size_t new_pos = h2.find_first_of ('\n', pos); | |
1461 std::string line = h2.substr (pos, new_pos-pos); | |
1462 size_t dot_pos; | |
1463 | |
8021 | 1464 while ((dot_pos = line.find_first_of ('.')) == std::string::npos) |
5447 | 1465 { |
1466 // Trim trailing blanks on line | |
1467 line.substr (0, line.find_last_not_of ("\n\t ") + 1); | |
1468 | |
1469 // Append next line | |
1470 size_t tmp_pos = h2.find_first_not_of ("\n\t ", new_pos + 1); | |
8021 | 1471 if (tmp_pos == std::string::npos || h2.substr (tmp_pos, 1) == "\n") |
5447 | 1472 break; |
1473 | |
1474 new_pos = h2.find_first_of ('\n', tmp_pos); | |
1475 std::string next = h2.substr (tmp_pos, new_pos-tmp_pos); | |
1476 | |
1477 if (short_sentence) | |
1478 { | |
8021 | 1479 if ((tmp_pos = next.find_first_of ('.')) != std::string::npos) |
5447 | 1480 { |
1481 line = line + " " + next; | |
1482 dot_pos = line.find_first_of ('.'); | |
1483 } | |
1484 break; | |
1485 } | |
1486 else | |
1487 line = line + " " + next; | |
1488 } | |
1489 | |
8021 | 1490 if (dot_pos == std::string::npos) |
5738 | 1491 retval = line; |
5447 | 1492 else |
5738 | 1493 retval = line.substr (0, dot_pos + 1); |
5447 | 1494 } |
1495 else | |
1496 { | |
1497 std::string _upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
1498 std::string _lower = "abcdefghijklmnopqrstuvwxyz"; | |
1499 std::string _alpha = _upper + _lower + "_"; | |
1500 std::string _alphanum = _alpha + "1234567890"; | |
1501 pos = 0; | |
1502 | |
1503 while (1) | |
1504 { | |
1505 // Skip leading whitespace and get new line | |
1506 pos = h.find_first_not_of ("\n\t ", pos); | |
1507 | |
8021 | 1508 if (pos == std::string::npos) |
5447 | 1509 break; |
1510 | |
1511 size_t new_pos = h.find_first_of ('\n', pos); | |
1512 std::string line = h.substr (pos, new_pos-pos); | |
1513 | |
1514 // Make a lower case copy to simplify some tests | |
1515 std::string lower = line; | |
5768 | 1516 std::transform (lower.begin (), lower.end (), lower.begin (), tolower); |
5447 | 1517 |
1518 // Skip lines starting in "-" or "Usage" | |
1519 if (lower.find_first_of ('-') == 0 | |
1520 || lower.substr (0, 5) == "usage") | |
1521 { | |
8021 | 1522 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1523 continue; |
1524 } | |
1525 | |
1526 size_t line_pos = 0; | |
1527 size_t tmp_pos = 0; | |
1528 | |
1529 // chop " blah : " | |
1530 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of | |
1531 (_alphanum, line_pos)); | |
8021 | 1532 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == ":") |
5447 | 1533 line_pos = line.find_first_not_of ("\t ", tmp_pos + 1); |
1534 | |
8021 | 1535 if (line_pos == std::string::npos) |
5447 | 1536 { |
8021 | 1537 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1538 continue; |
1539 } | |
1540 | |
1541 // chop " function " | |
1542 if (lower.substr (line_pos, 8) == "function") | |
1543 line_pos = line.find_first_not_of ("\t ", line_pos + 8); | |
1544 | |
8021 | 1545 if (line_pos == std::string::npos) |
5447 | 1546 { |
8021 | 1547 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1548 continue; |
1549 } | |
1550 | |
1551 // chop " [a,b] = " | |
1552 if (line.substr (line_pos, 1) == "[") | |
1553 { | |
1554 tmp_pos = line.find_first_not_of | |
1555 ("\t ", line.find_first_of ("]", line_pos) + 1); | |
1556 | |
8021 | 1557 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == "=") |
5447 | 1558 line_pos = line.find_first_not_of ("\t ",tmp_pos + 1); |
1559 } | |
1560 | |
8021 | 1561 if (line_pos == std::string::npos) |
5447 | 1562 { |
8021 | 1563 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1564 continue; |
1565 } | |
1566 | |
1567 // chop " a = " | |
1568 if (line.find_first_not_of (_alpha, line_pos) != line_pos) | |
1569 { | |
1570 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of | |
1571 (_alphanum, line_pos)); | |
8021 | 1572 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == "=") |
5447 | 1573 line_pos = line.find_first_not_of ("\t ", tmp_pos + 1); |
1574 } | |
1575 | |
8021 | 1576 if (line_pos == std::string::npos) |
5447 | 1577 { |
1578 pos = new_pos + 1; | |
1579 continue; | |
1580 } | |
1581 | |
1582 // chop " f(x) " | |
1583 if (line.find_first_not_of (_alpha, line_pos) != line_pos) | |
1584 { | |
1585 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of | |
1586 (_alphanum, line_pos)); | |
8021 | 1587 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == "(") |
5447 | 1588 line_pos = line.find_first_not_of ("\t ", line.find_first_of |
1589 (")", tmp_pos) + 1); | |
1590 } | |
1591 | |
8021 | 1592 if (line_pos == std::string::npos) |
5447 | 1593 { |
8021 | 1594 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1595 continue; |
1596 } | |
1597 | |
1598 // chop " ; " | |
1599 if (line.substr (line_pos, 1) == ":" | |
1600 || line.substr (line_pos, 1) == ";") | |
1601 line_pos = line.find_first_not_of ("\t ", line_pos + 1); | |
1602 | |
8021 | 1603 if (line_pos == std::string::npos) |
5447 | 1604 { |
8021 | 1605 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1606 continue; |
1607 } | |
1608 | |
1609 // chop " BLAH " | |
1610 if (line.length () > line_pos + 2 | |
1611 && line.find_first_of (_upper, line_pos) == line_pos | |
1612 && line.find_first_of (_upper, line_pos+1) == line_pos + 1) | |
1613 line_pos = line.find_first_not_of ("\t ", line.find_first_not_of | |
1614 (_upper + "0123456789_", line_pos)); | |
1615 | |
8021 | 1616 if (line_pos == std::string::npos) |
5447 | 1617 { |
8021 | 1618 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1619 continue; |
1620 } | |
1621 | |
1622 // chop " blah --- " | |
1623 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of | |
1624 (_alphanum, line_pos)); | |
8021 | 1625 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == "-") |
5447 | 1626 { |
1627 tmp_pos = line.find_first_not_of ("-", tmp_pos); | |
1628 if (line.substr (tmp_pos, 1) == " " | |
1629 || line.substr (tmp_pos, 1) == "\t") | |
1630 line_pos = line.find_first_not_of ("\t ", tmp_pos); | |
1631 } | |
1632 | |
8021 | 1633 if (line_pos == std::string::npos) |
5447 | 1634 { |
8021 | 1635 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1636 continue; |
1637 } | |
1638 | |
1639 // chop " blah <TAB> " | |
1640 if (line.find_first_not_of (_alpha, line_pos) != line_pos) | |
1641 { | |
1642 tmp_pos = line.find_first_not_of (" ", line.find_first_not_of | |
1643 (_alphanum, line_pos)); | |
8021 | 1644 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == "\t") |
5447 | 1645 line_pos = line.find_first_not_of ("\t ", line.find_first_of |
1646 (")", tmp_pos) + 1); | |
1647 } | |
1648 | |
8021 | 1649 if (line_pos == std::string::npos) |
5447 | 1650 { |
8021 | 1651 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1652 continue; |
1653 } | |
1654 | |
1655 // chop " blah " | |
1656 if (line.find_first_not_of (_alpha, line_pos) != line_pos) | |
1657 { | |
1658 tmp_pos = line.find_first_not_of (_alphanum, line_pos); | |
1659 | |
8021 | 1660 if (tmp_pos != std::string::npos |
5447 | 1661 && (line.substr (tmp_pos, 2) == "\t\t" |
1662 || line.substr (tmp_pos, 2) == "\t " | |
1663 || line.substr (tmp_pos, 2) == " \t" | |
1664 || line.substr (tmp_pos, 2) == " ")) | |
1665 line_pos = line.find_first_not_of ("\t ", tmp_pos); | |
1666 } | |
1667 | |
8021 | 1668 if (line_pos == std::string::npos) |
5447 | 1669 { |
8021 | 1670 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1671 continue; |
1672 } | |
1673 | |
1674 // skip blah \n or \n blah | |
1675 // skip blank line | |
1676 // skip "# !/usr/bin/octave" | |
1677 if ((line.substr (line_pos , 2) == "or" | |
8021 | 1678 && line.find_first_not_of ("\n\t ", line_pos + 2) == std::string::npos) |
1679 || line.find_first_not_of ("\n\t ", line_pos) == std::string::npos | |
5447 | 1680 || line.substr (line_pos, 2) == "!/") |
1681 { | |
8021 | 1682 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1683 continue; |
1684 } | |
1685 | |
1686 // Got the start of first sentence, break. | |
1687 pos = pos + line_pos; | |
1688 break; | |
1689 } | |
1690 | |
8021 | 1691 if (pos == std::string::npos) |
5738 | 1692 return retval; |
5447 | 1693 |
1694 // At start of real text. Get first line with the sentence | |
1695 size_t new_pos = h.find_first_of ('\n', pos); | |
1696 std::string line = h.substr (pos, new_pos-pos); | |
1697 size_t dot_pos; | |
1698 | |
8021 | 1699 while ((dot_pos = line.find_first_of ('.')) == std::string::npos) |
5447 | 1700 { |
1701 // Trim trailing blanks on line | |
1702 line = line.substr (0, line.find_last_not_of ("\n\t ") + 1); | |
1703 | |
1704 // Append next line | |
1705 size_t tmp_pos = h.find_first_not_of ("\t ", new_pos + 1); | |
8021 | 1706 if (tmp_pos == std::string::npos || h.substr (tmp_pos, 1) == "\n") |
5447 | 1707 break; |
1708 | |
1709 new_pos = h.find_first_of ('\n', tmp_pos); | |
1710 std::string next = h.substr (tmp_pos, new_pos-tmp_pos); | |
1711 | |
1712 if (short_sentence) | |
1713 { | |
1714 // Only add the next line if it terminates the sentence, then break | |
8021 | 1715 if ((tmp_pos = next.find_first_of ('.')) != std::string::npos) |
5447 | 1716 { |
1717 line = line + " " + next; | |
1718 dot_pos = line.find_first_of ('.'); | |
1719 } | |
1720 break; | |
1721 } | |
1722 else | |
1723 line = line + " " + next; | |
1724 } | |
1725 | |
8021 | 1726 if (dot_pos == std::string::npos) |
5738 | 1727 retval = line; |
5447 | 1728 else |
5738 | 1729 retval = line.substr (0, dot_pos + 1); |
5447 | 1730 } |
5738 | 1731 |
1732 return retval; | |
5447 | 1733 } |
1734 | |
1735 static void | |
1736 print_lookfor (const std::string& name, const std::string& line) | |
1737 { | |
1738 const size_t deflen = 20; | |
1739 | |
1740 size_t max_width = command_editor::terminal_cols () - deflen; | |
1741 if (max_width < deflen) | |
1742 max_width = deflen; | |
1743 | |
1744 size_t name_len = name.length (); | |
1745 | |
1746 size_t width = max_width; | |
1747 if (name_len > deflen) | |
1748 { | |
1749 width = command_editor::terminal_cols () - name_len; | |
1750 if (width < deflen) | |
1751 width = deflen; | |
1752 } | |
1753 | |
1754 size_t pad_len = deflen > name_len ? deflen - name_len + 1 : 1; | |
1755 octave_stdout << name << std::string (pad_len, ' '); | |
1756 | |
1757 size_t pos = 0; | |
1758 | |
1759 while (1) | |
1760 { | |
1761 size_t new_pos = line.find_first_of ("\n\t ", pos); | |
1762 size_t end_pos = new_pos; | |
1763 | |
1764 if (line.length () - pos < width) | |
8021 | 1765 new_pos = end_pos = std::string::npos; |
5447 | 1766 else |
8021 | 1767 while (new_pos != std::string::npos && new_pos - pos < width) |
5447 | 1768 { |
1769 end_pos = new_pos; | |
1770 new_pos = line.find_first_of ("\n\t ", new_pos + 1); | |
1771 } | |
1772 | |
1773 octave_stdout << line.substr (pos, end_pos-pos) << std::endl; | |
1774 | |
8021 | 1775 if (end_pos == std::string::npos) |
5447 | 1776 break; |
1777 | |
1778 pos = end_pos + 1; | |
1779 width = max_width; | |
1780 octave_stdout << std::string (deflen + 1, ' '); | |
1781 } | |
1782 } | |
1783 | |
1784 DEFCMD (lookfor, args, nargout, | |
1785 "-*- texinfo -*-\n\ | |
1786 @deffn {Command} lookfor @var{str}\n\ | |
1787 @deffnx {Command} lookfor -all @var{str}\n\ | |
1788 @deffnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor (@var{str})\n\ | |
1789 @deffnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor ('-all', @var{str})\n\ | |
5814 | 1790 Search for the string @var{str} in all of the functions found in the\n\ |
7001 | 1791 function search path. By default @code{lookfor} searches for @var{str}\n\ |
5814 | 1792 in the first sentence of the help string of each function found. The entire\n\ |
7001 | 1793 help string of each function found in the path can be searched if\n\ |
5447 | 1794 the '-all' argument is supplied. All searches are case insensitive.\n\ |
1795 \n\ | |
1796 Called with no output arguments, @code{lookfor} prints the list of matching\n\ | |
1797 functions to the terminal. Otherwise the output arguments @var{fun} and\n\ | |
1798 @var{helpstring} define the matching functions and the first sentence of\n\ | |
1799 each of their help strings.\n\ | |
1800 \n\ | |
1801 Note that the ability of @code{lookfor} to correctly identify the first\n\ | |
1802 sentence of the help of the functions is dependent on the format of the\n\ | |
6620 | 1803 functions help. All of the functions in Octave itself will correctly\n\ |
5447 | 1804 find the first sentence, but the same can not be guaranteed for other\n\ |
1805 functions. Therefore the use of the '-all' argument might be necessary\n\ | |
6620 | 1806 to find related functions that are not part of Octave.\n\ |
5667 | 1807 @seealso{help, which}\n\ |
5642 | 1808 @end deffn") |
5447 | 1809 { |
1810 octave_value_list retval; | |
7336 | 1811 |
5447 | 1812 int nargin = args.length (); |
1813 bool first_sentence_only = true; | |
1814 | |
1815 if (nargin != 1 && nargin != 2) | |
1816 { | |
7038 | 1817 print_usage (); |
5447 | 1818 return retval; |
1819 } | |
1820 | |
1821 string_vector ret[2]; | |
1822 | |
1823 std::string txt; | |
1824 | |
1825 if (args(0).is_string ()) | |
1826 { | |
1827 txt = args(0).string_value (); | |
1828 | |
1829 if (nargin == 2) | |
1830 { | |
1831 if (args(1).is_string ()) | |
1832 { | |
1833 std::string tmp = args(1).string_value (); | |
1834 | |
1835 if (txt.substr(0,1) == "-") | |
1836 { | |
1837 txt = tmp; | |
1838 tmp = args(0).string_value (); | |
1839 } | |
1840 | |
1841 if (tmp == "-all") | |
1842 first_sentence_only = false; | |
1843 else | |
1844 error ("lookfor: unrecognized option argument"); | |
1845 } | |
1846 else | |
1847 error ("lookfor: arguments must be a string"); | |
1848 } | |
1849 } | |
1850 else | |
1851 error ("lookfor: argument must be a string"); | |
1852 | |
1853 if (!error_state) | |
1854 { | |
1855 // All tests in lower case | |
5768 | 1856 std::transform (txt.begin (), txt.end (), txt.begin (), tolower); |
5447 | 1857 |
1858 help_list *ptr = keyword_help (); | |
1859 while (ptr->name) | |
1860 { | |
1861 std::string name = ptr->name; | |
1862 std::string h = ptr->help; | |
1863 | |
8021 | 1864 if (name.find (txt) != std::string::npos) |
5447 | 1865 { |
1866 if (nargout) | |
1867 { | |
1868 ret[0].append (name); | |
1869 ret[1].append (first_help_sentence (h)); | |
1870 } | |
1871 else | |
1872 print_lookfor (name, first_help_sentence (h)); | |
1873 } | |
5592 | 1874 else |
1875 { | |
1876 std::string s; | |
1877 | |
1878 if (first_sentence_only) | |
1879 s = first_help_sentence (h); | |
1880 else | |
1881 s = h; | |
1882 | |
5768 | 1883 std::transform (s.begin (), s.end (), s.begin (), tolower); |
5592 | 1884 |
8021 | 1885 if (s.length () > 0 && s.find (txt) != std::string::npos) |
5592 | 1886 { |
1887 if (nargout) | |
1888 { | |
1889 ret[0].append (name); | |
1890 ret[1].append (first_help_sentence (h)); | |
1891 } | |
1892 else | |
1893 print_lookfor (name, first_help_sentence (h)); | |
1894 } | |
1895 } | |
5447 | 1896 |
1897 OCTAVE_QUIT; | |
1898 | |
1899 ptr++; | |
1900 } | |
1901 | |
1902 ptr = operator_help (); | |
1903 while (ptr->name) | |
1904 { | |
1905 std::string name = ptr->name; | |
1906 std::string h = ptr->help; | |
1907 | |
8021 | 1908 if (name.find (txt) != std::string::npos) |
5447 | 1909 { |
1910 if (nargout) | |
1911 { | |
1912 ret[0].append (name); | |
1913 ret[1].append (first_help_sentence (h)); | |
1914 } | |
1915 else | |
1916 print_lookfor (name, first_help_sentence (h)); | |
1917 } | |
5592 | 1918 else |
1919 { | |
1920 std::string s; | |
1921 if (first_sentence_only) | |
1922 s = first_help_sentence (h); | |
1923 else | |
1924 s = h; | |
1925 | |
5768 | 1926 std::transform (s.begin (), s.end (), s.begin (), tolower); |
5592 | 1927 |
8021 | 1928 if (s.length () > 0 && s.find (txt) != std::string::npos) |
5592 | 1929 { |
1930 if (nargout) | |
1931 { | |
1932 ret[0].append (name); | |
1933 ret[1].append (first_help_sentence (h)); | |
1934 } | |
1935 else | |
1936 print_lookfor (name, first_help_sentence (h)); | |
1937 } | |
1938 } | |
5447 | 1939 |
1940 OCTAVE_QUIT; | |
1941 | |
1942 ptr++; | |
1943 } | |
1944 | |
7336 | 1945 string_vector names; |
1946 | |
1947 #ifdef OLD_SYMTAB | |
5447 | 1948 // Check the symbol record table |
7336 | 1949 names = fbi_sym_tab->name_list (string_vector (), true); |
5447 | 1950 |
1951 for (octave_idx_type i = 0; i < names.length (); i++) | |
1952 { | |
1953 std::string name = names (i); | |
1954 | |
1955 OCTAVE_QUIT; | |
1956 | |
1957 symbol_record *sr = lookup_by_name (name, 0); | |
5738 | 1958 if (sr && sr->is_defined () |
1959 && sr->type_name () != "overloaded function") | |
5447 | 1960 { |
1961 std::string h = sr->help (); | |
1962 | |
8021 | 1963 if (name.find (txt) != std::string::npos) |
5447 | 1964 { |
1965 if (nargout) | |
1966 { | |
1967 ret[0].append (name); | |
1968 ret[1].append (first_help_sentence (h)); | |
1969 } | |
1970 else | |
1971 print_lookfor (name, first_help_sentence (h)); | |
1972 } | |
5592 | 1973 else |
1974 { | |
1975 std::string s; | |
1976 | |
1977 if (first_sentence_only) | |
1978 s = first_help_sentence (h); | |
1979 else | |
1980 s = h; | |
1981 | |
5768 | 1982 std::transform (s.begin (), s.end (), s.begin (), tolower); |
5592 | 1983 |
8021 | 1984 if (s.length () > 0 && s.find (txt) != std::string::npos) |
5592 | 1985 { |
1986 if (nargout) | |
1987 { | |
1988 ret[0].append (name); | |
1989 ret[1].append (first_help_sentence (h)); | |
1990 } | |
1991 else | |
1992 print_lookfor (name, first_help_sentence (h)); | |
1993 } | |
1994 } | |
5447 | 1995 } |
1996 } | |
7336 | 1997 #endif |
5447 | 1998 |
5832 | 1999 string_vector dirs = load_path::dirs (); |
5447 | 2000 |
2001 int len = dirs.length (); | |
2002 | |
2003 for (int i = 0; i < len; i++) | |
2004 { | |
5832 | 2005 names = load_path::files (dirs[i]); |
5447 | 2006 |
2007 if (! names.empty ()) | |
2008 { | |
2009 for (int j = 0; j < names.length (); j++) | |
2010 { | |
2011 std::string name = names (j); | |
2012 | |
2013 OCTAVE_QUIT; | |
2014 | |
2015 // Strip extension | |
2016 size_t l = name.length (); | |
2017 if (l > 4 && name.substr (l-4) == ".oct") | |
2018 name = name.substr (0, l - 4); | |
2019 else if (l > 2 && name.substr (l-2) == ".m") | |
2020 name = name.substr (0, l - 2); | |
2021 else | |
2022 continue; | |
2023 | |
7336 | 2024 #ifdef OLD_SYMTAB |
5447 | 2025 // Check if already in symbol table |
2026 symbol_record *sr = fbi_sym_tab->lookup (name); | |
2027 | |
2028 if (!sr) | |
2029 { | |
2030 // Check if this version is first in the path | |
2031 | |
5832 | 2032 std::string file_name = load_path::find_fcn (name); |
2033 | |
7272 | 2034 std::string dir = dirs[i]; |
2035 | |
2036 if (! file_ops::is_dir_sep (dir[dir.length()-1])) | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
2037 dir += file_ops::dir_sep_str (); |
5832 | 2038 |
2039 if (file_name == dir + name + ".oct" | |
2040 || file_name == dir + name + ".m") | |
5447 | 2041 { |
5484 | 2042 bool symbol_found; |
2043 | |
5592 | 2044 std::string h; |
5832 | 2045 if (file_name == dir + name + ".oct") |
5592 | 2046 { |
2047 // oct-file. Must load to get help | |
2048 sr = lookup_by_name (name, false); | |
5447 | 2049 |
5592 | 2050 if (sr && sr->is_defined ()) |
2051 h = sr->help (); | |
2052 } | |
5447 | 2053 else |
5592 | 2054 h = get_help_from_file (file_name, symbol_found); |
5447 | 2055 |
8021 | 2056 if (name.find (txt) != std::string::npos) |
5447 | 2057 { |
2058 if (nargout) | |
2059 { | |
2060 ret[0].append (name); | |
2061 ret[1].append (first_help_sentence (h)); | |
2062 } | |
2063 else | |
2064 print_lookfor (name, first_help_sentence (h)); | |
2065 } | |
5592 | 2066 else |
2067 { | |
2068 std::string s; | |
2069 if (first_sentence_only) | |
2070 s = first_help_sentence (h); | |
2071 else | |
2072 s = h; | |
2073 | |
5768 | 2074 std::transform (s.begin (), s.end (), s.begin (), tolower); |
5592 | 2075 |
8021 | 2076 if (s.length () > 0 && s.find (txt) != std::string::npos) |
5592 | 2077 { |
2078 if (nargout) | |
2079 { | |
2080 ret[0].append (name); | |
2081 ret[1].append (first_help_sentence (h)); | |
2082 } | |
2083 else | |
2084 print_lookfor (name, first_help_sentence (h)); | |
2085 } | |
2086 } | |
2087 } | |
2088 } | |
7336 | 2089 #endif |
5592 | 2090 |
2091 // Check if this function has autoloaded functions attached to it | |
5832 | 2092 std::string file_name = load_path::find_fcn (name); |
2093 | |
5592 | 2094 string_vector autoload_fcns = reverse_lookup_autoload (file_name); |
2095 | |
2096 if (! autoload_fcns.empty ()) | |
2097 { | |
2098 for (int k = 0; k < autoload_fcns.length (); k++) | |
2099 { | |
2100 std::string aname = autoload_fcns (k); | |
2101 | |
7336 | 2102 #ifdef OLD_SYMTAB |
5592 | 2103 // Check if already in symbol table |
2104 sr = fbi_sym_tab->lookup (aname); | |
2105 | |
2106 if (!sr) | |
2107 { | |
2108 // Must load to get help | |
5738 | 2109 sr = lookup_by_name (aname, false); |
5592 | 2110 |
2111 std::string h; | |
2112 if (sr && sr->is_defined ()) | |
2113 h = sr->help (); | |
2114 | |
8021 | 2115 if (aname.find (txt) != std::string::npos) |
5592 | 2116 { |
2117 if (nargout) | |
2118 { | |
2119 ret[0].append (aname); | |
2120 ret[1].append (first_help_sentence (h)); | |
2121 } | |
2122 else | |
2123 print_lookfor (aname, first_help_sentence (h)); | |
2124 } | |
2125 else | |
2126 { | |
2127 std::string s; | |
2128 if (first_sentence_only) | |
2129 s = first_help_sentence (h); | |
2130 else | |
2131 s = h; | |
2132 | |
5768 | 2133 std::transform (s.begin (), s.end (), s.begin (), |
5592 | 2134 tolower); |
2135 | |
8021 | 2136 if (s.length () > 0 && s.find (txt) != std::string::npos) |
5592 | 2137 { |
2138 if (nargout) | |
2139 { | |
2140 ret[0].append (aname); | |
2141 ret[1].append (first_help_sentence (h)); | |
2142 } | |
2143 else | |
2144 print_lookfor (aname, first_help_sentence (h)); | |
2145 } | |
2146 } | |
2147 } | |
7336 | 2148 #endif |
5447 | 2149 } |
2150 } | |
2151 } | |
2152 } | |
2153 } | |
2154 | |
2155 if (nargout != 0) | |
2156 { | |
2157 retval (1) = ret[1]; | |
2158 retval (0) = ret[0]; | |
2159 } | |
2160 } | |
2161 else | |
2162 { | |
2163 error ("lookfor: argument must be a string"); | |
2164 } | |
2165 | |
2166 return retval; | |
2167 } | |
2168 | |
5794 | 2169 DEFUN (info_file, args, nargout, |
2170 "-*- texinfo -*-\n\ | |
2171 @deftypefn {Built-in Function} {@var{val} =} info_file ()\n\ | |
2172 @deftypefnx {Built-in Function} {@var{old_val} =} info_file (@var{new_val})\n\ | |
2173 Query or set the internal variable that specifies the name of the\n\ | |
2174 Octave info file. The default value is\n\ | |
2175 @code{\"@var{octave-home}/info/octave.info\"}, in\n\ | |
2176 which @var{octave-home} is the directory where all of Octave is installed.\n\ | |
2177 @seealso{info_program, doc, help, makeinfo_program}\n\ | |
2178 @end deftypefn") | |
2202 | 2179 { |
5794 | 2180 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (info_file); |
2202 | 2181 } |
2182 | |
5794 | 2183 DEFUN (info_program, args, nargout, |
2184 "-*- texinfo -*-\n\ | |
2185 @deftypefn {Built-in Function} {@var{val} =} info_program ()\n\ | |
2186 @deftypefnx {Built-in Function} {@var{old_val} =} info_program (@var{new_val})\n\ | |
2187 Query or set the internal variable that specifies the name of the\n\ | |
7096 | 2188 info program to run. The default value is\n\ |
3686 | 2189 @code{\"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info\"}\n\ |
2190 in which @var{octave-home} is the directory where all of Octave is\n\ | |
2191 installed, @var{version} is the Octave version number, and @var{arch}\n\ | |
2192 is the system type (for example, @code{i686-pc-linux-gnu}). The\n\ | |
2193 default initial value may be overridden by the environment variable\n\ | |
2194 @code{OCTAVE_INFO_PROGRAM}, or the command line argument\n\ | |
5794 | 2195 @code{--info-program NAME}.\n\ |
2196 @seealso{info_file, doc, help, makeinfo_program}\n\ | |
2197 @end deftypefn") | |
2198 { | |
2199 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (info_program); | |
2200 } | |
3686 | 2201 |
5794 | 2202 DEFUN (makeinfo_program, args, nargout, |
2203 "-*- texinfo -*-\n\ | |
2204 @deftypefn {Built-in Function} {@var{val} =} makeinfo_program ()\n\ | |
2205 @deftypefnx {Built-in Function} {@var{old_val} =} makeinfo_program (@var{new_val})\n\ | |
2206 Query or set the internal variable that specifies the name of the\n\ | |
2207 makeinfo program that Octave runs to format help text containing\n\ | |
2208 Texinfo markup commands. The default initial value is @code{\"makeinfo\"}.\n\ | |
2209 @seealso{info_file, info_program, doc, help}\n\ | |
2210 @end deftypefn") | |
2211 { | |
2212 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (makeinfo_program); | |
2213 } | |
2202 | 2214 |
5794 | 2215 DEFUN (suppress_verbose_help_message, args, nargout, |
2216 "-*- texinfo -*-\n\ | |
2217 @deftypefn {Built-in Function} {@var{val} =} suppress_verbose_help_message ()\n\ | |
2218 @deftypefnx {Built-in Function} {@var{old_val} =} suppress_verbose_help_message (@var{new_val})\n\ | |
7001 | 2219 Query or set the internal variable that controls whether Octave\n\ |
5794 | 2220 will add additional help information to the end of the output from\n\ |
3332 | 2221 the @code{help} command and usage messages for built-in commands.\n\ |
5794 | 2222 @end deftypefn") |
2223 { | |
2224 return SET_INTERNAL_VARIABLE (suppress_verbose_help_message); | |
2189 | 2225 } |
2226 | |
1 | 2227 /* |
2228 ;;; Local Variables: *** | |
2229 ;;; mode: C++ *** | |
2230 ;;; End: *** | |
2231 */ |