(planner-create-note): Fix for #10196, make sure that newly created notes get the...
[planner-el.git] / planner-bibtex.el
blob9571692353e355e7545657d2e7cd9f725f8c5d6a
1 ;; planner-bibtex.el -- BibTeX support for Planner
3 ;; Copyright (c) 2004, 2008 James Clarke <james@jamesclarke.info>
4 ;; Parts copyright (c) 2004, 2008 Jody Klymak <jklymak@ucsd.edu>
5 ;; Parts copyright (C) 2005, 2008 Free Software Foundation, Inc.
7 ;; Author: James Clarke <james@jamesclarke.info>
9 ;; This file is part of Planner. It is not part of GNU Emacs.
11 ;; Planner is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; Planner is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with Planner; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Provides bibtex annotations for Planner mode. Handles the two
29 ;; versions of bibtex.el I have encountered, those bundled with
30 ;; emacs-21.3 and emacs-21.2.
32 ;;; Contributors:
34 ;; Jody Klymak co-wrote this.
36 ;; Jim Ottaway fixed several bugs.
38 ;; Wei-Hao Lin fixed a typo.
40 (require 'planner)
41 (require 'bibtex)
43 ;; This function is not present in old bibtex.els so I've added it
44 ;; here as I cannot find alternative in old bibtex.els.
46 (defvar planner-bibtex-separator ":" "Character separating filename and label.")
47 (defvar planner-bibtex-regexp
48 (concat "\\`bibtex:\\([^"
49 planner-bibtex-separator "\n]+\\)"
50 planner-bibtex-separator "\\(.+\\)") "Regexp matching URLs.")
52 ;;; Code:
53 (defun planner-bibtex-key-in-head (&optional empty)
54 "Extract BibTeX key in head. Return optional arg EMPTY if key is empty."
55 (if (fboundp 'bibtex-key-in-head)
56 (bibtex-key-in-head empty)
57 (if (match-beginning bibtex-key-in-head)
58 (buffer-substring-no-properties (match-beginning bibtex-key-in-head)
59 (match-end bibtex-key-in-head))
60 empty)))
62 ;; makes a nice bibtex url with the title upto a terminator for the
63 ;; link name. For emacs 21.3 (updated bibtex.el)
64 ;;;###autoload
65 (defun planner-bibtex-annotation-new ()
66 "Return an annotation for the current bibtex entry."
67 (when (planner-derived-mode-p 'bibtex-mode)
68 (save-excursion
69 (let ((titlestring
70 (bibtex-autokey-get-field
71 "title" bibtex-autokey-titleword-change-strings)))
72 (if (string-match "[\.:!\?;]" titlestring)
73 (setq titlestring (substring titlestring 0 (match-beginning 0))))
74 (bibtex-beginning-of-entry)
75 (re-search-forward bibtex-entry-maybe-empty-head nil t)
76 (planner-make-link
77 (concat "bibtex:" (buffer-file-name) planner-bibtex-separator
78 (planner-bibtex-key-in-head))
79 titlestring
80 t)))))
82 ;; makes a nice bibtex url with the title up to a terminator for the
83 ;; link name. For emacs 21.2 (older bibtex)
84 ;;;###autoload
85 (defun planner-bibtex-annotation-old ()
86 "Return the filename on the current line in dired."
87 (when (planner-derived-mode-p 'bibtex-mode)
88 (save-excursion
89 (let* ((min (bibtex-beginning-of-entry))
90 (max (bibtex-end-of-entry))
91 (titlestr (planner-replace-regexp-in-string
92 "[ \t\n]+" " "
93 (bibtex-autokey-get-titlestring min max))))
94 (bibtex-beginning-of-entry)
95 (re-search-forward bibtex-entry-maybe-empty-head nil t)
96 (planner-make-link
97 (concat "bibtex:" (buffer-file-name) planner-bibtex-separator
98 (planner-bibtex-key-in-head))
99 titlestr
100 t)))))
102 ;;decide which version of the function to use
103 (if (fboundp 'bibtex-autokey-get-field)
104 (defalias 'planner-bibtex-annotation
105 'planner-bibtex-annotation-new)
106 (defalias 'planner-bibtex-annotation
107 'planner-bibtex-annotation-old))
109 ;;;###autoload
110 (defun planner-bibtex-browse-url (url)
111 "If this is a Bibtex URL, jump to it."
112 (when (string-match planner-bibtex-regexp url)
113 (let ((file (match-string 1 url))
114 (label (match-string 2 url)))
115 (find-file file)
116 (widen)
117 (goto-char (point-min))
118 (search-forward (concat "{" label ",")))))
120 (planner-add-protocol "bibtex:" 'planner-bibtex-browse-url nil)
121 (add-hook 'planner-annotation-functions 'planner-bibtex-annotation)
123 (planner-update-wiki-project)
125 (provide 'planner-bibtex)
127 ;;; planner-bibtex.el ends here