unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* find-file-noselect needs save-match-data
@ 2007-06-04 15:17 Herbert Euler
  2007-06-04 16:05 ` martin rudalics
                   ` (3 more replies)
  0 siblings, 4 replies; 117+ messages in thread
From: Herbert Euler @ 2007-06-04 15:17 UTC (permalink / raw)
  To: emacs-devel; +Cc: herberteuler

If `find-file-noselect' is without `save-match-data', there will be a
bug when `type-break-mode' is turned on.  To reproduce the bug, start
emacs with the -Q option and execute the following expressions:

(type-break-mode 1)
(setq s "This is a sentence")
(string-match "sen" s)
(substring s (match-beginning 0) (match-end 0))

The following patch helps understanding where is wrong:

*** files.el.~1.666.2.82.~      2007-06-04 23:06:41.000000000 -0400
--- files.el    2007-06-04 23:07:29.000000000 -0400
***************
*** 1422,1427 ****
--- 1422,1428 ----
  and visit all the matching files.  When wildcards are actually
  used and expanded, return a list of buffers that are visiting
  the various files."
+   (save-match-data
    (setq filename
        (abbreviate-file-name
         (expand-file-name filename)))
***************
*** 1593,1599 ****
          (setq buf (create-file-buffer filename))
          ;; find-file-noselect-1 may use a different buffer.
          (find-file-noselect-1 buf filename nowarn
!                               rawfile truename number))))))

  (defun find-file-noselect-1 (buf filename nowarn rawfile truename number)
    (let (error)
--- 1594,1600 ----
          (setq buf (create-file-buffer filename))
          ;; find-file-noselect-1 may use a different buffer.
          (find-file-noselect-1 buf filename nowarn
!                               rawfile truename number)))))))

  (defun find-file-noselect-1 (buf filename nowarn rawfile truename number)
    (let (error)

To make the patch short, many lines that should be re-indented are
left as they were.

Regards,
Guanpeng Xu

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

^ permalink raw reply	[flat|nested] 117+ messages in thread
* Re: find-file-noselect needs save-match-data
@ 2007-06-06  0:25 Herbert Euler
  0 siblings, 0 replies; 117+ messages in thread
From: Herbert Euler @ 2007-06-06  0:25 UTC (permalink / raw)
  To: rudalics; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1562 bytes --]

> > I just got a possibly wrong idea today morning.  The error happens in
> > the case that some command, which changes the match data, is added
> > into a hook.  When the hook is executed, the match data is also
> > changed.  Now, if we could assume that _hooks shall not change the
> > match data_, we can save and restore the match data automatically when
> > we run hooks.  I am not sure about this assumption, though.
>
>`save-match-data' is not cheap.  I suppose it's not advisable to
>automatically wrap every hook into it.
>
> > Adding `save-match-data' to functions one by one works, but it is
> > difficult to check all of the functions, and it is not the overall
> > solution.
>
>I think that `find-file-noselect' and `file-truename' should not change
>match-data regardless of whether they run in a hook or not (though a
>number of hooks should probably not run `find-file-noselect' in the
>first place).

Ok.  So I wrote a script to find out all such defuns.  I found many
defuns are possible needing `save-match-data' with it.  The script is
attached, and to start finding, first load the file and then execute
`list-possible-wrong-match-defuns-in-dir'.  An exception in the
executing is for some files a question about whether to apply local
variables list.

Should we check all these defuns and add `save-match-data' to some of
them?

Regards,
Guanpeng Xu

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar - get it now! 
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/

[-- Attachment #2: ss.el --]
[-- Type: text/plain, Size: 3665 bytes --]

(defvar change-match-data-calling
  (regexp-opt '("(search-forward "
		"(search-backward "
		"(word-search-forward "
		"(word-search-backward "
		"(re-search-forward "
		"(re-search-backward "
		"(string-match "
		"(looking-at "
		"(looking-back "
		"(search-spaces-regexp "
		"(posix-search-forward "
		"(posix-search-backward "
		"(posix-looking-at "
		"(posix-string-match "
		"(set-match-data ")))

(defvar defun-form
  (regexp-opt '("(defun "
		"(define-minor-mode "
		"(define-derived-mode ")))

(defun find-possible-wrong-match-defuns-in-buffer (buffer)
  (let (possible-wrong-functions)
    (catch 'skip-this-file
      (with-current-buffer buffer
	(goto-char (point-min))
	;; Find a calling.
	(while (re-search-forward change-match-data-calling nil t)
	  (let ((valid t)
		func-name)
	    (catch 'not-a-defun
	      (condition-case nil
		  (progn
		    (let ((last-pos (point)))
		      (mark-defun)
		      (unless (looking-at (concat "\n" defun-form))
			(goto-char last-pos)
			(throw 'not-a-defun t)))
		    (setq func-name (save-excursion
				      (down-list)
				      (forward-sexp)
				      (skip-syntax-forward " ")
				      (buffer-substring-no-properties (point)
								      (save-excursion
									(forward-sexp)
									(point)))))
		    (save-restriction
		      (narrow-to-region (region-beginning) (region-end))
		      ;; Check each calling.
		      (while (and valid (re-search-forward change-match-data-calling nil 
t))
			(save-excursion
			  (backward-up-list)
			  (catch 'found-save-match-data
			    (condition-case nil
				(while t
				  (backward-up-list)
				  (if (looking-at "(save-match-data")
				      (throw 'found-save-match-data t)))
			      (error (setq valid nil))))))
		      (unless valid
			(setq possible-wrong-functions (cons func-name
							     possible-wrong-functions))
			(while (re-search-forward change-match-data-calling nil t)
			  nil))))
		(error (throw 'skip-this-file t))))))))
    possible-wrong-functions))

(defun find-possible-wrong-match-defuns-in-file (file)
  (when (file-exists-p file)
    (let* ((buffer (find-file-noselect file))
	   (possible-wrong-functions (find-possible-wrong-match-defuns-in-buffer 
buffer)))
      (kill-buffer buffer)
      (cons file possible-wrong-functions))))

(defun find-possible-wrong-match-defuns-in-dir (dir)
  (when (file-exists-p dir)
    (let (functions-list)
      (dolist (file (directory-files dir))
	(when (not (or (string= file ".")
		       (string= file "..")
		       (string-match "elc$" file)))
	  (let* ((fullname (concat dir (if (not (string-match "/$" dir))
					   "/"
					 "") file))
		 (isdir (car (file-attributes fullname)))
		 functions
		 sub-functions-list)
	    (if isdir
		(progn (setq sub-functions-list
			     (find-possible-wrong-match-defuns-in-dir fullname))
		       (when sub-functions-list
			 (setq functions-list (append sub-functions-list functions-list))))
	      (setq functions (find-possible-wrong-match-defuns-in-file fullname))
	      (if (> (length functions) 1)
		  (setq functions-list (cons functions functions-list)))))))
      functions-list)))

(defun list-possible-wrong-match-defuns-in-dir (dir)
  (interactive "DDirectory: ")
  (let ((functions-list (find-possible-wrong-match-defuns-in-dir dir))
	(buffer (get-buffer-create "*possible wrong match defuns*")))
    (with-current-buffer buffer
      (dolist (functions functions-list)
	(insert "In " (car functions) ":\n")
	(dolist (function (cdr functions))
	  (insert "\t" function "\n"))
	(insert "\n")))
    (delete-other-windows)
    (split-window-vertically)
    (other-window 1)
    (switch-to-buffer buffer)
    (goto-char (point-min))))

[-- Attachment #3: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Emacs-devel mailing list
Emacs-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-devel

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

end of thread, other threads:[~2007-07-08  6:30 UTC | newest]

Thread overview: 117+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-04 15:17 find-file-noselect needs save-match-data Herbert Euler
2007-06-04 16:05 ` martin rudalics
2007-06-05  3:10   ` Herbert Euler
2007-06-05  5:52     ` martin rudalics
2007-06-05 22:32       ` Richard Stallman
2007-06-06  1:13   ` Stefan Monnier
2007-06-06  1:44     ` Herbert Euler
2007-06-06  6:58       ` martin rudalics
2007-06-06  7:25         ` David Kastrup
2007-06-06 22:09         ` Richard Stallman
2007-06-06 12:14       ` Stefan Monnier
2007-06-06 22:09         ` Richard Stallman
2007-06-06  6:57     ` martin rudalics
2007-06-06 12:17       ` Stefan Monnier
2007-06-06 12:33         ` Lennart Borgman (gmail)
2007-06-06 12:49         ` martin rudalics
2007-06-06 22:10         ` Richard Stallman
2007-06-06 22:29           ` Stefan Monnier
2007-06-08  7:11             ` Richard Stallman
2007-06-08  8:31               ` Kim F. Storm
2007-06-08  9:42                 ` martin rudalics
2007-06-08 10:31                   ` Kim F. Storm
2007-06-09  9:45                     ` Richard Stallman
2007-06-10  0:05                   ` Herbert Euler
2007-06-10  0:11                     ` Herbert Euler
2007-06-09  9:46                 ` Richard Stallman
2007-06-09 21:32                   ` Juri Linkov
2007-06-09 21:59                     ` David House
2007-06-09 22:43                       ` Juri Linkov
2007-06-09 22:53                       ` Jason Rumney
2007-06-09 23:59                       ` Miles Bader
2007-06-10  0:02                         ` Drew Adams
2007-06-10 13:19                     ` Richard Stallman
2007-06-12  3:05                       ` Bob Rogers
2007-06-12  4:06                         ` Miles Bader
2007-06-12 16:45                           ` Juri Linkov
2007-06-12 17:46                             ` Andreas Schwab
2007-06-12 21:55                               ` David Kastrup
2007-06-12 22:10                                 ` Miles Bader
2007-06-12 22:37                                   ` David Kastrup
2007-06-13 16:22                                     ` Richard Stallman
2007-06-13 17:31                                       ` Stefan Monnier
2007-06-13 18:33                                         ` David Kastrup
2007-06-13 19:55                                           ` Stefan Monnier
2007-06-14  6:37                                             ` Herbert Euler
2007-06-15  8:48                                               ` Richard Stallman
2007-06-15 14:22                                                 ` Stefan Monnier
2007-06-15 16:03                                                 ` Herbert Euler
2007-06-14  6:57                                             ` martin rudalics
2007-06-14 14:36                                               ` Stefan Monnier
2007-06-14 16:05                                                 ` Herbert Euler
2007-06-14 16:22                                                   ` Stefan Monnier
2007-06-15  1:59                                                     ` Herbert Euler
2007-06-15  8:49                                                     ` Richard Stallman
2007-06-15  8:49                                                   ` Richard Stallman
2007-06-14  6:57                                         ` martin rudalics
2007-06-15  8:48                                           ` Richard Stallman
2007-06-15 14:23                                             ` Stefan Monnier
2007-06-15 22:45                                               ` Richard Stallman
2007-06-17 19:54                                           ` Juri Linkov
2007-06-17 20:27                                             ` Lennart Borgman (gmail)
2007-06-14 16:19                                         ` Richard Stallman
2007-06-14 17:18                                           ` David Kastrup
2007-06-15 19:21                                             ` Richard Stallman
2007-06-15 19:52                                               ` Stefan Monnier
2007-06-13 23:39                                       ` Miles Bader
2007-06-14  0:52                                         ` Stefan Monnier
2007-06-14  6:58                                           ` martin rudalics
2007-06-14 16:20                                         ` Richard Stallman
2007-06-14 18:50                                           ` Stefan Monnier
2007-06-15  6:31                                             ` martin rudalics
2007-06-15 14:20                                               ` Stefan Monnier
2007-06-15 15:55                                                 ` Herbert Euler
2007-06-15 19:22                                               ` Richard Stallman
2007-06-16 10:01                                                 ` martin rudalics
2007-06-16 22:35                                                   ` Richard Stallman
2007-06-15 19:21                                             ` Richard Stallman
2007-06-15 19:55                                               ` Stefan Monnier
2007-06-15 20:03                                                 ` David Kastrup
2007-06-16 18:51                                                   ` Richard Stallman
2007-06-16 19:10                                                     ` David Kastrup
2007-06-17 21:49                                                       ` Richard Stallman
2007-06-18  5:16                                                         ` David Kastrup
2007-06-18 21:30                                                           ` Richard Stallman
2007-06-18 21:42                                                             ` David Kastrup
2007-06-19 22:26                                                               ` Richard Stallman
2007-06-19 22:42                                                                 ` David Kastrup
2007-06-25 13:19                                                                   ` Richard Stallman
2007-06-16  3:50                                                 ` Herbert Euler
2007-06-17 12:28                                                   ` Stefan Monnier
2007-06-16 18:51                                                 ` Richard Stallman
2007-06-16 21:39                                                   ` Miles Bader
2007-06-17  8:55                                                     ` Richard Stallman
2007-06-17  3:19                                                   ` Herbert Euler
2007-06-17  3:26                                                     ` Herbert Euler
2007-06-17 21:49                                                     ` Richard Stallman
2007-06-17 12:50                                                   ` Stefan Monnier
2007-06-18  7:25                                                     ` Richard Stallman
2007-06-20  8:07                                                       ` Herbert Euler
2007-06-20 14:12                                                         ` Juri Linkov
2007-06-20 14:44                                                           ` Herbert Euler
2007-06-21  1:07                                                           ` Richard Stallman
2007-06-21  1:29                                                             ` Herbert Euler
2007-06-21  6:31                                                               ` David Kastrup
2007-06-28 14:06                                                                 ` Herbert Euler
2007-06-29 15:07                                                                   ` Herbert Euler
2007-07-06 17:05                                                                 ` byte-compiling an elisp-file fails in Emacs 22.1 (reproducable) klaus.berndl
2007-07-06 22:20                                                                   ` Jason Rumney
2007-07-07 13:06                                                                   ` Richard Stallman
2007-07-08  6:30                                                                     ` AW: " klaus.berndl
2007-06-22  1:51                                                               ` find-file-noselect needs save-match-data Richard Stallman
2007-06-20 17:36                                                         ` Richard Stallman
2007-06-13 16:21                                 ` Richard Stallman
2007-06-05  5:18 ` Richard Stallman
2007-06-06  1:10 ` Stefan Monnier
2007-06-14 16:19 ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2007-06-06  0:25 Herbert Euler

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).