unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* file-expand-wildcards should use `save-match-data'
@ 2002-03-25 22:27 Michael Ernst
  2002-03-26  9:08 ` Juanma Barranquero
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Ernst @ 2002-03-25 22:27 UTC (permalink / raw)


In Emacs 21.2, file-expand-wildcards can cause problems in code that calls
it, because it changes the match-data.  This patch corrects the problem.


ChangeLog entry:

2002-03-25  Michael Ernst  <mernst@alum.mit.edu>

	* files.el (file-expand-wildcards): Use `save-match-data'.

*** /usr/local/share/emacs/21.2/lisp/files.el	Mon Jan 28 11:49:53 2002
--- -	Mon Mar 25 17:24:13 2002
***************
*** 3417,3451 ****
  The file names returned are normally also relative to the current
  default directory.  However, if FULL is non-nil, they are absolute."
!   (let* ((nondir (file-name-nondirectory pattern))
! 	 (dirpart (file-name-directory pattern))
! 	 ;; A list of all dirs that DIRPART specifies.
! 	 ;; This can be more than one dir
! 	 ;; if DIRPART contains wildcards.
! 	 (dirs (if (and dirpart (string-match "[[*?]" dirpart))
! 		   (mapcar 'file-name-as-directory
! 			   (file-expand-wildcards (directory-file-name dirpart)))
! 		 (list dirpart)))
! 	 contents)
!     (while dirs
!       (when (or (null (car dirs))	; Possible if DIRPART is not wild.
! 		(file-directory-p (directory-file-name (car dirs))))
! 	(let ((this-dir-contents
! 	       ;; Filter out "." and ".."
! 	       (delq nil
! 		     (mapcar #'(lambda (name)
! 				 (unless (string-match "\\`\\.\\.?\\'"
! 						       (file-name-nondirectory name))
! 				   name))
! 			     (directory-files (or (car dirs) ".") full
! 					      (wildcard-to-regexp nondir))))))
! 	  (setq contents
! 		(nconc
! 		 (if (and (car dirs) (not full))
! 		     (mapcar (function (lambda (name) (concat (car dirs) name)))
! 			     this-dir-contents)
! 		   this-dir-contents)
! 		 contents))))
!       (setq dirs (cdr dirs)))
!     contents))
  
  (defun list-directory (dirname &optional verbose)
--- 3417,3452 ----
  The file names returned are normally also relative to the current
  default directory.  However, if FULL is non-nil, they are absolute."
!   (save-match-data
!     (let* ((nondir (file-name-nondirectory pattern))
! 	   (dirpart (file-name-directory pattern))
! 	   ;; A list of all dirs that DIRPART specifies.
! 	   ;; This can be more than one dir
! 	   ;; if DIRPART contains wildcards.
! 	   (dirs (if (and dirpart (string-match "[[*?]" dirpart))
! 		     (mapcar 'file-name-as-directory
! 			     (file-expand-wildcards (directory-file-name dirpart)))
! 		   (list dirpart)))
! 	   contents)
!       (while dirs
! 	(when (or (null (car dirs))	; Possible if DIRPART is not wild.
! 		  (file-directory-p (directory-file-name (car dirs))))
! 	  (let ((this-dir-contents
! 		 ;; Filter out "." and ".."
! 		 (delq nil
! 		       (mapcar #'(lambda (name)
! 				   (unless (string-match "\\`\\.\\.?\\'"
! 							 (file-name-nondirectory name))
! 				     name))
! 			       (directory-files (or (car dirs) ".") full
! 						(wildcard-to-regexp nondir))))))
! 	    (setq contents
! 		  (nconc
! 		   (if (and (car dirs) (not full))
! 		       (mapcar (function (lambda (name) (concat (car dirs) name)))
! 			       this-dir-contents)
! 		     this-dir-contents)
! 		   contents))))
! 	(setq dirs (cdr dirs)))
!       contents)))
  
  (defun list-directory (dirname &optional verbose)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: file-expand-wildcards should use `save-match-data'
  2002-03-25 22:27 file-expand-wildcards should use `save-match-data' Michael Ernst
@ 2002-03-26  9:08 ` Juanma Barranquero
  2002-03-28  4:56   ` Richard Stallman
  0 siblings, 1 reply; 4+ messages in thread
From: Juanma Barranquero @ 2002-03-26  9:08 UTC (permalink / raw)
  Cc: bug-gnu-emacs


On Mon, 25 Mar 2002 17:27:33 -0500, Michael Ernst <mernst@alum.mit.edu> wrote:

> In Emacs 21.2, file-expand-wildcards can cause problems in code that calls
> it, because it changes the match-data.  This patch corrects the problem.

Please, see the thread starting on:

http://mail.gnu.org/pipermail/emacs-devel/2002-February/006028.html

Consensus seems to be that user code shouldn't trust the save-match-data
to be consistent across function invocations, unless they're basic
primitives or "functions that have the spirit of a general-purpose
facility". (FWIW, I don't know if file-expand-wildcards matchs that
definition :)


                                                           /L/e/k/t/u

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: file-expand-wildcards should use `save-match-data'
  2002-03-26  9:08 ` Juanma Barranquero
@ 2002-03-28  4:56   ` Richard Stallman
  2002-03-28 14:47     ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Richard Stallman @ 2002-03-28  4:56 UTC (permalink / raw)
  Cc: mernst, bug-gnu-emacs

file-expand-wildcards is a general-purpose facility,
and regexp matching is not its job, so I think it should
use save-match-data.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: file-expand-wildcards should use `save-match-data'
  2002-03-28  4:56   ` Richard Stallman
@ 2002-03-28 14:47     ` Stefan Monnier
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2002-03-28 14:47 UTC (permalink / raw)


>>>>> "Richard" == Richard Stallman <rms@gnu.org> writes:
> file-expand-wildcards is a general-purpose facility,
> and regexp matching is not its job, so I think it should
> use save-match-data.

I'm not sure what the criterion should be, and I don't really care
if file-expand-wildcards saves match-data or not, but it does
seem that file-expand-wildcards is rarely used (more specifically,
it's used twice in emacs/lisp/* plus one comment in complete.el
saying that it could now be used there) and more importantly that
few code if any would do

    match...file-expand-wildcards...use match-data

So I'd be interested to know Michael Ernst's code that broke because
of file-expand-wildcards not saving match-data.


	Stefan

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-03-28 14:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-03-25 22:27 file-expand-wildcards should use `save-match-data' Michael Ernst
2002-03-26  9:08 ` Juanma Barranquero
2002-03-28  4:56   ` Richard Stallman
2002-03-28 14:47     ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).