-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodex-cli-utils.el
More file actions
118 lines (105 loc) · 3.97 KB
/
codex-cli-utils.el
File metadata and controls
118 lines (105 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
;;; codex-cli-utils.el --- Utility functions for codex-cli -*- lexical-binding: t; -*-
;; Author: Benn <bennmsg@gmail.com>
;; Maintainer: Benn <bennmsg@gmail.com>
;; SPDX-License-Identifier: MIT
;; Keywords: tools convenience codex codex-cli
;; URL: https://github.com/bennfocus/codex-cli.el
;;; Commentary:
;; Fence formatting, language guessing, echo-area progress, and ring for last
;; injected block for codex-cli.
;;; Code:
;; Per-session: make last block buffer-local so each Codex buffer
;; maintains its own resend history.
(defvar-local codex-cli--last-block ""
"The last block that was sent to the CLI (buffer-local).")
(defun codex-cli--store-last-block (text)
"Store TEXT as the last block sent."
(setq codex-cli--last-block text))
(defun codex-cli--get-last-block ()
"Return the last block that was sent."
codex-cli--last-block)
(defvar codex-cli--mode-language-map
'((emacs-lisp-mode . "elisp")
(lisp-interaction-mode . "elisp")
(python-mode . "python")
(js-mode . "javascript")
(js2-mode . "javascript")
(typescript-mode . "typescript")
(json-mode . "json")
(yaml-mode . "yaml")
(html-mode . "html")
(css-mode . "css")
(sh-mode . "bash")
(shell-script-mode . "bash")
(elixir-mode . "elixir")
(go-mode . "go")
(rust-mode . "rust")
(php-mode . "php")
(java-mode . "java")
(c-mode . "c")
(c++-mode . "cpp")
(sql-mode . "sql"))
"Map of major modes to language tags for fenced code blocks.")
(defvar codex-cli--extension-language-map
'(("el" . "elisp")
("py" . "python")
("js" . "javascript")
("ts" . "typescript")
("json" . "json")
("yaml" . "yaml")
("yml" . "yaml")
("html" . "html")
("css" . "css")
("sh" . "bash")
("bash" . "bash")
("ex" . "elixir")
("exs" . "elixir")
("go" . "go")
("rs" . "rust")
("php" . "php")
("java" . "java")
("c" . "c")
("cpp" . "cpp")
("cc" . "cpp")
("sql" . "sql"))
"Map of file extensions to language tags for fenced code blocks.")
(defun codex-cli--detect-language ()
"Detect the language tag for the current buffer.
Returns nil if language cannot be determined."
(or (cdr (assoc major-mode codex-cli--mode-language-map))
(when buffer-file-name
(let ((ext (file-name-extension buffer-file-name)))
(when ext
(cdr (assoc ext codex-cli--extension-language-map)))))))
(defun codex-cli--format-fenced-block (content &optional language filepath)
"Format CONTENT as a fenced code block with optional LANGUAGE tag.
If FILEPATH is provided, include a header with the file path."
(let ((lang-tag (or language ""))
(header (when filepath (format "# File: %s\n" filepath))))
(concat header
"```" lang-tag "\n"
content
(unless (string-suffix-p "\n" content) "\n")
"```")))
(defun codex-cli--detect-language-from-extension (extension)
"Detect language tag from file EXTENSION."
(when extension
(cdr (assoc extension codex-cli--extension-language-map))))
(defun codex-cli--log-buffer-name (proj-name &optional session)
"Return the log buffer name for PROJ-NAME and optional SESSION."
(if (and session (> (length session) 0))
(format "*codex-cli-log:%s:%s*" proj-name session)
(format "*codex-cli-log:%s*" proj-name)))
(defun codex-cli--log-injection (proj-name operation text &optional session)
"Log injection to the log buffer if enabled.
PROJ-NAME is the project name, OPERATION is the type of operation,
TEXT is the injected content, and SESSION is an optional session name."
(when (bound-and-true-p codex-cli-log-injections)
(let* ((log-buffer-name (codex-cli--log-buffer-name proj-name session))
(timestamp (format-time-string "%F %T"))
(log-entry (format "[%s] %s:\n%s\n\n" timestamp operation text)))
(with-current-buffer (get-buffer-create log-buffer-name)
(goto-char (point-max))
(insert log-entry)))))
(provide 'codex-cli-utils)
;;; codex-cli-utils.el ends here