Emacs Elisp Overriding Default Value - emacs

I have several abbrev defined that I was accessible everywhere except in latex mode. I defined
(setq-default abbrev-mode t)
(add-hook 'latex-mode-hook (lambda () (abbrev-mode -1)))
But whenever I open a latex file it still has abbrev mode enabled. What's going on?

Never worked with latex before, but for me the following works fine:
(setq auto-mode-alist (cons '("\\.lat\\'" . latex-mode) auto-mode-alist))
(setq-default abbrev-mode t)
(add-hook 'latex-mode-hook (lambda () (abbrev-mode -1)))
M-x abbrev-mode
%Abbrev mode enabled in current buffer
Please make sure the emacs recognized your file as a latex file, the first line I wrote should do the trick.

The reason was that AUCTex uses LaTeX-mode-hook. Thanks to stefan in the comment for pointing that out

Related

Set variable under specific mode emacs

Looking to set a variable under latex mode. The idea is that the value set under latex mode will override the value of the same variable set in the customise section. I am very new to emacs so these are my attempts:
(add-hook 'LaTeX-mode-hook '(setq line-move-visual t))
(add-hook 'latex-mode-hook (lambda () (setq line-move-visual t)))
Why do these not work? What should I do instead?
Clarification: looking to set the variable (setq line-move-visual t) as I have it as (setq line-move-visual nil) for all other files
If you just setq the variable in your LaTeX-mode-hook it will also have an effect on any other open buffer. It is possible to make the change only effect the current buffer:
(add-hook 'LaTeX-mode-hook
(lambda ()
(make-local-variable 'line-move-visual)
(setq line-move-visual nil)))
Also, please note that the hook for the default mode for LaTeX in Emacs is called latex-mode-hook but the hook when you are using the (far superior) AUCTeX is called LaTeX-mode-hook
EDIT: Changed make-variable-buffer-local to make-local-variable. See comments to this answer.

whitespace-mode in minibuffer?

Is there a way I can turn on whitespace-mode in a minibuffer? I'm not sure I'll stick to it - but sometimes I need it. I tried to add-hook all the M-x apropos RET minibuffer hook RET:
(mapc
(lambda (language-mode-hook)
(add-hook language-mode-hook
(lambda () (interactive) (whitespace-mode 1))))
'(minibuffer-setup-hook
icicle-minibuffer-setup-hook
icomplete-minibuffer-setup-hook
ido-minibuffer-setup-hook
minibuffer-inactive-mode-hook))
but it doesn't work.
It looks like you've got something that should work, this worked for me:
(defun enable-ws-mode ()
(whitespace-mode 1))
(add-hook 'minibuffer-inactive-mode-hook 'enable-ws-mode)
as did the code you posted. Have you tried running Emacs without your initialization files?
emacs -q
emacs -q --no-site-init
and evaluating the code you had?
Perhaps this is only because it's been six years, but . . . with Emacs 26.3 I was able to add the regular whitespace mode command to the hook with no intermediating function:
(add-hook 'minibuffer-inactive-mode-hook 'whitespace-mode)
No fuss, no muss, works here.

why mode isn't enabled?

I've got an emacs configuration file whatever.el :
(abbrev-mode +1)
(provide 'whatever)
and in my init.el :
(require 'whatever)
but when i start emacs, abbrev-mode isn't enabled. why ?
thank you
Quoting from http://emacswiki.org/emacs/AbbrevMode:
You can also put the following in your ~/.emacs file if you want it
always on:
(setq default-abbrev-mode t)
If you only want it on in text and derived modes, you could do
something like this:
(add-hook 'text-mode-hook (lambda () (abbrev-mode 1)))
For multiple modes, use something like the following:
(dolist (hook '(erc-mode-hook
emacs-lisp-mode-hook
text-mode-hook))
(add-hook hook (lambda () (abbrev-mode 1))))
Abbrev-mode is enabled per-buffer.
One way is to create a hook function that you could add to the major mode hooks you will want to use it in.
For example:
(defun my-enable-abbrev-mode ()
(abbrev-mode 1))
(add-hook 'c-mode-hook 'my-enable-abbrev-hook)
(add-hook 'java-mode-hook 'my-enable-abbrev-hook)
Another approach is to use change-major-mode-hook.
While others explained how to get what you presumably want, I'll just point out that w.r.t to your actual question ("Why?"), the reason is simple: abbrev-mode is a buffer-local minor-mode, so when you run (abbrev-mode +1) at startup it will just enable abbrev-mode in the buffer that happens to be current during evaluation of the ~/.emacs (typically scratch) but not in subsequent buffers.

Conflicts between org-mode and yasnippet

EDIT My issue was related to snippet syntax all along... The configuration below totally works.
I'm trying to use org-mode and yasnippet together and it's not working even with some of the workarounds on the org-mode FAQ. Whenever I hit TAB on a snippet abbreviation the word gets deleted. TAB behaves normally if I'm not over a snippet word, so there's something going on...
I'm using Org-mode version 7.7, yasnippet (version 0.7.0), and GNU Emacs 23.4.1.
Here's my setup:
(setq load-path
(append (list nil
"~/.emacs.d/site-lisp/yasnippet"
"~/.emacs.d/site-lisp/org-7.7/lisp")
load-path))
;; set up yasnippet
(require 'yasnippet)
(yas/initialize)
(setq yas/snippet-dirs '("~/.emacs.d/mysnippets"
"~/.emacs.d/site-lisp/yasnippet/snippets"))
(mapc 'yas/load-directory yas/snippet-dirs)
;; set up org mode
(require 'org-install)
;; fix some org-mode + yasnippet conflicts:
(defun yas/org-very-safe-expand ()
(let ((yas/fallback-behavior 'return-nil)) (yas/expand)))
(add-hook 'org-mode-hook
(lambda ()
(make-variable-buffer-local 'yas/trigger-key)
(setq yas/trigger-key [tab])
(add-to-list 'org-tab-first-hook 'yas/org-very-safe-expand)
(define-key yas/keymap [tab] 'yas/next-field)))
And I'm pretty sure the hook is running as expected because of the following output of C-h v org-tab-first-hook in an org buffer:
org-tab-first-hook is a variable defined in `org.el'.
Its value is
(yas/org-very-safe-expand org-hide-block-toggle-maybe org-src-native-tab-command-maybe org-babel-hide-result-toggle-maybe)
And here's C-h k TAB in an org buffer:
<tab> runs the command org-cycle, which is an interactive Lisp
function in `org.el'.
EDIT
After doing a edebug-defun on my yas/org-very-safe-expand function I'm seeing the following message
Result: "[yas] elisp error! Symbol's value as variable is void: err"
So yas is error'ing out somewhere... My edebug foo is not quite up to par but if I get some time I'll try to single step through and see where the error is. My full emacs configuration is on github here.
Turns out my issue was related to snippet syntax, not setup. Silly silly me...
In other words, this totally works:
;; fix some org-mode + yasnippet conflicts:
(defun yas/org-very-safe-expand ()
(let ((yas/fallback-behavior 'return-nil)) (yas/expand)))
(add-hook 'org-mode-hook
(lambda ()
(make-variable-buffer-local 'yas/trigger-key)
(setq yas/trigger-key [tab])
(add-to-list 'org-tab-first-hook 'yas/org-very-safe-expand)
(define-key yas/keymap [tab] 'yas/next-field)))
This took me some time to get worked out as well. I'm using Org-mode version 7.7, yasnippet (version 0.6.1c), GNU Emacs 22.1.1. Here are the relevant portions of my .emacs file (there is some flyspell stuff that is irrelevant):
;;
;; org-mode stuff
;;
(add-to-list 'load-path "/Users/cmalone/install/org-mode/org-mode/lisp")
(add-to-list 'load-path "/Users/cmalone/install/org-mode/org-mode/contrib/lisp")
(require 'org-install)
;;
;; for YASnippet
;;
(add-to-list 'load-path "/Users/cmalone/.emacs.d/plugins/yasnippet-0.6.1c")
(require 'yasnippet)
(yas/initialize)
(yas/load-directory "/Users/cmalone/.emacs.d/plugins/yasnippet-0.6.1c/snippets")
;; Make TAB the yas trigger key in the org-mode-hook and enable flyspell mode and autofill
(add-hook 'org-mode-hook
(lambda ()
;; yasnippet
(make-variable-buffer-local 'yas/trigger-key)
(org-set-local 'yas/trigger-key [tab])
(define-key yas/keymap [tab] 'yas/next-field-group)
;; flyspell mode for spell checking everywhere
;; (flyspell-mode 1)
;; auto-fill mode on
(auto-fill-mode 1)))
C-h v org-tab-first-hook is the same as yours except of the yas/org-very-safe-expand of course. C-h k TAB shows:
TAB runs the command yas/expand
which is an interactive Lisp function in `yasnippet.el'.
It is bound to TAB, <menu-bar> <YASnippet> <Expand trigger>.
(yas/expand)
Expand a snippet before point.
If no snippet expansion is possible, fall back to the behaviour
defined in `yas/fallback-behavior'

emacs auto-fill-mode doesn't initialise

I want to enable line-wrapping without having to type 'M-x auto-fill-mode' everytime I start emacs. I've tried putting (setq auto-fill-mode 1) and (auto-fill-mode 1) in the .emacs file, but neither work. Why is this, and how do I fix it?
Thanks
It is a minor-mode so you need to enable it for the modes where you want it used. So, for example, if you want auto-fill-mode enabled in text mode, you need to add the following to your .emacs file:
(add-hook 'text-mode-hook '(lambda ()
(auto-fill-mode 1)))
auto-fill-mode is a minor mode so (setq auto-fill-mode 1) wont start it.
You can add a hook to start auto-fill-mode with the text-mode (with which it is normally used) or any other mode you normally use it with, by doing
(add-hook 'text-mode-hook 'turn-on-auto-fill)
Alternatively, if you want the auto-fill-mode on for all the files you edit. You can start it when any type of file is opened with:
(setq auto-mode-alist (cons '("*" . auto-fill-mode) auto-mode-alist))
But having it always on is irritating at times, so its better to bind the starting of the mode to a familiar key sequence
(global-set-key (kbd "C-c q") 'auto-fill-mode)