From 54618ec39a38ee95678d9f481240292dd4c9229b Mon Sep 17 00:00:00 2001 From: Andreas Gerler Date: Fri, 21 Nov 2025 11:13:30 +0100 Subject: [PATCH] add outline-yaml * lisp/outline-yaml.el: upstream has no melpa. * bba-programming.org: enable outline-yaml for yaml and yaml-ts modes. --- bba-programming.org | 4 +- lisp/outline-yaml.el | 120 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 lisp/outline-yaml.el diff --git a/bba-programming.org b/bba-programming.org index 608c55d1..cd056ae2 100644 --- a/bba-programming.org +++ b/bba-programming.org @@ -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 index 00000000..fad58fef --- /dev/null +++ b/lisp/outline-yaml.el @@ -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 . + +;;; 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 -- 2.47.3