unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Emanuel Berg <embe8573@student.uu.se>
To: help-gnu-emacs@gnu.org
Subject: avoiding global variables (was: Re: if and only if an Error message)
Date: Mon, 21 Jul 2014 00:48:17 +0200	[thread overview]
Message-ID: <87tx6br6j2.fsf_-_@debian.uxu> (raw)
In-Reply-To: 87lhrpd0wq.fsf@debian.uxu

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 ()
  (interactive)
  (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


  parent reply	other threads:[~2014-07-20 22:48 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-18 22:21 Emacs hangs with 100pc CPU during query-replace Thorsten Jolitz
2014-07-19  0:30 ` Michael Heerdegen
     [not found] ` <mailman.5641.1405730121.1147.help-gnu-emacs@gnu.org>
2014-07-19  1:16   ` Emanuel Berg
2014-07-19  1:36     ` Michael Heerdegen
2014-07-19  1:41       ` Michael Heerdegen
     [not found]       ` <mailman.5647.1405734322.1147.help-gnu-emacs@gnu.org>
2014-07-19  1:57         ` Emanuel Berg
2014-07-19  3:42           ` if and only if an Error message (was: Re: Emacs hangs with 100pc CPU during query-replace) Emanuel Berg
2014-07-19  4:00             ` if and only if an Error message Michael Heerdegen
     [not found]             ` <mailman.5654.1405742487.1147.help-gnu-emacs@gnu.org>
2014-07-19  4:10               ` Emanuel Berg
2014-07-19  4:38                 ` Michael Heerdegen
     [not found]                 ` <mailman.5655.1405744730.1147.help-gnu-emacs@gnu.org>
2014-07-19  4:49                   ` Emanuel Berg
2014-07-19  5:05                     ` Michael Heerdegen
2014-07-19  5:11                     ` Michael Heerdegen
     [not found]                     ` <mailman.5656.1405746345.1147.help-gnu-emacs@gnu.org>
2014-07-19  5:12                       ` Emanuel Berg
2014-07-19  5:49                         ` Michael Heerdegen
2014-07-19  6:01                           ` Michael Heerdegen
     [not found]                         ` <mailman.5660.1405749012.1147.help-gnu-emacs@gnu.org>
2014-07-19 11:48                           ` Emanuel Berg
2014-07-19 17:55                             ` Drew Adams
2014-07-20  1:42                             ` Michael Heerdegen
     [not found]                             ` <mailman.5724.1405820570.1147.help-gnu-emacs@gnu.org>
2014-07-20 21:38                               ` Emanuel Berg
2014-07-21  8:51                                 ` Michael Heerdegen
     [not found]                                 ` <mailman.5803.1405932728.1147.help-gnu-emacs@gnu.org>
2014-07-21 14:12                                   ` Emanuel Berg
2014-07-20 22:48                             ` Emanuel Berg [this message]
2014-07-21  5:01                               ` avoiding global variables Thien-Thi Nguyen
     [not found]                               ` <mailman.5793.1405918685.1147.help-gnu-emacs@gnu.org>
2014-07-21 14:10                                 ` Emanuel Berg
     [not found]     ` <mailman.5645.1405733824.1147.help-gnu-emacs@gnu.org>
2014-07-19  1:52       ` Emacs hangs with 100pc CPU during query-replace Emanuel Berg
2014-07-19  8:02     ` Thorsten Jolitz
2014-07-19  8:18       ` Eli Zaretskii
2014-07-19  8:27       ` Michael Heerdegen
2014-07-19  8:49         ` Thorsten Jolitz
2014-07-19  9:06         ` Thorsten Jolitz
2014-07-19 10:27           ` Eli Zaretskii
2014-07-19 11:29             ` Thorsten Jolitz
2014-07-19 10:33           ` Eli Zaretskii
2014-07-19 11:30             ` Thorsten Jolitz
     [not found]         ` <<871tth1zw9.fsf@gmail.com>
     [not found]           ` <<83iomt63uf.fsf@gnu.org>
2014-07-19 17:51             ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87tx6br6j2.fsf_-_@debian.uxu \
    --to=embe8573@student.uu.se \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).