Emacs menu v1.5

Michael Smith <[email protected]>
Newsgroups gmane.text.refdb.devel
Message-ID <20031210173435.GA1964@theThirdPoliceman>
Attached are a diff and also complete v1.5 refdb-menu.el file.

The main change is just in database selection. (Probably overkill since
it sounds like most people just use one db anyway, and don't switch, but
the idea was just to make it as unobtrusive as possible.)

Details:

  - Removed the "List Databases" menu items; all available databases are
    now shown on the "Select Database" submenu, with a check next to the
    currently selected database.

  - If you haven't specified a default database, the first time you try
    to do anything that requires database interaction, Emacs will prompt
    you for a database name using completion against the list of
    available databases

  - You can limit which databases show up in the menu and completion
    buffer by setting a different value for the
    refdb-menu-listdb-sql-regexp variable; e.g, set it to "refdb%", and
    only those databases that beging with "refdb" will be shown.

I also added an (incomplete) TODO list to the file.

Please try it and let me know if the new stuff works for you and/or if
it breaks anything.

Thanks,

  --Mike
refdb-menu.el.diff (text/plain, 14.4 KB)
? refdb-menu.el.diff
Index: refdb-menu.el
===================================================================
RCS file: /cvsroot/refdb/elisp/refdb-menu.el,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- refdb-menu.el	9 Dec 2003 02:13:30 -0000	1.4
+++ refdb-menu.el	10 Dec 2003 17:16:57 -0000	1.5
@@ -1,5 +1,5 @@
 ;;; refdb-menu.el --- Menu for RefDB interaction
-;; $Id: refdb-menu.el,v 1.4 2003/12/09 02:13:30 xmldoc Exp $
+;; $Id: refdb-menu.el,v 1.5 2003/12/10 17:16:57 xmldoc Exp $
 
 ;; Copyright (C) 2003 Michael Smith
 
@@ -7,9 +7,9 @@
 ;; Maintainer: Michael Smith <[email protected]>
 ;; Created: 2003-11-04
 ;; Version: 0.90
-;; Revision: $Revision: 1.4 $
-;; Date: $Date: 2003/12/09 02:13:30 $
-;; RCS Id: $Id: refdb-menu.el,v 1.4 2003/12/09 02:13:30 xmldoc Exp $
+;; Revision: $Revision: 1.5 $
+;; Date: $Date: 2003/12/10 17:16:57 $
+;; RCS Id: $Id: refdb-menu.el,v 1.5 2003/12/10 17:16:57 xmldoc Exp $
 ;; X-URL: http://refdb.sf.net/
 ;; Keywords: xml docbook tei bibliography
 
@@ -131,16 +131,9 @@
 ;;     Select the output type passed as an argument to the -t option
 ;;     for the 'refdbc -C getref' command when performing queries.
 ;;
-;;   List All Databases
-;;     Runs 'refdbc -C listdb' with no argument.
-;;
-;;   List Matching Databases
-;;     Prompts for a pattern and passes that as an argument to
-;;     'refdbc -C listdb'
-;;
 ;;   Select Database
 ;;     Allows you to change the database name passed via the -d option
-;;     to RefDB programs.
+;;     to refdbc commands.
 ;;
 ;;   Customize RefDB Menu
 ;;     User-friendly interface for setting options and
@@ -149,10 +142,47 @@
 ;; -------------------------------------------------------------------
 ;;; Bugs
 ;; -------------------------------------------------------------------
-;; Probably.  Still needs more testing.  Reports of a possibly
-;; reproducible crash with OS X (Panther)
+;; Probably.  Still needs more testing.
 ;;
 ;; -------------------------------------------------------------------
+;;; TODO
+;; -------------------------------------------------------------------
+
+;;   - Create Document submenu
+;;     Add a "Create Document" submenu, with menu items for each
+;;     supported document type. For example:
+;;     
+;;     Create Document
+;;       DocBook SGML
+;;       DocBook XML
+;;       TEI XML
+;;
+;;     Selecting a document type calls refdbnd with the appropriate
+;;     doctype value for that document type, then prompts for a
+;;     basename, pubtype, and style. Styles are selected via Emacs
+;;     completion, based on query into database. Current database
+;;     named is also automatically passed as a param to refdbnd.
+;;
+;;   - Generate Output submenu
+;;     Add a "Generate Output" submenu, with menu items for each
+;;     supported output format. For example:
+;;
+;;     Process Document
+;;       HTML
+;;       PDF
+;;       RTF
+;;
+;;     Selecting a menu item calls 'make' and passes it the
+;;     corresponding params.
+;;
+;;   - Syntax highlighting
+;;     Implement some kind of syntax highlighting contents of results
+;;     returned from database queries (Question: is this only for
+;;     highlighting of non-xml/sgml output? because highlighting of
+;;     xml/sgml could be handled just by selecting nXML or PSGML as
+;;     the mode for the output buffer.
+
+;; -------------------------------------------------------------------
 ;;; History:
 ;; -------------------------------------------------------------------
 ;;   2003-12-nn - first public release
@@ -171,6 +201,105 @@
 (require 'easymenu)
 (require 'easy-mmode)
 
+(add-hook 'refdb-menu-mode-hook
+	  ;;create the actual RefDB menu using easy-menu
+	  '(lambda ()
+	     (easy-menu-define
+	       refdb-menu-menu
+	       refdb-menu-mode-map
+	       "Easy menu command/variable for `refdb-menu-mode'."
+	       refdb-menu-definition
+	       )
+	     )
+	  )
+(add-hook 'refdb-menu-mode-hook 'refdb-menu-initialize-all-menus)
+(add-hook 'refdb-menu-mode-hook 'refdb-menu-make-selectdb-menu)
+(add-hook 'refdb-menu-mode-hook 'refdb-menu-initialize-database-list)
+
+;; *******************************************************************
+;;; User-customizable options, part 1
+;; *******************************************************************
+;;
+;; These are all needed at startup in order to build the
+;; Select Database submenu.
+
+(defcustom refdb-menu-database ""
+  "*Name of a RefDB database to use in RefDB commands.
+Passed to RefDB commands as the parameter for the -d option."
+  :type 'string
+  :group 'refdb-menu
+  )
+
+(defcustom refdb-menu-listdb-sql-regexp "%"
+  "Expression for limiting list of available databases.
+Passed as the argument for the 'refdbc -C listdb' command.  Leave at
+'%' to show all available databases.  Set to some other value to
+narrow the list.  For example, set to 'refdb%' to list just those
+databases whose names begin with the string 'refdb'."
+  :type 'string
+  :group 'refdb-menu)
+
+(defcustom refdb-menu-refdbc-program "refdbc"
+  "File name of the refdbc executable."
+  :type 'string
+  :group 'refdb-menu)
+
+(defcustom refdb-menu-refdbc-options ""
+  "RefDB global options; always included, regardless of subcommand."
+  :set-after '(refdb-menu-database-default)
+  :type 'string
+  :group 'refdb-menu)
+
+(defcustom refdb-menu-refdbc-program-and-options
+  (format "%s %s"
+	  refdb-menu-refdbc-program
+	  refdb-menu-refdbc-options
+	  )
+  "RefDB command along with global options."
+  :type 'string
+  :group 'refdb-menu)
+
+;; need to have this here to ensure that menu item will be in Help
+;; menu even if RefDB hasn't been loaded yet.
+(defcustom refdb-menu-suppress-from-help-menu-flag nil
+  "*Non-nil means suppress 'Show RefDB Menu' from Help menu.
+Set to non-nil ¥(on¥) to suppress, leave at nil ¥(off¥) to show."
+  :type 'boolean
+  :group 'refdb-menu
+  :require 'refdb-menu
+  )
+
+;; *******************************************************************
+;; end of user-customizable options, part 1
+;; *******************************************************************
+
+(defun refdb-menu-list-databases ()
+  "List databases returned by 'refdb -C listdb'."
+  (progn
+    (setq refdb-menu-selectdb-submenu-contents nil)
+    (split-string
+     (with-output-to-string
+       (with-current-buffer
+	   standard-output
+	 (call-process shell-file-name nil '(t nil) nil shell-command-switch
+		       (format "%s -C listdb %s"
+			       refdb-menu-refdbc-program-and-options
+			       refdb-menu-listdb-sql-regexp
+			       )
+		       )
+	 )
+       )
+     )
+    )
+  )
+
+(defun refdb-menu-initialize-database-list ()
+  "Initialize list of RefDB databases."
+  (message "Initializing RefDB database list...")
+  (setq refdb-menu-current-database-list (refdb-menu-list-databases))
+  (message "")
+  )
+
 (defvar refdb-menu-addref-menu-item
   ["Add Reference" (refdb-menu-addref) t]
   "RefDB menu item for adding references."
@@ -241,7 +370,7 @@
 	 :selected (eq refdb-menu-output-type 'scrn)]
 	["HTML"
 	 (setq refdb-menu-output-type 'html)
-	 :style toggle 
+	 :style toggle
 	 :selected (eq refdb-menu-output-type 'html)]
 	["XHTML"
          (setq refdb-menu-output-type 'xhtml)
@@ -280,27 +409,6 @@
   "RefDB select-output-type submenu."
   )
 
-(defvar refdb-menu-listdb-all-menu-item
-  ["List All Databases"
-   (refdb-menu-listdb-all-command)
-   t]
-  "RefDB menu item for listing all databases."
-  )
-
-(defvar refdb-menu-listdb-matching-menu-item
-  ["List Matching Databases..."
-   (call-interactively 'refdb-menu-listdb-matching-command)
-   t]
-  "RefDB menu item for listing DBs matching a certain pattern."
-  )
-
-(defvar refdb-menu-selectdb-menu-item
-  ["Select Database..."
-   (call-interactively 'refdb-menu-selectdb)
-   t]
-  "RefDB menu item for selecting a database as the current database."
-  )
-
 (defun refdb-menu-addref ()
   "Add references in the current region to the current RefDB database."
   (interactive)
@@ -334,11 +442,10 @@
 	    "*refdbc-output*" nil)
 	   )
 	(message "Error: No region marked.
-	      You must mark a region containing references to add.")
+You must mark a region containing references to add.")
 	)
-    (message "Error: No database specified.
-	      You must either select a database using the RefDB > Select Database
-	      menu, or set a value for the refdb-menu-database option.")
+    ;; else if no db specified, prompt to select from available dbs
+    (call-interactively 'refdb-menu-selectdb)
     )
   )
 
@@ -355,9 +462,8 @@
 	refdb-menu-current-field
 	value)
        "*refdbc-output*" nil)
-    (message "Error: No database specified.
-	      You must either select a database using the RefDB > Select Database
-menu, or set a value for the refdb-menu-database option.")
+    ;; else if no db specified, prompt to select from available dbs
+    (call-interactively 'refdb-menu-selectdb)
     )
   )
 
@@ -374,37 +480,32 @@
 	refdb-menu-output-type
 	searchstring)
        "*refdbc-output*" nil)
-    (message "Error: No database specified.
-You must either select a database using the RefDB > Select Database
-menu, or set a value for the refdb-menu-database option.")
+    ;; else if no db specified, prompt to select from available dbs
+    (call-interactively 'refdb-menu-selectdb)
     )
   )
 
-(defun refdb-menu-listdb-all-command ()
-  "List all available RefDB databases."
-  (shell-command
-  (format "%s -d %s -C listdb"
-	  refdb-menu-refdbc-program-and-options
-	  refdb-menu-database
-	  )
-   "*refdbc-output*" nil)
-  )
-
-(defun refdb-menu-listdb-matching-command (pattern)
-  "List RefDB databases matching PATTERN."
-  (interactive "sPattern: ")
-  (shell-command
-  (format "%s %s -C listdb %s"
-	  refdb-menu-refdbc-program-and-options
-	  refdb-menu-database
-	  pattern
-	  )
-   "*refdbc-output*" nil)
-  )
+(defun refdb-menu-alist-from-list (list)
+  "Make an alist from LIST by cons'ing elements with themselves."
+  (mapcar
+   (lambda (atom)
+     (cons atom atom))
+   list)
+)
 
 (defun refdb-menu-selectdb (database)
-  "Select DATABASE as the current RefDB database."
-  (interactive "sDatabase: ")
+  "Select DATABASE as the current RefDB database.
+If called interactively, prompt user with completion list of available
+databases."
+  (interactive
+   (list
+	  (completing-read
+	   "Database: "
+	   (refdb-menu-alist-from-list refdb-menu-current-database-list)
+	   nil t
+	   )
+	  )
+    )
   (setq refdb-menu-database database)
   )
 
@@ -448,17 +549,41 @@
   :prefix "refdb-menu-"
   )
 
+(defun refdb-menu-make-selectdb-menu ()
+  "Build the Select Database submenu."
+(setq refdb-menu-selectdb-submenu-contents
+  (cons "Select Database"
+	(progn
+	  (setq my-database-list refdb-menu-current-database-list)
+	  (setq refdb-menu-selectdb-submenu-items nil)
+	  (while my-database-list
+	    (setq current-database (car my-database-list))
+	    (setq refdb-menu-selectdb-submenu-items
+		  (nconc refdb-menu-selectdb-submenu-items
+			 (list
+			  (vector
+			   current-database
+			   `(refdb-menu-selectdb ,current-database)
+			   :style 'toggle
+			   :selected `(eq refdb-menu-database ,current-database)
+			   )
+			  )
+			 )
+		  )
+	    (setq my-database-list (cdr my-database-list))
+	    )
+	  refdb-menu-selectdb-submenu-items
+	  )
+	)
+  )
+)
+
 ;; *******************************************************************
-;;; User-customizable options
+;;; User-customizable options, part 2
 ;; *******************************************************************
 
-(defcustom refdb-menu-database ""
-  "*Name of a RefDB database to use in RefDB commands.
-Passed to RefDB commands as the parameter for the -d option."
-  :type 'string
-  :group 'refdb-menu
-  )
-
+(defun refdb-menu-initialize-all-menus ()
+  "Build all menus."
 (defcustom refdb-menu-input-type-risx 'use-mode-name
   "*Indicates whether RISX is used as the input type.
 Passed to the 'refdbc -C addref' command as the argument for the -t
@@ -474,9 +599,9 @@
   "*Specifies the default output type.
 Passed to the 'refdbc -C getref' command as the argument for the -t
 option."
-  :type '(choice (const :tag "Screen"        scrn)
-		 (const :tag "HTML  "        html)
-		 (const :tag "XHTML  "       xhtml)
+  :type '(choice (const :tag "Screen      "  scrn)
+		 (const :tag "HTML        "  html)
+		 (const :tag "XHTML       "  xhtml)
 		 (const :tag "DocBook SGML"  db31)
 		 (const :tag "DocBook XML "  db31x)
 		 (const :tag "TEX XML     "  teix)
@@ -487,17 +612,6 @@
   :group 'refdb-menu
 )
 
-(defcustom refdb-menu-refdbc-program "refdbc"
-  "File name of the refdbc executable."
-  :type 'string
-  :group 'refdb-menu)
-
-(defcustom refdb-menu-refdbc-options ""
-  "RefDB global options; always included, regardless of subcommand."
-  :set-after '(refdb-menu-database-default)
-  :type 'string
-  :group 'refdb-menu)
-
 (defcustom refdb-menu-refdbc-addref-options (format "")
   "RefDB options to include whenever the addref command is called."
   :type 'string
@@ -508,15 +622,6 @@
   :type 'string
   :group 'refdb-menu)
 
-(defcustom refdb-menu-refdbc-program-and-options
-  (format "%s %s"
-	  refdb-menu-refdbc-program
-	  refdb-menu-refdbc-options
-	  )
-  "RefDB command along with global options."
-  :type 'string
-  :group 'refdb-menu)
-
 (defcustom refdb-menu-getref-submenu-contents
   '(
    refdb-menu-search-by-author-menu-item
@@ -547,9 +652,7 @@
    refdb-menu-addref-menu-item
    refdb-menu-getref-submenu-definition
    refdb-menu-output-type-submenu-contents
-   refdb-menu-listdb-all-menu-item
-   refdb-menu-listdb-matching-menu-item
-   refdb-menu-selectdb-menu-item
+   refdb-menu-selectdb-submenu-contents
    refdb-menu-item-separator4
    refdb-menu-customize
    )
@@ -567,28 +670,10 @@
   :type '(repeat variable)
   )
 
-(defcustom refdb-menu-suppress-from-help-menu-flag nil
-  "*Non-nil means suppress 'Show RefDB Menu' from Help menu.
-Set to non-nil ¥(on¥) to suppress, leave at nil ¥(off¥) to show."
-  :type 'boolean
-  :group 'refdb-menu
-  :require 'refdb-menu
-  )
-
+)
 ;; *******************************************************************
-;; end of user-customizable options
+;; end of user-customizable options, part 2
 ;; *******************************************************************
-
-(add-hook 'refdb-menu-mode-hook
-	  ;;create the actual RefDB menu using easy-menu
-	  '(lambda ()
-	     (easy-menu-define
-	       refdb-menu-menu
-	       refdb-menu-mode-map
-	       "Easy menu command/variable for `refdb-menu-mode'."
-	       refdb-menu-definition)
-	     )
-	  )
 
 (define-key-after menu-bar-help-menu [refdb-menu-show]
   ;; add "Show RefDB Menu" item to Help menu
refdb-menu.el (text/plain, 21 KB)
;;; refdb-menu.el --- Menu for RefDB interaction
;; $Id: refdb-menu.el,v 1.5 2003/12/10 17:16:57 xmldoc Exp $

;; Copyright (C) 2003 Michael Smith

;; Author: Michael Smith <[email protected]>
;; Maintainer: Michael Smith <[email protected]>
;; Created: 2003-11-04
;; Version: 0.90
;; Revision: $Revision: 1.5 $
;; Date: $Date: 2003/12/10 17:16:57 $
;; RCS Id: $Id: refdb-menu.el,v 1.5 2003/12/10 17:16:57 xmldoc Exp $
;; X-URL: http://refdb.sf.net/
;; Keywords: xml docbook tei bibliography

;; This file is NOT part of GNU emacs.

;; This 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 2, or (at your option)
;; any later version.

;; This software 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 GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;; -------------------------------------------------------------------
;;; Compatibility
;; -------------------------------------------------------------------
;; 
;; -------------------------------------------------------------------
;;; Commentary
;;-------------------------------------------------------------------
;; This package adds a customizable RefDB menu to your menu bar.
;;
;; See the Details section below for details about the menu contents.
;;
;; -------------------------------------------------------------------
;;; Installation and Configuration
;; -------------------------------------------------------------------
;;
;; To install and use this package:
;;
;; 1. Either put this file into a directory that's already in your
;;    load path, or add whatever directory it's already in to your
;;    load path.  For example,
;;
;;      (add-to-list 'load-path "c:/my-xml-stuff/elisp/")
;;
;; 2. Add the following to the end of your .emacs file exactly as
;;    shown:
;;
;;      (require 'refdb-menu)
;;
;; 3. You should probably specify a database that will be used by
;;    default.  You can do that by adding the following to your .emacs,
;;    with DATABASE replaced by the name of the database to use.
;;
;;      (setq refdb-menu-database "DATABASE")
;;
;; After you restart Emacs, to view the menu at any time, either:
;;
;;   - choose Show RefDB Menu from the Emacs Help menu
;;       or
;;   - type "¥M-x refdb-menu-mode<return>" (on MS Windows, that's
;;     "Alt-x refdb-menu-mode" and then hit <Enter>)
;;
;; Showing the menu automatically
;; ------------------------------
;; There are a couple of ways you can make the menu automatically
;; show up (rather than needing to invoke it manually)
;; 
;;   - To show the menu by default, after you open a new file, so that
;;     it shows up no matter what buffer/mode you're in, add the
;;     following to your .emacs file:
;;
;;       (add-hook 'find-file-hooks 'refdb-menu-mode)
;;
;;   - To show the menu only when you're in a certain major mode (for
;;     example, only in nxml-mode)
;;
;;       (add-hook 'nxml-mode-hook 'refdb-menu-mode)
;;
;; To be able to show/hide the menu using a shorter keyboard shortcut,
;; you need to add something like the following to your .emacs file.
;;
;;   (define-key global-map "¥C-cb" 'refdb-menu-mode)
;;
;; That example will enable you to use the 'Ctrl-c b' keyboard to
;; show/hide the menu.
;;
;; -------------------------------------------------------------------
;;; Customization
;; -------------------------------------------------------------------
;; To customize the menu contents and behavior, either:
;;
;;   - choose Customize RefDB Menu from the menu itself
;;       or
;;   - type ¥M-x customize-group<return>refdb-menu<return>
;;
;; -------------------------------------------------------------------
;;; Details
;; -------------------------------------------------------------------
;; By default, `refdb-menu-mode' provides the following submenus:
;;
;;   Add Reference
;;   Search
;;   Select Output Type
;;   List All Databases
;;   List Matching Databases
;;   Select Database
;;   ----
;;   Customize RefDB Menu
;;
;; Here are brief descriptions of each submenu:
;;
;;   Add Reference
;;     Runs 'refdbc -C addref' on the current region.
;;
;;   Search
;;     Provides submenus for passing various queries to
;;     'refdbc -C getref'.
;;
;;   Select Output Type
;;     Select the output type passed as an argument to the -t option
;;     for the 'refdbc -C getref' command when performing queries.
;;
;;   Select Database
;;     Allows you to change the database name passed via the -d option
;;     to refdbc commands.
;;
;;   Customize RefDB Menu
;;     User-friendly interface for setting options and
;;     adding/removing/rearranging submenus.
;; 
;; -------------------------------------------------------------------
;;; Bugs
;; -------------------------------------------------------------------
;; Probably.  Still needs more testing.
;;
;; -------------------------------------------------------------------
;;; TODO
;; -------------------------------------------------------------------

;;   - Create Document submenu
;;     Add a "Create Document" submenu, with menu items for each
;;     supported document type. For example:
;;     
;;     Create Document
;;       DocBook SGML
;;       DocBook XML
;;       TEI XML
;;
;;     Selecting a document type calls refdbnd with the appropriate
;;     doctype value for that document type, then prompts for a
;;     basename, pubtype, and style. Styles are selected via Emacs
;;     completion, based on query into database. Current database
;;     named is also automatically passed as a param to refdbnd.
;;
;;   - Generate Output submenu
;;     Add a "Generate Output" submenu, with menu items for each
;;     supported output format. For example:
;;
;;     Process Document
;;       HTML
;;       PDF
;;       RTF
;;
;;     Selecting a menu item calls 'make' and passes it the
;;     corresponding params.
;;
;;   - Syntax highlighting
;;     Implement some kind of syntax highlighting contents of results
;;     returned from database queries (Question: is this only for
;;     highlighting of non-xml/sgml output? because highlighting of
;;     xml/sgml could be handled just by selecting nXML or PSGML as
;;     the mode for the output buffer.

;; -------------------------------------------------------------------
;;; History:
;; -------------------------------------------------------------------
;;   2003-12-nn - first public release
;;
;; -------------------------------------------------------------------
;;; Acknowledgments
;; -------------------------------------------------------------------
;;

;;; Code:

;; *******************************************************************
;;; Initialization
;; *******************************************************************

(require 'easymenu)
(require 'easy-mmode)

(add-hook 'refdb-menu-mode-hook
	  ;;create the actual RefDB menu using easy-menu
	  '(lambda ()
	     (easy-menu-define
	       refdb-menu-menu
	       refdb-menu-mode-map
	       "Easy menu command/variable for `refdb-menu-mode'."
	       refdb-menu-definition
	       )
	     )
	  )
(add-hook 'refdb-menu-mode-hook 'refdb-menu-initialize-all-menus)
(add-hook 'refdb-menu-mode-hook 'refdb-menu-make-selectdb-menu)
(add-hook 'refdb-menu-mode-hook 'refdb-menu-initialize-database-list)

;; *******************************************************************
;;; User-customizable options, part 1
;; *******************************************************************
;;
;; These are all needed at startup in order to build the
;; Select Database submenu.

(defcustom refdb-menu-database ""
  "*Name of a RefDB database to use in RefDB commands.
Passed to RefDB commands as the parameter for the -d option."
  :type 'string
  :group 'refdb-menu
  )

(defcustom refdb-menu-listdb-sql-regexp "%"
  "Expression for limiting list of available databases.
Passed as the argument for the 'refdbc -C listdb' command.  Leave at
'%' to show all available databases.  Set to some other value to
narrow the list.  For example, set to 'refdb%' to list just those
databases whose names begin with the string 'refdb'."
  :type 'string
  :group 'refdb-menu)

(defcustom refdb-menu-refdbc-program "refdbc"
  "File name of the refdbc executable."
  :type 'string
  :group 'refdb-menu)

(defcustom refdb-menu-refdbc-options ""
  "RefDB global options; always included, regardless of subcommand."
  :set-after '(refdb-menu-database-default)
  :type 'string
  :group 'refdb-menu)

(defcustom refdb-menu-refdbc-program-and-options
  (format "%s %s"
	  refdb-menu-refdbc-program
	  refdb-menu-refdbc-options
	  )
  "RefDB command along with global options."
  :type 'string
  :group 'refdb-menu)

;; need to have this here to ensure that menu item will be in Help
;; menu even if RefDB hasn't been loaded yet.
(defcustom refdb-menu-suppress-from-help-menu-flag nil
  "*Non-nil means suppress 'Show RefDB Menu' from Help menu.
Set to non-nil ¥(on¥) to suppress, leave at nil ¥(off¥) to show."
  :type 'boolean
  :group 'refdb-menu
  :require 'refdb-menu
  )

;; *******************************************************************
;; end of user-customizable options, part 1
;; *******************************************************************

(defun refdb-menu-list-databases ()
  "List databases returned by 'refdb -C listdb'."
  (progn
    (setq refdb-menu-selectdb-submenu-contents nil)
    (split-string
     (with-output-to-string
       (with-current-buffer
	   standard-output
	 (call-process shell-file-name nil '(t nil) nil shell-command-switch
		       (format "%s -C listdb %s"
			       refdb-menu-refdbc-program-and-options
			       refdb-menu-listdb-sql-regexp
			       )
		       )
	 )
       )
     )
    )
  )

(defun refdb-menu-initialize-database-list ()
  "Initialize list of RefDB databases."
  (message "Initializing RefDB database list...")
  (setq refdb-menu-current-database-list (refdb-menu-list-databases))
  (message "")
  )

(defvar refdb-menu-addref-menu-item
  ["Add Reference" (refdb-menu-addref) t]
  "RefDB menu item for adding references."
  )

(defvar refdb-menu-search-by-author-menu-item
  ["Author..." (list
	     (setq refdb-menu-current-prompt "Author")
	     (setq refdb-menu-current-field "AU")
	     (call-interactively 'refdb-menu-getref-by-field)
	     )
   t]
  "RefDB search-by-author menu item."
  )

(defvar refdb-menu-search-by-title-menu-item
  ["Title..." (list
	     (setq refdb-menu-current-prompt "Title")
	     (setq refdb-menu-current-field "TI")
	     (call-interactively 'refdb-menu-getref-by-field)
	     )
   t]
  "RefDB search-by-title menu item."
  )

(defvar refdb-menu-search-by-keyword-menu-item
  ["Keyword..." (list
	     (setq refdb-menu-current-prompt "Keyword")
	     (setq refdb-menu-current-field "KW")
	     (call-interactively 'refdb-menu-getref-by-field)
	     )
   t]
  "RefDB search-by-keyword menu item."
  )

(defvar refdb-menu-search-by-id-menu-item
  ["ID..." (list
	     (setq refdb-menu-current-prompt "ID")
	     (setq refdb-menu-current-field "ID")
	     (call-interactively 'refdb-menu-getref-by-field)
	     )
   t]
  "RefDB search-by-id menu item."
  )

(defvar refdb-menu-search-by-citekey-menu-item
  ["Citation Key..." (list
	     (setq refdb-menu-current-prompt "Citation Key")
	     (setq refdb-menu-current-field "CK")
	     (call-interactively 'refdb-menu-getref-by-field)
	     )
   t]
  "RefDB search-by-citation-key menu item."
  )

(defvar refdb-menu-search-by-advanced-search-menu-item
  ["Advanced Search..."
   (call-interactively 'refdb-menu-getref-by-advanced-search)
   t]
  "RefDB advanced-search menu item."
  )

(defvar refdb-menu-output-type-submenu-contents
  (list "Select Output Type"
	["Screen"
	 (setq refdb-menu-output-type 'scrn)
	 :style toggle
	 :selected (eq refdb-menu-output-type 'scrn)]
	["HTML"
	 (setq refdb-menu-output-type 'html)
	 :style toggle
	 :selected (eq refdb-menu-output-type 'html)]
	["XHTML"
         (setq refdb-menu-output-type 'xhtml)
	 :style toggle
	 :selected (eq refdb-menu-output-type 'xhtml)]
	["DocBook SGML"
	 (setq refdb-menu-output-type 'db31)
	 :style toggle
	 :selected (eq refdb-menu-output-type 'db31)]
	["DocBook XML"
	 (setq refdb-menu-output-type 'db31x)
	 :style toggle
	 :selected (eq refdb-menu-output-type 'db31x)
	 ]
	["TEI XML"
	 (setq refdb-menu-output-type 'teix)
	 :style toggle
	 :selected (eq refdb-menu-output-type 'teix)
	 ]
	["BibTeX"
	 (setq refdb-menu-output-type 'bibtex)
	 :style toggle
	 :selected (eq refdb-menu-output-type 'bibtex)
	 ]
	["RIS"
	 (setq refdb-menu-output-type 'ris)
	 :style toggle
 	 :selected (eq refdb-menu-output-type 'ris)
	 ]
	["RISX"
	 (setq refdb-menu-output-type 'risx)
	 :style toggle
	 :selected (eq refdb-menu-output-type 'risx)
	 ]
	)
  "RefDB select-output-type submenu."
  )

(defun refdb-menu-addref ()
  "Add references in the current region to the current RefDB database."
  (interactive)
  (if (not (eq (length refdb-menu-database) 0))
      (if (mark)
	  (shell-command-on-region
	   (point) (mark)
	   (format
	    "%s -d %s -t %s -C addref"
	    refdb-menu-refdbc-program-and-options
	    refdb-menu-database
	    ;; check to see what value for input type should be
	    (if (and refdb-menu-input-type-risx
		     (not (eq refdb-menu-input-type-risx t)))
		;; unless user has specified that input type should
		;; never be RISX, or specified that input type should
		;; always be RISX, check to see what the name of the
		;; major mode is, and if the major mode name contains
		;; 'xml' or 'sgml', set the input type to
		;; RISX. Otherwise, set the input type to RIS
		(if
		    (not (null
			  (string-match
			   "sgml¥¥|xml"
			   (downcase (symbol-name major-mode)))))
		    (quote "risx")
		  (quote "ris")
		  )
	      (quote "ris")
	      )
	    "*refdbc-output*" nil)
	   )
	(message "Error: No region marked.
You must mark a region containing references to add.")
	)
    ;; else if no db specified, prompt to select from available dbs
    (call-interactively 'refdb-menu-selectdb)
    )
  )

(defun refdb-menu-getref-by-field (value)
  "Display all RefDB datasets that match the specified field and VALUE."
  (interactive (list (read-string (format "%s: " refdb-menu-current-prompt))))
  (if (not (eq (length refdb-menu-database) 0))
      (shell-command
       (format
	"%s -d %s -t %s -C getref :%s:=%s"
	refdb-menu-refdbc-program-and-options
	refdb-menu-database
	refdb-menu-output-type
	refdb-menu-current-field
	value)
       "*refdbc-output*" nil)
    ;; else if no db specified, prompt to select from available dbs
    (call-interactively 'refdb-menu-selectdb)
    )
  )

(defun refdb-menu-getref-by-advanced-search (searchstring)
  "Display all RefDB datasets that match the SEARCHSTRING."
  (interactive "sSearch string: ")
  (if (not (eq (length refdb-menu-database) 0))
      (shell-command
       (point) (mark)
       (format
	"%s -d %s -t %s -C getref %s"
	refdb-menu-refdbc-program-and-options
	refdb-menu-database
	refdb-menu-output-type
	searchstring)
       "*refdbc-output*" nil)
    ;; else if no db specified, prompt to select from available dbs
    (call-interactively 'refdb-menu-selectdb)
    )
  )

(defun refdb-menu-alist-from-list (list)
  "Make an alist from LIST by cons'ing elements with themselves."
  (mapcar
   (lambda (atom)
     (cons atom atom))
   list)
)

(defun refdb-menu-selectdb (database)
  "Select DATABASE as the current RefDB database.
If called interactively, prompt user with completion list of available
databases."
  (interactive
   (list
	  (completing-read
	   "Database: "
	   (refdb-menu-alist-from-list refdb-menu-current-database-list)
	   nil t
	   )
	  )
    )
  (setq refdb-menu-database database)
  )

(easy-mmode-define-minor-mode refdb-menu-mode
  "Minor mode that displays a menu for RefDB interaction."
  ;; Initial value is nil.
  nil
  ;; No indicator for the mode line.
  nil
  ;; Make empty default keymap;
  '( ("" . nil)))

(defvar refdb-menu-item-separator1
  ["--" t]
  "Separator between command `refdb-menu-mode' menu items."
  )
(defvar refdb-menu-item-separator2
  ["---" t]
  "Separator between command `refdb-menu-mode' menu items."
  )
(defvar refdb-menu-item-separator3
  ["----" t]
  "Separator between command `refdb-menu-mode' menu items."
  )
(defvar refdb-menu-item-separator4
  ["-----" t]
  "Separator between command `refdb-menu-mode' menu items."
  )

(defvar refdb-menu-customize
  ["Customize RefDB Menu..."
   (customize-group 'refdb-menu) t]
  "Customize submenu for command `refdb-menu-mode'."
  )

(defgroup refdb-menu nil
  "RefDB menu."
  :group 'help
  :link '(emacs-commentary-link "refdb-menu.el")
  :link '(url-link "http://refdb.sf.net/")
  :prefix "refdb-menu-"
  )

(defun refdb-menu-make-selectdb-menu ()
  "Build the Select Database submenu."
(setq refdb-menu-selectdb-submenu-contents
  (cons "Select Database"
	(progn
	  (setq my-database-list refdb-menu-current-database-list)
	  (setq refdb-menu-selectdb-submenu-items nil)
	  (while my-database-list
	    (setq current-database (car my-database-list))
	    (setq refdb-menu-selectdb-submenu-items
		  (nconc refdb-menu-selectdb-submenu-items
			 (list
			  (vector
			   current-database
			   `(refdb-menu-selectdb ,current-database)
			   :style 'toggle
			   :selected `(eq refdb-menu-database ,current-database)
			   )
			  )
			 )
		  )
	    (setq my-database-list (cdr my-database-list))
	    )
	  refdb-menu-selectdb-submenu-items
	  )
	)
  )
)

;; *******************************************************************
;;; User-customizable options, part 2
;; *******************************************************************

(defun refdb-menu-initialize-all-menus ()
  "Build all menus."
(defcustom refdb-menu-input-type-risx 'use-mode-name
  "*Indicates whether RISX is used as the input type.
Passed to the 'refdbc -C addref' command as the argument for the -t
option.  If t, RISX is always used.  If nil, RISX is never used.  If
non-t and non-nil, use RISX if the name of the buffer's major mode
contains 'xml' or 'sgml'."
  :type '(choice (const :tag "always" t) (const :tag "never" nil)
		 (const :tag "use-mode-name" use-mode-name))
  :group 'refdb-menu
)

(defcustom refdb-menu-output-type 'scrn
  "*Specifies the default output type.
Passed to the 'refdbc -C getref' command as the argument for the -t
option."
  :type '(choice (const :tag "Screen      "  scrn)
		 (const :tag "HTML        "  html)
		 (const :tag "XHTML       "  xhtml)
		 (const :tag "DocBook SGML"  db31)
		 (const :tag "DocBook XML "  db31x)
		 (const :tag "TEX XML     "  teix)
		 (const :tag "BibTeX      "  bibtex)
		 (const :tag "RIS         "  ris)
		 (const :tag "RISX        "  risx)
		 )
  :group 'refdb-menu
)

(defcustom refdb-menu-refdbc-addref-options (format "")
  "RefDB options to include whenever the addref command is called."
  :type 'string
  :group 'refdb-menu)

(defcustom refdb-menu-refdbc-getref-options (format "")
  "RefDB options to include whenever the getref command is called."
  :type 'string
  :group 'refdb-menu)

(defcustom refdb-menu-getref-submenu-contents
  '(
   refdb-menu-search-by-author-menu-item
   refdb-menu-search-by-title-menu-item
   refdb-menu-search-by-keyword-menu-item
   refdb-menu-search-by-id-menu-item
   refdb-menu-search-by-citekey-menu-item
   refdb-menu-search-by-advanced-search-menu-item
   )
  "*Contents of 'Search' submenu for the command `refdb-menu-mode' menu.
Customize this to add/remove/rearrange submenus."
  :set (lambda (sym val)
         (setq refdb-menu-getref-submenu-contents val)
	 (setq refdb-menu-getref-submenu-definition
	       (cons "Search"
		     ;; turn quoted refdb-menu-contents value back into a real list
		     ;; thanks, sachac :)
		     (mapcar (lambda (item) (if (symbolp item) (eval item) item)) val)
		     )
	       )
	 )
  :group 'refdb-menu
  :type '(repeat variable)
  )

(defcustom refdb-menu-contents
  '(
   refdb-menu-addref-menu-item
   refdb-menu-getref-submenu-definition
   refdb-menu-output-type-submenu-contents
   refdb-menu-selectdb-submenu-contents
   refdb-menu-item-separator4
   refdb-menu-customize
   )
  "*Contents of command `refdb-menu-mode' menu.
Customize this to add/remove/rearrange submenus."
  :set (lambda (sym val)
         (setq refdb-menu-contents val)
	 (setq refdb-menu-definition
	       (cons "RefDB"
		     (mapcar (lambda (item) (if (symbolp item) (eval item) item)) val)
		     )
	       )
	 )
  :group 'refdb-menu
  :type '(repeat variable)
  )

)
;; *******************************************************************
;; end of user-customizable options, part 2
;; *******************************************************************

(define-key-after menu-bar-help-menu [refdb-menu-show]
  ;; add "Show RefDB Menu" item to Help menu
  '(menu-item "Show RefDB Menu" refdb-menu-mode
	      :help "Toggle display of RefDB Menu"
	      :button (:toggle . (and (boundp 'refdb-menu-mode) refdb-menu-mode))
	      ;; hide if 'suppress' flag is set
	      :visible (not refdb-menu-suppress-from-help-menu-flag)
	      )
  nil)

(provide 'refdb-menu)

;;; refdb-menu.el ends here
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.