Xah Lee, ,
In emacs, there's a function browse-url. It sends a URL to a web browser. You can use it like this:
(browse-url "http://xahlee.org/")
The function browse-url is defined in 〔browse-url.el〕, which is bundled with emacs. It is used by for example html-mode. In that mode, you can press 【Ctrl+c Ctrl+v】 and it'll call browse-url-of-buffer, and view the current HTML file in a browser.
The browse-url function is very useful in your own lisp program. You can define a function with a key, so that pressing a button or clicking on URL lets you view that URL. For examples of how it is used, see: Emacs: Perl PHP Dictionary Wikipedia Google … Reference lookup.
Besides browse-url and browse-url-of-buffer, there are several others:
(See 〔browse-url.el〕 for more.) These will use a specific web browser. However, on my Windows machine, by default the browse-url-firefox doesn't work because emacs doesn't know its path.
To make it work, you'll need to add Firefox executable path in emacs's “exec-path” variable, like this:
(add-to-list 'exec-path "c:/Program Files (x86)/Mozilla Firefox/")
For example, i use multiple web browsers. Google Chrome for sites i have a account, especially Google sites. Firefox for all my web dev, usually with Flash and JavaScript off. Internet Explorer 9 for rest, and sometimes also Safari and Opera.
In emacs, sometimes i want to sent URL to a specific browser. Here's a function i defined:
(defun browse-url-of-buffer-with-firefox-2 () "Same as `browse-url-of-buffer' but using Firefox. You need to have Firefox's path in `exec-path'. e.g.: (add-to-list 'exec-path \"c:/Program Files (x86)/Mozilla Firefox/\")" (interactive) (let () (require 'browse-url) (browse-url-firefox (concat "file:///" buffer-file-name)) ))
You can also launch a browser by making a shell call. For example:
(defun browse-url-of-buffer-with-firefox () "Same as `browse-url-of-buffer' but using Firefox. You need Firefox's path in the path environment variable within emacs. e.g. (setenv \"PATH\" (concat \"C:/Program Files (x86)/Mozilla Firefox/\" \";\" (getenv \"PATH\") ) ) On Mac OS X, you don't need to. This command makes this shell call: 「open -a Firefox.app http://example.com/」" (interactive) (let () (cond ((string-equal system-type "windows-nt") ; Windows (shell-command (concat "firefox file://" buffer-file-name)) ) ((string-equal system-type "gnu/linux") (shell-command (concat "firefox file://" buffer-file-name)) ) ((string-equal system-type "darwin") ; Mac (shell-command (concat "open -a Firefox.app file://" buffer-file-name)) ) ) ))
You have to make sure that the path of firefox e.g. 〔C:/Program Files (x86)/Mozilla Firefox/〕 is in emacs's (getenv PATH) result. Here's a example of how to add it for Windows:
; example of setting env var named “path” by appending a new path to existing path (setenv "PATH" (concat "C:/Program Files (x86)/Mozilla Firefox/" ";" (getenv "PATH") ) )
For more detail about environment variables with emacs and Windows, see: Emacs and Microsoft Windows Tips.
For assigning a hotkey to any command, see: Emacs: How to Define Keyboard Shortcuts.
Of these 2 solutions of launching Firefox, the one with a shell call works best for me. I have problem using the solution provided by 〔browse-url.el〕. Here's the detail.
I have my mouse setup so that when i hover the pointer over a window, that window comes to the front. This is a super convenient setup. It saves you many clicks. I highly recocommended it. (➲ Windows Auto Raise; Single Click to Open File)
However, when using the 〔browse-url.el〕 solution, when i view a URL, it also for some reason moves the mouse point, so that emacs will switch me to the browser but then some other program behind it will come to the front. Very annoying. I'm not sure why emacs does that. Looking at the code of browse-url-firefox, it's some 49 lines, with lots of variables controlling the behavior, such as controlling whether to open in a new window or tab. It seems overkill.
With shell call, the solution is very simple. Emacs simply makes a shell call and the shell launchs the browser.
blog comments powered by Disqus