comparison scripts/miscellaneous/edit.m @ 7540:3422f39573b1

strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
author Ben Abbott <bpabbott@mac.com>
date Thu, 28 Feb 2008 02:41:19 -0500
parents 2ce6b4258e96
children eb7bdde776f2
comparison
equal deleted inserted replaced
7539:3e107d73aeb4 7540:3422f39573b1
138 138
139 function ret = edit (file, state) 139 function ret = edit (file, state)
140 140
141 ## Pick up globals or default them. 141 ## Pick up globals or default them.
142 142
143 persistent FUNCTION = struct ("EDITOR", strcat (EDITOR (), " %s"), 143 persistent FUNCTION = struct ("EDITOR", cstrcat (EDITOR (), " %s"),
144 "HOME", fullfile (default_home, "octave"), 144 "HOME", fullfile (default_home, "octave"),
145 "AUTHOR", default_user(1), 145 "AUTHOR", default_user(1),
146 "EMAIL", [], 146 "EMAIL", [],
147 "LICENSE", "GPL", 147 "LICENSE", "GPL",
148 "MODE", "sync", 148 "MODE", "sync",
195 endif 195 endif
196 196
197 ## Start the editor without a file if no file is given. 197 ## Start the editor without a file if no file is given.
198 if (nargin < 1) 198 if (nargin < 1)
199 if (exist (FUNCTION.HOME, "dir") == 7 && (isunix () || ! ispc ())) 199 if (exist (FUNCTION.HOME, "dir") == 7 && (isunix () || ! ispc ()))
200 system (strcat ("cd \"", FUNCTION.HOME, "\" ; ", 200 system (cstrcat ("cd \"", FUNCTION.HOME, "\" ; ",
201 sprintf (FUNCTION.EDITOR, "")), 201 sprintf (FUNCTION.EDITOR, "")),
202 [], FUNCTION.MODE); 202 [], FUNCTION.MODE);
203 else 203 else
204 system (sprintf (FUNCTION.EDITOR,""), [], FUNCTION.MODE); 204 system (sprintf (FUNCTION.EDITOR,""), [], FUNCTION.MODE);
205 endif 205 endif
269 269
270 if (! isempty (fileandpath)) 270 if (! isempty (fileandpath))
271 ## If the file exists, then edit it. 271 ## If the file exists, then edit it.
272 if (FUNCTION.EDITINPLACE) 272 if (FUNCTION.EDITINPLACE)
273 ## Edit in place even if it is protected. 273 ## Edit in place even if it is protected.
274 system (sprintf (FUNCTION.EDITOR, strcat ("\"", fileandpath, "\"")), 274 system (sprintf (FUNCTION.EDITOR, cstrcat ("\"", fileandpath, "\"")),
275 [], FUNCTION.MODE); 275 [], FUNCTION.MODE);
276 return; 276 return;
277 else 277 else
278 ## If the file is modifiable in place then edit it, otherwise make 278 ## If the file is modifiable in place then edit it, otherwise make
279 ## a copy in HOME and then edit it. 279 ## a copy in HOME and then edit it.
280 fid = fopen (fileandpath, "r+t"); 280 fid = fopen (fileandpath, "r+t");
281 if (fid < 0) 281 if (fid < 0)
282 from = fileandpath; 282 from = fileandpath;
283 fileandpath = strcat (FUNCTION.HOME, from (rindex (from, filesep):end)); 283 fileandpath = cstrcat (FUNCTION.HOME, from (rindex (from, filesep):end));
284 [status, msg] = copyfile (from, fileandpath, 1); 284 [status, msg] = copyfile (from, fileandpath, 1);
285 if (status == 0) 285 if (status == 0)
286 error (msg); 286 error (msg);
287 endif 287 endif
288 else 288 else
289 fclose (fid); 289 fclose (fid);
290 endif 290 endif
291 system (sprintf (FUNCTION.EDITOR, strcat ("\"", fileandpath, "\"")), 291 system (sprintf (FUNCTION.EDITOR, cstrcat ("\"", fileandpath, "\"")),
292 [], FUNCTION.MODE); 292 [], FUNCTION.MODE);
293 return; 293 return;
294 endif 294 endif
295 endif 295 endif
296 296
302 ext = file(idx+1:end); 302 ext = file(idx+1:end);
303 switch (ext) 303 switch (ext)
304 case {"cc", "m"} 304 case {"cc", "m"}
305 0; 305 0;
306 otherwise 306 otherwise
307 system (sprintf (FUNCTION.EDITOR, strcat ("\"", fileandpath, "\"")), 307 system (sprintf (FUNCTION.EDITOR, cstrcat ("\"", fileandpath, "\"")),
308 [], FUNCTION.MODE); 308 [], FUNCTION.MODE);
309 return; 309 return;
310 endswitch 310 endswitch
311 311
312 ## The file doesn't exist in path so create it, put in the function 312 ## The file doesn't exist in path so create it, put in the function
326 endif 326 endif
327 endif 327 endif
328 if (isempty (host)) 328 if (isempty (host))
329 FUNCTION.EMAIL = " "; 329 FUNCTION.EMAIL = " ";
330 else 330 else
331 FUNCTION.EMAIL = strcat ("<", default_user(0), "@", host, ">"); 331 FUNCTION.EMAIL = cstrcat ("<", default_user(0), "@", host, ">");
332 endif 332 endif
333 endif 333 endif
334 334
335 ## Fill in the revision string. 335 ## Fill in the revision string.
336 now = localtime (time); 336 now = localtime (time);
337 revs = strcat ("Created: ", strftime ("%Y-%m-%d", now)); 337 revs = cstrcat ("Created: ", strftime ("%Y-%m-%d", now));
338 338
339 ## Fill in the copyright string. 339 ## Fill in the copyright string.
340 copyright = strcat (strftime ("Copyright (C) %Y ", now), FUNCTION.AUTHOR); 340 copyright = cstrcat (strftime ("Copyright (C) %Y ", now), FUNCTION.AUTHOR);
341 341
342 ## Fill in the author tag field. 342 ## Fill in the author tag field.
343 author = strcat ("Author: ", FUNCTION.AUTHOR, " ", FUNCTION.EMAIL); 343 author = cstrcat ("Author: ", FUNCTION.AUTHOR, " ", FUNCTION.EMAIL);
344 344
345 ## Fill in the header. 345 ## Fill in the header.
346 uclicense = toupper (FUNCTION.LICENSE); 346 uclicense = toupper (FUNCTION.LICENSE);
347 switch (uclicense) 347 switch (uclicense)
348 case "GPL" 348 case "GPL"
349 head = strcat (copyright, "\n\n", "\ 349 head = cstrcat (copyright, "\n\n", "\
350 This program is free software; you can redistribute it and/or modify\n\ 350 This program is free software; you can redistribute it and/or modify\n\
351 it under the terms of the GNU General Public License as published by\n\ 351 it under the terms of the GNU General Public License as published by\n\
352 the Free Software Foundation; either version 2 of the License, or\n\ 352 the Free Software Foundation; either version 2 of the License, or\n\
353 (at your option) any later version.\n\ 353 (at your option) any later version.\n\
354 \n\ 354 \n\
359 \n\ 359 \n\
360 You should have received a copy of the GNU General Public License\n\ 360 You should have received a copy of the GNU General Public License\n\
361 along with Octave; see the file COPYING. If not, see\n\ 361 along with Octave; see the file COPYING. If not, see\n\
362 <http://www.gnu.org/licenses/>.\ 362 <http://www.gnu.org/licenses/>.\
363 "); 363 ");
364 tail = strcat (author, "\n", revs); 364 tail = cstrcat (author, "\n", revs);
365 365
366 case "BSD" 366 case "BSD"
367 head = strcat (copyright, "\n\n", "\ 367 head = cstrcat (copyright, "\n\n", "\
368 This program is free software; redistribution and use in source and\n\ 368 This program is free software; redistribution and use in source and\n\
369 binary forms, with or without modification, are permitted provided that\n\ 369 binary forms, with or without modification, are permitted provided that\n\
370 the following conditions are met:\n\ 370 the following conditions are met:\n\
371 \n\ 371 \n\
372 1.Redistributions of source code must retain the above copyright\n\ 372 1.Redistributions of source code must retain the above copyright\n\
385 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n\ 385 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n\
386 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n\ 386 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n\
387 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n\ 387 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n\
388 SUCH DAMAGE.\ 388 SUCH DAMAGE.\
389 "); 389 ");
390 tail = strcat (author, "\n", revs); 390 tail = cstrcat (author, "\n", revs);
391 391
392 case "PD" 392 case "PD"
393 head = ""; 393 head = "";
394 tail = strcat (author, "\n", revs, "\n\n", 394 tail = cstrcat (author, "\n", revs, "\n\n",
395 "This program is granted to the public domain."); 395 "This program is granted to the public domain.");
396 396
397 otherwise 397 otherwise
398 head = ""; 398 head = "";
399 tail = strcat (copyright, "\n\n", FUNCTION.LICENSE, "\n", 399 tail = cstrcat (copyright, "\n\n", FUNCTION.LICENSE, "\n",
400 author, "\n", revs); 400 author, "\n", revs);
401 endswitch 401 endswitch
402 402
403 ## Generate the function template. 403 ## Generate the function template.
404 exists = exist (name); 404 exists = exist (name);
405 switch (ext) 405 switch (ext)
406 case {"cc", "C", "cpp"} 406 case {"cc", "C", "cpp"}
407 if (isempty (head)) 407 if (isempty (head))
408 comment = strcat ("/*\n", tail, "\n\n*/\n\n"); 408 comment = cstrcat ("/*\n", tail, "\n\n*/\n\n");
409 else 409 else
410 comment = strcat ("/*\n", head, "\n\n", tail, "\n\n*/\n\n"); 410 comment = cstrcat ("/*\n", head, "\n\n", tail, "\n\n*/\n\n");
411 endif 411 endif
412 ## If we are shadowing an m-file, paste the code for the m-file. 412 ## If we are shadowing an m-file, paste the code for the m-file.
413 if (any (exists == [2, 103])) 413 if (any (exists == [2, 103]))
414 code = strcat ("\\ ", strrep (type (name), "\n", "\n// ")); 414 code = cstrcat ("\\ ", strrep (type (name), "\n", "\n// "));
415 else 415 else
416 code = " "; 416 code = " ";
417 endif 417 endif
418 body = strcat ("#include <octave/oct.h>\n\n", 418 body = cstrcat ("#include <octave/oct.h>\n\n",
419 "DEFUN_DLD(", name, ",args,nargout,\"\\\n", 419 "DEFUN_DLD(", name, ",args,nargout,\"\\\n",
420 name, "\\n\\\n\")\n{\n", 420 name, "\\n\\\n\")\n{\n",
421 " octave_value_list retval;\n", 421 " octave_value_list retval;\n",
422 " int nargin = args.length();\n\n", 422 " int nargin = args.length();\n\n",
423 code, "\n return retval;\n}\n"); 423 code, "\n return retval;\n}\n");
424 424
425 text = strcat (comment, body); 425 text = cstrcat (comment, body);
426 case "m" 426 case "m"
427 ## If we are editing a function defined on the fly, paste the 427 ## If we are editing a function defined on the fly, paste the
428 ## code. 428 ## code.
429 if (any (exists == [2, 103])) 429 if (any (exists == [2, 103]))
430 body = type (name); 430 body = type (name);
431 else 431 else
432 body = strcat ("function [ ret ] = ", name, " ()\n\nendfunction\n"); 432 body = cstrcat ("function [ ret ] = ", name, " ()\n\nendfunction\n");
433 endif 433 endif
434 if (isempty (head)) 434 if (isempty (head))
435 comment = strcat ("## ", name, "\n\n", 435 comment = cstrcat ("## ", name, "\n\n",
436 "## ", strrep (tail, "\n", "\n## "), "\n\n"); 436 "## ", strrep (tail, "\n", "\n## "), "\n\n");
437 else 437 else
438 comment = strcat ("## ", strrep(head,"\n","\n## "), "\n\n", ... 438 comment = cstrcat ("## ", strrep(head,"\n","\n## "), "\n\n", ...
439 "## ", name, "\n\n", ... 439 "## ", name, "\n\n", ...
440 "## ", strrep (tail, "\n", "\n## "), "\n\n"); 440 "## ", strrep (tail, "\n", "\n## "), "\n\n");
441 endif 441 endif
442 text = strcat (comment, body); 442 text = cstrcat (comment, body);
443 endswitch 443 endswitch
444 444
445 ## Write the initial file (if there is anything to write) 445 ## Write the initial file (if there is anything to write)
446 fid = fopen (fileandpath, "wt"); 446 fid = fopen (fileandpath, "wt");
447 if (fid < 0) 447 if (fid < 0)
449 endif 449 endif
450 fputs (fid, text); 450 fputs (fid, text);
451 fclose (fid); 451 fclose (fid);
452 452
453 ## Finally we are ready to edit it! 453 ## Finally we are ready to edit it!
454 system (sprintf (FUNCTION.EDITOR, strcat ("\"", fileandpath, "\"")), 454 system (sprintf (FUNCTION.EDITOR, cstrcat ("\"", fileandpath, "\"")),
455 [], FUNCTION.MODE); 455 [], FUNCTION.MODE);
456 456
457 endfunction 457 endfunction
458 458
459 function ret = default_home () 459 function ret = default_home ()