   :type 'string
   :group 'gnugol)
 
-(defcustom gnugol-default-opts "-o org"
-  "Shell command to invoke gnugol."
+(defcustom gnugol-default-opts nil
+  "Additional default options for gnugol."
   :type 'string
   :group 'gnugol)
 
 (defcustom gnugol-default-engine nil
-  "Default search engine backend for gnugol."
+  "Default search engine backend for gnugol. Presently supported are:
+  google: Full support (license key recommended)
+  bing: (with a license key)
+  dummy: (useful for testing)
+  credits: various options like about, licenses etc."
   :type 'string
   :group 'gnugol)
 

  :type 'integer
  :group 'gnugol)

(defcustom gnugol-default-safe-mode 1
  "Default safe mode to search in: 0 = none, 1 = moderate, 2 = active"
  :type 'integer
  :group 'gnugol)

(defcustom gnugol-default-base-output-format "org"
  "Default output format for gnugol."
  :type 'string
  :group 'gnugol)

(defcustom gnugol-default-output-buffer "*gnugol*"
  "Output buffer. Set this to ~/org/gnugol_history.org if you want to keep your history."
  :type 'string
  :group 'gnugol)


;;        set of keywords rather than call gnugol
;;        (if (search-forward (concat "[[Search: " str )) () (gnugol-cmd str)))?
;; FIXME: Sanitize the shell arguments to prevent abuse !! For example no CRs
;;        regexp? I'm *nearly* certain that url escaping and shell quoting the args
;;        is good enough.
;; FIXME: actually, going to the 4th char in on the title would work best
;; FIXME: make gnugol opts be local
;; FIXME: CNTR-U should set the position
;; FIXME: a query with the same keywords as the previous should fetch more 
;;        results (maybe)

(defun gnugol (str)
  "Search the web via gnugol, bring up results in org buffer."
  (interactive "sSearch: ")
  (if (< (length str) gnugol-search-maxlen)
      (let (newbuffer)
	(setq gnugol-opts (concat (if gnugol-default-opts (concat gnugol-default-opts) ()) 
				  (if gnugol-default-nresults (concat " -n " (int-to-string gnugol-default-nresults) ()))
				  (if gnugol-default-engine (concat " -e " gnugol-default-engine))
				  (if gnugol-default-output-format (concat " -o " gnugol-default-output-format) ())
				  (if gnugol-default-header (concat " -H " gnugol-default-header) ())
				  (if gnugol-default-footer (concat " -F " gnugol-default-footer) ())
;;				  (if gnugol-default-safe-mode (concat " -S " gnugol-default-safe-mode) ())
				  (if gnugol-default-input-language (concat " -l " gnugol-default-input-language) ())
				  (if gnugol-default-output-language (concat " -L " gnugol-default-output-language) ())
				  ))	
	(setq gnugol-full-cmd  (concat gnugol-cmd " " gnugol-opts " -U -- " 
				       (shell-quote-argument 
					(gnugol-url-encode str))))
;; FIXME: Open the file, or reload the file if not a *gnugol* buffer
	(setq newbuffer (get-buffer-create gnugol-default-output-buffer))
	(set-buffer newbuffer)
;; FIXME: Set mode of buffer based on the extension
	(org-mode)
	(goto-char (point-min))
	;; FIXME what we want to do is something like this but I'm getting it wrong
	;; (if (search-forward (concat "[Search: " str "]")) () 
;;	(message "%s" gnugol-full-cmd)	
	(save-excursion 
	  (insert-string (concat "* [[gnugol: " str "][Search: " str "]]\n"))
	  (insert 
	   (shell-command-to-string gnugol-full-cmd)
	    )
	  (switch-to-buffer newbuffer)
	  )
	;; (goto-char (+ point-min 4))
	)
    ( (beep) (message "search string too long"))))

(defun gnugol-search-selection ()
