Emacs CEDET Hook - emacs

I want to hook CEDET modes to c++ mode. I am using the following script in my .emacs file:
(add-hook 'c++-mode-hook
(lambda ()
...
(my_cedet_load)
)
)
where
(defun my_cedet_load ()
(interactive)
(semantic-mode)
(global-semantic-stickyfunc-mode t)
(global-semantic-idle-scheduler-mode t)
(global-semantic-idle-completions-mode t)
(global-semantic-highlight-edits-mode t)
)
Now, the problem is that once I open a .cpp file, the semantic-mode is enabled in all buffers. How do I only enable such mode in only .cpp files?

Semantic is a global minor mode. From semantic.el
To enable Semantic, turn on `semantic-mode', a global minor mode (M-x
semantic-mode RET, or "Source Code Parsers" from the Tools menu). To
enable it at startup, put (semantic-mode 1) in your init file.
As such when you do semantic-mode it is enabled in all buffers. You can use semantic-inhibit-functions to restrict the buffers in which semantic is activated. From the documentation
List of functions to call with no arguments before Semantic is setup.
If any of these functions returns non-nil, the current buffer is not
setup to use Semantic.
Below is an example of using this variable. it would instruct semantic to be activated only in c-mode, cc-mode and java-mode buffers
(add-to-list 'semantic-inhibit-functions
(lambda () (not (member major-mode '(java-mode c-mode c++-mode)))))

I'm guessing the key lies in the global word.
So use semantic-stickyfunc-mode instead of global-semantic-stickyfunc-mode etc.
UPDATE:
Try this:
(add-hook 'c++-mode-hook 'my-c++-hook)
(defun my-c++-hook ()
(semantic-mode 1)
(semantic-stickyfunc-mode 1)
(semantic-idle-scheduler-mode 1)
(semantic-idle-completions-mode 1)
(semantic-highlight-edits-mode 1))

Related

emacs autoload modes

I am a newbie.
Now I want to
auto start yasnippet auto-complete gtags flymake modes
when I opening a (php/java/el/...) file.
What Should I put in my emacs config file ?
As liu says, you use add-hook to specify that an action be run when a particular mode is started. Documentation here. Hooks are analogous to events in C#, or the observer pattern in OOP.
Every mode should define a hook that is run when that mode is started, eg. for a mode named java-mode there is a corresponding hook called java-mode-hook. add-hook lets you wire an action to this hook:
(add-hook 'java-mode-hook 'my-action)
You can use anonymous lambda functions to define the action in-place, like so:
(add-hook 'java-mode-hook (lambda () (message "hello from java-mode")))
This will print a message whenever you start up java mode.
It is usually better to define a named function for the purpose. If you use named functions, add-hook will ensure the same function is not called multiple times.
(defun hello ()
(message "hello from java-mode"))
(add-hook 'java-mode-hook 'hello)
Language editing modes are derived from prog-mode. If you want to run an action when you start up any programming language mode, you add your function to prog-mode-hook.
You need to manually configure autocomplete mode for each mode you want to use it in. For each mode, add that mode to the ac-modes list:
(add-to-list 'ac-modes 'java-mode)
As a side note, you will want to use flyspell-prog-mode for programming language modes, so you only receive spelling suggestions in comments and string literals.
(defun on-prog-mode ()
(flyspell-prog-mode t))
(add-hook 'prog-mode-hook 'on-prog-mode)
And you will probably want to use flycheck instead of the older flymake. Flycheck is under active development and has checkers for php.
you can add just like below:
(global-auto-complete-mode t)
(yas-global-mode 1)
then update below accordingly:
(add-hook 'php-mode-hook (lambda () (flyspell-mode 1)))
(add-hook 'php-mode-hook (lambda()(gtags-mode 1)))

why mode isn't enabled?

I've got an emacs configuration file whatever.el :
(abbrev-mode +1)
(provide 'whatever)
and in my init.el :
(require 'whatever)
but when i start emacs, abbrev-mode isn't enabled. why ?
thank you
Quoting from http://emacswiki.org/emacs/AbbrevMode:
You can also put the following in your ~/.emacs file if you want it
always on:
(setq default-abbrev-mode t)
If you only want it on in text and derived modes, you could do
something like this:
(add-hook 'text-mode-hook (lambda () (abbrev-mode 1)))
For multiple modes, use something like the following:
(dolist (hook '(erc-mode-hook
emacs-lisp-mode-hook
text-mode-hook))
(add-hook hook (lambda () (abbrev-mode 1))))
Abbrev-mode is enabled per-buffer.
One way is to create a hook function that you could add to the major mode hooks you will want to use it in.
For example:
(defun my-enable-abbrev-mode ()
(abbrev-mode 1))
(add-hook 'c-mode-hook 'my-enable-abbrev-hook)
(add-hook 'java-mode-hook 'my-enable-abbrev-hook)
Another approach is to use change-major-mode-hook.
While others explained how to get what you presumably want, I'll just point out that w.r.t to your actual question ("Why?"), the reason is simple: abbrev-mode is a buffer-local minor-mode, so when you run (abbrev-mode +1) at startup it will just enable abbrev-mode in the buffer that happens to be current during evaluation of the ~/.emacs (typically scratch) but not in subsequent buffers.

how to disable hl-line feature in specified mode

i enable hl-mode in global scope with the following code.
(global-hl-line-mode t)
to turn off hl-line feature in a specified mode. i do it with the following code.
(add-hook 'org-mode-hook (lambda () (global-hl-line-mode 0)))
but it turns off the hl-line feature for global scope.
how to disable hl-line feature in a specified mode?
There is often information and documentation in the Commentary
section in the source file.
[...]
;; You could make variable `global-hl-line-mode' buffer-local and set
;; it to nil to avoid highlighting specific buffers, when the global
;; mode is used.
[...]
Thus, you can put something like this in your .emacs.
(global-hl-line-mode)
(make-variable-buffer-local 'global-hl-line-mode)
(add-hook 'some-mode-hook (lambda () (setq global-hl-line-mode nil)))
...
use hl-line-mode insted of global-hl-line-mode.
EDIT: You're right. This doesn't work.
The commentary says that the global-mode isn't meant to be used. I take it to
mean that you can't selectively disable it once enabled.
I enable the hl-line-mode in major-mode hooks where I need it.
write the following line in your dotEmacs file, and use f5 to toggle the hl-line-mode
(global-set-key [f5] 'hl-line-mode)

Enable auto-complete in Emacs minibuffer

I'm trying to turn auto-complete in the minibuffer:
(add-hook 'minibuffer-setup-hook 'auto-complete-mode)
What I get is auto-complete working in the first instance of minibuffer, but no longer. That is the full minibuffer-setup-hook after loading:
(auto-complete-mode turn-on-visual-line-mode ido-minibuffer-setup rfn-eshadow-setup-minibuffer minibuffer-history-isearch-setup minibuffer-history-initialize)
How to turn auto-complete on persistently?
You rarely ever want to add a function symbol to a hook variable if that function acts as a toggle (which will be the case for most minor modes).
minibuffer-setup-hook runs "just after entry to minibuffer", which means that you would be enabling auto complete mode the first time you enter the minibuffer; disabling it the second time; enabling it the third time; etc...
Typically you would either look to see if there's a pre-defined turn-on-autocomplete-mode type of function, or define your own:
(defun my-turn-on-auto-complete-mode ()
(auto-complete-mode 1)) ;; an argument of 1 will enable most modes
(add-hook 'minibuffer-setup-hook 'my-turn-on-auto-complete-mode)
I can't test that, because you haven't linked to the autocomplete-mode you are using.
The creator of "auto-complete-mode" explicitly excludes the minibuffer for use with auto completion. The definition for the minor mode is:
(define-global-minor-mode global-auto-complete-mode
auto-complete-mode auto-complete-mode-maybe
:group 'auto-complete)
so the "turn mode on" function is "auto-complete-mode-maybe" - the definition of that function is:
(defun auto-complete-mode-maybe ()
"What buffer `auto-complete-mode' prefers."
(if (and (not (minibufferp (current-buffer)))
(memq major-mode ac-modes))
(auto-complete-mode 1)))
This function explicitly tests in the if statement if the current-buffer is the minibuffer and doesn't turn on the auto-complete-mode if it is.
If you want to use auto-complete-mode in the minibuffer, you should probably contact the maintainer of the mode and ask him why he excluded the minibuffer and what programming changes he feels are necessary to enable the mode in the minibuffer.
Zev called to my attention auto-complete-mode-maybe, and that is the required modifications (file auto-complete.el, all changes have comments):
;; Add this variable
(defcustom ac-in-minibuffer t
"Non-nil means expand in minibuffer."
:type 'boolean
:group 'auto-complete)
...
(defun ac-handle-post-command ()
(condition-case var
(when (and ac-triggered
(not (ido-active)) ;; Disable auto pop-up in ido mode
(or ac-auto-start
ac-completing)
(not isearch-mode))
(setq ac-last-point (point))
(ac-start :requires (unless ac-completing ac-auto-start))
(ac-inline-update))
(error (ac-error var))))
...
(defun auto-complete-mode-maybe ()
"What buffer `auto-complete-mode' prefers."
(if (or (and (minibufferp (current-buffer)) ac-in-minibuffer) ;; Changed
(memq major-mode ac-modes))
(auto-complete-mode 1)))
And .emacs:
(add-hook 'minibuffer-setup-hook 'auto-complete-mode)
Certainly, there are binding conflicts but it is possible to solve them.

Emacs disable modes

I'd like to disable line highlighting in term-mode. I tried this:
(add-hook 'term-mode-hook '(lambda () (global-hl-mode 0)))
but it complains about the symbol being void.
I have this further in my .emacs:
(global-hl-line-mode 1)
I agree with Ashutosh that that may be the source of your symbol error, but I'm not sure that that's the right approach anyways. I'm pretty sure that will disable highlighting everywhere, not just in terminal windows, when you load a terminal window.
I think the right thing is this:
(add-hook 'term-mode-hook '(lambda() (set (make-local-variable 'global-hl-line-mode) nil)))
...I'm going off hl-line.el where it says this:
;; You could make variable `global-hl-line-mode' buffer-local and set
;; it to nil to avoid highlighting specific buffers, when the global
;; mode is used.