home

My NixOS systems configurations.
Log | Files | Refs | LICENSE

config-projects.el (3331B)


      1 ;;; config-projects.el --- -*- lexical-binding: t; -*-
      2 ;;; Commentary:
      3 ;;; Project related configuration.
      4 ;;; Code:
      5 
      6 (use-package project
      7   :bind (("C-x p v" . vde-project-magit-status)
      8          ("C-x p s" . vde-project-vterm)
      9          ("C-x p X" . vde/run-in-project-vterm))
     10   :config
     11   (setq vde/project-local-identifier '(".project")) ;; "go.mod"
     12   (setq project-switch-commands
     13         '((?f "File" project-find-file)
     14           (?g "Grep" project-find-regexp)
     15           (?d "Dired" project-dired)
     16           (?b "Buffer" project-switch-to-buffer)
     17           (?q "Query replace" project-query-replace-regexp)
     18           (?m "Magit" vde-project-magit-status)
     19           (?e "Eshell" project-eshell)
     20           (?s "Vterm" vde-project-vterm)))
     21 
     22   (defun vde/project-try-local (dir)
     23     "Determine if DIR is a non-VC project."
     24     (if-let ((root (if (listp vde/project-local-identifier)
     25                        (seq-some (lambda (n)
     26                                    (locate-dominating-file dir n))
     27                                  vde/project-local-identifier)
     28                      (locate-dominating-file dir vde/project-local-identifier))))
     29         (cons 'local root)))
     30   (cl-defmethod project-root ((project (head local)))
     31     (cdr project))
     32 
     33   (cl-defmethod project-root ((project (eql nil))) nil)
     34 
     35   (add-hook 'project-find-functions #'vde/project-try-local)
     36 
     37   :init
     38   (setq-default project-compilation-buffer-name-function 'project-prefixed-buffer-name)
     39   (defun vde-project-magit-status ()
     40     "Run `magit-status' on project."
     41     (interactive)
     42     (magit-status (vde-project--project-current)))
     43 
     44   (defun vde-project-vterm (&optional command)
     45     "Run `vterm' on project.
     46 If a buffer already exists for running a vterm shell in the project's root,
     47 switch to it. Otherwise, create a new vterm shell."
     48     (interactive)
     49     (let* ((default-directory (vde-project--project-current))
     50            (default-project-vterm-name (project-prefixed-buffer-name "vterm"))
     51            (vterm-buffer (get-buffer default-project-vterm-name)))
     52       (if (and vterm-buffer (not current-prefix-arg))
     53           (pop-to-buffer-same-window vterm-buffer)
     54         (let* ((cd-cmd (concat " cd " (shell-quote-argument default-directory))))
     55           (vterm default-project-vterm-name)
     56           (with-current-buffer vterm-buffer
     57             (vterm-send-string cd-cmd)
     58             (vterm-send-return))))
     59       (when command
     60         (vterm-send-string command)
     61         (vterm-send-return))))
     62   (defun vde/run-in-project-vterm ()
     63     (interactive)
     64     (let* ((default-directory (vde-project--project-current))
     65            (default-project-vterm-name (project-prefixed-buffer-name "vterm"))
     66            (vterm-buffer (get-buffer default-project-vterm-name)))
     67       (vde-project-vterm (read-string "Command: "))))
     68   )
     69 
     70 (use-package conner
     71   :bind (("C-x p C" . conner-run-project-command))
     72   :init
     73   (require 'vterm))
     74 
     75 (use-package project-x
     76   :after project
     77   :config
     78   (add-hook 'project-find-functions 'project-x-try-local 90)
     79   (add-hook 'kill-emacs-hook 'project-x--window-state-write)
     80   (add-to-list 'project-switch-commands
     81                '(?j "Restore windows" project-x-windows) t)
     82     :bind (("C-x p w" . project-x-window-state-save)
     83            ("C-x p j" . project-x-window-state-load)))
     84 
     85 (provide 'config-projects)
     86 ;;; config-projects.el ends here