bug-gnu-emacs
[Top][All Lists]
Advanced

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

Re: why doesn't the buffer list abbreviate the home directory as ~?


From: Thien-Thi Nguyen
Subject: Re: why doesn't the buffer list abbreviate the home directory as ~?
Date: 02 Jun 2001 16:50:09 -0400

(add-hook 'electric-buffer-menu-mode-hook
          'buffer-substitute-file-env-vars)

thi

_____________________________________________________
;;; ID: buffer-substitute-file-env-vars.el,v 1.9 2000/06/01 23:39:49 ttn Rel
;;;
;;; Copyright (C) 1998-2000 Thien-Thi Nguyen
;;; This file is part of ttn's personal elisp library, released under GNU
;;; GPL with ABSOLUTELY NO WARRANTY.  See the file COPYING for details.

;;; Description: In current buffer, substitute env vars that look like files.

;;; Commentary:

;; Rewritten for inclusion in Emacs (remove external dependency).

;;; Code:

;;;###autoload
(defun buffer-substitute-file-env-vars ()
  (let (buffer-read-only
        (ms-lose (memq system-type '(ms-dos windows-nt))))
    (save-excursion
      (mapcar (lambda (ev-pair)
                ;; In buffer, replace EV w/ backward mapping.
                (goto-char (point-max))
                (let ((var (car ev-pair))
                      (val (cdr ev-pair)))
                  (while (search-backward val (point-min) t)
                    (replace-match (concat "$" var)))))
              ;; FEV-PAIRS is a sorted canonicallized list, longest first.
              (sort
               (let (fev-pairs)
                 (mapcar (lambda (ev)
                           (let* ((x (string-match "=" ev))
                                  (v (and x (substring ev (1+ x))))
                                  (val (and
                                        v
                                        (not (string= v ""))
                                        (if (eq ?~ (aref v 0))
                                                (concat (getenv "HOME")
                                                        (substring v 1))
                                              v))))
                             (when (and val (or (eq ?/ (aref val 0))
                                                (and ms-lose
                                                     (string-match
                                                      "^[a-zA-Z]:/" val))))
                               (setq fev-pairs
                                     (cons (cons (substring ev 0 x) val)
                                           fev-pairs)))))
                         process-environment)
                 fev-pairs)
               (lambda (a b)
                 (> (length (cdr a)) (length (cdr b))))))
      ;; Do HOME, then HOOD replacements.
      ;; HOOD is short for neighborhood, the parent dir of HOME.
      (goto-char 1)
      (while (re-search-forward "$HOME\\>" (point-max) t)
        (replace-match "~"))
      (let* ((hood (file-name-directory (getenv "HOME"))))
        (unless (string= hood "/")
          (goto-char (point-max))
          (while (search-backward hood (point-min) t)
            (replace-match "~")))))))

;;; buffer-substitute-file-env-vars.el,v1.9 ends here



reply via email to

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