Skip to content

Instantly share code, notes, and snippets.

@stassats
Created August 18, 2011 12:08
Show Gist options
  • Save stassats/1153933 to your computer and use it in GitHub Desktop.
Save stassats/1153933 to your computer and use it in GitHub Desktop.
Statistics of Emacs commands
(defvar key-stats-commands ())
(defun key-stats-record-command ()
(when (and (stringp (this-command-keys))
(not (eql this-command 'self-insert-command)))
(let* ((keys (this-command-keys))
(keys-list (assoc major-mode key-stats-commands))
(command (assoc keys (cdr keys-list))))
(cond (command
(incf (cdr command)))
(keys-list
(push (cons keys 1) (cdr keys-list)))
(t
(push (list major-mode (cons keys 1))
key-stats-commands))))))
(defun key-stats-print-keys (keys)
(insert (symbol-name (car keys)))
(newline)
(loop for (key . count) in (sort* (copy-list (cdr keys))
#'> :key #'cdr)
do (insert (format " %-7s %d\n" (key-description key)
count))))
(defun key-stats ()
(interactive)
(with-current-buffer (get-buffer-create "*key-stats*")
(erase-buffer)
(mapc 'key-stats-print-keys key-stats-commands)
(goto-char (point-min))
(pop-to-buffer (current-buffer))))
(add-hook 'pre-command-hook 'key-stats-record-command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment