Function: previous-line-or-history-element

previous-line-or-history-element is an interactive and byte-compiled function defined in simple.el.gz.

Signature

(previous-line-or-history-element &optional ARG)

Documentation

Move cursor vertically up ARG lines, or to the previous history element.

When point moves over the top line of multi-line minibuffer, puts ARGth previous element of the minibuffer history in the minibuffer.

View in manual

Probably introduced at or before Emacs version 25.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun previous-line-or-history-element (&optional arg)
  "Move cursor vertically up ARG lines, or to the previous history element.
When point moves over the top line of multi-line minibuffer, puts ARGth
previous element of the minibuffer history in the minibuffer."
  (interactive "^p")
  (or arg (setq arg 1))
  (let* ((old-point (point))
	 ;; Remember the original goal column of possibly multi-line input
	 ;; excluding the length of the prompt on the first line.
	 (prompt-end (minibuffer-prompt-end))
	 (old-column (unless (and (eolp) (> (point) prompt-end))
		       (if (= (line-number-at-pos) 1)
			   (max (- (current-column)
				   (save-excursion
				     (goto-char (1- prompt-end))
				     (current-column)))
				1)
			 (current-column)))))
    (condition-case nil
	(with-no-warnings
	  (previous-line arg)
          ;; Avoid moving point to the prompt
          (when (< (point) (minibuffer-prompt-end))
            ;; If there is minibuffer contents on the same line
            (if (<= (minibuffer-prompt-end)
                    (save-excursion
                      (if (or truncate-lines (not line-move-visual))
                          (end-of-line)
                        (end-of-visual-line))
                      (point)))
                ;; Move to the beginning of minibuffer contents
                (goto-char (minibuffer-prompt-end))
              ;; Otherwise, go to the previous history element
              (signal 'beginning-of-buffer nil))))
      (beginning-of-buffer
       ;; Restore old position since `line-move-visual' moves point to
       ;; the beginning of the line when it fails to go to the previous line.
       (goto-char old-point)
       (previous-history-element arg)
       ;; Reset `temporary-goal-column' because a correct value is not
       ;; calculated when `previous-line' above fails by bumping against
       ;; the top of the minibuffer (bug#22544).
       (setq temporary-goal-column 0)
       ;; Restore the original goal column on the first line
       ;; of possibly multi-line input.
       (goto-char (minibuffer-prompt-end))
       (if old-column
	   (if (= (line-number-at-pos) 1)
	       (move-to-column (+ old-column
				  (save-excursion
				    (goto-char (1- (minibuffer-prompt-end)))
				    (current-column))))
	     (move-to-column old-column))
	 (if (not line-move-visual) ; Handle logical lines (bug#42862)
	     (end-of-line)
	   ;; Put the cursor at the end of the visual line instead of the
	   ;; logical line, so the next `previous-line-or-history-element'
	   ;; would move to the previous history element, not to a possible upper
	   ;; visual line from the end of logical line in `line-move-visual' mode.
	   (end-of-visual-line)
	   ;; Since `end-of-visual-line' puts the cursor at the beginning
	   ;; of the next visual line, move it one char back to the end
	   ;; of the first visual line (bug#22544).
	   (unless (eolp) (backward-char 1))))))))