Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
259 lines (206 sloc) 8.32 KB
(require 'browse-url)
(require 'simple)
(require 'cl)
(require 'ido))
;; user options
(defvar pylookup-db-file "pylookup.db" "Pylookup database file")
(defvar pylookup-program "" "Pylookup execution file")
;; internal variables
(defvar pylookup-html-locations '(""))
(defvar pylookup-history nil)
(defvar pylookup-cache nil)
(defvar pylookup-return-window-config nil)
(defvar pylookup-temp-buffer-name "*Pylookup Completions*")
(defvar pylookup-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [mouse-1] 'pylookup-mode-lookup-and-leave)
(define-key map [mouse-2] 'pylookup-mode-lookup)
(define-key map "\C-m" 'pylookup-mode-lookup-and-leave)
(define-key map " " 'pylookup-mode-lookup-and-leave)
(define-key map "f" 'pylookup-mode-lookup)
(define-key map "q" 'pylookup-mode-quit-window)
(define-key map "n" 'pylookup-mode-next-line)
(define-key map "p" 'pylookup-move-prev-line)
(define-key map "v" 'scroll-down)
(define-key map "V" 'scroll-up)
(define-key map "l" 'recenter)
(define-key map "<" 'beginning-of-buffer)
(define-key map ">" 'end-of-buffer)
(define-key map "v" 'scroll-down)
"Keymap for `pylookup-mode-mode'.")
(put 'pylookup-mode 'mode-class 'special)
(defvar pylookup-completing-read
(if (null ido-mode) 'completing-read 'ido-completing-read)
"Ido support with convenience")
;; pylookup mode specific interactive functions
(defun pylookup-trim (desc n)
"Trim desc string to fit into the length, n"
(if (> (length desc) (- n 2))
(concat (substring desc 0 (- n 2)) "..")
(defun pylookup-mode ()
"Major mode for output from \\[pylookup-lookup]."
(use-local-map pylookup-mode-map)
(setq major-mode 'pylookup-mode)
(setq mode-name "Pylookup")
(setq buffer-read-only t))
(defun pylookup-move-prev-line ()
"Move to previous entry"
(when (< 3 (line-number-at-pos))
(call-interactively 'previous-line)))
(defun pylookup-mode-next-line ()
"Move to next entry"
(when (< (line-number-at-pos)
(- (line-number-at-pos (point-max)) 1))
(call-interactively 'next-line)))
(defun pylookup-mode-lookup-and-leave ()
"Lookup the current line in a browser and leave the completions window."
(call-interactively 'pylookup-mode-lookup)
(defun pylookup-mode-lookup ()
"Lookup the current line in a browser."
(let ((url (get-text-property (point) 'pylookup-target-url)))
(if url
(message "Browsing: \"%s\"" url)
(browse-url url))
(error "No URL on this line"))))
(defun pylookup-mode-quit-window ()
"Leave the completions window."
(set-window-configuration pylookup-return-window-config))
;; execute pylookup
(defun pylookup-exec-get-cache ()
"Run a pylookup process and get a list of cache (db key)"
(call-process pylookup-program nil standard-output nil
"-d" (expand-file-name pylookup-db-file)
(defun pylookup-exec-lookup (search-term)
"Runs a pylookup process and returns a list of (term, url) pairs."
(lambda (x) (split-string x ";"))
(call-process pylookup-program nil standard-output nil
"-d" (expand-file-name pylookup-db-file)
"-l" search-term))
"\n" t)))
;; interactive user interfaces
(defun pylookup-lookup (search-term)
"Lookup SEARCH-TERM in the Python HTML indexes."
(let ((initial (thing-at-point 'word)))
(funcall pylookup-completing-read
"Lookup Python Documentation for: "
(if pylookup-cache
(setq pylookup-cache (pylookup-exec-get-cache)))
nil nil initial 'pylookup-history))
(let ((matches (pylookup-exec-lookup search-term)))
;; 0. No results.
((eq matches nil)
(message "No matches for \"%s\"." search-term))
;; 1. A single result.
((= (length matches) 1)
;; Point the browser at the unique result and get rid of the buffer
(let ((data (car matches)))
(message "Browsing: \"%s\"" (car data))
(browse-url (cadr data))))
;; N. Multiple results.
;; Decorate the temporary buffer lines with appropriate properties for
;; selection.
(let* ((cur-window-conf (current-window-configuration))
(tmpbuf (get-buffer-create pylookup-temp-buffer-name))
(index 0))
(display-buffer tmpbuf)
(pop-to-buffer tmpbuf)
(setq buffer-read-only nil)
;; Insert the text in the buffer
(insert (format "Python index matches for %s:\n\n" search-term))
(dolist (x matches)
;; split like
;; waitpid() (in module os) [lib]
;; --------- -------------- -----
;; =>
;; waitpid (in module os) [lib]
;; api module type
(let* ((tokens (split-string (car x)))
(api (car tokens))
(iter (cdr tokens))
(type (car (last tokens)))
(module ""))
(while (not (or (equal iter nil)
(string= (car iter) type)))
(setq module (concat module " " (car iter)))
(setq iter (cdr iter)))
(incf index)
(insert (format " %03d) %-25s %-30s %10s"
(pylookup-trim api 25)
(pylookup-trim module 30)
(pylookup-trim type 10))))
(line-beginning-position) (line-end-position)
'pylookup-target-url (cadr x))
(insert "\n"))
;; goto first entry
(goto-line 3)
;; turn mode on
;; highlighting
(font-lock-add-keywords nil `((,(format "\\(%s\\|%s\\|%s\\)"
(upcase search-term)
(upcase-initials search-term))
font-lock-keyword-face prepend)))
(font-lock-add-keywords nil '(("\\<\\(lib\\)"
font-lock-constant-face prepend)))
(font-lock-add-keywords nil '(("\\<\\(in module.*)\\)"
font-lock-doc-face prepend)))
;; store window configuration
(set (make-local-variable 'pylookup-return-window-config) cur-window-conf)
;; make fit to screen
(shrink-window-if-larger-than-buffer (get-buffer-window tmpbuf)))))))
(defun pylookup-update (src)
"Run pylookup-update and create the database at `pylookup-db-file'."
(list (funcall pylookup-completing-read
"Python Html Documentation source: "
;; -d /home/myuser/.pylookup/pylookup.db -l <URL>
(call-process pylookup-program nil standard-output nil
"-d" (expand-file-name pylookup-db-file)
"-l" src))
(provide 'pylookup)
;;; pylookup.el ends here