1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) ????-2008 - INRIA
3 // Copyright (C) 2008 - INRIA - Allan CORNET
4 // Copyright (C) 2010 - DIGITEO - Allan CORNET
5 // Copyright (C) 2012 - 2016 - Scilab Enterprises
6 // Copyright (C) 2018 - Samuel GOUGEON
8 // This file is hereby licensed under the terms of the GNU GPL v2.0,
9 // pursuant to article 5.3.4 of the CeCILL v.2.1.
10 // This file was originally licensed under the terms of the CeCILL v2.1,
11 // and continues to be available under such terms.
12 // For more information, see the COPYING file which you should have received
13 // along with this program.
15 function edit(macroname, linenumber)
16 // macroname : character string giving a macroname
17 // linenumber : line number (as decimal number or literal one)
21 msg = _("%s: Wrong number of input argument(s): At least %d expected.\n")
22 error(msprintf(msg, "edit", 1));
25 if (rhs >= 1 & type(macroname) ~= 10) then
26 msg = _("%s: Wrong type for input argument #%d: String expected.\n")
27 error(msprintf(msg, "edit", 1));
31 if and(type(linenumber)~=[1 10]) | (type(linenumber)==10 & isnan(strtod(linenumber(1)))) then
32 msg = _("%s: Wrong type for input argument #%d: Number expected.\n")
33 error(msprintf(msg, "edit", 2));
35 linenumber = strtod(linenumber(1))
40 // tmpdir will have trailing / or \
41 tmpdir= pathconvert(TMPDIR);
43 if rhs >= 1 then // macroname or filename is given
44 if regexp(macroname, "/^([a-zA-Z%_#!$?][0-9a-zA-Z_#!$?]*)$/") == [] then
50 execstr("object = "+macroname)
52 // macroname may be an alias. Example: sinus = sind
53 // We need to get the original macro name to edit it:
54 tree = macr2tree(object);
56 if tmp ~= macroname then
57 msg = _("edit: ""%s"" is an alias of ""%s"" => editing %s()\n Please reset %s=%s if %s() is recompiled.\n")
58 warning(msprintf(msg, macroname, tmp, tmp, macroname, tmp, tmp))
62 libr = whereis(macroname);
63 if type(libr($))==10 & libr($)=="script"
64 // the user-defined macro may overwrite a macro in library
65 path = whereis_in_libs(macroname)
67 // priority = from library:
68 fname = pathconvert(path) + macroname + ".sci"
70 txt = tree2code(tree, %t);
71 fname = tmpdir + macroname + ".sci";
75 else // libr is either a module name or a library name
77 err = execstr("tmp="+libr, "errcatch")
78 if ~err & type(tmp)==14
79 [macrolist, path] = libraryinfo(libr);
80 fname = pathconvert(path) + macroname + ".sci"
82 else // either a module name or any variable name:
83 // If macroname is a user-defined function,
84 // whereis() would have reported it as "script".
85 // => it's a macro in a library, but with a
86 // macroname equal to a built-in name
87 // => we must retrieve the library without whereis()
88 path = whereis_in_libs(macroname)
90 fname = pathconvert(path) + macroname + ".sci"
97 if ~found & funptr(macroname)<>0 then
98 error(msprintf(gettext("%s: %s() is an uneditable hard coded function.\n"), "edit", macroname));
105 if ~isdef("macroname","l")
106 macroname = "untitled"
108 fname = tmpdir + macroname + ".sci";
109 txt = ["function [] = " + macroname + "()"; "endfunction"];
113 // call the editor with the filename
115 editor(fname, linenumber, macroname);
122 // ----------------------------------------------------------------------------
124 function path = whereis_in_libs(macroname)
126 for l = librarieslist()'
127 [m, p] = libraryinfo(l)