annotate scripts/path/pathdef.m @ 10549:95c3e38098bf

Untabify .m scripts
author Rik <code@nomad.inbox5.com>
date Fri, 23 Apr 2010 11:28:50 -0700
parents 9f25290a35e8
children d1978e7364ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9899
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
1 ## Copyright (C) 2005, 2006, 2007, 2008, 2009 Bill Denney
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
2 ## Copyright (C) 2007, 2008, 2009 Ben Abbott
7388
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
3 ##
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
5 ##
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
9 ## your option) any later version.
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
10 ##
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
15 ##
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
18 ## <http://www.gnu.org/licenses/>.
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
19
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefn {Function File} {@var{val} =} pathdef ()
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
22 ## Return the default path for Octave.
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
23 ## The path information is extracted from one of three sources.
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
24 ## In order of preference, those are;
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
25 ##
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
26 ## @enumerate
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
27 ## @item @file{~/.octaverc}
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 7393
diff changeset
28 ## @item @file{<octave-home>/@dots{}/<version>/m/startup/octaverc}
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
29 ## @item Octave's path prior to changes by any octaverc.
7388
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
30 ## @end enumerate
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
31 ## @seealso{path, addpath, rmpath, genpath, savepath, pathsep}
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
32 ## @end deftypefn
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
33
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
34 function val = pathdef ()
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
35
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
36 ## Locate the site octaverc file.
7391
f071480b7eac [project @ 2008-01-17 07:50:33 by jwe]
jwe
parents: 7388
diff changeset
37 pathdir = octave_config_info ("localstartupfiledir");
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
38 site_octaverc = fullfile (pathdir, "octaverc");
7388
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
39
7393
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
40 ## Locate the user ~\.octaverc file.
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
41 user_octaverc = fullfile ("~", ".octaverc");
7388
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
42
7391
f071480b7eac [project @ 2008-01-17 07:50:33 by jwe]
jwe
parents: 7388
diff changeset
43 ## Extract the specified paths from the site and user octaverc"s.
7393
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
44 site_path = __extractpath__ (site_octaverc);
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
45 if (exist (user_octaverc, "file"))
7393
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
46 user_path = __extractpath__ (user_octaverc);
7388
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
47 else
7393
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
48 user_path = "";
7388
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
49 endif
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
50
7392
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
51 ## A path definition in the user octaverc has precedence over the
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
52 ## site.
17f2cdb5232e [project @ 2008-01-17 08:14:32 by jwe]
jwe
parents: 7391
diff changeset
53
7393
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
54 if (! isempty (user_path))
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
55 val = user_path;
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
56 elseif (! isempty (site_path))
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
57 val = site_path;
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
58 else
a2e8cfe2fd17 [project @ 2008-01-17 08:46:54 by jwe]
jwe
parents: 7392
diff changeset
59 val = __pathorig__ ();
7388
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
60 endif
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
61
0e1ccfe1bf91 [project @ 2008-01-16 06:31:23 by jwe]
jwe
parents:
diff changeset
62 endfunction
9899
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
63
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
64 ## Extact the path information from the script/function @var{file},
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
65 ## created by @file{savepath.m}. If @var{file} is omitted,
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
66 ## @file{~/.octaverc} is used. If successful, @code{__extractpath__}
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
67 ## returns the path specified in @var{file}.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
68
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
69 ## Author: Ben Abbott <bpabbott@mac.com>
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
70
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
71 function specifiedpath = __extractpath__ (savefile)
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
72
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
73 ## The majority of this code was borrowed from savepath.m.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
74 ## FIXME -- is there some way to share the common parts instead of
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
75 ## duplicating?
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
76
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
77 beginstring = "## Begin savepath auto-created section, do not edit";
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
78 endstring = "## End savepath auto-created section";
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
79
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
80 if (nargin == 0)
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
81 savefile = tilde_expand ("~/.octaverc");
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
82 endif
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
83
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
84 ## Parse the file if it exists to see if we should replace a section
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
85 ## or create a section.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
86 startline = 0;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
87 endline = 0;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
88 filelines = {};
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
89 if (exist (savefile) == 2)
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
90 ## read in all lines of the file
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
91 [fid, msg] = fopen (savefile, "rt");
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
92 if (fid < 0)
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
93 error ("__extractpath__: could not open savefile, %s: %s", savefile, msg);
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
94 endif
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
95 unwind_protect
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
96 linenum = 0;
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
97 while (linenum >= 0)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
98 result = fgetl (fid);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
99 if (isnumeric (result))
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
100 ## End at the end of file.
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
101 linenum = -1;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
102 else
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
103 linenum++;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
104 filelines{linenum} = result;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
105 ## Find the first and last lines if they exist in the file.
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
106 if (strcmp (result, beginstring))
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
107 startline = linenum + 1;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
108 elseif (strcmp (result, endstring))
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
109 endline = linenum - 1;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
110 endif
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
111 endif
9899
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
112 endwhile
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
113 unwind_protect_cleanup
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
114 closeread = fclose (fid);
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
115 if (closeread < 0)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
116 error ("savepath: could not close savefile after reading, %s",
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
117 savefile);
9899
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
118 endif
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
119 end_unwind_protect
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
120 endif
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
121
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
122 ## Extract the path specifiation.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
123 if (startline > endline || (startline > 0 && endline == 0))
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
124 error ("savepath: unable to parse file, %s", savefile);
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
125 elseif (startline > 0)
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
126 ## Undo doubling of single quote characters performed by savepath.
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
127 specifiedpath = strrep (regexprep (cstrcat (filelines(startline:endline){:}),
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
128 " *path *\\('(.*)'\\); *", "$1"),
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9899
diff changeset
129 "''", "'");
9899
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
130 else
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
131 specifiedpath = "";
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
132 endif
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
133
9f25290a35e8 more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
134 endfunction