Set Cygwin emacs as the default opener for Windows Explorer

Source: Internet
Author: User
Tags tmp file touch command

Since I often use Emacs in Cygwin in my study and work on weekdays, it is natural to think of emacsclient as the default open program in Windows Explorer for the specified file type. This allows you to simply double-click the file and edit it in Emacs, eliminating the hassle of manually entering the path and file name under the Cygwin command line, or replacing Emacs dired mode with a quick and easy Windows Explorer. There are two issues that need to be addressed to implement such a feature:

    1. Conversion from the Windows file path format to the Cygwin path format.
    2. For XP operating systems, the Chinese file name is GBK encoded, while Cygwin uses UTF-8 encoding. To do this, you need to encode the file path (containing the Chinese).

My workaround is to create a Windows batch file Emacsclient.bat that associates the default opener for the specified file type. This batch file gets the file path name that was double-clicked in Windows Explorer and is stored in a temporary file with UTF-8 encoding. The path string is then extracted from the file and executed as a parameter call to the emacsclient. The contents of the batch file Emacsclient.bat are as follows:

@echo Offtouch "/cygdrive/c/.emacsclient_gbk.tmp" Cygpath%1 > c:\.emacsclient_gbk.tmpiconv-f gbk-t UTF-8-O "/cygdri Ve/c/.emacsclient_utf8.tmp ""/cygdrive/c/.emacsclient_gbk.tmp "for/f" usebackq delims=: "%%f in (" C:\.emacsclient_ Utf8.tmp ") do Emacsclient-w32.exe"%%f "@echo on

which

Touch "/cygdrive/c/.emacsclient_gbk.tmp"

Call Cygwin's touch command to generate a temporary file. Emacsclient_gbk.tmp, the path name of the file to be opened for GBK encoding. It is important to note that because touch is the Cygwin command, the path name of the temporary file as its parameter should be in Cygwin format. Then, call the Cygpath command to redirect the file path name you want to open (passing in the%1 parameter to Emacsclient.bat) as the standard output to c:\.emacsclient_gbk.tmp. It is important to note that because the standard output redirection at this time is done in a Windows batch file, the path to the temporary file. Emacsclient_gbk.tmp should be in Windows format. It is worth mentioning that the temporary file before the first call to Emacsclient.bat. Emacsclient_gbk.tmp has not been generated, and the Windows Batch command does not support standard output redirection to a nonexistent file. Therefore, it is necessary to call the touch command to generate the temporary file before this. Call the Iconv command again to encode the UTF-8 encoded path name into the. emacsclient_utf8.tmp file.

Iconv-f gbk-t UTF-8-o "/cygdrive/c/.emacsclient_utf8.tmp" "/cygdrive/c/.emacsclient_gbk.tmp"

Finally, extract the file path name from the. emacsclient_utf8.tmp file and leave it to the EMACSCLIENT-W32 program to open it.

Set Cygwin emacs as the default opener for Windows Explorer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.