annotate scripts/path/savepath.m @ 19530:bb8d3f17248d

Overhaul savepath.m, pathdef.m, matlabroot.m. Fix pathdef which has been broken since at least 3.2.4 (nobody noticed). Use project specific .octaverc for savepath, pathdef before other choices. * matlabroot.m: Remove stray extra newline before start of BIST tests. * scripts/path/module.mk: Add getsavepath.m to build system * scripts/path/private/getsavepath.m: New function abstracts common code used by both pathdef and savepath to read an rc file. * pathdef.m: Redo docstring. Validate number of input arguments. Look for project-specific .octaverc file ahead of other rc files. Use private function getsavepath to read in rc file. Return pathsep() separated list of directories (this was broken). Add one lame BIST test. * savepath.m: Redo docstring. Use project-specific .octaverc if it exists. Use private function getsavepath to read in rc file. Replace for loops around fprintf with single calls to fprintf and a comma-separated-list of arguments. Use Octave syntax, rather than for loops, to convert cell array of structs to cellstr of a single struct field value. Turn off backtrace warning temporarily when alerting the user about where the savepath information has been saved. Use ostrsplit and strcat rather than regexpi for performance. Add BIST test.
author Rik <rik@octave.org>
date Thu, 20 Nov 2014 08:08:35 -0800
parents d63878346099
children 3a905b642408
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 14216
diff changeset
1 ## Copyright (C) 2005-2013 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 -*-
14216
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
20 ## @deftypefn {Function File} {} savepath ()
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
21 ## @deftypefnx {Function File} {} savepath (@var{file})
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
22 ## @deftypefnx {Function File} {@var{status} =} savepath (@dots{})
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
23 ## Save the unique portion of the current function search path that is
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
24 ## not set during Octave's initialization process to @var{file}.
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
25 ##
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
26 ## If @var{file} is omitted, Octave looks in the current directory for a
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
27 ## project-specific @file{.octaverc} file in which to save the path
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
28 ## information. If no such file is present then the user's configuration file
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
29 ## @file{~/.octaverc} is used.
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
30 ##
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
31 ## If successful, @code{savepath} returns 0.
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
32 ##
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
33 ## The @code{savepath} function makes it simple to customize a user's
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
34 ## configuration file to restore the working paths necessary for a particular
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
35 ## instance of Octave. Assuming no filename is specified, Octave will
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
36 ## automatically restore the saved directory paths from the appropriate
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
37 ## @file{.octaverc} file when starting up. If a filename has been specified
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
38 ## then the paths may be restored manually by calling @code{source @var{file}}.
14216
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
39 ## @seealso{path, addpath, rmpath, genpath, pathdef}
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
40 ## @end deftypefn
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
41
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
42 ## Author: Bill Denney <bill@givebillmoney.com>
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
43
14216
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
44 function retval = savepath (file)
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
45
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
46 beginstring = "## Begin savepath auto-created section, do not edit";
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
47 endstring = "## End savepath auto-created section";
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
48
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
49 ## Use project-specific or user's .octaverc when no file specified
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
50 if (nargin == 0)
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
51 file = fullfile (pwd, ".octaverc");
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
52 if (! exist (file, "file"))
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
53 file = fullfile ("~", ".octaverc");
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
54 endif
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
55 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
56
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
57 ## Read in the file
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
58 [filelines, startline, endline] = getsavepath (file);
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
59
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
60 ## Determine where the savepath lines are placed in the file.
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
61 if (isempty (filelines)
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
62 || (startline == 1 && endline == length (filelines)))
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
63 ## savepath is the entire file.
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
64 pre = post = {};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
65 elseif (endline == 0)
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
66 ## 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
67 pre = filelines;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
68 post = {};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
69 elseif (startline == 1)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
70 pre = {};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
71 post = filelines(endline+1:end);
8608
5cc1fba0a7d6 style and ChangeLog fixes
John W. Eaton <jwe@octave.org>
parents: 8586
diff changeset
72 elseif (endline == length (filelines))
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
73 pre = filelines(1:startline-1);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
74 post = {};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
75 else
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
76 ## Insert in the middle.
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
77 pre = filelines(1:startline-1);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
78 post = filelines(endline+1:end);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
79 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
80
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
81 ## 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
82 [fid, msg] = fopen (file, "wt");
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
83 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
84 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
85 endif
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
86 unwind_protect
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
87 fprintf (fid, "%s\n", pre{:});
5736
e07baa8b93b9 [project @ 2006-04-04 19:41:45 by jwe]
jwe
parents: 5735
diff changeset
88
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
89 ## 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
90 ## and/or the environment.
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
91 workingpath = parsepath (path);
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
92 cmd_line_path = parsepath (command_line_path ());
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
93 octave_path = parsepath (getenv ("OCTAVE_PATH"));
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
94 default_path = pathdef ();
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
95 if (isempty (default_path))
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
96 ## This occurs when running octave via run-octave. In this instance
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
97 ## 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
98 ## is empty.
14216
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
99 [~, n] = setdiff (workingpath, octave_path);
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
100 default_path = cmd_line_path;
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
101 else
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
102 [~, n] = setdiff (workingpath, union (cmd_line_path, octave_path));
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
103 default_path = parsepath (default_path);
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
104 endif
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
105 ## This is the path we'd like to preserve when octave is run.
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
106 path_to_preserve = workingpath(sort (n));
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
107
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
108 ## Determine the path to Octave's user and system wide packages.
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
109 [pkg_user, pkg_system] = pkg ("list");
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
110 ## Conversion from cell array of structs to cellstr of archprefixes
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
111 if (isempty (pkg_user))
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
112 pkg_user_path = {};
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
113 else
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
114 pkg_user_path = {[pkg_user{:}].archprefix};
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
115 endif
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
116 if (isempty (pkg_system))
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
117 pkg_system_path = {};
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
118 else
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
119 pkg_system_path = {[pkg_system{:}].archprefix};
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
120 endif
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
121 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
122
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
123 ## 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
124 if (! isempty (pkg_path))
14216
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
125 [~, n] = setdiff (path_to_preserve, strcat (pkg_path, ":"));
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
126 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
127 endif
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
128
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
129 ## Split the path to be saved into two groups. Those path elements that
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
130 ## 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
131 if (! isempty (default_path))
14216
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
132 n1 = find (strcmp (default_path{1}, path_to_preserve));
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
133 n2 = find (strcmp (default_path{end}, path_to_preserve));
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
134 n_middle = round ((n1+n2)/2);
14216
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
135 [~, n] = setdiff (path_to_preserve, default_path);
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
136 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
137 ## Remove pwd
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
138 path_to_save(strcmp (path_to_save, ["." pathsep])) = [];
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
139 n = ones (size (path_to_save));
14216
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
140 for m = 1:numel (path_to_save)
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
141 n(m) = find (strcmp (path_to_save{m}, path_to_preserve));
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
142 endfor
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
143 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
144 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
145 else
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
146 path_to_save_begin = path_to_preserve;
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
147 path_to_save_end = {};
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 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
150 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
151
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
152 ## 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
153 ## 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
154 ## 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
155 fprintf (fid, "%s\n", beginstring);
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
156 if (! isempty (path_to_save_begin))
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
157 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
158 fprintf (fid, " addpath ('%s', '-begin');\n",
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
159 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
160 endif
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
161 if (! isempty (path_to_save_end))
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
162 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
163 fprintf (fid, " addpath ('%s', '-end');\n",
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
164 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
165 endif
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
166 fprintf (fid, "%s\n", endstring);
5736
e07baa8b93b9 [project @ 2006-04-04 19:41:45 by jwe]
jwe
parents: 5735
diff changeset
167
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
168 fprintf (fid, "%s\n", post{:});
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
169 unwind_protect_cleanup
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
170 status = fclose (fid);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
171 if (status < 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
172 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
173 elseif (nargin == 0)
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
174 warning ("off", "backtrace", "local");
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11032
diff changeset
175 warning ("savepath: current path saved to %s", file);
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
176 endif
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
177 end_unwind_protect
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
178
14216
b3730ed107a6 Clean up scripts in path/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
179 if (nargout > 0)
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
180 retval = 0;
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
181 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
182
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
183 endfunction
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
184
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
185 ## Convert single string of paths to cell array of paths
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
186 function path_elements = parsepath (p)
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
187 path_elements = strcat (ostrsplit (p, pathsep), pathsep);
8586
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
188 endfunction
31ab3b83bc8a savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents: 7393
diff changeset
189
19530
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
190
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
191 %!test
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
192 %! fname = tempname ();
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
193 %! status = savepath (fname);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
194 %! assert (status == 0);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
195 %! old_dir = pwd;
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
196 %! unwind_protect
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
197 %! cd (P_tmpdir);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
198 %! if (exist (fullfile (pwd, ".octaverc")))
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
199 %! unlink (".octaverc");
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
200 %! endif
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
201 %! ## Create blank .octaverc file
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
202 %! fid = fopen (".octaverc", "wt");
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
203 %! assert (fid >= 0);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
204 %! fclose (fid);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
205 %! ## Save path into local .octaverc file
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
206 %! status = savepath ();
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
207 %! assert (status == 0);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
208 %! ## Compare old and new versions
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
209 %! fid = fopen (fname, "rb");
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
210 %! assert (fid >= 0);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
211 %! orig_data = fread (fid);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
212 %! fclose (fid);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
213 %! fid = fopen (".octaverc", "rb");
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
214 %! assert (fid >= 0);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
215 %! new_data = fread (fid);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
216 %! fclose (fid);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
217 %! assert (orig_data, new_data);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
218 %! unwind_protect_cleanup
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
219 %! cd (old_dir);
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
220 %! end_unwind_protect
bb8d3f17248d Overhaul savepath.m, pathdef.m, matlabroot.m.
Rik <rik@octave.org>
parents: 17744
diff changeset
221