gnu-emacs-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

singlebind.el --- Bind commands to single characters


From: address@hidden
Subject: singlebind.el --- Bind commands to single characters
Date: 6 Mar 2007 06:23:25 -0800
User-agent: G2/1.0

I don't know yet how useful it is. It was just a sudden idea and I
implemented it on the spot.

I know the approach of rebinding self-insert-command is a bit heavy
handed. It's just an experiment.



;;; singlebind.el --- Bind commands to single characters

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; It occured to me that I rarely type single characters without doing
;; something afterwards (like saving the file), so I thought it could
;; be used for convenient single-key command execution.
;;
;; If a self-insert-command is typed after a non self-insert-command
;; then singlebind waits for a short while (singlebind-command-delay)
;; and if there are no new input events then it checks if there is a
;; command assigned to the key which invoked the self-insert-command.
;;
;; If a command is found then the effect of the self-insert-command is
;; canceled and the command is invoked. Here's how to assign a command
;; to a key:
;;
;;    (push '(?g . grep) singlebind-command-map)
;;
;;
;; In read-only buffers singlebind-command-delay is not used and the
;; bound command is invoked immediately.
;;

;;; Code:

(defvar singlebind-command-map nil)

(defvar singlebind-command-delay 1)


(substitute-key-definition 'self-insert-command
                           'singlebind-self-insert
                           (current-global-map))

(defun singlebind-self-insert ()
  (interactive)
  (let ((buffer-modified (buffer-modified-p)))
    ;; I know, universal-argument is not handled
    (unless buffer-read-only
      (self-insert-command 1))
    (if (and (not (eq last-command 'singlebind-self-insert))
             (or buffer-read-only
                 (sit-for singlebind-command-delay)))
        (let ((mapping (assoc last-command-char
                              singlebind-command-map)))
          (if (not mapping)
              ;; so that the user doesn't miss the error
              (if buffer-read-only
                  (self-insert-command 1))

            ;; undo didn't seem to work here
            (unless buffer-read-only
              (backward-delete-char 1)
              (unless buffer-modified
                (set-buffer-modified-p nil)))

            (call-interactively (cdr mapping)))))))


(provide 'singlebind)
;;; singlebind.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]