summaryrefslogtreecommitdiff
path: root/01_Internal.zsh
blob: 42d4af4ea163c10d19e366283fa0651072f2c18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
##
## Part of configuration files for Zsh 4
## by Hugues Hiegel <hugues@hiegel.fr>
## 
## NO WARRANTY PROVIDED, USE AT YOUR OWN RISKS
##
## You are encouraged to use, modify, and redistribute
## these files with or without this notice.
## 

##
## 	User-defined functions
##
#
# For preexec, precmd, chpwd and other built-in functions,
# go see the file Prompts.zsh
#

_cuu1_=$(tput cuu1)
_cud1_=$(tput cud1)
_cub1_=$(tput cub 1)
_cuf1_=$(tput cuf 1)

__cmd_exists ()
{
	which -p $1 >/dev/null 2>&1
}

__cmd_exists keychain && eval $(keychain --inherit any --eval --quiet)

__term_title()
{
    __debug -n "	Term title..."
	# Jobs
	typeset -A command
	for word in ${=@} ; command[$#command]=$word
	if [ "$command[0]" = "fg" ]
	then
		lastjob=$(ps t `tty` | grep "[0-9]\+[[:blank:]]\+`tty | sed 's/\/dev\///'`[[:blank:]]\+T.\? \+.:..  \\\_ " | tail -n1 | cut -c32-)
		set "$lastjob"
	fi
	if [ "$command[0]" = "screen" -o "$command[0]" = "tmux" ]
	then
		# discards screen args
		set $command[0]
	fi

	[ ! "$@" = "" ] && set " |" $@

	if [[ -t 1 ]]
	then
		case $TERM in
		  sun-cmd)
            __debug -n "\n		- sun"
			print -Pn "\e]l%n@%m %~$@\e\\"				# Never tested..
			;;
		  *term*|rxvt*|putty)
            __debug -n "\n		- term/rxvt/putty"
			print -Pn "\e]0;%n@%m (%l) %~${@//\%/%%}\a"			# Sets term title
			;;
		  screen*)
            __debug -n "\n		- screen"
			local _sep=""
			[ $# -gt 0 ] && _sep=$1 && shift # gets and discards the separator, if any.
			# Tmux
			#print -Pn "\e]0;%n@%m (%l) %~${_sep:+$_sep #[fg=yellow,bold]}$@\a"			# Sets term title
			case $(_process_tree) in
				*":: tmux ::"*)
					print -Pn "\e]0;#[fg=red]%n#[fg=default,bold]@#[fg=red]%m#[default] (#[fg=cyan]%l#[fg=default]) #[fg=red]%~${_sep:+#[default,fg=default]$_sep #[fg=yellow,bold]}$(echo $@|sed 's/%/%%/g')\a"
					;;
				*":: SCREEN ::"*)
					# Classic screen
					# hardstatus
					#print -Pn "\e]2;{+b W}SCREEN #n {-b W}| {R}?u(u) ?{W}{r}%n@%m{W} ({c}%l{W}) {R}%~{W}${_sep:+$_sep \{+b Y\}}$@{-b W}\a" # Sets hardstatus line (term title)
					#print -Pn "\e]2;{R}?u(u) ?{W}{r}%n{R}@{r}%m{-b W} ({+b c}%l{-b W}) {R}%~{W}${_sep:+$_sep \{+b Y\}}$@{-b W}\a" # Sets hardstatus line (term title)
					# caption
					#print -Pn "\ek"
					#[ "$SUDO_USER" != "" ] && print -Pn "($USER) "
					#print -Pn "${@:-%~}"
					#print -Pn "\e\\"
					;;
			esac
			;;
		  *)
			;;
		esac
	fi
    __debug
}

__preprint()
{
	local my_color i newline
    if [ "$1" = "-n" ]
    then
        newline='n'
        shift
    fi

	my_color=${2-"$_prompt_colors[generic]"}

	local _width=74
	local _space=5

	hbar=$T_${${(l:$(( $_width - ${#1} - $_space ))::q:)}//q/$_tq_}$_T

	if [ "$1" != "" ]
	then
		print -P$newline "${C_}$my_color;1${_C}${hbar}$T_$_tj_$_T${C_}0;$my_color${_C} $1 ${C_}0;$my_color;1${_C}$T_$_tm_$_tq_$_T\r${C_}0${_C}"
	else
		print -P$newline "${C_}$my_color;1${_C}${hbar}$T_${${(l:${_space}::q:)}//q/$_tq_}$_T${C_}0${_C}"
	fi
}

__get_gcl_branch ()
{
	case $1 in
		git)
            __debug -n "	GIT status..."
			__get_git_infos
            __debug
			;;
		hg)
            __debug -n "	HG status..."
			__get_hg_branch
            __debug
			;;
		*)
			;;
	esac
}

__get_hg_branch ()
{
	HGROOT=$(hg root 2>/dev/null)
	if [ ! -z "$HGROOT" ]
	then
		hg branch
	fi
}

__cleanup_git_branch_name() { sed '
		s,^tags/\(.*\),%{\\033['$color[standout]'m%}\1,
		/^remotes/ {
			s,^remotes/,,
			s,^devel/,~,
			s,^origin/,¬/,
			s,^,%{\\033['$color[standout]'m%},
		}
		s,\^0$,,
	' }

__get_git_fullstatus ()
{
	[ -n "$1" ] && pushd $1 >/dev/null

	local _branch _status _tracking _stashes

	_branch=$(__get_git_branch)

	if [ -n "$_branch" ]
	then
		_status=$(__get_git_branch_status)
		_branch=$C_$_prompt_colors[soft_generic]$_C${${_branch/→/$C_$_status$_C}/←/$C_$_prompt_colors[soft_generic]$_C}$C_$color[none]$_C
		_tracking=$(__get_git_tracking_status)
		_stashes=$(__get_git_stashes)
	fi

	[ -n "$1" ] && popd >/dev/null

	echo $_branch${_tracking:+ $_tracking}${_stashes:+ $_stashes}

}

__get_git_branch ()
{
	local my_git_branch checkouted_branch commit_ish

	__debug
	__debug -n "		repo..."
	if [ -f ".repo/manifests.git/config" ]
	then
		my_git_branch=$(grep merge .repo/manifests.git/config | awk '{print $3}')
		if [ $my_git_branch != "" ]
		then
			echo " [$my_git_branch]"
			return
		fi
	fi
	__debug

	__debug -n "		branch..."
	GIT_DIR=$(git rev-parse --git-dir 2>&-)
	# Get git branch only from git managed folders (not ignored subfolders..)
	[ "$( ( git ls-files ; git ls-tree HEAD . ) 2>&- | head -n1)" = "" -a \
	  \( ! -d .git -o -z "$GIT_DIR" \) -a \
	  "$(git rev-parse --is-inside-git-dir 2>&-)" != "true" ] && return

	# commit-ish, for future uses
	commit_ish=$(git rev-parse --verify HEAD 2>/dev/null)

	# Get current working GIT branch
	my_git_branch="$(git symbolic-ref --short -q HEAD)"
	#my_git_branch="$(git name-rev --name-only HEAD)"
	# for future use
	checkouted_branch=$my_git_branch

	if [ ! "$my_git_branch" ]
	then
		# Not on a working GIT branch. Get the named current commit-ish inside parenthesis
		[ ! "$my_git_branch" ] &&\
			checkouted_branch="" && \
			my_git_branch="$(git name-rev --name-only --always --no-undefined HEAD 2>&- | __cleanup_git_branch_name)"

	#else
	#	# Initial commit
	#	if [ -L $GIT_DIR/HEAD -a ! -f $GIT_DIR/HEAD ]
	#	then
	#		my_git_branch="$(basename $(readlink -f $GIT_DIR/HEAD))"
	#	else
	#		my_git_branch="$(basename $GIT_DIR/$(cat $GIT_DIR/HEAD | sed 's/^\([0-9a-f]\{2\}\)\([0-9a-f]\{38\}\)$/objects\/\1\/\2/;s/^ref: //'))"
	#	fi
	fi
	__debug

    my_git_branch="→"$my_git_branch"←"

	__debug -n "		bisect..."
	# Bisect in progress ?
	if [ -e $GIT_DIR/BISECT_LOG ]
	then
		local bisect bisect_good bisect_bad bisect_start
		# ▶ ▷ ▸ ▹ ► ▻ ◀ ◁ ◂ ◃ ◄ ◅

		eval $(awk '
			BEGIN {
				good=""
				bad=""
			}
			/^git bisect good/ { good=$4 }
			/^git bisect bad/ { bad=$4 }
			END {
				print "bisect_good="good 
				print "bisect_bad="bad
			}' $GIT_DIR/BISECT_LOG)

		bisect=""

		if [ "$bisect_good" ]
		then
			bisect_good=$(( $(git log --oneline $bisect_good..$commit_ish | wc -l) - 1))
			bisect+="$C_$_gcl_colors[bisect-good]$_C"
			[ $bisect_good -ge 1 ] && bisect+="₊"$(echo $bisect_good | _subscript_number)
			bisect+="▸"
		fi

		#bisect+="→"$commit_ish"←"
		bisect+=$my_git_branch

		if [ "$bisect_bad" ]
		then
			bisect_bad=$(git log --oneline $commit_ish..$bisect_bad | wc -l)
			bisect+="$C_$_gcl_colors[bisect-bad]$_C"
			bisect+="◂"
			[ $bisect_bad -ge 1 ] && bisect+="₊"$(echo $bisect_bad | _subscript_number)
		fi

		bisect+="$C_$_prompt_colors[soft_generic]$_C ("
		bisect+=$C_$color[magenta]$_C$(cat $GIT_DIR/BISECT_START)
		bisect+=$C_$_prompt_colors[soft_generic]$_C")"

		echo $bisect
		return
	fi
	__debug

	__debug -n "		rebase..."
	# Rebase in progress ?
	if [ -d $GIT_DIR/rebase-merge -o -d $GIT_DIR/rebase-apply ]
	then
		local rebase onto amend current last
		local REBASE_DIR

		if [ -d $GIT_DIR/rebase-merge ]
		then
			REBASE_DIR=$GIT_DIR/rebase-merge
		else
			REBASE_DIR=$GIT_DIR/rebase-apply
		fi

		if [ "$REBASE_DIR" = "$GIT_DIR/rebase-merge" ]
		then
			current=$(< $REBASE_DIR/done wc -l)
			last=$(( $current + $(< $REBASE_DIR/git-rebase-todo grep -v "^#\|^[[:blank:]]*$" | wc -l) ))
		else
			current=$(cat $REBASE_DIR/next)
			last=$(cat $REBASE_DIR/last)
		fi

		rebase="["$C_$_prompt_colors[bold_generic]$_C
		#while [ $current -gt 0 ] ; do rebase+=$T_"a"$_T ; current=$(( $current - 1 )) ; done
		#while [ $last -gt 0 ]    ; do rebase+=$T_"|"$_T ; last=$(( $last - 1 )) ; done
		rebase+=$current"/"$last

		# ▶ ▷ ▸ ▹ ► ▻ ◀ ◁ ◂ ◃ ◄ ◅

		# base
		onto=$(git name-rev --name-only --always --no-undefined $(cat $REBASE_DIR/onto) 2>&- | __cleanup_git_branch_name)

		# amended commit
		if [ -e $REBASE_DIR/stopped-sha ]
		then
			amend=$(cat $REBASE_DIR/stopped-sha)
		elif [ -e $REBASE_DIR/amend ]
		then
			amend=$(cat $REBASE_DIR/amend)
		elif [ -e $REBASE_DIR/original-commit ]
		then
			amend=$(cat $REBASE_DIR/original-commit)
		fi
		#
		if [ "$amend" != "$commit_ish" ]
		then
			#amend=$(git name-rev --name-only --always --no-undefined "$amend" 2>/dev/null | __cleanup_git_branch_name)
			#[ "$amend" = "undefined" ] &&
			amend=$(git name-rev --name-only --always --no-undefined $amend 2>&- | __cleanup_git_branch_name)
			amend=" ◃ "$C_$color[magenta]$_C$amend$C_$_prompt_colors[soft_generic]$_C
		else
			amend=""
		fi

		rebase+=$C_$_prompt_colors[soft_generic]$_C": $onto..$my_git_branch$amend]"
		[ -r $REBASE_DIR/head-name ] && rebase+=" ("$(< $REBASE_DIR/head-name sed 's/^refs\///;s/^heads\///')")"
		my_git_branch=$rebase
	else
		# No rebase in progress, put '(' ')' if needed
		[ ! "$checkouted_branch" ] && my_git_branch="($my_git_branch)"
	fi
	__debug

	echo $my_git_branch
}

__get_git_stashes() {
	__debug -n "		stashes..."
    # Show number of stashed commits by appending '+' signs for each
    if [ "$(git rev-parse --is-inside-git-dir)" != "true" -a "$(git config --get core.bare)" != "true" ]
    then
        local _stashed=$(git stash list | wc -l )
        if [ "$_stashed" -gt 0 ]
        then
			# ↙ ↯ ↲ ↵
			echo -n $C_$_gcl_colors[white]$_C"↙"
			[ $_stashed -gt 1 ] && echo -n "$(echo $_stashed | _subscript_number)"
        fi
    fi
	__debug
}

__get_git_tracking_status() {
	local git_tracking_status=""

	my_git_branch="$(git symbolic-ref --short -q HEAD)"

	if [ "$my_git_branch" ]
	then

		GIT_DIR=$(git rev-parse --git-dir 2>&-)
		if [ -d $GIT_DIR/svn ]
		then
			__debug -n "		git-svn tracking..."
			svn_current=$(git svn find-rev HEAD 2>/dev/null)

			# Finds any reference upwards current HEAD, and get the latest possible git-svn-id
			#svn_track=$(git log --grep git-svn-id -1 --pretty=%h $(git branch -a --contains=HEAD | cut -c3-) -- )
			svn_track=$(git log --grep git-svn-id -1 --pretty=%h HEAD)
			svn_rev=$(git svn find-rev $svn_track)
			# eval $( git svn info | awk '/^URL: /              { print "svn_url="$2 }
			#                             /^Repository UUID: /  { print "svn_repo="$3 }
			#                             /^Last Changed Rev: / { print "svn_rev="$2 }
			# ')
			# svn_track=$(git log --grep="git-svn-id: $svn_url@$svn_rev $svn_repo" --pretty=%h -1)

			if [ "$svn_current" != "" -a "$svn_current" != "$svn_rev" ]
			then
				git_tracking_status+=$C_$_gcl_colors[svncurrent]$_C"(r$svn_current) "
			fi

			if [ "$svn_track" != "" ]
			then

				local _ahead=0 ;
				local _behind=0 ;
				_ahead=$( git rev-list --count ${svn_track}..${my_git_branch})
				_behind=$(git rev-list --count ${my_git_branch}..${svn_track})

				# ᛨ ᛪ ⇅ ↟↟ ⇶ ⇶ ⇵ ⌥ ⬆ ⬇ ⬌  ⬍ ⤱ ⤲ ✖ ➠ ➟ ⤴ ⎇⬋⬉⬉⬈⬌⬍⬅⬄
				# ▶ ▷ ▸ ▹ ► ▻ ◀ ◁ ◂ ◃ ◄ ◅

				if [ $_behind -gt 0 ]
				then
					git_tracking_status+=$C_$_gcl_colors[ffwd_svn]$_C
					[ $_behind -gt 1 ] && git_tracking_status+="$(echo $_behind | _subscript_number)"
					git_tracking_status+="⬇"
				fi

				if [ $_ahead -gt 0 -a $_behind -gt 0 ]
				then
					svncolor="diverged_svn"
				elif [ $_ahead -gt 0 ]
				then
					svncolor="cached_svn"
				elif [ $_behind -gt 0 ]
				then
					svncolor="ffwd_svn"
				else
					svncolor="svnrev"
				fi

				git_tracking_status+=$C_$_gcl_colors[$svncolor]$_C"r$svn_rev"

				if [ $_ahead -gt 0 ]
				then
					if [ $_behind -gt 0 ]
					then
						git_tracking_status+=$C_$_gcl_colors[svnrev]$_C
					else
						git_tracking_status+=$C_$_gcl_colors[cached_svn]$_C
					fi
					git_tracking_status+="⬆"
					[ $_ahead -gt 1 ] && git_tracking_status+="$(echo $_ahead | _subscript_number)"
				fi

			fi
			__debug
			git_tracking_status+=" "
		fi

		__debug -n "		tracking..."
		local _upstream="$(git rev-parse --revs-only HEAD@\{upstream\} 2>/dev/null)"
		if [ ! "$_upstream" ]
		then
			git_tracking_status+=$C_$_gcl_colors[untracked]$_C"✖"
		else
			__debug
			__debug -n "		behind/ahead..."
			# Show number of stashed commits by appending '+' signs for each
			if [ "$(git rev-parse --is-inside-git-dir)" != "true" -a "$(git config --get core.bare)" != "true" ]
			then
				local _ahead=0 ;
				local _behind=0 ;
				_ahead=$( git rev-list --count ${_upstream}..${my_git_branch})
				_behind=$(git rev-list --count ${my_git_branch}..${_upstream})

				# ᛨ ᛪ ⇅ ↟↟ ⇶ ⇶ ⇵ ⌥ ⬆ ⬇ ⬌  ⬍ ⤱ ⤲ ✖ ➠ ➟ ⤴ ⎇⬋⬉⬉⬈⬌⬍⬅⬄

				if [ $_behind -gt 0 ]
				then
					git_tracking_status+=$C_$_gcl_colors[ffwd]$_C
					[ $_behind -gt 1 ] && git_tracking_status+="$(echo $_behind | _subscript_number)"
					git_tracking_status+="⬇"
				fi
				if [ $_ahead -gt 0 ]
				then
					if [ $_behind -gt 0 ]
					then
						git_tracking_status+=$C_$_prompt_colors[generic]$_C
					else
						git_tracking_status+=$C_$_gcl_colors[cached]$_C
					fi
					git_tracking_status+="⬆"
					[ $_ahead -gt 1 ] && git_tracking_status+="$(echo $_ahead | _subscript_number)"
				fi
			fi
		fi
		__debug
	fi

	echo $git_tracking_status
}

_subscript_number() {
	sed 's/0/₀/g;s/1/₁/g;s/2/₂/g;s/3/₃/g;s/4/₄/g;s/5/₅/g;s/6/₆/g;s/7/₇/g;s/8/₈/g;s/9/₉/g'
}

__get_guilt_series ()
{
	# Guilt
	#
	guilt=""

	__debug -n "       Guilt"

	if ( __cmd_exists guilt && test -d $GIT_DIR/patches && guilt status >/dev/null 2>&- )
	then
		applied=$(guilt applied 2>/dev/null | wc -l)
		unapplied=$(guilt unapplied 2>/dev/null | wc -l)
		if [ $(($applied + $unapplied)) -gt 0 ]
		then
			guilt=" "$c_$_guilt_colors[applied]$_C
			while [ $applied -gt 0 ]
			do
				guilt=$guilt"·"
				applied=$(($applied - 1))
			done
			guilt=$guilt$c_$_guilt_colors[unapplied]$_C
			while [ $unapplied -gt 0 ]
			do
				guilt=$guilt"·"
				unapplied=$(($unapplied - 1))
			done
			guilt=$guilt$C_$colors[none]$_C
		fi
	fi
	__debug

	echo $guilt
}

# We *must* have previously checked that
# we obtained a correct GIT branch with
# a call to `__get_git_branch`
__get_git_branch_status ()
{
	local my_git_branch_status cached changed managment_folder

	GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
	[ -z "$GIT_DIR" ] && return

	if [ "$(git config --get zsh.check-status)" = "false" ]
	then
		return
	fi

	my_git_branch_status=$_gcl_colors[uptodate];

	__debug -n "		where to..."

	if [ -f ".repo/manifests.git/config" ]
	then
		echo $my_git_branch_status
		return
	fi

	if [ "$(git rev-parse --is-inside-git-dir 2>/dev/null)" = "true" -o "$(git config --get core.bare)" = "true" ] ; then
		echo "$_gcl_colors[gitdir]"
		return
	fi

	if   [ "$(git status --porcelain | cut -c1 | tr -d ' ?\n')" != "" ] ; then 
		# Got any character but « » or «?» in first column : staged changes
		cached="yes"
	fi
	if [ "$(git status --porcelain . | cut -c2 | tr -d ' ?\n')" != "" ] ; then 
		# Got any character but « » or «?» in second column : working tree changes
		changed="yes"
	fi

	__debug

	__debug -n "		cached/changed..."

	if [ "$cached" != "" -a "$changed" != "" ]
	then
		my_git_branch_status="$_gcl_colors[mixed]"
	elif [ "$cached" != "" ]
	then
		my_git_branch_status="$_gcl_colors[cached]"
	elif [ "$changed" != "" ]
	then
		my_git_branch_status="$_gcl_colors[changed]"
	elif [ "$(git cat-file -t HEAD 2>/dev/null)" != "commit" ]
	then
		if [ ! -z "$(git ls-files)" ] ; then
			my_git_branch_status="$_gcl_colors[cached]"
		else
			my_git_branch_status="$_gcl_colors[init]"
		fi
	fi

	__debug

	__debug -n "		merges..."
	if [ $(git ls-files --unmerged | wc -l) -gt 0 ]
	then
		my_git_branch_status="${_gcl_colors[merging]:+$_gcl_colors[merging];}$my_git_branch_status"
	fi

	echo $my_git_branch_status
	__debug
}

__zsh_status ()
{
	if [ $ZDOTDIR != "" ]
	then
		cd $ZDOTDIR >/dev/null
		_status="$(git describe --always)"
		if [ "$(git status --porcelain | cut -c1-2 | tr -d ' ?\n')" != "" ]
		then
			# Dirty
			return 1
		fi
	fi
	# Clean
	return 0
}

__normal_user ()
{
	if [ -e /etc/login.defs ]
	then
		eval `grep -v '^[$#]' /etc/login.defs | grep "^UID_" | tr -d '[:blank:]' | sed 's/^[A-Z_]\+/&=/'`
		[ \( $UID -ge $UID_MIN \) ]
	else
		[ "`whoami`" != "root" ]
	fi
}

__privileged_user ()
{
	! __normal_user
}

__expand_text()
{
	# Strips unprintable characters
	print -Pn -- "$(echo $@ | sed 's/\r.*//g;s/%{[^(%})]*%}//g;s/'$T_'//g;s/'$_T'//g')"
}

export _COLUMNS_OLD=0
__hbar()
{
	if [ $COLUMNS != $_COLUMNS_OLD ]
	then
        __debug -n "	Horizontal bar..."
		_COLUMNS_OLD=$COLUMNS
		HBAR_COLOR=$C_$_prompt_colors[bar]$_C$T_
		HBAR=$T_${${(l:${COLUMNS}::q:)}//q/$_tq_}$_T
        __debug
	fi
}

__get_prompt_lines()
{
	local lines
	lines=$( (__expand_text "$PS1 $@" ) | sed "s/\\(.\{,$COLUMNS\}\\)/\\1\n/g" )
	lines=$( echo "$lines" | sed -n '/^$/n;p' | wc -l )
	# Got number of empty lines at end of command, because they are screwed up above...
	lines=$(( $lines + $( echo -n "$@" | tr ';\n' '.;' | sed 's/^\(.*[^;]\)\(;*\)$/\2/' | wc -c ) ))

	echo $lines
}

# Rewrites current prompt.
__redisplay_ps1 ()
{
    #__redefine_prompt

    tput sc
    up_up - 1
    print -Pn "$PS1"
    tput rc
}
zle -D reset-prompt
zle -N reset-prompt __redisplay_ps1

__clear ()
{
    tput clear
    [ -n "$1" ] && tput cup $1
}

up_up ()
{
    local _up=""
    for i in {1..$(($(__get_prompt_lines) $@ ))}
        _up+=$_cuu1_
    print -Pn $_up'\r'
}

_rehash ()
{
    builtin rehash
    __redisplay_ps1
}
zle -N _rehash


# Process helper
_process_tree()
{
	FULLCMD=0
	if [ "$1" = "-f" ]
	then
		FULLCMD=1
		shift
	fi

	for leaf in ${@:-$$}
	do
		ps -wweo pid,ppid,command | awk -v fullcmd=$FULLCMD -v leaf="$leaf" \
			'{
				parent[$1]=$2 ;
				command[$1]=$3 ;
				if (fullcmd && NF>=4) {
					for (i=4; i<=NF; i++) {
					command[$1]=command[$1]" "$i ;
					}
				}
			}
			function print_ancestry(pid)
			{
				if (pid != 1) { print_ancestry(parent[pid]) ; printf " :: " }
				printf command[pid];
			};
			END {
				print_ancestry(leaf)
				print ""
			}'
	done
}

# Asks confirmation before executing parameters
confirm() {
	read -q "?Are you sure? " && $@
}