comparison scripts/path/savepath.m @ 14216:b3730ed107a6

Clean up scripts in path/ directory * matlabroot.m: Use standard Octave convention of retval for return variable. * pathdef.m: Update docstring, Use new code for extracting savepath reference. * savepath.m: Update docstring. Remove use of deprecated strmatch function. Use '~' to ignore function return values rather than a tmp variable.
author Rik <octave@nomad.inbox5.com>
date Wed, 18 Jan 2012 21:15:50 -0800
parents 72c96de7a403
children d63878346099
comparison
equal deleted inserted replaced
14215:103ec2ea6914 14216:b3730ed107a6
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} savepath (@var{file}) 20 ## @deftypefn {Function File} {} savepath ()
21 ## Save the portion of the current function search path, that is 21 ## @deftypefnx {Function File} {} savepath (@var{file})
22 ## not set during Octave's initialization process, to @var{file}. 22 ## @deftypefnx {Function File} {@var{status} =} savepath (@dots{})
23 ## Save the unique portion of the current function search path that is
24 ## not set during Octave's initialization process to @var{file}.
23 ## If @var{file} is omitted, @file{~/.octaverc} is used. If successful, 25 ## If @var{file} is omitted, @file{~/.octaverc} is used. If successful,
24 ## @code{savepath} returns 0. 26 ## @code{savepath} returns 0.
25 ## @seealso{path, addpath, rmpath, genpath, pathdef, pathsep} 27 ## @seealso{path, addpath, rmpath, genpath, pathdef}
26 ## @end deftypefn 28 ## @end deftypefn
27 29
28 ## Author: Bill Denney <bill@givebillmoney.com> 30 ## Author: Bill Denney <bill@givebillmoney.com>
29 31
30 function varargout = savepath (file) 32 function retval = savepath (file)
31 33
32 retval = 1; 34 ret = 1;
33 35
34 beginstring = "## Begin savepath auto-created section, do not edit"; 36 beginstring = "## Begin savepath auto-created section, do not edit";
35 endstring = "## End savepath auto-created section"; 37 endstring = "## End savepath auto-created section";
36 38
37 if (nargin == 0) 39 if (nargin == 0)
38 file = fullfile ("~", ".octaverc"); 40 file = fullfile ("~", ".octaverc");
39 endif 41 endif
40 42
41 ## parse the file if it exists to see if we should replace a section 43 ## parse the file if it exists to see if we should replace an
42 ## or create a section 44 ## existing section or create a new section
43 startline = 0; 45 startline = endline = 0;
44 endline = 0;
45 filelines = {}; 46 filelines = {};
46 if (exist (file) == 2) 47 if (exist (file) == 2)
47 ## read in all lines of the file
48 [fid, msg] = fopen (file, "rt"); 48 [fid, msg] = fopen (file, "rt");
49 if (fid < 0) 49 if (fid < 0)
50 error ("savepath: could not open file, %s: %s", file, msg); 50 error ("savepath: could not open file, %s: %s", file, msg);
51 endif 51 endif
52 unwind_protect 52 unwind_protect
53 linenum = 0; 53 linenum = 0;
54 while (linenum >= 0) 54 while (ischar (line = fgetl (fid)))
55 result = fgetl (fid); 55 filelines{++linenum} = line;
56 if (isnumeric (result)) 56 ## find the first and last lines if they exist in the file
57 ## end at the end of file 57 if (strcmp (line, beginstring))
58 linenum = -1; 58 startline = linenum;
59 else 59 elseif (strcmp (line, endstring))
60 linenum = linenum + 1; 60 endline = linenum;
61 filelines{linenum} = result;
62 ## find the first and last lines if they exist in the file
63 if (strcmp (result, beginstring))
64 startline = linenum;
65 elseif (strcmp (result, endstring))
66 endline = linenum;
67 endif
68 endif 61 endif
69 endwhile 62 endwhile
70 unwind_protect_cleanup 63 unwind_protect_cleanup
71 closeread = fclose (fid); 64 closeread = fclose (fid);
72 if (closeread < 0) 65 if (closeread < 0)
73 error ("savepath: could not close file after reading, %s", 66 error ("savepath: could not close file after reading, %s", file);
74 file);
75 endif 67 endif
76 end_unwind_protect 68 end_unwind_protect
77 endif 69 endif
78 70
79 if (startline > endline || (startline > 0 && endline == 0)) 71 if (startline > endline || (startline > 0 && endline == 0))
114 ## Remove the portion of the path defined via the command line 106 ## Remove the portion of the path defined via the command line
115 ## and/or the environment. 107 ## and/or the environment.
116 workingpath = parsepath (path); 108 workingpath = parsepath (path);
117 command_line_path = parsepath (command_line_path ()); 109 command_line_path = parsepath (command_line_path ());
118 octave_path = parsepath (getenv ("OCTAVE_PATH")); 110 octave_path = parsepath (getenv ("OCTAVE_PATH"));
119 if (isempty (pathdef ())) 111 pathdef = pathdef ();
112 if (isempty (pathdef))
120 ## This occurs when running octave via run-octave. In this instance 113 ## This occurs when running octave via run-octave. In this instance
121 ## the entire path is specified via the command line and pathdef() 114 ## the entire path is specified via the command line and pathdef()
122 ## is empty. 115 ## is empty.
123 [tmp, n] = setdiff (workingpath, octave_path); 116 [~, n] = setdiff (workingpath, octave_path);
124 default_path = command_line_path; 117 default_path = command_line_path;
125 else 118 else
126 [tmp, n] = setdiff (workingpath, union (command_line_path, octave_path)); 119 [~, n] = setdiff (workingpath, union (command_line_path, octave_path));
127 default_path = parsepath (pathdef ()); 120 default_path = parsepath (pathdef);
128 endif 121 endif
129 ## This is the path we'd like to preserve when octave is run. 122 ## This is the path we'd like to preserve when octave is run.
130 path_to_preserve = workingpath (sort (n)); 123 path_to_preserve = workingpath (sort (n));
131 124
132 ## Determine the path to Octave's user and sytem wide pkgs. 125 ## Determine the path to Octave's user and sytem wide pkgs.
133 [pkg_user, pkg_system] = pkg ("list"); 126 [pkg_user, pkg_system] = pkg ("list");
134 pkg_user_path = cell (1, numel (pkg_user)); 127 pkg_user_path = cell (1, numel (pkg_user));
135 pkg_system_path = cell (1, numel (pkg_system)); 128 pkg_system_path = cell (1, numel (pkg_system));
136 for n = 1:numel(pkg_user) 129 for n = 1:numel (pkg_user)
137 pkg_user_path{n} = pkg_user{n}.archprefix; 130 pkg_user_path{n} = pkg_user{n}.archprefix;
138 endfor 131 endfor
139 for n = 1:numel(pkg_system) 132 for n = 1:numel (pkg_system)
140 pkg_system_path{n} = pkg_system{n}.archprefix; 133 pkg_system_path{n} = pkg_system{n}.archprefix;
141 endfor 134 endfor
142 pkg_path = union (pkg_user_path, pkg_system_path); 135 pkg_path = union (pkg_user_path, pkg_system_path);
143 136
144 ## Rely on Octave's initialization to include the pkg path elements. 137 ## Rely on Octave's initialization to include the pkg path elements.
145 if (! isempty (pkg_path)) 138 if (! isempty (pkg_path))
146 [tmp, n] = setdiff (path_to_preserve, strcat (pkg_path, ":")); 139 [~, n] = setdiff (path_to_preserve, strcat (pkg_path, ":"));
147 path_to_preserve = path_to_preserve (sort (n)); 140 path_to_preserve = path_to_preserve(sort (n));
148 endif 141 endif
149 142
150 ## Split the path to be saved into two groups. Those path elements that 143 ## Split the path to be saved into two groups. Those path elements that
151 ## belong at the beginning and those at the end. 144 ## belong at the beginning and those at the end.
152 if (! isempty (default_path)) 145 if (! isempty (default_path))
153 n1 = strmatch (default_path{1}, path_to_preserve, "exact"); 146 n1 = find (strcmp (default_path{1}, path_to_preserve));
154 n2 = strmatch (default_path{end}, path_to_preserve, "exact"); 147 n2 = find (strcmp (default_path{end}, path_to_preserve));
155 n_middle = round (0.5*(n1+n2)); 148 n_middle = round (0.5*(n1+n2));
156 [tmp, n] = setdiff (path_to_preserve, default_path); 149 [~, n] = setdiff (path_to_preserve, default_path);
157 path_to_save = path_to_preserve (sort (n)); 150 path_to_save = path_to_preserve(sort (n));
158 ## Remove pwd 151 ## Remove pwd
159 path_to_save = path_to_save (! strcmpi (path_to_save, 152 path_to_save = path_to_save(! strcmp (path_to_save, ["." pathsep]));
160 strcat (".", pathsep)));
161 n = ones (size (path_to_save)); 153 n = ones (size (path_to_save));
162 for m = 1:numel(path_to_save) 154 for m = 1:numel (path_to_save)
163 n(m) = strmatch (path_to_save{m}, path_to_preserve); 155 n(m) = find (strcmp (path_to_save{m}, path_to_preserve));
164 endfor 156 endfor
165 path_to_save_begin = path_to_save(n <= n_middle); 157 path_to_save_begin = path_to_save(n <= n_middle);
166 path_to_save_end = path_to_save(n > n_middle); 158 path_to_save_end = path_to_save(n > n_middle);
167 else 159 else
168 path_to_save_begin = path_to_preserve; 160 path_to_save_begin = path_to_preserve;
197 elseif (nargin == 0) 189 elseif (nargin == 0)
198 warning ("savepath: current path saved to %s", file); 190 warning ("savepath: current path saved to %s", file);
199 endif 191 endif
200 end_unwind_protect 192 end_unwind_protect
201 193
202 retval = 0; 194 ret = 0;
203 195
204 if (nargout == 1) 196 if (nargout > 0)
205 varargout{1} = retval; 197 retval = ret;
206 endif 198 endif
207 199
208 endfunction 200 endfunction
209 201
210 function path_elements = parsepath (p) 202 function path_elements = parsepath (p)
211 pat = sprintf ('([^%s]+[%s$])', pathsep, pathsep); 203 pat = sprintf ('([^%s]+[%s$])', pathsep, pathsep);
212 [~, ~, ~, path_elements] = regexpi (strcat (p, pathsep), pat); 204 path_elements = regexpi (strcat (p, pathsep), pat, "match");
213 endfunction 205 endfunction
214 206