Subject: bug#21782: 25.0.50; New functions nfront/front

From: Tino Calancha <[email-masked]>
Date: Fri, 30 Oct 2015 11:10:47 +0900 (JST)

The new patch has more sense to me.

I correct my previous wish about the func. behaviour with respect N:
*) N nil give same output as N = 1.
*) N <= 0 return nil.

(let ((ltest '(1 2 3 4 5 6)))
   (nfront ltest 0))
nil
(let ((ltest '(1 2 3 4 5 6)))
   (nfront ltest -1))
nil
(let ((ltest '(1 2 3 4 5 6)))
   (nfront ltest 3))
(1 2 3)
(let ((ltest '(1 2 3 4 5 6)))
   (nfront ltest))
(1)
(let ((ltest '(1 2 3 4 5 6)))
   (nfront ltest 100))
(1 2 3 4 5 6)

diff --git a/lisp/subr.el b/lisp/subr.el
index ea926ae..cde4006 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -391,13 +391,15 @@ last
 (defun butlast (list &optional n)
   "Return a copy of LIST with the last N elements removed.
 If N is omitted or nil, the last element is removed from the
-copy."
+copy.
+When N is <= 0 LIST is returned."
   (if (and n (<= n 0)) list
     (nbutlast (copy-sequence list) n)))
 
 (defun nbutlast (list &optional n)
   "Modifies LIST to remove the last N elements.
-If N is omitted or nil, remove the last element."
+If N is omitted or nil, remove the last element.
+When N is <= 0 LIST is returned unmodified."
   (let ((m (length list)))
     (or n (setq n 1))
     (and (< n m)
@@ -405,6 +407,20 @@ nbutlast
 	   (if (> n 0) (setcdr (nthcdr (- (1- m) n) list) nil))
 	   list))))
 
+(defun front(list &optional n)
+  "Return a copy of LIST with just the first N elements.
+If N is omitted or nil copy just the first element
+When N is <= 0 return nil."
+(if (or (null n) (> n 0))
+  (nfront (copy-sequence list) n)))
+
+(defun nfront(list &optional n)
+  "Modified LIST to remove all elements but the first N.
+If N is omitted or nil remove all but the first element
+When N is <= 0 return nil."
+  (or n (setq n 1))
+  (if (> n 0) (nreverse (last (nreverse list) n))))
+
 (defun zerop (number)
   "Return t if NUMBER is zero."
   ;; Used to be in C, but it's pointless since (= 0 n) is faster anyway because