Why the second statement does not work? - emacs

Can you help me in debugging this code snippet?
It should evaluate the emacs-major-version and sets the color theme accordingly. I've tested the first statement, the second does not return any error but the theme does not be applied. Any clue?
(if (<= emacs-major-version 23)
((require 'color-theme)
(eval-after-load "color-theme"
'(progn
(color-theme-initialize)
(color-theme-classic))))
(progn
(setq custom-enabled-themes (quote (tango-dark)))
(setq custom-safe-themes (quote
("16248150e4336572ff4aa21321015d37c3744a9eb243fbd1e934b594ff9cf394"
"9370aeac615012366188359cb05011aea721c73e1cb194798bc18576025cabeb"
default))))
)

Your conditional works after dealing with the progn issue that lawlist brought up, but you forgot to load the theme. Add (load-theme 'tango-dark t) at the end of the second progn as such:
(if (<= emacs-major-version 23)
(progn
(require 'color-theme)
(eval-after-load "color-theme"
'(progn
(color-theme-initialize)
(color-theme-classic))))
(progn
(setq custom-enabled-themes (quote (tango-dark)))
(setq custom-safe-themes (quote
("16248150e4336572ff4aa21321015d37c3744a9eb243fbd1e934b594ff9cf394"
"9370aeac615012366188359cb05011aea721c73e1cb194798bc18576025cabeb"
default)))
(load-theme 'tango-dark t))) ; add me here
Edited re: Phils's comment: sorry, I missed the require in the initial code. The first clause is now wrapped in a progn as needed.

Related

Emacs: How to enable a mode globally?

I just have a mode from an addon which I want to enable globally. In order to turn on it by hand I need to enter M-x highlight-indentation-mode. So, below is a list of what I have tried yet: (highlight-indentation-mode t), (highlight-indentation-mode 1), (setq highlight-indentation-mode t). Nothing worked. Next I figure out that may be I need to enable a mode globally, and I started google about it. What I have tried next:
(define-globalized-minor-mode global-highlight-indentation-mode highlight-indentation-mode
(lambda () (setq highlight-indentation-mode t)))
No, this surely aren't the droids I am looking for, it turns on the variable, but a mode still doesn't work.
(define-globalized-minor-mode global-highlight-indentation-mode highlight-indentation-mode
(lambda () highlight-indentation-mode t))
(define-globalized-minor-mode global-highlight-indentation-mode highlight-indentation-mode
(highlight-indentation-mode t))
These two just broke my Emacs: when I tried open file with this two commands in config, Emacs wrote an error, and refused to open a files.
UPD: Based on comments I also tried
(defun enable-highlight-indentation-mode ()
(interactive)
(highlight-indentation-mode t))
(define-globalized-minor-mode global-highlight-indentation-mode highlight-indentation-mode
enable-highlight-indentation-mode)
(global-highlight-indentation-mode t)
And the same without (interactive). When I am try to open a file with this, Emacs refuse to open, and write an error:
File mode specification error: (void-function nil)
c-font-lock-fontify-region: Symbol's function definition is void: nil
;;; highlight-indentation.el --- Minor modes for highlighting indentation
;; Author: Anton Johansson <anton.johansson#gmail.com> - http://antonj.se
;; Created: Dec 15 23:42:04 2010
;; Version: 0.6.0
;; URL: https://github.com/antonj/Highlight-Indentation-for-Emacs
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE. See the GNU General Public License for more details.
;;
;;; Commentary:
;; Customize `highlight-indentation-face', and
;; `highlight-indentation-current-column-face' to suit your theme.
;;; Code:
(defgroup highlight-indentation nil
"Highlight Indentation"
:prefix "highlight-indentation-"
:group 'basic-faces)
(defface highlight-indentation-face
;; Fringe has non intrusive color in most color-themes
'((t :inherit fringe))
"Basic face for highlighting indentation guides."
:group 'highlight-indentation)
(defcustom highlight-indentation-offset 4
"Default indentation offset, used if no other can be found from
major mode. This value is always used by
`highlight-indentation-mode' if set buffer local. Set buffer
local with `highlight-indentation-set-offset'"
:group 'highlight-indentation)
(defvar highlight-indentation-current-regex nil)
;;;###autoload
(define-minor-mode highlight-indentation-mode
"Highlight indentation minor mode highlights indentation based
on spaces"
:lighter " ||"
(when highlight-indentation-current-regex ;; OFF
(font-lock-remove-keywords nil `((,highlight-indentation-current-regex
(1 'highlight-indentation-face)))))
(set (make-local-variable 'highlight-indentation-current-regex) nil)
(when highlight-indentation-mode ;; ON
(when (not (local-variable-p 'highlight-indentation-offset))
(set (make-local-variable 'highlight-indentation-offset)
;; Set indentation offset from highlight-indentation-offset if set, otherwise
;; according to major mode
(cond ((and (eq major-mode 'python-mode) (boundp 'python-indent))
python-indent)
((and (eq major-mode 'python-mode) (boundp 'py-indent-offset))
py-indent-offset)
((and (eq major-mode 'python-mode) (boundp 'python-indent-offset))
python-indent-offset)
((eq major-mode 'ruby-mode)
ruby-indent-level)
((and (eq major-mode 'scala-mode) (boundp 'scala-indent:step))
scala-indent:step)
((and (eq major-mode 'scala-mode) (boundp 'scala-mode-indent:step))
scala-mode-indent:step)
((or (eq major-mode 'scss-mode) (eq major-mode 'css-mode))
css-indent-offset)
((eq major-mode 'nxml-mode)
nxml-child-indent)
((eq major-mode 'coffee-mode)
coffee-tab-width)
((eq major-mode 'js-mode)
js-indent-level)
((eq major-mode 'js2-mode)
js2-basic-offset)
((local-variable-p 'c-basic-offset)
c-basic-offset)
(t
(default-value 'highlight-indentation-offset)))))
(set (make-local-variable 'highlight-indentation-current-regex)
(format "\\( \\) \\{%s\\}" (- highlight-indentation-offset 1)))
(font-lock-add-keywords nil `((,highlight-indentation-current-regex
(1 'highlight-indentation-face)))))
(font-lock-fontify-buffer))
;;;###autoload
(defun highlight-indentation-set-offset (offset)
"Set indentation offset localy in buffer, will prevent
highlight-indentation from trying to guess indentation offset
from major mode"
(interactive
(if (and current-prefix-arg (not (consp current-prefix-arg)))
(list (prefix-numeric-value current-prefix-arg))
(list (read-number "Indentation offset: "))))
(set (make-local-variable 'highlight-indentation-offset) offset)
(when highlight-indentation-mode
(highlight-indentation-mode)))
;;;
;;; Copyright (C) Kresten Krab Thorup
;;; Available under Apache License, Version 2.
;;;
;;; This minor mode will highlight the indentation of the current line
;;; as a vertical bar (grey background color) aligned with the column of the
;;; first character of the current line.
;;;
(defface highlight-indentation-current-column-face
;; Fringe has non intrusive color in most color-themes
'((t :inherit fringe))
"Basic face for highlighting indentation guides."
:group 'highlight-indentation)
;; used to hold the last regex we installed
(defvar highlight-indentation-current-column-regex nil)
;;;###autoload
(define-minor-mode
highlight-indentation-current-column-mode
"Hilight Indentation minor mode displays
a vertical bar corresponding to the indentation of the current line"
:lighter " |"
(when highlight-indentation-current-column-regex
(font-lock-remove-keywords nil highlight-indentation-current-column-regex))
(set (make-local-variable 'highlight-indentation-current-column-regex) nil)
(cond (highlight-indentation-current-column-mode
(add-hook 'post-command-hook 'highlight-indentation-current-column-post-command-hook nil t))
(t
(remove-hook 'post-command-hook 'highlight-indentation-current-column-post-command-hook t)
(font-lock-fontify-buffer))))
(defun highlight-indentation-current-column-post-command-hook ()
"This hook runs after every keystroke"
(when highlight-indentation-current-column-regex
(font-lock-remove-keywords nil highlight-indentation-current-column-regex))
(let ((indent (save-excursion (back-to-indentation) (current-column))))
(when (and highlight-indentation-current-column-mode
(> indent 1))
(let* ((re (format "^ \\{%d\\}\\( \\)" indent))
(arg `((,re (1 'highlight-indentation-current-column-face prepend)))))
(set (make-local-variable 'highlight-indentation-current-column-regex) arg)
(font-lock-add-keywords nil arg))))
(font-lock-fontify-buffer))
(defun turn-on-highlight-indentation-mode ()
(interactive)
(highlight-indentation-mode 1))
(define-globalized-minor-mode global-highlight-indentation-mode
highlight-indentation-mode turn-on-highlight-indentation-mode)
(global-highlight-indentation-mode 1)
(provide 'highlight-indentation)

Can't control width of sr-speedbar

I'm using emacs with sr-speedbar, but can't control its width. When I resize the emacs window, the sr-speedbar always expands with it. I have tried this both with xemacs and emacs.
My .emacs file below:
(require 'package)
(package-initialize)
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")))
(setq c-default-style "linux"
c-basic-offset 4)
(iswitchb-mode 1)
(setq inhibit-splash-screen t)
(load-theme 'zenburn t)
(require 'ecb)
(setq stack-trace-on-error t)
(desktop-save-mode 1)
;(defun toggle-fullscreen ()
; (interactive)
; (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
; '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
; (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
; '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
;)
;(toggle-fullscreen)
(require 'sr-speedbar)
(setq
sr-speedbar-right-side nil
sr-speedbar-width-x 10
sr-speedbar-width-console 10
sr-speedbar-max-width 10
sr-speedbar-delete-windows t)
(sr-speedbar-open)
(put 'dired-find-alternate-file 'disabled nil)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
;; Single char cursor movement. (assuming you are on qwerty)
(global-set-key (kbd "M-j") 'backward-char)
(global-set-key (kbd "M-l") 'forward-char)
(global-set-key (kbd "M-i") 'previous-line)
(global-set-key (kbd "M-k") 'next-line)
(global-set-key (kbd "M-SPC") 'set-mark-command)
(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.
'(custom-safe-themes (quote ("d63e19a84fef5fa0341fa68814200749408ad4a321b6d9f30efc117aeaf68a2e" default)))
'(ecb-options-version "2.40"))
(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.
)
It consumes variables at load time, not when open.
The sr-speedbar-width-* should be set before the script loaded.
This does not work, because the variables sr-speedbar-width-x and sr-speedbar-width-console do not exist any more. They have been removed from the code on 03 Aug 2014. See here: https://www.emacswiki.org/emacs/download/sr-speedbar.el
The information that you probably found on https://www.emacswiki.org/emacs/SrSpeedbar is outdated.
There is only one variable sr-speedbar-width
The variable is "consumed" when sr-speedbar-open is called. The user can define the variable before or after loading the library. This is due to the behaviour of the Emacs Lisp special form defvar (line 364), which does not overwrite the variable. For more information on this see here: https://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Variables.html

Check if major mode is equal one of several emacs

I found a snippet to close all dired buffers, which I want to use in sunrise commander:
(defun er/kill-all-dired-buffers()
"Kill all dired buffers."
(interactive)
(save-excursion
(let((count 0))
(dolist(buffer (buffer-list))
(set-buffer buffer)
(when (equal major-mode 'sr-mode)
(or (equal major-mode 'dired-mode))
(setq count (1+ count))
(kill-buffer buffer)))
(message "Killed %i dired buffer(s)." count ))))
(setq sr-quit-hook 'er/kill-all-dired-buffers)
Issue being, I can't make it work both for sr-mode and dired-mode together.
How do I check "if major mode is sr-mode OR dired-mode"?
EDIT:
Just a syntax error.
Should be
(when (or (equal major-mode 'dired-mode) (equal major-mode 'sr-mode))
Have to admit it's not too intuitive.
The canonical way would be (when (derived-mode-p 'sr-mode 'dired-mode) ...).
I tried some things and found this to work on my emacs-ielm - perhaps it might help also:
(if (member major-mode '(fsharp-mode c-mode java-mode inferior-emacs-lisp-mode))
(message "yeah right"))
Maybe the correct check function is:
(derived-mode-p &rest MODES)
See 'subr.el'.

Setting default-directory in .emacs

I don't really know what to say.
I've been working on customizing my emacs, and I noticed that it isn't actually loading my .emacs on startup. According the(http://www.gnu.org/software/emacs/manual/html_node/emacs/Find-Init.html#Find-Init), emacs looks in the HOME directory (~/) for the initialization file first...
When I start emacs, my .emacs file seems to be read correctly - when I visit a .notes file, for example, the hooks are evaluated and such and such. What surprises me: the default-directory isn't set - a command in the same load file. I can either evaluate it manually or simply execute (load "~/.emacs") and it will work fine.
I guess the question can be summarized: If the load command works as expected when executed manually, why isn't it working on startup automatically?
Full (except for commented-out functions) .emacs file:
; http://stackoverflow.com/a/13946304/1443496
(defvar auto-minor-mode-alist ()
"Alist of filename patterns vs correpsonding minor mode functions,
see `auto-mode-alist'. All elements of this alist are checked,
meaning you can enable multiple minor modes for the same regexp.")
(defun enable-minor-mode-based-on-extension ()
"check file name against auto-minor-mode-alist to enable minor modes
the checking happens for all pairs in auto-minor-mode-alist"
(when buffer-file-name
(let ((name buffer-file-name)
(remote-id (file-remote-p buffer-file-name))
(alist auto-minor-mode-alist))
;; Remove backup-suffixes from file name.
(setq name (file-name-sans-versions name))
;; Remove remote file name identification.
(when (and (stringp remote-id)
(string-match-p (regexp-quote remote-id) name))
(setq name (substring name (match-end 0))))
(while (and alist (caar alist) (cdar alist))
(if (string-match (caar alist) name)
(funcall (cdar alist) 1))
(setq alist (cdr alist))))))
(add-hook 'find-file-hook 'enable-minor-mode-based-on-extension)
;; the wrapping up of the two loads make sure
;; auctex is loaded only when editing tex files.
(eval-after-load "tex-mode"
'(progn
(load "auctex.el" nil nil t)
(load "preview-latex.el" nil nil t)
)
)
; Sets my default directory to my dropbox (platform-dependent)
(setq default-directory
(concat
(if (eq system-type 'windows-nt)
"t:" "~")
"/Dropbox/Public/School/TeX/"))
; Set up the .notes extension
(setq auto-mode-alist
(cons '("\\.notes\\'" . text-mode)
auto-mode-alist))
(setq auto-minor-mode-alist
(cons '("\\.notes\\'" . auto-fill-mode)
auto-minor-mode-alist))
;; AUCTeX replaces latex-mode-hook with LaTeX-mode-hook
(add-hook 'LaTeX-mode-hook
(lambda ()
(setq TeX-auto-save t)
(setq TeX-parse-self t)
;; (setq-default TeX-master nil)
(reftex-mode t)
(TeX-fold-mode t)))
Default-directory is buffer local. Your .emacs is loading just fine; the value of default-directory is (re)set for each new buffer you open. When you re-load your .emacs, it changes the value of default-directory for the buffer you are in only.

How to map "jj" to Esc in emacs Evil mode

Recently I tried Emacs and found Evil helpful to keep my vim custom. I'm used to typing "jj" to return normal mode from insert mode like many Vimers do but don't know how to make it in Evil mode.
I map it like this but seems not correct:
(define-key evil-insert-state-map (kbd "jj") 'evil-normal-state)
This works for me. It requires the KeyChord library:
;;Exit insert mode by pressing j and then j quickly
(setq key-chord-two-keys-delay 0.5)
(key-chord-define evil-insert-state-map "jj" 'evil-normal-state)
(key-chord-mode 1)
It is inspired by #phils answer above and based on Simon's Coding Blog: Emacs and Unity Every Day.
I don't know whether it works with Evil, but for Emacs in general the KeyChord library is designed for this sort of thing.
Try it and see?
(key-chord-define evil-insert-state-map "jj" 'evil-normal-state)
If you're using Spacemacs then I just found that this setting (added to the beginning of user-init) works very well,
(setq-default evil-escape-key-sequence "jj")
See this blog post: http://zuttobenkyou.wordpress.com/2011/02/15/some-thoughts-on-emacs-and-vim/ and search for "cofi". I use the "kj" version myself and it works just like Vim.
EDIT: Here is the actual code snippet from the blog post:
(define-key evil-insert-state-map "k" #'cofi/maybe-exit)
(evil-define-command cofi/maybe-exit ()
:repeat change
(interactive)
(let ((modified (buffer-modified-p)))
(insert "k")
(let ((evt (read-event (format "Insert %c to exit insert state" ?j)
nil 0.5)))
(cond
((null evt) (message ""))
((and (integerp evt) (char-equal evt ?j))
(delete-char -1)
(set-buffer-modified-p modified)
(push 'escape unread-command-events))
(t (setq unread-command-events (append unread-command-events
(list evt))))))))
For my windows install, adding as part of use-package evil configuration worked for me in init.el:
(use-package evil
:ensure t
:config
(evil-mode 1)
(define-key evil-insert-state-map "jj" 'evil-normal-state)
)
For Ubuntu, I followed E. Sambo's answer.
It's a bit more complicated - you have to watch for the previous character. This should do the trick. (the gist is for "jk", you can easily modify it for "jj" though you will note that "jk" is more efficient/faster).
This is my own solution i've been using for some time, although i use `jf' actually.
(defun xwl-jj-as-esc ()
(interactive)
(if (memq evil-state '(insert replace))
(let ((changed? (buffer-modified-p)))
(insert "j")
(let* ((tm (current-time))
(ch (read-key)))
(if (and (eq ch ?j)
(< (time-to-seconds (time-since tm)) 0.5))
(save-excursion
(delete-char -1)
(evil-force-normal-state)
(set-buffer-modified-p changed?))
(insert ch))))
(call-interactively 'evil-next-line)))
(define-key evil-insert-state-map "j" 'xwl-jj-as-esc)
(define-key evil-replace-state-map "j" 'xwl-jj-as-esc)
Initially I naively shared the answer below which not only it doesn't work (since it "disables" j and k) but also many emacs folks would laugh!
I found that evil-mode provides evil-define-key according to https://evil.readthedocs.io/en/latest/keymaps.html#evil-define-key.
So I tried below and it works for me.
;; you should be able to do "jj" instead but I prefer "jk"
(evil-define-key 'insert 'global (kbd "jk") 'evil-normal-state)
Now I found a real answer that is satisfying to me and working (using https://github.com/noctuid/general.el)
;; Enable installation of packages from MELPA.
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
;; Download Evil
(unless (package-installed-p 'evil)
(package-install 'evil))
;; Enable Evil
(require 'evil)
(evil-mode 1)
;; ------------------------------------------------------
;; you might have all the above already
;; but I still included for the completeness of the code
;; ------------------------------------------------------
;; Download general.el
(unless (package-installed-p 'general)
(package-install 'general))
;; Enable general.el
(require 'general)
(general-evil-setup)
;; now usable `jk` is possible!
(general-imap "j"
(general-key-dispatch 'self-insert-command
:timeout 0.25
"k" 'evil-normal-state))
since I literally just discovered general.el, this code might not be optimal