How to toggle a command in Emacs lisp? - emacs

I would like to have a shortcut that would toggle the showing of line numbers in Emacs.
This is what I have so far:
(defun my-toggle-display-line-numbers-mode-function ()
"Toggles the line numbers"
(interactive)
(display-line-numbers-mode)
)
(global-set-key [(f7)] 'my-toggle-display-line-numbers-mode-function)
But it will only turn on the line numbers. I can't turn it off, unlike when I use M-x display-line-numbers-mode which will turn on or off without any problem.
Any idea how to improve my script so it works as intended?

describe-function (C-h f) on display-line-numbers-mode gives the following:
Signature:
(display-line-numbers-mode &optional ARG)
Documentation:
[...]
This is a minor mode. If called interactively, toggle the
Display-Line-Numbers mode mode. [...]
If called from Lisp, toggle the mode if ARG is toggle. Enable
the mode if ARG is nil, omitted, or is a positive number.
Disable the mode if ARG is a negative number.
This is the automatic desciption produced by the define-minor-mode macro, and for most minor modes, the previous documentation will be exactly the same.
What we get from this:
When called interactively (i.e. as a command, using for example M-x display-line-numbers-mode) this acts as a toggle.
When called from Elisp source code, you need to specify an argument.
If you want it to act as a toggle, use (display-line-numbers-mode 'toggle) in your code. If you want something slightly more advanced than a simple toggle (for example, checking some extra conditions), use 1 and -1 as arguments to respectively enable and disable the mode (incidentally, and although the documentation is unclear, 0 counts as a negative number here, and so (display-line-numbers-mode 0) disables the mode.
As it is often the case, Emacs built-in documentation shows all there is to know. Try to get familiar with the help system, it's really good compared to a lot of softwares.

Related

Showing line numbers in dired file browser in Emacs

I am unable to show line numbers in Emacs when browsing files using Dired.
Dotspacemacs-line-numbers '(:relative nil
:visual t
:disabled-for-modes ;; dired-mode
doc-view-mode
markdown-mode
;; org-mode
pdf-view-mode
;; text-mode
:size-limit-kb 1000)
but line numbers are not showing.
any idea how to enable line numbers in Dired?
Turn on minor mode display-line-numbers-mode:
M-x display-line-numbers-mode. C-h f tells you:
display-line-numbers-mode is an autoloaded interactive compiled Lisp
function in display-line-numbers.el.
(display-line-numbers-mode &optional ARG)
Toggle display of line numbers in the buffer.
This uses display-line-numbers internally.
This is a minor mode. If called interactively, toggle the
Display-Line-Numbers mode mode. If the prefix argument is positive,
enable the mode, and if it is zero or negative, disable the mode.
If called from Lisp, toggle the mode if ARG is toggle. Enable the
mode if ARG is nil, omitted, or is a positive number. Disable the
mode if ARG is a negative number.
To check whether the minor mode is enabled in the current buffer,
evaluate display-line-numbers-mode.
The mode’s hook is called both when the mode is enabled and when it is
disabled.
To change the type of line numbers displayed by default,
customize display-line-numbers-type. To change the type while
the mode is on, set display-line-numbers directly.
Probably introduced at or before Emacs version 26.1.

Can I change emacs settings while it's running?

I'm very new to the Emacs text editor and have just started customizing my ~/.emacs file. I'm wondering if it's possible to change an Emacs setting while it's running. For example, if I put the following in my ~/.emacs file:
(show-paren-mode 1)
I can do the same thing when Emacs is running by typing:
M-x then scroll-step then 1.
Why doesn't this work when I want to do something like this:
(setq scroll-step 1)
When I type:
M-x then setq,
all I get is (no match). There must something I'm not understanding here.
There is a few things:
yes, you can change settings at run-time. If you edit your .emacs file, put your cursor after the closing parenthesis in:
(show-paren-mode 1)
And hit ctrl-x ctrl-e and it will evaluate the code.
some things and variables (though very few) require them to run special things after the setting was changed. Generally there aren't too many and most variables you set will take effect immediately after doing the steps in #1.
However, if you edit your settings using M-x customize you'll find that when you make changes there it'll make the settings active immediately, even in those special cases where something special needs to be done after a value change.
M-x allows you to run "interactive" commands. Some elisp functions are supposed to be called directly by the end user, and others are really only meant when writing elisp into a file. And M-x only lets you easily do the ones that have been marked "interactive". Though M-shift-: will let you type an expression and see the results. EG, try M-shift-: followed by (1+ 2) at the prompt.

Emacs Lisp: How to, an interactive buffer with REPL?

Imagine the dunnet.el game, the eliza doctor or the IELM mode...; This is, evaluating answers and prompting new questions on the main buffer, then making some actions according to their elisp rules.
I would to create something like an interactive assistant, BUT NOT prompting from minibuffer as (interactive) does (not doing M-x)... I need a kind of REPL loop. So the question i how to get interaction on the working buffer (the main screen), like any text conversational/adventure, but for call my defun'ed functions on this way.
So i need a basic elisp skeleton, to make a custom REPL. The target is to make an assistant with a natural language, then parsing the anwsers, and making some actions according the rules.
Thank you for your time,
Steve,
Check out ielm, which is an elisp repl included with emacs.
Depending on what can go on in your buffer, can you just give it a major mode with an appropriate re-binding of return? (This answer vaguely inspired by Lisp-Interaction-Mode's C-j)
You should bind all keys that use for moving in buffer like C-p C-n, Up Down Arrows/ page up/down just bind them to (lambda ()). change function for C-b and C-f (Left Right Arrows) if cursor position is in beginning or end of the line. And bind Return key to function that read current line from end to optional prompt (you can use Regex to remove the prompt) do whatever you want with that string, and then go to the next line in the buffer and print your result.

Remove Emacs highlighting

I use the emacs command 'highlight-compare-buffers' to see the changes between two files. But I don't know how to turn the highlighting off. It seems like it should be really easy, but I can't seem to figure out how even with lots of googling and searching the emacs help files.
Try:
C-u -1 M-x highlight-compare-buffers
global-highlight-changes is an
interactive autoloaded Lisp function
in `hilit-chg'.
(global-highlight-changes &optional
arg)
Turn on or off global Highlight
Changes mode.
When called interactively:
if no prefix, toggle global Highlight Changes mode on or off
if called with a positive prefix (or just C-u) turn it on in active mode
if called with a zero prefix turn it on in passive mode
if called with a negative prefix turn it off
I don't have emacs installed on this box, so this is untested, but highlight-changes-toggle-visibility might work.

Does Emacs has word and line completion (like Vim's insert mode completion)?

Vim completes words and lines with CTRL-X P and CTRL-L. There's a Emacs plugin called Company mode but this plugin interfere and cause conflicts with lots of things within Emacs (with global linum and yasnippets). I know that I can complete words with CTRL-/ in Emacs. But it is possible to take previously written lines to complete code?
Maybe you're looking for hippie-expand? From that web page (as of this writing, anyway):
HippieExpand looks at the word before
point and tries to expand it in
various ways including expanding from
a fixed list (like expand-abbrev),
expanding from matching text found in
a buffer (like dabbrev-expand) or
expanding in ways defined by your own
functions. Which of these it tries and
in what order is controlled by a
configurable list of functions.
For a comprehensive list of completion options visit the emacs wiki page on completion.
There are a gazillion ways to do completion in Emacs. Some are mode specific, some inline, some configurable and what not. Here is a list of modes that might help you.
Use numberic argument to complete by line, say M-5 M-/ will complete by line, while M-/ alone still complete the normal way.
hippe-expend function has a very useful feature which is :
With a positive numeric argument, jumps directly to the ARG next function in this list. With a negative argument or just C-u, undoes the expansion.
Customize the expansion functions in hippie-expand-try-functions-list and put the function try-expand-line as 5th list element, then you could use M-5 M-/ to complete by line.
This tip is very handy and useful and I highly recommend it.
Also worth noting: if your window manager does not steal Alt-tab, emacs will auto-complete with Alt-tab (I set up my window manager to user the "windows key" instead of alt for this very reason).
If you are using evil, this is the most vim-like solution I use:
(defun my-expand-lines ()
(interactive)
(let ((hippie-expand-try-functions-list
'(try-expand-line-all-buffers)))
(call-interactively 'hippie-expand)))
(define-key evil-insert-state-map (kbd "C-x C-l") 'my-expand-lines)
This way you can use our old friend C-x C-l in insert mode for whole line all buffers completion.
Thanks #ymln for the suggestion of using try-expand-line-all-buffers.