From 3141955f9c9bf35498e8c69c7907e0b01f903a37 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 24 Jul 2024 10:26:33 -0400 Subject: [PATCH] Add language detection to YAML scalar editing Integrated language-detection to automatically detect and set major modes based on buffer/string content. Enhanced YAML editing with language detection for buffer initialization. --- emacs/.emacs.d/config/init-editing.el | 78 +++++++++++++++++++++++++++ emacs/.emacs.d/config/init-yaml.el | 11 ++++ 2 files changed, 89 insertions(+) diff --git a/emacs/.emacs.d/config/init-editing.el b/emacs/.emacs.d/config/init-editing.el index 86cb8b9..9a9b885 100644 --- a/emacs/.emacs.d/config/init-editing.el +++ b/emacs/.emacs.d/config/init-editing.el @@ -174,4 +174,82 @@ (evil-local-set-key 'normal (kbd "zM") #'origami-toggle-all-nodes-same-level)) (add-hook 'origami-mode-hook #'origami-setup)) +;; Language detection of arbitrary strings/buffers +(use-package language-detection + :commands (language-detection-buffer + language-detection-string) + :init + (defun language-detection-detect-mode (string) + (let* ((map '((ada ada-mode) + (awk awk-mode) + (c c-mode) + (cpp c++-mode) + (clojure clojure-mode lisp-mode) + (csharp csharp-mode java-mode) + (css css-mode) + (dart dart-mode) + (delphi delphi-mode) + (emacslisp emacs-lisp-mode) + (erlang erlang-mode) + (fortran fortran-mode) + (fsharp fsharp-mode) + (go go-mode) + (groovy groovy-mode) + (haskell haskell-mode) + (html html-mode) + (java java-mode) + (javascript javascript-mode) + (json json-mode javascript-mode) + (latex latex-mode) + (lisp lisp-mode) + (lua lua-mode) + (matlab matlab-mode octave-mode) + (objc objc-mode c-mode) + (perl perl-mode) + (php php-mode) + (prolog prolog-mode) + (python python-mode) + (r r-mode) + (ruby ruby-mode) + (rust rust-mode) + (scala scala-mode) + (shell shell-script-mode) + (smalltalk smalltalk-mode) + (sql sql-mode) + (swift swift-mode) + (visualbasic visual-basic-mode) + (xml sgml-mode))) + (language (language-detection-string string)) + (modes (cdr (assoc language map))) + (mode (cl-loop for mode in modes + when (fboundp mode) + return mode))) + (when (fboundp mode) + mode))) + (defun fontify-with-mode (mode text) + (with-temp-buffer + (insert text) + (delay-mode-hooks (funcall mode)) + (font-lock-default-function mode) + (font-lock-default-fontify-region (point-min) (point-max) nil) + (buffer-string))) + (defun fontify-using-faces (text) + (let ((pos 0)) + (while (setq next (next-single-property-change pos 'face text)) + (put-text-property pos next 'font-lock-face (get-text-property pos 'face text) text) + (setq pos next)) + (add-text-properties 0 (length text) '(fontified t) text) + text)) + (defun language-detection-fontify-region (start end) + (interactive "r") + (let* ((text (buffer-substring-no-properties start end)) + (mode (language-detection-detect-mode text)) + (fontified (fontify-using-faces (fontify-with-mode mode text)))) + (delete-region start end) + (insert fontified))) + (defun language-detection-fontify-buffer () + (interactive) + (language-detection-fontify-region (point-min) (point-max)))) + + (provide 'init-editing) diff --git a/emacs/.emacs.d/config/init-yaml.el b/emacs/.emacs.d/config/init-yaml.el index 981f459..3c5b667 100644 --- a/emacs/.emacs.d/config/init-yaml.el +++ b/emacs/.emacs.d/config/init-yaml.el @@ -9,6 +9,17 @@ (use-package yaml-pro :when (treesit-ready-p 'yaml) :hook (yaml-ts-mode . yaml-pro-ts-mode) + :config + (defun yaml-pro-edit-initialize-buffer-filter-args-advice (args) + (if-let ((mode (language-detection-detect-mode (buffer-string)))) + (cl-destructuring-bind (parent-buffer buffer initial-text type initialize path) args + (let ((init-func (lambda () + (funcall mode) + (when initialize + (call-interactively initialize))))) + (list parent-buffer buffer initial-text type init-func path))) + args)) + (advice-add 'yaml-pro-edit-initialize-buffer :filter-args #'yaml-pro-edit-initialize-buffer-filter-args-advice) :general (yaml-pro-ts-mode-map [remap evil-backward-section-begin] #'yaml-pro-ts-prev-subtree [remap evil-forward-section-begin] #'yaml-pro-ts-next-subtree