Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
ai-code/ai-code-input.el at main · emacsmirror/ai-code · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
emacsmirror
ai-code
Repository navigation
Code
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
ai-code
/
ai-code-input.el
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
162 lines (146 loc) · 6.94 KB
main
Breadcrumbs
ai-code
/
ai-code-input.el
Copy path
Top
File metadata and controls
Code
Blame
162 lines (146 loc) · 6.94 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
;
;; ai-code-input.el --- Helm completion for ai-code-interface.el -*-
lexical-binding
:
t
; -*-
;
; Author: Kang Tu <tninja@gmail.com>
;
; Keywords: convenience, tools
;
; URL: https://github.com/tninja/ai-code-interface.el
;
; SPDX-License-Identifier: Apache-2.0
;
;; Commentary:
;
; Optional Helm completion interface for ai-code-interface.el
;
; To use this, ensure both ai-code-interface.el and helm are installed.
;
;; Code:
(
require
'cl-lib
)
;
For `cl-subseq`
(
require
'imenu
)
(
declare-function
helm-comp-read
"
helm-mode
"
(prompt collection
&rest
args))
;;;###
autoload
(
defun
ai-code-plain-read-string
(
prompt
&optional
initial-input
candidate-list
)
"
Read a string from the user with PROMPT and optional INITIAL-INPUT.
CANDIDATE-LIST provides additional completion options if provided.
This function combines candidate-list with history for better completion.
"
;
; Combine candidate-list with history, removing duplicates
(
let
((completion-candidates
(
delete-dups
(
append
candidate-list
(
when
(
boundp
'ai-code-read-string-history
)
ai-code-read-string-history)))))
;
; Use completing-read with the combined candidates
(
completing-read
prompt
completion-candidates
nil
nil
initial-input
'ai-code-read-string-history
)))
;;;###
autoload
(
defalias
'ai-code-read-string
#
'ai-code-plain-read-string
)
(
defun
ai-code-helm-read-string-with-history
(
prompt
history-file-name
&optional
initial-input
candidate-list
)
"
Read a string with Helm completion using specified history file.
PROMPT is the prompt string.
HISTORY-FILE-NAME is the base name for history file.
INITIAL-INPUT is optional initial input string.
CANDIDATE-LIST is an optional list of candidate strings to show before history.
"
;
; Load history from file
(
let*
((helm-history-file (
expand-file-name
history-file-name user-emacs-directory))
(helm-history (
if
(
file-exists-p
helm-history-file)
(
with-temp-buffer
(
insert-file-contents
helm-history-file)
(
read
(
buffer-string
)))
;
Assumed newest first
'()))
;
; Use only Helm history, no CLI history
(history helm-history)
;
; Extract the most recent item from history (if exists)
(most-recent (
when
history
(
car
history)))
;
; Remove the first item to add it back later
(rest-history (
when
history
(
cl-remove-duplicates
(
cdr
history)
:test
#
'equal
)))
;
; Combine completion list: most recent + candidates + separator + rest of history
(completion-list
(
append
;
; If most recent item exists, put it at the top
(
when
most-recent
(
list
most-recent))
;
; Add candidate list
(
or
candidate-list '())
;
; Add separator and rest of history
(
when
rest-history
(
cons
"
==================== HISTORY ========================================
"
rest-history))))
;
; Read input with helm
(input (helm-comp-read
prompt
completion-list
:must-match
nil
:name
"
Helm Read String, Use C-c C-y to edit selected command. C-b and C-f to move cursor during editing
"
:fuzzy
nil
:initial-input
initial-input)))
;
; Add to history if non-empty, single-line and save
(
unless
(
or
(
string-empty-p
input) (
string-match
"
\n
"
input))
(
push
input history)
;
; (setq history (mapcar #'substring-no-properties history))
(
with-temp-file
helm-history-file
;
Save to the Helm-specific history file
(
let
((history-entries (
cl-subseq
history
0
(
min
(
length
history)
1000
))))
;
Keep last 1000 entries
(
insert
(
let
((
print-circle
nil
))
(
prin1-to-string
history-entries))))))
input))
(
defun
ai-code-helm-read-string
(
prompt
&optional
initial-input
candidate-list
)
"
Read a string with Helm completion for ai-code, showing historical inputs.
PROMPT is the prompt string.
INITIAL-INPUT is optional initial input string.
CANDIDATE-LIST is an optional list of candidate strings to show before history.
"
(ai-code-helm-read-string-with-history prompt
"
ai-code-helm-read-string-history.el
"
initial-input candidate-list))
;;;###
autoload
(
if
(
featurep
'helm
)
(
defalias
'ai-code-read-string
#
'ai-code-helm-read-string
))
(
defun
ai-code--get-window-files
()
"
Get a list of unique file paths from all visible windows.
"
(
let
((
files
nil
))
(
dolist
(window (
window-list
))
(
let
((buffer (
window-buffer
window)))
(
when
(
and
buffer (
buffer-file-name
buffer))
(
cl-pushnew
(
buffer-file-name
buffer)
files
:test
#
'string=
))))
files
))
(
defun
ai-code--get-context-files-string
()
"
Get a string of files in the current window for context.
The current buffer's file is always first.
"
(
if
(
not
buffer-file-name)
"
"
(
let*
((current-buffer-file-name buffer-file-name)
(all-buffer-files (ai-code--get-window-files))
(other-buffer-files (
remove
current-buffer-file-name all-buffer-files))
(sorted-files (
cons
current-buffer-file-name other-buffer-files)))
(
if
sorted-files
(
concat
"
\n
Files:
\n
"
(
mapconcat
#
'identity
sorted-files
"
\n
"
))
"
"
))))
(
defun
ai-code--flatten-imenu-index
(
index
)
"
Flatten imenu INDEX alist into a list of strings.
"
(
let
(result)
(
dolist
(item index)
(
when
(
consp
item)
(
let
((name (
car
item))
(payload (
cdr
item)))
(
cond
((
and
(
listp
payload) (
consp
(
car
payload)))
;
; Nested list (category or sub-index)
(
setq
result (
append
result (ai-code--flatten-imenu-index payload))))
((
stringp
name)
(
push
name result))))))
result))
(
defun
ai-code--get-functions-from-buffer
(
buffer
)
"
Get a list of function/symbol names from BUFFER using imenu.
"
(
with-current-buffer
buffer
(
when
(
derived-mode-p
'prog-mode
)
(
condition-case
nil
(
let
((imenu-auto-rescan
t
)
(index (
imenu--make-index-alist
t
)))
(ai-code--flatten-imenu-index index))
(
error
nil
)))))
;;;###
autoload
(
defun
ai-code-insert-function-at-point
()
"
Insert a function name selected from current windows' prog-mode buffers.
"
(
interactive
)
(
let
((functions
nil
))
(
dolist
(window (
window-list
))
(
let
((buffer (
window-buffer
window)))
(
setq
functions (
append
(ai-code--get-functions-from-buffer buffer) functions))))
(
setq
functions (
sort
(
delete-dups
(
cl-remove-if-not
#
'stringp
functions))
#
'string<
))
(
let
((selected (
completing-read
"
Insert function:
"
functions
nil
nil
)))
(
when
(
and
selected (
not
(
string-empty-p
selected)))
(
insert
selected)))))
(
provide
'ai-code-input
)
;
;; ai-code-input.el ends here
You can’t perform that action at this time.