Skip to content

Commit a09ae41

Browse files
author
Sebastian Flügge
committed
feat: toggle to show headlines of current file only
1 parent 95ade9b commit a09ae41

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

README.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ require('telescope').extensions.orgmode.insert_link
7777
By pressing `<C-Space>` the picker state can be toggled between two modes.
7878
Every mode is available in every function.
7979

80+
### Current file only mode
81+
82+
In headline mode, you can press `<C-f>` to toggle between showing all headlines
83+
vs only headlines from the current file. This is useful when you want to focus
84+
on the current file's structure.
85+
8086
### Search headlines
8187

8288
This is the first and default mode. It shows all the headlines, initially
@@ -116,15 +122,45 @@ For a particular command you can pass it directly in your key mapping to the fun
116122
require('telescope').extension.orgmode.search_headings({ max_depth = 3 })
117123
```
118124

119-
You can also create a key mapping, that allows you to search directly for org files:
125+
### Custom keymaps
126+
127+
You can customize the telescope picker keymaps by passing a `mappings` table:
128+
129+
```lua
130+
require('telescope').extensions.orgmode.search_headings({
131+
mappings = {
132+
i = {
133+
['<C-l>'] = require('telescope-orgmode.actions').toggle_current_file_only,
134+
['<C-s>'] = require('telescope-orgmode.actions').toggle_headlines_orgfiles,
135+
},
136+
n = {
137+
['<C-l>'] = require('telescope-orgmode.actions').toggle_current_file_only,
138+
['<C-s>'] = require('telescope-orgmode.actions').toggle_headlines_orgfiles,
139+
}
140+
}
141+
})
142+
```
143+
144+
You can also create key mappings for specific modes:
120145

121146
```lua
122-
vim.set.keymap(
147+
-- Search only org files
148+
vim.keymap.set(
123149
"n",
124-
"<Leader>off",
150+
"<Leader>off",
125151
function()
126-
require('telescope').extension.orgmode.search_headings({ mode = "orgfiles" })
152+
require('telescope').extensions.orgmode.search_headings({ mode = "orgfiles" })
127153
end,
128154
{ desc = "Find org files"}
129155
)
156+
157+
-- Search headlines in current file only
158+
vim.keymap.set(
159+
"n",
160+
"<Leader>ofc",
161+
function()
162+
require('telescope').extensions.orgmode.search_headings({ only_current_file = true })
163+
end,
164+
{ desc = "Find headlines in current file"}
165+
)
130166
```

lua/telescope-orgmode/actions.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ function M.search_orgfiles(opts)
3333
end
3434
end
3535

36+
function M.toggle_current_file_only(opts)
37+
return function(prompt_bufnr)
38+
-- Only toggle if we're in headlines mode
39+
if opts.state.current ~= 'headlines' then
40+
return
41+
end
42+
43+
opts.only_current_file = not opts.only_current_file
44+
M._find_headlines(opts, prompt_bufnr)
45+
end
46+
end
47+
3648
function M.refile(closest_headline)
3749
return function(prompt_bufnr)
3850
local entry = action_state.get_selected_entry()

lua/telescope-orgmode/mappings.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ local M = {}
55
function M.attach_mappings(map, opts)
66
map('i', '<c-space>', to_actions.toggle_headlines_orgfiles(opts), { desc = 'Toggle headline/orgfile' })
77
map('n', '<c-space>', to_actions.toggle_headlines_orgfiles(opts), { desc = 'Toggle headline/orgfile' })
8+
map('i', '<c-f>', to_actions.toggle_current_file_only(opts), { desc = 'Toggle current file only' })
9+
map('n', '<c-f>', to_actions.toggle_current_file_only(opts), { desc = 'Toggle current file only' })
810
M.attach_custom(map, opts)
911
end
1012

lua/telescope-orgmode/org.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ end
2828
function M.load_headlines(opts)
2929
-- Get files sorted by modification time (most recent first)
3030
local files = require('orgmode').files:all()
31+
32+
if opts.only_current_file then
33+
local current_file = opts.original_file or vim.api.nvim_buf_get_name(0)
34+
if current_file == '' then
35+
current_file = vim.fn.expand('%:p')
36+
end
37+
files = vim.tbl_filter(function(file)
38+
return file.filename == current_file
39+
end, files)
40+
end
41+
3142
if not opts.archived then
3243
files = vim.tbl_filter(function(file)
3344
return not (vim.fn.fnamemodify(file.filename, ':e') == 'org_archive')

lua/telescope-orgmode/picker/search_headings.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ return function(opts)
1111
orgfiles = 'Search org files',
1212
}, "headlines")
1313

14+
-- Capture the current buffer before opening telescope
15+
opts.original_buffer = vim.api.nvim_get_current_buf()
16+
opts.original_file = vim.api.nvim_buf_get_name(opts.original_buffer)
17+
1418
pickers
1519
.new(opts, {
1620
prompt_title = opts.prompt_titles[opts.state.current],

0 commit comments

Comments
 (0)