[alt.sources] emacs convenience bug fix

gaynor@topaz.rutgers.edu (Silver) (09/14/87)

Between testing and posting, I thought I would clean the code up a
little.  In doing so, I broke it.  The changes were, I thought,
relatively trivial, and so I did not retest it as thoroughly as I did
originally.  You may have found that it will appear to work properly
for a moment, but...

As the code is fairly short, here it is again - use this stuff instead
of the old.  Comments preceded by ;;;;;;;;;;.

;; Set up mode-line, by making mode-line-buffer-identification local
;; to every buffer.  A find-file-hook abbreviates the buffer-file-name
;; to something a little easier to read.
;;
;;   file name, originally = buffer-file-name
;;   abbreviations = file-name-abbreviation-alist
;;   means of abbreviation = string-replace-regexp-alist
;;   find-file-hook = abbreviate-mode-line-buffer-identification

;;;;;;;;;; Make sure you use mode-line-buffer-identification to
;;;;;;;;;; identify the buffer in your mode-line-format.  This
;;;;;;;;;; variable must be buffer-local (if it is not already).
;;;;;;;;;; Otherwise, the rest of the stuff is up to you - I've
;;;;;;;;;; included my stuff to keep things in perspective.

;; Customize mode-line-format and it's constituents.  Remember,
;; mode-line-buffer-identification must be used to identify the
;; buffer.  mode-line-modified is retained because it is in emacs's
;; own default mode-line-format, and emacs might do some clever
;; tricks with it.
(make-variable-buffer-local 'mode-line-buffer-identification)
;;;;;;;;;; The next line was not present in the original.  I don't
;;;;;;;;;; think it matters a whole lot (it should already be
;;;;;;;;;; buffer-local, I think), but just in case...
(make-variable-buffer-local 'mode-line-modified)
(setq-default mode-line-buffer-identification '("%b"))
(setq-default mode-line-modified '("--%*%*--"))
;;;;;;;;;; I originally had a typo here, an extra `default'.
(setq-default mode-line-format
  '("----Emacs: "
    mode-line-buffer-identification
    mode-line-modified
    "%[("
    mode-name
    minor-mode-alist
    "%n"
    mode-line-process
    ")%]----%p%-"))


;; Replace leading instances of "/u2/gaynor" and embedded instances of
;; "u2/gaynor" with "~".
(defvar file-name-abbreviation-alist
  '(("\\(^/\\|\\)u2/gaynor" . "~"))
"Alist of embedded filename patterns vs corresponding abbreviations.
Each element is of the form (<regexp> . <to-string>) (see
replace-regexp).")

;;;;;;;;;; Let's say that I often played with the files in
;;;;;;;;;; /u2/luser/foobar/bletch.  Then I might want to replace
;;;;;;;;;; leading instances of this path with "bletch" by including
;;;;;;;;;; the association ("^/u2/luser/foobar/bletch" .  "bletch").
;;;;;;;;;;
;;;;;;;;;; Let's say that I wanted to display only the last directory
;;;;;;;;;; in the path.  In this case, I would add the association
;;;;;;;;;; ("^.*/\\([^/]*/\\)" . "\\1").


;;;;;;;;;; The bug.  This baby should have originally be enclosed in a
;;;;;;;;;; save-excursion.  I didn't think it necessary, because I
;;;;;;;;;; killed the temp buffer at the end, and so thought that the
;;;;;;;;;; buffer for local-variable-affecting operations would be the
;;;;;;;;;; default.  Instead, I think that they were taking place
;;;;;;;;;; in this killed buffer, or to the globals.  (??)

(defun string-replace-regexp-alist (s al)
"Given a string s, replace each instance of regexp (cars of elements
in al) with to-string (cdrs of elements in al) as per replace-regexp."

  (save-excursion
    (let (tal ss)

      ;; Have to use a temporary buffer to pull this one off...
      (set-buffer (get-buffer-create "!@#$%^&*"))
      (insert s)

      ;; Walk down al with tal, regexp-replacing instances of
      ;; (car (car tal)) with (cdr (car tal)).
      (setq tal al)
      (while tal
        (goto-char (point-min))
        (replace-regexp (car (car tal)) (cdr (car tal)))
        (setq tal (cdr tal)))
      (setq ss (buffer-string))
      (kill-buffer "!@#$%^&*")
      ss)))


(defun abbreviate-mode-line-buffer-identification ()
"Abbreviates mode-line-buffer-identification locally, as per
string-replace-regexp-alist and file-name-abbreviation-alist."
  (setq mode-line-buffer-identification
    (list (string-replace-regexp-alist buffer-file-name
				       file-name-abbreviation-alist))))


;;;;;;;;;; Add abbreviate-mode-line-buffer-identification to
;;;;;;;;;; find-file-hooks.
(setq find-file-hooks '(abbreviate-mode-line-buffer-identification))


I humbly beg pardon,
Silver.

M-x device-open
Open device: mouth
M-x device-insert
Insert: foot

 Andy Gaynor   201-545-0458   81 Hassart St, New Brunswick, NJ 08901
   gaynor@topaz.rutgers.edu   ...!rutgers!topaz.rutgers.edu!gaynor
      "There is no editor but Emacs, and Lisp is its prophet."