From the point current position inside an Emacs buffer, how can I get the outermost list/s-expression which contains that point ?
Follows a few examples in order to illustrate what I want to achieve:
Example 1:
(defun xpto(arg)
(+ 2 4)[point])
Output 1:
(defun xpto(arg)
(+ 2 4))
Example 2:
(defun [point]xpto(arg)
(+ 2 4))
Output 2:
(defun xpto(arg)
(+ 2 4))
Example 3:
(defun xpto(arg)
(+ 2 4))[point]
Output 3:
NIL
Is there any "off the shelf" function provided by Emacs that accomplishes the above mentioned behavior?
If not, can you guide me towards the easier approach in order to accomplish it ?
UPDATE #1
As suggested I tried the following approach:
;; Gets the outermost brackets based on point position
(defun get-outermost-brackets()
(interactive)
(car (last (nth 9 (syntax-ppss)))))
;; Assigns that function to a key
(global-set-key [(control p)] 'get-outermost-brackets)
However, after pressing CTRL+P I get no output no matter where the point is at.
What am I doing wrong ?
What you are looking for is the function parse-partial-sexp
which returns the parser state (a 10-element list!) which you can use to figure out the start and end positions of your sexp.
Anther useful function will be scan-sexps
.
UPDATE#1:
Your function get-outermost-brackets
returns the position of of the outermost open paren, not a useful part of your buffer, and that value is not going to show in the echo area anyway.
UPDATE#2:
This will send the enclosing sexp to the echo area and the *Messages*
buffer:
(defun get-enclosing-sexp (&optional pos)
(interactive)
(message "%s" (save-excursion
(goto-char (car (last (nth 9 (syntax-ppss)))))
(read (current-buffer)))))
Drop message
if you want to return the sexp from the function instead of just showing it.
Where is my million?