Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In Emacs no window mode, the background is automatically the same as the terminal (gnome-terminal). When I look at (frame-parameters) I see that (background-color . "unspecified-bg"). Well, for me, the background happens to be black. Is there a way to find out what the actual background color is for an Emacs session?

share|improve this question
    
I don't think that the background color of a terminal emulator can be detected by an application inside of the terminal. –  jordanm Mar 6 '13 at 0:45
1  
I do know this information can be found with gconftool... –  Jeff Mar 6 '13 at 15:02
    
@Jeff: And that is all you need to know! –  Emanuel Berg Mar 6 '13 at 19:23
add comment

1 Answer

up vote 2 down vote accepted

If you know how to find out from the terminal, you can use that selfsame command to find out from Emacs.

In my case, I'd make a script like this:

#!/bin/zsh

cat .Xresources | grep 'URxvt\*background\:' | cut -d" " -f2

(Note: -d is to set the field delimiter, -f is to set what field is to be shown: the first field is 1, not 0)

The command looks the way it does because .Xresources, the file that sets the background color, looks like this:

# ...
URxvt*background: black
# ...

Make the script executable (chmod +x), and put it in your PATH (echo $PATH).

If the script is called what_bg, in Emacs, M-x shell-command RET what_bg.

Edit (in response to comment):

See if this works. I tested it from Emacs, and in urxvt, xterm, and rxvt. While it is more portable than the first script, it assumes .Xresources configuration (which is, while not uncommon, obviously not everywhere).

I'm starting to wonder, though, why you need this to begin with?

And, if you indeed need it, can't you just look on the window to determine its color?

Anyway, the script:

#!/bin/zsh

terminal_emulator_parents=`pstree -As $$`
tep_list=`echo $terminal_emulator_parents | tr -s "-" | tr "-" " " \
          | tac -s' ' | tr '\n' ' '`

found="false"
for process in `echo $tep_list`; do
    if [[ $process =~ ("urxvt"|"xterm"|"rxvt") ]]; then # here: add all
        found="true"                                    # terminal emulators
        break                                           # configurable
    fi                                                  # (and *configured*)
done                                                    # in ~/.Xresources

if [[ $found == "true" ]]; then
    echo -n "$process: "
    cat ~/.Xresources | grep -ie ^$process'\*background\:' \
                      | tr -s " " | cut -d" " -f2
else
    echo "Couldn't determine the terminal emulator."
fi
share|improve this answer
    
I guess this is as close as I'll get for now. I was hoping to find something a little more cross-platform, but this is unix.stackexchange... shell-command is new to me so that's a good find. –  Jeff Mar 8 '13 at 17:10
1  
@Jeff: Made an edit; take a look. –  Emanuel Berg Mar 9 '13 at 10:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.