;;; swapley.el --- key swapping mode.

;; Copyright (C) 2008  Taiki SUGAWARA

;; Author: Taiki SUGAWARA <buzz.taiki@gmail.com>
;; Keywords: convenience, hentai

;; 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., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; This mode provides key swappings.
;; `swapley-key-alist' define the swapped key pairs. Defualt definition translate number and symbols.

;;; Code:

(defvar swapley-key-alist
  '((?1 . ?!)
    (?2 . ?\")
    (?3 . ?#)
    (?4 . ?$)
    (?5 . ?%)
    (?6 . ?&)
    (?7 . ?')
    (?8 . ?\()
    (?9 . ?\))))

(defvar swapley-modifiers '(control meta super hyper))

(define-minor-mode swapley-mode
  "key swapping mode."
  :global t
  :keymap nil
  (if swapley-mode
      (swapley-apply)
    (swapley-apply 'cancel)))

(defun swapley-apply (&optional cancel-p)
  (mapc (lambda (key)
	  (mapc (lambda (mods)
		  (define-key
		    key-translation-map
		    (vector (append mods (list (car key))))
		    (unless cancel-p (vector (append mods (list (cdr key))))))
		  (define-key
		    key-translation-map
		    (vector (append mods (list (cdr key))))
		    (unless cancel-p (vector (append mods (list (car key)))))))
		(cons nil (swapley-modifier-combinations))))
	swapley-key-alist))

(defun swapley-modifier-combinations ()
  (swapley-modifier-combinations-1 swapley-modifiers))

(defun swapley-modifier-combinations-1 (list)
  (when list
    (append
     (list (list (car list)))
     (swapley-modifier-combinations-2 (list (car list))
				      (cdr list))
     (swapley-modifier-combinations-1 (cdr list)))))

(defun swapley-modifier-combinations-2 (list1 list2)
  (when list2
    (append
     (mapcar (lambda (x) (append list1 (list x))) list2)
     (swapley-modifier-combinations-2 (append list1 (list (car list2)))
				      (cdr list2)))))

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