修改 annotate 使其能在 Info mode 下做笔记

大家好!

最近发现了 annotate 这个包。可是这个包不支持 Info 文档, 我就 tweak 了一些函数,附在在后面。

这些是 Gif 展示:

  1. 写笔记

note-demo

  1. 在子 node 写笔记

note-demo-sub

  1. 重新打开 Emacs 后加载笔记。

note-demo-sub-show-al

相关修改函数:

(use-package annotate
  :ensure t
  :config (setq annotate-file (expand-file-name "~/Dropbox/myNote/annotations")))

;; for info file

;;;###autoload
(define-minor-mode annotate-mode
  "Toggle Annotate mode."
  :init-value nil
  :lighter " Ann"
  :keymap (make-sparse-keymap)
  :group 'annotate
  :after-hook (if annotate-mode
                  (progn (annotate-initialize) (split-window-below) (delete-other-windows))
                (annotate-shutdown)))

(defun tweak-annotate-load-annotations ()
  "Load all annotations from disk."
  (interactive)
  (let ((annotations (cdr (assoc-string
                           (substring-no-properties (or Info-current-file ""))
                           (annotate-load-annotation-data))))
        (modified-p (buffer-modified-p)))
    ;; remove empty annotations created by earlier bug:
    (setq annotations (cl-remove-if (lambda (ann) (eq (nth 2 ann) nil))
                                    annotations))
    (message "annotate is %s" annotations)
    (when (and (eq nil annotations) annotate-use-messages)
      (message "No annotations found."))
    (when (not (eq nil annotations))
      (save-excursion
        (dolist (annotation annotations)
          (let ((start (nth 0 annotation))
                (end (nth 1 annotation))
                (text (nth 2 annotation)))
            (message "hi")
            (annotate-create-annotation start end text)))))
    (set-buffer-modified-p modified-p)
    (font-lock-fontify-buffer)
    (if annotate-use-messages
        (message "Annotations loaded."))))

(defun annotate-initialize ()
  "Load annotations and set up save and display hooks."
  (tweak-annotate-load-annotations)
  (add-hook 'after-save-hook 'annotate-save-annotations t t)
  (add-hook 'window-configuration-change-hook 'font-lock-fontify-buffer t t)
  (font-lock-add-keywords
   nil '((annotate--font-lock-matcher (2 (annotate--annotation-builder))
                                      (1 (annotate--change-guard))))))

(defun tweak-annotate-save-annotations ()
  "Save all annotations to disk."
  (interactive)
  (let ((file-annotations (annotate-describe-annotations))
        (all-annotations (annotate-load-annotation-data))
        (filename Info-current-file))
    (if (assoc-string filename all-annotations)
        (setcdr (assoc-string filename all-annotations)
                file-annotations)
      (setq all-annotations
            (push (cons filename file-annotations)
                  all-annotations)))
    ;; remove duplicate entries (a user reported seeing them)
    (dolist (entry all-annotations)
      (delete-dups entry))
    ;; skip files with no annotations
    (annotate-dump-annotation-data (cl-remove-if
                                    (lambda (entry)
                                      (eq nil (cdr entry)))
                                    all-annotations))
    (if annotate-use-messages
        (message "Annotations saved."))))

纯新手,不知道还有什么副作用。欢迎大家提意见。

5 个赞

eiyou bu cuo ou