diff options
| author | Stefan Monnier | 2017-03-18 21:24:39 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2017-03-18 21:24:39 -0400 |
| commit | 32bb5a945a47b14fa85dc1c2f1776b6baa3b0dcc (patch) | |
| tree | 477818c6388435bcabbf968d2b2851f06b728ae1 | |
| parent | e0eb1af55f7b6d0b41e6f0180438f8317628894b (diff) | |
| download | emacs-32bb5a945a47b14fa85dc1c2f1776b6baa3b0dcc.tar.gz emacs-32bb5a945a47b14fa85dc1c2f1776b6baa3b0dcc.zip | |
Improve describe-symbol's layout of slots when describing types
* lisp/emacs-lisp/cl-extra.el (cl--print-table): New function.
(cl--describe-class-slots): Use it.
| -rw-r--r-- | lisp/emacs-lisp/cl-extra.el | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/lisp/emacs-lisp/cl-extra.el b/lisp/emacs-lisp/cl-extra.el index 8cba9137105..8b3d6eecf5c 100644 --- a/lisp/emacs-lisp/cl-extra.el +++ b/lisp/emacs-lisp/cl-extra.el | |||
| @@ -865,6 +865,40 @@ including `cl-block' and `cl-eval-when'." | |||
| 865 | "\n"))) | 865 | "\n"))) |
| 866 | "\n")) | 866 | "\n")) |
| 867 | 867 | ||
| 868 | (defun cl--print-table (header rows) | ||
| 869 | ;; FIXME: Isn't this functionality already implemented elsewhere? | ||
| 870 | (let ((cols (apply #'vector (mapcar #'string-width header))) | ||
| 871 | (col-space 2)) | ||
| 872 | (dolist (row rows) | ||
| 873 | (dotimes (i (length cols)) | ||
| 874 | (let* ((x (pop row)) | ||
| 875 | (curwidth (aref cols i)) | ||
| 876 | (newwidth (if x (string-width x) 0))) | ||
| 877 | (if (> newwidth curwidth) | ||
| 878 | (setf (aref cols i) newwidth))))) | ||
| 879 | (let ((formats '()) | ||
| 880 | (tmp-head header) | ||
| 881 | (col 0)) | ||
| 882 | (dotimes (i (length cols)) | ||
| 883 | (let ((head (pop tmp-head))) | ||
| 884 | (push (concat (propertize " " | ||
| 885 | 'display | ||
| 886 | `(space :align-to ,(+ col col-space))) | ||
| 887 | "%s") | ||
| 888 | formats) | ||
| 889 | (cl-incf col (+ col-space (aref cols i))))) | ||
| 890 | (let ((format (mapconcat #'identity (nreverse formats) ""))) | ||
| 891 | (insert (apply #'format format | ||
| 892 | (mapcar (lambda (str) (propertize str 'face 'italic)) | ||
| 893 | header)) | ||
| 894 | "\n") | ||
| 895 | (insert (apply #'format format | ||
| 896 | (mapcar (lambda (str) (make-string (string-width str) ?—)) | ||
| 897 | header)) | ||
| 898 | "\n") | ||
| 899 | (dolist (row rows) | ||
| 900 | (insert (apply #'format format row) "\n")))))) | ||
| 901 | |||
| 868 | (defun cl--describe-class-slots (class) | 902 | (defun cl--describe-class-slots (class) |
| 869 | "Print help description for the slots in CLASS. | 903 | "Print help description for the slots in CLASS. |
| 870 | Outputs to the current buffer." | 904 | Outputs to the current buffer." |
| @@ -877,7 +911,22 @@ Outputs to the current buffer." | |||
| 877 | (cl-struct-unknown-slot nil)))) | 911 | (cl-struct-unknown-slot nil)))) |
| 878 | (insert (propertize "Instance Allocated Slots:\n\n" | 912 | (insert (propertize "Instance Allocated Slots:\n\n" |
| 879 | 'face 'bold)) | 913 | 'face 'bold)) |
| 880 | (mapc #'cl--describe-class-slot slots) | 914 | (let* ((has-doc nil) |
| 915 | (slots-strings | ||
| 916 | (mapcar | ||
| 917 | (lambda (slot) | ||
| 918 | (list (cl-prin1-to-string (cl--slot-descriptor-name slot)) | ||
| 919 | (cl-prin1-to-string (cl--slot-descriptor-type slot)) | ||
| 920 | (cl-prin1-to-string (cl--slot-descriptor-initform slot)) | ||
| 921 | (let ((doc (alist-get :documentation | ||
| 922 | (cl--slot-descriptor-props slot)))) | ||
| 923 | (if (not doc) "" | ||
| 924 | (setq has-doc t) | ||
| 925 | (substitute-command-keys doc))))) | ||
| 926 | slots))) | ||
| 927 | (cl--print-table `("Name" "Type" "Default" . ,(if has-doc '("Doc"))) | ||
| 928 | slots-strings)) | ||
| 929 | (insert "\n") | ||
| 881 | (when (> (length cslots) 0) | 930 | (when (> (length cslots) 0) |
| 882 | (insert (propertize "\nClass Allocated Slots:\n\n" 'face 'bold)) | 931 | (insert (propertize "\nClass Allocated Slots:\n\n" 'face 'bold)) |
| 883 | (mapc #'cl--describe-class-slot cslots)))) | 932 | (mapc #'cl--describe-class-slot cslots)))) |