emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/gptel 6f7e005a8a 3/4: gptel-rewrite: New user option for r


From: ELPA Syncer
Subject: [nongnu] elpa/gptel 6f7e005a8a 3/4: gptel-rewrite: New user option for rewrite message
Date: Thu, 5 Sep 2024 15:59:47 -0400 (EDT)

branch: elpa/gptel
commit 6f7e005a8aae369357dac836583aadc7b7903f01
Author: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
Commit: Karthik Chikmagalur <karthikchikmagalur@gmail.com>

    gptel-rewrite: New user option for rewrite message
    
    * gptel-rewrite.el (gptel-rewrite-directives-hook,
    gptel-rewrite-menu): New user option
    `gptel-rewrite-directives-hook` for customizing the default
    rewrite/refactor directive.  Each function in the hook is called
    until one returns a non-nil value, the string to use as the
    directive in the current context.  This can be used to customize
    the refactoring prompt based on any predicate, such as the major
    mode or the contents of the buffer. (#370)
---
 gptel-rewrite.el | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/gptel-rewrite.el b/gptel-rewrite.el
index a76cbca744..bb6c6e4def 100644
--- a/gptel-rewrite.el
+++ b/gptel-rewrite.el
@@ -32,6 +32,20 @@
 
 (declare-function diff-no-select "diff")
 
+;; * User options
+
+(defcustom gptel-rewrite-directives-hook (list #'gptel--rewrite-message)
+  "Hook run to generate gptel's default rewrite directives.
+
+Each function in this hook is called with no arguments until one
+returns a non-nil value, the base string to use as the
+rewrite/refactor instruction.
+
+Use this hook to tailor context-specific refactoring directives.
+For example, you can specialize the default refactor directive
+for a particular major-mode."
+  :group 'gptel
+  :type 'hook)
 
 ;; * Variables
 
@@ -211,7 +225,7 @@ the changed regions. BUF is the (current) buffer."
    (lambda ()
      (format "Directive:  %s"
              (truncate-string-to-width
-              (or gptel--rewrite-message (gptel--rewrite-message))
+              gptel--rewrite-message
               (max (- (window-width) 14) 20) nil nil t)))
    (gptel--infix-rewrite-prompt)]
   [[:description "Diff Options"
@@ -232,7 +246,10 @@ the changed regions. BUF is the (current) buffer."
     (gptel--suffix-rewrite-clear)]]
   (interactive)
   (unless gptel--rewrite-message
-    (setq gptel--rewrite-message (gptel--rewrite-message)))
+    (setq gptel--rewrite-message
+          (save-mark-and-excursion
+            (run-hook-with-args-until-success
+             'gptel-rewrite-directives-hook))))
   (transient-setup 'gptel-rewrite-menu))
 
 ;; * Transient infixes for rewriting/refactoring
@@ -249,7 +266,11 @@ the changed regions. BUF is the (current) buffer."
   :prompt "Set directive for rewrite: "
   :reader (lambda (prompt _ history)
             (read-string
-             prompt (gptel--rewrite-message) history)))
+             prompt
+             (save-mark-and-excursion
+               (run-hook-with-args-until-success
+                'gptel-rewrite-directives-hook))
+             history)))
 
 (transient-define-argument gptel--rewrite-infix-diff:-U ()
   :description "Context lines"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]