Something for the emacs users! :smile:

This is a small script for all fellow emacs users. :smile:

It starts emacs by adjusting the window size of the emacs window according to the contents of given file.
For example:

  emacs ~/.bashrc

will start emacs with the window size (width and height) suited for the contents of ~/.bashrc.

If you think it is worth while, you can write it into your ~/.bashrc, or (preferably) into an executable file on your PATH, like /usr/local/bin/emacs.

You can also modify the limits given in the script (variables wmax, wmin, hmax and hmin) to suit better to your monitor size.

Enjoy! :vulcan_salute:t2:

#!/bin/bash

# Start 'emacs' by
#  - adjusting window width and height according to document's contents
#  - using reverse video colors for having dark window background

_emacs_geometry() {
    # Adjust emacs window size based on the file contents.

    local wmax=300     # max width in characters
    local wmin=80      # min width in characters
    local hmax=120     # max height in characters
    local hmin=35      # min height in characters
    local w h
    local doc="$(cat "$arg")"

    w=$(echo "$doc" | /usr/bin/wc -L)
    ((w+=7))                         # 5 is minimum?
    test $w -gt $wmax && w=$wmax
    test $w -lt $wmin && w=$wmin

    h=$(echo "$doc" | /usr/bin/wc -l)
    test $h -gt $hmax && h=$hmax
    test $h -lt $hmin && h=$hmin

    args+=("--geometry=${w}x$h")
}

emacs()
{
    local arg
    local args=(--reverse-video)  # use "dark" background

    for arg in "$@"
    do
        case "$arg" in
            -*) ;;
            *)  test -r "$arg" && _emacs_geometry ;;
        esac
        args+=("$arg")
    done

    /usr/bin/emacs "${args[@]}" >& /dev/null &
}

emacs "$@"
7 Likes