Skip to content

Commit 92e60f8

Browse files
committed
Move special printing macros to PrettyPrinting
1 parent d3e2bc6 commit 92e60f8

File tree

2 files changed

+155
-146
lines changed

2 files changed

+155
-146
lines changed

src/AbstractAlgebra.jl

+6-146
Original file line numberDiff line numberDiff line change
@@ -355,152 +355,6 @@ export number_of_variables
355355
@alias nrows number_of_rows
356356
@alias nvars number_of_variables
357357

358-
###############################################################################
359-
# Macros for fancy printing. to use, enable attribute storage for your struct,
360-
# i.e.m change
361-
#
362-
# mutable struct bla..
363-
# ...
364-
# end
365-
#
366-
# to
367-
#
368-
# @attributes mutable struct bla ..
369-
# ...
370-
# end
371-
#
372-
# Then, in the `show` method, start with
373-
# @show_name(io, obj)
374-
# If the user assigned a name to the object (in the REPL mainly) by doing
375-
# A = bla...
376-
# then, in the compact printing only the name "A" is printed
377-
# also adding
378-
# @show_special(io, obj)
379-
# allows, if present to call a different printing function for this instance
380-
# See FreeModule for an example
381-
#
382-
###############################################################################
383-
384-
function set_name!(G::Any, name::String)
385-
set_attribute!(G, :name => name)
386-
end
387-
388-
function set_name!(G)
389-
s = get_attribute(G, :name)
390-
s === nothing || return
391-
sy = find_name(G)
392-
sy === nothing && return
393-
set_name!(G, string(sy))
394-
end
395-
396-
extra_name(G) = nothing
397-
398-
macro show_name(io, O)
399-
return :( begin
400-
local i = $(esc(io))
401-
local o = $(esc(O))
402-
s = get_attribute(o, :name)
403-
if s === nothing
404-
sy = find_name(o)
405-
if sy === nothing
406-
sy = extra_name(o)
407-
end
408-
if sy !== nothing
409-
s = string(sy)
410-
set_name!(o, s)
411-
end
412-
end
413-
if s !== nothing && (get(i, :supercompact, false) || get(i, :compact, false))
414-
if AbstractAlgebra.PrettyPrinting._supports_io_custom(i)
415-
print(i, LowercaseOff())
416-
end
417-
print(i, s)
418-
return
419-
end
420-
end )
421-
end
422-
423-
const CurrentModule = Ref(Main)
424-
425-
function set_current_module(m)
426-
CurrentModule[] = m
427-
end
428-
429-
function get_current_module()
430-
return CurrentModule[]
431-
end
432-
433-
function find_name(A, M = Main; all::Bool = false)
434-
# in Documenter, the examples are not run in the REPL.
435-
# in the REPL: A = ... adds A to the global name space (Main....)
436-
# in Documenter (doctests) all examples are run in their own module
437-
# which is stored in CurrentModule, hence we need to search there as well
438-
#furthermore, they are not exported, hence the "all" option
439-
if M === Main && AbstractAlgebra.get_current_module() != Main
440-
a = find_name(A, AbstractAlgebra.get_current_module(), all = true)
441-
if a !== nothing
442-
return a
443-
end
444-
end
445-
for a = names(M, all = all)
446-
a === :ans && continue
447-
if isdefined(M, a) && getfield(M, a) === A
448-
return a
449-
end
450-
end
451-
end
452-
453-
macro show_special(io, O)
454-
return :( begin
455-
local i = $(esc(io))
456-
local o = $(esc(O))
457-
s = get_attribute(o, :show)
458-
if s !== nothing
459-
s(i, o)
460-
return
461-
end
462-
end )
463-
end
464-
465-
macro show_special(io, mime, O)
466-
return :( begin
467-
local i = $(esc(io))
468-
local m = $(esc(mime))
469-
local o = $(esc(O))
470-
s = get_attribute(o, :show)
471-
if s !== nothing
472-
s(i, m, o)
473-
return
474-
end
475-
end )
476-
end
477-
478-
macro show_special_elem(io, e)
479-
return :( begin
480-
local i = $(esc(io))
481-
local a = $(esc(e))
482-
local o = parent(a)
483-
s = get_attribute(o, :show_elem)
484-
if s !== nothing
485-
s(i, a)
486-
return
487-
end
488-
end )
489-
end
490-
491-
macro show_special_elem(io, mime, e)
492-
return :( begin
493-
local i = $(esc(io))
494-
local m = $(esc(mime))
495-
local a = $(esc(e))
496-
local o = parent(a)
497-
s = get_attribute(o, :show_elem)
498-
if s !== nothing
499-
s(i, m, a)
500-
return
501-
end
502-
end )
503-
end
504358

505359
###############################################################################
506360
# generic fall back if no immediate coercion is possible
@@ -607,11 +461,16 @@ include("fundamental_interface.jl")
607461
include("PrettyPrinting.jl")
608462

609463
import .PrettyPrinting: @enable_all_show_via_expressify
464+
import .PrettyPrinting: @show_name
465+
import .PrettyPrinting: @show_special
466+
import .PrettyPrinting: @show_special_elem
610467
import .PrettyPrinting: allow_unicode
611468
import .PrettyPrinting: canonicalize
612469
import .PrettyPrinting: expr_to_latex_string
613470
import .PrettyPrinting: expr_to_string
614471
import .PrettyPrinting: expressify
472+
import .PrettyPrinting: extra_name
473+
import .PrettyPrinting: get_current_module
615474
import .PrettyPrinting: get_html_as_latex
616475
import .PrettyPrinting: get_syntactic_sign_abs
617476
import .PrettyPrinting: is_syntactic_one
@@ -623,6 +482,7 @@ import .PrettyPrinting: obj_to_string_wrt_times
623482
import .PrettyPrinting: print_integer_string
624483
import .PrettyPrinting: print_obj
625484
import .PrettyPrinting: printer
485+
import .PrettyPrinting: set_current_module
626486
import .PrettyPrinting: set_html_as_latex
627487
import .PrettyPrinting: show_obj
628488
import .PrettyPrinting: show_via_expressify

src/PrettyPrinting.jl

+149
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,155 @@ function print_obj(S::printer, mi::MIME, obj, left::Int, right::Int)
13801380
push(S, "[??? unknown object ???]")
13811381
end
13821382

1383+
1384+
###############################################################################
1385+
# Macros for fancy printing. to use, enable attribute storage for your struct,
1386+
# i.e.m change
1387+
#
1388+
# mutable struct bla..
1389+
# ...
1390+
# end
1391+
#
1392+
# to
1393+
#
1394+
# @attributes mutable struct bla ..
1395+
# ...
1396+
# end
1397+
#
1398+
# Then, in the `show` method, start with
1399+
# @show_name(io, obj)
1400+
# If the user assigned a name to the object (in the REPL mainly) by doing
1401+
# A = bla...
1402+
# then, in the compact printing only the name "A" is printed
1403+
# also adding
1404+
# @show_special(io, obj)
1405+
# allows, if present to call a different printing function for this instance
1406+
# See FreeModule for an example
1407+
#
1408+
###############################################################################
1409+
1410+
function set_name!(G::Any, name::String)
1411+
set_attribute!(G, :name => name)
1412+
end
1413+
1414+
function set_name!(G)
1415+
s = get_attribute(G, :name)
1416+
s === nothing || return
1417+
sy = find_name(G)
1418+
sy === nothing && return
1419+
set_name!(G, string(sy))
1420+
end
1421+
1422+
extra_name(G) = nothing
1423+
1424+
macro show_name(io, O)
1425+
return :( begin
1426+
local i = $(esc(io))
1427+
local o = $(esc(O))
1428+
s = get_attribute(o, :name)
1429+
if s === nothing
1430+
sy = find_name(o)
1431+
if sy === nothing
1432+
sy = extra_name(o)
1433+
end
1434+
if sy !== nothing
1435+
s = string(sy)
1436+
set_name!(o, s)
1437+
end
1438+
end
1439+
if s !== nothing && (get(i, :supercompact, false) || get(i, :compact, false))
1440+
if AbstractAlgebra.PrettyPrinting._supports_io_custom(i)
1441+
print(i, LowercaseOff())
1442+
end
1443+
print(i, s)
1444+
return
1445+
end
1446+
end )
1447+
end
1448+
1449+
const CurrentModule = Ref(Main)
1450+
1451+
function set_current_module(m)
1452+
CurrentModule[] = m
1453+
end
1454+
1455+
function get_current_module()
1456+
return CurrentModule[]
1457+
end
1458+
1459+
function find_name(A, M = Main; all::Bool = false)
1460+
# in Documenter, the examples are not run in the REPL.
1461+
# in the REPL: A = ... adds A to the global name space (Main....)
1462+
# in Documenter (doctests) all examples are run in their own module
1463+
# which is stored in CurrentModule, hence we need to search there as well
1464+
#furthermore, they are not exported, hence the "all" option
1465+
if M === Main && AbstractAlgebra.get_current_module() != Main
1466+
a = find_name(A, AbstractAlgebra.get_current_module(), all = true)
1467+
if a !== nothing
1468+
return a
1469+
end
1470+
end
1471+
for a = names(M, all = all)
1472+
a === :ans && continue
1473+
if isdefined(M, a) && getfield(M, a) === A
1474+
return a
1475+
end
1476+
end
1477+
end
1478+
1479+
macro show_special(io, O)
1480+
return :( begin
1481+
local i = $(esc(io))
1482+
local o = $(esc(O))
1483+
s = get_attribute(o, :show)
1484+
if s !== nothing
1485+
s(i, o)
1486+
return
1487+
end
1488+
end )
1489+
end
1490+
1491+
macro show_special(io, mime, O)
1492+
return :( begin
1493+
local i = $(esc(io))
1494+
local m = $(esc(mime))
1495+
local o = $(esc(O))
1496+
s = get_attribute(o, :show)
1497+
if s !== nothing
1498+
s(i, m, o)
1499+
return
1500+
end
1501+
end )
1502+
end
1503+
1504+
macro show_special_elem(io, e)
1505+
return :( begin
1506+
local i = $(esc(io))
1507+
local a = $(esc(e))
1508+
local o = parent(a)
1509+
s = get_attribute(o, :show_elem)
1510+
if s !== nothing
1511+
s(i, a)
1512+
return
1513+
end
1514+
end )
1515+
end
1516+
1517+
macro show_special_elem(io, mime, e)
1518+
return :( begin
1519+
local i = $(esc(io))
1520+
local m = $(esc(mime))
1521+
local a = $(esc(e))
1522+
local o = parent(a)
1523+
s = get_attribute(o, :show_elem)
1524+
if s !== nothing
1525+
s(i, m, a)
1526+
return
1527+
end
1528+
end )
1529+
end
1530+
1531+
13831532
################################################################################
13841533
#
13851534
# Unicode printing

0 commit comments

Comments
 (0)