1

I am porting autotools project to CMake. So, every directory of the project has CMakeLists.txt and Makefile.am.

I want to make a split: CMakeLists.txt on one side and the corresponding Makefile.am on the other.

I want to automatically arrange it so that When on the one side file is opened, it has to open corresponding file on the other side.

Bonuses:

  • Add a third split: tree view of the filesystem, filtered to show only CMakeLists.txt, Makefile.am and configure.ac.

  • Sometimes there are all three files in the directory; so show both Makefile.am and configure.ac that correspond to the CMakeLists.txt in that dir.

update

Filters for the tree

let NERDTreeIgnore = ['\(CMakeLists\.txt\|Makefile\.am\|configure\.ac\)\@<!$[[file]]']
4

As an alternative (pun!) maybe look into file switcher like a.vim, fswitch, or altr. These let you switch between related files. It is often used to switch between .cpp and .h files, but they can be customized to switch between other types of files.

If you want a quick n' easy switcher than you can use the following:

function! Alternative(cmd, ...) abort
  let f = expand("%:p:gs?\\?/?")
  let files = map(copy(get(g:, 'related_files', [])), 'fnamemodify(expand(v:val), ":gs?\\?/?")')
  let files = a:0 && a:1 ? reverse(files) : files
  let idx = index(map(copy(files), 'f[len(f)-len(v:val)-1:] ==# "/" . v:val'), 1)
  if idx == -1
    return 'echoerr "No alternative file"'
  endif
  let path = fnamemodify(expand('%:p:h:gs?\\?/?') . repeat("/..", len(split(files[idx], "/"))-1), ":p")
  let files = files[idx+1 : ] + (idx ? files[0 : idx-1] : [])
  call filter(files, 'filereadable(path . "/" . v:val)')
  call map(files, 'a:cmd . " " . path . "/" . v:val')
  return get(files, 0, 'echoerr "No alternative file"')
endfunction

let g:related_files = ['CMakeLists.txt', 'Makefile.am', 'configure.ac']
command! -bang A execute Alternative('edit<bang>')
command! -bang AS execute Alternative('split<bang>')

This behaves much like A.vim. You can just use :A to cycle through your files and use :AS to open a related file in a split. Change g:related_files as you see fit.

If you have a more structured project you may want to consider something like projectionist.vim.

|improve this answer|||||
0

I've added a control from a tree to it:

___________________
|>. |CMake |ATools|
| . |      |      |
| . |      |      |
|>  |      |      |
| . |      |      |
-------------------

When the file is opened from the tree with the 'o' key, it opens it in the first split and the next corresponding file in the second split.

Tree filter is set to only show CMakeLists.txt, Makefile.am and configure.ac in the directories.

execute pathogen#infect()
syntax on
filetype plugin indent on

" toggle between relate files in dir
function! Alternative(cmd, ...) abort
  let f = expand("%:p:gs?\\?/?")
  let files = map(copy(get(g:, 'related_files', [])), 'fnamemodify(expand(v:val), ":gs?\\?/?")')
  let files = a:0 && a:1 ? reverse(files) : files
  let idx = index(map(copy(files), 'f[len(f)-len(v:val)-1:] ==# "/" . v:val'), 1)
  if idx == -1
    return 'echoerr "No alternative file"'
  endif
  let path = fnamemodify(expand('%:p:h:gs?\\?/?') . repeat("/..", len(split(files[idx], "/"))-1), ":p")
  let files = files[idx+1 : ] + (idx ? files[0 : idx-1] : [])
  call filter(files, 'filereadable(path . "/" . v:val)')
  call map(files, 'a:cmd . " " . path . "/" . v:val')
  return get(files, 0, 'echoerr "No alternative file"')
endfunction

let g:related_files = ['CMakeLists.txt', 'Makefile.am', 'configure.ac']
command! -bang A execute Alternative('edit<bang>')
command! -bang AS execute Alternative('vsplit<bang>')

" split to the right
set splitright

" launch the tree immediately
autocmd VimEnter * NERDTree

autocmd FileType nerdtree nnoremap <buffer> o :only^M :call nerdtree#ui_glue#invokeKeyMap("o")^M :AS^M :wincmd t^M

" filter tree for autotools-CMake porting
let NERDTreeIgnore = ['\(CMakeLists\.txt\|Makefile\.am\|configure\.ac\)\@<!$[[file]]']

Currently it is not adapted for displaying of all three files at once (but looking too much at configure.ac is not too useful anyways).

And one small thing that may be useful is to always display CMakeLists.txt in the left split and Makefile.am in the right regardless of which file was selected in the tree.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.