Emacs ignoring setq for line numbers - emacs

I am trying to set up my .emacs to use relative line numbering, and so I have included:
(setq display-line-numbers 'relative)
to my .emacs, however this is being ignored on load. Line numbers appear, however they are simply absolute line numbers, as opposed to relative.
Interestingly, when I run M-x load-file .emacs, it actually evaluates this line (as well as the rest of the file) and the line numbers become relative, however this doesn't happen the first time.
Below is my entire .emacs, and the line in question is on line 95/96:
(package-initialize)
(load "~/.emacs.rc/rc.el")
(load "~/.emacs.rc/misc-rc.el")
(defun rc/get-default-font ()
(cond
((eq system-type 'windows-nt) "Consolas-13")
((eq system-type 'gnu/linux) "Iosevka-24")))
(add-to-list 'default-frame-alist `(font . ,(rc/get-default-font)))
(tool-bar-mode 0)
(menu-bar-mode 0)
(scroll-bar-mode 0)
(column-number-mode 1)
(show-paren-mode 1)
(rc/require-theme 'gruber-darker)
(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.
'(package-selected-packages
'(svelte-mode tramp-mode elixir-mode evil move-text nasm-mode company yasnippet multiple-cursors magit haskell-mode ido-completing-read+ smex gruber-darker-theme dash-functional dash)))
(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.
)
(eval-after-load 'zenburn
(set-face-attribute 'line-number nil :inherit 'default))
(rc/require 'smex 'ido-completing-read+)
(require 'ido-completing-read+)
(ido-mode 1)
(ido-everywhere 1)
(ido-ubiquitous-mode 1)
(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "C-c C-c M-x") 'execute-extended-command)
;;; C mode
(setq-default c-basic-offset 4
c-default-style '((java-mode . "java")
(awk-mode . "awk")
(other . "bsd")))
(add-hook 'c-mode-hook (lambda ()
(interactive)
(c-toggle-comment-style -1)))
;;; Elisp
(add-hook 'emacs-lisp-mode-hook
'(lambda ()
(local-set-key (kbd "C-c C-j")
(quote eval-print-last-sexp))))
(add-to-list 'auto-mode-alist '("Cask" . emacs-lisp-mode))
;;; Haskell mode
(rc/require 'haskell-mode)
(setq haskell-process-type 'cabal-new-repl)
(setq haskell-process-log t)
(add-hook 'haskell-mode-hook 'haskell-ident-mode)
(add-hook 'haskell-mode-hook 'interactive-haskell-mode)
(add-hook 'haskell-mode-hook 'haskell-doc-mode)
(add-hook 'haskell-mode-hook 'hindent-mode)
;;; Whitespace mode
(defun rc/set-up-whitespace-handling ()
(interactive)
(whitespace-mode 1)
(add-to-list 'write-file-fucntions 'delete-trailing-whitespace))
(add-hook 'c++-mode-hook 'rc/set-up-whitespace-handling)
(add-hook 'java-mode-hook 'rc/set-up-whitespace-handling)
(add-hook 'emacs-lisp-mode 'rc/set-up-whitespace-handling)
(add-hook 'rust-mode-hook 'rc/set-up-whitespace-handling)
(add-hook 'haskell-mode-hook 'rc/set-up-whitespace-handling)
(add-hook 'python-mode-hook 'rc/set-up-whitespace-handling)
(add-hook 'erlang-mode-hook 'rc/set-up-whitespace-handling)
(add-hook 'asm-mode-hook 'rc/set-up-whitespace-handling)
(add-hook 'nasm-mode-hook 'rc/set-up-whitespace-handling)
(global-display-line-numbers-mode)
(setq display-line-numbers 'relative)
;;; Magit
(rc/require 'cl-lib)
(rc/require 'magit)
(setq magit-auto-revert-mode nil)
(global-set-key (kbd "C-c m s") 'magit-status)
(global-set-key (kbd "C-c m l") 'magit-log)
;;; Multiple Cursors
(rc/require 'multiple-cursors)
(global-set-key (kbd "C-S-c C-S-c") 'mc/edit-lines)
(global-set-key (kbd "C->") 'mc/mark-next-like-this)
(global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
(global-set-key (kbd "C-c C-<") 'mc/mark-all-like-this)
(global-set-key (kbd "C-\"") 'mc/skip-to-next-like-this)
(global-set-key (kbd "C-:") 'mc/skip-to-previous-like-this)
;;; Dired
(require 'dired-x)
(setq dired-omit-files
(concat dired-omit-files "\\|^\\..+$"))
(setq-default dired-dwim-target t)
(setq dired-listing-switches "-alh")
;;; Yasnippet
(rc/require 'yasnippet)
(require 'yasnippet)
(setq yas/triggers-in-field nil)
(setq yas-snipped-dirs '("~/.emacs.snippets/"))
(yas-global-mode 1)
;;; Word-Wrap
(defun rc/enable-word-wrap ()
(interactive)
(toggle-word-wrap 1))
(add-hook 'markdown-mode-hook 'rc/enable-word-wrap)
;;; Company Mode
(rc/require 'company)
(require 'company)
(global-company-mode)
;;; NASM mode
(rc/require 'nasm-mode)
(add-to-list 'auto-mode-alist '("\\.asm\\'" . nasm-mode))
;;; TeX mode
(add-hook 'tex-mode-hook
(lambda ()
(interactive)
(add-to-list 'tex-verbatim-environments "code")))
;;; Move Text
(rc/require 'move-text)
(global-set-key (kbd "M-p") 'move-text-up)
(global-set-key (kbd "M-n") 'move-text-down)
(rc/require
'rust-mode
'markdown-mode
'elixir-mode
'svelte-mode
)
;;; Svelte Mode
(add-to-list 'auto-mode-alist '("\\.svelte\\'" . svelte-mode))
;;; Kill all buffers
(defun kill-all-buffers ()
(interactive)
(dolist (cur (buffer-list))
(kill-buffer cur)))
(defun close-current-buffer ()
(interactive)
(kill-buffer (buffer-name)))
(global-set-key (kbd "C-x K") 'kill-all-buffers)
(global-set-key (kbd "C-S-w") 'close-current-buffer)
(defun indent-whole-file ()
(interactive)
(indent-region (point-min) (point-max)))
(global-set-key (kbd "C-<tab>") 'indent-whole-file)
(global-set-key (kbd "C-=") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)

Did you read this part of the doc for display-line-numbers:
This variable automatically becomes buffer-local when set outside Custom.
However, setting it through Custom sets the default value.
You're using setq with a user option - not a great idea in general. Use M-x customize-option instead, or use function customize-set-variable or custom-set-variables.
Unless you do some tricky finagling, you can't depend on your setq being evaluated in any particular buffer. And anyway, you apparently want to set the default value of the option. setq-default sets the default value of a variable. But anyway, use one of the custom* functions or (even better), M-x customize-option.

You need to use display-line-numbers-type instead as per the documentation
"The command M-x display-line-numbers-mode provides a convenient way to turn on display of line numbers. This mode has a globalized variant, global-display-line-numbers-mode. The user option display-line-numbers-type controls which sub-mode of line-number display, described above, these modes will activate."
So following works.
(global-display-line-numbers-mode 1)
(setq display-line-numbers-type 'relative)

Related

Locally override keybinding for emacs major mode

What is the proper way to override a keybinding for a major mode so it only affects the buffer-local keymap? I thought I could use local-set-key or make-local-variable, but my attempts there affect the keymaps globally (shown below).
Is it necessary to copy the map, as done below, or create a minor mode with a different keymap? It would be nice to be able to make it a local variable if possible.
For example, say I want to have original bound to C-c C-c in the global emacs-lisp-mode-map but after calling jump-to-other-buffer I want C-c C-c to be bound to local-version only in that buffer.
(defun original ()
(interactive)
(message "original"))
(defun local-version ()
(interactive)
(message "local binding"))
;; open new buffer in emacs-lisp mode and set a local key
(defun jump-to-other-buffer ()
(interactive)
(with-current-buffer (get-buffer-create "*test1*")
(emacs-lisp-mode)
;; These change bindings in all elisp buffers
;; (make-local-variable 'emacs-lisp-mode-map)
;; (define-key emacs-lisp-mode-map (kbd "C-c C-c") 'local-version)
;; (local-set-key (kbd "C-c C-c") 'local-version)
(let ((overriding-local-map (copy-keymap emacs-lisp-mode-map)))
(define-key overriding-local-map (kbd "C-c C-c") 'local-version)
(use-local-map overriding-local-map))
(pop-to-buffer (current-buffer))))
;; default binding in elisp buffers
(define-key emacs-lisp-mode-map (kbd "C-c C-c") 'original)

key binding in slime emacs

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)

wrong type argument commandp with a custom function in emacs

I know this type of question have been asked by many people,
but I have read many similar posts and still have no idea
what to do. So here is the elisp code in .emacs:
;; send line to python console
(require 'python-mode)
(defun py-execute-line-down ()
"execute python line and move cursor down"
(progn
(py-execute-line)
(evil-next-line)))
(add-hook 'python-mode-hook
(lambda () (define-key python-mode-map (kbd "C-c C-j") 'py-execute-line-down)))
I also tried to add (interactive) into the function, it didn't work.
Just to keep the record here, this seemed to do the trick, not sure if it's optimal though:
;; send line to python console
(require 'python-mode)
(defun py-execute-line-down ()
"execute python line and move cursor down"
(interactive)
(py-execute-line)
(evil-next-line 1))
(defun kaiyin-pykeys ()
"python mode custome keys"
(local-set-key (kbd "C-c j") 'py-execute-line-down)
)
(add-hook 'python-mode-hook 'kaiyin-pykeys)
Taking Dan's advice, I changed the above into:
;; send line to python console
(require 'python-mode)
(defun py-execute-line-down ()
"execute python line and move cursor down"
(interactive)
(py-execute-line)
(forward-line 1))
(define-key python-mode-map (kbd "C-c j") 'py-execute-line-down)

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

Is Emacs supposed to have a hook.el file?

So I've got this file called hooks.el in my emacs.d with this kind of content:
(add-hook 'term-mode-hook ...)
(add-hook 'term-exec-mode-hook ...)
(add-hook 'python-mode-hook ...)
(add-hook 'ido-setup-hook ...)
(add-hook 'makefile-mode-hook ...)
(add-hook 'c-mode-common-hook ...)
(add-hook 'c-mode-hook ...)
(add-hook 'c++-mode-hook ...)
(add-hook 'dired-mode-hook
(lambda()
(define-key dired-mode-map "h" 'dired-previous-line)
(define-key dired-mode-map "j" 'ido-find-file)
;; ...
))
I was just wondering if I'm doing something weird here
or is this indeed the idiomatic way to assign shortcuts based on mode?
I mean here adding hooks instead of plainly writing:
(define-key dired-mode-map "h" 'dired-previous-line)
Of course, this won't work unless dired is loaded and dired-mode-map is defined,
hence the hook. And it's probably not the best thing to just load all the modes,
even if I'm not using them always, just to define the custom shortcuts.
But on the other hand, the hook is being run for every new buffer open -
and all these shortcuts are being redefined over and over, instead of just once.
What's your take one the matter? I'm sure there's something more optimal that I could switch to.
In the meantime, I can show off the bookmarklet for my hooks.el:
(defun goto-hook-file ()
"Opens hooks.el at point specific to current `major-mode'"
(interactive)
(let ((str-mode-hook (format "%s-hook" major-mode)))
(find-file (concat emacs.d "hooks.el"))
(goto-char (point-min))
(search-forward str-mode-hook nil t)))
You can use eval-after-load:
(eval-after-load 'dired
'(progn
(define-key dired-mode-map "h" 'dired-previous-line)
(define-key dired-mode-map "j" 'ido-find-file)))
That way, the keys are only defined once.