emacs-diffs
[Top][All Lists]
Advanced

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

scratch/fcr 59f542e: fcr.el (accessor): New type


From: Stefan Monnier
Subject: scratch/fcr 59f542e: fcr.el (accessor): New type
Date: Wed, 22 Dec 2021 10:06:24 -0500 (EST)

branch: scratch/fcr
commit 59f542ef4fb04bc829f5af1728070a1a22c4fd55
Author: Stefan Monnier <monnier@iro.umontreal.ca>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>

    fcr.el (accessor): New type
    
    * lisp/emacs-lisp/fcr.el (accessor): New (FCR) type.
    (fcr-defstruct): Mark the accessor functions
    as being of type `accessor`.
    (fcr--accessor-cl-print, fcr--accessor-docstring): New functions.
    
    * src/doc.c (store_function_docstring): Improve message and fix check.
    * lisp/simple.el (function-docstring) <accessor>: New method.
    * lisp/emacs-lisp/cl-print.el (cl-print-object) <accessor>: New method.
---
 lisp/emacs-lisp/cl-print.el |  4 ++++
 lisp/emacs-lisp/crm.el      |  2 +-
 lisp/emacs-lisp/fcr.el      | 37 +++++++++++++++++++++++++++++++------
 lisp/simple.el              |  4 ++++
 src/doc.c                   |  6 +++++-
 5 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/lisp/emacs-lisp/cl-print.el b/lisp/emacs-lisp/cl-print.el
index 047d198..d5d9356 100644
--- a/lisp/emacs-lisp/cl-print.el
+++ b/lisp/emacs-lisp/cl-print.el
@@ -229,6 +229,10 @@ into a button whose action shows the function's 
disassembly.")
   ;; FIXME: η-reduce!
   (advice--cl-print-object object stream))
 
+(cl-defmethod cl-print-object ((object accessor) stream)
+  ;; FIXME: η-reduce!
+  (fcr--accessor-cl-print object stream))
+
 (cl-defmethod cl-print-object ((object cl-structure-object) stream)
   (if (and cl-print--depth (natnump print-level)
            (> cl-print--depth print-level))
diff --git a/lisp/emacs-lisp/crm.el b/lisp/emacs-lisp/crm.el
index 59cbc0e..9ac4747 100644
--- a/lisp/emacs-lisp/crm.el
+++ b/lisp/emacs-lisp/crm.el
@@ -210,7 +210,7 @@ This function is modeled after 
`minibuffer-complete-and-exit'."
     (if doexit (exit-minibuffer))))
 
 (defun crm--choose-completion-string (choice buffer base-position
-                                             &rest ignored)
+                                             &rest _)
   "Completion string chooser for `completing-read-multiple'.
 This is called from `choose-completion-string-functions'.
 It replaces the string that is currently being completed, without
diff --git a/lisp/emacs-lisp/fcr.el b/lisp/emacs-lisp/fcr.el
index 970dcfb..51933f0 100644
--- a/lisp/emacs-lisp/fcr.el
+++ b/lisp/emacs-lisp/fcr.el
@@ -38,6 +38,8 @@
 ;;   simply has an additional `docstring' slot.
 ;; - commands: this could be a subtype of documented functions, which simply
 ;;   has an additional `interactive-form' slot.
+;; - auto-generate docstrings for slot accessors instead of storing them
+;;   in the accessor itself?
 
 ;;; Code:
 
@@ -55,6 +57,11 @@
 ;;   store-conversion is indispensable, so if we want to avoid store-conversion
 ;;   we'd have to disallow such capture.
 
+;; FIXME:
+;; - Snarf-documentation leaves bogus fixnums in place in`create-file-buffer'.
+;; - `fcr-cl-defun', `fcr-cl-defsubst', `fcr-defsubst', `fcr-define-inline'?
+;; - Use accessor in cl-defstruct
+
 (eval-when-compile (require 'cl-lib))
 (eval-when-compile (require 'subr-x))   ;For `named-let'.
 
@@ -186,12 +193,13 @@
                        (when (gethash slot it)
                          (error "Duplicate slot name: %S" slot))
                        (setf (gethash slot it) i)
-                       ;; Always use a double hyphen: if the user wants to
-                       ;; make it public, it can do so with an alias.
-                       `(defun ,(intern (format "%S--%S" name slot)) (fcr)
-                          ,(format "Return slot `%S' of FCR, of type `%S'."
-                                   slot name)
-                          (fcr-get fcr ,i))))
+                       ;; Always use a double hyphen: if users wants to
+                       ;; make it public, they can do so with an alias.
+                       ;; FIXME: Use a copier!
+                       `(defalias ',(intern (format "%S--%S" name slot))
+                          (fcr-lambda accessor ((type ',name) (slot ',slot))
+                                      (fcr)
+                            (fcr-get fcr ,i)))))
                    slotdescs))
        ,@(fcr--defstruct-make-copiers copiers slots name))))
 
@@ -315,5 +323,22 @@
            (and (eq :type (car-safe first-var))
                 (cdr first-var))))))
 
+(fcr-defstruct accessor
+  "FCR to access the field of an object."
+  type slot)
+
+(defun fcr--accessor-cl-print (object stream)
+  (princ "#f(accessor " stream)
+  (prin1 (accessor--type object) stream)
+  (princ "." stream)
+  (prin1 (accessor--slot object) stream)
+  (princ ")" stream))
+
+(defun fcr--accessor-docstring (f)
+  (format "Access slot \"%S\" of OBJ of type `%S'.
+
+\(fn OBJ)"
+          (accessor--slot f) (accessor--type f)))
+
 (provide 'fcr)
 ;;; fcr.el ends here
diff --git a/lisp/simple.el b/lisp/simple.el
index 9227ee5..bfbfe1b 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -2348,6 +2348,10 @@ FUNCTION is expected to be a function value rather than, 
say, a mere symbol."
            doc)))
       (_ (signal 'invalid-function (list function))))))
 
+(cl-defmethod function-docstring ((function accessor))
+  ;; FIXME: η-reduce!
+  (fcr--accessor-docstring function))
+
 (cl-defgeneric interactive-form (cmd &optional original-name)
   "Return the interactive form of CMD or nil if none.
 If CMD is not a command, the return value is nil.
diff --git a/src/doc.c b/src/doc.c
index 336ca0b..5c8f059 100644
--- a/src/doc.c
+++ b/src/doc.c
@@ -469,11 +469,15 @@ store_function_docstring (Lisp_Object obj, EMACS_INT 
offset)
          /* Don't overwrite a non-docstring value placed there,
            * such as is used in FCRs.  */
          && (FIXNUMP (AREF (fun, COMPILED_DOC_STRING))
+             || STRINGP (AREF (fun, COMPILED_DOC_STRING))
              || CONSP (AREF (fun, COMPILED_DOC_STRING))))
        ASET (fun, COMPILED_DOC_STRING, make_fixnum (offset));
       else
        {
-         AUTO_STRING (format, "No docstring slot for %s");
+         AUTO_STRING (format,
+                      (PVSIZE (fun) > COMPILED_DOC_STRING
+                       ? "Docstring slot busy for %s"
+                       : "No docstring slot for %s"));
          CALLN (Fmessage, format,
                 (SYMBOLP (obj)
                  ? SYMBOL_NAME (obj)



reply via email to

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