From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Leo Liu Newsgroups: gmane.emacs.bugs Subject: bug#18652: 25.0.50; [PATCH] Add fresh-line Date: Wed, 08 Oct 2014 13:08:57 +0800 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1412745497 29717 80.91.229.3 (8 Oct 2014 05:18:17 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 8 Oct 2014 05:18:17 +0000 (UTC) Cc: 18652@debbugs.gnu.org To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Oct 08 07:18:11 2014 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XbjdW-0006Bu-Av for geb-bug-gnu-emacs@m.gmane.org; Wed, 08 Oct 2014 07:18:06 +0200 Original-Received: from localhost ([::1]:33930 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XbjVx-0005hq-Ul for geb-bug-gnu-emacs@m.gmane.org; Wed, 08 Oct 2014 01:10:17 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48853) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XbjVo-0005fK-NG for bug-gnu-emacs@gnu.org; Wed, 08 Oct 2014 01:10:14 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XbjVi-0001cI-Py for bug-gnu-emacs@gnu.org; Wed, 08 Oct 2014 01:10:08 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:45737) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XbjVi-0001c6-L3 for bug-gnu-emacs@gnu.org; Wed, 08 Oct 2014 01:10:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1XbjVh-0008P9-PM for bug-gnu-emacs@gnu.org; Wed, 08 Oct 2014 01:10:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Leo Liu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 08 Oct 2014 05:10:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 18652 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 18652-submit@debbugs.gnu.org id=B18652.141274494932217 (code B ref 18652); Wed, 08 Oct 2014 05:10:01 +0000 Original-Received: (at 18652) by debbugs.gnu.org; 8 Oct 2014 05:09:09 +0000 Original-Received: from localhost ([127.0.0.1]:37301 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1XbjUq-0008NV-4A for submit@debbugs.gnu.org; Wed, 08 Oct 2014 01:09:08 -0400 Original-Received: from mail-pd0-f172.google.com ([209.85.192.172]:51383) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1XbjUn-0008NC-2D for 18652@debbugs.gnu.org; Wed, 08 Oct 2014 01:09:05 -0400 Original-Received: by mail-pd0-f172.google.com with SMTP id ft15so6321691pdb.3 for <18652@debbugs.gnu.org>; Tue, 07 Oct 2014 22:09:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:face:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=CuhBsChwGWdyNPiH0BA9XoL2LjDGkST5Yy8SviZHkvU=; b=frztGzEjjp1PLFHo75MJVUPeV4Y1FFVO5FF3UTRfHKw/YMA2HIeVLMxYLpvdTsYqDn 00y3XCO6dpvIuk9cVN6Ei6kt0NZaT6OI1czGB4jSNHOezKFbwYhWYGcLYPluQ7TUIHvT fC1cNRnKAcEX+V1hkyWKsuJZrjqb2P2SRexnklsHGgoz4U/ZuR3jUclA593mOXsJNv1x O52Abu4pSXjd0qqbFCClgTR1yphwQdgu2b/GXyUthPDIz+a3d11LAeCYkUjIgPWjts75 ottW5Xr2BkanR6OKfsoFFYZXQHPkeosPJ65HSFFL8EH/n3Y7cyQ/Hbq+EHrF+NbQxSJa i8OA== X-Received: by 10.66.222.199 with SMTP id qo7mr6511875pac.134.1412744943910; Tue, 07 Oct 2014 22:09:03 -0700 (PDT) Original-Received: from fortuna ([221.222.150.213]) by mx.google.com with ESMTPSA id f12sm17710789pat.36.2014.10.07.22.09.01 for (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Tue, 07 Oct 2014 22:09:03 -0700 (PDT) Face: iVBORw0KGgoAAAANSUhEUgAAACgAAAAoBAMAAAB+0KVeAAAAGFBMVEUzRVhbQj4eZqO6SjnT eWpxnMetm5b6/PmidmqrAAAAAWJLR0QAiAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1F B9cBBwMLBfKABCMAAAFoSURBVCjPtZI9a8MwEIaFoc7aYDdelQMna0Em3tsSr0XUeE2Q6a22a+v+ fk8fSSBkbDUI6dHpfe9OEvRgiD+ApqKPJgJeB6iUUXWESjUe/ig38AJrhqqvaU2nTIXbNvOQ40fe qdry4kyGoVWsfCQalXpHnJGM01wjWdYbMlXNFdsZDO69m9aqNqxEJqTEgbM5OF7wlEfIoll1Ked4 LbM5X2EdILLokEdmI8z7g5cKED0cuTC930TYhy7ZDekkXVGw/L60TguJePPxcJF48lpsSUWEA/Ju jGFNgJOXc4Hz7TmAdBeu5Ve4AEjOi2/2jfd3cAJZ+IbNrvdjgBZY01b+HTuG3cLws6BJZqVOj/pp T0OqVwx3rFq+QmJwx3loK5JSLEhDIt62+mtC2C+SrAUxEbV6C6v2BRbd6pILBKFpepKZJHgGgrKF sptSUUoczpwg2pQ7ZH1tgs0ou/917mzz6Cs2//C978cv5l07L02orIEAAAAASUVORK5CYII= In-Reply-To: (Leo Liu's message of "Wed, 08 Oct 2014 09:54:28 +0800") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (CentOS 6.5) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:94272 --=-=-= Content-Type: text/plain On 2014-10-08 09:54 +0800, Leo Liu wrote: > How about something like this? Thanks, Leo. This is the full patch (documentation, tests etc.) without the changelog entries. Also improved to support print to stdout in noninteractive mode. Comments? Thanks, Leo === modified file 'doc/lispref/streams.texi' --- doc/lispref/streams.texi 2014-03-18 01:19:03 +0000 +++ doc/lispref/streams.texi 2014-10-08 02:36:52 +0000 @@ -621,6 +621,13 @@ for ``terminate print''. @end defun +@defun fresh-line &optional stream +@cindex fresh line in print +This function outputs a newline to @var{stream} unless already at the +beginning of a line. Return @code{t} if a newline is printed. Signal +an error if @var{stream} is a function. +@end defun + @defun write-char character &optional stream This function outputs @var{character} to @var{stream}. It returns @var{character}. === modified file 'src/print.c' --- src/print.c 2014-09-11 06:21:55 +0000 +++ src/print.c 2014-10-08 04:58:12 +0000 @@ -58,6 +58,9 @@ #define PRINT_CIRCLE 200 static Lisp_Object being_printed[PRINT_CIRCLE]; +/* Last char printed to stdout by printchar. */ +static unsigned int printchar_stdout_last; + /* When printing into a buffer, first we put the text in this block, then insert it all at once. */ static char *print_buffer; @@ -238,6 +241,7 @@ } else if (noninteractive) { + printchar_stdout_last = ch; fwrite (str, 1, len, stdout); noninteractive_need_newline = 1; } @@ -530,6 +534,32 @@ return Qt; } +DEFUN ("fresh-line", Ffresh_line, Sfresh_line, 0, 1, 0, + doc: /* Output a newline unless already at the beginning of a line. +Value is non-nil if a newline is printed. +Signal an error if PRINTCHARFUN is a function. */) + (Lisp_Object printcharfun) +{ + Lisp_Object val = Qnil; + + PRINTDECLARE; + if (NILP (printcharfun)) + printcharfun = Vstandard_output; + PRINTPREPARE; + + if (FUNCTIONP (printcharfun)) + signal_error ("Unsupported function argument", printcharfun); + + if (noninteractive && !NILP (printcharfun)) + val = printchar_stdout_last == 10 ? Qnil : Qt; + else if (NILP (Fbolp ())) + val = Qt; + + if (!NILP (val)) PRINTCHAR ('\n'); + PRINTFINISH; + return val; +} + DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0, doc: /* Output the printed representation of OBJECT, any Lisp object. Quoting characters are printed when needed to make output that `read' @@ -2334,6 +2364,7 @@ defsubr (&Sprinc); defsubr (&Sprint); defsubr (&Sterpri); + defsubr (&Sfresh_line); defsubr (&Swrite_char); #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT defsubr (&Sredirect_debugging_output); --=-=-= Content-Type: text/plain Content-Disposition: attachment; filename=print-tests.el ;;; print-tests.el --- tests for src/print.c -*- lexical-binding: t; -*- ;; Copyright (C) 2014 Free Software Foundation, Inc. ;; This file is part of GNU Emacs. ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Code: (require 'ert) (ert-deftest fresh-line () (should (string= (with-output-to-string (princ 'abc) (should (fresh-line))) "abc\n")) (should (string= (with-output-to-string (should-not (fresh-line)) (princ 'xyz)) "xyz")) (message nil) (if noninteractive (progn (should (fresh-line)) (should-not (fresh-line)) (princ 'abc) (should (fresh-line)) (should-not (fresh-line))) (should (string= (progn (should-not (fresh-line)) (princ 'abc) (should (fresh-line)) (current-message)) "abc\n"))) (let ((standard-output (with-current-buffer (get-buffer-create "*fresh-line-test*") (insert "--------") (point-max-marker)))) (should (fresh-line)) (should-not (fresh-line)) (should (string= (with-current-buffer (marker-buffer standard-output) (buffer-string)) "--------\n")))) (provide 'print-tests) ;;; print-tests.el ends here --=-=-=--