One Directory in Emacs ECB ecb-source-path? - emacs

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.

Related

Disable emacs from converting tabs to spaces

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.

Use flycheck for indent settings in js2-mode

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)

"malformed function" warning and require "Cannot open load file:" error

I am getting these two issues :
In toplevel form:
init.el:28:1:Warning: `(add-path (p) (add-to-list (quote load-path) (concat
emacs-root p)))' is a malformed function
init.el:42:1:Error: Cannot open load file: exec-path-from-shell
during compiling the following elisp:
(eval-when-compile (require 'cl))
;; root of all emacs-related stuff
(eval-when-compile
(defvar emacs-root
(if (or (eq system-type 'cygwin)
(eq system-type 'gnu/linux)
(eq system-type 'linux)
(eq system-type 'darwin))
"~/.emacs.d/" "z:/.emacs.d/")
"Path to where EMACS configuration root is."))
(eval-when-compile
(defvar emacs-root "~/.emacs.d"
"Path to where EMACS configuration root is."))
;; path to where plugins are kept
(defvar plugin-path (concat emacs-root "el-get")
"*Path to el-get plugins.")
;; for portability with < 24.3 EMACS
(unless (fboundp 'cl-labels) (fset 'cl-labels 'labels))
;; add paths to various configuration modes
(cl-labels
((add-path (p)
(add-to-list 'load-path
(concat emacs-root p))))
(add-path ".")
(add-path "settings")
(add-path "site-lisp")
(add-path "erlang")
(add-path "exec-path-from-shell"))
;; set PATH, because we don't load .bashrc
(require 'exec-path-from-shell) ;; <- Error: Cannot open load file: exec-path-from-shell
both these issues are very puzzling to me.
I don't see why this fund considered "malformed"
`(add-path (p) (add-to-list (quote load-path) (concat
emacs-root p)))'
and secondly why "require" is not able to load file.
these issues only happen during compilation, not compiled code works ok
would really appreciate any pointers
Regards, Roman
cl-labels is provided by cl-lib, not by cl. So you need (require 'cl-lib) (which you can also wrap in eval-when-compile).
You need to have "cl-lib" loaded at compile time:
(eval-when-compile (require 'cl-lib))
Also, as Stefan explained in his comments in your other question, you should not define variables in eval-when-compile. Just use defvar.

Going root when writing to file/saving file?

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.

How to choose system type in Emacs

I'm trying to configure my .emacs file to work in a Windows, Linux, and Mac environment--specifically, I need it to choose the correct font and a certain directory for org-mode.
I have tried the following which loads the correct font, but does not load the path specified for org-mode:
;; On Windows
(if (eq system-type 'windows-nt)
(set-default-font "-outline-Consolas-normal-r-normal-normal-14-97-96-96-c-*-iso8859-1")
(setq load-path (cons "~/elisp/org-6.34c/lisp" load-path))
)
;; On Linux
(if (eq system-type 'gnu/linux)
(set-default-font "Inconsolata-11")
(setq load-path (cons "~/elisp/org-current/lisp" load-path))
)
I have tried the following which on my Windows machine returns the error Font Inconsolata-11 is not defined, and on my Linux machine returns the error Font -outline-Consolas-normal-r-normal-normal-14-97-96-96-c-*-iso8859-1 is not defined. For both, the specified org path is not loaded:
;; On Windows
(if (eq system-type 'windows-nt)
(setq load-path (cons "~/elisp/org-6.34c/lisp" load-path))
(set-default-font "-outline-Consolas-normal-r-normal-normal-14-97-96-96-c-*-iso8859-1")
)
;; On Linux
(if (eq system-type 'gnu/linux)
(setq load-path (cons "~/elisp/org-current/lisp" load-path))
(set-default-font "Inconsolata-11")
)
I evaluated the system-type variable in both environments, and they both evaluate correctly.
Can anyone see what's wrong--also, I'm not very versed in emacs-lisp, can you see what incorrect assumptions I'm making?
Thank you,
Zachary
note that if in lisp is if-then-else. so, in your first case you are doing if windows, set the font, ELSE set the loadpath for windows! then independantly, you are doin if linux setthe font, else set the loadpath for linux!
try
(if (eq system-type 'windows-nt)
(progn
(setq load-path (cons "~/elisp/org-6.34c/lisp" load-path))
(set-default-font "-outline-Consolas-normal-r-normal-normal-14-97-96-96-c-*-iso8859-1")
)
(progn
(setq load-path (cons "~/elisp/org-current/lisp" load-path))
(set-default-font "Inconsolata-11")
)
)
this won't work on mac, or whatever, but if you're only ever using NT or linux, this should work. Otherwise you can stick the other if outside the 2nd progn...