How can load a .el configure file with a specified mode in emacs - 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.

Related

disable autocomplete for specific filetype or language in emacs [duplicate]

I am using Emacs 24 and would like to disable auto-complete mode while in python-mode so it does not conflict with jedi. How do I go about doing this (sadly I do not know Emacs Lisp). Below are my current settings regarding auto-complete in init.el:
;; auto-complete settings
(require 'auto-complete)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(require 'auto-complete-config)
(ac-config-default)
; Start auto-completion after 2 characters of a word
(setq ac-auto-start 2)
; case sensitivity is important when finding matches
(setq ac-ignore-case nil)
Thanks.
(ac-config-default) turns on global-auto-complete-mode, to stop (auto-complete-mode) from being called in python mode you can write an advice for it.
(defadvice auto-complete-mode (around disable-auto-complete-for-python)
(unless (eq major-mode 'python-mode) ad-do-it))
(ad-activate 'auto-complete-mode)
Also I am not sure this is what you want, since Jedi use auto-complete-mode as Dmitry pointed out in the comment, there should not be conflicts.
I think I had a similar problem: my yellow Jedi popups with Python-specific content were taken over by grey popups from a more general auto-complete feature. Hence, I could choose non-Python related options, but not the Python-specific ones.
What helped for me was ensuring that auto-complete-mode is disabled in Python mode:
(add-hook 'python-mode-hook (lambda () (auto-complete-mode -1)))

Disable fill in xml-mode

I'm trying to configure Emacs to not activate Auto-Fill when editing an XML document.
In my .emacs file, I add a hook so that text mode will have Auto-Fill on by default:
(add-hook 'text-mode-hook 'turn-on-auto-fill)
I have added a directory to my load path:
(add-to-list 'load-path "~/.emacs.d/lisp/")
Inside that directory, I have written a file xml.el for this workstation, and I have tried each of the following in it, to no avail:
(add-hook 'xml-mode-hook 'turn-off-auto-fill)
(add-hook 'xml-mode-hook 'auto-fill-mode)
(remove-hook 'xml-mode-hook 'turn-on-auto-fill)
(remove-hook 'xml-mode-hook 'auto-fill-mode)
How can I disable Auto-Fill in XML mode?
EDIT: It appears this is caused by my text-mode-hook mentioned above. How can I override this hook in nxml-mode?
Ah, nxml-mode derives from text-mode. That's slightly surprising to me (although on closer inspection, it does appear to be standard for markup language modes in Emacs).
In that case you can either disable it again in nxml-mode-hook (as text-mode-hook has already run by that point):
(add-hook 'nxml-mode-hook 'turn-off-auto-fill)
(n.b. you said in the comments that this didn't work for you, but it certainly works for me).
or else just change your text-mode-hook code to something like the following, in order to catch this case before auto-fill is enabled:
(defun my-text-mode-hook ()
"Custom behaviours for `text-mode'."
;; Enable `auto-fill-mode', except in `nxml-mode' (which is derived
;; from `text-mode').
(unless (eq major-mode 'nxml-mode)
(turn-on-auto-fill)))
(add-hook 'text-mode-hook 'my-text-mode-hook)

setting up semantic with cscope

I'm starting to experiment a bit with using emacs as my development envrionment and I am running into a bit of trouble. I wish to use cscope with semantic for a fairly robust way of searching through my code base. However, after installing cscope (with apt-get install cscope) and moving xscope.el into my ~/.emacs.d/, I am still having trouble calling some settings with my .emacs file. When I try to call (semanticdb-enable-cscope-databases), I get an error that the symbol's function definition is void. I am using emacs 24.3
(semantic-mode 1)
(global-ede-mode 1)
(require 'semantic/ia)
;; Semantic
(global-semantic-idle-completions-mode t)
(global-semantic-decoration-mode t)
(global-semantic-highlight-func-mode t)
(global-semantic-show-unmatched-syntax-mode t)
;; auto-complete stuff
(add-to-list 'load-path "~/.emacs.d")
(require 'auto-complete-config)
(ac-config-default)
(add-hook 'c-mode-common-hook '(lambda ()
;; ac-omni-completion-sources is made buffer local so
;; you need to add it to a mode hook to activate on
;; whatever buffer you want to use it with. This
;; example uses C mode (as you probably surmised).
;; auto-complete.el expects ac-omni-completion-sources to be
;; a list of cons cells where each cell's car is a regex
;; that describes the syntactical bits you want AutoComplete
;; to be aware of. The cdr of each cell is the source that will
;; supply the completion data. The following tells autocomplete
;; to begin completion when you type in a . or a ->
(add-to-list 'ac-omni-completion-sources
(cons "\\." '(ac-source-semantic)))
(add-to-list 'ac-omni-completion-sources
(cons "->" '(ac-source-semantic)))
;; ac-sources was also made buffer local in new versions of
;; autocomplete. In my case, I want AutoComplete to use
;; semantic and yasnippet (order matters, if reversed snippets
;; will appear before semantic tag completions).
(setq ac-sources '(ac-source-semantic ac-source-yasnippet))
))
(require 'xcscope)
(semanticdb-enable-cscope-databases) ;;This is causing problems
;;C mode
(require 'cc-mode)
;;Color theme
(require 'color-theme)
(setq color-theme-is-global t)
(add-to-list 'load-path "/home/bob/.emacs.d/theme/ample-theme/ample-theme.el")
;;(require 'ample-theme)
(eval-after-load "color-theme"
'(progn
(color-theme-initialize)
(color-theme-jsc-dark)))
;;set font
(set-face-attribute 'default nil :family "Anonymous Pro" :height 140)
;;line numbers
(global-linum-mode 1)
(custom-set-variables '(linum-format (quote "%4d \u2502 ")))
;;treat .h files at C++
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
;; use F5 as compile
(global-set-key [(f5)] 'compile)
;; make compilation window smaller
(setq compilation-window-height 8)
Now, I really start writing an answer to be able to refine it with time. That is how far I got until now:
There are several versions of cedet.
Emacs 24.3 includes cedet-2.0. But, with respect to the bazaar version cited below it seems to be slightly outdated.
I believe that in this version cscope is supported as one of the tools in semantic-symref-tool-alist.
The variable semantic-symref-tool-alist is described in the info manual. One gets there with the key strokes C-h i g (semantic-user) Configuring SymRef.
One can see the default value of semantic-symref-tool-alist after loading semantic/symref. One of its members is:
((lambda
(rootdir)
(file-exists-p
(expand-file-name "cscope.out" rootdir)))
. cscope)
I think that this is the cscope support in in the built-in version of cedet-2.0 and no additional enabling of cscope is required (?).
The official release is cedet-1.1 from https://sourceforge.net/projects/cedet/files/cedet/cedet-1.1.tar.gz/download.
In this version the function semanticdb-enable-cscope-databases is defined in the file semantic/semanticdb-cscope.el
The bazar-version of cedet is cedet-2.0. It is available via bazaar under:
bzr checkout bzr://cedet.bzr.sourceforge.net/bzrroot/cedet/code/trunk cedet
In this version the function semanticdb-enable-cscope-databases is defined in cedet/semantic/db-cscope.el.
This file is missing in the version of cedet shipped with emacs 24.3.
Σ: That makes me believe that if you want to use your setup you should use the bazaar version of cedet-2.0.

Emacs: load only necessary yasnippets

Suppose, I have 2 subdirectories for yasnippets:
~/.emacs.d/yasnippets/perl-mode
~/.emacs.d/yasnippets/php-mode
Currently I use the following code in my .emacs:
(defvar *my-emacs-lib-dir* "~/.emacs.d/")
(load (concat *my-emacs-lib-dir* "plugins/yasnippet/yasnippet"))
(setq yas/snippet-dirs nil)
(yas/initialize)
;; Develop and keep personal snippets under ~/emacs.d/yasnippets
(setq yas/root-directory (concat *my-emacs-lib-dir* "yasnippets"))
(yas/load-directory yas/root-directory)
So, it loads all the yasnippets in all the subdirectories of ~/.emacs.d/yasnippets.
Is it possible to make it load the yasnippets on demand? If I open a php file, and the snippets for php-mode were not loaded, load them. But not load everything on startup.
If I remember correctly, in fresh versions, the loading of snippets will performed on demand, if you'll use recommended loading sequence:
(add-to-list 'load-path "~/path-to-yasnippet")
(require 'yasnippet)
(yas-global-mode 1)
You can also use the optional use-jit flag to the yas-load-directory function, that will force on demand loading of snippets from this directory. See description of this function (C-h f yas-load-directory)
Maybe something like this can work.
(defvar yas/loaded-php-snippets nil)
(defun yas/load-php-snippets()
(if (not yas/loaded-php-snippets)
(progn
(yas/load-directory (concat yas/root-directory) "/php-mode")
(setq yas/loaded-php-snippets t))))
(add-hook 'php-mode-hook 'yas/loaded-php-snippets)
This is just an example but one could conceivably have map between mode-hooks and yas load directories and just load specific directories if they are not yet been loaded.

Auto-complete with go-mode

I'm trying to enable auto-complete-mode whenever a .go file is loaded through go-mode. It works if I invoke auto-complete-mode manually for Go source files, but when I tried adding it to .emacs as below, it doesn't work:
(add-hook 'go-mode-hook auto-complete-mode)
I've tried a few variations around it but none seem to work. Following is what the Go-Mode snippet currently looks like in my .emacs:
;; Load Go Mode
(require 'go-mode-load)
(add-hook 'go-mode-hook 'auto-complete-mode)
I tried creating my own hook function like this:
;; Load Go Mode
(require 'go-mode-load)
(defun auto-complete-for-go ()
(auto-complete-mode 1))
(add-hook 'go-mode-hook 'auto-complete-for-go)
I also tried including the hook in go-mode-load.el and go-mode.el, as well as calling auto-complete-mode like this:
(auto-complete-mode t)
(provide 'go-mode)
Doesn't work either way. I also added the go-mode-hook to auto-complete-default function like so:
(defun ac-config-default ()
(setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
(add-hook 'go-mode-hook 'ac-common-setup)
;; Other hooks
(global-auto-complete-mode t))
That doesn't work either. What's the best way to trigger a command just after a major mode is enabled for a buffer?
Here is workaround for now:
(add-to-list 'ac-modes 'go-mode)
I fixed the problem in v1.4 branch with the following commits.
Add go-mode to ac-modes
Add go-mode dictionary
Which variations have you tried? It should work if you add a single-quote in front of auto-complete-mode:
(add-hook 'go-mode-hook 'auto-complete-mode)
Without this quote, auto-complete-mode is interpreted as a variable and the value of that variable is added to go-mode-hook. For this to make sense, such a variable should contain a function reference as its value. Most likely though there will be no variable named auto-complete-mode and Emacs will complain.
By adding a quote, you tell Emacs that this is not a variable, but the actual function you want the hook to call. See also here and here.