The answer can be split into a general answer to the question as stated in the caption
Any way to access a lexical let variable outside of the let?
and an answer to your specific problem in the description, how to access link-end
in org-element-link-parser
. The general answer does not work for your specific problem since you do not want to modify org-element-link-parser
.
The short answer to both parts is: Yes, you can.
First, I want to address your specific problem:
An excerpt from org-element-parser
:
(defun org-element-link-parser ()
(let (... link-end ...)
(cond
...
;; all cases look like the following:
((looking-at ...)
...
(setq link-end (match-end 0))
...
)
)
;; After the `cond':
(save-excursion
(setq post-blank
(progn (goto-char link-end) (skip-chars-forward " \t")))
(setq end (point)))
(list 'link
(list ;; The element properties:
...
:end end
...))))
So, end
is essentially link-end
, only that there is the additional operation (skip-chars-forward " \t")
.
If you want to retrieve the value of link-end
you have to reverse the effect of (skip-chars-forward " \t")
to the property :end
. Luckily the number of chars skipped by (skip-chars-forward " \t")
is saved as property :post-blank
.
I.e., with point at a link, do:
(when-let ((link (org-element-link-parser))
(post-blank (org-element-property :post-blank link))
(end (org-element-property :end link))
(link-end (- end post-blank)))
link-end)
Now, the answer to the general question
Any way to access a lexical let variable outside of the let?
Yes, define setter/getter functions within the let for accessing the variable.
The variable will be in the lexical environment of these functions.
(declare-function my-set nil)
(declare-function my-get nil)
(defun my-function ()
"Function with the lexically bound variable `my-var'.
We define `my-set' and `my-get' here."
(let ((my-var 1))
(fset 'my-set
(lambda (value)
(setq my-var value)))
(fset 'my-get
(lambda ()
my-var)))
"The function could also return the lambdas.")
(format
"The return value of `my-function': %s
The value of `my-var': %d
Setting `my-var' to 2: %d
The value after `my-set': %d"
(my-function)
(my-get)
(my-set 2)
(my-get))
The resulting output is:
The return value of `my-function': The function could also return the lambdas.
The value of `my-var': 1
Setting `my-var' to 2: 2
The value after `my-set': 2