From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: Character repeation detection Date: Sun, 09 Mar 2014 21:20:38 -0400 Message-ID: References: <878usjewew.fsf@gmail.com> <87eh2bmlvd.fsf@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1394414474 7286 80.91.229.3 (10 Mar 2014 01:21:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 10 Mar 2014 01:21:14 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Mar 10 02:21:23 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WMouA-0001Ra-Ic for geh-help-gnu-emacs@m.gmane.org; Mon, 10 Mar 2014 02:21:22 +0100 Original-Received: from localhost ([::1]:46319 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WMouA-0001ni-4R for geh-help-gnu-emacs@m.gmane.org; Sun, 09 Mar 2014 21:21:22 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51447) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WMott-0001nM-4F for help-gnu-emacs@gnu.org; Sun, 09 Mar 2014 21:21:12 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WMotl-0005xF-J9 for help-gnu-emacs@gnu.org; Sun, 09 Mar 2014 21:21:05 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:49845) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WMotl-0005wE-D7 for help-gnu-emacs@gnu.org; Sun, 09 Mar 2014 21:20:57 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1WMotf-000174-Tv for help-gnu-emacs@gnu.org; Mon, 10 Mar 2014 02:20:51 +0100 Original-Received: from 76-10-154-114.dsl.teksavvy.com ([76.10.154.114]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 10 Mar 2014 02:20:51 +0100 Original-Received: from monnier by 76-10-154-114.dsl.teksavvy.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 10 Mar 2014 02:20:51 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 49 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 76-10-154-114.dsl.teksavvy.com User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) Cancel-Lock: sha1:VP+JOwqmUzKa0b9uxfRMYfy8Y0s= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:96370 Archived-At: > For example, I want to invoke occur. I press o and keep it pressed. > After auto repeat types 3 o characters this function kicks in, removes > the 3 typed Os from the buffer, removes the additional Os from the > keyboard buffer (if I kept it pressed for two long) and calls occur. I think that dealing with auto-repeat would be ugly: you have to wait "1 / auto-repeat-rate" before you know if you've seen the last repetition and in general you don't know auto-repeat-rate so it's a bit hackish. And if you additionally want to check if the event is really an auto-repeat and not some other event that also came quickly, then you need to "peek" at the events, which is somewhere between hard and impossible to do reliably. OTOH if you only want to handle the "3 presses" case, you can probably do it as follows: (defvar my-multi-self-insert-trick-map '((?o . occur))) (defun my-multi-self-insert-trick () (and (eq this-command last-command) (not current-prefix-arg) (looking-back "\\(.\\)\\1\\{2\\}" (- (point) 3)) ;; 3rd in a row. (let ((cmd (cdr (assq (char-before) my-multi-self-insert-trick-map)))) (when cmd (delete-region (match-beginning 0) (match-end 0)) (call-interactively cmd))))) (add-hook 'post-self-insert-hook 'my-multi-self-insert-trick) To handle auto-repeat, you'd have to add a loop along the lines of (let ((evt last-command-event)) (while (and (not (sit-for (/ 1.0 my-auto-repeat-rate) 'nodisp)) (let ((k (read-key ""))) (if (and (eq (length k) 1) (eq (aref k 0) evt)) ;; It's a repetition; just drop it. t ;; It's not a repetition; put it back. ;; FIXME: we basically can't do it 100% right because ;; read-key already had to decide whether the incoming ;; events are bytes or characters. (setq unread-command-events (nconc (append (this-single-command-raw-keys) nil) unread-command-events)) nil))))) after the `when cmd'. Stefan