;;; planner-bookmark.el --- bookmark URL support for the Emacs planner
;;

;; Copyright (C) 2004, 2005, 2008 Dryice Dong Liu.  All rights reserved.
;; Parts copyright (C) 2005, 2008 Free Software Foundation, Inc.

;; Keywords: emacs planner bookmark remember note
;; Author: Dryice Liu <dryice AT liu DOT com DOT cn>
;; Description: use bookmark.el in Emacs planner

;; This file is part of Planner.  It is not part of GNU Emacs.

;; Planner is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; Planner is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with Planner; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:
;;
;; Place planner-bookmark.el in your load path and add this to your .emacs:
;;
;;    (require 'planner-bookmark)
;;
;; Annotations will be of the form
;; [[bookmark://bookmark-name][bookmark-description]]
;; bookmark-description will use bookmark-annotation if available,
;; else bookmark-name will be used.
;;
;; Note this file advice `bookmark-set'. If you don't want to take a
;; note everytime you set a bookmark, set
;; `planner-bookmark-take-note-after-set-bookmark-flag' to nil

;;; CODE

(require 'planner)
(require 'bookmark)

;;; User variables

;;; Code:

(defgroup planner-bookmark nil
  "Bookmark URL support for planner.el."
  :prefix "planner-bookmark"
  :group 'planner)

(defcustom planner-bookmark-take-note-after-set-bookmark-flag
  t
  "Non-nil means show a `remember' buffer after setting a new bookmark."
  :type 'boolean
  :group 'planner-bookmark)

(defcustom planner-bookmark-add-note-title-flag
  t
  "Non-nil means add the bookmark name as the default note title"
  :type 'boolean
  :group 'planner-bookmark)

;;;; User variables stop here

(defadvice bookmark-set (after planner-bookmark activate)
  "Display a `remember' buffer for the bookmark.
This code is run only if
`planner-bookmark-take-note-after-set-bookmark-flag' is non-nil."
  (if (and planner-bookmark-take-note-after-set-bookmark-flag
           (condition-case nil
               (require 'remember)
             ('file-error nil)))
      ;; bookmark can take us where we want. we don't need two URLs
      (let ((remember-annotation-functions nil))
        (remember (concat 
		   (if planner-bookmark-add-note-title-flag
		       bookmark-current-bookmark)
		   "\n\n" (planner-bookmark-make-url
			   bookmark-current-bookmark))))))

;;;###autoload
(defun planner-bookmark-annotation-from-bookmark ()
  "If called from a bookmark buffer, return an annotation.
Suitable for use in `planner-annotation-functions'."
  (if (and (eq major-mode 'bookmark-bmenu-mode)
	   (bookmark-bmenu-check-position))
      (planner-bookmark-make-url (bookmark-bmenu-bookmark))))

(defun planner-bookmark-make-url (bookmark-name)
  "Make the bookmark URL by given BOOKMARK-NAME."
  (let ((bookmark-annotation (bookmark-get-annotation bookmark-name)))
    (if (string-equal bookmark-annotation "")
	(setq bookmark-annotation nil))
    (planner-make-link
     (concat "bookmark://" bookmark-name)
     (or bookmark-annotation bookmark-name)
     t)))

;;;###autoload
(defun planner-bookmark-browse-url (url)
  "If this is a bookmark URL, jump to it."
  (when (string-match "\\`bookmark:/?/?\\(.+\\)" url)
    (bookmark-jump (match-string 1 url))
    t))

(planner-add-protocol "bookmark:/?/?" 'planner-bookmark-browse-url nil)
(add-hook 'planner-annotation-functions 'planner-bookmark-annotation-from-bookmark)
(custom-add-option 'planner-annotation-functions 'planner-bookmark-annotation-from-bookmark)

(provide 'planner-bookmark)

;;; planner-bookmark.el ends here
