Created
September 30, 2019 18:27
-
-
Save ncalexan/21a1648398d6e6827e79406e49b42419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; searchfox.el --- A front-end for searchfox.org - The Mozilla Source Code Indexer -*- lexical-binding: t; -*- | |
;; Copyright (C) 2019 Nicholas Alexander | |
;; Author: Nicholas Alexander <nalexander@roboto.local> | |
;; Keywords: tools, matching | |
;; 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 this program. If not, see <https://www.gnu.org/licenses/>. | |
;;; Commentary: | |
;; A front-end for searchfox.org - The Mozilla Source Code Indexer. | |
;;; Code: | |
(require 'request) | |
(defgroup searchfox nil | |
"A front-end for searchfox.org - The Mozilla Source Code Indexer." | |
:group 'tools | |
:group 'matching) | |
(defvar searchfox-search-url "https://searchfox.org/mozilla-central/search" | |
"URL of searchfox service endpoint.") | |
(defvar searchfox-topsrcdir "/Users/nalexander/Mozilla/gecko/" | |
"Intepret searchfox results relative to this top source directory.") | |
(defcustom searchfox-reuse-buffers nil | |
"Non-nil means we reuse the existing searchfox results buffer. | |
Otherwise, create one buffer per unique search." | |
:type 'boolean | |
:group 'searchfox) | |
(defun searchfox/buffer-name (needle regexp path) | |
"Return a buffer name formatted according to searchfox.el conventions." | |
(let ((path-str (if (and path (not (string= path "./"))) | |
(format " path:%s" path) | |
""))) | |
(cond | |
(searchfox-reuse-buffers "*searchfox*") | |
(regexp (format "*searchfox regexp:%s%s*" needle path-str)) | |
(:else (format "*searchfox text:%s%s*" needle path-str))))) | |
;;;###autoload | |
(defun searchfox (string &optional regexp path) | |
"Query searchfox for STRING, with STRING defaulting to the symbol under point. | |
If REGEXP is truth-y, interpret STRING as a regular expression. | |
If PATH is not NIL, restrict to PATH. PATH may contain globs of the form * and **." | |
(let* ((default-directory searchfox-topsrcdir) | |
(params `((q . ,string) | |
(regexp . ,(if regexp "true" "false")) | |
(path . ,(if (and path (not (string= path "./"))) path "")))) | |
(url (format "%s?%s" searchfox-search-url (request--urlencode-alist params))) | |
(cmd (format | |
"curl --silent '%s' -H 'Accept: application/json' | jq -j '.[] | select(type|.==\"object\") | (.\"Files\" // [] | .[] | \"File: \", .path, \"\\n1:1:\n\"), (.\"Textual Occurrences\" | .[] | \"File: \", .path, \"\\n\", (.lines[] | (.lno, \":\", .bounds[0], \":\", .line, \"\\n\")), \"\\n\")'" | |
url))) | |
(compilation-start | |
cmd | |
#'ag-mode | |
`(lambda (mode-name) ,(searchfox/buffer-name string regexp path))))) | |
;;;###autoload | |
(defun sf (string) | |
"Query searchfox for STRING." | |
(interactive (list (ag/read-from-minibuffer "Searchfox string"))) | |
(searchfox string)) | |
;;;###autoload | |
(defun sfp (string path) | |
"Query searchfox for STRING, restricting to PATH. | |
PATH may contain globs of the form * and **." | |
(interactive (list (ag/read-from-minibuffer "Searchfox string") | |
(file-relative-name | |
(read-directory-name "Path glob: " searchfox-topsrcdir) | |
searchfox-topsrcdir))) | |
(searchfox string nil path)) | |
;;;###autoload | |
(defun sfr (regexp) | |
"Query searchfox for REGEXP." | |
(interactive (list (ag/read-from-minibuffer "Searchfox regexp"))) | |
(searchfox regexp t)) | |
;;;###autoload | |
(defun sfrp (regexp path) | |
"Query searchfox for REGEXP, restricting to PATH. | |
PATH may contain globs of the form * and **." | |
(interactive (list (ag/read-from-minibuffer "Searchfox regexp") | |
(file-relative-name | |
(read-directory-name "Path glob: " searchfox-topsrcdir) | |
searchfox-topsrcdir))) | |
(searchfox regexp t path)) | |
(provide 'searchfox) | |
;;; searchfox.el ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment