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.
Related
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.
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.
Is there a best practice around lazily loading modes when encountering a relevant file extension?
At this point I have roughly 25 different Emacs modes installed, and startup has become slow. For example, although it's great to have clojure-mode at the ready, I rarely use it, and I want to avoid loading it at all unless I open a file with extension .clj. Such a "lazy require" functionality seems like the right way do mode configuration in general..
I found nothing online, so I've taken a crack at it myself.
Instead of:
(require 'clojure-mode)
(require 'tpl-mode)
I have this:
(defun lazy-require (ext mode)
(add-hook
'find-file-hook
`(lambda ()
(when (and (stringp buffer-file-name)
(string-match (concat "\\." ,ext "\\'") buffer-file-name))
(require (quote ,mode))
(,mode)))))
(lazy-require "soy" 'soy-mode)
(lazy-require "tpl" 'tpl-mode)
This seems to work (I'm an elisp newbie so comments are welcome!), but I'm unnerved about finding nothing written about this topic online. Is this a reasonable approach?
The facility you want is called autoloading. The clojure-mode source file, clojure-mode.el, includes a comment for how to arrange this:
;; Add these lines to your .emacs:
;; (autoload 'clojure-mode "clojure-mode" "A major mode for Clojure" t)
;; (add-to-list 'auto-mode-alist '("\\.clj$" . clojure-mode))
This is one way,
(provide 'my-slime)
(eval-after-load "slime"
'(progn
(setq slime-lisp-implementations
'((sbcl ("/usr/bin/sbcl"))
(clisp ("/usr/bin/clisp")))
common-lisp-hyperspec-root "/home/sujoy/documents/hyperspec/")
(slime-setup '(slime-asdf
slime-autodoc
slime-editing-commands
slime-fancy-inspector
slime-fontifying-fu
slime-fuzzy
slime-indentation
slime-mdot-fu
slime-package-fu
slime-references
slime-repl
slime-sbcl-exts
slime-scratch
slime-xref-browser))
(slime-autodoc-mode)
(setq slime-complete-symbol*-fancy t)
(setq slime-complete-symbol-function 'slime-fuzzy-complete-symbol)
(add-hook 'lisp-mode-hook (lambda () (slime-mode t)))))
(require 'slime)
along with,
;; slime mode
(autoload 'slime "my-slime" "Slime mode." t)
(autoload 'slime-connect "my-slime" "Slime mode." t)
I am involved in python project where tabs are used, however i am not using them in every other code i write, it is vital to use them in that particular project. Projects are located in one directory under specific directories. I.E:
\main_folder
\project1
\project2
\project3
...etc
I have couple functions/hooks on file open and save that untabify and tabify whole buffer i work on.
;; My Functions
(defun untabify-buffer ()
"Untabify current buffer"
(interactive)
(untabify (point-min) (point-max)))
(defun tabify-buffer ()
"Tabify current buffer"
(interactive)
(tabify (point-min) (point-max)))
;; HOOKS
; untabify buffer on open
(add-hook 'find-file-hook 'untabify-buffer)
; tabify on save
(add-hook 'before-save-hook 'tabify-buffer)
If i put it in .emacs file it is run on every .py file i open which is not what i want. What i`d like to have is to have these hooks used only in one particular folder with respective subfolders. Tried .dir_locals but it works only for properties not hooks. I can not use hooks in specific modes (i.e. python-mode) as almost all projects are written in python. To be honest i tried writing elisp conditional save but failed.
A very easy solution is to just add a configuration variable that can be used to disable the hooks. For example:
(defvar tweak-tabs t)
(add-hook 'find-file-hook
(lambda () (when tweak-tabs (untabify (point-min) (point-max)))))
(add-hook 'before-save-hook
(lambda () (when tweak-tabs (tabify (point-min) (point-max)))))
Now you can add a .dir-locals.el file in the relevant directories, setting tweak-tabs to nil, disabling this feature there.
(But another problem is that this is a pretty bad way to deal with tabs. For example, after you save a file you do see the tabs in it.)
Just for the record, to answer the literal question in the title (as I reached this question via a web search): one way to add a hook that depends on file location is to make it a function that checks for buffer-file-name. (Idea from this answer.)
For example, for the exact same problem (turn on tabs only in a particular directory, and leave tabs turned off elsewhere), I'm currently doing something like (after having installed the package smart-tabs-mode with M-x package-install):
(smart-tabs-insinuate 'python) ; This screws up all Python files (inserts tabs)
(add-hook 'python-mode-hook ; So we need to un-screw most of them
(lambda ()
(unless (and (stringp buffer-file-name)
(string-match "specialproject" buffer-file-name))
(setq smart-tabs-mode nil)))
t) ; Add this hook to end of the list
(This is a bit inverted, as smart-tabs-insinuate itself modifies python-mode-hook and then we're modifying it back, but it should do as an example.)
I just started using emacs after having used vi for a long time. :)
One thing which is annoying me is that whenever I modify a file, save it and exit emacs, I see a backup file created in the same directory named filename~ (if the file I edited was filename).
Is there any way I can get rid of this? Or hide these files? It is very annoying to see tons of backup files when I do ls of the directory.
You can either move them to their own folder with the following code:
;; Don't clutter up directories with files~
(setq backup-directory-alist `(("." . ,(expand-file-name
(concat dotfiles-dir "backups")))))
;; Don't clutter with #files either
(setq auto-save-file-name-transforms
`((".*" ,(expand-file-name (concat dotfiles-dir "backups")))))
Or you can remove them completely, like so:
(setq make-backup-files nil)
(setq auto-save-default nil)
Personally I would be wary of removing them as they can come in useful. Further discussion is here:
http://www.emacswiki.org/emacs/BackupDirectory
http://www.emacswiki.org/emacs/AutoSave
I would recommend checking out the emacs-starter-kit it sorts out a load of issues that people have when coming to emacs, and is pretty heavily used.
http://github.com/technomancy/emacs-starter-kit/blob/master/starter-kit-misc.el
Update:
There seems to be much confusion over how to use the functions. I'm going to have a little play around later but here is some more information. Note that auto-save-file-name-transforms:
lets you specify a series of regular expressions and replacements to transform the auto save file name
[emacs-manual]
so it's not just as simple as adding in a folder name. That said it seems from a quick google search the following might just do what you all want:
;;; backup/autosave
(defvar backup-dir (expand-file-name "~/.emacs.d/backup/"))
(defvar autosave-dir (expand-file-name "~/.emacs.d/autosave/"))
(setq backup-directory-alist (list (cons ".*" backup-dir)))
(setq auto-save-list-file-prefix autosave-dir)
(setq auto-save-file-name-transforms `((".*" ,autosave-dir t)))
http://www.google.com/codesearch?hl=en&lr=&q=auto-save-file-name-transforms&sbtn=Search
The following lines in ~/.emacs will put all of the auto-save and backup files in /tmp:
(setq backup-directory-alist
`((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
`((".*" ,temporary-file-directory t)))
In your .emacs:
(setq make-backup-files nil)
Edit:
If you're unfamiliar with the .emacs file, it's a file named .emacs that resides in your user $HOME directory. If you don't have one already, you can just create it and emacs will load it on startup.
Here is a link to the same question answered on SuperUser and my response. And a StackOverflow question entitled Emacs: Don’t create #these# files when not saving modified buffer
And for completeness, as stated by others; to stop the backup files being created put this in your .emacs
(setq make-backup-files nil)