edit2.m (3531B)
1 function edit(cmdname) 2 %EDIT Edit function M-file or MEX source. 3 % EDIT FUNCTION opens a text editor containing the 4 % m-file for FUNCTION. If function is a built-in 5 % function, a variable or not found, the appropriate 6 % error message is produced. If FUNCTION is a MEX 7 % file, the C or FORTRAN source is opened if it resides 8 % in the same directory as the MEX file. 9 10 % Dennis W. Brown 5-17-94 11 % Naval Postgraduate School, Monterey, CA 12 % May be freely distributed. 13 % Not for use in commercial products. 14 %%% 15 %%% All, 16 %%% 17 %%% A couple days ago in a post on another subject I mentioned how I liked 18 %%% being able to use the WHICH command to get the path to an M-file which 19 %%% (no pun intended) I can then highlight and open with the menu 20 %%% 'File-Open selected' in MS-Window Matlab. This avoids having to 21 %%% navigate a file dialog in my editor. 22 %%% 23 %%% Missing this nice feature under Sun Matlab, I got to thinking (a 24 %%% sometimes dangerous act) and came up with the attached EDIT.M 25 %%% program. Simply put, just type 26 %%% 27 %%% >> edit function 28 %%% 29 %%% and it will open a SunOS texteditor containing the file function.m 30 %%% (or function.c or function.for if function is a mex-function). This 31 %%% is even better than having to go to the mouse, highlight the filename 32 %%% and choose the menu. Right now, it's setup for SunOS but it should 33 %%% customizable to work with any flavor of Matlab as long as the text 34 %%% editor creates it's own window. 35 %%% 36 %%% Here are a few examples of it's use (a [1]... line means the text 37 %%% editor opened successfully). 38 %%% 39 %%% >> edit edit 40 %%% [1] 984 41 %%% >> edit nobody 42 %%% edit: nobody is a variable or was not found. 43 %%% >> edit fft 44 %%% edit: fft built-in function. 45 %%% >> edit framdata 46 %%% edit: framdata is a mex-file, opening C source. 47 %%% [1] 992 48 %%% >> 49 %%% 50 %%% Enjoy, 51 %%% Dennis 52 %%% 53 %%% 54 %%% --- 55 %%% | Dennis W. Brown | 56 %%% | Naval Postgraduate School | email: browndw@ece.nps.navy.mil 57 %%% | Monterey, CA 93940 | Usenet: dwbrown@cc.nps.navy.mil 58 %%% | (408)656-2393 | CIS: 75450,1105 59 %%% 60 %%% 61 62 63 64 % find pathname to function 65 f = which(cmdname); 66 67 % check to see if it's built in or whatever 68 b = 'is a built-in function.'; 69 if f == 5, 70 71 % seems WHICH returns codes 5 == built-in 72 disp(['edit: ' cmdname ' built-in function.']); 73 74 elseif f == 0, 75 % 76 % % seems WHICH returns codes 0 == not found or variable. 77 % disp(['edit: ' cmdname ' is a variable or was not found.']); 78 79 cmd = ['!emacs ' cmdname ' &']; 80 81 % do it 82 eval(cmd); 83 fprintf([cmd '\n']); 84 85 elseif strcmp(f(length(f)-length(b)+1:length(f)),b), 86 87 % this code actually is never reached 88 % since f == 5 catches it (see EXIST command) 89 90 % show WHICH output as an error message 91 disp(f); 92 93 else, 94 95 % check for mex-file 96 ext = '.mex4'; 97 c1 = length(f)-length(ext)+1; 98 c2 = length(f); 99 if strcmp(f(c1:c2),ext), 100 101 msg = ['edit: ' cmdname ' is a mex-file']; 102 103 if exist([f(1:c1) 'c']) == 2, 104 105 % try looking for C code first 106 107 % assume it's in the same directory 108 f = [f(1:c1) 'c']; 109 msg = [msg ', opening C source.']; 110 111 elseif exist([f(1:c1) 'for']) == 2, 112 113 % try looking for FORTRAN code second 114 115 % assume it's in the same directory 116 f = [f(1:c1) 'for']; 117 msg = [msg ', opening FORTRAN source.']; 118 119 else 120 f = []; 121 msg = [msg ', aborted.']; 122 end; 123 124 disp(msg); 125 126 end; 127 128 if ~isempty(f), 129 130 % form the command (customize editor here) 131 cmd = ['!emacs ' f ' &']; 132 133 % do it 134 eval(cmd); 135 fprintf([cmd '\n']); 136 end; 137 end; 138 139 140 141