# -*- 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

# Need openat()
PortGroup       legacysupport 1.1

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
}

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

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

# 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

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

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

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

compiler.blacklist-append   *gcc-4.0 *gcc-4.2

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.1
    revision        1
    checksums       rmd160  d84d527bb0db3d56d558e8f4bed80af5e3eddef3 \
                    sha256  54404782ea5de37e8fcc4391fa9d4a41359a4ba9689b541f6bc97dd1ac283f6c \
                    size    82678582
}

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 ae8522af41bb67bf417b94ed54146a43fe2150ec
    github.tarball_from archive
    epoch           5
    version         20250223
    revision        1

    master_sites    ${github.master_sites}

    checksums       rmd160  a27dee7593339d1d16421f9ed8e60e1adc30aefd \
                    sha256  8614111d9478bf3293ba0cf66bf5494a46b49fc6cc403fb0d445a6a69f5d620e \
                    size    51933907

    livecheck.type none

    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"
    }
} 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} {
        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 \
            --with-lcms2 \
            --without-rsvg \
            --with-xft
        depends_lib-append  port:xorg-libXmu \
            port:xorg-libXaw \
            port:xpm \
            path:include/turbojpeg.h:libjpeg-turbo \
            port:tiff \
            port:giflib \
            port:libpng \
            port:lcms2 \
            port:Xft2 \
            path:lib/pkgconfig/harfbuzz.pc:harfbuzz

        # 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:openmotif
    }

    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      path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                                path:lib/pkgconfig/glib-2.0.pc:glib2 \
                                port:gconf \
                                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-delete    --without-dbus
        configure.args-append    --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

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

    configure.args-delete  --without-ns  --without-x

    depends_lib-append  port:lcms2

    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-delete  --without-imagemagick
        configure.args-append  --with-imagemagick
    }

    variant rsvg description {Use librsvg} {
        depends_lib-append     path:lib/pkgconfig/librsvg-2.0.pc:librsvg
        configure.args-delete  --without-rsvg
        configure.args-append  --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} {
    configure.args-delete   --without-tree-sitter
    configure.args-append   --with-tree-sitter
    depends_lib-append      port:tree-sitter

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

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

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

    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
}
