(defvar annotate-storage-file "~/.annotate"
"File containing the stored annotations.")
(defvar annotate-current-buffer nil
"The last buffer known to be the current one.")
(defvar annotate-window-configuration nil
"The window configuration before the annoation buffer was displayed.")
(defun annotate-file ()
(setq annotate-window-configuration (current-window-configuration))
(pop-to-buffer (get-file-buffer annotate-storage-file))
(local-set-key (kbd "C-c C-c") 'annotate-finish)
(message "Type C-c C-c to hide the annotation window.")
(setq annotate-current-buffer 'nosuchbuffer)
(defun annotate-narrow-to-annotation ()
"Narrow the annotation buffer to the portion belonging to the file associated
wit the current buffer.
If no annotation exists for the file a new section is created.
If the current buffer has no file associated with it then show a warning message."
(let* ((buffer (find-file-noselect annotate-storage-file))
(filename (if file
(unless (equal filename (expand-file-name annotate-storage-file))
(if (re-search-forward (concat "^\014 " filename) nil t)
(insert "\014 " filename "\n\n")
(let ((begin (point)))
(if (re-search-forward "^\014 " nil t)
(progn (forward-line -1)
(narrow-to-region begin (point))
(if (and (not file)
(= (point-min) (point-max)))
(insert "The current buffer has no file associated with it, "
"so it cannot have an annotation."))))))
(defadvice switch-to-buffer (after annotate-handle-buffer-change)
(defun annotate-update-annotation-display ()
"Synchronize the displayed annotation to the current buffer if the annotation
window is visible. Otherwise cancel current buffer monitoring."
(if (get-buffer-window (let ((buffer (get-file-buffer annotate-storage-file)))
(unless (equal (current-buffer) annotate-current-buffer)
(setq annotate-current-buffer (current-buffer))
(defun annotate-finish ()
"Hide the annotation buffer and restore previous window configuration."
(with-current-buffer (get-file-buffer annotate-storage-file)