-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanip.m
More file actions
328 lines (296 loc) · 10.2 KB
/
manip.m
File metadata and controls
328 lines (296 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
% MANIP Project manager for MATLAB
% MANIP(cmd, projectName) manages current working directory and files
% that are opened in MATLAB editor (but not the workspace).
% Available commands are:
% 'list', 'show', 'save', 'load', 'close', 'rename', 'delete', 'active'
%
% MANIP or
% MANIP('list') shows all stored manips. Arrow marks active project.
%
% MANIP('active') returns the name of the active project
%
% MANIP('show') shows information about the current project
% MANIP('show', project_name) shows information about the project
%
% MANIP('close') closes all opened files
%
% MANIP('save'[, projectName]) saves current working directory and
% editor state for current project or projectName
%
% MANIP('sethomedir' [, projectName]) set home director for current
% project or projectName
%
% MANIP('load') restores the project "default"
% MANIP('load', projectName) restores the project with specified name
%
% MANIP('open') is synonym for MANIP('load')
%
% MANIP('rename', newName) renames the active project
% MANIP('rename', projectName, newName) renames the project
%
% MANIP('delete') deletes the active project, alternatives: 'del' or 'rm'
% MANIP('delete', projectName) deletes the project with specified name
%
% Examples:
% manip list
% manip save myProject
% manip close
% manip load default
% manip rename myProject myLibrary
%
% All manips are stored in the %userpath%/manips.mat. This file with
% empty "default" project is created at the first run of the script. If
% %userpath% is empty, the script will execute userpath('reset').
%
% First project always has name "default"
% Copyright 2012-2013, Vladimir Filimonov (ETH Zurich).
% $Date: 12-May-2012 $
function varargout = manip(cmd, varargin)
persistent activeProject
if verLessThan('matlab','7.12')
error('Projects: MATLAB versions older than R2011a (7.12) are not supported')
end
if isempty(userpath)
userpath('reset');
end
fpath = regexp(userpath,'[^:]*','match');
fpath = fpath{1};
fpath = fullfile(fpath, 'manips.mat');
if ~exist(fpath, 'file') % first time run
openDocuments = matlab.desktop.editor.getAll;
filenames = {openDocuments.Filename};
projectsList = [];
projectsList(1).ProjectName = 'default';
projectsList(1).OpenedFiles = {};
projectsList(1).HomeDir = userpath;
projectsList(1).HomeDir(end) = [];
manip_sessid = round(rand*1e15);
activeProject = 1;
save(fpath, 'projectsList');
else
load(fpath)
end
if isempty(activeProject)
activeProject = 0;
end
if nargin==0
cmd = 'list';
end
switch lower(cmd)
case 'cd'
if not(isempty(varargin)) && ~strcmpi(manip('active'),varargin{1})
manip('load',varargin{1})
end
cd(projectsList(activeProject).HomeDir);
case 'close'
if nargin < 2 || ~strcmp(varargin{1},'nosave')
manip('save')
end
openDocuments = matlab.desktop.editor.getAll;
openDocuments.close;
activeProject = 0;
varargout = {true};
%=========================================
case 'list'
% openDocuments = matlab.desktop.editor.getAll;
% alldocs = {projectsList.OpenedFiles};
% comp = ismember(openDocuments,
disp('List of available manips:')
for ii = 1:length(projectsList)
if ii == activeProject
str = '-> ';
else
str = ' ';
end
disp([str num2str(ii) ': ' projectsList(ii).ProjectName])
end
varargout = {projectsList.ProjectName};
%=========================================
case {'show', 'info'}
if nargin==1
ind = activeProject;
else
prjname = varargin{1};
ind = find(strcmpi(prjname, {projectsList.ProjectName}), 1);
if isempty(ind)
error('Projects: unknown project name')
end
end
if ~ind
varargout = {[]};
else
projectsList(ind)
varargout{1} = projectsList(ind);
end
%=========================================
case 'active'
if ~activeProject
if nargout == 1
varargout = {''};
end
return
end
varargout{1} = projectsList(activeProject).ProjectName;
if nargout == 0
disp(['Active project is "' varargout{1} '"'])
end
%=========================================
case 'save'
if nargin==1
ind = activeProject;
if ~ind
disp('No active project. Nothing to save.')
return
else
prjname = projectsList(ind).ProjectName;
end
else
prjname = varargin{1};
ind = find(strcmpi(prjname, {projectsList.ProjectName}), 1);
if isempty(ind)
ind = length(projectsList) + 1;
end
end
openDocuments = matlab.desktop.editor.getAll;
filenames = {openDocuments.Filename};
projectsList(ind).ProjectName = prjname;
projectsList(ind).OpenedFiles = filenames;
if isempty(projectsList(ind).HomeDir)
projectsList(ind).HomeDir = pwd;
end
activeProject = ind;
save(fpath, 'projectsList');
disp(['Manip "' prjname '" was saved'])
%=========================================
case {'sethomedir'}
if nargin==1
ind = activeProject;
if ~ind
disp('No active project. Nothing do.')
return
else
prjname = projectsList(ind).ProjectName;
end
else
prjname = varargin{1};
ind = find(strcmpi(prjname, {projectsList.ProjectName}), 1);
if isempty(ind)
error(['unknown project ' prjname])
end
end
projectsList(ind).HomeDir = pwd;
save(fpath, 'projectsList');
disp(['HomeDir set for "' prjname '"'])
case {'open', 'load'}
if nargin==1
error('no project to open')
else
prjname = varargin{1};
if ~isnan(str2double(prjname))
prjname = projectsList(str2double(prjname)).ProjectName;
end
end
ind = find(strcmpi(prjname, {projectsList.ProjectName}), 1);
if isempty(ind)
error(['unknown project ' prjname])
end
manip close
load(fpath)
% switch getenv('computername')
% case 'maxinux'
% projectsList(ind).HomeDir = strrep(projectsList(ind).HomeDir,'/home/maximilien.chaumon/','/home/maxi/');
% case 'labinux'
% projectsList(ind).HomeDir = strrep(projectsList(ind).HomeDir,'/home/maxi/','/home/maximilien.chaumon/');
% end
try
cd(projectsList(ind).HomeDir);
catch
warning(['Directory "' projectsList(ind).HomeDir '" does not exist.'])
end
filenames = projectsList(ind).OpenedFiles;
% switch getenv('computername')
% case 'maxinux'
% filenames = strrep(filenames,'/home/maximilien.chaumon/','/home/maxi/');
% case 'labinux'
% filenames = strrep(filenames,'/home/maxi/','/home/maximilien.chaumon/');
% end
for ii = 1:length(filenames)
if exist(filenames{ii}, 'file')
matlab.desktop.editor.openDocument(filenames{ii});
else
warning(['File "' filenames{ii} '" was not found'])
end
end
od = matlab.desktop.editor.getAll;
projectsList(ind).OpenedFiles = {od.Filename};
activeProject = ind;
save(fpath, 'projectsList');
disp(['Manip "' prjname '" was restored'])
commandwindow
%=========================================
case 'rename'
if nargin==1
error('Projects: project name was not specified')
elseif nargin==2
ind = activeProject;
if ~ind
error('No active project to rename')
else
prjold = projectsList(ind).ProjectName;
prjnew = varargin{1};
if strcmp(prjnew,'default')
error('cannot rename project to "default"')
end
end
elseif nargin==3
prjold = varargin{1};
prjnew = varargin{2};
end
ind = find(strcmpi(prjold, {projectsList.ProjectName}), 1);
if isempty(ind)
error(['unknown project name ' prjold])
end
projectsList(ind).ProjectName = prjnew;
save(fpath, 'projectsList');
disp(['Manip "' prjold '" was renamed to "' prjnew '"'])
%=========================================
case {'delete' 'del' 'rm'}
if nargin==1
todel = activeProject;
if ~todel
error('could not delete "default" project')
end
else
todel = find(strcmpi(varargin{1}, {projectsList.ProjectName}), 1);
if isempty(todel)
error('Projects: required project was not found')
end
active_name = projectsList(activeProject).ProjectName;
end
todelname = projectsList(todel).ProjectName;
projectsList(todel) = [];
if todel == activeProject
activeProject = 0;
else
activeProject = find(strcmpi(active_name, {projectsList.ProjectName}), 1);
end
save(fpath, 'projectsList');
disp(['Manip "' todelname '" was deleted'])
%=========================================
otherwise
try
if strcmp(manip('active'),cmd)
disp(['manip ' cmd ' already active'])
manip('cd',cmd)
return
else
manip('load',cmd)
end
catch ME
disp(ME)
error(['Manip: unknown command: ' cmd '.'])
end
end
if nargout==0
varargout = {};
end