home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / hyperbole / hpath.el < prev    next >
Encoding:
Text File  |  1995-08-26  |  24.4 KB  |  645 lines

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