Emacs: Using and initializing CEDET - emacs

I'm using Emacs with CEDET and auto complete for code completion. Originally I set up CEDET so it loads at Emacs start up every time.
However, this took quite a long time, so i thought it would be clever to load it just if needed, i.e. - in my case - when entering C++-Mode.
So I moved the original function into a lambda that is called when entering C++-mode:
; cscope for c(++) programming (finding symbols, etc.)
(require 'xcscope)
; C++ stuff
(add-hook 'c++-mode-hook
(lambda ()
(load-file "/usr/share/emacs/site-lisp/cedet-common/cedet.el")
(global-ede-mode 1) ; enable project management system
(semantic-load-enable-code-helpers) ; enable prototype help and smart completion
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/elisp/ac-dict")
(add-to-list 'ac-sources 'ac-source-semantic)
(local-set-key (kbd "C-:") 'semantic-ia-complete-symbol-menu) ; set shortcut for auto completion.
(local-set-key (kbd "C-.") 'ac-complete-semantic)
(ac-config-default)
)
)
There are no errors, but I have the following problem: When Emacs enters C++-mode for the first time, code completion does not work properly. But if Emacs enters C++-mode the second time, everything works just fine.
Does anybody know what I'm doing wrong?

CEDET initialization sets up C and C++ mode hooks of it's own. If it installs it's hook while it is running the same hook, then it won't run, and your first buffer won't have initialized.
What you could do is load CEDET at startup, but then init the code helpers in the C mode hook. That way C++ mode will initialize with mode-local, but the extra functions will be installed after the hook runs, so that may work. I hadn't tried it myself.
I think EDE mode isn't slow, so it is probably ok to do that at init time too.

Related

How can load a .el configure file with a specified mode in emacs

Usually, I put the confugire .el files in src directory for all kinds of languages. Such as Go, the go-conf.el file:
(add-hook 'before-save-hook 'gofmt-before-save)
(add-hook 'go-mode-hook (lambda ()
(local-set-key (kbd "M-.") 'godef-jump)))
(add-hook 'go-mode-hook (lambda ()
(local-set-key (kbd "M-,") 'godef-jump-back)))
(add-to-list 'load-path "/usr/local/go/src/github.com/dougm/goflymake")
(add-hook 'after-init-hook #'global-flycheck-mode)
(require 'flycheck)
(require 'go-autocomplete)
(require 'auto-complete-config)
(ac-config-default)
)
(provide 'go-conf)
Then, in init.el, I write this line
(require 'go-conf)
Although go-conf can be loaded successfully, emacs launches slowly. It is because that emacs loads go-conf whatever files are opened. I can not tolerate it.
It is better that only when Go file is opened, go-conf is loaded.
I modify the init.el as :
(add-hook 'go-mode-hook '(lambda ()
(require 'go-conf)
(go-conf)
))
But it does not work!!
who can help me?
Your code seems to assume that the whole Emacs only has a single buffer and mode, whereas that is not the case. E.g. (add-hook 'before-save-hook 'gofmt-before-save) affects all buffers, whether they're using go-mode or not. Same for (add-hook 'after-init-hook #'global-flycheck-mode). Emacs is designed such that you can start it once and then edit hundreds of different files at the same time in that one Emacs session. So you should probably rewrite your code along the lines of:
(defun my-go-lang-config ()
(add-hook 'before-save-hook #'gofmt-before-save nil 'local)
(local-set-key (kbd "M-.") 'godef-jump)
(local-set-key (kbd "M-,") 'godef-jump-back)
(add-to-list 'load-path "/usr/local/go/src/github.com/dougm/goflymake")
(require 'go-autocomplete))
(add-hook 'go-mode-hook #'my-go-lang-config)
(require 'auto-complete-config)
(ac-config-default)
(global-flycheck-mode 1)
where the last three lines are part of your "generic" config (not specific to support for the Go language), meaning that you want to use flycheck and auto-complete whenever it's available rather than only in go-mode.
Your code to add to the hook doesn't work because the hook is run only after the mode is turned on, and the mode is not defined until the library is loaded. It makes no sense to load the same library in the mode hook.
If Emacs becomes slow after loading some library, it is probably due to that library. Is is slow after loading the library even if you do not turn the mode on?
You can try byte-compiling the library code. That can sometimes make a big difference in performance. You can use M-x byte-compile to compile a given file.
If compiling does not help, and if you do not seen anything suspect in buffer *Messages* (e.g., warnings that seem like they might be pertinent), then consider contacting the library maintainer, reporting the problem and asking for a remedy.
If go-mode itself is already available (most likely loaded on demand via an addition to auto-mode-alist, which is probably taken care of automatically if it was installed as an ELPA package), and you're just looking to load your custom library at the same time, then you can use eval-after-load:
(eval-after-load 'go-mode
'(require 'go-conf))
Make sure that the parent directory for your go-conf.el library is in the load-path, of course, otherwise require won't find it.

Keep getting mode name when switching buffers in Emacs

I have strange thing in my Emacs and I can't locate it, everytime I switch a buffer I get message with major mode name even when I call the function I get minibuffer-inactive-mode
The only global function (for all modes) in my .emacs file (I think) is this:
(add-hook 'after-change-major-mode-hook (lambda ()
(if (not (memql (intern (major-mode))
'(fundamental-mode
erc-mode
text-mode
sql-mode)))
(local-set-key (kbd "RET")
'new-line-and-indent-fix))))
How to find the place that add this annoying thing? What different hook can be executed on each mode?
There is no major-mode function in vanilla Emacs. Whatever that function is in your config, it's probably responsible for displaying the message you're seeing.
You want to fix your code (as per Stefan's comment), but you probably also want to look into that non-standard function:
M-x find-function RET major-mode RET

Auto Completion with auto-complete OR predictive based on mode

I'm using emacs-snapshot (24.2.50) on Xubuntu 12.10. For completion I am currently using auto-complete (v1.4).
I have a pretty nice setup of auto-complete with various sources, including the semantic source for code completion of my c++ programming. However, I'd like to switch to predictive-mode for completion of LaTeX documents (I'm using AUCTeX for all LaTeX related stuff).
My current auto-complete config (for LaTeX; I omitted all non-LaTeX config) looks like this:
(require 'auto-complete-latex)
(require 'ac-math)
(add-to-list 'ac-modes 'latex-mode) ; make auto-complete aware of {{{latex-mode}}}
(defun ac-latex-mode-setup () ; add ac-sources to default ac-sources
(setq ac-sources
(append '(ac-source-math-latex ac-source-latex-commands)
ac-sources))
)
(add-hook 'LaTeX-mode-hook 'ac-latex-mode-setup)
After adding predictive to the load-path and doing the auto-load stuff as described here I tried to disable auto-complete for LaTeX-mode and enable predictive-mode in turn by exchanging the above code by this:
(defun ele/latex-mode-completion-setup ()
(auto-complete-mode -1)
(predictive-mode 1)
)
(add-hook 'LaTeX-mode-hook 'ele/latex-mode-completion-setup)
Unfortunately this doesn't work as expected: auto-complete is actually disabled but predictive-mode is not enabled and instead of using AUCTeX the build-in tex-mode is used.
I have uploaded all LaTeX related config here. Note that this is loaded after the above completion setup, but this is the case for the old auto-complete based setup as well. Also note that exchanging that order does not make a difference as far as I can tell. Furthermore I found that simply commenting the auto-complete config (first snippet I posted) results in the same behaviour: tex-mode is used instead of AUCTeX.
I don't want to switch to predictive-mode for all modes but I really like many features it provides when doing LaTeX editing.
Any suggestions?
(remove-hook
might clear the hook, not to run unwanted stuff - just a suggestion indeed

Auto-complete mode doesn't turn on automatically in ObjC buffers

I load auto-complete mode like this:
(let ((ac-path "path/to/auto-complete"))
(add-to-list 'load-path ac-path)
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories (concat ac-path "ac-dict"))
(ac-config-default))
It works fine with C major mode, but doesn't turn on automatically when I open ObjC files. I can still turn it on manually and it will work fine along with the ObjC major mode.
Here's a snippet from the docs regarding ObjC major mode:
The hook `c-mode-common-hook' is run with no args
at mode initialization, then `objc-mode-hook'.
If I understand correctly, auto-complete adds a hook to the c-mode-common-hook, but objc-mode-hook somehow overrides it. Is there a way to fix this?
Thanks.
While looking through the source code of auto-complete.el, I've stumbled upon this definition
(defcustom ac-modes
'(emacs-lisp-mode
lisp-interaction-mode
c-mode cc-mode c++-mode
java-mode clojure-mode scala-mode
scheme-mode
ocaml-mode tuareg-mode
perl-mode cperl-mode python-mode ruby-mode
ecmascript-mode javascript-mode js-mode js2-mode php-mode css-mode
makefile-mode sh-mode fortran-mode f90-mode ada-mode
xml-mode sgml-mode)
"Major modes `auto-complete-mode' can run on."
:type '(repeat symbol)
:group 'auto-complete)
It turns out that auto-complete doesn't have a true global mode. It is enabled only with those major modes that are included in the ac-modes variable.
So, adding the following line to the .emacs file has solved the issue for me.
; add this line after the auto-complete mode has been loaded
(add-to-list 'ac-modes 'objc-mode)
Use the following:
(defun my-objc-mode-hook ()
(auto-complete-mode 1))
(add-hook 'objc-mode-hook 'my-objc-mode-hook)
Note 1: The function auto-complete-mode is a toggle function, when called with no arguments.
Note 2: It's possible to add an anonymous function using lambda, but this have several drawbacks. The most important ones are: modifying the function and reevaluating the expression will add the modified function in addition to the earlier version and C-h v xxx will print the full unformatted lambda function, which typically is hopeless to read and understand.
(add-hook 'objc-mode-hook 'auto-complete-mode)
That should do it if you're using auto-complete-mode. You can add more complex things to mode hooks by doing:
(add-hook 'objc-mode-hook '(lambda ()
(something-with arguments)))
Note that both arguments to add-hook are quoted, this is necessary and if you add unquoted functions they will probably not work.

Emacs imenu integration with cedet code auto-completion

Hi I can't integrate the imenu with the CEDET code completion. what appears when I invoke auto-completion is another buffer with the possible words.
reference
My .emacs file:
(require 'color-theme)
(color-theme-initialize)
(color-theme-blue-mood)
;; Load CEDET
(load-file "/home/user/cedet-1/common/cedet.el")
(global-ede-mode 1) ; Enable the Project management system
(semantic-load-enable-code-helpers) ; Enable prototype help and smart completion
(global-srecode-minor-mode 1) ; Enable template insertion menu
;; control + space
(global-set-key [?\C- ] 'semantic-complete-analyze-inline)
(load-library "completion")
(global-set-key (kbd "C-.") 'complete)
(defun my-semantic-hook ()
(imenu-add-to-menubar "TAGS"))
(add-hook 'semantic-init-hooks 'my-semantic-hook)
I'm not entirely sure what you are asking, but I'll guess that when you select C-SPC, you expect a menu to pop up? The code completion engine uses a bunch of different completion output mechanisms, but a menu isn't one of them because the Emacs menu system grabs focus, and prevents further typing. If you just want a menu, then you should bind C-SPC to semantic-ia-complete-symbol-menu instead.
Imenu is a tool that shows all tags in a buffer in a menu. Completion is a system by which Emacs provides a list of possible words that will complete some symbol. They are not related with the sole exception of when Imenu's tag collection mechanism is used by by a completion prompt, which CEDET does not enable.