1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
% Author: Eric Ludlam <zappo@gnu.org>, John Ciolfi <john.ciolfi.32@gmail.com>
% Copyright (C) 2010-2025 Free Software Foundation, Inc.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
function emacsdocomplete(substring)
% EMACSDOCOMPLETE - get completions of SUBSTRING
%
% This is used by Emacs TAB in matlab-shell to provide possible completions.
%
if UseDashComplete(substring)
return
end
persistent verNum;
if isempty(verNum)
v = ver('MATLAB'); %#ok<*VERMATLAB>
verNum = str2double(v.Version);
end
qStr = strrep(substring, '''', '''''');
lStr = num2str(length(substring));
if verNum >= 25 % R2025a and later
cmd = ['builtin(''_programmingAidsTest'', '''', ''', qStr, ''', ', lStr, ', [])'];
cInfo = jsondecode(evalin('base', cmd)); % use base workspace for variable completions
cMap = dictionary('', true);
disp(['Completions-Lisp:', newline, '''(']);
if isfield(cInfo, "signatures")
sigs = cInfo.signatures;
cSigs = iscell(sigs);
for i = 1 : length(sigs)
if cSigs, args = sigs{i}.inputArguments; else, args = sigs(i).inputArguments; end
cArgs = iscell(args);
for j = 1 : length(args)
if cArgs, arg = args{j}; else, arg = args(j); end
if isfield(arg, 'widgetData') && isfield(arg.widgetData, 'choices')
DispCompletionChoices(arg.widgetData.choices);
end
end
end
elseif isfield(cInfo, 'widgetData') && isfield(cInfo.widgetData, 'choices')
DispCompletionChoices(cInfo.widgetData.choices);
end
disp(')');
else % R2024b and earlier
if verNum < 8.4
extracmd = ''; % Pre R2014b: partial_string
else
extracmd = [', ', lStr, ', 0']; % Post R2014b: partial_string, caret, num
end
cmd = ['matlabMCRprocess_emacs = com.mathworks.jmi.MatlabMCR;' ...
'emacs_completions_output = matlabMCRprocess_emacs.mtFindAllTabCompletions(''' ...
qStr '''' extracmd '),' ...
'clear(''matlabMCRprocess_emacs'',''emacs_completions_output'');'];
evalin('base', cmd); % run in base to get completions on base workspace variables
end
function DispCompletionChoices(choices)
nChoices = length(choices);
cChoices = iscell(choices);
for choiceIdx = 1 : nChoices
if cChoices, entry = choices{choiceIdx}; else, entry = choices(choiceIdx); end
if ~cMap.isKey(entry.completion)
cMap(entry.completion) = true;
if isfield(entry, 'purpose'), info = [entry.purpose, ' ']; else, info = ''; end
desc = [info, '(' entry.matchType, ')'];
desc = regexprep(desc, '"', '\\"');
comp = regexprep(entry.completion, '"', '\\"');
disp([' ("', comp, '" . "', desc, '")']);
end
end
end
end % emacsdocomplete
function done = UseDashComplete(substring)
% UseDashComplete - given SUBSTRING 'CMD ARGS', run 'CMD -complete ARGS' to get completions?
%
% For substring of form 'cmd ARGS', use 'cmd -complete ARGS' to get completions if cmd is a *.m file
% and it contains the string 'SUPPORTS_DASH_COMPLETE'.
% 1. In a comment, place the string "SUPPORTS_DASH_COMPLETE"
% 2. Handle the -complete argument which produces completion strings
% of the form:
% 'CMD_TEXT_TO_REPLACE' --> 'REPLACEMENT_TEXT'
% 'OPTION1'
% 'OPTION2'
% ...
% Example
% >> cd2 -complete $d/s*
% '$d/s*' --> '/local/USER/'
% 'sub1'
% 'sub2'
% 'sub3'
%
% See details in `matlab-shell-completion-list'.
%
% TODO: remove and use function signatures.
% https://www.mathworks.com/help/mps/restfuljson/matlab-function-signatures-in-json.html
%
persistent completeSw; % if completeSw(cmd), then supports -complete
if isempty(completeSw)
completeSw = containers.Map(); % use containers.Map instead of dictionary for old releases
end
done = false;
cmd = regexp(substring, '^(\w+)\s+[^\)]', 'tokens');
if isscalar(cmd)
cmd=cmd{1}{1};
if completeSw.isKey(cmd)
supportsDashComplete = completeSw(cmd);
else
supportsDashComplete = false; % assume
f = which(cmd);
if regexp(f, '\.m$')
fid=fopen(f, 'r');
if fid ~= -1
while true
l = fgetl(fid);
if ~ischar(l), break, end
if regexp(l, 'SUPPORTS_DASH_COMPLETE')
supportsDashComplete = true;
break
end
end
fclose(fid);
end
end
completeSw(cmd) = supportsDashComplete;
end
if supportsDashComplete
% For /path/to/cmd.ext we have /path/to/cmd.complete which
% signals that we can get the completions by calling
% CMD -complete ARGS
completeCmd = regexprep(substring, '^(\w+)', '$1 -complete');
disp('emacs_completions_output =');
evalin('base', completeCmd);
done = true;
end
end
end
% [EOF] toolbox/emacsdocomplete.m
% LocalWords: ciolfi ludlam zappo gmail Rprocess Sw
|