Switch from projectile to project.el

This commit is contained in:
David Morgan 2021-09-01 16:28:44 +01:00
parent afa8c2eab0
commit 1c9f64d02c
6 changed files with 130 additions and 73 deletions

View File

@ -26,7 +26,7 @@
(require 'init-search)
(require 'init-navigation)
(require 'init-windows)
(require 'init-projectile)
(require 'init-project)
(require 'init-modeline)
(require 'init-dashboard)
(require 'init-completion)

View File

@ -8,8 +8,8 @@
dashboard-startup-banner 'logo
dashboard-set-footer nil
dashboard-week-agenda t
dashboard-projects-backend 'projectile
dashboard-projects-switch-function 'projectile-persp-switch-project
dashboard-projects-backend 'project-el
dashboard-projects-switch-function 'switch-project
dashboard-items '((recents . 15)
(bookmarks . 5)
(projects . 5)

View File

@ -64,7 +64,7 @@
:init
(defvar switching-project nil)
(defun vertico-directory-enter-or-switch-project ()
"Wrapper around vertico-directory-enter that plays nicely with Projectile."
"Wrapper around vertico-directory-enter that plays nicely with adding projects."
(interactive)
(if switching-project
(vertico-exit)
@ -72,7 +72,7 @@
(defun read-project (orig &rest args)
(let ((switching-project t))
(apply orig args)))
(advice-add 'projectile-completing-read :around
(advice-add 'project-prompt-project-dir :around
'read-project)
:config
(defun vertico-directory-slash ()
@ -104,7 +104,6 @@
:bind ("M-P" . vertico-repeat))
(use-package consult
:after projectile
:bind (;; C-c bindings (mode-specific-map)
("C-c h" . consult-history)
("C-c m" . consult-mode-command)
@ -253,20 +252,78 @@
;; You may want to use `embark-prefix-help-command' or which-key instead.
;; (define-key consult-narrow-map (vconcat consult-narrow-key "?") #'consult-narrow-help)
(autoload 'projectile-project-root "projectile")
(setq consult-project-root-function #'projectile-project-root)
(setq consult-project-root-function
(lambda ()
(when-let (project (project-current))
(car (project-roots project)))))
;; Switch perspective when switching buffer if needed
(setq consult--display-buffer #'persp-switch-to-buffer)
(defvar consult-initial-narrow-config
'((consult-buffer . ?p)
(consult-buffer-no-preview . ?p)))
'((consult-buffer . ?x)
(consult-buffer-no-preview . ?x)))
;; Add initial narrowing hook
(defun consult-initial-narrow ()
(when-let (key (alist-get this-command consult-initial-narrow-config))
(setq unread-command-events (append unread-command-events (list key 32)))))
(add-hook 'minibuffer-setup-hook #'consult-initial-narrow)
(when (and (eq system-type 'darwin) (string-match-p "^find" consult-find-command))
(setq consult-find-command (concat "g" consult-find-command)))
(when (and (eq system-type 'darwin) (string-match-p "^find" consult-find-args))
(setq consult-find-args (concat "g" consult-find-args)))
;; Use fd that that we aren't just getting recentf, but also respect .gitignore
(setq consult--source-project-file
(plist-put consult--source-project-file
:items '(lambda ()
(let* ((root (consult--project-root))
(len (length root))
(inv-root (propertize root 'invisible t)))
(mapcar (lambda (x)
(concat inv-root (substring x len)))
(split-string
(shell-command-to-string
(format "fd --color never -t f -0 . %s" root))
"\0" t))))))
(defvar consult--source-perspective-buffer
`(:name "Perspective Buffer"
:narrow (?x . "Perspective")
:hidden t
:category buffer
:face consult-buffer
:history buffer-name-history
:state ,#'consult--buffer-state
:enabled ,(lambda () persp-mode)
:items
,(lambda ()
(consult--buffer-query :sort 'visibility
:predicate #'persp-is-current-buffer
:as #'buffer-name)))
"Perspective buffer candidate source for `consult-buffer'.")
(add-to-list 'consult-buffer-sources 'consult--source-perspective-buffer t)
;; Copy of consult--source-project-file to use with perspective narrowing (identical except for narrowing key)
(defvar consult--source-perspective-files
(plist-put (copy-sequence consult--source-project-file) :narrow '(?x "Project Files")))
(add-to-list 'consult-buffer-sources 'consult--source-perspective-files t)
;; Versions of consult--source-project-buffer and consult--source-project-file for use by consult-project-buffer
;; They allow narrowing with b and f (instead of p)
(defvar consult--project-source-project-buffer
(plist-put (plist-put (copy-sequence consult--source-project-buffer)
:hidden nil)
:narrow '(?b . "Project Buffer")))
(defvar consult--project-source-project-file
(plist-put (plist-put (copy-sequence consult--source-project-file)
:hidden nil)
:narrow '(?f . "Project File")))
(defun consult-project-buffer ()
(interactive)
(let ((consult-buffer-sources '(consult--project-source-project-buffer
consult--project-source-project-file)))
(consult-buffer)))
(defun consult--orderless-regexp-compiler (input type)
(setq input (orderless-pattern-compiler input))

View File

@ -0,0 +1,60 @@
;;; init-project.el --- Project (and Perspective) Configuration File -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:
(use-package project
:ensure nil
:config
(defun project-recentf ()
"Show a list of recently visited files in a project."
(interactive)
(if (boundp 'recentf-list)
(let* ((project-root (expand-file-name (project-root (project-current))))
(project-recentf-files (mapcar
(lambda (f) (file-relative-name f project-root))
(seq-filter (apply-partially 'string-prefix-p project-root) recentf-list))))
(find-file (expand-file-name
(funcall project-read-file-name-function
"Find recent project files"
project-recentf-files nil 'file-name-history nil)
project-root)))
(message "recentf is not enabled")))
(add-to-list 'project-switch-commands '(?h "Recentf" project-recentf) t)
(add-to-list 'project-switch-commands '(?b "Consult Project Buffer" consult-project-buffer) t)
(add-to-list 'project-switch-commands '(?r "Consult Ripgrep" consult-ripgrep) t)
(add-to-list 'project-switch-commands '(?m "Magit" magit-status) t)
(add-to-list 'project-switch-commands '(?R "Replace Regexp" project-query-replace-regexp) t)
;; project-root and project-try-local copied/modified from https://github.com/karthink/project-x/blob/master/project-x.el
(cl-defmethod project-root ((project (head local)))
"Return root directory of current PROJECT."
(cdr project))
(defun project-try-local (dir)
"Treat DIR as a project if it contains a .project file."
(if-let ((root (locate-dominating-file dir ".project")))
(cons 'local root)))
;; Add this hook last so so that vc takes precedence over local
(add-hook 'project-find-functions 'project-try-local 90)
:bind
("C-x p P" . project-switch-project)
("C-x f" . project-recentf))
(use-package perspective
:config (persp-mode)
(defun switch-project (proj)
"Switch to project or already open project perspective."
(interactive (list (project-prompt-project-dir)))
(let* ((persp-name (file-name-nondirectory (directory-file-name proj)))
(persp (gethash persp-name (perspectives-hash))))
(unless (equal persp (persp-curr))
;; Create or switch to a perspective named after the project
(persp-switch persp-name)
;; If the perspective did not exist, switch to the project
(when (not persp)
(project-switch-project proj)))))
:bind
("C-x p p" . switch-project))
(provide 'init-project)
;;; init-project.el ends here

View File

@ -1,61 +0,0 @@
;;; init-projectile.el --- Projectile Configuration File -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:
(defcustom projectile-default-dir "~/src"
"Starting directory when looking for new projects."
:group 'djm
:type 'directory)
(defcustom projectile-switch-project-command 'projectile-persp-switch-project
"Projectile switch project command."
:group 'djm
:type 'function)
(use-package projectile
:diminish
:after smartrep
:config
(projectile-mode t)
(define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map)
(def-projectile-commander-method ?b
"consult-buffer"
(progn
(setq unread-command-events (listify-key-sequence "p "))
(consult-buffer)))
(def-projectile-commander-method ?B
"Switch to project buffer"
(projectile-switch-to-buffer))
(def-projectile-commander-method ?r
"consult-ripgrep"
(consult-ripgrep))
(def-projectile-commander-method ?p
"DWIM"
(cond ((> (length (projectile-project-buffer-names)) 4) (projectile-switch-to-buffer))
((> (length (projectile-recentf-files)) 0) (projectile-recentf))
(t (projectile-find-file))))
(defun projectile-open-new-project (project-root)
(interactive (list (read-directory-name "Select project directory: " (file-name-as-directory projectile-default-dir))))
(projectile-add-known-project project-root)
(funcall projectile-switch-project-command project-root))
(smartrep-define-key projectile-mode-map
"C-c p" '(("C-p" . projectile-previous-project-buffer)
("C-n" . projectile-next-project-buffer)))
:bind-keymap ("C-c p" . projectile-command-map)
:bind
(:map projectile-mode-map
("C-c p n" . projectile-open-new-project)
("C-x f" . projectile-recentf))
:custom
(projectile-switch-project-action 'projectile-commander)
(projectile-cache-file (expand-file-name "projectile.cache" save-dir)))
(use-package perspective
:init (persp-mode)
:bind ("C-x C-b" . persp-ibuffer)
:custom (persp-modestring-short t))
(use-package persp-projectile)
(provide 'init-projectile)
;;; init-projectile.el ends here

View File

@ -33,6 +33,7 @@
(use-package multi-vterm
:bind (("C-c t" . multi-vterm-next)
("C-x p t" . multi-vterm-project)
("C-c C-M-t" . multi-vterm)
(:map vterm-mode-map
("M-[" . multi-vterm-prev)