aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorGemini Lasswell2018-06-15 10:26:13 -0700
committerGemini Lasswell2018-08-03 08:53:02 -0700
commit8a7620955b4d859caecd9a5dc9f2a986baf994fd (patch)
treeb0749d1815b471e881579d6483cf0684089ff4a5 /lisp
parenteba16e5e5829c244d313101a769d4988946387d9 (diff)
downloademacs-8a7620955b4d859caecd9a5dc9f2a986baf994fd.tar.gz
emacs-8a7620955b4d859caecd9a5dc9f2a986baf994fd.zip
Add methods for strings to cl-print
* lisp/emacs-lisp/cl-print.el (cl-print-object) <string>: New method. (cl-print-object-contents) <string>: New method. (cl-print--find-sharing): Look in string property lists. * test/lisp/emacs-lisp/cl-print-tests.el (cl-print-tests-3): Test printing of long strings. (cl-print-tests-4): Test printing of strings nested in other objects. (cl-print-tests-strings, cl-print-tests-ellipsis-string): New tests.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/emacs-lisp/cl-print.el102
1 files changed, 100 insertions, 2 deletions
diff --git a/lisp/emacs-lisp/cl-print.el b/lisp/emacs-lisp/cl-print.el
index e638e58275a..337efa465a0 100644
--- a/lisp/emacs-lisp/cl-print.el
+++ b/lisp/emacs-lisp/cl-print.el
@@ -285,6 +285,95 @@ into a button whose action shows the function's disassembly.")
285 (princ " " stream) 285 (princ " " stream)
286 (cl-print-insert-ellipsis object limit stream)))) 286 (cl-print-insert-ellipsis object limit stream))))
287 287
288(cl-defmethod cl-print-object ((object string) stream)
289 (unless stream (setq stream standard-output))
290 (let* ((has-properties (or (text-properties-at 0 object)
291 (next-property-change 0 object)))
292 (len (length object))
293 (limit (if (natnump print-length) (min print-length len) len)))
294 (if (and has-properties
295 cl-print--depth
296 (natnump print-level)
297 (> cl-print--depth print-level))
298 (cl-print-insert-ellipsis object 0 stream)
299 ;; Print all or part of the string
300 (when has-properties
301 (princ "#(" stream))
302 (if (= limit len)
303 (prin1 (if has-properties (substring-no-properties object) object)
304 stream)
305 (let ((part (concat (substring-no-properties object 0 limit) "...")))
306 (prin1 part stream)
307 (when (bufferp stream)
308 (with-current-buffer stream
309 (cl-print-propertize-ellipsis object limit
310 (- (point) 4)
311 (- (point) 1) stream)))))
312 ;; Print the property list.
313 (when has-properties
314 (let* ((interval-limit (and (natnump print-length)
315 (max 1 (/ print-length 3))))
316 (interval-count 0)
317 (start-pos (if (text-properties-at 0 object)
318 0 (next-property-change 0 object)))
319 (end-pos (next-property-change start-pos object len)))
320 (while (and (or (null interval-limit)
321 (< interval-count interval-limit))
322 (< start-pos len))
323 (let ((props (text-properties-at start-pos object)))
324 (when props
325 (princ " " stream) (princ start-pos stream)
326 (princ " " stream) (princ end-pos stream)
327 (princ " " stream) (cl-print-object props stream)
328 (cl-incf interval-count))
329 (setq start-pos end-pos
330 end-pos (next-property-change start-pos object len))))
331 (when (< start-pos len)
332 (princ " " stream)
333 (cl-print-insert-ellipsis object (list start-pos) stream)))
334 (princ ")" stream)))))
335
336(cl-defmethod cl-print-object-contents ((object string) start stream)
337 ;; If START is an integer, it is an index into the string, and the
338 ;; ellipsis that needs to be expanded is part of the string. If
339 ;; START is a cons, its car is an index into the string, and the
340 ;; ellipsis that needs to be expanded is in the property list.
341 (let* ((len (length object)))
342 (if (atom start)
343 ;; Print part of the string.
344 (let* ((limit (if (natnump print-length)
345 (min (+ start print-length) len) len))
346 (substr (substring-no-properties object start limit))
347 (printed (prin1-to-string substr))
348 (trimmed (substring printed 1 (1- (length printed)))))
349 (princ trimmed)
350 (when (< limit len)
351 (cl-print-insert-ellipsis object limit stream)))
352
353 ;; Print part of the property list.
354 (let* ((first t)
355 (interval-limit (and (natnump print-length)
356 (max 1 (/ print-length 3))))
357 (interval-count 0)
358 (start-pos (car start))
359 (end-pos (next-property-change start-pos object len)))
360 (while (and (or (null interval-limit)
361 (< interval-count interval-limit))
362 (< start-pos len))
363 (let ((props (text-properties-at start-pos object)))
364 (when props
365 (if first
366 (setq first nil)
367 (princ " " stream))
368 (princ start-pos stream)
369 (princ " " stream) (princ end-pos stream)
370 (princ " " stream) (cl-print-object props stream)
371 (cl-incf interval-count))
372 (setq start-pos end-pos
373 end-pos (next-property-change start-pos object len))))
374 (when (< start-pos len)
375 (princ " " stream)
376 (cl-print-insert-ellipsis object (list start-pos) stream))))))
288 377
289;;; Circularity and sharing. 378;;; Circularity and sharing.
290 379
@@ -346,8 +435,17 @@ into a button whose action shows the function's disassembly.")
346 (push cdr stack) 435 (push cdr stack)
347 (push car stack)) 436 (push car stack))
348 ((pred stringp) 437 ((pred stringp)
349 ;; We presumably won't print its text-properties. 438 (let* ((len (length object))
350 nil) 439 (start (if (text-properties-at 0 object)
440 0 (next-property-change 0 object)))
441 (end (and start
442 (next-property-change start object len))))
443 (while (and start (< start len))
444 (let ((props (text-properties-at start object)))
445 (when props
446 (push props stack))
447 (setq start end
448 end (next-property-change start object len))))))
351 ((or (pred arrayp) (pred byte-code-function-p)) 449 ((or (pred arrayp) (pred byte-code-function-p))
352 ;; FIXME: Inefficient for char-tables! 450 ;; FIXME: Inefficient for char-tables!
353 (dotimes (i (length object)) 451 (dotimes (i (length object))