1 ## Copyright (C) 2005-2012 Bill Denney
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {} savepath (@var{file})
21 ## Save the portion of the current function search path, that is
22 ## not set during Octave's initialization process, to @var{file}.
23 ## If @var{file} is omitted, @file{~/.octaverc} is used. If successful,
24 ## @code{savepath} returns 0.
25 ## @seealso{path, addpath, rmpath, genpath, pathdef, pathsep}
28 ## Author: Bill Denney <bill@givebillmoney.com>
30 function varargout = savepath (file)
34 beginstring = "## Begin savepath auto-created section, do not edit";
35 endstring = "## End savepath auto-created section";
38 file = fullfile ("~", ".octaverc");
41 ## parse the file if it exists to see if we should replace a section
42 ## or create a section
46 if (exist (file) == 2)
47 ## read in all lines of the file
48 [fid, msg] = fopen (file, "rt");
50 error ("savepath: could not open file, %s: %s", file, msg);
56 if (isnumeric (result))
57 ## end at the end of file
60 linenum = linenum + 1;
61 filelines{linenum} = result;
62 ## find the first and last lines if they exist in the file
63 if (strcmp (result, beginstring))
65 elseif (strcmp (result, endstring))
70 unwind_protect_cleanup
71 closeread = fclose (fid);
73 error ("savepath: could not close file after reading, %s",
79 if (startline > endline || (startline > 0 && endline == 0))
80 error ("savepath: unable to parse file, %s", file);
83 ## put the current savepath lines into the file
84 if (isempty (filelines)
85 || (startline == 1 && endline == length (filelines)))
86 ## savepath is the entire file
89 ## drop the savepath statements at the end of the file
92 elseif (startline == 1)
94 post = filelines(endline+1:end);
95 elseif (endline == length (filelines))
96 pre = filelines(1:startline-1);
99 ## insert in the middle
100 pre = filelines(1:startline-1);
101 post = filelines(endline+1:end);
105 [fid, msg] = fopen (file, "wt");
107 error ("savepath: unable to open file for writing, %s, %s", file, msg);
110 for i = 1:length (pre)
111 fprintf (fid, "%s\n", pre{i});
114 ## Remove the portion of the path defined via the command line
115 ## and/or the environment.
116 workingpath = parsepath (path);
117 command_line_path = parsepath (command_line_path ());
118 octave_path = parsepath (getenv ("OCTAVE_PATH"));
119 if (isempty (pathdef ()))
120 ## This occurs when running octave via run-octave. In this instance
121 ## the entire path is specified via the command line and pathdef()
123 [tmp, n] = setdiff (workingpath, octave_path);
124 default_path = command_line_path;
126 [tmp, n] = setdiff (workingpath, union (command_line_path, octave_path));
127 default_path = parsepath (pathdef ());
129 ## This is the path we'd like to preserve when octave is run.
130 path_to_preserve = workingpath (sort (n));
132 ## Determine the path to Octave's user and sytem wide pkgs.
133 [pkg_user, pkg_system] = pkg ("list");
134 pkg_user_path = cell (1, numel (pkg_user));
135 pkg_system_path = cell (1, numel (pkg_system));
136 for n = 1:numel(pkg_user)
137 pkg_user_path{n} = pkg_user{n}.archprefix;
139 for n = 1:numel(pkg_system)
140 pkg_system_path{n} = pkg_system{n}.archprefix;
142 pkg_path = union (pkg_user_path, pkg_system_path);
144 ## Rely on Octave's initialization to include the pkg path elements.
145 if (! isempty (pkg_path))
146 [tmp, n] = setdiff (path_to_preserve, strcat (pkg_path, ":"));
147 path_to_preserve = path_to_preserve (sort (n));
150 ## Split the path to be saved into two groups. Those path elements that
151 ## belong at the beginning and those at the end.
152 if (! isempty (default_path))
153 n1 = strmatch (default_path{1}, path_to_preserve, "exact");
154 n2 = strmatch (default_path{end}, path_to_preserve, "exact");
155 n_middle = round (0.5*(n1+n2));
156 [tmp, n] = setdiff (path_to_preserve, default_path);
157 path_to_save = path_to_preserve (sort (n));
159 path_to_save = path_to_save (! strcmpi (path_to_save,
160 strcat (".", pathsep)));
161 n = ones (size (path_to_save));
162 for m = 1:numel(path_to_save)
163 n(m) = strmatch (path_to_save{m}, path_to_preserve);
165 path_to_save_begin = path_to_save(n <= n_middle);
166 path_to_save_end = path_to_save(n > n_middle);
168 path_to_save_begin = path_to_preserve;
169 path_to_save_end = {};
171 path_to_save_begin = cell2mat (path_to_save_begin);
172 path_to_save_end = cell2mat (path_to_save_end);
174 ## Use single quotes for PATH argument to avoid string escape
175 ## processing. Since we are using single quotes around the arg,
176 ## double any single quote characters found in the string.
177 fprintf (fid, "%s\n", beginstring);
178 if (! isempty (path_to_save_begin))
179 n = find (path_to_save_begin != pathsep, 1, "last");
180 fprintf (fid, " addpath ('%s', '-begin');\n",
181 strrep (path_to_save_begin(1:n), "'", "''"));
183 if (! isempty (path_to_save_end))
184 n = find (path_to_save_end != pathsep, 1, "last");
185 fprintf (fid, " addpath ('%s', '-end');\n",
186 strrep (path_to_save_end(1:n), "'", "''"));
188 fprintf (fid, "%s\n", endstring);
190 for i = 1:length (post)
191 fprintf (fid, "%s\n", post{i});
193 unwind_protect_cleanup
194 closeread = fclose (fid);
196 error ("savepath: could not close savefile after writing, %s", file);
198 warning ("savepath: current path saved to %s", file);
205 varargout{1} = retval;
210 function path_elements = parsepath (p)
211 pat = sprintf ('([^%s]+[%s$])', pathsep, pathsep);
212 [~, ~, ~, path_elements] = regexpi (strcat (p, pathsep), pat);