初稿: 2018-08-09 Thu 10:45
最終更新日: 2018-12-14 Fri 20:44
ホーム | 文書トップ | 目次

org-string-display
文字列を表示したバッファを返す

 1: (defun org-string-display (string)
 2:   "Return STRING as it is displayed in the current buffer.
 3: This function takes into consideration `invisible' and `display'
 4: text properties."
 5:   (let* ((build-from-parts
 6:           (lambda (s property filter)
 7:             ;; Build a new string out of string S.  On every group of
 8:             ;; contiguous characters with the same PROPERTY value,
 9:             ;; call FILTER on the properties list at the beginning of
10:             ;; the group.  If it returns a string, replace the
11:             ;; characters in the group with it.  Otherwise, preserve
12:             ;; those characters.
13:             (let ((len (length s))
14:                   (new "")
15:                   (i 0)
16:                   (cursor 0))
17:               (while (setq i (text-property-not-all i len property nil s))
18:                 (let ((end (next-single-property-change i property s len))
19:                       (value (funcall filter (text-properties-at i s))))
20:                   (when value
21:                     (setq new (concat new (substring s cursor i) value))
22:                     (setq cursor end))
23:                   (setq i end)))
24:               (concat new (substring s cursor)))))
25:          (prune-invisible
26:           (lambda (s)
27:             (funcall build-from-parts s 'invisible
28:                      (lambda (props)
29:                        ;; If `invisible' property in PROPS means text
30:                        ;; is to be invisible, return the empty string.
31:                        ;; Otherwise return nil so that the part is
32:                        ;; skipped.
33:                        (and (or (eq t buffer-invisibility-spec)
34:                                 (assoc-string (plist-get props 'invisible)
35:                                               buffer-invisibility-spec))
36:                             "")))))
37:          (replace-display
38:           (lambda (s)
39:             (funcall build-from-parts s 'display
40:                      (lambda (props)
41:                        ;; If there is any string specification in
42:                        ;; `display' property return it.  Also attach
43:                        ;; other text properties on the part to that
44:                        ;; string (face...).
45:                        (let* ((display (plist-get props 'display))
46:                               (value (if (stringp display) display
47:                                        (cl-some #'stringp display))))
48:                          (when value
49:                            (apply #'propertize
50:                                   ;; Displayed string could contain
51:                                   ;; invisible parts, but no nested
52:                                   ;; display.
53:                                   (funcall prune-invisible value)
54:                                   'display
55:                                   (and (not (stringp display))
56:                                        (cl-remove-if #'stringp display))
57:                                   props))))))))
58:     ;; `display' property overrides `invisible' one.  So we first
59:     ;; replace characters with `display' property.  Then we remove
60:     ;; invisible characters.
61:     (funcall prune-invisible (funcall replace-display string))))

日付: 2018-08-09 Thu 10:45

著者: conao

Created: 2018-12-14 Fri 21:04

Validate