Smattr / clink

a modern re-implementation of Cscope
The Unlicense
38 stars 2 forks source link

Emacs support #231

Closed abougouffa closed 5 months ago

abougouffa commented 5 months ago

Hello @Smattr ,

Thank you for this awesome tool,

I'm wondering if there is a simple way to integrate Clink with Emacs? I've googled it but didn't find any package so far.

Thanks in advance

Smattr commented 5 months ago

Thanks for your interest!

There’s no Emacs support right now, but it would be possible to add. There are two things Clink uses Vim for:

  1. Syntax highlighting
  2. Opening a file to edit

Implementing (2) for Emacs is pretty trivial. (1) will be significantly harder. Clink actually leans on another project to get that done.

I guess you need both (1) and (2)?

abougouffa commented 5 months ago

Hi again,

Thank you for the quick reply.

There are two things Clink uses Vim for:

Ah OK, I see now.

Well for me, I'm looking for an extension (a package) to make use of Clink while working on C/C++ projects in Emacs.

I've found a similar package for Cscope, it it's readme page, it says:

It can answer such questions as

  • Where is this variable used?
  • What is the value of this preprocessor symbol?
  • Where is this function in the source files?
  • What functions call this function?
  • What functions are called by this function?
  • Where does the message “out of space” come from?
  • Where is this source file in the directory structure?
  • What files include this header file?
  • Where was this variable assigned-to?

I don't know if Clink is supposed to provide these same features (I suppose!). I will give it a try later, if Clink arguments are compatible with cscope's one, it should work with it out of the box.

Smattr commented 5 months ago

Most of those queries are supported, though I dropped some that I found less useful.

Clink uses some command line parameters to Vim to teach it to run Clink instead of Cscope. Look in https://github.com/Smattr/clink/blob/82eb5bba6b1d9014b764e046f61975d2b0022d3a/libclink/src/vim_open.c for cscopeprg and +cs. This works because Cscope support is implemented within Vim itself and exposes these variables. I do not know if there is an Emacs equivalent. The fundamental problem being solved here is that Vim is hard coded to speak Cscope’s line protocol for querying this stuff, so a script alongside Clink reads Clink’s database and speaks this protocol to Vim.

As a first step, you probably want to try creating a database. If you’re in a directory that contains some C/C++ code, clink --build-only --syntax-highlighting=lazy should create a .clink.db. This file is just a SQLite database, so you can sqlite3 .clink.db and explore it if you’re curious. Clink will look for a compile_commands.json but if it can’t find one (likely), it will end up falling back to execing Cscope.

abougouffa commented 5 months ago

Thank you for the information.

I will try to integrate it in my Emacs configuration, I will inform you if I managed to make a useful package for Clink.

Thanks again!

Smattr commented 5 months ago

No problem! Good luck! Let me know if you have any more questions.

abougouffa commented 4 months ago

Hi again @Smattr

Just to let you know that I started writing an Emacs package to integrate with Clink.

I used the clink-repl example to know how to query the database and get the needed information ~(only clink-find-symbol is implemented for now, the rest should be easier as I defined all the helper functions)~.

It's worth noting that the package still a WIP, I will complete it when I get some free time.

I have a small question, as I've seen in the README, the “find assignments to this symbol” functionality is not yet implemented in Clink. Will it take a lot of effort to be implemented?

I would like to have such a feature since I find it very helpful. My main use case is when I'm exploring a big codebase and trying to track down where a variable has been modified in order to understand the code structure and so on.

Smattr commented 4 months ago

Just to let you know that I started writing an Emacs package to integrate with Clink.

Cool!!

“find assignments to this symbol” functionality is not yet implemented in Clink. Will it take a lot of effort to be implemented?

That I do not know offhand. I can certainly look into it though. Stay tuned…

While we’re discussing this, would it help you for me to implement preliminary Emacs stuff? Or does this make your job harder by churn in the very areas of the code base you’re likely to be touching? E.g. what I’m thinking is querying the $EDITOR environment variable and suppressing Vim-based syntax highlighting if it looks like your editor is Emacs.

abougouffa commented 4 months ago

Hi @Smattr ,

While we’re discussing this, would it help you for me to implement preliminary Emacs stuff? Or does this make your job harder by churn in the very areas of the code base you’re likely to be touching? E.g. what I’m thinking is querying the $EDITOR environment variable and suppressing Vim-based syntax highlighting if it looks like your editor is Emacs.

In my use case (and I think, it is the case for many other Emacs users), I would like to use Emacs as my main editing/searching environment and have Clink do the background magic to find the symbols and stuff. I almost never use the Clink ncurses interface (so, features like syntax highlighting, open with editor, ... aren't that important to me).

I have a side question, I use Clink (or cscope, global, ctags) for a huge code base which contains many projects (hence, it is extremely difficult to setup an LSP server for the project). With other tools including cscope, there is an option to pass a list of files to index (cscope.files for example). However, I don't see an equivalent in Clink! Is there any option to pass a list of files to process, knowing that this list can be huge (in my current project, a curated list of absolute paths sizes over 300MB).

Smattr commented 4 months ago

Interesting question. It’s not a Cscope option I’ve used before, but it looks pretty straightforward to implement. I’ve created #234 for that and will give it a go.

abougouffa commented 4 months ago

@Smattr Thank you !

Smattr commented 4 months ago

I just implemented -i. Let me know if it works for what you need.

BTW if you’re in the process of kicking the tires on Clink, you probably want to make sure you’re building an optimised version (-DCMAKE_BUILD_TYPE=RelWithDebInfo to CMake). Clink is written in a style that results in a large performance difference between debug and release builds.

abougouffa commented 4 months ago

Awesome! I will give it a try!

Thank you!