vimpager 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!/bin/sh
  2. # Script for using ViM as a PAGER.
  3. # Based on Bram's less.sh.
  4. # Version 1.5.7
  5. # git://github.com/rkitover/vimpager.git
  6. # Just pass through if not on a tty
  7. if [ ! -t 1 ]; then
  8. exec cat "$@"
  9. fi
  10. case `uname -s` in
  11. Linux) linux=1 ;;
  12. SunOS) solaris=1 ;;
  13. Darwin) osx=1; bsd=1 ;;
  14. CYGWIN*) cygwin=1 ;;
  15. MINGW*) msys=1 ;;
  16. *) bsd=1 ;;
  17. esac
  18. less_vim() {
  19. if [ -z "$msys" ]; then
  20. (vim --noplugin -X -e -c 'let vimpager_use_gvim' -c q < /dev/tty) \
  21. | cut -d\# -f2 | head -1 | awk '$1 ~ /^1/ {t=1} END { exit 1-t }' \
  22. && use_gvim=1
  23. else
  24. # The above doesn't work on msys for some reason, so we use
  25. # grep.
  26. use_gvim=`grep '^[ \t]*\<let[ \t]\+vimpager_use_gvim[ \t]\+=[ \t]\+1' ~/_vimrc 2>/dev/null | head -1`
  27. [ -n "$use_gvim" ] || use_gvim=`grep '^[ \t]*\<let[ \t]\+vimpager_use_gvim[ \t]\+=[ \t]\+1' ~/.vimrc 2>/dev/null | head -1`
  28. # msys may not be installed with an msys vim, and if we're
  29. # not in a real console the native Windows vim will not
  30. # work, so we have to use gvim.
  31. if [ "x$TERM" != "xdumb" -a "x$TERM" != "xcygwin" -a "x$TERM" != "x" ]; then
  32. if [ \( ! -x /bin/vim \) -a \( ! -x /usr/bin/vim \) -a \( ! -x /usr/local/bin/vim \) ]; then
  33. use_gvim=1
  34. fi
  35. fi
  36. fi
  37. if [ -n "$use_gvim" ]; then
  38. if [ -n "$osx" ]; then
  39. if [ -z "$SSH_CONNECTION" ] && command -v mvim >/dev/null; then
  40. vim_cmd="mvim -R"
  41. else
  42. vim_cmd="vim -X -R"
  43. fi
  44. elif [ -n "$cygwin" ]; then
  45. if command -v gvim >/dev/null; then
  46. # The Cygwin gvim uses X
  47. if ldd `command -v gvim` 2>/dev/null \
  48. | awk '$1 ~ /cygwin/ {t=1} END { exit 1-t }'; then
  49. if [ -z "$DISPLAY" ]; then
  50. vim_cmd="vim -X -R"
  51. else
  52. vim_cmd='gvim -R'
  53. fi
  54. elif [ -z "$SSH_CONNECTION" ]; then
  55. vim_cmd='gvim -R'
  56. else
  57. vim_cmd="vim -X -R"
  58. fi
  59. else
  60. vim_cmd="vim -X -R"
  61. fi
  62. elif [ -n "$msys" ]; then
  63. if [ -z "$SSH_CONNECTION" ] && command -v gvim >/dev/null; then
  64. vim_cmd='gvim -R'
  65. else
  66. vim_cmd="vim -X -R"
  67. fi
  68. elif [ -z "$DISPLAY" ]; then
  69. vim_cmd='vim -X -R'
  70. else
  71. if command -v gvim >/dev/null; then
  72. vim_cmd='gvim -R'
  73. else
  74. vim_cmd="vim -X -R"
  75. fi
  76. fi
  77. else
  78. vim_cmd='vim -X -R'
  79. fi
  80. rm -f gvim.exe.stackdump # for cygwin gvim, which can be part of vim
  81. vimrc=""
  82. if [ -n "$VIMPAGER_RC" ]; then
  83. vimrc=$VIMPAGER_RC
  84. elif [ -f ~/.vimpagerrc ]; then
  85. vimrc=~/.vimpagerrc
  86. elif [ -f ~/_vimpagerrc ]; then
  87. vimrc=~/_vimpagerrc
  88. fi
  89. case $vim_cmd in
  90. vim*)
  91. if [ -n "$vimrc" ]; then
  92. $vim_cmd \
  93. --cmd 'let vimpager=1' \
  94. -u $vimrc \
  95. -c 'set scrolloff=5 | runtime! macros/less.vim | set foldlevel=999 | set nonu' \
  96. -c 'nmap <ESC>u :nohlsearch<cr>' \
  97. "${@:--}"
  98. else
  99. $vim_cmd \
  100. --cmd 'let vimpager=1' \
  101. -c 'set scrolloff=5 | runtime! macros/less.vim | set foldlevel=999 | set nonu' \
  102. -c 'nmap <ESC>u :nohlsearch<cr>' \
  103. "${@:--}"
  104. fi
  105. ;;
  106. *) # gvim or mvim GUI
  107. if [ -f ~/.gvimrc ]; then
  108. gvimrc=~/.gvimrc
  109. elif [ -f ~/_gvimrc ]; then
  110. gvimrc=~/_gvimrc
  111. elif [ -n "$cygwin" ]; then
  112. # this from Corinna at http://cygwin.com/ml/cygwin/2010-08/msg00246.html
  113. _sid=$(grep "^$USER:" /etc/passwd | sed -e 's/[^:]*:[^:]*:[^:]*:[^:]*:[^:]*,\(S-1-5-[^:]*\):.*/\1/')
  114. _prof=$(cat /proc/registry/HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows\ NT/CurrentVersion/ProfileList/${_sid}/ProfileImagePath)
  115. if [ -f "${_prof}/_gvimrc" ]; then
  116. gvimrc="${_prof}/_gvimrc"
  117. elif [ -f "${_prof}/.gvimrc" ]; then
  118. gvimrc="${_prof}/.gvimrc"
  119. fi
  120. unset _sid
  121. unset _prof
  122. fi
  123. [ -n "$gvimrc" ] && colors=`grep guifg $gvimrc 2>/dev/null | head -1`
  124. # Check if the user maximized the window in ~/_gvimrc on Win32, if
  125. # so restore on startup.
  126. if [ -n "$gvimrc" -a \( -n "$cygwin" -o -n "$msys" \) ]; then
  127. simalt=`grep simalt $gvimrc 2>/dev/null | head -1`
  128. if [ -n "$simalt" ]; then
  129. restore="simalt ~r"
  130. fi
  131. fi
  132. if command -v tput > /dev/null; then
  133. # this is the only way it works on Cygwin
  134. tput cols > /tmp/vimpager_cols_$$
  135. tput lines > /tmp/vimpager_lines_$$
  136. cols=`cat /tmp/vimpager_cols_$$`
  137. lines=`cat /tmp/vimpager_lines_$$`
  138. rm -f /tmp/vimpager_cols_$$ /tmp/vimpager_lines_$$
  139. fi
  140. # msys has no tput, this doesn't work on Cygwin by the way
  141. if [ -z "$cols" ] && command -v bash >/dev/null; then
  142. cols=`bash -i -c 'echo $COLUMNS'`
  143. lines=`bash -i -c 'echo $LINES'`
  144. fi
  145. # If we are unable to detect lines/columns, maximize
  146. # the window.
  147. if [ -z "$cols" ]; then
  148. cols=999
  149. lines=999
  150. fi
  151. if [ -n "$vimrc" ]; then
  152. $vim_cmd \
  153. --cmd 'let vimpager=1' \
  154. -u $vimrc \
  155. -c 'set scrolloff=5 | runtime! macros/less.vim | set foldlevel=999 | set nonu' \
  156. -c 'nmap <ESC>u :nohlsearch<cr>' \
  157. -c "${colors:-echo}" \
  158. -c "${restore:-echo}" \
  159. -c "set lines=$lines | set columns=$cols" \
  160. "${@:--}" &
  161. else
  162. $vim_cmd \
  163. --cmd 'let vimpager=1' \
  164. -c 'set scrolloff=5 | runtime! macros/less.vim | set foldlevel=999 | set nonu' \
  165. -c 'nmap <ESC>u :nohlsearch<cr>' \
  166. -c "${colors:-echo}" \
  167. -c "${restore:-echo}" \
  168. -c "set lines=$lines | set columns=$cols" \
  169. "${@:--}" &
  170. fi
  171. ;;
  172. esac
  173. rm -f gvim.exe.stackdump # for cygwin gvim, which can be part of vim
  174. }
  175. awk_pstree() {
  176. awk -v mypid=$1 '{
  177. cmd[$1]=$3
  178. ppid[$1]=$2
  179. }
  180. END {
  181. while (mypid != 1 && cmd[mypid]) {
  182. ptree=mypid " " cmd[mypid] "\n" ptree
  183. mypid=ppid[mypid]
  184. }
  185. print ptree
  186. }'
  187. }
  188. do_ptree() {
  189. if [ -n "$solaris" ]; then
  190. # Tested on Solaris 8 and 10
  191. ptree $$
  192. elif [ -n "$cygwin" -o -n "$msys" ]; then
  193. ps | awk '{ print $1 "\t" $2 "\t" $NF }' | awk_pstree $$
  194. else
  195. # Tested on Linux and OS X
  196. ps awo pid=,ppid=,comm= | awk_pstree $$
  197. fi
  198. }
  199. # Check if called from man, perldoc or pydoc
  200. if do_ptree | awk '$2 ~ /(^|\/)(man|perl(doc)?([0-9.]*)?|py(thon|doc|doc2))/ {t=1} END { exit 1-t }'; then
  201. extra_cmd="set ft=man"
  202. fi
  203. trap "rm -rf /tmp/vimpager_$$" HUP INT QUIT ILL TRAP KILL BUS TERM
  204. mkdir /tmp/vimpager_$$
  205. filename=${@:-stdin}
  206. filename=`echo "$filename" | tr '/' '_'`
  207. filename="/tmp/vimpager_${$}/$filename"
  208. case "$@" in
  209. *.gz) gunzip -c "$@" | sed -e 's/\[[^m]*m//g' -e 's/.//g' > "$filename" ;;
  210. *.Z) uncompress -c "$@" | sed -e 's/\[[^m]*m//g' -e 's/.//g' > "$filename" ;;
  211. *) sed -e 's/\[[^m]*m//g' -e 's/.//g' "$@" > "$filename" ;;
  212. esac
  213. # if file is zero length, exit immediately
  214. if [ ! -s "$filename" ]; then
  215. exit
  216. fi
  217. # On cygwin it might be the win32 gvim, but windows paths work for cygwin
  218. # vim just fine as well.
  219. if [ -n "$cygwin" ]; then
  220. filename=`cygpath -w "$filename"`
  221. fi
  222. less_vim -c "${extra_cmd:-echo}" "$filename" </dev/tty
  223. # terminal vim on OSX can screw up the terminal
  224. # (but doesn't anymore for some reason...)
  225. #if [ -n "$osx" ]; then
  226. # case $vim_cmd in
  227. # vim*)
  228. # reset
  229. # clear
  230. # ;;
  231. # esac
  232. #fi
  233. # Give gvim/mvim time to open the file.
  234. # On Cygwin we must also wait for the process to exit before the file can
  235. # be deleted.
  236. (
  237. while [ -d /tmp/vimpager_$$ ]; do
  238. sleep 3
  239. rm -rf /tmp/vimpager_$$ 2>/dev/null
  240. rm -f gvim.exe.stackdump 2>/dev/null
  241. done
  242. ) &
  243. # CONTRIBUTORS:
  244. #
  245. # Rafael Kitover
  246. # Antonio Ospite
  247. # Jean-Marie Gaillourdet
  248. # Perry Hargrave
  249. # Koen Smits
  250. # Ivan S. Freitas <ivansichfreitas@gmail.com>
  251. # Wout Mertens (Solaris compatibility, less processes)
  252. # Jacobo de Vera (add -X option for faster startup)
  253. # Damien Pollet <damien.pollet@gmail.com>
  254. # vim:noet ts=8 sts=8 sw=8: