Shulchan Arukh, Orach Chayim Siman 400
Can copy this into an AI, or connect the whole library to your assistant. For deeper learning, you can directly use the original seforim, sites, and apps.
מי שישב לו בדרך לנוח ולא ידע אם הוא בתחום אם לאו. ובו סעיף אחד
מי שהיה בא בדרך ליכנס לעיר וישב בדרך לנוח וחשכה לו ולא ידע שהוא בתחום העיר ואח"כ מצא עצמו בתחומה קנה שביתה בעיר ונכנס לתוכה בשבת ומהלך את כולה וחוצה לה אלפים אמה לכל רוח אבל אם לא היה דעתו ליכנס למדינה זו קנה שביתה במקומו ומשם יש לו אלפים אמה לכל רוח ואם כלים אלפים אמה ממקום שביתתו בחצי העיר אינו מהלך בעיר יותר מחציה: הגה והוא הדין אם היה דעתו ליכנס לעיר ואמר שביתתי במקומי נמי דינא הכי (המגיד פרק כ"ז):
Mishna Berura Yomi
- A Traveler Who Sat Down to Rest Without Knowing Whether or Not He is Within the Shabbos Limits: One who was on his way to enter a city and sat down on the way to rest, and darkness fell upon him and he did not realize that he was within the city limits, and later found himself within its limits, acquires a residence in the city and may traverse its entire length and breadth on Shabbat and proceed another 2000 cubits beyond the town in all directions. However, if he had absolutely no intention to enter this town and acquired a residence there instead, {i.e. the place in which he was located at the onset of Shabbos is established as his Shabbos place.} he may traverse a distance of two thousand cubits in any direction from his residence. If the 2000 [cubits] from his Shabbos place conclude in the middle of the town, he may not walk in the city beyond that point. Rema: The same law applies if he had intended to enter the city and said, "My residence is in my place," as well. (The Magid, Chapter 27)