annotate scripts/path/savepath.m @ 14138:72c96de7a403 stable

maint: update copyright notices for 2012
author John W. Eaton <jwe@octave.org>
date Mon, 02 Jan 2012 14:25:41 -0500
parents b0084095098e
children b3730ed107a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 11589
diff changeset
1 ## Copyright (C) 2005-2012 Bill Denney
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
2 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
4 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6807
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6807
diff changeset
8 ## your option) any later version.
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
9 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
14 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6807
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6807
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
18
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
20 ## @deftypefn {Function File} {} savepath (@var{file})
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
21 ## Save the portion of the current function search path, that is
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
22 ## not set during Octave's initialization process, to @var{file}.
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
23 ## If @var{file} is omitted, @file{~/.octaverc} is used. If successful,
5804
296cefb48d7e [project @ 2006-05-10 20:10:10 by jwe]
jwe
parents: 5736
diff changeset
24 ## @code{savepath} returns 0.
5814
67bf9b4f2ae2 [project @ 2006-05-11 21:58:34 by jwe]
jwe
parents: 5804
diff changeset
25 ## @seealso{path, addpath, rmpath, genpath, pathdef, pathsep}
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
26 ## @end deftypefn
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
27
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
28 ## Author: Bill Denney <bill@givebillmoney.com>
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
29
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
30 function varargout = savepath (file)
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
31
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
32 retval = 1;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
33
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
34 beginstring = "## Begin savepath auto-created section, do not edit";
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
35 endstring = "## End savepath auto-created section";
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
36
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
37 if (nargin == 0)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
38 file = fullfile ("~", ".octaverc");
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
39 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
40
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
41 ## parse the file if it exists to see if we should replace a section
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
42 ## or create a section
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
43 startline = 0;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
44 endline = 0;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
45 filelines = {};
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
46 if (exist (file) == 2)
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
47 ## read in all lines of the file
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
48 [fid, msg] = fopen (file, "rt");
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
49 if (fid < 0)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
50 error ("savepath: could not open file, %s: %s", file, msg);
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
51 endif
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
52 unwind_protect
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
53 linenum = 0;
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
54 while (linenum >= 0)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
55 result = fgetl (fid);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
56 if (isnumeric (result))
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
57 ## end at the end of file
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
58 linenum = -1;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
59 else
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
60 linenum = linenum + 1;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
61 filelines{linenum} = result;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
62 ## find the first and last lines if they exist in the file
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
63 if (strcmp (result, beginstring))
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
64 startline = linenum;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
65 elseif (strcmp (result, endstring))
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
66 endline = linenum;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
67 endif
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
68 endif
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
69 endwhile
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
70 unwind_protect_cleanup
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
71 closeread = fclose (fid);
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
72 if (closeread < 0)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
73 error ("savepath: could not close file after reading, %s",
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
74 file);
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
75 endif
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
76 end_unwind_protect
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
77 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
78
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
79 if (startline > endline || (startline > 0 && endline == 0))
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
80 error ("savepath: unable to parse file, %s", file);
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
81 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
82
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
83 ## put the current savepath lines into the file
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
84 if (isempty (filelines)
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
85 || (startline == 1 && endline == length (filelines)))
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
86 ## savepath is the entire file
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
87 pre = post = {};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
88 elseif (endline == 0)
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
89 ## drop the savepath statements at the end of the file
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
90 pre = filelines;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
91 post = {};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
92 elseif (startline == 1)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
93 pre = {};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
94 post = filelines(endline+1:end);
8608
5cc1fba0a7d6 style and ChangeLog fixes
John W. Eaton <jwe@octave.org>
parents: 8586
diff changeset
95 elseif (endline == length (filelines))
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
96 pre = filelines(1:startline-1);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
97 post = {};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
98 else
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
99 ## insert in the middle
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
100 pre = filelines(1:startline-1);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
101 post = filelines(endline+1:end);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
102 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
103
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
104 ## write the results
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
105 [fid, msg] = fopen (file, "wt");
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
106 if (fid < 0)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
107 error ("savepath: unable to open file for writing, %s, %s", file, msg);
7151
aeeb646f6538 [project @ 2007-11-09 19:34:17 by jwe]
jwe
parents: 7017
diff changeset
108 endif
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
109 unwind_protect
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
110 for i = 1:length (pre)
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
111 fprintf (fid, "%s\n", pre{i});
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
112 endfor
5736
e07baa8b93b9 [project @ 2006-04-04 19:41:45 by jwe]
jwe
parents: 5735
diff changeset
113
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
114 ## Remove the portion of the path defined via the command line
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
115 ## and/or the environment.
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
116 workingpath = parsepath (path);
8609
fcf762ba66cf load-path.cc (Fcommand_line_path): rename from Fcommandlinepath
John W. Eaton <jwe@octave.org>
parents: 8608
diff changeset
117 command_line_path = parsepath (command_line_path ());
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
118 octave_path = parsepath (getenv ("OCTAVE_PATH"));
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
119 if (isempty (pathdef ()))
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
120 ## This occurs when running octave via run-octave. In this instance
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
121 ## the entire path is specified via the command line and pathdef()
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
122 ## is empty.
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
123 [tmp, n] = setdiff (workingpath, octave_path);
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
124 default_path = command_line_path;
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
125 else
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
126 [tmp, n] = setdiff (workingpath, union (command_line_path, octave_path));
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
127 default_path = parsepath (pathdef ());
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
128 endif
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
129 ## This is the path we'd like to preserve when octave is run.
8608
5cc1fba0a7d6 style and ChangeLog fixes
John W. Eaton <jwe@octave.org>
parents: 8586
diff changeset
130 path_to_preserve = workingpath (sort (n));
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
131
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
132 ## Determine the path to Octave's user and sytem wide pkgs.
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
133 [pkg_user, pkg_system] = pkg ("list");
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
134 pkg_user_path = cell (1, numel (pkg_user));
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
135 pkg_system_path = cell (1, numel (pkg_system));
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
136 for n = 1:numel(pkg_user)
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
137 pkg_user_path{n} = pkg_user{n}.archprefix;
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
138 endfor
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
139 for n = 1:numel(pkg_system)
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
140 pkg_system_path{n} = pkg_system{n}.archprefix;
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
141 endfor
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
142 pkg_path = union (pkg_user_path, pkg_system_path);
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
143
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
144 ## Rely on Octave's initialization to include the pkg path elements.
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
145 if (! isempty (pkg_path))
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
146 [tmp, n] = setdiff (path_to_preserve, strcat (pkg_path, ":"));
8608
5cc1fba0a7d6 style and ChangeLog fixes
John W. Eaton <jwe@octave.org>
parents: 8586
diff changeset
147 path_to_preserve = path_to_preserve (sort (n));
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
148 endif
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
149
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
150 ## Split the path to be saved into two groups. Those path elements that
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
151 ## belong at the beginning and those at the end.
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
152 if (! isempty (default_path))
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
153 n1 = strmatch (default_path{1}, path_to_preserve, "exact");
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
154 n2 = strmatch (default_path{end}, path_to_preserve, "exact");
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
155 n_middle = round (0.5*(n1+n2));
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
156 [tmp, n] = setdiff (path_to_preserve, default_path);
8608
5cc1fba0a7d6 style and ChangeLog fixes
John W. Eaton <jwe@octave.org>
parents: 8586
diff changeset
157 path_to_save = path_to_preserve (sort (n));
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
158 ## Remove pwd
8608
5cc1fba0a7d6 style and ChangeLog fixes
John W. Eaton <jwe@octave.org>
parents: 8586
diff changeset
159 path_to_save = path_to_save (! strcmpi (path_to_save,
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9038
diff changeset
160 strcat (".", pathsep)));
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
161 n = ones (size (path_to_save));
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
162 for m = 1:numel(path_to_save)
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
163 n(m) = strmatch (path_to_save{m}, path_to_preserve);
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
164 endfor
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
165 path_to_save_begin = path_to_save(n <= n_middle);
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
166 path_to_save_end = path_to_save(n > n_middle);
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
167 else
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
168 path_to_save_begin = path_to_preserve;
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
169 path_to_save_end = {};
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
170 endif
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
171 path_to_save_begin = cell2mat (path_to_save_begin);
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
172 path_to_save_end = cell2mat (path_to_save_end);
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
173
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
174 ## Use single quotes for PATH argument to avoid string escape
7393
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
175 ## processing. Since we are using single quotes around the arg,
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
176 ## double any single quote characters found in the string.
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
177 fprintf (fid, "%s\n", beginstring);
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
178 if (! isempty (path_to_save_begin))
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
179 n = find (path_to_save_begin != pathsep, 1, "last");
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
180 fprintf (fid, " addpath ('%s', '-begin');\n",
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
181 strrep (path_to_save_begin(1:n), "'", "''"));
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
182 endif
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
183 if (! isempty (path_to_save_end))
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
184 n = find (path_to_save_end != pathsep, 1, "last");
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
185 fprintf (fid, " addpath ('%s', '-end');\n",
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
186 strrep (path_to_save_end(1:n), "'", "''"));
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
187 endif
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
188 fprintf (fid, "%s\n", endstring);
5736
e07baa8b93b9 [project @ 2006-04-04 19:41:45 by jwe]
jwe
parents: 5735
diff changeset
189
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
190 for i = 1:length (post)
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
191 fprintf (fid, "%s\n", post{i});
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
192 endfor
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
193 unwind_protect_cleanup
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
194 closeread = fclose (fid);
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
195 if (closeread < 0)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
196 error ("savepath: could not close savefile after writing, %s", file);
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
197 elseif (nargin == 0)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
198 warning ("savepath: current path saved to %s", file);
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
199 endif
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
200 end_unwind_protect
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
201
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
202 retval = 0;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
203
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
204 if (nargout == 1)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
205 varargout{1} = retval;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
206 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
207
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
208 endfunction
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
209
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
210 function path_elements = parsepath (p)
11032
c9b0a75b02e8 Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
211 pat = sprintf ('([^%s]+[%s$])', pathsep, pathsep);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
212 [~, ~, ~, path_elements] = regexpi (strcat (p, pathsep), pat);
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
213 endfunction
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
214