3 | 3 |
;;
|
4 | 4 |
;; Author: Wilfred Hughes <me@wilfred.me.uk>
|
5 | 5 |
;; Created: 11 September 2014
|
6 | |
;; Version: 0.5
|
|
6 |
;; Version: 0.6
|
7 | 7 |
;; Package-Requires: ((dash "2.8.0"))
|
8 | 8 |
|
9 | 9 |
;;; License:
|
|
54 | 54 |
:type 'hook
|
55 | 55 |
:risky t)
|
56 | 56 |
|
|
57 |
(defcustom pip-requirements-index-url
|
|
58 |
"https://pypi.org/simple/"
|
|
59 |
"The URL used to fetch the list of packages used for completion."
|
|
60 |
:group 'pip-requirements
|
|
61 |
:type 'string)
|
|
62 |
|
57 | 63 |
;;;###autoload
|
58 | 64 |
(add-to-list 'auto-mode-alist
|
59 | 65 |
`(,(rx ".pip" string-end) . pip-requirements-mode))
|
|
71 | 77 |
(group (1+ (or alphanumeric "-" "_" ".")))))
|
72 | 78 |
|
73 | 79 |
(defconst pip-requirements-version-regex
|
|
80 |
;; https://www.python.org/dev/peps/pep-0440/#version-specifiers
|
74 | 81 |
(rx
|
75 | |
(group (or "==" ">" ">=" "<" "<=" "!="))
|
76 | |
(group (1+ (or digit "b" "." "post")))))
|
|
82 |
(group (or "==" ">" ">=" "<" "<=" "!=" "~="))
|
|
83 |
(group (1+ (or digit "b" "." "post" "*")))))
|
|
84 |
|
|
85 |
(defconst pip-requirements-arbitrary-version-regex
|
|
86 |
;; https://www.python.org/dev/peps/pep-0440/#arbitrary-equality
|
|
87 |
(rx (group "===") (group (1+ not-newline))))
|
77 | 88 |
|
78 | 89 |
(defconst pip-requirements-operators
|
79 | 90 |
(list
|
80 | 91 |
(list pip-requirements-name-regex 1 'font-lock-variable-name-face)
|
81 | 92 |
(list pip-requirements-version-regex 1 'font-lock-builtin-face)
|
82 | |
(list pip-requirements-version-regex 2 'font-lock-constant-face)))
|
|
93 |
(list pip-requirements-arbitrary-version-regex 1 'font-lock-builtin-face)
|
|
94 |
(list pip-requirements-version-regex 2 'font-lock-constant-face)
|
|
95 |
(list pip-requirements-arbitrary-version-regex 2 'font-lock-constant-face)))
|
83 | 96 |
|
84 | 97 |
(defconst pip-requirements-syntax-table
|
85 | 98 |
(let ((table (make-syntax-table)))
|
86 | 99 |
(modify-syntax-entry ?# "<" table)
|
87 | 100 |
(modify-syntax-entry ?\n ">" table)
|
|
101 |
(modify-syntax-entry ?> "." table)
|
|
102 |
(modify-syntax-entry ?< "." table)
|
|
103 |
(modify-syntax-entry ?= "." table)
|
|
104 |
(modify-syntax-entry ?~ "." table)
|
88 | 105 |
table))
|
89 | 106 |
|
90 | 107 |
(defvar pip-http-buffer nil)
|
|
97 | 114 |
(goto-char (point-min))
|
98 | 115 |
(re-search-forward "^$" nil 'move)
|
99 | 116 |
|
100 | |
(setq pip-packages
|
101 | |
(->> (libxml-parse-html-region (point) (point-max))
|
102 | |
;; Get the body tag.
|
103 | |
-last-item
|
104 | |
;; Immediate children of the body.
|
105 | |
cdr cdr cdr
|
106 | |
;; Anchor tags.
|
107 | |
(--filter (eq (car it) 'a))
|
108 | |
;; Inner text of anchor tags.
|
109 | |
(-map #'cl-third))))
|
|
117 |
(let* ((dom (libxml-parse-html-region (point) (point-max)))
|
|
118 |
(body-tag (-last-item dom))
|
|
119 |
(body-children (cdddr body-tag))
|
|
120 |
(a-tags (--filter (eq (car-safe it) 'a) body-children)))
|
|
121 |
(setq pip-packages
|
|
122 |
;; Inner text of anchor tags.
|
|
123 |
(-map #'cl-third a-tags))))
|
110 | 124 |
(kill-buffer pip-http-buffer))
|
111 | 125 |
|
112 | 126 |
(defun pip-requirements-fetch-packages ()
|
113 | 127 |
"Get a list of all packages available on PyPI and store them in `pip-packages'.
|
114 | 128 |
Assumes Emacs is compiled with libxml."
|
115 | 129 |
(setq pip-http-buffer
|
116 | |
(url-retrieve "https://pypi.python.org/simple/"
|
|
130 |
(url-retrieve pip-requirements-index-url
|
117 | 131 |
#'pip-requirements-callback nil t)))
|
118 | 132 |
|
119 | 133 |
(defun pip-requirements-complete-at-point ()
|