How to regenerate latex fragments in org-mode? - emacs

I have latex fragments in org-mode that were generated with my previous emacs theme and now that I switched my theme, the old latex fragments still have my old background color instead of the new one. How can I clear them and regenerate them (linux)?

C-c C-x C-l is bound to org-toggle-latex-fragment. Do it once to get rid of the overlay and do it again to regenerate the overlay. Depending on where you are in the buffer and whether you invoke it with one C-u or two C-u or no C-u, it will do different things (affect the current latex fragment, all fragments in a subtree, or all fragments in the buffer). You should read the doc string of the funcion with
C-h C-f org-toggle-latex-fragment RET

I'm using Doom Emacs and my previews are cached in the following directory:
~/.emacs.d/.local/cache/org-latex/
I need to delete the folder to make sure the previews are regenerated with the new foreground color.

The latex fragments are stored in a folder named "ltximg" in the same folder as where the org file is located. To recreate the fragments, delete that folder, restart emacs and do org-toggle-latex-fragment again.

Below is a solution which will change the cache folder automatically when you call load-theme. It will update the images as well. To avoid unnecessary operations, this is done only in the buffers where org-toggle-latex-fragment has been called.
The LaTeX fragments are computed for the whole buffer once a new theme is loaded, but you can remove '(16) in the last function below and it won't happen that way. Instead, only the current section will be updated (with the latex images in the rest of the buffer simply removed).
Make sure to transfer your latex fragment options to my/org-latex-set-options. That's because keeping these options within org-mode-hook will not work if you use #+STARTUP: latexpreview.
(defun my/org-latex-set-options ()
(setq org-format-latex-options (plist-put org-format-latex-options :scale 1.5)))
(defvar my/org-latex-toggle-fragment-has-been-called nil
"Tracks if org-toggle-latex-fragment has ever been called (updated locally).")
(defadvice org-toggle-latex-fragment (before my/latex-fragments-advice activate)
"Keep Org LaTeX fragments in a directory with background color name."
(if (not my/org-latex-toggle-fragment-has-been-called) (my/org-latex-set-options))
(setq-local my/org-latex-toggle-fragment-has-been-called t)
(my/org-latex-set-directory-name-to-background))
(defadvice load-theme (after my/load-theme-advice-for-latex activate)
"Conditionally update Org LaTeX fragments for current background."
(if my/org-latex-toggle-fragment-has-been-called (my/org-latex-update-fragments-for-background)))
(defadvice disable-theme (after my/disable-theme-advice-for-latex activate)
"Conditionally update Org LaTeX fragments for current background."
(if my/org-latex-toggle-fragment-has-been-called (my/org-latex-update-fragments-for-background)))
(defun my/org-latex-set-directory-name-to-background ()
"Set Org LaTeX directory name to background color name: c_Red_Green_Blue."
(setq org-preview-latex-image-directory
(concat "ltximg/c"
(let ((color (color-values (alist-get 'background-color (frame-parameters)))))
(apply 'concat (mapcar (lambda (x) (concat "_" x)) (mapcar 'int-to-string color))))
"/")))
(defun my/org-latex-update-fragments-for-background ()
"Remove Org LaTeX fragment layout, switch directory for background, turn fragments back on."
;; removes latex overlays in the whole buffer
(org-remove-latex-fragment-image-overlays)
;; background directory switch
(my/org-latex-set-directory-name-to-background)
;; recreate overlay
;; Argument '(16) is same as prefix C-u C-u,
;; means create images in the whole buffer instead of just the current section.
;; For many new images this will take time.
(org-toggle-latex-fragment '(16)))

Related

Emacs disable mouse-1 in dired

In Emacs 23.2.1 in Dired mode the mouse-1 (left mouse button) performs visit file in other window. It also changes shape to a finger and highlights the filename when cursor hovers over the filename. How do I disable both visit file and filename highlighting ? I want mouse-1 to do its usual stuff: selecting text.
I can still select text if I start by clicking down in an area outside the filename or directory name. But I only want the filename marked, and not have a space in front included.
I just turn off mouse-1-click-follows-link by customizing it to nil. (You can also set it to a long time-limit value.)
Or if you want to do that only for Dired buffers, you can do this:
(add-hook 'dired-mode-hook
(lambda ()
(set (make-local-variable 'mouse-1-click-follows-link) nil)))
But it is typically better to name a function that you use on a hook (it's easier to remove it, for one thing):
(defun foo ()
(set (make-local-variable 'mouse-1-click-follows-link) nil)))
(add-hook 'dired-mode-hook 'foo)
If you have a recent version of Emacs, where setq-local is defined, then you can use just (setq-local mouse-1-click-follows-link nil) in the hook function, in place of (set (make-local-variable 'mouse-1-click-follows-link) nil)

How to copy path of a file in emacs helm-mode

I'm using helm-mode in emacs to open files. However, when I try to copy the path of a file (say /home/user1/Documents/file1.txt) through mouse left-click and hold to paste it in terminal, I get a message saying
<down-mouse-1> is undefined
I guess helm does not support mouse operations as described here, in which case how can I copy path of a file from emacs (in helm-mode) to paste it in terminal
The answer given in this other thread may seem more straightforward.
In short: with the file selected in the minibuffer use C-u C-c C-k. This invokes helm-kill-selection-and-quit. The file's full path is copied to the kill ring and can be pasted in Emacs or elsewhere.
I guess you want to copy from Minibuffer to your system clipboard. Minibuffer keybinding isn't different from other buffers. If in other buffers you use M-w to copy the region, it should also work in Minibuffer. Note that if you niled x-select-enable-clipboard you need to enable it first. I have the following functions in my init.el
(defun copy-to-clipboard()
(interactive)
(setq x-select-enable-clipboard t)
(kill-ring-save (region-beginning) (region-end))
(setq x-select-enable-clipboard nil))
and
(defun paste-from-clipboard ()
(interactive)
(setq x-select-enable-clipboard t)
(yank)
(setq x-select-enable-clipboard nil))
Unfortunately you can't use your mouse to select the texts (ie. to make a region) in helm-mode; you need to set-mark-command (by default C-SPC or C-#) and move your point (ie. cursor). Or just hold the shift and move the point like most other text editors. There is also a mark-word command (by default M-#) that expands the region word by word.
I also recorded an asciinema (because they're fun 🙂) that you can watch it here

How to create a new, unnamed file in emacs?

This is a very simple request: I want to create a new, blank file without giving it a name (yet). I can use the scratch buffer but there's only one. I can C-x C-f and open a new file, but then I have to give it a name and path. If I'm just writing notes to myself or sketching out ideas, I don't want to have to give it a name. How do I create a new, empty, unnamed file?
You can create a new buffer with: C-xb and type in the buffer name and it will create a new buffer with a name that you choose.
If you want to save that buffer, just hit C-x w to create the file with its contents to a desired location.
My recommendation is that you give org-mode a try. It will do what want among a million other things.
Give it a name. Just don't save it.
Not what you wanted to hear, but this is the Emacs way.
Use C-x C-f, giving the (to-be-file-visiting) buffer a name. Edit the text. Do not use C-x C-s to save the buffer to the file (i.e., to disk).
Note that you can first put yourself in a directory whose contents you don't care about, so that if you accidentally do save the buffer there then you can easily find, recognize and toss the file. To change directories, you can use M-x cd. Or just do it by editing the directory part when you first use C-x C-f. Or use C-x d to put yourself in a Dired buffer for the directory.
If you don't want to take the chance of accidentally hitting C-x C-s and thus saving your edits, then use C-x b instead of C-x C-f. You are (even here) prompted for the buffer name. Giving it a new name (not the name of an existing buffer) creates a new buffer. In this case, if you use C-x C-s then Emacs prompts you for the file location to save the buffer in.
Why would you want to use C-x C-f instead of C-x b, if you might not want to save the buffer? Providing a file extension in the file name you give automatically puts the buffer in the proper major mode (typically). Otherwise (for C-x b) you need to put the buffer in the mode you want.
Buffer *scratch* is by default in Lisp-Interaction mode, which is similar to Emacs-Lisp mode (but not the same).
In every other text editor or word processor the intuition is to create a “new file” or a “new buffer”, not to switch to idiosyncratic *scratch* buffer. For example, you write quick notes or thoughts in several different buffers to keep trace of them—later you decide if you throw them away or save them. Or you manipulate a snippet of text or code, but you don't want to change the original buffer, so you just copypaste it to a new temporary buffer.
*scratch* is set to Lisp Interaction mode, but if I want to quickly evaluate some Elisp code, I could eval it running eval-expression (Alt+:) or in Elisp interpreter IELM (Alt+x Enter ielm). Also, if you close *scratch* buffer, it doesn't ask you to save it, so you can accidentally lose all your work. Drew's traditional solution seems too sub-optimal. And I don't buy the argument that “this is how you do it in Emacs”. Emacs is a customizable editor, so you can and should create whatever workflow is comfortable for you.
That's how ErgoEmacs solves it, buy creating a new-empty-buffer command. You can implemented like this:
(defun new-empty-buffer ()
"Opens a new empty buffer."
(interactive)
(switch-to-buffer (generate-new-buffer "untitled"))
(funcall initial-major-mode)
(put 'buffer-offer-save 'permanent-local t)
(setq buffer-offer-save t))
The variable buffer-offer-save resets every time you change a major mode, therefore you need to annotate it with permanent-local. It also prompts only when you exit Emacs. I think it is intuitive for it to also ask, when you close a modified untitled buffer, therefore see my solution on how to upgrade kill-buffer to prompt before closing a modified buffer.
ErgoEmacs revamps the default keybindings completely and has new-empty-buffer bound to Ctrl+N, like in almost all software. Change variable initial-major-mode if you want the new buffer to have another mode on start.
See also:
Emacs: Problems of the Scratch Buffer # Xah Lee
Emacs: New Empty Buffer # Xah Lee
You can try the following snippet, just add it to your .emacs
(defun new-file-tmp()
"Create a new empty file."
(interactive)
(let ((buf (generate-new-buffer "untitled")))
(switch-to-buffer buf)
(put 'buffer-offer-save 'permanent-local t)
(setq buffer-offer-save t)))
(defun tool-bar-local-item-pre (icon def key map after_item &rest props)
"Add an item to the tool bar in map MAP.
ICON names the image, DEF is the key definition and KEY is a symbol
for the fake function key in the menu keymap. Remaining arguments
PROPS are additional items to add to the menu item specification. See
Info node ‘(elisp)Tool Bar’. The item is added after AFTER_ITEM.
ICON is the base name of a file containing the image to use. The
function will first try to use low-color/ICON.xpm if ‘display-color-cells’
is less or equal to 256, then ICON.xpm, then ICON.pbm, and finally
ICON.xbm, using ‘find-image’."
(let* ((image-exp (tool-bar--image-expression icon)))
(define-key-after map (vector key)
`(menu-item ,(symbol-name key) ,def :image ,image-exp ,#props) after_item)
(force-mode-line-update)))
(when (boundp 'tool-bar-map)
(tool-bar-local-item-pre "new" 'new-file-tmp 'new-file-tmp tool-bar-map
'new-file :label "" :help "New untitled File")
(define-key tool-bar-map (vector 'new-file) nil)
;; comment the above line if you want to keep the button for the default behavior
)
(global-set-key (kbd "C-n") 'new-file-tmp)
It defines a new command which creates new empty buffers
Then the code binds it to the top left button of the toolbar, if you are in gui mode, and to the Control-n shortcut.
You can check the post on my site about it.
disclaimer: it's my site
A buffer and a file are not the same thing.
terminology
Regarding buffers,
The text you are editing in Emacs resides in an object called a
buffer. Each time you visit a file, a buffer is used to hold the file's text.
Regarding files,
The operating system stores data permanently in named files, so most
of the text you edit with Emacs comes from a file and is ultimately
stored in a file.
Buffers and files are related through visiting,
Visiting a file means reading its contents into an Emacs buffer so you
can edit them. Emacs makes a new buffer for each file that you visit.
answering the question
Unless I'm mistaken, technically speaking, your question, as written, can't be answered. Pedantically speaking, there's no such thing as (or little practical use for) an unnamed file. A file is a handle for something stored on disk. If you have no handle, then why make a file?
The question can then be interpreted as having two possible meanings1.
1. Making a new buffer (without regard to name)
A new buffer must have a name. An unsaved buffer can be saved to file with a given name using write-file (C-x C-w). You will be prompted for a path/name. Once written, the buffer update to be visiting the file you just wrote.
Since the name of the buffer doesn't matter (until you write it to file), here's a function which creates buffers named *scratch1*, *scratch2*, ... .
(defun create-scratch-buffer ()
"Create a new numbered scratch buffer.
Taken from URL `https://stackoverflow.com/a/21058075/5065796' "
(interactive)
(let ((n 0)
bufname)
(while (progn
(setq bufname (concat "*scratch"
(if (= n 0) "" (int-to-string n))
"*"))
(setq n (1+ n))
(get-buffer bufname)))
(switch-to-buffer (get-buffer-create bufname))
(org-mode)
(if (= n 1) initial-major-mode)))
2. Making a new file (without regard to name)
As hinted at in the new buffer solution, new files can be created with write-file.
When called interactively (M-x make-random-file), this function prompts for a directory. It then writes an empty file named something random like tmp-17388716387615.txt.
(defun make-random-file (dir)
"Make a random file in DIR."
(interactive "DDir: ")
(let* ((filename (concat "tmp-" (int-to-string (random t)) ".txt"))
(filepath (concat dir filename)))
(write-region "" nil filepath nil 'silent nil 'excl)))
As always, if the code above doesn't make sense, look at the documentation for functions and variables with C-h f and C-h v, respectively. And/or read the Introduction to Programming in Emacs Lisp.
1 It was the second meaning which I was searching for solutions to when I found this as the top search engine hit. Apologies if this was a bit obtuse. Figured it was better to share my solutions with y'all than not. :)

emacs - save current buffer list to a text file

Quite often I need to get a simple text copy of my currently opened files. The reasons are usually:
I want to send the list to a colleague
I want to document whatever I am working on (usually in an org document)
I want to act on one of my currently opened files, on the shell. I need to copy-paste the pathname for that.
The fact is that the usual buffer-menu or list-buffers provide a convenient menu to navigate the opened buffers, but are very inconvenient to copy-paste to the the terminal the names of the opened files, or to perform any of the actions mentioned above. For example: I can not double-click in a line to select the full path-name, and I can not use the kill/yank emacs sequence to copy around the path-name.
Summary: I would like a way to export to a text file (or to a new buffer) the list of opened files, without other data; no file size, mode, or any other emacs metadata.
Is there a command for that? An extra package I can install?
EDIT
Adding solution by Trey Jackson, modified to provide some feedback of what has been done:
(defun copy-open-files ()
"Add paths to all open files to kill ring"
(interactive)
(kill-new (mapconcat 'identity
(delq nil (mapcar 'buffer-file-name (buffer-list)))
"\n"))
(message "List of files copied to kill ring"))
This command will do the job for you:
(defun copy-open-files ()
"Add paths to all open files to kill ring"
(interactive)
(kill-new (mapconcat 'identity
(delq nil (mapcar 'buffer-file-name (buffer-list)))
"\n")))
You can change the mode of your *Buffer List* buffer. By default, it will be in mode Buffer Menu, but changing it to text-mode or fundamental-mode will remove all the special behavior allowing you to cut and paste from it just like a regular buffer. The metadata can easily be chopped off with delete-rectangle.
Alternatively, you can access the buffer list programmatically with elisp:
(dolist (buffer (buffer-list))
(when (buffer-file-name buffer)
(insert (buffer-file-name buffer) "\n")))
You certainly should be able to copy and yank from the buffer list.
e.g. copy everything with C-xhM-w and then yank into a new buffer for editing.

Emacs ido start with open buffers first in cycle list

When switching buffers with emacs ido mode enabled, a list of completions are displayed in the minibuffer. It appears there is a "feature" that buffers that are already open are put to the end of the list. I, however, often open the same buffer in multiple panes.
Is there a way to either turn this "feature" off, or alternatively do the opposite: have the buffers that are already open be at the front of the completion list?
The main point of ido mode is that you don't use arrows to navigate between buffers in the minibuffer. Instead you type the part of the buffer's name. In this case it doesn't matter where the buffer is in the list.
This is not possible unless you want to wade deep in ido's intestines.
As eGlyph already said: You're likely using ido wrongly (and there's also C-s for <right> and C-r for <left>; no need for arrow keys).
But you can define command for choosing among the already shown buffers (here only from the current frame, if you want all shown buffers you have to collect the windows first via `frame-list):
(defun choose-from-shown-buffers ()
(interactive)
(let ((buffers (mapcar (lambda (window)
(buffer-name (window-buffer window)))
(window-list))))
(pop-to-buffer (ido-completing-read "Buffer: " buffers))))