help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: how to get around deprecated function


From: Emanuel Berg
Subject: Re: how to get around deprecated function
Date: Tue, 05 May 2015 19:31:12 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

B. T. Raven <btraven@nihil.net> writes:

> The following related function might also benefit
> from some "subtractional betterment" in case you
> care to comment. This just appends the line where
> cursor (or maybe more accurately 'point') happens to
> be to the *scratch* buffer. I know I can make beg,
> end local to function by using let instead of setq
> but I always re-initialize beg and end:
>
> (defun append-line-to-starscratch ();; M-x asl
> (interactive)
> (move-beginning-of-line nil)
> (set-mark-command nil) (setq beg (point))
> (move-end-of-line nil) (setq end (point))
> (kill-ring-save beg end)
> (with-current-buffer "*scratch*"
>      (end-of-buffer) (insert "\n")
>      (yank)))

There are many ways to do that.

The only thing I would change (as in correcting an
error) in the above code is to use `save-excursion'
because you move point but the operation has nothing
to do with moving it - you only do it to get
the endpoints.

I would do it like this:

    (defun append-line-to-buffer (buffer) ; make the function generic (or more 
so)
      (when (get-buffer buffer)  ; check if input makes sense (always do that)
        (save-excursion          ; use this when point is moved (almost always 
do that)
          (beginning-of-line)    ; `move-beginning-of-line' is Lisp, this 
faster (?) C
          (let ((start (point))) ; get start
            (end-of-line)        ; and end
            (let ((line-str (buffer-substring-no-properties start (point)))) ; 
get string
              (with-current-buffer buffer
                (end-of-buffer)
                (insert line-str "\n")) )))))

    (defun append-line-to-scratch () ; then do (specific) help function
      (interactive)
      (append-line-to-buffer "*scratch*") )

-- 
underground experts united
http://user.it.uu.se/~embe8573


reply via email to

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