KEDUSHAS BEIS HA'KENESES AND BEIS MEDRASH
Gemara
Beraisa: We do not conduct frivolously in Batei Kenesiyos. We do not eat, drink, adorn ourselves, or stroll in them.
We do not enter a Beis ha'Keneses to avoid heat or rain.
We do not eulogize an individual in a Beis ha'Keneses.
We may learn in a Beis ha'Keneses, and give mass eulogies (e.g. of a Chacham).
Rav Asi: Batei Kenesiyos in Bavel are made on condition. Nevertheless, we do not act in them frivolously, i.e. to make calculations;
We leave a corpse overnight in a Beis ha'Keneses in which calculations are made.
Objection: Surely there is no obligation to do so!
It means that (Hash-m will punish for this, and) a Mes Mitzvah will be left there overnight.
26b: Ravina owned the ruins of a Beis ha'Keneses. He asked whether he may plant on it.
Rav Ashi: Buy it from the Tuvei ha'Ir in front of the city residents, then you may plant on it.
Rishonim
The Rif and Rosh (4:7) bring our Gemara. Their text of the Beraisa discusses Batei Kenesiyos and Batei Medrashos.
Rambam (Hilchos Tefilah 11:6,7,9): Lightheadedness, i.e. jesting or idle talk is forbidden in a Beis ha'Keneses or Beis Medrash. We do not eat, drink, adorn ourselves, or stroll in them. We do not enter to avoid heat or rain. We may not make calculations, unless they are for a Mitzvah such as Tzedakah or redeeming captives. We may eulogize only a mass eulogy, such as for a great Chacham of the city, which everyone attends.
Rosh (ibid.): Even though Batei Kenesiyos in Bavel are made on condition, we do not permit frivolity. Lightheadedness, i.e. calculations are explicitly forbidden. One may not enter to avoid the rain, only for the sake of learning. Rather, the condition helps for when they are desolate. Then, one may twine ropes there. Nevertheless, calculations are still forbidden, and one may not seed it until buying it from the Tuvei ha'Ir, for these are disgraceful.
Hagahos Maimoniyos (5): This is unlike Rashi (DH Tanai), who says that the condition allows benefit from them (even while they are in use).
Kesef Mishneh (7): The condition allows benefit from them (even while they are in use), but not lightheadedness, i.e. calculations.
Poskim
Shulchan Aruch (OC 151:1): Lightheadedness, i.e. jesting or idle talk is forbidden in a Beis ha'Keneses or Beis Medrash. We do not eat, drink, adorn ourselves, or stroll in them.
Mishnah Berurah (1): Regarding Batei Kenesiyos and Batei Medrashos it says "Va'Ehi Lahem l'Mikdash Me'at". The sin of lightheadedness causes them to be converted to places for worship of idolatry.
Mishnah Berurah (2): This forbids even Sichas Chulin that is not idle, e.g. for the needs of one's income, which is permitted outside. All the more so idle talk, from which it is always proper to refrain, is forbidden. Forbidden talk, such as Lashon ha'Ra and quarrels, is much more severe when done in a Kodesh place, for it disgraces the Shechinah. One who sins in the king's palace in front of the king is far worse than one who sins in private. Even worse, such talk often draws others in the Beis ha'Keneses to quarrel, shame others and curse. Sometimes this is in front of a Sefer Torah. This is worse than shaming someone in front of a Chacham, for which one is called an Apikorus and has no share in the world to come. Therefore, one who Yir'as Shamayim should be careful to speak only Torah and Tefilah in a Beis ha'Keneses or Beis Medrash.
Kaf ha'Chayim (8): If one speaks Sichas Chulin in a Beis ha'Keneses, it would be better if he did not go. Even Chachamim who learn at the time of Tefilah and make it hard to hear the Tefilah do improperly. Further, others learn from their example not to listen to the Tefilah. The Zohar says that one who speaks Chulin in a Beis ha'Keneses has no share in Hash-m, does not fear Hash-m, and denies Hash-m's presence in the Beis ha'Keneses. The Ari Zal was careful to say only words of Tefilah; he would not even say words of Musar, rebuke or Teshuvah, lest he come to say Chulin words. Chulin words are even more serious on Shabbos, for there is more Shechinah then.
Mishnah Berurah (3): Likewise, we do not do Melachah in a Beis ha'Keneses or Beis Medrash.
Kaf ha'Chayim (2): Perhaps it is permitted to slaughter Kaparos in the Chatzer in front of the Beis ha'Keneses, since it is proper that this be supervised. Certainly this is forbidden in the Ezras Nashim, or in an area where people pray when it is too crowded in the Beis ha'Keneses. If people learn in rooms, there is no Kedushah to the Chatzer to which the rooms open, unless people learn in the Chatzer.
Shulchan Aruch (ibid.): We do not enter to avoid heat or rain. We may not make calculations, unless they are for a Mitzvah such as Tzedakah or redeeming captives. We may eulogize only a mass eulogy, such as for a great Chacham of the city.
Shulchan Aruch (2): Some say that a Beis Medrash has Kedushah only if it is public, like a Beis ha'Keneses, but if one designated a private Beis Medrash in his home for himself it does not have so much Kedushah.
Kaf ha'Chayim (26): No one argues about this. The Shulchan Aruch writes 'some say' whenever bringing a Halachah taught by an individual.
Kaf ha'Chayim (28): One should not be so lenient about its Kedushah, for (nowadays) Hash-m has in His world only the four Amos of Halachah.
Mishnah Berurah (14): If one designated a Beis Medrash for the needs of Rabim it has Kedushas Beis Medrash.
Shulchan Aruch (6): Some forbid to enter with a long knife or a bare head.
Source (Gra DH b'Sachin): It is improper to plaster the Mizbe'ach with an iron trowel because iron was created (with potential) to shorten life, whereas the Mizbe'ach extends life (Midos 36a). The same applies to a Beis ha'Keneses, which lengthens life (Berachos 8a).
Question: The Shulchan Aruch does not forbid a small knife. Regarding Birkas ha'Mazon we cover all knives (for the same reason)!
Answer #1 (Taz): We are stringent regarding Birkas ha'Mazon because it is not difficult to cover all knives. Regarding a Beis ha'Keneses we are stringent only regarding a big knife, which is not needed so much.
Answer #2 (Kaf ha'Chayim 42): A small knife is permitted in a Beis ha'Keneses because it can be covered well. Mekubalim do not have anything iron on them during prayer.
Bi'ur Halachah (91:3 DH v'Yesh): Nowadays, letter of the law it is forbidden for one's head to be bare in any place, even without walking four Amos.
Shulchan Aruch (8): It is proper to clean mud off one's shoes before entering to pray. It is improper to have any dirt on his clothes.
Kaf ha'Chayim (51): Even one who not a Chacham should be careful (then) not to have any dirt on his clothes.