summaryrefslogtreecommitdiff
path: root/.emacs.local
diff options
context:
space:
mode:
authorCori Barker <coribarker2@gmail.com>2026-02-19 20:44:16 +0000
committerCori Barker <coribarker2@gmail.com>2026-02-19 20:44:16 +0000
commiteffd0f6e45aef565869af1bcf07d43c6bc7df73b (patch)
tree81386e4822e7f8f0c6754a30acfebaa90140ddf0 /.emacs.local
[emacs] added dotfiles
Diffstat (limited to '.emacs.local')
-rw-r--r--.emacs.local/simpc-mode/LICENSE20
-rw-r--r--.emacs.local/simpc-mode/README.md61
-rw-r--r--.emacs.local/simpc-mode/simpc-mode.el127
3 files changed, 208 insertions, 0 deletions
diff --git a/.emacs.local/simpc-mode/LICENSE b/.emacs.local/simpc-mode/LICENSE
new file mode 100644
index 0000000..b196837
--- /dev/null
+++ b/.emacs.local/simpc-mode/LICENSE
@@ -0,0 +1,20 @@
+Copyright 2020 Alexey Kutepov <reximkut@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file
diff --git a/.emacs.local/simpc-mode/README.md b/.emacs.local/simpc-mode/README.md
new file mode 100644
index 0000000..7c879fa
--- /dev/null
+++ b/.emacs.local/simpc-mode/README.md
@@ -0,0 +1,61 @@
+# Simple C mode for Emacs
+
+Because whatever comes with Emacs is too slow.
+
+This mode does not try to be as featureful as `c-mode`, `c++-mode`,
+`cc-mode`, etc. Instead we are trying to implement a bare minimum
+syntax highlighting and indentation to maintain high performance on
+big files with long lines.
+
+The goal is to be able to comfortably browse and modify the following files:
+- [imgui](https://raw.githubusercontent.com/ocornut/imgui/fb7f6cab8c322731da336e553915e944bf386e62/imgui.h)
+- [amalgamated sqlite3.c](https://raw.githubusercontent.com/IreneKnapp/direct-sqlite/a74cc50c735053c7c49c487a66e7756b524db883/cbits/sqlite3.c)
+- [miniaudio.h](https://raw.githubusercontent.com/mackron/miniaudio/refs/heads/master/miniaudio.h)
+- ...
+
+Right now the only way to work with these files in Emacs is to use
+`text-mode`. Which is actually a good evidence that Emacs itself can
+handle such files! It's `c-mode` (and others) that cannot.
+
+## Installing locally
+
+Put [simpc-mode.el](./simpc-mode.el) to some folder `/path/to/simpc/`. Add this to your `.emacs`:
+
+```el
+;; Adding `/path/to/simpc` to load-path so `require` can find it
+(add-to-list 'load-path "/path/to/simpc/")
+;; Importing simpc-mode
+(require 'simpc-mode)
+;; Automatically enabling simpc-mode on files with extensions like .h, .c, .cpp, .hpp
+(add-to-list 'auto-mode-alist '("\\.[hc]\\(pp\\)?\\'" . simpc-mode))
+```
+
+## Indentation
+
+Right now the mode supports only very simple indentations based on the
+analysing the previous non-empty line and its surrounding curly
+braces. Anything more complicated is outside of the scope of the
+project.
+
+It is recommended to use an external formatter such as
+[indent](https://www.gnu.org/software/indent/),
+[astyle](http://astyle.sourceforge.net/),
+[clang-format](https://clang.llvm.org/docs/ClangFormat.html), etc.
+
+Here is how I use [astyle](http://astyle.sourceforge.net/):
+
+```emacs-lisp
+(defun astyle-buffer ()
+ (interactive)
+ (let ((saved-line-number (line-number-at-pos)))
+ (shell-command-on-region
+ (point-min)
+ (point-max)
+ "astyle --style=kr"
+ nil
+ t)
+ (goto-line saved-line-number)))
+```
+
+Then I bind `astyle-buffer` to some key combination and invoke it
+periodically to reformat the whole current buffer.
diff --git a/.emacs.local/simpc-mode/simpc-mode.el b/.emacs.local/simpc-mode/simpc-mode.el
new file mode 100644
index 0000000..928bb93
--- /dev/null
+++ b/.emacs.local/simpc-mode/simpc-mode.el
@@ -0,0 +1,127 @@
+(require 'subr-x)
+
+(defvar simpc-mode-syntax-table
+ (let ((table (make-syntax-table)))
+ ;; C/C++ style comments
+ (modify-syntax-entry ?/ ". 124b" table)
+ (modify-syntax-entry ?* ". 23" table)
+ (modify-syntax-entry ?\n "> b" table)
+ ;; Preprocessor stuff?
+ (modify-syntax-entry ?# "." table)
+ ;; Chars are the same as strings
+ (modify-syntax-entry ?' "\"" table)
+ ;; Treat <> as punctuation (needed to highlight C++ keywords
+ ;; properly in template syntax)
+ (modify-syntax-entry ?< "." table)
+ (modify-syntax-entry ?> "." table)
+
+ (modify-syntax-entry ?& "." table)
+ (modify-syntax-entry ?% "." table)
+ table))
+
+(defun simpc-types ()
+ '("char" "int" "long" "short" "void" "bool" "float" "double" "signed" "unsigned"
+ "char16_t" "char32_t" "char8_t"
+ "int8_t" "uint8_t" "int16_t" "uint16_t" "int32_t" "uint32_t" "int64_t" "uint64_t"
+ "uintptr_t"
+ "size_t" "ptrdiff_t"
+ "va_list"))
+
+(defun simpc-keywords ()
+ '("auto" "break" "case" "const" "continue" "default" "do"
+ "else" "enum" "extern" "for" "goto" "if" "register"
+ "return" "sizeof" "static" "struct" "switch" "typedef"
+ "union" "volatile" "while" "alignas" "alignof" "and"
+ "and_eq" "asm" "atomic_cancel" "atomic_commit" "atomic_noexcept" "bitand"
+ "bitor" "catch" "class" "co_await"
+ "co_return" "co_yield" "compl" "concept" "const_cast" "consteval" "constexpr"
+ "constinit" "decltype" "delete" "dynamic_cast" "explicit" "export" "false"
+ "friend" "inline" "mutable" "namespace" "new" "noexcept" "not" "not_eq"
+ "nullptr" "operator" "or" "or_eq" "private" "protected" "public" "reflexpr"
+ "reinterpret_cast" "requires" "static_assert" "static_cast" "synchronized"
+ "template" "this" "thread_local" "throw" "true" "try" "typeid" "typename"
+ "using" "virtual" "wchar_t" "xor" "xor_eq"))
+
+(defun simpc-font-lock-keywords ()
+ (list
+ `("# *\\(warn\\|error\\)" . font-lock-warning-face)
+ `("# *[#a-zA-Z0-9_]+" . font-lock-preprocessor-face)
+ `("# *include\\(?:_next\\)?\\s-+\\(\\(<\\|\"\\).*\\(>\\|\"\\)\\)" . (1 font-lock-string-face))
+ `("\\(?:enum\\|struct\\)\\s-+\\([a-zA-Z0-9_]+\\)" . (1 font-lock-type-face))
+ `(,(regexp-opt (simpc-keywords) 'symbols) . font-lock-keyword-face)
+ `(,(regexp-opt (simpc-types) 'symbols) . font-lock-type-face)))
+
+(defun simpc--previous-non-empty-line ()
+ "Returns either NIL when there is no such line or a pair (line . indentation)"
+ (save-excursion
+ ;; If you are on the first line, but not at the beginning of buffer (BOB) the `(bobp)`
+ ;; function does not return `t`. So we have to move to the beginning of the line first.
+ ;; TODO: feel free to suggest a better approach for checking BOB here.
+ (move-beginning-of-line nil)
+ (if (bobp)
+ ;; If you are standing at the BOB, you by definition don't have a previous non-empty line.
+ nil
+ ;; Moving one line backwards because the current line is by definition is not
+ ;; the previous non-empty line.
+ (forward-line -1)
+ ;; Keep moving backwards until we hit BOB or a non-empty line.
+ (while (and (not (bobp))
+ (string-empty-p
+ (string-trim-right
+ (thing-at-point 'line t))))
+ (forward-line -1))
+
+ (if (string-empty-p
+ (string-trim-right
+ (thing-at-point 'line t)))
+ ;; If after moving backwards for this long we still look at an empty
+ ;; line we by definition didn't find the previous non-empty line.
+ nil
+ ;; We found the previous non-empty line!
+ (cons (thing-at-point 'line t)
+ (current-indentation))))))
+
+(defun simpc--desired-indentation ()
+ (let ((prev (simpc--previous-non-empty-line)))
+ (if (not prev)
+ (current-indentation)
+ (let ((indent-len 4)
+ (cur-line (string-trim-right (thing-at-point 'line t)))
+ (prev-line (string-trim-right (car prev)))
+ (prev-indent (cdr prev)))
+ (cond
+ ((string-match-p "^\\s-*switch\\s-*(.+)" prev-line)
+ prev-indent)
+ ((and (string-suffix-p "{" prev-line)
+ (string-prefix-p "}" (string-trim-left cur-line)))
+ prev-indent)
+ ((string-suffix-p "{" prev-line)
+ (+ prev-indent indent-len))
+ ((string-prefix-p "}" (string-trim-left cur-line))
+ (max (- prev-indent indent-len) 0))
+ ((string-suffix-p ":" prev-line)
+ (if (string-suffix-p ":" cur-line)
+ prev-indent
+ (+ prev-indent indent-len)))
+ ((string-suffix-p ":" cur-line)
+ (max (- prev-indent indent-len) 0))
+ (t prev-indent))))))
+
+;;; TODO: customizable indentation (amount of spaces, tabs, etc)
+(defun simpc-indent-line ()
+ (interactive)
+ (when (not (bobp))
+ (let* ((desired-indentation
+ (simpc--desired-indentation))
+ (n (max (- (current-column) (current-indentation)) 0)))
+ (indent-line-to desired-indentation)
+ (forward-char n))))
+
+(define-derived-mode simpc-mode prog-mode "Simple C"
+ "Simple major mode for editing C files."
+ :syntax-table simpc-mode-syntax-table
+ (setq-local font-lock-defaults '(simpc-font-lock-keywords))
+ (setq-local indent-line-function 'simpc-indent-line)
+ (setq-local comment-start "// "))
+
+(provide 'simpc-mode)