Alejandro Aguilar Sierra wrote:
> Could you please send the patch so we can evaluate whether it could be
> incorporated to 1.0.2?
Here it is!
I should add that I have more experience programming in C, than in C++
so you are welcome to have a look at the code.

The code adds a method to the InsetBibtex class that can be used to
retrieve the keys from the corresponding bibtex databases. It implements
a very simple bibtex parser, that simply scans the file for lines
beginning with @, and then parses that line.

Every time the Citation pop up is opened this new method in InsetBibtex
is called and the keys inserted in the combobox. This may seem like a
waste, but it is the best method I can think of now and it allows you to
edit the bibtex file and see the changes reflected in the keys listed
immediately. On a Pentium 100 notebook it is not a perfomance problem,
even with a big annotated bibtex file of about 100 K.

I have chosen to let the code fail silently, if the file can't be found.
That means, you can type your text and insert references, even if your
bibtex file is not present (e.g. on a notebook).

One last thing: The code searches for bibtex files in the same directory
as the lyx file and then in the directories listed in the BIBINPUTS
environment variable. It does NOT search in the standard texmf-tree, so
if your bibtex files are there, you will have to add the path to
BIBINPUTS.

I think that's all. Have a look at the code and see if you can use it.

Greetings

Jesper
Index: lyx-1_0_x/src/insetbib.C
===================================================================
RCS file: /usr/local/lyxsrc/cvsroot/lyx-1_0_x/src/insetbib.C,v
retrieving revision 1.6
diff -u -r1.6 insetbib.C
--- insetbib.C  1999/02/24 04:35:25     1.6
+++ insetbib.C  1999/04/05 07:59:44
@@ -368,6 +368,64 @@
        return 2;
 }
 
+// This method returns a comma separated list of Bibtex entries
+LString InsetBibtex::getKeys() const
+{
+       // This hack is copied from InsetBibtex::Latex.
+       // Is it still needed?
+       if (!owner) {
+               owner = current_view->currentBuffer();
+       }
+
+       // We need to create absolute path names for bibliographies
+       // First look for bib-file in same directory as document,
+       // then in all directories listed in environment variable 
+       // BIBINPUTS
+       LString bibfiles, linebuf, tmp, keys;
+       bibfiles = getContents();
+       bibfiles.split(tmp, ',');
+       while(!tmp.empty()) {
+               if (IsFileReadable(MakeAbsPath(tmp,owner->filepath)+".bib"))
+                       tmp = MakeAbsPath(tmp,owner->filepath)+".bib";
+               else
+                       tmp = FileOpenSearch(getenv("BIBINPUTS"),tmp,"bib");
+
+               // If we didn't find a matching file name just fail silently
+               if (!tmp.empty()) {
+      
+                       // This is a _very_ simple parser for Bibtex database files.
+                       // All it does is to look for lines starting in @ and not
+                       // being @preamble and @string entries.
+                       // It does NOT do any syntax checking!
+                       FilePtr file(tmp,FilePtr::read);
+                       char c;
+
+                       while (! feof(file)) {
+                               c = fgetc(file);
+
+                               // At end of each line check if line begins with '@'
+                               if ( c == '\n') {
+                                       if ( linebuf.prefixIs("@") ) {
+                                               linebuf.subst('{','(');
+                                               linebuf.split(tmp,'(');
+                                               tmp.lowercase();
+                                               if ( ! tmp.prefixIs("@string") && 
+!tmp.prefixIs("@preamble") ) {
+                                                       linebuf.split(tmp,',');
+                                                       if (!tmp.empty())
+                                                               keys +=tmp.strip()+",";
+                                               }
+                                       }
+                                       linebuf.clean();
+                               } else {
+                                       linebuf += c;
+                               }
+                       }
+               }
+               // Get next file name
+               bibfiles.split(tmp, ',');
+       }
+       return keys;
+}
 
 // BibTeX should have its own dialog. This is provisional.
 void InsetBibtex::Edit(int, int)
@@ -439,29 +497,34 @@
        }
        
        if (combox->empty()) {
-               // might be using bibtex instead.
-               // Hmmm... how can I be sure?? ARRae
+               // Might be using bibtex instead.
+               // Search for Bibtex inset
                par = current_view->currentBuffer()->paragraph;
                while (par) {
-                       Inset *inset;
+                       InsetBibtex *inset;
                        int pos = -1;
-                       while ((inset = par->ReturnNextInsetPointer(pos))) {
-                               LString tmp;
-                               inset->Latex(tmp,0);
-                               if (tmp.contains("\\cite")) {
+                       while ((inset = (InsetBibtex*) 
+par->ReturnNextInsetPointer(pos))) {
+                               LString tmp,bibkeys;
+                               inset->Latex(tmp,0);
+                               if (tmp.contains("\\bibliography")) {
                                        // while not the nicest test it is
                                        // about the only one that currently
                                        // works.  I'd certainly be 
                                        // interested in an alternative.
-                                       combox->addto(((InsetCitation*)inset)
-                                                     ->getContents().c_str());
-                               }
-                               ++pos;
+                                       bibkeys = inset->getKeys();
+                                       bibkeys.split(tmp,',');
+                                       while (!tmp.empty()) {
+                                               combox->addto(tmp.c_str());
+                                               bibkeys.split(tmp,',');
+                                       }
+                               }
+                               pos++;
                        }
-                       par = par->next;
+                       par = par->next;
                }
        }
 }
+
 
 
 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
Index: lyx-1_0_x/src/insetbib.h
===================================================================
RCS file: /usr/local/lyxsrc/cvsroot/lyx-1_0_x/src/insetbib.h,v
retrieving revision 1.2
diff -u -r1.2 insetbib.h
--- insetbib.h  1999/02/19 16:45:23     1.2
+++ insetbib.h  1999/04/05 07:59:45
@@ -113,6 +113,8 @@
        ///
        int Latex(LString &file, signed char fragile);
        ///
+       LString getKeys() const;
+       ///
        unsigned char Editable() const {
                return 1;
        }

Reply via email to