From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: batkins57@gmail.com Newsgroups: gmane.emacs.help Subject: Keybindings and minor modes Date: 28 Dec 2005 18:18:15 -0800 Organization: http://groups.google.com Message-ID: <1135822695.537535.182680@f14g2000cwb.googlegroups.com> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: sea.gmane.org 1135822755 5166 80.91.229.2 (29 Dec 2005 02:19:15 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 29 Dec 2005 02:19:15 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Dec 29 03:19:14 2005 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1ErnNk-00079V-AF for geh-help-gnu-emacs@m.gmane.org; Thu, 29 Dec 2005 03:19:09 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ErnP4-0006PW-QN for geh-help-gnu-emacs@m.gmane.org; Wed, 28 Dec 2005 21:20:30 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!f14g2000cwb.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.emacs Original-Lines: 102 Original-NNTP-Posting-Host: 68.239.200.76 Original-X-Trace: posting.google.com 1135822700 9264 127.0.0.1 (29 Dec 2005 02:18:20 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Thu, 29 Dec 2005 02:18:20 +0000 (UTC) User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20051005 Firefox/1.0.7,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: f14g2000cwb.googlegroups.com; posting-host=68.239.200.76; posting-account=RQyFAQ0AAAAIiTCD-QkZwj2n8DsgdVTY Original-Xref: shelby.stanford.edu gnu.emacs.help:136656 comp.emacs:90798 Original-To: help-gnu-emacs@gnu.org 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:32275 Archived-At: I'd like to bind a command to tab in a minor mode I'm writing, but I'd also like to preserve any command that might have been attached to that key before making my binding (such as indentation). In other words, I'd like to add functionality to an existing keybinding without completely overwriting the binding. The solution I came up with was to simply store the currently bound command in a buffer-local variable whenever the minor mode is invoked. Then the keys are bound to functions that provide my additional functionality and then funcall the old bindings. This works, but I wonder if there isn't a cleaner way to pass a keychord event on to the next handler. The code I have now is pretty ugly, since a key bound to self-insert-command has to be handled separately so that self-insert-command will get an argument of 0. I've attached my code below in case it's useful. Thanks, Bill Atkins -- ;; ehinter.el - echo lambda-lists when editing emacs lisp code (defvar ehinter--old-space) (defvar ehinter--old-tab) (defvar ehinter--old-ret) (make-variable-buffer-local 'ehinter--old-space) (make-variable-buffer-local 'ehinter--old-tab) (make-variable-buffer-local 'ehinter--old-ret) (defvar ehinter-mode-map (let ((keymap (make-sparse-keymap))) (define-key keymap " " 'ehinter--space-cmd) (define-key keymap "\t" 'ehinter--tab-cmd) (define-key keymap "\C-m" 'ehinter--ret-cmd) keymap) "Keymap for the Elisp Hinter minor mode") (define-minor-mode ehinter-mode "A minor mode that echoes an arglist for whatever Elisp function is being called at point. \\{ehinter-mode-map}" :lighter " EHinter" :init-value nil (setq ehinter--old-space (ehinter--old-binding " ")) (setq ehinter--old-tab (ehinter--old-binding "\t")) (setq ehinter--old-ret (ehinter--old-binding "\C-m"))) (defun ehinter--old-binding (key) (or (lookup-key (current-local-map) key) (lookup-key (current-global-map) key))) (defun ehinter--invoke-binding (binding) (if (eq binding 'self-insert-command) (self-insert-command 1) (funcall binding))) (defun ehinter--space-cmd () (interactive) (ehinter-hint) (ehinter--invoke-binding ehinter--old-space)) (defun ehinter--ret-cmd () (interactive) (ehinter-hint) (ehinter--invoke-binding ehinter--old-ret)) (defun ehinter--tab-cmd () (interactive) (ehinter-hint) (ehinter--invoke-binding ehinter--old-tab)) (defun ehinter-hint () (interactive) (when (and (function-called-at-point) (condition-case () (symbol-function (function-called-at-point)) (void-function nil))) (message "%s" (lambda-list (function-called-at-point)))) (self-insert-command 0)) (defun strip-ends (str) (substring str 1 (1- (length str)))) (defun lambda-list (func) (let ((fn (symbol-function func))) (cond ((and (consp fn) (eq (car-safe fn) 'lambda)) (prin1-to-string (cons func (second fn)))) ((and (consp fn) (eq (car-safe fn) 'macro)) (prin1-to-string (cons func (svref (cdr fn) 0)))) ((subrp fn) (strip-ends (downcase (prin1-to-string (first (help-split-fundoc (documentation func) func)))))) (t (error "%s" (type-of func))))))