.emacs-autogen.sh
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sat, 28 Feb 2015 22:37:48 +0200
changeset 1238 c9e507cde2b7
parent 702 0617020b53d8
child 1446 78d0fef31a4b
permissions -rwxr-xr-x
Detect Emacs sources for 'find-function-C-source-directory'.

#!/usr/bin/env bash

if [ -z "$1" ]; then
  echo "Where config file?"
  exit 1
fi

cfg=$1

# Set host_os:
#   windows, linux, unknown
# host_distro:
#   cygwin, debian, unknown
check_platform() {
  case `uname -s` in
    CYGWIN_NT-*)
      host_os=windows
      host_distro=cygwin
      ;;
    Linux)
      host_os=linux
      if [ -e /etc/debian_version ]; then
        host_distro=debian
      else
        host_distro=unknown
      fi
      ;;
    Darwin)
      host_os=darwin
      ;;
    *)
      host_os=unknown
      ;;
  esac
}

print_header() {
  echo
  echo ';; ================================================================'
  echo ";; ${FUNCNAME[1]}."
  echo
}

check_ispell() {
  command -v ispell >/dev/null && return 0 || return 1
}

check_aspell() {
  command -v aspell >/dev/null && return 0 || return 1
}

print_ispell() {
  echo '(setq-default ispell-program-name "ispell")'
  case $host_distro in
    debian)
      echo '(setq-default ispell-local-dictionary "british")'
      ;;
    unknown)
      echo '(setq-default ispell-local-dictionary "default")'
      ;;
  esac
}

print_aspell() {
  echo '(setq-default ispell-program-name "aspell")'
  echo '(setq-default ispell-local-dictionary "en")'
}

print_speller() {
  print_header
  check_ispell
  ispell_present=$?
  check_aspell
  aspell_present=$?
  case $host_os in
    windows)
      case $host_distro in
        cygwin)
          if [ $aspell_present = 0 ]; then
            print_aspell
          elif [ $ispell_present = 0 ]; then
            print_ispell
          fi
          ;;
      esac
      ;;
    linux)
      case $host_distro in
        debian)
          if [ $ispell_present = 0 ]; then
            print_ispell
          elif [ $aspell_present = 0 ]; then
            print_aspell
          fi
          ;;
      esac
      ;;
    darwin)
      print_aspell
      ;;
    *)
      :
      ;;
  esac
}

print_man() {
  print_header
  if man -a man >/dev/null 2>&1; then
    echo '(setq Man-switches "-a")'
  else
    echo '(setq Man-switches "")'
  fi
}

print_emacs_sources_dir() {
  print_header
  if [[ -d ~/devel/emacs/emacs/src ]]; then
    echo '(setq find-function-C-source-directory "~/devel/emacs/emacs/src")'
  else
    ls ~/devel/emacs/src/emacs.c ~/devel/emacs/emacs*/src/emacs.c 2>/dev/null | {
      read f
      printf '(setq find-function-C-source-directory "%s")' ${f%emacs.c}
    }
  fi
}

cat >$cfg <<EOF
;; -*- mode: emacs-lisp; coding: utf-8; fill-column: 78 -*-
;;
;; For load order see README.
EOF

check_platform
{
  print_speller
  print_man
  print_emacs_sources_dir
} >>$cfg