dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

restclient-capf.el (3164B)


      1 ;; -*- lexical-binding: t; -*-
      2 ;; basic capf for restclient
      3 ;; only works for first request in buffer
      4 
      5 (defun restclient-capf--load-mimefile ()
      6   (let ((filename "/etc/mime.types")
      7         (mimelist))
      8     (when (file-exists-p filename)
      9       (with-temp-buffer
     10         (insert-file filename)
     11         (goto-char 0)
     12         (while (progn
     13                  (line-beginning-position)
     14                  (when (search-forward-regexp "[[:space:]]" nil t)
     15                    (push (buffer-substring-no-properties (line-beginning-position) (- (point) 1)) mimelist))
     16                  (= 0 (forward-line))))))
     17     mimelist))
     18 
     19 (defvar restclient-capf-mime-types
     20   (pcase system-type
     21     ('gnu/linux (restclient-capf--load-mimefile))))
     22 
     23 (defconst restclient-capf-http-methods
     24   '(GET
     25     HEAD
     26     POST
     27     PUT
     28     DELETE
     29     OPTIONS
     30     TRACE
     31     PATCH))
     32 
     33 (defconst restclient-capf-http-headers
     34   `(;;; 3. Representations
     35     ;; 3.1. Representation Metadata
     36     ("Content-Type" . ,restclient-capf-mime-types)
     37     ("Content-Encoding")
     38     ("Content-Language")
     39     ("Content-Location")
     40     ;; 3.3. Payload Semantics
     41     ("Content-Length")
     42     ("Trailer")
     43     ("Transfer-Encoding")
     44     ;;; 5. Request Header Fields
     45     ;; 5.1. Controls
     46     ("Cache-Control")
     47     ("Expect")
     48     ("Host")
     49     ("Max-Forwards")
     50     ("Pragma")
     51     ("Range")
     52     ("TE")
     53     ;; 5.2. Conditionals
     54     ("If-Match")
     55     ("If-None-Match")
     56     ("If-Modified-Since")
     57     ("If-Unmodified-Since")
     58     ("If-Range")
     59     ;; 5.3. Content Negotiation
     60     ("Accept" . ,restclient-capf-mime-types)
     61     ("Accept-Charset")
     62     ("Accept-Encoding")
     63     ("Accept-Language")
     64     ;; 5.4. Authentication Credentials
     65     ("Authorization")
     66     ("Proxy-Authorization")
     67     ;; 5.5. Request Context
     68     ("From")
     69     ("Referer")
     70     ("User-Agent")))
     71 
     72 (defun restclient-completion-at-point ()
     73   (let* ((start (save-excursion
     74                   (search-backward-regexp "^\\|[[:space:]]")
     75                   (skip-chars-forward " " (line-end-position))
     76                   (point)))
     77          (end (point)))
     78     
     79     (list start end
     80           (cond
     81            ((save-excursion
     82               (search-backward-regexp "[\r\n][\r\n]" nil t))
     83             nil)
     84            ((= (line-beginning-position) 1)
     85             (save-excursion
     86               (when (not (search-backward-regexp "[[:space:]]" nil t))
     87                 restclient-capf-http-methods)))
     88            (t
     89             (save-excursion
     90               (beginning-of-line)
     91               (if (search-forward ":" (line-end-position) t)
     92                   (cdr (assoc (string-trim (buffer-substring-no-properties (line-beginning-position)
     93                                                                            (- (point) 1)))
     94                               restclient-capf-http-headers
     95                               (lambda (a b)
     96                                 (compare-strings a nil nil b nil nil t))))
     97                 (mapcar (lambda (x) (car x)) restclient-capf-http-headers))))))))
     98 
     99 (defun restclient-capf-register ()
    100   (add-hook 'completion-at-point-functions #'restclient-completion-at-point nil t))
    101 
    102 (add-hook 'restclient-mode-hook #'restclient-capf-register)
    103 
    104 (provide 'restclient-capf)