diff --git a/core/events.lua b/core/events.lua
index b2f1dd1d..149c62e1 100644
--- a/core/events.lua
+++ b/core/events.lua
@@ -170,6 +170,12 @@ local M = {}
-- This event is never emitted when Textadept is running in the terminal.
-- @field FOCUS
+--- Emitted after code folding is done by the lexer.
+-- This is useful for performing manual folding and manipulation of the folding table
+-- `buffer.fold_level` after lexer is done folding the code, where the built-in facilities for
+-- code folding doesn't suffice.
+-- @field FOLDED
+
--- Emitted when clicking the mouse on text that has an indicator present.
-- Arguments:
--
diff --git a/core/lexer.lua b/core/lexer.lua
index 41d30f39..7d87bf0a 100644
--- a/core/lexer.lua
+++ b/core/lexer.lua
@@ -126,6 +126,7 @@ local function highlight(buffer, start_pos, end_pos)
local level = buffer.fold_level[line] & buffer.FOLDLEVELNUMBERMASK
local folds = buffer.lexer:fold(buffer:text_range(start_pos, end_pos), line, level)
for line, level in pairs(folds) do buffer.fold_level[line] = level end
+ events.emit(events.FOLDED)
end
local mutex
diff --git a/docs/api.md b/docs/api.md
index 71c7807e..eb47aca0 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -4871,7 +4871,7 @@ shortly after this event.
The buffer **must not** be modified during this event.
-#### `events.BUFFER_BEFORE_SWITCH`
+#### `events.BUFFER_BEFORE_SWITCH`
Emitted right before switching to another buffer.
The buffer being switched from is [`buffer`](#buffer).
@@ -5119,6 +5119,14 @@ Arguments:
- *name*: The language lexer's name.
+
+#### `events.FOLDED`
+
+Emitted after code folding is done by the lexer.
+This is useful for performing manual folding and manipulation of the folding table
+`buffer.fold_level` after lexer is done folding the code, where the built-in facilities for
+[code folding](#code-folding) doesn't suffice.
+
#### `events.MARGIN_CLICK`