No specific charges have been brought against Gen Fonseka
The Supreme Court in Sri Lanka has refused to order the immediate release of defeated presidential candidate General Sarath Fonseka.
He is currently being held in military detention at the navy's headquarters. The court was responding to a petition lodged by Gen Fonseka's wife.
The government wants to put him before a military court, but the general argues that as he is retired, he is only subject to civilian law.
A further hearing is set for 26 April.
Gen Fonseka's wife, Anoma, told the BBC that she believes the court will eventually find in her husband's favour.
She had asked the court to order the immediate release of her husband pending a judgement on the main petition. The court is looking at wider allegations that Gen Fonseka's detention is illegal and that a whole series of his rights are being infringed.
"I believe the court will do justice. The court rejected an interim relief but I believe the court will eventually be fair to us," she said.
The authorities accuse Gen Fonseka of having worked with what they call anti-government forces but no specific charges have been brought.
A lawyer for the general, Chrishmal Warnasuriya, told the BBC that the court upheld two of the petitioners' other requests - that his safety and security be guaranteed, he receives medical attention and broader access to his family, colleagues and lawyers.
The BBC's Charles Haviland in Colombo says that Gen Fonseka's situation looks bleak.
Officials say that the general worked to destabilise the government - he has consistently denied this.
Military spokesman Maj-Gen Prasad Samarasinghe told the BBC it would finish amassing evidence against him "within a very short time" and would then set up a military court to try him.
This seems set to happen before his wife's petition is reconsidered, our correspondent says.
Gen Fonseka would like to contest legislative elections set for early April and may now have to do this from detention. At the same time, the coalition that supported his presidential candidacy has split up into several blocs.