From Putrid Agouti, 2 Weeks ago, written in Plain Text.
  1. #!/bin/bash
  2. # Desc: Miscellaneous shenanigans and hacks to make vim comfier.
  3. # Usage: vim [-] [OPTS] [FILE]
  4. # Features: - Enable ^S and ^Q in vim by reassigning stty start and quit to '\0'
  5. #           - Create a tempfile to allow `:foo %:p:h` style commands
  6. #           - Dismiss "Press ENTER or type..." prompt on first open
  7.  
  8.  
  9. # Default tty chars are:
  10. # intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?;
  11. # eol2 = M-^?; swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z;
  12. # rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O;
  13.  
  14.  
  15. # Possible invocations. Correct value, behavior, and current state
  16. # format: var -> [test:action:status] where
  17. #   test=on/off, action=run/exit, status=pass/fail
  18. #            1:"vim"   2:"vim -"   3:"vim ./foo"   4:"foo|vim -"   5:"foo|vim"
  19. # stdin ->  [off:run:pass, !:exit:pass,  off:run:pass, on:run:pass, !:exit:pass]
  20. # nofile -> [on:run:pass,  on:exit:pass, off:run:pass  on:run:pass, !:exit:pass]
  21. # istty ->  [?:run:pass    !:exit:pass   ?:run:pass    on:run:pass  !:exit:pass]
  22.  
  23.  
  24.  
  25. #declare stdin nofile
  26. #stdin='false' nofile='false'
  27. #
  28. #while (($#)); do
  29. #  [[ -f "$1" ]] || nofile="${nofile:-false}"
  30. #  case "$1" in
  31. #    -) stdin='true'; argv+=("$1"); shift;;
  32. #    *) argv+=("$1"); shift;;
  33. #  esac
  34. #done
  35. #
  36. #if [[ "$nofile" == "true" ]]; then
  37. #  bufnam="$(mktemp -u tmp.vim.XXXXXXXXXX)"
  38. #  mapfile -t argv < <(printf -- "-c \":file $bufnam\"\n"; printf -- "%s\n" "${argv[@]}")
  39. #fi
  40. #
  41. #/usr/bin/vim --cmd 'call feedkeys("jk", "t")' --not-a-term "${argv[@]}"
  42. #
  43. #
  44. #exit
  45.  
  46. # /* FUNCDEF */ --------------------------------------------------------------
  47. msg(){ printf -- "%s\n" "$@"; }
  48. emsg(){ printf -- "%s\n" "$@" >&2; }
  49. bail(){ emsg "$@"; usage; exit 1; }
  50. usage(){ awk -F '# ' 'NR>2{print $2};$0==""{exit}' <"$0"; }
  51.  
  52.  
  53. # /* ENV */ ------------------------------------------------------------------
  54. declare -a argv
  55. declare SHITTYOPTS stdin nofile istty tempfile
  56.  
  57. stdin='false'; nofile='true'; istty='true'
  58. SHITTYOPTS="$(stty -g 2<&-)"
  59. trap 'rm "$tempfile" 2<&-; stty "$SHITTYOPTS" 2<&-' EXIT
  60.  
  61.  
  62. # /* INIT */ -----------------------------------------------------------------
  63. while (($#)); do
  64.   case "$1" in
  65.     --help) # Print usage and vim -h
  66.       vim -h; usage; exit 0
  67.       ;;
  68.     -g) # Use X11
  69.       declare istty='false'
  70.       argv+=("$1")
  71.       shift; continue
  72.       ;;
  73.     -) # Read from stdin
  74.       [[ -t 0 ]] && exit 1 || stdin='true'
  75.       shift; continue
  76.       ;;
  77.     *) # Accumulate other args. Test for input file
  78.       [[ -f "$1" ]] && nofile='false' # NOTE: Filetest must go here to prevent
  79.       argv+=("$1"); shift; continue   # inadvertant toggling of $nofile
  80.       ;;
  81.   esac
  82. done
  83.  
  84.  
  85. # /* MAIN */ -----------------------------------------------------------------
  86. if [[ "$nofile" == "true" ]]; then
  87.   tempfile="$(mktemp -u vim.tmp.XXXXXXXXXXXXXXXX)"
  88.   # Use tempfs if its mounted
  89.   awk '$2~/\/home\/mnrmnaugh\/dev\/mem/{exit 1}' </proc/mounts \
  90.     || tempfile="${HOME}/dev/mem/tmp/${tempfile}"
  91.   argv+=("--" "$tempfile")
  92.  
  93.   # Dump stdin to tempfile
  94.   if [[ "$stdin" == "true" ]]; then
  95.     { while read -r; do
  96.       printf -- "%s\n" "$REPLY" >>"$tempfile"
  97.     done < <(cat <&0); }
  98.     # Reset stdin
  99.     exec 0<&1
  100.   fi
  101. fi
  102.  
  103.  
  104. if [[ "$istty" == "true" ]]; then
  105.   stty start \000 2<&-
  106.   stty stop \000 2<&-
  107.   stty intr \000 2<&-
  108.   stty quit \000 2<&-
  109. fi
  110.  
  111.  
  112. # NOTE: Calling feedkeys() dismisses "Press ENTER or type..." prompt
  113. /usr/bin/vim --cmd 'call feedkeys("jk", "t")' --not-a-term "${argv[@]}"
  114. #/usr/bin/vim --cmd 'call feedkeys("jk", "t")' -c 'set balloonevalterm ballooneval balloondelay=50' -c 'au InsertEnter * call balloon_show("BALLOON!")' --not-a-term "${argv[@]}"
  115.  
  116.  
  117.  
  118. # Grrrrrr if only...
  119. #printf "$@" | \command $realvim < /dev/tty
  120. # AAAaaaahhh
  121. #\command /usr/bin/vim -c ':silent! read '"$@"' | filetype detect | file! '"$@"'' ${vargs} </dev/tty
  122.  
  123. #umktemp(){
  124. #  declare -a argv; declare TMPDIR template
  125. #  template="${1:-tmp.XXXXXXXXXXXXXXXX}"
  126. #
  127. #  while (($#)); do
  128. #    case "$1" in
  129. #      -t) [[  ! -z "$2" ]] && argv+=("$1" "$2"); shift 2; continue;;
  130. #      *)  [[ -d "$1" ]] && TMPDIR="$1"; shift; continue;;
  131. #    esac
  132. #  done
  133. #  [[ ! -d "$TMPDIR" ]] && printf -- "W!: unset variable \"\$TMPDIR\"" >&2
  134. #  [[ ! -z "${argv[@]}" ]] && set -- "${argv[$@]}" || set --
  135. #  \command mktemp "$@" "$template"
  136. #}
captcha