diff options
| -rw-r--r-- | README.md | 68 | ||||
| -rw-r--r-- | claude-code-context.el | 49 | ||||
| -rw-r--r-- | claude-code-hook-example.json | 2 |
3 files changed, 60 insertions, 59 deletions
@@ -49,7 +49,7 @@ Add to your `init.el`: (setq claude-code-context-update-interval 3) ;; Optional: customize the context file location -(setq claude-code-context-file "~/.emacs.d/claude-context.txt") +(setq claude-code-context-file "~/.emacs.d/claude-code-context.json") ``` ### Claude Code Hook Setup @@ -64,7 +64,7 @@ Add this hook configuration to your `~/.claude/settings.json`: "hooks": [ { "type": "command", - "command": "CONTEXT_FILE=\"$HOME/.emacs.d/claude-context.txt\"; if [ -f \"$CONTEXT_FILE\" ]; then echo \"\\n---\\n## Emacs Context\\n\"; cat \"$CONTEXT_FILE\"; echo \"\\n---\"; fi" + "command": "CONTEXT_FILE=\"$HOME/.emacs.d/claude-code-context.json\"; if [ -f \"$CONTEXT_FILE\" ]; then echo \"\\n---\\n## Emacs Context\\n\"; cat \"$CONTEXT_FILE\"; echo \"\\n---\"; fi" } ] } @@ -79,7 +79,7 @@ See [claude-code-hook-example.json](claude-code-hook-example.json) for a complet ### Automatic Mode -When `claude-code-context-mode` is enabled, your current buffer context is automatically written to `~/.emacs.d/claude-context.txt` every time Emacs is idle for the configured interval. +When `claude-code-context-mode` is enabled, your current buffer context is automatically written to `~/.emacs.d/claude-code-context.json` every time Emacs is idle for the configured interval. ### Manual Commands @@ -101,38 +101,46 @@ When `claude-code-context-mode` is enabled, your current buffer context is autom ## Context Format -The context file includes: +The context file is JSON format for easy parsing and includes: -- **File**: Full path to the current buffer's file -- **Line**: Current line number -- **Column**: Current column number -- **Selection**: Any highlighted text (if region is active) -- **Diagnostics**: Flymake errors/warnings (when using `C-c C-l d`) +- **file**: Full path to the current buffer's file +- **line**: Current line number +- **column**: Current column number +- **modified**: Boolean indicating if the buffer has unsaved changes +- **selection**: Any highlighted text (if region is active) +- **diagnostics**: Flymake errors/warnings (when using `C-c C-l d`) Example context: -``` -# Emacs Context for Claude Code -# This file is automatically updated by Emacs - -File: /Users/username/project/src/main.py -Line: 42, Column: 8 -Selection: -``` -def process_data(items): - return [x * 2 for x in items] -``` - -Flymake Diagnostics: - 45 error e-f-b-c undefined variable 'result' - 52 warning e-f-b-c unused variable 'temp' +```json +{ + "file": "/Users/username/project/src/main.py", + "line": 42, + "column": 8, + "modified": true, + "selection": "def process_data(items):\n return [x * 2 for x in items]", + "diagnostics": [ + { + "line": 45, + "type": "error", + "backend": "eglot", + "text": "undefined variable 'result'" + }, + { + "line": 52, + "type": "warning", + "backend": "eglot", + "text": "unused variable 'temp'" + } + ] +} ``` ## Customization ### Variables -- `claude-code-context-file`: Location of the context file (default: `~/.emacs.d/claude-context.txt`) +- `claude-code-context-file`: Location of the context file (default: `~/.emacs.d/claude-code-context.json`) - `claude-code-context-update-interval`: Idle time in seconds before updating context (default: 2) ### Custom Keybindings @@ -146,15 +154,15 @@ If you prefer different keybindings: ## How It Works -1. **Emacs side**: This package writes your current buffer context to a file (`~/.emacs.d/claude-context.txt`) -2. **Claude Code side**: A hook in Claude Code's settings reads this file before every prompt submission -3. **Integration**: Claude Code automatically receives your Emacs context with each query +1. **Emacs side**: This package writes your current buffer context to a JSON file (`~/.emacs.d/claude-code-context.json`) +2. **Claude Code side**: A hook in Claude Code's settings reads this JSON file before every prompt submission +3. **Integration**: Claude Code automatically receives and parses your Emacs context with each query ## Troubleshooting **Context not appearing in Claude Code:** - Verify the hook is properly configured in `~/.claude/settings.json` -- Check that the context file exists: `cat ~/.emacs.d/claude-context.txt` +- Check that the context file exists: `cat ~/.emacs.d/claude-code-context.json` - Make sure `claude-code-context-mode` is enabled in Emacs **Performance issues:** @@ -171,4 +179,4 @@ GPL-3.0-or-later ## Author -Luke Hoersten <[email protected]> +Luke Hoersten <[email protected]> diff --git a/claude-code-context.el b/claude-code-context.el index 95168c1..db4faab 100644 --- a/claude-code-context.el +++ b/claude-code-context.el @@ -42,7 +42,7 @@ ;; "hooks": [ ;; { ;; "type": "command", -;; "command": "CONTEXT_FILE=\"$HOME/.emacs.d/claude-context.txt\"; if [ -f \"$CONTEXT_FILE\" ]; then echo \"\\n---\\n## Emacs Context\\n\"; cat \"$CONTEXT_FILE\"; echo \"\\n---\"; fi" +;; "command": "CONTEXT_FILE=\"$HOME/.emacs.d/claude-code-context.json\"; if [ -f \"$CONTEXT_FILE\" ]; then echo \"\\n---\\n## Emacs Context\\n\"; cat \"$CONTEXT_FILE\"; echo \"\\n---\"; fi" ;; } ;; ] ;; } @@ -64,7 +64,7 @@ :prefix "claude-code-context-") (defcustom claude-code-context-file - (expand-file-name "claude-context.txt" user-emacs-directory) + (expand-file-name "claude-code-context.json" user-emacs-directory) "File where Claude Code context is written." :type 'file :group 'claude-code-context) @@ -78,36 +78,33 @@ "Timer for updating Claude Code context.") (defun claude-code--get-current-context () - "Get current buffer context as a string." + "Get current buffer context as an alist." (when (buffer-file-name) (let* ((file (buffer-file-name)) (line (line-number-at-pos)) (col (current-column)) + (modified (buffer-modified-p)) (selection (when (use-region-p) (buffer-substring-no-properties (region-beginning) (region-end))))) - (concat - (format "File: %s\n" file) - (format "Line: %d, Column: %d\n" line col) - (when selection - (format "Selection:\n```\n%s\n```\n" selection)))))) + `((file . ,file) + (line . ,line) + (column . ,col) + (modified . ,(if modified t :json-false)) + ,@(when selection `((selection . ,selection))))))) (defun claude-code--get-flymake-diagnostics () - "Get flymake diagnostics for current buffer." + "Get flymake diagnostics for current buffer as a list." (when (and (bound-and-true-p flymake-mode) (buffer-file-name)) (let ((diags (flymake-diagnostics))) (when diags - (concat - "\nFlymake Diagnostics:\n" - (mapconcat - (lambda (diag) - (format " %4d %8s %-8s %s" - (line-number-at-pos (flymake-diagnostic-beg diag)) - (flymake-diagnostic-type diag) - (flymake-diagnostic-backend diag) - (flymake-diagnostic-text diag))) - diags - "\n")))))) + (mapcar + (lambda (diag) + `((line . ,(line-number-at-pos (flymake-diagnostic-beg diag))) + (type . ,(symbol-name (flymake-diagnostic-type diag))) + (backend . ,(symbol-name (flymake-diagnostic-backend diag))) + (text . ,(flymake-diagnostic-text diag)))) + diags))))) (defun claude-code-update-context () "Update Claude Code context file with current buffer state." @@ -115,9 +112,7 @@ (let ((context (claude-code--get-current-context))) (when context (with-temp-file claude-code-context-file - (insert "# Emacs Context for Claude Code\n") - (insert "# This file is automatically updated by Emacs\n\n") - (insert context)) + (insert (json-encode context))) (message "Claude Code context updated")))) (defun claude-code-add-diagnostics () @@ -126,12 +121,10 @@ (let ((context (claude-code--get-current-context)) (diags (claude-code--get-flymake-diagnostics))) (when context + (when diags + (setq context (append context `((diagnostics . ,diags))))) (with-temp-file claude-code-context-file - (insert "# Emacs Context for Claude Code\n") - (insert "# This file is automatically updated by Emacs\n\n") - (insert context) - (when diags - (insert diags))) + (insert (json-encode context))) (message "Claude Code context updated with diagnostics")))) (defun claude-code-clear-context () diff --git a/claude-code-hook-example.json b/claude-code-hook-example.json index 3e4c1a0..8cc880f 100644 --- a/claude-code-hook-example.json +++ b/claude-code-hook-example.json @@ -7,7 +7,7 @@ "hooks": [ { "type": "command", - "command": "CONTEXT_FILE=\"$HOME/.emacs.d/claude-context.txt\"; if [ -f \"$CONTEXT_FILE\" ]; then echo \"\\n---\\n## Emacs Context\\n\"; cat \"$CONTEXT_FILE\"; echo \"\\n---\"; fi" + "command": "CONTEXT_FILE=\"$HOME/.emacs.d/claude-code-context.json\"; if [ -f \"$CONTEXT_FILE\" ]; then echo \"\\n---\\n## Emacs Context\\n\"; cat \"$CONTEXT_FILE\"; echo \"\\n---\"; fi" } ] } |
