emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: find-file-noselect needs save-match-data


From: Herbert Euler
Subject: Re: find-file-noselect needs save-match-data
Date: Sun, 17 Jun 2007 11:19:50 +0800

> This function is good because it will enable programmers to DTRT in a
    > simpler way in many cases.

I fail to see the simplicity. All the difficulty comes from having to think
    about when to be careful about the match-data.

Yes, but if people get in the habit of using a new function
whenever they don't actually want to use the match data,
which I expect is most of the time, that need to think carefully
will be much less frequent.

No, they would think carefully as frequently as now.  I mean, when
they would think carefully now they would think carefully after adding
such a function; when they don't need to think carefully now they
wouldn't think carefully after adding such a function.  Please take a
look at the following case, in `c-electric-brace':

     ;; cc-cmds.el, line 749
     ;; `}': compact to a one-liner defun?
     (save-match-data
        (when
            (and (eq last-command-char ?\})
                 (memq 'one-liner-defun c-cleanup-list)
                 (c-intersect-lists '(defun-close) syntax)
                 (c-try-one-liner))
          (setq here (- (point-max) pos))))

`save-match-data' is used here, but nothing implies directly that
match-data will be changed in its body.  Some functions change it, but
is called _indirectly_.  One would need to check the source of each
function in its body to see where the match-data is changed.  This is
the only case that the problems caused by unexpected match-data
changes happen.  If one writes the following code:

     ;; dired.el, line 2207, in function `dired-build-subdir-alist'
     (while (re-search-forward dired-subdir-regexp nil t)
        ;; Avoid taking a file name ending in a colon
        ;; as a subdir name.
        (unless (save-excursion
                  (goto-char (match-beginning 0))
                  (beginning-of-line)
                  (forward-char 2)
                  (save-match-data (looking-at dired-re-perms)))

one will know that `looking-at' changes match-data, and so uses
`save-match-data' properly to avoid the problem.

And if match-data needs to be preserved, the programmer would pay
attention to it, no matter whether there are some functions that won't
change match-data exist.  When they think about which function to use,
they've already thought about whether the match-data should be
preserved.

Regards,
Guanpeng Xu

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! http://search.msn.com/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]