Copying source code from posts

If a post contains source code that is too long (vertically) to be visible without being scrolled, I can’t select (and copy) the entire source code.

Perhaps I’m missing something obvious?

(Ie, for example, how do I select and copy the following 102 lines of code:

import AppKit

extension String {
    enum PaddingMethod { case left, right }
    func padded(_ m: PaddingMethod, to w: Int, with c: Character) -> String {
        let n = w - count
        guard n > 0 else { return self }
        switch m {
        case .left: return String(repeating: c, count: n) + self
        case .right: return self + String(repeating: c, count: n)

extension FixedWidthInteger where Self: UnsignedInteger {
    var binaryString : String {
        return String(self, radix: 2)
            .padded(.left, to: MemoryLayout<Self>.stride * 8, with: "0")
    var hexString : String {
        return String(self, radix: 16)
            .padded(.left, to: MemoryLayout<Self>.stride * 2, with: "0")

extension BinaryFloatingPoint where
    RawExponent: FixedWidthInteger,
    RawSignificand: FixedWidthInteger
    var binaryStringInSections : String {
        let signBit: String
        switch self.sign {
        case .plus: signBit = "0"
        case .minus: signBit = "1"
        let exponentBits = String(exponentBitPattern, radix: 2)
            .padded(.left, to: Self.exponentBitCount, with: "0")
        let significandBits = String(significandBitPattern, radix: 2)
            .padded(.left, to: Self.significandBitCount, with: "0")
        return signBit + "_" + exponentBits + "_" + significandBits

extension Array {
    mutating func unsafeSetElementsToRandomBitPatterns() {
        let byteCount = count * MemoryLayout<Element>.stride
        let r = SecRandomCopyBytes(nil, byteCount, &self)
        precondition(r == errSecSuccess)

extension NSBitmapImageRep {
    struct Pixel { var a, r, g, b: UInt8 }
    convenience init(width: Int, height: Int) {
        self.init(bitmapDataPlanes: nil, pixelsWide: width, pixelsHigh: height,
                  bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true,
                  isPlanar: false, colorSpaceName: .deviceRGB,
                  bitmapFormat: .alphaFirst, bytesPerRow: 0, bitsPerPixel: 0)!
    func withEachPixel(_ op: (_ x: Int, _ y: Int, _ px: inout Pixel) -> ()) {
        let data = UnsafeMutableRawPointer(self.bitmapData!)
        for y in 0 ..< pixelsHigh {
            for x in 0 ..< pixelsWide {
                let offset = x &* samplesPerPixel &+ y &* bytesPerRow
                var px = data.load(fromByteOffset: offset, as: Pixel.self)
                op(x, y, &px)
                data.storeBytes(of: px, toByteOffset: offset, as: Pixel.self)
    func withCurrentGraphicsContext(_ draw: (_ bounds: NSRect) -> ()) {
        let ctx = NSGraphicsContext.init(bitmapImageRep: self)
        NSGraphicsContext.current = ctx
        draw(NSRect(origin: .zero, size:
            NSSize(width: pixelsWide, height: pixelsHigh)))
    func save(url: URL, fileType: FileType, properties: [PropertyKey : Any]) {
        let data = representation(using: fileType, properties: properties)!
        try! data.write(to: url, options: .atomic)

extension Collection where Element == (NSBitmapImageRep, Double) {
    func saveAsAnimatedGIF(url: URL) {
        let fileProperties = [kCGImagePropertyGIFDictionary as String:
            [kCGImagePropertyGIFLoopCount as String: 0]] as CFDictionary?
        guard let destination = CGImageDestinationCreateWithURL(
            url as CFURL, kUTTypeGIF, count, nil)
            else { preconditionFailure("Can't save animated GIF as \(url)") }
        CGImageDestinationSetProperties(destination, fileProperties)
        for (bitmap, delay) in self {
            let gifProps = [
                kCGImagePropertyGIFDictionary as String:
                    [kCGImagePropertyGIFDelayTime as String: delay]
                ] as CFDictionary?
            CGImageDestinationAddImage(destination, bitmap.cgImage!, gifProps)


Works fine on my computer? If you move the mouse cursor to the edge of the code scroll view, it scrolls down and allows further selection for me.

Huh, now when I tried to just select everything, it scrolled and selected as expected! Hmm, I was really trying to do the same on another post earlier and I didn’t manage to select only the source code (got other UI in the selection while scrolling no matter how I tried.).

And I went back to the place where I tried before and it just worked … Hmm.
Sorry for the noise.

You can view the raw text of a post (by manually editing the URL):

This might help to avoid scrolling, if the source code is wider than 80 columns.


One theme component that may be interesting for this use case is:

It smoothes the experience a bit, but will only work on browsers that support the clipboard apis.