emacsrunregion.m:

Check input file more rigoruously.  If not on path, make it be on path
before evaluating.
Implement version that uses 'eval'.  Leave structure in place for
calling a ML utility for doing eval of a region of a file.
This commit is contained in:
Eric Ludlam 2019-11-23 17:20:29 -05:00
parent 3f0c078dd8
commit 51170c0619

View file

@ -1,28 +1,49 @@
function emacsrunregion(file, startchar, endchar)
% Run code from FILE between STARTCHAR and ENDCHAR.
% Uses internal Editor API to enable support of local functions and breakpoints.
fullFileName = which(file); % get full expansion
if ~exist(fullFileName,'file')
error('You must save your region into an accessible file.');
if exist(file,'file')
fullFileName = file;
else
error('You must save your region into a file accessible by MATLAB process.');
end
% Now figure out if shortFileName is on the path. We can use a special eval
% if it is.
[ fullFilePath, shortFileName ] = fileparts(fullFileName);
revlookup = which(shortFileName);
if ~isempty(revlookup) && strcmp(revlookup, fullFileName)
onpath = true;
else
onpath = false;
end
% Of not on the path, temporarilly switch to that directory.
if ~onpath
oldpath = pwd;
cd(fullFilePath);
end
if true % TODO remove true eventually
[ fullFilePath, shortFileName ] = fileparts(fullFileName); %#ok % Get the file parts
fullFileText = fileread(fullFileName); % Get the text
% When other options aren't a vailable, use eval on extracted text.
% disp('Evaluating with eval');
TXT = fileread(fullFileName);
ETXT = TXT(startchar:endchar);
eval(ETXT);
% TODO - find out if this is available. If it isn't, we can extract the text and
% then use evalin or something.
else
% Newer versions of MATLAB with Live Editor can call into the evaluator.
% disp('Evaluating with LXE');
% TODO - get permission for using internal API for running regions of a file.
% builtin('_xxxx', 'base', shortFileName, startchar, endchar-startchar)
end
% Call into the evaluator.
feature('DisablePrelineEvents', true);
% TODO - Info I got says fullFilePath, not the shortFileName for
% below. Need to try in more versions of MATLAB.
builtin('_LiveEvaluate', 'caller', shortFileName, startchar, endchar-startchar, [], [], [], [], ...
true, fullFileText)
feature('DisablePrelineEvents', false);
if ~onpath % Return to previous directory
cd(oldpath);
end
end