[net.emacs] a simple problem for 4.2 gosling-emacs

podar@sbcs.UUCP (Sunil Podar) (05/04/85)

I wanted to write a very simple mlisp function, but couldn't get it to do
what I wanted; This is for Gosling emacs on 4.2BSD.
Here is what I eventually settled with:
;-------------------------------------
(defun  (forget-it buff-name
	    (setq buff-name (get-tty-string (concat ": delete-buffer [" 
				   (current-buffer-name) "] ")))
	    (if (= buff-name "")	    
		    (delete-buffer (current-buffer-name))
		    (delete-buffer buff-name))))
;;;-------------------------------------

The problem is that I wanted to use "get-tty-buffer" in place of 
"get-tty-string" so that name-completion would be provided, which is 
a very useful (& sometimes essential) feature for many buffer names are
often very cryptic (e.g. **Scratch Stuff**) and "get-tty-string" needs 
to have the entire name specified exactly. Moreover I wanted to provide 
a default for which you merely had to press <return>, but get-tty-buffer
does not have provision for such, pressing return gives you a whole list
of buffers. One ugly way around is to have two separate functions(bound to
two keys <a no,no> , one for deleting current buffer & other for other
buffers. Another ugly way around is to first use a "get-tty-character" and
then if answer <> "" then ask-again, this time with no default & 
get-tty-buffer.
In short, is there a way to give default to get-tty-buffer without having
to modify the c-code that handles buffers. I am sure the other similar
commands like "get-tty-file", etc. have the same problem, excepting of course
"get-tty-char.../string". 
Any ideas? Thanks in advance. I think it is a fairly simple problem,
perhaps net.emacs should be spared the answers,if any,well I leave it
upto people.net.emacs.

-- 
Sunil Podar		CSNET: podar@sbcs.csnet
SUNY at Stony Brook	ARPA: podar%suny-sb.csnet@csnet-relay.arpa
			UUCP: {allegra, hocsd, philabs, ogcvax} !sbcs!podar

glenn@nsc.UUCP (Glenn Skinner) (05/09/85)

In article <271@sbcs.UUCP> podar@sbcs.UUCP (Sunil Podar) wanted to write
an MLisp function that used command completion to get a buffer name, but
that supplied a default buffer name if the user simply typed CR as a response.

The following function can be used to construct such a function.  He suggested
that someone send him a solution by mail, but since the technique used is
nonobvious, I decided to post the solution.  The basic idea is to use a local
keymap that binds everything to self-insert-and-exit to obtain a single
character.  If this character is CR, we return the default buffer name.
Otherwise, we push back the character obtained, restore keymaps and use
get-tty-buffer to get the rest of the name.  The function below assumes that
you have the UniPress V2.00 version of gosmacs -- it uses current-local-keymap.
If you have an older version, you'll have to kludge around it.

(defun
    (get-defaulted-buffer-name	default oldmap key buffer
	(setq default (arg 1 ": get-defaulted-buffer-name "))
	(setq oldmap (current-local-keymap)
	(use-local-map "single-char-keymap")
	(setq key "")
	; It's important to use error-occurred here.  Otherwise we could
	; end up with a keymap that leaves us totally helpless!
	(error-occurred
	    (setq key (get-tty-string ""))
	)
	(use-local-map oldmap)
	(if (!= key "")
	    (progn
		(push-back-string key)
		(setq buffer (get-tty-buffer ""))
	    )
	    (setq buffer default)
	)
	buffer
    )
)

(progn i oldmap
    (define-keymap "single-char-keymap")
    (setq oldmap (current-local-keymap))
    (use-local-map "single-char-keymap")
    (setq i 0)
    (while (< i 0200)
	(local-bind-to-key "self-insert-and-exit" i)
	(setq i (+ i 1))
    )
    (use-local-map oldmap)
)

Caveat: I haven't actually tested this code.  However, I've used code very
similar to it for other purposes.

		-- Glenn Skinner
		National Semiconductor, Microcomputer Systems Division
		(408) 733-2600 x 335

cdh@calmasd.UUCP (C. Douglas Hodges) (05/10/85)

> >From: podar@sbcs.UUCP (Sunil Podar)
> Subject: a simple problem for 4.2 gosling-emacs
> Date: 4 May 85 02:56:40 GMT
> Organization: Computer Science Dept, SUNY@Stony Brook
> 
> I wanted to write a very simple mlisp function, but couldn't get it to do
> what I wanted; This is for Gosling emacs on 4.2BSD.
> Here is what I eventually settled with:
> ;-------------------------------------
> (defun  (forget-it buff-name
> 	    (setq buff-name (get-tty-string (concat ": delete-buffer [" 
> 				   (current-buffer-name) "] ")))
> 	    (if (= buff-name "")	    
> 		    (delete-buffer (current-buffer-name))
> 		    (delete-buffer buff-name))))
> ;;;-------------------------------------
> 
> The problem is that I wanted to use "get-tty-buffer" in place of 
> "get-tty-string" so that name-completion would be provided, which is 
> a very useful (& sometimes essential) feature for many buffer names are
> often very cryptic (e.g. **Scratch Stuff**) and "get-tty-string" needs 
> to have the entire name specified exactly. Moreover I wanted to provide 
> a default for which you merely had to press <return>, .......

I think the following does what you desire:


(defun 
    
    ; prompts the user for a buffer name (similar to "get-tty-buffer")
    ; except that it provides two ways to default to the current buffer:
    ;       <CR>          -- immediate default to current buffer name
    ;       space, escape -- expand the current buffer name out for the user
    ;                        but do not return (give a chance to change it)

    (get-tty-buf-w/-default char buf-name prompt
	(if (error-occurred (setq prompt (arg 1)))
	    (setq prompt ": buffer-name ")
	)
	(message prompt)
	(setq char (get-tty-character)) 
	(if (= char '\^M')
	    (setq buf-name (current-buffer-name))
	    (progn 
		   (if (| (= char ' ') (= char '\e'))
		       (push-back-string (current-buffer-name))
		       (push-back-character char)
		   )
		   (setq buf-name (get-tty-buffer prompt))
	    )
	)
	buf-name
    )
)

(defun 
    (forget-it
	(delete-buffer (get-tty-buf-w/-default ": delete-buffer "))
    )
)