 ;;         'string<)
 ;;   (lambda (selection)
 ;;     (insert selection)))
-
-;; (defc ar/insert-objc-import
+;;
+;; (defhelm ar/insert-objc-import
 ;;   "My ObjC imports"
-;;   (sort (delete-dups (ar/find-instances "^import"
-;;                                         "\\*.java"
-;;                                         '("root/to/objc/source")))
+;;   (sort (delete-dups (ar/find "^import"
+;;                               "\\*.java"
+;;                               '("root/to/objc/source")))
 ;;         'string<)
 ;;   (lambda (selection)
 ;;     (insert selection)))

(use-package highlight-thing :ensure t)
(global-highlight-thing-mode)

(defun ar/grep (pattern filename-pattern search-paths)
  "Grep for PATTERN and narrow to FILENAME-PATTERN and SEARCH-PATHS."
  (let* ((search-paths-string (mapconcat 'identity search-paths " "))
         (command (format (concat "grep "
                                  "--binary-file=without-match "
                                  "--recursive "
                                  "--no-filename "
                                  "--regexp=%s "
                                  "--include %s "
                                  " %s")
                          pattern
                          filename-pattern
                          search-paths-string)))
    (split-string (shell-command-to-string command) "\n")))

(defun ar/find (filename-pattern search-paths)
  "Find file with FILENAME-PATTERN and SEARCH-PATHS."
  (let* ((search-paths-string (mapconcat 'identity search-paths " "))
         (find-command (format "find %s -iname %s"

                               filename-pattern)))
    (split-string (shell-command-to-string find-command) "\n")))

(defun ar/helm (title candidates on-select-function)
  (helm :sources `((name . ,title)
                   (candidates . ,candidates)
                   (action . ,on-select-function))
        :buffer "*helm-exec*"
        :candidate-number-limit 10000))

(defun ar/helm-sample-command ()
  "Dummy helm sample command."
  (interactive)
  (ar/helm "My Options"
           '("option 1"
             "option 2"
             "option 3")
           (lambda (selection)
             (message "selected: %s" selection))))

(defmacro defhelm (name title candidates on-select-function)
  "Create a helm command with NAME, source TITLE, CANDIDATES list and
ON-SELECT-FUNCTION."
  `(defun ,name ()
     (interactive)
     (ar/helm ,title
              ,candidates
              ,(lambda (selection)
                 (message "selected: %s" selection)))))

;; defhelm examples:
;;
;; (defhelm ar/insert-java-import
;;   "My Java imports"
;;   (sort (delete-dups (ar/grep "^import"
;;                               "\\*.java"
