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: martin rudalics
Subject: Re: find-file-noselect needs save-match-data
Date: Fri, 15 Jun 2007 08:31:16 +0200
User-agent: Mozilla Thunderbird 1.0 (Windows/20041206)

> Actually, I'd even argue against any such new function,

How about adding another optional argument then?


string-match is a built-in function in `C source code'.
(string-match regexp string &optional start preserve-match-data))

Return index of start of first match for REGEXP in STRING, or nil.
Matching ignores case if `case-fold-search' is non-nil.
If third arg START is non-nil, start search at that index in string.

For index of first char beyond the match, do (match-end 0).
`match-end' and `match-beginning' also give indices of substrings
matched by parenthesis constructs in the pattern.
You can use the function `match-string' to extract the substrings
matched by the parenthesis constructions in REGEXP.

Fourth argument PRESERVE-MATCH-DATA non-nil means do not modify match
data.  This means you cannot use `match-beginning', `match-end', or
`match-string' to extract further details of the current match.
On the other hand, you don't have to use `save-match-data' in order
to preserve data of a previous match.


> on the grounds that
> it's solving a non-problem, i.e. it will just add new functions, new code,
> new complexity for no real benefit.

Consider `abbreviate-file-name'.  It has the following construct:

      (if (and (string-match abbreviated-home-dir filename)
               ;; If the home dir is just /, don't change it.
               (not (and (= (match-end 0) 1)
                         (= (aref filename 0) ?/)))
               ;; MS-DOS root directories can come with a drive letter;
               ;; Novell Netware allows drive letters beyond `Z:'.
               (not (and (or (eq system-type 'ms-dos)
                             (eq system-type 'cygwin)
                             (eq system-type 'windows-nt))
                         (save-match-data
                           (string-match "^[a-zA-`]:/$" filename)))))
          (setq filename
                (concat "~"
                        (match-string 1 filename)
                        (substring filename (match-end 0)))))


How would you eliminate the call to `save-match-data' without
introducing new variables and complexity?  I could simply write:

      (if (and (string-match abbreviated-home-dir filename)
               ;; If the home dir is just /, don't change it.
               (not (and (= (match-end 0) 1)
                         (= (aref filename 0) ?/)))
               ;; MS-DOS root directories can come with a drive letter;
               ;; Novell Netware allows drive letters beyond `Z:'.
               (not (and (or (eq system-type 'ms-dos)
                             (eq system-type 'cygwin)
                             (eq system-type 'windows-nt))
                         (string-match "^[a-zA-`]:/$" filename nil t))))
          (setq filename
                (concat "~"
                        (match-string 1 filename)
                        (substring filename (match-end 0)))))





reply via email to

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