]> code.delx.au - gnu-emacs-elpa/blob - packages/caps-lock/caps-lock.el
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / caps-lock / caps-lock.el
1 ;;; caps-lock.el --- Caps-lock as a minor mode -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Version: 1.0
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;;; Code:
24
25 (defvar caps-lock-commands
26 '(self-insert-command isearch-printing-char)
27 "List of commands that are subject to `caps-lock-mode'.")
28
29 ;;;###autoload
30 (define-minor-mode caps-lock-mode
31 "Make self-inserting keys invert the capitalization."
32 :global t
33 (if caps-lock-mode
34 (add-hook 'pre-command-hook #'caps-lock--pch)
35 (remove-hook 'pre-command-hook #'caps-lock--pch)))
36
37 (defun caps-lock--pch ()
38 (when (and (characterp last-command-event)
39 (or (memq this-command caps-lock-commands)
40 (eq this-command (key-binding [remap self-insert-command]))))
41 (setq last-command-event
42 (condition-case nil
43 (let ((up (upcase last-command-event)))
44 (if (eq up last-command-event)
45 (downcase last-command-event)
46 up))
47 (error last-command-event)))))
48
49 (provide 'caps-lock)
50 ;;; caps-lock.el ends here