[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: prefer application on Gnome Desktop

From: Teemu Likonen
Subject: Re: prefer application on Gnome Desktop
Date: Tue, 14 Jul 2009 10:46:34 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

On 2009-07-14 14:41 (+0900), Byung-Hee HWANG wrote:

> Hi, i use Emacs on Gnome Desktop (FreeBSD 7.2-RELEASE). In Gnome,
> there is the config named "prefer application" in which we can decide
> the best application for us such as mailer, webbrowser. The default
> value in mailer is "Evolution". By the way i would like to switch to
> Gnus from Evolution. So how can i change without any errors?

I have written a Bash script which parses mailto URLs and email headers'
possible RFC2047 quotations and then launches emacsclient to compose
mail. I don't know if it works in FreeBSD but it works on GNU/Linux
systems. It uses Bash's printf features to decode URL's %xx codes and
GNU "recode" utility to decode RFC2047-quoted headers. It also uses
"sed" a lot but I have no idea know if it needs GNU sed's extensions.

It's meant to be an executable script and it takes one command-line
argument: a mailto URL string, like this:

    mailto-handler "mailto:address@hidden&subject=This%20is%20subject";

I use the script with Firefox and it's my KDE's default mail client.
Works great at least in my system. Be free to edit it to suit your
purposes. The program is public domain; absolutely no warranty.

# Mailto link handler script for Emacs.
# Written by Teemu Likonen <address@hidden>
# This program is placed in the public domain.


echopf() {
        printf '%s' "$@"
        printf '\n'

unquote_url() {
        read -r string
        string=$(echopf "$string" | \
                sed -re 's/%([0-9a-fA-F]{2})/\\x\1/g')
        printf "%b\n" "$string"

convert_url_unix_lf() {
        sed -e 's/%0[Dd]%0[Aa]/%0A/g;s/%0[Dd]/%0A/g'

convert_url_lf_tab_to_space() {
        sed -e 's/%0[Aa]%09/%20/g'

elisp_string_escape() {
        sed -e 's/[\"]/\\&/g'

unquote_rfc2047() {
        local string
        local new_string
        local mime_regexp mime_string mime_charset mime_encoding
        local text_string text_string_unquoted 
        local mime_string_escaped text_string_unquoted_escaped
        read -r string
        mime_regexp='(=\?([^? ]+)\?([qQbB])\?([^? ]+)\?=)'
        # Remove whitespace between MIME strings.
        while true; do
                new_string=$(echopf "$string" | \
                        sed -re "s/(${mime_regexp})\s+(${mime_regexp})/\1\6/")
                if [ "$new_string" = "$string" ]; then
        mime_string=$(echopf "$string" | \
                sed -nre "s/.*${mime_regexp}.*/\1/p")
        # Decode MIME strings one by one
        while [ "$mime_string" ]; do
                mime_charset=$(echopf "$mime_string" | \
                        sed -re "s/.*${mime_regexp}.*/\2/")
                mime_encoding=$(echopf "$mime_string" | \
                        sed -re "s/.*${mime_regexp}.*/\3/")
                text_string=$(echopf "$mime_string" | \
                        sed -re "s/.*${mime_regexp}.*/\4/")

                case "$mime_encoding" in
                        text_string=$(echopf "$text_string" | \
                                sed -e 's/_/=20/g')

                text_string_unquoted=$(echopf "$text_string" | \
                        recode -- "$mime_charset/$mime_encoding")
                mime_string_escaped=$(echopf "$mime_string" | \
                        sed -re 's,[][()|\^$.?+/*{}],\\&,g')
                text_string_unquoted_escaped=$(echopf \
                        "$text_string_unquoted" | sed -re 's,[/&\],\\&,g')
                string=$(echopf "$string" | sed -re \
                mime_string=$(echopf "$string" | \
                        sed -nre "s/.*${mime_regexp}.*/\1/p")
        echopf "$string"

to1_address=$(echopf "$url" | \
        sed -nre 's/^mailto:([^?&]*).*$/\1/p' | \
        unquote_url | unquote_rfc2047)
to2_address=$(echopf "$url" | \
        sed -nre 's/^mailto:.*[?&][Tt][Oo]=([^?&]*).*$/\1/p' | \
        unquote_url | unquote_rfc2047)
to_address=$(printf "%s, %s\n" "$to1_address" "$to2_address" | \
        sed -re 's/^[, ]+|[, ]+$//g' | elisp_string_escape)

cc_address=$(echopf "$url" | \
        sed -nre 's/^mailto:.*[?&][Cc][Cc]=([^?&]*).*$/\1/p' | \
        unquote_url | unquote_rfc2047 | elisp_string_escape)
bcc_address=$(echopf "$url" | \
        sed -nre 's/^mailto:.*[?&][Bb][Cc][Cc]=([^?&]*).*$/\1/p' | \
        unquote_url | unquote_rfc2047 | elisp_string_escape)
subject=$(echopf "$url" | \
        sed -nre 's/^mailto:.*[?&][Ss]ubject=([^?&]*).*$/\1/p' | \
        convert_url_unix_lf | convert_url_lf_tab_to_space | \
        unquote_url | unquote_rfc2047 | elisp_string_escape)
in_reply_to=$(echopf "$url" | \
        sed -nre 's/^mailto:.*[?&]In-Reply-To=([^?&]*).*$/\1/p' | \
        unquote_url | sed -re 's/^[< ]+|[> ]+$//g' | elisp_string_escape)

elisp_expression=$(printf '(compose-mail "%s" "%s" (quote (' \
        "$to_address" "$subject")

[ "$cc_address" ] && elisp_expression=$(printf '%s("Cc" . "%s")' \
        "$elisp_expression" "$cc_address")

[ "$bcc_address" ] && elisp_expression=$(printf '%s("Bcc" . "%s")' \
        "$elisp_expression" "$bcc_address")

[ "$in_reply_to" ] && elisp_expression=$(printf '%s("In-Reply-To" . "<%s>")' \
        "$elisp_expression" "$in_reply_to")

elisp_expression=$(printf '%s)))\n' "$elisp_expression")

echopf "$elisp_expression"

exec emacsclient -c -n -a '' --eval "$elisp_expression"

reply via email to

[Prev in Thread] Current Thread [Next in Thread]