1 ## Copyright (C) 2009-2012 S�ren Hauberg
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} print_usage ()
21 ## @deftypefnx {Function File} {} print_usage (@var{name})
22 ## Print the usage message for a function. When called with no input arguments
23 ## the @code{print_usage} function displays the usage message of the currently
24 ## executing function.
28 function print_usage (name)
32 ## Determine the name of the calling function
36 error ("print_usage: invalid function\n");
38 fullpath = evalin ("caller", "mfilename (""fullpath"")");
39 if (strcmp (fullpath(end-length(name)+1:end), name))
40 fullname = [fullpath, ".m"];
42 elseif (!ischar (name))
43 error ("print_usage: input argument must be a string");
48 ## Determine if we're called from top level.
49 at_toplev = length (x) < 2 || (length (x) == 2 && strcmp (x(2).name, name));
52 [text, format] = get_help_text (fullname);
54 switch (lower (format))
56 [usage_string, status] = get_usage_plain_text (text, max_len);
58 [usage_string, status] = get_usage_texinfo (text, max_len);
60 [usage_string, status] = get_usage_html (text, max_len);
62 error ("print_usage: `%s' is not documented\n", name);
64 error ("print_usage: `%s' not found\n", name);
66 error ("print_usage: internal error: unsupported help text format: '%s'\n", format);
69 ## Raise the final error
71 warning ("print_usage: Texinfo formatting filter exited abnormally");
72 warning ("print_usage: raw Texinfo source of help text follows...\n");
76 error ("Invalid call to %s. Correct usage is:\n\n%s\n%s",
77 name, usage_string, __additional_help_message__ ());
79 msg = sprintf ("Invalid call to %s. Correct usage is:\n\n%s",
81 ## Ensure that the error doesn't end up with a newline, as that disables
92 function [retval, status] = get_usage_plain_text (help_text, max_len)
93 ## Extract first line by searching for a double line-end.
94 line_end_idx = strfind (help_text, "\n\n");
95 retval = help_text (1:min ([line_end_idx , max_len, length(help_text)]));
99 function [retval, status] = get_usage_texinfo (help_text, max_len)
100 ## Lines ending with "@\n" are continuation lines, so they should be
101 ## concatenated with the following line.
102 help_text = strrep (help_text, "@\n", " ");
104 ## Find, and keep, lines that start with @def or @end def. This should include things
105 ## such as @deftypefn, @deftypefnx, @defvar, etc. and their corresponding @end's
106 def_idx = strfind (help_text, "@def");
107 if (!isempty (def_idx))
109 endl_idx = find (help_text == "\n");
110 for k = 1:length (def_idx)
111 endl = endl_idx (find (endl_idx > def_idx (k), 1));
113 buffer = strcat (buffer, help_text (def_idx (k):end), "\n");
115 buffer = strcat (buffer, help_text (def_idx (k):endl));
119 end_def_idx = strfind (help_text, "@end def");
120 if (!isempty (end_def_idx))
121 buffer = strcat (buffer, help_text (end_def_idx:end));
124 [retval, status] = get_usage_plain_text (help_text, max_len);
127 ## Run makeinfo to generate plain text
128 [retval, status] = __makeinfo__ (buffer, "plain text");
131 function [retval, status] = get_usage_html (help_text, max_len)
133 [help_text, status] = strip_html_tags (help_text);
135 ## Extract first line with plain text method.
136 retval = get_usage_plain_text (help_text, max_len);
140 ## Stop reporting function as missing tests. No good tests possible.