@@ -27,63 +27,71 @@ function emacsdocomplete(substring)
27
27
28
28
persistent verNum ;
29
29
if isempty(verNum )
30
- v = ver(' MATLAB' ); % #ok<*VERMATLAB>
30
+ v = ver(' MATLAB' ); % #ok<*VERMATLAB>
31
31
verNum = str2double(v .Version );
32
32
end
33
33
34
+ qStr = strrep(substring , ' '' ' , ' '''' ' );
35
+ lStr = num2str(length(substring ));
36
+
34
37
if verNum >= 25 % R2025a and later
35
38
36
- cmd = [' builtin('' _programmingAidsTest'' , '''' , '' ' , ...
37
- substring , ' '' , ' , num2str(length( substring )), ' , []) ' ];
38
- json = evalin( ' base ' , cmd );
39
- completionInfo = jsondecode( json );
39
+ cmd = [' builtin('' _programmingAidsTest'' , '''' , '' ' , qStr , ' '' , ' , lStr , ' , []) ' ];
40
+ cInfo = jsondecode(evalin( ' base ' , cmd )); % use base workspace for variable completions
41
+ cMap = dictionary( ' ' , true );
42
+
40
43
disp([' Completions-Lisp:' , newline , ' '' (' ]);
41
- if strcmp(completionInfo .widgetType , ' completion' )
42
- useCellIndex = iscell(completionInfo .widgetData .choices );
43
- nChoices = length(completionInfo .widgetData .choices );
44
- for idx = 1 : nChoices
45
- if useCellIndex
46
- entry = completionInfo.widgetData.choices{idx };
47
- else
48
- entry = completionInfo .widgetData .choices(idx );
49
- end
50
- if isfield(entry , ' purpose' )
51
- purpose = [entry .purpose , ' ' ];
52
- else
53
- purpose = ' ' ;
44
+ if isfield(cInfo , " signatures" )
45
+ sigs = cInfo .signatures ;
46
+ cSigs = iscell(sigs );
47
+ for i = 1 : length(sigs )
48
+ if cSigs , args = sigs{i }.inputArguments; else , args = sigs(i ).inputArguments; end
49
+ cArgs = iscell(args );
50
+ for j = 1 : length(args )
51
+ if cArgs , arg = args{j }; else , arg = args(j ); end
52
+ if isfield(arg , ' widgetData' ) && isfield(arg .widgetData , ' choices' )
53
+ DispCompletionChoices(arg .widgetData .choices );
54
+ end
54
55
end
55
- desc = [purpose , ' (' entry .matchType , ' )' ];
56
- desc = regexprep(desc ,' "' , ' \\ "' );
57
- disp([' ("' , entry .completion , ' " . "' , desc , ' ")' ]);
58
56
end
57
+ elseif isfield(cInfo , ' widgetData' ) && isfield(cInfo .widgetData , ' choices' )
58
+ DispCompletionChoices(cInfo .widgetData .choices );
59
59
end
60
60
disp(' )' );
61
61
62
62
else % R2024b and earlier
63
63
64
64
if verNum < 8.4
65
- % Pre R2014b: partial_string
66
- extracmd = ' ' ;
65
+ extracmd = ' ' ; % Pre R2014b: partial_string
67
66
else
68
- % Post R2014b: partial_string, caret, num
69
- extracmd = [ ' , ' num2str(length(substring )) ' ,0' ];
67
+ extracmd = [' , ' , lStr , ' , 0' ]; % Post R2014b: partial_string, caret, num
70
68
end
69
+ cmd = [' matlabMCRprocess_emacs = com.mathworks.jmi.MatlabMCR;' ...
70
+ ' emacs_completions_output = matlabMCRprocess_emacs.mtFindAllTabCompletions('' ' ...
71
+ qStr ' '' ' extracmd ' ),' ...
72
+ ' clear('' matlabMCRprocess_emacs'' ,'' emacs_completions_output'' );' ];
73
+ evalin(' base' , cmd ); % run in base to get completions on base workspace variables
71
74
72
- substringQuoted = strrep(substring , ' '' ' , ' '''' ' );
73
-
74
- command = ...
75
- [' matlabMCRprocess_emacs = com.mathworks.jmi.MatlabMCR;' ...
76
- ' emacs_completions_output = matlabMCRprocess_emacs.mtFindAllTabCompletions('' ' ...
77
- substringQuoted ' '' ' extracmd ' ),' ...
78
- ' clear('' matlabMCRprocess_emacs'' ,'' emacs_completions_output'' );' ];
79
-
80
- % Completion engine needs to run in the base workspace to know what the variables you have
81
- % to work with are.
82
- evalin(' base' , command );
75
+ end
83
76
77
+ function DispCompletionChoices(choices )
78
+ nChoices = length(choices );
79
+ cChoices = iscell(choices );
80
+ for choiceIdx = 1 : nChoices
81
+ if cChoices , entry = choices{choiceIdx }; else , entry = choices(choiceIdx ); end
82
+ if ~cMap .isKey(entry .completion )
83
+ cMap(entry .completion ) = true ;
84
+ if isfield(entry , ' purpose' ), info = [entry .purpose , ' ' ]; else , info = ' ' ; end
85
+ desc = [info , ' (' entry .matchType , ' )' ];
86
+ desc = regexprep(desc , ' "' , ' \\ "' );
87
+ comp = regexprep(entry .completion , ' "' , ' \\ "' );
88
+ disp([' ("' , comp , ' " . "' , desc , ' ")' ]);
89
+ end
90
+ end
84
91
end
85
92
86
- end
93
+ end % emacsdocomplete
94
+
87
95
88
96
89
97
function done = UseDashComplete(substring )
@@ -106,11 +114,14 @@ function emacsdocomplete(substring)
106
114
% 'sub3'
107
115
%
108
116
% See details in `matlab-shell-completion-list'.
117
+ %
118
+ % TODO: remove and use function signatures.
119
+ % https://www.mathworks.com/help/mps/restfuljson/matlab-function-signatures-in-json.html
109
120
%
110
121
111
122
persistent completeSw ; % if completeSw(cmd), then supports -complete
112
123
if isempty(completeSw )
113
- completeSw= containers .Map();
124
+ completeSw = containers .Map(); % use containers.Map instead of dictionary for old releases
114
125
end
115
126
116
127
done = false ;
@@ -124,13 +135,13 @@ function emacsdocomplete(substring)
124
135
else
125
136
supportsDashComplete = false ; % assume
126
137
f = which(cmd );
127
- if regexp(f ,' \.m$' )
138
+ if regexp(f , ' \.m$' )
128
139
fid= fopen(f , ' r' );
129
140
if fid ~= - 1
130
141
while true
131
142
l = fgetl(fid );
132
143
if ~ischar(l ), break , end
133
- if regexp(l ,' SUPPORTS_DASH_COMPLETE' )
144
+ if regexp(l , ' SUPPORTS_DASH_COMPLETE' )
134
145
supportsDashComplete = true ;
135
146
break
136
147
end
@@ -145,11 +156,15 @@ function emacsdocomplete(substring)
145
156
% For /path/to/cmd.ext we have /path/to/cmd.complete which
146
157
% signals that we can get the completions by calling
147
158
% CMD -complete ARGS
148
- completeCmd = regexprep(substring ,' ^(\w+)' ,' $1 -complete' );
159
+ completeCmd = regexprep(substring , ' ^(\w+)' , ' $1 -complete' );
149
160
disp(' emacs_completions_output =' );
150
- evalin(' base' ,completeCmd );
161
+ evalin(' base' , completeCmd );
151
162
done = true ;
152
163
end
153
164
end
154
165
155
166
end
167
+
168
+ % [EOF] toolbox/emacsdocomplete.m
169
+
170
+ % LocalWords: ciolfi ludlam zappo gmail Rprocess Sw
0 commit comments