[comp.emacs] fast buffer switching in emacs

nat%DRAO.NRC.CA@VM.TCS.TULANE.EDU (Natalie Prowse) (12/14/90)

Ok, I've had a few requests for this so here it is...  No flames
about my kludgey lisp code please..;-) I have only been exposed
to emacs for about 3 months now, and I didn't know any lisp before
I started...
All standard disclaimers apply.  If you need help, my address is in the
comment line of the ebuffer.el file.

-Natalie

-------------------------cut here --- ebuffer.el-------------------

;; This file was created by Natalie Prowse (nat@drao.nrc.ca) in November/90
;; to emulate eve-plus buffer-switching features...
;; I don't profess to understand how the keyboard mapping for the GOLD-key
;; works... All I know is that if, prior to starting your emacs session, set
 your
;; terminal type to VT100, VT200, or VT220, it works (Ie. at the shell command
 line,
;; type: set term=vt200 ).  I would imagine it works
;; for many terminal types, as I suspect that 'enable-arrow-keys' has a
 different
;; definition dependant on terminal type.

(require 'keypad)

(defvar buff-assgn-1 "" "buffer name mapped to slot 1")
(defvar buff-assgn-2 "" "buffer name mapped to slot 2")
(defvar buff-assgn-3 "" "buffer name mapped to slot 3")
(defvar buff-assgn-4 "" "buffer name mapped to slot 4")
(defvar buff-assgn-5 "" "buffer name mapped to slot 5")
(defvar buff-assgn-6 "" "buffer name mapped to slot 6")
(defvar buff-assgn-7 "" "buffer name mapped to slot 7")
(defvar buff-assgn-8 "" "buffer name mapped to slot 8")
(defvar buff-assgn-9 "" "buffer name mapped to slot 9")
(defvar evh-rect-mode "F" "*mode switch for cutting and pasting rectangular")

(defvar buff-map "" "*name of buffer symbol")
(defvar keynum  nil "*number of top-row key that was pressed")


(defun initial-file-load ()
 (setq keynum 1)
 (setq found nil)
 (setq intbuf (buffer-name))

 (while (and (/= keynum 10) (not found))
   (setq buff-map (symbol-value (intern (concat "buff-assgn-" keynum))))
   (cond ((string= buff-map "")
	  (set (intern (concat "buff-assgn-" keynum)) (buffer-name))
	  (setq found t))
	 (t
	  (cond ((get-buffer buff-map)     ;; if there is already a buffer assigned
		 (setq keynum (+ keynum 1))) ;; then try the next key
		(t                           ;; if the buffer has been killed, use it
		 (setq found t)
		 (set (intern (concat "buff-assgn-" keynum)) (buffer-name)))))))

 (cond ((eq keynum 10)  ;; if we finished the loop and we couldn't find a key
	(message (concat "All top-row keys are full, file: " (buffer-name) " loaded
 anyway.")))
       (t
	(message (concat "File: " (buffer-name) " mapped to top-row key GOLD-"
 keynum)))))




(defun map-buffer-to-key ()
  " Map a buffer to a top-row numeric key equivalent.
With the find-file-hooks in place, the next available key will be chosen, if
 there
is space, otherwise, if the key is already mapped to an active buffer, the user
will be switched to the buffer.
Ie.  if this procedure invokes find-file, it also will invoke intial-load-file,
which will try to map the new filebuffer to a top row numeric key."

  (interactive)
  ;; get the keynumber of the last key pressed (a top row numeric)
  (setq keynum (- last-command-char 48)) ;; key 1 is 49... key 9 is 57

      ;; get the contents of the variable that maps to the key
  (setq buff-map (symbol-value (intern (concat "buff-assgn-" keynum))))
  ;;if the buffer has not yet been mapped, get a file
  (cond ((string= buff-map "")
	 (setq buff-map (read-string "File to get: " default-directory ))
	 (cond ((not (string= buff-map default-directory))
		(setq dlen  (length default-directory))
		(cond ((get-buffer (substring buff-map dlen))
		       (switch-to-buffer (substring buff-map dlen))
		       (initial-file-load))
		      (t
		       (find-file buff-map))))))
    ;;else
	;; if the buffer is mapped to a file, but the buffer no longer exists,
	;; get a new file to fill the buffer
	(t
	 (cond ((get-buffer buff-map)
		(switch-to-buffer buff-map))
	       (t
		(setq buff-map (read-string "File to get: " default-directory ))
		(cond ((not (string= buff-map default-directory))
		       (find-file buff-map))))))))


(defun nat-buffer-list()
" list all the mapped buffers and the associated key-maps."
(interactive)
(with-output-to-temp-buffer "bufflist"
  (setq numcnt 1)
  (while (/= 10 numcnt)
    (setq buffnam (symbol-value (intern (concat "buff-assgn-" numcnt))))
    (if (not (string= buffnam ""))
	(print (concat buffnam " is mapped to PF1-" numcnt)))
    (setq numcnt (+ numcnt 1)))))

(setq GOLD-map (make-keymap))
(fset 'GOLD-prefix GOLD-map)

(defvar GOLD-map nil
   "GOLD-map maps the function keys on the VT100 keyboard preceeded
by the PF1 key.  GOLD is the ASCII the 7-bit escape sequence <ESC>OP.")

(define-key function-keymap "\C-a" 'GOLD-prefix)	;PF1 ("gold")
(define-key GOLD-map "1" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "2" 'map-buffer-to-key)                ; like "eve-plus"
 does
(define-key GOLD-map "3" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "4" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "5" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "6" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "7" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "8" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "9" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "0" 'nat-buffer-list)                      ; like
 "eve-plus" does

(initial-file-load)




--------------------------cut here-------(.emacs file)

;; To customise this file, just copy it as .emacs in your home
;; directory and set the variable inhibit-default-init to non-nil
;; in order to avoid loading the default.el file after your .emacs file.

;; Example to redefine the load-path for the *.el and *.elc files
;; (first specification is your local directory - where the ebuffer.el file
 resides,
;;   the second is where system-wide emacs files live)

         (setq load-path '("/users/newobs/einit" "/usr/new/emacs/lib/lisp"))

(defun my-terminal ()
  "This function should  be called by the term-setup-hook mechanism in your
  .emacs file in order to tailor the VT200 keypad map to your own purposes"
  (interactive)
  (load "ebuffer")
  (enable-arrow-keys)
  (setq find-file-hooks (list 'initial-file-load)))

(setq inhibit-default-init t)           ; in your .emacs file

(setq require-final-newline t)          ; remap keys in due time
(setq term-setup-hook 'my-terminal)     ; remap keys in due time
(redraw-display)

nat%DRAO.NRC.CA@VM.TCS.TULANE.EDU (Natalie Prowse) (12/17/90)

Due to the number of requests for this, and due to the fact that I
inadvertantly delted my entire inbox... (aarrggh!), I am posting
this bit of code that I use to do fast buffer switching.  For those
of you familiar with EVE-PLUS, under VMS, it is VERY similar to EAG
windows...
---------------------.emacs----------------------

==================
;; To customise this file, just copy it as .emacs in your home
;; directory and set the variable inhibit-default-init to non-nil
;; in order to avoid loading the default.el file after your .emacs file.

;; Example to redefine the load-path for the *.el and *.elc files
;; (first specification is your local directory, second is where system-wide
 emacs files live)

         (setq load-path '("/users/newobs/einit" "/usr/new/emacs/lib/lisp"))

(defun vax-terminal ()
  "This function should  be called by the term-setup-hook mechanism in your
  .emacs file in order to tailor the VT200 keypad map to your own purposes"
  (interactive)
  (load "ebuffer")
  (enable-arrow-keys)
  (setq find-file-hooks (list 'initial-file-load)))

(setq inhibit-default-init t)         ; in your .emacs file

(setq require-final-newline t)           ; remap keys in due time
(setq term-setup-hook 'vax-terminal)     ; remap keys in due time
(redraw-display)

==================
----------------------ebuffer.el-------------------------

==================
;; This file was created by Natalie Prowse (nat@drao.nrc.ca) in November/90
;; to emulate eve-plus buffer-switching features...
;; I don't profess to understand how the keyboard mapping for the GOLD-key
;; works... All I know is that if, prior to starting your emacs session, set
 your
;; terminal type to VT100, VT200, or VT220, it works (Ie. at the shell command
 line,
;; type: set term=vt200 ).  I would imagine it works
;; for many terminal types, as I suspect that 'enable-arrow-keys' has a
 different
;; definition dependant on terminal type.

(require 'keypad)

(defvar buff-assgn-1 "" "buffer name mapped to slot 1")
(defvar buff-assgn-2 "" "buffer name mapped to slot 2")
(defvar buff-assgn-3 "" "buffer name mapped to slot 3")
(defvar buff-assgn-4 "" "buffer name mapped to slot 4")
(defvar buff-assgn-5 "" "buffer name mapped to slot 5")
(defvar buff-assgn-6 "" "buffer name mapped to slot 6")
(defvar buff-assgn-7 "" "buffer name mapped to slot 7")
(defvar buff-assgn-8 "" "buffer name mapped to slot 8")
(defvar buff-assgn-9 "" "buffer name mapped to slot 9")
(defvar evh-rect-mode "F" "*mode switch for cutting and pasting rectangular")

(defvar buff-map "" "*name of buffer symbol")
(defvar keynum  nil "*number of top-row key that was pressed")


(defun initial-file-load ()
 (setq keynum 1)
 (setq found nil)
 (setq intbuf (buffer-name))

 (while (and (/= keynum 10) (not found))
   (setq buff-map (symbol-value (intern (concat "buff-assgn-" keynum))))
   (cond ((string= buff-map "")
	  (set (intern (concat "buff-assgn-" keynum)) (buffer-name))
	  (setq found t))
	 (t
	  (cond ((get-buffer buff-map)     ;; if there is already a buffer assigned
		 (setq keynum (+ keynum 1))) ;; then try the next key
		(t                           ;; if the buffer has been killed, use it
		 (setq found t)
		 (set (intern (concat "buff-assgn-" keynum)) (buffer-name)))))))

 (cond ((eq keynum 10)  ;; if we finished the loop and we couldn't find a key
	(message (concat "All top-row keys are full, file: " (buffer-name) " loaded
 anyway.")))
       (t
	(message (concat "File: " (buffer-name) " mapped to top-row key GOLD-"
 keynum)))))




(defun map-buffer-to-key ()
  " Map a buffer to a top-row numeric key equivalent.
With the find-file-hooks in place, the next available key will be chosen, if
 there
is space, otherwise, if the key is already mapped to an active buffer, the user
will be switched to the buffer.
Ie.  if this procedure invokes find-file, it also will invoke intial-load-file,
which will try to map the new filebuffer to a top row numeric key."

  (interactive)
  ;; get the keynumber of the last key pressed (a top row numeric)
  (setq keynum (- last-command-char 48)) ;; key 1 is 49... key 9 is 57

      ;; get the contents of the variable that maps to the key
  (setq buff-map (symbol-value (intern (concat "buff-assgn-" keynum))))
  ;;if the buffer has not yet been mapped, get a file
  (cond ((string= buff-map "")
	 (setq buff-map (read-string "File to get: " default-directory ))
	 (cond ((not (string= buff-map default-directory))
		(setq dlen  (length default-directory))
		(cond ((get-buffer (substring buff-map dlen))
		       (switch-to-buffer (substring buff-map dlen))
		       (initial-file-load))
		      (t
		       (find-file buff-map))))))
    ;;else
	;; if the buffer is mapped to a file, but the buffer no longer exists,
	;; get a new file to fill the buffer
	(t
	 (cond ((get-buffer buff-map)
		(switch-to-buffer buff-map))
	       (t
		(setq buff-map (read-string "File to get: " default-directory ))
		(cond ((not (string= buff-map default-directory))
		       (find-file buff-map))))))))


(defun nat-buffer-list()
" list all the mapped buffers and the associated key-maps."
(interactive)
(with-output-to-temp-buffer "bufflist"
  (setq numcnt 1)
  (while (/= 10 numcnt)
    (setq buffnam (symbol-value (intern (concat "buff-assgn-" numcnt))))
    (if (not (string= buffnam ""))
	(print (concat buffnam " is mapped to PF1-" numcnt)))
    (setq numcnt (+ numcnt 1)))))

(setq GOLD-map (make-keymap))
(fset 'GOLD-prefix GOLD-map)

(defvar GOLD-map nil
   "GOLD-map maps the function keys on the VT100 keyboard preceeded
by the PF1 key.  GOLD is the ASCII the 7-bit escape sequence <ESC>OP.")

(define-key function-keymap "\C-a" 'GOLD-prefix)	;PF1 ("gold")
(define-key GOLD-map "1" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "2" 'map-buffer-to-key)                ; like "eve-plus"
 does
(define-key GOLD-map "3" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "4" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "5" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "6" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "7" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "8" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "9" 'map-buffer-to-key)                       ; like
 "eve-plus" does
(define-key GOLD-map "0" 'nat-buffer-list)                      ; like
 "eve-plus" does

(initial-file-load)




==================
---------------------------------------------------------
-natalie

==================

Natalie Prowse
Dominion Radio Astrophysical Observatory,        nat@drao.nrc.ca
National Research Council,                       (604) 497-5321
Box 248,
Penticton, BC, Canada
V2A 6K3