diff --git a/bibtexbrowser-documentation.wiki b/bibtexbrowser-documentation.wiki index d6335cb..93f651d 100644 --- a/bibtexbrowser-documentation.wiki +++ b/bibtexbrowser-documentation.wiki @@ -304,7 +304,16 @@ Add in ''bibtexbrowser.local.php'': ?> -====How to order a list of Bibtex entries?==== +====How to configure the order of Bibtex entries?==== +You can set the default order function in ''bibtexbrowser.local.php'': +
+<?php
+// can be @define('ORDER_FUNCTION','compare_bib_entry_by_title');
+define('ORDER_FUNCTION','compare_bib_entry_by_bibtex_order');
+?>
+
+ +Or programmatically:
 <?php
 $_GET['library']=1;
diff --git a/bibtexbrowser.php b/bibtexbrowser.php
index 3562d75..6556ee3 100755
--- a/bibtexbrowser.php
+++ b/bibtexbrowser.php
@@ -5,7 +5,7 @@ Feedback & Bug Reports: martin.monperrus@gnieh.org
 
 (C) 2012-2014 Github contributors
 (C) 2014 Markus Jochim
-(C) 2006-2014 Martin Monperrus
+(C) 2006-2015 Martin Monperrus
 (C) 2013 Matthieu Guillaumin
 (C) 2005-2006 The University of Texas at El Paso / Joel Garcia, Leonardo Ruiz, and Yoonsik Cheon
 This program is free software; you can redistribute it and/or
@@ -85,6 +85,7 @@ function bibtexbrowser_configure($key, $value) {
 // default order functions
 // Contract Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
 // can be @define('ORDER_FUNCTION','compare_bib_entry_by_title');
+// can be @define('ORDER_FUNCTION','compare_bib_entry_by_bibtex_order');
 @define('ORDER_FUNCTION','compare_bib_entry_by_year');
 @define('ORDER_FUNCTION_FINE','compare_bib_entry_by_month');
 
@@ -1026,6 +1027,9 @@ class BibEntry {
   /** The index in a list of publications (e.g. [1] Foo */
   var $index = '';
 
+  /** The location in the original bibtex file (set by addEntry) */
+  var $order = -1;
+
   /** returns a debug string representation */
   function __toString() {
     return $this->getType()." ".$this->getKey();
@@ -1773,6 +1777,13 @@ function compare_bib_entry_by_mtime($a, $b)
   return -($a->getTimestamp()-$b->getTimestamp());
 }
 
+/** compares two instances of BibEntry by order in Bibtex file
+ */
+function compare_bib_entry_by_bibtex_order($a, $b)
+{
+  return $a->order-$b->order;
+}
+
 /** compares two instances of BibEntry by year
  */
 function compare_bib_entry_by_year($a, $b)
@@ -3224,7 +3235,7 @@ class BibDataBase {
       // new entries:
       if (!isset($this->bibdb[$b->getKey()])) {
         //echo 'adding...
'; - $this->bibdb[$b->getKey()] = $b; + $this->addEntry($b); } // update entry else if (isset($this->bibdb[$b->getKey()]) && ($b->getText() !== $this->bibdb[$b->getKey()]->getText())) { @@ -3383,6 +3394,8 @@ class BibDataBase { if (!$entry->hasField('key')) { die('error: a bibliographic entry must have a key'); } + // we keep its insertion order + $entry->order = count($this->bibdb); $this->bibdb[$entry->getKey()] = $entry; }