* towards sh-like 1> and 2> emulation: emacs-exit-messages
@ 2012-07-31 21:24 Thien-Thi Nguyen
0 siblings, 0 replies; only message in thread
From: Thien-Thi Nguyen @ 2012-07-31 21:24 UTC (permalink / raw)
To: emacs-devel
[-- Attachment #1.1: Type: text/plain, Size: 1194 bytes --]
On another (explicitly unnamed) mailing list, there is discussion of
replacing the standalone info reader w/ a specially-dumped (purpose
built) Emacs. Reuben Thomas posted a workable sketch that exposes two
problems (i.e., "code enhancement opportunities" as was known in some
circles):
- forced async operation of ‘man’
workaround: zonk ‘start-process’
- reliance on /bin/sh to display a message to stderr
The first can be addressed purely in Lisp, and is not the subject of
this message (i trust someone will DTRT). The second is part of a more
general infelicity in using Emacs in batch (or "semi-batch") mode. The
global var ‘noninteractive’ is much too crude a switch for such cases.
So, refinement is indicated.
But where?
At first, i tried mucking w/ src/print.c func ‘strout’, interpreting a
small-integer (file-descriptor-ish) ‘printcharfun’ as a specification
for stdout (1) or stderr (2) in another ‘else if’ clause. This failed
to produce output, and i didn't investigate. Was i too hasty?
The second approach is attached (caveat: just a sketch; no multibyte,
locale, bidi awareness; poor error handling) here:
[-- Attachment #1.2: emacs-exit-messages.diff --]
[-- Type: text/x-diff, Size: 2292 bytes --]
commit bb69d9879f9ff7ed0659a8e7505479275affd9b9
Author: Thien-Thi Nguyen <ttn@gnuvola.org>
Date: 2012-07-31 22:18:28 +0200
Add var ‘emacs-exit-messages’ and handle it during shutdown.
* emacs.c (shut_down_emacs): After resetting the tty, display
any strings specified by ‘emacs-exit-messages’ to stderr/stdout.
(syms_of_emacs Vemacs_exit_messages): New DEFVAR_LISP.
---
src/emacs.c | 32 ++++++++++++++++++++++++++++++++
1 files changed, 32 insertions(+), 0 deletions(-)
diff --git a/src/emacs.c b/src/emacs.c
index 2194cfe..210c5a5 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -2051,6 +2051,30 @@ shut_down_emacs (int sig, int no_x, Lisp_Object stuff)
reset_all_sys_modes ();
#endif
+ /* If there are any messages, display them now.
+ Silently ignore ill-formed data, which is a latent bug. */
+ while (CONSP (Vemacs_exit_messages))
+ {
+ Lisp_Object msg = Fcar (Vemacs_exit_messages);
+ FILE *to = stderr;
+
+ if (CONSP (msg) && INTEGERP (Fcar (msg)))
+ {
+ if (1 == XINT (Fcar (msg)))
+ to = stdout;
+ msg = Fcdr (msg);
+ }
+ if (STRINGP (msg))
+ {
+ size_t expected = SBYTES (msg);
+ size_t actually = fwrite (SDATA (msg), 1, expected, to);
+
+ if (expected > actually)
+ abort ();
+ }
+ Vemacs_exit_messages = Fcdr (Vemacs_exit_messages);
+ }
+
stuff_buffered_input (stuff);
inhibit_sentinels = 1;
@@ -2438,6 +2462,14 @@ Before Emacs 24.1, the hook was not run in batch mode, i.e., if
`noninteractive' was non-nil. */);
Vkill_emacs_hook = Qnil;
+ DEFVAR_LISP ("emacs-exit-messages", Vemacs_exit_messages,
+ doc: /* List of strings to display to stderr at exit.
+These are output as-is, so you need to include a newline.
+Each element may also have form (FD . STRING), where FD is
+a small integer: 1 for standard output, 2 for standard error.
+All other FD values are taken as 2. */);
+ Vemacs_exit_messages = Qnil;
+
DEFVAR_LISP ("path-separator", Vpath_separator,
doc: /* String containing the character that separates directories in
search paths, such as PATH and other similar environment variables. */);
[-- Attachment #1.3: Type: text/plain, Size: 662 bytes --]
OT1H, this is still relatively crude; saving output to the end is both
inelegant and suboptimal (but perhaps i repeat myself). OTOH, it does
the job: Emacs can effect sh-like ‘1>’ and ‘2>’ control to fellow and
parent processes, finally.
Anyway, w/ that patch applied, the following program, slightly modified
From the original, works fine (look ma, no /bin/sh!) w/ invocations:
$ /tmp/info cat # interactive, "(coreutils) cat invocation"
$ /tmp/info emacs # interactive, "(emacs) Top"
$ /tmp/info zow # noninteractive, exit 1, msg on stderr
To play, save to /tmp/info, chmod +x, and frob "bloody-emacs" to taste.
[-- Attachment #1.4: info --]
[-- Type: application/octet-stream, Size: 1764 bytes --]
":" ; exec bloody-emacs -q --no-splash --load $0 "$@"
;;; info --- Use emacs as a standalone info & man reader
;; Author: Reuben Thomas
;; Thien-Thi Nguyen
;; Last-Modified: 2012-07-31
;;; Commentary:
;; Usage: info TOPIC
;;
;; Mimic the standalone info, searching for info format documentation
;; for TOPIC, and falling back to manpage. If neither method succeeds,
;; display a message to stderr and exit failurefully.
;;; Code:
(require 'man)
(require 'info)
(defun info-or-man (topic)
(condition-case ierr (info topic)
(error (condition-case ierr
(progn (info)
(Info-goto-node (Info-extract-menu-item topic)))
(error (unless (man topic)
;; What is ‘user-error’? --ttn
(user-error "No info or manual entry for %s"
topic)))))))
;; FIXME: Trapping the error from Emacs's man function requires a hack:
;; unbind ‘start-process’ to force man to be run synchronously, and
;; hence allow the error to be trapped. Otherwise, it is trapped by the
;; process sentinel.
(fmakunbound 'start-process)
(define-key Man-mode-map "q" 'kill-emacs)
(define-key Info-mode-map "q" 'kill-emacs)
(setq Man-notify-method 'pushy)
(let ((topic (pop command-line-args-left)))
(set-frame-parameter nil 'title (format "info: %s" topic))
(condition-case ierr (info-or-man topic)
(error (push
;; NB: Naked ‘STRING’ is the same as ‘(2 . STRING)’.
;; This is for demonstration purposes.
(cons 2 (format "info: No such info or man page: %s\n"
topic))
emacs-exit-messages)
(kill-emacs 1))))
;;; Local variables:
;;; mode: emacs-lisp
;;; End:
;;; info ends here
[-- Attachment #1.5: Type: text/plain, Size: 447 bytes --]
Now just watch: Someone will tell me this is already doable... :-/
--
Thien-Thi Nguyen ..................................... GPG key: 4C807502
. NB: ttn at glug dot org is not me .
. (and has not been since 2007 or so) .
. ACCEPT NO SUBSTITUTES .
........... please send technical questions to mailing lists ...........
[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2012-07-31 21:24 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-31 21:24 towards sh-like 1> and 2> emulation: emacs-exit-messages Thien-Thi Nguyen
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.