Вопрос

I'm an Emacs novice. I'm currently in the middle of learning the basic commands for navigation, editing etc.

Some key-combinations take me longer to complete...they usually require either more keys to be pressed or a higher degree of finger-position acrobatics :). I'm wondering if anybody knows if there is an existing Emacs plugin that:

  1. Recognises when the user has begun typing a command (e.g. 'C-' something or 'M-' something) and records the time at that instant, then
  2. waits until the user has finished typing the command (i.e. the point at which the command is recognised, before the command is actually executed) and records that time too, then
  3. appends some simple logging info to a configurable file (e.g. writes 'command description', 'keys', 'entry duration') for that particular occurrence.

Why? Perhaps it's overkill, but it would probably save me time in the future if I were able to analyse such a log file and determine which commands have a high usage frequency as well as a long completion time. Such commands could be bound to keys that are simpler for my humble fingers to reach, for example :).

It could be a nice learning exercise for me to write this myself, but I'd probably prefer to save the time if a solution already exists.

Это было полезно?

Решение

Here, take this lisp code:

(global-set-key (kbd "C-t") 'timed-key-seq)

(defvar timed-keys-hash (make-hash-table :test 'equal))

(defun timed-key-seq ()
  (interactive)
  (let* ((t-beg (current-time))
         (key (read-key-sequence "command:"))
         (t-end (current-time))
         (t-diff (time-subtract t-end t-beg))
         (data (gethash key timed-keys-hash)))
    (if data
        (destructuring-bind (times . time) data
          (puthash key (cons (1+ times) (time-add time t-diff))
                   timed-keys-hash))
      (puthash key (cons 1 t-diff) timed-keys-hash))
    (call-interactively (key-binding key))))

(defun anb-report ()
  (interactive)
  (let (entries)
    (maphash (lambda (key data)
               (push
                (format "%s:\t%sx = %s ms\n"
                     key (car data)
                     (format-time-string "%3N" (cdr data)))
                entries))
           timed-keys-hash)
    (message (apply #'concat entries))))

The disadvantage is that you need to bind timed-key-seq to something, I'd suggest C-t. And then you just prefix all your commands by C-t. Maybe someone can suggest a defadvice that will make sure that this function will be called each time without having to prefix it.

Use anb-report to generate the report. It will show you how many times you called each combination and how many milliseconds they took together.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top