aboutsummaryrefslogtreecommitdiff
path: root/modules/prog-general.el
diff options
context:
space:
mode:
Diffstat (limited to 'modules/prog-general.el')
-rw-r--r--modules/prog-general.el118
1 files changed, 72 insertions, 46 deletions
diff --git a/modules/prog-general.el b/modules/prog-general.el
index a4be72050..f22f89923 100644
--- a/modules/prog-general.el
+++ b/modules/prog-general.el
@@ -59,25 +59,44 @@
(declare-function treesit-auto-add-to-auto-mode-alist "treesit-auto")
(declare-function treesit-auto-recipe-lang "treesit-auto")
(declare-function highlight-indent-guides-mode "highlight-indent-guides")
+(declare-function electric-pair-default-inhibit "elec-pair")
+(declare-function yas-reload-all "yasnippet")
+(declare-function yas-activate-extra-mode "yasnippet")
;; Forward declarations for treesit-auto variables
(defvar treesit-auto-recipe-list)
+(defvar electric-pair-inhibit-predicate)
;; Forward declarations for functions defined later in this file
-(declare-function cj/find-project-root-file "prog-general")
(declare-function cj/project-switch-actions "prog-general")
-(declare-function cj/deadgrep--initial-term "prog-general")
+
+(defun cj/find-project-root-file (regexp)
+ "Return first file in the current Projectile project root matching REGEXP.
+
+Match is done against (downcase file) for case-insensitivity.
+REGEXP must be a string or an rx form."
+ (when-let ((root (projectile-project-root)))
+ (seq-find (lambda (file)
+ (string-match-p (if (stringp regexp)
+ regexp
+ (rx-to-string regexp))
+ (downcase file)))
+ (directory-files root))))
(declare-function cj/highlight-indent-guides-disable-in-non-prog-modes "prog-general")
;; --------------------- General Programming Mode Settings ---------------------
;; keybindings, minor-modes, and prog-mode settings
+;; Set the line-number type and width before any prog buffer enables
+;; display-line-numbers-mode. Setting them inside the hook ran after the mode
+;; turned on, so the first prog buffer of a session got absolute numbers.
+(setq display-line-numbers-type 'relative) ;; numbers relative to point
+(setq-default display-line-numbers-width 3) ;; 3 chars reserved for numbers
+
(defun cj/general-prog-settings ()
"Keybindings, minor modes, and settings for programming mode."
(interactive)
(display-line-numbers-mode) ;; show line numbers
- (setq display-line-numbers-type 'relative) ;; display numbers relative to 'the point'
- (setq-default display-line-numbers-width 3) ;; 3 characters reserved for line numbers
(turn-on-visual-line-mode) ;; word-wrapping
(auto-fill-mode) ;; auto wrap at the fill column set
(local-set-key (kbd "M-;") 'comment-dwim) ;; comment/uncomment region as appropriate
@@ -173,19 +192,6 @@ reuses the current window otherwise, matching `cj/open-project-root-todo'."
:config
(require 'seq)
- (defun cj/find-project-root-file (regexp)
- "Return first file in the current Projectile project root matching REGEXP.
-
-Match is done against (downcase file) for case-insensitivity.
-REGEXP must be a string or an rx form."
- (when-let ((root (projectile-project-root)))
- (seq-find (lambda (file)
- (string-match-p (if (stringp regexp)
- regexp
- (rx-to-string regexp))
- (downcase file)))
- (directory-files root))))
-
(defun cj/open-project-root-todo ()
"Open todo.org in the current Projectile project root.
@@ -229,6 +235,23 @@ If no such file exists there, display a message."
;; ---------------------------------- Ripgrep ----------------------------------
+(declare-function deadgrep "deadgrep")
+
+(defun cj/deadgrep--initial-term ()
+ "Return the region text or the symbol at point, to seed a Deadgrep search."
+ (cond
+ ((use-region-p)
+ (buffer-substring-no-properties (region-beginning) (region-end)))
+ (t (thing-at-point 'symbol t))))
+
+(defun cj/--deadgrep-run (root &optional term)
+ "Run Deadgrep for TERM under directory ROOT.
+ROOT is normalized to a directory name; TERM defaults to a minibuffer read
+seeded by `cj/deadgrep--initial-term'. Shared tail of the deadgrep commands."
+ (let ((root (file-name-as-directory (expand-file-name root)))
+ (term (or term (read-from-minibuffer "Search: " (cj/deadgrep--initial-term)))))
+ (deadgrep term root)))
+
(use-package deadgrep
:after projectile
:bind
@@ -239,12 +262,6 @@ If no such file exists there, display a message."
:config
(require 'thingatpt)
- (defun cj/deadgrep--initial-term ()
- (cond
- ((use-region-p)
- (buffer-substring-no-properties (region-beginning) (region-end)))
- (t (thing-at-point 'symbol t))))
-
(defun cj/deadgrep-here (&optional term)
"Search with Deadgrep in the most relevant directory at point."
(interactive)
@@ -261,21 +278,28 @@ If no such file exists there, display a message."
(buffer-file-name
(file-name-directory (file-truename buffer-file-name)))
(t default-directory)))
- (root (file-name-as-directory (expand-file-name root)))
- (term (or term (read-from-minibuffer "Search: " (cj/deadgrep--initial-term)))))
- (deadgrep term root)))
+ )
+ (cj/--deadgrep-run root term)))
(defun cj/deadgrep-in-dir (&optional dir term)
"Prompt for a directory, then search there with Deadgrep."
(interactive)
- (let* ((dir (or dir (read-directory-name "Search in directory: " default-directory nil t)))
- (dir (file-name-as-directory (expand-file-name dir)))
- (term (or term (read-from-minibuffer "Search: " (cj/deadgrep--initial-term)))))
- (deadgrep term dir))))
+ (let ((dir (or dir (read-directory-name "Search in directory: " default-directory nil t))))
+ (cj/--deadgrep-run dir term))))
(with-eval-after-load 'dired
(keymap-set dired-mode-map "G" #'cj/deadgrep-here))
+;; ------------------------------------ wgrep ----------------------------------
+;; Make a grep buffer editable, then write the edits back across files -- turns
+;; a consult-grep / embark-export result into a project-wide find-and-replace.
+;; In a grep buffer: C-c C-p to start editing, C-c C-c to apply.
+
+(use-package wgrep
+ :custom
+ (wgrep-auto-save-buffer t) ;; save the touched files when applying
+ (wgrep-change-readonly-file t)) ;; let edits flow into read-only buffers
+
;; ---------------------------------- Snippets ---------------------------------
;; reusable code and text
@@ -298,6 +322,22 @@ This is what makes universal snippets like =<cj= work in any buffer."
(yas-reload-all)
(yas-global-mode 1))
+;; Most of the snippet keys start with "<" (=<cj=, =<for=, =<main=…), mirroring
+;; org-tempo. But `electric-pair-mode' pairs "<" into "<>" wherever the mode's
+;; syntax table gives "<" paren syntax (org, and the prog modes that enable
+;; pairing), so typing "<cj" lands as "<cj>"; expanding the "<cj" key then
+;; strands the ">" after the snippet — the cj-comment fence comes out as
+;; "#+end_src>", which breaks the cj-scan fence parser. Inhibit pairing for the
+;; open angle bracket globally; defer to the default for every other character.
+(defun cj/--electric-pair-inhibit-angle (char)
+ "Return non-nil to stop `electric-pair-mode' from pairing the angle CHAR.
+Inhibit the open angle bracket so \"<\"-prefixed yasnippet keys expand cleanly;
+defer to `electric-pair-default-inhibit' for any other CHAR."
+ (or (eq char ?<)
+ (electric-pair-default-inhibit char)))
+
+(setq electric-pair-inhibit-predicate #'cj/--electric-pair-inhibit-angle)
+
;; --------------------- Display Color On Color Declaration --------------------
;; display the actual color as highlight to color hex code
@@ -320,14 +360,9 @@ This is what makes universal snippets like =<cj= work in any buffer."
(use-package highlight-indent-guides
:hook (prog-mode . cj/highlight-indent-guides-enable)
:config
- ;; Disable auto face coloring to use explicit faces for better visibility across themes
+ ;; Disable auto face coloring; the guide faces are left to the theme
(setq highlight-indent-guides-auto-enabled nil)
- ;; Set explicit face backgrounds and foreground for the indentation guides
- (set-face-background 'highlight-indent-guides-odd-face "darkgray")
- (set-face-background 'highlight-indent-guides-even-face "darkgray")
- (set-face-foreground 'highlight-indent-guides-character-face "dimgray")
-
(defun cj/highlight-indent-guides-enable ()
"Enable highlight-indent-guides with preferred settings for programming modes."
(setq-local highlight-indent-guides-method 'bitmap)
@@ -349,16 +384,7 @@ This is what makes universal snippets like =<cj= work in any buffer."
(use-package hl-todo
:defer 1
:hook
- (prog-mode . hl-todo-mode)
- :config
- (setq hl-todo-keyword-faces
- '(("FIXME" . "#FF0000")
- ("BUG" . "#FF0000")
- ("HACK" . "#FF0000")
- ("ISSUE" . "#DAA520")
- ("TASK" . "#DAA520")
- ("NOTE" . "#2C780E")
- ("WIP" . "#1E90FF"))))
+ (prog-mode . hl-todo-mode))
;; --------------------------- Whitespace Management ---------------------------
;; trims trailing whitespace only from lines you've modified when saving buffer