@@ -843,11 +843,9 @@ contains |
| 843 | 843 | else |
| 844 | 844 | ! Complete files and directories |
| 845 | 845 | call complete_files_enhanced(last_word, completions, num_completions) |
| 846 | | - |
| 847 | | - ! Add prefix back to completions |
| 848 | | - do i = 1, num_completions |
| 849 | | - completions(i) = trim(prefix_part) // trim(completions(i)) |
| 850 | | - end do |
| 846 | + |
| 847 | + ! Don't add prefix to completions - they are for display only |
| 848 | + ! The prefix will be added when constructing the completed line |
| 851 | 849 | end if |
| 852 | 850 | end subroutine |
| 853 | 851 | |
@@ -1206,27 +1204,51 @@ contains |
| 1206 | 1204 | integer, intent(out) :: num_completions |
| 1207 | 1205 | character(len=*), intent(out) :: completed_line |
| 1208 | 1206 | logical, intent(out) :: completed |
| 1209 | | - |
| 1210 | | - character(len=MAX_LINE_LEN) :: common_prefix |
| 1211 | | - |
| 1207 | + |
| 1208 | + character(len=MAX_LINE_LEN) :: common_prefix, prefix_part |
| 1209 | + integer :: last_space_pos, i |
| 1210 | + |
| 1212 | 1211 | completed = .false. |
| 1213 | 1212 | completed_line = partial_input |
| 1214 | | - |
| 1213 | + |
| 1214 | + ! Find the prefix (command and any earlier arguments) |
| 1215 | + last_space_pos = 0 |
| 1216 | + do i = len_trim(partial_input), 1, -1 |
| 1217 | + if (partial_input(i:i) == ' ') then |
| 1218 | + last_space_pos = i |
| 1219 | + exit |
| 1220 | + end if |
| 1221 | + end do |
| 1222 | + |
| 1223 | + if (last_space_pos > 0) then |
| 1224 | + prefix_part = partial_input(:last_space_pos) |
| 1225 | + else |
| 1226 | + prefix_part = '' |
| 1227 | + end if |
| 1228 | + |
| 1215 | 1229 | call enhanced_tab_complete(partial_input, completions, num_completions) |
| 1216 | | - |
| 1230 | + |
| 1217 | 1231 | if (num_completions == 0) then |
| 1218 | 1232 | ! No completions found |
| 1219 | 1233 | return |
| 1220 | 1234 | else if (num_completions == 1) then |
| 1221 | | - ! Single completion - use it |
| 1222 | | - completed_line = trim(completions(1)) |
| 1235 | + ! Single completion - add prefix back (preserve spacing) |
| 1236 | + if (last_space_pos > 0) then |
| 1237 | + completed_line = prefix_part(:last_space_pos) // trim(completions(1)) |
| 1238 | + else |
| 1239 | + completed_line = trim(completions(1)) |
| 1240 | + end if |
| 1223 | 1241 | completed = .true. |
| 1224 | 1242 | else |
| 1225 | 1243 | ! Multiple completions - try common prefix |
| 1226 | 1244 | common_prefix = get_common_prefix(completions, num_completions) |
| 1227 | | - |
| 1245 | + |
| 1228 | 1246 | if (len_trim(common_prefix) > 0) then |
| 1229 | | - completed_line = trim(common_prefix) |
| 1247 | + if (last_space_pos > 0) then |
| 1248 | + completed_line = prefix_part(:last_space_pos) // trim(common_prefix) |
| 1249 | + else |
| 1250 | + completed_line = trim(common_prefix) |
| 1251 | + end if |
| 1230 | 1252 | completed = .true. |
| 1231 | 1253 | end if |
| 1232 | 1254 | end if |