From 10c62e541ffed9a116f4955517a29756d1947102 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Wed, 14 Feb 2024 12:09:18 +0800 Subject: [PATCH] Emacs: project.el open project files TODO, README, LICENSE, NOTES, HOURS --- .config/emacs/init.org | 80 +++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/.config/emacs/init.org b/.config/emacs/init.org index 712c844..039c353 100644 --- a/.config/emacs/init.org +++ b/.config/emacs/init.org @@ -14,9 +14,10 @@ Skipping a bunch of regular expression searching in the file-name-handler-alist #+end_src *** Garbage Collection -The default Garbage Collector is triggered at 800 KB, way too conservative, let's bump to 512 MB. -Garbage collection is a big contributor to startup times. This fends it off, then is reset later by -enabling `gcmh-mode'. Not resetting it will cause stuttering/freezes. +The default Garbage Collector is triggered at 800 KB, way too conservative, +let's bump to 512 MB. Garbage collection is a big contributor to startup times. +This fends it off, then is reset later by enabling `gcmh-mode'. Not resetting it +will cause stuttering/freezes. #+begin_src emacs-lisp :tangle ./early-init.el ;; -*- lexical-binding: t -*- @@ -161,9 +162,6 @@ Finish up ("elpa-devel" . "https://elpa.gnu.org/devel/") ("nongnu" . "https://elpa.nongnu.org/nongnu/") ("melpa" . "https://melpa.org/packages/"))) -;; Add MELPA to `package-archives' -(add-to-list 'package-archives - '("melpa" . "https://melpa.org/packages/")) ;; Proof-of-concept to install a list of packages @@ -1366,27 +1364,75 @@ Ace Window will show a hint if there are more than 2 windows, but I don't really (define-key 'ctl-x-5-prefix (kbd "RET") #'joe/select-frame) #+end_src ** Projects + +Basic enhancements to project.el + #+begin_src emacs-lisp (with-eval-after-load 'project (setq project-vc-ignores '("target/" "bin/" "obj/")) (add-to-list 'project-switch-commands '(magit-project-status "Magit" ?m)) ;; TODO: This doesn't start in the correct working directory (add-to-list 'project-switch-commands '(joe/vterm-here "VTerm" ?s)) - (add-to-list 'project-vc-extra-root-markers ".dir-locals.el") + (add-to-list 'project-vc-extra-root-markers ".dir-locals.el")) +#+end_src - (defun joe/project-root-override (dir) - (let ((proj-name (project-try-vc dir))) - (when (and proj-name - (not (frame-parameter (selected-frame) 'explicit-name))) - (progn - (set-frame-name (file-name-nondirectory (directory-file-name (nth 2 proj-name))))) - proj-name))) +These are functions to load a project specific file given the conventions I use. -;; TODO: There's an issue with this and it's causing some weird nesting/recursion - ;; (add-hook 'project-find-functions #'joe/project-root-override) +#+begin_src emacs-lisp +(defun joe/project-open-project-file (FILENAME) + (when (project-current) + (let ((proj-dir (project-root (project-current t)))) + (find-file-other-window (expand-file-name FILENAME proj-dir))))) - (define-key 'ctl-x-5-prefix "n" #'set-frame-name)) +(defun joe/project-open-project-todo () + (interactive) + (joe/project-open-project-file "TODO.org")) +(defun joe/project-open-project-hours () + (interactive) + (joe/project-open-project-file "HOURS.org")) + +(defun joe/project-open-project-notes () + (interactive) + (joe/project-open-project-file "NOTES.org")) + +(defun joe/project-open-project-readme () + (interactive) + (when (project-current) + (if (file-exists-p (expand-file-name "README.org" (project-root (project-current)))) + (joe/project-open-project-file "README.org") + (joe/project-open-project-file "README.md")))) + +(defun joe/project-open-project-license () + (interactive) + (joe/project-open-project-file "LICENSE")) + +(defun joe/project-dirvish-dwim () + (interactive) + (when (project-current) + (dirvish-dwim (project-root (project-current))))) + + +(evil-define-key 'normal joe/evil-space-mode-map (kbd "_") #'joe/project-dirvish-dwim) +(define-key project-prefix-map "t" #'joe/project-open-project-todo) +(define-key project-prefix-map "h" #'joe/project-open-project-hours) +(define-key project-prefix-map "n" #'joe/project-open-project-notes) +(define-key project-prefix-map "r" #'joe/project-open-project-readme) +(define-key project-prefix-map "l" #'joe/project-open-project-license) +;; Remape this +(define-key project-prefix-map "C-r" #'project-query-replace-regexp) +#+end_src + +If you want to try and incorporate something a bit more robust, try this + +#+begin_src emacs-lisp :tangle no +(defun +project-todo () + "Edit the to-do file at the root of the current project." + (interactive) + (let* ((project (project-root (project-current t))) + (todo (car (directory-files project t "^TODO\\(\\..*\\)?$")))) + (cond ((and todo (file-exists-p todo)) (find-file todo)) + (t (message "Project does not contain a TODO file."))))) #+end_src Stuff to immediately switch to Jetbrains for debugging