Skip to content

Commit bd2ec81

Browse files
tninjaCopilot
andauthored
Feat: Add toggle buffer window dedication command (#69)
* add example games * Improve Connect4 AI with minimax, alpha-beta pruning, and smarter evaluation function * Add toggle buffer window dedication command * Update examples/battleship/game.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update examples/battleship/game.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update examples/battleship/game.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update examples/connect4/test_game.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update examples/battleship/test_game.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update examples/battleship/test_game.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update examples/battleship/test_fleet.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update examples/battleship/game.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update examples/battleship/game.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update examples/battleship/fleet.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * bump version. add entry in HISTORY --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent e17431a commit bd2ec81

File tree

13 files changed

+1284
-45
lines changed

13 files changed

+1284
-45
lines changed

HISTORY.org

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
** Main branch change
55

6+
- Feat: Add toggle buffer window dedication command
7+
- Chore: Make ai-code-implement-todo flexible to modify code in the context. When it is a pure new code block, place it after the comment.
68
- Feat: Improve Implement TODO feature
79
- ai-code-implement-todo (C-a i) on blank line will ask user and insert TODO comment.
810
- After ai-code-implement-todo (C-a i) finishing code generation, mark TODO as DONE, instead of delete it

ai-code-change.el

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -110,43 +110,6 @@ Returns (TEXT START-POS END-POS) if TODO found, nil otherwise."
110110
(if region-active (region-beginning) (line-beginning-position))
111111
(if region-active (region-end) (line-end-position))))))))))
112112

113-
(defun ai-code--handle-todo-implementation (todo-info arg)
114-
"Handle TODO implementation with given TODO-INFO.
115-
ARG is the prefix argument for clipboard context."
116-
(let* ((clipboard-context (when arg (ai-code--get-clipboard-text)))
117-
(todo-content (nth 0 todo-info))
118-
(todo-region-beg (nth 1 todo-info))
119-
(todo-region-end (nth 2 todo-info))
120-
(region-location-info (ai-code--get-region-location-info todo-region-beg todo-region-end))
121-
(files-context-string (ai-code--get-context-files-string))
122-
(function-name (ai-code--get-function-name-for-comment))
123-
(function-context (if function-name
124-
(format "\nFunction: %s" function-name)
125-
""))
126-
(prompt-label (if (and clipboard-context
127-
(string-match-p "\\S-" clipboard-context))
128-
"Implement TODO in place (clipboard context): "
129-
"Implement TODO in place: "))
130-
(initial-prompt
131-
(format (concat "Please implement the requirement from the following TODO comment. "
132-
"After implementation, mark the original TODO comment as 'DONE'. "
133-
"For example, a comment `;; TODO: new feature` could become `;; DONE: new feature`.\n"
134-
"The TODO comment to implement is inside file %s.\n\n"
135-
"Context about the location:\n%s\n\n"
136-
"The TODO comment content:\n```\n%s\n```\n%s%s")
137-
(file-name-nondirectory buffer-file-name)
138-
region-location-info
139-
todo-content
140-
function-context
141-
files-context-string))
142-
(prompt (ai-code-read-string prompt-label initial-prompt))
143-
(final-prompt
144-
(concat prompt
145-
(when (and clipboard-context
146-
(string-match-p "\\S-" clipboard-context))
147-
(concat "\n\nClipboard context:\n" clipboard-context)))))
148-
(ai-code--insert-prompt final-prompt)))
149-
150113
(defun ai-code--generate-prompt-label (clipboard-context region-active function-name)
151114
"Generate appropriate prompt label based on context."
152115
(cond
@@ -219,7 +182,7 @@ Argument ARG is the prefix argument."
219182
(let* ((region-active (region-active-p))
220183
(todo-info (ai-code--detect-todo-info region-active)))
221184
(if todo-info
222-
(ai-code--handle-todo-implementation todo-info arg)
185+
(ai-code-implement-todo arg)
223186
(ai-code--handle-regular-code-change arg region-active))))
224187

225188
;;;###autoload
@@ -356,13 +319,13 @@ ARG is the prefix argument for clipboard context."
356319
(region-text
357320
(unless (ai-code--is-comment-block region-text)
358321
(user-error "Selected region must be a comment block"))
359-
(format (concat
360-
"Please implement code after this requirement comment block in the selected region. "
361-
"Keep the comment in place and ensure it begins with a DONE prefix (change TODO to DONE or prepend DONE if no prefix) before adding the implementation code after it. "
362-
"Keep the existing code structure and add the implementation after this specific block.\n%s\n%s%s%s")
322+
(format (concat
323+
"Please implement code for this requirement comment block in the selected region. "
324+
"Keep the comment in place and ensure it begins with a DONE prefix (change TODO to DONE or prepend DONE if no prefix). "
325+
"If this is a pure new code block, place it after the comment; otherwise keep the existing structure and make corresponding change for the context.\n%s\n%s%s%s")
363326
region-location-line region-text function-context files-context-string))
364327
(is-comment
365-
(format "Please implement code after this requirement comment on line %d: '%s'. Keep the comment in place and ensure it begins with a DONE prefix (change TODO to DONE or prepend DONE if needed) before adding the implementation code after it. Keep the existing code structure and add the implementation after this specific comment.%s%s"
328+
(format "Please implement code for this requirement comment on line %d: '%s'. Keep the comment in place and ensure it begins with a DONE prefix (change TODO to DONE or prepend DONE if needed). If this is a pure new code block, place it after the comment; otherwise keep the existing structure and make corresponding change for the context.%s%s"
366329
current-line-number current-line function-context files-context-string))
367330
;; (function-name
368331
;; (format "Please implement code after all TODO comments in function '%s'. The TODOs are TODO comments. Keep each comment in place and ensure each begins with a DONE prefix (change TODO to DONE or prepend DONE if needed) before adding implementation code after it. Keep the existing code structure and only add code after these marked items.%s"

ai-code-file.el

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,28 @@ Includes stored context entries for the current Git repository if available."
429429
(reverse entries)
430430
"\n"))))))))
431431

432+
;;;###autoload
433+
(defun ai-code-toggle-current-buffer-dedicated (arg)
434+
"Toggle the dedicated state of the current buffer's window.
435+
When a window is dedicated, Emacs will not automatically reuse it for displaying other buffers.
436+
With prefix ARG (C-u), toggle dedication for every window in the current frame."
437+
;; DONE: if C-u pressed, toggle dedicate to all buffers inside current window
438+
(interactive "P")
439+
(let* ((targets (if arg
440+
(window-list nil 'no-minibuf)
441+
(list (selected-window))))
442+
(results '()))
443+
(dolist (window targets)
444+
(let ((buffer (window-buffer window)))
445+
(when (buffer-file-name buffer)
446+
(let ((new-status (not (window-dedicated-p window))))
447+
(set-window-dedicated-p window new-status)
448+
(push (format "%s: %s" (buffer-name buffer) (if new-status "dedicated" "free")) results)))))
449+
;; DONE: show each affected buffer name and dedicate status
450+
(if results
451+
(message "Window dedication updated: %s" (mapconcat #'identity (nreverse results) ", "))
452+
(message "No file buffers found in target windows"))))
453+
432454
(provide 'ai-code-file)
433455

434456
;;; ai-code-file.el ends here

ai-code-interface.el

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
;;; ai-code-interface.el --- AI code interface for editing AI prompt files -*- lexical-binding: t; -*-
22

33
;; Author: Kang Tu <tninja@gmail.com>
4-
;; Version: 0.61
4+
;; Version: 0.62
55
;; Package-Requires: ((emacs "26.1") (transient "0.8.0") (magit "2.1.0"))
66

77
;; SPDX-License-Identifier: Apache-2.0
@@ -174,7 +174,8 @@ Shows the current backend label to the right."
174174
("e" "Debug exception (C-u: clipboard)" ai-code-investigate-exception)
175175
("f" "Fix Flycheck errors in scope" ai-code-flycheck-fix-errors-in-scope)
176176
("k" "Copy Cur File Name (C-u: full)" ai-code-copy-buffer-file-name-to-clipboard)
177-
("o" "Open Clipboard file dir" ai-code-open-clipboard-file-path-as-dired)
177+
("d" "Toggle current buffer dedicated" ai-code-toggle-current-buffer-dedicated)
178+
;; ("o" "Open Clipboard file dir" ai-code-open-clipboard-file-path-as-dired)
178179
("m" "Debug python MCP server" ai-code-debug-mcp)
179180
("n" "Take notes from AI session region" ai-code-take-notes)
180181
]

examples/README.org

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Two games were written with the assistance of aider / aider.el
3+
4+
- [[./battleship][battleship]] - https://en.wikipedia.org/wiki/Battleship_(game)
5+
- 2.5h to write the game with human vs. human mode
6+
- using DeepSeek model
7+
- 1.5h to iterate and add human vs. computer mode
8+
- using Claude model
9+
- 1h to have a better AI
10+
- using OpenAI o4-mini model
11+
- [[./connect4][connect4]] - https://en.wikipedia.org/wiki/Connect_Four
12+
- 0.5h to write the game with basic human vs. computer mode
13+
- 0.5h to iterate the AI and make it more effective
14+
- all using Claude model
15+
16+
- These games are CLI-based text games. In the future, I plan to create graphical versions using pygame, with the help of aider and aider.el

0 commit comments

Comments
 (0)