@Override
	public boolean applyTo(Program program, TaskMonitor monitor) {

		Listing listing = program.getListing();
		AddressSet addressSet = new AddressSet(selection);
		Address address = addressSet.getMinAddress();
		int functionsCreated = 0;
		long numInitialAddresses = addressSet.getNumAddresses();
		monitor.initialize(numInitialAddresses);
		monitor.setMessage("Creating functions from selection.");

		while (address != null && !monitor.isCancelled()) {
			// Try to get a function containing the current address.
			Function function = listing.getFunctionContaining(address);
			if (function == null) {
				// Create the next function.
				CreateFunctionCmd createFunctionCmd =
					new CreateFunctionCmd(null, address, null, source);
				if (createFunctionCmd.applyTo(program, monitor)) {
					function = createFunctionCmd.getFunction();
					functionsCreated++;
				}
			}

			if (function != null) {
				AddressSetView body = function.getBody();
				addressSet.delete(body);
			}
			addressSet.deleteRange(address, address);
			address = addressSet.getMinAddress();
			long numRemainingAddresses = addressSet.getNumAddresses();
			monitor.setProgress(numInitialAddresses - numRemainingAddresses);
		}
		if (monitor.isCancelled()) {
			Msg.info(this, "User canceled " + getName() + ".");
			return false;
		}
		Msg.info(this, functionsCreated + " functions created by " + getName() + ".");
		return true;
	}
Command for Creating multiple functions from a selection.
This tries to create functions by working from the minimum address to the maximum address in
the selection. Any addresses in the selection that are already in existing functions are
discarded. Every time a function is created, all the other addresses for that function are
also discarded.
Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateMultipleFunctionsCmd.java