@peccul is peccu

(love peccu '(emacs lisp cat outdoor bicycle mac linux coffee))

bm.elでブックマーク一覧からブックマークにジャンプする

タイトルだけ見たらanything-config.elにあるanything-c-bm-initでいいじゃんと思われるかもしれませんが,これだと,カレントバッファしか参照しませんよね.

というわけで,bm-repositoryに保存されてる全バッファ,ファイルを対象にブックマークにジャンプできるソースを書きました.

anything.elが使える環境にしておいてください.

(auto-install-batch "anything")

あと,bm.elも使えるようにしてください.

(install-elisp "http://cvs.savannah.gnu.org/viewvc/*checkout*/bm/bm/bm.el")

bm.elの設定はこんな感じ

(require 'bm)
(global-set-key (kbd "<M-f2>") 'bm-toggle)
;; save bookmarks
(setq-default bm-buffer-persistence t)
;; Filename to store persistent bookmarks
(setq bm-repository-file "/Users/peccu/.emacs.d/.bm-repository")

;; Loading the repository from file when on start up.
(add-hook' after-init-hook 'bm-repository-load)

;; Restoring bookmarks when on file find.
(add-hook 'find-file-hooks 'bm-buffer-restore)
 
;; Saving bookmark data on killing and saving a buffer
(add-hook 'kill-buffer-hook 'bm-buffer-save)
(add-hook 'auto-save-hook 'bm-buffer-save)
(add-hook 'after-save-hook 'bm-buffer-save)
 
;; Saving the repository to file when on exit.
;; kill-buffer-hook is not called when emacs is killed, so we
;; must save all bookmarks first.
(add-hook 'kill-emacs-hook '(lambda nil
                              (bm-buffer-save-all)
                              (bm-repository-save)))

anything-bm-globalの本体

したのをloadなりevalなりした後に(anything 'anything-c-source-bm-global)をevalすると,ブックマークが一覧されるはずです.お試しあれ.
おかしかったら教えてください.

(defvar anything-c-source-bm-global-use-candidates-in-buffer
  '((name . "Global Bookmarks")
    (init . anything-c-bm-global-init)
    (candidates-in-buffer)
    (type . file-line))
  "show global bookmarks list.
Global means All bookmarks exist in `bm-repository'.

Needs bm.el.
http://www.nongnu.org/bm/")
;; (anything 'anything-c-source-bm-global-use-candidates-in-buffer)
(defvaralias 'anything-c-source-bm-global 'anything-c-source-bm-global-use-candidates-in-buffer)
;; (anything 'anything-c-source-bm-global)

(defun anything-c-bm-global-init ()
  "Init function for `anything-c-source-bm-global'."
  (when (require 'bm nil t)
    (with-no-warnings
      (let ((files bm-repository)
            (buf (anything-candidate-buffer 'global)))
        (dolist (file files)            ;ブックマークされてるファイルリストから,1つ取り出す
          (dolist (bookmark (cdr (assoc 'bookmarks (cdr file)))) ;1つのファイルに対して複数のブックマークがあるので
            (let ((position (cdr (assoc 'position bookmark))) ;position
                  (annotation (cdr (assoc 'annotation bookmark))) ;annotation
                  (file (car file))                               ;file名を取り出す
                  line
                  str)
              (setq str (with-current-buffer (find-file-noselect file) ;anything用の文字列にformat
                               (goto-char position)
                               (beginning-of-line)
                               (unless annotation
                                   (setq annotation ""))
                               (if (string= "" line)
                                   (setq line  "<EMPTY LINE>")
                                 (setq line (car (split-string (thing-at-point 'line) "[\n\r]"))))
                               (format "%s:%d: [%s]: %s\n" file (line-number-at-pos) annotation line)))
              (with-current-buffer buf (insert str)))))))))