[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

avoiding global variables (was: Re: if and only if an Error message)

From: Emanuel Berg
Subject: avoiding global variables (was: Re: if and only if an Error message)
Date: Mon, 21 Jul 2014 00:48:17 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

I thought perhaps the new insights on dynamic and
lexical scope could help me with a problem I've had a
couple of times. I think the reason I never had any
problems with dynamic vs. lexical is the way I almost
exclusively used identifiers that were either of `let's
or function parameters. But I came to think of one case
that wasn't so, and an example of that I posted in the
thread on the debugger (code last). Here, is there any
way to not make `error-buffer-name' global but to
associate it exclusively with the two defuns that use
it? If it were part of an interface for other Elisp to
perhaps use, a global variable is one way of doing
that, but when I wrote it, I have to admit I only made
it global so that both defuns could get access to
it. The only way I can think of not having it that way
is to merge the two defuns, then have them separated
internally with `labels' (`cl-labels'), and used the
one or the other a function of an argument - and that
would be... bulky. If it is OK to use global like this,
should you rely on long and original prefixes to avoid
collisions? Or should you just code whatever you like
and solve collisions if/when they appear? It is not
like this is a Mars expedition or
something. Still... it is the attitude. It doesn't
matter if you recycle cans or do stock exchange, you
want to be all slick just the same.

(setq error-buffer-name "*Errors*")

;; inhibit the debugger -
;;   try this if nothing else: a wonderful thing to not
;;   have it hop out over half the screen for something
;;   that can be communicated in a few words
(setq debug-on-error nil)
(setq eval-expression-debug-on-error nil)

(defun say-and-log-error (data _ fun)
  (let*((error-str (format "%S in %S" data fun))
        (error-buffer (get-buffer-create error-buffer-name))
        (error-win (get-buffer-window error-buffer)) )
    (message error-str)                  ; echo the error message
    (with-current-buffer error-buffer
      (goto-char (point-max))
      (insert error-str "\n") )          ; log it
    (if error-win
        (with-selected-window error-win  ; move focus of error window
          (goto-char (point-max))        ; if visible
          (recenter -1)) )))             ; (is there a better way?)

(setq command-error-function
      (lambda (&rest args)
        (apply #'say-and-log-error args)) )

(defun show-errors ()
  (switch-to-buffer (get-buffer-create error-buffer-name))
  (goto-char (point-max)) ; the same situation: the last line of data
  (recenter -1) )         ; should be on the last window line
                          ; whenever possible

; test here:
; (message 'error-as-not-string)

underground experts united

reply via email to

[Prev in Thread] Current Thread [Next in Thread]