From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: add-to-list with lexical variables Date: Sat, 08 Jun 2013 16:00:22 +0200 Organization: Informatimago Message-ID: <87r4gcln2h.fsf@kuiper.lan.informatimago.com> References: <87txl8u68b.fsf@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1370700445 9988 80.91.229.3 (8 Jun 2013 14:07:25 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 8 Jun 2013 14:07:25 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jun 08 16:07:26 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 1UlJng-0008GV-Rq for geh-help-gnu-emacs@m.gmane.org; Sat, 08 Jun 2013 16:07:25 +0200 Original-Received: from localhost ([::1]:58740 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UlJng-0001lQ-BX for geh-help-gnu-emacs@m.gmane.org; Sat, 08 Jun 2013 10:07:24 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:35242) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UlJnS-0001ix-A4 for help-gnu-emacs@gnu.org; Sat, 08 Jun 2013 10:07:12 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UlJnO-0004uv-Ch for help-gnu-emacs@gnu.org; Sat, 08 Jun 2013 10:07:10 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:46560) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UlJnO-0004ul-2h for help-gnu-emacs@gnu.org; Sat, 08 Jun 2013 10:07:06 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1UlJnK-00082H-Ai for help-gnu-emacs@gnu.org; Sat, 08 Jun 2013 16:07:02 +0200 Original-Received: from amontsouris-651-1-111-59.w83-202.abo.wanadoo.fr ([83.202.226.59]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 08 Jun 2013 16:07:02 +0200 Original-Received: from pjb by amontsouris-651-1-111-59.w83-202.abo.wanadoo.fr with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 08 Jun 2013 16:07:02 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 158 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: amontsouris-651-1-111-59.w83-202.abo.wanadoo.fr Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:N2Q2N2M3ODFjYjliZDA3ZTExODIzZjBkZDcwYTY4ZTBmOTY5OTdjYw== 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:91394 Archived-At: Hongxu Chen writes: > Hi list, > > I am writing a snippet to add element into environment variables, and > it is written as below: > > #+BEGIN_SRC elisp > (defun no-dup-add-env-ele (env env-ele-string) > (let* ((env-separator (if (string-equal system-type "windows-nt") ";" ":")) > (env-list (split-string (getenv env) env-separator))) > (if (string-match-p env-separator env-ele-string) > (dolist (env-ele (split-string env-ele-string env-separator)) > (add-to-list 'env-list env-ele)) > (add-to-list 'env-list env-ele-string)) > (setenv env (mapconcat 'identity env-list ":")))) > #+END_SRC > > 1. when I set `lexical-binding' to t and byte-compile the file, it > would report this error: > > add-to-list cannot use lexical var `env-list' > > 2. And when I using `lexical-let*' instead, there would be an warning: > > Warning: assignment to free variable `env-list' > > 3. However after resetting `lexical-binding' to nil, byte-compiles well. > > So what are the differences? The difference is not. (not t) --> nil (not nil) --> t or: not lexical binding is dynamic binding. not dynamic binding is lexical binding. Now, to add new elements to a list bound to some place, there's the pushnew cl operator (a macro). push can be used to push unconditionnaly, and pop to remove the first element from a list bound to a place. That's how things have to be done with lexical binding. (require 'cl) (defun* pushnew/envvar (env-ele-string env &key (test (function equal))) (let* ((env-separator (if (member system-type '(windows-nt ms-dos)) ";" ":")) (env-list (split-string (or (getenv env) "") env-separator t))) (dolist (env-ele (split-string env-ele-string env-separator)) (pushnew env-ele env-list :test test)) (setenv env (mapconcat 'identity env-list env-separator)))) (defun test/pushnew/envvar () (setenv "TEST" nil) (assert (equal (pushnew/envvar "A:B:C" "TEST") "C:B:A")) (assert (equal (getenv "TEST") "C:B:A")) (assert (equal (pushnew/envvar "D:E:F" "TEST") "F:E:D:C:B:A")) (assert (equal (getenv "TEST") "F:E:D:C:B:A")) (assert (equal (pushnew/envvar "G" "TEST") "G:F:E:D:C:B:A")) (assert (equal (getenv "TEST") "G:F:E:D:C:B:A")) (assert (equal (pushnew/envvar "G:F:E:D:C:B:A" "TEST") "G:F:E:D:C:B:A")) (assert (equal (pushnew/envvar "X:F:Y:D:Z:B" "TEST") "Z:Y:X:G:F:E:D:C:B:A")) (assert (equal (pushnew/envvar "a:b:c:d" "TEST") "d:c:b:a:Z:Y:X:G:F:E:D:C:B:A")) (assert (equal (pushnew/envvar "x:y:z" "TEST" :test (function equalp)) "d:c:b:a:Z:Y:X:G:F:E:D:C:B:A")) :success) (test/pushnew/envvar) --> :success You have to add a test argument, since for some environment variables, and for some values, you may want to do case insensitive, or more complex comparison. For example, if you mount a MS-DOS file system on a case sensitive unix file system, itself mounted a HFS+ case insensitive file system, you will have to compare parts of the path case sensitively, and parts case insensitively: mount /dev/disk1s1 /Volumes/case-sensitive mount /dev/disk2s1 /Volumes/case-sensitive/mnt/ms-dos Now, /VOLUMES/case-sensitive/mnt/ms-dos/DESCENT is the same path as: /Volumes/case-sensitive/mnt/ms-dos/Descent but not the same as: /Volumes/case-sensitive/MNT/ms-dos/descent Perhaps there are both /Volumes/case-sensitive/MNT and: /Volumes/case-sensitive/mnt on the case sensitive file system! so you will have to write: (pushnew/envvar "/Volumes/case-sensitive/mnt/ms-dos/Descent" "PATH" :test (function file-system-sensitive-path-equal-p)) with: (defun file-system-sensitive-path-equal-p (a b) (labels ((compare-path (curdir ac bc) (cond ((null ac) (null bc)) ((null bc) nil) (t (and (funcall (if (case-sensitive-file-system-at-path-p curdir) (function equal) (function equalp)) (car ac) (car ab)) (compare-path (path-append curdir (car ac)) (cdr ac) (cdr bc))))))) (compare-path "/" (split-path (expand-file-name a)) (split-path (expand-file-name b))))) So that if there's already /Volumes/case-sensitive/mnt/ms-dos/DESCENT on PATH, (pushnew/envvar "/VOLUMES/case-sensitive/mnt/ms-dos/Descent" "PATH" :test (function file-system-sensitive-path-equal-p)) won't add it, but: (pushnew/envvar "/Volumes/case-sensitive/mnt/MS-DOS/DESCENT" "PATH" :test (function file-system-sensitive-path-equal-p)) will. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. You can take the lisper out of the lisp job, but you can't take the lisp out of the lisper (; -- antifuchs