changeset: 5535:25325da1d1a8
user: Stephen J. Turnbull <stephen(a)xemacs.org>
date: Mon Aug 08 13:57:18 2011 +0900
files: src/ChangeLog src/dumper.c
description:
Suppress the "no prototype" warning for pdump_file_try.
diff -r 68db75473fc6 -r 25325da1d1a8 src/ChangeLog
--- a/src/ChangeLog Sun Aug 07 23:22:59 2011 +0200
+++ b/src/ChangeLog Mon Aug 08 13:57:18 2011 +0900
@@ -1,3 +1,7 @@
+2011-08-03 Stephen J. Turnbull <stephen(a)xemacs.org>
+
+ * dumper.c (pdump_file_try): Add prototype to suppress GCC warning.
+
2011-06-25 Aidan Kehoe <kehoea(a)parhasard.net>
* console.c:
diff -r 68db75473fc6 -r 25325da1d1a8 src/dumper.c
--- a/src/dumper.c Sun Aug 07 23:22:59 2011 +0200
+++ b/src/dumper.c Mon Aug 08 13:57:18 2011 +0900
@@ -2595,6 +2595,7 @@
/* This used to be static, but there seems to be a bug in the GCC 4.1.2
optimizer that clobbers exe_path. */
+int pdump_file_try (Wexttext*);
int
pdump_file_try (Wexttext *exe_path)
{
_______________________________________________
XEmacs-Patches mailing list
XEmacs-Patches(a)xemacs.org
http://lists.xemacs.org/mailman/listinfo/xemacs-patches
Hi,
APPROVE COMMIT
NOTE: This patch has been committed.
# HG changeset patch
# User Mats Lidell <mats.lidell(a)cag.se>
# Date 1312068549 -7200
# Node ID 11da5b828d1047278de9af4978ff0755fc25748a
# Parent 1b054bc2ac40913a80076e3958dc7b81e840253d
shell-command and shell-command-on-region API compliant with FSF 23.3.1
diff -r 1b054bc2ac40 -r 11da5b828d10 lisp/ChangeLog
--- a/lisp/ChangeLog Sun Jul 03 14:17:39 2011 +0100
+++ b/lisp/ChangeLog Sun Jul 31 01:29:09 2011 +0200
@@ -1,3 +1,9 @@
+2011-07-29 Mats Lidell <matsl(a)xemacs.org>
+
+ * process.el (shell-command):
+ * process.el (shell-command-on-region): API compatible/synced with
+ FSF 23.3.1.
+
2011-07-03 Aidan Kehoe <kehoea(a)parhasard.net>
* cl.el (cl-macroexpand):
diff -r 1b054bc2ac40 -r 11da5b828d10 lisp/process.el
--- a/lisp/process.el Sun Jul 03 14:17:39 2011 +0100
+++ b/lisp/process.el Sun Jul 31 01:29:09 2011 +0200
@@ -1,6 +1,6 @@
;;; process.el --- commands for subprocesses; split out of simple.el
-;; Copyright (C) 1985-7, 1993,4, 1997 Free Software Foundation, Inc.
+;; Copyright (C) 1985-7, 1993,4, 1997, 2011 Free Software Foundation, Inc.
;; Copyright (C) 1995, 2000, 2001, 2002 Ben Wing.
;; Author: Ben Wing
@@ -24,6 +24,7 @@
;;; Synched up with: FSF 19.30, except for setenv/getenv (synched with FSF
;;; 21.2.1).
+;;; shell-command and shell-command-on-region synced with FSF 23.3.1.
;;; Authorship:
@@ -57,6 +58,12 @@
(defvar shell-command-switch "-c"
"Switch used to have the shell execute its command line argument.")
+(defvar shell-command-default-error-buffer nil
+ "*Buffer name for `shell-command' and `shell-command-on-region' error output.
+This buffer is used when `shell-command' or `shell-command-on-region'
+is run interactively. A value of nil means that output to stderr and
+stdout will be intermixed in the output stream.")
+
(defun start-process-shell-command (name buffer &rest args)
"Start a program in a subprocess. Return the process object for it.
NAME is name for process. It is modified if necessary to make it unique.
@@ -316,12 +323,12 @@
(error nil)))))
-(defun shell-command (command &optional output-buffer)
+(defun shell-command (command &optional output-buffer error-buffer)
"Execute string COMMAND in inferior shell; display output, if any.
-If COMMAND ends in ampersand, execute it asynchronously.
-The output appears in the buffer `*Async Shell Command*'.
-That buffer is in shell mode.
+If COMMAND ends in ampersand, execute it asynchronously. The command
+is executed using the background package. See `background' for
+details.
Otherwise, COMMAND is executed synchronously. The output appears in the
buffer `*Shell Command Output*'.
@@ -336,9 +343,16 @@
If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
If OUTPUT-BUFFER is not a buffer and not nil,
insert output in current buffer. (This cannot be done asynchronously.)
-In either case, the output is inserted after point (leaving mark after it)."
+In either case, the output is inserted after point (leaving mark after it).
+
+If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
+or buffer name to which to direct the command's standard error output.
+If it is nil, error output is mingled with regular output. In an
+interactive call, the variable `shell-command-default-error-buffer'
+specifies the value of ERROR-BUFFER."
(interactive (list (read-shell-command "Shell command: ")
- current-prefix-arg))
+ current-prefix-arg
+ shell-command-default-error-buffer))
(if (and output-buffer
(not (or (bufferp output-buffer) (stringp output-buffer))))
(progn (barf-if-buffer-read-only)
@@ -363,7 +377,7 @@
'unimplemented
"backgrounding a shell command requires package `background'")))
- (shell-command-on-region (point) (point) command output-buffer)))))
+ (shell-command-on-region (point) (point) command output-buffer nil error-buffer)))))
;; We have a sentinel to prevent insertion of a termination message
;; in the buffer itself.
@@ -374,7 +388,8 @@
(substring signal 0 -1))))
(defun shell-command-on-region (start end command
- &optional output-buffer replace)
+ &optional output-buffer replace
+ error-buffer display-error-buffer)
"Execute string COMMAND in inferior shell with region as input.
Normally display output (if any) in temp buffer `*Shell Command Output*';
Prefix arg means replace the region with it.
@@ -394,7 +409,15 @@
If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
If OUTPUT-BUFFER is not a buffer and not nil,
insert output in the current buffer.
-In either case, the output is inserted after point (leaving mark after it)."
+In either case, the output is inserted after point (leaving mark after it).
+
+If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
+or buffer name to which to direct the command's standard error output.
+If it is nil, error output is mingled with regular output.
+If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
+were any errors. (This is always t, interactively.)
+In an interactive call, the variable `shell-command-default-error-buffer'
+specifies the value of ERROR-BUFFER."
(interactive (let ((string
;; Do this before calling region-beginning
;; and region-end, in case subprocess output
@@ -405,79 +428,142 @@
(list (region-beginning) (region-end)
string
current-prefix-arg
- current-prefix-arg)))
- (if (or replace
- (and output-buffer
- (not (or (bufferp output-buffer) (stringp output-buffer)))))
- ;; Replace specified region with output from command.
- (let ((swap (and replace (< start end))))
- ;; Don't muck with mark unless REPLACE says we should.
- (goto-char start)
- (and replace (push-mark))
- (call-process-region start end shell-file-name t t nil
- shell-command-switch command)
- (let ((shell-buffer (get-buffer "*Shell Command Output*")))
- (and shell-buffer (not (eq shell-buffer (current-buffer)))
- (kill-buffer shell-buffer)))
- ;; Don't muck with mark unless REPLACE says we should.
- (and replace swap (exchange-point-and-mark t)))
+ current-prefix-arg
+ shell-command-default-error-buffer
+ t)))
+ (let ((error-file
+ (if error-buffer
+ (make-temp-file
+ (expand-file-name "scor"
+ (or small-temporary-file-directory
+ (temp-directory))))
+ nil))
+ (exit-status nil))
+ (if (or replace
+ (and output-buffer
+ (not (or (bufferp output-buffer) (stringp output-buffer)))))
+ ;; Replace specified region with output from command.
+ (let ((swap (and replace (< start end))))
+ ;; Don't muck with mark unless REPLACE says we should.
+ (goto-char start)
+ (and replace (push-mark))
+ (setq exit-status (call-process-region start end shell-file-name t
+ (if error-file
+ (list t error-file)
+ t)
+ nil shell-command-switch command))
+ (let ((shell-buffer (get-buffer "*Shell Command Output*")))
+ (and shell-buffer (nnot (eq shell-buffer (current-buffer)))
+ (kill-buffer shell-buffer)))
+ ;; Don't muck with mark unless REPLACE says we should.
+ (and replace swap (exchange-point-and-mark t)))
;; No prefix argument: put the output in a temp buffer,
;; replacing its entire contents.
- (let ((buffer (get-buffer-create
- (or output-buffer "*Shell Command Output*")))
- (success nil)
- (exit-status nil)
- (directory default-directory))
- (unwind-protect
- (if (eq buffer (current-buffer))
- ;; If the input is the same buffer as the output,
- ;; delete everything but the specified region,
- ;; then replace that region with the output.
- (progn (setq buffer-read-only nil)
- (delete-region (max start end) (point-max))
- (delete-region (point-min) (min start end))
- (setq exit-status
- (call-process-region (point-min) (point-max)
- shell-file-name t t nil
- shell-command-switch command))
- (setq success t))
- ;; Clear the output buffer,
- ;; then run the command with output there.
- (save-excursion
- (set-buffer buffer)
- (setq buffer-read-only nil)
- ;; XEmacs change
- (setq default-directory directory)
- (erase-buffer))
- (setq exit-status
- (call-process-region start end shell-file-name
- nil buffer nil
- shell-command-switch command))
- (setq success t))
- ;; Report the amount of output.
- (let ((lines (save-excursion
- (set-buffer buffer)
- (if (= (buffer-size) 0)
- 0
- (count-lines (point-min) (point-max))))))
- (cond ((= lines 0)
- (if success
- (display-message
- 'command
- (if (eql exit-status 0)
- "(Shell command succeeded with no output)"
- "(Shell command failed with no output)")))
- (kill-buffer buffer))
- ((and success (= lines 1))
- (message "%s"
- (save-excursion
- (set-buffer buffer)
- (goto-char (point-min))
- (buffer-substring (point)
- (progn (end-of-line)
- (point))))))
- (t
- (set-window-start (display-buffer buffer) 1))))))))
+ (let ((buffer (get-buffer-create
+ (or output-buffer "*Shell Command Output*")))
+ (directory default-directory))
+ (unwind-protect
+ (if (eq buffer (current-buffer))
+ ;; If the input is the same buffer as the output,
+ ;; delete everything but the specified region,
+ ;; then replace that region with the output.
+ (progn (setq buffer-read-only nil)
+ (delete-region (max start end) (point-max))
+ (delete-region (point-min) (min start end))
+ (setq exit-status
+ (call-process-region (point-min) (point-max)
+ shell-file-name t
+ (if error-file
+ (list t error-file)
+ t)
+ nil shell-command-switch
+ command)))
+ ;; Clear the output buffer,
+ ;; then run the command with output there.
+ (save-excursion
+ (set-buffer buffer)
+ (setq buffer-read-only nil)
+ ;; XEmacs change
+ (setq default-directory directory)
+ (erase-buffer))
+ (setq exit-status
+ (call-process-region start end shell-file-name
+ nil
+ (if error-file
+ (list buffer error-file)
+ buffer)
+ nil shell-command-switch command)))
+
+ ;; Report the output.
+ (with-current-buffer buffer
+ (setq modeline-process
+ (cond ((null exit-status)
+ " - Error")
+ ((stringp exit-status)
+ (format " - Signal [%s]" exit-status))
+ ((not (equal 0 exit-status))
+ (format " - Exit [%d]" exit-status)))))
+ (if (with-current-buffer buffer (> (point-max) (point-min)))
+ ;; There's some output, display it
+ (let ((lines (save-excursion
+ (set-buffer buffer)
+ (if (= (buffer-size) 0)
+ 0
+ (count-lines (point-min) (point-max))))))
+ (cond ((= lines 0)
+ (display-message
+ 'command
+ (if (eql exit-status 0)
+ "(Shell command succeeded with no output)"
+ "(Shell command failed with no output)"))
+ (kill-buffer buffer))
+ ((= lines 1)
+ (message "%s"
+ (save-excursion
+ (set-buffer buffer)
+ (goto-char (point-min))
+ (buffer-substring (point)
+ (progn (end-of-line)
+ (point))))))
+ (t
+ (set-window-start (display-buffer buffer) 1))))
+ ;; No output; error?
+ (let ((output
+ (if (and error-file
+ (< 0 (nth 7 (file-attributes error-file))))
+ "some error output"
+ "no output")))
+ (cond ((null exit-status)
+ (message "(Shell command failed with error)"))
+ ((equal 0 exit-status)
+ (message "(Shell command succeeded with %s)"
+ output))
+ ((stringp exit-status)
+ (message "(Shell command killed by signal %s)"
+ exit-status))
+ (t
+ (message "(Shell command failed with code %d and %s)"
+ exit-status output))))
+ ;; Don't kill: there might be useful info in the undo-log.
+ ;; (kill-buffer buffer)
+ ))))
+
+ (when (and error-file (file-exists-p error-file))
+ (if (< 0 (nth 7 (file-attributes error-file)))
+ (with-current-buffer (get-buffer-create error-buffer)
+ (let ((pos-from-end (- (point-max) (point))))
+ (or (bobp)
+ (insert "\f\n"))
+ ;; Do no formatting while reading error file,
+ ;; because that can run a shell command, and we
+ ;; don't want that to cause an infinite recursion.
+ (format-insert-file error-file nil)
+ ;; Put point after the inserted errors.
+ (goto-char (- (point-max) pos-from-end)))
+ (and display-error-buffer
+ (display-buffer (current-buffer)))))
+ (delete-file error-file))
+ exit-status))
(defun shell-quote-argument (argument)
"Quote an argument for passing as argument to an inferior shell."
diff -r 1b054bc2ac40 -r 11da5b828d10 tests/ChangeLog
--- a/tests/ChangeLog Sun Jul 03 14:17:39 2011 +0100
+++ b/tests/ChangeLog Sun Jul 31 01:29:09 2011 +0200
@@ -1,3 +1,7 @@
+2011-07-31 Mats Lidell <matsl(a)xemacs.org>
+
+ * automated/process-tests.el: shell-command tests.
+
2011-06-28 Aidan Kehoe <kehoea(a)parhasard.net>
* automated/mule-tests.el:
diff -r 1b054bc2ac40 -r 11da5b828d10 tests/automated/process-tests.el
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/automated/process-tests.el Sun Jul 31 01:29:09 2011 +0200
@@ -0,0 +1,70 @@
+;; Copyright (C) 2011 Free Software Foundation, Inc.
+
+;; Author: Mats Lidell <matsl(a)xemacs.org>
+;; Maintainer:
+;; Created: 2011
+;; Keywords: tests
+
+;; This file is part of XEmacs.
+
+;; XEmacs 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.
+
+;; XEmacs 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 XEmacs. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Synched up with: Not in FSF.
+
+;;; Commentary:
+
+;; Test tag support.
+;; See test-harness.el for instructions on how to run these tests.
+
+(require 'test-harness)
+
+(when (equal system-type 'linux)
+ (setenv "LANG" "C")
+
+ ;; One line output
+ (Assert (= 0 (shell-command "echo hello")))
+ (Assert (equal "hello" (message-displayed-p t)))
+ (with-current-buffer " *Echo Area*"
+ (goto-char (point-min))
+ (Assert (looking-at "hello")))
+
+ ;; Two lines. No output in echo area. (GNU resizes minibuffer but we
+ ;; haven't implemented that.)
+ (message "")
+ (Assert (= 0 (shell-command "echo -e \"foo\nbar\n\"")))
+ (with-current-buffer " *Echo Area*"
+ (Assert (= 0 (buffer-size))))
+ (with-current-buffer "*Shell Command Output*"
+ (goto-char (point-min))
+ (Assert (looking-at "foo"))):
+
+ (Assert (= 127 (shell-command "unknown_command")))
+ (Assert (= 2 (shell-command "exit 2")))
+ (Assert (equal "(Shell command failed with code 2 and no output)" (message-displayed-p t)))
+
+ ;; Output to stderr With error buffer
+ (Assert (= 0 (shell-command "echo -e \"foo\nbar\n\" 1>&2" "Output buffer" "Error buffer")))
+ (Assert (equal "(Shell command succeeded with some error output)" (message-displayed-p t)))
+ (with-current-buffer "Error buffer"
+ (goto-char (point-min))
+ (Assert (looking-at "foo")))
+ (with-current-buffer "Output buffer"
+ (Assert (= 0 (buffer-size))))
+
+ ;; Output to stderr but no error buffer
+ (Assert (= 0 (shell-command "echo -e \"foobar\nfoobar\n\" 1>&2" "Output buffer")))
+ (with-current-buffer "Output buffer"
+ (goto-char (point-min))
+ (Assert (looking-at "foobar")))
+)
Yours
--
%% Mats
_______________________________________________
XEmacs-Patches mailing list
XEmacs-Patches(a)xemacs.org
http://lists.xemacs.org/mailman/listinfo/xemacs-patches
Hi,
APPROVE COMMIT
NOTE: This patch has been committed.
# HG changeset patch
# User Mats Lidell <mats.lidell(a)cag.se>
# Date 1311115336 -7200
# Node ID 69a08906ad27984ef492876a55587653c12d133c
# Parent 1b054bc2ac40913a80076e3958dc7b81e840253d
Introducing syntax-ppss.el from GNU.
diff -r 1b054bc2ac40 -r 69a08906ad27 lisp/ChangeLog
--- a/lisp/ChangeLog Sun Jul 03 14:17:39 2011 +0100
+++ b/lisp/ChangeLog Wed Jul 20 00:42:16 2011 +0200
@@ -1,3 +1,7 @@
+2011-07-22 Mats Lidell <matsl(a)xemacs.or>
+
+ * syntax-ppss.el: Synced up with Emacs 23.3 (syntax.el)
+
2011-07-03 Aidan Kehoe <kehoea(a)parhasard.net>
* cl.el (cl-macroexpand):
diff -r 1b054bc2ac40 -r 69a08906ad27 lisp/dumped-lisp.el
--- a/lisp/dumped-lisp.el Sun Jul 03 14:17:39 2011 +0100
+++ b/lisp/dumped-lisp.el Wed Jul 20 00:42:16 2011 +0200
@@ -54,6 +54,7 @@
"cmdloop"
"keymap"
"syntax"
+ "syntax-ppss"
"device"
"console"
"obsolete"
diff -r 1b054bc2ac40 -r 69a08906ad27 lisp/syntax-ppss.el
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/syntax-ppss.el Wed Jul 20 00:42:16 2011 +0200
@@ -0,0 +1,288 @@
+;;; syntax-ppss.el --- helper functions to find syntactic context
+
+;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
+;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+
+;; Maintainer: XEmacs Development Team
+;; Keywords: internal
+
+;; This file is part of XEmacs.
+
+;; XEmacs 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.
+
+;; XEmacs 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 XEmacs. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Synched up with: FSF 23,3, syntax.el.
+
+;;; Commentary:
+
+;; The main exported function is `syntax-ppss'. You might also need
+;; to call `syntax-ppss-flush-cache' or to add it to
+;; before-change-functions'(although this is automatically done by
+;; syntax-ppss when needed, but that might fail if syntax-ppss is
+;; called in a context where before-change-functions is temporarily
+;; let-bound to nil).
+
+;;; Todo:
+
+;; - do something about the case where the syntax-table is changed.
+;; This typically happens with tex-mode and its `$' operator.
+;; - move font-lock-syntactic-keywords in here. Then again, maybe not.
+;; - new functions `syntax-state', ... to replace uses of parse-partial-state
+;; with something higher-level (similar to syntax-ppss-context).
+;; - interaction with mmm-mode.
+
+;;; Code:
+
+;; Note: PPSS stands for `parse-partial-sexp state'
+
+(eval-when-compile (require 'cl))
+
+(defvar font-lock-beginning-of-syntax-function)
+
+(defsubst syntax-ppss-depth (ppss)
+ (nth 0 ppss))
+
+(defun syntax-ppss-toplevel-pos (ppss)
+ "Get the latest syntactically outermost position found in a syntactic scan.
+PPSS is a scan state, as returned by `parse-partial-sexp' or `syntax-ppss'.
+An \"outermost position\" means one that it is outside of any syntactic entity:
+outside of any parentheses, comments, or strings encountered in the scan.
+If no such position is recorded in PPSS (because the end of the scan was
+itself at the outermost level), return nil."
+ ;; BEWARE! We rely on the undocumented 9th field. The 9th field currently
+ ;; contains the list of positions of the enclosing open-parens.
+ ;; I.e. those positions are outside of any string/comment and the first of
+ ;; those is outside of any paren (i.e. corresponds to a nil ppss).
+ ;; If this list is empty but we are in a string or comment, then the 8th
+ ;; field contains a similar "toplevel" position.
+ (or (car (nth 9 ppss))
+ (nth 8 ppss)))
+
+(defsubst syntax-ppss-context (ppss)
+ (cond
+ ((nth 3 ppss) 'string)
+ ((nth 4 ppss) 'comment)
+ (t nil)))
+
+(defvar syntax-ppss-max-span 20000
+ "Threshold below which cache info is deemed unnecessary.
+We try to make sure that cache entries are at least this far apart
+from each other, to avoid keeping too much useless info.")
+
+(defvar syntax-begin-function nil
+ "Function to move back outside of any comment/string/paren.
+This function should move the cursor back to some syntactically safe
+point (where the PPSS is equivalent to nil).")
+
+(defvar syntax-ppss-cache nil
+ "List of (POS . PPSS) pairs, in decreasing POS order.")
+(make-variable-buffer-local 'syntax-ppss-cache)
+(defvar syntax-ppss-last nil
+ "Cache of (LAST-POS . LAST-PPSS).")
+(make-variable-buffer-local 'syntax-ppss-last)
+
+(defalias 'syntax-ppss-after-change-function 'syntax-ppss-flush-cache)
+(defun syntax-ppss-flush-cache (beg &rest ignored)
+ "Flush the cache of `syntax-ppss' starting at position BEG."
+ ;; Flush invalid cache entries.
+ (while (and syntax-ppss-cache (> (caar syntax-ppss-cache) beg))
+ (setq syntax-ppss-cache (cdr syntax-ppss-cache)))
+ ;; Throw away `last' value if made invalid.
+ (when (< beg (or (car syntax-ppss-last) 0))
+ ;; If syntax-begin-function jumped to BEG, then the old state at BEG can
+ ;; depend on the text after BEG (which is presumably changed). So if
+ ;; BEG=(car (nth 10 syntax-ppss-last)) don't reuse that data because the
+ ;; assumed nil state at BEG may not be valid any more.
+ (if (<= beg (or (syntax-ppss-toplevel-pos (cdr syntax-ppss-last))
+ (nth 3 syntax-ppss-last)
+ 0))
+ (setq syntax-ppss-last nil)
+ (setcar syntax-ppss-last nil)))
+ ;; Unregister if there's no cache left. Sadly this doesn't work
+ ;; because `before-change-functions' is temporarily bound to nil here.
+ ;; (unless syntax-ppss-cache
+ ;; (remove-hook 'before-change-functions 'syntax-ppss-flush-cache t))
+ )
+
+(defvar syntax-ppss-stats
+ [(0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (1 . 2500.0)])
+(defun syntax-ppss-stats ()
+ (mapcar (lambda (x)
+ (condition-case nil
+ (cons (car x) (truncate (/ (cdr x) (car x))))
+ (error nil)))
+ syntax-ppss-stats))
+
+(defun syntax-ppss (&optional pos)
+ "Parse-Partial-Sexp State at POS, defaulting to point.
+The returned value is the same as `parse-partial-sexp' except that
+the 2nd and 6th values of the returned state cannot be relied upon.
+Point is at POS when this function returns."
+ ;; Default values.
+ (unless pos (setq pos (point)))
+ ;;
+ (let ((old-ppss (cdr syntax-ppss-last))
+ (old-pos (car syntax-ppss-last))
+ (ppss nil)
+ (pt-min (point-min)))
+ (if (and old-pos (> old-pos pos)) (setq old-pos nil))
+ ;; Use the OLD-POS if usable and close. Don't update the `last' cache.
+ (condition-case nil
+ (if (and old-pos (< (- pos old-pos)
+ ;; The time to use syntax-begin-function and
+ ;; find PPSS is assumed to be about 2 * distance.
+ (* 2 (/ (cdr (aref syntax-ppss-stats 5))
+ (1+ (car (aref syntax-ppss-stats 5)))))))
+ (progn
+ (incf (car (aref syntax-ppss-stats 0)))
+ (incf (cdr (aref syntax-ppss-stats 0)) (- pos old-pos))
+ (parse-partial-sexp old-pos pos nil nil old-ppss))
+
+ (cond
+ ;; Use OLD-PPSS if possible and close enough.
+ ((and (not old-pos) old-ppss
+ ;; If `pt-min' is too far from `pos', we could try to use
+ ;; other positions in (nth 9 old-ppss), but that doesn't
+ ;; seem to happen in practice and it would complicate this
+ ;; code (and the before-change-function code even more).
+ ;; But maybe it would be useful in "degenerate" cases such
+ ;; as when the whole file is wrapped in a set
+ ;; of parentheses.
+ (setq pt-min (or (syntax-ppss-toplevel-pos old-ppss)
+ (nth 2 old-ppss)))
+ (<= pt-min pos) (< (- pos pt-min) syntax-ppss-max-span))
+ (incf (car (aref syntax-ppss-stats 1)))
+ (incf (cdr (aref syntax-ppss-stats 1)) (- pos pt-min))
+ (setq ppss (parse-partial-sexp pt-min pos)))
+ ;; The OLD-* data can't be used. Consult the cache.
+ (t
+ (let ((cache-pred nil)
+ (cache syntax-ppss-cache)
+ (pt-min (point-min))
+ ;; I differentiate between PT-MIN and PT-BEST because
+ ;; I feel like it might be important to ensure that the
+ ;; cache is only filled with 100% sure data (whereas
+ ;; syntax-begin-function might return incorrect data).
+ ;; Maybe that's just stupid.
+ (pt-best (point-min))
+ (ppss-best nil))
+ ;; look for a usable cache entry.
+ (while (and cache (< pos (caar cache)))
+ (setq cache-pred cache)
+ (setq cache (cdr cache)))
+ (if cache (setq pt-min (caar cache) ppss (cdar cache)))
+
+ ;; Setup the before-change function if necessary.
+ (unless (or syntax-ppss-cache syntax-ppss-last)
+ (add-hook 'before-change-functions
+ 'syntax-ppss-flush-cache t t))
+
+ ;; Use the best of OLD-POS and CACHE.
+ (if (or (not old-pos) (< old-pos pt-min))
+ (setq pt-best pt-min ppss-best ppss)
+ (incf (car (aref syntax-ppss-stats 4)))
+ (incf (cdr (aref syntax-ppss-stats 4)) (- pos old-pos))
+ (setq pt-best old-pos ppss-best old-ppss))
+
+ ;; Use the `syntax-begin-function' if available.
+ ;; We could try using that function earlier, but:
+ ;; - The result might not be 100% reliable, so it's better to use
+ ;; the cache if available.
+ ;; - The function might be slow.
+ ;; - If this function almost always finds a safe nearby spot,
+ ;; the cache won't be populated, so consulting it is cheap.
+ (when (and (not syntax-begin-function)
+ (boundp 'font-lock-beginning-of-syntax-function)
+ font-lock-beginning-of-syntax-function)
+ (set (make-local-variable 'syntax-begin-function)
+ font-lock-beginning-of-syntax-function))
+ (when (and syntax-begin-function
+ (progn (goto-char pos)
+ (funcall syntax-begin-function)
+ ;; Make sure it's better.
+ (> (point) pt-best))
+ ;; Simple sanity check.
+ (not (memq (get-text-property (point) 'face)
+ '(font-lock-string-face font-lock-doc-face
+ font-lock-comment-face))))
+ (incf (car (aref syntax-ppss-stats 5)))
+ (incf (cdr (aref syntax-ppss-stats 5)) (- pos (point)))
+ (setq pt-best (point) ppss-best nil))
+
+ (cond
+ ;; Quick case when we found a nearby pos.
+ ((< (- pos pt-best) syntax-ppss-max-span)
+ (incf (car (aref syntax-ppss-stats 2)))
+ (incf (cdr (aref syntax-ppss-stats 2)) (- pos pt-best))
+ (setq ppss (parse-partial-sexp pt-best pos nil nil ppss-best)))
+ ;; Slow case: compute the state from some known position and
+ ;; populate the cache so we won't need to do it again soon.
+ (t
+ (incf (car (aref syntax-ppss-stats 3)))
+ (incf (cdr (aref syntax-ppss-stats 3)) (- pos pt-min))
+
+ ;; If `pt-min' is too far, add a few intermediate entries.
+ (while (> (- pos pt-min) (* 2 syntax-ppss-max-span))
+ (setq ppss (parse-partial-sexp
+ pt-min (setq pt-min (/ (+ pt-min pos) 2))
+ nil nil ppss))
+ (let ((pair (cons pt-min ppss)))
+ (if cache-pred
+ (push pair (cdr cache-pred))
+ (push pair syntax-ppss-cache))))
+
+ ;; Compute the actual return value.
+ (setq ppss (parse-partial-sexp pt-min pos nil nil ppss))
+
+ ;; Debugging check.
+ ;; (let ((real-ppss (parse-partial-sexp (point-min) pos)))
+ ;; (setcar (last ppss 4) 0)
+ ;; (setcar (last real-ppss 4) 0)
+ ;; (setcar (last ppss 8) nil)
+ ;; (setcar (last real-ppss 8) nil)
+ ;; (unless (equal ppss real-ppss)
+ ;; (message "!!Syntax: %s != %s" ppss real-ppss)
+ ;; (setq ppss real-ppss)))
+
+ ;; Store it in the cache.
+ (let ((pair (cons pos ppss)))
+ (if cache-pred
+ (if (> (- (caar cache-pred) pos) syntax-ppss-max-span)
+ (push pair (cdr cache-pred))
+ (setcar cache-pred pair))
+ (if (or (null syntax-ppss-cache)
+ (> (- (caar syntax-ppss-cache) pos)
+ syntax-ppss-max-span))
+ (push pair syntax-ppss-cache)
+ (setcar syntax-ppss-cache pair)))))))))
+
+ (setq syntax-ppss-last (cons pos ppss))
+ ppss)
+ (args-out-of-range
+ ;; If the buffer is more narrowed than when we built the cache,
+ ;; we may end up calling parse-partial-sexp with a position before
+ ;; point-min. In that case, just parse from point-min assuming
+ ;; a nil state.
+ (parse-partial-sexp (point-min) pos)))))
+
+;; Debugging functions
+
+(defun syntax-ppss-debug ()
+ (let ((pt nil)
+ (min-diffs nil))
+ (dolist (x (append syntax-ppss-cache (list (cons (point-min) nil))))
+ (when pt (push (- pt (car x)) min-diffs))
+ (setq pt (car x)))
+ min-diffs))
+
+;;; syntax-ppss.el ends here
diff -r 1b054bc2ac40 -r 69a08906ad27 tests/ChangeLog
--- a/tests/ChangeLog Sun Jul 03 14:17:39 2011 +0100
+++ b/tests/ChangeLog Wed Jul 20 00:42:16 2011 +0200
@@ -1,3 +1,7 @@
+2011-07-22 Mats Lidell <matsl(a)xemacs.org>
+
+ * automated/syntax-ppss-tests.el: Add tests for syntax-ppss.el.
+
2011-06-28 Aidan Kehoe <kehoea(a)parhasard.net>
* automated/mule-tests.el:
diff -r 1b054bc2ac40 -r 69a08906ad27 tests/automated/syntax-ppss-tests.el
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/automated/syntax-ppss-tests.el Wed Jul 20 00:42:16 2011 +0200
@@ -0,0 +1,37 @@
+;; Copyright (C) 2011 Free Software Foundation, Inc.
+
+;; Author: Mats Lidell <matsl(a)xemacs.org>
+;; Maintainer:
+;; Created: 2011
+;; Keywords: tests
+
+;; This file is part of XEmacs.
+
+;; XEmacs 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.
+
+;; XEmacs 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 XEmacs. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Synched up with: Not in FSF.
+
+;;; Commentary:
+
+;; Test tag support.
+;; See test-harness.el for instructions on how to run these tests.
+
+(with-temp-buffer
+ (insert ";;;")
+ (Assert (equal (syntax-ppss (point-min)) '(0 nil nil nil nil nil 0 nil nil nil)))
+ (Assert (eq (point-min) (point)))
+ (Assert (equal (syntax-ppss 3) '(0 nil nil nil nil nil 0 nil nil nil)))
+ (Assert (eq 3 (point)))
+ )
+
Yours
--
%% Mats
_______________________________________________
XEmacs-Patches mailing list
XEmacs-Patches(a)xemacs.org
http://lists.xemacs.org/mailman/listinfo/xemacs-patches
changeset: 5534:68db75473fc6
tag: tip
parent: 5532:69a08906ad27
parent: 5533:11da5b828d10
user: Mats Lidell <mats.lidell(a)cag.se>
date: Sun Aug 07 23:22:59 2011 +0200
files: lisp/ChangeLog tests/ChangeLog
description:
Merge.
diff -r 69a08906ad27 -r 68db75473fc6 lisp/ChangeLog
--- a/lisp/ChangeLog Wed Jul 20 00:42:16 2011 +0200
+++ b/lisp/ChangeLog Sun Aug 07 23:22:59 2011 +0200
@@ -1,5 +1,9 @@
+2011-07-29 Mats Lidell <matsl(a)xemacs.org>
+ * process.el (shell-command):
+ * process.el (shell-command-on-region): API compatible/synced with
+ FSF 23.3.1.
+
2011-07-22 Mats Lidell <matsl(a)xemacs.or>
-
* syntax-ppss.el: Synced up with Emacs 23.3 (syntax.el)
2011-07-03 Aidan Kehoe <kehoea(a)parhasard.net>
diff -r 69a08906ad27 -r 68db75473fc6 lisp/process.el
--- a/lisp/process.el Wed Jul 20 00:42:16 2011 +0200
+++ b/lisp/process.el Sun Aug 07 23:22:59 2011 +0200
@@ -1,6 +1,6 @@
;;; process.el --- commands for subprocesses; split out of simple.el
-;; Copyright (C) 1985-7, 1993,4, 1997 Free Software Foundation, Inc.
+;; Copyright (C) 1985-7, 1993,4, 1997, 2011 Free Software Foundation, Inc.
;; Copyright (C) 1995, 2000, 2001, 2002 Ben Wing.
;; Author: Ben Wing
@@ -24,6 +24,7 @@
;;; Synched up with: FSF 19.30, except for setenv/getenv (synched with FSF
;;; 21.2.1).
+;;; shell-command and shell-command-on-region synced with FSF 23.3.1.
;;; Authorship:
@@ -57,6 +58,12 @@
(defvar shell-command-switch "-c"
"Switch used to have the shell execute its command line argument.")
+(defvar shell-command-default-error-buffer nil
+ "*Buffer name for `shell-command' and `shell-command-on-region' error output.
+This buffer is used when `shell-command' or `shell-command-on-region'
+is run interactively. A value of nil means that output to stderr and
+stdout will be intermixed in the output stream.")
+
(defun start-process-shell-command (name buffer &rest args)
"Start a program in a subprocess. Return the process object for it.
NAME is name for process. It is modified if necessary to make it unique.
@@ -316,12 +323,12 @@
(error nil)))))
-(defun shell-command (command &optional output-buffer)
+(defun shell-command (command &optional output-buffer error-buffer)
"Execute string COMMAND in inferior shell; display output, if any.
-If COMMAND ends in ampersand, execute it asynchronously.
-The output appears in the buffer `*Async Shell Command*'.
-That buffer is in shell mode.
+If COMMAND ends in ampersand, execute it asynchronously. The command
+is executed using the background package. See `background' for
+details.
Otherwise, COMMAND is executed synchronously. The output appears in the
buffer `*Shell Command Output*'.
@@ -336,9 +343,16 @@
If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
If OUTPUT-BUFFER is not a buffer and not nil,
insert output in current buffer. (This cannot be done asynchronously.)
-In either case, the output is inserted after point (leaving mark after it)."
+In either case, the output is inserted after point (leaving mark after it).
+
+If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
+or buffer name to which to direct the command's standard error output.
+If it is nil, error output is mingled with regular output. In an
+interactive call, the variable `shell-command-default-error-buffer'
+specifies the value of ERROR-BUFFER."
(interactive (list (read-shell-command "Shell command: ")
- current-prefix-arg))
+ current-prefix-arg
+ shell-command-default-error-buffer))
(if (and output-buffer
(not (or (bufferp output-buffer) (stringp output-buffer))))
(progn (barf-if-buffer-read-only)
@@ -363,7 +377,7 @@
'unimplemented
"backgrounding a shell command requires package `background'")))
- (shell-command-on-region (point) (point) command output-buffer)))))
+ (shell-command-on-region (point) (point) command output-buffer nil error-buffer)))))
;; We have a sentinel to prevent insertion of a termination message
;; in the buffer itself.
@@ -374,7 +388,8 @@
(substring signal 0 -1))))
(defun shell-command-on-region (start end command
- &optional output-buffer replace)
+ &optional output-buffer replace
+ error-buffer display-error-buffer)
"Execute string COMMAND in inferior shell with region as input.
Normally display output (if any) in temp buffer `*Shell Command Output*';
Prefix arg means replace the region with it.
@@ -394,7 +409,15 @@
If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
If OUTPUT-BUFFER is not a buffer and not nil,
insert output in the current buffer.
-In either case, the output is inserted after point (leaving mark after it)."
+In either case, the output is inserted after point (leaving mark after it).
+
+If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
+or buffer name to which to direct the command's standard error output.
+If it is nil, error output is mingled with regular output.
+If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
+were any errors. (This is always t, interactively.)
+In an interactive call, the variable `shell-command-default-error-buffer'
+specifies the value of ERROR-BUFFER."
(interactive (let ((string
;; Do this before calling region-beginning
;; and region-end, in case subprocess output
@@ -405,79 +428,142 @@
(list (region-beginning) (region-end)
string
current-prefix-arg
- current-prefix-arg)))
- (if (or replace
- (and output-buffer
- (not (or (bufferp output-buffer) (stringp output-buffer)))))
- ;; Replace specified region with output from command.
- (let ((swap (and replace (< start end))))
- ;; Don't muck with mark unless REPLACE says we should.
- (goto-char start)
- (and replace (push-mark))
- (call-process-region start end shell-file-name t t nil
- shell-command-switch command)
- (let ((shell-buffer (get-buffer "*Shell Command Output*")))
- (and shell-buffer (not (eq shell-buffer (current-buffer)))
- (kill-buffer shell-buffer)))
- ;; Don't muck with mark unless REPLACE says we should.
- (and replace swap (exchange-point-and-mark t)))
+ current-prefix-arg
+ shell-command-default-error-buffer
+ t)))
+ (let ((error-file
+ (if error-buffer
+ (make-temp-file
+ (expand-file-name "scor"
+ (or small-temporary-file-directory
+ (temp-directory))))
+ nil))
+ (exit-status nil))
+ (if (or replace
+ (and output-buffer
+ (not (or (bufferp output-buffer) (stringp output-buffer)))))
+ ;; Replace specified region with output from command.
+ (let ((swap (and replace (< start end))))
+ ;; Don't muck with mark unless REPLACE says we should.
+ (goto-char start)
+ (and replace (push-mark))
+ (setq exit-status (call-process-region start end shell-file-name t
+ (if error-file
+ (list t error-file)
+ t)
+ nil shell-command-switch command))
+ (let ((shell-buffer (get-buffer "*Shell Command Output*")))
+ (and shell-buffer (nnot (eq shell-buffer (current-buffer)))
+ (kill-buffer shell-buffer)))
+ ;; Don't muck with mark unless REPLACE says we should.
+ (and replace swap (exchange-point-and-mark t)))
;; No prefix argument: put the output in a temp buffer,
;; replacing its entire contents.
- (let ((buffer (get-buffer-create
- (or output-buffer "*Shell Command Output*")))
- (success nil)
- (exit-status nil)
- (directory default-directory))
- (unwind-protect
- (if (eq buffer (current-buffer))
- ;; If the input is the same buffer as the output,
- ;; delete everything but the specified region,
- ;; then replace that region with the output.
- (progn (setq buffer-read-only nil)
- (delete-region (max start end) (point-max))
- (delete-region (point-min) (min start end))
- (setq exit-status
- (call-process-region (point-min) (point-max)
- shell-file-name t t nil
- shell-command-switch command))
- (setq success t))
- ;; Clear the output buffer,
- ;; then run the command with output there.
- (save-excursion
- (set-buffer buffer)
- (setq buffer-read-only nil)
- ;; XEmacs change
- (setq default-directory directory)
- (erase-buffer))
- (setq exit-status
- (call-process-region start end shell-file-name
- nil buffer nil
- shell-command-switch command))
- (setq success t))
- ;; Report the amount of output.
- (let ((lines (save-excursion
- (set-buffer buffer)
- (if (= (buffer-size) 0)
- 0
- (count-lines (point-min) (point-max))))))
- (cond ((= lines 0)
- (if success
- (display-message
- 'command
- (if (eql exit-status 0)
- "(Shell command succeeded with no output)"
- "(Shell command failed with no output)")))
- (kill-buffer buffer))
- ((and success (= lines 1))
- (message "%s"
- (save-excursion
- (set-buffer buffer)
- (goto-char (point-min))
- (buffer-substring (point)
- (progn (end-of-line)
- (point))))))
- (t
- (set-window-start (display-buffer buffer) 1))))))))
+ (let ((buffer (get-buffer-create
+ (or output-buffer "*Shell Command Output*")))
+ (directory default-directory))
+ (unwind-protect
+ (if (eq buffer (current-buffer))
+ ;; If the input is the same buffer as the output,
+ ;; delete everything but the specified region,
+ ;; then replace that region with the output.
+ (progn (setq buffer-read-only nil)
+ (delete-region (max start end) (point-max))
+ (delete-region (point-min) (min start end))
+ (setq exit-status
+ (call-process-region (point-min) (point-max)
+ shell-file-name t
+ (if error-file
+ (list t error-file)
+ t)
+ nil shell-command-switch
+ command)))
+ ;; Clear the output buffer,
+ ;; then run the command with output there.
+ (save-excursion
+ (set-buffer buffer)
+ (setq buffer-read-only nil)
+ ;; XEmacs change
+ (setq default-directory directory)
+ (erase-buffer))
+ (setq exit-status
+ (call-process-region start end shell-file-name
+ nil
+ (if error-file
+ (list buffer error-file)
+ buffer)
+ nil shell-command-switch command)))
+
+ ;; Report the output.
+ (with-current-buffer buffer
+ (setq modeline-process
+ (cond ((null exit-status)
+ " - Error")
+ ((stringp exit-status)
+ (format " - Signal [%s]" exit-status))
+ ((not (equal 0 exit-status))
+ (format " - Exit [%d]" exit-status)))))
+ (if (with-current-buffer buffer (> (point-max) (point-min)))
+ ;; There's some output, display it
+ (let ((lines (save-excursion
+ (set-buffer buffer)
+ (if (= (buffer-size) 0)
+ 0
+ (count-lines (point-min) (point-max))))))
+ (cond ((= lines 0)
+ (display-message
+ 'command
+ (if (eql exit-status 0)
+ "(Shell command succeeded with no output)"
+ "(Shell command failed with no output)"))
+ (kill-buffer buffer))
+ ((= lines 1)
+ (message "%s"
+ (save-excursion
+ (set-buffer buffer)
+ (goto-char (point-min))
+ (buffer-substring (point)
+ (progn (end-of-line)
+ (point))))))
+ (t
+ (set-window-start (display-buffer buffer) 1))))
+ ;; No output; error?
+ (let ((output
+ (if (and error-file
+ (< 0 (nth 7 (file-attributes error-file))))
+ "some error output"
+ "no output")))
+ (cond ((null exit-status)
+ (message "(Shell command failed with error)"))
+ ((equal 0 exit-status)
+ (message "(Shell command succeeded with %s)"
+ output))
+ ((stringp exit-status)
+ (message "(Shell command killed by signal %s)"
+ exit-status))
+ (t
+ (message "(Shell command failed with code %d and %s)"
+ exit-status output))))
+ ;; Don't kill: there might be useful info in the undo-log.
+ ;; (kill-buffer buffer)
+ ))))
+
+ (when (and error-file (file-exists-p error-file))
+ (if (< 0 (nth 7 (file-attributes error-file)))
+ (with-current-buffer (get-buffer-create error-buffer)
+ (let ((pos-from-end (- (point-max) (point))))
+ (or (bobp)
+ (insert "\f\n"))
+ ;; Do no formatting while reading error file,
+ ;; because that can run a shell command, and we
+ ;; don't want that to cause an infinite recursion.
+ (format-insert-file error-file nil)
+ ;; Put point after the inserted errors.
+ (goto-char (- (point-max) pos-from-end)))
+ (and display-error-buffer
+ (display-buffer (current-buffer)))))
+ (delete-file error-file))
+ exit-status))
(defun shell-quote-argument (argument)
"Quote an argument for passing as argument to an inferior shell."
diff -r 69a08906ad27 -r 68db75473fc6 tests/ChangeLog
--- a/tests/ChangeLog Wed Jul 20 00:42:16 2011 +0200
+++ b/tests/ChangeLog Sun Aug 07 23:22:59 2011 +0200
@@ -1,5 +1,7 @@
+2011-07-31 Mats Lidell <matsl(a)xemacs.org>
+ * automated/process-tests.el: shell-command tests.
+
2011-07-22 Mats Lidell <matsl(a)xemacs.org>
-
* automated/syntax-ppss-tests.el: Add tests for syntax-ppss.el.
2011-06-28 Aidan Kehoe <kehoea(a)parhasard.net>
diff -r 69a08906ad27 -r 68db75473fc6 tests/automated/process-tests.el
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/automated/process-tests.el Sun Aug 07 23:22:59 2011 +0200
@@ -0,0 +1,70 @@
+;; Copyright (C) 2011 Free Software Foundation, Inc.
+
+;; Author: Mats Lidell <matsl(a)xemacs.org>
+;; Maintainer:
+;; Created: 2011
+;; Keywords: tests
+
+;; This file is part of XEmacs.
+
+;; XEmacs 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.
+
+;; XEmacs 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 XEmacs. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Synched up with: Not in FSF.
+
+;;; Commentary:
+
+;; Test tag support.
+;; See test-harness.el for instructions on how to run these tests.
+
+(require 'test-harness)
+
+(when (equal system-type 'linux)
+ (setenv "LANG" "C")
+
+ ;; One line output
+ (Assert (= 0 (shell-command "echo hello")))
+ (Assert (equal "hello" (message-displayed-p t)))
+ (with-current-buffer " *Echo Area*"
+ (goto-char (point-min))
+ (Assert (looking-at "hello")))
+
+ ;; Two lines. No output in echo area. (GNU resizes minibuffer but we
+ ;; haven't implemented that.)
+ (message "")
+ (Assert (= 0 (shell-command "echo -e \"foo\nbar\n\"")))
+ (with-current-buffer " *Echo Area*"
+ (Assert (= 0 (buffer-size))))
+ (with-current-buffer "*Shell Command Output*"
+ (goto-char (point-min))
+ (Assert (looking-at "foo"))):
+
+ (Assert (= 127 (shell-command "unknown_command")))
+ (Assert (= 2 (shell-command "exit 2")))
+ (Assert (equal "(Shell command failed with code 2 and no output)" (message-displayed-p t)))
+
+ ;; Output to stderr With error buffer
+ (Assert (= 0 (shell-command "echo -e \"foo\nbar\n\" 1>&2" "Output buffer" "Error buffer")))
+ (Assert (equal "(Shell command succeeded with some error output)" (message-displayed-p t)))
+ (with-current-buffer "Error buffer"
+ (goto-char (point-min))
+ (Assert (looking-at "foo")))
+ (with-current-buffer "Output buffer"
+ (Assert (= 0 (buffer-size))))
+
+ ;; Output to stderr but no error buffer
+ (Assert (= 0 (shell-command "echo -e \"foobar\nfoobar\n\" 1>&2" "Output buffer")))
+ (with-current-buffer "Output buffer"
+ (goto-char (point-min))
+ (Assert (looking-at "foobar")))
+)
_______________________________________________
XEmacs-Patches mailing list
XEmacs-Patches(a)xemacs.org
http://lists.xemacs.org/mailman/listinfo/xemacs-patches
changeset: 5533:11da5b828d10
parent: 5531:1b054bc2ac40
user: Mats Lidell <mats.lidell(a)cag.se>
date: Sun Jul 31 01:29:09 2011 +0200
files: lisp/ChangeLog lisp/process.el tests/ChangeLog tests/automated/process-tests.el
description:
shell-command and shell-command-on-region API compliant with FSF 23.3.1
diff -r 1b054bc2ac40 -r 11da5b828d10 lisp/ChangeLog
--- a/lisp/ChangeLog Sun Jul 03 14:17:39 2011 +0100
+++ b/lisp/ChangeLog Sun Jul 31 01:29:09 2011 +0200
@@ -1,3 +1,9 @@
+2011-07-29 Mats Lidell <matsl(a)xemacs.org>
+
+ * process.el (shell-command):
+ * process.el (shell-command-on-region): API compatible/synced with
+ FSF 23.3.1.
+
2011-07-03 Aidan Kehoe <kehoea(a)parhasard.net>
* cl.el (cl-macroexpand):
diff -r 1b054bc2ac40 -r 11da5b828d10 lisp/process.el
--- a/lisp/process.el Sun Jul 03 14:17:39 2011 +0100
+++ b/lisp/process.el Sun Jul 31 01:29:09 2011 +0200
@@ -1,6 +1,6 @@
;;; process.el --- commands for subprocesses; split out of simple.el
-;; Copyright (C) 1985-7, 1993,4, 1997 Free Software Foundation, Inc.
+;; Copyright (C) 1985-7, 1993,4, 1997, 2011 Free Software Foundation, Inc.
;; Copyright (C) 1995, 2000, 2001, 2002 Ben Wing.
;; Author: Ben Wing
@@ -24,6 +24,7 @@
;;; Synched up with: FSF 19.30, except for setenv/getenv (synched with FSF
;;; 21.2.1).
+;;; shell-command and shell-command-on-region synced with FSF 23.3.1.
;;; Authorship:
@@ -57,6 +58,12 @@
(defvar shell-command-switch "-c"
"Switch used to have the shell execute its command line argument.")
+(defvar shell-command-default-error-buffer nil
+ "*Buffer name for `shell-command' and `shell-command-on-region' error output.
+This buffer is used when `shell-command' or `shell-command-on-region'
+is run interactively. A value of nil means that output to stderr and
+stdout will be intermixed in the output stream.")
+
(defun start-process-shell-command (name buffer &rest args)
"Start a program in a subprocess. Return the process object for it.
NAME is name for process. It is modified if necessary to make it unique.
@@ -316,12 +323,12 @@
(error nil)))))
-(defun shell-command (command &optional output-buffer)
+(defun shell-command (command &optional output-buffer error-buffer)
"Execute string COMMAND in inferior shell; display output, if any.
-If COMMAND ends in ampersand, execute it asynchronously.
-The output appears in the buffer `*Async Shell Command*'.
-That buffer is in shell mode.
+If COMMAND ends in ampersand, execute it asynchronously. The command
+is executed using the background package. See `background' for
+details.
Otherwise, COMMAND is executed synchronously. The output appears in the
buffer `*Shell Command Output*'.
@@ -336,9 +343,16 @@
If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
If OUTPUT-BUFFER is not a buffer and not nil,
insert output in current buffer. (This cannot be done asynchronously.)
-In either case, the output is inserted after point (leaving mark after it)."
+In either case, the output is inserted after point (leaving mark after it).
+
+If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
+or buffer name to which to direct the command's standard error output.
+If it is nil, error output is mingled with regular output. In an
+interactive call, the variable `shell-command-default-error-buffer'
+specifies the value of ERROR-BUFFER."
(interactive (list (read-shell-command "Shell command: ")
- current-prefix-arg))
+ current-prefix-arg
+ shell-command-default-error-buffer))
(if (and output-buffer
(not (or (bufferp output-buffer) (stringp output-buffer))))
(progn (barf-if-buffer-read-only)
@@ -363,7 +377,7 @@
'unimplemented
"backgrounding a shell command requires package `background'")))
- (shell-command-on-region (point) (point) command output-buffer)))))
+ (shell-command-on-region (point) (point) command output-buffer nil error-buffer)))))
;; We have a sentinel to prevent insertion of a termination message
;; in the buffer itself.
@@ -374,7 +388,8 @@
(substring signal 0 -1))))
(defun shell-command-on-region (start end command
- &optional output-buffer replace)
+ &optional output-buffer replace
+ error-buffer display-error-buffer)
"Execute string COMMAND in inferior shell with region as input.
Normally display output (if any) in temp buffer `*Shell Command Output*';
Prefix arg means replace the region with it.
@@ -394,7 +409,15 @@
If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
If OUTPUT-BUFFER is not a buffer and not nil,
insert output in the current buffer.
-In either case, the output is inserted after point (leaving mark after it)."
+In either case, the output is inserted after point (leaving mark after it).
+
+If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
+or buffer name to which to direct the command's standard error output.
+If it is nil, error output is mingled with regular output.
+If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
+were any errors. (This is always t, interactively.)
+In an interactive call, the variable `shell-command-default-error-buffer'
+specifies the value of ERROR-BUFFER."
(interactive (let ((string
;; Do this before calling region-beginning
;; and region-end, in case subprocess output
@@ -405,79 +428,142 @@
(list (region-beginning) (region-end)
string
current-prefix-arg
- current-prefix-arg)))
- (if (or replace
- (and output-buffer
- (not (or (bufferp output-buffer) (stringp output-buffer)))))
- ;; Replace specified region with output from command.
- (let ((swap (and replace (< start end))))
- ;; Don't muck with mark unless REPLACE says we should.
- (goto-char start)
- (and replace (push-mark))
- (call-process-region start end shell-file-name t t nil
- shell-command-switch command)
- (let ((shell-buffer (get-buffer "*Shell Command Output*")))
- (and shell-buffer (not (eq shell-buffer (current-buffer)))
- (kill-buffer shell-buffer)))
- ;; Don't muck with mark unless REPLACE says we should.
- (and replace swap (exchange-point-and-mark t)))
+ current-prefix-arg
+ shell-command-default-error-buffer
+ t)))
+ (let ((error-file
+ (if error-buffer
+ (make-temp-file
+ (expand-file-name "scor"
+ (or small-temporary-file-directory
+ (temp-directory))))
+ nil))
+ (exit-status nil))
+ (if (or replace
+ (and output-buffer
+ (not (or (bufferp output-buffer) (stringp output-buffer)))))
+ ;; Replace specified region with output from command.
+ (let ((swap (and replace (< start end))))
+ ;; Don't muck with mark unless REPLACE says we should.
+ (goto-char start)
+ (and replace (push-mark))
+ (setq exit-status (call-process-region start end shell-file-name t
+ (if error-file
+ (list t error-file)
+ t)
+ nil shell-command-switch command))
+ (let ((shell-buffer (get-buffer "*Shell Command Output*")))
+ (and shell-buffer (nnot (eq shell-buffer (current-buffer)))
+ (kill-buffer shell-buffer)))
+ ;; Don't muck with mark unless REPLACE says we should.
+ (and replace swap (exchange-point-and-mark t)))
;; No prefix argument: put the output in a temp buffer,
;; replacing its entire contents.
- (let ((buffer (get-buffer-create
- (or output-buffer "*Shell Command Output*")))
- (success nil)
- (exit-status nil)
- (directory default-directory))
- (unwind-protect
- (if (eq buffer (current-buffer))
- ;; If the input is the same buffer as the output,
- ;; delete everything but the specified region,
- ;; then replace that region with the output.
- (progn (setq buffer-read-only nil)
- (delete-region (max start end) (point-max))
- (delete-region (point-min) (min start end))
- (setq exit-status
- (call-process-region (point-min) (point-max)
- shell-file-name t t nil
- shell-command-switch command))
- (setq success t))
- ;; Clear the output buffer,
- ;; then run the command with output there.
- (save-excursion
- (set-buffer buffer)
- (setq buffer-read-only nil)
- ;; XEmacs change
- (setq default-directory directory)
- (erase-buffer))
- (setq exit-status
- (call-process-region start end shell-file-name
- nil buffer nil
- shell-command-switch command))
- (setq success t))
- ;; Report the amount of output.
- (let ((lines (save-excursion
- (set-buffer buffer)
- (if (= (buffer-size) 0)
- 0
- (count-lines (point-min) (point-max))))))
- (cond ((= lines 0)
- (if success
- (display-message
- 'command
- (if (eql exit-status 0)
- "(Shell command succeeded with no output)"
- "(Shell command failed with no output)")))
- (kill-buffer buffer))
- ((and success (= lines 1))
- (message "%s"
- (save-excursion
- (set-buffer buffer)
- (goto-char (point-min))
- (buffer-substring (point)
- (progn (end-of-line)
- (point))))))
- (t
- (set-window-start (display-buffer buffer) 1))))))))
+ (let ((buffer (get-buffer-create
+ (or output-buffer "*Shell Command Output*")))
+ (directory default-directory))
+ (unwind-protect
+ (if (eq buffer (current-buffer))
+ ;; If the input is the same buffer as the output,
+ ;; delete everything but the specified region,
+ ;; then replace that region with the output.
+ (progn (setq buffer-read-only nil)
+ (delete-region (max start end) (point-max))
+ (delete-region (point-min) (min start end))
+ (setq exit-status
+ (call-process-region (point-min) (point-max)
+ shell-file-name t
+ (if error-file
+ (list t error-file)
+ t)
+ nil shell-command-switch
+ command)))
+ ;; Clear the output buffer,
+ ;; then run the command with output there.
+ (save-excursion
+ (set-buffer buffer)
+ (setq buffer-read-only nil)
+ ;; XEmacs change
+ (setq default-directory directory)
+ (erase-buffer))
+ (setq exit-status
+ (call-process-region start end shell-file-name
+ nil
+ (if error-file
+ (list buffer error-file)
+ buffer)
+ nil shell-command-switch command)))
+
+ ;; Report the output.
+ (with-current-buffer buffer
+ (setq modeline-process
+ (cond ((null exit-status)
+ " - Error")
+ ((stringp exit-status)
+ (format " - Signal [%s]" exit-status))
+ ((not (equal 0 exit-status))
+ (format " - Exit [%d]" exit-status)))))
+ (if (with-current-buffer buffer (> (point-max) (point-min)))
+ ;; There's some output, display it
+ (let ((lines (save-excursion
+ (set-buffer buffer)
+ (if (= (buffer-size) 0)
+ 0
+ (count-lines (point-min) (point-max))))))
+ (cond ((= lines 0)
+ (display-message
+ 'command
+ (if (eql exit-status 0)
+ "(Shell command succeeded with no output)"
+ "(Shell command failed with no output)"))
+ (kill-buffer buffer))
+ ((= lines 1)
+ (message "%s"
+ (save-excursion
+ (set-buffer buffer)
+ (goto-char (point-min))
+ (buffer-substring (point)
+ (progn (end-of-line)
+ (point))))))
+ (t
+ (set-window-start (display-buffer buffer) 1))))
+ ;; No output; error?
+ (let ((output
+ (if (and error-file
+ (< 0 (nth 7 (file-attributes error-file))))
+ "some error output"
+ "no output")))
+ (cond ((null exit-status)
+ (message "(Shell command failed with error)"))
+ ((equal 0 exit-status)
+ (message "(Shell command succeeded with %s)"
+ output))
+ ((stringp exit-status)
+ (message "(Shell command killed by signal %s)"
+ exit-status))
+ (t
+ (message "(Shell command failed with code %d and %s)"
+ exit-status output))))
+ ;; Don't kill: there might be useful info in the undo-log.
+ ;; (kill-buffer buffer)
+ ))))
+
+ (when (and error-file (file-exists-p error-file))
+ (if (< 0 (nth 7 (file-attributes error-file)))
+ (with-current-buffer (get-buffer-create error-buffer)
+ (let ((pos-from-end (- (point-max) (point))))
+ (or (bobp)
+ (insert "\f\n"))
+ ;; Do no formatting while reading error file,
+ ;; because that can run a shell command, and we
+ ;; don't want that to cause an infinite recursion.
+ (format-insert-file error-file nil)
+ ;; Put point after the inserted errors.
+ (goto-char (- (point-max) pos-from-end)))
+ (and display-error-buffer
+ (display-buffer (current-buffer)))))
+ (delete-file error-file))
+ exit-status))
(defun shell-quote-argument (argument)
"Quote an argument for passing as argument to an inferior shell."
diff -r 1b054bc2ac40 -r 11da5b828d10 tests/ChangeLog
--- a/tests/ChangeLog Sun Jul 03 14:17:39 2011 +0100
+++ b/tests/ChangeLog Sun Jul 31 01:29:09 2011 +0200
@@ -1,3 +1,7 @@
+2011-07-31 Mats Lidell <matsl(a)xemacs.org>
+
+ * automated/process-tests.el: shell-command tests.
+
2011-06-28 Aidan Kehoe <kehoea(a)parhasard.net>
* automated/mule-tests.el:
diff -r 1b054bc2ac40 -r 11da5b828d10 tests/automated/process-tests.el
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/automated/process-tests.el Sun Jul 31 01:29:09 2011 +0200
@@ -0,0 +1,70 @@
+;; Copyright (C) 2011 Free Software Foundation, Inc.
+
+;; Author: Mats Lidell <matsl(a)xemacs.org>
+;; Maintainer:
+;; Created: 2011
+;; Keywords: tests
+
+;; This file is part of XEmacs.
+
+;; XEmacs 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.
+
+;; XEmacs 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 XEmacs. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Synched up with: Not in FSF.
+
+;;; Commentary:
+
+;; Test tag support.
+;; See test-harness.el for instructions on how to run these tests.
+
+(require 'test-harness)
+
+(when (equal system-type 'linux)
+ (setenv "LANG" "C")
+
+ ;; One line output
+ (Assert (= 0 (shell-command "echo hello")))
+ (Assert (equal "hello" (message-displayed-p t)))
+ (with-current-buffer " *Echo Area*"
+ (goto-char (point-min))
+ (Assert (looking-at "hello")))
+
+ ;; Two lines. No output in echo area. (GNU resizes minibuffer but we
+ ;; haven't implemented that.)
+ (message "")
+ (Assert (= 0 (shell-command "echo -e \"foo\nbar\n\"")))
+ (with-current-buffer " *Echo Area*"
+ (Assert (= 0 (buffer-size))))
+ (with-current-buffer "*Shell Command Output*"
+ (goto-char (point-min))
+ (Assert (looking-at "foo"))):
+
+ (Assert (= 127 (shell-command "unknown_command")))
+ (Assert (= 2 (shell-command "exit 2")))
+ (Assert (equal "(Shell command failed with code 2 and no output)" (message-displayed-p t)))
+
+ ;; Output to stderr With error buffer
+ (Assert (= 0 (shell-command "echo -e \"foo\nbar\n\" 1>&2" "Output buffer" "Error buffer")))
+ (Assert (equal "(Shell command succeeded with some error output)" (message-displayed-p t)))
+ (with-current-buffer "Error buffer"
+ (goto-char (point-min))
+ (Assert (looking-at "foo")))
+ (with-current-buffer "Output buffer"
+ (Assert (= 0 (buffer-size))))
+
+ ;; Output to stderr but no error buffer
+ (Assert (= 0 (shell-command "echo -e \"foobar\nfoobar\n\" 1>&2" "Output buffer")))
+ (with-current-buffer "Output buffer"
+ (goto-char (point-min))
+ (Assert (looking-at "foobar")))
+)
_______________________________________________
XEmacs-Patches mailing list
XEmacs-Patches(a)xemacs.org
http://lists.xemacs.org/mailman/listinfo/xemacs-patches
changeset: 5532:69a08906ad27
user: Mats Lidell <mats.lidell(a)cag.se>
date: Wed Jul 20 00:42:16 2011 +0200
files: lisp/ChangeLog lisp/dumped-lisp.el lisp/syntax-ppss.el tests/ChangeLog tests/automated/syntax-ppss-tests.el
description:
Introducing syntax-ppss.el from GNU.
diff -r 1b054bc2ac40 -r 69a08906ad27 lisp/ChangeLog
--- a/lisp/ChangeLog Sun Jul 03 14:17:39 2011 +0100
+++ b/lisp/ChangeLog Wed Jul 20 00:42:16 2011 +0200
@@ -1,3 +1,7 @@
+2011-07-22 Mats Lidell <matsl(a)xemacs.or>
+
+ * syntax-ppss.el: Synced up with Emacs 23.3 (syntax.el)
+
2011-07-03 Aidan Kehoe <kehoea(a)parhasard.net>
* cl.el (cl-macroexpand):
diff -r 1b054bc2ac40 -r 69a08906ad27 lisp/dumped-lisp.el
--- a/lisp/dumped-lisp.el Sun Jul 03 14:17:39 2011 +0100
+++ b/lisp/dumped-lisp.el Wed Jul 20 00:42:16 2011 +0200
@@ -54,6 +54,7 @@
"cmdloop"
"keymap"
"syntax"
+ "syntax-ppss"
"device"
"console"
"obsolete"
diff -r 1b054bc2ac40 -r 69a08906ad27 lisp/syntax-ppss.el
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/syntax-ppss.el Wed Jul 20 00:42:16 2011 +0200
@@ -0,0 +1,288 @@
+;;; syntax-ppss.el --- helper functions to find syntactic context
+
+;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
+;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+
+;; Maintainer: XEmacs Development Team
+;; Keywords: internal
+
+;; This file is part of XEmacs.
+
+;; XEmacs 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.
+
+;; XEmacs 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 XEmacs. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Synched up with: FSF 23,3, syntax.el.
+
+;;; Commentary:
+
+;; The main exported function is `syntax-ppss'. You might also need
+;; to call `syntax-ppss-flush-cache' or to add it to
+;; before-change-functions'(although this is automatically done by
+;; syntax-ppss when needed, but that might fail if syntax-ppss is
+;; called in a context where before-change-functions is temporarily
+;; let-bound to nil).
+
+;;; Todo:
+
+;; - do something about the case where the syntax-table is changed.
+;; This typically happens with tex-mode and its `$' operator.
+;; - move font-lock-syntactic-keywords in here. Then again, maybe not.
+;; - new functions `syntax-state', ... to replace uses of parse-partial-state
+;; with something higher-level (similar to syntax-ppss-context).
+;; - interaction with mmm-mode.
+
+;;; Code:
+
+;; Note: PPSS stands for `parse-partial-sexp state'
+
+(eval-when-compile (require 'cl))
+
+(defvar font-lock-beginning-of-syntax-function)
+
+(defsubst syntax-ppss-depth (ppss)
+ (nth 0 ppss))
+
+(defun syntax-ppss-toplevel-pos (ppss)
+ "Get the latest syntactically outermost position found in a syntactic scan.
+PPSS is a scan state, as returned by `parse-partial-sexp' or `syntax-ppss'.
+An \"outermost position\" means one that it is outside of any syntactic entity:
+outside of any parentheses, comments, or strings encountered in the scan.
+If no such position is recorded in PPSS (because the end of the scan was
+itself at the outermost level), return nil."
+ ;; BEWARE! We rely on the undocumented 9th field. The 9th field currently
+ ;; contains the list of positions of the enclosing open-parens.
+ ;; I.e. those positions are outside of any string/comment and the first of
+ ;; those is outside of any paren (i.e. corresponds to a nil ppss).
+ ;; If this list is empty but we are in a string or comment, then the 8th
+ ;; field contains a similar "toplevel" position.
+ (or (car (nth 9 ppss))
+ (nth 8 ppss)))
+
+(defsubst syntax-ppss-context (ppss)
+ (cond
+ ((nth 3 ppss) 'string)
+ ((nth 4 ppss) 'comment)
+ (t nil)))
+
+(defvar syntax-ppss-max-span 20000
+ "Threshold below which cache info is deemed unnecessary.
+We try to make sure that cache entries are at least this far apart
+from each other, to avoid keeping too much useless info.")
+
+(defvar syntax-begin-function nil
+ "Function to move back outside of any comment/string/paren.
+This function should move the cursor back to some syntactically safe
+point (where the PPSS is equivalent to nil).")
+
+(defvar syntax-ppss-cache nil
+ "List of (POS . PPSS) pairs, in decreasing POS order.")
+(make-variable-buffer-local 'syntax-ppss-cache)
+(defvar syntax-ppss-last nil
+ "Cache of (LAST-POS . LAST-PPSS).")
+(make-variable-buffer-local 'syntax-ppss-last)
+
+(defalias 'syntax-ppss-after-change-function 'syntax-ppss-flush-cache)
+(defun syntax-ppss-flush-cache (beg &rest ignored)
+ "Flush the cache of `syntax-ppss' starting at position BEG."
+ ;; Flush invalid cache entries.
+ (while (and syntax-ppss-cache (> (caar syntax-ppss-cache) beg))
+ (setq syntax-ppss-cache (cdr syntax-ppss-cache)))
+ ;; Throw away `last' value if made invalid.
+ (when (< beg (or (car syntax-ppss-last) 0))
+ ;; If syntax-begin-function jumped to BEG, then the old state at BEG can
+ ;; depend on the text after BEG (which is presumably changed). So if
+ ;; BEG=(car (nth 10 syntax-ppss-last)) don't reuse that data because the
+ ;; assumed nil state at BEG may not be valid any more.
+ (if (<= beg (or (syntax-ppss-toplevel-pos (cdr syntax-ppss-last))
+ (nth 3 syntax-ppss-last)
+ 0))
+ (setq syntax-ppss-last nil)
+ (setcar syntax-ppss-last nil)))
+ ;; Unregister if there's no cache left. Sadly this doesn't work
+ ;; because `before-change-functions' is temporarily bound to nil here.
+ ;; (unless syntax-ppss-cache
+ ;; (remove-hook 'before-change-functions 'syntax-ppss-flush-cache t))
+ )
+
+(defvar syntax-ppss-stats
+ [(0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (1 . 2500.0)])
+(defun syntax-ppss-stats ()
+ (mapcar (lambda (x)
+ (condition-case nil
+ (cons (car x) (truncate (/ (cdr x) (car x))))
+ (error nil)))
+ syntax-ppss-stats))
+
+(defun syntax-ppss (&optional pos)
+ "Parse-Partial-Sexp State at POS, defaulting to point.
+The returned value is the same as `parse-partial-sexp' except that
+the 2nd and 6th values of the returned state cannot be relied upon.
+Point is at POS when this function returns."
+ ;; Default values.
+ (unless pos (setq pos (point)))
+ ;;
+ (let ((old-ppss (cdr syntax-ppss-last))
+ (old-pos (car syntax-ppss-last))
+ (ppss nil)
+ (pt-min (point-min)))
+ (if (and old-pos (> old-pos pos)) (setq old-pos nil))
+ ;; Use the OLD-POS if usable and close. Don't update the `last' cache.
+ (condition-case nil
+ (if (and old-pos (< (- pos old-pos)
+ ;; The time to use syntax-begin-function and
+ ;; find PPSS is assumed to be about 2 * distance.
+ (* 2 (/ (cdr (aref syntax-ppss-stats 5))
+ (1+ (car (aref syntax-ppss-stats 5)))))))
+ (progn
+ (incf (car (aref syntax-ppss-stats 0)))
+ (incf (cdr (aref syntax-ppss-stats 0)) (- pos old-pos))
+ (parse-partial-sexp old-pos pos nil nil old-ppss))
+
+ (cond
+ ;; Use OLD-PPSS if possible and close enough.
+ ((and (not old-pos) old-ppss
+ ;; If `pt-min' is too far from `pos', we could try to use
+ ;; other positions in (nth 9 old-ppss), but that doesn't
+ ;; seem to happen in practice and it would complicate this
+ ;; code (and the before-change-function code even more).
+ ;; But maybe it would be useful in "degenerate" cases such
+ ;; as when the whole file is wrapped in a set
+ ;; of parentheses.
+ (setq pt-min (or (syntax-ppss-toplevel-pos old-ppss)
+ (nth 2 old-ppss)))
+ (<= pt-min pos) (< (- pos pt-min) syntax-ppss-max-span))
+ (incf (car (aref syntax-ppss-stats 1)))
+ (incf (cdr (aref syntax-ppss-stats 1)) (- pos pt-min))
+ (setq ppss (parse-partial-sexp pt-min pos)))
+ ;; The OLD-* data can't be used. Consult the cache.
+ (t
+ (let ((cache-pred nil)
+ (cache syntax-ppss-cache)
+ (pt-min (point-min))
+ ;; I differentiate between PT-MIN and PT-BEST because
+ ;; I feel like it might be important to ensure that the
+ ;; cache is only filled with 100% sure data (whereas
+ ;; syntax-begin-function might return incorrect data).
+ ;; Maybe that's just stupid.
+ (pt-best (point-min))
+ (ppss-best nil))
+ ;; look for a usable cache entry.
+ (while (and cache (< pos (caar cache)))
+ (setq cache-pred cache)
+ (setq cache (cdr cache)))
+ (if cache (setq pt-min (caar cache) ppss (cdar cache)))
+
+ ;; Setup the before-change function if necessary.
+ (unless (or syntax-ppss-cache syntax-ppss-last)
+ (add-hook 'before-change-functions
+ 'syntax-ppss-flush-cache t t))
+
+ ;; Use the best of OLD-POS and CACHE.
+ (if (or (not old-pos) (< old-pos pt-min))
+ (setq pt-best pt-min ppss-best ppss)
+ (incf (car (aref syntax-ppss-stats 4)))
+ (incf (cdr (aref syntax-ppss-stats 4)) (- pos old-pos))
+ (setq pt-best old-pos ppss-best old-ppss))
+
+ ;; Use the `syntax-begin-function' if available.
+ ;; We could try using that function earlier, but:
+ ;; - The result might not be 100% reliable, so it's better to use
+ ;; the cache if available.
+ ;; - The function might be slow.
+ ;; - If this function almost always finds a safe nearby spot,
+ ;; the cache won't be populated, so consulting it is cheap.
+ (when (and (not syntax-begin-function)
+ (boundp 'font-lock-beginning-of-syntax-function)
+ font-lock-beginning-of-syntax-function)
+ (set (make-local-variable 'syntax-begin-function)
+ font-lock-beginning-of-syntax-function))
+ (when (and syntax-begin-function
+ (progn (goto-char pos)
+ (funcall syntax-begin-function)
+ ;; Make sure it's better.
+ (> (point) pt-best))
+ ;; Simple sanity check.
+ (not (memq (get-text-property (point) 'face)
+ '(font-lock-string-face font-lock-doc-face
+ font-lock-comment-face))))
+ (incf (car (aref syntax-ppss-stats 5)))
+ (incf (cdr (aref syntax-ppss-stats 5)) (- pos (point)))
+ (setq pt-best (point) ppss-best nil))
+
+ (cond
+ ;; Quick case when we found a nearby pos.
+ ((< (- pos pt-best) syntax-ppss-max-span)
+ (incf (car (aref syntax-ppss-stats 2)))
+ (incf (cdr (aref syntax-ppss-stats 2)) (- pos pt-best))
+ (setq ppss (parse-partial-sexp pt-best pos nil nil ppss-best)))
+ ;; Slow case: compute the state from some known position and
+ ;; populate the cache so we won't need to do it again soon.
+ (t
+ (incf (car (aref syntax-ppss-stats 3)))
+ (incf (cdr (aref syntax-ppss-stats 3)) (- pos pt-min))
+
+ ;; If `pt-min' is too far, add a few intermediate entries.
+ (while (> (- pos pt-min) (* 2 syntax-ppss-max-span))
+ (setq ppss (parse-partial-sexp
+ pt-min (setq pt-min (/ (+ pt-min pos) 2))
+ nil nil ppss))
+ (let ((pair (cons pt-min ppss)))
+ (if cache-pred
+ (push pair (cdr cache-pred))
+ (push pair syntax-ppss-cache))))
+
+ ;; Compute the actual return value.
+ (setq ppss (parse-partial-sexp pt-min pos nil nil ppss))
+
+ ;; Debugging check.
+ ;; (let ((real-ppss (parse-partial-sexp (point-min) pos)))
+ ;; (setcar (last ppss 4) 0)
+ ;; (setcar (last real-ppss 4) 0)
+ ;; (setcar (last ppss 8) nil)
+ ;; (setcar (last real-ppss 8) nil)
+ ;; (unless (equal ppss real-ppss)
+ ;; (message "!!Syntax: %s != %s" ppss real-ppss)
+ ;; (setq ppss real-ppss)))
+
+ ;; Store it in the cache.
+ (let ((pair (cons pos ppss)))
+ (if cache-pred
+ (if (> (- (caar cache-pred) pos) syntax-ppss-max-span)
+ (push pair (cdr cache-pred))
+ (setcar cache-pred pair))
+ (if (or (null syntax-ppss-cache)
+ (> (- (caar syntax-ppss-cache) pos)
+ syntax-ppss-max-span))
+ (push pair syntax-ppss-cache)
+ (setcar syntax-ppss-cache pair)))))))))
+
+ (setq syntax-ppss-last (cons pos ppss))
+ ppss)
+ (args-out-of-range
+ ;; If the buffer is more narrowed than when we built the cache,
+ ;; we may end up calling parse-partial-sexp with a position before
+ ;; point-min. In that case, just parse from point-min assuming
+ ;; a nil state.
+ (parse-partial-sexp (point-min) pos)))))
+
+;; Debugging functions
+
+(defun syntax-ppss-debug ()
+ (let ((pt nil)
+ (min-diffs nil))
+ (dolist (x (append syntax-ppss-cache (list (cons (point-min) nil))))
+ (when pt (push (- pt (car x)) min-diffs))
+ (setq pt (car x)))
+ min-diffs))
+
+;;; syntax-ppss.el ends here
diff -r 1b054bc2ac40 -r 69a08906ad27 tests/ChangeLog
--- a/tests/ChangeLog Sun Jul 03 14:17:39 2011 +0100
+++ b/tests/ChangeLog Wed Jul 20 00:42:16 2011 +0200
@@ -1,3 +1,7 @@
+2011-07-22 Mats Lidell <matsl(a)xemacs.org>
+
+ * automated/syntax-ppss-tests.el: Add tests for syntax-ppss.el.
+
2011-06-28 Aidan Kehoe <kehoea(a)parhasard.net>
* automated/mule-tests.el:
diff -r 1b054bc2ac40 -r 69a08906ad27 tests/automated/syntax-ppss-tests.el
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/automated/syntax-ppss-tests.el Wed Jul 20 00:42:16 2011 +0200
@@ -0,0 +1,37 @@
+;; Copyright (C) 2011 Free Software Foundation, Inc.
+
+;; Author: Mats Lidell <matsl(a)xemacs.org>
+;; Maintainer:
+;; Created: 2011
+;; Keywords: tests
+
+;; This file is part of XEmacs.
+
+;; XEmacs 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.
+
+;; XEmacs 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 XEmacs. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Synched up with: Not in FSF.
+
+;;; Commentary:
+
+;; Test tag support.
+;; See test-harness.el for instructions on how to run these tests.
+
+(with-temp-buffer
+ (insert ";;;")
+ (Assert (equal (syntax-ppss (point-min)) '(0 nil nil nil nil nil 0 nil nil nil)))
+ (Assert (eq (point-min) (point)))
+ (Assert (equal (syntax-ppss 3) '(0 nil nil nil nil nil 0 nil nil nil)))
+ (Assert (eq 3 (point)))
+ )
+
_______________________________________________
XEmacs-Patches mailing list
XEmacs-Patches(a)xemacs.org
http://lists.xemacs.org/mailman/listinfo/xemacs-patches