I'm on Emacs 25.2 with js2-mode and flycheck/eslint enabled.
Currenty pressing tab (or newline) will indent as per js2-mode-js-indent-level.
I would like for it to be dynamic to match flycheck/eslint settings
Is there a way to do this ?
Emacs has already facilities to parse json (eslint config in this case).
Parse config and setting indent config as js-indent-level:
(defun js2-mode-use-eslint-indent ()
(let ((json-object-type 'hash-table)
(json-config (shell-command-to-string (format "eslint --print-config %s"
(shell-quote-argument
(buffer-file-name))))))
(ignore-errors
(setq js-indent-level
(aref (gethash "indent" (gethash "rules" (json-read-from-string json-config))) 1)))))
(add-hook 'js2-mode-hook #'js2-mode-use-eslint-indent)
Related
I've recently started using Emacs and an issue I've been facing is that the editor automatically converts all the tabs to spaces. It has started to get a bit annoying now.
Here's my .emacs file for reference:
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
(not (gnutls-available-p))))
(proto (if no-ssl "http" "https")))
(when no-ssl
(warn "\
Your version of Emacs does not support SSL connections,
which is unsafe because it allows man-in-the-middle attacks.
There are two things you can do about this warning:
1. Install an Emacs version that does support SSL and be safe.
2. Remove this warning from your init file so you won't see it again."))
;; Comment/uncomment these two lines to enable/disable MELPA and MELPA Stable as desired
(add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
;;(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
(when (< emacs-major-version 24)
(add-to-list 'package-archives (cons "gnu" (concat proto "://elpa.gnu.org/packages/")))))
(package-initialize)
(custom-set-variables
'(custom-enabled-themes (quote (dracula)))
'(custom-safe-themes)
'(display-line-numbers-type (quote relative))
'(global-display-line-numbers-mode t)
'(menu-bar-mode nil)
'(package-selected-packages
(quote
(company-irony-c-headers company-irony micgoline elpy company-jedi molokai-theme gruvbox-theme autopair auto-complete anaconda-mode nyan-mode dracula-theme company)))
'(scroll-bar-mode nil)
'(tool-bar-mode nil))
(custom-set-faces
)
(setq make-backup-files nil)
(setq auto-save-default nil)
(setq inhibit-startup-message t) ;; hide the startup message
(elpy-enable)
(pyenv-mode)
(setq python-shell-interpreter "ipython"
python-shell-interpreter-args "-i --simple-prompt")
(require 'powerline)
Any suggestions on how to stop emacs from doing this behaviour?
As suggested by Drew, I am posting this as answer:
Did you check variable indent-tabs-mode?
With this you should be able to switch between emacs using spaces or tabs.
As described in the emacs wiki here I would assume, some active mode is setting this to nil in your emacs.
You can find another explanation with links to discussions about if tabs are evil or not here
EDIT:
It seems that strangely the python-mode sets indent-tabs-mode to t.
Maybe this Emacs Wiki entry solves your problem. This snippet from the wiki:
(add-hook 'python-mode-hook
(lambda ()
(setq-default indent-tabs-mode t)
(setq-default tab-width 4)
(setq-default py-indent-tabs-mode t)
(add-to-list 'write-file-functions 'delete-trailing-whitespace)))
looks like it will do the trick.
Hope this helps.
I've recently made a few changes in my init emacs file, and I've noticed with --debug-init that some load files seem to be missing. Here is the output from the debugger
Debugger entered--Lisp error: (file-error "Cannot open load file" "/home/andre/a-m")
load("/home/andre/a-m" nil nil t)
load-file("a-m")
eval-buffer(#<buffer *load*> nil "/usr/local/share/emacs/site-lisp/default.el" nil t) ; Reading at buffer position 269
load-with-code-conversion("/usr/local/share/emacs/site-lisp/default.el" "/usr/local/share/emacs/site-lisp/default.el" t t)
load("default" t t)
#[0 "\205\262
command-line()
normal-top-level()
I've searched for what the a-m file stands for online, and how to correct these errors but without success. Thanks for any help!
Best
Andre
Edit: Here is the init file. I've just changed some info on jabber (email) but other than that the file is unchanged
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; File name: ` ~/.emacs '
;; ---------------------
;; If you need your own personal ~/.emacs
;; please make a copy of this file
;; an placein your changes and/or extension.
;; Copyright (c) 1997-2002 SuSE Gmbh Nuernberg, Germany.
;; Author: Werner Fink, <feedback#suse.de> 1997,98,99,2002
;; Test of Emacs derivates
;; -----------------------
(load-library "url-handlers")
;; Always use PDFLaTeX
(setq TeX-PDF-mode t)
;; TeXcount setup for AUCTeX
(require 'tex)
(add-to-list 'TeX-command-list
(list "TeXcount" "texcount %s.tex" 'TeX-run-command nil t))
(load "auctex.el" nil t t)`
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
;; MELPA
(when (>= emacs-major-version 24)
(require 'package)
(add-to-list
'package-archives
'("melpa" . "http://melpa.org/packages/")
t)
(package-initialize))
;; YASNIPPETS
(add-to-list 'load-path
"~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas-global-mode 1)
(setq yas-snippet-dirs
'("~/.emacs.d/snippets" ;; personal snippets
"~/.emacs.d/plugins/yasnippet/snippets/" ;; the default collection
))
(yas-global-mode 1) ;; or M-x yas-reload-all if you've started YASnippet already.
;; Timestamp controls
(add-hook 'before-save-hook 'time-stamp)
(setq time-stamp-pattern nil)
;; Controls for calendar
(setq european-calendar-style t)
(set-default 'truncate-lines t)
;; Controls for jabber
(require 'jabber)
;;;;;;;;;;;;;;;
;; TRAMP mode
;;;;;;;;;;;;;;;
(setq tramp-default-method "ssh")
;;;;;;;;;;;
;;ORG mode
;;;;;;;;;;;
(define-key global-map "\C-cl" 'org-store-link)
(define-key global-map "\C-ca" 'org-agenda)
(setq org-log-done t)
(setq org-agenda-files (list "~/org/NOTES.org"))
(setq browse-url-browser-function 'browse-url-generic
browse-url-generic-program "firefox")
(setq org-support-shift-select 't)
(setq org-agenda-include-diary t)
(setq org-src-fontify-natively t)
;;;;;;;;;;;
;;G-CODE
;;;;;;;;;;;
(require 'generic-x)
(define-generic-mode gcode-generic-mode
'(("(" . ")"))
(apply 'append
(mapcar #'(lambda (s) (list (upcase s) (downcase s) (capitalize s)))
'("sub" "endsub" "if" "do" "while" "endwhile" "call" "endif"
"sqrt" "return" "mod" "eq" "ne" "gt" "ge" "lt" "le" "and"
"or" "xor" "atan" "abs" "acos" "asin" "cos" "exp"
"fix" "fup" "round" "ln" "sin" "tan" "repeat" "endrepeat")))
'(("\\(#<_?[A-Za-z0-9_]+>\\)" (1 font-lock-type-face))
("\\([NnGgMmFfSsTtOo]\\)" (1 font-lock-function-name-face))
("\\([XxYyZzAaBbCcUuVvWwIiJjKkPpQqRr]\\)" (1 font-lock-string-face))
("\\([\-+]?[0-9]*\\.[0-9]+\\)" (1 font-lock-constant-face))
("\\(#[0-9]+\\)" (1 font-lock-type-face))
("\\([0-9]+\\)" (1 font-lock-constant-face)))
'("\\.gcode\\'")
nil
"Generic mode for g-code files.")
;;;;;;;;;;;
;;OpenSCAD
;;;;;;;;;;;
(add-to-list 'load-path
"/usr/local/share/emacs/site-lisp/")
(autoload 'scad-mode "scad-mode" "Activate OpenSCAD mode." 'interactive)
(add-to-list 'auto-mode-alist '("\\.scad\\'" . scad-mode))
;;;;;;;;
;;ANSYS
;;;;;;;;
(add-to-list 'load-path
"/usr/local/share/emacs/site-lisp/")
(autoload 'ansys-mode "ansys-mode" "Activate Ansys mode." 'interactive)
(add-to-list 'auto-mode-alist '("\\.txt\\'" . ansys-mode))
(add-to-list 'auto-mode-alist '("\\.db\\'" . ansys-mode))
; (add-to-list 'auto-mode-alist '("\\.dat\\'" . ansys-mode))
; (add-to-list 'auto-mode-alist '("\\.inp\\'" . ansys-mode))
;;;;;;;;;;;
;;LS-DYNA
;;;;;;;;;;;
(add-to-list 'load-path
"/usr/local/share/emacs/site-lisp/")
(autoload 'lsdyna-mode "lsdyna" "Enter ls-dyna mode." t)
(setq auto-mode-alist (cons '("\\.k\\'" . lsdyna-mode) auto-mode-alist))
;;;;;;;;;;;
;;ABAQUS
;;;;;;;;;;;
;; ;; setup files ending in “.inp” to open in python-mode
;; (add-to-list 'auto-mode-alist '("\\.inp\\'" . python-mode))
(add-to-list 'load-path
"/usr/local/share/emacs/site-lisp/")
(autoload 'abaqus-mode "abaqus" "Enter abaqus mode." t)
(setq auto-mode-alist (cons '("\\.inp\\'" . abaqus-mode) auto-mode-alist))
;;;;;;;;;;;;;;;;;;;;;;
;; PDF-LATEX+INKSCAPE
;;;;;;;;;;;;;;;;;;;;;;
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(LaTeX-command-style (quote (("" "%(PDF)%(latex) -shell-escape %S%(PDFout)"))))
'(ansys-current-ansys-version "140")
'(jabber-account-list (quote (("myemail.com"))))
'(org-agenda-files (quote ("~/org/NOTES.org")))
'(send-mail-function (quote smtpmail-send-it))
'(smtpmail-smtp-server "smtp.googlemail.com")
'(smtpmail-smtp-service 587))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
;; ;;;;;;;;;;;
;; ;;GNUPLOT
;; ;;;;;;;;;;;
;; (add-to-list 'Info-default-directory-list "/usr/info")
;; ;; load the file
;; (require 'gnuplot)
;; ;; specify the gnuplot executable (if other than /usr/bin/gnuplot)
;; (setq gnuplot-program "/usr/bin/gnuplot")
;; ;; automatically open files ending with .gp or .gnuplot in gnuplot mode
;; (setq auto-mode-alist
;; (append '(("\\.\\(gp\\|gnuplot\\|p\\)$" . gnuplot-mode)) auto-mode-alist))
; GIT controls
(add-to-list 'load-path ".../git/contrib/emacs")
(require 'git)
(require 'git-blame)
(require 'vc-git)
;; OCTAVE
(autoload 'run-octave "octave-inf" nil t)
(put 'upcase-region 'disabled nil)
;; PHP
(autoload 'php-mode "php-mode" "Major mode for editing php code." t)
(add-to-list 'auto-mode-alist '("\\.php$" . php-mode))
(add-to-list 'auto-mode-alist '("\\.inc$" . php-mode))
;; Command for automatic alignment of comments
(defun align-comment (beg end)
(interactive "r")
(align-regexp beg end (concat "\\(\\s-*\\)" comment-start))
)
(global-set-key "\C-c\C-a" 'align-comment )
;; Flyspell mode binding to F6 key
(global-set-key [f6] 'flyspell-mode) ; F6
;; Command for automatic auto-fill
(add-hook 'text-mode-hook 'turn-on-auto-fill)
Thanks for your help. I didn't read the new ansys-mode https://github.com/dieter-wilhelm/ansys-mode documentation properly. Here is the issue:
Please have a look at the accompanying default.el’ customisation example. It can be used as a configuration file (after moving it e. g. to/usr/share/emacs/site-lisp’ or c:\EMACS_INSTALLDIR\site-lisp’, hint: The directory site-lisp/ in the Emacs installation tree is in its default load-path). Yetdefault.el’ is loaded AFTER your personal Emacs configuration file (if there is any) ~/.emacs’ (or~/.emacs.d/init.el’)! If you intend to change the following settings with Emacs’ customisation system or changing them directly in your personal configuration file, you must either set the variable inhibit-default-init’ tot’ “(setq inhibit-default-init t)” in your personal configuration file or remove `default.el’ otherwise your settings might be overwritten!
So I just added (setq inhibit-default-init t) to the init file.
Thanks again!
Note that with Git 2.18 (Q2 2018), the scripts in contrib/emacs/ have outlived their usefulness and have been replaced with a stub that errors out and tells the user there are replacements.
So the init error message might not apply anymore in 2018.
See commit 6d5ed48 (11 Apr 2018) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit 7d7d051, 08 May 2018)
git{,-blame}.el: remove old bitrotting Emacs code
The git-blame.el mode has been superseded by Emacs's own
vc-annotate
(invoked by C-x v g).
Users of the git.el mode are now much better off using either Magit or the Git backend for Emacs's own VC mode.
These modes were added over 10 years ago when Emacs's own Git support
was much less mature, and there weren't other mature modes in the wild
or shipped with Emacs itself.
These days these modes have few if any users, and users of git aren't
well served by us shipping these (some OS's install them alongside git
by default, which is confusing and leads users astray).
[...] rather than receive a cryptic load error when they upgrade,
existing users will get an error directing them to the README file, or
to just stop requiring these modes
See "git/contrib/emacs/README" for more.
My emacs takes a few seconds to start up every time, which is slower than I'd expect. During that time, it says "contacting host: " marmalade-repo and melpa.
Is my current config a reasonable one? Is there a way I can speed it up, while still being able to install packages when I need them?
;; init.el --- Emacs configuration
;; INSTALL PACKAGES
;; --------------------------------------
(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)
(add-to-list 'package-archives '("marmalade" . "https://marmalade-repo.org/packages/"))
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))
(defvar myPackages
'(better-defaults
paredit
idle-highlight-mode
ido-ubiquitous
find-file-in-project
smex
scpaste
ein
elpy
flycheck
material-theme
py-autopep8))
(package-refresh-contents)
(mapc #'(lambda (package)
(unless (package-installed-p package)
(package-install package)))
myPackages)
;; BASIC CUSTOMIZATION
;; --------------------------------------
(setq inhibit-startup-message t) ;; hide the startup message
(load-theme 'material t) ;; load material theme
(global-linum-mode t) ;; enable line numbers globally
;; PYTHON CONFIGURATION
;; --------------------------------------
(elpy-enable)
(elpy-use-ipython)
;; use flycheck not flymake with elpy
(when (require 'flycheck nil t)
(setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
(add-hook 'elpy-mode-hook 'flycheck-mode))
;; enable autopep8 formatting on save
(require 'py-autopep8)
(add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)
;; enable electric pair minor mode
(defun electric-pair ()
"If at end of line, insert character pair without surrounding spaces.
Otherwise, just insert the typed character."
(interactive)
(if (eolp) (let (parens-require-spaces) (insert-pair)) (self-insert-command 1)))
(add-hook 'python-mode-hook
(lambda ()
(define-key python-mode-map "\"" 'electric-pair)
(define-key python-mode-map "\'" 'electric-pair)
(define-key python-mode-map "(" 'electric-pair)
(define-key python-mode-map "[" 'electric-pair)
(define-key python-mode-map "{" 'electric-pair)))
;; Send line from code buffer
;; http://stackoverflow.com/questions/27777133/change-the-emacs-send-code-to-interpreter-c-c-c-r-command-in-ipython-mode/30774439#30774439
(defun my-python-line ()
(interactive)
(save-excursion
(setq the_script_buffer (format (buffer-name)))
(end-of-line)
(kill-region (point) (progn (back-to-indentation) (point)))
(if (get-buffer "*Python*")
(message "")
(run-python "ipython" nil nil))
;; (setq the_py_buffer (format "*Python[%s]*" (buffer-file-name)))
(setq the_py_buffer "*Python*")
(switch-to-buffer-other-window the_py_buffer)
(goto-char (buffer-end 1))
(yank)
(comint-send-input)
(switch-to-buffer-other-window the_script_buffer)
(yank))
(end-of-line)
(next-line)
)
(add-hook 'python-mode-hook
(lambda ()
(define-key python-mode-map "\C-cn" 'my-python-line)))
Recently, loading packages from MELPA has been really slow, I'm not so sure why. What you can do to get around this (or at least make sure it only happens once) is to run Emacs in daemon mode, and then connect to it whenever you want to edit a file. It means that the cost of loading is only incurred once. It's as simple as running emacs --daemon at startup. Then you connect via emacsclient.
Is it possible to open a file(in root location) as non-root user in Emacs, edit it and then when its time to save provide the password so the Emacs can get write to the file? Better still provide different buffers with different user privileges?
I know of Tramp but couldn't get my head around it.
Here's how I do it:
(require 'tramp)
(defun sudired ()
(interactive)
(dired "/sudo::/"))
You'll get a dired buffer where you have root privileges.
Any subsequent directory or file that you open from here will be with root.
Any other dired buffers will not be affected.
Update: I now use sudo-edit (available on Melpa or at https://github.com/nflath/sudo-edit), which has the header warning and is more robust than this function.
This is what I use. You can open a file (even one that doesn't exist yet) or directory as a normal user, and run this function to get root privileges.
(defun find-alternative-file-with-sudo ()
(interactive)
(let ((bname (expand-file-name (or buffer-file-name
default-directory)))
(pt (point)))
(setq bname (or (file-remote-p bname 'localname)
(concat "/sudo::" bname)))
(cl-flet ((server-buffer-done
(buffer &optional for-killing)
nil))
(find-alternate-file bname))
(goto-char pt)))
I also have this, which makes a big red banner across the top of the buffer telling me it's opened as root.
(defface find-file-root-header-face
'((t (:foreground "white" :background "red3")))
"*Face use to display header-lines for files opened as root.")
(defun find-file-root-header-warning ()
"*Display a warning in header line of the current buffer.
This function is suitable to add to `find-file-hook'."
(when (string-equal
(file-remote-p (or buffer-file-name default-directory) 'user)
"root")
(let* ((warning "WARNING: EDITING FILE AS ROOT!")
(space (+ 6 (- (window-width) (length warning))))
(bracket (make-string (/ space 2) ?-))
(warning (concat bracket warning bracket)))
(setq header-line-format
(propertize warning 'face 'find-file-root-header-face)))))
(add-hook 'find-file-hook 'find-file-root-header-warning)
(add-hook 'dired-mode-hook 'find-file-root-header-warning)
You don't need any special functions for this, it's built-in to Emacs (at least it is for version 24).
To open a file as root:
C-x C-f to open the find-file dialog in the minibuffer.
Then prepend /su::/ to the file path:
/su::/path/to/root/file
You'll be prompted for the root password. After that, you can open the file as if you are root. The rest of your buffers will be unaffected. However, if you open another file from the same buffer, you'll automatically be opening it as root.
I wanted to have a way to open root files too, so I came up with this function that replaced build in find-file, now I have this in my .emacs:
(defun test (&rest args)
(with-temp-buffer
(eq (apply 'call-process "test" nil (current-buffer) nil args) 0)))
(defun have-permission (filename)
;; only bash expand ~ with home directory
(let ((expanded (replace-regexp-in-string "~"
(concat "/home/" (user-real-login-name))
filename)))
(if (not (file-exists-p expanded))
(let ((directory (file-name-directory expanded)))
(and (test "-r" directory) (test "-x" directory) (test "-w" directory)))
(and (test "-r" expanded) (test "-w" expanded)))))
(defun find-every-file (filename &optional wildcards)
"Open file use sudo:: if user have no permissions to open the file"
(interactive
(find-file-read-args "Find All Files: "
(confirm-nonexistent-file-or-buffer)))
(find-file (if (have-permission filename)
filename
;; you can replace that with /su:: if you don't have sudo access
(concat "/sudo::" (file-truename filename)))))
(global-set-key (kbd "C-x C-f") 'find-every-file)
It also work if you try to open non existing file or non existing file in directory you don't have write permissions.
you can combine it with #jpkotta warning popup.
I'm setting up emacs for Ruby on Rails development, and would like the ECB window to show only the directory for the project I'm working on. Is that possible?
Let's assume that I start emacs after I cd to the project's directory. I've added the following to my .emacs:
(defvar start-dir (getenv "PWD"))
(custom-set-variables
'(ecb-layout-name "left14")
'(ecb-layout-window-sizes (quote (("left14" (0.2564102564102564 . 0.6949152542372882) (0.2564102564102564 . 0.23728813559322035)))))
'(ecb-options-version "2.32")
'(ecb-primary-secondary-mouse-buttons (quote mouse-1--C-mouse-1))
'(ecb-source-path (list start-dir))
'(ecb-tip-of-the-day nil)
'(ecb-tree-buffer-style (quote ascii-guides))
'(inhibit-startup-screen t))
Notice I've created a list containing only the start-dir. However ECB shows both the start-dir and the root (/) dir.
I figured out a solution to only show the directory from which I run emacs in the ECB window:
(defvar start-dir (getenv "PWD"))
(defvar start-dir-name (car (last (split-string start-dir "/"))))
(custom-set-variables
'(ecb-layout-name "left14")
'(ecb-layout-window-sizes (quote (("left14" (0.2564102564102564 . 0.6949152542372882) (0.2564102564102564 . 0.23728813559322035)))))
'(ecb-options-version "2.32")
'(ecb-primary-secondary-mouse-buttons (quote mouse-1--C-mouse-1))
'(ecb-source-path (list (list start-dir start-dir-name)))
'(ecb-tip-of-the-day nil)
'(ecb-tree-buffer-style (quote ascii-guides))
'(inhibit-startup-screen t))
Try (setq ecb-source-path (quote "/path/to/project/")). Your present setup makes ecb-source-path nil, which you can see if you do describe-variable.