From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.help Subject: Problems with catch and throw Date: Thu, 04 Oct 2007 18:16:52 +0200 Organization: University Koblenz-Landau Campus Koblenz Message-ID: <87myuy26tn.fsf@baldur.tsdh.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1191519643 10770 80.91.229.12 (4 Oct 2007 17:40:43 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 4 Oct 2007 17:40:43 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Oct 04 19:40:39 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1IdUgh-0005GF-6T for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Oct 2007 19:40:39 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IdUgc-0001xK-Lg for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Oct 2007 13:40:34 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!news-fra1.dfn.de!news-stu1.dfn.de!news.belwue.de!news.uni-kl.de!cache.uni-koblenz.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 114 Original-NNTP-Posting-Host: dslb-084-063-003-099.pools.arcor-ip.net Original-X-Trace: cache.uni-koblenz.de 1191514613 25057 84.63.3.99 (4 Oct 2007 16:16:53 GMT) Original-X-Complaints-To: news@cache.uni-koblenz.de Original-NNTP-Posting-Date: Thu, 4 Oct 2007 16:16:53 +0000 (UTC) Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwEAAAAAAi+XoYAAAACXBIWXMAAABIAAAASABGyWs+ AAAATnRFWHRSYXcgcHJvZmlsZSB0eXBlIGV4aWYACmV4aWYKICAgICAgMjAKNDU3ODY5NjYwMDAw NGQ0ZDAwMmEwMDAwMDAwODAwMDAwMDAwMDAwMAqJuBZbAAAACXZwQWcAAAAwAAAAMADO7oxXAAAB I0lEQVRYw+VX2w3DMAi87+zTFbr/IBmiqlTLjnkTLDmqrH5UMRzcQSA4X2sPpAfvYzFADoRagF6h x+NYsgG9OF73R94suzUB6M7v0jRCoLpqRvffH3qUFiWRXFiKZBc9gHZD16grwWhg1QitGE10BwBX thZE4/8HICvgcS5lMVEUi572y1zaSh94etpSYuoDniK9SrTbbCfPEXIu9K6QsoWU8uhAFlKGaE8h 13v85cxZgus/2T3HPe34ERq6Ays+DsAx0TSQ2H/nyy46Jo0Mqsa9Y+hnMph1QoXT0FZRC5jai6zC dJWpZehfacq3ir00iC8pz6XoJgC/mNgL2B9RpC+USwD8s3svijKjaF+Rr8tJIUCUJqyNPwVw/R5e KvLz+iBzPs4pHcCUQrRFAAAAKnpUWHRDb21tZW50AAB42vMKcHW3Ss8r1c1ITUzRyypIVzAyN6sw MjMEAGiaB6m0ORgZAAAAGnpUWHRqcGVnOmNvbG9yc3BhY2UAAHjaMwQAADIAMimkmokAAAAhelRY dGpwZWc6c2FtcGxpbmctZmFjdG9yAAB42jOqMAIAAbsA3XNgMqsAAAAASUVORK5CYII= User-Agent: Gnus/5.110007 (No Gnus v0.7) Emacs/23.0.50 (gnu/linux) Cancel-Lock: sha1:XSalH5h+RPARrXWfQjYZqhsDaVo= Original-Xref: shelby.stanford.edu gnu.emacs.help:152611 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:48119 Archived-At: Hi, I use a home-brewn function [1] instead of `execute-extended-command' most of the time. It uses `read-from-minibuffer' to get a command abbrev from the user and then executes the command whose abbreviation was given. Now I'd like to bind TAB in my function so that it aborts `read-from-minibuffer' and my function and falls back to `execute-extended-command' where the current minibuffer contents are inserted as initial input. That sounds like a good candidate for catch/throw to me. A minimal working example is: --8<---------------cut here---------------start------------->8--- (catch 'foo (read-from-minibuffer "test: " nil (let ((map (copy-keymap minibuffer-local-map))) (define-key map "\C-i" (lambda () (interactive) (throw 'foo (minibuffer-contents)))) map))) --8<---------------cut here---------------end--------------->8--- Hit `C-j' after the last closing paren and type "foobar" at the "test: " prompt. The form's result is "foobar" then. So now here's my function with this mechanism applied. But it doesn't work like the minimal example. (minibuffer-contents) always returns the empty string "". Why is that? --8<---------------cut here---------------start------------->8--- (defun exec-abbrev-cmd (prefixarg) "Query for a command abbreviation like \"mbm\" and calculate a list of all commands of the form \"m[^-]*-b[^-]*-m[^-]*$\". If this list has only one item, this command will be executed directly. If there a more choices, the user will be queried which one to call. The PREFIXARG is passed on to the invoked command. With TAB fall back to `execute-extended-command'." (interactive "P") (let ((catch-val (catch 'escape-from-exec-abbrev-cmd (let* ((abbrev (read-from-minibuffer "Command Abbrev: " nil (let ((map (copy-keymap minibuffer-local-map))) (define-key map "\C-i" (lambda () (interactive) ;; WHY RETURN MINIBUFFER-CONTENTS "" ;; HERE, ALTHOUGH I TYPED FOOBAR AT THE ;; PROMPT??? (message "cont = " (minibuffer-contents)) (throw 'escape-from-exec-abbrev-cmd (minibuffer-contents)))) map))) (regexp (concat "^" (let ((part-list (split-string abbrev "-"))) (if (= 1 (length part-list)) ;; abc => a*-b*-c* (mapconcat #'list abbrev "[^-]*-") ;; ahead-b-c => ahead*-b*-c* (mapconcat #'identity part-list "[^-]*-"))) "[^-]*$")) (commands (exec-abbrev-cmd-sort (remove-if-not (lambda (string) (string-match regexp string)) (let (c) (mapatoms (lambda (a) (if (commandp a) (push (symbol-name a) c)))) c))))) (if (not commands) (message "No such command.") (let ((c (cond ((> (length commands) 1) (intern (if (and (featurep 'ido) ido-mode) ;; ido is available and enabled, so use it. (ido-completing-read "Command: " commands) ;; fallback to normal completion with the ;; most frequently used command as default. (completing-read (concat "Command (defaults to `" (car commands) "'): ") commands nil t nil nil (car commands))))) (t (intern (car commands)))))) (call-interactively c t) (when exec-abbrev-cmd-mode (exec-abbrev-cmd-record c))))) ;; Always return nil so that an escape with TAB is the only thing ;; that makes `catch-val' non-nil. nil))) (when catch-val (message "catch-val = " catch-val) (execute-extended-command prefixarg) (insert catch-val)))) --8<---------------cut here---------------end--------------->8--- I guess it's something obvious, but I cannot see any principal difference between the simple working example and the code of my function. Any pointers are highly welcome! Bye, Tassilo __________ [1] http://www.tsdh.de/cgi-bin/wiki.pl/exec-abbrev-cmd.el