简体   繁体   中英

Open emacs application in a specific mode for given path

I was using emacs on command line and I was using following command to open current git directory in magit mode:

emacs -nw -f magit-status --eval "(call-interactively #'delete-other-windows)"

I switched to using emacs app instead of opening it in command line. I am using following command alias:

alias emacs='open -a /Applications/Emacs.app $1'

Because of this magit command doesn't work anymore. Is there any way to achieve the same functionality with Emacs app?

Also, tried this function as suggested in comments:

function magit() {
 open -a /Applications/Emacs.app --args -f magit-status $1
}

Regards,
Pawan.

When starting a new Emacs session, the function

function magit() {
 open -a Emacs --args --file "$1" -f magit-status
}

will do what you want. That is, load a file into a buffer (via find-file ) and run the function magit-status on that buffer. Note, that order counts on the Emacs command line. While open -a Emacs myfile --args -f magit-status and open -a Emacs --args -f magit-status myfile are correct from the perspective of open , they aren't correct from Emacs's perspective. ( magit-status is executed on nothing, and then myfile is opened. Not what you want.)

If you want to do this with a currently running Emacs session, you can't. The clue is in open 's manpage.

 --args All remaining arguments are passed to the opened application in the argv parameter to main(). These argu- ments are not opened or interpreted by the open tool.

main() has already executed, so you can't pass anymore parameters to it, so --args is effectively ignored. So, you'll have to get creative with the Emacs server and emacsclient .

If you have an open Emacs session with an associated server (via server-start for instance), you can do the following to "load and execute-on" a file

# my emacs will load *.log files in `fundamental-mode`
# this will load them in `text-mode`

$ emacsclient -e '(find-file "/tmp/foo.log")' -e '(text-mode)'

# or

$ emacsclient -e '(progn (find-file "/tmp/foo.log") (text-mode))'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM