[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
scratch/oclosure f44ee8cd53 17/25: oclosure.el (accessor): New type
From: |
Stefan Monnier |
Subject: |
scratch/oclosure f44ee8cd53 17/25: oclosure.el (accessor): New type |
Date: |
Fri, 31 Dec 2021 15:40:58 -0500 (EST) |
branch: scratch/oclosure
commit f44ee8cd5321b508462e03563e1d305debe2228d
Author: Stefan Monnier <monnier@iro.umontreal.ca>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>
oclosure.el (accessor): New type
* lisp/emacs-lisp/oclosure.el (accessor): New (OClosure) type.
(oclosure-define): Mark the accessor functions
as being of type `accessor`.
(oclosure--accessor-cl-print, oclosure--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/oclosure.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 047d198859..6521c3bf7c 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!
+ (oclosure--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 59cbc0e50d..9ac4747f54 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/oclosure.el b/lisp/emacs-lisp/oclosure.el
index 65785a7ed8..956dff7ffa 100644
--- a/lisp/emacs-lisp/oclosure.el
+++ b/lisp/emacs-lisp/oclosure.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'.
+;; - `oclosure-cl-defun', `oclosure-cl-defsubst', `oclosure-defsubst',
`oclosure-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)) (oclosure)
- ,(format "Return slot `%S' of OClosure, of type
`%S'."
- slot name)
- (oclosure-get oclosure ,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))
+ (oclosure-lambda accessor ((type ',name) (slot
',slot))
+ (oclosure)
+ (oclosure-get oclosure ,i)))))
slotdescs))
,@(oclosure--defstruct-make-copiers copiers slots name))))
@@ -315,5 +323,22 @@
(and (eq :type (car-safe first-var))
(cdr first-var))))))
+(oclosure-define accessor
+ "OClosure to access the field of an object."
+ type slot)
+
+(defun oclosure--accessor-cl-print (object stream)
+ (princ "#f(accessor " stream)
+ (prin1 (accessor--type object) stream)
+ (princ "." stream)
+ (prin1 (accessor--slot object) stream)
+ (princ ")" stream))
+
+(defun oclosure--accessor-docstring (f)
+ (format "Access slot \"%S\" of OBJ of type `%S'.
+
+\(fn OBJ)"
+ (accessor--slot f) (accessor--type f)))
+
(provide 'oclosure)
;;; oclosure.el ends here
diff --git a/lisp/simple.el b/lisp/simple.el
index 9227ee5caa..65234732cb 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!
+ (oclosure--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 336ca0b852..5c8f059288 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)
- scratch/oclosure 5574871ec7 09/25: nadvice.el: Use OClosures rather than handmade bytecodes, (continued)
- scratch/oclosure 5574871ec7 09/25: nadvice.el: Use OClosures rather than handmade bytecodes, Stefan Monnier, 2021/12/31
- scratch/oclosure f11349ed20 03/25: * lisp/emacs-lisp/cl-generic.el: Use OClosure for `cl-next-method-p`, Stefan Monnier, 2021/12/31
- scratch/oclosure 230617c90c 16/25: lisp/emacs-lisp/oclosure.el: Signal errors for invalid code, Stefan Monnier, 2021/12/31
- scratch/oclosure e052bb2770 04/25: * lisp/kmacro.el: Use OClosure instead of messing with internals, Stefan Monnier, 2021/12/31
- scratch/oclosure ae493f3513 01/25: OClosure: Hybrids between functions and defstructs, Stefan Monnier, 2021/12/31
- scratch/oclosure ae0bfc4f75 05/25: * lisp/loadup.el (oclosure): Load before `nadvice`, Stefan Monnier, 2021/12/31
- scratch/oclosure afa68def26 11/25: cl-print.el: Dispatch on `advice` type, Stefan Monnier, 2021/12/31
- scratch/oclosure fe5457ff75 19/25: oclosure.el (oclosure-lambda): Change calling convention, Stefan Monnier, 2021/12/31
- scratch/oclosure d93b0ad4d4 06/25: (interactive-form, function-docstring): New generic functions, Stefan Monnier, 2021/12/31
- scratch/oclosure a444d85977 08/25: Fix bootstrap problems and various misc issues found along the way, Stefan Monnier, 2021/12/31
- scratch/oclosure f44ee8cd53 17/25: oclosure.el (accessor): New type,
Stefan Monnier <=
- scratch/oclosure 55a8e92413 20/25: oclosure.el: Add support for mutable slots, Stefan Monnier, 2021/12/31
- scratch/oclosure bc1d94a0d8 21/25: * lisp/emacs-lisp/oclosure.el (Commentary:): Add a few notes, Stefan Monnier, 2021/12/31
- scratch/oclosure 263172dbfb 02/25: lisp/emacs-lisp/oclosure.el: Make it available to cl-generic, Stefan Monnier, 2021/12/31
- scratch/oclosure 3119e59252 07/25: lisp/emacs-lisp/oclosure.el: Rename `oclosure-make` to `oclosure-lambda`, Stefan Monnier, 2021/12/31
- scratch/oclosure 9465a7e59e 10/25: nadvice.el: Restore interactive-form handling, Stefan Monnier, 2021/12/31
- scratch/oclosure 3c9d64b602 14/25: cl-macs.el (cl--transform-lambda): Fix last change, Stefan Monnier, 2021/12/31
- scratch/oclosure 01002ebba0 18/25: oclosure.el (oclosure-define): Use `oclosure--copy` to define accessors, Stefan Monnier, 2021/12/31
- scratch/oclosure 20e5cd82ae 13/25: Fix bug#28557, Stefan Monnier, 2021/12/31
- scratch/oclosure 44dbab47f7 23/25: * lisp/emacs-lisp/oclosure.el: Remove obsolete comment, Stefan Monnier, 2021/12/31
- scratch/oclosure 1ace4acd54 22/25: Replace uniquify.el's advice with direct calls, Stefan Monnier, 2021/12/31