emacs auto-fill-mode doesn't initialise - emacs

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)

Related

Emacs Elisp Overriding Default Value

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

How to change GUD breakpoint keybinding to the old one

Currently, I am using GUD in the newest version of Emacs. The keybinding has changed since the old Emacs. Now it is "\C-x \C-a \C-b" for setting a breakpoint but it was \C-[space].
I was wondering if there is anyway to change the keybinding to the old format? (For some reason I cannot change my Emacs version)
I am using Emacs 24.5
Here is my .emacs file:
;; .emacs
;;; uncomment this line to disable loading of "default.el" at startup
;; (setq inhibit-default-init t)
;; turn on font-lock mode
(when (fboundp 'global-font-lock-mode)
(global-font-lock-mode t))
;; enable visual feedback on selections
;(setq transient-mark-mode t)
;; default to better frame titles
(setq frame-title-format
(concat "%b - emacs#" (system-name)))
;; default to unified diffs
(setq diff-switches "-u")
;; always end a file with a newline
;(setq require-final-newline 'query)
;; Show main source buffer when using gdb
(setq gdb-show-main t)
;; Show all debugging frames in GDB
(setq gdb-many-windows t)
;; see buffer list on the same frame
(global-set-key "\C-x\C-b" 'buffer-menu)
;; old keybinding for breakoint in GUD
(require 'gud)
(define-key gud-mode-map "\C-x SPC" 'gud-break)
Changing your Emacs version should not be necessary. Try this:
(require 'gud)
(define-key gud-mode-map (kbd "C-SPC") 'gud-break)
This will allow you to trigger gud-break with C-SPC. If you are not talking about the gud-break command, replace it with the command you are referring too.
Generally, the answer to the question "can I change this keybinding?" is always "yes" in Emacs.
Somehow I was able to fix it with this:
(require 'gud)
(global-set-key [24 32] (quote gud-break))

emacs truncate mode for specific buffers?

In my ~/.emacs, I have
(global-visual-line-mode t)
However, this is making buffer-list hard to read if the emacs window is and narrow. How I can I set it up so I can have buffer-list (and potentially other buffers) to truncate mode?
Most modes have a hook that runs when the mode is set, usually named on the form ...-mode-hook. You could add to a modes' hook to truncate lines (effectively turning off visual-line-mode):
(add-hook
'some-mode-hook
'(lambda ()
(toggle-truncate-lines 1)
)
)
It's worked form me, whit speedbar-mode:
(add-hook
'speedbar-mode-hook
'(lambda ()
(visual-line-mode 0) ; disable only in the buffer sr-speedbar
)
)
This worked for me in markdown mode, so it's always in truncate mode when I'm working with markdown files. (Tested in Emacs v28.1)
;; init.el
(add-hook 'markdown-mode-hook (lambda () (setq truncate-lines t)))

How to remap a function to another in Emacs?

In coffee-mode RET is bound to coffee-newline-and-indent which works fine.
I also use evil-mode to have Vim emulation. evil-mode uses the standard newline-and-indent so the indentation is not correct for some vim commands such as o or O.
What would be the best way to rebind newline-and-indent to coffee-newline-and-indent ?
I'm still a newbie in ELisp and tried the line below but it doesn't work.
(add-hook 'coffee-mode-hook
(lambda ()
(setq newline-and-indent '(funcall coffee-newline-and-indent))))
Here's my attempt. It should work, but I don't really like it.
(add-hook
'coffee-mode-hook
(lambda ()
(defalias
'newline-and-indent
(lambda()
(interactive)
(if (eq major-mode 'coffee-mode)
(coffee-newline-and-indent)
(delete-horizontal-space t)
(newline)
(indent-according-to-mode))))))
I wish I could use something more elegant that just copying the source
of newline-and-indent, but make-variable-buffer-local doesn't work for this case,
and I couldn't get a deep copy for symbol-function either.
I'd be happy to see a better method.
The standard way to accomplish what you seem to be asking for is
(autoload 'coffee-newline-and-indent "coffee-mode") ; (or whatever)
(define-key evil-mode-map (kbd "RET") 'coffee-newline-and-indent)
EDIT: to enable coffee-newline-and-indent only in coffee-mode:
(define-key evil-mode-map (kbd "RET")
(lambda ()
(interactive)
(if (eq major-mode 'coffee-mode)
(coffee-newline-and-indent)
(newline-and-indent))))
Try the following:
(define-key evil-mode-map (kbd "RET") nil)
I know it looks overly simple, but if evil-mode works the way I think it does then it should work.
This will wipe the ret key from your evil-mode-map, which will let the binding of coffee-mode-map shine through.
In non-coffee buffers, the ret key will still work, because it's still bind in the global map.
I found the solution.
Evil-mode actually uses coffee-indent-line. The problem comes from coffee-indent-line which doesn't indent correctly. Evil-mode works correctly after patching it to behave like coffee-newline-and-indent:
(defadvice coffee-indent-line (after wants-indent activate)
(let ((tabs-needed (- (/ (coffee-previous-indent) coffee-tab-width) 1)))
(when (> tabs-needed 0)
(insert-tab tabs-needed)))
(when(coffee-line-wants-indent)
(insert-tab)))
if you want to remap a func, but only if some major mode is active
- create a func which defines an alias
and run the func (A)
- another func (B) calls (A)
- finally, a major mode can advice the func A to set the correct
func. It has to test major mode.
let's say A is define-my-indent-f
then it says (defalias my-indent 'newline-and-indent)
the func b runs A then run command my-indent.
finally coffe mode does defadice A to say
(if eq major mode coffee defalais my-indent 'coffe-newline-and-indent)
of course this is super heavy to define, but then
- each major mode can add its piece
- only loaded major mode will advice

Code completion key bindings in Emacs

When doing a M-x describe-mode in a .el file, I noticed that the Emacs-Lisp mode actually does code completion. However, lisp-complete-symbol is bound to M-TAB. In Windows, this key binding is taken by Windows for switching the active window. Most IDE's use C-SPC, but that's taken in Emacs as well. What is a good, fairly common key binding for code completion?
If you like completion of all kinds, I recommend M-/ and binding that to hippie-expand.
(global-set-key (kbd "M-/") 'hippie-expand)
It does a variety of completions, which are controlled by the variable hippie-expand-try-functions-list. In the .el files, you can set that to do the 'try-complete-lisp-symbol first to get the behavior you're asking for above, along with all the other expansions hippie-expand provides.
This would do that for you:
(add-hook 'emacs-lisp-mode-hook 'move-lisp-completion-to-front)
(defun move-lisp-completion-to-front ()
"Adjust hippie-expand-try-functions-list to have lisp completion at the front."
(make-local-variable 'hippie-expand-try-functions-list)
(setq hippie-expand-try-functions-list
(cons 'try-complete-lisp-symbol
(delq 'try-complete-lisp-symbol hippie-expand-try-functions-list)))
(setq hippie-expand-try-functions-list
(cons 'try-complete-lisp-symbol-partially
(delq 'try-complete-lisp-symbol-partially hippie-expand-try-functions-list))))
As Trey Jackson mentioned, hippie-expand is the way to go, but along with binding it to M-/, I also like having the TAB key do all my completion work for me. So I have this from the Emacs-Wiki in my .emacs file:
;;function to implement a smarter TAB (EmacsWiki)
(defun smart-tab ()
"This smart tab is minibuffer compliant: it acts as usual in
the minibuffer. Else, if mark is active, indents region. Else if
point is at the end of a symbol, expands it. Else indents the
current line."
(interactive)
(if (minibufferp)
(unless (minibuffer-complete)
(hippie-expand nil))
(if mark-active
(indent-region (region-beginning)
(region-end))
(if (looking-at "\\_>")
(hippie-expand nil)
(indent-for-tab-command)))))
(global-set-key (kbd "TAB") 'smart-tab)
You could have hippie expand settings as follows:
;;settings for hippie-expand
(setq hippie-expand-try-functions-list
'(try-complete-lisp-symbol
try-complete-lisp-symbol-partially
try-expand-dabbrev
try-expand-dabbrev-from-kill
try-expand-dabbrev-all-buffers
try-expand-line
try-complete-file-name-partially
try-complete-file-name))
C-M-i; no customization required.
I use:
(define-key function-key-map [(control tab)] [?\M-\t])
I use M-. and M-/ for the 2 completion modes - hippie-expand and the standard emacs one.
Put this in your .emacs to make Windows give Emacs the use of M-TAB:
(when (fboundp 'w32-register-hot-key) (w32-register-hot-key [M-tab]))