// PreviousLine moves cursor to beginning of the line n lines up.
9739
func (c *Cursor) PreviousLine(n int) {
	fmt.Fprintf(c.Out, "\x1b[%dF", n)
}
264597
func (p Pos) Before(u Pos) bool {
	return u.Offset > p.Offset || u.Line > p.Line
}
512285
func FirstLine(src string) (line string, err error) {
	err = Filter(src, func(_ int, l []byte) (error) {
		line = string(l)

		return io.EOF
	})

	return
}
211922
func (s *ShapeIndexIterator) Prev() bool {
	if s.position <= 0 {
		return false
	}

	s.position--
	s.refresh()
	return true
}
524951
func (s *Statement) Line() *Statement {
	t := token{
		typ:     layoutToken,
		content: "\n",
	}
	*s = append(*s, t)
	return s
}
562145
func (e *Element) Prev() *Element {
	if p := e.prev; e.list != nil && p != &e.list.root {
		return p
	}
	return nil
}
513597
func (calc *LineCentroidCalculator) AddLine(line *geom.LineString) *LineCentroidCalculator {
	coords := line.FlatCoords()
	calc.addLine(coords, 0, len(coords))
	return calc
}
868123
func (s *IPRoleList) PreviousPage() (*IPRoleList, error) {
	if !s.HasPreviousPage() {
		return nil, Error{"No previous page"}
	}

	return s.getPage(s.Meta.NextPageUri)
}
136452
func (l *lexer) newline() {
	// adjust the counters for the new line
	if l._previous == nil {
		l._previous = make([]int, 0)
	}
	l._previous = append(l._previous, l._column)
	l._column = 1
	l._line++
}
1162316
func (c *IPMemberList) PreviousPage() (*IPMemberList, error) {
	if !c.HasPreviousPage() {
		return nil, Error{"No previous page"}
	}

	return c.getPage(c.Meta.NextPageUri)
}
137403
func (d *Document) lineStartIndexes() []int {
	// TODO: Cache, because this is often reused.
	// (If it is used, it's often used many times.
	// And this has to be fast for editing big documents!)
	lc := d.LineCount()
	lengths := make([]int, lc)
	for i, l := range d.Lines() {
		lengths[i] = len(l)
	}

	// Calculate cumulative sums.
	indexes := make([]int, lc+1)
	indexes[0] = 0 // https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/document.py#L189
	pos := 0
	for i, l := range lengths {
		pos += l + 1
		indexes[i+1] = pos
	}
	if lc > 1 {
		// Pop the last item. (This is not a new line.)
		indexes = indexes[:lc]
	}
	return indexes
}
1118630
func (d *Dispenser) Line() int {
	if d.cursor < 0 || d.cursor >= len(d.tokens) {
		return 0
	}
	return d.tokens[d.cursor].Line
}
558468
func (b *Builder) priorCommentLines(pos token.Pos, lines int) *ast.CommentGroup {
	position := b.fset.Position(pos)
	key := fileLine{position.Filename, position.Line - lines}
	return b.endLineToCommentGroup[key]
}
669886
func (c *Cursor) Prev() *Position {
	n := c.curr.cmp(&c.start)
	if n == 0 {
		if c.pi == 0 {
			return nil
		}
		c.pi--
		if c.ps[c.pi].IP.To4() != nil {
			c.curr = c.ps[c.pi].lastIPv4MappedIPv6Int()
			c.end = c.curr
		}
		if c.ps[c.pi].IP.To16() != nil && c.ps[c.pi].IP.To4() == nil {
			c.curr = c.ps[c.pi].lastIPv6Int()
			c.end = c.curr
		}
		c.start = ipToIPv6Int(c.ps[c.pi].IP.To16())
	} else {
		c.curr.decr()
	}
	return c.Pos()
}
1041957
func (ln *Line) Read() (line string, err error) {
	var anotherLine []rune // For lines got from history.
	var isHistoryUsed bool // If the history has been accessed.
	var action keyAction

	in := bufio.NewReader(term.Input) // Read input.
	esc := make([]byte, 2)            // For escape sequences.
	extEsc := make([]byte, 3)         // Extended escape sequences.

	// Print the primary prompt.
	if err = ln.Prompt(); err != nil {
		return "", err
	}

	// == Detect change of window size.
	winSize := term.DetectWinSize()

	go func() {
		for {
			select {
			case <-winSize.Change: // Wait for.
				_, col, err := ln.ter.GetSize()
				if err != nil {
					ln.buf.columns = col
					ln.buf.refresh()
				}
			}
		}
	}()
	defer winSize.Close()

	for ; ; action = 0 {
		char, _, err := in.ReadRune()
		if err != nil {
			return "", inputError(err.Error())
		}

	_S:
		switch char {
		default:
			if err = ln.buf.insertRune(char); err != nil {
				return "", err
			}
			continue

		case sys.K_RETURN:
			line = ln.buf.toString()

			if ln.useHistory {
				ln.hist.Add(line)
			}
			if _, err = term.Output.Write(CRLF); err != nil {
				return "", outputError(err.Error())
			}
			return strings.TrimSpace(line), nil

		case sys.K_TAB:
			// TODO: disabled by now
			continue

		case sys.K_BACK, sys.K_CTRL_H:
			if err = ln.buf.deleteCharPrev(); err != nil {
				return "", err
			}
			continue

		case sys.K_CTRL_C:
			if err = ln.buf.insertRunes(CtrlC); err != nil {
				return "", err
			}
			if _, err = term.Output.Write(CRLF); err != nil {
				return "", outputError(err.Error())
			}

			ChanCtrlC <- 1 //TODO: is really necessary?

			if err = ln.Prompt(); err != nil {
				return "", err
			}
			continue
		case sys.K_CTRL_D:
			if err = ln.buf.insertRunes(CtrlD); err != nil {
				return "", err
			}
			if _, err = term.Output.Write(CRLF); err != nil {
				return "", outputError(err.Error())
			}

			ln.Restore()
			ChanCtrlD <- 1
			return "", ErrCtrlD

		// Escape sequence
		case sys.K_ESCAPE: // Ctrl+[ ("\x1b" in hexadecimal, "033" in octal)
			if _, err = in.Read(esc); err != nil {
				return "", inputError(err.Error())
			}

			if esc[0] == 79 { // 'O'
				switch esc[1] {
				case 72: // Home: "\x1b O H"
					action = _HOME
					break _S
				case 70: // End: "\x1b O F"
					action = _END
					break _S
				}
			}

			if esc[0] == 91 { // '['
				switch esc[1] {
				case 65: // Up: "\x1b [ A"
					if !ln.useHistory {
						continue
					}
					action = _UP
					break _S
				case 66: // Down: "\x1b [ B"
					if !ln.useHistory {
						continue
					}
					action = _DOWN
					break _S
				case 68: // "\x1b [ D"
					action = _LEFT
					break _S
				case 67: // "\x1b [ C"
					action = _RIGHT
					break _S
				}

				// Extended escape.
				if esc[1] > 48 && esc[1] < 55 {
					if _, err = in.Read(extEsc); err != nil {
						return "", inputError(err.Error())
					}

					if extEsc[0] == 126 { // '~'
						switch esc[1] {
						//case 50: // Insert: "\x1b [ 2 ~"

						case 51: // Delete: "\x1b [ 3 ~"
							if err = ln.buf.deleteChar(); err != nil {
								return "", err
							}
							continue
							//case 53: // RePag: "\x1b [ 5 ~"

							//case 54: // AvPag: "\x1b [ 6 ~"

						}
					}
					if esc[1] == 49 && extEsc[0] == 59 && extEsc[1] == 53 { // "1;5"
						switch extEsc[2] {
						case 68: // Ctrl+left arrow: "\x1b [ 1 ; 5 D"
							// move to last word
							if err = ln.buf.wordBackward(); err != nil {
								return "", err
							}
							continue
						case 67: // Ctrl+right arrow: "\x1b [ 1 ; 5 C"
							// move to next word
							if err = ln.buf.wordForward(); err != nil {
								return "", err
							}
							continue
						}
					}
				}
			}
			continue

		case sys.K_CTRL_T: // Swap actual character by the previous one.
			if err = ln.buf.swap(); err != nil {
				return "", err
			}
			continue

		case sys.K_CTRL_L: // Clear screen.
			if _, err = term.Output.Write(DelScreenToUpper); err != nil {
				return "", err
			}
			if err = ln.Prompt(); err != nil {
				return "", err
			}
			continue
		case sys.K_CTRL_U: // Delete the whole line.
			if err = ln.buf.deleteLine(); err != nil {
				return "", err
			}
			if err = ln.Prompt(); err != nil {
				return "", err
			}
			continue
		case sys.K_CTRL_K: // Delete from current to end of line.
			if err = ln.buf.deleteToRight(); err != nil {
				return "", err
			}
			continue

		case sys.K_CTRL_P: // Up
			if !ln.useHistory {
				continue
			}
			action = _UP
		case sys.K_CTRL_N: // Down
			if !ln.useHistory {
				continue
			}
			action = _DOWN
		case sys.K_CTRL_B: // Left
			action = _LEFT
		case sys.K_CTRL_F: // Right
			action = _RIGHT

		case sys.K_CTRL_A: // Start of line.
			action = _HOME
		case sys.K_CTRL_E: // End of line.
			action = _END
		}

		switch action {
		case _UP, _DOWN: // Up and down arrow: history
			if action == _UP {
				anotherLine, err = ln.hist.Prev()
			} else {
				anotherLine, err = ln.hist.Next()
			}
			if err != nil {
				continue
			}

			// Update the current history entry before to overwrite it with
			// the next one.
			// TODO: it has to be removed before of to be saved the history
			if !isHistoryUsed {
				ln.hist.Add(ln.buf.toString())
			}
			isHistoryUsed = true

			ln.buf.grow(len(anotherLine))
			ln.buf.size = len(anotherLine) + ln.buf.promptLen
			copy(ln.buf.data[ln.lenPS1:], anotherLine)

			if err = ln.buf.refresh(); err != nil {
				return "", err
			}
			continue
		case _LEFT:
			if _, err = ln.buf.backward(); err != nil {
				return "", err
			}
			continue
		case _RIGHT:
			if _, err = ln.buf.forward(); err != nil {
				return "", err
			}
			continue
		case _HOME:
			if err = ln.buf.start(); err != nil {
				return "", err
			}
			continue
		case _END:
			if _, err = ln.buf.end(); err != nil {
				return "", err
			}
			continue
		}
	}
}
249577
func (e *PacketElement) Prev() *PacketElement {
	if p := e.prev; e.list != nil && p != &e.list.root {
		return p
	}
	return nil
}
256606
func (pc *Periodic) Previous(ref time.Time) (p *Period) {
	p = &Period{}
	if pc.start.dayOfWeek != -1 { // Weekly
		if pc.cmpDayOfWeek(ref) >= 0 {
			// this week
			p.Start = pc.shiftTimeByDays(ref, -(int(ref.Weekday()) - pc.start.dayOfWeek))
		} else {
			// last week
			p.Start = pc.shiftTimeByDays(ref, -(int(ref.Weekday()) + (7 - pc.start.dayOfWeek)))
		}
	} else if pc.start.hourOfDay != -1 { // Daily
		if pc.cmpHourOfDay(ref) >= 0 {
			// today
			p.Start = pc.shiftTimeByDays(ref, 0)
		} else {
			// yesterday
			p.Start = pc.shiftTimeByDays(ref, -1)
		}
	} // XXX(mischief): other intervals unsupported atm.

	p.End = p.Start.Add(pc.duration)
	return
}