How to create a right-click context shell shortcut “edit with Emacs”?

扶醉桌前 提交于 2019-12-02 17:10:16

Here's what I have - similar to some other answer. Create a new text file somewhere called emacs-conextmenu.reg (or anything-you-want.reg) and paste the following in:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell]
[HKEY_CLASSES_ROOT\*\shell\openwemacs]
@="&Edit with Emacs"
[HKEY_CLASSES_ROOT\*\shell\openwemacs\command]
@="Absolute\\Path\\to\\your\\emacs\\bin\\emacsclientw.exe -n \"%1\""
[HKEY_CLASSES_ROOT\Directory\shell\openwemacs]
@="Edit &with Emacs"
[HKEY_CLASSES_ROOT\Directory\shell\openwemacs\command]
@="Absolute\\Path\\to\\your\\emacs\\bin\\emacsclientw.exe -n \"%1\""

Change the path to your emacs installation path; remember to escape the "\" (whenever you have \, change that to \\).

Now all you need to do is double-click this *.reg file in the explorer and you shall have a context menu entry for emacs for any file and any directory (if you are a dired fan!).

Note that for this to work, emacs has to be started and emacs-server also has to be started (M-x server-start). I would suggest starting emacs with Windows and put (server-start) in your .emacs file.

As a bonus, the following snippet for autohotkey (http://www.autohotkey.com/) will start the file selected in emacs when you press ctrl-shift-enter in windows explorer. This might be more handy if you edit lots of files in emacs but does not necessarily want to navigate to the file in emacs itself.

#IfWinActive ahk_class CabinetWClass 
^+Enter::
  GetText(tmpvar)
  If (tmpvar != "")
     Run, d:/path/to/your/emacs/bin/dir/emacsclientw.exe -n "%tmpvar%"
Return
Return

Just like polyglot's answer, but no need to start a server or any of that mess.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell]
[HKEY_CLASSES_ROOT\*\shell\openwemacs]
@="&Edit with Emacs"
[HKEY_CLASSES_ROOT\*\shell\openwemacs\command]
@="C:\\Program Files (x86)\\Emacs\\bin\\emacsclientw.exe --alternate-editor=\"C:\\Program Files (x86)\\Emacs\\bin\\runemacs.exe\" -n \"%1\""
[HKEY_CLASSES_ROOT\Directory\shell\openwemacs]
@="Edit &with Emacs"
[HKEY_CLASSES_ROOT\Directory\shell\openwemacs\command]
@="C:\\Program Files (x86)\\Emacs\\bin\\emacsclientw.exe --alternate-editor=\"C:\\Program Files (x86)\\Emacs\\bin\\runemacs.exe\" -n \"%1\""

If you're not up for spelunking the registry, you can do this from the Windows Explorer (instructions for any editor, not just Emacs):

  1. Open the Windows Explorer and select the Tools\Folder Options... menu item.

  2. Click on the File Types tab and select the extension for the type of file you want to associate with your editor. For this example, I'll use the TXT extension.

  3. Click the Advanced button in the details frame to bring up the Edit File Type dialog.

  4. You can either change the current open action, or specify a new action like Open with MyEditor.

    If you choose to edit, click the Edit... button and in the Application used to peform action textbox, put the full path to your editor, followed by "%1". For example, C:\SciTe\wscite\SciTE.exe "%1".

    If you want to create a new action, click the New... button, give the action a name, and give the full path to your editor, followed by "%1".

  5. If you want to make an action the default for that file type, select the action, then click the Set Default button.

  6. OK out of all dialogs.

Another option would be to put a shortcut to your editor's executable in your Send To folder, %USERSPROFILE%\SendTo. I usually create a new folder in here called "Editors" and put shortcuts to the various editors I use.

With a little addition, also opening the current directory in emacs by clicking on the background becomes possible.

<<<Code as posted by polyglot>>>

[HKEY_CLASSES_ROOT\Directory\Background\shell\openwemacs]
@="Open &with Emacs"
[HKEY_CLASSES_ROOT\Directory\Background\shell\openwemacs\command]
@="C:\\Program Files\\emacs-24.2\\bin\\runemacs.exe \"%V\""

Here %V is the current directory. Using %1 doesn't work in this case.

This site explains how to do it with another app. Just change the path and you should be all set.

Create this key/value:

[HKEY_CLASSES_ROOT\*\shell\Edit with AppName\command]
@=”\”C:\\Program Files\\Notepad2\\Notepad2.exe\” \”%1\”"

Here's another reference, which is a little easier to follow.

Check out an Emacs distribution with Windows integration: http://ourcomments.org/Emacs/EmacsW32.html

Its installer creates an Explorer menu entry which does what you want.

Here's is another way to do the same thing. Works in WinXP and Vista.

Add this to your registery:

edit-with-emacs.reg

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\Emacs]
@="Edit With &Emacs"
[HKEY_CLASSES_ROOT\*\shell\Emacs\command]
@="Wscript.exe C:\\emacs\\emacs-22.3\\bin\\launch-emacs-client.vbs \"%1\""

Place this file in your emacs bin directory:

launch-emacs-client.vbs

Set objShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

If WScript.Arguments.Count = 1 Then

  strComputer = "."

  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

  Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

  Dim isRunning
  isRunning = False

  For Each objItem in colItems
    If InStr(objItem.CommandLine, "emacs.exe") Then
      isRunning = True
    End If
  Next

  If isRunning Then
    objShell.Run(fso.GetParentFolderName(WScript.ScriptFullName) & "/emacsclientw.exe -n """ & WScript.Arguments(0) & """")
  Else
    objShell.Run(fso.GetParentFolderName(WScript.ScriptFullName) & "/runemacs.exe """ & WScript.Arguments(0) & """")
  End If

Else
  objShell.Run(fso.GetParentFolderName(WScript.ScriptFullName) & "/runemacs.exe")
End If

Note: the W32 installer runs a similar script on installation.

I want to add to polyglot's answer above -- the AutoHotkey shortcut he mentions is very handy, but the code is incomplete: the GetText() function was written by somebody on the AutoHotkey forum (http://www.autohotkey.com/forum/viewtopic.php?p=279576#279576), and it is:

GetText(ByRef MyText = "")
{
   SavedClip := ClipboardAll
   Clipboard =
   Send ^c
   ClipWait 0.1
   If ERRORLEVEL
   {
      Clipboard := SavedClip
      MyText =
      ERRORLEVEL := 1
      Return
   }
   MyText := Clipboard
   Clipboard := SavedClip
   Return MyText
}

;to test: 
#k::
GetText(FileName)
msgbox clipboard=%FileName%
return

Note that ClipWait delay might need to be modified: I had to set it to 2!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!