@@ -707,6 +707,51 @@ function OrgMappings:_insert_heading_from_plain_line(suffix)
707707 end
708708end
709709
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+
710755function OrgMappings :move_subtree_up ()
711756 local item = Files .get_closest_headline ()
712757 local prev_headline = item :get_prev_headline_same_level ()
@@ -751,7 +796,7 @@ function OrgMappings:open_at_point()
751796 return
752797 end
753798
754- local parts = vim .split (link , ' ][' , true )
799+ local parts = vim .split (link . content , ' ][' , true )
755800 local url = parts [1 ]
756801 local link_ctx = { base = url , skip_add_prefix = true }
757802 if url :find (' ^file:' ) then
@@ -991,6 +1036,7 @@ function OrgMappings:_get_date_under_cursor(col_offset)
9911036 return nil
9921037 end
9931038
1039+ -- TODO: this will result in a bug, when more than one date is in the line
9941040 return dates [1 ]
9951041end
9961042
@@ -1025,7 +1071,7 @@ function OrgMappings:_adjust_date(amount, span, fallback)
10251071 return vim .api .nvim_feedkeys (utils .esc (fallback ), ' n' , true )
10261072end
10271073
1028- --- @return string | nil
1074+ --- @return table | nil
10291075function OrgMappings :_get_link_under_cursor ()
10301076 local found_link = nil
10311077 local links = {}
@@ -1035,10 +1081,10 @@ function OrgMappings:_get_link_under_cursor()
10351081 local start_from = # links > 0 and links [# links ].to or nil
10361082 local from , to = line :find (' %[%[(.-)%]%]' , start_from )
10371083 if col >= from and col <= to then
1038- found_link = link
1084+ found_link = { content = link , from = from , to = to }
10391085 break
10401086 end
1041- table.insert (links , { link = link , from = from , to = to })
1087+ table.insert (links , { content = link , from = from , to = to })
10421088 end
10431089 return found_link
10441090end
0 commit comments