From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: Declaring a local dynamic variable? Date: Sun, 13 Oct 2013 09:37:49 -0400 Message-ID: References: <1319c316-9f24-46e1-b774-b7ea11a84ea2@googlegroups.com> <525A55FA.10601@easy-emacs.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1381671506 795 80.91.229.3 (13 Oct 2013 13:38:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 13 Oct 2013 13:38:26 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Oct 13 15:38:30 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VVLsL-000817-DO for geh-help-gnu-emacs@m.gmane.org; Sun, 13 Oct 2013 15:38:29 +0200 Original-Received: from localhost ([::1]:33183 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VVLsI-0005C4-UI for geh-help-gnu-emacs@m.gmane.org; Sun, 13 Oct 2013 09:38:26 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60636) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VVLs0-0005By-Ta for help-gnu-emacs@gnu.org; Sun, 13 Oct 2013 09:38:16 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VVLrt-0005Ws-KU for help-gnu-emacs@gnu.org; Sun, 13 Oct 2013 09:38:08 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:55108) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VVLrt-0005Wo-Dp for help-gnu-emacs@gnu.org; Sun, 13 Oct 2013 09:38:01 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1VVLrr-0007kI-U5 for help-gnu-emacs@gnu.org; Sun, 13 Oct 2013 15:37:59 +0200 Original-Received: from 76-10-183-132.dsl.teksavvy.com ([76.10.183.132]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 13 Oct 2013 15:37:59 +0200 Original-Received: from monnier by 76-10-183-132.dsl.teksavvy.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 13 Oct 2013 15:37:59 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 22 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 76-10-183-132.dsl.teksavvy.com User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) Cancel-Lock: sha1:mdVT610En/WEjuPe7zpKmFzAC8c= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:93985 Archived-At: >> The above code has the same effect as >> - create global variable x, initialize it to 5 >> - execute bla bla >> - change value of x to 6 > No. Introduces a let-bound x, which is unrelated to global x You're confused. Take a look at how `let' works inside Emacs, for example. It does exactly what Kai suggests, i.e. the equivalent of: - stash value of `x' on a stack (called specpdl in Emacs's C code). - set `x' to the new value - run the let's body - go fetch the old value of `x' on the stack and restore it. The real story is more complicated because the old value stashed on the stack includes the information about whether `x' was already defined or not, and more importantly because variables can be buffer-local, so it needs also to remember which buffer's value was modified and only restore `x' for that buffer. Stefan