Mercurial > emacs
annotate lisp/cedet/cedet-files.el @ 104470:6ccad1511df1
Minor whitespace changes and `require' fixes.
author | Chong Yidong <cyd@stupidchicken.com> |
---|---|
date | Sun, 06 Sep 2009 21:22:05 +0000 |
parents | 33171bfc7147 |
children |
rev | line source |
---|---|
104438
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
1 ;;; cedet-files.el --- Common routines dealing with file names. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
2 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
3 ;;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
4 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com> |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
6 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
7 ;; This file is part of GNU Emacs. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
8 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
9 ;; GNU Emacs is free software: you can redistribute it and/or modify |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
10 ;; it under the terms of the GNU General Public License as published by |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
11 ;; the Free Software Foundation, either version 3 of the License, or |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
12 ;; (at your option) any later version. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
13 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
14 ;; GNU Emacs is distributed in the hope that it will be useful, |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
17 ;; GNU General Public License for more details. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
18 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
19 ;; You should have received a copy of the GNU General Public License |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
21 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
22 ;;; Commentary: |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
23 ;; |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
24 ;; Various useful routines for dealing with file names in the tools |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
25 ;; which are a part of CEDET. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
26 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
27 ;;; Code: |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
28 (defvar cedet-dir-sep-char (if (boundp 'directory-sep-char) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
29 (symbol-value 'directory-sep-char) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
30 ?/) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
31 "Character used for directory separation. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
32 Obsoleted in some versions of Emacs. Needed in others.") |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
33 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
34 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
35 (defun cedet-directory-name-to-file-name (referencedir &optional testmode) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
36 "Convert the REFERENCEDIR (a full path name) into a filename. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
37 Converts directory seperation characters into ! characters. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
38 Optional argument TESTMODE is used by tests to avoid conversion |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
39 to the file's truename, and dodging platform tricks." |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
40 (let ((file referencedir) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
41 dir-sep-string) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
42 ;; Expand to full file name |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
43 (when (not testmode) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
44 (setq file (file-truename file))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
45 ;; If FILE is a directory, then force it to end in /. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
46 (when (file-directory-p file) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
47 (setq file (file-name-as-directory file))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
48 ;; Handle Windows Special cases |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
49 (when (or (memq system-type '(windows-nt ms-dos)) testmode) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
50 ;; Replace any invalid file-name characters (for the |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
51 ;; case of backing up remote files). |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
52 (when (not testmode) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
53 (setq file (expand-file-name (convert-standard-filename file)))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
54 (setq dir-sep-string (char-to-string cedet-dir-sep-char)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
55 ;; Normalize DOSish file names: convert all slashes to |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
56 ;; directory-sep-char, downcase the drive letter, if any, |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
57 ;; and replace the leading "x:" with "/drive_x". |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
58 (if (eq (aref file 1) ?:) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
59 (setq file (concat dir-sep-string |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
60 "drive_" |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
61 (char-to-string (downcase (aref file 0))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
62 (if (eq (aref file 2) cedet-dir-sep-char) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
63 "" |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
64 dir-sep-string) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
65 (substring file 2))))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
66 ;; Make the name unique by substituting directory |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
67 ;; separators. It may not really be worth bothering about |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
68 ;; doubling `!'s in the original name... |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
69 (setq file (subst-char-in-string |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
70 cedet-dir-sep-char ?! |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
71 (replace-regexp-in-string "!" "!!" file))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
72 file)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
73 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
74 (defun cedet-file-name-to-directory-name (referencefile &optional testmode) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
75 "Reverse the process of `cedet-directory-name-to-file-name'. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
76 Convert REFERENCEFILE to a directory name replacing ! with /. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
77 Optional TESTMODE is used in tests to avoid doing some platform |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
78 specific conversions during tests." |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
79 (let ((file referencefile)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
80 ;; Replace the ! with / |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
81 (setq file (subst-char-in-string ?! ?/ file)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
82 ;; Occurances of // meant there was once a single !. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
83 (setq file (replace-regexp-in-string "//" "!" file)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
84 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
85 ;; Handle Windows special cases |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
86 (when (or (memq system-type '(windows-nt ms-dos)) testmode) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
87 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
88 ;; Handle drive letters from DOSish file names. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
89 (when (string-match "^/drive_\\([a-z]\\)/" file) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
90 (let ((driveletter (match-string 1 file)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
91 ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
92 (setq file (concat driveletter ":" |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
93 (substring file (match-end 1)))))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
94 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
95 ;; Handle the \\file\name nomenclature on some windows boxes. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
96 (when (string-match "^!" file) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
97 (setq file (concat "//" (substring file 1)))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
98 ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
99 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
100 file)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
101 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
102 ;;; Tests |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
103 ;; |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
104 (defvar cedet-files-utest-list |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
105 '( |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
106 ( "/home/me/src/myproj/src/foo.c" . "!home!me!src!myproj!src!foo.c" ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
107 ( "c:/work/myproj/foo.el" . "!drive_c!work!myproj!foo.el" ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
108 ( "//windows/proj/foo.java" . "!!windows!proj!foo.java" ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
109 ( "/home/me/proj!bang/foo.c" . "!home!me!proj!!bang!foo.c" ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
110 ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
111 "List of different file names to test. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
112 Each entry is a cons cell of ( FNAME . CONVERTED ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
113 where FNAME is some file name, and CONVERTED is what it should be |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
114 converted into.") |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
115 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
116 (defun cedet-files-utest () |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
117 "Test out some file name conversions." |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
118 (interactive) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
119 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
120 (let ((idx 0)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
121 (dolist (FT cedet-files-utest-list) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
122 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
123 (setq idx (+ idx 1)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
124 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
125 (let ((dir->file (cedet-directory-name-to-file-name (car FT) t)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
126 (file->dir (cedet-file-name-to-directory-name (cdr FT) t)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
127 ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
128 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
129 (unless (string= (cdr FT) dir->file) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
130 (error "Failed: %d. Found: %S Wanted: %S" |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
131 idx dir->file (cdr FT)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
132 ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
133 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
134 (unless (string= file->dir (car FT)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
135 (error "Failed: %d. Found: %S Wanted: %S" |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
136 idx file->dir (car FT)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
137 ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
138 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
139 )))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
140 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
141 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
142 ;;; Compatibility |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
143 ;; |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
144 ;; replace-regexp-in-string is in subr.el in Emacs 21. Provide |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
145 ;; here for compatibility. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
146 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
147 (when (not (fboundp 'replace-regexp-in-string)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
148 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
149 (defun replace-regexp-in-string (regexp rep string &optional |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
150 fixedcase literal subexp start) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
151 "Replace all matches for REGEXP with REP in STRING. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
152 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
153 Return a new string containing the replacements. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
154 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
155 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
156 arguments with the same names of function `replace-match'. If START |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
157 is non-nil, start replacements at that index in STRING. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
158 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
159 REP is either a string used as the NEWTEXT arg of `replace-match' or a |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
160 function. If it is a function it is applied to each match to generate |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
161 the replacement passed to `replace-match'; the match-data at this |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
162 point are such that match 0 is the function's argument. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
163 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
164 To replace only the first match (if any), make REGEXP match up to \\' |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
165 and replace a sub-expression, e.g. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
166 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
167 => \" bar foo\"" |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
168 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
169 ;; To avoid excessive consing from multiple matches in long strings, |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
170 ;; don't just call `replace-match' continually. Walk down the |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
171 ;; string looking for matches of REGEXP and building up a (reversed) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
172 ;; list MATCHES. This comprises segments of STRING which weren't |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
173 ;; matched interspersed with replacements for segments that were. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
174 ;; [For a `large' number of replacements it's more efficient to |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
175 ;; operate in a temporary buffer; we can't tell from the function's |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
176 ;; args whether to choose the buffer-based implementation, though it |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
177 ;; might be reasonable to do so for long enough STRING.] |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
178 (let ((l (length string)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
179 (start (or start 0)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
180 matches str mb me) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
181 (save-match-data |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
182 (while (and (< start l) (string-match regexp string start)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
183 (setq mb (match-beginning 0) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
184 me (match-end 0)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
185 ;; If we matched the empty string, make sure we advance by one char |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
186 (when (= me mb) (setq me (min l (1+ mb)))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
187 ;; Generate a replacement for the matched substring. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
188 ;; Operate only on the substring to minimize string consing. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
189 ;; Set up match data for the substring for replacement; |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
190 ;; presumably this is likely to be faster than munging the |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
191 ;; match data directly in Lisp. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
192 (string-match regexp (setq str (substring string mb me))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
193 (setq matches |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
194 (cons (replace-match (if (stringp rep) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
195 rep |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
196 (funcall rep (match-string 0 str))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
197 fixedcase literal str subexp) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
198 (cons (substring string start mb) ; unmatched prefix |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
199 matches))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
200 (setq start me)) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
201 ;; Reconstruct a string from the pieces. |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
202 (setq matches (cons (substring string start l) matches)) ; leftover |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
203 (apply #'concat (nreverse matches))))) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
204 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
205 ) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
206 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
207 (provide 'cedet-files) |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
208 |
33171bfc7147
cedet/cedet-files.el: New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff
changeset
|
209 ;;; cedet-files.el ends here |