Skip to content

Instantly share code, notes, and snippets.

@mooz
Created January 15, 2012 14:30
Show Gist options
  • Save mooz/1616018 to your computer and use it in GitHub Desktop.
Save mooz/1616018 to your computer and use it in GitHub Desktop.
anything-c-source-bm-global
(require 'cl)
(defvar anything-c-source-bm-global
'((name . "Global Bookmarks")
(init . anything-c-bm-global-init)
(candidates-in-buffer)
(type . global-bm))
"Bookmarks exist in `bm-repository'.")
(define-anything-type-attribute 'global-bm
`((filtered-candidate-transformer anything-c-filtered-candidate-transformer-file-line)
(multiline)
(action ("Go to" . anything-c-action-file-line-goto)
("Remove Bookmark" . anything-c-action-bm-remove-annotation)
("Change Annotation" . anything-c-action-bm-change-annotation)))
"")
(defun anything-c-action-bm-helper-with-bookmark (file-line-content callback)
(when (stringp file-line-content)
(setq file-line-content
(cdr (anything-c-filtered-candidate-transformer-file-line-1
file-line-content))))
(destructuring-bind (file lineno content) file-line-content
(when file
(let ((newly-opened (null (get-file-buffer file))))
(with-current-buffer (find-file-noselect file)
(goto-line lineno)
(let ((bookmark (bm-bookmark-at (point))))
(when bookmark
(funcall callback bookmark file lineno content)
(bm-buffer-save)))
(when newly-opened
(kill-buffer (current-buffer))))))))
(defun* anything-c-action-bm-remove-annotation
(file-line-content &optional (find-file-function #'find-file))
(anything-c-action-bm-helper-with-bookmark file-line-content
(lambda (bookmark file lineno content)
(bm-bookmark-remove bookmark))))
(defun* anything-c-action-bm-change-annotation
(file-line-content &optional (find-file-function #'find-file))
(anything-c-action-bm-helper-with-bookmark file-line-content
(lambda (bookmark file lineno content)
(bm-bookmark-annotate
bookmark
(read-from-minibuffer (format "Annotation for %s: "
content
(overlay-get bookmark 'annotation)))))))
;; http://d.hatena.ne.jp/peccu/20100402/bmglobal
(defun anything-c-bm-global-init ()
"Init function for `anything-c-source-bm-global'."
(when (require 'bm nil t)
(with-no-warnings
(let ((anything-output-buffer (anything-candidate-buffer 'global)))
(loop for (filename . bm-info) in bm-repository
when (file-exists-p filename)
do
(with-temp-buffer
(insert-file-contents filename) ; insert-file-contents-literally is faster
(loop for bookmark in (cdr (assoc 'bookmarks bm-info))
with output-line
do
(goto-char (cdr (assoc 'position bookmark)))
(beginning-of-line)
(setq output-line
(format "%s:%d: [%s]: %s\n"
filename
(line-number-at-pos)
(or (cdr (assoc 'annotation bookmark)) "")
(car (split-string (thing-at-point 'line) "[\n\r]"))))
(with-current-buffer anything-output-buffer
(insert output-line)))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment