]> Creatis software - CreaPhase.git/blob - octave_packages/m/help/print_usage.m
update packages
[CreaPhase.git] / octave_packages / m / help / print_usage.m
1 ## Copyright (C) 2009-2012 S�ren Hauberg
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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.
25 ## @seealso{help}
26 ## @end deftypefn
27
28 function print_usage (name)
29   x = dbstack ();
30   ## Handle input
31   if (nargin == 0)
32     ## Determine the name of the calling function
33     if (numel (x) > 1)
34       name = x (2).name;
35     else
36       error ("print_usage: invalid function\n");
37     endif
38     fullpath = evalin ("caller", "mfilename (""fullpath"")");
39     if (strcmp (fullpath(end-length(name)+1:end), name))
40       fullname = [fullpath, ".m"];
41     endif
42   elseif (!ischar (name))
43     error ("print_usage: input argument must be a string");
44   else
45     fullname = name;
46   endif
47
48   ## Determine if we're called from top level.
49   at_toplev = length (x) < 2 || (length (x) == 2 && strcmp (x(2).name, name));
50
51   ## Do the actual work
52   [text, format] = get_help_text (fullname);
53   max_len = 80;
54   switch (lower (format))
55     case "plain text"
56       [usage_string, status] = get_usage_plain_text (text, max_len);
57     case "texinfo"
58       [usage_string, status] = get_usage_texinfo (text, max_len);
59     case "html"
60       [usage_string, status] = get_usage_html (text, max_len);
61     case "not documented"
62       error ("print_usage: `%s' is not documented\n", name);
63     case "not found"
64       error ("print_usage: `%s' not found\n", name);
65     otherwise
66       error ("print_usage: internal error: unsupported help text format: '%s'\n", format);
67   endswitch
68
69   ## Raise the final error
70   if (status != 0)
71     warning ("print_usage: Texinfo formatting filter exited abnormally");
72     warning ("print_usage: raw Texinfo source of help text follows...\n");
73   endif
74
75   if (at_toplev)
76     error ("Invalid call to %s.  Correct usage is:\n\n%s\n%s",
77            name, usage_string, __additional_help_message__ ());
78   else
79     msg = sprintf ("Invalid call to %s.  Correct usage is:\n\n%s",
80                    name, usage_string);
81     ## Ensure that the error doesn't end up with a newline, as that disables
82     ## backtraces.
83     if (msg(end) == "\n")
84       msg(end) = " ";
85     endif
86
87     error (msg);
88   endif
89
90 endfunction
91
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)]));
96   status = 0;
97 endfunction
98
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", " ");
103
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))
108     buffer = "";
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));
112       if (isempty (endl))
113         buffer = strcat (buffer, help_text (def_idx (k):end), "\n");
114       else
115         buffer = strcat (buffer, help_text (def_idx (k):endl));
116       endif
117     endfor
118
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));
122     endif
123   else
124     [retval, status] = get_usage_plain_text (help_text, max_len);
125   endif
126
127   ## Run makeinfo to generate plain text
128   [retval, status] = __makeinfo__ (buffer, "plain text");
129 endfunction
130
131 function [retval, status] = get_usage_html (help_text, max_len)
132   ## Strip tags
133   [help_text, status] = strip_html_tags (help_text);
134
135   ## Extract first line with plain text method.
136   retval = get_usage_plain_text (help_text, max_len);
137 endfunction
138
139
140 ## Stop reporting function as missing tests.  No good tests possible.
141 %!assert (1)
142