How to modify the contents of all the alive buffers in emacs lisp - emacs

I am planning on the following steps to get the output of the question.
get the list of buffers and iterate on it
get the contents of the buffers using the buffer name obtained in step 1.
change the contents and overwrite the buffer.
The (buffer-list) fn output is very confusing. It shows like
(# ...)
I dont understand what does # < and * stands for.

The output of buffer-list is a list of buffer objects.
Functions dealing with a specific buffer will typically accept a buffer object as an argument:
(buffer-name (car (buffer-list)))
The #<...> syntax is explained here:
M-: (info "(elisp) Printed Representation") RET
I can't imagine why you'd want to modify all buffers (remember this includes the minibuffer, possibly some internal-use buffers, various others like *Help*, *Messages*, and maybe some read-only buffers), but you could use the following as the basis:
(let ((buffers (buffer-list)))
(while (car buffers)
(set-buffer (car buffers))
(let ((contents (save-restriction (widen) (buffer-string))))
[do something with contents] )
(setq buffers (cdr buffers))))

Related

Running keep-lines command on all opened buffers in emacs

I have many files opened in emacs buffers. Now I want to run keep-lines command on all this opened buffer.
What is steps for doing this?
I am using emacs version 24.3.
Iterate over all opened buffers: Use dolist to iterate over buffer-list (or any suitable subset you like). Use with-current-buffer for the current iteration's buffer, and call keep-lines inside that.
You can tweak it to have more flexibility (e.g., in the keep-lines call):
(defun foo (regexp)
(interactive (list (read-regexp "Regexp: ")))
(dolist (buf (buffer-list))
(goto-char (point-min)) ; Is this what you want, to start at bob?
(with-current-buffer buf (keep-lines regexp))))
However, buffer-list returns many buffers that you probably do not want to do this to. Filter it as needed, e.g., using remove-if (in cl-lib.el) or whatever.
If you deal only with file buffers, then you might want to use find-file-noselect. There are multiple possibilities, depending on what you really need.
You can practice with something like this:
(defun foo (regexp)
(interactive (list (read-regexp "Regexp: ")))
(dolist (buf (buffer-list))
(with-current-buffer buf
(when (y-or-n-p (format "Do it to `%s'? " buf))
(goto-char (point-min))
(keep-lines regexp)))))
And then you might want to think about whether you also want to save the modified buffer, etc.
For a more manually-controlled approach, you might use ibuffer:
M-x ibuffer RET
Mark some buffers
E (keep-lines "pattern") RET
Or, as per Drew's suggestion, (progn (goto-char (point-min)) (keep-lines "pattern"))
You can mark buffers using lots of different criteria in ibuffer. Browse the "Mark" menu, and see C-hm for details.

Emacs make ibuffer filter per frame

I use ibuffer as well as have opened several frames. The very frequent use-case of mine for ibuffer is to filter the list (C-x C-b) of buffers by name/filename. The catch is that filter works in the same way for all frame (it is predicted I believe). I'd want to have to apply filter for ibuffer listing per each frame I have.
E.g. in a first frame I'd like to see list of buffers filtered by a filename, in a second one filtered by mode.
Looking around ibuffer.el I have discovered an entity called ibuffer-filtering-qualifiers which holds an associative list of currently applied filters. I have tried played:
(let ((ibuffer-filtering-qualifiers '())) ;;; Imitate that there are no filters applied
(call-interactively 'ibuffer))
But this was not worked for me.
Could you please point me out how to achieve such desired behavior of ibuffer?
The function ibuffer-update uses the buffer-list, which includes all frames. Modifying ibuffer-update from using buffer-list to (frame-parameter (selected-frame) 'buffer-list) approaches the behavior you are seeking. An ibuffer filter based upon that same concept should also be possible.
In my opinion, the per frame buffer list (frame-parameter (selected-frame) 'buffer-list) is not the best method to associate buffers with frames because it gets modified too easily. The best method I have seen is frame-bufs by Al Parker:  https://github.com/alpaker/Frame-Bufs  This has been updated to work with a current version of Emacs, however, I am using a slightly modified version to suit my own needs.
So, the easiest method that I am aware to answer your question is to use a combination of frame-bufs by Al Parker, and use the frame-bufs-buffer-list within ibuffer-update. That gives you the best of both worlds. Here is an example that will give you a nice ibuffer with only the buffers associated with a given frame -- all other buffers will not be displayed. [FYI:  frame-bufs also sorts by mode or by file name.]
(require 'ibuffer)
(defalias 'ibuffer-update 'lawlist-ibuffer-update)
(defun lawlist-ibuffer-update (arg &optional silent)
"Regenerate the list of all buffers.
Prefix arg non-nil means to toggle whether buffers that match
`ibuffer-maybe-show-predicates' should be displayed.
If optional arg SILENT is non-nil, do not display progress messages."
(interactive "P")
(if arg
(setq ibuffer-display-maybe-show-predicates
(not ibuffer-display-maybe-show-predicates)))
(ibuffer-forward-line 0)
;; (let* ((bufs (buffer-list))
(let* ((bufs (frame-parameter (selected-frame) 'frame-bufs-buffer-list))
(blist (ibuffer-filter-buffers
(current-buffer)
(if (and
(cadr bufs)
(eq ibuffer-always-show-last-buffer
:nomini)
(minibufferp (cadr bufs)))
(cl-caddr bufs)
(cadr bufs))
(ibuffer-current-buffers-with-marks bufs)
ibuffer-display-maybe-show-predicates)))
(and (null blist)
(featurep 'ibuf-ext)
ibuffer-filtering-qualifiers
(message "No buffers! (note: filtering in effect)"))
(unless silent
(message "Updating buffer list..."))
(ibuffer-redisplay-engine blist arg)
(unless silent
(message "Updating buffer list...done")))
(if (eq ibuffer-shrink-to-minimum-size 'onewindow)
(ibuffer-shrink-to-fit t)
(when ibuffer-shrink-to-minimum-size
(ibuffer-shrink-to-fit)))
(ibuffer-forward-line 0)
;; I tried to update this automatically from the mode-line-process format,
;; but changing nil-ness of header-line-format while computing
;; mode-line-format is asking a bit too much it seems. --Stef
(setq header-line-format
(and ibuffer-use-header-line
ibuffer-filtering-qualifiers
ibuffer-header-line-format)))
The issue you have is not that the ibuffer works in the same way for all frames. The issue is that the ibuffer buffer is the same for all the frames (each frame has a separate window (with its cursor) showing your ibuffer unique instance).
A simple solution is to clone the ibuffer buffer (M-x clone-buffer). And that is: you have two different buffers and you can apply filters to them independently.

Dedicated misc buffer in Emacs (autocomplete, function info etc.)

I have been wondering for a very long time now: how to get a dedicated misc buffer in Emacs?
Auto-completion, function descriptions and perhaps documentation all can go there without ending up somewhere unexpected, but instead at a predefined location (a quarter of the screen perhaps?).
(I'm assuming you mean a dedicated window instead of a dedicated buffer.) If you keep a window open without doing any other window-splitting commands, help/repl buffers will automatically use it. You can change the size of the window as described in this question.
If you want to do be able to do normal window manipulation but have help windows be a certain size, I suggest you investigate temp-buffer-show-hook, a hook that is run when temporary buffers (such as help buffers) are shown. I haven't tried it, but it would probably be possible to set it to a function that arranges your window configuration in a particular way.
Here is what I do in One On One, to define a dedicated *Help* frame:
;; *Help* frame
(if 1on1-*Help*-frame-flag
(add-to-list
'special-display-buffer-names
(list "*Help*" '1on1-display-*Help*-frame
(list (cons 'background-color 1on1-help-frame-background)
(cons 'mouse-color 1on1-help-frame-mouse+cursor-color)
(cons 'cursor-color 1on1-help-frame-mouse+cursor-color)
'(height . 40))))
(setq special-display-buffer-names
(1on1-remove-if (lambda (elt) (equal "*Help*" (car elt)))
special-display-buffer-names)))
(defun 1on1-display-*Help*-frame (buf &optional args)
"Display *Help* buffer in its own frame.
`special-display-function' is used to do the actual displaying.
BUF and ARGS are the arguments to `special-display-function'."
(let ((old-ptr-shape (and (boundp 'x-pointer-shape) x-pointer-shape))
return-window)
(when (boundp 'x-pointer-xterm) (setq x-pointer-shape x-pointer-xterm))
(setq return-window (select-window (funcall special-display-function buf args)))
(raise-frame)
(setq x-pointer-shape old-ptr-shape)
return-window))
You don't need all of those details (pointer shapes etc.), but that gives you the idea. The main thing is to put *Help* on special-display-buffer-names. That's really all you need to do.
The 1on1-* variables used for the frame parameters here are pretty obvious. The *-remove-if function is a standard remove-if. The complete code is here: oneonone.el.

Emulate dabbrev-expand in hippie-expand, limiting to matching buffers

If I use dabbrev-expand for expansion, Emacs searches the current buffer, then other buffers with the same mode. This is handled by dabbrev-friend-buffer-function which by default is set to dabbrev--same-major-mode-p.
This works fine, but I'd like to use hippie-expand.
(setq hippie-expand-try-functions-list
'(try-expand-dabbrev
try-expand-dabbrev-all-buffers))
This pulls completions from all buffers, even the buffers that don't match my current major mode.
How can I use hippie-expand with dabbrev completions only coming from buffers using the same major-mode as the current buffer?
Quick and dirty solution: Copy the source code of the function try-expand-dabbrev-all-buffers to a new location, rename it (say) try-expand-dabbrev-all-buffers-same-mode, and replace the expression (buffer-list) with the expression:
(remove-if-not (lambda (x) (eq major-mode (with-current-buffer x major-mode)))
(buffer-list))
(You'll need to (require 'cl) to get remove-if-not, or else re-implement it in terms of mapcar and delq.)
Then, of course, replace try-expand-dabbrev-all-buffers with try-expand-dabbrev-all-buffers-same-mode in hippie-expand-try-functions-list.
You can get the source of try-expand-dabbrev-all-buffers using C-hf.
Based on Sean's excellent suggestion (and assuming you have the dash.el list utility library installed):
(autoload '--filter "dash" nil t)
;; only consider buffers in the same mode with try-expand-dabbrev-all-buffers
(defun try-expand-dabbrev-matching-buffers (old)
(let ((matching-buffers (--filter
(eq major-mode (with-current-buffer it major-mode))
(buffer-list))))
(flet ((buffer-list () matching-buffers))
(try-expand-dabbrev-all-buffers old))))

.emacs.desktop file destroyed by custom function

I use the Emacs desktop module to save my open buffers between sessions. However I found that this accumulates more buffers than I want, so I wrote a small function to clean up the buffer list immediately before saving to the desktop file. This works as expected, but for strange reasons the .emacs.desktop gets scrambled occasionally, i.e. it contains a part of another buffer at its start, then the intended contents and then the result of the other buffer. I don't have the slightest idea, why this happens. Here is an excerpt from my .emacs file:
(defun kill-old-buffers ()
"Kill buffers from end of buffer list (not used recently) until no more than 50 buffers are left. Remove temporary buffers first"
(interactive)
(let* (desktop-buffer (current-buffer))
(dolist (buffer (buffer-list))
(if (or (string-match "^\*" (buffer-name buffer)) (string-match "\.hpp$" (buffer-name buffer)))
(kill-buffer buffer)
)
)
(setq all-buffers (reverse (buffer-list)))
(while (> (safe-length all-buffers) 50)
(setq buffer (pop all-buffers))
(if (not (string-equal (buffer-name buffer) (buffer-name (current-buffer))))
(kill-buffer buffer)
)
)
(switch-to-buffer desktop-buffer)
))
;; Kill old rarely-used buffers before saving
(add-hook 'desktop-save-hook
'(lambda () (kill-old-buffers)))
Any help would be appreciated.
I'm not sure if your function is really the cause of your problem. If it should be the case, the wrong usage of the let* that scottfrazer pointed out might be the cause. But you don't even need that let* (and switch-to-buffer) at all, because
what you're trying to do is better done with Emacs' built in save-excursion, and
you aren't actually switching the buffer ever.
OTOH, you should have used let instead of the setqs in the lower half of your function, because setq will otherwise change a variable from an enclosing lexical scope. In this case you might very well have stomped over a buffer variable from the function that's executing the desktop-save-hook which is another potential cause of your problem.
But you don't need those lets either because you can do the second loop with another dolist. You can get rid of those first 50 buffers that you don't want to loop over with nthcdr.
Here's my improved version of kill-old-buffers:
(defun kill-old-buffers ()
"Kill buffers from end of buffer list (not used recently) until
no more than 50 buffers are left. Remove temporary buffers first."
(interactive)
(save-excursion
(dolist (buffer (buffer-list))
(if (or (string-match "^\*" (buffer-name buffer))
(string-match "\.hpp$" (buffer-name buffer)))
(kill-buffer buffer)))
(dolist (buffer (reverse (nthcdr 50 (buffer-list))))
(unless (eq buffer (current-buffer))
(kill-buffer buffer)))))
It may not fix all your problems, but for starters you need another set of parens around the variable in your let* statement
(let* ((desktop-buffer (current-buffer)))