Skip to content

The org_global_cycle keybinding affects non-org buffers #863

Open
@ficcdaf

Description

@ficcdaf

Describe the bug

Kindly note that I have org_cycle bound to Tab and org_global_cycle bound to S-Tab and every orgmode setting is the defaults.

When opening an org file, headings are folded (as per my org_startup_folded default setting). When I press S-Tab to cycle the folds on all the headlines, it seems to modify Neovim's foldlevel. Then, when I open a new buffer to a non-org file, that foldlevel persists, and the contents of that file are folded. This behavior persists after all org buffers are closed. The behavior also persists when opening the new buffers in split windows.

I observed this only happens with the global cycle command. org_cycle does not cause this behavior.

Steps to reproduce

Note that my foldlevel is set to 99 in my Neovim config.

  1. Open an org file in a buffer.
  2. Press org_global_cycle (S-tab) an arbitrary number of times.
  3. Open a non-org file (such as a lua file) in another buffer.
  4. Observe that the file is displayed with folding.
  5. Close the org buffer.
  6. Open another non-org file.
  7. Observe that the file is still displayed with folding.

Screenshots:

Image

Observation at step 4.

Expected behavior

The org global cycle keybind should only affect org buffers.

Emacs functionality

No response

Minimal init.lua

With the minimal init, you can do :e /tmp/test.org and then :e /tmp/test.lua to reproduce the issue. In the minimal init, the Lua file doesn't fold automatically, but you can observe that set foldlevel does not return 99 after opening test.lua, and it is the foldlevel which is the problem here.

local org = io.open("/tmp/test.org", "w")
if not org then
	error("failed open org")
end
local orgc = [[
* A headline is here.
** TODO This is a task.
]]
local lua = io.open("/tmp/test.lua", "w")
local luac = [[
-- a file from my config that has a lot of folds in it
-- this is the file on which I noticed the bug
return {
  "folke/snacks.nvim",
  opts = {
    toggle = {
      notify = false,
    },
    bigfile = {
      notify = true, -- show notification when big file detected
      size = 1.5 * 1024 * 1024, -- 1.5MB
      -- Enable or disable features when big file detected
      ---@param ctx {buf: number, ft:string}
      setup = function(ctx)
        -- Snacks.util.wo(0, { foldmethod = "manual", statuscolumn = "", conceallevel = 0 })
        -- vim.b.minianimate_disable = true
        vim.schedule(function()
          vim.bo[ctx.buf].syntax = ctx.ft
        end)
      end,
    },
    zen = {
      notify = false,
      toggles = {
        dim = false,
        git_signs = false,
        mini_diff_signs = false,
        diagnostics = true,
        inlay_hints = true,
      },
      win = {
        backdrop = {
          transparent = false,
          blend = 20,
        },
      },
    },
  },
  keys = {
    {
      "<leader>z",
      function()
        Snacks.zen({
          win = {
            backdrop = {
              transparent = false,
              blend = 10,
            },
          },
        })
      end,
      desc = "Toggle Zen Mode",
    },
    {
      "<leader>Z",
      function()
        Snacks.zen({
          win = {
            backdrop = {
              transparent = false,
            },
          },
        })
      end,
      desc = "Toggle Zen Mode (no backdrop)",
    },
  },
}
]]
org:write(orgc)
lua:write(luac)

local tmp_dir = vim.env.TMPDIR or vim.env.TMP or vim.env.TEMP or "/tmp"
local nvim_root = tmp_dir .. "/nvim_orgmode"
local lazy_root = nvim_root .. "/lazy"
local lazypath = lazy_root .. "/lazy.nvim"

for _, name in ipairs({ "config", "data", "state", "cache" }) do
	vim.env[("XDG_%s_HOME"):format(name:upper())] = nvim_root .. "/" .. name
end

-- Install lazy.nvim if not already installed
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({
		"git",
		"clone",
		"--filter=blob:none",
		"https://github.com/folke/lazy.nvim.git",
		"--branch=stable", -- latest stable release
		lazypath,
	})
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
	{
		"nvim-orgmode/orgmode",
		event = "VeryLazy",
		ft = { "org" },
		config = function()
			require("orgmode").setup()
		end,
	},
}, {
	root = lazy_root,
	lockfile = nvim_root .. "/lazy.json",
	install = {
		missing = false,
	},
})

require("lazy").sync({
	wait = true,
	show = false,
})

vim.o.foldlevel = 99

Screenshots and recordings

No response

OS / Distro

Arch Linux

Neovim version/commit

NVIM v0.11.0-dev-1589+g71507281fb Build type: RelWithDebInfo LuaJIT 2.1.1736781742

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions