Programmatically setting major mode of buffer with emacs lisp - emacs

I wish to programmatically set the major mode of a buffer. I have dug into the documentation, and the function set-buffer-major-mode only takes a buffer object. I am essentially looking for a function that takes a buffer object and a mode name.
Alternatively, I also tried using auto-mode-alist by forcing the buffer to have a file extension but that did not work even when I called the set-buffer-major-mode function.
Thanks for all the help!

The basics are that if you want to switch to my mode, all you have to do is to call my-mode, for example:
(with-current-buffer buffer
(my-mode))
If you have a buffer and a variable bound to the major mode, you could use the following:
(with-current-buffer buffer
(funcall the-mode-i-want))
Again, if you have a variable bound to a string, you have to convert it to a symbol using intern:
(with-current-buffer buffer
(funcall (intern the-name-of-the-mode-i-want)))

Related

Get the content as string of a region in a buffer by elisp program

Here is the problem that I'm solving:
Convert a region of text into a string data structure for subsequent processing by elisp program. The challenge is that
I want to execute an elisp program interactively without affecting the selection of a region
store the string value into a variable so that I can further manipulate it.
By my understanding, a region is defined by a mark and the subsequent cursor position. And I usually execute elisp program in *scratch* buffer. Furthermore, the region is also in the *scratch* buffer.
But to write the function call and execute it in the buffer, I need to move the cursor away from the end of the text selection (region) in order to write the program of
(setq grabbed (buffer-substring-no-properties (region-beginning) (region-end)))
but then the region of selection would change due to the cursor movement.
So I wonder how I could execute the elisp program while keeping the selection intact and still can access the return value.
If you want to run the function from some Elisp code but as if the user had invoked it via a keybinding, you can use call-interactively:
(setq variable-to-keep-the-value (call-interactively 'lines-to-list))
But in most cases, what you want instead is to take yourself the responsibility to choose to which part of text the function should apply:
(setq variable-to-keep-the-value
(lines-to-list (region-beginning) (region-end)))
Notice that the region's boundaries are nothing magical, regardless if they've been set by the mouse or what.
Finally, I found a desirable solution! It's using ielm buffer, the real repl of elisp.
In the ielm buffer, I can set working buffer (by C-c C-b) to be a buffer where I have the text to be manipulated, for example, *scratch*.
I can then select a region of the working buffer to be processed, and in the ielm buffer then I can type and execute elisp code to extract the text in the selected region in the working buffer, for example,
ELISP> (setq grabbed (buffer-substring-no-properties (region-beginning) (region-end)))
"One\nTwo\nThre"
ELISP> grabbed
"One\nTwo\nThre"
ELISP> (split-string grabbed)
("One" "Two" "Thre")
I can then work with the value held by the set variable, grabbed.
Here is a very helpful description of ielm:
https://www.masteringemacs.org/article/evaluating-elisp-emacs

emacs dispatch help window from original buffer

Whenever I do apropos, describe-key, or some other help function in emacs, it displays the help in my other window. In order to get rid of it, I must change windows/buffers to go there, type "q", and then change back to my original working buffer.
Is there a way I can do this in code somehow? I know how to save-excursion, switch buffers, etc, but I don't know how to send the "q" to the minibuffer/emacs while I'm over in the other buffer. Thanks
The help-window-select variable might be exactly what you want.
If you set its value to true (setq help-window-select t) then the help window will automatically be selected when you open it via one of the help commands. You can then press q to quit out of it and go back to your original buffer. There are many other options so you should check those out too.
For the apropos window or any window that uses display-buffer you can use.
(add-to-list 'display-buffer-alist
'("*Apropos*" display-buffer-same-window))
display-buffer-same-window is one options of many; it opens the buffer in the current window. The other possible options can be seen by looking up the docs on the display-buffer function.
Here's my solution to this problem. I bind this command to C-c q:
(defvar my/help-window-names
'(
;; Ubiquitous help buffers
"*Help*"
"*Apropos*"
"*Messages*"
"*Completions*"
;; Other general buffers
"*Command History*"
"*Compile-Log*"
"*disabled command*")
"Names of buffers that `my/quit-help-windows' should quit.")
(defun my/quit-help-windows (&optional kill frame)
"Quit all windows with help-like buffers.
Call `quit-windows-on' for every buffer named in
`my/help-windows-name'. The optional parameters KILL and FRAME
are just as in `quit-windows-on', except FRAME defaults to t (so
that only windows on the selected frame are considered).
Note that a nil value for FRAME cannot be distinguished from an
omitted parameter and will be ignored; use some other value if
you want to quit windows on all frames."
(interactive)
(let ((frame (or frame t)))
(dolist (name my/help-window-names)
(ignore-errors
(quit-windows-on name kill frame)))))
I suggest putting (winner-mode 1) in your init file, and then using C-c<left> to call winner-undo (repeatedly, if necessary) to return to a previous window configuration.

&optional BUFFER for other-buffer function does not work?

When I need to switch to another buffer I have a key binding that will create a buffer called "*Buffer List*" from which I select the new buffer.
I need a command to switch between current buffer and the previous one. Except I don't want "*Buffer List*". I looked up function definition for other-buffer:
(other-buffer &optional BUFFER VISIBLE-OK FRAME)
Return most recently selected buffer other than BUFFER...
so I tried using:
(other-buffer "*Buffer List*")
Now if in the new buffer I execute above code with C-x C-e it will echo "*Buffer List*", instead of the initial buffer that I called "*Buffer List*" from. So &optional BUFFER does not seem to work the way I do it. Can anyone explain why?
BUFFER must be a buffer object. To use the buffer name you would need to use:
(other-buffer (get-buffer "*Buffer List*"))
Notice that when you can use a buffer's name as the argument, the documentation tends to name that argument BUFFER-OR-NAME (e.g. see the docstring for get-buffer itself), and to be explicit about the fact in the text.
If you see BUFFER as an argument, it likely requires the actual buffer object (as in this instance).

Emacs: How to visit all sql-mode buffers and set the appropriate sql-buffer

My typical usage of sql-mode in emacs is to:
a. open a foo.sql file and begin editing
b. decide I want to run it using the key bindings for sql-send-region
c. fire up my custom (db-connect) function to connect to an appropriate db and create a *SQL* buffer.
However the foo.sql doesn't know about the existence of the *SQL* buffer unless I perform a "m-x sql-mode" in the buffer in order to refresh its environment and detect that such a buffer exists at this point. I would like to embed some code in my custom db-connect function to visit all buffers using sql-mode and update the sql-buffer variable. I am sure several stack overflow members must have done this or something similar before.
Thanks,
SetJmp
A quick look in the sql.el file revealed the command sql-set-sqli-buffer-generally, maybe this is something for you?
Another way you could hand this is to kill the buffer-local variant of sql-buffer by calling kill-local-variable in your major-mode hook. (That way, the effect would be that all SQL buffers would talk to the latest SQL buffer.)
Disclaimer: I don't know anything about SQL or SQL mode, only Emacs in general.
I've implemented this small helper function to filter buffers by their major-mode
(defun buffer-mode (buffer-or-name)
(with-current-buffer buffer-or-name major-mode))
(defun filter-buffers-by-mode (mode)
(delq nil
(mapcar
(lambda (x) (and (eq (buffer-mode x) mode) x))
(buffer-list))))
You can pass 'sql-mode as the argument and you'll get a list of all open sql buffers.

How do you list the active minor modes in emacs?

How do you list the active minor modes in emacs?
C-h m or M-x describe-mode shows all the active minor modes (and major mode) and a brief description of each.
A list of all the minor mode commands is stored in the variable minor-mode-list. Finding out whether they're active or not is usually done by checking the variable of the same name. So you can do something like this:
(defun which-active-modes ()
"Give a message of which minor modes are enabled in the current buffer."
(interactive)
(let ((active-modes))
(mapc (lambda (mode) (condition-case nil
(if (and (symbolp mode) (symbol-value mode))
(add-to-list 'active-modes mode))
(error nil) ))
minor-mode-list)
(message "Active modes are %s" active-modes)))
Note: this only works for the current buffer (because the minor modes might be only enabled in certain buffers).
describe-mode can somehow come up with a list of enabled minor modes, why couldn't I? So after reading its source code I realized that it gets the list of active minor modes from both minor-mode-list and minor-mode-alist. Using 3rd-party dash.el list manipulation library I came with this code:
(--filter (and (boundp it) (symbol-value it)) minor-mode-list)
So, for example, to disable all minor modes, use -each:
(--each (--filter (and (boundp it) (symbol-value it)) minor-mode-list)
(funcall it -1))
Don't forget to save the list of minor modes in a variable, otherwise you would have to restart Emacs or enable them by memory.
If you want to programmatically do something with all buffers that have a certain mode active, then the best, most minimalistic, cleanest, built-in solution is as follows:
(dolist ($buf (buffer-list (current-buffer)))
(with-current-buffer $buf
(when some-buffer-local-minor-or-major-mode-variable-you-want-to-find
(message "x %s" $buf))))
It does the following:
Retrieve a list of all buffers via buffer-list, with the currently active buffer at the head of the list (so it's treated first, usually what you want, but leave out the current-buffer parameter if you don't care).
Loop through the buffer list and assign each buffer name to the variable $buf.
Use with-current-buffer $buf to tell Emacs that all code within the body should run as if it was running inside buffer $buf instead of whatever buffer you're really displaying on screen.
when <some mode variable> is the correct way to check if a mode is enabled; you can also use if and other such methods. Either way, the goal is to check if a minor or major-mode's main mode variable is set in the buffer. Almost all modes define a variable via "defining" a mode, which automatically causes them to create a buffer-local variable named after the mode, which is how this works. And if they don't have a standard variable, look at their own source code to see how their "toggle" code determines how to toggle them on and off. 99% of them use the existence of their modename's variable (and if they don't, I suggest reporting that as a bug to the mode's author). For example, to check if a buffer has whitespace-mode active, you would say when whitespace-mode.
After that, it just outputs a message to the Messages buffer, with an "x" and the name of the buffer that had the mode active. That's where you'd put your own code, to do whatever you wanted to do with the discovered buffer.
Enjoy! Onwards to greater and cleaner lisp code!
Here is a simple alternative snippet similar to some of the methods that have already been addressed in other answers:
(delq nil
(mapcar
(lambda (x)
(let ((car-x (car x)))
(when (and (symbolp car-x) (symbol-value car-x))
x)))
minor-mode-alist))
If you just want to know if a particular minor mode (say, evil-mode) is active in the buffer, you could evaluate the following:
(when (member 'evil-mode minor-mode-list)
(message "`evil-mode' is active!"))