First, I wanted it to open two buffers, one with the annotated file (the file you're working on with the change list number at the start of each line), and another with the changelist you're interested in. When running the command multiple times I want these buffers to be deleted. So I added a helper function that creates a named buffer, and if it exists already, makes sure it is empty.
(interactive "sName: ")
(let ((buffer (get-buffer-create name)))
(goto-line 1 buffer)
Ok now for the blame code. As you can see, mostly what it's doing is called the P4 command line and passing the output to buffers. Firstly I call annotate to get the change list numbered lines. Then I go to that file and go to the same line that point was at. (number-at-point) returns that number. Finally I call p4 describe to get the changelist information.
"blame the current line of code... it seeks out the change list of the last person
to change this line. The arguments to annotate, which dumps the source file with the
change list number it was last altered in, are q (no header) i (follow branches) and
c for change numbers rather than revision numbers."
(let* ((line (line-number-at-pos))
(annotate-buffer (get-buffer-create-and-clear "*p4-annotate*")))
(shell-command (format "p4.exe annotate -q -i -c %s" source-file) annotate-buffer)
(goto-line line annotate-buffer)
(let ((change-number (number-at-point)))
(let ((blame-buffer (get-buffer-create-and-clear "*p4-blame*")))
(shell-command (format "p4.exe describe %d" change-number) blame-buffer))
(message "error: could not get change list number for line %d" line)))))
The error detection is a bit sloppy here. I don't know how to figure out if the shell-command failed. But it's not a big deal because if you do this in a file that is not in P4 then it will simply print a message saying could not get change list.
Ideally I could send the error output to a buffer, and then parse it to see if it's empty.