Mercurial > hg > octave-nkf
annotate src/help.cc @ 8503:8ba2ee57c594
remove qsort in favor of sort
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 13 Jan 2009 12:16:21 +0100 |
parents | fa78cb8d8a5c |
children | f134925a1cfa |
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 |
8503
8ba2ee57c594
remove qsort in favor of sort
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
649 symbols.sort (); |
3259 | 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 | |
8503
8ba2ee57c594
remove qsort in favor of sort
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
696 autoloaded.sort (); |
5592 | 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 { | |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
982 std::string dispatch_help = symbol_table::help_for_dispatch (nm); |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
983 |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
984 if (! dispatch_help.empty ()) |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
985 { |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
986 size_t pos = 0; |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
987 |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
988 std::string pfx = looks_like_texinfo (h, pos) |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
989 ? std::string ("\n\n@noindent\n") : std::string ("\n\n"); |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
990 |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
991 h += pfx + dispatch_help; |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
992 } |
6243 | 993 |
3355 | 994 display_help_text (os, h); |
6243 | 995 |
996 if (w.length () > 0 && ! Vsuppress_verbose_help_message) | |
997 os << w << "\n"; | |
998 | |
3355 | 999 os << "\n"; |
6243 | 1000 |
3355 | 1001 retval = true; |
1002 } | |
1003 } | |
1004 | |
1005 return retval; | |
1006 } | |
1007 | |
1008 static bool | |
6243 | 1009 raw_help_from_file (const std::string& nm, std::string& h, |
1010 std::string& file, bool& symbol_found) | |
1011 { | |
1012 bool retval = false; | |
1013 | |
1014 h = get_help_from_file (nm, symbol_found, file); | |
1015 | |
1016 if (h.length () > 0) | |
1017 retval = true; | |
8041
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1018 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
|
1019 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1020 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
|
1021 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1022 if (! file.empty ()) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1023 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1024 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
|
1025 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1026 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
|
1027 retval = true; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1028 } |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1029 } |
6243 | 1030 |
1031 return retval; | |
1032 } | |
1033 | |
1034 static bool | |
5399 | 1035 help_from_file (std::ostream& os, const std::string& nm, bool& symbol_found) |
3355 | 1036 { |
1037 bool retval = false; | |
1038 | |
6243 | 1039 std::string h; |
5931 | 1040 std::string file; |
1041 | |
6243 | 1042 if (raw_help_from_file (nm, h, file, symbol_found)) |
5484 | 1043 { |
6243 | 1044 if (h.length () > 0) |
1045 { | |
6253 | 1046 // Strip extension |
1047 size_t l = file.length (); | |
1048 if (l > 2 && file.substr (l-2) == ".m") | |
1049 { | |
1050 std::string tmp = file.substr (0, l - 2); | |
1051 | |
1052 if (file_stat (tmp + ".oct")) | |
1053 file = tmp + ".oct"; | |
1054 else if (file_stat (tmp + ".mex")) | |
1055 file = tmp + ".mex"; | |
1056 } | |
1057 | |
6243 | 1058 os << nm << " is the file " << file << "\n\n"; |
1059 | |
1060 display_help_text (os, h); | |
1061 | |
1062 os << "\n"; | |
1063 | |
1064 retval = true; | |
1065 } | |
3355 | 1066 } |
1067 | |
1068 return retval; | |
1069 } | |
1070 | |
6243 | 1071 std::string |
1072 raw_help (const std::string& nm, bool &symbol_found) | |
1073 { | |
1074 std::string h; | |
1075 std::string w; | |
1076 std::string f; | |
1077 | |
1078 (raw_help_from_list (operator_help (), nm, h, symbol_found) | |
1079 || raw_help_from_list (keyword_help (), nm, h, symbol_found) | |
1080 || raw_help_from_symbol_table (nm, h, w, symbol_found) | |
1081 || raw_help_from_file (nm, h, f, symbol_found)); | |
1082 | |
1083 return h; | |
1084 } | |
1085 | |
1140 | 1086 static void |
1755 | 1087 builtin_help (int argc, const string_vector& argv) |
1140 | 1088 { |
1089 help_list *op_help_list = operator_help (); | |
1090 help_list *kw_help_list = keyword_help (); | |
1091 | |
1755 | 1092 for (int i = 1; i < argc; i++) |
1140 | 1093 { |
5399 | 1094 bool symbol_found = false; |
1095 | |
1096 if (help_from_list (octave_stdout, op_help_list, argv[i], 0, | |
1097 symbol_found)) | |
1140 | 1098 continue; |
1099 | |
5399 | 1100 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0, |
1101 symbol_found)) | |
1140 | 1102 continue; |
1103 | |
5399 | 1104 if (help_from_symbol_table (octave_stdout, argv[i], symbol_found)) |
1105 continue; | |
1106 | |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1107 if (error_state) |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1108 { |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1109 octave_stdout << "\n"; |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1110 error_state = 0; |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1111 continue; |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1112 } |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1113 |
5399 | 1114 if (help_from_file (octave_stdout, argv[i], symbol_found)) |
3355 | 1115 continue; |
1755 | 1116 |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1117 if (error_state) |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1118 { |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1119 octave_stdout << "\n"; |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1120 error_state = 0; |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1121 continue; |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1122 } |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1123 |
5399 | 1124 if (symbol_found) |
1125 octave_stdout << "\nhelp: `" << argv[i] | |
1126 << "' is not documented\n"; | |
1127 else | |
1128 octave_stdout << "\nhelp: `" << argv[i] | |
1129 << "' not found\n"; | |
1140 | 1130 } |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1131 |
2095 | 1132 additional_help_message (octave_stdout); |
1140 | 1133 } |
1134 | |
4208 | 1135 DEFCMD (help, args, , |
3332 | 1136 "-*- texinfo -*-\n\ |
6615 | 1137 @deffn {Command} help @var{name}\n\ |
1138 Display the help text for @var{name}.\n\ | |
1139 If invoked without any arguments, @code{help} prints a list\n\ | |
1140 of all the available operators and functions.\n\ | |
3168 | 1141 \n\ |
3332 | 1142 For example, the command @kbd{help help} prints a short message\n\ |
6615 | 1143 describing the @code{help} command.\n\ |
3332 | 1144 \n\ |
6615 | 1145 The help command can give you information about operators, but not the\n\ |
1146 comma and semicolons that are used as command separators. To get help\n\ | |
1147 for those, you must type @kbd{help comma} or @kbd{help semicolon}.\n\ | |
5672 | 1148 @seealso{doc, which, lookfor}\n\ |
3333 | 1149 @end deffn") |
529 | 1150 { |
2086 | 1151 octave_value_list retval; |
529 | 1152 |
1755 | 1153 int argc = args.length () + 1; |
1154 | |
1968 | 1155 string_vector argv = args.make_argv ("help"); |
1755 | 1156 |
1157 if (error_state) | |
1158 return retval; | |
529 | 1159 |
1160 if (argc == 1) | |
3014 | 1161 simple_help (); |
529 | 1162 else |
1163 { | |
1755 | 1164 if (argv[1] == "-i") |
3014 | 1165 help_from_info (argv, 2, argc); |
529 | 1166 else |
3014 | 1167 builtin_help (argc, argv); |
529 | 1168 } |
1169 | |
1170 return retval; | |
1171 } | |
1172 | |
3355 | 1173 static void |
7336 | 1174 display_file (std::ostream& os, const std::string& name, |
1175 const std::string& fname, const std::string& type, | |
1176 bool pr_type_info, bool quiet) | |
1177 { | |
1178 std::ifstream fs (fname.c_str (), std::ios::in); | |
1179 | |
1180 if (fs) | |
1181 { | |
1182 if (pr_type_info && ! quiet) | |
7438 | 1183 os << name << " is the " << type << " defined from the file\n" |
1184 << fname << ":\n\n"; | |
7336 | 1185 |
1186 char ch; | |
1187 | |
1188 while (fs.get (ch)) | |
1189 os << ch; | |
1190 } | |
1191 else | |
1192 os << "unable to open `" << fname << "' for reading!\n"; | |
1193 } | |
1194 | |
1195 static void | |
3523 | 1196 do_type (std::ostream& os, const std::string& name, bool pr_type_info, |
3355 | 1197 bool quiet, bool pr_orig_txt) |
1198 { | |
7336 | 1199 // FIXME -- should we bother with variables here (earlier versions |
1200 // of Octave displayed them)? | |
1201 | |
1202 octave_value val = symbol_table::varval (name); | |
3355 | 1203 |
7336 | 1204 if (val.is_defined ()) |
1205 { | |
1206 if (pr_type_info && ! quiet) | |
1207 os << name << " is a variable\n"; | |
1208 | |
1209 val.print_raw (os, pr_orig_txt); | |
1210 | |
1211 if (pr_type_info) | |
1212 os << "\n"; | |
1213 } | |
3355 | 1214 else |
1215 { | |
7336 | 1216 val = symbol_table::find_function (name); |
3355 | 1217 |
7336 | 1218 if (val.is_defined ()) |
3355 | 1219 { |
7336 | 1220 octave_function *fcn = val.function_value (); |
1221 | |
7438 | 1222 if (fcn) |
1223 { | |
1224 std::string fn = fcn->fcn_file_name (); | |
3355 | 1225 |
7438 | 1226 if (fcn->is_builtin_function ()) |
1227 os << name << " is a built-in function" << std::endl; | |
1228 else if (fcn->is_dld_function () || fcn->is_mex_function ()) | |
1229 os << name | |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1230 << " is a dyanmically loaded function from the file\n" |
7438 | 1231 << fn << std::endl; |
1232 else if (pr_orig_txt && ! fn.empty ()) | |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1233 display_file (os, name, fn, |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1234 val.is_user_script () ? "script" : "function", |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1235 pr_type_info, quiet); |
7438 | 1236 else |
7336 | 1237 { |
7438 | 1238 if (pr_type_info && ! quiet) |
1239 { | |
1240 os << name; | |
3355 | 1241 |
7438 | 1242 if (fcn->is_user_function ()) |
1243 { | |
1244 if (fn.empty ()) | |
1245 os << " is a command-line function:\n\n"; | |
1246 else | |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1247 os << " is a " |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1248 << (val.is_user_script () |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1249 ? std::string ("script") |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1250 : std::string ("function")) |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1251 << " defined from the file\n" |
7438 | 1252 << fn << ":\n\n"; |
1253 } | |
1254 } | |
1255 | |
1256 tree_print_code tpc (os, "", pr_orig_txt); | |
1257 | |
1258 fcn->accept (tpc); | |
7336 | 1259 } |
3355 | 1260 } |
1261 } | |
1262 } | |
1263 } | |
1264 | |
4208 | 1265 DEFCMD (type, args, nargout, |
3361 | 1266 "-*- texinfo -*-\n\ |
1267 \n\ | |
1268 @deffn {Command} type options name @dots{}\n\ | |
1269 Display the definition of each @var{name} that refers to a function.\n\ | |
1270 \n\ | |
6620 | 1271 Normally also displays whether each @var{name} is user-defined or built-in;\n\ |
3361 | 1272 the @code{-q} option suppresses this behaviour.\n\ |
1273 @end deffn") | |
581 | 1274 { |
3584 | 1275 octave_value retval; |
1588 | 1276 |
1755 | 1277 int argc = args.length () + 1; |
1278 | |
1968 | 1279 string_vector argv = args.make_argv ("type"); |
1755 | 1280 |
3355 | 1281 if (! error_state) |
581 | 1282 { |
3355 | 1283 if (argc > 1) |
1284 { | |
5775 | 1285 // FIXME -- we should really use getopt () |
2532 | 1286 |
3355 | 1287 bool quiet = false; |
1288 bool pr_orig_txt = true; | |
2532 | 1289 |
3355 | 1290 int idx; |
581 | 1291 |
3355 | 1292 for (idx = 1; idx < argc; idx++) |
1281 | 1293 { |
3355 | 1294 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
1295 quiet = true; | |
1296 else if (argv[idx] == "-t" || argv[idx] == "-transformed") | |
1297 pr_orig_txt = false; | |
1298 else | |
1299 break; | |
1281 | 1300 } |
1301 | |
3355 | 1302 if (idx < argc) |
1303 { | |
5765 | 1304 std::ostringstream output_buf; |
581 | 1305 |
3355 | 1306 for (int i = idx; i < argc; i++) |
581 | 1307 { |
3523 | 1308 std::string id = argv[i]; |
2534 | 1309 |
3355 | 1310 if (nargout == 0) |
1311 do_type (octave_stdout, id, true, quiet, pr_orig_txt); | |
1312 else | |
1313 do_type (output_buf, id, false, quiet, pr_orig_txt); | |
581 | 1314 |
3355 | 1315 if (error_state) |
1316 goto abort; | |
581 | 1317 } |
1318 | |
3584 | 1319 if (nargout != 0) |
5765 | 1320 retval = output_buf.str (); |
581 | 1321 } |
1322 else | |
5823 | 1323 print_usage (); |
3355 | 1324 } |
1325 else | |
5823 | 1326 print_usage (); |
3355 | 1327 } |
3141 | 1328 |
3355 | 1329 abort: |
581 | 1330 |
1331 return retval; | |
1332 } | |
1333 | |
7082 | 1334 std::string |
3523 | 1335 do_which (const std::string& name) |
3355 | 1336 { |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1337 std::string retval; |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1338 |
7336 | 1339 octave_value val = symbol_table::find_function (name); |
3355 | 1340 |
7336 | 1341 if (val.is_defined ()) |
1342 { | |
1343 octave_function *fcn = val.function_value (); | |
3355 | 1344 |
7336 | 1345 if (fcn) |
1346 { | |
1347 std::string fn = fcn->fcn_file_name (); | |
3355 | 1348 |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1349 retval = fn.empty () |
7336 | 1350 ? (fcn->is_user_function () |
1351 ? "command-line function" : "built-in function") | |
1352 : fn; | |
1353 } | |
1354 } | |
1355 | |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1356 return retval; |
3355 | 1357 } |
1358 | |
1359 static void | |
3523 | 1360 do_which (std::ostream& os, const std::string& name) |
3355 | 1361 { |
7336 | 1362 std::string desc; |
3355 | 1363 |
7336 | 1364 octave_value val = symbol_table::find_function (name); |
1365 | |
1366 if (val.is_defined ()) | |
3355 | 1367 { |
7336 | 1368 octave_function *fcn = val.function_value (); |
1369 | |
1370 if (fcn) | |
1371 { | |
1372 desc = fcn->fcn_file_name (); | |
3355 | 1373 |
7336 | 1374 if (desc.empty ()) |
1375 { | |
1376 if (fcn->is_user_function ()) | |
1377 desc = "is a command-line function"; | |
1378 else | |
1379 desc = "is a built-in function"; | |
1380 } | |
1381 else | |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1382 desc = "is the " |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1383 + (val.is_user_script () |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1384 ? std::string ("script") : std::string ("function")) |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1385 + " from the file " + desc; |
7336 | 1386 } |
8044
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1387 |
faf0abc5fd51
handle errors in calls to help, type, and which
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
1388 os << "which: `" << name << "' " << desc << std::endl; |
7336 | 1389 } |
3355 | 1390 } |
1391 | |
4208 | 1392 DEFCMD (which, args, nargout, |
3361 | 1393 "-*- texinfo -*-\n\ |
1394 @deffn {Command} which name @dots{}\n\ | |
1395 Display the type of each @var{name}. If @var{name} is defined from a\n\ | |
1396 function file, the full name of the file is also displayed.\n\ | |
5667 | 1397 @seealso{help, lookfor}\n\ |
3361 | 1398 @end deffn") |
581 | 1399 { |
2086 | 1400 octave_value_list retval; |
581 | 1401 |
1968 | 1402 string_vector argv = args.make_argv ("which"); |
1755 | 1403 |
3355 | 1404 if (! error_state) |
1405 { | |
1406 int argc = argv.length (); | |
581 | 1407 |
1408 if (nargout > 0) | |
1409 retval.resize (argc-1, Matrix ()); | |
1410 | |
3355 | 1411 if (argc > 1) |
581 | 1412 { |
3355 | 1413 for (int i = 1; i < argc; i++) |
581 | 1414 { |
3523 | 1415 std::string id = argv[i]; |
3141 | 1416 |
3355 | 1417 if (nargout == 0) |
1418 do_which (octave_stdout, id); | |
581 | 1419 else |
3355 | 1420 retval(i-1) = do_which (id); |
581 | 1421 } |
1422 } | |
3355 | 1423 else |
5823 | 1424 print_usage (); |
581 | 1425 } |
1426 | |
1427 return retval; | |
1428 } | |
1429 | |
5775 | 1430 // FIXME |
5447 | 1431 // This function attempts to find the first sentence of a help string, though |
1432 // given that the user can create the help in an arbitrary format, your | |
1433 // success might vary.. it works much better with help string formated in | |
1434 // texinfo. Using regex might make this function much simpler. | |
1435 | |
1436 std::string | |
1437 first_help_sentence (const std::string& h, bool short_sentence = true) | |
1438 { | |
5738 | 1439 std::string retval; |
1440 | |
5447 | 1441 size_t pos = 0; |
1442 | |
1443 if (looks_like_texinfo (h, pos)) | |
5592 | 1444 { |
1445 // Get the parsed help string. | |
5447 | 1446 pos = 0; |
5765 | 1447 std::ostringstream os; |
5447 | 1448 display_help_text (os, h); |
1449 std::string h2 = os.str (); | |
1450 | |
1451 while (1) | |
1452 { | |
1453 // Skip leading whitespace and get new line | |
1454 pos = h2.find_first_not_of ("\n\t ", pos); | |
1455 | |
8021 | 1456 if (pos == std::string::npos) |
5447 | 1457 break; |
1458 | |
1459 size_t new_pos = h2.find_first_of ('\n', pos); | |
1460 std::string line = h2.substr (pos, new_pos-pos); | |
1461 | |
1462 // Skip lines starting in "-" | |
1463 if (line.find_first_of ('-') == 0) | |
1464 { | |
1465 pos = new_pos + 1; | |
1466 continue; | |
1467 } | |
1468 | |
1469 break; | |
1470 } | |
1471 | |
8021 | 1472 if (pos == std::string::npos) |
5738 | 1473 return retval; |
5447 | 1474 |
1475 // At start of real text. Get first line with the sentence | |
1476 size_t new_pos = h2.find_first_of ('\n', pos); | |
1477 std::string line = h2.substr (pos, new_pos-pos); | |
1478 size_t dot_pos; | |
1479 | |
8021 | 1480 while ((dot_pos = line.find_first_of ('.')) == std::string::npos) |
5447 | 1481 { |
1482 // Trim trailing blanks on line | |
1483 line.substr (0, line.find_last_not_of ("\n\t ") + 1); | |
1484 | |
1485 // Append next line | |
1486 size_t tmp_pos = h2.find_first_not_of ("\n\t ", new_pos + 1); | |
8021 | 1487 if (tmp_pos == std::string::npos || h2.substr (tmp_pos, 1) == "\n") |
5447 | 1488 break; |
1489 | |
1490 new_pos = h2.find_first_of ('\n', tmp_pos); | |
1491 std::string next = h2.substr (tmp_pos, new_pos-tmp_pos); | |
1492 | |
1493 if (short_sentence) | |
1494 { | |
8021 | 1495 if ((tmp_pos = next.find_first_of ('.')) != std::string::npos) |
5447 | 1496 { |
1497 line = line + " " + next; | |
1498 dot_pos = line.find_first_of ('.'); | |
1499 } | |
1500 break; | |
1501 } | |
1502 else | |
1503 line = line + " " + next; | |
1504 } | |
1505 | |
8021 | 1506 if (dot_pos == std::string::npos) |
5738 | 1507 retval = line; |
5447 | 1508 else |
5738 | 1509 retval = line.substr (0, dot_pos + 1); |
5447 | 1510 } |
1511 else | |
1512 { | |
1513 std::string _upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
1514 std::string _lower = "abcdefghijklmnopqrstuvwxyz"; | |
1515 std::string _alpha = _upper + _lower + "_"; | |
1516 std::string _alphanum = _alpha + "1234567890"; | |
1517 pos = 0; | |
1518 | |
1519 while (1) | |
1520 { | |
1521 // Skip leading whitespace and get new line | |
1522 pos = h.find_first_not_of ("\n\t ", pos); | |
1523 | |
8021 | 1524 if (pos == std::string::npos) |
5447 | 1525 break; |
1526 | |
1527 size_t new_pos = h.find_first_of ('\n', pos); | |
1528 std::string line = h.substr (pos, new_pos-pos); | |
1529 | |
1530 // Make a lower case copy to simplify some tests | |
1531 std::string lower = line; | |
5768 | 1532 std::transform (lower.begin (), lower.end (), lower.begin (), tolower); |
5447 | 1533 |
1534 // Skip lines starting in "-" or "Usage" | |
1535 if (lower.find_first_of ('-') == 0 | |
1536 || lower.substr (0, 5) == "usage") | |
1537 { | |
8021 | 1538 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1539 continue; |
1540 } | |
1541 | |
1542 size_t line_pos = 0; | |
1543 size_t tmp_pos = 0; | |
1544 | |
1545 // chop " blah : " | |
1546 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of | |
1547 (_alphanum, line_pos)); | |
8021 | 1548 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == ":") |
5447 | 1549 line_pos = line.find_first_not_of ("\t ", tmp_pos + 1); |
1550 | |
8021 | 1551 if (line_pos == std::string::npos) |
5447 | 1552 { |
8021 | 1553 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1554 continue; |
1555 } | |
1556 | |
1557 // chop " function " | |
1558 if (lower.substr (line_pos, 8) == "function") | |
1559 line_pos = line.find_first_not_of ("\t ", line_pos + 8); | |
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,b] = " | |
1568 if (line.substr (line_pos, 1) == "[") | |
1569 { | |
1570 tmp_pos = line.find_first_not_of | |
1571 ("\t ", line.find_first_of ("]", line_pos) + 1); | |
1572 | |
8021 | 1573 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == "=") |
5447 | 1574 line_pos = line.find_first_not_of ("\t ",tmp_pos + 1); |
1575 } | |
1576 | |
8021 | 1577 if (line_pos == std::string::npos) |
5447 | 1578 { |
8021 | 1579 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1580 continue; |
1581 } | |
1582 | |
1583 // chop " a = " | |
1584 if (line.find_first_not_of (_alpha, line_pos) != line_pos) | |
1585 { | |
1586 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of | |
1587 (_alphanum, line_pos)); | |
8021 | 1588 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == "=") |
5447 | 1589 line_pos = line.find_first_not_of ("\t ", tmp_pos + 1); |
1590 } | |
1591 | |
8021 | 1592 if (line_pos == std::string::npos) |
5447 | 1593 { |
1594 pos = new_pos + 1; | |
1595 continue; | |
1596 } | |
1597 | |
1598 // chop " f(x) " | |
1599 if (line.find_first_not_of (_alpha, line_pos) != line_pos) | |
1600 { | |
1601 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of | |
1602 (_alphanum, line_pos)); | |
8021 | 1603 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == "(") |
5447 | 1604 line_pos = line.find_first_not_of ("\t ", line.find_first_of |
1605 (")", tmp_pos) + 1); | |
1606 } | |
1607 | |
8021 | 1608 if (line_pos == std::string::npos) |
5447 | 1609 { |
8021 | 1610 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1611 continue; |
1612 } | |
1613 | |
1614 // chop " ; " | |
1615 if (line.substr (line_pos, 1) == ":" | |
1616 || line.substr (line_pos, 1) == ";") | |
1617 line_pos = line.find_first_not_of ("\t ", line_pos + 1); | |
1618 | |
8021 | 1619 if (line_pos == std::string::npos) |
5447 | 1620 { |
8021 | 1621 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1622 continue; |
1623 } | |
1624 | |
1625 // chop " BLAH " | |
1626 if (line.length () > line_pos + 2 | |
1627 && line.find_first_of (_upper, line_pos) == line_pos | |
1628 && line.find_first_of (_upper, line_pos+1) == line_pos + 1) | |
1629 line_pos = line.find_first_not_of ("\t ", line.find_first_not_of | |
1630 (_upper + "0123456789_", line_pos)); | |
1631 | |
8021 | 1632 if (line_pos == std::string::npos) |
5447 | 1633 { |
8021 | 1634 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1635 continue; |
1636 } | |
1637 | |
1638 // chop " blah --- " | |
1639 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of | |
1640 (_alphanum, line_pos)); | |
8021 | 1641 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == "-") |
5447 | 1642 { |
1643 tmp_pos = line.find_first_not_of ("-", tmp_pos); | |
1644 if (line.substr (tmp_pos, 1) == " " | |
1645 || line.substr (tmp_pos, 1) == "\t") | |
1646 line_pos = line.find_first_not_of ("\t ", tmp_pos); | |
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 <TAB> " | |
1656 if (line.find_first_not_of (_alpha, line_pos) != line_pos) | |
1657 { | |
1658 tmp_pos = line.find_first_not_of (" ", line.find_first_not_of | |
1659 (_alphanum, line_pos)); | |
8021 | 1660 if (tmp_pos != std::string::npos && line.substr (tmp_pos, 1) == "\t") |
5447 | 1661 line_pos = line.find_first_not_of ("\t ", line.find_first_of |
1662 (")", tmp_pos) + 1); | |
1663 } | |
1664 | |
8021 | 1665 if (line_pos == std::string::npos) |
5447 | 1666 { |
8021 | 1667 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1668 continue; |
1669 } | |
1670 | |
1671 // chop " blah " | |
1672 if (line.find_first_not_of (_alpha, line_pos) != line_pos) | |
1673 { | |
1674 tmp_pos = line.find_first_not_of (_alphanum, line_pos); | |
1675 | |
8021 | 1676 if (tmp_pos != std::string::npos |
5447 | 1677 && (line.substr (tmp_pos, 2) == "\t\t" |
1678 || line.substr (tmp_pos, 2) == "\t " | |
1679 || line.substr (tmp_pos, 2) == " \t" | |
1680 || line.substr (tmp_pos, 2) == " ")) | |
1681 line_pos = line.find_first_not_of ("\t ", tmp_pos); | |
1682 } | |
1683 | |
8021 | 1684 if (line_pos == std::string::npos) |
5447 | 1685 { |
8021 | 1686 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1687 continue; |
1688 } | |
1689 | |
1690 // skip blah \n or \n blah | |
1691 // skip blank line | |
1692 // skip "# !/usr/bin/octave" | |
1693 if ((line.substr (line_pos , 2) == "or" | |
8021 | 1694 && line.find_first_not_of ("\n\t ", line_pos + 2) == std::string::npos) |
1695 || line.find_first_not_of ("\n\t ", line_pos) == std::string::npos | |
5447 | 1696 || line.substr (line_pos, 2) == "!/") |
1697 { | |
8021 | 1698 pos = (new_pos == std::string::npos ? std::string::npos : new_pos + 1); |
5447 | 1699 continue; |
1700 } | |
1701 | |
1702 // Got the start of first sentence, break. | |
1703 pos = pos + line_pos; | |
1704 break; | |
1705 } | |
1706 | |
8021 | 1707 if (pos == std::string::npos) |
5738 | 1708 return retval; |
5447 | 1709 |
1710 // At start of real text. Get first line with the sentence | |
1711 size_t new_pos = h.find_first_of ('\n', pos); | |
1712 std::string line = h.substr (pos, new_pos-pos); | |
1713 size_t dot_pos; | |
1714 | |
8021 | 1715 while ((dot_pos = line.find_first_of ('.')) == std::string::npos) |
5447 | 1716 { |
1717 // Trim trailing blanks on line | |
1718 line = line.substr (0, line.find_last_not_of ("\n\t ") + 1); | |
1719 | |
1720 // Append next line | |
1721 size_t tmp_pos = h.find_first_not_of ("\t ", new_pos + 1); | |
8021 | 1722 if (tmp_pos == std::string::npos || h.substr (tmp_pos, 1) == "\n") |
5447 | 1723 break; |
1724 | |
1725 new_pos = h.find_first_of ('\n', tmp_pos); | |
1726 std::string next = h.substr (tmp_pos, new_pos-tmp_pos); | |
1727 | |
1728 if (short_sentence) | |
1729 { | |
1730 // Only add the next line if it terminates the sentence, then break | |
8021 | 1731 if ((tmp_pos = next.find_first_of ('.')) != std::string::npos) |
5447 | 1732 { |
1733 line = line + " " + next; | |
1734 dot_pos = line.find_first_of ('.'); | |
1735 } | |
1736 break; | |
1737 } | |
1738 else | |
1739 line = line + " " + next; | |
1740 } | |
1741 | |
8021 | 1742 if (dot_pos == std::string::npos) |
5738 | 1743 retval = line; |
5447 | 1744 else |
5738 | 1745 retval = line.substr (0, dot_pos + 1); |
5447 | 1746 } |
5738 | 1747 |
1748 return retval; | |
5447 | 1749 } |
1750 | |
1751 static void | |
1752 print_lookfor (const std::string& name, const std::string& line) | |
1753 { | |
1754 const size_t deflen = 20; | |
1755 | |
1756 size_t max_width = command_editor::terminal_cols () - deflen; | |
1757 if (max_width < deflen) | |
1758 max_width = deflen; | |
1759 | |
1760 size_t name_len = name.length (); | |
1761 | |
1762 size_t width = max_width; | |
1763 if (name_len > deflen) | |
1764 { | |
1765 width = command_editor::terminal_cols () - name_len; | |
1766 if (width < deflen) | |
1767 width = deflen; | |
1768 } | |
1769 | |
1770 size_t pad_len = deflen > name_len ? deflen - name_len + 1 : 1; | |
1771 octave_stdout << name << std::string (pad_len, ' '); | |
1772 | |
1773 size_t pos = 0; | |
1774 | |
1775 while (1) | |
1776 { | |
1777 size_t new_pos = line.find_first_of ("\n\t ", pos); | |
1778 size_t end_pos = new_pos; | |
1779 | |
1780 if (line.length () - pos < width) | |
8021 | 1781 new_pos = end_pos = std::string::npos; |
5447 | 1782 else |
8021 | 1783 while (new_pos != std::string::npos && new_pos - pos < width) |
5447 | 1784 { |
1785 end_pos = new_pos; | |
1786 new_pos = line.find_first_of ("\n\t ", new_pos + 1); | |
1787 } | |
1788 | |
1789 octave_stdout << line.substr (pos, end_pos-pos) << std::endl; | |
1790 | |
8021 | 1791 if (end_pos == std::string::npos) |
5447 | 1792 break; |
1793 | |
1794 pos = end_pos + 1; | |
1795 width = max_width; | |
1796 octave_stdout << std::string (deflen + 1, ' '); | |
1797 } | |
1798 } | |
1799 | |
1800 DEFCMD (lookfor, args, nargout, | |
1801 "-*- texinfo -*-\n\ | |
1802 @deffn {Command} lookfor @var{str}\n\ | |
1803 @deffnx {Command} lookfor -all @var{str}\n\ | |
1804 @deffnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor (@var{str})\n\ | |
1805 @deffnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor ('-all', @var{str})\n\ | |
5814 | 1806 Search for the string @var{str} in all of the functions found in the\n\ |
7001 | 1807 function search path. By default @code{lookfor} searches for @var{str}\n\ |
5814 | 1808 in the first sentence of the help string of each function found. The entire\n\ |
7001 | 1809 help string of each function found in the path can be searched if\n\ |
5447 | 1810 the '-all' argument is supplied. All searches are case insensitive.\n\ |
1811 \n\ | |
1812 Called with no output arguments, @code{lookfor} prints the list of matching\n\ | |
1813 functions to the terminal. Otherwise the output arguments @var{fun} and\n\ | |
1814 @var{helpstring} define the matching functions and the first sentence of\n\ | |
1815 each of their help strings.\n\ | |
1816 \n\ | |
1817 Note that the ability of @code{lookfor} to correctly identify the first\n\ | |
1818 sentence of the help of the functions is dependent on the format of the\n\ | |
6620 | 1819 functions help. All of the functions in Octave itself will correctly\n\ |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8044
diff
changeset
|
1820 find the first sentence, but the same cannot be guaranteed for other\n\ |
5447 | 1821 functions. Therefore the use of the '-all' argument might be necessary\n\ |
6620 | 1822 to find related functions that are not part of Octave.\n\ |
5667 | 1823 @seealso{help, which}\n\ |
5642 | 1824 @end deffn") |
5447 | 1825 { |
1826 octave_value_list retval; | |
7336 | 1827 |
5447 | 1828 int nargin = args.length (); |
1829 bool first_sentence_only = true; | |
1830 | |
1831 if (nargin != 1 && nargin != 2) | |
1832 { | |
7038 | 1833 print_usage (); |
5447 | 1834 return retval; |
1835 } | |
1836 | |
1837 string_vector ret[2]; | |
1838 | |
1839 std::string txt; | |
1840 | |
1841 if (args(0).is_string ()) | |
1842 { | |
1843 txt = args(0).string_value (); | |
1844 | |
1845 if (nargin == 2) | |
1846 { | |
1847 if (args(1).is_string ()) | |
1848 { | |
1849 std::string tmp = args(1).string_value (); | |
1850 | |
1851 if (txt.substr(0,1) == "-") | |
1852 { | |
1853 txt = tmp; | |
1854 tmp = args(0).string_value (); | |
1855 } | |
1856 | |
1857 if (tmp == "-all") | |
1858 first_sentence_only = false; | |
1859 else | |
1860 error ("lookfor: unrecognized option argument"); | |
1861 } | |
1862 else | |
1863 error ("lookfor: arguments must be a string"); | |
1864 } | |
1865 } | |
1866 else | |
1867 error ("lookfor: argument must be a string"); | |
1868 | |
1869 if (!error_state) | |
1870 { | |
1871 // All tests in lower case | |
5768 | 1872 std::transform (txt.begin (), txt.end (), txt.begin (), tolower); |
5447 | 1873 |
1874 help_list *ptr = keyword_help (); | |
1875 while (ptr->name) | |
1876 { | |
1877 std::string name = ptr->name; | |
1878 std::string h = ptr->help; | |
1879 | |
8021 | 1880 if (name.find (txt) != std::string::npos) |
5447 | 1881 { |
1882 if (nargout) | |
1883 { | |
1884 ret[0].append (name); | |
1885 ret[1].append (first_help_sentence (h)); | |
1886 } | |
1887 else | |
1888 print_lookfor (name, first_help_sentence (h)); | |
1889 } | |
5592 | 1890 else |
1891 { | |
1892 std::string s; | |
1893 | |
1894 if (first_sentence_only) | |
1895 s = first_help_sentence (h); | |
1896 else | |
1897 s = h; | |
1898 | |
5768 | 1899 std::transform (s.begin (), s.end (), s.begin (), tolower); |
5592 | 1900 |
8021 | 1901 if (s.length () > 0 && s.find (txt) != std::string::npos) |
5592 | 1902 { |
1903 if (nargout) | |
1904 { | |
1905 ret[0].append (name); | |
1906 ret[1].append (first_help_sentence (h)); | |
1907 } | |
1908 else | |
1909 print_lookfor (name, first_help_sentence (h)); | |
1910 } | |
1911 } | |
5447 | 1912 |
1913 OCTAVE_QUIT; | |
1914 | |
1915 ptr++; | |
1916 } | |
1917 | |
1918 ptr = operator_help (); | |
1919 while (ptr->name) | |
1920 { | |
1921 std::string name = ptr->name; | |
1922 std::string h = ptr->help; | |
1923 | |
8021 | 1924 if (name.find (txt) != std::string::npos) |
5447 | 1925 { |
1926 if (nargout) | |
1927 { | |
1928 ret[0].append (name); | |
1929 ret[1].append (first_help_sentence (h)); | |
1930 } | |
1931 else | |
1932 print_lookfor (name, first_help_sentence (h)); | |
1933 } | |
5592 | 1934 else |
1935 { | |
1936 std::string s; | |
1937 if (first_sentence_only) | |
1938 s = first_help_sentence (h); | |
1939 else | |
1940 s = h; | |
1941 | |
5768 | 1942 std::transform (s.begin (), s.end (), s.begin (), tolower); |
5592 | 1943 |
8021 | 1944 if (s.length () > 0 && s.find (txt) != std::string::npos) |
5592 | 1945 { |
1946 if (nargout) | |
1947 { | |
1948 ret[0].append (name); | |
1949 ret[1].append (first_help_sentence (h)); | |
1950 } | |
1951 else | |
1952 print_lookfor (name, first_help_sentence (h)); | |
1953 } | |
1954 } | |
5447 | 1955 |
1956 OCTAVE_QUIT; | |
1957 | |
1958 ptr++; | |
1959 } | |
1960 | |
7336 | 1961 string_vector names; |
1962 | |
1963 #ifdef OLD_SYMTAB | |
5447 | 1964 // Check the symbol record table |
7336 | 1965 names = fbi_sym_tab->name_list (string_vector (), true); |
5447 | 1966 |
1967 for (octave_idx_type i = 0; i < names.length (); i++) | |
1968 { | |
1969 std::string name = names (i); | |
1970 | |
1971 OCTAVE_QUIT; | |
1972 | |
1973 symbol_record *sr = lookup_by_name (name, 0); | |
5738 | 1974 if (sr && sr->is_defined () |
1975 && sr->type_name () != "overloaded function") | |
5447 | 1976 { |
1977 std::string h = sr->help (); | |
1978 | |
8021 | 1979 if (name.find (txt) != std::string::npos) |
5447 | 1980 { |
1981 if (nargout) | |
1982 { | |
1983 ret[0].append (name); | |
1984 ret[1].append (first_help_sentence (h)); | |
1985 } | |
1986 else | |
1987 print_lookfor (name, first_help_sentence (h)); | |
1988 } | |
5592 | 1989 else |
1990 { | |
1991 std::string s; | |
1992 | |
1993 if (first_sentence_only) | |
1994 s = first_help_sentence (h); | |
1995 else | |
1996 s = h; | |
1997 | |
5768 | 1998 std::transform (s.begin (), s.end (), s.begin (), tolower); |
5592 | 1999 |
8021 | 2000 if (s.length () > 0 && s.find (txt) != std::string::npos) |
5592 | 2001 { |
2002 if (nargout) | |
2003 { | |
2004 ret[0].append (name); | |
2005 ret[1].append (first_help_sentence (h)); | |
2006 } | |
2007 else | |
2008 print_lookfor (name, first_help_sentence (h)); | |
2009 } | |
2010 } | |
5447 | 2011 } |
2012 } | |
7336 | 2013 #endif |
5447 | 2014 |
5832 | 2015 string_vector dirs = load_path::dirs (); |
5447 | 2016 |
2017 int len = dirs.length (); | |
2018 | |
2019 for (int i = 0; i < len; i++) | |
2020 { | |
5832 | 2021 names = load_path::files (dirs[i]); |
5447 | 2022 |
2023 if (! names.empty ()) | |
2024 { | |
2025 for (int j = 0; j < names.length (); j++) | |
2026 { | |
2027 std::string name = names (j); | |
2028 | |
2029 OCTAVE_QUIT; | |
2030 | |
2031 // Strip extension | |
2032 size_t l = name.length (); | |
2033 if (l > 4 && name.substr (l-4) == ".oct") | |
2034 name = name.substr (0, l - 4); | |
2035 else if (l > 2 && name.substr (l-2) == ".m") | |
2036 name = name.substr (0, l - 2); | |
2037 else | |
2038 continue; | |
2039 | |
7336 | 2040 #ifdef OLD_SYMTAB |
5447 | 2041 // Check if already in symbol table |
2042 symbol_record *sr = fbi_sym_tab->lookup (name); | |
2043 | |
2044 if (!sr) | |
2045 { | |
2046 // Check if this version is first in the path | |
2047 | |
5832 | 2048 std::string file_name = load_path::find_fcn (name); |
2049 | |
7272 | 2050 std::string dir = dirs[i]; |
2051 | |
2052 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
|
2053 dir += file_ops::dir_sep_str (); |
5832 | 2054 |
2055 if (file_name == dir + name + ".oct" | |
2056 || file_name == dir + name + ".m") | |
5447 | 2057 { |
5484 | 2058 bool symbol_found; |
2059 | |
5592 | 2060 std::string h; |
5832 | 2061 if (file_name == dir + name + ".oct") |
5592 | 2062 { |
2063 // oct-file. Must load to get help | |
2064 sr = lookup_by_name (name, false); | |
5447 | 2065 |
5592 | 2066 if (sr && sr->is_defined ()) |
2067 h = sr->help (); | |
2068 } | |
5447 | 2069 else |
5592 | 2070 h = get_help_from_file (file_name, symbol_found); |
5447 | 2071 |
8021 | 2072 if (name.find (txt) != std::string::npos) |
5447 | 2073 { |
2074 if (nargout) | |
2075 { | |
2076 ret[0].append (name); | |
2077 ret[1].append (first_help_sentence (h)); | |
2078 } | |
2079 else | |
2080 print_lookfor (name, first_help_sentence (h)); | |
2081 } | |
5592 | 2082 else |
2083 { | |
2084 std::string s; | |
2085 if (first_sentence_only) | |
2086 s = first_help_sentence (h); | |
2087 else | |
2088 s = h; | |
2089 | |
5768 | 2090 std::transform (s.begin (), s.end (), s.begin (), tolower); |
5592 | 2091 |
8021 | 2092 if (s.length () > 0 && s.find (txt) != std::string::npos) |
5592 | 2093 { |
2094 if (nargout) | |
2095 { | |
2096 ret[0].append (name); | |
2097 ret[1].append (first_help_sentence (h)); | |
2098 } | |
2099 else | |
2100 print_lookfor (name, first_help_sentence (h)); | |
2101 } | |
2102 } | |
2103 } | |
2104 } | |
7336 | 2105 #endif |
5592 | 2106 |
2107 // Check if this function has autoloaded functions attached to it | |
5832 | 2108 std::string file_name = load_path::find_fcn (name); |
2109 | |
5592 | 2110 string_vector autoload_fcns = reverse_lookup_autoload (file_name); |
2111 | |
2112 if (! autoload_fcns.empty ()) | |
2113 { | |
2114 for (int k = 0; k < autoload_fcns.length (); k++) | |
2115 { | |
2116 std::string aname = autoload_fcns (k); | |
2117 | |
7336 | 2118 #ifdef OLD_SYMTAB |
5592 | 2119 // Check if already in symbol table |
2120 sr = fbi_sym_tab->lookup (aname); | |
2121 | |
2122 if (!sr) | |
2123 { | |
2124 // Must load to get help | |
5738 | 2125 sr = lookup_by_name (aname, false); |
5592 | 2126 |
2127 std::string h; | |
2128 if (sr && sr->is_defined ()) | |
2129 h = sr->help (); | |
2130 | |
8021 | 2131 if (aname.find (txt) != std::string::npos) |
5592 | 2132 { |
2133 if (nargout) | |
2134 { | |
2135 ret[0].append (aname); | |
2136 ret[1].append (first_help_sentence (h)); | |
2137 } | |
2138 else | |
2139 print_lookfor (aname, first_help_sentence (h)); | |
2140 } | |
2141 else | |
2142 { | |
2143 std::string s; | |
2144 if (first_sentence_only) | |
2145 s = first_help_sentence (h); | |
2146 else | |
2147 s = h; | |
2148 | |
5768 | 2149 std::transform (s.begin (), s.end (), s.begin (), |
5592 | 2150 tolower); |
2151 | |
8021 | 2152 if (s.length () > 0 && s.find (txt) != std::string::npos) |
5592 | 2153 { |
2154 if (nargout) | |
2155 { | |
2156 ret[0].append (aname); | |
2157 ret[1].append (first_help_sentence (h)); | |
2158 } | |
2159 else | |
2160 print_lookfor (aname, first_help_sentence (h)); | |
2161 } | |
2162 } | |
2163 } | |
7336 | 2164 #endif |
5447 | 2165 } |
2166 } | |
2167 } | |
2168 } | |
2169 } | |
2170 | |
2171 if (nargout != 0) | |
2172 { | |
2173 retval (1) = ret[1]; | |
2174 retval (0) = ret[0]; | |
2175 } | |
2176 } | |
2177 else | |
2178 { | |
2179 error ("lookfor: argument must be a string"); | |
2180 } | |
2181 | |
2182 return retval; | |
2183 } | |
2184 | |
5794 | 2185 DEFUN (info_file, args, nargout, |
2186 "-*- texinfo -*-\n\ | |
2187 @deftypefn {Built-in Function} {@var{val} =} info_file ()\n\ | |
2188 @deftypefnx {Built-in Function} {@var{old_val} =} info_file (@var{new_val})\n\ | |
2189 Query or set the internal variable that specifies the name of the\n\ | |
2190 Octave info file. The default value is\n\ | |
2191 @code{\"@var{octave-home}/info/octave.info\"}, in\n\ | |
2192 which @var{octave-home} is the directory where all of Octave is installed.\n\ | |
2193 @seealso{info_program, doc, help, makeinfo_program}\n\ | |
2194 @end deftypefn") | |
2202 | 2195 { |
5794 | 2196 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (info_file); |
2202 | 2197 } |
2198 | |
5794 | 2199 DEFUN (info_program, args, nargout, |
2200 "-*- texinfo -*-\n\ | |
2201 @deftypefn {Built-in Function} {@var{val} =} info_program ()\n\ | |
2202 @deftypefnx {Built-in Function} {@var{old_val} =} info_program (@var{new_val})\n\ | |
2203 Query or set the internal variable that specifies the name of the\n\ | |
7096 | 2204 info program to run. The default value is\n\ |
3686 | 2205 @code{\"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info\"}\n\ |
2206 in which @var{octave-home} is the directory where all of Octave is\n\ | |
2207 installed, @var{version} is the Octave version number, and @var{arch}\n\ | |
2208 is the system type (for example, @code{i686-pc-linux-gnu}). The\n\ | |
2209 default initial value may be overridden by the environment variable\n\ | |
2210 @code{OCTAVE_INFO_PROGRAM}, or the command line argument\n\ | |
5794 | 2211 @code{--info-program NAME}.\n\ |
2212 @seealso{info_file, doc, help, makeinfo_program}\n\ | |
2213 @end deftypefn") | |
2214 { | |
2215 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (info_program); | |
2216 } | |
3686 | 2217 |
5794 | 2218 DEFUN (makeinfo_program, args, nargout, |
2219 "-*- texinfo -*-\n\ | |
2220 @deftypefn {Built-in Function} {@var{val} =} makeinfo_program ()\n\ | |
2221 @deftypefnx {Built-in Function} {@var{old_val} =} makeinfo_program (@var{new_val})\n\ | |
2222 Query or set the internal variable that specifies the name of the\n\ | |
2223 makeinfo program that Octave runs to format help text containing\n\ | |
2224 Texinfo markup commands. The default initial value is @code{\"makeinfo\"}.\n\ | |
2225 @seealso{info_file, info_program, doc, help}\n\ | |
2226 @end deftypefn") | |
2227 { | |
2228 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (makeinfo_program); | |
2229 } | |
2202 | 2230 |
5794 | 2231 DEFUN (suppress_verbose_help_message, args, nargout, |
2232 "-*- texinfo -*-\n\ | |
2233 @deftypefn {Built-in Function} {@var{val} =} suppress_verbose_help_message ()\n\ | |
2234 @deftypefnx {Built-in Function} {@var{old_val} =} suppress_verbose_help_message (@var{new_val})\n\ | |
7001 | 2235 Query or set the internal variable that controls whether Octave\n\ |
5794 | 2236 will add additional help information to the end of the output from\n\ |
3332 | 2237 the @code{help} command and usage messages for built-in commands.\n\ |
5794 | 2238 @end deftypefn") |
2239 { | |
2240 return SET_INTERNAL_VARIABLE (suppress_verbose_help_message); | |
2189 | 2241 } |
2242 | |
1 | 2243 /* |
2244 ;;; Local Variables: *** | |
2245 ;;; mode: C++ *** | |
2246 ;;; End: *** | |
2247 */ |