]> git.bundesbrandschatzamt.de Git - emacsconfig.git/commitdiff
add outline-yaml
authorAndreas Gerler <baronNOSPAMbundesbrandschatzamt.de>
Fri, 21 Nov 2025 10:13:30 +0000 (11:13 +0100)
committerAndreas Gerler <baronNOSPAMbundesbrandschatzamt.de>
Fri, 21 Nov 2025 10:13:30 +0000 (11:13 +0100)
* lisp/outline-yaml.el: upstream has no melpa.
* bba-programming.org: enable outline-yaml for yaml and yaml-ts modes.

bba-programming.org
lisp/outline-yaml.el [new file with mode: 0644]

index 608c55d1ce74452bf85c0dcd2aafadc849a770c4..cd056ae2a70765f5f53dd492612a8c904f6cd48f 100644 (file)
@@ -314,5 +314,7 @@ Adjust indentation at work.
               (auto-fill-mode -1)
               ;; (stripes-mode 1)
             (setq bba-keep-whitespaces 1)))
-
+  (load-file (expand-file-name "~/.emacs.d/lisp/outline-yaml.el"))
+  (add-hook 'yaml-mode-hook 'outline-yaml-minor-mode)
+  (add-hook 'yaml-ts-mode-hook 'outline-yaml-minor-mode)
 #+END_SRC
diff --git a/lisp/outline-yaml.el b/lisp/outline-yaml.el
new file mode 100644 (file)
index 0000000..fad58fe
--- /dev/null
@@ -0,0 +1,120 @@
+;;; outline-yaml.el --- Code folding and Outline for Yaml files  -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2024-2025  James Cherti | https://www.jamescherti.com/contact/
+
+;; Author: James Cherti
+;; Version: 1.0.2
+;; URL: https://github.com/jamescherti/outline-yaml.el
+;; Keywords: outlines
+;; Package-Requires: ((emacs "24.3"))
+;; SPDX-License-Identifier: GPL-3.0-or-later
+
+;; This file is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+
+;; This file is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This package provides a minor mode for code folding and outlining YAML files
+;; in Emacs. It leverages the built-in `outline` package to create a structured
+;; view of YAML files, allowing users to collapse and expand sections based on
+;; indentation levels.
+
+;; Outline Yaml files.
+
+;;; Code:
+
+(defgroup outline-yaml nil
+  "Non-nil if outline-yaml mode mode is enabled."
+  :group 'outline-yaml
+  :prefix "outline-yaml-"
+  :link '(url-link
+          :tag "Github"
+          "https://github.com/jamescherti/outline-yaml.el"))
+
+(defvar outline-yaml-minor-mode-map
+  (let ((map (make-sparse-keymap)))
+    map)
+  "Keymap for `outline-yaml-minor-mode'.")
+
+(defun outline-yaml--level ()
+  "Determine the outline level for YAML mode based on indentation."
+  (let* ((space-count (current-indentation))
+         (spaces-per-level (cond ((boundp 'yaml-indent-offset)
+                                  yaml-indent-offset)
+                                 (t 1))))
+    (+ 1 (/ space-count spaces-per-level))))
+
+;;;###autoload
+(define-minor-mode outline-yaml-minor-mode
+  "Toggle `outline-yaml-minor-mode'."
+  :lighter " OutlYaml"
+  :keymap outline-yaml-minor-mode-map
+  :group 'outline-yaml
+  (if outline-yaml-minor-mode
+      (progn
+        (setq-local outline-level 'outline-yaml--level)
+        (setq-local outline-heading-end-regexp "\n")
+        (setq-local outline-regexp
+                    (rx
+                     bol
+                     (or
+                      ;; 1. Document separator "---"
+                      (seq "---"
+                           (seq (zero-or-more " \t")
+                                (or (optional "#")
+                                    eol)))
+
+                      (seq
+                       (zero-or-more (any " \t"))  ; Indentation
+                       (or
+                        ;; 2. List
+                        (seq "-" (any " \t"))
+
+                        ;; 3. Handling list items or keys in mappings
+                        (seq (or
+                              ;; Double quoted strings
+                              (seq "\""
+                                   ;; Matches an escaped quote or any
+                                   ;; other character except a quote
+                                   ;; or newline
+                                   (zero-or-more
+                                    (or (seq "\\" "\"")
+                                        (not (in "\""
+                                                 "\n"))))
+                                   "\"")
+
+                              ;; Single quoted strings
+                              (seq "'"
+                                   ;; Matches an escaped single quote
+                                   ;; Matches any other character
+                                   ;; except a single quote or newline
+                                   (zero-or-more
+                                    (or "''"
+                                        (not (in "'"
+                                                 "\n"))))
+                                   "'")
+
+                              ;; Unquoted keys
+                              (one-or-more (not (in "#"
+                                                    ":"
+                                                    "\n"))))
+
+                             ;; Colon indicating a key in YAML
+                             ":"))))
+                     (zero-or-more nonl)
+                     eol))
+        (outline-minor-mode 1))
+    (outline-minor-mode -1)))
+
+(provide 'outline-yaml)
+;;; outline-yaml.el ends here