Switch auto-fill-mode in Emacs Lisp - emacs

I am trying to make keyboard shortcut for toggling auto-fill-mode on or off.
In my ~/.emacs I have
(setq-default auto-fill-function 'do-auto-fill)
Now I tried to solve my problem using this function:
(defun my-switch-auto-fill-mode ()
(interactive)
(if (auto-fill-function)
(auto-fill-mode 0)
(auto-fill-mode 1)))
But it does not work..

A simple thing like will work:
(global-set-key (kbd "<f2>") 'auto-fill-mode)
However, calling
(auto-fill-mode)
only turns it on.
To toggle it from lisp code, use:
(call-interactively 'auto-fill-mode)

Related

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

having two functions executed with one keyboard combo

I'm trying to have C-<return> map to move-end-of-line then newline-and-indent
In my .emacs I have been playing around with the following:
(global-set-key (kbd "C-<return>") '(progn (move-end-of-line) (newline-and-indent)))
And
(defun c-ret()
'(move-end-of-line newline-and-indent))
(global-set-key (kbd "C-<return>") 'c-ret)
but neither work.
Pointers?
You are quoting the commands.
That implies that they won't be executed. You also need the (interactive) to signify to emacs that it's callable from the keyboard.
Then, you need to have your parameters to your functions correct.
Further, I think your nomenclature for return is wrong.
Your basic misunderstanding here is knowing how eLisp works. That's okay, it's an arcane programming language.
' a.k.a QUOTE is pretty much a Lisp-specific special instruction. It says, "Don't evaluate what comes after me", and returns the unevaluated parameter.
So '(foo bar) is desugared into (QUOTE (FOO BAR)), which returns (FOO BAR).
Try this:
(defun c-ret()
(interactive)
(move-end-of-line nil)
(newline-and-indent))
(global-set-key (kbd "C-RET") 'c-ret)
You can do this without writing any code yourself. See http://www.emacswiki.org/emacs/KeyboardMacrosTricks for instructions on capturing a sequence of commands as a keyboard macro, naming it, and saving it in your .emacs. You can then give the new command a key binding of your choice with e.g. (global-set-key (kbd "C-c i") 'new-command-name).
If you want a one-line solution, this will work too:
(global-set-key (kbd "C-<return>") (lambda () (interactive) (move-end-of-line nil) (newline-and-indent)))

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)

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]))

How do you activate line-wrapping in Emacs?

How do you use line-wrap (autoscrolling?) in emacs?
So that the portion that doesn't fit on the screen isn't shown as opposed to shown on the next line?
What you describe sounds more like linewrap than scrolling. If that's what you're actually interested in, it's controlled with buffer-local variable truncate-lines. You can use customization to set it globally, or use hooks. For example, I used to prevent linewrap in dired with this:
(add-hook 'dired-mode-hook (lambda () (setq truncate-lines t)))
toggle-truncate-lines allows you to turn it on-off (as Peter noted in a comment on this post)
I have it mapped to a function-key:
(define-key global-map [f5] 'toggle-truncate-lines)
It won't work in vertically-split (partial-width) windows unless truncate-partial-width-windows is set to nil (from my .emacs):
(setq-default truncate-lines t)
(setq truncate-partial-width-windows nil) ;; for vertically-split windows
You can also change this from the menu.
Options->Line Wrapping in this Buffer->Truncate Long Lines
Or if you want this globally you can use the function
global-visual-line-mode
i prefer these settings:
;; disable line wrap
(setq default-truncate-lines t)
;; make side by side buffers function the same as the main window
(setq truncate-partial-width-windows nil)
;; Add F12 to toggle line wrap
(global-set-key (kbd "<f12>") 'toggle-truncate-lines)
;; Wrapping the lines
(global-set-key (kbd "C-x p") 'toggle-truncate-lines)
This works with Emacs-27.1