App-lookup

 view release on metacpan or  search on metacpan

etc/lookup.el  view on Meta::CPAN

(defcustom lookup-command "lookup"
  "The lookup command.

  If you're using cygwin, you should modify this variable like so
  (see the CAVEATS section in lookup documentation):

      (when (eq system-type 'cygwin)
        (setq lookup-command \"lookup -w 'cygstart'\"))"
  :group 'lookup :type 'string)

(defcustom lookup-prompt-function
  (if (fboundp 'ido-completing-read)
      'ido-completing-read
    'completing-read)
  "The function used to prompt user for lookup sites")

(defcustom lookup-sites-mode-alist nil
  "Alist mapping major-modes to their associated sites.
  Each cons cell in this alist contains the name of the major-mode
  as the associated key and the name of the sites (multiple sites
  are accepted) as the associated value.

  For example, to associate `ruby-mode' with the sites rubygems and
  ruby-doc (assuming both sites are defined in the configuration
  file ~/.lookuprc):

etc/lookup.el  view on Meta::CPAN

          (push (match-string 1) sites)))
      ;; TODO: make the regexp above less stupid, so we don't have to strip
      ;; the trailing whitespace
      (setq lookup-sites-cache (mapcar
                                (lambda (site)
                                  (replace-regexp-in-string
                                   "\s*$" "" site))
                                sites))))
  lookup-sites-cache)

(defun lookup--prompt-for-query (&optional site)
  "Prompt user for query argument to the command lookup.
  The prompt message is adjusted depending on the existence of
  symbol under point and whether the argument SITE is supplied or
  not."
  (let* ((word (thing-at-point 'symbol))
         (query (read-string
                 (cond ((and word site)
                        (format "Query %s (default %s): " site word))
                       (site
                        (format "Query %s: " site))
                       (word
                        (format "Query (default %s): " word))
                       (t
                        "Query: "))
                 nil 'lookup-hist word)))
    (when (string= "" query)
      (error "The query can not be empty"))
    query))

(defvar lookup-sites-hist nil
  "History of previously searched sites")

(defun lookup--prompt-for-sites (sites)
  (funcall lookup-prompt-function "Site: " sites nil t nil 'lookup-sites-hist))

(defun lookup-quick ()
  "Call `lookup' with predefined sites based on current `major-mode'.
  This command will complain if current `major-mode' is not
  associated with any sites (see the variable
  `lookup-sites-mode-alist' on how to associate a major-mode with
  lookup sites)"
  (interactive)
  (let ((possible-sites (cdr (assoc major-mode lookup-sites-mode-alist)))
        query sitename)
    (unless possible-sites
      (error "%s is not associated with any site" major-mode))
    (setq sitename
          (if (> (length possible-sites) 1)
              (lookup--prompt-for-sites possible-sites)
            (car possible-sites)))
    (setq query (lookup--prompt-for-query sitename))
    (lookup sitename query)))

(defun lookup (sitename query &optional recache)
  "Interact with the program lookup within emacs.
  SITENAME and QUERY will be passed to lookup as its command line
  arguments.

  When used interactively, prompt user the list of available sites
  for SITENAME and the query string QUERY (defaults to symbol at
  point). With one prefix-arg (the argument RECACHE if called from
  lisp), recache the sites first."
  (interactive
   (let ((sites (lookup--get-sites current-prefix-arg)))
     (list
      (lookup--prompt-for-sites sites)
      (lookup--prompt-for-query)
      current-prefix-arg)))
  (shell-command
   (format "%s '%s' %s" lookup-command sitename query)))

(provide 'lookup)

;;; lookup.el ends here



( run in 1.257 second using v1.01-cache-2.11-cpan-6aa56a78535 )