  We generalize and unify the $f(R,T)$ and $f(R,L_m)$ type gravity models by
assuming that the gravitational Lagrangian is given by an arbitrary function of
the Ricci scalar $R$, of the trace of the energy-momentum tensor $T$, and of
the matter Lagrangian $L_m$, so that $L_{grav}=f(R,L_m,T)$. We obtain the
gravitational field equations in the metric formalism, the equations of motion
for test particles, and the energy and momentum balance equations, which follow
from the covariant divergence of the energy-momentum tensor. Generally, the
motion is non-geodesic, and takes place in the presence of an extra force
orthogonal to the four-velocity. The Newtonian limit of the equations of motion
is also investigated, and the expression of the extra acceleration is obtained
for small velocities and weak gravitational fields. The generalized Poisson
equation is also obtained in the Newtonian limit, and the Dolgov-Kawasaki
instability is also investigated. The cosmological implications of the theory
are investigated for a homogeneous, isotropic and flat Universe for two
particular choices of the Lagrangian density $f(R,L_m,T)$ of the gravitational
field, with a multiplicative and additive algebraic structure in the matter
couplings, respectively, and for two choices of the matter Lagrangian, by using
both analytical and numerical methods.
