save function can save environment in sod format
[scilab.git] / scilab / modules / io / macros / %_sodload.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2012 - DIGITEO - Antoine ELIAS
3 // Copyright (C) 2012 - DIGITEO - Vincent COUVERT
4 //
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution.  The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10
11 function %_sodload(%__filename__, varargin)
12
13     function [varValues] = %__convertVariable__(varValues, varNames)
14         for i = 1:size(varValues)
15             if typeof(varValues(i)) == "ScilabMatrixHandle" then
16                 //convert tlist to handle
17                 varValues(i) = createMatrixHandle(varValues(i));
18             elseif typeof(varValues(i)) == "ScilabMacro" then
19                 //convert tlist to macro
20                 varValues(i) = createMacro(varValues(i), varNames(i));
21             elseif isList(varValues(i)) then
22                 //list container
23                 varValues(i) = parseList(varValues(i));
24             end
25         end
26     endfunction
27
28     function result = isList(var)
29         //15 : list
30         //16 : tlist
31         //17 : mlist
32         if or(type(var) == [15, 16, 17]) then
33             result  = %t;
34         else
35             result = %f;
36         end
37     endfunction
38
39     function varValue = parseList(varValue)
40         if typeof(varValue)=="list" then
41             for i = definedfields(varValue)
42                 if typeof(varValue(i)) == "ScilabMatrixHandle" then
43                     varValue(i) = createMatrixHandle(varValue(i));
44                 elseif isList(varValue(i)) then
45                     varValue(i) = parseList(varValue(i));
46                 else
47                     varValue(i) = varValue(i);
48                 end
49             end
50         else
51             fieldNb = size(getfield(1, varValue), "*");
52             for kField = 2:fieldNb // Do not inspect first field (field names)
53                 fieldValue = getfield(kField, varValue);
54                 if typeof(fieldValue) == "ScilabMatrixHandle" then
55                     fieldValue = createMatrixHandle(fieldValue);
56                 elseif isList(fieldValue) then
57                     fieldValue = parseList(fieldValue);
58                 end
59                 setfield(kField, fieldValue, varValue);
60             end
61         end
62     endfunction
63
64     function h = createMatrixHandle(matrixHandle)
65         h = [];
66         if typeof(matrixHandle) <> "ScilabMatrixHandle" then
67             return;
68         end
69
70         for i = prod(matrixHandle.dims):-1:1
71             h($+1) = createSingleHandle(matrixHandle.values(i));
72             if or(fieldnames(matrixHandle.values(i))=="user_data") then // TODO Remove after graphic branch merge
73                 if isList(matrixHandle.values(i).user_data) then
74                     set(h($), "user_data", parseList(matrixHandle.values(i).user_data));
75                 elseif typeof(matrixHandle.values(i).user_data) == "ScilabMatrixHandle" then
76                     set(h($), "user_data", createMatrixHandle(matrixHandle.values(i).user_data));
77                 end
78             end
79         end
80         h = matrix(h, matrixHandle.dims);
81     endfunction
82
83     function h = createSingleHandle(item)
84         select item.type
85         case "Figure"
86             h = createFigure(item);
87         case "Axes"
88             h = createAxes(item);
89         case "Polyline"
90             h = createPolyline(item);
91         case "Plot3d"
92             h = createPlot3d(item);
93         case "Fac3d"
94             h = createFac3d(item);
95         case "Compound"
96             h = createCompound(item);
97         case "Rectangle"
98             h = createRectangle(item);
99         case "Arc"
100             h = createArc(item);
101         case "Champ"
102             h = createChamp(item);
103         case "Segs"
104             h = createSegs(item);
105         case "Grayplot"
106             h = createGrayplot(item);
107         case "Matplot"
108             h = createMatplot(item);
109         case "Fec"
110             h = createFec(item);
111         case "Legend"
112             h = createLegend(item);
113         case "Text"
114             h = createText(item);
115         case "Axis"
116             h = createAxis(item);
117         case "uimenu"
118             h = createuimenu(item);
119         case "uicontextmenu"
120             h = createuicontextmenu(item);
121         case "uicontrol"
122             h = createuicontrol(item);
123         else
124             error("handle of type " + item.type + " unhandled");
125             h = [];
126         end
127     endfunction
128
129     //
130     // FIGURE
131     //
132     function h = createFigure(figureProperties)
133         fields = fieldnames(figureProperties);
134         fields(1) = [];
135
136         h = gcf();
137         fields(fields=="figure_id") = [];
138
139         h.figure_position=figureProperties.figure_position;
140         fields(fields=="figure_position") = [];
141         // set auto_resize first otherwise viewport modification may not have any effect.
142         h.auto_resize = figureProperties.auto_resize;
143         fields(fields=="auto_resize") = [];
144         h.figure_size = figureProperties.figure_size;
145         fields(fields=="figure_size") = [];
146         // set axes_size last because it's more important than figure_size
147         h.axes_size = figureProperties.axes_size;
148         fields(fields=="axes_size") = [];
149
150         for i = 1:size(fields, "*")
151             if fields(i) == "children" then
152                 createMatrixHandle(figureProperties(fields(i)));
153             else
154                 set(h, fields(i), figureProperties(fields(i)));
155             end
156         end
157     endfunction
158
159     //
160     // LABEL
161     //
162     function h = createLabel(labelProperties, h)
163         fields = fieldnames(labelProperties);
164         fields(1) = [];
165         for i = 1:size(fields, "*")
166             set(h, fields(i), labelProperties(fields(i)));
167         end
168     endfunction
169
170     //
171     // TICKS
172     //
173     function h = createTicks(ticksProperties)
174         h = tlist(['ticks','locations','labels'], [], []);
175         fields = fieldnames(ticksProperties);
176         for i = 1:size(fields, "*")
177             h(fields(i)) = ticksProperties(fields(i));
178         end
179     endfunction
180
181     //
182     // AXES
183     //
184     function h = createAxes(axesProperties)
185         // Hack to determine whether %h_load has been called by the %h_copy macro
186         // in which case a new Axes object is created
187         [lnums, fnames] = where();
188         ind = grep(fnames, '%h_copy');
189         if ~isempty(ind) then
190           newaxes();
191         end;
192
193         h = gca();
194         fields = fieldnames(axesProperties);
195         fields(1) = [];
196
197         // Get log_flags to be sure to set them after data_bounds
198         log_flags = axesProperties.log_flags;
199         fields(fields=="log_flags") = [];
200
201         // Get mark_mode to be sure to set it after mark_style
202         mark_mode = axesProperties.mark_mode;
203         fields(fields=="mark_mode") = [];
204
205         // Get auto_ticks to be sure to set it after ticks labels
206         auto_ticks = axesProperties.auto_ticks;
207         fields(fields=="auto_ticks") = [];
208
209         for i = 1:size(fields, "*")
210             if or(fields(i) == ["title","x_label","y_label","z_label"]) then
211                 createLabel(axesProperties(fields(i)), h(fields(i)));
212             elseif or(fields(i) == ["x_ticks", "y_ticks", "z_ticks"]) then
213                 h(fields(i)) = createTicks(axesProperties(fields(i)));
214             elseif fields(i) == "children" then
215                 createMatrixHandle(axesProperties(fields(i)));
216             elseif fields(i) == "clip_state" then
217                 if axesProperties.clip_state=="on" then
218                     set(h,"clip_box",axesProperties.clip_box);
219                 end
220                 set(h,"clip_state", axesProperties.clip_state);
221             elseif fields(i) == "clip_box" then
222                 // managed with 'clip_state'
223             elseif fields(i) == "data_bounds" then
224                 set(h, "data_bounds", axesProperties.data_bounds);
225                 set(h, "log_flags", log_flags);
226             elseif fields(i) == "mark_style" then
227                 set(h, "mark_style", axesProperties.mark_style);
228                 set(h, "mark_mode", mark_mode);
229             else
230                 set(h, fields(i), axesProperties(fields(i)));;
231             end
232         end
233
234         set(h, "auto_ticks", auto_ticks);
235
236         // Legend management
237         global %LEG
238         if ~isempty(%LEG) then
239             // Get handles from paths
240             links=getlinksfrompath(h, %LEG.paths)
241             if ~isempty(links) then
242                 L = captions(links, %LEG.text)
243                 L.visible         = %LEG.visible
244                 L.font_style      = %LEG.font_style
245                 L.font_size       = %LEG.font_size
246                 L.font_color      = %LEG.font_color
247                 L.fractional_font = %LEG.fractional_font
248                 L.mark_mode       = 'off';
249                 L.legend_location = %LEG.legend_location
250                 L.position        = %LEG.position
251                 L.line_mode       = %LEG.line_mode
252                 L.thickness       = %LEG.thickness
253                 L.foreground      = %LEG.foreground
254                 L.fill_mode       = %LEG.fill_mode
255                 L.background      = %LEG.background
256                 if %LEG.clip_state=="on" then
257                     L.clip_box      = %LEG.clip_box
258                 end
259                 L.clip_state      = %LEG.clip_state
260                 L.user_data       = %LEG.user_data
261             else
262                 warning(msprintf(_("%s: Legend does not fit with the current context. Skipped\n"), "load"));
263             end
264         end
265         clearglobal %LEG
266     endfunction
267
268     //
269     // POLYLINE
270     //
271     function h = createPolyline(polylineProperties)
272         fields = fieldnames(polylineProperties);
273         fields(1) = [];
274
275         xpoly(polylineProperties.data(:,1), polylineProperties.data(:,2))
276
277         h = gce();
278
279         if polylineProperties.clip_state=="on" then
280             set(h, "clip_box", polylineProperties.clip_box)
281         end
282         set(h, "clip_state", polylineProperties.clip_state);
283         fields(fields=="clip_box") = [];
284         fields(fields=="clip_state") = [];
285
286         if polylineProperties.interp_color_mode=="on" & ~isempty(polylineProperties.interp_color_vector) then
287             set(h, "interp_color_vector", polylineProperties.interp_color_vector);
288             set(h, "interp_color_mode", polylineProperties.interp_color_mode);
289         else
290             if ~isempty(polylineProperties.interp_color_vector) then
291                 h.interp_color_vector = polylineProperties.interp_color_vector;
292             end
293             h.interp_color_mode = polylineProperties.interp_color_mode;
294         end
295         fields(fields=="interp_color_vector") = [];
296         fields(fields=="interp_color_mode") = [];
297
298         // Get mark_mode to be sure to set it after mark_style
299         mark_mode = polylineProperties.mark_mode;
300         fields(fields=="mark_mode") = [];
301
302         for i = 1:size(fields, "*")
303             if fields(i) == "mark_style" then
304                 set(h, "mark_style", polylineProperties.mark_style);
305                 set(h, "mark_mode", mark_mode);
306             else
307                 h(fields(i)) = polylineProperties(fields(i));
308             end
309         end
310     endfunction
311
312     //
313     // PLOT3D
314     //
315     function h = createPlot3d(plot3dProperties)
316         h = createSurface(plot3dProperties);
317     endfunction
318
319     //
320     // FAC3D
321     //
322     function h = createFac3d(fac3dProperties)
323         h = createSurface(fac3dProperties);
324     endfunction
325
326     //
327     // SURFACE
328     //
329     function h = createSurface(surfaceProperties)
330         fields = fieldnames(surfaceProperties);
331         fields(1) = [];
332         // plot3d modify the axes properties
333         //  - Save it
334         // - Draw plot3d
335         // - Restore it
336         a=gca();
337         rotation_angles = a.rotation_angles;
338         axes_visible = a.axes_visible;
339         box = a.box;
340         margins = a.margins;
341         x_label_visible = a.x_label.visible;
342         y_label_visible = a.y_label.visible;
343         z_label_visible = a.z_label.visible;
344         x_label_text = a.x_label.text;
345         y_label_text = a.y_label.text;
346         z_label_text = a.z_label.text;
347         axes_isoview = a.isoview;
348
349         if (or(surfaceProperties.color_flag==[2 5]) & ~or(fields=="cdata_mapping")) | ..
350             ((surfaceProperties.color_flag>=2) & or(fields=="cdata_mapping")) then
351             plot3d1(surfaceProperties.data.x, surfaceProperties.data.y, list(surfaceProperties.data.z, surfaceProperties.data.color))
352         else
353             plot3d(surfaceProperties.data.x,surfaceProperties.data.y,surfaceProperties.data.z)
354         end
355         fields(fields=="data") = [];
356
357         // Restore this properties after plot3d.
358         a.rotation_angles = rotation_angles;
359         a.axes_visible = axes_visible;
360         a.box = box;
361         a.margins = margins;
362         a.x_label.visible = x_label_visible;
363         a.y_label.visible = y_label_visible;
364         a.z_label.visible = z_label_visible;
365         a.x_label.text = x_label_text;
366         a.y_label.text = y_label_text;
367         a.z_label.text = z_label_text;
368         a.isoview = axes_isoview;
369
370         // Get mark_mode to be sure to set it after mark_style
371         mark_mode = surfaceProperties.mark_mode;
372         fields(fields=="mark_mode") = [];
373
374         h=gce();
375
376         if or(fields=="cdata_mapping") then // Fac3d specific
377             if surfaceProperties.color_flag >= 2 then
378                 set(h, "cdata_mapping", surfaceProperties.cdata_mapping);
379             end
380             fields(fields=="cdata_mapping") = [];
381         end
382
383         if surfaceProperties.clip_state == "on" then
384             set(h,"clip_box", surfaceProperties.clip_box);
385         end
386         set(h,"clip_state",surfaceProperties.clip_state);
387         fields(fields=="clip_box") = [];
388         fields(fields=="clip_state") = [];
389
390         for i = 1:size(fields, "*")
391             if fields(i) == "mark_style" then
392                 set(h, "mark_style", surfaceProperties.mark_style);
393                 set(h, "mark_mode", mark_mode);
394             else
395                 h(fields(i)) = surfaceProperties(fields(i));
396             end
397         end
398     endfunction
399
400     //
401     // COMPOUND
402     //
403     function h = createCompound(compoundProperties)
404         fields = fieldnames(compoundProperties);
405         fields(1) = [];
406
407         h = glue(createMatrixHandle(compoundProperties.children));
408         fields(fields=="children") = [];
409
410         for i = 1:size(fields, "*")
411             set(h, fields(i), compoundProperties(fields(i)));
412         end
413     endfunction
414
415     //
416     // RECTANGLE
417     //
418     function h = createRectangle(rectangleProperties)
419         fields = fieldnames(rectangleProperties);
420         fields(1) = [];
421
422         xrect(0,1,1,1); // create the rectangle with dummy values
423         h = gce();
424
425         if rectangleProperties.clip_state == "on" then
426             set(h,"clip_box", rectangleProperties.clip_box);
427         end
428         set(h,"clip_state",rectangleProperties.clip_state);
429         fields(fields=="clip_box") = [];
430         fields(fields=="clip_state") = [];
431
432         // Get mark_mode to be sure to set it after mark_style
433         mark_mode = rectangleProperties.mark_mode;
434         fields(fields=="mark_mode") = [];
435
436         for i = 1:size(fields, "*")
437             if fields(i) == "mark_style" then
438                 set(h, "mark_style", rectangleProperties.mark_style);
439                 set(h, "mark_mode", mark_mode);
440             else
441                 h(fields(i)) = rectangleProperties(fields(i));
442             end
443         end
444     endfunction
445
446     //
447     // ARC
448     //
449     function h = createArc(arcProperties)
450         fields = fieldnames(arcProperties);
451         fields(1) = [];
452
453         xarc(0,1,1,1,0,360); // create the arc with dummy values
454         h = gce();
455
456         if arcProperties.clip_state == "on" then
457             set(h,"clip_box", arcProperties.clip_box);
458         end
459         set(h,"clip_state",arcProperties.clip_state);
460         fields(fields=="clip_box") = [];
461         fields(fields=="clip_state") = [];
462
463         for i = 1:size(fields, "*")
464             set(h, fields(i), arcProperties(fields(i)));
465         end
466     endfunction
467
468     //
469     // CHAMP
470     //
471     function h = createChamp(champProperties)
472         fields = fieldnames(champProperties);
473         fields(1) = [];
474
475         champ(champProperties.data.x, champProperties.data.y, champProperties.data.fx, champProperties.data.fy);
476         fields(fields=="data") = [];
477
478         h=gce();
479
480         if champProperties.clip_state == "on" then
481             set(h,"clip_box", champProperties.clip_box);
482         end
483         set(h,"clip_state",champProperties.clip_state);
484         fields(fields=="clip_box") = [];
485         fields(fields=="clip_state") = [];
486
487         for i = 1:size(fields, "*")
488             set(h, fields(i), champProperties(fields(i)));
489         end
490     endfunction
491
492     //
493     // SEG
494     //
495     function h = createSegs(segsProperties)
496         fields = fieldnames(segsProperties);
497         fields(1) = [];
498
499         xsegs(segsProperties.data(:,1), segsProperties.data(:,2))
500
501         h=gce()
502
503         if segsProperties.clip_state == "on" then
504             set(h,"clip_box", segsProperties.clip_box);
505         end
506         set(h,"clip_state",segsProperties.clip_state);
507         fields(fields=="clip_box") = [];
508         fields(fields=="clip_state") = [];
509
510         // Get mark_mode to be sure to set it after mark_style
511         mark_mode = segsProperties.mark_mode;
512         fields(fields=="mark_mode") = [];
513
514         for i = 1:size(fields, "*")
515             if fields(i) == "mark_style" then
516                 set(h, "mark_style", segsProperties.mark_style);
517                 set(h, "mark_mode", mark_mode);
518             else
519                 h(fields(i)) = segsProperties(fields(i));
520             end
521         end
522     endfunction
523
524     //
525     // GRAYPLOT
526     //
527     function h = createGrayplot(grayplotProperties)
528         fields = fieldnames(grayplotProperties);
529         fields(1) = [];
530
531         grayplot(grayplotProperties.data.x, grayplotProperties.data.y, grayplotProperties.data.z);
532         fields(fields=="data") = [];
533
534         h = gce();
535
536         if grayplotProperties.clip_state=="on" then
537             set(h, "clip_box", grayplotProperties.clip_box)
538         end
539         set(h, "clip_state", grayplotProperties.clip_state);
540         fields(fields=="clip_box") = [];
541         fields(fields=="clip_state") = [];
542
543         for i = 1:size(fields, "*")
544             set(h, fields(i), grayplotProperties(fields(i)));
545         end
546     endfunction
547
548     //
549     // MATPLOT
550     //
551     function h = createMatplot(matplotProperties)
552         fields = fieldnames(matplotProperties);
553         fields(1) = [];
554
555         Matplot(matplotProperties.data);
556         fields(fields=="data") = [];
557
558         h = gce();
559
560         if matplotProperties.clip_state=="on" then
561             set(h, "clip_box", matplotProperties.clip_box)
562         end
563         set(h, "clip_state", matplotProperties.clip_state);
564         fields(fields=="clip_box") = [];
565         fields(fields=="clip_state") = [];
566
567         for i = 1:size(fields, "*")
568             set(h, fields(i), matplotProperties(fields(i)));
569         end
570     endfunction
571
572     //
573     // FEC
574     //
575     function h = createFec(fecProperties)
576         fields = fieldnames(fecProperties);
577         fields(1) = [];
578
579         fec(fecProperties.data(:,1), fecProperties.data(:,2), fecProperties.triangles, fecProperties.data(:,3));
580         fields(fields=="data") = [];
581         fields(fields=="triangles") = [];
582
583         h = unglue(gce());
584
585         if fecProperties.clip_state=="on" then
586             set(h, "clip_box", fecProperties.clip_box)
587         end
588         set(h, "clip_state", fecProperties.clip_state);
589         fields(fields=="clip_box") = [];
590         fields(fields=="clip_state") = [];
591
592         for i = 1:size(fields, "*")
593             set(h, fields(i), fecProperties(fields(i)));
594         end
595     endfunction
596
597     //
598     // LEGEND
599     //
600     function h = createLegend(legendProperties)
601         global %LEG
602         %LEG = legendProperties;
603         endfunction
604
605         //
606         // TEXT
607         //
608         function h = createText(textProperties)
609         fields = fieldnames(textProperties);
610         fields(1) = [];
611
612         if textProperties.text_box_mode == 'off' then
613             xstring(textProperties.data(1), textProperties.data(2), textProperties.text)
614         else
615             xstringb(textProperties.data(1), textProperties.data(2), textProperties.text, textProperties.text_box(1), textProperties.text_box(2))
616         end
617
618         h = gce();
619
620         if textProperties.clip_state=="on" then
621             set(h, "clip_box", textProperties.clip_box)
622         end
623         set(h, "clip_state", textProperties.clip_state);
624         fields(fields=="clip_box") = [];
625         fields(fields=="clip_state") = [];
626
627         for i = 1:size(fields, "*")
628             set(h, fields(i), textProperties(fields(i)));
629         end
630     endfunction
631
632     //
633     // AXIS
634     //
635     function h = createAxis(axisProperties)
636         fields = fieldnames(axisProperties);
637         fields(1) = [];
638
639         if axisProperties.tics_direction == "bottom" then
640             axisdir='d';
641         elseif axisProperties.tics_direction == "top" then
642             axisdir='u';
643         elseif axisProperties.tics_direction == "left" then
644             axisdir='l';
645         elseif axisProperties.tics_direction == "right" then
646             axisdir='r';
647         elseif size(axisProperties.xtics_coord, "*") > 1 then
648             axisdir='u';
649         else
650             axisdir='l';
651         end
652         fields(fields=="tics_direction") = [];
653
654         drawaxis(x=axisProperties.xtics_coord,y=axisProperties.ytics_coord,dir=axisdir);
655         fields(fields=="xtics_coord") = [];
656         fields(fields=="ytics_coord") = [];
657
658         h=gce()
659
660         if axisProperties.clip_state=="on" then
661             set(h, "clip_box", axisProperties.clip_box)
662         end
663         set(h, "clip_state", axisProperties.clip_state);
664         fields(fields=="clip_box") = [];
665         fields(fields=="clip_state") = [];
666
667         for i = 1:size(fields, "*")
668             set(h, fields(i), axisProperties(fields(i)));
669         end
670     endfunction
671
672     //
673     // uimenu
674     //
675     function h = createuimenu(uimenuProperties)
676         fields = fieldnames(uimenuProperties);
677         fields(1) = [];
678
679         h = uimenu();
680
681         for i = 1:size(fields, "*")
682             if fields(i) == "children" then
683                 children = createMatrixHandle(uimenuProperties(fields(i)));
684                 for k=1:size(children, "*")
685                     set(children(k), "parent", h);
686                 end
687             else
688                 set(h, fields(i), uimenuProperties(fields(i)));
689             end
690         end
691     endfunction
692
693     //
694     // UICONTEXTMENU
695     //
696     function h = createuicontextmenu(uicontextmenuProperties)
697         fields = fieldnames(uicontextmenuProperties);
698         fields(1) = [];
699
700         h = uicontextmenu();
701
702         for i = 1:size(fields, "*")
703             if fields(i) == "children" then
704                 children = createMatrixHandle(uicontextmenuProperties(fields(i)));
705                 for k=1:size(children, "*")
706                     set(children(k), "parent", h);
707                 end
708             else
709                 set(h, fields(i), uicontextmenuProperties(fields(i)));
710             end
711         end
712     endfunction
713
714     //
715     // uicontrol
716     //
717     function h = createuicontrol(uicontrolProperties)
718         fields = fieldnames(uicontrolProperties);
719         fields(1) = [];
720
721         h = uicontrol("Style", uicontrolProperties.style);
722         fields(fields=="style") = [];
723
724         for i = 1:size(fields, "*")
725             if fields(i) == "children" then
726                 children = createMatrixHandle(uicontrolProperties(fields(i)));
727                 for k=1:size(children, "*")
728                     set(children(k), "parent", h);
729                 end
730             else
731                 set(h, fields(i), uicontrolProperties(fields(i)));
732             end
733         end
734     endfunction
735
736     // Utility function for legends, copy/paste from %h_load
737     function links=getlinksfrompath(ax,paths)
738         //  ax is a  handle on an axes entity
739         //  paths a list or row vector which gives the set of paths relative to
740         //  the axes
741         links=[];
742         ok=%t;
743         for p=paths
744             e=ax;
745             p(1)=p(1)-1// the caption does not exists yet
746             for kp=1:size(p,'*'),
747                 if or(e.type==['Axes','Compound'])&p(kp)<=size(e.children,'*') then
748                     e=e.children(p(kp)),
749                 else
750                     ok=%f
751                     break
752                 end
753             end
754             if ~ok then
755                 break
756             end
757             links=[links,e]
758         end
759         if ~ok then
760             links=[];
761         end
762     endfunction
763
764     function macro = createMacro(macroStr, macroName)
765         macroSt = macroStr(3);
766         if macroStr(2) == %t then
767             flag = "c";
768         else
769             flag = "n";
770         end
771         header = strsubst(macroSt(1), "function ", "");
772         body = macroSt(2:$-1);
773         if body == [] then
774             body = "";
775         end
776         deff(header, body, flag);
777         execstr("macro = " + macroName);
778     endfunction
779
780     [%__lhs__, %__rhs__] = argn();
781     %__resumeList__ = list();
782     %__resumeVarlist__ = [];
783     if %__rhs__ < 1 then
784         error(999, msprintf(gettext("%s: Wrong number of input arguments: %d expected.\n"), "load", 1));
785     end
786
787     if %__rhs__ >= 1 then
788         if typeof(%__filename__) <> "string" | size(%__filename__, "*") <> 1 then
789             error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "load", 1));
790         end
791     end
792
793     if isfile(%__filename__) & is_hdf5_file(%__filename__) then
794         %__loadFunction__ = import_from_hdf5;
795     else
796         %__loadFunction__ = %_load;
797     end
798
799     //multiple output variables to prevent listinfile prints
800     [%__variableList__, %__varB__, %__varC__, %__varD__] = listvarinfile(%__filename__);
801
802     //
803     if size(varargin) <> 0 then
804         for i = 1:size(varargin)
805             %__variableName__ = varargin(i);
806             if typeof(%__variableName__) <> "string" | size(%__variableName__, "*") <> 1 then
807                 error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "load", i));
808             end
809
810             if or(%__variableList__ == %__variableName__) then
811                 %__loadFunction__(%__filename__, %__variableName__);
812                 %__resumeList__($+1) = evstr(%__variableName__);
813                 %__resumeVarlist__($+1) = %__variableName__;
814             else
815                 error(999, msprintf(gettext("%s: variable ''%s'' does not exist in ''%s''.\n"), "load", %__variableName__, %__filename__));
816             end
817         end
818     else
819         for i = 1:size(%__variableList__, "*")
820             %__variableName__ = %__variableList__(i);
821             %__loadFunction__(%__filename__, %__variableName__);
822             %__resumeList__($+1) = evstr(%__variableName__);
823             %__resumeVarlist__($+1) = %__variableName__;
824         end
825     end
826
827     if isfile(%__filename__) & is_hdf5_file(%__filename__) then
828         %__resumeList__ = %__convertVariable__(%__resumeList__, %__resumeVarlist__);
829     end
830
831     execstr("[" + strcat(%__resumeVarlist__, ",") + "] = resume(%__resumeList__(:))");
832 endfunction