From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: Is it safe to modify a property list directly with PLIST-PUT? Date: Sun, 26 Jul 2009 22:07:36 +0200 Organization: Informatimago Message-ID: <87zlargqd3.fsf@galatea.local> References: <87ab2rs8kv.fsf@iki.fi> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1248640856 5711 80.91.229.12 (26 Jul 2009 20:40:56 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 26 Jul 2009 20:40:56 +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 Jul 26 22:40:49 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MVAWW-0007gF-NX for geh-help-gnu-emacs@m.gmane.org; Sun, 26 Jul 2009 22:40:49 +0200 Original-Received: from localhost ([127.0.0.1]:51112 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MVAWW-0004VI-2G for geh-help-gnu-emacs@m.gmane.org; Sun, 26 Jul 2009 16:40:48 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 50 Original-X-Trace: individual.net d0KS7am3ZA7YaysCH38CRgDVZmKUpeQ6WTEcUapkO4MyNeMK97 Cancel-Lock: sha1:MzkzOGI2YmM4ZmVjMzA5YmZlYWIyZDE5ZDUyNWVlZGNjYmUxY2UyMg== sha1:91nedpM5GdDCdMJjC9U7jguioZo= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin) Original-Xref: news.stanford.edu gnu.emacs.help:171219 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:66405 Archived-At: Teemu Likonen writes: > I use a list of property lists to store data. It's like this: > > (setq my-data '((:foo "one" :bar "two") > (:foo "three" :bar "four"))) It is never safe to modify literal data! (setq my-data (list (list :foo "one" :bar "two") (list :foo "three" :bar "four"))) or: (setq my-data (copy-tree '((:foo "one" :bar "two") (:foo "three" :bar "four")))) > Sometimes I need to modify the data and a command like this seems to > work: > > (plist-put (nth 1 my-data) :bar "New value") > > That is, PLIST-PUT modifies the property list and variable MY-DATA > contains now the modified list: > > ((:foo "one" :bar "two") > (:foo "three" :bar "New value")) > > The question: Is this reliable? Is it guaranteed that it will always > modify the list correctly? If not, how would you suggest doing it > instead? It is safe, as long as the property list is not literal data (that must be considered immutable). Notice also that like delete, plist-put returns the result, it cannot always modify the property list in place. So you have to restore the result: (setf (nth 1 my-data) (plist-put (nth 1 my-data) :bar "New value")) > Common Lisp has so nice SETF macro... Of course, emacs has it too: (require 'cl) ; put that in your ~/.emacs -- __Pascal Bourguignon__