annotate scripts/miscellaneous/grabcode.m @ 33617:ec2635a02328 bytecode-interpreter tip

maint: Merge default to bytecode-interpreter.
author Markus Mützel <markus.muetzel@gmx.de>
date Tue, 21 May 2024 18:29:03 +0200
parents 2e484f9f1f18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
3 ## Copyright (C) 2016-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
7 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
8 ## This file is part of Octave.
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22299
diff changeset
13 ## (at your option) any later version.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
14 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22299
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22299
diff changeset
18 ## GNU General Public License for more details.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
19 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
25
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
27 ## @deftypefn {} {} grabcode @var{filename}
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
28 ## @deftypefnx {} {} grabcode @var{url}
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
29 ## @deftypefnx {} {@var{code_str} =} grabcode (@dots{})
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
30 ##
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
31 ## Grab the code from a report created by the @code{publish} function.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
32 ##
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
33 ## The grabbed code inside the published report must be enclosed by the
23246
184ab9034ad5 * grabcode.m: Fix typo in doc string.
John W. Eaton <jwe@octave.org>
parents: 23244
diff changeset
34 ## strings @samp{##### SOURCE BEGIN #####} and @samp{##### SOURCE END #####}.
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
35 ## The @code{publish} function creates this format automatically.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
36 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
37 ## If no return value is requested the code is saved to a temporary file and
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
38 ## opened in the default editor. NOTE: The temporary file must be saved to a
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
39 ## new filename or the code will be lost.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
40 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
41 ## If an output is requested the grabbed code will be returned as string
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
42 ## @var{code_str}.
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
43 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
44 ## Example:
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
45 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
46 ## @example
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
47 ## @group
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
48 ## publish ("my_script.m");
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
49 ## grabcode ("html/my_script.html");
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
50 ## @end group
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
51 ## @end example
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
52 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
53 ## The example above publishes @file{my_script.m} to the default location
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
54 ## @file{html/my_script.html}. Next, the published Octave script is grabbed to
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
55 ## edit its content in a new temporary file.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
56 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
57 ## @seealso{publish}
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
58 ## @end deftypefn
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
59
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
60 function code_str = grabcode (url)
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
61
28891
de5f2f9a64ff maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents: 28886
diff changeset
62 if (nargin < 1)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
63 print_usage ();
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
64 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
65
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
66 if (exist (url) == 2)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
67 ## URL is a local file
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
68 oct_code = fileread (url);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
69 else
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
70 ## Otherwise, try to read remote URL
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
71 [oct_code, success, message] = urlread (url);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
72 if (! success)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
73 error (["grabcode: " message]);
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
74 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
75 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
76
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
77 ## Extract relevant part
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
78 oct_code = regexp (oct_code, ...
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
79 '##### SOURCE BEGIN #####\n(.*)##### SOURCE END #####', "once", "tokens");
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
80 oct_code = oct_code{1};
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
81
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
82 if (nargout == 1)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
83 code_str = oct_code;
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
84 else
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
85 ## Open temporary file in editor
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
86 fname = [tempname() ".m"];
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
87 fid = fopen (fname, "w");
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
88 if (fid < 0)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
89 error ("grabcode: could not open temporary file");
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
90 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
91 fprintf (fid, "%s", oct_code);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
92 fclose (fid);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
93 edit (fname);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
94 warndlg (["grabcode: Make sure to save the temporary file\n\n\t", ...
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
95 fname, "\n\nto a location of your choice. ", ...
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
96 "Otherwise all grabbed code will be lost!"]);
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
97 endif
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
98
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
99 endfunction
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
100
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
101
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
102 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 27923
diff changeset
103 %!error <Invalid call> grabcode ()