Visiting required file from .emacs - file

Is it possible to visit a mentioned file in require like:
(require 'key-chord)
For which when clicking on "key-chord" (or some other input than clicking), makes you visit that file?
EDIT: Given that we want to visit the file "key-chord.el"

Put the cursor on whatever is being required, e.g. the "k" in "key-chord", and do M-x ffap RET RET. ffap is an alias for find-file-at-point, which understands require, at least in lisp-mode. I have ffap bound to C-x f, because I rarely have use for that key's default binding.
Some Emacs installations don't have the .el files (only the .elc files). In Debian/Ubuntu, you need to install a separate package to get them (emacs-version-el, IIRC).

locate-library tells you the file name, and find-file opens the file in a buffer for editing. (You might want find-file-read-only if you merely want to inspect it.)
(find-file (locate-library "key-chord.el" t))
You can turn it into a function, something like this:
(defun find-locate-library (lib)
"Visit the source for library LIB in a buffer."
(let ((location (locate-library (concat lib ".el") t)))
(if location
(find-file location)
(message "Could not find library %s" lib)) ))
How to hook this into a viewing or editing buffer is a separate topic. I suppose you could amend Elisp mode to make library names clickable, but I cannot tell off-hand how to do that.
Thanks to #Deokhwan Kim for the suggestion to limit search to ".el" files only.

M-x find-library RET prompts you for the name of a library, and then visits that file.
Calling it with point on a valid library name will make that the default prompt value.
Calling it with point anywhere within a require statement will also do the right thing.
I use find-library frequently, and bind it to C-hC-l for convenience.

Related

.emacs: c-set-style will delay display of new style until I change the buffer (type a key)

Dealing with files of different C flavours, I defined the following function and a keyboard mapping to invoke it:
(defun my-c-set-gnu-style ()
"doc string"
(interactive)
(setq tab-width 8
indent-tabs-mode t)
(c-set-style "gnu")
)
(global-set-key (kbd "C-x :") 'my-c-set-gnu-style)
Now when I open a C file with emacs, and then type C-x: then nothing happens. Only after I hit some key (and thus I change the code, which is really bad), emacs will re-draw the code according to the new style.
How can I achieve that emacs will re-indent the code right after the respective command has been issued? The need to change the file is really bad.
Version is GNU Emacs 26.3.
The issue isn't related to c-set-style. Changing the TABs mode like in (setq tab-width 8 indent-tabs-mode t) should also have an immediate visual effect — which it does not have.
Using (redisplay t) didn't solve the problem, but what worked is (force-window-update) at the end of the defun. Dunno if that's supposed to be issued by hand or whether the requirement is an Emacs bug, though.

Prevent CEDET semantic from parsing certain file types

I have to work with a C/C++ build environment that drops intermediate files all over the place:
.i files containing the output of the C-preprocessor (roughly raw C)
.s files containing the input of the C-assembler
CEDET (I assume the semantic analyzer) eventually finds these files and attempts to index them. This results in jumping to .i files containing raw C for definitions and generally slowing down parsing and loading of the .semanticdb.
I never open these files in emacs, so they must be being loaded by the background analyser.
Is it possible to prevent the analyser from loading these files? I can't find any configuration options that define the file-types that are parsed by the background analyser.
If you never need C mode for these files, here's a quick fix:
(add-to-list 'auto-mode-alist '("\\.i\\'" . fundamental-mode))
(add-to-list 'auto-mode-alist '("\\.s\\'" . fundamental-mode))
The answer from abo-abo gave me the clues I needed. The grep implementation (used by EDE) of semantic-symref-perform-search uses auto-mode-alist to find matching files for a given semantic mode (based on the current buffer's mode - eg `c-mode) when trying to resolve symbols.
The final fix I used is to specifically eliminate the default entries in the auto-mode-alist using:
(delete '("\\.i\\'" . c-mode) auto-mode-alist)
(delete '("\\.ii\\'" . c++-mode) auto-mode-alist)
Adding fundamental-mode entries as suggested by abo-abo seems to work also, however I was concerned that since the c-mode entries were still in the list a change in implementation could result in them being reactivated.

When cedet semantic parsing c header files of my system, here comes "Idle Parse Error":

No method definition: semanticdb-add-reference, (nil (\"stdio.h\"
include (:system-flag t) (unlink-copy-hook
(semantic--tag-unlink-copy-secondary-overlays) link-hook
(semantic--tag-link-secondary-overlays) secondary-overlays (# #) unlink-hook
(semantic--tag-unlink-secondary-overlays) dependency-file
\"d:/MinGW/include/stdio.h\") #))"
I use mingw in windows 7 system, c.c is the file name, and there are two sentence in init.el which are relevant to the problem:
(semantic-add-system-include "D:/MinGW/include" 'c-mode)
(setq semantic-c-dependency-system-include-path "D:/MinGW/include")
The include path you are setting should be a list, such as:
(setq semantic-c-dependency-system-include-path '("d:/MinGW/include"))
but you don't need that line if you are using semantic-add-system-include.
While I haven't tried it, you can probably skip doing either of the above by instead using semantic-gcc-setup and ask your minGW compiler. Just make sure a command "gcc" is on your exec-path when you start Emacs, and it will be automatically detected.
No one has reported this working or not working for MinGW, so if it works out, let us know.
"M-x customize" then
search "Global Semanticdb Minor Mode" then
toggle on it

How to find the callers and callee of a function in C code in vi/vim?

I want to know how can I easily click (or maybe use some easy shortcuts) on a function name and find all its callee or open where it has been defined. Most of the web manuals in web are really hard to follow or don't happen to work out. Say I want to click on allocuvm and see where it has been defined?
uint newstk=allocuvm(pgdir, USERTOP-PGSIZE, USERTOP);
cscope minimal example
Ingo mentioned it, here is an example.
First you should set on your .vimrc:
set cscopequickfix=s-,c-,d-,i-,t-,e-
Then to the base directory of your project and run:
cscope -Rb
This generates a cscope.out file which contains the parsed information. Generation is reasonably fast, even for huge projects like the Linux kernel.
Open vim and run:
:cs add cscope.out
:cs find c my_func
c is a mnemonic for callers. The other cscope provided queries are also possible, mnemonics are listed under:
help cscope
This adds a list of the callers to the quickfix list, which you can open with:
:copen
Go to the line that interests you and hit enter to jump there.
To find callers of the function name currently under the cursor, add to your .vimrc:
function! Csc()
cscope find c <cword>
copen
endfunction
command! Csc call Csc()
and enter :Csc<enter> when the cursor is on top of the function.
TODO:
do it for the current function under cursor with a single command. Related: Show function name in status line
automatically add the nearest database (parent directories) when you enter a file: how to auto load cscope.out in vim
interactively open the call graph like Eclipse. Related: Generate Call-Tree from cscope database
A word of advice: I love vim, but it is too complicated for me to setup this kind of thing. And it does not take into account classes e.g. in C++. If a project matters enough to you, try to get the project working on some "IDE". It may involve some overhead if the project does not track the IDE configuration files (which are auto-changing blobs that pollute the repo...), but it is worth it to me. For C / C++, my favorite so far was KDevelop 4.
For that, Vim integrates with the cscope tool; see :help cscope for more information.
vi / . --- / is the search function in vi, and . will repeat the same command.
you could also use sed ( stream editor ) if it is a large file
sed
grep can get you the line numbers
read the man page

emacs open includes in code files, multiple directories

ECB, cscope, xcscope. All working. Is cedet necessary?
MSVS, eclipse, code::blocks, xcode. All of them allow easy click on an included source file and take you to it.
Now, with the above setup, emacs does too.
Except emacs doesn't take you to the std:: libraries, doesn't assume their location in /src/linux or some such. Emacs is a little blind and needs you to manually set it up.
But I can't find anything that explains how to set up ff-find-other-file to search for any other directories, let alone standard major libraries, outside of a project's directory.
So, how do I do it?
Edit; Most important is to be able to request on either a file name (.h, .c, .cpp, .anything) or a library (iostream) and open the file in which the code resides.
Additional directories for ff-find-other-file to look into are in ff-search-directories variable which by default uses the value of cc-search-directories, so you should be able to customize any of the two to specify additional search paths.
As for the second question about requesting a file name and finding corresponding file, something like that will do:
(defun ff-query-find-file (file-name)
(interactive "sFilename: ")
;; dirs expansion is borrowed from `ff-find-the-other-file` function
(setq dirs
(if (symbolp ff-search-directories)
(ff-list-replace-env-vars (symbol-value ff-search-directories))
(ff-list-replace-env-vars ff-search-directories)))
(ff-get-file dirs file-name))
Call it with M-x ff-query-find-file or bind it to a key to your liking.

Resources