# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem      1.0
PortGroup       active_variants 1.1
PortGroup       legacysupport 1.1

# Need openat()
legacysupport.newest_darwin_requires_legacy 13

name            emacs
categories      editors
license         GPL-3+
maintainers     {dports @drkp} openmaintainer
description     The GNU Emacs text editor
long_description \
    GNU Emacs is a self-documenting, customizable, extensible real-time \
    display editor. Users new to Emacs will be able to use basic        \
    features fairly rapidly by studying the tutorial and using          \
    the self-documentation features. Emacs also has an extensive        \
    interactive manual browser. It is easily extensible since its       \
    editing commands are written in Lisp.

platforms       darwin freebsd
homepage        https://www.gnu.org/software/emacs/emacs.html
master_sites    gnu

set rpaths [list]

if {$subport eq $name} {
    conflicts   emacs-devel
}
subport emacs-devel {
    conflicts   $name
}
subport emacs-app {
    conflicts   emacs-app-devel
}
subport emacs-app-devel {
    conflicts   emacs-app
}

# FIXME: for a reason yet unknown emacs* currently fails to build on 10.6.8,
# but builds on 10a190. So we keep the override port simply to pin the version
# and control future updates. Packages are built on 10a190 and then moved to 10.6.8.

depends_build-append    port:autoconf \
                        port:automake \
                        port:libtool

# https://trac.macports.org/wiki/WimplicitFunctionDeclaration#strchr
configure.checks.implicit_function_declaration.whitelist-append strchr

# Avoid build failure if hdf4 is installed.
# https://trac.macports.org/ticket/68414
configure.cppflags-replace \
                        -I${prefix}/include -isystem${prefix}/include

configure.args          --disable-silent-rules \
                        --infodir ${prefix}/share/info/${name} \
                        --with-gnutls \
                        --with-libgmp \
                        --with-modules \
                        --with-native-compilation=no \
                        --with-sqlite3 \
                        --with-webp \
                        --with-xml2 \
                        --without-dbus \
                        --without-gconf \
                        --without-lcms2 \
                        --without-libotf \
                        --without-m17n-flt \
                        --without-ns \
                        --without-tree-sitter \
                        --without-x

# Later-on this could potentially be a variant, given enough demand. See:
# https://github.com/emacs-mirror/emacs/commit/4c6f45fa8eef1a15d5790c1f3d3e608b548015db#diff-731c852b7c27c6ee90c44c871e2988ecfc2d225bc720156d7a47c1d563c2683aR59
configure.args-append   --disable-gc-mark-trace

# Needed on 10.6.8 x86_64 to build for 32-bit arch, since autodetection is wrong then.
if {${configure.build_arch} in [list arm i386 ppc]} {
    configure.args-append \
                        --disable-year2038
}

platform darwin powerpc {
    configure.args-append \
                        --build=${os.arch}-apple-${os.platform}${os.major}
}

pre-configure {
    if {[llength $rpaths] > 0} {
        configure.ldflags-append "-Wl,-rpath " [join $rpaths " -Wl,-rpath "]
    }

    system -W ${worksrcpath} "sh ./autogen.sh"
}

depends_build-append    path:bin/pkg-config:pkgconfig \
                        port:texinfo
depends_lib-append      port:gmp \
                        path:lib/pkgconfig/gnutls.pc:gnutls \
                        port:libxml2 \
                        port:ncurses \
                        port:sqlite3 \
                        port:webp \
                        port:zlib

compiler.blacklist-append   *gcc-4.0 *gcc-4.2 {clang < 421}

post-destroot {
    xinstall -d ${destroot}${prefix}/share/emacs/${version}/leim
    delete ${destroot}${prefix}/bin/ctags
    delete ${destroot}${prefix}/share/man/man1/ctags.1.gz

    if {$subport eq $name || $subport eq "emacs-devel"} {
        # avoid conflicts with xemacs
        move ${destroot}${prefix}/bin/etags ${destroot}${prefix}/bin/etags-emacs
        move ${destroot}${prefix}/share/man/man1/etags.1.gz ${destroot}${prefix}/share/man/man1/etags-emacs.1.gz
    }
}

platform darwin {
    post-patch {
        # Leopard's Xcode 3.1.x ld(1) man page claims -no_pie is supported, but it's not
        if {${os.major} < 9} {
            reinplace "s:-fno-pie::" ${worksrcpath}/configure
        } elseif {${os.major} > 10} {
            configure.ldflags-append -Wl,-no_pie
        }
        # Without this flag, there are more than 90000 warnings if
        # the system's `clang` compiler is used, increasing the log
        # file size by a factor of 200, see:
        # https://debbugs.gnu.org/cgi/bugreport.cgi?bug=56736
        if {${os.major} >= 11 || ${os.major} <= 12} {
            configure.cflags-append -Wno-attributes
        }
    }
}

if {$subport eq $name || $subport eq "emacs-app"} {
    version         30.2
    revision        2
    checksums       rmd160  fefdd3fcd453d733114f609a3e122b2529cc7410 \
                    sha256  1d79a4ba4d6596f302a7146843fe59cf5caec798190bcc07c907e7ba244b076d \
                    size    83059014

    patchfiles-append \
                    patch-tree-sitter.diff
}

patchfiles-append   patch-allow-powerpc.diff

if {$subport eq "emacs-devel" || $subport eq "emacs-app-devel"} {
    PortGroup       github 1.0

    # do not forget to check that configure hasn't introduce some suprises via
    # git diff [old]..[new] -- '**/configure.ac'
    github.setup    emacs-mirror emacs c852297037cbf5fad63dc548530491e2d9fa3b48
    epoch           5
    version         20260319
    revision        0

    master_sites    ${github.master_sites}

    checksums       rmd160  f3ee6111ddddfd116412b8a8aed2a00706e6e083 \
                    sha256  0b392578bfc2049fe915faf5c0a80de4a90c99d6424063261bb343667c3df235 \
                    size    52986653
    github.tarball_from archive

    if {[variant_isset nativecomp]} {
        notes "emacs devel subports don't always keep compatibility for native\
            compiled files. Better to cleanup your ~/.emacs.d/.local/cache/eln"
    }

    livecheck.type none
} else {
    livecheck.type  regex
    livecheck.url   https://ftp.gnu.org/gnu/emacs/?C=M&O=D
    livecheck.regex ${name}-(\\d+\\.\\d+\\w*)\\.tar
}

if {$subport eq $name || $subport eq "emacs-devel"} {
    PortGroup  muniversal 1.0

    variant x11 description {Builds emacs as a X11 program with Lucid widgets} {
        depends_lib-append \
            port:giflib \
            path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
            port:lcms2 \
            path:include/turbojpeg.h:libjpeg-turbo \
            port:libpng \
            port:tiff \
            port:Xft2 \
            port:xorg-libXaw \
            port:xorg-libXmu \
            port:xpm
        configure.args-delete \
            --without-x
        configure.args-append \
            --with-x-toolkit=lucid \
            --without-xaw3d \
            --without-imagemagick \
            --with-xpm \
            --with-jpeg \
            --with-tiff \
            --with-gif \
            --with-png \
            --without-rsvg \
            --with-xft
        configure.args-replace \
            --without-lcms2 --with-lcms2

        # autoconf appears to be dropping linker flags for freetype &
        # fontconfig; work around this. See #28083
        configure.ldflags-append -lfreetype -lfontconfig
    }

    variant motif requires x11 description {Builds emacs as an X11 program with Motif widgets} {
        configure.args-delete   --with-x-toolkit=lucid
        configure.args-append   --with-x-toolkit=motif
        depends_lib-append      lib:libXm:motif
    }

    variant gtk requires x11 description {Builds emacs as an X11 program with GTK+3 widgets} {
        configure.args-delete   --with-x-toolkit=lucid
        configure.args-delete   --without-gconf
        configure.args-delete   --without-rsvg
        configure.args-append   --with-x-toolkit=gtk3
        configure.args-append   --with-gconf
        configure.args-append   --with-rsvg
        depends_lib-append      port:gconf \
                                path:lib/pkgconfig/glib-2.0.pc:glib2 \
                                path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                                path:lib/pkgconfig/librsvg-2.0.pc:librsvg
        # see #21917
        require_active_variants gtk2 x11
    }

    # imagemagick variant requires either x11 or quartz, so we have to
    # define it twice; see #53315
    variant imagemagick requires x11 description {Use ImageMagick} {
        depends_lib-append      port:ImageMagick
        configure.args-append   --with-imagemagick
    }

    variant dbus description {Builds emacs with D-Bus support} {
        # our dbus is autolaunched by launchd, so disable the check that it's running
        patchfiles-append       patch-src_dbusbind.c.diff
        configure.args-replace  --without-dbus --with-dbus
        depends_lib-append      port:dbus
    }

    variant xwidgets requires gtk description {Enable use of xwidgets in Emacs buffers} {
        configure.args-append   --with-xwidgets
    }

    post-destroot {
        xinstall -m 0755 -d ${destroot}${prefix}/include/emacs
        xinstall -m 0644 ${worksrcpath}/src/emacs-module.h \
            ${destroot}${prefix}/include/emacs
    }
}

if {$subport eq "emacs-app" || $subport eq "emacs-app-devel"} {
    categories-append   aqua

    depends_lib-append  port:lcms2

    configure.args-append \
        --without-harfbuzz \
        --without-imagemagick \
        --without-rsvg \
        --without-xaw3d
    configure.args-replace \
        --without-lcms2 --with-lcms2 \
        --without-ns --with-ns
    configure.args-delete \
        --without-x

    universal_variant   no

    patchfiles-append   patch-Info.plist.in.diff

    post-patch {
        reinplace "s,@PATH@,${prefix}/bin:${prefix}/sbin:/usr/bin:/bin:/usr/sbin:/sbin," ${worksrcpath}/nextstep/templates/Info.plist.in
    }

    destroot {
        system -W ${worksrcpath} "make install"
        xinstall -m 0755 -d ${destroot}${applications_dir}
        file copy ${worksrcpath}/nextstep/Emacs.app \
            ${destroot}${applications_dir}
        # fix read-permission to resources
        system "chmod a+r ${destroot}${applications_dir}/Emacs.app/Contents/Resources/*"
        file copy ${filespath}/site-start-app.el \
            ${destroot}${applications_dir}/Emacs.app/Contents/Resources/site-lisp/site-start.el
        reinplace "s|__PREFIX__|${prefix}|g" \
            ${destroot}${applications_dir}/Emacs.app/Contents/Resources/site-lisp/site-start.el
    }

    variant imagemagick description {Use ImageMagick} {
        depends_lib-append     port:ImageMagick
        configure.args-replace --without-imagemagick --with-imagemagick
    }

    variant rsvg description {Use librsvg} {
        depends_lib-append     path:lib/pkgconfig/librsvg-2.0.pc:librsvg
        configure.args-replace --without-rsvg --with-rsvg
    }

    variant xwidgets description {Enable use of xwidgets in Emacs buffers} {
        configure.args-append  --with-xwidgets
    }

    default_variants-append +rsvg
}

variant nativecomp description {Builds Emacs with native compilation support} {
    set gcc_v                      14
    depends_lib-append             port:gcc${gcc_v}
    configure.args-replace         --with-native-compilation=no \
                                   --with-native-compilation=aot
    compiler.cpath-prepend         ${prefix}/include/gcc${gcc_v}
    compiler.library_path-prepend  ${prefix}/lib/gcc${gcc_v}
    lappend rpaths                 ${prefix}/lib/gcc${gcc_v}
}

variant treesitter description {Builds Emacs with tree-sitter support} {
    depends_lib-append      port:tree-sitter
    configure.args-replace  --without-tree-sitter --with-tree-sitter

    if {$subport eq "emacs-app" || $subport eq "emacs-app-devel"} {
        lappend rpaths ${prefix}/lib
    }

#    depends_run-append \
        port:tree-sitter-bash \
        port:tree-sitter-c \
        port:tree-sitter-c-sharp \
        port:tree-sitter-cmake \
        port:tree-sitter-cpp \
        port:tree-sitter-css \
        port:tree-sitter-dockerfile \
        port:tree-sitter-elixir \
        port:tree-sitter-go \
        port:tree-sitter-go-mod \
        port:tree-sitter-heex \
        port:tree-sitter-html \
        port:tree-sitter-java \
        port:tree-sitter-javascript \
        port:tree-sitter-json \
        port:tree-sitter-lua \
        port:tree-sitter-php \
        port:tree-sitter-python \
        port:tree-sitter-ruby \
        port:tree-sitter-rust \
        port:tree-sitter-toml \
        port:tree-sitter-tsx \
        port:tree-sitter-typescript \
        port:tree-sitter-yaml

    if {$subport eq "emacs-devel" || $subport eq "emacs-app-devel"} {
#        depends_run-append \
            port:tree-sitter-go-work \
            port:tree-sitter-markdown
    }

    notes-append "
To install tree-sitter grammar libraries not required by built-in *-ts-modes,\
please use M-x treesit-install-language-grammar. For details, please refer to\
etc/NEWS or the Emacs Lisp reference manual.
"
}

default_variants-append     +treesitter

# JIT not supported in gcc for i386, so configure will fail.
# On ppc it is supported, but the build with +nativecomp segfaults.
# https://trac.macports.org/ticket/69677
if {${configure.build_arch} ni [list i386 ppc]} {
    default_variants-append +nativecomp
}
