Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 136 lines (119 sloc) 3.242 kb
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
;;; shell-parse.el

;; minor mode definitions
(define-minor-mode shell-parse-mode
 "Minor mode to parse arbitrary shell commands for terminal completion"
 nil
 " sp"
 '(([C-tab] . shell-parse-complete)
   ([C-M-tab] . shell-parse-complete-first)
   ([C-S-iso-lefttab] . shell-parse-complete-backwards)
   ("\C-c\C-q" . shell-parse-query))
 :group 'shell-parse)

(defcustom shell-parse-command
 "echo -en $(curl -H \"Accept: application/json\" \"http://suggestqueries.google.com/complete/search?client=firefox&q=%s\" 2>/dev/null)"
 "Command to be parsed. Use a %s for the query to be substituted."
 :type 'string
 :group 'shell-parse)

(defcustom shell-parse-split
 ","
 "Delimiter for command output"
 :type 'string
 :group 'shell-parse)

;; persistent variables
;; todo: make these local to buffer

(setq shell-parse-list '())
(setq shell-parse-word "")
(setq shell-parse-pos (point-min))

;; functions

(defun shell-parse-query ()
 (interactive)
 (if (thing-at-point 'word)
   (message
    (mapconcat 'identity
(shell-parse-preprocess
(shell-parse-request
(thing-at-point 'word)))
","))
  (message "")))

(defun shell-parse-complete-first ()
 (interactive)
 (end-of-thing 'word)
 (let ((s (thing-at-point 'word)))
  (when s
   (let ((q (shell-parse-preprocess
(shell-parse-request
s))))
    (when q
     (let ((p (substring
(car q)
(length s))))
      (setq shell-parse-pos (point))
      (insert p)
      (setq shell-parse-list q)
      (setq shell-parse-word s)))))))

(defun shell-parse-complete ()
 (interactive)
 (shell-parse-complete-proto 'rotate-forwards))

(defun shell-parse-complete-backwards ()
 (interactive)
 (shell-parse-complete-proto 'rotate-backwards))

(defun shell-parse-complete-proto (f)
 ;; (interactive)
 (if (and
      shell-parse-list
      (string=
       (buffer-substring
(min shell-parse-pos (point-max))
(point))
       (substring
(car shell-parse-list)
(length shell-parse-word))))
   (progn
    (end-of-thing 'word)
    (backward-delete-char
     (-
      (length (car shell-parse-list))
      (length shell-parse-word)))
    (setq shell-parse-list (funcall f shell-parse-list))
    (insert (substring
(car shell-parse-list)
(length shell-parse-word))))
  (let ((s (thing-at-point 'word)))
   (when s
    (let ((q (shell-parse-preprocess
(shell-parse-request
s))))
     (when q
      (let ((p (substring
(car q)
(length s))))
       (end-of-thing 'word)
       (setq shell-parse-pos (point))
       (insert p)
       (setq shell-parse-list q)
       (setq shell-parse-word s))))))))

(defun shell-parse-preprocess (query)
 (let ((l (split-string
(apply 'string
(removel
'(?\" ?\[ ?\]) ; todo: unhardwire this
(string-to-list query)))
shell-parse-split)))
  (if (> (length (car (cdr l))) 0)
    (remove
     (car l)
     (cdr l))
   nil)))

(defun shell-parse-request (query)
 (shell-command-to-string
  (format
   shell-parse-command
   query)))

;; utility functions

(defun removel (el l)
 (cond (el (removel (cdr el) (remove (car el) l)))
       (t l)))

(defun rotate-forwards (l)
 (append (cdr l) (list (car l))))

(defun rotate-backwards (l)
 (append (last l) (butlast l)))
Something went wrong with that request. Please try again.