feat(emacs): Fill some search-related stubs

This commit is contained in:
Madeleine Sydney
2025-02-20 15:45:20 -07:00
parent 98a71dc13b
commit e2193dbd53
13 changed files with 227 additions and 26 deletions

View File

@@ -63,15 +63,13 @@ If FORCE-P, delete without confirmation."
:desc "Move current file. See `doom/move-this-file'."
:interactive t)
(syd-define-stub
syd/find-file-under-emacs-user-directory
:desc "Find under `emacs-user-directory'. See `doom/find-file-in-private-config'."
:interactive t)
(syd-define-stub
syd/find-file-under-here
:desc "Find under CWD. See `+default/find-file-under-here'."
:interactive t)
(defun syd-find-file-in (root)
(interactive (list (read-directory-name
"Find file in: " default-directory nil t)))
;; HACK: To avoid reimplementation, we pretend `root' is a project and
;; delegate the work to project.el.
(syd-with-project-root root
(project-find-file)))
(syd-define-stub
syd/yank-buffer-path
@@ -81,11 +79,21 @@ If FORCE-P, delete without confirmation."
(defun syd/find-file-in-emacs-user-directory ()
(interactive)
(unless (file-directory-p user-emacs-directory)
(user-error "`emacs-user-directory' doesn't exist! (%s)"
(abbreviate-file-name emacs-user-directory)))
(user-error "`user-emacs-directory' doesn't exist! (%s)"
(abbreviate-file-name user-emacs-directory)))
(let ((default-directory user-emacs-directory))
(call-interactively #'find-file)))
(defun syd-switch-to-emacs-user-directory ()
"Switch project to `user-emacs-directory' via `project-switch-project'."
(interactive)
(require 'syd-project)
(if (file-directory-p user-emacs-directory)
(syd-with-project-root user-emacs-directory
(project-switch-project user-emacs-directory))
(user-error "`user-emacs-directory' (%s) does not exist or is not a directory!"
(abbreviate-file-name user-emacs-directory))))
(syd-define-stub
syd/open-this-file-as-root
:desc "Open current file as root. See `doom/sudo-this-file'."
@@ -137,6 +145,7 @@ If FORCE-P, delete without confirmation."
base-file
conflict-file)))
;;;###autoload
(defun syd-split-tramp-file-name (file-name)
"Split FILE-NAME into (TRAMP-PREFIX . LOCAL-NAME). Returns (nil . FILE-NAME)
if FILE-NAME has no TRAMP prefix."

View File

@@ -8,9 +8,32 @@
(when-let* ((project (project-current nil dir)))
(project-root project)))
(defun syd-cd-project ()
(defun syd-project-cd ()
"Change the working directory to the root of the current project."
(cd (syd-project-root)))
(define-obsolete-function-alias 'syd-cd-project 'syd-project-cd
"2025-02-20")
(defmacro syd-with-project-root (root &rest body)
"Execute BODY with ROOT recognised as what project.el calls a \"transient
project\"."
(declare (indent defun))
(let ((root* (gensym "root"))
(forget-after-p (gensym "forget-after-p")))
`(let* ((,root* ,root)
(,forget-after-p
(not (member ,root* (project-known-project-roots)))))
(let ((project-find-functions (lambda (_) (cons 'transient ,root*))))
,@body)
(when ,forget-after-p
(project-forget-project ,root*)))))
(defun syd-project-search ()
(interactive)
(require 'syd-file)
;; TODO: Prompt for path project root is not found.
(syd-search-directory (syd-project-root)))
(provide 'syd-project)
;;; syd-project.el ends here

View File

@@ -0,0 +1,55 @@
;;; syd-search.el -*- lexical-binding: t; -*-
(cl-defun syd-search-region (beg end &key initial)
(save-restriction
(narrow-to-region beg end)
(consult-line initial)))
(defun syd-search--escape-regexp (str)
(require 'syd-text)
(replace-regexp-in-string " " "\\\\ "
(syd-pcre-quote str)))
(defun syd-search-buffer (buffer)
"Conduct a text search on BUFFER.
If a selection is active and multi-line, perform a search restricted to that
region.
If a selection is active and not multi-line, use the selection as the initial
input and search the whole buffer for it."
(interactive (list (current-buffer)))
(save-restriction
(let* ((beg (region-beginning))
(end (region-end))
(multiline-p (/= (line-number-at-pos beg)
(line-number-at-pos end))))
(if (and beg end (region-active-p))
(progn (deactivate-mark)
(if multiline-p
(syd-search-region beg end)
;; Treat as a single pattern, not several
;; space-separated patterns.
(consult-line (syd-search--escape-regexp
(buffer-substring-no-properties beg end)))))
(consult-line)))))
;;;###autoload
(defun syd-search-directory (dir)
(interactive (list (read-directory-name
"Search directory: "
default-directory nil t)))
(cond ((executable-find "rg")
(consult-ripgrep dir))
((executable-find "grep")
(message "Couldn't find ripgrep; using grep")
(consult-grep dir))))
;;;###autoload
(defun syd-search-current-directory ()
(interactive)
(syd-search-directory default-directory))
(provide 'syd-search)
;;; syd-search.el ends here

View File

@@ -120,4 +120,16 @@ in some cases."
(interactive "P<x><y>")
(syd-evil-paste t arg register yank-handler))
;;;###autoload
(defun syd-pcre-quote (str)
"Like `reqexp-quote', but for PCREs."
(let ((special '(?. ?^ ?$ ?* ?+ ?? ?{ ?\\ ?\[ ?\| ?\())
(quoted nil))
(mapc (lambda (c)
(when (memq c special)
(push ?\\ quoted))
(push c quoted))
str)
(concat (nreverse quoted))))
(provide 'syd-text)