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
163
164
165
166
167
|
;;; prog-c --- C Programming Settings and Functionality -*- lexical-binding: t; coding: utf-8; -*-
;; author Craig Jennings <c@cjennings.net>
;;; Commentary:
;;
;; Layer: 3 (Domain Workflow).
;; Category: D/P.
;; Load shape: eager.
;; Eager reason: none necessary; currently eager but should load by C major mode
;; (Phase 6 deferral candidate).
;; Top-level side effects: six add-hook, package configuration via use-package.
;; Runtime requires: none (configures packages via use-package).
;; Direct test load: yes.
;;
;; Modern C programming environment with LSP, tree-sitter, debugging, and formatting.
;;
;; Installation:
;; sudo pacman -S clang # Provides clangd and clang-format
;;
;; LSP will provide:
;; - Intelligent code completion
;; - Jump to definition (M-.)
;; - Find references
;; - On-the-fly error checking
;; - Documentation on hover
;;
;; Workflow Example:
;; 1. Open a .c file → LSP auto-starts, provides completions
;; 2. F4 → compile + run dispatcher (dev-fkeys.el)
;; 3. S-F4 → recompile (repeat last)
;; 4. S-F6 → start GDB
;; 5. C-; f → format code with clang-format
;; 6. M-. → jump to function definition
;; 7. C-c l → LSP commands (rename, find references, etc.)
;;; Code:
(defvar c-ts-mode-map)
(defvar c-mode-base-map)
(defvar c-default-style)
;; Forward declarations for LSP
(declare-function lsp-deferred "lsp-mode")
(defvar lsp-idle-delay)
(defvar lsp-log-io)
(defvar lsp-enable-folding)
(defvar lsp-enable-snippet)
(defvar lsp-headerline-breadcrumb-enable)
;; Forward declarations for compile
(declare-function recompile "compile")
;; Forward declarations for system utilities
(declare-function cj/disabled "system-defaults")
(defvar clangd-path "clangd"
"Path to clangd language server executable.")
(defvar clang-format-path "clang-format"
"Path to clang-format executable.")
;; -------------------------------- C Mode Setup -------------------------------
;; preferences for C programming following common conventions
(defun cj/c-mode-settings ()
"Settings for C programming (works with both c-mode and c-ts-mode)."
(setq-local indent-tabs-mode nil) ;; use spaces, not tabs
(setq-local c-basic-offset 4) ;; 4 spaces per indent level
(setq-local tab-width 4) ;; tab displays as 4 spaces
(setq-local fill-column 80) ;; wrap at 80 columns
(setq-local comment-auto-fill-only-comments t) ;; only auto-fill inside comments
(auto-fill-mode) ;; auto-fill multiline comments
(electric-pair-mode) ;; automatic parenthesis pairing
;; Enable LSP if available
(when (and (fboundp 'lsp-deferred)
(executable-find clangd-path))
(lsp-deferred)))
;; Apply to both legacy c-mode and modern c-ts-mode
(add-hook 'c-mode-hook 'cj/c-mode-settings)
(add-hook 'c-ts-mode-hook 'cj/c-mode-settings)
;; Set default C style globally (before modes load)
(setq c-default-style '((c-mode . "stroustrup")
(c-ts-mode . "stroustrup")
(other . "gnu")))
;; -------------------------------- LSP for C ----------------------------------
;; C-specific LSP configuration using clangd
;; Core LSP setup is in prog-general.el
(use-package lsp-mode
:hook ((c-mode c-ts-mode) . lsp-deferred)
:custom
(lsp-clients-clangd-executable clangd-path)
(lsp-clients-clangd-args '("--header-insertion-decorators=0"
"--clang-tidy"
"--completion-style=detailed"
"--background-index")))
;; ----------------------------- Code Formatting -------------------------------
;; Format C code using clang-format
(use-package clang-format
:if (executable-find clang-format-path)
:bind (:map c-mode-base-map
("C-; f" . clang-format-buffer)))
;; -------------------------------- Compilation --------------------------------
;; Smart compilation with project detection
(defun cj/c--single-file-compile-command (file)
"Return a GCC compile command for C source FILE.
The output binary is FILE without its extension. Both paths are shell
quoted so spaces and shell metacharacters in filenames are handled."
(unless file
(user-error "C compile: buffer is not visiting a file"))
(format "gcc -Wall -Wextra -g -o %s %s"
(shell-quote-argument (file-name-sans-extension file))
(shell-quote-argument file)))
(defun cj/c-compile-command ()
"Set buffer-local compile command based on project structure."
(let* ((makefile (locate-dominating-file default-directory "Makefile"))
(cmakefile (locate-dominating-file default-directory "CMakeLists.txt")))
(cond
(makefile
(setq-local compile-command
(format "cd %s && make -k " (shell-quote-argument makefile))))
(cmakefile
(setq-local compile-command
(format "cd %s && cmake --build build " (shell-quote-argument cmakefile))))
(t
;; Single file compilation
(setq-local compile-command
(cj/c--single-file-compile-command buffer-file-name))))))
(add-hook 'c-mode-hook 'cj/c-compile-command)
(add-hook 'c-ts-mode-hook 'cj/c-compile-command)
;; -------------------------------- Debugging ----------------------------------
;; Enhanced GDB integration
(use-package gdb-mi
:ensure nil ;; built-in
:custom
(gdb-many-windows t) ;; Show multiple windows (source, locals, stack, etc.)
(gdb-show-main t) ;; Show main source window
(gdb-display-io-natively t)) ;; Display program I/O in separate window
;; -------------------------------- Keybindings --------------------------------
(defun cj/c-mode-keybindings ()
"Set up C-specific S-modifier overrides on the dev F-keys.
S-F4 (recompile) is global, owned by dev-fkeys.el — not duplicated here."
;; S-f5: Static analysis placeholder (could add clang-tidy, cppcheck, etc.)
(local-set-key (kbd "S-<f5>") #'cj/disabled)
;; S-f6: Debug with GDB
(local-set-key (kbd "S-<f6>") #'gdb))
(add-hook 'c-mode-hook 'cj/c-mode-keybindings)
(add-hook 'c-ts-mode-hook 'cj/c-mode-keybindings)
(provide 'prog-c)
;;; prog-c.el ends here
|