[commit: ghc] master: Add bash completion and README (643635e)

git at git.haskell.org git at git.haskell.org
Sun Nov 30 18:01:39 UTC 2014


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/643635ea1d779054e1bb3b1825cd7894c5748811/ghc

>---------------------------------------------------------------

commit 643635ea1d779054e1bb3b1825cd7894c5748811
Author: Lennart Kolmodin <kolmodin at gmail.com>
Date:   Sun Nov 30 11:59:59 2014 -0600

    Add bash completion and README
    
    Summary:
    The bash completion is simple but works both for ghc and ghci.
    The README explains to the user what they have to do to get
    it working (hopefully nothing).
    
    Test Plan: Follow the README, then enjoy the cli completion in your terminal!
    
    Reviewers: austin
    
    Subscribers: thomie, carter, jstolarek
    
    Differential Revision: https://phabricator.haskell.org/D536
    
    GHC Trac Issues: #9005


>---------------------------------------------------------------

643635ea1d779054e1bb3b1825cd7894c5748811
 completion/README   | 43 ++++++++++++++++++++++++++++++++++++++
 completion/ghc.bash | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+)

diff --git a/completion/README b/completion/README
new file mode 100644
index 0000000..18b171c
--- /dev/null
+++ b/completion/README
@@ -0,0 +1,43 @@
+========================================
+Glasgow Haskell Compiler Bash Completion
+========================================
+
+Depending on how you've installed GHC, there are different ways to turn
+on the bash completion.  With a bit of luck, your OS distribution has already
+setup everything and it's already working!  But since you're reading this file,
+we assume that was not the case. Read on!
+
+
+Option 1: Using your OS distribution's tools
+--------------------------------------------
+
+This should work if you installed GHC using the package manager of your *nix
+distribution.  You need the 'bash-completion' package from your package manager
+(likely already installed), and something like this in your ~/.bashrc file;
+
+    if [ -f /usr/share/bash-completion/bash_completion ]; then
+      . /usr/share/bash-completion/bash_completion
+    elif [ -f /etc/bash_completion ]; then
+      . /etc/bash_completion
+    fi
+
+When you installed GHC using your OS distribution's package manager it should
+have copied the bash completion file to the right directory.
+Open a new terminal and try it out!
+
+
+Option 2: Without OS distribution support
+-----------------------------------------
+
+Maybe your OS distribution doesn't support GHC's bash completion, maybe
+you've installed your own build of GHC.  In either case, you can still have
+bash completion!  You can even use GHC's bash completion without the
+'bash-completion' package. Here are the steps;
+
+ 1) Copy the ghc.bash file somewhere (eg. ~/ghc.bash) or use it directly from
+    the source directory.
+ 2) Add the following to your ~/.bashrc;
+
+      source ~/ghc.bash
+
+That's it!
diff --git a/completion/ghc.bash b/completion/ghc.bash
new file mode 100755
index 0000000..af5bf9b
--- /dev/null
+++ b/completion/ghc.bash
@@ -0,0 +1,60 @@
+# ========================================
+# Glasgow Haskell Compiler Bash Completion
+# ========================================
+#
+# For how to use the GHC bash completion, see the README.
+#
+# This file implements bash completion for both GHC and GHCi.
+#
+#  - We use GHC's --show-options to get a list of the available
+#    flags. It is aware that some flags are used for GHC, and others for GHCi.
+#  - We understand when the argument;
+#     * has to be a directory name (eg. following -hidir)
+#     * cannot be completed (eg. following -e)
+#
+# Future work;
+#  - Some flags needs their argument after an equal sign;
+#      eg. -fmax-simplifier-iterations=N
+#      Currently the flag will be completed without knowledge of
+#      the required argument.
+#  - Complete package names/ids.
+#      eg. -package-id <TAB><TAB> should list valid package-ids
+#  - The +RTS flags are not supported.
+#      +RTS <TAB><TAB> should list valid RTS flags.
+
+_ghc ()
+{
+  local completions=`$1 --show-options`
+  local cur="${COMP_WORDS[COMP_CWORD]}"
+  local prev="${COMP_WORDS[COMP_CWORD-1]}"
+
+  # Complete the current flag based on the previous flag.
+  case "$prev" in
+    -hidir|-odir|-stubdir|-dumpdir|-outputdir|-tmpdir|-hpcdir|-dylib-install-name|-framework-path)
+      # Complete only with directory names.
+      compopt -o dirnames
+      return 0
+      ;;
+    -package-name|-package|-hide-package|-ignore-package|-trust|-distrust)
+      # Should complete package names. Not implemented.
+      # To do this well, ghc has to be invoked with --show-packages with all
+      # package related flags the user has provided.
+      return 0
+      ;;
+    -e|-x|-hcsuf|-hisuf|-osuf|-framework)
+      # Do nothing. Next argument is not a flag.
+      return 0
+      ;;
+  esac
+
+  # Look at the current flag.
+  if [[ "$cur" == -* ]]; then
+    # All GHC flags start with a dash, so we want to see this before we start
+    # suggesting flags. Otherwise we would complete flags when the user might
+    # want to type a file name.
+    COMPREPLY=( $( compgen -W "$completions -x" -- "$cur" ) )
+  fi
+}
+
+complete -F _ghc -o default ghc
+complete -F _ghc -o default ghci



More information about the ghc-commits mailing list