Search code examples
javascriptemacsyasnippet

Javascript environment in emacs


I have been trying to start with emacs for doing node.js based project development.

I have not been able to set up my emacs for this. It has more become like a trial and error kind of black box leading to frustation. Can you help me in setting this up :

I installed plugins with M-x package-install

My emacs file looks like :

;; added for javascript -------
; Use dictionaries by default
(setq-default ac-sources (add-to-list 'ac-sources 'ac-source-dictionary))
(global-auto-complete-mode t)
; 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)

(require 'yasnippet)
;; now enable the yasnippet for snippets
(yas-global-mode 1)

;; set the snippets directory
(setq yas-snippet-dirs
      '( "~/.emacs.d/plugins_old/yasnippet/yasmate/snippets" ;; the yasmate collection
        "~/.emacs.d/plugins_old/yasnippet/snippets" ;; the snippet collection
        "~/.emacs.d/elpa/yasnippet/snippets"         ;; the default collection
        ))

(ac-set-trigger-key "TAB")
(ac-set-trigger-key "<tab>")

;; (define-key yas-minor-mode-map (kbd "TAB") 'yas-expand)
;; (define-key yas-minor-mode-map (kbd "<tab>") 'yas-expand)
;; (setq yas-trigger-key "")
;; (setq yas-next-field-key "")
;; (setq yas-prev-field-key "")


;; Let's have snippets in the auto-complete dropdown
(add-to-list 'ac-sources 'ac-source-yasnippet)

;; adding lint node
;; (add-to-list 'load-path "~/.emacs.d/plugins_old/lintnode")

;; finally followed this : http://www.emacswiki.org/emacs/FlymakeJavaScript to install jslint in emacs
(require 'flymake-jslint)
(add-hook 'js-mode-hook 'flymake-jslint-load)

;; Make sure we can find the lintnode executable
;; (setq lintnode-location "~/.emacs.d/plugins_old/lintnode")
;; JSLint can be... opinionated
;; (setq lintnode-jslint-excludes (list 'nomen 'undef 'plusplus 'onevar 'white))
;; Start the server when we first open a js file and start checking
;; (add-hook 'js-mode-hook
;;           (lambda ()
;;             (lintnode-hook)))

;; added red cursor for wrong lint node errors
(require 'flymake-cursor)

;; fold the functions
( add-hook 'js-mode-hook
       (lambda ()
         ;; Scan the file for nested code blocks
         (imenu-add-menubar-index)
         ;; Activate the folding mode
         (hs-minor-mode t)))
;; Show-hide
(global-set-key (kbd "") 'hs-show-block)
(global-set-key (kbd "") 'hs-show-all)
(global-set-key (kbd "") 'hs-hide-block)
(global-set-key (kbd "") 'hs-hide-all)

;; repl for javascript
(require 'js-comint)
;; Use node as our repl
(setq inferior-js-program-command "node")

(setq inferior-js-mode-hook
      (lambda ()
        ;; We like nice colors
        (ansi-color-for-comint-mode-on)
        ;; Deal with some prompt nonsense
        (add-to-list 'comint-preoutput-filter-functions
                     (lambda (output)
                       (replace-regexp-in-string ".*1G\.\.\..*5G" "..."
                     (replace-regexp-in-string ".*1G.*3G" "&gt;" output))))))
;; -----------------------------

This is the part that i recently added for setting up the environment ; you may see a lot of commented code which shows i tried this or that from everywhere. I followed mostly : deadpansincerity and then tried lately with turnongtx

What i mostly want is yasnippet and autocomplete with jslint to work ? Other features can be addons, but i am struggling for two days with these only.

My emacs version is 24.3.1 where many things do break.

I observed that after 1 complete one day of trial and error, yasnippet started working but after restart of emacs it is not working. It means that i might have done some right things during that emacs session with reloading the .emacs file. but again i do not know what is happening where.

When i open any js file and write if and hit TAB nothing happens.

Also, doing M-x yas-expand after writing if nothing happens. but a message do come, You can run the command "yas-expand" with TAB, but this too happens sometimes.

I remember that at one point of time doing any thing above was showing the if-expanded yesterday.

The good thing is that my jslint is working. I installed it using npm and added the flymake. :) Also i can use comint nodejs repl :)

Any help would be appreciated.

My complete emacs file is here my emacs


Solution

  • Use yas-global-mode after you set yas-snippet-dirs. Otherwise you have to use yas-reload-all, which is what happened.

    Follow the example here