karthink / gptel

A simple LLM client for Emacs
GNU General Public License v3.0
1.65k stars 154 forks source link

Add `gptel--get-prompt-from-region` function #359

Open eval-exec opened 3 months ago

eval-exec commented 3 months ago

When using a region as a prompt, I suggest extract it to independent function:gptel--get-prompt-from-region.

Then user will be convinient to hack the function. For example, I can add markdown code fence symbol around prompt by:

  (defun exec/major-mod-to-markdown-lang()
    (let* ((major
            (car (split-string (symbol-name major-mode) "-"))))
      (cond ((string= major "emacs")
             "elisp")
            (t major))))

  (require 'gptel-transient)

  (defun gptel--get-prompt-from-region()
    "Get the prompt from the region."
    (format "\n```%s\n%s\n```\n"
            ;; major mode symbol, if rust-ts-mode, then rust
            (exec/major-mod-to-markdown-lang)
            (buffer-substring-no-properties
             (region-beginning) (region-end))))