0

I have written a function snippet that copies a paragraph to the kill-ring. I try to collect different point positions and corresponding line numbers:

  • bop - begin of paragraph
  • blnum - line umber of the paragraph begin
  • eop - end of paragraph
  • elnum - line number of the paragraph ending

...see below code

and store them in some variable via setq. I know that I'm using here a global variable context in a false way. What is the equivalent code using a local variables scope only?

To be clear, none of variables (bop, eop, blnum, elnum) will be used outside of the function context.

CODE

(defun my/copy-paragraph ()
  "Copy a paragraph to the kill ring."
  (interactive)
  (backward-paragraph)
  (setq bop (point))
  (setq blnum (line-number-at-pos))
  (forward-paragraph)
  (setq elnum (line-number-at-pos))
  (setq eop (point))
  (kill-ring-save bop eop)
  (move-beginning-of-line nil)
  (forward-line)
  (message "Copy paragraph from '%s' line %d - %d to kill-ring "
           (buffer-name) blnum elnum))
shynur
  • 4,065
  • 1
  • 3
  • 23
huckfinn
  • 113
  • 5

1 Answers1

1

The question isn't clear. What do you want the (lexical) scope of the variables to be? What code do you want to be inside the scope, and what code outside it? A wild guess is that you just want to use let in your function body. Bind variables, then use setq in the body to give them values at the appropriate places.

(defun my/copy-paragraph ()
  "Copy a paragraph to the kill ring."
  (interactive)
  (let (bop blnum elnum eop)
    (backward-paragraph)
    (setq bop    (point)
          blnum  (line-number-at-pos))
    (forward-paragraph)
    (setq elnum  (line-number-at-pos)
          eop    (point))
    (kill-ring-save bop eop)
    (line-beginning-position)
    (forward-line)
    (message "Copy paragraph from '%s' line %d - %d to kill-ring "
             (buffer-name) blnum elnum)))

But you're not even using variables bop and eop here, so presumably you want to use them somewhere else? It's not clear where, which is why the question as a whole isn't clear. What's the lexical scope for them - is it just this function? This function doesn't call anything that would conceivably use them, so I can't imagine what they're for or what their scope should be.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • To be clear, none of them (`bop, eop, blnum, elnum`) will be used outside of the function context. As I understood from your code, a simple `(let ...)` declaration will change the variable scope using `(setq ...)`. My understanding was `setq` works always in global scope. The points `bop & eop` are used in `(kill-ring-save bop eop)` ..not clear what you meaning here? – huckfinn Apr 10 '23 at 20:13
  • Oh right, all of those var are used here. Yes, if a var is not declared to be global (e.g. via `defvar`) then let-binding a variable of that name creates a lexical binding, and `setq` within the `let` binds the variable to the `setq` value. From what you say you want, all you need is what I wrote: a `let` with `setq`s for its vars in the `let` body. – Drew Apr 11 '23 at 00:10