Emacs conceived and open

I want to configure emacs to use external applications when I open image files from standby.

On the other hand, I also want to use inline images in emacs buffers.

To open files in external applications, I use the openwith.el package http://www.emacswiki.org/emacs/OpenWith

The problem with openwith minor mode is that it is global, and when it is enabled with a hook in standby mode

(add-hook 'dired-mode-hook (lambda () (setq truncate-lines t) (openwith-mode t) )) 

it works everywhere, and all embedded images in emacs buffers open in external applications.

I tried to change

 :global t 

to

 :global nil 

in openwith.el , but it somehow completely disables openwith.

So my question is: how do I tell emacs to use openwith minor mode only with a buffer with a buffer and nowhere else?

Thanks.

+6
source share
3 answers

The openwith-mode method works a little strange: it really assumes that you use it either globally or not at all. However, you want to use it locally, namely only from the buffer with the buffer.

This cannot be done very easily, but here is the way.

Open the openwith-mode source file, openwith.el. Then scroll all the way down until you reach the definition of the actual secondary mode. Then comment out this definition by putting a semicolon at the beginning of each line:

 ;;;###autoload ; (define-minor-mode openwith-mode ; "Automatically open files with external programs." ; :lighter "" ; :global t ; (if openwith-mode ; (progn ; ;; register `openwith-file-handler' for all files ; (put 'openwith-file-handler 'safe-magic t) ; (put 'openwith-file-handler 'operations '(insert-file-contents)) ; (add-to-list 'file-name-handler-alist '("" . openwith-file-handler))) ; (setq file-name-handler-alist ; (delete '("" . openwith-file-handler) file-name-handler-alist)))) 

Then, under this code (but before (provide 'openwith) ), paste the following code:

 (defvar openwith-mode nil) (mapc (lambda (function) (ad-add-advice function '(dired-openwith nil t (advice . (lambda () (let ((openwith-mode t)) ad-do-it)))) 'around 0)) '(dired-find-alternate-file dired-find-file dired-find-file-other-window dired-mouse-find-file-other-window dired-view-file)) (put 'openwith-file-handler 'safe-magic t) (put 'openwith-file-handler 'operations '(insert-file-contents)) (add-to-list 'file-name-handler-alist '("" . openwith-file-handler)) 

This code does a few things.

First, it defines a variable called openwith-mode. This variable is used inside one of the openwith-mode functions, which decides whether to use an external application or not. Typically, such a variable is automatically provided by Emacs when you define a minor mode, however, since we just commented on the definition of the current minor mode above, we will explicitly introduce this variable here.

The purpose of the variable is to act as a kind of switch through which we can control whether the image file should be embedded or transferred to an external viewer.

Next we have the expression (mapc ...) . What we are doing here is sorting out a list of five functions:

  • Dired-find-alternate file
  • Dired-find file
  • Dired-find files in another window
  • Dired mouse find files another window
  • Dired view file

which are functions for opening a file. A small code is added to each of these functions in the advising method: in which (ad-add-advice...) variable openwith-mode to t is set when one of these five functions is called. Outside the function call, the variable remains set to nil .

This leads to the fact that whenever you use one of the launched functions to open a file, the openwith-mode function responsible for calling external applications sees a variable set to t and immediately tries to open an external application if it knows one . The reason we should jump over such hoops is because the same openwith-mode function is also called whenever you open the image file with Cx Cf - this is the same as openwith-mode.

(NB: unfortunately, we cannot just use the current main mode as a switch, because this will be the main mode of the new buffer that will be created to open the file. It is always fundamental-mode .)

Finally, the last three lines are simply copied and pasted from the definition of the secondary mode, which we have already commented on. They say that the function that I have already mentioned a lot, and which is responsible for calling external applications - called open-with-filehandler - is the so-called file handler. It really does nothing special to actually access the file, so we set safe-magic for this function to t . In addition, we declare that the insert-file-contents operation is processed by our function in a non-trivial way. (See here for more information on these properties.)

And at the very end, we actually install the file handler.


It is important . Documentation in openwith mode recommends putting the following two lines in your .emacs file:

 (require 'openwith) (openwith-mode t) 

Now that there is no smaller openwith-mode (after we commented on its definition), make sure you delete the second line, for example. commenting on this as well:

 ;; (openwith-mode t) 

After restarting Emacs, if you open the image file, it should open in an external application; if you open it through Cx Cf , it will be inserted into the buffer.

+4
source

I do not use openwith . I use the function defined in

http://ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html

+3
source

One way could be to install emacsclient as your preferred program for file types that you like in openwith-associations in openwith.el, and start by using emacsclient.

You can start a new emacs session every time, but that sounds uglier than the previous sentence.

0
source

Source: https://habr.com/ru/post/919082/


All Articles