-
Notifications
You must be signed in to change notification settings - Fork 23
/
company-sparql.el
77 lines (63 loc) · 2.92 KB
/
company-sparql.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
;;; company-sparql.el --- Add company support for sparql mode
;; Copyright (C) 2017 Bjarte Johansen
;; Author: Bjarte Johansen <Bjarte dot Johansen at gmail dot com>
;; Homepage: https://github.com/ljos/sparql-mode
;; Package-Requires: ((cl-lib "0.5") (company "0.9.0") (emacs "24.3"))
;; This file is not part of GNU Emacs.
;; This program 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 3 of the License, or
;; (at your option) any later version.
;; This program 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 SPARQL mode. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Functions to add company support to sparql-mode.
;;; Usage:
;; (eval-after-load 'company
;; '(add-to-list 'company-backends 'company-sparql))
;; (eval-after-load 'company-keywords
;; '(add-to-list 'company-keywords-alist `(sparql-mode . ,sparql--keywords)))
;;; Code:
(require 'cl-lib)
(require 'company)
(require 'url)
(require 'url-handlers)
(defvar company-sparql-prefix-namespaces nil)
(defvar company-sparql-use-prefixcc t)
;;;###autoload
(defun company-sparql (command &optional arg &rest ignored)
"`company-mode' completion back-end for `sparql-mode'. Right
now it only completes prefixes, `company-keywords' takes care of
keywords."
(interactive (list 'interactive))
(cl-case command
(init (with-current-buffer (get-buffer-create "*SPARQL PREFIX*")
(when (zerop (buffer-size))
(when company-sparql-use-prefixcc
(let ((url-request-method "GET"))
(url-insert
(url-retrieve-synchronously
"http://prefix.cc/popular/all.file.sparql" t)))
(goto-char (point-min))
(while (search-forward "PREFIX " nil t)
(replace-match "")))
(dolist (prefix company-sparql-prefix-namespaces)
(insert prefix "\n"))
(sort-lines nil (point-min) (point-max))
(bury-buffer))))
(interactive (company-begin-backend 'company-sparql))
(prefix (and (eq major-mode 'sparql-mode)
(< 0 (buffer-size
(get-buffer "*SPARQL PREFIX*")))
(let ((case-fold-search t))
(looking-back "^\\s-*PREFIX \\(.*\\)"))
(match-string 1)))
(candidates (cl-remove-if-not (lambda (c) (string-prefix-p arg c))
(with-current-buffer (get-buffer "*SPARQL PREFIX*")
(split-string (buffer-string) "\n" t))))
(require-match 'never)))
(provide 'company-sparql)