The national mind-set that condones this sort of extremism was cultivated and encouraged under the military dictatorships of Gen. Mohammad Zia ul-Haq from 1977 to 1988 and Gen. Pervez Musharraf from 1999 to 2008. A whole generation of Pakistanis has grown up with textbooks that conflate Pakistani nationalism with Islamist exclusivism.

Anti-Western sentiment and a sense of collective victimhood were cultivated as a substitute for serious debate on social or economic policy. Militant groups were given free rein, originally with American support, to resist the Soviet occupation of Afghanistan, and later became an instrument of Pakistani regional influence there and in Indian-occupied Kashmir.

Pakistan’s return to democracy, after the elections of 2008, offered hope. But the elected government has since been hobbled by domestic political infighting and judicial activism on every issue except extremism and terrorism.

Before Mr. Musharraf was ousted, a populist lawyers’ movement successfully challenged his firing of Supreme Court justices. The lawyers’ willingness to confront Mr. Musharraf in his last days raised hopes of a new era. But over the last four years, the Court has spent most of its energy trying to dislodge the government by insisting on reopening cases of alleged corruption from the 1990s. During the same period, no significant terrorist leader has been convicted, and many have been set free by judges who overtly sympathize with their ideology.

This has happened because the lawyers’ movement split into two factions after Mr. Musharraf’s fall: those emphasizing the rule of law and those seeking to use the judiciary as a rival to elected leaders.

Asma Jahangir, who helped lead the lawyers’ movement, has become a critic of the courts, accusing them of overstepping their constitutional mandate and falling under the influence of the security establishment. And Aitzaz Ahsan, who represented the Supreme Court’s chief justice during the lawyers’ showdown with Mr. Musharraf, is now Prime Minister Gilani’s lawyer in the contempt-of-court case — a clear indication of the political realignment that has taken place.

Meanwhile, Pakistan’s raucous media, whose hard-won freedom is crucial for the success of democracy, has done little to help generate support for eliminating extremism and fighting terrorism. The Supreme Court, conservative opposition parties and the news media insist that confronting alleged incompetence and corruption in the current government is more important than turning Pakistan away from Islamist radicalism.