Mercurial > hg > octave-lojdl
changeset 17000:5069b6c6c062
edit.m: Redo default templates. Improve docstring. Use Octave coding conventions.
* scripts/miscellaneous/edit.m: Redo default templates. Improve docstring.
Use Octave coding conventions.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 17 Jul 2013 20:11:36 -0700 |
parents | 90d50e56a06a |
children | 477ed3b55497 |
files | scripts/miscellaneous/edit.m |
diffstat | 1 files changed, 124 insertions(+), 129 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/miscellaneous/edit.m +++ b/scripts/miscellaneous/edit.m @@ -30,7 +30,7 @@ ## If the function @var{name} is available in a file on your path and ## that file is modifiable, then it will be edited in place. If it ## is a system function, then it will first be copied to the directory -## @env{HOME} (see further down) and then edited. +## @env{HOME} (see below) and then edited. ## If no file is found, then the m-file ## variant, ending with ".m", will be considered. If still no file ## is found, then variants with a leading "@@" and then with both a @@ -42,26 +42,27 @@ ## to contain that function along with its current definition. ## ## @item -## If @code{name.cc} is specified, then it will search for @code{name.cc} -## in the path and try to modify it, otherwise it will create a new -## @file{.cc} file in @env{HOME}. If @var{name} happens to be an +## If @code{@var{name}.cc} is specified, then it will search for +## @code{@var{name}.cc} in the path and try to modify it, otherwise it will +## create a new @file{.cc} file in @env{HOME}. If @var{name} happens to be an ## m-file or interpreter defined function, then the text of that ## function will be inserted into the .cc file as a comment. ## ## @item -## If @var{name.ext} is on your path then it will be edited, otherwise -## the editor will be started with @file{HOME/name.ext} as the -## filename. If @file{name.ext} is not modifiable, it will be copied to +## If @file{@var{name}.ext} is on your path then it will be edited, otherwise +## the editor will be started with @file{@env{HOME}/@var{name}.ext} as the +## filename. If @file{@var{name}.ext} is not modifiable, it will be copied to ## @env{HOME} before editing. ## -## @strong{Warning:} You may need to clear name before the new definition +## @strong{Warning:} You may need to clear @var{name} before the new definition ## is available. If you are editing a .cc file, you will need -## to mkoctfile @file{name.cc} before the definition will be available. +## to execute @code{mkoctfile @file{@var{name}.cc}} before the definition +## will be available. ## @end itemize ## ## If @code{edit} is called with @var{field} and @var{value} variables, -## the value of the control field @var{field} will be @var{value}. -## If an output argument is requested and the first argument is @code{get} +## the value of the control field @var{field} will be set to @var{value}. +## If an output argument is requested and the first input argument is @code{get} ## then @code{edit} will return the value of the control field @var{field}. ## If the control field does not exist, edit will return a structure ## containing all fields and values. Thus, @code{edit get all} returns @@ -75,8 +76,8 @@ ## @code{getenv ("EDITOR")} and defaults to @code{emacs}. Use @code{%s} ## In place of the function name. For example, ## -## @table @samp -## @item [EDITOR, " %s"] +## @table @asis +## @item @code{[EDITOR, " %s"]} ## Use the editor which Octave uses for @code{edit_history}. ## ## @item "xedit %s &" @@ -97,18 +98,18 @@ ## @end smallexample ## ## @item home -## This is the location of user local m-files. Be be sure it is in your +## This is the location of user local m-files. Be sure it is in your ## path. The default is @file{~/octave}. ## ## @item author ## This is the name to put after the "## Author:" field of new functions. -## By default it guesses from the @code{gecos} field of password database. +## By default it guesses from the @code{gecos} field of the password database. ## ## @item email ## This is the e-mail address to list after the name in the author field. ## By default it guesses @code{<$LOGNAME@@$HOSTNAME>}, and if @code{$HOSTNAME} ## is not defined it uses @code{uname -n}. You probably want to override this. -## Be sure to use @code{<user@@host>} as your format. +## Be sure to use the format @code{<user@@host>}. ## ## @item license ## @@ -133,7 +134,7 @@ ## This value determines whether the editor should be started in async mode ## (editor is started in the background and Octave continues) or sync mode ## (Octave waits until the editor exits). Set it to "sync" to start the editor -## in sync mode. The default is "async" (see also "system"). +## in sync mode. The default is "async" (@pxref{docXsystem,,system}). ## ## @item editinplace ## Determines whether files should be edited in place, without regard to @@ -153,53 +154,52 @@ persistent FUNCTION = struct ("EDITOR", [EDITOR() " %s"], "HOME", fullfile (default_home, "octave"), "AUTHOR", default_user(1), - "EMAIL", [], - "LICENSE", "GPL", + "EMAIL", [], + "LICENSE", "GPL", "MODE", "async", "EDITINPLACE", false); ## Make sure the stateval variables survive "clear functions". - mlock; + #mlock; if (nargin == 1) ## User has supplied one arg, this can be a single file name - ## or a cell array of strings containing multiple files to be - ## opened - if (iscellstr(varargin{1})) - ## If first arg is a cell array of strings, it becomes the - ## list of files to be edited + ## or a cell array of strings containing multiple files to be opened + if (iscellstr (varargin{1})) + ## If first arg is a cell array of strings, + ## it becomes the list of files to be edited editfilelist = varargin{1}; - elseif (ischar(varargin{1})) + elseif (ischar (varargin{1})) ## If first arg is a string, create a cell array of strings - ## of length one (by copying the input cell array) + ## of length 1 (by copying the input cell array) editfilelist = varargin(1); else - error('edit: expected file to be a string or cell array of strings'); + error ("edit: expected file to be a string or cell array of strings"); endif elseif (nargin == 2) - ## User has supplied two arguments, these could be two file - ## names, or a combination of editor state name and new value - ## for that state, so first check for the various states + ## User has supplied two arguments, these could be two file names, + ## or a combination of editor state name and new value for that state, + ## so first check for the various states statevar = varargin{1}; stateval = varargin{2}; switch (toupper (statevar)) case "EDITOR" FUNCTION.EDITOR = stateval; - return + return; case "HOME" if (! isempty (stateval) && stateval(1) == "~") stateval = [ default_home, stateval(2:end) ]; endif FUNCTION.HOME = stateval; - return + return; case "AUTHOR" FUNCTION.AUTHOR = stateval; - return + return; case "EMAIL" FUNCTION.EMAIL = stateval; - return + return; case "LICENSE" FUNCTION.LICENSE = stateval; - return + return; case "MODE" if (strcmp (stateval, "sync") || strcmp (stateval, "async")) FUNCTION.MODE = stateval; @@ -232,15 +232,15 @@ editfilelist = varargin; endswitch elseif (nargin > 2) - if (iscellstr(varargin)) + if (iscellstr (varargin)) editfilelist = varargin; else - error('edit: if supplying more than one input all inputs must be strings containing fiel names to open.'); + error ("edit: if supplying more than one input all inputs must be strings containing field names to open."); endif endif ## Start the editor without a file if no file is given. - if (nargin < 1) + if (nargin == 0) if (exist (FUNCTION.HOME, "dir") == 7) curr_dir = pwd (); unwind_protect @@ -255,11 +255,11 @@ return; endif - if (numel(editfilelist) > 1) + if (numel (editfilelist) > 1) ## Call edit on each of the files in the list if there are more than 1 - for i = 1:numel(editfilelist) - edit(editfilelist{i}); + for i = 1:numel (editfilelist) + edit (editfilelist{i}); endfor else @@ -296,15 +296,13 @@ ## If file has no extension, add file.m and file.cc to the list. idx = rindex (file, "."); if (idx == 0) - ## Create the list of files to look for - filelist = {file}; if (isempty (regexp (file, '\.m$'))) ## No ".m" at the end of the file, add to the list. - filelist{end+1} = cat (2, file, ".m"); + filelist{end+1} = [file ".m"]; endif if (isempty (regexp (file, '\.cc$'))) ## No ".cc" at the end of the file, add to the list. - filelist{end+1} = cat (2, file, ".cc"); + filelist{end+1} = [file ".cc"]; endif endif @@ -313,7 +311,7 @@ ## No "@" at the beginning of the file, add to the list. numfiles = numel (filelist); for n = 1:numfiles - filelist{n+numfiles} = cat (2, "@", filelist{n}); + filelist{n+numfiles} = ["@" filelist{n}]; endfor endif @@ -335,8 +333,8 @@ do_edit (FUNCTION.EDITOR, fileandpath, FUNCTION.MODE); return; else - ## If the file is modifiable in place then edit it, otherwise make - ## a copy in HOME and then edit it. + ## If the file is modifiable in place then edit it, + ## otherwise make a copy in HOME and then edit it. fid = fopen (fileandpath, "r+t"); if (fid < 0) from = fileandpath; @@ -359,16 +357,14 @@ idx = rindex (file, "."); name = file(1:idx-1); ext = file(idx+1:end); - switch (ext) - case {"cc", "m"} - 0; - otherwise - do_edit (FUNCTION.EDITOR, fileandpath, FUNCTION.MODE); - return; - endswitch + if (! any (strcmp (ext, {"cc", "m"}))) + ## Some unknown file. Just open it up. + do_edit (FUNCTION.EDITOR, fileandpath, FUNCTION.MODE); + return; + endif - ## The file doesn't exist in path so create it, put in the function - ## template and edit it. + ## The file doesn't exist in path so + ## create it, put in the function template, and edit it. ## Guess the email name if it was not given. if (isempty (FUNCTION.EMAIL)) @@ -405,46 +401,46 @@ switch (uclicense) case "GPL" head = cstrcat (copyright, "\n\n", "\ - This program is free software; you can redistribute it and/or modify\n\ - it under the terms of the GNU General Public License as published by\n\ - the Free Software Foundation; either version 3 of the License, or\n\ - (at your option) any later version.\n\ - \n\ - This program is distributed in the hope that it will be useful,\n\ - but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ - GNU General Public License for more details.\n\ - \n\ - You should have received a copy of the GNU General Public License\n\ - along with Octave; see the file COPYING. If not, see\n\ - <http://www.gnu.org/licenses/>.\ - "); +This program is free software; you can redistribute it and/or modify\n\ +it under the terms of the GNU General Public License as published by\n\ +the Free Software Foundation; either version 3 of the License, or\n\ +(at your option) any later version.\n\ +\n\ +This program is distributed in the hope that it will be useful,\n\ +but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ +GNU General Public License for more details.\n\ +\n\ +You should have received a copy of the GNU General Public License\n\ +along with Octave; see the file COPYING. If not, see\n\ +<http://www.gnu.org/licenses/>.\ +"); tail = [author, "\n", revs]; case "BSD" head = cstrcat (copyright, "\n\n", "\ - This program is free software; redistribution and use in source and\n\ - binary forms, with or without modification, are permitted provided that\n\ - the following conditions are met:\n\ - \n\ - 1.Redistributions of source code must retain the above copyright\n\ - notice, this list of conditions and the following disclaimer.\n\ - 2.Redistributions in binary form must reproduce the above copyright\n\ - notice, this list of conditions and the following disclaimer in the\n\ - documentation and/or other materials provided with the distribution.\n\ - \n\ - THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND\n\ - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n\ - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n\ - ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\n\ - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n\ - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n\ - OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n\ - HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n\ - LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n\ - OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n\ - SUCH DAMAGE.\ - "); +This program is free software; redistribution and use in source and\n\ +binary forms, with or without modification, are permitted provided that\n\ +the following conditions are met:\n\ +\n\ + 1.Redistributions of source code must retain the above copyright\n\ + notice, this list of conditions and the following disclaimer.\n\ + 2.Redistributions in binary form must reproduce the above copyright\n\ + notice, this list of conditions and the following disclaimer in the\n\ + documentation and/or other materials provided with the distribution.\n\ +\n\ +THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND\n\ +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n\ +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n\ +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\n\ +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n\ +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n\ +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n\ +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n\ +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n\ +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n\ +SUCH DAMAGE.\ +"); tail = [author, "\n", revs]; case "PD" @@ -462,9 +458,9 @@ switch (ext) case {"cc", "C", "cpp"} if (isempty (head)) - comment = ["/*\n", tail, "\n\n*/\n\n"]; + comment = ["/*\n\n", tail, "\n\n*/\n\n"]; else - comment = ["/*\n", head, "\n\n", tail, "\n\n*/\n\n"]; + comment = ["/*\n\n", head, "\n\n", tail, "\n\n*/\n\n"]; endif ## If we are shadowing an m-file, paste the code for the m-file. if (any (exists == [2, 103])) @@ -472,32 +468,31 @@ else code = " "; endif - body = cstrcat ("#include <octave/oct.h>\n\n", - "DEFUN_DLD(", name, ",args,nargout,\"\\\n", - name, "\\n\\\n\")\n{\n", - " octave_value_list retval;\n", - " int nargin = args.length ();\n\n", - code, "\n return retval;\n}\n"); + body = ["#include <octave/oct.h>\n\n", \ + "DEFUN_DLD(" name ", args, nargout, \"\\\n", \ + name, "\\n\\\n\")\n{\n", \ + " octave_value_list retval;\n", \ + " int nargin = args.length ();\n\n", \ + code, "\n return retval;\n}\n"]; text = [comment, body]; case "m" - ## If we are editing a function defined on the fly, paste the - ## code. + ## If we are editing a function defined on the fly, paste the code. if (any (exists == [2, 103])) body = type (name){1}; else - body = ["function [ret] = " name " ()\n\nendfunction\n"]; + body = ["function [retval] = " name " ()\n\nendfunction\n"]; endif if (isempty (head)) - comment = ["## -*- texinfo -*- \n## @deftypefn {Function File}" \ - "{@var{ret} =}" name "(@var{x}, @var{y})\n##\n" \ - "## @seealso{}\n## @end deftypefn\n\n" \ + comment = ["## -*- texinfo -*- \n## @deftypefn {Function File} " \ + "{@var{retval} =} " name " (@var{x}, @var{y})\n##\n" \ + "## @seealso{}\n## @end deftypefn\n\n" \ "## " strrep(tail, "\n", "\n## ") "\n\n"]; else - comment = ["## " strrep(head,"\n","\n## ") "\n\n" \ - "## -*- texinfo -*- \n## @deftypefn {Function File}" \ - "{@var{ret} =}" name "(@var{x} @var{y})\n##\n" \ - "## @seealso{}\n## @end deftypefn\n\n" \ + comment = ["## " strrep(head,"\n","\n## ") "\n\n" \ + "## -*- texinfo -*- \n## @deftypefn {Function File} " \ + "{@var{retval} =} " name " (@var{x} @var{y})\n##\n" \ + "## @seealso{}\n## @end deftypefn\n\n" \ "## " strrep(tail, "\n", "\n## ") "\n\n"]; endif text = [comment, body]; @@ -517,15 +512,15 @@ endfunction -function ret = default_home () +function retval = default_home () - ret = getenv ("HOME"); - if (isempty (ret)) - ret = glob ("~"); - if (! isempty (ret)) - ret = ret{1}; + retval = getenv ("HOME"); + if (isempty (retval)) + retval = glob ("~"); + if (! isempty (retval)) + retval = retval{1}; else - ret = ""; + retval = ""; endif endif @@ -537,30 +532,30 @@ ## default author. Otherwise return the login name. ## login@host will be the default email address. -function ret = default_user (long_form) +function retval = default_user (long_form) ent = getpwuid (getuid); if (! isstruct (ent)) - ret = getenv ("USER"); - if (isempty (ret)) - ret = getenv ("USERNAME"); + retval = getenv ("USER"); + if (isempty (retval)) + retval = getenv ("USERNAME"); endif elseif (long_form) - ret = ent.gecos; - pos = strfind (ret, ","); + retval = ent.gecos; + pos = strfind (retval, ","); if (! isempty (pos)) - ret = ret(1:pos-1); + retval = retval(1:pos-1); endif else - ret = ent.name; + retval = ent.name; endif endfunction function do_edit (editor, file, mode) - ## Give the hook function a chance. If that fails, fall back - ## on running an editor with the system function. + ## Give the hook function a chance. + ## If that fails, fall back on running an editor with the system function. status = __octave_link_edit_file__ (file);