annotate lisp/hyperbole/hpath.el @ 90:99da576a67e7 xemacs-20-0

Import from CVS: tag xemacs-20-0
author cvs
date Mon, 13 Aug 2007 09:10:46 +0200
parents 131b0175ea99
children 4be1180a9e89
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;!emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; FILE: hpath.el
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; SUMMARY: Hyperbole support routines for handling UNIX paths.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; USAGE: GNU Emacs Lisp Library
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; KEYWORDS: comm, hypermedia, unix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; AUTHOR: Bob Weiner
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
9 ;; ORG: Brown U.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; ORIG-DATE: 1-Nov-91 at 00:44:23
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
12 ;; LAST-MOD: 10-Oct-95 at 23:31:56 by Bob Weiner
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
13 ;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
14 ;; This file is part of Hyperbole.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
15 ;; Available for use and distribution under the same terms as GNU Emacs.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
16 ;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
17 ;; Copyright (C) 1991-1995, Free Software Foundation, Inc.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
18 ;; Developed with support from Motorola Inc.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
19 ;;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
20 ;; DESCRIPTION:
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
21 ;; DESCRIP-END.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;;; Public variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 (defvar hpath:rfc "/anonymous@ds.internic.net:rfc/rfc%s.txt"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 "*String to be used in the call: (hpath:rfc rfc-num)
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
29 to create an path to the RFC document for 'rfc-num'.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 (defvar hpath:suffixes '(".gz" ".Z")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 "*List of filename suffixes to add or remove within (hpath:exists-p) calls.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 (defvar hpath:tmp-prefix "/tmp/remote-"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 "*Pathname prefix to attach to remote files copied locally for use with external viewers.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;;; Public functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 (defun hpath:absolute-to (path &optional default-dirs)
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
42 "Returns PATH as an absolute path relative to one directory from optional DEFAULT-DIRS or 'default-directory'.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 Returns PATH unchanged when it is not a valid path or when DEFAULT-DIRS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 is invalid. DEFAULT-DIRS when non-nil may be a single directory or a list of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 directories. The first one in which PATH is found is used."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (if (not (and (stringp path) (hpath:is-p path nil t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (if (not (cond ((null default-dirs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (setq default-dirs (cons default-directory nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ((stringp default-dirs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (setq default-dirs (cons default-dirs nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ((listp default-dirs))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (t nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (let ((rtn) dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (while (and default-dirs (null rtn))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (setq dir (expand-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (file-name-as-directory (car default-dirs)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 rtn (expand-file-name path dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 default-dirs (cdr default-dirs))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (or (file-exists-p rtn) (setq rtn nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (or rtn path)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (defun hpath:ange-ftp-at-p ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 "Returns an ange-ftp pathname that point is within or nil.
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
66 See the 'ange-ftp' or 'efs' Elisp packages for pathname format details.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 Always returns nil if (hpath:ange-ftp-available-p) returns nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (if (hpath:ange-ftp-available-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (let ((user (hpath:ange-ftp-default-user))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (setq path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (skip-chars-backward "^[ \t\n\"`'\(\{<")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 ((hpath:url-at-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (if (string-equal
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
77 (buffer-substring (match-beginning 1) (match-end 1))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 "ftp")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 "/"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 ;; user
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
82 (if (match-beginning 2)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (buffer-substring
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
84 (match-beginning 2) (match-end 2))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (concat user "@"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 ;; domain
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (hpath:delete-trailer
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
88 (buffer-substring (match-beginning 3) (match-end 3)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 ":"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 ;; path
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
91 (if (match-beginning 5)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
92 (buffer-substring (match-beginning 5)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
93 (match-end 5))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 ;; else ignore this other type of WWW path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 ;; user, domain and path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ((looking-at "/?[^/:@ \t\n\^M\"`']+@[^/:@ \t\n\^M\"`']+:[^]@ \t\n\^M\"`'\)\}]*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (buffer-substring (match-beginning 0) (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ;; @domain and path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ((looking-at "@[^/:@ \t\n\^M\"`']+:[^]@ \t\n\^M\"`'\)\}]*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (concat "/" user (buffer-substring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (match-beginning 0) (match-end 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 ;; domain and path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 ((and (looking-at
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 "/?\\(\\([^/:@ \t\n\^M\"`']+\\):[^]@:, \t\n\^M\"`'\)\}]*\\)[] \t\n\^M,.\"`'\)\}]")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (setq path (buffer-substring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (match-beginning 1) (match-end 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (string-match "[^.]\\.[^.]"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (buffer-substring (match-beginning 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (match-end 2))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (concat "/" user "@" path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 ;; host and path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 ((and (looking-at
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 "/\\([^/:@ \t\n\^M\"`']+:[^]@:, \t\n\^M\"`'\)\}]*\\)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (setq path (buffer-substring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (match-beginning 1) (match-end 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (concat "/" user "@" path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (hpath:delete-trailer path))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (defun hpath:ange-ftp-p (path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 "Returns non-nil iff PATH is an ange-ftp pathname.
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
123 See the 'ange-ftp' or 'efs' Elisp package for pathname format details.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 Always returns nil if (hpath:ange-ftp-available-p) returns nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (and (stringp path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (or (featurep 'ange-ftp) (featurep 'efs))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (let ((user (hpath:ange-ftp-default-user))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 result)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (setq result
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 ((hpath:url-p path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (if (string-equal
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
133 (substring path (match-beginning 1) (match-end 1))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 "ftp")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 "/"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 ;; user
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
138 (if (match-beginning 2)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
139 (substring path (match-beginning 2) (match-end 2))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (concat user "@"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 ;; domain
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (hpath:delete-trailer
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
143 (substring path (match-beginning 3) (match-end 3)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 ":"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 ;; path
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
146 (if (match-beginning 5)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
147 (substring path (match-beginning 5)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
148 (match-end 5))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 ;; else ignore this other type of WWW path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 ;; user, domain and path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 ((string-match "/?[^/:@ \t\n\^M\"`']+@[^/:@ \t\n\^M\"`']+:[^]@ \t\n\^M\"`'\)\}]*"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (substring path (match-beginning 0) (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 ;; @domain and path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 ((string-match "@[^/:@ \t\n\^M\"`']+:[^]@ \t\n\^M\"`'\)\}]*"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (concat "/" user
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (substring path (match-beginning 0) (match-end 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 ;; domain and path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 ((and (string-match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 "/?\\(\\([^/:@ \t\n\^M\"`']+\\):[^]@:, \t\n\^M\"`'\)\}]*\\)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (setq result (substring path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (match-beginning 1) (match-end 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (string-match "[^.]\\.[^.]"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (substring path (match-beginning 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (match-end 2))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (concat "/" user "@" result))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 ;; host and path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 ((and (string-match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 "/\\([^/:@ \t\n\^M\"`']+:[^]@:, \t\n\^M\"`'\)\}]*\\)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (setq result (substring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (match-beginning 1) (match-end 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (concat "/" user "@" result))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (hpath:delete-trailer result))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 (defun hpath:at-p (&optional type non-exist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 "Returns delimited path or non-delimited ange-ftp path at point, if any.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 World wide web urls are treated as non-paths so they are handled elsewhere.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 Delimiters may be: double quotes, open and close single quote, or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 Texinfo file references.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 If optional TYPE is the symbol 'file or 'directory, then only that path type is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 accepted as a match. Only locally reachable paths are checked for existence.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 With optional NON-EXIST, nonexistent local paths are allowed.
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
189 Absolute pathnames must begin with a '/' or '~'. Relative pathnames
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
190 must begin with a './' or '../' to be recognized."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 (cond (;; Local file URLs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (hpath:is-p (hargs:delimited
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 "file://localhost" "[ \t\n\^M\"\'\}]" nil t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 ((hpath:ange-ftp-at-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 ((hpath:www-at-p) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 ((hpath:is-p (or (hargs:delimited "\"" "\"")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 ;; Filenames in Info docs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (hargs:delimited "\`" "\'")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 ;; Filenames in TexInfo docs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 (hargs:delimited "@file{" "}"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 type non-exist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
203 (defun hpath:find (filename &optional other-window-p)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
204 "Edit file FILENAME using program from hpath:find-alist if available.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
205 Otherwise, switch to a buffer visiting file FILENAME, creating one if none
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
206 already exists.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 FILENAME may start with a special prefix character which is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 handled as follows:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 !filename - execute as a non-windowed program within a shell;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 &filename - execute as a windowed program;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 -filename - load as an Emacs Lisp program.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
214 Return non-nil iff file is displayed within a buffer (not with an external
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 program)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (interactive "FFind file: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (let (modifier)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 (if (string-match hpath:prefix-regexp filename)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (setq modifier (aref filename 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 filename (substring filename (match-end 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (setq filename (hpath:substitute-value filename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (cond (modifier (cond ((eq modifier ?!)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 (hact 'exec-shell-cmd filename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 ((eq modifier ?&)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (hact 'exec-window-cmd filename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 ((eq modifier ?-)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (load filename)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 (t (let ((find-program (hpath:find-program filename)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (cond ((stringp find-program)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (hact 'exec-window-cmd find-program)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 ((hypb:functionp find-program)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (funcall find-program filename)
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
235 nil)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (t (setq filename (hpath:validate filename))
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
237 (funcall (if (and other-window-p
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
238 (not (br-in-browser)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
239 'switch-to-buffer-other-window
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
240 'switch-to-buffer)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
241 (find-file-noselect filename))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 t)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
244 (defun hpath:find-other-window (filename)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
245 "Edit file FILENAME, in another window or using program from hpath:find-alist.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
246 May create a new window, or reuse an existing one; see the function display-buffer.
24
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents: 0
diff changeset
247
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
248 Alternatively, FILENAME may start with a prefix character to indicate special
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
249 handling. See documentation of `hpath:find' for details.
24
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents: 0
diff changeset
250
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
251 Return non-nil iff file is displayed within a buffer."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (interactive "FFind file in other window: ")
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
253 (hpath:find filename t))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (defun hpath:is-p (path &optional type non-exist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 "Returns PATH if PATH is a Unix path, else nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 If optional TYPE is the symbol 'file or 'directory, then only that path type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 is accepted as a match. The existence of the path is checked only for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 locally reachable paths (Info paths are not checked). Single spaces are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 permitted in middle of existing pathnames, but not at the start or end. Tabs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 and newlines are converted to space before the pathname is checked, this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 normalized path form is what is returned for PATH. With optional NON-EXIST,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 nonexistent local paths are allowed."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (let ((rtn-path path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (suffix))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (and (stringp path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 ;; Path may be a link reference with other components other than a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 ;; pathname. These components always follow a comma, so strip them,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 ;; if any, before checking path.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 (if (string-match "[ \t\n\^M]*," path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (setq rtn-path (concat (substring path 0 (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 "%s" (substring path (match-beginning 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 path (substring path 0 (match-beginning 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 (setq rtn-path (concat rtn-path "%s")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 (if (string-match hpath:prefix-regexp path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (setq path (substring path (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 t)
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
278 (not (or (string= path "")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 (string-match "\\`\\s \\|\\s \\'" path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 ;; Convert tabs and newlines to space.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (setq path (hbut:key-to-label (hbut:label-to-key path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (or (not (string-match "[()]" path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 (string-match "\\`([^ \t\n\^M\)]+)[ *A-Za-z0-9]" path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 (if (string-match "\\${[^}]+}" path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 (setq path (hpath:substitute-value path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (not (string-match "[\t\n\^M\"`'{}|\\]" path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 (or (not (hpath:www-p path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 (string-match "^ftp:" path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (let ((remote-path (string-match "@.+:\\|^/.+:\\|.+:/" path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 (if (cond (remote-path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (cond ((eq type 'file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (not (string-equal "/" (substring path -1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 ((eq type 'directory)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (string-equal "/" (substring path -1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 ((or (and non-exist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 ;; Info or ange-ftp path, so don't check for.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (string-match "[()]" path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 (hpath:ange-ftp-p path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 (setq suffix (hpath:exists-p path t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 ;; Don't allow spaces in non-existent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 ;; pathnames.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 (not (string-match " " path))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 (setq suffix (hpath:exists-p path t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 (cond ((eq type 'file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 (not (file-directory-p path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 ((eq type 'directory)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 (file-directory-p path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 (t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 )
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
313 ;; Return path if non-nil return value
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
314 (if (stringp suffix) ;; suffix could = t, which we ignore
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
315 (if (string-match
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
316 (concat (regexp-quote suffix) "%s") rtn-path)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
317 ;; remove suffix
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
318 (concat (substring rtn-path 0 (match-beginning 0))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
319 (substring rtn-path (match-end 0)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
320 ;; add suffix
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
321 (format rtn-path suffix))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
322 (format rtn-path "")))))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (defun hpath:relative-to (path &optional default-dir)
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
325 "Returns PATH relative to optional DEFAULT-DIR or 'default-directory'.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 Returns PATH unchanged when it is not a valid path."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 (if (not (and (stringp path) (hpath:is-p path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (setq default-dir
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 (expand-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 (file-name-as-directory (or default-dir default-directory)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 path (expand-file-name path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (and path default-dir
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 (if (string-equal "/" default-dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 (let ((end-dir (min (length path) (length default-dir))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 ((string-equal (substring path 0 end-dir) default-dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 (concat "./" (substring path end-dir)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 ((progn (setq default-dir (file-name-directory (directory-file-name default-dir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 end-dir (min (length path) (length default-dir)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 (string-equal (substring path 0 end-dir) default-dir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (concat "../" (substring path end-dir)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 ((progn (setq default-dir (file-name-directory (directory-file-name default-dir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 end-dir (min (length path) (length default-dir)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 (string-equal (substring path 0 end-dir) default-dir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 (concat "../../" (substring path end-dir)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 (t path)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 (defun hpath:rfc (rfc-num)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 "Return pathname to textual rfc document indexed by RFC-NUM.
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
352 See the documentation of the 'hpath:rfc' variable."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 (format hpath:rfc rfc-num))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 (defun hpath:substitute-value (path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 "Substitutes matching value for Emacs Lisp variables and environment variables in PATH.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 Returns path with variable values substituted."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 (substitute-in-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 (hypb:replace-match-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 "\\${[^}]+}"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 (lambda (str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 (let* ((var-group (substring path match start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 (var-name (substring path (+ match 2) (1- start)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 (rest-of-path (substring path start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 (sym (intern-soft var-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 (if (file-name-absolute-p rest-of-path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 (setq rest-of-path (substring rest-of-path 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 (if (and sym (boundp sym))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 (directory-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 (hpath:substitute-dir var-name rest-of-path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 var-group))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 (defun hpath:substitute-var (path)
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
377 "Replaces up to one match in PATH with first matching variable from 'hpath:variables'."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 (if (not (and (stringp path) (string-match "/" path) (hpath:is-p path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 (setq path (hpath:symlink-referent path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 (let ((new-path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 (vars hpath:variables)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 result var val)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 (while (and vars (null new-path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 (setq var (car vars) vars (cdr vars))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 (if (boundp var)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 (progn (setq val (symbol-value var))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 (cond ((stringp val)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 (if (setq result
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 (hpath:substitute-var-name var val path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 (setq new-path result)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 ((null val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 ((listp val)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 (while (and val (null new-path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 (if (setq result
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 (hpath:substitute-var-name var (car val) path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 (setq new-path result))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 (setq val (cdr val))))
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
399 (t (error "(hpath:substitute-var): '%s' has invalid value for hpath:variables" var))))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 (or new-path path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 ;; The following function recursively resolves all UNIX links to their
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 ;; final referents.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 ;; Works with Apollo's variant and other strange links like:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 ;; /usr/local -> $(SERVER_LOCAL)/usr/local, /usr/bin ->
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 ;; ../$(SYSTYPE)/usr/bin and /tmp -> `node_data/tmp. It also handles
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 ;; relative links properly as in /usr/local/emacs -> gnu/emacs which must
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
410 ;; be resolved relative to the '/usr/local' directory.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
411 ;; It will fail on Apollos if the '../' notation is used to move just
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
412 ;; above the '/' directory level. This is fairly uncommon and so the
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 ;; problem has not been fixed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 (defun hpath:symlink-referent (linkname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 "Returns expanded file or directory referent of LINKNAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 LINKNAME should not end with a directory delimiter.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 Returns nil if LINKNAME is not a string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 Returns LINKNAME unchanged if it is not a symbolic link but is a pathname."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 (if (stringp linkname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 (or (file-symlink-p linkname) linkname)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 (defun hpath:symlink-expand (referent dirname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 "Returns expanded file or directory REFERENT relative to DIRNAME."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 (let ((var-link)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 (dir dirname))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 (while (string-match "\\$(\\([^\)]*\\))" referent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 (setq var-link (getenv (substring referent (match-beginning 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 (match-end 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 referent (concat (substring referent 0 (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 var-link
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 (substring referent (match-end 0)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 ;; If referent is not an absolute path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 (let ((nd-abbrev (string-match "`node_data" referent)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 (if (and nd-abbrev (= nd-abbrev 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 (setq referent (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 ;; Prepend node name given in dirname, if any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 (and (string-match "^//[^/]+" dirname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 (substring dirname 0 (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 "/sys/" (substring referent 1)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 (while (string-match "\\(^\\|/\\)\\.\\.\\(/\\|$\\)" referent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 ;; Match to "//.." or "/.." at the start of link referent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 (while (string-match "^\\(//\\.\\.\\|/\\.\\.\\)\\(/\\|$\\)" referent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 (setq referent (substring referent (match-end 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 ;; Match to "../" or ".." at the start of link referent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 (while (string-match "^\\.\\.\\(/\\|$\\)" referent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 (setq dir (file-name-directory (directory-file-name dir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 referent (concat dir (substring referent (match-end 0)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 ;; Match to rest of "../" in link referent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 (while (string-match "[^/]+/\\.\\./" referent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 (setq referent (concat (substring referent 0 (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 (substring referent (match-end 0))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 (and (/= (aref referent 0) ?~)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 (/= (aref referent 0) ?/)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 (setq referent (expand-file-name referent dirname))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 referent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 (defun hpath:validate (path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 "Returns PATH if PATH is a valid, readable path, else signals error.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 Info and ange-ftp remote pathnames are considered readable without any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 validation checks.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 Default-directory should be equal to current Hyperbole button source
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 directory when called, so that PATH is expanded relative to it."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 (cond ((not (stringp path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (error "(hpath:validate): \"%s\" is not a pathname." path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 ((or (string-match "[()]" path) (hpath:ange-ftp-p path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 ;; info or ange-ftp path, so don't validate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 ((if (not (hpath:www-p path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 ;; Otherwise, must not be a WWW link ref and must be a readable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 ;; path.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 (let ((return-path (hpath:exists-p path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 (and return-path (file-readable-p return-path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 return-path))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 (t (error "(hpath:validate): \"%s\" is not readable." path))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 (defun hpath:url-at-p ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 "Return world-wide-web universal resource locator (url) that point immediately precedes or nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 Use buffer-substring with match-beginning and match-end on the following
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 groupings:
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
481 1 = access protocol
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
482 2 = optional username
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
483 3 = host and domain to connect to
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
484 4 = optional port number to use
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
485 5 = pathname to access."
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
486 ;; WWW URL format: <protocol>:/[<user>@]<domain>[:<port>]/<path>
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
487 ;; or <protocol>://[<user>@]<domain>[:<port>]<path>
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 ;; Avoid [a-z]:/path patterns since these may be disk paths on OS/2, DOS or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 ;; Windows.
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
490 (if (looking-at "\\([a-zA-Z][a-zA-Z]+\\)://?\\([^@/: \t\n\^M]+@\\)?\\([^/:@ \t\n\^M\"`']+\\)\\(:[0-9]+\\)?\\([/~][^]@ \t\n\^M\"`'\)\}>]*\\)?")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 (save-excursion
36
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 24
diff changeset
492 (goto-char (match-end 0))
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
493 (skip-chars-backward ".?#!*()")
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
494 (buffer-substring (match-beginning 0) (point)))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 (defun hpath:url-p (obj)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 "Return t if OBJ is a world-wide-web universal resource locator (url) string, else nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 Use string-match with match-beginning and match-end on the following groupings:
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
499 1 = access protocol
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
500 2 = optional username
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
501 3 = host and domain to connect to
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
502 4 = optional port number to use
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
503 5 = pathname to access."
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
504 ;; WWW URL format: <protocol>:/[<user>@]<domain>[:<port>]/<path>
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
505 ;; or <protocol>://[<user>@]<domain>[:<port>]<path>
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 ;; Avoid [a-z]:/path patterns since these may be disk paths on OS/2, DOS or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 ;; Windows.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 (and (stringp obj)
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
509 (string-match "\\`<?\\([a-zA-Z][a-zA-Z]+\\)://?\\([^@/: \t\n\^M]+@\\)?\\([^/:@ \t\n\^M\"`']+\\)\\(:[0-9]+\\)?\\([/~][^]@ \t\n\^M\"`'\)\}>]*\\)?>?\\'"
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 obj)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 (defun hpath:www-at-p (&optional include-start-and-end-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 "Returns a world-wide-web link reference that point is within or nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 With optional INCLUDE-START-AND-END-P non-nil, returns list of:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 (link-string begin-position end-position)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 (skip-chars-backward "^[ \t\n\"`'\(\{<")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 (cond ((not include-start-and-end-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 (hpath:url-at-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 ((hpath:url-at-p)
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
522 (list (buffer-substring (match-beginning 0) (match-end 0))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
523 (match-beginning 0)
36
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 24
diff changeset
524 (match-end 0))))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 (defun hpath:www-p (path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 "Returns non-nil iff PATH is a world-wide-web link reference."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 (and (stringp path) (hpath:url-p path) path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 ;;; Private functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 (defun hpath:ange-ftp-available-p ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 "Return t if the ange-ftp or efs package is available, nil otherwise.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 Either the package must have been loaded already or under versions of Emacs
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
537 19, it must be set for autoloading via 'file-name-handler-alist'."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 (or (featurep 'ange-ftp) (featurep 'efs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 (and (boundp 'file-name-handler-alist) ; v19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 (or (rassq 'ange-ftp-hook-function file-name-handler-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 (rassq 'efs-file-handler-function file-name-handler-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 (defun hpath:ange-ftp-default-user ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 "Return default user account for remote file access with ange-ftp or efs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 Returns \"anonymous\" if neither ange-ftp-default-user nor efs-default-user
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 is set."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 (cond ((and (boundp 'ange-ftp-default-user)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 (stringp ange-ftp-default-user))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 ange-ftp-default-user)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 ((and (boundp 'efs-default-user)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 (stringp efs-default-user))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 efs-default-user)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 (t "anonymous")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 (defun hpath:delete-trailer (string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 "Return string minus any trailing .?#!*() characters."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 (save-match-data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 (if (and (stringp string) (> (length string) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 (string-match "[.?#!*()]+\\'" string))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 (substring string 0 (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 string)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 (defun hpath:exists-p (path &optional suffix-flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 "Return PATH if it exists. (This does not mean you can read it.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 If PATH exists with or without a suffix from hpath:suffixes, then that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 pathname is returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 With optional SUFFIX-FLAG and PATH exists, return suffix added or removed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 from path or t."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 (let ((return-path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 (suffix) suffixes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 (if (file-exists-p path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 (setq return-path path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 (setq suffixes hpath:suffixes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 (while suffixes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 (setq suffix (car suffixes))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 (if (string-match (concat (regexp-quote suffix) "\\'") path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 ;; Remove suffix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 (setq return-path (substring path 0 (match-beginning 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 ;; Add suffix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 (setq return-path (concat path suffix)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 (if (file-exists-p return-path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 (setq suffixes nil);; found a match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 (setq suffix nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 suffixes (cdr suffixes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 return-path nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 (if return-path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 (if suffix-flag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 (or suffix t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 return-path))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 (defun hpath:find-program (filename)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 "Return shell or Lisp command to execute to display FILENAME or nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 Return nil if FILENAME is a directory name.
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
598 See also documentation for 'hpath:find-alist' and 'hpath:display-alist'."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 (let ((cmd))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 (cond ((and (stringp filename) (file-directory-p filename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 ((stringp (setq cmd (hpath:match filename hpath:find-alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 (let ((orig-path filename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 ;; If filename is a remote path, we have to copy it to a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 ;; temporary local file and then display that.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 (if (hpath:ange-ftp-p filename)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 (copy-file orig-path
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 (setq filename
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 (concat hpath:tmp-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 (file-name-nondirectory orig-path)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 t t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 ;; Permit %s substitution of filename within program.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 (if (string-match "[^%]%s" cmd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 (format cmd filename)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 (concat cmd " " filename))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 ((null cmd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 (hpath:match filename hpath:display-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 (t cmd))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 (defun hpath:match (filename regexp-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 "If FILENAME matches the car of any element in REGEXP-ALIST, return its cdr.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 REGEXP-ALIST elements must be of the form (<filename-regexp>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 . <command-to-display-file>). <command-to-display-file> may be a string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 representing an external window-system command to run or it may be a Lisp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 function to call with FILENAME as its single argument."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 (let ((cmd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 (while (and (not cmd) regexp-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 (if (string-match (car (setq elt (car regexp-alist))) filename)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 (setq cmd (cdr elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 (setq regexp-alist (cdr regexp-alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 cmd))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 (defun hpath:substitute-dir (var-name rest-of-path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 "Returns a dir for VAR-NAME using REST-OF-PATH to find match or triggers an error when no match.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 VAR-NAME's value may be a directory or a list of directories. If it is a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 list, the first directory prepended to REST-OF-PATH which produces a valid
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 local pathname is returned."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 (let (sym val)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 (cond ((not (stringp var-name))
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 38
diff changeset
642 (error "(hpath:substitute-dir): VAR-NAME arg, '%s', must be a string" var-name))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 ((not (and (setq sym (intern-soft var-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 (boundp sym)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 (error "(hpath:substitute-dir): VAR-NAME arg, \"%s\", is not a bound variable"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 var-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 ((stringp (setq val (symbol-value sym)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 (if (hpath:validate (expand-file-name rest-of-path val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 ((listp val)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 (let ((dir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 (while (and val (not dir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 (setq dir (car val) val (cdr val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 (or (and (stringp dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 (file-name-absolute-p dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 (file-readable-p (expand-file-name rest-of-path dir)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 (setq dir nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 (if dir (hpath:validate (directory-file-name dir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 (error "(hpath:substitute-dir): Can't find match for \"%s\""
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 (concat "${" var-name "}/" rest-of-path))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 (t (error "(hpath:substitute-dir): Value of VAR-NAME, \"%s\", must be a string or list" var-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 (defun hpath:substitute-var-name (var-symbol var-dir-val path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 "Replaces with VAR-SYMBOL any occurrences of VAR-DIR-VAL in PATH.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 Replacement is done iff VAR-DIR-VAL is an absolute path.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 If PATH is modified, returns PATH, otherwise returns nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 (if (and (stringp var-dir-val) (file-name-absolute-p var-dir-val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 (let ((new-path (hypb:replace-match-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 (regexp-quote (file-name-as-directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 (or var-dir-val default-directory)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 path (concat "${" (symbol-name var-symbol) "}/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 (if (equal new-path path) nil new-path))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 ;;; Private variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 (defvar hpath:prefix-regexp "\\`[-!&][ ]*"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 "Regexp matching command characters which may precede a pathname.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 These are used to indicate how to display or execute the pathname.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 - means evaluate it as Emacs Lisp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 ! means execute it as a shell script
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 & means run it under the current window system.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 (provide 'hpath)