@@ -707,6 +707,51 @@ function OrgMappings:_insert_heading_from_plain_line(suffix)
707
707
end
708
708
end
709
709
710
+ -- Inserts a new link at the cursor position or modifies the link the cursor is
711
+ -- currently on
712
+ function OrgMappings :insert_link ()
713
+ local link_location = vim .fn .OrgmodeInput (' Links: ' , ' ' )
714
+ if vim .trim (link_location ) ~= ' ' then
715
+ link_location = ' [' .. link_location .. ' ]'
716
+ else
717
+ utils .echo_warning (' No Link selected' )
718
+ return
719
+ end
720
+ local link_description = vim .trim (vim .fn .OrgmodeInput (' Description: ' , ' ' ))
721
+ if link_description ~= ' ' then
722
+ link_description = ' [' .. link_description .. ' ]'
723
+ end
724
+
725
+ local insert_from
726
+ local insert_to
727
+ local target_col = # link_location + # link_description + 2
728
+
729
+ -- check if currently on link
730
+ local link = self :_get_link_under_cursor ()
731
+ if link then
732
+ insert_from = link .from - 1
733
+ insert_to = link .to + 1
734
+ target_col = target_col + link .from
735
+ else
736
+ local colnr = vim .fn .col (' .' )
737
+ insert_from = colnr - 1
738
+ insert_to = colnr
739
+ target_col = target_col + colnr
740
+ end
741
+
742
+ local linenr = vim .fn .line (' .' )
743
+ local curr_line = vim .fn .getline (linenr )
744
+ local new_line = string.sub (curr_line , 0 , insert_from )
745
+ .. ' ['
746
+ .. link_location
747
+ .. link_description
748
+ .. ' ]'
749
+ .. string.sub (curr_line , insert_to , # curr_line )
750
+
751
+ vim .fn .setline (linenr , new_line )
752
+ vim .fn .cursor (linenr , target_col )
753
+ end
754
+
710
755
function OrgMappings :move_subtree_up ()
711
756
local item = Files .get_closest_headline ()
712
757
local prev_headline = item :get_prev_headline_same_level ()
@@ -751,7 +796,7 @@ function OrgMappings:open_at_point()
751
796
return
752
797
end
753
798
754
- local parts = vim .split (link , ' ][' , true )
799
+ local parts = vim .split (link . content , ' ][' , true )
755
800
local url = parts [1 ]
756
801
local link_ctx = { base = url , skip_add_prefix = true }
757
802
if url :find (' ^file:' ) then
@@ -991,6 +1036,7 @@ function OrgMappings:_get_date_under_cursor(col_offset)
991
1036
return nil
992
1037
end
993
1038
1039
+ -- TODO: this will result in a bug, when more than one date is in the line
994
1040
return dates [1 ]
995
1041
end
996
1042
@@ -1025,7 +1071,7 @@ function OrgMappings:_adjust_date(amount, span, fallback)
1025
1071
return vim .api .nvim_feedkeys (utils .esc (fallback ), ' n' , true )
1026
1072
end
1027
1073
1028
- --- @return string | nil
1074
+ --- @return table | nil
1029
1075
function OrgMappings :_get_link_under_cursor ()
1030
1076
local found_link = nil
1031
1077
local links = {}
@@ -1035,10 +1081,10 @@ function OrgMappings:_get_link_under_cursor()
1035
1081
local start_from = # links > 0 and links [# links ].to or nil
1036
1082
local from , to = line :find (' %[%[(.-)%]%]' , start_from )
1037
1083
if col >= from and col <= to then
1038
- found_link = link
1084
+ found_link = { content = link , from = from , to = to }
1039
1085
break
1040
1086
end
1041
- table.insert (links , { link = link , from = from , to = to })
1087
+ table.insert (links , { content = link , from = from , to = to })
1042
1088
end
1043
1089
return found_link
1044
1090
end
0 commit comments