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

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

Re: FW: [External] : Re: Is there symbol-plist functionality in Emacs Li


From: Jean Louis
Subject: Re: FW: [External] : Re: Is there symbol-plist functionality in Emacs Lisp?
Date: Sat, 8 May 2021 19:23:35 +0300
User-agent: Mutt/2.0.6 (2021-03-06)

* Drew Adams <drew.adams@oracle.com> [2021-05-08 17:39]:
> As for how to get the function def, the answer
> is `symbol-function', AFAIK.

That is it.

(symbol-function 'rcd-markdown) ⇒ (closure (t) (text) "Markdown processing" (if 
text (rcd-command-output-from-input "markdown" text) ""))

I see what it means in the manual:

,----
|    Currently, an Emacs Lisp closure object is represented by a list with
| the symbol ‘closure’ as the first element, a list representing the
| lexical environment as the second element, and the argument list and
| body forms as the remaining elements:
| 
|      ;; lexical binding is enabled.
|      (lambda (x) (* x x))
|           ⇒ (closure (t) (x) (* x x))
`----

If not defined under lexical scoping it looks like:

(symbol-function 'myfn) ⇒ (lambda (a) a)

Thank you, yhat is good enough, as it is list and I can, under
conditions, change it to become able to save the function. It
serves tracking of some unused and soon to be deleted functions.

(defun save-function (f)
  (let* ((fun (symbol-function f))
         (type (car fun)))
    (cond ((eq type 'lambda) (setq fun (append (list 'defun f) (cdr fun))))
          ((eq type 'closure) (setq fun (append (list 'defun f) (nthcdr 2 
fun)))))
    ;; save to file comes here
    fun))

(defun myfn (a) (+ 2 2 a))

(save-function 'myfn) ⇒ (defun myfn (a) (+ 2 2 a))

(save-function 'rcd-markdown) ⇒ (defun rcd-markdown (text) "Markdown 
processing" (if text (rcd-command-output-from-input "markdown" text) ""))

(defun string-to-file-force (string file)
  "Prints string into file, matters not if file exists. Returns FILE as file 
name."
    (with-temp-file file
      (insert string))
    file)

Then now I can save at least those functions defined by myself, it
serves my long term introspection, and preservation of single, from
file deleted functions.

(defun save-function ()
  (interactive)
  (let ((function (function-called-at-point)))
    (when (and (symbolp function)
               (y-or-n-p (format "Save `%s'" (symbol-name function))))
      (let* ((fun (symbol-function function))
             (type (car fun))
             (file (concat "/tmp/function-" (symbol-name function) ".el")))
    (cond ((eq type 'lambda) (setq fun (append (list 'defun function) (cdr 
fun))))
          ((eq type 'closure) (setq fun (append (list 'defun function) (nthcdr 
2 fun)))))
    (message (string-to-file-force (prin1-to-string fun) file))))))

(define-key emacs-lisp-mode-map (kbd "<f6>") 'save-function)

It gives output like this, that is acceptable:

(defun save-function nil (interactive) (let (#'(function-called-at-point)) (if 
(and (symbolp function) (y-or-n-p (format "Save `%s'" (symbol-name function)))) 
(progn (let* ((fun (symbol-function function)) (type (car fun)) (file (concat 
"/tmp/function-" (symbol-name function) ".el"))) (cond ((eq type 'lambda) (setq 
fun (append (list 'defun function) (cdr fun)))) ((eq type 'closure) (setq fun 
(append (list 'defun function) (nthcdr 2 fun))))) (message 
(string-to-file-force (prin1-to-string fun) file)))))))



-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/
https://rms-support-letter.github.io/




reply via email to

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