function dummy = updatelims(Editor)
%UPDATELIMS  Resets axis limits.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc.
%   $Revision: 1.15 $  $Date: 2000/07/31 16:04:02 $

% REVISIT
dummy = [];

% Editor modes and HG database
% RE: Protect against uninitialized call through SISO prefs (g84952)
EditorOn = ~strcmp(Editor.EditMode,'off');
AutoX = EditorOn & strcmp(Editor.XlimMode,'auto');
AutoY = EditorOn & strcmp(Editor.YlimMode,'auto');  % 2x1
MarginOn = EditorOn & strcmp(Editor.MarginVisible,'on');
HG = Editor.HG;
MagAxes = HG.PlotAxes(1);
PhaseAxes = HG.PlotAxes(2);

% Hide lines that may interfer with Y auto range
set(HG.NyquistLine,'visible','off')


%%%%%%%%%%%%
% X limits %
%%%%%%%%%%%%

% Reset X limits if in auto mode
% REVISIT: OPTLIMS (FGRID) should be unit-aware to minimize wasted space in Hz
if AutoX
	Xlim = Editor.XlimOpt;
	if MarginOn
		% Include gain and phase margin crossover freqs when within two decades
		Margins = Editor.LoopData.Margins;  
		Wc = [Margins.Wcg , Margins.Wcp];
		Wc = Wc(:,isfinite(Wc) & Wc>=Xlim(1)/100 & Wc<=100*Xlim(2));
		Xlim(1) = min([Xlim(1) , Wc]);
		Xlim(2) = max([Xlim(2) , Wc]);
	end
	Xlim = Xlim * unitconv(1,'rad/sec',Editor.FrequencyUnits);
	% Rounding
	if strcmp(Editor.FrequencyScale,'log')
		Xlim = 10.^[floor(log10(Xlim(1))+1e-3),ceil(log10(Xlim(2))-1e-3)];
	else
		Xlim = [floor(Xlim(1)) , ceil(Xlim(2))];
	end        
	set(HG.PlotAxes,'Xlim',Xlim)
else
	Xlim = get(MagAxes,'Xlim');
	if Xlim(1)<0,
		% Show only positive frequencies (can get here after zoom)
		Xlim(1) = 0;
		set(HG.PlotAxes,'Xlim',Xlim)
	end
end


%%%%%%%%%%%%
% Y limits %
%%%%%%%%%%%%

if MarginOn & ~isempty(HG.GainMargin) & ishandle(HG.GainMargin.hLine)
	% Hide lines that may interfer with the auto Y limit picker
	set([HG.GainMargin.hLine,HG.GainMargin.vLine,...
			HG.GainMargin.Patch,HG.GainMargin.Text],'Visible','off')
end

% Mag Limits
set(MagAxes,'YlimMode',Editor.YlimMode{1})
YlimM = get(MagAxes,'Ylim');
if ~AutoY(1) 
	% Make sure mag>0 (could be <0 after zoom)
	if strcmpi(Editor.MagnitudeUnits,'abs')
		YlimM(1) = max(0,YlimM(1));
	end
elseif YlimM(2)-YlimM(1)<1e-3*(abs(YlimM(1))+YlimM(2))
	% Gard against flat gain curve
	if strcmpi(Editor.MagnitudeUnits,'dB')
		YlimM = [floor(0.999*YlimM(1)) , ceil(1.001*YlimM(2))];
	else
		YlimM = [max(0,0.999*YlimM(1)) , ceil(1.001*YlimM(2))];
	end
end

% Phase limits
set(PhaseAxes,'YlimMode',Editor.YlimMode{2})
YlimP = get(PhaseAxes,'Ylim');
% Adjust phase ticks
set(PhaseAxes,'YtickMode','auto')
if strcmpi(Editor.PhaseUnits,'deg')
	Yticks = get(PhaseAxes,'YTick');
	if AutoY(2)
		% Auto mode. Determine data extent
		DataHandles = [HG.BodePlot(2) ; findobj(PhaseAxes,'Tag','MarginVline')];
		Yextent = datext(DataHandles,'y'); 
		[NewTicks,NewLims] = phaseticks(Yticks,YlimP,Yextent);
		YlimP = NewLims + [-0.01 0.01] * (NewLims(2)-NewLims(1));
	else
		% Fixed limit mode
		NewTicks = phaseticks(Yticks,YlimP);
	end
	set(PhaseAxes,'YTick',NewTicks)
end

% Adjust Nyquist lines
set(HG.NyquistLine(1,:),'Ydata',YlimM,'Visible',Editor.MagVisible)
set(HG.NyquistLine(2,:),'Ydata',YlimP,'Visible',Editor.PhaseVisible)

% All low-level limit modes are manual 
set(MagAxes,'Xlim',Xlim,'Ylim',YlimM)
set(PhaseAxes,'Xlim',Xlim,'Ylim',YlimP)

% Update/refresh margin display
if MarginOn
	showmargin(Editor);
	GMS = Editor.HG.GainMargin;
	set([GMS.hLine,GMS.vLine,GMS.Patch,GMS.Text],'Visible',Editor.MagVisible)
end

% Reorder axes children
% RE: Run TOPKIDS2 through UNIQUE to eliminate repeated handles for complex roots
if EditorOn
	HG = Editor.HG;
	% Gain plot
	TopKids1 = comphandles(Editor,'mag');
	if MarginOn
		TopKids1 = [HG.GainMargin.Text ; HG.GainMargin.Patch ; ...
				TopKids1 ; HG.GainMargin.Dot];
	end
	TopKids2 = unique([HG.System(1).Pole ; HG.System(1).Zero]);
	OtherKids = setdiff(get(MagAxes,'Children'),[TopKids1;TopKids2]);
	set(MagAxes,'Children',[TopKids1;TopKids2;OtherKids(:)])
	
	% Phase plot
	TopKids1 = comphandles(Editor,'phase');
	if MarginOn
		TopKids1 = [HG.PhaseMargin.Text ; HG.PhaseMargin.Patch ; ...
				TopKids1 ; HG.PhaseMargin.Dot];
	end
	TopKids2 = unique([HG.System(2).Pole ; HG.System(2).Zero]);
	OtherKids = setdiff(get(PhaseAxes,'Children'),[TopKids1;TopKids2]);
	set(PhaseAxes,'Children',[TopKids1;TopKids2;OtherKids(:)])
end

