private int getNumInstructionsInSelection(Program program, ProgramSelection selection) {

		// First get the addresses in the selection;
		AddressRangeIterator addressRanges = selection.getAddressRanges();

		// Keep track of the number of instructions we find.  
		int numInstructions = 0;

		// Loop over all the addresses, getting all code units and checking to see which ones
		// are instructions.  For those that are, increment our counter.
		while (addressRanges.hasNext()) {
			AddressRange range = addressRanges.next();
			AddressSet addrSet = new AddressSet(range);
			CodeUnitIterator cuIter = program.getListing().getCodeUnits(addrSet, true);

			while (cuIter.hasNext()) {
				CodeUnit cu = cuIter.next();
				if ((cu instanceof Instruction) || (cu instanceof Data)) {
					numInstructions++;
				}
			}
		}

		return numInstructions;
	}
Returns the number of instructions (and data) in the selection.

@param program the current program
@param selection the program selection
@return number of instructions in the selection
Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/instructionsearch/InstructionSearchPlugin.java