Emacs C++, opening corresponding header file - c++

I am new to emacs and I was wondering
if there is a shortcut to switch between header/source and the corresponding source/header file
if there is a reference card like the general emacs ref card
Thanks !

There's ff-find-other-file.
You can bind this to your own key using something like:
(global-set-key (kbd "C-x C-o") 'ff-find-other-file)
But of course you'll need to pick a key that doesn't already have something useful bound to it :)

Check the excellent Emacs-fu article Quickly switching between header and implementation
In case the blog post has gone, here is copy of the solution:
(add-hook 'c-mode-common-hook
(lambda()
(local-set-key (kbd "C-c o") 'ff-find-other-file)))
Now, we can quickly switch between myfile.cc and myfile.h with C-c o.
Note the use of the c-mode-common-hook, so it will work
for both C and C++.

I'm using eassist package from CEDET contrib, and it has eassist-switch-h-cpp function to do this taks

Related

How to find documentation in Core.Std?

Where is the documentation for functions, symbols under Core.Std ?
Or, is there any conventional way to look up ocaml documentation rather than guessing in utop REPL ?
E.g. I know if open Core.Std, then the function String.split will be imported. But it's hard to find out what's the function parameter etc.
Also, Ocaml don't have source code links to it's documentation like Haskell does, I guess.
The current best solution is looking at: https://ocaml.janestreet.com/ocaml-core/111.28.00/doc/core_kernel/#Core_string
Merlin is able to perform documentation lookup and jump to the definition. In Emacs, I'm assuming, that you're using Emacs, the documentation lookup is not bound by default to any key, so I bound it to C-c C-d. Add the following to your emacs configuration file:
(define-key merlin-mode-map (kbd "C-c C-d") 'merlin-document)
(define-key merlin-mode-map (kbd "C-c d") 'merlin-destruct)
The lookup function will jump directly to the mli file. This is my favorite way of reading the documentation. Unfortunately, due to a bug, Janestreet stopped to ship the mli file, so the feature is somewhat broken with the core. As a workaround, you can install source code with
opam source core_kernel
And then create a .merlin file, and point it to the sources:
S <path-to-sources>
Usually, it would be something like this
S ../core_kernel.113.33.00/src
Note, you should point merlin directly to the source subfolder.
It is also worth noting, that Merlin has intellisence like completion, that helps a lot, and you can hit F1 when you choose an entry from a completion list, and a window with documentation will pop up.
And finally, the link to the documentation, that you're referencing is very outdated. Here is the link to the latest documentation.

Getting rid of ;; in OCaml toplevel

I like Try Ocaml's toplevel behavior: ;; is added implicitly when I press Enter, and I'm able to use Shift-Enter for multiline editing.
Is this possible to get the same feature in standard toplevel or utop?
Not currently. You could imagine implementing this feature as a patch on top (no pun intended) of utop.

C++ Keywords not Colored in Emacs

I have been using emacs for a while for mainly python programming, and have started C++ coding with it. When I open a c++ file, it opens without problems with c++-mode. The background and foreground colors are normal for the theme I have with color-theme, but keywords and strings are not colored differently. Below is the code in my .emacs to initialize color-theme.
(add-to-list 'load-path "D:\\emacs\\color-theme-6.6.0")
(require 'color-theme)
(color-theme-initialize)
(setq color-theme-is-global t)
(color-theme-hober)
I have not put in any code for c++. Any ideas?
Edit: I tried turning off color-theme to see if at least then there would be some coloring, and there was not, even after ensuring font-lock-mode was on. Also, this is GNU Emacs 23.1.1
Place the cursor over a keyword and do M-x describe-face. The face should be identified as font-lock-keyword-face and the description of the face given. At that point it should be easier to determine whether the keyword has the wrong face, or the face just has a default appearance. You could also double-check that font-lock-mode is on with describe-mode.
I finally got syntax highlighting by removing a folder called site-lisp from the folder d:/emacs. My Emacs installation is in d:/emacs/emacs-23.1, and somehow it was reading from these files. So, removing this folder forced Emacs to use the correct ones, I think.
(font-lock-mode) should help (with non negative argument to make sure it's turned on)
Generally this should work straight out of the box on a standard install - to verify start emacs with the "-q" option to avoid loading stuff from your init file. I have verified this with a quick install (on XP Pro) of the official 23.2 binaries. Here's the output: (apparently can't post images due to low reputation - the link is img444.imageshack.us/img444/2680/46117077.png).
If this displays code as you expect, then there's an issue with your init file - I suggest using the standard approach of commenting everything out and selectively adding things back in, until you come across the offending line(s).

emacs completions or IntelliSense the same as on Visual Studio

emacs 22.2.1 on Linux
I am doing some C/C++ programming using emacs. I am wondering does emacs support completions (IntelliSense in Visual Studio).
For example when filling structures I would like to see the list of members when I type the dot operator or arrow operator.
The same would go for function signatures that give me the types I am passing would display.
Meta-/ isn't exactly intelligent, but it does iterate through known names.
This project provides the dropdown style menus you're used to:
http://ecb.sourceforge.net/
you need to take latest version of CEDET package (better, directly from CVS). You can setup it, as described in documentation on this site
I am using cedet with emacs. I tried using the cedet version in Debian but it has some bugs so I uninstalled that and downloaded the cvs version from http://sourceforge.net/projects/cedet/develop
I compiled it in my ~/tmp/emacs-stuff/ directory and then added the following lines to my ~/.emacs.d/custom.el file:
;;needed if cedet is in a custom location
(load-file "~/tmp/emacs-stuff/cedet/common/cedet.el")
;; Enable EDE (Project Management) features
(global-ede-mode t)
;;to enable code folding
(global-semantic-tag-folding-mode)
;; Enabling Semantic (code parsing, smart completion) features
;; (select only one)
;;(semantic-load-enable-minimum-features)
;;(semantic-load-enable-code-helpers)
(semantic-load-enable-gaudy-code-helpers)
;;(semantic-load-enable-all-exuberent-ctags-support)
(global-semantic-idle-scheduler-mode 1) ;The idle scheduler with automatically reparse buffers in idle time.
(global-semantic-idle-completions-mode 1) ;Display a tooltip with a list of possible completions near the cursor.
(global-semantic-idle-summary-mode 1) ;Display a tag summary of the lexical token under the cursor.
;;to work with my include files and cedet
(semantic-add-system-include "~/include" 'c++-mode)
(semantic-add-system-include "~/include" 'c-mode)
;;To use additional features for names completion, and displaying of information for tags & classes,
;; you also need to load the semantic-ia package. This could be performed with following command:
(require 'semantic-ia)
;;to work with systme include files and gcc
(require 'semantic-gcc)
;;integrate semantic with Imenu
(defun my-semantic-hook ()
(imenu-add-to-menubar "TAGS"))
(add-hook 'semantic-init-hooks 'my-semantic-hook)
;;load Semanticdb
(require 'semanticdb)
;;(global-semanticdb-minor-mode 1)
;;working with tags
;; gnu global support
(require 'semanticdb-global)
(semanticdb-enable-gnu-global-databases 'c-mode)
(semanticdb-enable-gnu-global-databases 'c++-mode)
;; ctags
(require 'semanticdb-ectag)
(semantic-load-enable-primary-exuberent-ctags-support)
(defun my-semantic-hook ()
(imenu-add-to-menubar "TAGS"))
(add-hook 'semantic-init-hooks 'my-semantic-hook)
This file gets called by my ~/.emacs file which the following line in it:
(load-file "~/.emacs.d/custom.el")
Now when you are typing a variable and press CTRL+SHIFT+ENTER, a menu of selections will come up with suggestions.
Further, if you have set semantic-complete-inline-analyzer-idle-displayor-class variable to quote semantic-displayor-tooltip, a tooltip with suggestions will also come up after some idle time (1 or 2 seconds).
For some short intro, see http://xtalk.msk.su/~ott/en/writings/emacs-devenv/EmacsCedet.html
For Cedet docs, see: http://cedet.sourceforge.net/
Good luck.
I think you're looking for etags.
http://tulrich.com/geekstuff/emacs.html
Search for TAGS.
If you'd like to use stock emacs to do completion from your project and library include files try this answer
I have this in my .emacs, which makes things a bit easier.
(require 'c-eldoc)
(add-hook 'c-mode-hook 'c-turn-on-eldoc-mode)
This way, I don't have to look up function definitions.
I don't write that much, but I agree that TAGS are also a very useful feature.

How can I refactor C++ source code using emacs?

I'm interested mostly in C++ and method/class name/signature automatic changes.
In recent Emacs versions (24), Semantic is able to this.
Possibly activate semantic mode M-x semantic-mode RET.
Bring up the Symref buffer with C-c , g.
Press C-c C-e to open all references.
Rename with R.
If you can program in elisp, you can look to combination of cedet + srecode from CEDET libraries - it provide all instruments for this task - find callers of functions, get signature, etc. But you need to create refactory tool yourself, using these instruments
I do this a lot, so I'm axiously awaiting other replies too.
The only tricks I know are really basic. Here are my best friends in Emacs when refactoring code:
M-x query-replace
This allows you to do a global search and replace. You'll be doing this a ton when you move methods and commonly-accessed data to other classes or namespaces.
C-x 3
This gives you a display with two buffers side-by side. You can then proceed to load different files in them, and move your cursor from one to the other with C-x o. This is pretty basic stuff, but I mention it because of how powerful it makes the next one...
C-x (
(type any amount of stuff and/or emacs commands here)
C-x )
This is how you define a macro in emacs. Any time you find yourself needing to do the same thing over and over to a bunch of code (and it is too complex for query-replace), this is a lifesaver. If you mess up, you can hit C-g to stop the macro definition, and then undo (C-_) until you are back to where you started. The keys to invoke the macro are C-x e. If you want to do it a bunch of times, you can hit Esc and type in a number first. Eg: Esc 100 C-x e will try to invoke your macro 100 times.
(Note: On Windows you can get "Meta" by hitting the Esc key, or holding down Alt).
The current (2022) state of the art is, I would say, using emacs lsp-mode with a suitable language server.
With the clangd or ccls, which provide the "language server protocol" (lsp) and connect to lsp-mode, you can refactor names with:
M-x lsp-rename
To simplify this setup, I'd recommend using Spacemacs with c-c++ and lsp layers (and using clangd).
For somewhere in between refactoring tools and simple regex, since Emacs 22 you can embed arbitrary elisp expressions in your replacement text, which allows you to do incredibly powerful text manipulation. Steve Yegge wrote a good article on this a while ago.
A friend of mine was playing with xrefactory and said it worked pretty well. It isn't cheap though.
Build cscope symbols.
lookup the symbol you want to refactor.
get into the cscope window, and start a macro after placing cursor on first occurence
ret
c-f your symbol start
navigate to start of your symbol
modify the word
c-x o (back to cscope)
n (for next cscope symbol)
you have to just c-x c-e now
I totally agree that find-and-replace work fine. However , a really nice feature of cedet is 'semantic-symref-list'.
With the cursor on a method, run this command, and you will be presented with a buffer that lists all of the places in your code that reference this tag.
You can still use find-and-replace tricks, and this will confirm that you have changed all the references.
I've been using cquery for my C++ completion which uses Microsoft LSP for IDE <-> Tool communication. cquery server satisfies the requests of the LSP protocol using a clang backend.
lsp-emacs is the package that sits between emacs and the cquery backend (cquery-emacs) which exposes an lsp-rename function. As a completion system, cquery has been very reliable and fast by the way, highly recommended.
Give it a try, follow the getting-started guide on the cquery github:
https://github.com/cquery-project/cquery/wiki/Emacs
Once you've got cquery setup:
Hover your cursor over an identifier (class, var, whatever) you'd like to rename.
M-x lsp-rename
Enter the new name for the identifier.
Do C-x s (save some buffers), which will prompt you to save
all the buffers that were touched by the refactor.
You should probably go through all modified buffers and check what was done after refactoring with any tool/language.