Emacs disable mouse-1 in dired - emacs

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)

Related

How to regenerate latex fragments in org-mode?

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

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

Executing shell command on emacs file opening

I'm currently using the terminal Terminator,
and i got a bash function
set_title() { printf '\e]2;%s\a' "$*"; }
wich permit me to set the terminator window title
So I would want to know if it's possible to execute this specific shell command (like this):
set_title ##filename
on each opening (or re-opening) of the said file in emacs?
(btw english is not my native language, please be indulgent!)
If you mean that you're running the non-GUI Emacs inside bash inside Terminator and you want Terminator's title to reflect the current file in Emacs, then the fact that bash sits in between is of no use to you: Emacs is in control here.
But you can define an Elisp function that will do the same job as your set_title:
(defun my-set-title (title)
(send-string-to-terminal (format "\e]2;%s\a" title)))
And then you could use it via find-file-hook:
(add-hook 'find-file-hook (lambda () (my-set-title buffer-file-name)))
Note that this will set the terminal's title to the last file Emacs visits, so if you switch back to a previous file-buffer via C-x C-b, the title won't be updated to reflect the current buffer's file name. If you want to do that, you'd need something more like:
(defvar my-last-file nil)
(defun my-keep-file-title ()
(when (and buffer-file-name
(not (equal buffer-file-name my-last-file)))
(setq my-last-file buffer-file-name)
(my-set-title buffer-file-name)))
(add-hook 'post-command-hook #'my-keep-file-title)
As suggested by #Dan, you can do
(add-hook find-file-hook
(lambda ()
(when (string= buffer-file-name "my-file")
(shell-command "printf ...."))))
to call printf when you open "my-file".
However, if what you want is to set the frame title (emacs calls "frame" what window managers call "window"),
you should be setting frame-title-format, e.g.:
(setq frame-title-format
'(buffer-file-name "%b - %f" ; File buffer
(dired-directory dired-directory ; Dired buffer
(revert-buffer-function "%b" ; Buffer Menu
("%b - Dir: " default-directory)))) ; Plain buffer
icon-title-format "%b")

Emacs org-mode: textual reference to a file:line

I am using org-mode in Emacs to document my development activities. One of the tasks which I must continuously do by hand is to describe areas of code. Emacs has a very nice Bookmark List: create a bookmark with CTRL-x r m, list them with CTRL-x r l. This is very useful, but is not quite what I need.
Org-mode has the concept of link, and the command org-store-link will record a link to the current position in any file, which can be pasted to the org-file. The problem with this is two-fold:
It is stored as an org-link, and the linked position is not directly visible (just the description).
It is stored in the format file/search, which is not what I want.
I need to have the bookmark in textual form, so that I can copy paste it into org-mode, end edit it if needed, with a simple format like this:
absolute-file-path:line
And this must be obtained from the current point position. The workflow would be as simple as:
Go to the position which I want to record
Call a function: position-to-kill-ring (I would bind this to a keyboard shortcut)
Go to the org-mode buffer.
Yank the position.
Edit if needed (sometimes I need to change absolute paths by relative paths, since my code is in a different location in different machines)
Unfortunately my lisp is non-existent, so I do not know how to do this. Is there a simple solution to my problem?
(defun position-to-kill-ring ()
"Copy to the kill ring a string in the format \"file-name:line-number\"
for the current buffer's file name, and the line number at point."
(interactive)
(kill-new
(format "%s:%d" (buffer-file-name) (save-restriction
(widen) (line-number-at-pos)))))
You want to use the org-create-file-search-functions and org-execute-file-search-functions hooks.
For example, if you need the search you describe for text-mode files, use this:
(add-hook 'org-create-file-search-functions
'(lambda ()
(when (eq major-mode 'text-mode)
(number-to-string (line-number-at-pos)))))
(add-hook 'org-execute-file-search-functions
'(lambda (search-string)
(when (eq major-mode 'text-mode)
(goto-line (string-to-number search-string)))))
Then M-x org-store-link RET will do the right thing (store a line number as the search string) and C-c C-o (i.e. M-x org-open-at-point RET) will open the file and go to this line number.
You can of course check for other modes and/or conditions.
An elisp beginner myself I though of it as a good exercise et voila:
Edit: Rewrote it using the format methode, but I still think not storing it to the kill-ring is less intrusive in my workflow (don't know about you). Also I have added the capability to add column position.
(defvar current-file-reference "" "Global variable to store the current file reference")
(defun store-file-line-and-col ()
"Stores the current file, line and column point is at in a string in format \"file-name:line-number-column-number\". Insert the string using \"insert-file-reference\"."
(interactive)
(setq current-file-reference (format "%s:%d:%d" (buffer-file-name) (line-number-at-pos) (current-column))))
(defun store-file-and-line ()
"Stores the current file and line oint is at in a string in format \"file-name:line-number\". Insert the string using \"insert-file-reference\"."
(interactive)
(setq current-file-reference (format "%s:%d" (buffer-file-name) (line-number-at-pos))))
(defun insert-file-reference ()
"Inserts the value stored for current-file-reference at point."
(interactive)
(if (string= "" current-file-reference)
(message "No current file/line/column set!")
(insert current-file-reference)))
Not tested extensively but working for me. Just hit store-file-and-line or store-file-line-and-col to store current location and insert-file-reference to insert the stored value at point.
BTW, if you want something better than FILE:LINE, you can try to use add-log-current-defun (in add-log.el) which should return the name of the current function.
;; Insert a org link to the function in the next window
(defun insert-org-link-to-func ()
(interactive)
(insert (with-current-buffer (window-buffer (next-window))
(org-make-link-string
(concat "file:" (buffer-file-name)
"::" (number-to-string (line-number-at-pos)))
(which-function)
))))
This func generates link with the function name as the description.
Open two windows, one is the org file and the other is src code.
Then M-x insert-org-link-to-func RET

Pasting text into emacs on Macintosh

I'm on a Macintosh and am using "terminal" for my shell. When I copy text from any window (via mouse drag then right mouse button menu -> copy) and then I paste the text (right mouse button -> paste) into a terminal with emacs running, it doesn't act as a paste. Instead, it is just like entering or typing in text. The problem occurs when the text is indented. Emacs does its auto-indentation on top of that so I get a cascading staircase-like look of text. I just want it to be a true "paste" so that whatever was copied shows up exactly as it was. Any ideas on how to change something to get this to work?
Try this:
(defun pt-pbpaste ()
"Paste data from pasteboard."
(interactive)
(shell-command-on-region
(point)
(if mark-active (mark) (point))
"pbpaste" nil t))
(defun pt-pbcopy ()
"Copy region to pasteboard."
(interactive)
(print (mark))
(when mark-active
(shell-command-on-region
(point) (mark) "pbcopy")
(kill-buffer "*Shell Command Output*")))
(global-set-key [?\C-x ?\C-y] 'pt-pbpaste)
(global-set-key [?\C-x ?\M-w] 'pt-pbcopy)
Use C-x C-y to paste and C-x M-w to copy.
For a quick and dirty solution which doesn't require configuring custom commands, you can run shell-command with a prefix argument to insert the results of calling pbpaste into the current buffer.
Thus:
C-u M-! pbpaste <RET>