I'm using auto-complete and yasnippet in Emacs and I am confused by their settings. I placed the following code in my .emacs:
(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas/global-mode 1)
(global-set-key (kbd "C-i") 'yas/expand)
(setq yas/also-auto-indent-first-line t)
(add-to-list 'load-path "~/.emacs.d/plugins/autocomplete/")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/plugins/autocomplete/ac-dict")
(ac-config-default)
(setq ac-use-menu-map t)
(define-key ac-menu-map "\C-n" 'ac-next)
(define-key ac-menu-map "\C-p" 'ac-previous)
(defun ac-js-mode()
(setq ac-sources '(ac-source-yasnippet
ac-source-symbols
ac-source-words-in-buffer
ac-source-words-in-same-mode-buffers
ac-source-files-in-current-dir
)))
(add-hook 'js-mode-hook 'ac-js-mode)
I am trying to set yasnippet as the first candidate in the auto-complete popup menu. However, as the example below shows, this doesn't work with my current settings: when I type the word for, formatItem is in first position and for in second. formatItem is just a local function in current buffer.
for_____________
|formatItem |
|for a|
|for s|
|force s|
|foreachv s|
----------------
So my question is: how can I make yasnippet the first candidate in auto-complete? And is there something missing in my .emacs config?
Any help is appreciated.
ac-config-default installs hooks to setup sensible default ac-sources values. These hooks (especially ac-common-setup) might interfere with your settings.
You can check this by looking at the actual value of ac-sources (C-h vac-sources) in a js-mode buffer to see if it has been modified by comparison to your custom value.
If this is the case, I see two solutions:
stop using ac-config-default and specifiy all autocomplete settings,
advise the faulty hook to put ac-source-yasnippet at the beginning of ac-sources after it has run. Assuming ac-common-setup is indeed the troublemaker, this would look like:
(defadvice ac-common-setup (after give-yasnippet-highest-priority activate)
(setq ac-sources (delq 'ac-source-yasnippet ac-sources))
(add-to-list 'ac-sources 'ac-source-yasnippet))
Related
I want to change the key binding M-x slime-fuzzy-complete-symbol to M-TAB in both slime-mode and slime-repl.
I looked up the common lisp manual, and tried the following sentences:
(eval-after-load 'slime
`(define-key slime-prefix-map (kbd "M-TAB") 'slime-fuzzy-complete-symbol))
(add-hook 'slime-load-hook
#'(lambda ()
(define-key slime-prefix-map (kbd "M-TAB") 'slime-fuzzy-complete-symbol)))
(define-key slime-repl-mode-map (kbd "M-TAB")
'slime-fuzzy-complete-symbol)
None of them is useful. The third sentence even have an error:
Symbol's value as variable is void: slime-repl-mode-map
For reference, the following the is my init.el relate to slime:
(let ((default-directory "/usr/local/share/emacs/site-lisp/"))
(normal-top-level-add-subdirs-to-load-path))
;; Setup load-path, autoloads and your lisp system
;; Not needed if you install SLIME via MELPA
(add-to-list 'load-path "~/default-directory/slime")
(require 'slime-autoloads)
(setq inferior-lisp-program "/usr/local/opt/clozure-cl/bin/ccl64")
;; Setup slime-repl
(setq slime-contribs '(slime-scratch slime-editing-commands))
;;(setq slime-contribs '(slime-repl)) ; repl only
(setq slime-contribs '(slime-fancy)) ; almost everything
;;Setup suto-complete
(add-to-list 'load-path "~/default-directory/auto-complete/")
(require 'auto-complete-config)
;;(add-to-list 'ac-dictionary-directories "~/default-directory/auto-complete/ac-dict")
(ac-config-default)
I know this is an old question, but a few weeks ago I was looking for an answer to a similar one and I hope additional info may be useful for someone...
Here's what I've found out:
using (eval-after-load 'slime) in your .emacs (dot-emacs) file is not a good idea until you have (autoload 'slime) in it - and this is OK - without that (eval-after-load "...") may wait till the end of time for loading slime
the previous answer is OK if global-key-binding is appropriate for you
if you require or prefere local binding you may define your own function and bind it locally, e.g.:
(defun my-slime-mode-keybindings ()
"For use in `slime-mode-hook' and 'slime-repl-mode-hook."
(local-set-key (kbd "<C-f1>") 'slime-describe-symbol)
(local-set-key (kbd "<M-f1>") 'slime-apropos-all)
(local-set-key (kbd "C-c C-p") nil) ;; when you want to remove a key/sequence
(local-set-key (kbd "C-<tab>") 'ace-window)
(local-set-key (kbd "M-<tab>") 'slime-fuzzy-complete-symbol) ;; your case :)
) ;; end of defun my-slime-mode-keybindings()
;; tell emacs to use your function only in required mode(s)
(add-hook 'slime-mode-hook #'my-slime-mode-keybindings)
(add-hook 'slime-repl-mode-hook #'my-slime-mode-keybindings)
best regards
It will be better to follow the emacs documentation for this case:
Key binding commands
In your case with a global key bindoing should work:
(global-set-key (kbd "M-TAB") 'slime-fuzzy-complete-symbol)
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)))
I want autocomplete or company to work with C code and I need structure context level autocomplete i.e if I have a struct:
typefdef struct HOST_ {
IP4_ADDRESS ip4;
IP6_ADDRESS ip6;
MAC_ADDRESS mac;
} HOST;
... and then I have code:
HOST host;
host->
Then if I hit tab then I should get a drop down menu giving me all the (and only the) three options.
This is what I did:
(defun my-tab-del-commands(key-map)
"This function sets the <tab> and <backspace> keymaps according to the special commands
that I have set. This needs to be set per mode keymap where it is needed. We cannot
use the global keymap because that would override command completion in minibuffer"
;; Set TAB in major-mode keymap to "tab-to-tab" stop
;; Use tab2tab stop and not just add +4 spaces (tab stop defined by tag-stop-list)
(define-key key-map (kbd "<tab>") 'tab-to-tab-stop)
;; Define Key for <backtab>
(define-key key-map (kbd "<S-tab>") 'backward-move-to-tab-stop)
(define-key key-map (kbd "<backtab>") 'backward-move-to-tab-stop)
;; Define C-tab as my-indent-system
(define-key key-map (kbd "<C-tab>") 'my-indent-system)
;; Also provide a duplicate "\C-ci" in case C-TAB is overridden by any minor mode
(define-key emacs-lisp-mode-map "\C-ci" 'my-indent-system)
;; Shift backspace moves by one tab stop till no more whitespace
(define-key key-map (kbd "<S-backspace>") 'backspace-whitespace-to-tab-stop)
;; Alt backspace is c-hungry-delete-backwards
(define-key key-map (kbd "<M-backspace>") 'c-hungry-delete-backwards)
)
;; Simple Backend of semantic with ede as project manager
;; Lets try semantic (CDET) backend for now only.
;; Later learn clang or rtags or other big stuff to handle the whole project
(defun backend:semantic-ede ()
"Setup for semantic-mode with ede as project management.
Simple stuff for starters"
(semantic-mode 1)
(global-semanticdb-minor-mode 1)
(global-semantic-idle-scheduler-mode 1)
(setq-mode-local c-mode semanticdb-find-default-throttle
'(project unloaded system recursive))
(semantic-add-system-include my-lp-build-path)
(semanticdb-enable-gnu-global-databases 'c-mode t)
(semanticdb-enable-gnu-global-databases 'c++-mode t)
(require 'ede)
(global-ede-mode)
(let ((makefile (format "%s/Makefile" my-mp-source-path)))
(when (file-exists-p makefile)
(ede-cpp-root-project "openflow" :file makefile)))
)
;; There are two "schools of thought" of intelligent autocomplete
;; (a) auto-complete package and (b) company package.
;; This is auto-complete
(defun completion:auto-complete ()
"Setup auto-complete with triggers. We still need to hook with C mode"
;; Then autocomplete
(require 'auto-complete)
(require 'auto-complete-config)
(global-auto-complete-mode t)
;; auto complete mod
;; should be loaded after yasnippet so that they can work together
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(ac-config-default)
(add-hook 'auto-complete-mode-hook 'ac-common-setup)
;; set the trigger key so that it can work together with yasnippet on tab key,
;; if the word exists in yasnippet, pressing tab will cause yasnippet to
;; activate, otherwise, auto-complete will
(ac-set-trigger-key "TAB")
(ac-set-trigger-key "<tab>")
(global-auto-complete-mode t)
)
(defun my-smart-c-auto-complete()
;; Now tie up autocomplete with semantic
;; Later hook it up to c-mode-hook via c-mode-config func
(require 'auto-complete-c-headers)
(add-to-list 'ac-sources 'ac-source-words-in-same-mode-buffers)
(add-to-list 'ac-sources 'ac-source-dictionary)
(add-to-list 'ac-sources 'ac-source-abbrev)
(add-to-list 'ac-sources 'ac-source-semantic)
(add-to-list 'ac-sources 'ac-source-c-headers)
)
;; This is company
(defun completion:company ()
"Setup comany mode"
(require 'company)
(add-hook 'after-init-hook 'global-company-mode)
)
(defun tab-side-story ()
(interactive)
;; First load and init Yasnippet - This sets the TAB first to Ysnippet
;; This will work on LISP & SHELL SCRIPTS also
;; (require 'yasnippet)
;; (yas-global-mode 1)
;; Second, use whatever completion mechanism you wanna use auto-complete or company
;; This will work on LISP also!
;;(completion:auto-complete)
(completion:company)
;; Then this function sets tab command
(my-tab-del-commands c-mode-base-map)
)
;; Completion mechanism needs a backend
(backend:semantic-ede)
(tab-side-story)
;; define My Great C style with linux as the parent style (choose google if you wish)
(c-add-style "my-great-c-style"
'("linux" ; use linux as parent style
(indent-tabs-mode . nil) ; tabs are spaces
(c-syntactic-indentation . t) ; auto-syntax
(c-syntactic-indentation-in-macros . t) ; ditto for macros
(c-tab-always-indent . nil) ; Otherwise our complex TAB will not work!
(tab-width . 4) ; if there is a <tab> expand to 4 spaces
(backward-delete-function . nil) ; DO NOT expand tabs when deleting
(c-continued-statement-offset . 4))) ;continued statement offset 4
(defun my-c-mode-config ()
(c-set-style "my-great-c-style")
(when (= emacs-major-version 23)
(gtags-mode 1))
(when (= emacs-major-version 24)
(tab-side-story)
;; (my-smart-c-auto-complete)
(ggtags-mode 1)
(setq ggtags-update-on-save nil))
(electric-indent-mode)
(setq c-basic-offset 4)
(setq c-tab-always-indent nil))
;; Needs to be hooked - do it here instead of common area
(add-hook 'c-mode-hook 'my-c-mode-config)
Sorry for the very long code but most of them are my comments and notes jotted down so far less "code" than it seems. In this I have set it for company mode though I have auto-complete.
Both are not working! Something is clashing with something.
EDIT My issue was related to snippet syntax all along... The configuration below totally works.
I'm trying to use org-mode and yasnippet together and it's not working even with some of the workarounds on the org-mode FAQ. Whenever I hit TAB on a snippet abbreviation the word gets deleted. TAB behaves normally if I'm not over a snippet word, so there's something going on...
I'm using Org-mode version 7.7, yasnippet (version 0.7.0), and GNU Emacs 23.4.1.
Here's my setup:
(setq load-path
(append (list nil
"~/.emacs.d/site-lisp/yasnippet"
"~/.emacs.d/site-lisp/org-7.7/lisp")
load-path))
;; set up yasnippet
(require 'yasnippet)
(yas/initialize)
(setq yas/snippet-dirs '("~/.emacs.d/mysnippets"
"~/.emacs.d/site-lisp/yasnippet/snippets"))
(mapc 'yas/load-directory yas/snippet-dirs)
;; set up org mode
(require 'org-install)
;; fix some org-mode + yasnippet conflicts:
(defun yas/org-very-safe-expand ()
(let ((yas/fallback-behavior 'return-nil)) (yas/expand)))
(add-hook 'org-mode-hook
(lambda ()
(make-variable-buffer-local 'yas/trigger-key)
(setq yas/trigger-key [tab])
(add-to-list 'org-tab-first-hook 'yas/org-very-safe-expand)
(define-key yas/keymap [tab] 'yas/next-field)))
And I'm pretty sure the hook is running as expected because of the following output of C-h v org-tab-first-hook in an org buffer:
org-tab-first-hook is a variable defined in `org.el'.
Its value is
(yas/org-very-safe-expand org-hide-block-toggle-maybe org-src-native-tab-command-maybe org-babel-hide-result-toggle-maybe)
And here's C-h k TAB in an org buffer:
<tab> runs the command org-cycle, which is an interactive Lisp
function in `org.el'.
EDIT
After doing a edebug-defun on my yas/org-very-safe-expand function I'm seeing the following message
Result: "[yas] elisp error! Symbol's value as variable is void: err"
So yas is error'ing out somewhere... My edebug foo is not quite up to par but if I get some time I'll try to single step through and see where the error is. My full emacs configuration is on github here.
Turns out my issue was related to snippet syntax, not setup. Silly silly me...
In other words, this totally works:
;; fix some org-mode + yasnippet conflicts:
(defun yas/org-very-safe-expand ()
(let ((yas/fallback-behavior 'return-nil)) (yas/expand)))
(add-hook 'org-mode-hook
(lambda ()
(make-variable-buffer-local 'yas/trigger-key)
(setq yas/trigger-key [tab])
(add-to-list 'org-tab-first-hook 'yas/org-very-safe-expand)
(define-key yas/keymap [tab] 'yas/next-field)))
This took me some time to get worked out as well. I'm using Org-mode version 7.7, yasnippet (version 0.6.1c), GNU Emacs 22.1.1. Here are the relevant portions of my .emacs file (there is some flyspell stuff that is irrelevant):
;;
;; org-mode stuff
;;
(add-to-list 'load-path "/Users/cmalone/install/org-mode/org-mode/lisp")
(add-to-list 'load-path "/Users/cmalone/install/org-mode/org-mode/contrib/lisp")
(require 'org-install)
;;
;; for YASnippet
;;
(add-to-list 'load-path "/Users/cmalone/.emacs.d/plugins/yasnippet-0.6.1c")
(require 'yasnippet)
(yas/initialize)
(yas/load-directory "/Users/cmalone/.emacs.d/plugins/yasnippet-0.6.1c/snippets")
;; Make TAB the yas trigger key in the org-mode-hook and enable flyspell mode and autofill
(add-hook 'org-mode-hook
(lambda ()
;; yasnippet
(make-variable-buffer-local 'yas/trigger-key)
(org-set-local 'yas/trigger-key [tab])
(define-key yas/keymap [tab] 'yas/next-field-group)
;; flyspell mode for spell checking everywhere
;; (flyspell-mode 1)
;; auto-fill mode on
(auto-fill-mode 1)))
C-h v org-tab-first-hook is the same as yours except of the yas/org-very-safe-expand of course. C-h k TAB shows:
TAB runs the command yas/expand
which is an interactive Lisp function in `yasnippet.el'.
It is bound to TAB, <menu-bar> <YASnippet> <Expand trigger>.
(yas/expand)
Expand a snippet before point.
If no snippet expansion is possible, fall back to the behaviour
defined in `yas/fallback-behavior'
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)